Value Error: Shapes (None, 21) and (None, 7, 7, 21) are incompatible.

huangapple go评论50阅读模式
英文:

Value Error: Shapes (None, 21) and (None, 7, 7, 21) are incompatible

问题

抱歉,以下是您要的代码部分的翻译:

# 冻结预训练模型层的权重
for layer in base_model.layers:
    layer.trainable = False

# 用3个新层替换最后的3个全连接层
x = base_model.output
x = Dense(50, activation='relu')(x)
x = Dense(20, activation='relu')(x)
x = Dense(num_classes, activation='softmax')(x)

# 通过将VGG16的基本部分与新层连接来创建一个新模型
model = Model(inputs=base_model.input, outputs=x)

希望这有所帮助。如果您有更多问题,请随时提问。

英文:

Hello I am new to using keras. and I am doing an exercise with the VGG16 model.

I have loaded the model and now I have to Replace the last 3 fully connected layers with 3 new layers: the first 2 of 50 and 20 neurons respectively with ReLU activation, and a last layer with the appropriate number of neurons.

I have done the following:

enter code here

# Freeze the weights of the pretrained model layers
for layer in base_model.layers:
    layer.trainable = False

# Replace the last 3 fully connected layers with 3 new layers
x = base_model.output
x = Dense(50, activation='relu')(x)
x = Dense(20, activation='relu')(x)
x = Dense(num_classes, activation='softmax')(x)

# Create a new model by joining the base of VGG16 with the new layers
model = Model(inputs=base_model.input, outputs=x)

the model.summary() is like this:

=================================================================

input_13 (InputLayer) [(None, 224, 224, 3)] 0

block1_conv1 (Conv2D) (None, 224, 224, 64) 1792

block1_conv2 (Conv2D) (None, 224, 224, 64) 36928

block1_pool (MaxPooling2D) (None, 112, 112, 64) 0

block2_conv1 (Conv2D) (None, 112, 112, 128) 73856

block2_conv2 (Conv2D) (None, 112, 112, 128) 147584

block2_pool (MaxPooling2D) (None, 56, 56, 128) 0

block3_conv1 (Conv2D) (None, 56, 56, 256) 295168

block3_conv2 (Conv2D) (None, 56, 56, 256) 590080

block3_conv3 (Conv2D) (None, 56, 56, 256) 590080

block3_pool (MaxPooling2D) (None, 28, 28, 256) 0

block4_conv1 (Conv2D) (None, 28, 28, 512) 1180160

block4_conv2 (Conv2D) (None, 28, 28, 512) 2359808

block4_conv3 (Conv2D) (None, 28, 28, 512) 2359808

block4_pool (MaxPooling2D) (None, 14, 14, 512) 0

block5_conv1 (Conv2D) (None, 14, 14, 512) 2359808

block5_conv2 (Conv2D) (None, 14, 14, 512) 2359808

block5_conv3 (Conv2D) (None, 14, 14, 512) 2359808

block5_pool (MaxPooling2D) (None, 7, 7, 512) 0

dense_6 (Dense) (None, 7, 7, 50) 25650

dense_7 (Dense) (None, 7, 7, 20) 1020

dense_8 (Dense) (None, 7, 7, 21) 441

=================================================================

Total params: 14,741,799
Trainable params: 27,111
Non-trainable params: 14,714,688


When I try to train the model it gives me the following error:

"ValueError: Shapes (None, 21) and (None, 7, 7, 21) are incompatible"

What could be the problem?

Thanks!

答案1

得分: 1

# 摊平输出层:
x = base_model.output
x = Flatten(name="flatten")(x)
x = Dense(50, activation='relu')(x)
x = Dense(20, activation='relu')(x)
x = Dense(num_classes, activation='softmax')(x)
英文:

Flatten the output layer:


x = base_model.output
x = Flatten(name="flatten")(x)
x = Dense(50, activation='relu')(x)
x = Dense(20, activation='relu')(x)
x = Dense(num_classes, activation='softmax')(x)

huangapple
  • 本文由 发表于 2023年4月4日 17:34:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927777.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定