# Detectron2

**Detectron2** 是 Facebook 人工智能研究院（FAIR）推出的一个开源软件库，用于目标检测、实例分割及其他视觉识别任务，以 Python 编写，构建在 PyTorch 之上。它是 FAIR 早先的 Detectron 与 maskrcnn-benchmark 代码库的继任者，支撑着 Facebook 内部的研究项目与生产应用。<sup>[1](https://github.com/facebookresearch/detectron2)</sup> 该库的引用格式为 Yuxin Wu、Alexander Kirillov、Francisco Massa、Wan-Yen Lo 与 [Ross Girshick](https://www.edgepedia.org/zh/ross-girshick) 合著的 "Detectron2"，2019 年。<sup>[1](https://github.com/facebookresearch/detectron2)</sup>

| 关键事实 | 详情 |
|---|---|
| 开发者 | Facebook 人工智能研究院；Detectron 与 maskrcnn-benchmark 的继任者<sup>[1](https://github.com/facebookresearch/detectron2)</sup> |
| 许可证与平台 | Apache 2.0 许可证；基于 PyTorch 的 Python；GitHub 上获 34.9k 星标、7.9k 复刻<sup>[1](https://github.com/facebookresearch/detectron2)</sup> |
| 能力 | 全景分割、DensePose、Cascade R-CNN、旋转边界框、PointRend、DeepLab、ViTDet、MViTv2<sup>[1](https://github.com/facebookresearch/detectron2)</sup> |
| 训练速度 | 在 8 块 NVIDIA V100 上，R-50-FPN Mask R-CNN 达到每秒 62 张图像，而 mmdetection 为 53，原版 Detectron 为 19<sup>[2](https://github.com/facebookresearch/detectron2/blob/main/docs/notes/benchmarks.md/)</sup> |
| 环境要求 | Linux 或 macOS，Python ≥ 3.7，PyTorch ≥ 1.8 及匹配的 torchvision<sup>[3](https://github.com/facebookresearch/detectron2/blob/main/INSTALL.md)</sup> |
| 部署 | 模型可导出为 TorchScript 或 Caffe2 格式，从而无需 detectron2 依赖即可使用<sup>[1](https://github.com/facebookresearch/detectron2)</sup><sup> • </sup><sup>[4](https://detectron2.readthedocs.io/en/latest/tutorials/deployment.html)</sup> |

## 背景

Detectron2 取代了原版 Detectron——那是 FAIR 基于 Caffe2 的研究平台，实现了 [Mask R-CNN](https://www.edgepedia.org/zh/mask-r-cnn)、RetinaNet、Faster R-CNN、RPN、Fast R-CNN 和 R-FCN 等算法，并催生了特征金字塔网络、密集目标检测的焦点损失、非局部神经网络、DensePose 和组归一化等研究项目。<sup>[5](https://github.com/facebookresearch/Detectron)</sup> 原版 Detectron 仓库已于 2023 年 11 月 21 日被其所有者归档，现为只读状态。<sup>[5](https://github.com/facebookresearch/Detectron)</sup>

## 架构与用法

**配置与模型。** 模型由 yacs 配置对象通过 `build_model`、`build_backbone` 和 `build_roi_heads` 等函数构建；`build_model` 返回一个参数随机的 `torch.nn.Module`，权重则通过 `DetectionCheckpointer` 加载，后者既能识别 PyTorch 的 `.pth` 文件，也能识别模型库中的 `.pkl` 文件。<sup>[6](https://detectron2.readthedocs.io/en/latest/tutorials/models.html)</sup> 元架构（meta architecture）定义了一个模型由哪些组件构成以及它们如何交互；广义 R-CNN（generalized R-CNN）便是其中一种元架构。<sup>[7](https://elib.uni-stuttgart.de/server/api/core/bitstreams/910256d5-529f-4289-91e5-91836bf13b19/content)</sup> 模型以 `list[dict]` 作为输入，每个 dict 对应一张图像，其中包含一个 (C, H, W) 格式的 "image" 张量；训练期间，模型必须在收集训练统计数据的 `EventStorage` 下运行。<sup>[6](https://detectron2.readthedocs.io/en/latest/tutorials/models.html)</sup>

**数据加载。** 函数 `build_detection_train_loader` 与 `build_detection_test_loader` 根据给定配置创建默认数据加载器。它们接收一个已注册数据集的名称（例如 "coco_2017_train"），加载轻量级的 `list[dict]` 表示，并将每一项传入映射器函数，默认使用 `DatasetMapper`，后者读取图像、施加随机数据增强，并将数据转换为 torch 张量。映射器的输出被批处理为一个列表，通常即 `model.forward()` 的输入。<sup>[8](https://github.com/facebookresearch/detectron2/blob/main/docs/tutorials/data_loading.md)</sup> 通过 `mapper=` 参数传入自定义映射器即可满足大多数自定义数据加载需求；若要更改采样或批处理逻辑，则需编写不同的数据加载器——它不过是一个 Python 迭代器，产出模型所接受的格式。<sup>[8](https://github.com/facebookresearch/detectron2/blob/main/docs/tutorials/data_loading.md)</sup>

**训练。** 用户完全可以只用 PyTorch 编写自定义训练循环，也可以使用该库带有钩子系统的训练器抽象。`SimpleTrainer` 提供单代价、单优化器、单数据源训练的最小循环；`DefaultTrainer` 则允许覆写其 `build_{train,test}_loader` 方法以接入自定义数据加载器。<sup>[9](https://github.com/facebookresearch/detectron2/blob/main/docs/tutorials/training.md)</sup>

## 性能

该库自带的基准测试衡量的是端到端 R-50-FPN Mask R-CNN 的训练吞吐量，各实现采用相同超参数，在配备 NVLink 的 8 块 NVIDIA V100 上运行，环境为 Python 3.7、CUDA 10.1 和 PyTorch 1.5，取第 100 至 500 次迭代的平均吞吐量以跳过 GPU 预热阶段。Detectron2 达到每秒 62 张图像，领先于 mmdetection 与 maskrcnn-benchmark 的 53、tensorpack（TensorFlow）的 50、simpledet（MXNet）的 39、原版基于 Caffe2 的 Detectron 的 19，以及 matterport/Mask_RCNN（TensorFlow）的 14。<sup>[2](https://github.com/facebookresearch/detectron2/blob/main/docs/notes/benchmarks.md/)</sup> 原版 Detectron 数字偏低，是因为它的许多运算在 CPU 上运行。<sup>[2](https://github.com/facebookresearch/detectron2/blob/main/docs/notes/benchmarks.md/)</sup>

## 版本发布与配置系统

0.5 版引入了 "LazyConfig" 系统——一种基于 Python 的替代配置系统，语法更灵活，且无需预定义结构；同时带来了使用大尺度抖动与更长训练改进的 Mask R-CNN 基线，以及允许在 detectron2 内部训练和评估 mmdetection 模型的封装器。<sup>[10](https://github.com/facebookresearch/detectron2/releases)</sup> 0.6 版于 11 月 15 日发布，为 CUDA 11.3、11.1 和 10.2 搭配 PyTorch 1.8 至 1.10 提供了预构建的 Linux 二进制文件。<sup>[10](https://github.com/facebookresearch/detectron2/releases)</sup> 从源码构建需要 gcc 与 g++ ≥ 5.4，ninja 为可选项，但推荐使用以加快构建速度。<sup>[3](https://github.com/facebookresearch/detectron2/blob/main/INSTALL.md)</sup>

## 部署

转换后的模型可在不依赖 detectron2 或 torchvision 的情况下，以 Python 或 C++ 在 CPU 或 GPU 上运行。三种常见元架构 `GeneralizedRCNN`、`RetinaNet` 和 `PanopticFPN` 下的大多数官方模型均支持导出。<sup>[4](https://detectron2.readthedocs.io/en/latest/tutorials/deployment.html)</sup> 使用 `caffe2_tracing` 方法导出的模型采用部署文档中描述的特殊输入格式。<sup>[4](https://detectron2.readthedocs.io/en/latest/tutorials/deployment.html)</sup>

## 应用

Detectron2 在研究界与工业界被广泛用于需要检测、分割或关键点估计的任务。已发表的应用包括道路损坏检测与分类：Detectron2 的 Faster R-CNN 搭配 X101-FPN 基础模型与默认配置，在 2020 年全球道路损坏检测挑战赛的两个测试集上分别取得 51.0% 和 51.4% 的 F1 分数，并证明可跨国家迁移。<sup>[11](https://arxiv.org/abs/2010.15021)</sup> 在农业领域，Detectron2 被用于实现 Mask R-CNN，以在甘薯田间等级估计中进行实例分割。<sup>[12](https://arxiv.org/abs/2308.08534)</sup> 据报道，应用领域还包括零售货架监控与商品编目、视频背景去除与实时分割、野生动物相机陷阱分析，以及自动驾驶感知。<sup>[13](https://detectron2.com/key-use-cases-of-detectron2-in-real-world-projects/)</sup><sup> • </sup><sup>[14](https://detectron2.org/real-world-applications-and-use-cases-of-detectron2/)</sup>

## 与 MMDetection 的比较

MMDetection 是 Open MMLab 的目标检测工具箱，也是 Detectron2 最常被拿来比较的替代品；两者都是覆盖目标检测与实例分割的 PyTorch 工具箱，MMDetection 的文档在同一硬件上将其 Mask R-CNN 配置与 detectron2 的 `mask_rcnn_R_50_FPN_noaug_1x.yaml` 进行基准对比，以求公平比较。<sup>[15](https://mmdetection.readthedocs.io/en/v2.12.0/model_zoo.html)</sup> 在上文引用的 detectron2 基准测试中，同一模型下 mmdetection 达到每秒 53 张图像，而 detectron2 为 62 张。<sup>[2](https://github.com/facebookresearch/detectron2/blob/main/docs/notes/benchmarks.md/)</sup>

## References

1. [Detectron2 GitHub repository, facebookresearch](https://github.com/facebookresearch/detectron2)
2. [detectron2 benchmarks.md, facebookresearch](https://github.com/facebookresearch/detectron2/blob/main/docs/notes/benchmarks.md/)
3. [detectron2 INSTALL.md, facebookresearch](https://github.com/facebookresearch/detectron2/blob/main/INSTALL.md)
4. [Deployment, detectron2 0.6 documentation](https://detectron2.readthedocs.io/en/latest/tutorials/deployment.html)
5. [Detectron GitHub repository, facebookresearch](https://github.com/facebookresearch/Detectron)
6. [Use Models, detectron2 0.6 documentation](https://detectron2.readthedocs.io/en/latest/tutorials/models.html)
7. [University of Stuttgart thesis subsection on detectron2 meta architectures](https://elib.uni-stuttgart.de/server/api/core/bitstreams/910256d5-529f-4289-91e5-91836bf13b19/content)
8. [Dataloader tutorial, detectron2 docs, facebookresearch](https://github.com/facebookresearch/detectron2/blob/main/docs/tutorials/data_loading.md)
9. [Training tutorial, detectron2 docs, facebookresearch](https://github.com/facebookresearch/detectron2/blob/main/docs/tutorials/training.md)
10. [Releases, facebookresearch/detectron2](https://github.com/facebookresearch/detectron2/releases)
11. [Vung Pham, Chau Pham, Tommy Dang (2020). Road Damage Detection and Classification with Detectron2 and Faster R-CNN. arXiv:2010.15021](https://arxiv.org/abs/2010.15021)
12. [Deployment and Analysis of Instance Segmentation Algorithm for In-field Grade Estimation of Sweetpotatoes. arXiv:2308.08534](https://arxiv.org/abs/2308.08534)
13. [Key Use Cases of Detectron2 in Real-World Projects](https://detectron2.com/key-use-cases-of-detectron2-in-real-world-projects/)
14. [Real-World Applications and Use Cases of Detectron2](https://detectron2.org/real-world-applications-and-use-cases-of-detectron2/)
15. [Benchmark and Model Zoo, MMDetection 2.12.0 documentation](https://mmdetection.readthedocs.io/en/v2.12.0/model_zoo.html)

---
*Initially written Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Language and vision AI › Computer vision › Vision datasets, software, and community › CV software and toolkits

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/detectron2

English original: https://www.edgepedia.org/detectron2
