我正在尝试使用inceptionV3预训练模型(在keras应用程序中提供(提取特征。我的代码有以下块:
base_model = InceptionV3(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3)))
model = Model(input=base_model.input, output=base_model.get_layer('custom').output)
image_size = (299, 299)
当我运行这个时,它会给出以下错误:
ValueError Traceback (most recent call last)
<ipython-input-24-fa1f85b62b84> in <module>()
20 elif model_name == "inceptionv3":
21 base_model = InceptionV3(include_top=include_top, weights=weights, input_tensor=Input(shape=(299,299,3)))
---> 22 model = Model(input=base_model.input, output=base_model.get_layer('custom').output)
23 image_size = (299, 299)
24 elif model_name == "inceptionresnetv2":
~Anaconda3libsite-packageskerasenginenetwork.py in get_layer(self, name, index)
362 """Retrieves the model's updates.
363
--> 364 Will only include updates that are either
365 unconditional, or conditional on inputs to this model
366 (e.g. will not include updates that depend on tensors
ValueError: No such layer: custom
我已经尝试完全卸载并重新安装Keras。另外,我在inceptionV3.py文件(在keras应用程序文件夹中(中读到了以下内容:
from ..layers import Flatten
我在进口中添加了这个。仍然没有运气。有人能帮我吗?我是喀拉拉邦的新手。
好的。。。我想你正在学习这个教程,在我看来,这个教程是由一个不是最伟大的Keras用户编写的。
引用的自定义层是由教程在更改keras源代码时创建的(请不要这样做,这不是一种安全的工作方式,会在您未来的项目中制造麻烦(
自定义层是在教程的这一部分创建的:
`Add in "<model>.py"
...
...
if include_top:
# Classification block
x = GlobalAveragePooling2D(name='avg_pool')(x)
x = Dense(classes, activation='softmax', name='predictions')(x)
else:
if pooling == 'avg':
x = GlobalAveragePooling2D()(x)
elif pooling == 'max':
x = GlobalMaxPooling2D()(x)
x = Flatten(name='custom')(x)
...
评论:
- 这完全没有必要。全球联营的产出已经趋于平稳
- 这对您未来的项目是危险的,因为您正在更改keras源代码
只需取模型的最后一层:,就可以在不创建平坦层的情况下完成完全相同的操作
lastLayer = base_model.layers[-1]
更重要的是:如果目标层是最后一层,则不需要任何这些。按原样使用base_model
即可。
如果您想要最后有Dense
层的完整模型,只需使用include_top=True
即可。
如果您想要自定义数量的类,请将其告知模型构造函数。
如果您想要一个真正的中间层,请通过调用model.summary()
找到层的名称。