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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33783672/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:59:20  来源:igfitidea点击:

How can I visualize the weights(variables) in cnn in Tensorflow?

pythontensorflow

提问by Wuchen

After training the cnn model, I want to visualize the weight or print out the weights, what can I do? I cannot even print out the variables after training. Thank you!

训练完cnn模型后,想可视化权重或者打印权重,怎么办?训练后我什至无法打印出变量。谢谢!

回答by mrry

To visualize the weights, you can use a tf.image_summary()op to transform a convolutional filter (or a slice of a filter) into a summary proto, write them to a log using a tf.train.SummaryWriter, and visualize the log using TensorBoard.

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

Let's say you have the following (simplified) program:

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

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)

After doing this, you can start TensorBoard to visualize the logs in /tmp/logs, and you will be able to see a visualization of the filter.

完成此操作后,您可以启动 TensorBoard 来可视化 中的日志/tmp/logs,您将能够看到过滤器的可视化。

Note that this trick visualizes depth-3 filters as RGB images (to match the channels of the input image). If you have deeper filters, or they don't make sense to interpret as color channels, you can use the tf.split()op to split the filter on the depth dimension, and generate one image summary per depth.

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

回答by etoropov

Like @mrry said, you can use tf.image_summary. For example, for cifar10_train.py, you can put this code somewhere under def train(). Note how you access a var under scope 'conv1'

就像@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)

If you want to visualize all your conv1filters in one nice grid, you would have to organize them into a grid yourself. I did that today, so now I'd like to share a gist for visualizing conv1 as a grid

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

回答by Martin Thoma

You can extract the values as numpy arrays the following way:

您可以通过以下方式将值提取为 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)

Note that you have to adjust the scope ('conv1'in my case) and the variable name ('weights'in my case).

请注意,您必须调整范围('conv1'在我的情况下)和变量名称('weights'在我的情况下)。

Then it boils down on visualizing numpy arrays. One example how to visualize numpy arrays is

然后归结为可视化 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)