Skip to content Skip to sidebar Skip to footer

The Added Layer Must Be An Instance Of Class Layer. Found:

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

Solution 1:

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])

Solution 2:

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

import keras
from keras.modelsimportSequentialfrom keras.layersimportInputLayer

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.kerasas keras
from tensorflow.keras.modelsimportSequentialfrom tensorflow.keras.layersimportInputLayer

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)

Solution 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.layersimportConv2D, Flatten, MaxPooling2D, Dropout, BatchNormalization

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

from tensorflow.keras.layersimportConv2D, Flatten, MaxPooling2D, Dropoutfrom keras.layersimportBatchNormalization

Solution 4:

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

Post a Comment for "The Added Layer Must Be An Instance Of Class Layer. Found: "