'Graph disconnected: cannot obtain value for tensor KerasTensor

I has this model:

def get_resnet_simclr(hidden_1, hidden_2, hidden_3):
    
    base_model = tf.keras.applications.ResNet50(include_top=False, weights=None, input_shape=(224, 224, 3))
    base_model.trainable = True
    
    inputs = Input((224, 224, 3))
    h = base_model(inputs, training=True)
    h = GlobalAveragePooling2D()(h )

    projection_1 = Dense(hidden_1)(h)
    projection_1 = Activation("relu")(projection_1)
    projection_2 = Dense(hidden_2)(projection_1)
    projection_2 = Activation("relu")(projection_2)
    projection_3 = Dense(hidden_3)(projection_2)

    resnet_simclr = Model(inputs, projection_3)

    return resnet_simclr 

I was trying to get only the base model, which is the ResNet50, so I did the following:

inputs1 = Input((224, 224, 3))
layer_output = resnet_simclr.get_layer("resnet50").output
model2 = Model(inputs=inputs1, outputs=layer_output) 

And I got the below error, I don't know why, or how to fix it:

ValueError                                Traceback (most recent call last)
<ipython-input-64-7c9937cd86ca> in <module>()
      3 inputs1 = Input((224, 224, 3))
      4 layer_output = resnet_simclr.get_layer("resnet50").output
----> 5 model2 = Model(inputs=inputs1, outputs=layer_output)
      

4 frames
/usr/local/lib/python3.7/dist-packages/keras/engine/functional.py in _map_graph_network(inputs, outputs)
   1035           if id(x) not in computable_tensors:
   1036             raise ValueError(
-> 1037                 f'Graph disconnected: cannot obtain value for tensor {x} '
   1038                 f'at layer "{layer.name}". The following previous layers '
   1039                 f'were accessed without issue: {layers_with_complete_input}')

ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 224, 224, 3), dtype=tf.float32, name='input_3'), name='input_3', description="created by layer 'input_3'") at layer "conv1_pad". The following previous layers were accessed without issue: []


Sources

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

Source: Stack Overflow

Solution Source