一直陷在物体检测的坑里出不来了。谁让这坑如此之深 ! 继续加点深度 ! 自己训练一个yolov5 object detect 模型 再跑跑 tensorRT 加速吧。
技术主题:
yolov5s 训练人手检测模型并使用 tensortRT 加速。
一 准备数据集
1.1 下载
https://www.robots.ox.ac.uk/~vgg/data/hands/
1.2 转换 yolo 需要的格式
参考:
https://github.com/coldlarry/YOLOv3-complete-pruning/blob/master/data/converter.py
1.3 扩充数据集
这个数据集下载下来只有 5000多张图片。 训练显得有点少。 怎么多块好省的步入共产主义? 写个脚本镜像一下把。 1w 数据集有了。
为训练准备 yolov5/data/hand.yaml
train: /workspace/data/hand_dataset/images/train/
val: /workspace/data/hand_dataset/images/validation/
# number of classes
nc: 1
# class names
names: ['hand']
二 训练
我是先到了一个终点栽了坑,又反向传播回到这里,建议:
jetson nano 下 跑训练的模型 img-size 就用 416 (为了检测速度快)
yolov5 为使用 3.1 版本 (为了能成功导出 tensorRT)
yolov5 下载:
https://github.com/ultralytics/yolov5/tags
同时下载 yolov5s.pt
yolov5/ 下执行:
python3 train.py --img 416 --batch 16 --epochs 300 --data hand.yaml --weights yolov5s.pt --cfg yolov5s.yaml --cache-images --single-cls
最后的训练结果:
P->0.8322 , Recall->0.9663 , map->0.6981
294/299 1.82G 0.02242 0.0224 0 0.04482 74 416 0.8299 0.9553 0.9667 0.6961 0.02342 0.02203 0
295/299 1.82G 0.02225 0.02221 0 0.04446 68 416 0.83 0.9553 0.9668 0.6966 0.02339 0.02202 0
296/299 1.82G 0.0224 0.02195 0 0.04435 50 416 0.8312 0.9553 0.9669 0.6975 0.02336 0.022 0
297/299 1.82G 0.02225 0.02205 0 0.0443 65 416 0.8326 0.9551 0.9668 0.6982 0.02334 0.02199 0
298/299 1.82G 0.02235 0.02194 0 0.04429 69 416 0.8325 0.9551 0.9667 0.6981 0.02331 0.02198 0
299/299 1.82G 0.02256 0.02214 0 0.0447 52 416 0.8322 0.9549 0.9663 0.6981 0.02329 0.02197 0
三 开发主机环境验证
python3 detect.py --source 0 --weights runs/exp10/weights/best.pt
VID
四 jetson nano 下部署验证
4.1 简单验证
把训练好的 模型 best.pt 放到 jetson nano下可跟开发环境同样验证。
测试速度 : 640x360@8fps
python3 detect.py --source test_dir/ --weights runs/exp10/weights/best.pt
4.2 yolov5s 转 onnx
python3 models/export.py --weights runs/exp10/weights/best.pt --img-size 416
得到 onnx 模型用 onnxruntime 运行. onnxruntime 编译了四个多小时 , 检测速度没多少提升呢。
# 部分代码
def main():
img_raw = cv2.imread('/tmp/in.jpg')
print(img_raw.shape)
t0 = time.time()
img,TestData = process_image_raw(img_raw)
t1 = time.time()
print("process img raw cost = %.1f ms " %( 1000 * (t1 - t0)))
session

本文介绍如何使用YOLOv5进行人手检测模型训练,并利用TensorRT进行加速部署的过程。主要内容包括数据集准备、模型训练、开发主机验证及Jetson Nano上的部署步骤。
最低0.47元/天 解锁文章





