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