You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

get_started.md 10 kB

2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. ## 依赖
  2. - Linux 和 macOS (Windows 理论上支持)
  3. - Python 3.6+
  4. - PyTorch 1.3+
  5. - CUDA 9.2+ (如果基于 PyTorch 源码安装,也能够支持 CUDA 9.0)
  6. - GCC 5+
  7. - [MMCV](https://mmcv.readthedocs.io/en/latest/#installation)
  8. MMDetection 和 MMCV 版本兼容性如下所示,需要安装正确的 MMCV 版本以避免安装出现问题。
  9. | MMDetection 版本 | MMCV 版本 |
  10. | :--------------: | :----------------------: |
  11. | master | mmcv-full>=1.3.14, <1.4.0 |
  12. | 2.18.0 | mmcv-full>=1.3.14, <1.4.0 |
  13. | 2.17.0 | mmcv-full>=1.3.14, <1.4.0 |
  14. | 2.16.0 | mmcv-full>=1.3.8, <1.4.0 |
  15. | 2.15.1 | mmcv-full>=1.3.8, <1.4.0 |
  16. | 2.15.0 | mmcv-full>=1.3.8, <1.4.0 |
  17. | 2.14.0 | mmcv-full>=1.3.8, <1.4.0 |
  18. | 2.13.0 | mmcv-full>=1.3.3, <1.4.0 |
  19. | 2.12.0 | mmcv-full>=1.3.3, <1.4.0 |
  20. | 2.11.0 | mmcv-full>=1.2.4, <1.4.0 |
  21. | 2.10.0 | mmcv-full>=1.2.4, <1.4.0 |
  22. | 2.9.0 | mmcv-full>=1.2.4, <1.4.0 |
  23. | 2.8.0 | mmcv-full>=1.2.4, <1.4.0 |
  24. | 2.7.0 | mmcv-full>=1.1.5, <1.4.0 |
  25. | 2.6.0 | mmcv-full>=1.1.5, <1.4.0 |
  26. | 2.5.0 | mmcv-full>=1.1.5, <1.4.0 |
  27. | 2.4.0 | mmcv-full>=1.1.1, <1.4.0 |
  28. | 2.3.0 | mmcv-full==1.0.5 |
  29. | 2.3.0rc0 | mmcv-full>=1.0.2 |
  30. | 2.2.1 | mmcv==0.6.2 |
  31. | 2.2.0 | mmcv==0.6.2 |
  32. | 2.1.0 | mmcv>=0.5.9, <=0.6.1 |
  33. | 2.0.0 | mmcv>=0.5.1, <=0.5.8 |
  34. **注意:**如果已经安装了 mmcv,首先需要使用 `pip uninstall mmcv` 卸载已安装的 mmcv,如果同时安装了 mmcv 和 mmcv-full,将会报 `ModuleNotFoundError` 错误。
  35. ## 安装流程
  36. ### 准备环境
  37. 1. 使用 conda 新建虚拟环境,并进入该虚拟环境;
  38. ```shell
  39. conda create -n open-mmlab python=3.7 -y
  40. conda activate open-mmlab
  41. ```
  42. 2. 基于 [PyTorch 官网](https://pytorch.org/)安装 PyTorch 和 torchvision,例如:
  43. ```shell
  44. conda install pytorch torchvision -c pytorch
  45. ```
  46. **注意**:需要确保 CUDA 的编译版本和运行版本匹配。可以在 [PyTorch 官网](https://pytorch.org/)查看预编译包所支持的 CUDA 版本。
  47. `例 1` 例如在 `/usr/local/cuda` 下安装了 CUDA 10.1, 并想安装 PyTorch 1.5,则需要安装支持 CUDA 10.1 的预构建 PyTorch:
  48. ```shell
  49. conda install pytorch cudatoolkit=10.1 torchvision -c pytorch
  50. ```
  51. `例 2` 例如在 `/usr/local/cuda` 下安装了 CUDA 9.2, 并想安装 PyTorch 1.3.1,则需要安装支持 CUDA 9.2 的预构建 PyTorch:
  52. ```shell
  53. conda install pytorch=1.3.1 cudatoolkit=9.2 torchvision=0.4.2 -c pytorch
  54. ```
  55. 如果不是安装预构建的包,而是从源码中构建 PyTorch,则可以使用更多的 CUDA 版本,例如 CUDA 9.0。
  56. ### 安装 MMDetection
  57. 我们建议使用 [MIM](https://github.com/open-mmlab/mim) 来安装 MMDetection:
  58. ``` shell
  59. pip install openmim
  60. mim install mmdet
  61. ```
  62. MIM 能够自动地安装 OpenMMLab 的项目以及对应的依赖包。
  63. 或者,可以手动安装 MMDetection:
  64. 1. 安装 mmcv-full,我们建议使用预构建包来安装:
  65. ```shell
  66. pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/{cu_version}/{torch_version}/index.html
  67. ```
  68. 需要把命令行中的 `{cu_version}` 和 `{torch_version}` 替换成对应的版本。例如:在 CUDA 11 和 PyTorch 1.7.0 的环境下,可以使用下面命令安装最新版本的 MMCV:
  69. ```shell
  70. pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu110/torch1.7.0/index.html
  71. ```
  72. 请参考 [MMCV](https://mmcv.readthedocs.io/en/latest/#installation) 获取不同版本的 MMCV 所兼容的的不同的 PyTorch 和 CUDA 版本。同时,也可以通过以下命令行从源码编译 MMCV:
  73. ```shell
  74. git clone https://github.com/open-mmlab/mmcv.git
  75. cd mmcv
  76. MMCV_WITH_OPS=1 pip install -e . # 安装好 mmcv-full
  77. cd ..
  78. ```
  79. 或者,可以直接使用命令行安装:
  80. ```shell
  81. pip install mmcv-full
  82. ```
  83. 2. 安装 MMDetection:
  84. 你可以直接通过如下命令从 pip 安装使用 mmdetection:
  85. ```shell
  86. pip install mmdet
  87. ```
  88. 或者从 git 仓库编译源码
  89. ```shell
  90. git clone https://github.com/open-mmlab/mmdetection.git
  91. cd mmdetection
  92. pip install -r requirements/build.txt
  93. pip install -v -e . # or "python setup.py develop"
  94. ```
  95. 3. 安装额外的依赖以使用 Instaboost, 全景分割, 或者 LVIS 数据集
  96. ```shell
  97. # 安装 instaboost 依赖
  98. pip install instaboostfast
  99. # 安装全景分割依赖
  100. pip install git+https://github.com/cocodataset/panopticapi.git
  101. # 安装 LVIS 数据集依赖
  102. pip install git+https://github.com/lvis-dataset/lvis-api.git
  103. # 安装 albumentations 依赖
  104. pip install albumentations>=0.3.2 --no-binary imgaug,albumentations
  105. ```
  106. **注意:**
  107. (1) 按照上述说明,MMDetection 安装在 `dev` 模式下,因此在本地对代码做的任何修改都会生效,无需重新安装;
  108. (2) 如果希望使用 `opencv-python-headless` 而不是 `opencv-python`, 可以在安装 MMCV 之前安装;
  109. (3) 一些安装依赖是可以选择的。例如只需要安装最低运行要求的版本,则可以使用 `pip install -v -e .` 命令。如果希望使用可选择的像 `albumentations` 和 `imagecorruptions` 这种依赖项,可以使用 `pip install -r requirements/optional.txt ` 进行手动安装,或者在使用 `pip` 时指定所需的附加功能(例如 `pip install -v -e .[optional]`),支持附加功能的有效键值包括 `all`、`tests`、`build` 以及 `optional` 。
  110. (4) 如果希望使用 `albumentations`,我们建议使用 `pip install albumentations>=0.3.2 --no-binary imgaug,albumentations` 进行安装。 如果简单地使用 `pip install albumentations>=0.3.2` 进行安装,则会同时安装 `opencv-python-headless`(即便已经安装了 `opencv-python` 也会再次安装)。我们不允许同时安装 `opencv-python` 和 `opencv-python-headless`,因为这样可能会导致一些问题。更多细节请参考[官方文档](https://albumentations.ai/docs/getting_started/installation/#note-on-opencv-dependencies)。
  111. ### 只在 CPU 安装
  112. 我们的代码能够建立在只使用 CPU 的环境(CUDA 不可用)。
  113. 在CPU模式下,可以运行 `demo/webcam_demo.py` 示例,然而以下功能将在 CPU 模式下不能使用:
  114. - Deformable Convolution
  115. - Modulated Deformable Convolution
  116. - ROI pooling
  117. - Deformable ROI pooling
  118. - CARAFE: Content-Aware ReAssembly of FEatures
  119. - SyncBatchNorm
  120. - CrissCrossAttention: Criss-Cross Attention
  121. - MaskedConv2d
  122. - Temporal Interlace Shift
  123. - nms_cuda
  124. - sigmoid_focal_loss_cuda
  125. - bbox_overlaps
  126. 因此,如果尝试使用包含上述操作的模型进行推理,将会报错。下表列出了由于依赖上述算子而无法在 CPU 上运行的相关模型:
  127. | 操作 | 模型 |
  128. | :-----------------------------------------------------: | :----------------------------------------------------------: |
  129. | Deformable Convolution/Modulated Deformable Convolution | DCN、Guided Anchoring、RepPoints、CentripetalNet、VFNet、CascadeRPN、NAS-FCOS、DetectoRS |
  130. | MaskedConv2d | Guided Anchoring |
  131. | CARAFE | CARAFE |
  132. | SyncBatchNorm | ResNeSt |
  133. **注意**: MMDetection 目前不支持使用 CPU 进行训练。
  134. ### 另一种选择: Docker 镜像
  135. 我们提供了 [Dockerfile](https://github.com/open-mmlab/mmdetection/blob/master/docker/Dockerfile) 来生成镜像,请确保 [docker](https://docs.docker.com/engine/install/) 的版本 >= 19.03。
  136. ```shell
  137. # 基于 PyTorch 1.6, CUDA 10.1 生成镜像
  138. docker build -t mmdetection docker/
  139. ```
  140. 运行命令:
  141. ```shell
  142. docker run --gpus all --shm-size=8g -it -v {DATA_DIR}:/mmdetection/data mmdetection
  143. ```
  144. ### 从零开始设置脚本
  145. 假设当前已经成功安装 CUDA 10.1,这里提供了一个完整的基于 conda 安装 MMDetection 的脚本:
  146. ```shell
  147. conda create -n open-mmlab python=3.7 -y
  148. conda activate open-mmlab
  149. conda install pytorch==1.6.0 torchvision==0.7.0 cudatoolkit=10.1 -c pytorch -y
  150. # 安装最新版本的 mmcv
  151. pip install mmcv-full -f https://download.openmmlab.com/mmcv/dist/cu101/torch1.6.0/index.html
  152. # 安装 MMDetection
  153. git clone https://github.com/open-mmlab/mmdetection.git
  154. cd mmdetection
  155. pip install -r requirements/build.txt
  156. pip install -v -e .
  157. ```
  158. ### 使用多个 MMDetection 版本进行开发
  159. 训练和测试的脚本已经在 PYTHONPATH 中进行了修改,以确保脚本使用当前目录中的 MMDetection。
  160. 要使环境中安装默认的 MMDetection 而不是当前正在在使用的,可以删除出现在相关脚本中的代码:
  161. ```shell
  162. PYTHONPATH="$(dirname $0)/..":$PYTHONPATH
  163. ```
  164. ## 验证
  165. 为了验证是否正确安装了 MMDetection 和所需的环境,我们可以运行示例的 Python 代码来初始化检测器并推理一个演示图像:
  166. ```python
  167. from mmdet.apis import init_detector, inference_detector
  168. config_file = 'configs/faster_rcnn/faster_rcnn_r50_fpn_1x_coco.py'
  169. # 从 model zoo 下载 checkpoint 并放在 `checkpoints/` 文件下
  170. # 网址为: http://download.openmmlab.com/mmdetection/v2.0/faster_rcnn/faster_rcnn_r50_fpn_1x_coco/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth
  171. checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_coco_20200130-047c8118.pth'
  172. device = 'cuda:0'
  173. # 初始化检测器
  174. model = init_detector(config_file, checkpoint_file, device=device)
  175. # 推理演示图像
  176. inference_detector(model, 'demo/demo.jpg')
  177. ```
  178. 如果成功安装 MMDetection,则上面的代码可以完整地运行。

No Description

Contributors (3)