gpt4 book ai didi

tensorflow - 在 TensorFlow 中在 NHWC 和 NCHW 之间转换

转载 作者:行者123 更新时间:2023-12-02 08:15:39 31 4
gpt4 key购买 nike

将张量从 NHWC 格式转换为 NCHW 格式(反之亦然)的最佳方法是什么?

是否有专门执行此操作的操作,或者我需要使用拆分/连接类型操作的某种组合?

最佳答案

您需要做的就是将维度从 NHWC 排列到 NCHW(或相反)。

每个字母的含义可能有助于理解:

  • N:批处理中的图像数量
  • H:图像的高度
  • W:图像的宽度
  • C:图像的 channel 数(例如: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]

关于tensorflow - 在 TensorFlow 中在 NHWC 和 NCHW 之间转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37689423/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com