1

I have a model that needs to be optimized to CPU.

Currently the model takes a 1024 x 1024 bytes data.

images = img[y:y+1024,x:x+1024,:]

As per this document, they want to change the default tensorflow data format from NHCW to NCHW format.

How can I transform from NHWC to NCHW format?

https://software.intel.com/en-us/articles/tensorflow-optimizations-on-modern-intel-architecture

Maxim
  • 52,561
  • 27
  • 155
  • 209
user1050619
  • 19,822
  • 85
  • 237
  • 413

1 Answers1

1

As per this document, they want to change the default tensorflow data format from NHCW to NCHW format.

Actually, I've never seen any Tensorflow function that supports NHCW format. For example, tf.nn.conv2d and tf.nn.conv2d_transpose support NHWC (current default) and NCHW format. tf.nn.max_pool supports NHWC, NCHW and NCHW_VECT_C (the last one is the most performant tensor format for cudnn6's quantized convolution, similar to NCHW).

How can I transform from NHCW to NCHW format?

But this transformation is possible, e.g. via tf.transpose that works with high-dimensional tensors as well:

# NHCW
original = tf.placeholder(dtype=tf.float32, shape=[None, 1024, 3, 1024])
# NCHW: swap 1 and 2 axis
transformed = tf.transpose(original, perm=[0, 2, 1, 3])

You can also do this in numpy via np.swapaxes(array, 1, 2).

Maxim
  • 52,561
  • 27
  • 155
  • 209
  • Thanks, I have provided the document link. – user1050619 Nov 17 '17 at 16:27
  • OK. I guess you meant NHWC, because it says "TensorFlow default NHWC format is not the most efficient ...", which seems to be true. Anyway, the proposed solution should work – Maxim Nov 17 '17 at 16:31