WIN10+Pytorch+YOLOv5的运行,训练,测试

环境搭建

以后faster-rcnn的pytorch环境,对环境进行复制

conda create -n yolo --clone fasterrcnn        

然后一键pip -r requirements.txt

DEBUG!

# 使用yolov5s模型训练coco128数据集5个epochs,batch size设为16
python  detect.py --source ./data/images/bus.jpg

Results saved to runs\detect\exp4

就运行成功了??

尝试训练自己的模型

数据处理

  1. 标注处理
    json转为yolo要求的txt格式
    转换代码如下

  2. 图像数据和标签数据集分别 划分为:训练集、验证集、测试集
    比例为6:2:2,一个简单的复制文件

# VOC数据集制作——ImageSets\Main里的四个txt文件
# 0.6 : 0.2 : 0.2
import os
import random
import os
import numpy as np
import codecs
import json
from glob import glob
import cv2
import shutil
from sklearn.model_selection import train_test_split

trainval_percent = 0.8
train_percent = 0.75
xmlfilepath = 'C:/Users/SuLiang/Desktop/pythonProject/turn/yolo/label/'
imgfilepath = 'C:/Users/SuLiang/Desktop/pythonProject/turn/yolo/images/'
total_xml = os.listdir(xmlfilepath)
saved_path = "./images/"  # 不懂者勿动
if not os.path.exists(saved_path + "train"):
    os.makedirs(saved_path + "train")
if not os.path.exists(saved_path + "val"):
    os.makedirs(saved_path + "val")
if not os.path.exists(saved_path + "test"):
    os.makedirs(saved_path + "test")

saved_path = "./label/"  # 不懂者勿动
if not os.path.exists(saved_path + "train"):
    os.makedirs(saved_path + "train")
if not os.path.exists(saved_path + "val"):
    os.makedirs(saved_path + "val")
if not os.path.exists(saved_path + "test"):
    os.makedirs(saved_path + "test")

num = len(total_xml)
print(num)
list = range(num)
tv = int(num * trainval_percent)
tr = int(tv * train_percent)
trainval = random.sample(list, tv)
train = random.sample(trainval, tr)

#ftrainval = open(txtsavepath + '/trainval.txt', 'w')
#ftest = open(txtsavepath + '/test.txt', 'w')
#ftrain = open(txtsavepath + '/train.txt', 'w')
#fval = open(txtsavepath + '/val.txt', 'w')

for i in list:
    name = total_xml[i][:-4]
    print(name)
    if i in trainval:
        #shutil.copy(xmlfilepath + name + '.jpg', xmlfilepath+'/')
        if i in train:
            shutil.copy(xmlfilepath + name + '.txt', xmlfilepath+'/train')
            shutil.copy(imgfilepath + name + '.jpg', imgfilepath + '/train')

        else:
            shutil.copy(xmlfilepath + name + '.txt', xmlfilepath+'/val')
            shutil.copy(imgfilepath + name + '.jpg', imgfilepath + '/val')
    else:
        shutil.copy(xmlfilepath + name + '.txt', xmlfilepath+'/test')
        shutil.copy(imgfilepath + name + '.jpg', imgfilepath + '/test')




  1. 文件存储格式
    WIN10+Pytorch+YOLOv5的运行,训练,测试_第1张图片

  2. 制作yaml文件

# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: ../datasets/mydata  # dataset root dir
train: images/train  # train images (relative to 'path') 118287 images
val: images/val  # val images (relative to 'path') 5000 images
test: images/test  # 20288 of 40670 images, submit to https://competitions.codalab.org/competitions/20794

# Classes
nc: 4  # number of classes
names: ['break', 'errorbreak', 'strip', 'whitebreak']  # class names

常用命令

#训练
python train.py --batch 16 --epochs 50 --data ./data/mydata.yaml --weights ./weights/yolov5s.pt --workers 1 --batch-size 8
#检测
python detect.py --weights best.pt --source ../datasets/mydata/images/test
#tensorboard
tensorboard --logdir=runs

你可能感兴趣的:(#,yolov5,pytorch,pytorch,深度学习,机器学习)