简体   繁体   English

输入0与图层global_average_pooling2d_4不兼容:预期ndim = 4,发现ndim = 2错误

[英]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.

相关问题 ValueError:global_average_pooling2d 层的输入 0 与该层不兼容:预期 ndim=4,发现 ndim=2。 收到的完整形状:[无,128] - ValueError: Input 0 of layer global_average_pooling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 128] Keras 尺寸错误 - (层“max_pooling2d”的输入 0 与层不兼容:预期 ndim=4,发现 ndim=6。) - Keras Dimension error - (Input 0 of layer "max_pooling2d" is incompatible with the layer: expected ndim=4, found ndim=6.) 层“max_pooling2d_30”的输入0与层不兼容:预期ndim=4,发现ndim=5 - Input 0 of layer "max_pooling2d_30" is incompatible with the layer: expected ndim=4, found ndim=5 ValueError: 层 max_pooling1d 的输入 0 与层不兼容:预期 ndim=3,发现 ndim=4。 收到的完整形状:(无、128、1、32) - ValueError: Input 0 of layer max_pooling1d is incompatible with the layer: expected ndim=3, found ndim=4. Full shape received: (None, 128, 1, 32) 层 max_pooling2d 的输入 0 与层不兼容:预期 ndim=4,发现 ndim=5。 收到完整形状:[无、4、10、8、32] - Input 0 of layer max_pooling2d is incompatible with the layer: expected ndim=4, found ndim=5. Full shape received: [None, 4, 10, 8, 32] 层“bidirectional_2”的输入 0 与层不兼容:预期 ndim=3,发现 ndim=2 - Input 0 of layer "bidirectional_2" is incompatible with the layer: expected ndim=3, found ndim=2 lstm_5层的输入0与层不兼容:预期ndim=3,发现ndim=2 - Input 0 of layer lstm_5 is incompatible with the layer: expected ndim=3, found ndim=2 添加Conv1D层时出现错误“输入0与层conv1d_48不兼容:预期的ndim = 3,找到的ndim = 2” - Error 'Input 0 is incompatible with layer conv1d_48: expected ndim=3, found ndim=2' when adding Conv1D layer 在 Tensorflow 中创建 model 时出错。 ValueError: 层 conv2d_1 的输入 0 与层不兼容: : 预期 min_ndim=4, 发现 ndim=3 - Error with creating a model in Tensorflow. ValueError: Input 0 of layer conv2d_1 is incompatible with the layer: : expected min_ndim=4, found ndim=3 Keras CNN错误conv1d_13层的输入0与层不兼容::预期min_ndim=3,发现ndim=2 - Keras CNN error Input 0 of layer conv1d_13 is incompatible with the layer: : expected min_ndim=3, found ndim=2
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM