[英]ValueError: Input 0 of layer global_average_pooling2d is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: [None, 128]
[英]Input 0 is incompatible with layer global_average_pooling2d_4: expected ndim=4, found ndim=2 error
我正在嘗試微調VGG16模型。 我已經刪除了最后5層
(*block5_pool (MaxPooling2D),flatten(Flatten),fc1 (Dense),fc2 (Dense),predictions (Dense)*).
現在,我想添加一個全局平均池層,但出現此錯誤
輸入0與圖層global_average_pooling2d_4不兼容:預期ndim = 4,找到的ndim = 2 **
這里似乎是什么問題?
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)
如果要刪除最后四層,則只需使用include_top=False
。 此外,使用pooling='avg'
將GlobalAveragePooling2D
圖層添加為最后一層:
model = VGG16(weights='imagenet', include_top=False, pooling='avg')
關於為什么原始解決方案不起作用的說明:如該答案中已經建議的那樣,您不能在模型的layers
屬性上使用pop()
方法來刪除層。 相反,您需要直接引用它們的輸出(例如, model.layers[-4].output
),然后如果要添加新的連接,則將它們提供給其他層。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.