登录谷歌网盘:https://drive.google.com/
选择 My Drive文件夹,点击上方的New,新建一个文件夹。
1)新建一个文件夹,取名:colab_yolov5(可以自己定义),再新建一个Google Colaboratory
2.点击修改-》笔记本设置-〉改成GPU并保存。建议大家在不用的时候,将其改为none,以免过载。
3.查看相应参数
!nvidia-smi
4.查看pytorch
import torch
torch.__version__
5.安装torchvision
!pip3 install torchvision
6.接下来就把google drive挂载过来
import os
from google.colab import drive
drive.mount('/content/drive')
path = "/content/drive/My Drive"
os.chdir(path)
os.listdir(path)
7.下载yolov5算法包
!git clone https://github.com/ultralytics/yolov5.git
8.导入数据
from google.colab import drive
drive.mount('/content/drive')
9.安装环境依赖
%pip install -qr requirements.txt
我的数据集是voc格式,标签是xml格式,需要再训练前将数据进行格式转换、数据集划分等操作。
关于标注格式等问题,可以参考下面这边博文,介绍的非常详细:https://blog.csdn.net/didiaopao/article/details/119808973
1.我们参考这边博文的标签及文件的格式来制作自己的数据集。以下是我的数据集文件结构,建议大家在自己的电脑中,按照这个结构来,名称最好也不要改动。下面的分别是Annotations、JPEGImages,(打字打错了)
2.接下来将VOCdevkit整个文件夹拖到刚刚在谷歌网盘上yolov5文件夹中。
3.数据集格式转换及划分
在yolov5文件夹下创建一个.py文件,例如data_init.py
文件内容如下:
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 = ["1"]
#改成自己的类
#classes=["ball"]
TRAIN_RATIO = 90
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.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))
bb = convert((w,h), b)
out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
in_file.close()
out_file.close()
wd = os.getcwd()
wd = os.getcwd()
data_base_dir = os.path.join(wd, "VOCdevkit/")
if not os.path.isdir(data_base_dir):
os.mkdir(data_base_dir)
work_sapce_dir = os.path.join(data_base_dir, "VOC2007/")
if not os.path.isdir(work_sapce_dir):
os.mkdir(work_sapce_dir)
annotation_dir = os.path.join(work_sapce_dir, "Annotations/")
if not os.path.isdir(annotation_dir):
os.mkdir(annotation_dir)
clear_hidden_files(annotation_dir)
image_dir = os.path.join(work_sapce_dir, "JPEGImages/")
if not os.path.isdir(image_dir):
os.mkdir(image_dir)
clear_hidden_files(image_dir)
yolo_labels_dir = os.path.join(work_sapce_dir, "YOLOLabels/")
if not os.path.isdir(yolo_labels_dir):
os.mkdir(yolo_labels_dir)
clear_hidden_files(yolo_labels_dir)
yolov5_images_dir = os.path.join(data_base_dir, "images/")
if not os.path.isdir(yolov5_images_dir):
os.mkdir(yolov5_images_dir)
clear_hidden_files(yolov5_images_dir)
yolov5_labels_dir = os.path.join(data_base_dir, "labels/")
if not os.path.isdir(yolov5_labels_dir):
os.mkdir(yolov5_labels_dir)
clear_hidden_files(yolov5_labels_dir)
yolov5_images_train_dir = os.path.join(yolov5_images_dir, "train/")
if not os.path.isdir(yolov5_images_train_dir):
os.mkdir(yolov5_images_train_dir)
clear_hidden_files(yolov5_images_train_dir)
yolov5_images_test_dir = os.path.join(yolov5_images_dir, "val/")
if not os.path.isdir(yolov5_images_test_dir):
os.mkdir(yolov5_images_test_dir)
clear_hidden_files(yolov5_images_test_dir)
yolov5_labels_train_dir = os.path.join(yolov5_labels_dir, "train/")
if not os.path.isdir(yolov5_labels_train_dir):
os.mkdir(yolov5_labels_train_dir)
clear_hidden_files(yolov5_labels_train_dir)
yolov5_labels_test_dir = os.path.join(yolov5_labels_dir, "val/")
if not os.path.isdir(yolov5_labels_test_dir):
os.mkdir(yolov5_labels_test_dir)
clear_hidden_files(yolov5_labels_test_dir)
train_file = open(os.path.join(wd, "yolov5_train.txt"), 'w')
test_file = open(os.path.join(wd, "yolov5_val.txt"), 'w')
train_file.close()
test_file.close()
train_file = open(os.path.join(wd, "yolov5_train.txt"), 'a')
test_file = open(os.path.join(wd, "yolov5_val.txt"), 'a')
list_imgs = os.listdir(image_dir) # list image files
prob = random.randint(1, 100)
print("Probability: %d" % prob)
for i in range(0,len(list_imgs)):
path = os.path.join(image_dir,list_imgs[i])
if os.path.isfile(path):
image_path = image_dir + list_imgs[i]
voc_path = list_imgs[i]
(nameWithoutExtention, extention) = os.path.splitext(os.path.basename(image_path))
(voc_nameWithoutExtention, voc_extention) = os.path.splitext(os.path.basename(voc_path))
annotation_name = nameWithoutExtention + '.xml'
annotation_path = os.path.join(annotation_dir, annotation_name)
label_name = nameWithoutExtention + '.txt'
label_path = os.path.join(yolo_labels_dir, label_name)
prob = random.randint(1, 100)
print("Probability: %d" % prob)
if(prob < TRAIN_RATIO): # train dataset
if os.path.exists(annotation_path):
train_file.write(image_path + '\n')
convert_annotation(nameWithoutExtention) # convert label
copyfile(image_path, yolov5_images_train_dir + voc_path)
copyfile(label_path, yolov5_labels_train_dir + label_name)
else: # test dataset
if os.path.exists(annotation_path):
test_file.write(image_path + '\n')
convert_annotation(nameWithoutExtention) # convert label
copyfile(image_path, yolov5_images_test_dir + voc_path)
copyfile(label_path, yolov5_labels_test_dir + label_name)
train_file.close()
test_file.close()
3.如果你的文件结构和上述结构一样,此代码无需修改,直接运行。
!python data_init.py
4.yaml文件
在yolov5文件夹下的data文件夹下新建一个data2.yaml文件:
data2.yaml文件内容如下:
train: /content/drive/MyDrive/yolov5/VOCdevkit/images/train
val: /content/drive/MyDrive/yolov5/VOCdevkit/images/val
nc: 1
names: ['1']
其中,nc代表类别数,请改成自己的,names代表类别名称,也请改成自己的。
1.为了防止中断导致训练无法延续,建议修改train.py中的resume中的default为True。中断后重新训练直接从断点开始。
1.开始训练,选用yolo5s作为初始权重。
!python train.py --data data/data2.yaml --cfg models/yolov5s.yaml --weights 'yolov5s.pt' --batch-size 64
pip install -U pyyaml
首先训练时会提示,权重的保存地址:如下:runs/train/exp15
1.选择一部分数据集,将其放在自己电脑里的test文件夹里。上传的谷歌网盘中的yolov5/data文件夹下面。
2.回到colab界面,并刷新文件列表。
3.修改以下两个位置的权重地址,和测试集地址如下:
最后,测试完成后,根据其保存的位置查看带有标签的测试图片。