【问题标题】:Visualizing Feature maps: IndexError: too many indices for array可视化特征图:IndexError:数组索引过多
【发布时间】:2020-11-10 16:45:55
【问题描述】:

按照本教程,我正在尝试可视化特征图。

我的模型如下所示:

model.summary()
Model: "model_3"
__________________________________________________________________________________________________
Layer (type)                    Output Shape         Param #     Connected to                     
==================================================================================================
input_5 (InputLayer)            [(None, 224, 224, 3) 0                                            
__________________________________________________________________________________________________
efficientnet-b0 (Functional)    (None, 7, 7, 1280)   4049564     input_5[0][0]                    
__________________________________________________________________________________________________
flatten_4 (Flatten)             (None, 62720)        0           efficientnet-b0[0][0]            
__________________________________________________________________________________________________
branch_0_Dense_16000 (Dense)    (None, 256)          16056576    flatten_4[0][0]                  
__________________________________________________________________________________________________
branch_1_Dense_16000 (Dense)    (None, 256)          16056576    flatten_4[0][0]                  
__________________________________________________________________________________________________
branch_2_Dense_16000 (Dense)    (None, 256)          16056576    flatten_4[0][0]                  
__________________________________________________________________________________________________
branch_3_Dense_16000 (Dense)    (None, 256)          16056576    flatten_4[0][0]                  
__________________________________________________________________________________________________
branch_4_Dense_16000 (Dense)    (None, 256)          16056576    flatten_4[0][0]                  
__________________________________________________________________________________________________
branch_5_Dense_16000 (Dense)    (None, 256)          16056576    flatten_4[0][0]                  
__________________________________________________________________________________________________
branch_6_Dense_16000 (Dense)    (None, 256)          16056576    flatten_4[0][0]                  
__________________________________________________________________________________________________
branch_0_output (Dense)         (None, 35)           8995        branch_0_Dense_16000[0][0]       
__________________________________________________________________________________________________
branch_1_output (Dense)         (None, 35)           8995        branch_1_Dense_16000[0][0]       
__________________________________________________________________________________________________
branch_2_output (Dense)         (None, 35)           8995        branch_2_Dense_16000[0][0]       
__________________________________________________________________________________________________
branch_3_output (Dense)         (None, 35)           8995        branch_3_Dense_16000[0][0]       
__________________________________________________________________________________________________
branch_4_output (Dense)         (None, 35)           8995        branch_4_Dense_16000[0][0]       
__________________________________________________________________________________________________
branch_5_output (Dense)         (None, 35)           8995        branch_5_Dense_16000[0][0]       
__________________________________________________________________________________________________
branch_6_output (Dense)         (None, 35)           8995        branch_6_Dense_16000[0][0]       
__________________________________________________________________________________________________
concatenate_4 (Concatenate)     (None, 245)          0           branch_0_output[0][0]            
                                                                 branch_1_output[0][0]            
                                                                 branch_2_output[0][0]            
                                                                 branch_3_output[0][0]            
                                                                 branch_4_output[0][0]            
                                                                 branch_5_output[0][0]            
                                                                 branch_6_output[0][0]            
__________________________________________________________________________________________________
reshape_4 (Reshape)             (None, 7, 35)        0           concatenate_4[0][0]              
==================================================================================================
Total params: 116,508,561
Trainable params: 116,466,545
Non-trainable params: 42,016

我现在想可视化索引为 10 的层:10 branch_0_output (None, 35)

3 branch_0_Dense_16000 (None, 256)
4 branch_1_Dense_16000 (None, 256)
5 branch_2_Dense_16000 (None, 256)
6 branch_3_Dense_16000 (None, 256)
7 branch_4_Dense_16000 (None, 256)
8 branch_5_Dense_16000 (None, 256)
9 branch_6_Dense_16000 (None, 256)
10 branch_0_output (None, 35)
11 branch_1_output (None, 35)
12 branch_2_output (None, 35)
13 branch_3_output (None, 35)
14 branch_4_output (None, 35)
15 branch_5_output (None, 35)
16 branch_6_output (None, 35)

我按照教程中所述的代码对图像进行了预处理,现在我想绘制该层的 35 个(?)特征图: 我使用教程中的代码并修改了平方数,这里是1但我尝试了几个:

# plot all 35 maps
square = 1
ix = 1
for _ in range(square):
    for _ in range(square):
        # specify subplot and turn of axis
        ax = pyplot.subplot(square, square, ix)
        ax.set_xticks([])
        ax.set_yticks([])
        # plot filter channel in grayscale
        pyplot.imshow(feature_maps[0, :, :, ix-1], cmap='gray')
        ix += 1
# show the figure
pyplot.show()

与我尝试的号码无关,我收到此错误消息:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-28-4c1f464f6978> in <module>()
      9                 ax.set_yticks([])
     10                 # plot filter channel in grayscale
---> 11                 pyplot.imshow(feature_maps[0, :, ix-1], cmap='gray')
     12                 ix += 1
     13 # show the figure

IndexError: too many indices for array

有人可以帮助我修改什么吗?

非常感谢!

【问题讨论】:

    标签: python matplotlib error-handling conv-neural-network index-error


    【解决方案1】:

    错误在第 11 行显示 too many indices for array。这是因为您在特征图中错误地传递了索引。在这里,您尝试在 1*1 网格中绘制 35 张地图,因为您已给出 square = 1。

    假设你需要绘制64的地图,那么我们就取square = 8,那么输出将是一个8*8的网格。

    【讨论】:

    • 好的,这意味着在我的情况下它应该是feature_maps[0:17, 18:35]?或者你会推荐什么?
    • 我已经编辑了我的答案,请参考看看是否可行。
    猜你喜欢
    • 2020-09-26
    • 2019-06-07
    • 2016-09-10
    • 2019-07-17
    • 2020-09-14
    • 2018-05-23
    • 1970-01-01
    相关资源
    最近更新 更多