mmdetection-2.25.3的环境配置与安装

跌跌撞撞

配置任何一个深度学习的环境,依赖冲突都是难免的,何况看着一两年前的教程,加之版本更替,踩得坑就更多了

1.Anaconda虚拟环境搭建

打开Anaconda Prompt,创建新的虚拟环境:mmlab

conda create -n mmlab python=3.6

这里我踩了一个坑,我应该定位虚拟环境的位置,方便pycharm配置的时候好找一些

conda create --prefix D:\Anaconda3\envs\openmmlab  python=3.8 -y

之后进行虚拟环境的切换

activate mmlab

2.Pytorch安装

根据Pytorch官网的教程安装Pytorch,注意根据自己电脑的CUDA版本选择。在命令行运行nvcc -V查看当前cuda版本:
查找到自己的版本之后,进行如下代码
conda install pytorch==1.10.2 torchvision torchaudio cudatoolkit=11.3 -c pytorch

3.MMCV安装(1.6.2版本)

pip install mmcv==1.6.2 -i https://download.openmmlab.com/mmcv/dist/cu113/torch1.10/index.html

MMDetection安装(2.25.3版本)

到github上选中tag为2.25.3版本的MMDetection后,下载zip后解压
在mmlab虚拟环境中打开mmdetection-2.25.3文件夹
运行如下代码,这里使用清华镜像

pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple/ --trusted-host pypi.tuna.tsinghua.edu.cn
python setup.py develop

最后在mmdetection-2.25.3文件夹中创建一个checkpoints文件夹,下载预训练模型(下载链接)放置在这个文件夹中,在主目录下新建test.py,代码如下

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()

若出现目标检测图片,则说明成功

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容