Skip to content

Commit 0773fff

Browse files
committed
ncnn
1 parent e81fb12 commit 0773fff

File tree

4 files changed

+35
-21
lines changed

4 files changed

+35
-21
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,9 @@ pretrained/*
117117
run.sh
118118
openvino/build/*
119119
openvino/output*
120+
ncnn/models/*
120121
*.onnx
121122
tis/cpp_client/build/*
123+
log*txt
122124

123125
tvm/

lib/models/resnet.py

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
resnet18_url = 'https://download.pytorch.org/models/resnet18-5c106cde.pth'
1010

11+
1112
from torch.nn import BatchNorm2d
1213

1314

ncnn/README.md

+25-18
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11

2-
### My platform
2+
### My platform
33

44
* raspberry pi 3b
55
* 2022-04-04-raspios-bullseye-armhf-lite.img
66
* cpu: 4 core armv8, memory: 1G
77

88

99

10-
### Install ncnn
10+
### Install ncnn
1111

12-
#### 1. dependencies
13-
```
14-
$ python -m pip install onnx-simplifier
15-
```
16-
17-
#### 2. build ncnn
1812
Just follow the ncnn official tutoral of [build-for-linux](https://github.com/Tencent/ncnn/wiki/how-to-build#build-for-linux) to install ncnn. Following steps are all carried out on my raspberry pi:
1913

2014
**step 1:** install dependencies
@@ -25,21 +19,26 @@ $ sudo apt install build-essential git cmake libprotobuf-dev protobuf-compiler l
2519
**step 2:** (optional) install vulkan
2620

2721
**step 3:** build
28-
I am using commit `5725c028c0980efd`, and I have not tested over other commits.
22+
I am using commit `6869c81ed3e7170dc0`, and I have not tested over other commits.
2923
```
3024
$ git clone https://github.com/Tencent/ncnn.git
3125
$ cd ncnn
32-
$ git reset --hard 5725c028c0980efd
26+
$ git reset --hard 6869c81ed3e7170dc0
3327
$ git submodule update --init
3428
$ mkdir -p build
3529
$ cmake -DCMAKE_BUILD_TYPE=Release -DNCNN_VULKAN=OFF -DNCNN_BUILD_TOOLS=ON -DCMAKE_TOOLCHAIN_FILE=../toolchains/pi3.toolchain.cmake ..
3630
$ make -j2
3731
$ make install
3832
```
3933

40-
### Convert model, build and run the demo
34+
### Convert pytorch model to ncnn model
4135

42-
#### 1. convert pytorch model to ncnn model via onnx
36+
#### 1. dependencies
37+
```
38+
$ python -m pip install onnx-simplifier
39+
```
40+
41+
#### 2. convert pytorch model to ncnn model via onnx
4342
On your training platform:
4443
```
4544
$ cd BiSeNet/
@@ -52,13 +51,21 @@ Then copy your `model_v2_sim.onnx` from training platform to raspberry device.
5251
On raspberry device:
5352
```
5453
$ /path/to/ncnn/build/tools/onnx/onnx2ncnn model_v2_sim.onnx model_v2_sim.param model_v2_sim.bin
55-
$ cd BiSeNet/ncnn/
56-
$ mkdir -p models
57-
$ mv model_v2_sim.param models/
58-
$ mv model_v2_sim.bin models/
5954
```
6055

61-
#### 2. compile demo code
56+
You can optimize the ncnn model by fusing the layers and save the weights with fp16 datatype.
57+
On raspberry device:
58+
```
59+
$ /path/to/ncnn/build/tools/ncnnoptimize model_v2_sim.param model_v2_sim.bin model_v2_sim_opt.param model_v2_sim_opt.bin 65536
60+
$ mv model_v2_sim_opt.param model_v2_sim.param
61+
$ mv model_v2_sim_opt.bin model_v2_sim.bin
62+
```
63+
64+
You can also quantize the model for int8 inference, following this [tutorial](https://github.com/Tencent/ncnn/wiki/quantized-int8-inference). Make sure your device support int8 inference.
65+
66+
67+
### build and run the demo
68+
#### 1. compile demo code
6269
On raspberry device:
6370
```
6471
$ mkdir -p BiSeNet/ncnn/build
@@ -67,7 +74,7 @@ $ cmake .. -DNCNN_ROOT=/path/to/ncnn/build/install
6774
$ make
6875
```
6976

70-
#### 3. run demo
77+
#### 2. run demo
7178
```
7279
./segment
7380
```

ncnn/segment.cpp

+7-3
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,16 @@ void inference() {
5151
mod.opt.use_vulkan_compute = 1;
5252
mod.set_vulkan_device(1);
5353
#endif
54-
// ncnn enable fp16 by default, so we do not need these options
55-
// int8 depends on the model itself, so we do not set here
54+
//// switch off fp16
5655
// bool use_fp16 = false;
5756
// mod.opt.use_fp16_packed = use_fp16;
5857
// mod.opt.use_fp16_storage = use_fp16;
5958
// mod.opt.use_fp16_arithmetic = use_fp16;
59+
//// switch on bf16
60+
// mod.opt.use_packing_layout = true;
61+
// mod.opt.use_ff16_storage = true;
62+
//// reduce cpu usage
63+
// net.opt.openmp_blocktime = 0;
6064
mod.opt.use_winograd_convolution = true;
6165

6266
// we should set opt before load model
@@ -78,7 +82,7 @@ void inference() {
7882

7983
// set input, run, get output
8084
ncnn::Extractor ex = mod.create_extractor();
81-
ex.set_light_mode(true); // not sure what this mean
85+
ex.set_light_mode(true);
8286
ex.set_num_threads(nthreads);
8387
#if NCNN_VULKAN
8488
ex.set_vulkan_compute(true);

0 commit comments

Comments
 (0)