Tensorflow - 可视化预训练网络的学习过滤器

Tensorflow - Visualizing learned filters of a pretrained network

我目前正在尝试在 python 中使用 tensorflow 可视化我的 CNN 学习的过滤器。 我发现许多版本在训练新网络时都使用 mnist 数据集,但无法将其应用于我的应用程序。 我使用自定义数据集训练了一个存储在我的磁盘上的 Estimator 对象。该模型包含

等层
conv1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.sigmoid)

并且只想可视化对维度为 (28,28,3) 的单张图片的预测。 在tensorboard中,这一层简称为"conv2d",其他层简称为"conv2d_2",依此类推,除了使用了sigmoid函数外,它的结构与默认的MNIST网络基本相同。

我不知道如何实现这个 - 我考虑过获取权重和偏差并根据步幅和过滤器大小重新计算每一层,但我已经无法获取权重而且我认为是一个更简单的解决方案。

I'm currently trying to visualize the learned filters of my CNN with tensorflow in python.

我想你的意思是可视化特定层的激活?如果是这样,你只需要运行你想要的图像的这一层的张量,如下所示:

import matplotlib.pyplot as plt

# Model definition
...
conv1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.sigmoid)
...

# Getting activations
acts = sess.run(conv1, {input_layer: your_image})

# Visualizing every filters
for i in range(acts.shape[3]):
  plt.imshow(acts[:,:,:,i].squeeze())
  plt.show()

如果您使用 Estimator,您可以在 model_fn:

中使用 tf.summary.image() 直接可视化激活的演变
# In model_fn
...
conv1 = tf.layers.conv2d(inputs=input_layer, filters=32, kernel_size=[5, 5], padding="same", activation=tf.nn.sigmoid)
acts_filters = tf.unstack(conv1, axis=3)
for i, filter in enumerate(acts_filters):
    tf.summary.image('filter' + str(i), tf.expand_dims(filter, axis=3))