0

我想用 DenseNet 进行迁移学习,我找到了一个我想研究的例子。

model_d=DenseNet121(weights='imagenet',include_top=False, 
input_shape=(224, 224, 3)) 

x=model_d.output

x= GlobalAveragePooling2D()(x)
x= BatchNormalization()(x)
x= Dropout(0.5)(x)
x= Dense(1024,activation='relu')(x) 
x= Dense(512,activation='relu')(x) 
x= BatchNormalization()(x)
x= Dropout(0.5)(x)

preds=Dense(7,activation='softmax')(x) 

model=Model(inputs=model_d.input,outputs=preds) 
model.summary()

所以这是用这些层替换原始模型的输出。但是,当我尝试拟合模型时,出现不兼容的形状错误:

ValueError: Shapes (None, 1) and (None, 7) are incompatible

但是,查看模型摘要,我不知道这是什么原因。

4

1 回答 1

0

我认为你在 Softmax 函数之后只有一个预测类,但preds=Dense(7,activation='softmax')(x)预计会有 7 个预测类。number 7 to 1如果您只预测 1 个班级,更改可能会解决您的问题。此外,您应该将激活更改为sigmoid,否则有 1 个神经元softmax将始终输出 1

请注意,这只是我的假设。

于 2021-01-31T13:04:40.537 回答