我正在尝试将Keras chexNet重量文件加载到Densenet121,https://www.kaggle.com/theewok/chexnet-keras-weights
我得到了ValueError:您正试图将包含242层的权重文件加载到包含241层的模型中。如果我打电话给densenet121
densenet = tf.keras.applications.DenseNet121(
include_top=False,
weights="CheXNet_Keras_0.3.0_weights.h5",
input_shape=(224,224,3)
)
如果我尝试:-
densenet = tf.keras.applications.DenseNet121(
include_top=True,
weights="CheXNet_Keras_0.3.0_weights.h5",
input_shape=(224,224,3)
)
我会得到ValueError:形状(1024,1000(和(1024,14(不兼容
弹出最后一层的答案现在不再有效,弹出只返回最后一层,但模型保持不变。
我推荐这样的东西:
densenet = DenseNet121(weights=None, include_top=False,
input_shape=(224, 224, 3), pooling="avg")
output = tf.keras.layers.Dense(14, activation='sigmoid', name='output')(densenet.layers[-1].output)
model = tf.keras.Model(inputs=[densenet.input], outputs=[output])
model.load_weights("./CheXNet_weights.h5")
他们在没有正确输出层的情况下保存了模型,修复方法如下:
base_model = densenet.DenseNet121(weights=None,
include_top=False,
input_shape=(224,224,3), pooling="avg")
predictions = tf.keras.layers.Dense(14, activation='sigmoid', name='predictions')(base_model.output)
base_model = tf.keras.Model(inputs=base_model.input, outputs=predictions)
base_model.load_weights("./temp/CheXNet_Keras_0.3.0_weights.h5")
base_model.layers.pop()
print("CheXNet loaded")