赞
踩
部署项目
git clone https://github.com/ultralytics/yolov5 # clone cd yolov5 pip install -r requirements.txt # install
- 1
- 2
- 3
准备数据
数据存为两个文件夹 images, annotation. images 存图片数据,annotation存xml文件。txt文件夹由数据集划分代码生成。
数据文件结构为:
data–
++++ images
++++ annotation
++++ txt修改配置文件。
# config.json { "root": "data", "annotation": "data/annotation", "image": "data/images", "unicode": ".xml", "txt": "data/txt", "classes": ["类名1","类名2",...] }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
以下为数据集划分脚本,train,val表示划分训练集和验证集比例。测试集占比为1-train-val;
在这里插入代码片def split_data(train=0.8, val=0.1): # read file path from json file with open('cleandata/config.json', 'r') as f: data = json.load(f) # data = json.load('/home/s4/Desktop/yolo/yolov5/yolov5/cleandata/config.json') xml_file_path = data['annotation'] txt_save_path = data['txt'] img_file_path = data['image'] total_xml = os.listdir(xml_file_path) total_img = os.listdir(img_file_path) print(txt_save_path) if not os.path.exists(txt_save_path): print(txt_save_path) os.makedirs(txt_save_path) num = len(total_xml) list_index = range(num) num_train = int(num * train) num_val = int(num * val) # random split train_val = random.sample(list_index, num_train + num_val) train = random.sample(train_val, num_train) # load in file file_train_val = open(os.path.join(txt_save_path, 'train_val.txt'), 'w') file_test = open(os.path.join(txt_save_path, 'test.txt'), 'w') file_train = open(os.path.join(txt_save_path, 'train.txt'), 'w') file_val = open(os.path.join(txt_save_path, 'val.txt'), 'w') for i in list_index: full_path = os.path.join(img_file_path, total_img[i])+'\n' file = None if i in train_val: file_train_val.write(full_path) if i in train: file = file_train else: file = file_val pass else: file = file_test pass file.write(full_path) file_train_val.close() file_train.close() file_val.close() file_test.close() pass split_data()
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 生成用于训练的标签文件。将annotation文件夹中的xml文件进行转换生成的包括所有txt标签文件的label文件夹,txt文件每行包括:类别,x,y,w,h五个数据。
import shutil import random import xml.etree.ElementTree as ET import os import json from tqdm import tqdm sets = ['train', 'val', 'test'] def convert(size, box): dw = 1. / (size[0]) dh = 1. / (size[1]) x = (box[0] + box[1]) / 2.0 - 1 y = (box[2] + box[3]) / 2.0 - 1 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, annotation_path, root_path, classes): in_file = open(annotation_path + '/%s.xml' % (image_id), encoding='UTF-8') out_file = open(os.path.join(root_path, 'labels') + '/%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 # difficult = obj.find('Difficult').text cls = obj.find('name').text 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)) b1, b2, b3, b4 = b # 标注越界修正 if b2 > w: b2 = w if b4 > h: b4 = h b = (b1, b2, b3, b4) bb = convert((w, h), b) out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n') def generate_labels(): with open('config.json', 'r') as f: data = json.load(f) classes = data['classes'] root_path = data['root'] annotation_path = data['annotation'] image_path = data['image'] txt_path = data['txt'] for image_set in sets: if not os.path.exists(os.path.join(root_path, 'labels')): os.makedirs(os.path.join(root_path, 'labels')) image_ids = open( txt_path + '/%s.txt' % ( image_set)).read().strip().split() for image_id in tqdm(image_ids): convert_annotation(os.path.basename(image_id)[:-4],annotation_path,root_path,classes) generate_labels()
训练
配置yolo5x.yaml文件。
修改yolo5x.yaml中的nc 参数为训练数据的类别数。
配置 类别 yaml文件。
新建yaml文件,根据训练数据修改对应参数。
train: ./data/txt/train.txt val: ./data/txt/val.txt test: ./data/txt/test.txt # Classes nc: 7 # number of classes names: ["类别1","类别2",""] # class names
- 1
- 2
- 3
- 4
- 5
- 6
- 7
python train.py --data 自定义类别.yaml --cfg yolov5n.yaml --weights yolov5n.pt --batch-size 128 yolov5s yolov5s yolov5m yolov5m yolov5l yolov5l yolov5x yolov5x
- 1
- 2
- 3
- 4
- 5
推理
python detect.py --weights best.pt --source 0 # webcam
img.jpg # image
vid.mp4 # video
path/ # directory
path/*.jpg # glob
'https://youtu.be/Zgi9g1ksQHc' # YouTube
'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream
模型转换
将pt模型转换为用于TensorRT框架下的.engine文件.
python detect.py --weights best.pt --data 自定义类别.yaml --include 'engine' simplify
生成.engine文件后使用.engine进行推理
ython detect.py --weights best.engine --source 0 # webcam
img.jpg # image
vid.mp4 # video
path/ # directory
path/*.jpg # glob
'https://youtu.be/Zgi9g1ksQHc' # YouTube
'rtsp://example.com/media.mp4' # RTSP, RTMP, HTTP stream
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。