【问题标题】:Input 0 is incompatible with layer flatten_5: expected min_ndim=3, found ndim=2输入 0 与 flatten_5 层不兼容:预期 min_ndim=3,发现 ndim=2
【发布时间】:2018-12-10 10:10:45
【问题描述】:

我正在尝试微调 VGG16 神经网络,代码如下:

vgg16_model = VGG16(weights="imagenet", include_top="false", input_shape=(224,224,3))
model = Sequential()
model.add(vgg16_model)
#add fully connected layer:
model.add(Flatten())
model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(3, activation='softmax')) 

我收到此错误:

ValueError Traceback(最近一次调用最后一次) 在

2 model.add(vgg16_model)

3 #添加全连接层:

----> 4 model.add(Flatten())

5 model.add(Dense(256, activation='relu'))

6 model.add(Dropout(0.5))

/usr/local/anaconda/lib/python3.6/site-packages/keras/engine/sequential.py in add(self, layer) 179 self.inputs = network.get_source_inputs(self.outputs[0])

180 elif self.outputs:

--> 181 output_tensor = layer(self.outputs[0])

182 if isinstance(output_tensor, list):

183 raise TypeError('顺序模型中的所有层'

/usr/local/anaconda/lib/python3.6/site-packages/keras/engine/base_layer.py in 调用(self,inputs,**kwargs)

412 # 在输入不兼容的情况下引发异常

413 # 使用层构造函数中指定的 input_spec。

--> 414 self.assert_input_compatibility(inputs)

415

416 # 收集输入形状以构建层。

/usr/local/anaconda/lib/python3.6/site-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)

325 self.name + ': 预期 min_ndim=' +

326 str(spec.min_ndim) + ',找到 ndim=' +

--> 327 str(K.ndim(x)))

328 # 检查数据类型。

329 如果 spec.dtype 不是 None:

ValueError: Input 0 is in compatible with layer flatten_5: expected min_ndim=3, found ndim=2

我尝试了许多建议的解决方案,但没有一个可以解决我的问题。我该如何解决这个问题?

【问题讨论】:

  • 为什么要展平输出? VGG16 输出的不是 2D 图像。

标签: python-3.x tensorflow keras jupyter-notebook vgg-net


【解决方案1】:

在官方keras网页上,在

在一组新的类上微调 InceptionV3

from keras.models import Model
vgg16_model = VGG16(weights="imagenet", include_top="false", input_shape=(224,224,3))
x = vgg16_model.output
x=Flatten()(x)
x=Dense(256, activation='relu')(x)
x=Dropout(0.5)(x)
predictions=Dense(3, activation='softmax')(x)

model = Model(inputs=base_model.input, outputs=predictions)

include_top="false" 中有错误,这会导致您收到错误消息。试试:

vgg16_model = VGG16(weights="imagenet", include_top=False, input_shape=(224,224,3))

【讨论】:

  • 感谢您的回答,但我遇到了同样的错误。
  • 你是对的。我已经更新了我的答案,现在应该可以正常运行了。
  • 谢谢!现在运行良好。
  • include_top=False,不是“假”
猜你喜欢
  • 2019-07-13
  • 2022-07-13
  • 1970-01-01
  • 2020-06-12
  • 1970-01-01
  • 1970-01-01
  • 2020-10-05
  • 2022-08-15
  • 2019-06-04
相关资源
最近更新 更多