【问题标题】:How to Display/Visualize Images after Convolution and Relu in Tensorflow如何在 Tensorflow 中显示/可视化卷积和 Relu 后的图像
【发布时间】:2019-04-03 05:03:24
【问题描述】:

我的输入图像和第一个卷积和 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 数据/变量?它有 32 个通道,所以我假设我可以显示 32 个尺寸为 52x52 的黑白图像。

【问题讨论】:

  • 您可以对每个通道的值进行平均,并显示生成的 52x52 图像。
  • 是的,我可以这样做,这样我就可以生成一张图像,但我不知道如何编写代码......我做了一些重塑,但它有错误。
  • 检查尺寸非常关键。一开始可能会很烦人,要找出需要压扁的地方等等,但有了经验,一切都会得到解决。

标签: python tensorflow conv-neural-network visualization convolution


【解决方案1】:

如果你想在一个图中打印 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()

【讨论】:

  • 在 ax.imshow(img, vmin=w_min, vmax=w_max, interpolation='nearest', cmap='seismic') 行中,变量 'img' 来自哪里?还是“我”?
  • 我已经编辑了我的答案,这是一个错字img 应该是im,它在上面的行中定义。
  • 嗨@D.negn,我如何在 C = Session.run(convnet) 中导入 Session?它总是给我一个错误 NameError: name 'Session' is not defined。如果我使用 tf.Session() 声明另一个会话,那么它也会给我一个错误,因为它不属于同一个图。
  • 顺便说一下,我通过 model = tflearn.DNN(convnet_final, tensorboard_dir='log') model.load(MODEL_NAME) 运行并加载我的模型
  • 张量 convnet1 打印为 Tensor("MaxPool2D/MaxPool:0", shape=(?, 52, 52, 32), dtype=float32) 需要使用 Session.run()Tensor.eval() 进行评估。因此,您可以在 Session 中对其进行评估,从 Session 内部调用函数 plot_convnet 并从函数中删除行 C = Session.run(convnet)
猜你喜欢
  • 2017-01-14
  • 1970-01-01
  • 2016-05-31
  • 2023-03-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-17
  • 2016-05-27
相关资源
最近更新 更多