简体   繁体   English

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

[英]Keras shape error in applications Inception Resnet v2

I'm using Keras 2.1.3 and I'm trying to fine tune a Inception Resnetv2 with Keras application.我正在使用 Keras 2.1.3,并且正在尝试使用 Keras 应用程序微调 Inception Resnetv2。

So I load the pretrained model from keras.applications所以我从 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))

I create the bottleneck for my problem :我为我的问题创造了瓶颈:

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

And finally create a new model to concatenate the two parts :最后创建一个新模型来连接两个部分:

new_model = Sequential()
for l in model.layers:
      new_model.add(l)

At this step, I got an error在这一步,我得到了一个错误

ValueError: Input 0 is incompatible with layer conv2d_7: expected axis -1 of input shape to have value 192 but got shape (None, 35, 35, 64) ValueError:输入 0 与层 conv2d_7 不兼容:输入形状的预期轴 -1 具有值 192 但得到形状(无、35、35、64)

So I printed each layer shape and I have所以我打印了每一层的形状,我有

Layer n-1 : Input : (None, 35, 35, 64), Output : (None, 35, 35, 64)第 n-1 层:输入:(无、35、35、64)、输出:(无、35、35、64)

Layer n : Input : (None, 35, 35, 192), Output : (None, 35, 35, 48)第 n 层:输入:(无、35、35、192)、输出:(无、35、35、48)

As you can see shapes dismatch and it seems weird that come from Keras.如您所见,形状不匹配,来自 Keras 的形状似乎很奇怪。

I am not sure top_model.add(Flatten(input_shape=model.output_shape[1:])) is passing the required dimensions.我不确定top_model.add(Flatten(input_shape=model.output_shape[1:]))是否通过了所需的尺寸。
An alternative way would be to try.另一种方法是尝试。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM