修改yolov5的detect层,提高Triton推理服务的性能
Infer模式下, yolov5 默认的detect层输出的数据是一个形状为[batches, 25200, 85]的张量。如果部署在Nvidia Triton中,输出层的张量大小过大,处理输出的时间会变大,造成队列积压。 特别是在Triton Server和Client不在同一台机器,无法使用shared memory的情况下,通过网络将数据传输到client的时间还会变大,影响推理服务的性能。 相关代码链接
1. 测试方法
将模型转换为tensorrt engine, 并部署在Triton Inference Server,instance group数量为1,类型为GPU,在其他机器上通过Triton提供的perf_analyzer工具进行性能测试。
- 将yolov5s.pt转换为onnx格式
-
将onnx转换为tensorrt engine
/usr/src/tensorrt/bin/trtexec \ --onnx=yolov5s.onnx \ --minShapes=images:1x3x640x640 \ --optShapes=images:8x3x640x640 \ --maxShapes=images:32x3x640x640 \ --workspace=4096 \ --saveEngine=yolov5s.engine \ --shapes=images:1x3x640x640 \ --verbose \ --fp16 \ > result-FP16.txt -
部署在Triton Inference Server
模型上传到Triton server 设置的model repository路径,编写模型服务配置
-
python generate_input.py --input_images <image_path> ----output_file <real_data>.json -
利用真实数据进行性能测试
perf_analyzer -m <triton_model_name> -b 1 --input-data <real_data>.json --concurrency-range 1:10 --measurement-interval 10000 -u <triton server endpoint> -i gRPC -f <triton_model_name>.csv
2. 修改前的性能指标
如下为使用默认detect层的yolov5 trt engine, 部署在triton的性能测试结果,可以看到,使用默认的detect层,大量时间消耗在队列积压(Server Queue)和输出数据的处理(Server Compute Output),吞吐量甚至达不到 1 infer/sec
除了吞吐,其余指标的单位均为us, 其中Client Send和Client Recv分别为gRPC序列化、反序列化数据的时间
| Concurrency | Inferences/Second | Client Send | Network+Server Send/Recv | Server Queue | Server Compute Input | Server Compute Infer | Server Compute Output | p90 latency |
|---|---|---|---|---|---|---|---|---|
| 1 | 0.7 | 1683 | 1517232 | 466 | 8003 | 4412 | 9311 | 1592936 |
| 2 | 0.8 | 1464 | 1514475 | 393 | 10659 | 4616 | 956736 | 2583025 |
| 3 | 0.7 | 2613 | 1485868 | 1013992 | 7370 | 4396 | 1268070 | 3879331 |
| 4 | 0.7 | 2268 | 1463386 | 2230040 | 9933 | 5734 | 1250245 | 4983687 |
| 5 | 0.6 | 2064 | 1540583 | 3512025 | 11057 | 4843 | 1226058 | 6512305 |
| 6 | 0.6 | 2819 | 1573869 | 4802885 | 10134 | 4320 | 1234644 | 7888080 |
| 7 | 0.5 | 1664 | 1507386 | 6007235 | 11197 | 4899 | 1244482 | 8854777 |
因此,改造的一个方案就是将数据层进行精简,在送入nms之前根据conf对bbox进行粗略的筛选, 最后参考tensorrtx中对detect层的处理,将输出改造成形状为[batches, num_bboxes, 6]的向量, 其中num_bboxes=1000
6 = [cx,cy,w,h,conf,cls_id], 其中conf = obj_conf * cls_prob
3. 具体步骤
3.1 clone ultralytics yolov5 repo
git clone -b v6.1 https://github.com/ultralytics/yolov5.git
3.2 改造detect层
将detect的forward函数修改为
def forward(self, x):
z

博客围绕Yolov5在Infer模式下的性能优化展开。默认detect层输出数据处理时间长,影响推理服务性能。通过将模型转换为TensorRT engine并部署在Triton Inference Server进行测试,对detect层改造后,不同batch size下吞吐量等性能指标均有显著提升。
最低0.47元/天 解锁文章
1665

被折叠的 条评论
为什么被折叠?



