简体   繁体   English

合并两个深度学习模型 VGG16 和 ResNet50 后出错

[英]Error after merging two Deep Learning models VGG16 and ResNet50

I have merged two different models namely VGG16 and ResNet50 and given the outputs of the two models as input to another model.我合并了两个不同的模型,即 VGG16 和 ResNet50,并将这两个模型的输出作为另一个 model 的输入。 I have checked the Layers graph is correct.我检查了图层图是否正确。 Before merging the code was running perfectly fine giving correct outputs.在合并之前,代码运行得非常好,给出了正确的输出。 I am getting an error:我收到一个错误:

 "ValueError: Shapes (None, None) and (None, 7, 7, 3) are incompatible" on the line 6 

ValueError                                Traceback (most recent call last)  
<ipython-input-36-620554d0106f> in <module>()  

   4     epochs = 200,  

   5     validation_data = validation_generator,  

----> 6     validation_steps=2  

My code is below:我的代码如下:

inputs_2 = keras.Input(shape=(224, 224, 3), name="img")
vgg = VGG16(input_tensor=inputs_2, weights='imagenet', include_top=False) #VGG16(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
for layer in vgg.layers:
    layer.trainable = False
#x = Flatten()(vgg.output)

resnet = ResNet50(input_tensor=inputs_2, weights='imagenet', include_top=False) #ResNet50(input_shape=IMAGE_SIZE + [3], weights='imagenet', include_top=False)
for layer in resnet.layers:
    layer.trainable = False
#y = Flatten()(resnet.output)

#concatenated_output= layers.add([x, y])
mergedOutput = Concatenate()([vgg.output, resnet.output])
x = layers.Dense(256, activation="relu")(mergedOutput)
prediction = Dense(3, activation='softmax')(x)
model = Model(inputs=vgg.input, outputs=prediction)


model.compile(loss="categorical_crossentropy",optimizer='adam',metrics=['accuracy'])

keras.utils.plot_model(model, "mini_resnet.png", show_shapes=True)  
    train_datagen = image.ImageDataGenerator(
    rescale = 1./255,
    shear_range = 0.2,
    zoom_range = 0.2,
    horizontal_flip = True,
)

test_dataset = image.ImageDataGenerator(rescale=1./255)

train_generator = train_datagen.flow_from_directory(
    TRAIN_PATH,
    color_mode = "rgb",
    target_size = (224,224),
    batch_size = 32,
    class_mode = 'categorical'
    )
print(train_generator.class_indices)

validation_generator = test_dataset.flow_from_directory(
    VAL_PATH,
    color_mode = "rgb",
    target_size = (224,224),
    batch_size = 32,
    class_mode = 'categorical')

history = model.fit_generator(
    train_generator,
    steps_per_epoch=8,
    epochs = 200,
    validation_data = validation_generator,
    validation_steps=2
)

Graph of merged models合并模型图

I am using one-hot encoding and have correctly written the parameters.我正在使用单热编码并正确编写了参数。

Try this:尝试这个:

model = Model(inputs=inputs_2, outputs=prediction)

If you set your inputs with vgg.input , you don't feed the Resnet part so it outputs nothing.如果您使用vgg.input设置输入,则不会提供 Resnet 部分,因此它不会输出任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM