yolov5-seg训练自己的数据

一、yolov5-seg官方源码下载

网址指路:https://github.com/ultralytics/yolov5.git

二、环境配置

1. 预先安装好pytorch、cuda、cudnn等相关深度学习模块

2. 文件夹中有一个文件requirements.txt,这里是环境依赖的说明

3. 我们在终端输入pip install -r requirements.txt下载安装依赖包

三、创建数据集

1. 数据集格式

yolov5

     images

          train

          val

          test

    labels

          train

          val

          test

其中,images下每个文件夹存放图片数据,各个文件夹作用名字清晰可见;labels下每个文件夹存放标注txt文件,各个文件夹作用名字清晰可见。txt文件内容具体解释如下:

图像绿色框为数据集类别,红色框中为标注点的相对位置。 

 2. 数据集制作

大多数公司采用labelme对polygon进行标注,最后会得到json文件,利用json文件制作yolov5-seg数据集,代码如下:

def json2txt(json_path,save_txt_path):

    # json_path     : json文件路径
    # save_txt_path : txt文件保存位置

    with open(json_path, "r") as f:
            data = f.read()
        data = json.loads(data)
    
    img_name = data['imagePath'].split('.')[0]
    img_x = data['imageHeight']
    img_y = data['imageWidth']
    shapes = data['shapes'] 

    # 保存一个txt文件
    txt = open(os.path.join(save_txt_path, img_name + '.txt'), 'w')
    
    for shape in shapes:

        label = shape['label']
        txt.write(str(label))

        points = np.array(shape['points'])
        points[:,0] /= img_y
        points[:,1] /= img_x
        points.tolist()

        for x,y in points:
            txt.write(' ' + str(round(x, 6)))
            txt.write(' ' + str(round(y, 6)))
            
        txt.write('\n')
    txt.close()

3. 划分数据集

import os
import random
import shutil
from tqdm import tqdm

images_path = '../images' # 全部图片位置
labels_path = '../labels' # 全部txt位置

# 比例
train,val,test = 0.8,0.1,0.1

# 图片划分位置
train_images_save_path = '../yolov5/images/train2023'
os.makedirs(train_images_save_path,exist_ok=True)
val_images_save_path = '../yolov5/images/val2023'
os.makedirs(val_images_save_path,exist_ok=True)
test_images_save_path = '../yolov5/images/test2023'
os.makedirs(test_images_save_path,exist_ok=True)

# txt划分位置
train_labels_save_path = '../yolov5/labels/train2023'
os.makedirs(train_labels_save_path,exist_ok=True)
val_labels_save_path = '../yolov5/labels/val2023'
os.makedirs(val_labels_save_path,exist_ok=True)
test_labels_save_path = '../yolov5/labels/test2023'
os.makedirs(test_labels_save_path,exist_ok=True)


files = os.listdir(images_path)

random.shuffle(files)

for idx,file in tqdm(enumerate(files)):

    if idx <= len(files) * train:
        shutil.copy(os.path.join(images_path,file),train_images_save_path)
        shutil.copy(os.path.join(labels_path,file.replace('jpg','txt')),train_labels_save_path)

    elif idx <= len(files) * (train + val):
        shutil.copy(os.path.join(images_path, file), test_images_save_path)
        shutil.copy(os.path.join(labels_path, file.replace('jpg', 'txt')), test_labels_save_path)
        
    else:
        shutil.copy(os.path.join(images_path, file), val_images_save_path)
        shutil.copy(os.path.join(labels_path, file.replace('jpg', 'txt')),val_labels_save_path)

四、更改配置

1. coco128-seg.yaml

 2. yolov5s-seg.yaml

可以修改nc为自己类别数量,也可以不修改、depth与width调节网络大小、anchors为预置锚框可以自己重新聚类,但其实大多数不用。

五、训练

1. segment/train.py

--weights 预训练模型位置

--cfg  yolov5s-seg.yaml位置

--data coco128-seg.yaml位置

--hyp data/hyps/hyp.no-augmentation.yaml 超参数配置文件位置

 --imgsz 训练图片尺寸

--image-weights 图像加强,默认不开启,为了解决样本不平衡问题

--multi-scale 多尺度训练,默认不开启,解决小目标问题

--single-cls 单一类别训练,默认不开启,仅适用于仅有一个类别

--label-smoothing 标签平滑,默认为0.0

--patience early stop,默认100epoch不变化时

以上时常用参数选项,且对于上述默认不开启的开关,还在测试效果中,后续会更新实验结果。

更新中。。。。。

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值