将张量从NHWC格式转换为NCHW格式的最佳方法是什么,反之亦然?
是否有专门用于执行此操作的操作,或者我是否需要使用split/concat类型操作的某些组合?
Oli*_*rot 56
您需要做的就是从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
Run Code Online (Sandbox Code Playgroud)
在实践中:
images_nhwc = tf.placeholder(tf.float32, [None, 200, 300, 3]) # input batch
out = tf.transpose(x, [0, 3, 1, 2])
print(out.get_shape()) # the shape of out is [None, 3, 200, 300]
Run Code Online (Sandbox Code Playgroud)
图像形状是(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
Run Code Online (Sandbox Code Playgroud)
在实践中:
images_nchw = tf.placeholder(tf.float32, [None, 3, 200, 300]) # input batch
out = tf.transpose(x, [0, 2, 3, 1])
print(out.get_shape()) # the shape of out is [None, 200, 300, 3]
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
19179 次 |
最近记录: |