1
votes

I'm fine-tuning a VGG19 model for the MNIST task. The images in MNIST is (28,28,1),which is one channel. But the VGG wants to input to be (?,?,3), which is three channels.

So,my way is to add one more Conv2D layer before all the VGG layers to change the (28,28,1) data to (28,28,3), here's my code:

inputs = Input(shape=(28,28,1))
x = Conv2D(3,kernel_size=(1,1),activation='relu')(inputs)
print(x.shape)
# out: (?, 28, 28, 3)

Now my input shape is correct(I think).

Here's my whole model: # change the input shape: inputs = Input(shape=(28,28,1)) x = Conv2D(3,kernel_size=(1,1),activation='relu')(inputs)

# add POOL and FC layers:
x = base_model(x)
x = GlobalMaxPooling2D()(x)
x = Dense(1024,activation='relu')(x)
predictions = Dense(10,activation='softmax')(x)

model = Model(inputs=inputs, outputs=predictions)

# freeze the base_model:
for layer in base_model.layers:
    layer.trainable = False

model.compile(optimizer='adam',loss='categorical_crossentropy',metric=['accuracy'])

and I got:

InvalidArgumentError: Negative dimension size caused by subtracting 2 from 1 for 'vgg19_10/block5_pool/MaxPool' (op: 'MaxPool') with input shapes: [?,1,1,512].

I had searched the problem and one solution is to add

from keras import backend as K
K.set_image_dim_ordering('th')

but it didn't work for me.

What's wrong with my code?

1

1 Answers

1
votes

When we are doing classification we should use softmax activation on output.

Change the activation of your last layer to softmax

From

predictions = Dense(10,activation='relu')(x)

To

predictions = Dense(10,activation='softmax')(x)

You second mistake which is causing the error is about your input size. According to keras vgg19 your minimum image size should be no less than 48.

inputs = Input(shape=(48,48,1))