预先训练Keras Xception和InceptionV3模型 [英] Pre-training Keras Xception and InceptionV3 models
问题描述
我正在尝试使用Keras及其预建的ImageNet CNN架构来做一个简单的二进制分类问题.
I'm trying to do a simple binary classification problem using Keras and its pre-built ImageNet CNN architecture.
对于VGG16,我采用了以下方法,
For VGG16, I took the following approach,
vgg16_model = keras.application.vgg16.VGG16()
'''Rebuild the vgg16 using an empty sequential model'''
model = Sequential()
for layer in vgg16_model.layers:
model.add(layer)
'''Since the problem is binary, I got rid of the output layer and added a more appropriate output layer.'''
model.pop()
'''Freeze other pre-trained weights'''
for layer in model.layers:
layer.trainable = False
'''Add the modified final layer'''
model.add(Dense(2, activation = 'softmax'))
与我自定义的CNN相比,此方法的工作精度更高.但是花了一些时间训练,我想使用Xception和InceptionV3采取类似的方法,因为它们是更轻便,精度更高的模型.
And this worked marvelously with higher accuracy than my custom built CNN. But it took a while to train and I wanted to take a similar approach using Xception and InceptionV3 since they were lighter models with higher accuracy.
xception_model = keras.applicaitons.xception.Xception()
model = Sequential()
for layer in xception_model.layers:
model_xception.add(layer)
运行上面的代码时,出现以下错误:
When I run the above code, I get the following error:
ValueError: Input 0 is incompatible with layer conv2d_193: expected axis -1 of input shape to have value 64 but got shape (None, None, None, 128)
基本上,我想做与VGG16模型相同的事情;保持其他预训练的权重不变,只需将输出层修改为二进制分类输出,而不是将输出层具有1000个结果即可.我可以看到,与具有相对简单的卷积层结构的VGG16不同,Xception和InceptionV3具有一些我并不100%熟悉的时髦节点,我假设它们是引起问题的原因.如果有人可以帮助您解决问题,将不胜感激!
Basically, I would like to do the same thing as I did with VGG16 model; keep the other pretrained weights as they are and simply modify the output layer to a binary classification output instead of an output layer with 1000 outcomes. I can see that unlike VGG16, which has relatively straightforward convolution layer structure, Xception and InceptionV3 have some funky nodes that I'm not 100% familiar with and I'm assuming those are causing issues. If anyone can help out sort the problem, it'd be much appreciated!
谢谢!
推荐答案
您的代码失败,因为InceptionV3
和Xception
不是Sequential
模型(即,它们包含分支").因此,您不能只是将图层添加到Sequential
容器中.
Your code fails because InceptionV3
and Xception
are not Sequential
models (i.e., they contain "branches"). So you can't just add the layers into a Sequential
container.
现在,由于InceptionV3
和Xception
的顶层都由GlobalAveragePooling2D
层和最后的Dense(1000)
层组成,
Now since the top layers of both InceptionV3
and Xception
consist of a GlobalAveragePooling2D
layer and the final Dense(1000)
layer,
if include_top:
x = GlobalAveragePooling2D(name='avg_pool')(x)
x = Dense(classes, activation='softmax', name='predictions')(x)
如果要删除最终的致密层,则可以在创建这些模型时设置include_top=False
加pooling='avg'
.
if you want to remove the final dense layer, you can just set include_top=False
plus pooling='avg'
when creating these models.
base_model = InceptionV3(include_top=False, pooling='avg')
for layer in base_model.layers:
layer.trainable = False
output = Dense(2, activation='softmax')(base_model.output)
model = Model(base_model.input, output)
这篇关于预先训练Keras Xception和InceptionV3模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!