
pip install mxnet
pip install keras-mxnet
git clone https://github.com/kalyc/SmileCNN
cd SmileCNN
sudo python setup.py install
python datasetprep.py
{
“backend”:“mxnet”,
“image_data_format”:“channels_first”
}import keras as k
X = k.utils.to_channels_first(X)
# Save the trained Keras Model as MXNet Model
keras.models.save_mxnet_model(model=model, prefix='smileCNN_model')
python train.py
Test accuracy: 0.9638663504942575
MXNet Backend: Successfully exported the model as MXNet model!
MXNet symbol file - smileCNN_model-symbol.json
MXNet params file - smileCNN_model-0000.params
Model input data_names and data_shapes are:
data_names : ['/conv2d_1_input1']
data_shapes : [DataDesc[/conv2d_1_input1,(4L, 1L, 32L, 32L),float32,NCHW]]
python evaluation.py

评估图像
('non-smiling', '------------------------###-', 'smiling')sudo apt-get install protobuf-compiler libprotoc-dev
pip install mxnet-model-server
conda install -c
conda-forge protobuf pip install mxnet-model-server
cp smileCNN_model- * ./keras-mms/
cd keras-mms /
- signature.json
- synset.txt
- smileCNN_model-symbol.json
- smileCNN_model-0000.params
- custom_service.py
{
"inputs": [
{
"data_name": "/conv2d_1_input1",
"data_shape": [0, 1, 32, 32]
}
],
"input_type": "image/jpeg",
"outputs": [
{
"data_name": "softmax",
"data_shape": [0, 1]
}
],
"output_type": "application/json"
}non-smiling
smiling
"""CustomService defines a MXNet base vision service"""
from mms.model_service.mxnet_model_service import MXNetBaseService
from mms.utils.mxnet import image, ndarray
class CustomService(MXNetBaseService):
"""
CustomService defines a custom service for image classification task. In preprocess,
input image buffer is read to NDArray and resized respect to input shape in signature.
In post process, top-2 labels are returned.
"""
def _preprocess(self, data):
img_list = []
for idx, img in enumerate(data):
input_shape = self.signature['inputs'][idx]['data_shape'] #Input shape is NCHW
[h, w] = input_shape[2:]
img_arr = image.read(img, 0) #Set flag to 0 for reading grayscale images
img_arr = image.resize(img_arr, w, h)
img_arr = image.transform_shape(img_arr)
img_list.append(img_arr)
return img_list
def _postprocess(self, data):
assert hasattr(self, 'labels'), \
"Can't find labels attribute. Did you put synset.txt file into " \
"model archive or manually load class label file in __init__?"
return [ndarray.top_probability(d, self.labels, top=2) for d in data]
mxnet-model-export --model-name smileCNN --model-path。--service-file-path custom_service.py
mxnet-model-server --models smileCNN = smileCNN.model

测试图像

测试1.JPG
curl -X POST http://127.0.0.1:8080/smileCNN/predict -F "/conv2d_1_input1=@test-1.jpg"
{
"prediction": [
[
{
"class": "smiling",
"probability": 1.0
},
{
"class": "non-smiling",
"probability": 0.0
}
]
]
}
测试2.JPG
curl -X POST http://127.0.0.1:8080/smileCNN/predict -F“/conv2d_1_input1=@test-2.jpg”
{
"prediction": [
[
{
"class": "non-smiling",
"probability": 1.0
},
{
"class": "smiling",
"probability": 0.0
}
]
]
}