Keras-所有图层名称应唯一 [英] Keras - All layer names should be unique

查看:178
本文介绍了Keras-所有图层名称应唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将两个VGG网络在keras中组合在一起以进行分类任务.当我运行该程序时,它显示一个错误:

I combine two VGG net in keras together to make classification task. When I run the program, it shows an error:

RuntimeError:名称"predictions"在模型中使用了2次.所有图层名称都应该是唯一的.

RuntimeError: The name "predictions" is used 2 times in the model. All layer names should be unique.

我很困惑,因为我在代码中只使用过一次prediction层:

I was confused because I only use prediction layer once in my code:

from keras.layers import Dense
import keras
from keras.models import  Model
model1 = keras.applications.vgg16.VGG16(include_top=True, weights='imagenet',
                                input_tensor=None, input_shape=None,
                                pooling=None,
                                classes=1000)
model1.layers.pop()

model2 =  keras.applications.vgg16.VGG16(include_top=True, weights='imagenet',
                                input_tensor=None, input_shape=None,
                                pooling=None,
                                classes=1000)
model2.layers.pop()
for layer in model2.layers:
    layer.name = layer.name + str("two")
model1.summary()
model2.summary()
featureLayer1 = model1.output
featureLayer2 = model2.output
combineFeatureLayer = keras.layers.concatenate([featureLayer1, featureLayer2])
prediction = Dense(1, activation='sigmoid', name='main_output')(combineFeatureLayer)

model = Model(inputs=[model1.input, model2.input], outputs= prediction)
model.summary()

感谢@putonspectacles的帮助,我按照他的指示进行了操作,找到了一些有趣的部分.如果使用model2.layers.pop()并使用"model.layers.keras.layers.concatenate([model1.output, model2.output])"组合两个模型的最后一层,则会发现使用model.summary()仍显示最后一层信息.但是实际上它们不存在于结构中.因此,可以使用model.layers.keras.layers.concatenate([model1.layers[-1].output, model2.layers[-1].output]).它看起来很棘手,但确实有效..我认为这是有关日志和结构同步的问题.

Thanks for @putonspectacles help, I follow his instruction and find some interesting part. If you use model2.layers.pop() and combine the last layer of two models using "model.layers.keras.layers.concatenate([model1.output, model2.output])", you will find that the last layer information is still showed using the model.summary(). But actually they do not exist in the structure. So instead, you can use model.layers.keras.layers.concatenate([model1.layers[-1].output, model2.layers[-1].output]). It looks tricky but it works.. I think it is a problem about synchronization of the log and structure.

推荐答案

首先,根据您发布的代码,您有 no 层,其名称属性为"predictions",因此此错误没有任何作用做你的图层 Denseprediction:即:

First, based on the code you posted you have no layers with a name attribute 'predictions', so this error has nothing to do with your layer Dense layer prediction: i.e:

prediction = Dense(1, activation='sigmoid', 
             name='main_output')(combineFeatureLayer)

VGG16 模型具有具有name predictionsDense层.特别是这一行:

The VGG16 model has a Dense layer with name predictions. In particular this line:

x = Dense(classes, activation='softmax', name='predictions')(x)

由于您正在使用其中两个模型,因此您的图层具有重复的名称.

And since you're using two of these models you have layers with duplicate names.

您可以做的是将第二个模型中的图层重命名为除预测之外的其他名称,例如predictions_1,如下所示:

What you could do is rename the layer in the second model to something other than predictions, maybe predictions_1, like so:

model2 =  keras.applications.vgg16.VGG16(include_top=True, weights='imagenet',
                                input_tensor=None, input_shape=None,
                                pooling=None,
                                classes=1000)

# now change the name of the layer inplace.
model2.get_layer(name='predictions').name='predictions_1'

这篇关于Keras-所有图层名称应唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆