首先,我想用tf.reshape()把二维张量重塑成四维张量.
我以为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]]]]因此,我将conv和合用层的data_format='channels_first'更改为使用NCHW顺序的整形张量。事实上,训练很好。--verbose:给出了更好的结果,正如@mrry在这里中提到的那样,我认为这是可以理解的,因为NCHW是cuDNN的默认顺序。
但是,我不能使用tf.summary.image(),这里有记载将图像添加到摘要中,因为所需的张量形状应该是[batch_size, height, width, channels]顺序。
此外,如果按照[batch, width, height, channels]顺序对输入图像进行训练和可视化,则表示不正确的图像。

我要指出的是,培训结果不如使用[batch, channels, width, height]订单好。
有几个问题:
1.为什么 tf.reshape()转换[batch , array] -> (NCHW)命令而不是(NHWC)命令?我用tf CPU和GPU进行了测试,结果相同。我还使用了np.reshape(),也使用了相同的结果。(这就是为什么我想我会误解这里的一些东西)
2. 如何使用tf.summary.image() in (NCHW)顺序将图片可视化?(问题2使用@Maosi Chen的建议解决了问题。谢谢)

我在GPU上训练过模型(1.4版),图像来自CIFAR-10数据集。
谢谢
发布于 2017-12-08 00:37:35
您可以通过tf.transpose (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]]]]https://stackoverflow.com/questions/47705614
复制相似问题