Multioutput custom keras ResNet50 model; ValueError: Graph disconnected

Multioutput custom keras ResNet50 model; ValueError: Graph disconnected

在我的代码中出现了这个错误。我该如何解决?

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)