首页 文章

输入0与图层flatten_5不兼容:预期min_ndim = 3,发现ndim = 2

提问于
浏览
1

我正在尝试微调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 #add完全连接层:----> 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引发TypeError('所有层中的'顺序模型'/usr/local/anaconda/lib/python3.6/site-packages/keras/engine/base_layer.py in call(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 assert_input_compatibility中的/base_layer.py(self,inputs)325 self.name':expected min_n dim ='326 str(spec.min_ndim)',found ndim =' - > 327 str(K.ndim(x)))328#检查dtype . 329如果spec.dtype不是None:ValueError:输入0与图层flatten_5不兼容:预期min_ndim = 3,发现ndim = 2

我尝试了许多建议的解决方案,但没有一个能解决我的问题 . 我怎么解决这个问题?

1 回答

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

相关问题