Centernet 移动端的优化
简介
centernet 是一个很简洁的目标检测框架,原理很简单,关键点估计加宽高回归。
文章不介绍算法细节, 主要是一个centernet在端侧的优化。
backbone 采用了mobilenetv3,但是只改backbone 提速不够,head 还是比较重的,所以采用可分离卷积来实现,并且采用了类似inception的结构,并把深层和浅层的特征进行了融合,取得了还算可以的效果,当然如果不关注小目标,还是可以继续优化的。
centernet 思路是十分简单的, heatmap 找出目标的中心,wh 找出该中心的宽高, 核心就是如此。至于loss 函数,这些都是为了
source
pretrained model , and preformance
mscoco
model | input_size | map | map@0.5 | map@0.75 |
---|---|---|---|---|
mbv3-large-0.75-modified_head | 512x512 | 0.225 | 0.401 | 0.224 |
fddb
model | input_size | fddb |
---|---|---|
mbv3-small-minimalistic_modified | 512x512 | 0.95 |
widerface
model | input_size | wider easy | wider medium | wider hard |
---|---|---|---|---|
mbv3-small-minimalistic_modified | 512x512 | 0.842 | 0.790 | 0.455 |
requirment
-
tensorflow 1.14
-
tensorpack 0.9.9 (for data provider)
-
opencv
-
python 3.6
-
MNNConverter
-
tfcoreml
使用方法
MSCOCO
train
-
下载数据download mscoco data, 然后
python prepare_coco_data.py --mscocodir ./mscoco
. (数据格式还是img_path| x1,y1,x2,y2,label… ( label 从0 开始),如果训练自己数据就这样来就行) -
下载预训练模型
mbv3-large0.75
解压 -
改 config=mb3_config in train_config.py, 然后:
python train.py
如果想可视化一下数据 可以设置 confifs/mscoco/mbv3_config.py 为 True
-
训练完, freezeas .pb by
python tools/auto_freeze.py --pretrained_mobile ./model/yourmodel.ckpt
it will produce a detector.pb
evaluation
python model_eval/custome_eval.py [--model [TRAINED_MODEL]] [--annFile [cocostyle annFile]]
[--imgDir [the images dir]] [--is_show [show the result]]
python model_eval/custome_eval.py --model model/detector.pb
--annFile ../mscoco/annotations/instances_val2017.json
--imgDir ../mscoco/val2017
--is_show 1
ps, no test time augmentation is used.
FACE
train
-
下载 widerface data http://shuoyang1213.me/WIDERFACE/,
and release the WIDER_train, WIDER_val and wider_face_split into ./WIDER,
then run python prepare_wider_data.py -
download pretrained model from mbv3-small-minimalistic
release it in current dir -
then, modify in config=face_config in train_config.py, then run:
python train.py
and if u want to check the data when training, u could set vis in confifs/mscoco/mbv3_config.py as True
-
After training, freeze the model as .pb by
python tools/auto_freeze.py --pretrained_mobile ./model/yourmodel.ckpt
it will produce a detector.pb
evaluation
** fddb **
python model_eval/fddb.py [--model [TRAINED_MODEL]] [--data_dir [DATA_DIR]]
[--split_dir [SPLIT_DIR]] [--result [RESULT_DIR]]
--model Path of the saved model,default ./model/detector.pb
--data_dir Path of fddb all images
--split_dir Path of fddb folds
--result Path to save fddb results
example python model_eval/fddb.py --model model/detector.pb --data_dir 'fddb/img/' --split_dir fddb/FDDB-folds/ --result 'result/'
** widerface **
python model_eval/wider.py [--model [TRAINED_MODEL]] [--data_dir [DATA_DIR]]
[--result [RESULT_DIR]]
--model Path of the saved model,default ./model/detector.pb
--data_dir Path of WIDER
--result Path to save WIDERface results
example python model_eval/wider.py --model model/detector.pb --data_dir 'WIDER/WIDER_val/' --result 'result/'
finetune
- 下载模型
然后改config config.MODEL.pretrained_model=‘yourmodel.ckpt’,
config.MODEL.continue_train=True python train.py
visualization
把图固定,其实很多人用tensorflow,都不太熟悉,导致转换模型的时候总是各种op报错,需要把training_flag 写死成false, 在freeze pb的时候才会进行图优化,从而把一些op fuse,最常见的就是batchnorm这个op,很多人工作多少年了,batchnorm都说不清。
python tools/auto_freeze.py --pretrained_model your.ckpt
, 会产生一个detector.pb
python visualization/vis.py
model convert for mobile device
后处理已经经过适配了,在MNN和coreml上都可以端到端的转换
4.1 MNN
+ 4.1.1 convert model
直接用mnn的api,
`./MNNConvert -f TF --modelFile detector.pb --MNNModel centernet.mnn --bizCode biz --fp16 1`
+ 4.1.2 visualization with mnn python wrapper
`python visualization/vis_with_mnn.py --mnn_model centernet.mnn --imgDir 'your image dir'`
4.2 coreml
+ 4.2.1 convert
`python tools/converter_to_coreml.py`
+ 4.2.2 visualization with coreml python wrapper
`python visualization/vis_with_coreml.py --coreml_model centernet.mlmodel --imgDir 'your image dir'`
ps, if you want to do quantization, please reffer to the official doc, it is easy.
TODO:
- Android project.