如何在 Tensorflow 中 Display/Visualize 卷积和 Relu 后的图像

How to Display/Visualize Images after Convolution and Relu in Tensorflow

我的输入图像和第一个卷积层和 Relu 层有这段代码(在测试期间,不是训练期间):

convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, IMAGE_CHANNELS], name='input')
convnet1 = conv_2d(convnet, FIRST_NUM_CHANNEL, FILTER_SIZE, activation='relu')
convnet1 = max_pool_2d(convnet1, FILTER_SIZE)

如果我打印变量 convnet1,我得到这个结果 Tensor("MaxPool2D/MaxPool:0", shape=(?, 52, 52, 32), dtype=float32)这是正确的,因为我的输入图像是 256x256,过滤器大小是 5x5。

我的问题是如何可视化我的 convnet1 data/variable?它有 32 个通道,所以我假设我可以显示 32 张尺寸为 52x52 的黑白图像。

如果你想在一个图中打印 32 个,你可以这样做

def plot_convnet(convnet, input_num=0):
    # since convnet1 is 4dim (?,52,52,32) Assuming the first dim is Batch size you 
    # can plot the 32 channels of a single image from the batch given by input_num

    C = Session.run(convnet) # remove the session run if the tensor is already 
                             #evaluated

    # Number of channels -- 32 in your case 
    num_chnls = C.shape[3]

    # Number of grids to plot.
    # Rounded-up, square-root of the number of channels
    grids = math.ceil(math.sqrt(num_chnls))

    #Create figure with a grid of sub-plots.
    fig, axes = plt.subplots(grids, grids)

    for i, ax in enumerate(axes.flat):
       if i<num_chnls:
           im = C[input_num,:, :,  i]
           #Plot image.
           ax.imshow(im,                  
                     interpolation='nearest', cmap='seismic')
    plt.show()