Hi I'm a newbie to data science, I followed this tutorial https://mxnet.incubator.apache.org/tutorials/nlp/cnn.html but I am confused over how to make a single prediction using the trained model generated by the above mentioned tutorial. Please guide me the right direction to fix this. Thanks.
Asked
Active
Viewed 1,468 times
2 Answers
2
Here is a tutorial of how to predict with pretrained model: https://mxnet.incubator.apache.org/tutorials/python/predict_image.html
Steps:
1. Load pretrained model and create a MXNet module instance.
2. Grab your data and run forward with module

Jérémy Blain
- 249
- 2
- 14

kevinthesun
- 336
- 1
- 5
-
Thanks I loaded the model with the .params file and the .json file created after the model was trained, but still am unable to create a module instance. How do I do that? – Zann Sep 23 '17 at 09:53
-
1You need to bind the model parameters that you read from the files (mod.bind and mod.set_param), and once you have a functioning model, you can call the feed_forward/forward/predict function (mod.forward and mod.get_outputs). – Guy Sep 24 '17 at 18:03
-
@Guy and @kevinthesun, the concept is relatively simple, but in practice is not quite like that. For example, what is the `data_shapes` to be used for the `bind` method? Can one of you give an example of tying these two tutorials together? – Interfector Nov 13 '17 at 21:57
-
Check this one: https://github.com/dmlc/mxnet-notebooks/blob/master/python/tutorials/predict_imagenet.ipynb – Guy Nov 21 '17 at 16:40
0
One can load the model like this:
sym, arg_params, aux_params = mx.model.load_checkpoint('cnn', 3)
mod = mx.mod.Module(symbol=sym, context=mx.cpu(), label_names=None)
mod.bind(for_training=False, data_shapes=[('data', (50,56))],
label_shapes=mod._label_shapes)
mod.set_params(arg_params, aux_params, allow_missing=True)
But unfortunately, you cannot use this for making only one prediction. You would need a batch of 50.

Interfector
- 1,868
- 1
- 23
- 43
-
1You don't have to have a batch of 50. The first argument in the data_shapes controls the size of the expected batch size. You can simply use 1: `mod.bind(for_training=False, data_shapes=[('data', (1,3,224,224))], label_shapes=mod._label_shapes)` as described in the tutorial https://github.com/dmlc/mxnet-notebooks/blob/master/python/tutorials/predict_imagenet.ipynb – Guy Nov 21 '17 at 16:42
-
@Guy I urge you to actually run the code. I can tell you that your solution won't work. The problem is that the batch size is actually embedded in the `Reshape` layers. – Interfector Nov 21 '17 at 22:20