模型的kerasvis可视化

2024-04-26 01:33:10 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的模型架构中可视化conv层有一个问题,因为keras-vis似乎无法处理模型架构中的模型。我想在vgg19转换模型中可视化conv层。但我无法访问它们。你知道吗

有没有人知道如何在模型架构中可视化模型中的conv层,甚至知道如何“展平”vgg19模型以便我可以访问所有层。这似乎是一个开放的问题keras-vis和我需要我的学士学位论文的可视化。你知道吗

_________________________________________________________________
Layer (type)                 Output Shape              Param #  
========================================
input_1 (InputLayer)         (None, 120, 160, 1)       0        
_________________________________________________________________
lambda_1 (Lambda)            (None, 120, 160, 3)       0        
_________________________________________________________________
batch_normalization_1 (Batch (None, 120, 160, 3)       12       
_________________________________________________________________
vgg19 (Model)                multiple                  20024384 
_________________________________________________________________
flatten_1 (Flatten)          (None, 7680)              0        
_________________________________________________________________
dense_1 (Dense)              (None, 80)                614480   
_________________________________________________________________
dropout_1 (Dropout)          (None, 80)                0        
_________________________________________________________________
dense_2 (Dense)              (None, 4800)              388800   
_________________________________________________________________
reshape_1 (Reshape)          (None, 60, 80)            0        
========================================
Total params: 21,027,676
Trainable params: 1,003,286
Non-trainable params: 20,024,390
_________________________________________________________________
None

VGG19模型由以下几层组成,我无法访问!你知道吗

['input_2', 'block1_conv1', 'block1_conv2', 'block1_pool', 'block2_conv1', 'block2_conv2', 'block2_pool', 'block3_conv1', 'block3_conv2', 'block3_conv3', 'block3_conv4', 'block3_pool', 'block4_conv1', 'block4_conv2', 'block4_conv3', 'block4_conv4', 'block4_pool', 'block5_conv1', 'block5_conv2', 'block5_conv3', 'block5_conv4', 'block5_pool']
model = load_model('/home/marco/HyperOptBachelor/vgg19_final100epochs/weights.hdf5', custom_objects={'motion_metric': motion_metric})
print(model.summary())
print([layer.name for layer in model.get_layer('vgg19').layers])
layer_idx = utils.find_layer_idx(model, 'block5_conv4')
# This is just a teacher frame from my test dataset, but it can to test it any image
img = utils.load_img('/home/marco/HyperOptBachelor/vgg19_final100epochs/out/test-96-teacher.png')
f, ax = plt.subplots(1,1)
grads = visualize_saliency(model,-5,filter_indices=10,seed_input=img)
ax[1].imshow(grads, cmap='jet')

当我尝试获取vgg19模型中特定层的索引时,会出现以下错误:

> Traceback (most recent call last):
  File "/home/marco/PycharmProjects/ideal-system/helper_scripts/visualization.py", line 11, in <module>
    layer_idx = utils.find_layer_idx(model, 'block5_conv4')
  File "/home/marco/bachelor/lib/python3.5/site-packages/vis/utils/utils.py", line 164, in find_layer_idx
    raise ValueError("No layer with name '{}' within the model".format(layer_name))
ValueError: No layer with name 'block5_conv4' within the model

所以它找不到那个层,因为只有模型,我猜没有列出所有的层。如果我只是按索引调用vgg19模型,它会抛出一个错误,即该模型有多个输出节点。我在网上搜索了一下,这似乎是一个悬而未决的问题。你知道吗


Tags: 模型nonelayermodel可视化utilspoolidx