keras-team / keras-applications

Reference implementations of popular deep learning models.
Other
2k stars 907 forks source link

Unable to use pre-trained weights when additional layers added #17

Closed gan3sh500 closed 6 years ago

gan3sh500 commented 6 years ago

I am trying to replace Pooling layers in Densenet with a custom pooling layer I implemented. I am able to use the model without loading imagenet weights but when I try with by_name=False which is default, it raises an error due to difference in number of layers. I tried changing by_name=True which stops the error but does not actually load the weights(I assume it does not load since the predictions it makes on a given image is different on each load and performance on training is worse compared to pre-trained).

Is there some way to use the pre-trained weights along with additional layers?

taehoonlee commented 6 years ago

@gan3sh500, The answer is using by_name=True but the reason why you got the poor performance is that the current weight names in the pre-trained files don't match the names described in the model definition.

The names will be corrected, and you can use the following trick until the updates.

_model = DenseNet121(weights='imagenet')
values = _model.get_weights()

model = yourDenseNet()
model.set_weights(values[:-2])
gan3sh500 commented 6 years ago

I found another way too. I did model.save_weights() then reloaded that with by_name=True.

taehoonlee commented 6 years ago

@gan3sh500, Now you can load correctly with by_name=True if you use the latest Keras. Please see the commit.