干货 | YOLOV5 训练自动驾驶数据集,并转Tensorrt,收藏!

AI视线python


点击关注上方AI深度学习视线”,并“星标”公号
nginx

技术硬文,第一时间送达!git




  • 准备数据集github

  • 环境配置web

  • 配置文件修改算法

  • 训练sql

  • 推理shell

  • 转Tensorrtjson



1微信


准备数据集

1.1 BDD数据集

BDD100K是最大的开放式驾驶视频数据集之一,其中包含10万个视频和10个任务,目的是方便评估自动驾驶图像识别算法的的进展。每一个高分辨率视频一共40秒。该数据集包括超过1000个小时的驾驶数据,总共超过1亿帧。这些视频带有GPU / IMU数据以获取轨迹信息。该数据集具备地理,环境和天气多样性,从而能让模型可以识别多种场景,具有更多的泛化能力。这些丰富的户外场景和复杂的车辆运动使感知任务更具挑战性。该数据集上的任务包括图像标记,车道检测,可驾驶区域分割,道路对象检测,语义分割,实例分割,多对象检测跟踪,多对象分割跟踪,领域自适应和模仿学习。咱们能够在BDD100K数据网站上下载数据


Bdd100k的标签是由Scalabel生成的JSON格式。
- labels [ ]: - id: int32 - category: string (classification) - manualShape: boolean (whether the shape of the label is created or modified manually) - manualAttributes: boolean (whether the attribute of the label is created or modified manually) - score: float (the confidence or some other ways of measuring the quality of the label.) - attributes: - occluded: boolean - truncated: boolean - trafficLightColor: "red|green|yellow|none" - areaType: "direct | alternative" (for driving area) - laneDirection: "parallel|vertical" (for lanes) - laneStyle: "solid | dashed" (for lanes) - laneTypes: (for lanes) - box2d: - x1: float - y1: float - x2: float - y2: float


道路对象类别包括如下几类:

[ "bike", "bus", "car", "motor", "person", "rider", "traffic light", "traffic sign", "train", "truck"]

1.2 YOLO数据格式

每一个图片文件.jpg,都有同一命名的标签文件.txt。

标签文件中每一个对象独占一行,格式为<object-class> <x> <y> <width> <height>

其中:

  • <object-class>-表示对象的类别序号:从0 到 (classes-1)

  • <x> <y> <width> <height> -参照图片宽度和高度的相对比例(浮点数值),从0.0到1.0

  • 例如:<x> = <absolute_x> / <image_width><height> = <absolute_height> / <image_height>

  • 注意:<x> <y>是矩形的中心,而不是左上角位置。


以下图所示:


YOLO V5的标签文件和图像文件应位于同一目录下。


1.3 BDD数据转YOLO格式

Berkerley 提供了Bdd100k数据集的标签查看及标签格式转化工具。因为没有直接从bdd100k转换成YOLO的工具,所以咱们首先得使用将bdd100k的标签转换为coco格式,而后再将coco格式转换为yolo格式。

  • bdd to coco

个人目的是识别包括不一样颜色交通灯在内的全部交通对象,所以咱们须要对原版的bdd2coco.py进行一些修改,以获取交通灯颜色并产生新的类别。

这是修改完的核心代码:

for label in i['labels']: annotation = dict() category=label['category'] if (category == "traffic light"): color = label['attributes']['trafficLightColor'] category = "tl_" + color if category in id_dict.keys(): empty_image = False annotation["iscrowd"] = 0 annotation["image_id"] = image['id'] x1 = label['box2d']['x1'] y1 = label['box2d']['y1'] x2 = label['box2d']['x2'] y2 = label['box2d']['y2'] annotation['bbox'] = [x1, y1, x2-x1, y2-y1] annotation['area'] = float((x2 - x1) * (y2 - y1)) annotation['category_id'] = id_dict[category] annotation['ignore'] = 0 annotation['id'] = label['id'] annotation['segmentation'] = [[x1, y1, x1, y2, x2, y2, x2, y1]] annotations.append(annotation)
 
  

在完成bdd100k格式到yolo格式的转换后,会得到bdd100k_labels_images_det_coco_train.jsonbdd100k_labels_images_det_coco_val.json两个文件。

  • Coco to yolo

在完成先前的转换以后,咱们须要将训练集和验证集的coco格式标签转换为yolo格式。注意须要分别指定训练集和验证集图片位置,对应的coco标签文件位置,及生成yolo标签的目标位置。

config_train ={ "datasets": "COCO", "img_path": "bdd100k_images/bdd100k/images/100k/train", "label": "labels/bdd100k_labels_images_det_coco_train.json", "img_type": ".jpg", "manipast_path": "./", "output_path": "labels/trains/", "cls_list": "bdd100k.names", } config_valid ={ "datasets": "COCO", "img_path": "bdd100k_images/bdd100k/images/100k/val", "label": "labels/bdd100k_labels_images_det_coco_val.json", "img_type": ".jpg", "manipast_path": "./", "output_path": "labels/valids/", "cls_list": "bdd100k.names", }

除此以外,咱们还得将全部的类别写入bdd100k.names文件。

personridercarbustruckbikemotortl_greentl_redtl_yellowtl_nonetraffic signtraintl_green

运行Bdd_preprocessing中的完整代码能够完成Bdd100k格式标签到YOLO标签格式的转换。

Bdd2coco以及coco2yolo的详细说明能够参bdd100k代码库convert2Yolo代码库




2


环境配置

2.1 官方代码

https://github.com/ultralytics/yolov5/tree/v3.0

因为后面转tensorrt版本支持yolov5到3.0版本,因此以3.0版本进行实验。

环境配置可经过下面命令进行一键配置。


# pip install -r requirements.txt
# base ----------------------------------------Cythonmatplotlib>=3.2.2numpy>=1.18.5opencv-python>=4.1.2pillowPyYAML>=5.3scipy>=1.4.1tensorboard>=2.2torch>=1.6.0torchvision>=0.7.0tqdm>=4.41.0
# coco ----------------------------------------# pycocotools>=2.0
# export --------------------------------------# packaging # for coremltools# coremltools==4.0b4# onnx>=1.7.0# scikit-learn==0.19.2 # for coreml quantization
# extras --------------------------------------# thop # FLOPS computation# seaborn # plotting




3


配置文件修改

3.1 修改 ./data/coco.yaml

修改./data/coco.yaml--》存为bdd.yaml

修改内容:

(1)train/val/test 路径

其中的txt内容均为各集合图像实际绝对路径。

(2)nc:number class 类别数量,BDD数据类别为10

(3)names:前面bdd数据集介绍时候已经列出


3.2 修改 ./model/yolov5.yaml

修改:nc为BDD数据类别数:10

3.3 修改./train.py

修改:

(1)--weights,这里s/m/l/x四个型号能够选择

(2)--cfg,这里s/m/l/x四个型号能够选择

(3)--data,选择根据coco.yaml修改后的bdd.yaml

(4)--batch-size 和 --img-size 能够再这里修改也能够默认不动,再训练命令行里设定




4


训练

预训练模型

python train.py --img 640 --batch 32 --epochs 300 --data './data/bdd.yaml' --cfg ./models/custom_yolov5x.yaml --weights "./weights/yolov5x.pt" --name yolov5x_bdd_prew  --cache

从头训练

python train.py --img 640 --batch 32 --epochs 300 --data './data/bdd.yaml' --cfg ./models/custom_yolov5x.yaml --weights "" --name yolov5x_bdd  --cache

train_loss:

val_loss:



5


推断


可选参数:

  • — weights: 训练权重的路径

  • — source:推理目标的路径,能够是图片,视频,网络摄像头等

  • — source:推理结果的输出路径

  • — img-size:推理图片的大小

  • — conf-thres:对象置信阈值,默认0.4

  • — iou-thres:NMS的IOU阈值,能够根据实际对象的重叠度调节,默认0.5

  • — device: 选择使用CUDA或者CPU

  • — view-img:显示全部推理结果

  • — save-txt:将每一帧的推理结果及边界框的位置,存入*.txt文件

  • — classes:类别过滤,意思是只推理目标类别

  • — agnostic-nms:使用agnostic-nms NMS

python detect.py --source 0 # webcam file.jpg # image file.mp4 # video path/ # directory path/*.jpg # glob rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa # rtsp stream rtmp://192.168.1.105/live/test # rtmp stream                            http://112.50.243.8/PLTV/88888888/224/3221225900/1.m3u8  # http stream



5


转Tensorrt

6.1 工程配置

https://github.com/wang-xinyu/tensorrtx/tree/master/yolov5,

该项目提供了一大批常见模型的转Tensorrt方法。


环境要求:

GTX1080 / Ubuntu16.04 / cuda10.0 / cudnn7.6.5 / tensorrt7.0.0 / nvinfer7.0.0 / opencv3.3

高版本tensorrt7的变化以下:

6.2 生成转tensorrt的中间文件 yolov5.wts

拷贝 ./tensorrt/yolov5/gen_wts.py文件到./yolov5 工程下,修改其中加载模型路径,执行该python文件,获得yolov5.wts,并将其拷贝回 ./tensorrt/yolov5/下。

1. generate yolov5s.wts from pytorch with yolov5s.pt
git clone https://github.com/wang-xinyu/tensorrtx.gitgit clone https://github.com/ultralytics/yolov5.git// download its weights 'yolov5s.pt'// copy tensorrtx/yolov5/gen_wts.py into ultralytics/yolov5// ensure the file name is yolov5s.pt and yolov5s.wts in gen_wts.py// go to ultralytics/yolov5python gen_wts.py// a file 'yolov5s.wts' will be generated.


6.3 编译yolov5并生成tensorrt模型yolov5.engine

编译以前须要修改:

(1)选模型

(2)CMakeLists.txt

若是tensorrt是经过tar包解压安装的,还须要在CMakeList.txt中对tensorrt路径进行指定,否则会报错找不到nvinfer

(3)另外,若是系统是Ubuntu18.04的话还会存在opencv的问题,找不到libpng12.so和libjasper.so.

这个问题可经过https://blog.csdn.net/baobei0112/article/details/108991915 该博客内容找到答案。

(4)./tensorrt/yolov5/下新建个samples文件夹,把须要测试的图片放进去。

作好准备工做,下面就能够进行YOLOV5的engine编译工做。

build tensorrtx/yolov5 and run
// put yolov5s.wts into tensorrtx/yolov5// go to tensorrtx/yolov5// ensure the macro NET in yolov5.cpp is smkdir buildcd buildcmake ..makesudo ./yolov5 -s // serialize model to plan file i.e. 'yolov5s.engine'sudo ./yolov5 -d  ../samples // deserialize plan file and run inference, the images in samples will be processed.


6.4 Tensorrt各yolo模型对比




潮水自会来去,但心志得坚若磐石。即使成不了那根定海神针,也至少不是那随意被拍上岸的野鬼游魂。 

长按关注 "AI深度视线"
最新人工智能、深度学习、SLAM干货奉上!









本文分享自微信公众号 - AI深度学习视线(AI_DeepSight)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。

相关文章
相关标签/搜索