CheXNet-Keras icon indicating copy to clipboard operation
CheXNet-Keras copied to clipboard

can't load pretrained model CheXNet_Keras_0.3.0_weights.h5 into Keras Densenet121

Open WrathofBhuvan11 opened this issue 4 years ago • 1 comments

Here I have asked the question https://stackoverflow.com/questions/64390544/unable-to-load-chexnet-pre-trained-weight-file-to-densenet121 densenet = tf.keras.applications.DenseNet121( include_top=False, weights="CheXNet_Keras_0.3.0_weights.h5", input_shape=(224,224,3) ) ValueError: You are trying to load a weight file containing 242 layers into a model with 241 layers. if I Call densenet121 Please help...

WrathofBhuvan11 avatar Oct 16 '20 18:10 WrathofBhuvan11

They saved the model without the correct output layer, here's the fix:

        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")

ddofer avatar Dec 21 '20 12:12 ddofer