YOLOv8检测和分割训练自己数据集

Yolov8下载地址:GitHub – ultralytics/ultralytics: YOLOv8 🚀 in PyTorch > ONNX > CoreML > TFLitexx

下载完成后 按照YOLOv8教程系列:一、使用自定义数据集训练YOLOv8模型(详细版教程,你只看一篇->调参攻略),包含环境搭建/数据准备/模型训练/预测/验证/导出等_Zhijun.li@Studio的博客-CSDN博客

所写的进行目标检测的自己数据集的训练即可。

但是网上对于yolov8进行自己数据集的分割训练的教程较少 ,不会的可以看本文。

首先就是数据集的格式:(我自己使用的是这样,可以训练,参照coco数据集)

YOLOv8检测和分割训练自己数据集

其次,就是配置文件 ,在下图所示位置,并且按照右下所示进行修改:

 YOLOv8检测和分割训练自己数据集YOLOv8检测和分割训练自己数据集

此处修改完后,就是修改参数配置文件 (cdg里的default.yaml),要做分割任务首先是将task设置为segment,其实就是model改为yolov8n-seg.py(分割预训练权重),最后就是将data修改为自己数据集所在文件夹的路径就可以开始训练。

 如果报错runtimeerror: sizes of tensors must match except in dimension 1. expected size 2 but got size 0 for tensor number 1 in the list.

        原因是 torch.cat()函数中参数的维度不对应,如果数据集没有问题,那就是丢失信息了。

        那你使用的数据集中label里的txt文件的内容,大概率是转出来的是只有对应类别索引和四个点坐标(也就是rectangle格式),只能做检测,不可做分割,会丢失segment信息。

        可使用以下代码进行转换数据集:

# 处理labelme多边形矩阵的标注  json转化txt,提取点
import json
import os


name2id = {'chip': 0}    #  修改你的类别并且赋与index


def decode_json(json_floder_path, txt_outer_path, json_name):
    txt_name = txt_outer_path + json_name[:-5] + '.txt'
    with open(txt_name, 'a') as f:
        json_path = os.path.join(json_floder_path, json_name)
        data = json.load(open(json_path, 'r', encoding='gb2312', errors='ignore'))
        img_w = data['imageWidth']
        img_h = data['imageHeight']
        isshape_type = data['shapes'][0]['shape_type']
        print(isshape_type)
        dw = 1. / (img_w)
        dh = 1. / (img_h)
        for i in data['shapes']:
            label_name = i['label']
            if (i['shape_type'] == 'polygon'):
                point = []
                for lk in range(len(i['points'])):
                    x = float(i['points'][lk][0])
                    y = float(i['points'][lk][1])
                    point_x = x * dw
                    point_y = y * dh
                    point.append(point_x)
                    point.append(point_y)
                f.write(str(name2id[label_name]) + " " + " ".join([str(a) for a in point]) + '\n')
        f.close()


if __name__ == "__main__":
    json_floder_path = r'F:\Workprojects\front_corner_drop\train_data\json'  # 存放json的文件夹的绝对路径
    txt_outer_path = r'F:\Workprojects\front_corner_drop\labels\segment/'  # 存放txt的文件夹绝对路径
    json_names = os.listdir(json_floder_path)
    flagcount = 0
    for json_name in json_names:
        decode_json(json_floder_path, txt_outer_path, json_name)
        flagcount += 1

    print('-----------转化完毕------------')

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(1)
乘风的头像乘风管理团队
上一篇 2023年3月12日 下午10:57
下一篇 2023年3月12日

相关推荐