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

Created on 3 Feb 2017  路  15Comments  路  Source: keras-team/keras

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?

stale

Most helpful comment

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

All 15 comments

I usually use the pre-trained VGG16 like this:

input = Input(...)
vgg16 = VGG16(weights="imagenet", include_top=False)
x = vgg16(input)
x = Flatten()(x)
...

The input must have 3 channels.

x = Flatten(input_shape=base_model.output_shape[1:])(x)

In the functional API there is no need to specify the input_shape parameter.

Thanks. Will try with the Input() - maybe that will stop me having to enter the input shape which I found works.
Be good to see a real, full example but can't find any. The examples from Keras are too cut down and simplified. ;-)

Hi! Any update on this? I'm getting the same error, and I tried the solution from @AdrianNunez, but no luck.

Hi @tanmayb123, could you share your network with us so that we can spot the source of the error?

Thanks! I was able to fix the error. I'll share my code here in a day :)

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

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

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

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

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 30 days if no further activity occurs, but feel free to re-open a closed issue if needed.

Can we not use the flatter layer with variable-length input? I have an input of shape (batch_size, None, num_features), where the 'None' dimension is the temporal sequential data. I would like to not have to specify it. But I am unable to apply Flatten() to the output from my CNN

@aalok-sathe I tried setting the target size of the training/validation generator to the same as input size and everything worked for me for variable size images.

@shuuchen 's fix works. If it does not, make sure your keras.json is on par with backends and you do not call set_image_dims or change backends in your code.

I usually use the pre-trained VGG16 like this:
input = Input(...)
vgg16 = VGG16(weights="imagenet", include_top=False)
x = vgg16(input)
x = Flatten()(x)
...

The input must have 3 channels.
x = Flatten(input_shape=base_model.output_shape[1:])(x)
In the functional API there is no need to specify the input_shape parameter.

@AdrianNunez What exactly happens when you leave input_shape blank. I saw the code internally interprets it as (None,None,3), but what does this mean?

My output of base model is of 4 dim with 1st dim as one Can anyone help me?

Was this page helpful?
0 / 5 - 0 ratings