keras: Unable to fine tune Keras vgg16 model - input shape issue

I’m trying to fine-tune the VGG16 model which is provided in keras.applications.vgg16 but getting errors on the input shapes to my additions.

I load the vgg16 model like so: base_model = VGG16(weights="imagenet", include_top=False)

Now want to add to this model with my own Dense layers like so:

x = base_model.output
#x = Flatten(input_shape=(7, 7, 512))(x)
x = Flatten(input_shape=base_model.output_shape[1:])(x)
x = Dense(100, activation="relu")(x)
x = Dropout(0.2)(x)
x = Dense(50, activation="relu")(x)
x = Dropout(0.2)(x)
x = Dense(10, activation="relu")(x)
x = Dense(1, activation="linear")(x)

model = Model(input=base_model.input, output=x)

So far the above is just copying the samples on Keras.io.

However I get the following error: ValueError: The shape of the input to "Flatten" is not fully defined (got (None, None, 512). Make sure to pass a complete "input_shape" or "batch_input_shape" argument to the first layer in your model.

Reading through some of the issues here it was suggested that the shape should be (7, 7, 512) so tried that but get exactly the same error.

Any ideas?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15

Most upvoted comments

Thanks! I was able to fix the error. I’ll share my code here in a day 😃

What worked for me was defining the input shape when I called the model: (I’m using VGG19, but had the same problem)

vgg19_base = VGG19(weights='imagenet', include_top=False, input_shape = (3,224,224))

Just specify the input_shape parameter

for tensorflow backend: vgg19_base = VGG19(weights=‘imagenet’, include_top=False, input_shape = (224,224,3))

for theano backend: vgg19_base = VGG19(weights=‘imagenet’, include_top=False, input_shape = (3,224,224))

Try vgg19_base = VGG19(weights='imagenet', include_top=False, input_shape = (width, height, 3) ), where width, height are the images width and height, respectively.

Hi @tanmayb123 can you share the code? I’m facing the same issue. Thanks!