如何在Tensorflow中可视化cnn中的权重(变量)?

Wuc*_*hen 41 python tensorflow

在训练cnn模型后,我想要显示重量或打印出重量,我该怎么办?我甚至无法在训练后打印出变量.谢谢!

mrr*_*rry 34

要显示权重,您可以使用tf.image_summary()op将卷积过滤器(或过滤器的一部分)转换为摘要原型,使用a将它们写入日志tf.train.SummaryWriter,并使用TensorBoard可视化日志.

假设你有以下(简化)程序:

filter = tf.Variable(tf.truncated_normal([8, 8, 3]))
images = tf.placeholder(tf.float32, shape=[None, 28, 28])

conv = tf.nn.conv2d(images, filter, strides=[1, 1, 1, 1], padding="SAME")

# More ops...
loss = ...
optimizer = tf.GradientDescentOptimizer(0.01)
train_op = optimizer.minimize(loss)

filter_summary = tf.image_summary(filter)

sess = tf.Session()
summary_writer = tf.train.SummaryWriter('/tmp/logs', sess.graph_def)
for i in range(10000):
  sess.run(train_op)
  if i % 10 == 0:
    # Log a summary every 10 steps.
    summary_writer.add_summary(filter_summary, i)
Run Code Online (Sandbox Code Playgroud)

执行此操作后,您可以启动TensorBoard以显示日志/tmp/logs,并且您将能够看到过滤器的可视化.

请注意,此技巧可将深度为3的滤镜显示为RGB图像(以匹配输入图像的通道).如果您有更深的滤镜,或者它们没有意义解释为颜色通道,您可以使用tf.split()op在深度维度上拆分滤镜,并为每个深度生成一个图像摘要.

  • `tf.image_summary`现在自2016-11-30以来已被弃用,并替换为`tf.summary.image` cf. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/ops/logging_ops.py (16认同)
  • 请注意,语法在以后的版本中已更改为`tf.image_summary(tag,tensor,...)` (4认同)

eto*_*pov 22

就像@mrry说的那样,你可以使用tf.image_summary.例如,对于cifar10_train.py,您可以将此代码放在某处def train().请注意如何访问范围'conv1'下的var

# Visualize conv1 features
with tf.variable_scope('conv1') as scope_conv:
  weights = tf.get_variable('weights')

  # scale weights to [0 255] and convert to uint8 (maybe change scaling?)
  x_min = tf.reduce_min(weights)
  x_max = tf.reduce_max(weights)
  weights_0_to_1 = (weights - x_min) / (x_max - x_min)
  weights_0_to_255_uint8 = tf.image.convert_image_dtype (weights_0_to_1, dtype=tf.uint8)

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

  # this will display random 3 filters from the 64 in conv1
  tf.image_summary('conv1/filters', weights_transposed, max_images=3)
Run Code Online (Sandbox Code Playgroud)

如果你想conv1在一个漂亮的网格中可视化所有过滤器,你必须自己将它们组织成一个网格.我今天就这样做了,所以现在我想分享一个将conv1可视化为网格要点


Mar*_*oma 5

您可以通过以下方式将值提取为numpy数组:

with tf.variable_scope('conv1', reuse=True) as scope_conv:
    W_conv1 = tf.get_variable('weights', shape=[5, 5, 1, 32])
    weights = W_conv1.eval()
    with open("conv1.weights.npz", "w") as outfile:
        np.save(outfile, weights)
Run Code Online (Sandbox Code Playgroud)

请注意,您必须调整范围('conv1'就我而言)和变量名('weights'就我而言)。

然后归结为可视化numpy数组。如何可视化numpy数组的一个示例是

#!/usr/bin/env python

"""Visualize numpy arrays."""

import numpy as np
import scipy.misc

arr = np.load('conv1.weights.npb')

# Get each 5x5 filter from the 5x5x1x32 array
for filter_ in range(arr.shape[3]):
    # Get the 5x5x1 filter:
    extracted_filter = arr[:, :, :, filter_]

    # Get rid of the last dimension (hence get 5x5):
    extracted_filter = np.squeeze(extracted_filter)

    # display the filter (might be very small - you can resize the window)
    scipy.misc.imshow(extracted_filter)
Run Code Online (Sandbox Code Playgroud)