【发布时间】:2016-10-07 22:47:45
【问题描述】:
将张量从 NHWC 格式转换为 NCHW 格式(反之亦然)的最佳方法是什么?
是否有专门执行此操作的操作,或者我需要使用 split/concat 类型操作的某种组合?
【问题讨论】:
标签: tensorflow
将张量从 NHWC 格式转换为 NCHW 格式(反之亦然)的最佳方法是什么?
是否有专门执行此操作的操作,或者我需要使用 split/concat 类型操作的某种组合?
【问题讨论】:
标签: tensorflow
您需要做的只是将维度从 NHWC 排列到 NCHW(或相反)。
每个字母的含义可能有助于理解:
图像形状为(N, H, W, C),我们希望输出的形状为(N, C, H, W)。因此,我们需要应用tf.transpose,并使用精心选择的排列perm。
返回的张量维度
i将对应输入维度perm[i]
perm[0] = 0 # output dimension 0 will be 'N', which was dimension 0 in the input
perm[1] = 3 # output dimension 1 will be 'C', which was dimension 3 in the input
perm[2] = 1 # output dimension 2 will be 'H', which was dimension 1 in the input
perm[3] = 2 # output dimension 3 will be 'W', which was dimension 2 in the input
在实践中:
images_nhwc = tf.placeholder(tf.float32, [None, 200, 300, 3]) # input batch
out = tf.transpose(images_nhwc, [0, 3, 1, 2])
print(out.get_shape()) # the shape of out is [None, 3, 200, 300]
图像形状为(N, C, H, W),我们希望输出的形状为(N, H, W, C)。因此,我们需要使用tf.transpose 和精心选择的排列perm。
返回的张量维度
i将对应输入维度perm[i]
perm[0] = 0 # output dimension 0 will be 'N', which was dimension 0 in the input
perm[1] = 2 # output dimension 1 will be 'H', which was dimension 2 in the input
perm[2] = 3 # output dimension 2 will be 'W', which was dimension 3 in the input
perm[3] = 1 # output dimension 3 will be 'C', which was dimension 1 in the input
在实践中:
images_nchw = tf.placeholder(tf.float32, [None, 3, 200, 300]) # input batch
out = tf.transpose(images_nchw, [0, 2, 3, 1])
print(out.get_shape()) # the shape of out is [None, 200, 300, 3]
【讨论】:
perm 是什么 - 或者它是如何定义的?
perm 是图像尺寸的排列,例如从 (N, H, W, C) 到 (N, C, H, W)。
x和perm?
将“NCHW”转换为“NHWC”
from keras import backend
backend.set_image_data_format('channels_last') #channels_first for NCHW
【讨论】: