MMDetection框架入门教程(一):Anaconda3下的安装教程(mmdet+mmdet3d)

本文介绍了MMDetection的安装步骤。MMDetection是MMLab家族成员,依赖Pytorch和MMCV。安装前需先搭建Anaconda虚拟环境,再依次安装Pytorch、MMCV、MMDetection和MMDetection3D,同时给出了各步骤的安装要点及验证安装是否成功的方法。
部署运行你感兴趣的模型镜像

MMDetection概述

MMDetection是MMLab家族的一员,是由香港中文大学和商汤科技共同推出的,以一个统一的架构支撑了15个大方向的研究领域。MMDetection依赖Pytorch和MMCV,因此安装之前需要先安装这两个库。
在这里插入图片描述

Step1: Anaconda虚拟环境搭建

打开Anaconda Prompt,创建一个新的虚拟环境mmlab。注意我这里选择了python3.6,因为在python3.7下出现了mmdet3d.apis中的类无法无法import的情况(但mmdet3d可以import),暂时不清楚原因。

// An highlighted block
conda create -n mmlab python=3.6

然后切换到该环境下:

// An highlighted block
activate mmlab

Step2: Pytorch安装

在这里插入图片描述
根据Pytorch官网的教程安装Pytorch,注意根据自己电脑的CUDA版本选择。在命令行运行nvcc -V查看当前cuda版本:
在这里插入图片描述另外,当前(2021.10.23)MMCV最新仅支持到pytorch-1.9,安装最新的pytorch1.10会导致MMDetection无法使用,因此安装的时候需要指定pytorch版本为1.9。

// An highlighted block
conda install pytorch==1.9.1 torchvision torchaudio cudatoolkit=10.2 -c pytorch

验证pytorch安装成功:
在这里插入图片描述

Step3: MMCV安装

MMCV有两mmcv-full和mmcv两个版本,两者差别在于是否包含CUDA操作,如果不需要使用CUDA可以安装mmcv,不过官方还是推荐安装完整版的mmcv-full,如果处于服务器这类无法联网的环境,可以参照官方说明使用源码安装:Build MMCV from source

Step4: MMDetection安装

首先从github上下载mmdetection的源码,然后根据requirements.txt安装所需的依赖库,最后执行setup.py安装mmdetection。

// An highlighted block
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -r requirements.txt
python setup.py develop

官方提供了Demo验证安装是否成功,位于./demo文件夹下。先新建一个demo_test工程,下载预训练模型(下载链接)放至test_demo\checkpoints文件下,新建main.py并运行:

// An highlighted block
import os

from mmdet.apis import init_detector, inference_detector


def demo_mmdet():
    base_dir = r'D:\Program Files\Third_Part_Lib\mmdetection'       # mmdetection的安装目录

    config_file = os.path.join(base_dir, r'configs\faster_rcnn\faster_rcnn_r50_fpn_1x_coco.py')
    # download the checkpoint from model zoo and put it in `checkpoints/`
    # url: https://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 = r'checkpoints\faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'

    # 根据配置文件和 checkpoint 文件构建模型
    model = init_detector(config_file, checkpoint_file, device='cuda:0')

    # 测试单张图片并展示结果
    img = os.path.join(base_dir, r'demo\demo.jpg') # 或者 img = mmcv.imread(img),这样图片仅会被读一次
    result = inference_detector(model, img)
    # 在一个新的窗口中将结果可视化
    model.show_result(img, result, out_file=None, show=True)


if __name__ == '__main__':
    demo_mmdet()

运行成功效果如下:
在这里插入图片描述

Step5: MMDetection3D安装

MMDetection3D依赖MMSegmentation,先安装MMSegmentation:

// An highlighted block
git clone https://github.com/open-mmlab/mmsegmentation.git
cd mmsegmentation
pip install -r requirements.txt
python setup.py develop

然后再安装MMDetection3D,这一步编译时间可能会久一点。

// An highlighted block
git clone https://github.com/open-mmlab/mmdetection3d.git
cd mmdetection3d
pip install -r requirements.txt
python setup.py develop

最后再利用官方提供的Demo,先下载预训练模型下载链接放至test_demo\checkpoints文件下,然后运行下面代码:

// An highlighted block
import os
from mmdet3d.apis import inference_detector, init_model, show_result_meshlab


def demo_mmdet3d():
    base_dir = r'D:\Program Files\Third_Part_Lib\mmdetection3d'  # mmdetection3d的安装目录

    config_file = os.path.join(base_dir, r'configs\second/hv_second_secfpn_6x8_80e_kitti-3d-car.py')
    # download the checkpoint from model zoo and put it in `checkpoints/`
    # url: https://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 = r'checkpoints\hv_second_secfpn_6x8_80e_kitti-3d-car_20200620_230238-393f000c.pth'
    # build the model from a config file and a checkpoint file
    model = init_model(config_file, checkpoint_file, device='cuda:0')

    # test a single bin
    pcd = os.path.join(base_dir, r'demo/data/kitti/kitti_000008.bin')
    result, data = inference_detector(model, pcd)
    # show the results
    show_result_meshlab(
        data,
        result,
        '',
        0,
        show=True,
        snapshot=False,
        task='det')


if __name__ == '__main__':
    demo_mmdet3d()

运行成功效果如下:
在这里插入图片描述
转载:Link

您可能感兴趣的与本文相关的镜像

PyTorch 2.5

PyTorch 2.5

PyTorch
Cuda

PyTorch 是一个开源的 Python 机器学习库,基于 Torch 库,底层由 C++ 实现,应用于人工智能领域,如计算机视觉和自然语言处理

### 寻找mmdetection3d预编译版本的方法 对于希望获取 `mmdetection3d` 预编译版本的用户而言,官方通常不会直接提供该软件包的二进制文件下载链接。大多数情况下,安装此库涉及从源码构建或是通过 Python 的包管理工具 pip 安装已发布的稳定版。 然而,如果确实存在特定平台上的预编译轮子(wheel),这些资源般托管于 PyPI 或者项目维护者的 GitHub 发布页面上。为了查找可能存在的预编译版本: - 访问 [PyPI](https://pypi.org/) 并搜索 `mmdet3d` 关键词。 - 浏览 [MMDetection3D GitHub Releases 页面](https://github.com/open-mmlab/mmdetection3d/releases),这里可能会有适用于不同操作系统和依赖配置的打包文件。 值得注意的是,在很多场景下推荐的方式还是按照文档指导来设置环境并从源码安装最新特性或修复过的提交版本[^2]。 当考虑安装类似 mmdetection 这样的深度学习框架扩展时,建议先确认所使用的 CUDA 和 PyTorch 版本组合,并据此选择合适的 MMCV 版本来确保兼容性[^3]。 另外需要注意的是,某些组件如 PCL 可能会有额外的编译需求以及与其他库比如 NumPy 的版本匹配问题[^4];不过这些问题主要针对更底层的支持库而非高层的应用级模型库如 mmdetection3d。 ```bash pip install mmdet3d -f https://download.openmmlab.com/mmdetection3d/wheels/cu101/torch1.8.0/index.html ``` 上述命令展示了如何指定 URL 来安装与特定 CUDA 和 PyTorch 版本相适应的 mmdetection3d 轮子文件。实际操作前应当查阅最新的官方指南以获得最准确的信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值