首页 > 其他分享 >目标检测框架MMDetection推理实验记录

目标检测框架MMDetection推理实验记录

时间:2023-09-30 19:33:08浏览次数:43  
标签:parser help 框架 -- args argument call MMDetection 推理


在进行目标检测算法的学习过程中,需要进行对比实验,这里可以直接使用MMDetection框架来完成,该框架集成了许多现有的目标检测算法,方便我们进行对比实验。

环境配置

首先是环境配置,先前博主曾经有过相关方面的配置,这里就简要记录一下:
创建conda环境:

conda create --name openmmlab python=3.7 -y
conda activate openmmlab

安装pytorch

pip install torch==1.7.0+cu110 torchvision==0.8.1+cu110 torchaudio==0.7.0 -f https://download.pytorch.org/whl/torch_stable.html

安装pytorch时推荐使用pip安装,否则会报错:

OSError: /home/ubuntu/.conda/envs/mmdet/lib/python3.7/site-packages/torch/lib/../../../../libcublas.so.11: symbol free_gemm_select version libcublasLt.so.11 not defined in file libcublasLt.so.11 with link time reference

安装mmcv-full,安装MMCV需要对应CUDA和torch,安装命令要符合下面格式:

pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/{cu_version}/{torch_version}/index.html

如安装CUDA11.0,pytorch=1.7.0:

pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.h

安装mmdet

pip install mmdet -i https://pypi.tuna.tsinghua.edu.cn/simple

运行报错:

File "/home/ubuntu/.conda/envs/mmdet/lib/python3.7/site-packages/mmdet/__init__.py", line 18, in <module>
    f'MMCV=={mmcv.__version__} is used but incompatible. ' \
AssertionError: MMCV==1.7.1 is used but incompatible. Please install mmcv>=2.0.0rc4, <2.1.0.

mmcv版本不正确,要求安装2.0.0,进入下面网址:
https://mmcv.readthedocs.io/en/latest/get_started/installation.html

随后根据版本选择mmcv版本:

目标检测框架MMDetection推理实验记录_计算机视觉

pip install mmcv==2.0.0rc4 -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7/index.html

简单测试

运行一下demo/image_demo.py,修改input(输入图片),model(配置文件),weights(权重文件)三个参数即可。

from argparse import ArgumentParser
from mmengine.logging import print_log
from mmdet.apis import DetInferencer
def parse_args():
    parser = ArgumentParser()
    parser.add_argument(
        '--inputs', type=str,default="/home/ubuntu/programs/mmdetection/images/000000263594.jpg", help='Input image file or folder path.')
    parser.add_argument(
        '--model',       type=str,default="/home/ubuntu/programs/mmdetection/configs/faster_rcnn/faster-rcnn_r50_fpn_2x_coco.py",
        help='Config or checkpoint .pth file or the model name '
        'and alias defined in metafile. The model configuration '
        'file will try to read from .pth if the parameter is '
        'a .pth weights file.')
    parser.add_argument('--weights', default="/home/ubuntu/programs/mmdetection/weights/faster_rcnn_r50_fpn_2x_coco.pth", help='Checkpoint file')
    parser.add_argument(
        '--out-dir',
        type=str,
        default='/home/ubuntu/programs/mmdetection/outputs/',
        help='Output directory of images or prediction results.')
    parser.add_argument('--texts', help='text prompt')
    parser.add_argument(
        '--device', default='cuda:0', help='Device used for inference')
    parser.add_argument(
        '--pred-score-thr',
        type=float,
        default=0.8,
        help='bbox score threshold')
    parser.add_argument(
        '--batch-size', type=int, default=1, help='Inference batch size.')
    parser.add_argument(
        '--show',
        action='store_true',
        help='Display the image in a popup window.')
    parser.add_argument(
        '--no-save-vis',
        action='store_true',
        help='Do not save detection vis results')
    parser.add_argument(
        '--no-save-pred',
        action='store_true',
        help='Do not save detection json results')
    parser.add_argument(
        '--print-result',
        action='store_true',
        help='Whether to print the results.')
    parser.add_argument(
        '--palette',
        default='none',
        choices=['coco', 'voc', 'citys', 'random', 'none'],
        help='Color palette used for visualization')
    # only for GLIP
    parser.add_argument(
        '--custom-entities',
        '-c',
        action='store_true',
        help='Whether to customize entity names? '
        'If so, the input text should be '
        '"cls_name1 . cls_name2 . cls_name3 ." format')
    call_args = vars(parser.parse_args())
    if call_args['no_save_vis'] and call_args['no_save_pred']:
        call_args['out_dir'] = ''
    if call_args['model'].endswith('.pth'):
        print_log('The model is a weight file, automatically '
                  'assign the model to --weights')
        call_args['weights'] = call_args['model']
        call_args['model'] = None
    init_kws = ['model', 'weights', 'device', 'palette']
    init_args = {}
    for init_kw in init_kws:
        init_args[init_kw] = call_args.pop(init_kw)
    return init_args, call_args
def main():
    init_args, call_args = parse_args()
    # TODO: Video and Webcam are currently not supported and
    #  may consume too much memory if your input folder has a lot of images.
    #  We will be optimized later.
    inferencer = DetInferencer(**init_args)
    inferencer(**call_args)

    if call_args['out_dir'] != '' and not (call_args['no_save_vis']
                                           and call_args['no_save_pred']):
        print_log(f'results have been saved at {call_args["out_dir"]}')
if __name__ == '__main__':
    main()

权重文件下载

将训练好的权重文件下载完成后,对应好配置文件即可,权重文件在github中可以找到,如faster-r-cnn的文件,我们进入到config/faster-r-cnn后可以看到许多版本的faster-rcnn:

https://github.com/open-mmlab/mmdetection/tree/main/configs/faster_rcnn

目标检测框架MMDetection推理实验记录_ubuntu_02

然后下面有对应的权重文件,我们将其下载后即可完成推理过程。

目标检测框架MMDetection推理实验记录_权重_03

推理结果如下:

目标检测框架MMDetection推理实验记录_人工智能_04


标签:parser,help,框架,--,args,argument,call,MMDetection,推理
From: https://blog.51cto.com/u_15876949/7664968

相关文章

  • FlyUI-WPF框架
    关注我,WPFFlyUI框架作者github地址:https://github.com/AatroxBot/FlyUI.Demo.git码云地址:https://gitee.com/Aatrox1/fly-ui-demo.git......
  • PPT| IBM企业流程框架方法论 P43
    本人从事咨询工作多年,二十年一线数字化规划咨询经验,提供制造业数智化转型规划服务,顶层规划/企业架构/数据治理/数据安全解决方案资料干货.【智能制造数字化咨询】该PPT共43页,由于篇幅有限,以下为部分资料,如需完整原版 方案,点击关注下方。当企业进行IT系统建设或数据资产盘点时,一般......
  • Spring缓存框架使用及原理
    使用maven依赖<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId>......
  • Java集合框架(部分)
    类图List:有序可重复集合特点1.元素允许重复2.元素有序(元素的顺序就是插入时的顺序)3.元素可以通过索引来访问或者设置{ArrayLIst:底层为数组,查询速度快,增删慢LinkedList:底层是链表,查询速度慢,增删快两者的优缺点,:效率高,线程不安全}Set:无序不可重复集合HashSet:底层为数组,......
  • Go每日一库之184:katana(新一代爬虫框架)
    项目链接https://github.com/projectdiscovery/katana项目简介katana是一个使用golang编写的新一代爬虫框架,支持HTTP和headless抓取网页信息不仅可以作为库集成到Golang项目,还可以通过命令行直接抓取,对于有一些轻量级的抓取任务的开发者配合jq一起使用简直就是福......
  • Go每日一库之146:bbs-go(bbs框架)
    概要bbs-go是一款基于Go语言研发的开源、前后端分离、精美小巧、跨平台的社区系统。初期该项目仅用过学习和交流,开源之后越来越多的小伙伴儿开始喜欢和关注他,这也是我长期升级和维护的动力。bbs-go为前后端分离设计,后端接口服务使用简洁的Go语言进行开发,前端页面使用Vue.js进......
  • Go每日一库之135:Ent(Facebook 开源 Golang 实体框架)
    对于后端开发者来说,一款好用的框架能够大大提升应用的开发效率。为了降低开发者使用TiDB的门槛,方便开发者快速连接到TiDB,我们也在和合作伙伴一起,逐步完善面向主流开发语言和框架的连接支持。近日,Facebook开源的Golang实体框架Ent完成了对TiDB数据库的支持。Ent是......
  • vue前端框架ruoyi介绍
    Ruoyi是一个基于Vue.js和SpringBoot框架构建的开源前后端分离的企业级快速开发平台。它遵循了前后端分离的架构模式,将前端和后端进行解耦,使得系统更加灵活、可扩展和易于维护。Ruoyi的前端部分采用了Vue.js框架,这是一个流行的JavaScript前端框架,专注于构建用户界面。Vue......
  • 在线CAD 的前端框架搭建(网页显示CAD图纸)
    前言DWG格式的图纸是AutoCAD的私有格式,很多用户需要在网页端查看和编辑CAD图纸,传统的方式是企业购买梦想CAD控件的OCX方案,此方案开发时间久且编辑功能丰富,但因新版谷歌浏览器不再支持AcitveX控件,因此更多的用户希望以Html5的方式实现在线CAD功能,今天我们就来讲一下梦想CAD控件的H5......
  • Aveva Marine VBNET 编程系列-搭建开发框架
    引用的DllAveva.ApplicationFramework.dllAveva.ApplicationFramework.Presentation菜单展示效果创建Attribute,用于反射来动态创建菜单,不用每次都去写commandPublicClassMyAmFunctionAttInheritsAttributePrivate_menuNameAsStringPublicPropertyM......