如何在Keras中访问已加载的multi_gpu模型的图层?

时间:2019-06-13 19:57:17

标签: python-3.x tensorflow keras keras-layer tf.keras

我有一个深度学习模型,我想介绍其层次this article。我想可视化测试图像上的激活。但是,我使用多个GPU进行训练并保存最佳训练点。因此,当我在加载的模型上点击model.summary()时,而不是传统的体系结构输出,我得到了它,我无法使用:

__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_2 (InputLayer)            (None, 256, 256, 3)  0                                            
__________________________________________________________________________________________________
lambda_1 (Lambda)               (None, 256, 256, 3)  0           input_2[0][0]                    
__________________________________________________________________________________________________
lambda_2 (Lambda)               (None, 256, 256, 3)  0           input_2[0][0]                    
__________________________________________________________________________________________________
lambda_3 (Lambda)               (None, 256, 256, 3)  0           input_2[0][0]                    
__________________________________________________________________________________________________
lambda_4 (Lambda)               (None, 256, 256, 3)  0           input_2[0][0]                    
__________________________________________________________________________________________________
lambda_5 (Lambda)               (None, 256, 256, 3)  0           input_2[0][0]                    
__________________________________________________________________________________________________
lambda_6 (Lambda)               (None, 256, 256, 3)  0           input_2[0][0]                    
__________________________________________________________________________________________________
lambda_7 (Lambda)               (None, 256, 256, 3)  0           input_2[0][0]                    
__________________________________________________________________________________________________
lambda_8 (Lambda)               (None, 256, 256, 3)  0           input_2[0][0]                    
__________________________________________________________________________________________________
model_2 (Model)                 (None, 256, 256, 1)  31032837    lambda_1[0][0]                   
                                                                 lambda_2[0][0]                   
                                                                 lambda_3[0][0]                   
                                                                 lambda_4[0][0]                   
                                                                 lambda_5[0][0]                   
                                                                 lambda_6[0][0]                   
                                                                 lambda_7[0][0]                   
                                                                 lambda_8[0][0]                   
__________________________________________________________________________________________________
conv2d_48 (Concatenate)         (None, 256, 256, 1)  0           model_2[1][0]                    
                                                                 model_2[2][0]                    
                                                                 model_2[3][0]                    
                                                                 model_2[4][0]                    
                                                                 model_2[5][0]                    
                                                                 model_2[6][0]                    
                                                                 model_2[7][0]                    
                                                                 model_2[8][0]                    
==================================================================================================
Total params: 31,032,837
Trainable params: 31,032,837
Non-trainable params: 0

如何检索/公开已保存的多GPU模型的权重和体系结构?可以这么说,是否有办法将其“恢复正常”?

谢谢!

1 个答案:

答案 0 :(得分:1)

使用model = load_model('model.h5')model.load_weights(load_path)加载模型后,只需执行

single_model = model.layers[-2]

然后,您可以使用model.layers[i]或通过迭代访问所需的图层

for layer in model.layers:
    #do smth