Swin-Transfomer环境配置及推理测试
环境要求一览
- Linux or macOS (Windows is in experimental support)
- Python 3.6+
- PyTorch 1.3+
- CUDA 9.2+ (If you build PyTorch from source, CUDA 9.0 is also compatible)
- GCC 5+
- MMCV
安装
虚拟环境准备
创建conda虚拟环境并激活
conda create -n openmmlab python=3.7 -y
conda activate openmmlab
安装Pyorch和torchvision
conda install pytorch torchvision -c pytorch
确保CUDA编译版本和CUDA运行时版本是匹配的。由于我本机nvcc -V
查询得到的结果是cuda_11.1,所以执行下面的代码安装。
conda install pytorch torchvision torchaudio cudatoolkit=11.1 -c pytorch -c nvidia
安装MMDetection
自动安装
推荐使用MIM安装MMDetection,它可以自动处理OpenMMLab项目的各种依赖,包括mmcv和其他python包。
pip install openmim
mim install mmdet
手动安装
Install mmcv-full
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/{cu_version}/{torch_version}/index.html
Please replace {cu_version}
and {torch_version}
in the url to your desired one. For example, to install the latest mmcv-full
with CUDA 11.0
and PyTorch 1.7.0
, use the following command:
pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.html
See here for different versions of MMCV compatible to different PyTorch and CUDA versions.
Optionally you can compile mmcv from source if you need to develop both mmcv and mmdet. Refer to the guide for details.
Install MMDetection
方式1
pip install mmdet
方式2
git clone https://github.com/open-mmlab/mmdetection.git
cd mmdetection
pip install -r requirements/build.txt
pip install -v -e . # or "python setup.py develop"
Install extra dependencies
For Instaboost, Panoptic Segmentation, LVIS dataset, or Albumentations.
# for instaboost
pip install instaboostfast
# for panoptic segmentation
pip install git+https://github.com/cocodataset/panopticapi.git
# for LVIS dataset
pip install git+https://github.com/lvis-dataset/lvis-api.git
# for albumentations
pip install albumentations>=0.3.2 --no-binary imgaug,albumentations
Note:
a. When specifying -e
or develop
, MMDetection is installed on dev mode , any local modifications made to the code will take effect without reinstallation.
b. If you would like to use opencv-python-headless
instead of opencv-python
, you can install it before installing MMCV.
c. Some dependencies are optional. Simply running pip install -v -e .
will only install the minimum runtime requirements. To use optional dependencies like albumentations
and imagecorruptions
either install them manually with pip install -r requirements/optional.txt
or specify desired extras when calling pip
(e.g. pip install -v -e .[optional]
). Valid keys for the extras field are: all
, tests
, build
, and optional
.
d. If you would like to use albumentations
, we suggest using pip install albumentations>=0.3.2 --no-binary imgaug,albumentations
. If you simply use pip install albumentations>=0.3.2
, it will install opencv-python-headless
simultaneously (even though you have already installed opencv-python
). We should not allow opencv-python
and opencv-python-headless
installed at the same time, because it might cause unexpected issues. Please refer to official documentation for more details.
在GPU不支持的环境下安装
MMDetection可以在只支持CPU的环境下创建。在CPU模式下可以运行demo/webcam_demo.py,但是有些功能将无法使用。
不支持在CPU下进行训练。
其他选项:Docker Image
提供了Dockerfile用于创建镜像,确保你的docker版本>=19.03
# build an image with PyTorch 1.6, CUDA 10.1
docker build -t mmdetection docker/
使用下面的命令运行
docker run --gpus all --shm-size=8g -it -v {DATA_DIR}:/mmdetection/data mmdetection
验证
验证MMDetection是否正确安装,可以运行下面的sample code来初始化检测器和推理一张demo图像。
from mmdet.apis import init_detector, inference_detector
config_file = '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 = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
device = 'cuda:0'
# init a detector
model = init_detector(config_file, checkpoint_file, device=device)
# inference the demo image
inference_detector(model, 'demo/demo.jpg')
在程序根目录下,先下载权重,再使用下面的验证脚本:
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