I'm trying to convert the Tensorflow CIFAR10 tutorial from NHWC to NCHW, but can't figure out how to do so. I have only found answers such as this, which is a couple of lines of code without an explanation of how it works and where to use it. Here are a couple of unsuccessful attempts I have made using this approach:
def inference(images):
with tf.variable_scope('conv1') as scope:
kernel = _variable_with_weight_decay('weights',
shape=[5, 5, 3, 64],
stddev=5e-2,
wd=0.0)
# ****************************************************************** #
### Original
conv = tf.nn.conv2d(images, kernel, [1, 1, 1, 1], padding='SAME')
### Attempt 1
imgs = tf.transpose(images, [0, 3, 1, 2]) # NHWC -> NCHW
conv = tf.nn.conv2d(imgs, kernel, [1, 1, 1, 1], padding='SAME')
conv = tf.transpose(conv, [0, 2, 3, 1]) # NCHW -> NHWC
### Attempt 2
kern = tf.transpose(kernel, [0, 3, 1, 2]) # NHWC -> NCHW
conv = tf.nn.conv2d(images, kern, [1, 1, 1, 1], padding='SAME')
conv = tf.transpose(conv, [0, 2, 3, 1]) # NCHW -> NHWC
# ****************************************************************** #
biases = _variable_on_cpu('biases', [64], tf.constant_initializer(0.0))
pre_activation = tf.nn.bias_add(conv, biases)
conv1 = tf.nn.relu(pre_activation, name=scope.name)
_activation_summary(conv1)
...
Which get the errors (respectively):
ValueError: Dimensions must be equal, but are 24 and 3 for 'conv1/Conv2D' (op: 'Conv2D') with input shapes: [64,3,24,24], [5,5,3,64].
ValueError: Dimensions must be equal, but are 3 and 5 for 'conv1/Conv2D' (op: 'Conv2D') with input shapes: [64,24,24,3], [5,64,5,3].
Can someone please provide a set of steps I can follow to convert this example to NCHW successfully.