'Using Pre-trained VGG16 for 128X128X1 images

I have a grayscale dataset of image dimensions (128,128,1). I am trying to use the VGGG16 pre-trained model for classification. VGG16 input is (224,224,3). I am trying to add a 2Dconv layer before VGG16 layer (128,128,3). But I am unable to do that. I want to do the fine-tuning of weights during the training phase. The code is given below. Would appreciate the help. Thanks.

vgg_base = keras.applications.VGG16(weights="imagenet", include_top=True,
    input_tensor=Input(shape=(128, 128,3)))
vgg_base.trainable = False
input_tensor = Input(shape=(128,128,1) )
x = keras.layers.Conv2D(3,(3,3),padding='same')(input_tensor)
print(x.shape)
x = vgg_base(x)
x = keras.layers.GlobalAveragePooling2D()(x)
outputs = keras.layers.Dense(23)(x)
model = keras.Model(input_tensor, outputs)

The error that I am facing is given by:

ValueError                                Traceback (most recent call last)
<ipython-input-12-a7b1b7273d95> in <module>()
      1 
      2 vgg_base = keras.applications.VGG16(weights="imagenet", include_top=True,
----> 3     input_tensor=Input(shape=(128, 128,3)))
      4 vgg_base.trainable = False
      5 input_tensor = Input(shape=(128,128,1) )

2 frames
/usr/local/lib/python3.7/dist-packages/keras/backend.py in batch_set_value(tuples)
   4017   if tf.compat.v1.executing_eagerly_outside_functions():
   4018     for x, value in tuples:
-> 4019       x.assign(np.asarray(value, dtype=dtype_numpy(x)))
   4020   else:
   4021     with get_graph().as_default():

ValueError: Cannot assign value to variable ' fc1/kernel:0': Shape mismatch.The variable shape (8192, 4096), and the assigned value shape (25088, 4096) are incompatible.


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source