如何用detr(detectiontransformer)训练自己的数据集(代码片段)

小小凡sir 小小凡sir     2023-03-14     352

关键词:

DETR(detection transformer)简介

DETR是Facebook AI的研究者提出的Transformer的视觉版本,是CNN和transformer的融合,实现了端到端的预测,主要用于目标检测和全景分割。
DETR的Github地址:https://github.com/facebookresearch/detr
DETR的论文地址:https://arxiv.org/pdf/2005.12872.pdf

DETR训练自己数据集

数据准备

DETR需要coco数据集才可以进行训练,需要将数据标签和图片保存为如下格式:
其中,annotations是如下json文件,

test、train和val2017存储的只有图片。
那么要如何得到coco数据集格式的文件呢,接下来我提供两种方法:

coco数据集获取

1、labelme打好json文件后转换为coco格式数据集
2、roboflow标注后直接生成coco格式数据集(需要连外网,需要的联系我可以免费给你提供好用的外网扩展程序)。roboflow网址:https://app.roboflow.com/
然后介绍如何用labelme转换数据集,首先在cmd python环境或者在pycharm终端输入pip install labelme,下载好后输入labelme进入打标签页面,打好标签后生成json文件,再运行如下脚本:

import argparse
import json
import matplotlib.pyplot as plt
import skimage.io as io
from labelme import utils
import numpy as np
import glob
import PIL.Image

class MyEncoder(json.JSONEncoder):
    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        else:
            return super(MyEncoder, self).default(obj)

class labelme2coco(object):
    def __init__(self, labelme_json=[], save_json_path='./tran.json'):
        self.labelme_json = labelme_json
        self.save_json_path = save_json_path
        self.images = []
        self.categories = []
        self.annotations = []
        # self.data_coco = 
        self.label = []
        self.annID = 1
        self.height = 0
        self.width = 0

        self.save_json()

    def data_transfer(self):
        for num, json_file in enumerate(self.labelme_json):
            with open(json_file, 'r') as fp:
                data = json.load(fp)  # 加载json文件
                self.images.append(self.image(data, num))
                for shapes in data['shapes']:
                    label = shapes['label']
                    if label not in self.label:
                        self.categories.append(self.categorie(label))
                        self.label.append(label)
                    points = shapes['points']  # 这里的point是用rectangle标注得到的,只有两个点,需要转成四个点
                    points.append([points[0][0], points[1][1]])
                    points.append([points[1][0], points[0][1]])
                    self.annotations.append(self.annotation(points, label, num))
                    self.annID += 1

    def image(self, data, num):
        image = 
        img = utils.img_b64_to_arr(data['imageData'])  # 解析原图片数据
        # img=io.imread(data['imagePath']) # 通过图片路径打开图片
        # img = cv2.imread(data['imagePath'], 0)
        height, width = img.shape[:2]
        img = None
        image['height'] = height
        image['width'] = width
        image['id'] = num + 1
        image['file_name'] = data['imagePath'].split('/')[-1]

        self.height = height
        self.width = width
        return image

    def categorie(self, label):
        categorie = 
        categorie['supercategory'] = 'Cancer'
        categorie['id'] = len(self.label) + 1  # 0 默认为背景
        categorie['name'] = label
        return categorie

    def annotation(self, points, label, num):
        annotation = 
        annotation['segmentation'] = [list(np.asarray(points).flatten())]
        annotation['iscrowd'] = 0
        annotation['image_id'] = num + 1
        # annotation['bbox'] = str(self.getbbox(points)) # 使用list保存json文件时报错(不知道为什么)
        # list(map(int,a[1:-1].split(','))) a=annotation['bbox'] 使用该方式转成list
        annotation['bbox'] = list(map(float, self.getbbox(points)))
        annotation['area'] = annotation['bbox'][2] * annotation['bbox'][3]
        # annotation['category_id'] = self.getcatid(label)
        annotation['category_id'] = self.getcatid(label)  # 注意,源代码默认为1
        annotation['id'] = self.annID
        return annotation

    def getcatid(self, label):
        for categorie in self.categories:
            if label == categorie['name']:
                return categorie['id']
        return 1

    def getbbox(self, points):
        # img = np.zeros([self.height,self.width],np.uint8)
        # cv2.polylines(img, [np.asarray(points)], True, 1, lineType=cv2.LINE_AA)  # 画边界线
        # cv2.fillPoly(img, [np.asarray(points)], 1)  # 画多边形 内部像素值为1
        polygons = points
        mask = self.polygons_to_mask([self.height, self.width], polygons)
        return self.mask2box(mask)

    def mask2box(self, mask):
        '''从mask反算出其边框
        mask:[h,w]  0、1组成的图片
        1对应对象,只需计算1对应的行列号(左上角行列号,右下角行列号,就可以算出其边框)
        '''
        # np.where(mask==1)
        index = np.argwhere(mask == 1)
        rows = index[:, 0]
        clos = index[:, 1]
        # 解析左上角行列号
        left_top_r = np.min(rows)  # y
        left_top_c = np.min(clos)  # x

        # 解析右下角行列号
        right_bottom_r = np.max(rows)
        right_bottom_c = np.max(clos)

        # return [(left_top_r,left_top_c),(right_bottom_r,right_bottom_c)]
        # return [(left_top_c, left_top_r), (right_bottom_c, right_bottom_r)]
        # return [left_top_c, left_top_r, right_bottom_c, right_bottom_r]  # [x1,y1,x2,y2]
        return [left_top_c, left_top_r, right_bottom_c - left_top_c,
                right_bottom_r - left_top_r]  # [x1,y1,w,h] 对应COCO的bbox格式
    def polygons_to_mask(self, img_shape, polygons):
        mask = np.zeros(img_shape, dtype=np.uint8)
        mask = PIL.Image.fromarray(mask)
        xy = list(map(tuple, polygons))
        PIL.ImageDraw.Draw(mask).polygon(xy=xy, outline=1, fill=1)
        mask = np.array(mask, dtype=bool)
        return mask

    def data2coco(self):
        data_coco = 
        data_coco['images'] = self.images
        data_coco['categories'] = self.categories
        data_coco['annotations'] = self.annotations
        return data_coco

    def save_json(self):
        self.data_transfer()
        self.data_coco = self.data2coco()
        # 保存json文件
        json.dump(self.data_coco, open(self.save_json_path, 'w'), indent=4, cls=MyEncoder)  # indent=4 更加美观显示

labelme_json = glob.glob(r'./*.json')
# labelme_json=['./1.json']
labelme2coco(labelme_json, '.\\\\instances_val2017.json')

这个脚本是我之前在别人CSDN找的,比较好用。

预训练文件下载

有了数据集后,为了加快学习速度,可以去官网下载预训练模型,官网提供的有resnet_50和resnet_101两个预训练版本,下载后得到pth文件。下载如下:

修改detr-main文件的一些配置

因为detr是针对的是91(数字可能错了,不是记得了)个目标进行预测,所以我们在进行预测的时候,需要把目标预测数目改为自己的需要检测目标的数目。首先需要修改上一步下载好的pth文件,运行如下脚本:

import torch
model1  = torch.load('detr-r101-2c7b67e5.pth')

num_class = 2 #我只需要检测一个物体,所以是2(检测个数+background)
model1["model"]["class_embed.weight"].resize_(num_class+1, 256)
model1["model"]["class_embed.bias"].resize_(num_class+1)
torch.save(model1, "detr-r50_test_%d.pth"%num_class)

然后还需要修改detr.py文件夹下的num_classes,

训练模型

训练模型这块,可以直接执行命令行,或者在main.py里面修改好参数后运行,
官方提供的命令行如下:

python -m torch.distributed.launch --nproc_per_node=8 --use_env main.py --coco_path /path/to/coco 

结束语

我觉得在训练那块还是改main.py文件比较好,需要改的地方挺多,我觉得需要修改的主要有–epoch(轮次)、–num_workers(主要看你电脑性能怎么样,好点可以调高些)、–output_dir(输出的模型权重,pth文件)、–dataset_file(数据存放位置)、–coco_path(coco数据集的位置)和–resume(预训练权重文件位置)。
还一点就是官方只提供了训练脚本,但是没预测脚本,其实预测脚本也挺简单的,就是加载模型,加载权重参数,然后传入图片预处理什么的,代码挺多的,放上来内容太多了,我写了两个预测脚本,需要的可以联系我,或者不会运行的可以问我,其实也挺简单的,多玩几次就会了~~
最后展示下效果吧,预测的还是挺准的

计算机视觉算法——detr/deformabledetr/detr3d(代码片段)

计算机视觉算法——DETR/DeformableDETR/DETR3D计算机视觉算法——DETR/DeformableDETR/DETR3D1.DETR1.1TransformerEncoder-Decoder1.2Set-to-SetLoss1.3PositionalEmbedding2.DeformableDETR2.1DeformableAttentionModule2.2Deformabl 查看详情

计算机视觉算法——基于transformer的目标检测(detr/deformabledetr/detr3d)(代码片段)

计算机视觉算法——基于Transformer的目标检测(DETR/DeformableDETR/DETR3D)计算机视觉算法——基于Transformer的目标检测(DETR/DeformableDETR/DETR3D)1.DETR1.1TransformerEncoder-Decoder1.2Set-to-SetLoss1.3PositionalEmbedding2.DeformableDETR2.1Defor... 查看详情

基于easycv复现detr和dab-detr,objectquery的正确打开方式

DETR是最近几年最新的目标检测框架,第一个真正意义上的端到端检测算法,省去了繁琐的RPN、anchor和NMS等操作,直接输入图片输出检测框。DETR的成功主要归功于Transformer强大的建模能力,还有匈牙利匹配算法解决了如何通过学... 查看详情

睿智的目标检测65——pytorch搭建detr目标检测平台(代码片段)

睿智的目标检测65——Pytorch搭建DETR目标检测平台学习前言源码下载DETR实现思路一、整体结构解析二、网络结构解析1、主干网络Backbone介绍a、什么是残差网络b、什么是ResNet50模型c、位置编码2、编码网络Encoder网络介绍a、Transformer... 查看详情

vit社区开放麦#38目标检测新范式!detr系列算法解读-知识点目录

社区开放麦#38目标检测新范式!DETR系列算法解读1.IntrotoDETRDETR论文解读DETR系列算法在MMDet-v2.0实现时的缺陷2.DETR(ECCV2022)3.ConditionalDETR(ICCV2021)4.DAB-DETR(ICLR2022)5.DeformableDETR(ICLR2021)6.DINO(ICLR2023) 查看详情

detr目标检测算法学习记录

...够在CV领域取得如此瞩目的成果属实令人惊叹,随后DETR模型横空出世,在目标检测领域掀起一阵狂潮。今天我们就来进行该算法的学习,探究其能够在众多的目标检测算法中脱颖而出的奥秘。模型结构主要思想DETR的... 查看详情

detr训练自己的数据集-实践笔记(代码片段)

DETR(DetectionwithTRansformers)训练自己的数据集-实践笔记&问题总结DETR(DetectionwithTRansformers)是基于transformer的端对端目标检测,无NMS后处理步骤,无anchor。实现使用NWPUVHR10数据集训练DETR.NWPU数据集总共包含十种类别目标,... 查看详情

transformer检测神器!detrex:面向detr系列的目标检测开源框架(代码片段)

前言在我们IDEA研究院CVR团队分别开源了DAB-DETR,DN-DETR,DINO后,CVR团队一直计划做一个统一的DETR系列代码框架,支持DETR系列的算法工作,并且希望这套codebase可以拓展到更多相关的任务上,终于在9.21号,CVR团队... 查看详情

百度飞桨顶会论文复现营detr解读笔记

百度飞桨顶会论文复现营DETR解读笔记目标检测两个关键子任务:目标分类和目标定位。DETR模型是将目标检测视为集合预测(setprediction)的方式,将训练和预测做到真正的端对端,不需要NMS的后处理,也不... 查看详情

keil中如何用keil中如何用汇编调用c函数?

如图所示,我在汇编中用IMPORT声明,调用C函数,但是编译提示错误,请问是什么原因?检查C文件是否加入项目。检查C文件中是否有这两个函数的原型声明。参考技术A关注这个问题 查看详情

如何用excel做散点图(如何用excel做柱状图)

参考技术A您好,现在我来为大家解答以上的问题。如何用excel做散点图,如何用excel做柱状图相信很多小伙伴还不知道,现在让我们一起来看看吧!1、exc...您好,现在我来为大家解答以上的问题。如何用excel做散点图,如何用excel做... 查看详情

如何用手机查看ip地址

...A  如何查看手机IP地址和MAC地址呢?下面是我整理的如何用手机查看ip地址的两个方法,欢迎大家阅读!  如何用手机查看ip地址  方法1:进入【设置】—【WLAN】—【高级设置】查看。  方法2:进入【设置】—【关于... 查看详情

如何用端口访问网站

1、准备一台windows2008R2操作系统的虚拟机环境2、3、 查看详情

如何用黑色填充灰色空间? SwiftUI

】如何用黑色填充灰色空间?SwiftUI【英文标题】:Howtofillgrayspacewithblack?SwiftUI【发布时间】:2021-10-2814:08:42【问题描述】:如何用黑色绘制一个完全灰色的空间?看图片这是我的代码:.onTapGestureself.showModal=true.sheet(isPresented:self.$... 查看详情

脑筋急转弯:如何用两个栈实现一个队列&&如何用两个队列实现一个栈(代码片段)

文章目录前言我的想法如何用两个栈实现一个队列如何用两个队列实现一个栈别人的想法两个栈实现一个队列如何用两个队列实现一个栈前言其实我也不知道这两个问题又什么意义,就像不知道我们平时刷的算法题除了练脑... 查看详情

如何用电脑发布视频号?

用电脑发布视频号的方法有以下几步: 查看详情

如何用动态值编写场景大纲?

】如何用动态值编写场景大纲?【英文标题】:HowtowriteScenarioOutlineswithdinamicvalues​?【发布时间】:2021-09-0115:45:49【问题描述】:enterimagedescriptionhere例如:ScenarioOutline:GiventheuserisloggedintoHomeOperationsWhensearchingfor"<status>"Thenthe" 查看详情

如何用零填充数组?

】如何用零填充数组?【英文标题】:Howtopadanarraywithzeros?【发布时间】:2021-11-2509:42:57【问题描述】:fnmain()letarr:[u8;8]=[97,112,112,108,101];println!("Lenis",arr.len());println!("Elementsare:?",arr);error[E0308]:mismatchedtypes-->src/main.rs 查看详情