Changing the name of the input layer of a pre-trained model in TensorFlow Transfer Learning

56 Views Asked by At

I am trying to change the input layer's name of a Pre-trained TensorFlow model imported from Keras

I am able to add the output layer name but I keep failing in adding input model name. Is it even possible to change the input layer name of a pre-trained model?

I can't find any info about this in keras documentation as well. Not sure if I am missing something.

tf_model = Sequential(name = 'mobilenet_model')

pretrained_model = tf.keras.applications.MobileNetV3Small(include_top = False,
                                                  input_shape = (180, 180, 3),
                                                  pooling = 'avg',
                                                  classes = len(CLASS_NAMES),
                                                  weights = 'imagenet')

for layer in pretrained_model.layers:
    layer.trainable = False

tf_model.add(pretrained_model)
tf_model.add(Flatten())
tf_model.add(Dense(512, activation = 'relu'))
tf_model.add(Dense(len(CLASS_NAMES), activation = 'softmax', name = 'MODEL_OUTPUT_LAYER'))
tf_model.summary()

Currently the input model name is the default "MobilenetV3small" but I want to change it to something else.

enter image description here

1

There are 1 best solutions below

0
JSVJ On

Got a potential solution from here - Keras rename model and layers guys.

The solution is to use _name property before compiling the model.

input_layer = tf_model.layers[0]
input_layer._name = 'MODEL_INPUT_LAYER'

However, it doesn't change the pre-trained model's name.