Hello,
I am trying to train a model, then add an additional classifier output to trained model to create a new model. The following is a minimal example to reproduce the error I am encountering. The real application involves some preprocessing in the Model and additional outputs.
inputs_img = tf.keras.Input(shape=(224, 224, 3), name="x")
base_model = tf.keras.applications.densenet.DenseNet121 (
include_top=False,
pooling=None,
input_shape=(224,224,3),
weights="imagenet",
)
prediction = tf.keras.Sequential( [ tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(1, activation="sigmoid") ])
x = base_model(inputs_img, training=False)
x = prediction(x)
model = tf.keras.Model(inputs=inputs_img, outputs=x)
print(model.summary())
# Then the model is compiled, trained, save, and loaded in the application but those steps were
# not needed to reproduce the issue.
base_model_2 = model.get_layer('densenet121')
prediction2 = tf.keras.Sequential(
[ tf.keras.layers.GlobalAveragePooling2D(), tf.keras.layers.Dense(1, activation="sigmoid") ]
)
x2 = prediction2(base_model_2.output)
model2 = tf.keras.Model(inputs=model.inputs, outputs=base_model_2.outputs+[x2])
# The following simpler line fails too
model2 = tf.keras.Model(inputs=model.inputs, outputs=[base_model_2.output])
print(model2.summary())
Here is the error message:
Traceback (most recent call last):
File .../scripts/test_reload.py", line 30, in <module>
model2 = tf.keras.Model(inputs=model.inputs, outputs=base_model_2.outputs+[x2])
File ".../anaconda_envs/tf_cpu/lib/python3.10/site-packages/tensorflow/python/trackable/base.py", line 204, in _method_wrapper
result = method(self, *args, **kwargs)
File ".../anaconda_envs/tf_cpu/lib/python3.10/site-packages/keras/src/engine/functional.py", line 166, in __init__
self._init_graph_network(inputs, outputs)
File ".../anaconda_envs/tf_cpu/lib/python3.10/site-packages/tensorflow/python/trackable/base.py", line 204, in _method_wrapper
result = method(self, *args, **kwargs)
File ".../anaconda_envs/tf_cpu/lib/python3.10/site-packages/keras/src/engine/functional.py", line 265, in _init_graph_network
nodes, nodes_by_depth, layers, _ = _map_graph_network(
File ".../anaconda_envs/tf_cpu/lib/python3.10/site-packages/keras/src/engine/functional.py", line 1145, in _map_graph_network
raise ValueError(
ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='input_1'), name='input_1', description="created by layer 'input_1'") at layer "zero_padding2d". The following previous layers were accessed without issue: []
Any idea on how to create the second Model or a better approach?
TIA