ubuntu18 YOLOv5 配置及训练
环境搭建
#下载anaconda安装脚本(本教程采用清华源)
wget https://mirrors.tuna.tsinghua.edu.cn/anaconda/archive/Anaconda3-2020.07-Linux-x86_64.sh
#安装anaconda
bash Anaconda3-2020.07-Linux-x86_64.sh
# Please, press ENTER to continue -> 回车继续
# 阅读协议,同意按回车(跳过ctrl+c)
# Do you accept the license terms? [yes|no] -> 同意协议输入yes回车
# Anaconda3 will now be installed into this location -> 选择安装路径(本文安装/usr/local/anaconda3),等待安装
# by running conda init? [yes|no] -> 是否添加系统环境,输入yes回车
# 待安装结束
# 刷新当前用户环境(激活环境)
source ~/.bashrc
#更新Anaconda相关配置
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
创建环境
#使用conda创建单独的环境,在系统命令行中运行如下命令:
conda create -n yolov5_1 python==3.8
#进入该环境继续安装依赖库:
conda activate yolov5_1
#删除环境
conda remove -n yolov5 --all
#退出当前环境
conda deactivate yolov5_1
#安装pytorch环境,可能需要等待
#pytorch的gpu版本,官方网站地址: https://pytorch.org/get-started/locally/,找到自己匹配的相关命令
pip install torch==1.8.0+cu111 torchvision==0.9.0+cu111 torchaudio==0.8.0 -f https://download.pytorch.org/whl/torch_stable.html
#查看安装的结果
pip install ipython
#(yolov5_1) :~$ ipython
#Python 3.8.0 (default, Nov 6 2019, 21:49:08)
#Type 'copyright', 'credits' or 'license' for more information
#IPython 7.21.0 -- An enhanced Interactive Python. Type '?' for help.
#In [1]: import torch
#In [2]: torch.cuda.is_available()
#Out[2]: True
#出现以上内容表明环境已经安装好啦。
1.下载
YOLOv5源码下载地址:https://codechina.csdn.net/mirrors/ultralytics/yolov5
2.运行requirements
安装运行环境以及所需依赖,运行以下命令即可:
pip install -U -r requirements.txt
#等待依赖下载完成,可能比较耗时
3.测试
权重文件下载地址:https://github.com/ultralytics/yolov5/releases/
python detect.py --source data/images/ --weights yolov5s.pt --conf 0.4
#查看结果,运行结束后会提示结果的保存路径
#yolov5支持直接输出txt文件,文件内容与标注的文件一致
报错如下:
modules/module.py", line 947, in __getattr__
raise AttributeError("'{}' object has no attribute '{}'".format(
AttributeError: 'Detect' object has no attribute 'm'
解决方法:在官网下载最新的weight文件------yolov5s.pt
即可。
4.训练coco128数据集
python train.py --data coco128.yaml --cfg yolov5s.yaml --weights '' --batch-size 32 --device 0 --epochs 300 --img 640
报错如下:
RuntimeError: cuDNN error: CUDNN_STATUS_NOT_INITIALIZED
解决:
将原命令中的batch-size
调小即可。
5.制作自己的数据集
-
labelImg
可直接标注YOLO格式的数据。 -
使用
labelImg
进行标注,之后我们需要将标注生成的xml转换成txt文件,代码如下:import xml.etree.ElementTree as ET import pickle import os from os import listdir, getcwd from os.path import join #修改自己所标注使用的类名 classes = ["marker"] def convert(size, box): dw = 1./size[0] dh = 1./size[1] x = (box[0] + box[1])/2.0 y = (box[2] + box[3])/2.0 w = box[1] - box[0] h = box[3] - box[2] x = x*dw w = w*dw y = y*dh h = h*dh return (x,y,w,h) def convert_annotation(image_id): #xml文件的路径 in_file = open('xml1117/%s.xml'%(image_id)) #生成txt文件的路径 out_file = open('xml1117/%s.txt'%(image_id), 'w') tree=ET.parse(in_file) root = tree.getroot() size = root.find('size') w = int(size.find('width').text) h = int(size.find('height').text) for obj in root.iter('object'): difficult = obj.find('difficult').text cls = obj.find('name').text #查看当前标注的xml文件中的类名是否存在在classes里 if cls not in classes or int(difficult) == 1: continue cls_id = classes.index(cls) xmlbox = obj.find('bndbox') b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text)) bb = convert((w,h), b) out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') wd = getcwd() #if not os.path.exists('VOCdevkit/VOC%s/labels/'%(year)): # os.makedirs('VOCdevkit/VOC%s/labels/'%(year)) path = os.listdir(r"./xml1117") path.sort() #将所有图像的绝对路径存入在train.txt中 #list_file = open('train.txt', 'w') for filename in path: name = filename[:filename.rindex('.')] #list_file.write('%s/%s/%s.jpg\n'%(wd,"image1117",name)) convert_annotation(name) list_file.close()
-
将标注好的txt文件以及图形文件参照coco128文件的格式放置即可。
-
之后我们就可以开始训练啦,命令如下:
python train.py --data outlets.yaml --cfg yolov5s.yaml --weights yolov5s.pt --batch-size 4 --device 0 --epochs 1000 --img 640
6.测试:
#conf为阈值,即置信度为0.4以下的不被检测到
python detect.py --source data/img/ --weights /runs/train/exp21/weights/best.pt --conf 0.4