RuntimeError:可视化CNN层时libpng表示错误

时间:2019-02-25 13:04:50

标签: matplotlib keras

我正在用keras可视化cnn层。可视化效果显示在mnist测试图像上。The model summary is here 可视化代码如下:

layer_names = []
for layer in model.layers[:12]:
    layer_names.append(layer.name) # Names of the layers, so you can have them as part of your plot

images_per_row = 16

for layer_name, layer_activation in zip(layer_names, activations): # Displays the feature maps
    n_features = layer_activation.shape[-1] # Number of features in the feature map
    size = layer_activation.shape[1] #The feature map has shape (1, size, size, n_features).
    n_cols = n_features // images_per_row # Tiles the activation channels in this matrix
    display_grid = np.zeros((size * n_cols, images_per_row * size))
    for col in range(n_cols): # Tiles each filter into a big horizontal grid
        for row in range(images_per_row):
            channel_image = layer_activation[0,
                                         :, :,
                                         col * images_per_row + row]
            channel_image -= channel_image.mean() # Post-processes the feature to make it visually palatable
            channel_image /= channel_image.std()
            channel_image *= 64
            channel_image += 128
            channel_image = np.clip(channel_image, 0, 255).astype('uint8')
            display_grid[col * size : (col + 1) * size, # Displays the grid
                     row * size : (row + 1) * size] = channel_image
    scale = 1. / size
    plt.figure(figsize=(scale * display_grid.shape[1],
                    scale * display_grid.shape[0]))
    plt.title(layer_name)
    plt.grid(False)
    plt.imshow(display_grid, aspect='auto', cmap='viridis')

此代码可视化前两层的输出,并显示带有滤镜的图像。但是在第三层中,它会引发如下错误:

RuntimeError: libpng signaled error

<Figure size 1152x0 with 1 Axes>

我尝试卸载并重新安装matplotlib,但仍然无法正常工作。

1 个答案:

答案 0 :(得分:0)

这是一个逻辑错误:

if (this.platform.is("android")) {
    this.platform.backButton.subscribe(() => {
        if (window.location.pathname === "/home") {
            navigator['app'].exitApp();
        }
    });
}

暗示<Figure size 1152x0 with 1 Axes> 只有在此行中将scale * display_grid.shape[0] == 0设置为零时才会发生:

n_cols

n_cols = n_features // images_per_row 为<n_features引起。

应该有一个更好的错误in future versions of matplotlib