AttributeError : Layer model has multiple inbound nodes

前言:当在一个baseline上做修改(改变原有结构,增/减一些自定义网络),模型重载并获得输入输出的时候容易出一些问题,就比如这个error:
AttributeError: Layer model has multiple inbound nodes,
hence the notion of “layer output” is ill-defined.
Use get_output_at(node_index) instead.

这个问题主要由于TF的图造成的,比如下述对网络的定义,仅仅是将原有的dense layer进行了全连接和激活的分解:

# model define
base_model = load_model(weight_path)

out = base_model.layers[-2].output

predictions=layers.Dense(classes)(out)

predictions=layers.Activation('softmax')(predictions)

model=Model(base_model.input,predictions)

# call function, throwing error
dense_out = K.function([model.ibput], [model.output)

AttributeError: Layer model has multiple inbound nodes,
hence the notion of "layer input" is ill-defined.
Use `get_input_at(node_index)` instead.

可以认为模型在load_model的时候定义了一套输入和输出,当再重新定义一个Model的时候也会注册一套输入和输出,因此在使用的时候会造成上述的错误,无法再多个节点中定位给到你想要的那一个,因此要显示指定。

方式一:
# call function
dense_out = K.function([model.get_input_at(0)], 
								[model.get_output_at(0))

当你不太确定 node index 是 0 还是 1 的时候,可以尝试下,进行一张图像的输入和输出,并可以知道正确的索引(如果更好的确认方式可以评论区留言)

方式二:更为显示的指定:
# call function
last_dense_layer = model.layers[-1]
dense_out = K.function([model.get_input_at(0)], 
								[last_dense_layer.output)

参考资料-1
参考资料-2

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值