前提・実現したいこと
keras.applications
のXception
を中間層に追加したモデルをモデル①とします。
以下の2つを実現したいのですが、実現方法を教えていただけないでしょうか。
0. モデル①のXception
の出力層までの新しいモデルを作りたい
(モデル①のXception
以下の層を削除したようなもの)
0. モデル①のXception
の中間層(出力層から6層)までの新しいモデルを作りたい
よろしくお願いいたします。
発生している問題・エラーメッセージ
以下のコードを実行したのですが、以下のエラーが起きました。
AttributeError: Layer xception has multiple inbound nodes, hence the notion of "layer output" is ill-defined. Use `get_output_at(node_index)` instead.
該当のソースコード
Python
1from keras.models import model_from_json, Model, Sequential 2from keras.utils import plot_model 3from keras.applications import Xception 4from keras.layers import Input, Concatenate, Conv2D, Lambda, Dense 5from keras import backend as K 6 7# ベースとなるモデル作成 8base_model = Xception(include_top=False, weights='imagenet', input_shape=(299,299,3), pooling='avg') 9 10# 入力は画像2枚 11inputs1 = Input(shape=(299,299,3)) 12inputs2 = Input(shape=(299,299,3)) 13 14# 入力画像を結合 15x = Concatenate()([inputs1, inputs2]) 16x = Conv2D(3, (3, 3), padding='same', activation='relu')(x) 17 18# 入力とベースモデルを結合 19c = base_model(x) 20 21# L2層と全結合層を付ける 22c = Lambda(lambda xx: 5*(xx)/K.sqrt(K.sum(xx**2)))(c) #metric learning 23c = Dense(2, activation='softmax')(c) 24 25# モデル作成 26model = Model([inputs1, inputs2], c) 27 28# ベースモデルの出力層を出力とした新しいモデルの作成 29# エラー発生箇所 30new_model1 = Model(inputs=model.input, outputs=model.layers[-3].output) 31 32# ベースモデルの中間層を出力とした新しいモデルの作成 33# エラー発生箇所 34new_model2 = Model(inputs=model.input, outputs=model.layers[-3].layers[-6].output)
補足情報(FW/ツールのバージョンなど)
環境は次のとおりです。
Ubuntu 18.04.3 LTS
Python 3.6.8
Keras 2.3.1
tensorflow-gpu 2.0.0
あなたの回答
tips
プレビュー