尝试从经过训练的顺序模型构建新模型输出时出现图形断开错误

时间:2020-06-03 06:02:40

标签: python tf.keras tensorflow2.x efficientnet

我有一个训练有素的顺序模型,该模型由预先训练的无头高效网和最后一层组成。 model.summary()如下所示,

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
efficientnet-b3 (Model)      (None, 5, 5, 1536)        10783528  
_________________________________________________________________
gap (GlobalMaxPooling2D)     (None, 1536)              0         
_________________________________________________________________
dropout_out (Dropout)        (None, 1536)              0         
_________________________________________________________________
fc_out (Dense)               (None, 1)                 1537      
=================================================================
Total params: 10,785,065
Trainable params: 1,479,937
Non-trainable params: 9,305,128
_________________________________________________________________

我尝试构建一个模型,该模型使用来输出efficiencynet-b3模型的预测输出和最后一个卷积层输出,

gradModel = Model(
inputs=[model.inputs],
outputs=[model.layers[0].layers[-3].output, model.output])

但是我得到了错误,

ValueError: Graph disconnected: cannot obtain value for tensor Tensor("input_1:0", shape=(None, 150, 150, 3), dtype=float32) at layer "input_1". The following previous layers were accessed without issue: []

我应该怎么解决这个问题?

我的efficiencynet-b3模型看起来像

Model: "efficientnet-b3"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_1 (InputLayer)            [(None, 150, 150, 3) 0                                            
__________________________________________________________________________________________________
conv2d (Conv2D)                 (None, 75, 75, 40)   1080        input_1[0][0]                    
__________________________________________________________________________________________________   
.
.
.
__________________________________________________________________________________________________  
add_18 (Add)                    (None, 5, 5, 384)    0           drop_connect_18[0][0]            
                                                                 batch_normalization_73[0][0]     
__________________________________________________________________________________________________
conv2d_103 (Conv2D)             (None, 5, 5, 1536)   589824      add_18[0][0]                     
__________________________________________________________________________________________________
batch_normalization_77 (BatchNo (None, 5, 5, 1536)   6144        conv2d_103[0][0]                 
__________________________________________________________________________________________________
swish_77 (Swish)                (None, 5, 5, 1536)   0           batch_normalization_77[0][0]     
==================================================================================================
Total params: 10,783,528
Trainable params: 1,478,400
Non-trainable params: 9,305,128
__________________________________________________________________________________________________

0 个答案:

没有答案