When you have youth
you have to feel it
Don't waste your golden years
Live out your precious inner life
Don't miss anything
1. Project introduction and environment configuration
Select version 5.0 tag and download the source code
Open code using Pycharm
Select the interpreter. I chose the pytorch environment created by conda before.
Install the packages needed for the project
Open the requirements.txt file of the project. It contains the packages needed to run the project. This is a good coding habit. You can also do this with the code you write yourself.
Then run the download code in the terminal
2. Use YOLOv5 for prediction
Find the detect.py file and run the code
Possible error 1:
AttributeError: Can't get attribute 'SPPF' on <module 'models.common' from '
solution:
1. Download the corresponding yolov5s.pt model from the official website and put it into the project (recommended).
2. Modify the project code as follows
Add code
class SPPF(nn.Module):
def __init__(self, c1, c2, k=5):
super().__init__()
c_ = c1 // 2
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c_ * 4, c2, 1, 1)
self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)
def forward(self, x):
x = self.cv1(x)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
y1 = self.m(x)
y2 = self.m(y1)
return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1))
Possible error 2:
File "/Users/lihui/Documents/ai/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/upsampling.py", line 157, in forward
recompute_scale_factor=self.recompute_scale_factor)
File "/Users/lihui/Documents/ai/anaconda3/envs/pytorch/lib/python3.8/site-packages/torch/nn/modules/module.py", line 1614, in __getattr__
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'Upsample' object has no attribute 'recompute_scale_factor'
Find the corresponding file along the path
Find the problematic code and delete it
It should be a problem caused by the python version.
After modifying the error, run the code again
Using different models for training will produce different results.
For example, I download the following four models and test them
The training sequence is 5s, 5m, 5l, 5x
It can be seen that choosing a more complex model will take longer to train, but the effect will be better.
Detect video
The detection model will detect the video frame by frame and finally output the results.
Advanced gameplay
Mobile phone download APP ip camera
Then let the service and mobile phone be in the same LAN, and change the path of the code to
http://admin:admin@The IP address displayed after opening on the mobile phone can perform real-time object detection through the mobile phone.
Similarly, if you want to detect objects captured by the camera in real time, you can change the path to the RTSP stream pushed by the camera.
Parameter explanation
--weights:模型权重文件的路径。就是使用什么模型来跑,可以指定一个或多个路径,用空格分隔。
--source:数据源的路径或URL。可以是文件夹路径、视频文件路径或者网络摄像头的URL。
--img-size:推理时输入图像的大小(像素)。默认为640。
--conf-thres:目标置信度阈值。检测到的目标的置信度必须大于此阈值才会被接受。比如你检测人的置信度设置为0.8,那么在检测中只有大于0.8概率的才会被检测框框出来。
--iou-thres:非最大值抑制(NMS)的IOU阈值。交并集,取检测最接近的一个检测框。
--device:设备选择,可以是CUDA设备的索引(如0)或者多个设备的索引(如0,1,2,3),也可以是cpu。默认为空字符串,表示使用默认设备。
--view-img:显示结果图像。如果设置了该参数,在跑代码的时候会实时显示检测结果的图像。
--save-txt:保存结果到.txt文件。如果设置了该参数,则会将检测结果保存为文本文件。
--save-conf:在保存的.txt标签中保存置信度。如果设置了该参数,则会在保存的文本标签中包含目标的置信度。
--nosave:不保存图像/视频。如果设置了该参数,则不会保存检测结果的图像或视频。
--classes:按类别过滤。可以指定一个或多个类别的索引,用空格分隔。例如,--classes 0表示只保留类别索引为0的目标。每一个类别的值,比如人的类别是0,如果你设置了classes 0,那么只会检测人。
--agnostic-nms:类别不可知的NMS。如果设置了该参数,则使用类别不可知的非最大值抑制。
--augment:增强推理。如果设置了该参数,则在推理过程中使用数据增强技术,更准确。
--update:更新所有模型。如果设置了该参数,则会更新所有模型。没啥用。
--project:结果保存的项目路径。默认为runs/detect。
--name:结果保存的名称。默认为exp。
--exist-ok:允许覆盖现有的项目/名称。如果设置了该参数,则不会增加项目/名称的后缀。
Parameter setting tips:
Set the parameters you need here, so that you don’t have to manually type in the command line every time you run the project.
3. Training YOLOv5 neural network
Open the train.py training file
Parameter explanation
- `--weights`:初始权重文件的路径。选择训练模型,默认为`yolov5s.pt`。
- `--cfg`:模型配置文件(`model.yaml`)的路径。
- `--data`:数据集配置文件(`data.yaml`)的路径。默认为`data/coco128.yaml`。
- `--hyp`:超参数文件的路径。默认为`data/hyp.scratch.yaml`。
- `--epochs`:训练的总轮数,默认为300。
- `--batch-size`:每个批次的样本数。默认为16。
- `--img-size`:训练和测试图像的尺寸。可以指定一个或两个整数值作为列表,例如`--img-size 640 640`。默认为`[640, 640]`。
- `--rect`:使用矩形训练。如果设置了该参数,则训练过程中使用矩形图像而不是正方形图像。不是矩阵的会自动进行填充。
- `--resume`:恢复最近的训练。如果设置了该参数,则会恢复最近一次的训练。
- `--nosave`:只保存最终的检查点。如果设置了该参数,则只保存最后一个轮次的模型检查点。
- `--notest`:只测试最终的轮次。如果设置了该参数,则只在最后一个轮次进行测试。
- `--noautoanchor`:禁用自动锚框检查。
- `--evolve`:演化超参数。如果设置了该参数,则会根据训练过程中的结果自动调整超参数。
- `--bucket`:Google Cloud Storage(GCS)存储桶的路径。
- `--cache-images`:缓存图像以加快训练速度。
- `--image-weights`:使用加权的图像选择进行训练。
- `--device`:设备选择,可以是CUDA设备的索引(如`0`)或者多个设备的索引(如`0,1,2,3`),也可以是`cpu`。
- `--multi-scale`:变化的图像尺寸。如果设置了该参数,则训练过程中会随机调整图像尺寸的大小。
- `--single-cls`:将多类别数据作为单一类别进行训练。
- `--adam`:使用`torch.optim.Adam()`优化器。
- `--sync-bn`:使用`SyncBatchNorm`,只在DDP模式下可用。
- `--local_rank`:DDP参数,不要修改。
- `--workers`:最大数据加载器的工作进程数。
- `--project`:保存结果的项目路径。默认为`runs/train`。
- `--entity`:W&B实体。
- `--name`:保存结果的名称。默认为`exp`。
- `--exist-ok`:允许覆盖现有的项目/名称。
- `--quad`:使用四分之一的数据加载器。
- `--linear-lr`:使用线性学习率。
- `--label-smoothing`:标签平滑的ε值。
- `--upload_dataset`:将数据集上传为W&B的artifact表格。
- `--bbox_interval`:设置W&B的边界框图像日志记录间隔。
- `--save_period`:每隔多少轮保存一次模型。
- `--artifact_alias`:数据集artifact的版本别名。
Cloud GPU training model
Free website provided by Google
Package and upload the project (delete unnecessary models and videos before packaging)
Unzip command
!unzip /content/yolov5-5.0.zip
delete file command
!rm -rf /content/__MACOSX
Install the environment needed to run the code
%cd /content/yolov5-5.0
!pip install -r requirements.txt
Use tensorboard to view running conditions in real time
%load_ext tensorboard
%tensorboard --logdir=runs/train
Run the training code
!python train.py --rect
After training, you can export the model to best.pt
4. Create and train your own data set
Finally, you only need to put your labeled data set into the specified path, and then modify the path location that needs training in the yaml file.