1.预备知识:
tf.summary.image(name, tensor, max_outputs=3, collections=None, family=None)
参数解析:
name:A name for the generated node. Will also serve as a series name in TensorBoard.
tensor:A 4-D `uint8` or `float32` `Tensor` of shape `[batch_size, height, width, channels]` where `channels` is 1, 3, or 4.
* 1: `tensor` is interpreted as Grayscale.
* 3: `tensor` is interpreted as RGB.
* 4: `tensor` is interpreted as RGBA.
max_outputs: Max number of batch elements to generate images for.
Returns: A scalar `Tensor` of type `string`. The serialized `Summary` protocol buffer.
2.核心思想:
即将特征图看作是多幅(通道)的图像。首先将当前特征图按照其通道数上的维度拆分,然后对拆分后的特征图逐个可视化。
3.代码实现:
with tf.variable_scope('layer1-conv1'):
conv1_weights=tf.get_variable('weights',[5,5,1,6],initializer=tf.truncated_normal_initializer(stddev=0.1))
conv1_biases=tf.get_variable('biases',[6],initializer=tf.constant_initializer(0.1))
conv1=tf.nn.conv2d(input_tensor,conv1_weights,strides=[1,1,1,1],padding='VALID')
bias1=tf.nn.bias_add(conv1,conv1_biases)
relu1=tf.nn.relu(bias1)
split = tf.split(relu1, relu1.shape[-1], axis=3)
for i in range(relu1.shape[-1]):
tf.summary.image("layer1-conv1-channel_"+str(i),split[i],1) #可视化卷积层特征图
4.结果: