【问题标题】:How do I store an intermediate convolutional layer's result in tensorflow for later processing?如何将中间卷积层的结果存储在 tensorflow 中以供以后处理?
【发布时间】:2018-12-23 12:18:47
【问题描述】:

下图描述了 CNN 的单个中间过滤器层的最大池化层应用之前的输出。 我想存储强度为 4 的像素的坐标(在箭头 LHS 上矩阵的右下角),因为它在箭头 LHS 上的矩阵中。也就是说,右侧矩阵中坐标 (4,4)(基于 1 的索引)处的像素是存储在右箭头右侧矩阵右下角单元格中的像素。现在我想要做的是存储这个坐标值 (4,4) 以及其他像素的坐标 {(2,2) 用于强度为 6 的像素,(2, 4) 用于强度为像素8 和 (3, 1) 用于强度为 3 的像素} 作为后续处理的列表。我如何在 Tensorflow 中做到这一点。

使用大小为 2 x 2 且步幅为 2 的过滤器完成最大池化

【问题讨论】:

    标签: tensorflow machine-learning neural-network computer-vision conv-neural-network


    【解决方案1】:

    您可以使用tf.nn.max_pool_with_argmax (link)。 注意:

    argmax 中的索引是扁平的,因此最大值位于 位置 [b, y, x, c] 变为扁平索引 ((b * height + y) * 宽度 + x) * 通道 + c.

    我们需要进行一些处理以使其适合您的坐标。 一个例子:

    import tensorflow as tf
    import numpy as np
    
    def max_pool_with_argmax(net,filter_h,filter_w,stride):
        output, mask = tf.nn.max_pool_with_argmax( net,ksize=[1, filter_h, filter_w, 1],
                                                strides=[1, stride, stride, 1],padding='SAME')
    
        # If your ksize looks like [1, stride, stride, 1]
        loc_x = mask // net.shape[2]
        loc_y = mask % net.shape[2]
        loc = tf.concat([loc_x+1,loc_y+1],axis=-1) #count from 0 so add 1
    
        # If your ksize is all changing, use the following
        # c = tf.mod(mask,net.shape[3])
        # remain = tf.cast(tf.divide(tf.subtract(mask,c),net.shape[3]),tf.int64)
        # x = tf.mod(remain,net.shape[2])
        # remain = tf.cast(tf.divide(tf.subtract(remain,x),net.shape[2]),tf.int64)
        # y = tf.mod(remain,net.shape[1])
        # remain = tf.cast(tf.divide(tf.subtract(remain, y), net.shape[1]),tf.int64)
        # b = tf.mod(remain, net.shape[0])
        # loc = tf.concat([y+1,x+1], axis=-1)
        return output,loc
    
    input = tf.Variable(np.random.rand(1, 6, 4, 1), dtype=np.float32)
    output, mask = max_pool_with_argmax(input,2,2,2)
    
    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        input_value,output_value,mask_value = sess.run([input,output,mask])
        print(input_value[0,:,:,0])
        print(output_value[0,:,:,0])
        print(mask_value[0,:,:,:])
    
    #print
    [[0.20101677 0.09207255 0.32177696 0.34424785]
     [0.4116488  0.5965447  0.20575707 0.63288754]
     [0.3145412  0.16090539 0.59698933 0.709239  ]
     [0.00252096 0.18027237 0.11163216 0.40613824]
     [0.4027637  0.1995668  0.7462126  0.68812144]
     [0.8993007  0.55828506 0.5263306  0.09376772]]
    [[0.5965447  0.63288754]
     [0.3145412  0.709239  ]
     [0.8993007  0.7462126 ]]
    [[[2 2]
      [2 4]]
    
     [[3 1]
      [3 4]]
    
     [[6 1]
      [5 3]]]
    

    对于强度为 0.5965447 的像素,您可以看到 (2,2),对于强度为 0.63288754 的像素,您可以看到 (2, 4),依此类推。

    【讨论】:

    • 如果我的 ksize 看起来像 [1,filter_h,filter_w,1] 怎么办?
    • @AnubhavPandey 你需要做的是添加参数。当您的 ksize 看起来像 [1,filter_h,filter_w,1] 时,我已经在答案中更改了它。
    【解决方案2】:

    假设您有以下最大池化层:

    pool_layer= tf.nn.max_pool(conv_output,
                               ksize=[1, 2, 2, 1],
                               strides=[1, 2, 2, 1],
                               padding='VALID')
    

    你可以使用:

    max_pos = tf.gradients([pool_layer], [conv_output])[0]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-09-10
      • 1970-01-01
      • 2011-05-06
      • 2012-12-12
      • 2017-10-21
      • 2020-09-12
      • 1970-01-01
      • 2011-04-13
      相关资源
      最近更新 更多