我正在尝试微调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跟踪(最近一次通话最近) 在
2个model.add(vgg16_model)
3#添加完全连接的层:
----> 4个模型。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在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引发TypeError('顺序模型中的所有层'
呼叫中的/usr/local/anaconda/lib/python3.6/site-packages/keras/engine/base_layer.py(自身,输入,**)
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#检查dtype。
329,如果spec.dtype不是None:
ValueError:输入0与flatten_5层不兼容:预期的min_ndim = 3,找到的ndim = 2
我尝试了许多建议的解决方案,但没有一个可以解决我的问题。我该如何解决?
答案 0 :(得分: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))