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.
1 Answer
Reset to default 0This 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
版权声明:本文标题:machine learning - Graph disconnected error when loading model in Tensorflow - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736283923a1927129.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论