admin管理员组

文章数量:1122832

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])

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])
Share Improve this question edited yesterday Innat 17.2k6 gold badges59 silver badges111 bronze badges asked yesterday Vincent MaVincent Ma 111 bronze badge New contributor Vincent Ma is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
Add a comment  | 

1 Answer 1

Reset to default 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)

本文标签: machine learningGraph disconnected error when loading model in TensorflowStack Overflow