The added layer must be an instance of class Layer. Found: <tensorflow.python.keras.engine.input_layer.InputLayer>
Asked Answered
A

4

27

I am new to machine learning. I was following this tutorial on fine-tuning VGG16 models.

The model loaded fine with this code:

vgg_model = tensorflow.keras.applications.vgg16.VGG16()

but gets this ERROR:

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

When running this code:

model = Sequential()
for layer in vgg_model.layers[:-1]:
    model.add(layer)

Dependencies:

  • Keras 2.2.3
  • Tensorflow 1.12.0
  • tensorflow-gpu1.12.0
  • Python 3.6.0

I am following this blog but instead, I want to use VGG16.

Any help to fix this would be appreciated. Thank you so much.

Anselme answered 24/3, 2019 at 14:21 Comment(1)
Version Keras 2.2.4 tf. changed import from import from import keras.layers as layers to tensorflow.keras.layers as layersTyphoid
P
37

This won't work because a tensorflow.keras layer is getting added to a keras Model.

vgg_model = tensorflow.keras.applications.vgg16.VGG16()
model = keras.Sequential()
model.add(vgg_model.layers[0])

Instantiate tensorflow.keras.Sequential(). This will work.

model = tensorflow.keras.Sequential()
model.add(vgg_model.layers[0])
Pilsen answered 24/3, 2019 at 15:38 Comment(1)
Yes, this is one scenario: mixing keras.Sequential() with tf.keras.Sequential(). The other problem is with Input (which is a tensor) vs InputLayer which is a layer, and can be added to a Sequential model. However, I got some code which added Input to a Sequential model, and it worked for somebody else (who had a different configuration / version etc.). (I needed to patch this code...)Biondo
V
12

Adding to @Manoj Mohan's answer, you can add an input_layer to your model using input_layer from Keras layers as below:

import keras
from keras.models import Sequential
from keras.layers import InputLayer

model = Sequential()
model.add(InputLayer(input_shape=shape, name=name))
....

if you are using the TensorFlow builtin Keras then import is different other things are still the same

import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import InputLayer

model = Sequential()
model.add(InputLayer(input_shape=shape, name=name))
....

Coming to the main part, if you want to import layers to your sequential model, you can use the following syntax.

import keras
from keras.models import Sequential, load_model
from keras import optimizers
from keras.applications.vgg16 import VGG16
from keras.applications.vgg19 import VGG19

# For VGG16 loading to sequential model  
model = Sequential(VGG16().layers)
# For VGG19 loading to sequential model  
model = Sequential(VGG19().layers)
Voorhis answered 21/11, 2019 at 21:46 Comment(0)
K
3

You do not need to create an InputLayer, you simply must import the BatchNormalization layer in the same manner as your Conv2D/other layers, e.g:

from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dropout, BatchNormalization

Instead of importing it as an independent Keras layer, i.e:

from tensorflow.keras.layers import Conv2D, Flatten, MaxPooling2D, Dropout
from keras.layers import BatchNormalization
Kwangchow answered 1/10, 2019 at 19:59 Comment(0)
C
0

The above code snippet works for TensorFlow version 2.x. You can run the above snippet by upgrading your TensorFlow using the following command:

pip install --upgrade tensorflow
Coheir answered 6/12, 2021 at 11:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.