Adding a few more layers to pre_trained_model

Can someone explain me why first version of code works but not second?

I first loaded a InceptionV3 pertained model as follow:

pre_trained_model = InceptionV3(input_shape = (150, 150, 3),
                                  include_top = False,
                                  weights = None)

pre_trained_model.load_weights(local_weights_file)

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

Then tried to create a model adding a few more layers to it.

First version:

def create_model1():
  last_desired_layer = pre_trained_model.get_layer('mixed7')
  last_output = last_desired_layer.output
  x = layers.Flatten()(last_output)
  x = tf.keras.layers.Dropout(0.2)(x)
  x = tf.keras.layers.Dense(1024, activation='relu')(x)
  x = tf.keras.layers.Dense(1, activation='sigmoid')(x)

  model = Model(inputs=pre_trained_model.input, outputs=x)

  model.compile(optimizer=RMSprop(learning_rate=0.0001),
                loss='binary_crossentropy',
                metrics=['accuracy'])
  
  return model

Second version:

def create_model2():
  inputs1 = keras.Input(shape=(150, 150, 3))
  dummy_output = pre_trained_model(inputs1)
  last_desired_layer = pre_trained_model.get_layer('mixed7')
  last_output = last_desired_layer.output
  x = layers.Flatten()(last_output)
  x = tf.keras.layers.Dropout(0.2)(x)
  x = tf.keras.layers.Dense(1024, activation='relu')(x)
  x = tf.keras.layers.Dense(1, activation='sigmoid')(x)

  model = Model(inputs=inputs1, outputs=x)

  model.compile(optimizer=RMSprop(learning_rate=0.0001),
                loss='binary_crossentropy',
                metrics=['accuracy'])

  return model

First version works fine, but second version gives error saying:
ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 150, 150, 3), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'") at layer "conv2d". The following previous layers were accessed without issue: []

I double checked the pre_trained_model’s input shape is (None, 150, 150, 3). But why this error is happening?

Hi @Seungjun_Lee,

Sorry for the delay in response.
I could see you are directly using the inputs as in the pretrained model to your new model but in model2 creates a new input tensor inputs1 which still uses the original model’s last layer last_desired_layer, causing a graph disconnection error. To fix this, I suggest to use the output of the new input (dummy_output) as the last_desired_layer as below.

def create_model2():
  inputs1 = keras.Input(shape=(150, 150, 3))
  dummy_output = pre_trained_model(inputs1)
  last_desired_layer = dummy_output 

Please let us know if this works as expected.Thank You.