前言:當在一個baseline上做修改(改變原有結構,增/減一些自定義網絡),模型重載并獲得輸入輸出的時候容易出一些問題,就比如這個error:
AttributeError: Layer model has multiple inbound nodes,
hence the notion of “layer output” is ill-defined.
Use
instead.
get_output_at(node_index)
這個問題主要由于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