Train using Keras-MXNet and inference using MXNet Scala API

Wei Lai
Wei Lai
Jun 13, 2018 · 5 min read

Overview

Train a ResNet-50 Model on CIFAR10 Dataset

pip install mxnet-cu90
pip install keras-mxnet
wget https://raw.githubusercontent.com/awslabs/keras-apache-mxnet/master/examples/cifar10_resnet_multi_gpu.py
data_names, data_shapes = save_mxnet_model(model=model, prefix='cifar10_resnet50', epoch=0)
python cifar10_resnet_multi_gpu.py
Test loss: 1.13192281456
Test accuracy: 0.8308
MXNet Backend: Successfully exported the model as MXNet model!
MXNet symbol file - cifar10-resnet50-symbol.json
MXNet params file - cifar10-resnet50-0000.params
Model input data_names and data_shapes are:
data_names : ['/input_11']
data_shapes : [DataDesc[/input_11,(128L, 3L, 32L, 32L),float32,NCHW]]

Inference using MXNet Scala Inference API

Environment Setup

Prepare Model and Data

0 airplane
1 automobile
2 bird
3 cat
4 deer
5 dog
6 frog
7 horse
8 ship
9 truck
.
└── resnet-50
├── cifar10-resnet50-0000.params
├── cifar10-resnet50-symbol.json
└── synset.txt
import keras
import random
import scipy.misc
from keras.datasets import cifar10
(x_train, y_train),(x_test, y_test) = cifar10.load_data()
num_test = len(x_test)
num_images = 10
for idx in random.sample(range(0, num_test), num_images):
label = y_test[idx][0]
output = 'x_test_' + str(idx) + '_' + str(label) +'.jpg'
scipy.misc.imsave(output, x_test[idx])
Inference Test Images

Load Pre-trained MXNet Model using Scala API

val inputShape = Shape(1, 3, 32, 32)
val inputDescriptor = IndexedSeq(DataDesc("/input_11", inputShape, dType, "NCHW"))

Run Inference

1. The model file prefix we saved in training section
--model-path-prefix "absolute/path/to/your/model"
e.g. "/Users/lawei/scalaMXNet/models/resnet-50/cifar10-resnet50"
2. Image to be predicted
--input-image "absolute/path/to/your/image"
e.g. "/Users/lawei/scalaMXNet/images/x_test_202_8.jpg"
3. Directory of images to be predicted
--input-dir "absolute/path/to/image/directory"
e.g. "/Users/lawei/scalaMXNet/images"
Pass the arguments in IntelliJ
Input image for inference
Input image x_test_1584_7.jpg 
Class with probability =Vector((7 horse,0.7975957), (3 cat,0.09379086), (4 deer,0.07512564), (0 airplane,0.032357983), (8 ship,4.789475E-4))

What’s Next

Citations

Apache MXNet

Apache MXNet (incubating) is a deep learning framework designed for both efficiency and flexibility. It allows you to mix symbolic and imperative programming to maximize efficiency and productivity.

Thanks to Thomas Delteil.

    Wei Lai

    Written by

    Wei Lai

    Software Development Engineer @AWS AI working on deep learning framework MXNet.

    Apache MXNet

    Apache MXNet (incubating) is a deep learning framework designed for both efficiency and flexibility. It allows you to mix symbolic and imperative programming to maximize efficiency and productivity.