一. 下载代码
3. YOLOv5github地址:https://github.com/ultralytics/yolov5
在target里选择5.0版本。
二.安装所需要的库和环境。
pip install -r requirements.txt
三 .准备自己的数据集。
使用PASCAL VOC数据集的目录结构:
建立文件夹层次为 yolov5 / VOCdevkit / VOC2007
VOC2007下面建立两个文件夹:Annotations和JPEGImages
JPEGImages放所有的训练和测试图片;Annotations放所有的xml标记文件。
建立完文件之后执行如下代码生成训练集和验证集文件:
注意:要在当前路径下。
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=["heidian","shenban","qianban","guoba","guodi"] # 你的类别信息
TRAIN_RATIO = 5 # 类别数量
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