Strange Behavior in NHWC layout for conv2d


I want to build a network based on NHWC layout, however I run into some trouble. Coming from a quite complex scenario I figured that at least one simple case doesn’t t really work as expected:

This is my code:

import mxnet

ctx = mxnet.gpu()

print('Current context: {}' .format(ctx))
test_data = mxnet.ndarray.random.randn(1,10,10,1, ctx=ctx)
print('Test Data Shape: {}'.format(test_data.shape))
nhwc = mxnet.gluon.nn.Conv2D(1,(3,3), layout='NHWC')
res2 = nhwc(test_data)
print("Conv2d result mean(): {}".format(res2.mean().asscalar()))

If I run that on GPU everything works quite like expected:

Current context: gpu(0)
Test Data Shape: (1, 10, 10, 1)
Conv2d result mean(): -0.014221698977053165

while if i set the context to cpu, i get the following error message:

Current context: cpu(0)
Test Data Shape: (1, 10, 10, 1)
Traceback (most recent call last):
  File "C:/Users/schoenfs/Projekte/IA_ML/aiml_street_lighting/gluon_faster_rcnn/", line 12, in <module>
    print("Conv2d result mean(): {}".format(res2.mean().asscalar()))
  File "C:\Users\schoenfs\Envs\streetlight\lib\site-packages\mxnet\ndarray\", line 2014, in asscalar
    return self.asnumpy()[0]
  File "C:\Users\schoenfs\Envs\streetlight\lib\site-packages\mxnet\ndarray\", line 1996, in asnumpy
  File "C:\Users\schoenfs\Envs\streetlight\lib\site-packages\mxnet\", line 253, in check_call
    raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [18:13:35] c:\jenkins\workspace\mxnet-tag\mxnet\src\operator\nn\./convolution-inl.h:169: Check failed: param_.layout.value() == mshadow::kNCW || param_.layout.value() == mshadow::kNCHW || param_.layout.value() == mshadow::kNCDHW: Only support NCW, NCHW and NCDHW layout

would be great if someone shed some light on why this happens ?

Best Regards

Do you get the same error if you change the layout?

No, if use the default Layout (NCHW) there is no issue.

Best Regards

This seems to be a known and long time accepted api restriction:

Hi @Knulph, the latest MXNet 1.6.0 doesn’t support “NHWC”, so you will need to change the shape to “NCHW” using mx.nd.moveaxis:

X = mx.nd.zeros((1,10,10,1))
mx.nd.moveaxis(X, [-1], [1]).shape
(1, 1, 10, 10)

@gold_piggy, yeah that is what I thought the problem was, that’s why I suggested @spx to first trying to change the layout.

do you mean 1.6.0 doesn’t support NHWC at all ? Not even on GPU ?