TF2 [Object Detection API] Converting mobilenet-SSD models into .tflite uint8 format

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

TF2 [Object Detection API] Converting mobilenet-SSD models into .tflite uint8 format

2,224 Views
gnar_fang
NXP Employee
NXP Employee

1. Prepare the environment

pip install tensorflow==2.5.0

2. Install  tf2 Object detect API

Make sure you have protobuf compiler version >= 3.0, by typing protoc --version, or install it on Ubuntu by typing apt install protobuf-compiler.

git clone https://github.com/tensorflow/models.git

# remember to activate your python environment first
cd models/research
# compile protos:
protoc object_detection/protos/*.proto --python_out=.
# Install TensorFlow Object Detection API as a python package:
cp object_detection/packages/tf2/setup.py .
python -m pip install .

3. export tflite graph and convert to quante tflite module.

1) $cd models
   $wget http://download.tensorflow.org/models/object_detection/tf2/20200711/
ssd_mobilenet_v2_320x320_coco17_tpu-8.tar.gz
2)tar -zxvf ssd_mobilenet_v2_320x320_coco17_tpu-8.tar.gz
3)tar -zxvf convert.tar.gz

3.1   modify export_tflite.sh

export_tflite.sh
1 model_dir=../ssd_mobilenet_v2_320x320_coco17_tpu-8 <--pre-trained model path 
2 out_dir=$model_dir/exported_tflite
3 mkdir -p $out_dir


3.2  export tflite graph

./export_tflite.sh

Use EIQ toolkit  model tool to open the saved model, the input tensor shape is 300x300

gnar_fang_0-1632395542817.png

3.3  modify convert.py

line 3 TEST_DIR       The parent directory of the sample images
line 4  IMAGE_SIZE  The model input tensor shape
line 8 quante              The sample images folder
 
3.4  run converter script.
python convert.py

 

4. test performance on imx8mpevk

root@imx8mpevk:/usr/bin/tensorflow-lite-2.5.0/examples# ./label_image -m ~/ssd_mobilenet_v2_quant.tflite -a 1
INFO: Applied NNAPI delegate.
INFO: invoked
INFO: average time: 27.365 ms

 

 

Labels (1)
0 Replies