keras-team / keras-applications

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

Loading pretrained weights fails when `input_tensor` is non-trivial #120

Closed mahehu closed 4 years ago

mahehu commented 5 years ago

Documentation gives this example for initializing the pretrained network with custom input_tensor:

input_tensor = Input(shape=(224, 224, 3)) 
model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)

However, adding another layer at the input fails:

input_tensor = Input(shape = (224,224,3))
input_tensor = Conv2D(3, kernel_size = (1,1))(input_tensor) # new layer
model = InceptionV3(input_tensor=input_tensor, weights='imagenet', include_top=True)

This results in the following error:

ValueError: You are trying to load a weight file containing 189 layers into a model with 190 layers.

With weights=None or without the Conv2D layer this works fine.

I'm using keras version 2.2.4 and keras-applications version 1.0.8.

taehoonlee commented 5 years ago

@mahehu, I hope the following workaround will help you.

from keras.models import Model
from keras.layers import Input, Conv2D

newinput = Input(shape=(224,224,3))
conv = Conv2D(3, kernel_size=1)(newinput)
newmodel = Model(inputs=[newinput], outputs=[model(conv)])
taehoonlee commented 4 years ago

@mahehu, I'll close the issue for now. Please feel free to open it again at any time if you have additional comments.

mahehu commented 4 years ago

Thanks, this solved the issue. Sorry for not remembering to comment on that.

Thanks for your great work!