使用预先训练的Inceptionv3的Keras问题 [英] Issue with Keras using pretrained Inceptionv3
问题描述
我在Keras中将InceptionV3与imagenet权重一起使用.我正在使用的Keras版本是2.2.4,而Keras-applications是1.0.8.张量流版本为1.14.0.我正在遵循此处中概述的使用InceptionV3进行迁移学习的标准方法.我收到此错误ValueError: Input 0 is incompatible with layer global_average_pooling2d_3: expected ndim=4, found ndim=2
.我在用户遇到相同问题的GitHub 帖子中找到了一个帖子.我遵循了在GitHub帖子上解决该问题的建议,但是我没有这种运气. MWE在下面
I am using InceptionV3 with imagenet weights in Keras. The version of Keras I am using is 2.2.4 and Keras-applications is 1.0.8. The tensorflow version is 1.14.0. I am following the standard way of using InceptionV3 for transfer learning, as outlined here. I am getting this error ValueError: Input 0 is incompatible with layer global_average_pooling2d_3: expected ndim=4, found ndim=2
. I found a GitHub post where the user was facing the same issue. I followed the suggestion which fixed the issue on the GitHub post, but I have had no such luck. MWE is below
from keras.layers import Input, Dense, Activation, GlobalAveragePooling2D
from keras.models import Model
from keras.applications.inception_v3 import InceptionV3
base_model = InceptionV3(weights='imagenet', include_top='False')
x = base_model.output
x = GlobalAveragePooling2D()(x) # Error appears here
x = Dense(1024, activation='relu')(x)
predictions = Dense(3, activation='softmax')(x)
model = Model(inputs=base_model.input, outputs=predictions)
推荐答案
原因是您将 string 'False'
传递给了include_top
.非空字符串的评估结果为True
,因此,您认为的裸照模型实际上与降低维数的平均池和完全连接的层进行了装饰.
The reason is that you passed the string 'False'
to include_top
. Non-empty strings evaluate to True
, so what you thought was the topless model was, in fact, fully adorned with the dimensionality-reducing average pooling and fully-connected layers.
因此,解决问题的一种方法是将'False'
更改为False
.但是,我要添加的是,您只需指定pooling='avg'
,因此只需添加最后一个Dense
层...
Accordingly, one way to solve your problem would be to change 'False'
to False
. I would add, however, that you can just specify pooling='avg'
, so you only have to add the last Dense
layer...
这篇关于使用预先训练的Inceptionv3的Keras问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!