python-3.x - 输入 0 与图层 flatten_5 : expected min_ndim=3, 发现 ndim=2 不兼容

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

我正在尝试微调 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 (most recent call last) in

2 model.add(vgg16_model)

3 #add fully connected layer:

----> 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('All layers in a Sequential model '

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

412 # Raise exceptions in case the input is not compatible

413 # with the input_spec specified in the layer constructor.

--> 414 self.assert_input_compatibility(inputs)

415

416 # Collect input shapes to build layer.

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

325 self.name + ': expected min_ndim=' +

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

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

328 # Check dtype.

329 if spec.dtype is not None:

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

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

最佳答案

正式发布keras网页,位于

Fine-tune InceptionV3 on a new set of classes

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))

关于python-3.x - 输入 0 与图层 flatten_5 : expected min_ndim=3, 发现 ndim=2 不兼容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53703440/

相关文章:

python - 在字符串 python 中查找未知模式

python - TensorFlow 并行平均小批量梯度

android - 执行在线预测 Google ML 部署模型时出错

python - 给定需要添加的空格数量,通过在单词之间添加空格来格式化字符串

python - 检测列表中的时间戳字符串

Python Selenium 网络驱动程序 : Does not scroll down the page if browser is minimised

python - 凯拉斯CNN : Add text as additional input besides image to CNN

python - 无效参数错误 : input_1_1:0 is both fed and fetched

python - 如何将输入传递给 Keras 中的 2D Conv?

python - 为什么 Keras fit_generator() 在实际 "training"之前加载