提前准备
- Pycharm
- python3.8
- yolov5源码 官网链接
1. 环境搭建并运行官方案例
-
下载yolov5源码,推荐使用最新版,如下图,直接下载master分支即可

-
解压下载的zip文件,使用pycharm打开项目,接着添加解释器

-
这里使用新环境,没有用anaconda,因为使用pycharm自带的python环境可以让虚拟环境跟随项目,在其他电脑运行时候就不用重新配置环境了。


- 使用pycharm的终端安装requirements.txt中需要的环境,直接在终端
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/
- 使用国内源可以下载快些。
- 提前下载yolov5s.pt预训练权重GitHub链接,(也可以不下载,运行detect.py的时候会自动下载,但是容易下载出错,因为GitHub网络问题),下载好后放在yolov5-master文件夹里面


- 右键运行detect.py文件,如果出现下图即表示环境搭建成功(运行的是自带的模型,识别人,汽车等),此时生成run文件夹,结果存放在yolov5-master/run/detect/exp文件内



- 没有使用cuda进行加速,因为有些小伙伴可能没有N卡,所以使用的CPU版本,想使用GPU加速只需要安装GPU版本的pytorch即可,但是要看看自己显卡支持的cuda版本,参考这篇博文https://blog.csdn.net/didiaopao/article/details/119787139 链接直达
2. 创建并划分数据集
参考下面博文即可
https://blog.csdn.net/didiaopao/article/details/120022845链接直达
- VOC标签格式转yolo格式并划分训练集和测试集
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir, getcwd
from os.path import join
import random
from shutil import copyfile
classes = ["hat", "person"] # 自定义类
TRAIN_RATIO = 80 # 8:2 训练集和验证集比率
def clear_hidden_files(path):
dir_list = os.listdir(path)
for i in dir_list:
abspath = os.path.join(os.path.abspath(path), i)
if os.path.isfile(abspath):
if i.startswith("._"):
os.remove(abspath)
else:
clear_hidden_files(abspath)
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):
in_file = open('VOCdevkit/VOC2007/Annotations/%s.xml' %image_id)
out_file = open('VOCdevkit/VOC2007/YOLOLabels/%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
if cls not in classes or int(difficult) == 1:
continue
cls_id = classes.<

最低0.47元/天 解锁文章
5170

被折叠的 条评论
为什么被折叠?



