NCHW转化为NHWC:
NHWC:[batch,height,width,channels]
NCHW:[batch,channels,height,width]
Tensorflow中使用的格式为NHWC:
API:
tf.transpose(a,perm)
- a:需要转置的张量。
- perm:按照哪种排列组合进行转置。
实例:
import tensorflow as tf
#定义一个常量 是一个三阶张量,[channels,height,width] [0,1,2]
nchw = tf.constant(value=[1,2,3,4,5,6,7,8,9,10,11,12],shape=[3,2,2])
#将[channels,height,width]转置为[height,width,channels]格式,[0,1,2] ->[1,2,0]
nhwc = tf.transpose(nchw,[1,2,0])
with tf.Session() as sess:
nchw_new,nhwc_new = sess.run([nchw,nhwc])
print("nchw:\n",nchw_new)
print("nhwc:\n",nhwc_new)
'''
nchw:
[[[ 1 2]
[ 3 4]]
[[ 5 6]
[ 7 8]]
[[ 9 10]
[11 12]]]
nhwc:
[[[ 1 5 9]
[ 2 6 10]]
[[ 3 7 11]
[ 4 8 12]]]
'''