Check failed: param.pad_width.ndim() == dshape.ndim() (4 vs. 3)

File “/usr/local/lib/python2.7/dist-packages/nnvm-0.8.0-py2.7.egg/nnvm/compiler/graph_util.py”, line 31, in infer_shape
graph = graph.apply(“InferShape”)
File “/usr/local/lib/python2.7/dist-packages/nnvm-0.8.0-py2.7.egg/nnvm/graph.py”, line 234, in apply
check_call(_LIB.NNGraphApplyPasses(self.handle, npass, cpass, ctypes.byref(ghandle)))
File “/usr/local/lib/python2.7/dist-packages/nnvm-0.8.0-py2.7.egg/nnvm/_base.py”, line 75, in check_call
raise NNVMError(py_str(_LIB.NNGetLastError()))
nnvm._base.NNVMError: Error in operator pad27: [16:21:43] /home/zhoukun/FrameWork/nnvm/src/top/nn/nn.cc:576: Check failed: param.pad_width.ndim() == dshape.ndim() (4 vs. 3)

I use tensorflow as backend, keras as frontend, I want to use the nnvm as my compiler to compile the mobilenet model.
this is part of my code .
target = 'llvm’
shape_dict = {‘data’: (1, 3, 224, 224)}

with nnvm.compiler.build_config(opt_level=2):
graph, lib, params = nnvm.compiler.build(sym, target, shape_dict, params=params)

the error occurs in the 4th code

I think some where in process we are loosing dimensions to input data.

We use pad operator in convolution and pooling . Ref. https://github.com/dmlc/nnvm/blob/0e00ca3b81d1317bb47dfc75a9ef3ec4684293f9/python/nnvm/frontend/keras.py#L159

Print the shape of insym in all calls to _sym.pad in frontend to find which operator it is.

On my environment, the following PR fixes the problem.

1 Like

Yes, the above PR should work which make dim back to 4.

where did you reshape the data? could you explain this in detail

reshape may be an operation before (convolution / pad).

The reshape operation through keras front end was not considering the batch which results in output dim length 3 instead of 4. Hence the pad operator report and error on dimension mismatch.

where can I fix this problem

I tried the fix reshape solution, and find that the result obtained by NNVM and keras are different, and not right. I used a cat picture and mobileNet.

NNVM top-1 id: 0, class name: tench, Tinca tinca

Keras top-1 id: 688, class name: oscilloscope, scope, cathode-ray oscilloscope, CRO

This part require debugging !

You mean on keras the model detected a cat as “oscilloscope, scope, cathode-ray oscilloscope, CRO” ?

I think we first need to check on keras first.

And also I found if I use the fix reshape solution, it seem that the nnvm consumes longer time than the pure keras

I’ve sent many fixes recently for Keras and I think all of them are necessary for MobileNet.

Can you try the below branch?
https://github.com/kazum/nnvm/tree/keras-mobilenet

It includes all of the patches and test for MobileNet. The test works fine on my environment.

OK, thaks, I will try this branch.

porblem solved, the fix reshape works

1 Like