xiaomoguhzz commited on
Commit
3951436
·
verified ·
1 Parent(s): add3caf

Upload code/detection_trt/README.md with huggingface_hub

Browse files
Files changed (1) hide show
  1. code/detection_trt/README.md +253 -0
code/detection_trt/README.md ADDED
@@ -0,0 +1,253 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # TRT 加速检测器性能对比实验
2
+
3
+ 本模块用于对比 DeCLIP (csa模式) 和 CLIP (vanilla模式) 在 TensorRT 加速前后的速度和精度。
4
+
5
+ ## 目录结构
6
+
7
+ ```
8
+ detection_trt/
9
+ ├── README.md # 本文档
10
+ ├── install_mmdeploy.sh # MMDeploy 安装脚本
11
+ ├── configs/
12
+ │ ├── __init__.py
13
+ │ ├── fvit_tensorrt_fp16.py # TRT FP16 部署配置
14
+ │ └── custom_modules.py # 自定义模块注册 (ONNX 导出适配)
15
+ ├── convert_model.py # 模型转换脚本 (PyTorch -> ONNX -> TRT)
16
+ ├── benchmark_speed.py # 速度测试脚本
17
+ ├── eval_panoptic.py # COCO Panoptic 评估脚本
18
+ ├── run_all.sh # 一键运行脚本
19
+ ├── engines/ # TRT 引擎输出目录
20
+ └── results/ # 评估结果输出目录
21
+ ```
22
+
23
+ ## 实验矩阵
24
+
25
+ | 模型 | Backbone Mode | 推理后端 | 精度 | COCO Panoptic | 速度 |
26
+ |------|---------------|----------|------|---------------|------|
27
+ | CLIP | vanilla | PyTorch FP32 | 测试 | 测试 | 测试 |
28
+ | CLIP | vanilla | PyTorch FP16 | 测试 | 测试 | 测试 |
29
+ | CLIP | vanilla | TRT-FP16 | 测试 | 测试 | 测试 |
30
+ | DeCLIP | csa | PyTorch FP32 | 测试 | 测试 | 测试 |
31
+ | DeCLIP | csa | PyTorch FP16 | 测试 | 测试 | 测试 |
32
+ | DeCLIP | csa | TRT-FP16 | 测试 | 测试 | 测试 |
33
+
34
+ ## 依赖安装
35
+
36
+ ### 1. 基础依赖
37
+
38
+ ```bash
39
+ pip install onnx onnxruntime onnx-simplifier
40
+ ```
41
+
42
+ ### 2. TensorRT
43
+
44
+ TensorRT 需要与 CUDA 版本匹配:
45
+
46
+ ```bash
47
+ # CUDA 12.x
48
+ pip install tensorrt
49
+
50
+ # 或者从 NVIDIA 官网下载安装
51
+ # https://developer.nvidia.com/tensorrt
52
+ ```
53
+
54
+ ### 3. PyCUDA (TRT 推理需要)
55
+
56
+ ```bash
57
+ pip install pycuda
58
+ ```
59
+
60
+ ### 4. MMDeploy (可选)
61
+
62
+ ```bash
63
+ bash install_mmdeploy.sh
64
+ ```
65
+
66
+ ## 快速开始
67
+
68
+ ### 一键运行
69
+
70
+ ```bash
71
+ cd detection_trt
72
+ bash run_all.sh
73
+ ```
74
+
75
+ ### 分步运行
76
+
77
+ #### 1. 模型转换
78
+
79
+ ```bash
80
+ # 转换 CLIP (vanilla 模式)
81
+ python convert_model.py \
82
+ --model-type clip \
83
+ --mode vanilla \
84
+ --checkpoint /path/to/EVA02_CLIP_B_psz16_s8B.pt \
85
+ --image-size 560 \
86
+ --fp16 \
87
+ --verify
88
+
89
+ # 转换 DeCLIP (csa 模式)
90
+ python convert_model.py \
91
+ --model-type declip \
92
+ --mode csa \
93
+ --checkpoint /path/to/EVA02_CLIP_B_psz16_s8B.pt \
94
+ --image-size 560 \
95
+ --fp16 \
96
+ --verify
97
+ ```
98
+
99
+ #### 2. 速度测试
100
+
101
+ ```bash
102
+ # PyTorch 速度测试
103
+ python benchmark_speed.py \
104
+ --pytorch-model /path/to/checkpoint.pt \
105
+ --mode csa \
106
+ --image-size 560 560
107
+
108
+ # TensorRT 速度测试
109
+ python benchmark_speed.py \
110
+ --trt-engine engines/declip_csa_560_fp16.engine \
111
+ --mode csa \
112
+ --image-size 560 560
113
+ ```
114
+
115
+ #### 3. 精度评估
116
+
117
+ ```bash
118
+ # PyTorch 精度评估
119
+ python eval_panoptic.py \
120
+ --backend pytorch \
121
+ --checkpoint /path/to/checkpoint.pt \
122
+ --mode csa \
123
+ --coco-root /path/to/coco
124
+
125
+ # TensorRT 精度评估
126
+ python eval_panoptic.py \
127
+ --backend tensorrt \
128
+ --engine engines/declip_csa_560_fp16.engine \
129
+ --mode csa \
130
+ --coco-root /path/to/coco
131
+ ```
132
+
133
+ ## 配置说明
134
+
135
+ ### fvit_tensorrt_fp16.py
136
+
137
+ 主要配置项:
138
+
139
+ ```python
140
+ # TRT 后端配置
141
+ backend_config = dict(
142
+ type='tensorrt',
143
+ common_config=dict(fp16_mode=True),
144
+ model_inputs=[dict(input_shapes=dict(...))]
145
+ )
146
+
147
+ # EVA-CLIP 特定配置
148
+ evaclip_config = dict(
149
+ model_name='EVA02-CLIP-B-16',
150
+ image_size=560,
151
+ feature_mode='csa', # 或 'vanilla'
152
+ disable_xformers=True, # ONNX 导出需要禁用
153
+ )
154
+ ```
155
+
156
+ ### custom_modules.py
157
+
158
+ 处理 EVA-CLIP 的 ONNX 导出兼容性:
159
+
160
+ 1. **Attention.forward**: 禁用 xformers,使用标准 attention
161
+ 2. **Attention.ss_attn**: 处理 DeCLIP 的 csa 模式
162
+ 3. **encode_dense**: 简化推理逻辑
163
+
164
+ ## 输出说明
165
+
166
+ ### 速度测试结果
167
+
168
+ ```json
169
+ {
170
+ "model_name": "EVA02-CLIP-B-16",
171
+ "mode": "csa",
172
+ "backend": "tensorrt",
173
+ "precision": "fp16",
174
+ "image_size": [560, 560],
175
+ "batch_size": 1,
176
+ "latency_mean_ms": 12.5,
177
+ "latency_std_ms": 0.3,
178
+ "throughput_fps": 80.0
179
+ }
180
+ ```
181
+
182
+ ### 精度评估结果
183
+
184
+ ```json
185
+ {
186
+ "model_name": "EVA02-CLIP-B-16",
187
+ "mode": "csa",
188
+ "backend": "pytorch",
189
+ "rois_thing_macc1": 0.75,
190
+ "rois_thing_macc5": 0.92,
191
+ "rois_stuff_macc1": 0.68,
192
+ "rois_stuff_macc5": 0.88
193
+ }
194
+ ```
195
+
196
+ ## 关键技术点
197
+
198
+ ### 1. ONNX 导出适配
199
+
200
+ EVA-CLIP 使用了一些不兼容 ONNX 的操作:
201
+
202
+ - **xformers**: 需要禁用,使用标准 PyTorch attention
203
+ - **RoPE 位置编码**: 需要处理动态形状
204
+ - **环境变量检查**: 需要移除 `os.getenv()` 调用
205
+
206
+ ### 2. 动态形状支持
207
+
208
+ TRT 引擎支持动态输入尺寸:
209
+
210
+ ```python
211
+ profile.set_shape(input_name,
212
+ min_shape=(1, 3, 560, 560),
213
+ opt_shape=(1, 3, 560, 560),
214
+ max_shape=(1, 3, 800, 1333)
215
+ )
216
+ ```
217
+
218
+ ### 3. 精度对齐
219
+
220
+ TRT 转换后需要验证输出与 PyTorch 一致:
221
+
222
+ - FP32: 误差 < 1e-5
223
+ - FP16: 误差 < 1e-3
224
+
225
+ ## 常见问题
226
+
227
+ ### Q1: ONNX 导��失败
228
+
229
+ 检查是否禁用了 xformers:
230
+
231
+ ```python
232
+ from configs.custom_modules import disable_xformers_for_export
233
+ model = disable_xformers_for_export(model)
234
+ ```
235
+
236
+ ### Q2: TRT 构建失败
237
+
238
+ 1. 检查 CUDA/TensorRT 版本匹配
239
+ 2. 增加 workspace 大小: `--workspace 8`
240
+ 3. 检查输入形状是否合理
241
+
242
+ ### Q3: 精度下降明显
243
+
244
+ 1. 检查是否正确加载了模型权重
245
+ 2. 尝试使用 FP32 精度进行对比
246
+ 3. 检查输入预处理是否一致
247
+
248
+ ## 参考
249
+
250
+ - [MMDeploy 文档](https://mmdeploy.readthedocs.io/)
251
+ - [TensorRT 文档](https://docs.nvidia.com/deeplearning/tensorrt/)
252
+ - [EVA-CLIP 代码](https://github.com/baaivision/EVA)
253
+ - [DeCLIP 论文](https://arxiv.org/)