2

I am trying to copy vgg16 layers (except the last layer) into a new Sequential model. I loop through every layer of the vgg16 except the last one and add them to my new model. But when I print the summary of the model I see that the input layer of vgg16 has not been copied. I am following this tutorial https://www.youtube.com/watch?v=oDHpqu52soI in which the same code that I have is used but the input layer is copied too. I am wondering if someone could please help me with why my code does not copy the input layer.

I am using keras 2.2.4-tf

Here is my code:

vgg16_model = keras.applications.vgg16.VGG16(weights='imagenet')
model = Sequential()
for layer in vgg16_model.layers[:-1]:
   model.add(layer)
model.summary()

These are the first few layers of the "model" (input layer is missing):

enter image description here These are the first few layers of "vgg16_model" which has the input layer too.

enter image description here

Rane
  • 41
  • 1
  • 5
  • You can just add your own `Input` layer. It does not have any learned parameters, so does not matter if you add your own. – Anakin Apr 11 '19 at 09:20
  • You can check by printing the `vgg16_model` layer names to see if your iteration is missing the first one. – Anakin Apr 11 '19 at 09:21
  • Thanks for your comments @Anakin. I had already printed the layers names. It showed all the names of the VGG including the Input. To me the strange thing was that my code loops through the InputLayer but it does not copy it to my new model. So, I did further research and noticed that you cannot add input layer to Sequential models. For these models the input shape is set in the first layer on the network something like: model = Sequential([ Dense(32, input_shape=(784,)),....]) taken from https://keras.io/getting-started/sequential-model-guide/. – Rane Apr 12 '19 at 14:48
  • Hi @Rane I know the question is a little old, but I recently switched from keras to tf.keras under the new version and I'm facing a similar issue. Could you please post a detailed answer of how you solved it and accept it as an answer ? Thanks in advance ! – Wazaki Apr 08 '21 at 02:26

0 Answers0