如何在 [batch, channels, width, height] (NCHW) 顺序中使用 tf.summary.image() 在 tensorboard 中可视化图像?

How to visualize images in tensorboard using tf.summary.image() in [batch, channels, width, height] (NCHW) order?

首先,我想使用 tf.reshape().
将 2-D 张量重塑为 4-D 张量 我以为tf.reshape()会变形
[batch, array] -> [batch, width, height, channels] (NHWC) 订单
但实际上它改变了
[batch, array] -> [batch, channels, width, height] (NCHW) 订单

示例:

a = np.array([[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]])
print(a.shape)

# [batch_size, channels, height, width]
b = sess.run(tf.reshape(a, shape=[2, 3, 4, 4]))

# [batch_size, height, width, channels]
c = sess.run(tf.reshape(a, shape=[2, 4, 4, 3]))

print(b)
print('*******')
print(c)

结果是:

(2, 48)
[[[[ 1  2  3  4]
   [ 5  6  7  8]
   [ 9 10 11 12]
   [13 14 15 16]]

  [[ 1  2  3  4]
   [ 5  6  7  8]
   [ 9 10 11 12]
   [13 14 15 16]]

  [[ 1  2  3  4]
   [ 5  6  7  8]
   [ 9 10 11 12]
   [13 14 15 16]]]


 [[[ 1  2  3  4]
   [ 5  6  7  8]
   [ 9 10 11 12]
   [13 14 15 16]]

  [[ 1  2  3  4]
   [ 5  6  7  8]
   [ 9 10 11 12]
   [13 14 15 16]]

  [[ 1  2  3  4]
   [ 5  6  7  8]
   [ 9 10 11 12]
   [13 14 15 16]]]]
*******
[[[[ 1  2  3]
   [ 4  5  6]
   [ 7  8  9]
   [10 11 12]]

  [[13 14 15]
   [16  1  2]
   [ 3  4  5]
   [ 6  7  8]]

  [[ 9 10 11]
   [12 13 14]
   [15 16  1]
   [ 2  3  4]]

  [[ 5  6  7]
   [ 8  9 10]
   [11 12 13]
   [14 15 16]]]


 [[[ 1  2  3]
   [ 4  5  6]
   [ 7  8  9]
   [10 11 12]]

  [[13 14 15]
   [16  1  2]
   [ 3  4  5]
   [ 6  7  8]]

  [[ 9 10 11]
   [12 13 14]
   [15 16  1]
   [ 2  3  4]]

  [[ 5  6  7]
   [ 8  9 10]
   [11 12 13]
   [14 15 16]]]]

因此,我将转换层和池化层的 data_format='channels_first' 更改为按 NCHW 顺序使用重塑的张量。其实培训还是不错的。 --verbose: 它给出了更好的结果,正如@mrry 在 中提到的,我认为这是可以理解的,因为 NCHW 是 cuDNN 的默认顺序。

但是,我无法使用 tf.summary.image()which is documented here 将图像添加到摘要中,因为所需的张量形状应按 [batch_size, height, width, channels] 顺序排列。

此外,如果我按 [batch, width, height, channels] 顺序训练和可视化输入图像,它表示不正确的图像。
值得一提的是,训练结果不如使用 [batch, channels, width, height] order.

有几个问题:
1。为什么 tf.reshape() 转换 [batch , array] -> (NCHW) 顺序而不是 (NHWC) 顺序?我用 tf CPU 和 GPU 进行了测试,结果相同。我也使用了 np.reshape(),结果也是一样的。 (这就是为什么我认为我可能在这里误解了一些东西)
2。我如何 使用 tf.summary.image() 以 (NCHW) 顺序在 tensorboard 中可视化图像? (问题 #2 使用 @Maosi Chen 的建议解决了。谢谢)

我已经在 GPU(版本 1.4) 上训练了模型,图像来自 CIFAR-10 数据集。
谢谢

您可以按 tf.transpose (https://www.tensorflow.org/api_docs/python/tf/transpose) 重新排序维度。

注意perm个元素是源张量的维度索引(a)

import tensorflow as tf
import numpy as np

sess = tf.InteractiveSession()

a = np.array([[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]])
print(a.shape)

# [batch_size, channels, height, width]
b = sess.run(tf.reshape(a, shape=[2, 3, 4, 4]))

# [batch_size, height, width, channels]
c = sess.run(tf.transpose(b, perm=[0, 2, 3, 1]))

print(b)
print('*******')
print(c)

结果:

(2, 48) [[[[ 1  2  3  4]    [ 5  6  7  8]    [ 9 10 11 12]    [13 14 15 16]]

  [[ 1  2  3  4]    [ 5  6  7  8]    [ 9 10 11 12]    [13 14 15 16]]

  [[ 1  2  3  4]    [ 5  6  7  8]    [ 9 10 11 12]    [13 14 15 16]]]


 [[[ 1  2  3  4]    [ 5  6  7  8]    [ 9 10 11 12]    [13 14 15 16]]

  [[ 1  2  3  4]    [ 5  6  7  8]    [ 9 10 11 12]    [13 14 15 16]]

  [[ 1  2  3  4]    [ 5  6  7  8]    [ 9 10 11 12]    [13 14 15 16]]]]
******* [[[[ 1  1  1]    [ 2  2  2]    [ 3  3  3]    [ 4  4  4]]

  [[ 5  5  5]    [ 6  6  6]    [ 7  7  7]    [ 8  8  8]]

  [[ 9  9  9]    [10 10 10]    [11 11 11]    [12 12 12]]

  [[13 13 13]    [14 14 14]    [15 15 15]    [16 16 16]]]


 [[[ 1  1  1]    [ 2  2  2]    [ 3  3  3]    [ 4  4  4]]

  [[ 5  5  5]    [ 6  6  6]    [ 7  7  7]    [ 8  8  8]]

  [[ 9  9  9]    [10 10 10]    [11 11 11]    [12 12 12]]

  [[13 13 13]    [14 14 14]    [15 15 15]    [16 16 16]]]]