MMDetection是一个基于 PyTorch 的目标检测开源工具箱。接下来就安装看看吧。

本人安装环境:

  • 系统环境:Ubuntu 20.04.2 LTS
  • cuda版本:11.0
  • cudnn版本:8.0.5
  • torch版本:1.7.0
  • torchvision版本:0.8.0
  • 在Linux下查看cuda和cudnn的版本:

    cat /usr/local/cuda/version.txt
    cat /usr/local/cuda/include/cudnn_version.h | grep CUDNN_MAJOR -A 2
    

    【注】新版本的cudnn的版本文件不在cudnn.h文件里,而是在cudnn_version.h,所以使用 cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2 命令没有输出。

    进入安装正题!

    1.创建虚拟环境,并激活虚拟环境

    conda create -n mmdetection python=3.7
    conda acitvate mmdetection
    

    2.安装pytorch

    pytorch根据自己的cuda和要安装的版本在PyTorch官网可以找到命令。

    conda install pytorch==1.7.0 torchvision==0.8.0 torchaudio==0.7.0 cudatoolkit=11.0 -c pytorch
    

    3.安装 mmcv-full

    MMDetection 和 MMCV 版本兼容性见github说明,需要安装正确的 MMCV 版本以避免安装出现问题。

    参考 MMCV 获取不同版本的 MMCV 所兼容的的不同的 PyTorch 和 CUDA 版本。

    例如:在 CUDA 11 和 PyTorch 1.7.0 的环境下,可以使用下面命令安装最新版本的 MMCV:

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

    4.安装mmdetection

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

    5.测试mmdetection是否安装成功

    在mmdetection目录下新建test.py和checkpoints文件夹,打开网址下载好权重文件放在checkpoints文件夹下,运行以下代码即可。

    from mmdet.apis import init_detector, inference_detector
    
    config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
    # 从 model zoo 下载 checkpoint 并放在 `checkpoints/` 文件下
    # 网址为: http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
    checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
    device = 'cuda:0'
    # 初始化检测器
    model = init_detector(config_file, checkpoint_file, device=device)
    # 推理演示图像
    img = 'demo/demo.jpg'
    result = inference_detector(model, img)
    model.show_result(img, result)
    # 将推理的结果保存
    model.show_result(img, result, out_file='result.jpg')
    

    运行虽然没错,但出现了一个警告:

    UserWarning: “ImageToTensor” pipeline is replaced by “DefaultFormatBundle” for batch inference. It is recommended to manually replace it in the test data pipeline in your config file. ‘data pipeline in your config file.’, UserWarning)

    解决方案:官方文档也给出了修改。在mmdetection/configs/_ base_/datasets/coco_detection.py下面修改test_pipeline。只修改了一行,把ImageToTensor改为DefaultFormatBundle,并将后面的keys=[‘img’]删掉。之后再运行就没有警告了。


    如果成功安装 MMDetection,则上面的代码可以完整地运行。

    6.安装其他依赖包

    进入到mmdetection项目目录中

    cd mmdetection
    pip install -r requirements/build.txt
    

    可看到,一共有四个安装依赖文件:build.txt, optional.txt, runtime.txt, tests.txt。

    来源:橘也

    物联沃分享整理
    物联沃-IOTWORD物联网 » MMDetection亲测安装教程

    发表评论