1 克隆yolov5代码
git clone https://gitee.com/monkeycc/yolov5.git
可以去gitee找代码
2 相关文件
split.py
import xml.etree.ElementTree as ET
import pickle
import os
from os import listdir , getcwd
from os.path import join
import glob
import sys
classes = {"front_head":0,"side_head":1,"back_head":2,"hand":3,"mobile_phone":4,"person_on_phone":5}
def convert(size, box):
dw = 1.0/size[0]
dh = 1.0/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
x = min(x,1.0)
w = min(w,1.0)
y = min(y,1.0)
h = min(h,1.0)
return (x,y,w,h)
def convert_annotation(path):
in_file = open(path.replace('.xml','.xml'),encoding="utf-8")
out_file = open(path.replace('.xml','.txt'),'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)
if w == 0 or h == 0:
print(1)
return
for obj in root.iter('object'):
name = obj.find('name').text
cls_id = classes[name]
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')
files = glob.glob('./trainval/*.xml')
if __name__ == '__main__':
for file in files:
convert_annotation(file)
run.sh
cd /project/train/src_repo
cp -r /home/data/720 ./trainval
python split.py && python path.py
cd yolov5
python train.py --batch-size 16 --epochs 500 --data ./data/buy.yaml --hyp ./data/hyps/hyp.scratch.yaml --weight ./yolov5.pt --img 480 --project /project/train/models/ --cfg ./models/yolov5s.yaml
path.py
from glob import glob
from random import randint
files = glob('/projrct/train/src_repo/trainval/*.txt')
train = open('train.txt','w')
val = open('val.txt','w')
for i in files:
num = randint(1,10)
name = i.replace('.txt','jpg')
if num < 8:
train.write(name + '\n')
else:
val.write(name + '\n')
train.close()
val.close()
buy.yaml
train: ../train.txt
val: ../val.txt
nc: 6
names: ["front_head","side_head","back_head","hand","mobile_phone","person_on_phone"]
3 安装所需要的包
cd yolov5 pip install --index-url https://pypi.douban.com/simple -r requirements.txt
4 运行
bash run.sh
|