YOLOV5训练模型
一、创建YOLO环境
1.激活提前创建好的虚拟环境,例如yolov5
activate yolov5
2.在克隆好的yolov5目录下安装所需库
pip install -r requirements.txt
二、准备训练数据
1.images用来存放图片,label用来存放标签
2.使用labeling工具进行标注
下载链接:https://github.com/tzutalin/labelImg
下载解压后
1)删除labelImg-master\data\predefined_classes.txt
2)yolov5环境安装所需依赖
conda install pyqt=5
conda install -c anaconda lxml
pyrcc5 -o libs/resources.py resources.qrc
3)yolov5环境下运行lambeling
python labelimg.py
4)设置好lambeling(lambeling文件路径不能有中文)
勾选这个选项
lambeling的使用快捷键
Ctrl + u Load all of the images from a directory
Ctrl + r Change the default annotation target dir
Ctrl + s Save
Ctrl + d Copy the current label and rect box
Ctrl + Shift + d Delete the current image
Space Flag the current image as verified
w Create a rect box
d Next image
a Previous image
del Delete the selected rect box
Ctrl++ Zoom in
Ctrl-- Zoom out
↑→↓← | Keyboard arrows to move selected rect box
5)建立yaml文件,可以用已有的文件上面更改
示例
# train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
train: ../data/images/
val: ../data/images/
# number of classes
nc: 1
# class names
names: ['Doraemon']
三、模型训练
1.在Google Colab中训练,速度更可观(前提是有科学上网工具)
1)查看torch是否正常
import torch
torch.cuda.is_available()
true则正常
2)克隆yolov5仓库
!git clone https://github.com/ultralytics/yolov5 # clone repo``%cd yolov5
3)安装依赖
%pip install -qr requirements.txt # install dependencies
4)运行以下指令,给予权限访问谷歌云盘
import os
from google.colab import drive
drive.mount('/content/drive')
``
path = "/content/drive/My Drive"
``
os.chdir(path)
os.listdir(path)
2.上传data数据集至目录下
开始训练
1.用自带的数据集测试训练,没有问题
!python train.py
2.训练自己的数据集
!python train.py --img 640 --batch 50 --epochs 100 --data /content/drive/MyDrive/yolov5/data/test.yaml --weights yolov5s.pt --nosave --cache
其中test.yaml文件建议用绝对路径,如下图所示
执行上述代码后,系统开始训练。
训练结果如下图:
在系统结果路径下可以看到训练结果图
测试模型
!python detect.py --weights /content/drive/MyDrive/yolov5/runs/train/exp9/weights/best.pt --img 640 --conf 0.25 --source /content/drive/MyDrive/yolov5/data/images/1.jpg
copy right from 抛到海里
以下是它的原文链接
https://blog.csdn.net/qq_45701791/article/details/113992622