• 目标检测工具箱MMDetection安装及使用示例


          之前在https://blog.csdn.net/fengbingchun/article/details/86693037 中介绍过MMDetection,它是OpenMMLab项目的一部分,是基于PyTorch的目标检测开源工具箱,最新发布的版本为v2.25.1,License为Apache-2.0。

          最新版的MMDetection既有CUDA模式也有CPU模式。在CPU模式下,可以进行模型训练、测试或者推理,但是有些功能在CPU模式下不支持,如ROI pooling、Deformable Convolution等,因此推荐使用CUDA模式。

          最新版本与之前介绍的在安装及接口方面变化都较大,在之前版本可正常执行的程序,在新版本已无法运行,因此这里重新做了整理:

          1.安装:使用conda安装

          (1).创建openmmlab虚拟环境:

    1. conda create -n openmmlab python=3.8
    2. conda activate openmmlab

          (2).安装PyTorch:这里PyTorch使用1.11.0版本,CUDA使用10.2版本,此CUDA版本对PyTorch各版本都支持

    conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 cudatoolkit=10.2 -c pytorch

          (3).安装MMCV:MMCV有两个版本,这里安装带CUDA的mmcv-full

          1).mmcv-full: 完整版,包含所有的特性以及丰富的开箱即用的CUDA算子,安装此版本需要较长时间。

          2).mmcv:精简版,不包含CUDA算子但包含其余所有特性和功能,类似MMCV 1.0之前的版本。

          不要在同一个环境中安装两个版本,否则可能会遇到类似ModuleNotFound的错误。在安装一个版本之前,需要先卸载另一个:

    1. pip uninstall mmcv-full
    2. pip uninstall mmcv

          注意:mmcv-full版本与mmdetection版本存在兼容对应关系,mmcv不能使用最新版,MMDetection 2.25.1要求MMCV版本(mmcv-full)为[1.3.17, 1.6.0),这里使用1.5.3版本。CUDA版本和PyTorch版本与安装PyTorch时保持一致

    pip install mmcv-full==1.5.3 -f https://download.openmmlab.com/mmcv/dist/cu102/torch1.11.0/index.html

          (4).安装MMDetection:花费时间较长

    pip install mmdet==2.25.1

          2.测试:通过Faster R-CNN进行目标检测,训练数据集是COCO

          (1).下载模型(checkpoint):

    1. model_path = "../../data/model/"
    2. model_name = "faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth"
    3. if os.path.isfile(model_path + model_name) == False:
    4. print("model file does not exist, now download ...")
    5. url = "http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth"
    6. subprocess.run(["wget", "-P", model_path, url])

          (2).根据配置文件和checkpoint文件构建模型:

    1. config_file = "../../src/mmdetection/configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py"
    2. model = init_detector(config_file, model_path+model_name, device='cuda:0')

          (3).准备测试图像:

    1. image_path = "../../data/image/"
    2. images_name = ["1.jpg", "2.jpg", "3.jpg"]
    3. images_name[:] = [image_path+x for x in images_name]

          (4).进行推理检测:

    inference_detector(model, images_name)

          (5).显示执行结果及保存图像:显示框的多少有阈值score_thr来控制

    1. out_dir = "../../data/result/"
    2. if not os.path.exists(out_dir):
    3. os.mkdir(out_dir)
    4. def show_and_save_result(img, result, out_dir, dataset="coco", score_thr=0.6):
    5. print("test image:", img)
    6. class_names = get_classes(dataset)
    7. labels = [np.full(bbox.shape[0], i, dtype=np.int32) for i, bbox in enumerate(result)]
    8. labels = np.concatenate(labels)
    9. bboxes = np.vstack(result)
    10. index = img.rfind("/")
    11. mmcv.imshow_det_bboxes(img, bboxes, labels, class_names, score_thr, show=True, out_file=out_dir+img[index+1:])

          执行结果如下图所示:以下原始图像均来自网络

     

           GitHubhttps://github.com/fengbingchun/PyTorch_Test

  • 相关阅读:
    Gin框架入门实战系列教程之Gin环境搭建 Gin程序的热加载 Gin路由 GET POST PUT DELETE
    什么是DFT?FT、FS、DTFT、DFS、DFT的关系
    医院项目-预约挂号-第一部分
    为AI电脑生态注入强悍动力,安耐美PlatiGemini 1200W高性能电源
    上市企业管理层短视,新的视角,整理好的面板数据,stata或excel版本
    UE4 UEngine.GameInstance.WorldContext.World.Level.Actor.Component
    Mac系统在idea中安装tomcat报错 error=13, Permission denied和error =1 Operation not...解决办法
    Linux 安装mysql8.X超详细图文教程
    使用Flink1.16.0的SQLGateway迁移Hive SQL任务
    程序员如何选择职业赛道?
  • 原文地址:https://blog.csdn.net/fengbingchun/article/details/126199218