MMDetection v2 目标检测(1):环境搭建

上次介绍了如何在 Ubuntu 18.04 上搭建深度学习环境。

最近开始要做实验了,打算先学下 MMDetection

这里总结下 MMDetection v2 的安装和使用。

本文先介绍如何搭建 MMDetection v2 的实验环境。

2021.9.1 更新:适配 MMDetection v2.16

目录:

  • MMDetection v2 目标检测(1):环境搭建
  • MMDetection v2 目标检测(2):数据准备
  • MMDetection v2 目标检测(3):配置修改
  • MMDetection v2 目标检测(4):模型训练和测试

服务器的环境配置:

  • Ubuntu:18.04.5
  • CUDA:10.1.243
  • Python:3.7.9
  • PyTorch:1.5.1
  • MMDetection:2.16.0

1 PyTorch

1.1 搭建虚拟环境

  1. 创建虚拟环境:
conda create -n pytorch python=3.7
  1. 激活虚拟环境:
conda activate pytorch

相关命令:

conda env list  # 查看已创建的虚拟环境
conda deactivate  # 退出虚拟环境
conda create -n  --clone   # 克隆虚拟环境
conda remove -n  --all  # 删除虚拟环境

注意:
接下来均在该虚拟环境下操作。

  1. 查看 python 版本:
python --version

Python 3.7.9

1.2 安装常用的包

conda 安装:

conda install numpy scipy pandas matplotlib seaborn scikit-learn

其他的包,可根据需要,另行安装。

相关命令:

conda list  # 查看已安装的包
conda update   # 更新包
conda uninstall   # 卸载包

1.3 安装 PyTorch

  1. conda 安装:
conda install pytorch=1.5 torchvision cudatoolkit=10.1 -c pytorch
  1. Ipython 查看 pytorch 版本,并检测 cuda 是否可用:
In [1]: import torch
In [2]: print(torch.__version__)
In [3]: print(torch.cuda.is_available())
Out [2]: 1.5.1
Out [3]: True

2 MMCV

2.1 安装必要的包

pip 依次安装:

pip install cython
pip install opencv-python

2.2 安装 MMCV

  1. 下载预编译包安装(推荐)
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.5.0/index.html

其中,cu101torch1.5.0 需要根据自己版本修改。

或本地编译安装:

pip install mmcv-full
  1. Ipython 导入 mmcv 查看 cudagcc 版本:
In [1]: from mmcv.ops import get_compiling_cuda_version, get_compiler_version
In [2]: print(get_compiling_cuda_version())
In [3]: print(get_compiler_version())
Out [2]: 10.1
Out [3]: GCC 7.5

3 MMDetection

3.1 下载 MMDetection

  1. git 克隆 mmdetection 目录到本地:
git clone https://github.com/open-mmlab/mmdetection.git

Cloning into 'mmdetection'...
remote: Enumerating objects: 26, done.
remote: Counting objects: 100% (26/26), done.
remote: Compressing objects: 100% (23/23), done.
remote: Total 13701 (delta 10), reused 4 (delta 2), pack-reused 13675
Receiving objects: 100% (13701/13701), 13.97 MiB | 75.00 KiB/s, done.
Resolving deltas: 100% (9273/9273), done.

  1. 打开 mmdetection 目录:
cd mmdetection

3.2 安装依赖的包

pip 安装:

pip install -r requirements.txt

打开 ./requirements.txt

-r requirements/build.txt
-r requirements/optional.txt
-r requirements/runtime.txt
-r requirements/tests.txt

可以看到,包含四个依赖文件,具体内容如下:

# build.txt
# These must be installed before building mmdetection
cython
numpy

# optional.txt
cityscapesscripts
imagecorruptions
scipy
sklearn

# runtime.txt
matplotlib
numpy
pycocotools; platform_system == "Linux"
pycocotools-windows; platform_system == "Windows"
six
terminaltables

# tests.txt
asynctest
codecov
flake8
interrogate
isort==4.3.21
# Note: used for kwarray.group_items, this may be ported to mmcv in the future.
kwarray
-e git+https://github.com/open-mmlab/mmtracking#egg=mmtrack
onnx==1.7.0
onnxruntime>=1.8.0
pytest
ubelt
xdoctest>=0.10.0
yapf

3.3 安装 MMDetection

  1. 编译 mmdetection
python setup.py develop

running develop
running egg_info
creating mmdet.egg-info
writing mmdet.egg-info/PKG-INFO
writing dependency_links to mmdet.egg-info/dependency_links.txt
writing requirements to mmdet.egg-info/requires.txt
writing top-level names to mmdet.egg-info/top_level.txt
writing manifest file 'mmdet.egg-info/SOURCES.txt'
reading manifest file 'mmdet.egg-info/SOURCES.txt'
writing manifest file 'mmdet.egg-info/SOURCES.txt'
running build_ext
Creating /home/user/anaconda3/envs/pytorch/lib/python3.7/site-packages/mmdet.egg-link (link to .)

  1. Ipython 查看 mmdetection 版本:
In [1]: import mmdet
In [2]: print(mmdet.__version__)
Out [2]: 2.16.0

3.4 测试 MMDetection

  1. 新建模型参数目录:
mkdir checkpoints
  1. 下载 Faster R-CNN 的模型参数文件,并存放到 ./checkpoints 目录下。
wget -c http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth \
    -O checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
  1. 在终端运行:
python demo/image_demo.py demo/demo.jpg \
    configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py \
    checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth --device cpu

或在 JupyterLab 依次执行:

from mmdet.apis import init_detector, inference_detector, show_result_pyplot
import mmcv

config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'

model = init_detector(config_file, checkpoint_file, device='cuda:0')

img = 'demo/demo.jpg'
result = inference_detector(model, img)

show_result_pyplot(model, img, result)
测试结果

4 结语

最后再来回顾一下:

# pytorch
conda create -n pytorch python=3.7
conda activate pytorch
conda install pytorch=1.5 torchvision cudatoolkit=10.1 -c pytorch

# mmcv
pip install cython
pip install opencv-python
pip install mmcv-full

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

有帮助的话,点个赞再走吧,谢谢~

参考:

  1. 最新版本的mmdetection2.0 (v2.0.0版本)环境搭建、训练自己的数据集、测试以及常见错误集合
  2. Get Started

你可能感兴趣的:(MMDetection v2 目标检测(1):环境搭建)