[英]Input 0 is incompatible with layer global_average_pooling2d_4: expected ndim=4, found ndim=2 error
I am trying to fine-tune a VGG16 model. 我正在尝试微调VGG16模型。 I have removed the last 5 layers 我已经删除了最后5层
(*block5_pool (MaxPooling2D),flatten(Flatten),fc1 (Dense),fc2 (Dense),predictions (Dense)*).
Now, I want to add a global average pooling layer, but I am getting this error 现在,我想添加一个全局平均池层,但出现此错误
Input 0 is incompatible with layer global_average_pooling2d_4: expected ndim=4, found ndim=2** 输入0与图层global_average_pooling2d_4不兼容:预期ndim = 4,找到的ndim = 2 **
what seems to be the problem here? 这里似乎是什么问题?
model = VGG16(weights='imagenet', include_top=True)
model.layers.pop()
model.layers.pop()
model.layers.pop()
model.layers.pop()
model.layers.pop()
x = model.output
x = GlobalAveragePooling2D()(x)
If want to remove the last four layers, then just use include_top=False
. 如果要删除最后四层,则只需使用include_top=False
。 Further, use pooling='avg'
to add a GlobalAveragePooling2D
layer as the last layer: 此外,使用pooling='avg'
将GlobalAveragePooling2D
图层添加为最后一层:
model = VGG16(weights='imagenet', include_top=False, pooling='avg')
A note about why your original solution does not work: as already suggested in this answer , you can't use pop()
method on layers
attribute of the models to remove a layer. 关于为什么原始解决方案不起作用的说明:如该答案中已经建议的那样,您不能在模型的layers
属性上使用pop()
方法来删除层。 Instead, you need to reference their output directly (eg model.layers[-4].output
) and then feed them to other layers if you want to add new connections. 相反,您需要直接引用它们的输出(例如, model.layers[-4].output
),然后如果要添加新的连接,则将它们提供给其他层。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.