Reputation:
I'm a little lost right now trying to merge my own model layers using Keras Functional API and layers from a VGG16 model into one new model. I need to add new layers after block3_pool with my custom ones. There is 8 classes that will be used (Fashion Mnist). All images are scaled up from 28x28 to 32x32. This is how far I've come:
# VGG16 Model
vgg_model = VGG16(include_top=False, weights='imagenet', input_shape=(32, 32, 3), classes=8)
vgg_model.save_weights('vgg.model')
# vgg_model.summary()
for layer in model.layers:
layer.trainable = False
inputs = Input(shape=(4, 4,))
x = tf.keras.layers.Dense(64, activation=tf.nn.relu)(inputs)
outputs = tf.keras.layers.Dense(8, activation=tf.nn.softmax)(x)
new_model = tf.keras.Model(inputs=inputs, outputs=outputs, name='new_model')
new_model.load_weights('vgg.model')
block3_pool = vgg_model.get_layer('block3_pool').output
# Now combine the two models
full_output = new_model(block3_pool)
full_model = Model(inputs=vgg_model.input, outputs=full_output)
full_model.summary()
When trying to run this code I get the following error in Google Colab:
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/tensor_shape.py in assert_is_compatible_with(self, other)
1132 """
1133 if not self.is_compatible_with(other):
-> 1134 raise ValueError("Shapes %s and %s are incompatible" % (self, other))
1135
1136 def most_specific_compatible_shape(self, other):
ValueError: Shapes (4, 64) and (3, 3, 3, 64) are incompatible
I do not seem to understand completely why that is and what I have to do to make it work.
Upvotes: 0
Views: 544
Reputation: 520
You have given 2 different shapes.
input_shape
parameter in VGG16
and shape
parameter in Input
must have same shape
inputs = Input(shape=(32, 32, 3))
replace this in your code
Upvotes: 1