1

I am running a code repository and I got an error message while recreate the model.

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

I am using Tensorflow 2.14.0 and Python 3.10

This is the test code,which I found is the code giving the error from the repo:

num_classes = 10
input_shape = (32, 32, 3)
model = tf.keras.Sequential()
base_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_shape=input_shape)
for layer in base_model.layers:
    layer.trainable = False
model.add(base_model)
model.add(tf.keras.layers.GlobalAveragePooling2D())
model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))

extractor = tf.keras.Model(inputs=model.layers[0].input,
                                outputs=[layer.output for layer in model.layers])

I searched through Google and tried every methods. Some people said I should first create an input but still got the error with the code below. Can anyone give me a solution. I appreciate it.

num_classes = 10
input_shape = (32, 32, 3)
input_tensor = tf.keras.Input(shape=input_shape)
model = tf.keras.Sequential()
base_model = tf.keras.applications.ResNet50(weights='imagenet', include_top=False, input_shape=input_shape, input_tensor=input_tensor)
for layer in base_model.layers:
    layer.trainable = False
model.add(base_model)
model.add(tf.keras.layers.GlobalAveragePooling2D())
model.add(tf.keras.layers.Dense(num_classes, activation='softmax'))

extractor = tf.keras.Model(inputs=model.layers[0].input,
                                outputs=[layer.output for layer in model.layers])
1
  • Did the given answer solve the issue? Commented Feb 25 at 19:05

1 Answer 1

0

This is a know limitation of sequential modelling with keras, see this ticket. In order to get intermediate feature vector, you can adopt functional modelling approach. Here's how to structure it:

import tensorflow as tf
import numpy as np

inputs = tf.keras.Input(shape=input_shape)
pretrained_model = tf.keras.applications.ResNet50(
        weights='imagenet', 
        include_top=False, 
        input_tensor=inputs,
    )
pretrained_model.trainable = False

x = tf.keras.layers.GlobalAveragePooling2D()(
    pretrained_model.output
)
outputs = tf.keras.layers.Dense(10, activation='softmax')(x)
model = tf.keras.Model(inputs, outputs)
feature_extractor = tf.keras.Model(
    model.inputs, [layer.output for layer in model.layers]
)

Let's see some test result.

# test
sample = np.ones((1, 32, 32, 3))

sample_output = model(sample)
sample_output.shape
TensorShape([1, 10])

feat_output = feature_extractor(sample)
print(len(feat_output))
for fo in feat_output:
    print(fo.shape)

177
(1, 32, 32, 3)
(1, 38, 38, 3)
(1, 16, 16, 64)
...
(1, 1, 1, 2048)
(1, 2048)
(1, 10)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.