forked from aim-uofa/AdelaiDet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpytorch-onnx-caffe-ncnn-rt.sh
89 lines (78 loc) · 2.65 KB
/
pytorch-onnx-caffe-ncnn-rt.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
if [ "$1" == "update" ];
then
update='update'
else
update=
fi
if [ "$1" == "test" ];
then
onnx_rt='test'
else
onnx_rt=
fi
caffe_repo=/data/pretrained/caffe/fcos/
onnx_repo=/data/pretrained/onnx/fcos/
pytorch_repo=/data/pretrained/pytorch/fcos/
ncnn_repo=/data/pretrained/ncnn/fcos/
config=configs/FCOS-Detection/FCOS_RT/MS_R_50_4x_syncbn_bn_head.yaml
case=FCOS_RT_MS_R_50_4x_syncbn_bn_head
if [ ! -e $onnx_repo/$case.onnx ] || [ "$update" != "" ];
then
cd /workspace/git/uofa-AdelaiDet/ # folder of project https://github.com/aim-uofa/AdelaiDet
pwd
python -V # ensure python3.x
python onnx/export_model_to_onnx.py \
--config-file $config \
--output $onnx_repo/$case.onnx \
--width 768 --height 640 \
--opts MODEL.WEIGHTS $pytorch_repo/$case.pth MODEL.FCOS.NORM "BN" MODEL.DEVICE cpu
if [ $? -ne 0 ]; then exit; fi
fi
if [ ! -e $onnx_repo/$case-update.onnx ] || [ "$update" != "" ];
then
# advise version 1.3.0
cd /workspace/git/onnx-simplifier # folder of project: https://github.com/daquexian/onnx-simplifier
pwd
python -V # ensure python3.x
python -m onnxsim $onnx_repo/$case.onnx $onnx_repo/$case-update.onnx
if [ $? -ne 0 ]; then exit; fi
fi
# optional
if [ ! -e $caffe_repo/$case-update.caffemodel ];
then
# switch to python2 and ensure caffe (with the upsample patch) ready
# refer: https://github.com/blueardour/caffe.git for patched version
cd /workspace/git/onnx2caffe # folder of project: https://github.com/MTlab/onnx2caffe
pwd
python -V
python convertCaffe.py $onnx_repo/$case-update.onnx $caffe_repo/$case-update.prototxt $caffe_repo/$case-update.caffemodel
if [ $? -ne 0 ]; then exit; fi
fi
# ncnn
if [ ! -e $ncnn_repo/$case-update-opt.bin ] || [ "$update" != "" ]
then
cd /workspace/git/ncnn # folder of project: https://github.com/Tencent/ncnn
pwd
mkdir -p $ncnn_repo
./build-host-gcc-linux/tools/onnx/onnx2ncnn $onnx_repo/$case-update.onnx $ncnn_repo/$case-update.param $ncnn_repo/$case-update.bin
if [ $? -eq 0 ]; then
echo "Optimizing"
./build-host-gcc-linux/tools/ncnnoptimize $ncnn_repo/$case-update.param $ncnn_repo/$case-update.bin \
$ncnn_repo/$case-update-opt.param $ncnn_repo/$case-update-opt.bin \
0 #data 640 512 3
else
echo "Convert failed"
fi
fi
if [ "$onnx_rt" == "test" ];
then
cd /workspace/git/uofa-AdelaiDet/ # folder of project https://github.com/aim-uofa/AdelaiDet
pwd
python -V # ensure python3.x
python onnx/test_onnxruntime.py \
--config-file $config \
--output $onnx_repo/$case-update.onnx \
--width 768 --height 640 \
--opts MODEL.WEIGHTS $pytorch_repo/$case.pth MODEL.FCOS.NORM "BN" MODEL.DEVICE cpu
if [ $? -ne 0 ]; then exit; fi
fi