DETR训练自己的数据集(少走弯路版)

#模型训练#

我是一个科研小白,因为跑项目需要,所以需要训练一个DETR的目标检测模型作为自己的第一阶段模型来使用。按照网上很多流程下来,训练的DETR结果不错,但是放到我的模型中,object_error的识别错误率为100,于是开始溯源,发现训练出来的DETR模型用于检测图片,根本检测不出来。主要是数据转化出问题了。因此写下我的第一篇优快云博客。

一、labelme标注的json数据集转为coco数据集格式

我的方法是先转为yolo格式的txt文件,然后转为coco数据集

1、json to yolo

import json
import numpy as np
import os
import cv2
def json2yolo(path, cls_dict, types="bbox"):
    with open(path, 'r', encoding='utf-8') as fp:
        data = json.load(fp)
        h = data["imageHeight"]
        w = data["imageWidth"]
        shapes = data["shapes"]
        all_lines = ""
        for shape in shapes:
            if True:
                points = np.array(shape["points"])
                if types == "bbox":
                    x, y, wi, hi = cv2.boundingRect(points.reshape((-1, 1, 2)).astype(np.float32))
                    cx, cy = x + wi / 2, y + hi / 2
                    cx, cy, wi, hi = cx / w, cy / h, wi / w, hi / h
                    msg = "%.4f %.4f %.4f %.4f" % (cx, cy, wi, hi)
                else:
                    points[:, 0] = points[:, 0] / w
                    points[:, 1] = points[:, 1] / h
                    # 把np数组转换为yolo格式的str
                    points = points.reshape(-1)
                    points = list(points)
                    points = ['%.4f' % x for x in points]
                    msg = " ".join(points)
                l = shape['label'].lower()
                line = str(cls_dict[l]) + " " + msg + "\n"
                all_lines += line
    print(all_lines)
    filename = path.replace('json', 'txt')
    fh = open(filename, 'w', encoding='utf-8')
    fh.write(all_lines)
    fh.close()


path = r"     " #路径修改为自己的路径
path_list = os.listdir(path)
cls_dict = {'hand': 0,   } #需要更改为自己的标签
path_list2 = [x for x in path_list if ".json" in x]
for p in path_list2:
    json2yolo(path + "/" + p, cls_dict)

2、数据集划分

这里的数据集划分,替换为自己的路径文件即可

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值