目标检测算法——mmdetection下面的deformable-detr运行

38 篇文章 15 订阅
6 篇文章 0 订阅

1、环境

版本
mmcv-full1.4.2
mmdet2.19.1
torch1.10.0+cu113
torchvision0.11.1+cu113

在这里插入图片描述

2、文档

mmet官方文档
mmcv官方文档
源码下载

3、数据集

自定义数据集

4、修改代码

4.1、生成文件

打开mmdetection-master/tools下面执行train.py文件

其中配置文件--configmmdetection-master/configs/deformable_detr/deformable_detr_r50_16x2_50e_coco.py

python train.py {path}/mmdetection-master/configs/deformable_detr/deformable_detr_r50_16x2_50e_coco.py

会报错,不用管(在work_dirs/deformable_detr_r50_16x2_50e_coco生成需要的配置文件)
在这里插入图片描述

4.2、修改配置文件

my_deformable_detr_r50_16x2_50e_coco.py文件修改

  1. 将该文件复制到mmdetection-master/configs/deformable_detr/并改名为my_deformable_detr_r50_16x2_50e_coco.py
  2. 找到对应行数,修改为自己的路径
    在这里插入图片描述
  3. 修改类别数目
    在这里插入图片描述
  4. 加载预训练权重(可自己评估,我训练数据较少,加上官方预训练权重)
    在这里插入图片描述
  5. 上一步下载路径

在这里插入图片描述

修改mmdet源码

  1. 修改{path}/mmdet/core/evaluation/class_names.py下面的coco_classes()

    在这里插入图片描述

  2. 修改{path}/mmdet/datasets/coco.py下面的CLASSES和PALETTE

    有的源码里面没有PALETTE可不添加

    在这里插入图片描述

5、训练模型

再次进入mmdetection-master/tools下面,执行下面代码

其中的my_deformable_detr_r50_16x2_50e_coco.py是上面刚刚修改的文件名称

python train.py {path}/mmdetection-master/configs/deformable_detr/my_deformable_detr_r50_16x2_50e_coco.py

训练结束之后会生成相应的权重文件
在这里插入图片描述

6、测试模型数据

  1. 打开{path}/mmdetection-master/demo/文件夹执行image_demo.py

    python image_demo.py 1.jpg {path}/mmdetection-master/configs/deformable_detr/my_deformable_detr_r50_16x2_50e_coco.py {path}/mmdetection-master/tools/work_dirs/deformable_detr_r50_16x2_50e_coco/latest.pth
    
  2. 由于我在ubuntu虚拟机上面进行的代码测试,无法使用 show_result_pyplot()函数,稍作修改存储到对应的目录中
    ( 其中的第一个参数 img 修改成一个目录,可以直接进行对一个目录里面的文件读取并且处理后保存)

    from argparse import ArgumentParser
    from mmdet.apis import (inference_detector, init_detector)
    import cv2
    import os
    
    def parse_args():
        parser = ArgumentParser()
        parser.add_argument('--img', default='img2', help='Image file')
        parser.add_argument('--config', default='../configs/deformable_detr/my_deformable_detr_r50_16x2_50e_coco.py',help='Config file')
        parser.add_argument('--checkpoint',default='../tools/work_dirs/deformable_detr_r50_16x2_50e_coco/latest.pth', help='Checkpoint file')
        parser.add_argument('--device', default='cpu', help='Device used for inference')
        parser.add_argument(
            '--palette',
            default='coco',
            choices=['coco', 'voc', 'citys', 'random'],
            help='Color palette used for visualization')
        parser.add_argument(
            '--score-thr', type=float, default=0.3, help='bbox score threshold')
        parser.add_argument(
            '--async-test',
            action='store_true',
            help='whether to set async options for async inference.')
        args = parser.parse_args()
        return args
    
    
    def getfiles(file):
        path_list = []
        filenames = os.listdir(file)
        print(filenames)
        for filename in filenames:
            a = os.path.join(file, filename)
            # print(a)
            path_list.append(a)
        # print(path_list)
        return path_list,filenames
    
    
    def main(args):
        model = init_detector(args.config, args.checkpoint, device=args.device)
        # test a single image
        path_list,filenames = getfiles(args.img)
        for path,filename in zip(path_list,filenames):
            result = inference_detector(model, path)
            img = show_result_pyplot2(model, path, result, score_thr=0.8)
            cv2.imwrite(args.img+"/out/out_"+filename, img)
    
    
    def show_result_pyplot2(model, img, result, score_thr=0.3, fig_size=(15, 10)):
        if hasattr(model, 'module'):
            model = model.module
        img = model.show_result(img, result, score_thr=score_thr, show=False)
        return img
    
    
    if __name__ == '__main__':
        args = parse_args()
        main(args)
    

7、结果

预测结果还是比较准确的

在这里插入图片描述
在这里插入图片描述

  • 8
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lzh~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值