多输出自定义keras ResNet50模型;ValueError:图形已断开连接



在我的代码中,我收到了这个错误。我该怎么解决?

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

我的型号是

def multiple_outputs(generator):
for batch_x,batch_y in generator:
yield (batch_x, np.hsplit(batch_y,[26,28])) #here splitting input data into 6 groups

image_input = Input(shape=(input_size))
base_model =ResNet50(weights='imagenet',include_top=False)
m = base_model.output
x = GlobalAveragePooling2D(name='avg_pool')(m)
x = Dropout(0.2)(x)
type_out = Dense(26, activation='sigmoid', name='type_output')(x)
top_out = Dense(3, activation='softmax', name='top_output')(x)
model = Model(inputs=image_input,outputs= [type_out, top_out])

下面我提到了model.fit部分

history = model.fit(x=multiple_outputs(train_generator),
steps_per_epoch=STEP_SIZE_TRAIN,
validation_data=multiple_outputs(valid_generator),
validation_steps=STEP_SIZE_VALID,
callbacks=callbacks,
max_queue_size=10,
workers=1,
use_multiprocessing=False,
epochs=1)

有人能帮我解决这个问题吗?

答案在于仔细检查您的模型体系结构和所得到的错误的性质。

在这里,你的image_input没有连接到你的模型的其余部分

image_input = Input(shape=(input_size))

在代码的其余部分中,您可以看到每个层都连接到其他层,并且您可以从每个层遍历到输出但input_image未连接到其中任何一个

base_model =ResNet50(weights='imagenet',include_top=False)
m = base_model.output
x = GlobalAveragePooling2D(name='avg_pool')(m)
x = Dropout(0.2)(x)
type_out = Dense(26, activation='sigmoid', name='type_output')(x)
top_out = Dense(3, activation='softmax', name='top_output')(x)
bottom_out = Dense(8, activation='softmax', name='bottom_output')(x)
headwear_out = Dense(3, activation='softmax', name='headwear_output')(x)
footwear_out = Dense(3, activation='softmax', name='footwear_output')(x)
sleeve_out = Dense(5, activation='softmax', name='sleeve_output')(x)
gender_out = Dense(3, activation='softmax', name='gender_output')(x)

但在你的模型中,你试图采用inputs=input_imageoutputs=[type_out, top_out, bottom_out, headwear_out, footwear_out, sleeve_out, gender_out]

但由于你的input_image根本没有连接,模型有一个断开的图

所以,试着将你的input_image层与base_model层连接起来,或者以任何你想要的方式连接起来,图形就会被连接起来。

答案是,我在模型初始化中犯了一个错误。模型初始化中应添加Input_tensor。

base_model = ResNet50(weights='imagenet', include_top=False, input_tensor=image_input)

最新更新