Try to build MobilenetSSD300 got error: mxnet.base.MXNetError: Shape inconsistent, Provided = [32,1917], inferred shape=[32,2781]


Hi everyone,
I don’t know the reason why i couldn’t see any pre-trained model of mobilenetSSD with input size 300x300 in any MXnet’s repo (include Gluon, mxnet-ssd…). Actualy current Gluon-cv doesn’t support well to do that. So i built this model by myself.
I refered the model from Chuanqi and modify some lines here (model is ssd_300_mobilenet1_0_voc) and i got this error when run

INFO:root:Namespace(batch_size=32, data_shape=300, dataset='voc', epochs=240, gpus='0', log_interval=1000, lr=0.001, lr_decay=0.1, lr_decay_epoch='160,200', momentum=0.9, network='mobilenet1.0', num_workers=4, resume='', save_interval=5, save_prefix='ssd_300_mobilenet1.0_voc', seed=233, start_epoch=0, val_interval=5, wd=0.0005)
INFO:root:Start training from [Epoch 0]
[13:57:24] src/operator/nn/./cudnn/./cudnn_algoreg-inl.h:109: Running performance tests to find the best convolution algorithm, this can take a while... (setting env variable MXNET_CUDNN_AUTOTUNE_DEFAULT to 0 to disable)
Traceback (most recent call last):
  File "/home/prdcv/PycharmProjects/gvh205/gluon-cv/scripts/detection/ssd/", line 303, in <module>
    train(net, train_data, val_data, eval_metric, ctx, args)
  File "/home/prdcv/PycharmProjects/gvh205/gluon-cv/scripts/detection/ssd/", line 199, in train
    cls_preds, box_preds, cls_targets, box_targets)
  File "/usr/local/lib/python2.7/dist-packages/mxnet/gluon/", line 541, in __call__
    out = self.forward(*args)
  File "build/bdist.linux-x86_64/egg/gluoncv/", line 147, in forward
  File "<string>", line 89, in pick
  File "/usr/local/lib/python2.7/dist-packages/mxnet/_ctypes/", line 92, in _imperative_invoke
  File "/usr/local/lib/python2.7/dist-packages/mxnet/", line 252, in check_call
    raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: Shape inconsistent, Provided = [32,1917], inferred shape=[32,2781]

this mean that i successfully build my model with 1917 priorbox, but i had no idea why the model refer to 2781 priorbox?


It seems that some parameters were initialized with a different shape, and it doesn’t match to the one you provided. I would set a breakpoint on the line where exception happens, run debugger, and look the shape of the parameters (comment model hybridization to see shapes).

Btw, you also can pass 300x300 image to existing MobilenetSSD version. Here is the example:

from matplotlib import pyplot as plt
import mxnet as mx
import gluoncv
from gluoncv.utils import download, viz

net = gluoncv.model_zoo.get_model('ssd_512_mobilenet1_0_voc', pretrained=True)
# image is downloaded from and exactly 300 by 300
x, image ='bike.jpg', 300)

cid, score, bbox = net(x)
ax = viz.plot_bbox(image, bbox[0], score[0], cid[0])

If I run the code in Jupyter Notebook I receive the following result: