在 TensorFlow 中在 NHWC 和 NCHW 之间转换
Convert between NHWC and NCHW in TensorFlow
将张量从 NHWC 格式转换为 NCHW 格式,反之亦然的最佳方法是什么?
是否有专门执行此操作的操作,或者我是否需要使用 split/concat 类型操作的某种组合?
您需要做的就是将维度从 NHWC 排列到 NCHW(或相反)。
每个字母的含义可能有助于理解:
- N: 批处理中的图像数量
- H: 图片的高度
- W: 图片的宽度
- C:图像的通道数(例如:RGB 为 3,灰度为 1...)
从 NHWC 到 NCHW
图像的形状是 (N, H, W, C)
,我们希望输出的形状是 (N, C, H, W)
。因此,我们需要应用 tf.transpose
并精心选择排列 perm
.
The returned tensor's dimension i
will correspond to the input dimension 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]
从 NCHW 到 NHWC
图像形状为 (N, C, H, W)
,我们希望输出的形状为 (N, H, W, C)
。因此,我们需要应用 tf.transpose
并精心选择排列 perm
.
The returned tensor's dimension i
will correspond to the input dimension 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]
将'NCHW'转换为'NHWC'
from keras import backend
backend.set_image_data_format('channels_last') #channels_first for NCHW
将张量从 NHWC 格式转换为 NCHW 格式,反之亦然的最佳方法是什么?
是否有专门执行此操作的操作,或者我是否需要使用 split/concat 类型操作的某种组合?
您需要做的就是将维度从 NHWC 排列到 NCHW(或相反)。
每个字母的含义可能有助于理解:
- N: 批处理中的图像数量
- H: 图片的高度
- W: 图片的宽度
- C:图像的通道数(例如:RGB 为 3,灰度为 1...)
从 NHWC 到 NCHW
图像的形状是 (N, H, W, C)
,我们希望输出的形状是 (N, C, H, W)
。因此,我们需要应用 tf.transpose
并精心选择排列 perm
.
The returned tensor's dimension
i
will correspond to the input dimensionperm[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]
从 NCHW 到 NHWC
图像形状为 (N, C, H, W)
,我们希望输出的形状为 (N, H, W, C)
。因此,我们需要应用 tf.transpose
并精心选择排列 perm
.
The returned tensor's dimension
i
will correspond to the input dimensionperm[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]
将'NCHW'转换为'NHWC'
from keras import backend
backend.set_image_data_format('channels_last') #channels_first for NCHW