TF之data_format:data_format中的NHWC&NCHW简介、转换的详细攻略

 

 

目录

NHWC&NCHW简介

NHWC&NCHW转换


 

 

NHWC&NCHW简介

NHWC & NCHW是两种参数呈现的表达方式。在如何表示一组彩色图片的问题上,不同的DL框架有不同的表达。

  形式 适合的框架  

NHWC

channels_first

[batch, in_height, in_width, in_channels]

批量批次、高度、宽度、通道数

TensorFlow  

NCHW

channels_last

[batch, in_channels, in_height, in_width]

批量批次、通道数、高度、宽度

Theano、Caffe  

 

NHWC&NCHW转换

1、NHWC →  NCHW

import tensorflow as tf

x = tf.reshape(tf.range(24), [1, 3, 4, 2])
out = tf.transpose(x, [0, 3, 1, 2])

print(x.shape)
print(out.shape)


(1, 3, 4, 2)
(1, 2, 3, 4)

 

2、NCHW → NHWC

import tensorflow as tf

x = tf.reshape(tf.range(24), [1, 2, 3, 4])
out = tf.transpose(x, [0, 2, 3, 1])

print(x.shape)
print(out.shape)


(1, 2, 3, 4)
(1, 3, 4, 2)