i have two models:
first one is mobile_net, which is a mobilenet assign with input shape of 192,192,3, and output shape of 6,6,1280.it contains pretrain weights.
second model is model_0 assign with input shape of 6,6,1280, which has two layers of GlobalAveragePooling2D and Dense.( model_0 is well trianed)
now i connect the mobile_net and model_0 together, hoping the data will flow through.
I finally assembled the two model successfully, the assembled model is named model_1
But when i extract some layer outputs of the model_1, wired things happened~~
i can get the -4 layer output successfully
i can get the -3 layer output successfully
i can not get the -2 layer output
i can not get the -1 layer output
the demo code is here
inputs=tf.keras.layers.Input([6,6,1280])
x=tf.keras.layers.GlobalAveragePooling2D(
name=‘global_average_pooling2d_0’)(inputs)
y=tf.keras.layers.Dense(5,name=‘dense_0’)(x)
model_0 = tf.keras.Model(inputs,y,name=‘model_0’)
mobile_net = tf.keras.applications.MobileNetV2(
input_shape=(192,192,3),
include_top=False,
weights=‘imagenet’)
assert mobile_net.output_shape==(None, 6, 6, 1280)
y1=model_0.get_layer(‘global_average_pooling2d_0’)(mobile_net.output)
y2=model_0.get_layer(‘dense_0’)(y1)
model_1 = tf.keras.Model(mobile_net.input,y2,name=‘assembled_model’)
tmp=tf.keras.Model(model_1.input,model_1.layers[-3].output) # successfully
tmp=tf.keras.Model(model_1.input,model_1.layers[-2].output) # failed
tmp=tf.keras.Model(model_1.input,model_1.layers[-1].output) # failed
the failed outputs are
ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 6, 6, 1280), dtype=tf.float32, name=‘input_12’), name=‘input_12’, description=“created by layer ‘input_12’”) at layer “global_average_pooling2d_0”. The following previous layers were accessed without issue:
ValueError: Graph disconnected: cannot obtain value for tensor KerasTensor(type_spec=TensorSpec(shape=(None, 6, 6, 1280), dtype=tf.float32, name=‘input_12’), name=‘input_12’, description=“created by layer ‘input_12’”) at layer “global_average_pooling2d_0”. The following previous layers were accessed without issue: