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

Posted

技术标签:

【中文标题】多输出自定义keras ResNet50模型; ValueError:图表已断开连接【英文标题】:Multioutput custom keras ResNet50 model; ValueError: Graph disconnected 【发布时间】:2021-08-30 09:18:15 【问题描述】:

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

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)

请问有人可以帮我解决这个问题吗?

【问题讨论】:

【参考方案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 的层连接起来,或者以您想要的任何方式连接图表。

【讨论】:

【参考方案2】:

答案是,这里我在模型初始化时犯了一个错误。 input_tensor 应该在模型初始化中添加。

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

【讨论】:

以上是关于多输出自定义keras ResNet50模型; ValueError:图表已断开连接的主要内容,如果未能解决你的问题,请参考以下文章

Image_classification 使用 resnet50 模型和带有我的自定义标签的 imagenet db

Keras深度学习实战——基于ResNet模型实现性别分类

keras BatchNormalization 之坑

如何在 Resnet 50 分类中输出置信度?

如何为 LSTM 实现 Keras 自定义损失函数

将自定义输出层添加到 R 中的内置(功能)keras 模型