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?