赞
踩
系统配置:win11
进入VOCdevkit,创建images、labels文件夹,
进入images文件夹,创建test、train、val文件夹,用于储存图片数据集
进入labels文件夹,创建train、val文件夹,用于储存数据标签
从数据集网站下载所需检测目标的图片,或百度图片下载,存放于VOCdevkit\images\train中
数据集网站:Kaggle: Your Machine Learning and Data Science Community
下载地址:Miniconda — Anaconda documentation
找到对应系统、Python版本下载。下载完,打开安装包,按照默认提示,下一步下一步。(不建议安装到C盘)
右键此电脑 -> 属性 -> 下滑找到“高级系统设置”
点击环境变量
找到 “系统变量” 里的path,点编辑
新建环境变量
- 我的安装路径是E:\miniconda\miniconda
- 我的新建环境变量是:
- E:\miniconda\miniconda
- E:\miniconda\miniconda\Scripts
- E:\miniconda\miniconda\Library\bin
测试:win + r,输入cmd,打开命令提示符,输入conda,如下图就是安装成功了
win + r,输入cmd,打开命令提示符,输入
- activate base
-
- pip install Pillow
- pip install labelimg -i https://pypi.tuna.tsinghua.edu.cn/simple
创建python虚拟环境
conda create -n yolov5 python=3.8 # -n是name的缩写,python不指定版本就默认最新版
查看已经安装的虚拟环境列表
conda env list
进入虚拟环境
activate yolov5
退出当前虚拟环境
deactivate
安装Sublime Text 3,网址:Download - Sublime Text
创建translate.py文件,并用Sublime Text 3打开,使用快捷键Ctrl+B运行Python代码(代码如下)
使用方法:将图片跟脚本放在同一个目录,然后运行脚本即可,图片会自动保存在save
文件夹中
- from PIL import Image
- import os
-
- d = os.listdir('.')
- pic_id = 1
- save_path = os.path.join(os.path.abspath(''), 'save')
- if not os.path.exists(save_path):
- os.mkdir(save_path)
- for p in [i for i in d if ('.jpg' in i) or ('.JPG' in i) or ('.png' in i)]:
- im = Image.open(p)
- try:
- im.save(os.path.join(save_path, str(pic_id) + '.jpg'))
- except Exception as e:
- pass
- pic_id += 1
win + r,输入cmd,打开命令提示符,输入
labelimg
打开labelimg
简单配置
标注
- 1.按下w,出现十字叉
- 2.鼠标左键按住开始拖动选控
- 3.松开鼠标左键
- 4.输入标签名称,例如yellow bell pepper
- 5.按下d,进入下一张图片
示例:
下面以训练集:验证集=3:1为例,划分数据集,python代码如下,若验证集不需要太多,自己手动移动即可(图片和标注文件,不要忘了class.txt)(经测试,下面代码对于已经排好序号的数据集并非随机划分),若要使用需修改文件路径
- import os
- import shutil
-
- image_path = 'E:/test/Image/save' # 图片文件
- txt_path = 'E:/test/Image/label' # 标签文件
- new_file_path = 'E:/test/Image/later' # 划分数据后的文件
- train_rate = 0.75 # 训练集比例
- val_rate = 0.25 # 验证集比例
-
- # 将有对应标签的图片找出来,放到新文件夹下
- labels = []
- for label in os.listdir(txt_path):
- labels.append(os.path.splitext(label)[0])
- for image_name in os.listdir(image_path):
- image_name = os.path.splitext(image_name)[0]
-
- if image_name in labels:
- image_name = image_name + ".jpg"
- shutil.copy(image_path + '/' + image_name, new_file_path)
-
- # 计算训练集与验证集数量
- images = []
- for image in os.listdir(new_file_path):
- images.append(image)
- total = len(images)
- train_images = images[0:int(train_rate * total)]
- val_images = images[int(train_rate * total):int((train_rate + val_rate) * total)]
-
- # 图片-train
- for image in train_images:
- print(image)
- old_path = new_file_path + '/' + image
- new_path1 = new_file_path + '/' + 'images' + '/' + 'train'
- # new_path1 = new_file_path + '/' + 'train' + '/' + 'images'
- if not os.path.exists(new_path1):
- os.makedirs(new_path1)
- # new_path = new_path1 + '/' + image
- shutil.copy(old_path, new_path1)
-
- # 图片-val
- for image in val_images:
- old_path = new_file_path + '/' + image
- new_path1 = new_file_path + '/' + 'images' + '/' + 'val'
- # new_path1 = new_file_path + '/' + 'val' + '/' + 'images'
- if not os.path.exists(new_path1):
- os.makedirs(new_path1)
- # new_path = new_path1 + '/' + image
- shutil.copy(old_path, new_path1)
-
- # 标签-train
- images1 = []
- for image in os.listdir(new_file_path + '/' + 'images' + '/' + 'train'):
- images1.append(os.path.splitext(image)[0])
- for label_name in os.listdir(txt_path):
- label_name = os.path.splitext(label_name)[0]
- if label_name in images1:
- label_name = label_name + ".txt"
- label_train_path = new_file_path + '/' + 'labels' + '/' + 'train'
- if not os.path.exists(label_train_path):
- os.makedirs(label_train_path)
- shutil.copy(txt_path + '/' + label_name, label_train_path)
- shutil.copy(txt_path + '/' + 'classes.txt', label_train_path)
-
- # 标签-val
- images2 = []
- for image in os.listdir(new_file_path + '/' + 'images' + '/' + 'val'):
- images2.append(os.path.splitext(image)[0])
- for label_name in os.listdir(txt_path):
- label_name = os.path.splitext(label_name)[0]
- if label_name in images2:
- label_name = label_name + ".txt"
- label_val_path = new_file_path + '/' + 'labels' + '/' + 'val'
- if not os.path.exists(label_val_path):
- os.makedirs(label_val_path)
- shutil.copy(txt_path + '/' + label_name, label_val_path)
- shutil.copy(txt_path + '/' + 'classes.txt', label_val_path)
-
- # 删除新文件夹下对应标签的图片
- for name in os.listdir(new_file_path):
- if name.endswith('.jpg'):
- os.remove(os.path.join(new_file_path, name))
-
-
- if image_name in labels:
- image_name = image_name + ".jpg"
- shutil.copy(image_path + '/' + image_name, new_file_path)
-
- # 计算训练集与验证集数量
- images = []
- for image in os.listdir(new_file_path):
- images.append(image)
- total = len(images)
- train_images = images[0:int(train_rate * total)]
- val_images = images[int(train_rate * total):int((train_rate + val_rate) * total)]
-
- # 图片-train
- for image in train_images:
- print(image)
- old_path = new_file_path + '/' + image
- new_path1 = new_file_path + '/' + 'images' + '/' + 'train'
- # new_path1 = new_file_path + '/' + 'train' + '/' + 'images'
- if not os.path.exists(new_path1):
- os.makedirs(new_path1)
- # new_path = new_path1 + '/' + image
- shutil.copy(old_path, new_path1)
-
- # 图片-val
- for image in val_images:
- old_path = new_file_path + '/' + image
- new_path1 = new_file_path + '/' + 'images' + '/' + 'val'
- # new_path1 = new_file_path + '/' + 'val' + '/' + 'images'
- if not os.path.exists(new_path1):
- os.makedirs(new_path1)
- # new_path = new_path1 + '/' + image
- shutil.copy(old_path, new_path1)
-
- # 标签-train
- images1 = []
- for image in os.listdir(new_file_path + '/' + 'images' + '/' + 'train'):
- images1.append(os.path.splitext(image)[0])
- for label_name in os.listdir(txt_path):
- label_name = os.path.splitext(label_name)[0]
- if label_name in images1:
- label_name = label_name + ".txt"
- label_train_path = new_file_path + '/' + 'labels' + '/' + 'train'
- if not os.path.exists(label_train_path):
- os.makedirs(label_train_path)
- shutil.copy(txt_path + '/' + label_name, label_train_path)
- shutil.copy(txt_path + '/' + 'classes.txt', label_train_path)
-
- # 标签-val
- images2 = []
- for image in os.listdir(new_file_path + '/' + 'images' + '/' + 'val'):
- images2.append(os.path.splitext(image)[0])
- for label_name in os.listdir(txt_path):
- label_name = os.path.splitext(label_name)[0]
- if label_name in images2:
- label_name = label_name + ".txt"
- label_val_path = new_file_path + '/' + 'labels' + '/' + 'val'
- if not os.path.exists(label_val_path):
- os.makedirs(label_val_path)
- shutil.copy(txt_path + '/' + label_name, label_val_path)
- shutil.copy(txt_path + '/' + 'classes.txt', label_val_path)
-
- # 删除新文件夹下对应标签的图片
- for name in os.listdir(new_file_path):
- if name.endswith('.jpg'):
- os.remove(os.path.join(new_file_path, name))
下载地址:ultralytics/yolov5 at v5.0 (github.com)
将整个yolov5压缩包下载下来(注意路径不要有中文),发现是没有预训练权重文件的,这时我们要自己下载一个,这里我下载yolo5s.pt,放于yolov5-5.0文件夹中
将自己的数据集文件VOCdevkit放到yolov5-5.0文件夹中
首先要在data文件夹里新建一个配置文件myvoc.yaml,修改为你的类别。
- # train and val data as 1) directory: path/images/, 2) file: path/images.txt, or 3) list: [path1/images/, path2/images/]
- train: /mnt/yolov5-5.0/VOCdevkit/images/train #训练集路径
- val: /mnt/yolov5-5.0/VOCdevkit/images/val #验证集路径
-
- # number of classes
- nc: 3
-
- # class names
- names: ['yellow bell pepper', 'red bell pepper','green bell pepper' ]
然后是model文件夹里复制一下yolo5s.yaml并命名为yolo5s_mine.yaml。修改类别的数量。本来nc=80,这里改为3
下载后,将yolov5-5.0文件夹拖拽上传到矩池云网盘
选择环境
win + r,输入cmd,打开命令提示符,输入SSH命令和密码,再输入代码,进入网盘
cd /mnt
输入代码,进入 yolov5-5.0
cd yolov5-5.0/
由于系统镜像是yolov5-6.1,输入代码,需要配置环境
pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple
输入代码,开始训练(由于系统镜像是yolov5-6.1,所以还需要配置环境)
python3 train.py --weights yolov5s.pt --data data/myvoc.yaml --workers 4 --batch-size 20 --epochs 10 #epochs为训练轮数,可改
出现下列结果,则训练成功
下载得到的best.pt,即为最好的训练模型
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。