0

I am trying to do image classification using VGG16 pre-trained model. For the same, I did the following:

vgg16_model = keras.applications.vgg16.VGG16()

The type of the model is as follows:

type(vgg16_model)

And the result is:

tensorflow.python.keras.engine.training.Model

Then, I defined a Sequential model as:

model = Sequential()

Then, I tried to convert the vgg16_model into sequential by:

for layer in vgg16_model.layers:
    model.add(layer)

It shows me an error as follows:

TypeError: The added layer must be an instance of class Layer. Found: < tensorflow.python.keras.engine.input_layer.InputLayer object at 0x1ddbce5e80>**

It would be great if anyone could help me on this one.

today
  • 32,602
  • 8
  • 95
  • 115
Kris
  • 327
  • 3
  • 6
  • 19

2 Answers2

1

Solution:

My mistake was that my import statement was:

from keras.applications.vgg16 import VGG16

Then, again, when I initialised the model, I called it again as:

vgg16_model = keras.applications.vgg16.VGG16()

So, a stupid mistake on my part. The fix is as follows:

vgg16_model = VGG16()

I realise that the problem is very specific and might not be so much useful to the community. Still, I am posting the solution just in case someone else faces it again.

Kris
  • 327
  • 3
  • 6
  • 19
0

One simpler way to do this is to pass the layers directly to the Sequential model instance, instead of using a for loop:

from keras.applications.vgg16 import VGG16

vgg = VGG16(weights='imagenet', ...)
model = Sequential(vgg.layers)
today
  • 32,602
  • 8
  • 95
  • 115