如何在TensorFlow上可视化已学习的滤波器

12

你可以使用tensorflow debugger工具。 - fabrizioM
1
我认为这就是我在寻找的东西。谢谢!不过,TensorBoard 应该有这个功能。 - Twimnox
可能是如何在Tensorflow中可视化CNN中的权重(变量)?的重复问题。 - Martin Thoma
谢谢!这就是我要找的!谢谢! - Soulduck
1个回答

14

想在Tensorboard中只查看一些conv1滤波器,您可以使用以下代码(适用于cifar10)

# this should be a part of the inference(images) function in cifar10.py file

# conv1
with tf.variable_scope('conv1') as scope:
  kernel = _variable_with_weight_decay('weights', shape=[5, 5, 3, 64],
                                       stddev=1e-4, wd=0.0)
  conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
  biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.0))
  bias = tf.nn.bias_add(conv, biases)
  conv1 = tf.nn.relu(bias, name=scope.name)
  _activation_summary(conv1)

  with tf.variable_scope('visualization'):
    # scale weights to [0 1], type is still float
    x_min = tf.reduce_min(kernel)
    x_max = tf.reduce_max(kernel)
    kernel_0_to_1 = (kernel - x_min) / (x_max - x_min)

    # to tf.image_summary format [batch_size, height, width, channels]
    kernel_transposed = tf.transpose (kernel_0_to_1, [3, 0, 1, 2])

    # this will display random 3 filters from the 64 in conv1
    tf.image_summary('conv1/filters', kernel_transposed, max_images=3)

我还写了一个简单的代码片段,用于以网格形式显示所有 64 个 conv1 过滤器。


你把这段代码放在 cifar 10 脚本的“推理”函数里面吗? - Twimnox
我没有,但那是个好主意 :) 我已经相应地更新了代码。 - etoropov
可以!谢谢!我使用“convert_image_dtype”时出现了一个错误,所以我将tf.image.convert_image_dtype(kernel_0_to_1, dtype=tf.uint8)更改为kernel_0_to_255_uint8 = tf.cast(kernel_0_to_1, dtype=tf.float32) - Twimnox
奇怪 - 我没有任何错误... 好吧,显然Tensorboard也可以可视化范围在[0,1]之间的浮点图像。顺便说一下,kernel_0_to_1已经是浮点型了,所以你的转换是多余的。我更新了代码。 - etoropov
2
它只在conv1中起作用。那么conv2、conv3呢? - m0z4rt
@m0z4rt,从conv2及更深层的角度来可视化滤波器并没有太多意义。其他方法可以用于此目的。话虽如此,您可以修改要点代码以显示N个灰度网格,其中N是输入通道的数量(每个网格显示所有滤波器的一个通道)。 - etoropov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接