端到端车道线检测_如何使用Yolov5创建端到端对象检测器?

端到端车道线检测

Ultralytics recently launched YOLOv5 amid controversy surrounding its name. For context, the first three versions of YOLO (You Only Look Once) were created by Joseph Redmon. Following this, Alexey Bochkovskiy created YOLOv4 on darknet, which boasted higher Average Precision (AP) and faster results than previous iterations.

Ultralytics最近在围绕其名称的争议中推出了YOLOv5。 对于上下文,约瑟夫·雷德蒙(Joseph Redmon)创建了YOLO的前三个版本。 此后,Alexey Bochkovskiy在Darknet上创建了YOLOv4,与以前的迭代相比,它拥有更高的平均精度(AP)和更快的结果。

Now, Ultralytics has released YOLOv5, with comparable AP and faster inference times than YOLOv4. This has left many asking: is a new version warranted given similar accuracy to YOLOv4? Whatever the answer may be, it’s definitely a sign of how quickly the detection community is evolving.

现在,Ultralytics已发布YOLOv5,具有可比的AP和比YOLOv4更快的推理时间。 这引起了很多人的疑问:是否应授予与YOLOv4相似的准确性的新版本? 无论答案是什么,这绝对标志着检测社区发展的速度有多快。

Source: Ultralytics Yolov5 资料来源 :Ultralytics Yolov5

Since they first ported YOLOv3, Ultralytics has made it very simple to create and deploy models using Pytorch, so I was eager to try out YOLOv5. As it turns out, Ultralytics has further simplified the process, and the results speak for themselves.

自从他们首次移植YOLOv3以来,Ultralytics就使使用Pytorch创建和部署模型非常简单,所以我很想尝试YOLOv5。 事实证明,Ultralytics进一步简化了流程,其结果不言而喻。

In this article, we’ll create a detection model using YOLOv5, from creating our dataset and annotating it to training and inferencing using their remarkable library. This post focuses on the implementation of YOLOv5, including:

在本文中,我们将使用YOLOv5创建检测模型,从创建数据集并对其进行注释到使用其卓越的库进行训练和推理。 这篇文章重点介绍YOLOv5的实现,包括:

  • Creating a toy dataset

    创建玩具数据集
  • Annotating the image data

    注释图像数据
  • Creating the project structure

    创建项目结构
  • Training YOLOv5

    培训YOLOv5

创建自定义数据集 (Creating Custom Dataset)

You can forgo the first step if you have your image Dataset. Since I don’t have images, I am downloading data from the Open Image Dataset(OID), which is an excellent resource for getting annotated image data that can be used for classification as well as detection. Note that we won’t be using the provided annotations from OID and create our own for the sake of learning.

如果您有图像数据集,则可以放弃第一步。 由于我没有图像,因此我正在从开放图像数据集(OID)下载数据,这是获取可用于分类和检测的带注释图像数据的绝佳资源。 请注意,我们不会使用OID提供的注释,而是为了学习而创建自己的注释。

1. OIDv4下载图片: (1. OIDv4 Download Images:)

To download images from the Open Image dataset, we start by cloning the OIDv4_ToolKit and installing all requirements.

要从Open Image数据集下载图像,我们首先克隆OIDv4_ToolKit并安装所有要求。

git clone https://github.com/EscVM/OIDv4_ToolKit
cd OIDv4_ToolKit
pip install -r requirements.txt

We can now use the main.py script within this folder to download images as well as labels for multiple classes.

现在,我们可以使用此文件夹中的main.py脚本来下载图像以及多个类的标签。

Below I am downloading the data for Cricketball and Football to create our Custom Dataset. That is, we will be creating a dataset with footballs and cricket balls, and the learning task is to detect these balls.

下面,我正在下载板球和橄榄球的数据以创建我们的自定义数据集。 也就是说,我们将创建一个包含足球和板球的数据集,学习任务是检测这些球。

python3 main.py downloader --classes Cricket_ball  Football --type_csv all -y --limit 500

The below command creates a directory named “OID” with the following structure:

下面的命令使用以下结构创建一个名为“ OID”的目录:

OID directory structure. We will take only the image files(.jpgs) from here and not the labels as we will annotate manually to create our Custom Dataset, though we can use them if required for a different project. OID目录结构。 我们将仅从此处获取图像文件(.jpgs),而不从标签中获取标签,因为我们将手动添加注释以创建“自定义数据集”,尽管如果不同项目需要,我们也可以使用它们。

Before we continue, we will need to copy all the images in the same folder to start our labeling exercise from Scratch. You can choose to do this manually, but this can also be quickly done programmatically using recursive glob function:

在继续之前,我们需要复制同一文件夹中的所有图像,以便从头开始我们的标签练习。 您可以选择手动执行此操作,但是也可以使用递归glob函数以编程方式快速完成此操作:

import os
from glob import globos.system("mkdir Images")
images = glob(r'OID/**/*.jpg', recursive=True)
for img in images:
os.system(f"cp {img} Images/")

2.使用HyperLabel标记图像 (2. Label Images with HyperLabel)

We will use a tool called Hyperlabel to label our images. In the past, I have used many tools to create annotations like labelimg, labelbox, etc. but never came across a tool so straightforward and that too open source. The only downside is that you cannot get this tool for Linux and only for Mac and Windows, but I guess that is fine for most of us.

我们将使用称为Hyperlabel的工具来标记图像。 过去,我曾使用过许多工具来创建诸如labelimg,labelbox等的批注,但从未遇到过如此简单,过于开源的工具。 唯一的缺点是您无法在Linux以及Mac和Windows上使用此工具,但是我认为这对我们大多数人都很好。

1. Create Project, 2, Set up Labels, 3. Add Local Image data source, 4. Annotate 1.创建项目,2,设置标签,3.添加本地图像数据源,4.注释

The best part of this tool is the variety of output formats it provides. Since we want to get the data for Yolo, we will close Yolo Format and export it after being done with our annotations. But you can choose to use this tool if you want to get annotations in JSON format(COCO) or XML format(Pascal VOC) too.

该工具最好的部分是它提供的各种输出格式。 由于我们要获取Yolo的数据,因此我们将关闭Yolo格式并在完成注释后将其导出。 但是,如果您也想以JSON格式(COCO)或XML格式(Pascal VOC)获得注释,则可以选择使用此工具。

5. Export 5.出口

Exporting in Yolo format essentially creates a .txt file for each of our images, which contains the class_id, x_center, y_center, width, and the height of the image. It also creates a file named obj.names , which helps map the class_id to the class name. For example:

以Yolo格式导出实际上会为我们的每个图像创建一个.txt文件,其中包含class_id,x_center,y_center,图像的宽度和高度。 它还创建一个名为obj.names的文件,该文件有助于将class_id映射到类名。 例如:

Image, its annotation, and the obj.names file 图像,其注释和obj.names文件

Notice that the coordinates are scaled from 0 to 1 in the annotation file. Also, note that the class_id is 0 for Cricketball and 1 for football as per obj.names file, which starts from 0. There are a few other files we create using this, but we won’t be using them in this example.

请注意,注释文件中的坐标从0缩放到1。 另外,请注意,根据obj.names文件,板球的class_id为0,橄榄球为1,从0开始。我们使用此文件创建了一些其他文件,但在本示例中将不再使用它们。

Once we have done this, we are mostly set up with our custom dataset and would only need to rearrange some of these files for subsequent training and validation splits later when we train our model. The dataset currently will be a single folder like below containing both the images as well as annotations:

完成此操作后,我们大部分将使用自定义数据集进行设置,并且在我们训练模型时,仅需要重新排列其中一些文件即可进行后续的训练和验证拆分。 数据集当前将是一个单独的文件夹,如下所示,其中既包含图像又包含注释:

dataset
- 0027773a6d54b960.jpg
- 0027773a6d54b960.txt
- 2bded1f9cb587843.jpg
- 2bded1f9cb587843.txt
--
--

设置项目 (Setting up the project)

To train our custom object detector, we will be using Yolov5 from Ultralytics. We start by cloning the repository and installing the dependencies:

为了训练我们的自定义对象检测器,我们将使用Ultralytics的Yolov5。 我们首先克隆存储库并安装依赖项:

git clone https://github.com/ultralytics/yolov5 # clone repo
cd yolov5
pip install -U -r requirements.txt

We then start with creating our own folder named training in which we will keep our custom dataset.

然后,我们从创建自己的名为training的文件夹开始,在其中保留我们的自定义数据集。

!mkdir training

We start by copying our custom dataset folder in this folder and creating the train validation folders using the simple train_val_folder_split.ipynb notebook. This code below just creates some train and validation folders and populates them with images.

我们首先将自定义数据集文件夹复制到此文件夹中,然后使用简单的train_val_folder_split.ipynb笔记本创建火车验证文件夹。 下面的代码仅创建了一些train和validation文件夹,并在其中填充了图像。

import glob, os
import random# put your own path here
dataset_path = 'dataset'# Percentage of images to be used for the validation set
percentage_test = 20!mkdir data
!mkdir data/images
!mkdir data/labels
!mkdir data/images/train
!mkdir data/images/valid
!mkdir data/labels/train
!mkdir data/labels/valid# Populate the folders
p = percentage_test/100
for pathAndFilename in glob.iglob(os.path.join(dataset_path, "*.jpg")):
title, ext = os.path.splitext(os.path.basename(pathAndFilename))
if random.random() <=p :
os.system(f"cp {dataset_path}/{title}.jpg data/images/valid")
os.system(f"cp {dataset_path}/{title}.txt data/labels/valid")
else:
os.system(f"cp {dataset_path}/{title}.jpg data/images/train")
os.system(f"cp {dataset_path}/{title}.txt data/labels/train")

After running this, your data folder structure should look like below. It should have two directories images and labels.

运行此命令后,您的data文件夹结构应如下所示。 它应该有两个目录imageslabels

We now have to add two configuration files to training folder:

现在,我们必须向training文件夹添加两个配置文件:

1. Dataset.yaml: We create a file “dataset.yaml” that contains the path of training and validation images and also the classes.

1. Dataset.yaml:我们创建一个文件“ dataset.yaml ”,其中包含训练和验证图像的路径以及类。

# train and val datasets (image directory or *.txt file with image paths)
train: training/data/images/train/
val: training/data/images/valid/# number of classes
nc: 2# class names
names: ['Cricketball', 'Football']

2. Model.yaml: We can use multiple models ranging from small to large while creating our network. For example, yolov5s.yaml file in the yolov5/models directory is the small Yolo model with 7M parameters, while the yolov5x.yaml is the largest Yolo model with 96M Params. For this project, I will use the yolov5l.yaml which has 50M params. We start by copying the file from yolov5/models/yolov5l.yaml to the training folder and changing nc , which is the number of classes to 2 as per our project requirements.

2. Model.yaml:创建网络时,我们可以使用从小到大的多种模型。 例如, yolov5s.yaml yolov5/models目录中的yolov5s.yaml文件是具有7M参数的小型Yolo模型,而yolov5x.yaml是具有96M Params的最大Yolo模型。 对于这个项目,我将使用具有50M参数的yolov5l.yaml 。 我们首先将文件从yolov5/models/yolov5l.yaml到training文件夹,然后将nc更改为nc ,根据我们的项目要求, yolov5/models/yolov5l.yaml的数量为2。

# parameters
nc: 2 # change number of classes
depth_multiple: 1.0 # model depth multiple
width_multiple: 1.0 # layer channel multiple

培养 (Train)

At this point our training folder looks like:

此时,我们的训练文件夹如下所示:

Once we are done with the above steps, we can start training our model. This is as simple as running the below command, where we provide the locations of our config files and various other params. You can check out the different other options in train.py file, but these are the ones I found noteworthy.

完成上述步骤后,就可以开始训练模型了。 这就像运行以下命令一样简单,我们在其中提供配置文件和各种其他参数的位置。 您可以在train.py文件中签出其他不同的选项,但是这些是我发现值得注意的选项。

# Train yolov5l on custom dataset for 300 epochs
$ python train.py --img 640 --batch 16 --epochs 300--data training/dataset.yaml --cfg training/yolov5l.yaml --weights ''

Sometimes you might get an error with PyTorch version 1.5 in that case run on a single GPU using:

有时,在那种情况下,使用以下命令在单个GPU上运行的PyTorch 1.5版可能会出错:

# Train yolov5l on custom dataset for 300 epochs
$ python train.py --img 640 --batch 16 --epochs 300--data training/dataset.yaml --cfg training/yolov5l.yaml --weights '' --device 0

Once you start the training, you can check whether the training has been set up by checking the automatically created filetrain_batch0.jpg , which contains the training labels for the first batch and test_batch0_gt.jpg which includes the ground truth for test images. This is how they look for me.

开始训练后,您可以通过检查自动创建的文件train_batch0.jpg来检查训练是否已设置,该文件包含第一批的训练标签和test_batch0_gt.jpg ,其中包括测试图像的基本情况。 这就是他们寻找我的方式。

Left: train_batch0.jpg, Right: test_batch0_gt.jpg 左:train_batch0.jpg,右:test_batch0_gt.jpg

结果 (Results)

To see the results for the training at localhost:6006 in your browser using tensorboard, run this command in another terminal tab

要使用tensorboard在浏览器中的localhost:6006上查看培训的结果,请在另一个终端选项卡中运行此命令

tensorboard --logdir=runs

Here are the various validation metrics. These metrics also get saved in a file results.png at the end of the training run.

以下是各种验证指标。 在训练运行结束时,这些指标也会保存在文件results.png中。

预测 (Predict)

Ultralytics Yolov5 provides a lot of different ways to check the results on new data.

Ultralytics Yolov5提供了许多不同的方法来检查新数据的结果。

To detect some images you can simply put them in the folder named inference/images and run the inference using the best weights as per validation AP:

要检测一些图像,您可以将它们简单地放在名为inference/images的文件夹中,并根据验证AP使用最佳权重运行推理:

python detect.py --weights weights/best.pt
Results 结果

You can also detect in a video using the detect.py file:

您还可以使用detect.py文件在视频中进行检测:

python detect.py --weights weights/best.pt --source inference/videos/messi.mp4 --view-img --output inference/output

Here I specify that I want to see the output using the — view-img flag, and we store the output at the location inference/output. This will create a .mp4 file in this location. It's impressive that the network can see the ball, the speed at which inference is made here, and also the mindblowing accuracy on never observed data.

在这里,我指定使用— view-img标志— view-img输出,并将输出存储在位置推断/输出中。 这将在此位置创建一个.mp4文件。 令人印象深刻的是,网络可以看到球,在这里进行推理的速度,以及从未观察到的数据的惊人准确性。

And, Here is Messi….. 而且,这是梅西…..

You can also use the webcam as a source by specifying the --source as 0. You can check out the various other options in detect.py file.

您也可以通过将--source指定为0来将网络摄像头用作源。您可以在detect.py文件中签出各种其他选项。

结论 (Conclusion)

In this post, I talked about how to create a Yolov5 object detection model using a Custom Dataset. I love the way Ultralytics has made it so easy to create an object detection model.

在本文中,我讨论了如何使用“自定义数据集”创建Yolov5对象检测模型。 我喜欢Ultralytics轻松创建对象检测模型的方式。

Additionally, the various ways that they have provided to see the model results make it a complete package I have seen in a long time.

此外,他们提供的各种方式来查看模型结果使之成为我很长一段时间以来看到的完整软件包。

If you would like to experiment with the custom dataset yourself, you can download the annotated data on Kaggle and the code at Github.

如果您想自己尝试使用自定义数据集,则可以在Kaggle上下载带注释的数据,并在Github上下载代码。

If you want to know more about various Object Detection techniques, motion estimation, object tracking in video, etc., I would like to recommend this excellent course on Deep Learning in Computer Vision in the Advanced machine learning specialization. If you wish to know more about how the object detection field has evolved over the years, you can also take a look at my last post on Object detection.

如果您想了解各种对象检测技术,运动估计,视频中的对象跟踪等更多信息 ,我想向您推荐这本关于高级机器学习专业的 计算机视觉深度学习的优秀课程。 如果您想进一步了解对象检测领域多年来的发展,还可以查看我关于对象检测的最新文章 。

Thanks for the read. I am going to be writing more beginner-friendly posts in the future too. Follow me up at Medium or Subscribe to my blog to be informed about them. As always, I welcome feedback and constructive criticism and can be reached on Twitter @mlwhiz

感谢您的阅读。 我将来也会写更多对初学者友好的文章。 在Medium上关注我,或订阅我的博客以了解有关它们的信息。 与往常一样,我欢迎您提供反馈和建设性的批评,可以在Twitter @mlwhiz上与他们联系。

翻译自: https://towardsdatascience.com/how-to-create-an-end-to-end-object-detector-using-yolov5-35fbb1a02810

端到端车道线检测

你可能感兴趣的:(java,python)