happyme531
commited on
Upload 6 files
Browse files- .gitattributes +1 -0
- README.md +139 -3
- convert_rknn.py +66 -0
- input.jpg +0 -0
- model.rknn +3 -0
- run_rknn.py +88 -0
- selected_tags.csv +0 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
model.rknn filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,3 +1,139 @@
|
|
1 |
-
---
|
2 |
-
license: agpl-3.0
|
3 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
---
|
2 |
+
license: agpl-3.0
|
3 |
+
base_model: SmilingWolf/wd-convnext-tagger-v3
|
4 |
+
tags:
|
5 |
+
- rknn
|
6 |
+
---
|
7 |
+
|
8 |
+
# WD ConvNext Tagger v3 RKNN2
|
9 |
+
|
10 |
+
## (English README see below)
|
11 |
+
|
12 |
+
在RK3588上运行WaifuDiffusion图像标签模型!
|
13 |
+
|
14 |
+
- 推理速度(RK3588):
|
15 |
+
- 单NPU核: 320ms
|
16 |
+
|
17 |
+
- 内存占用(RK3588):
|
18 |
+
- 0.45GB
|
19 |
+
|
20 |
+
## 使用方法
|
21 |
+
|
22 |
+
1. 克隆或者下载此仓库到本地
|
23 |
+
|
24 |
+
2. 安装依赖
|
25 |
+
|
26 |
+
```bash
|
27 |
+
pip install numpy<2 pandas opencv-python rknn-toolkit-lite2
|
28 |
+
```
|
29 |
+
|
30 |
+
3. 运行
|
31 |
+
|
32 |
+
```bash
|
33 |
+
python run_rknn.py input.jpg
|
34 |
+
```
|
35 |
+
|
36 |
+
输出结果示例:
|
37 |
+
```log
|
38 |
+
tag_id name probs
|
39 |
+
0 9999999 general 0.521484
|
40 |
+
5 212816 solo 0.929199
|
41 |
+
12 15080 short_hair 0.520508
|
42 |
+
25 540830 1boy 0.947754
|
43 |
+
40 16613 jewelry 0.577148
|
44 |
+
72 1300281 male_focus 0.907227
|
45 |
+
130 10926 pants 0.803223
|
46 |
+
346 1094664 colored_skin 0.570312
|
47 |
+
373 4009 turtleneck 0.552246
|
48 |
+
1532 1314823 black_sweater 0.514160
|
49 |
+
```
|
50 |
+
|
51 |
+
## 模型转换
|
52 |
+
|
53 |
+
1. 安装依赖
|
54 |
+
|
55 |
+
```bash
|
56 |
+
pip install numpy<2 onnxruntime rknn-toolkit2
|
57 |
+
```
|
58 |
+
|
59 |
+
2. 下载原始onnx模型
|
60 |
+
|
61 |
+
3. 转换onnx模型到rknn模型:
|
62 |
+
|
63 |
+
```bash
|
64 |
+
python convert_rknn.py
|
65 |
+
```
|
66 |
+
|
67 |
+
## 已知问题
|
68 |
+
|
69 |
+
- int8量化后精度损失极大, 基本不可用. 不建议使用量化推理.
|
70 |
+
|
71 |
+
## 参考
|
72 |
+
|
73 |
+
- [SmilingWolf/wd-convnext-tagger-v3](https://huggingface.co/SmilingWolf/wd-convnext-tagger-v3)
|
74 |
+
|
75 |
+
## English README
|
76 |
+
|
77 |
+
Run efficient image tagging model on RK3588!
|
78 |
+
|
79 |
+
- Inference Speed (RK3588):
|
80 |
+
- Single NPU Core: 320ms
|
81 |
+
|
82 |
+
- Memory Usage (RK3588):
|
83 |
+
- 0.45GB
|
84 |
+
|
85 |
+
## Usage
|
86 |
+
|
87 |
+
1. Clone or download this repository
|
88 |
+
|
89 |
+
2. Install dependencies
|
90 |
+
|
91 |
+
```bash
|
92 |
+
pip install numpy<2 pandas opencv-python rknn-toolkit-lite2
|
93 |
+
```
|
94 |
+
|
95 |
+
3. Run
|
96 |
+
|
97 |
+
```bash
|
98 |
+
python run_rknn.py input.jpg
|
99 |
+
```
|
100 |
+
|
101 |
+
Output example:
|
102 |
+
```log
|
103 |
+
tag_id name probs
|
104 |
+
0 9999999 general 0.521484
|
105 |
+
5 212816 solo 0.929199
|
106 |
+
12 15080 short_hair 0.520508
|
107 |
+
25 540830 1boy 0.947754
|
108 |
+
40 16613 jewelry 0.577148
|
109 |
+
72 1300281 male_focus 0.907227
|
110 |
+
130 10926 pants 0.803223
|
111 |
+
346 1094664 colored_skin 0.570312
|
112 |
+
373 4009 turtleneck 0.552246
|
113 |
+
1532 1314823 black_sweater 0.514160
|
114 |
+
```
|
115 |
+
|
116 |
+
|
117 |
+
## Model Conversion
|
118 |
+
|
119 |
+
1. Install dependencies
|
120 |
+
|
121 |
+
```bash
|
122 |
+
pip install numpy<2 onnxruntime rknn-toolkit2
|
123 |
+
```
|
124 |
+
|
125 |
+
2. Download original onnx model
|
126 |
+
|
127 |
+
3. Convert onnx model to rknn model:
|
128 |
+
|
129 |
+
```bash
|
130 |
+
python convert_rknn.py
|
131 |
+
```
|
132 |
+
|
133 |
+
## Known Issues
|
134 |
+
|
135 |
+
- Huge precision loss after int8 quantization, not recommended to use quantized inference.
|
136 |
+
|
137 |
+
## References
|
138 |
+
|
139 |
+
- [SmilingWolf/wd-convnext-tagger-v3](https://huggingface.co/SmilingWolf/wd-convnext-tagger-v3)
|
convert_rknn.py
ADDED
@@ -0,0 +1,66 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
#!/usr/bin/env python
|
2 |
+
# coding: utf-8
|
3 |
+
|
4 |
+
import datetime
|
5 |
+
from rknn.api import RKNN
|
6 |
+
from sys import exit
|
7 |
+
import argparse
|
8 |
+
|
9 |
+
|
10 |
+
ONNX_MODEL="model.onnx"
|
11 |
+
detailed_performance_log = True
|
12 |
+
|
13 |
+
# 添加命令行参数解析
|
14 |
+
parser = argparse.ArgumentParser(description='将ONNX模型转换为RKNN模型')
|
15 |
+
parser.add_argument('--quantize', '-q', action='store_true',
|
16 |
+
help='是否对模型进行量化')
|
17 |
+
parser.add_argument('--dataset', '-d',
|
18 |
+
default="/home/zt/rk3588-nn/rknn_model_zoo/datasets/COCO/coco_subset_20.txt",
|
19 |
+
help='量化校准数据集的路径')
|
20 |
+
args = parser.parse_args()
|
21 |
+
|
22 |
+
DATASET=args.dataset # 使用命令行参数指定数据集路径
|
23 |
+
QUANTIZE=args.quantize
|
24 |
+
|
25 |
+
# 根据是否量化决定输出文件名
|
26 |
+
if args.quantize:
|
27 |
+
RKNN_MODEL=ONNX_MODEL.replace(".onnx","_int8.rknn")
|
28 |
+
else:
|
29 |
+
RKNN_MODEL=ONNX_MODEL.replace(".onnx",".rknn")
|
30 |
+
|
31 |
+
timedate_iso = datetime.datetime.now().isoformat()
|
32 |
+
|
33 |
+
rknn = RKNN(verbose=True)
|
34 |
+
rknn.config(
|
35 |
+
# mean_values=[x * 255 for x in [0.485, 0.456, 0.406]],
|
36 |
+
# std_values=[x * 255 for x in [0.229, 0.224, 0.225]],
|
37 |
+
quantized_dtype='w8a8',
|
38 |
+
quantized_algorithm='kl_divergence',
|
39 |
+
quantized_method='channel',
|
40 |
+
quantized_hybrid_level=0,
|
41 |
+
target_platform='rk3588',
|
42 |
+
quant_img_RGB2BGR = False,
|
43 |
+
float_dtype='float16',
|
44 |
+
optimization_level=3,
|
45 |
+
custom_string=f"converted by: qq: 232004040, email: 2302004040@qq.com at {timedate_iso}",
|
46 |
+
remove_weight=False,
|
47 |
+
compress_weight=False,
|
48 |
+
inputs_yuv_fmt=None,
|
49 |
+
single_core_mode=False,
|
50 |
+
dynamic_input=None,
|
51 |
+
model_pruning=False,
|
52 |
+
op_target=None,
|
53 |
+
quantize_weight=False,
|
54 |
+
remove_reshape=False,
|
55 |
+
sparse_infer=False,
|
56 |
+
enable_flash_attention=False,
|
57 |
+
)
|
58 |
+
|
59 |
+
ret = rknn.load_onnx(model=ONNX_MODEL, inputs=["/Transpose_output_0"], input_size_list=[[1,3,448,448]])
|
60 |
+
ret = rknn.build(do_quantization=QUANTIZE, dataset=DATASET, rknn_batch_size=None)
|
61 |
+
ret = rknn.export_rknn(RKNN_MODEL)
|
62 |
+
|
63 |
+
# ret = rknn.init_runtime(target='rk3588',device_id='cbb956772bf5dac9',core_mask=RKNN.NPU_CORE_0,perf_debug=detailed_performance_log)
|
64 |
+
# rknn.eval_perf()
|
65 |
+
ret = rknn.accuracy_analysis(inputs=['img.npy'], target='rk3588')
|
66 |
+
|
input.jpg
ADDED
model.rknn
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:563126adc90241146f3d736593c2f86f203d1f4b539c2f7b44f937828b69cabc
|
3 |
+
size 204725760
|
run_rknn.py
ADDED
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import sys
|
3 |
+
import time
|
4 |
+
import cv2
|
5 |
+
import numpy as np
|
6 |
+
import pandas as pd
|
7 |
+
from rknnlite.api import RKNNLite
|
8 |
+
|
9 |
+
pd.set_option("display.max_rows", 1000)
|
10 |
+
|
11 |
+
dim = 448
|
12 |
+
thresh = 0.5
|
13 |
+
|
14 |
+
# 初始化RKNNLite
|
15 |
+
rknn_lite = RKNNLite(verbose=False)
|
16 |
+
|
17 |
+
# 加载RKNN模型
|
18 |
+
ret = rknn_lite.load_rknn("model.rknn")
|
19 |
+
if ret != 0:
|
20 |
+
print('加载RKNN模型失败')
|
21 |
+
exit(ret)
|
22 |
+
|
23 |
+
# 初始化运行时环境
|
24 |
+
ret = rknn_lite.init_runtime()
|
25 |
+
if ret != 0:
|
26 |
+
print('初始化运行时环境失败')
|
27 |
+
exit(ret)
|
28 |
+
|
29 |
+
label_names = pd.read_csv("selected_tags.csv")
|
30 |
+
|
31 |
+
target_img = "input.jpg" if len(sys.argv) < 2 else sys.argv[1]
|
32 |
+
|
33 |
+
try:
|
34 |
+
# 图像预处理
|
35 |
+
# 1. 读取图像并转RGB
|
36 |
+
img = cv2.imread(target_img)
|
37 |
+
if img is None:
|
38 |
+
print(f"无法读取图像: {target_img}")
|
39 |
+
exit(1)
|
40 |
+
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
41 |
+
|
42 |
+
# 2. 处理透明通道
|
43 |
+
if img.shape[2] == 4: # RGBA图像
|
44 |
+
img = img[:, :, :3] # 只保留RGB通道
|
45 |
+
elif len(img.shape) == 2: # 灰度图
|
46 |
+
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB)
|
47 |
+
|
48 |
+
# 3. 填充为正方形
|
49 |
+
h, w = img.shape[:2]
|
50 |
+
if h > w:
|
51 |
+
diff = h - w
|
52 |
+
pad_left = diff // 2
|
53 |
+
pad_right = diff - pad_left
|
54 |
+
img = cv2.copyMakeBorder(img, 0, 0, pad_left, pad_right,
|
55 |
+
cv2.BORDER_CONSTANT, value=(255, 255, 255))
|
56 |
+
elif w > h:
|
57 |
+
diff = w - h
|
58 |
+
pad_top = diff // 2
|
59 |
+
pad_bottom = diff - pad_top
|
60 |
+
img = cv2.copyMakeBorder(img, pad_top, pad_bottom, 0, 0,
|
61 |
+
cv2.BORDER_CONSTANT, value=(255, 255, 255))
|
62 |
+
|
63 |
+
# 4. resize到目标尺寸
|
64 |
+
img = cv2.resize(img, (dim, dim), interpolation=cv2.INTER_AREA)
|
65 |
+
|
66 |
+
# 5. 转换为float32并添加batch维度
|
67 |
+
img = img.astype(np.float32)
|
68 |
+
img = np.expand_dims(img, 0)
|
69 |
+
print(img.shape) # 1, 448, 448, 3
|
70 |
+
# NHWC -> NWCH
|
71 |
+
# img = np.transpose(img, (0, 2, 3, 1))
|
72 |
+
# 执行推理
|
73 |
+
# np.save("img.npy",img)
|
74 |
+
start = time.time()
|
75 |
+
outputs = rknn_lite.inference(inputs=[img], data_format="nhwc")
|
76 |
+
end = time.time()
|
77 |
+
print(f"Inference time: {end - start} seconds")
|
78 |
+
probs = outputs[0] # 获取第一个输出
|
79 |
+
print(probs.shape)
|
80 |
+
|
81 |
+
# 后处理
|
82 |
+
label_names["probs"] = probs[0]
|
83 |
+
found_tags = label_names[label_names["probs"] > thresh][["tag_id", "name", "probs"]]
|
84 |
+
print(found_tags)
|
85 |
+
|
86 |
+
finally:
|
87 |
+
# 释放资源
|
88 |
+
rknn_lite.release()
|
selected_tags.csv
ADDED
The diff for this file is too large to render.
See raw diff
|
|