Python keras接收错误v3“;基本“模型.获取”层(“自定义”);ValueError:没有这样的图层:自定义
我正在尝试使用inceptionV3预训练模型(在keras应用程序中提供)提取特征。我的代码包含以下区块:Python keras接收错误v3“;基本“模型.获取”层(“自定义”);ValueError:没有这样的图层:自定义,python,tensorflow,keras,classification,Python,Tensorflow,Keras,Classification,我正在尝试使用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) im
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":
~\Anaconda3\lib\site-packages\keras\engine\network.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的新手。好的。。。我想你是在关注,在我看来,这本书是由一个不是真正最伟大的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)
...
更重要的是:如果目标层是最后一层,那么您不需要这些。只需按原样使用base_模型
如果您希望完整的模型末尾有密集
层,只需使用include\u top=True
如果需要自定义数量的类,请告诉模型构造函数
如果您想要一个真正的中间层,请通过调用
model.summary()
找到该层的名称 好的。。。我想你是在关注,在我看来,这本书是由一个不是真正最伟大的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)
...
更重要的是:如果目标层是最后一层,那么您不需要这些。只需按原样使用base_模型
如果您希望完整的模型末尾有密集
层,只需使用include\u top=True
如果需要自定义数量的类,请告诉模型构造函数
如果您想要一个真正的中间层,请通过调用
model.summary()
找到该层的名称 您想做什么?@DanielMöller->预训练的模型从Keras库的应用模块加载,模型是基于conf.json文件中用户指定的配置构建的。然后,在使用ImageNet数据集预先训练的模型中,从用户指定的层中提取特征。这些功能及其标签使用HDF5文件格式存储在本地。此外,保存模型和权重只是为了表明这些也可以在Keras中完成。这就是我想要实现的。InceptionV2
是由Keras严格构建的预构建模型。模型中没有conf.json,也没有“自定义”层。@DanielMöller-如上面的注释所述,conf.json是用户指定的。感谢您提供有关海关的信息。我见过一些人在使用“自定义”代码进行InceptionResNetV2、inceptionV3和MobileNet时,你想做什么?@DanielMöller->预先训练的模型是从Keras库的应用模块加载的,模型是基于conf.json文件中用户指定的配置构建的。然后,在使用ImageNet数据集预先训练的模型中,从用户指定的层中提取特征。这些功能及其标签使用HDF5文件格式存储在本地。此外,保存模型和权重只是为了表明这些也可以在Keras中完成。这就是我想要实现的。InceptionV2
是由Keras严格构建的预构建模型。模型中没有conf.json,也没有“自定义”层。@DanielMöller-如上面的注释所述,conf.json是用户指定的。感谢您提供有关海关的信息。我见过一些人在使用“自定义”代码进行InceptionResNetV2、inceptionV3和MobileNet时使用的代码
lastLayer = base_model.layers[-1]