Python Resnet v2应用程序中的Keras形状错误

Python Resnet v2应用程序中的Keras形状错误,python,deep-learning,keras,Python,Deep Learning,Keras,我正在使用Keras 2.1.3,并试图用Keras应用程序微调Inception Resnetv2 所以我从keras.applications加载预训练模型 input_tensor = Input(shape=(299,299,3)) model = applications.inception_resnet_v2.InceptionResNetV2(weights='imagenet',

我正在使用Keras 2.1.3,并试图用Keras应用程序微调Inception Resnetv2

所以我从keras.applications加载预训练模型

input_tensor = Input(shape=(299,299,3))
model = applications.inception_resnet_v2.InceptionResNetV2(weights='imagenet', 
                                                        include_top=False,
                                                        input_tensor=input_tensor,
                                                        input_shape=(299, 299,3))
我为我的问题制造了瓶颈:

top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(40, activation='softmax'))
最后创建一个新模型来连接两个部分:

new_model = Sequential()
for l in model.layers:
      new_model.add(l)
在这一步中,我犯了一个错误

ValueError:输入0与层conv2d_7不兼容:输入形状的轴-1应有值192,但得到形状(无、35、35、64)

所以我打印了每一层的形状

第n-1层:输入:(无,35,35,64),输出:(无,35,35,64)

第n层:输入:(无,35,35,192),输出:(无,35,35,48)


正如您所看到的,形状不匹配,来自Keras的形状看起来很奇怪。

我不确定
top\u model.add(展平(input\u shape=model.output\u shape[1:])
是否通过了所需的尺寸。
另一种方法是尝试。

ResNetV2_model_output = model.output
new_concatenated_model = Flatten()(ResNetV2_model_output)
new_concatenated_model = (Dense(256, activation='relu'))(new_concatenated_model)
new_concatenated_model = ((Dropout(0.5)))(new_concatenated_model)
new_concatenated_model = (Dense(40, activation='softmax'))(new_concatenated_model)

我不明白你的选择,新的连接模式必须是新的顺序模式?然后添加新层?但我的错误似乎来自早期层(~21-22),因此我认为问题不在于新的顶层模型。您可以选择在模型上添加层,而无需顺序,此选项使您可以从那里开始并添加自定义层。我尝试了您的解决方案,但我不知道我的模型是什么,当我打印新的连接模型时,我有keras.layers.core.Dense,我不能在层上迭代或编译它。Ok model2=keras.model(输入=input\u张量,输出=new\u连接模型)和model2=keras.model(输入=model.input,输出=new\u连接模型),谢谢你的回答。交叉发布:。请每个社区都应该诚实地回答问题,而不浪费任何人的时间。