Image Classification Based on MobileNetV2
1. Configure Docker development environment
Refer to here. After configuring the Docker development environment, return here to continue the next step.
If you are using a configured Docker development environment, please make sure to follow the Docker configuration tutorial to execute command source ./tpu-mlir/envsetup.sh
after starting Docker, otherwise errors may occur in subsequent steps.
2. Prepare the working directory in Docker
Create and enter the mobilenet_v2
working directory, note that it is a directory at the same level as tpu-mlir
.
# mkdir mobilenet_v2 && cd mobilenet_v2
Download the MobileNet model from the official website:
git clone https://github.com/shicai/MobileNet-Caffe.git
Place the model files in the cloned MobileNet-Caffe
directory and the image files in the tpu-mlir
tool chain directory into the current directory.
# cp MobileNet-Caffe/mobilenet_v2_deploy.prototxt .
# cp MobileNet-Caffe/mobilenet_v2.caffemodel .
# cp -rf ${TPUC_ROOT}/regression/dataset/ILSVRC2012/ .
# cp -rf ${TPUC_ROOT}/regression/image/ .
${TPUC_ROOT}
here is an environment variable, corresponding to the tpu-mlir
directory, which is loaded in the source ./tpu-mlir/envsetup.sh
step in the previous configuration of the Docker development environment.
Create and enter the work
working directory to store compiled files such as MLIR
and cvimodel
# mkdir work && cd work
3. MobileNet-Caffe Model Conversion
The Duo development board is equipped with the CV1800B chip, which supports the ONNX series and Caffe models. Currently, it does not support TFLite models. In terms of quantized data types, it supports quantization in BF16 format and asymmetric quantization in INT8 format.
The steps for model conversion are as follows:
- Convert Caffe model to MLIR
- Generate calibration tables required for quantification
- MLIR quantization into INT8 asymmetric cvimodel
Caffe model converted to MLIR
The model input are pictures. Before converting the model, we need to understand the preprocessing of the model. If the model uses preprocessed npz files as input, there is no need to consider preprocessing. The preprocessing process is expressed as follows ($x$ represents the input): $$ y = (x-mean)\times scale $$
The model in this example is BGR input, mean
and scale
are 103.94
, 116.78
, 123.68
and 0.017
, 0.017
, 0.017
respectively. The model conversion command is as follows:
model_transform.py \
--model_name mobilenet_v2 \
--model_def ../mobilenet_v2_deploy.prototxt \
--model_data ../mobilenet_v2.caffemodel \
--input_shapes [[1,3,224,224]] \
--resize_dims=256,256 \
--mean 103.94,116.78,123.68 \
--scale 0.017,0.017,0.017 \
--pixel_format bgr \
--test_input ../image/cat.jpg \
--test_result mobilenet_v2_top_outputs.npz \
--mlir mobilenet_v2.mlir
Example of successful operation
After converting to the MLIR model, a mobilenet_v2.mlir
file will be generated, which is the MLIR model file. A mobilenet_v2_in_f32.npz
file and a mobilenet_v2_top_outputs.npz
file will also be generated, which are the input files for subsequent model conversion.
MLIR to INT8 model
Generate calibration tables required for quantification
Run run_calibration.py
to get the calibration table. The number of input data should be about 100~1000 pieces depending on the situation. Here we use the existing 100 images from ILSVRC2012 as an example and execute the calibration command:
run_calibration.py mobilenet_v2.mlir \
--dataset ../ILSVRC2012 \
--input_num 100 \
-o mobilenet_v2_cali_table