'NameError: name 'Model is not defined'-how to resolve this?

I am trying to classify 2 categories with transfer learning. After preprocessing my data I want to apply 'InceptionResNetV2'. Where I want to remove the last layer of this Keras application and want to add a layer. The following script I wrote to do this:

irv2 = tf.keras.applications.inception_resnet_v2.InceptionResNetV2()
irv2.summary()

x = irv2.layers[-1].output
x = Dropout(0.25)(x)
predictions = Dense(2, activation='softmax')(x)

model = Model(inputs=mobile.input, outputs=predictions)

Then an error occurred:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-40-911de74d9eaf> in <module>()
      5 predictions = Dense(2, activation='softmax')(x)
      6 
----> 7 model = Model(inputs=mobile.input, outputs=predictions)

NameError: name 'Model' is not defined

If is there another way to remove the last layer and add a new layer(predictions = Dense(2, activation='softmax')) please let me know.

This is my full code.



Solution 1:[1]

You can use this code snippet to define your transfer learning model.

Here, we are using weights trained on imagenet datsaset and are ignoring the final layer (the 1000 neuron layer that was used to train 1000 classes in imagenet dataset) and adding our custom layers. In this example we are adding a GAP layer followed by a dense layer for binary classification.

 from tensorflow import keras

 input_layer = keras.layers.Input(shape=(224, 224, 3))
 irv2 = keras.applications.Xception(weights='imagenet',include_top=False,input_tensor = input_layer)
 global_avg = keras.layers.GlobalAveragePooling2D()(irv2.output)
 dense_1 = keras.layers.Dense(1,activation = 'sigmoid')(global_avg)
 model = keras.Model(inputs=irv2.inputs,outputs=dense_1)

 model.summary()

The error you faced could possibly be due to the import changes between tf 1.x and tf 2.x

Try out any one of the below import methods depending on your tensorflow version. It should fix the error.

from tensorflow.keras.models import Model

or

from tensorflow.keras import Model

And also make sure you either import everything from tensorflow or from keras. Using the functions which are imported from either of the libraries in the same script would cause incompatibility errors.

Solution 2:[2]

  • -1 will give you the last Dense layer, but what you really what it a layer above that which is -2
  • Input should be the inception model input layer
import tensorflow as tf
from tensorflow.keras.layers import Dense
from keras.models import Model

irv2 = tf.keras.applications.inception_resnet_v2.InceptionResNetV2()
predictions = Dense(2, activation='softmax')(irv2.layers[-2].output)
model = Model(inputs=irv2.input, outputs=predictions)

model.summary()

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 vamshi rvk
Solution 2 mujjiga