├── docs ├── .gitignore ├── tutorials │ ├── install.md │ ├── getting_started.md │ ├── README.md │ ├── index.rst │ ├── evaluation.md │ ├── write-models.md │ ├── training.md │ ├── configs.md │ └── extend.md ├── notes │ ├── contributing.md │ ├── index.rst │ └── changelog.md ├── modules │ ├── export.rst │ ├── layers.rst │ ├── solver.rst │ ├── model_zoo.rst │ ├── checkpoint.rst │ ├── evaluation.rst │ ├── structures.rst │ ├── index.rst │ ├── config.rst │ ├── engine.rst │ ├── data.rst │ ├── utils.rst │ └── modeling.rst ├── index.rst ├── requirements.txt ├── README.md └── Makefile ├── .github ├── ISSUE_TEMPLATE │ ├── config.yml │ ├── questions-help-support.md │ ├── feature-request.md │ └── unexpected-problems-bugs.md ├── ISSUE_TEMPLATE.md ├── CODE_OF_CONDUCT.md ├── pull_request_template.md └── CONTRIBUTING.md ├── projects ├── EQL │ ├── eql │ │ ├── __init__.py │ │ └── cfg.py │ └── configs │ │ ├── eql_mask_rcnn_R_50_FPN_1x.yaml │ │ ├── eql_resampling_mask_rcnn_R_50_FPN_1x.yaml │ │ └── Base-EQL-RCNN-FPN.yaml ├── TensorMask │ ├── tests │ │ ├── __init__.py │ │ └── test_swap_align2nat.py │ ├── configs │ │ ├── tensormask_R_50_FPN_1x.yaml │ │ ├── tensormask_R_50_FPN_6x.yaml │ │ └── Base-TensorMask.yaml │ ├── tensormask │ │ ├── __init__.py │ │ ├── layers │ │ │ ├── __init__.py │ │ │ ├── csrc │ │ │ │ ├── vision.cpp │ │ │ │ └── SwapAlign2Nat │ │ │ │ │ └── SwapAlign2Nat.h │ │ │ └── swap_align2nat.py │ │ └── config.py │ ├── train_net.py │ └── setup.py ├── DensePose │ ├── doc │ │ ├── images │ │ │ ├── res_bbox_dp_u.jpg │ │ │ ├── res_bbox_dp_v.jpg │ │ │ ├── vis_bbox_dp_i.jpg │ │ │ ├── vis_bbox_dp_u.jpg │ │ │ ├── vis_bbox_dp_v.jpg │ │ │ ├── res_bbox_dp_segm.jpg │ │ │ ├── vis_bbox_dp_pts.jpg │ │ │ ├── vis_bbox_dp_segm.jpg │ │ │ └── res_bbox_dp_contour.jpg │ │ └── GETTING_STARTED.md │ ├── configs │ │ ├── densepose_rcnn_R_101_FPN_s1x.yaml │ │ ├── densepose_rcnn_R_50_FPN_s1x.yaml │ │ ├── densepose_rcnn_R_50_FPN_DL_s1x.yaml │ │ ├── densepose_rcnn_R_101_FPN_DL_s1x.yaml │ │ ├── quick_schedules │ │ │ ├── densepose_rcnn_R_50_FPN_instant_test.yaml │ │ │ ├── densepose_rcnn_R_50_FPN_DL_instant_test.yaml │ │ │ ├── densepose_rcnn_R_50_FPN_inference_acc_test.yaml │ │ │ └── densepose_rcnn_R_50_FPN_training_acc_test.yaml │ │ ├── densepose_rcnn_R_101_FPN_s1x_legacy.yaml │ │ ├── densepose_rcnn_R_50_FPN_s1x_legacy.yaml │ │ └── Base-DensePose-RCNN-FPN.yaml │ ├── dev │ │ ├── README.md │ │ ├── run_instant_tests.sh │ │ └── run_inference_tests.sh │ ├── densepose │ │ ├── utils │ │ │ └── logger.py │ │ ├── __init__.py │ │ ├── vis │ │ │ └── bounding_box.py │ │ ├── dataset.py │ │ └── config.py │ └── README.md ├── TridentNet │ ├── configs │ │ ├── tridentnet_fast_R_50_C4_1x.yaml │ │ ├── tridentnet_fast_R_101_C4_3x.yaml │ │ ├── tridentnet_fast_R_50_C4_3x.yaml │ │ └── Base-TridentNet-Fast-C4.yaml │ ├── tridentnet │ │ ├── __init__.py │ │ ├── config.py │ │ └── trident_rpn.py │ └── train_net.py ├── PointRend │ ├── point_rend │ │ ├── __init__.py │ │ └── config.py │ └── configs │ │ └── InstanceSegmentation │ │ ├── pointrend_rcnn_R_50_FPN_1x_coco.yaml │ │ ├── pointrend_rcnn_R_50_FPN_3x_coco.yaml │ │ ├── Base-PointRend-RCNN-FPN.yaml │ │ └── pointrend_rcnn_R_50_FPN_1x_cityscapes.yaml └── README.md ├── tests ├── __init__.py ├── README.md ├── test_sampler.py ├── test_model_zoo.py ├── test_config.py ├── test_checkpoint.py ├── test_model_analysis.py └── test_box2box_transform.py ├── detectron2 ├── utils │ ├── __init__.py │ ├── registry.py │ ├── README.md │ └── serialize.py ├── export │ ├── __init__.py │ └── README.md ├── layers │ ├── csrc │ │ ├── README.md │ │ ├── cuda_version.cu │ │ ├── box_iou_rotated │ │ │ ├── box_iou_rotated.h │ │ │ └── box_iou_rotated_cpu.cpp │ │ └── nms_rotated │ │ │ ├── nms_rotated.h │ │ │ └── nms_rotated_cpu.cpp │ ├── __init__.py │ ├── shape_spec.py │ └── rotated_boxes.py ├── modeling │ ├── proposal_generator │ │ ├── __init__.py │ │ ├── build.py │ │ └── proposal_utils.py │ ├── backbone │ │ ├── __init__.py │ │ ├── build.py │ │ └── backbone.py │ ├── meta_arch │ │ ├── __init__.py │ │ └── build.py │ ├── roi_heads │ │ └── __init__.py │ ├── __init__.py │ └── sampling.py ├── data │ ├── transforms │ │ └── __init__.py │ ├── datasets │ │ ├── README.md │ │ └── __init__.py │ ├── samplers │ │ ├── __init__.py │ │ └── grouped_batch_sampler.py │ └── __init__.py ├── solver │ └── __init__.py ├── __init__.py ├── config │ └── __init__.py ├── checkpoint │ ├── __init__.py │ └── detection_checkpoint.py ├── engine │ └── __init__.py ├── model_zoo │ └── __init__.py ├── structures │ └── __init__.py └── evaluation │ ├── __init__.py │ └── testing.py ├── configs ├── quick_schedules │ ├── README.md │ ├── rpn_R_50_FPN_inference_acc_test.yaml │ ├── fast_rcnn_R_50_FPN_inference_acc_test.yaml │ ├── retinanet_R_50_FPN_inference_acc_test.yaml │ ├── mask_rcnn_R_50_C4_inference_acc_test.yaml │ ├── mask_rcnn_R_50_DC5_inference_acc_test.yaml │ ├── keypoint_rcnn_R_50_FPN_inference_acc_test.yaml │ ├── rpn_R_50_FPN_instant_test.yaml │ ├── mask_rcnn_R_50_C4_instant_test.yaml │ ├── mask_rcnn_R_50_FPN_instant_test.yaml │ ├── retinanet_R_50_FPN_instant_test.yaml │ ├── keypoint_rcnn_R_50_FPN_instant_test.yaml │ ├── semantic_R_50_FPN_inference_acc_test.yaml │ ├── panoptic_fpn_R_50_inference_acc_test.yaml │ ├── panoptic_fpn_R_50_instant_test.yaml │ ├── mask_rcnn_R_50_FPN_inference_acc_test.yaml │ ├── semantic_R_50_FPN_instant_test.yaml │ ├── mask_rcnn_R_50_FPN_training_acc_test.yaml │ ├── fast_rcnn_R_50_FPN_instant_test.yaml │ ├── semantic_R_50_FPN_training_acc_test.yaml │ ├── mask_rcnn_R_50_C4_training_acc_test.yaml │ ├── panoptic_fpn_R_50_training_acc_test.yaml │ ├── keypoint_rcnn_R_50_FPN_training_acc_test.yaml │ └── keypoint_rcnn_R_50_FPN_normalized_training_acc_test.yaml ├── COCO-Detection │ ├── retinanet_R_50_FPN_1x.yaml │ ├── faster_rcnn_R_50_C4_1x.yaml │ ├── faster_rcnn_R_50_FPN_1x.yaml │ ├── faster_rcnn_R_50_DC5_1x.yaml │ ├── retinanet_R_50_FPN_3x.yaml │ ├── retinanet_R_101_FPN_3x.yaml │ ├── faster_rcnn_R_50_C4_3x.yaml │ ├── faster_rcnn_R_101_C4_3x.yaml │ ├── faster_rcnn_R_101_FPN_3x.yaml │ ├── faster_rcnn_R_50_FPN_3x.yaml │ ├── faster_rcnn_R_50_DC5_3x.yaml │ ├── faster_rcnn_R_101_DC5_3x.yaml │ ├── rpn_R_50_FPN_1x.yaml │ ├── rpn_R_50_C4_1x.yaml │ ├── faster_rcnn_X_101_32x8d_FPN_3x.yaml │ └── fast_rcnn_R_50_FPN_1x.yaml ├── COCO-Keypoints │ ├── keypoint_rcnn_R_50_FPN_1x.yaml │ ├── keypoint_rcnn_R_50_FPN_3x.yaml │ ├── keypoint_rcnn_R_101_FPN_3x.yaml │ ├── keypoint_rcnn_X_101_32x8d_FPN_3x.yaml │ └── Base-Keypoint-RCNN-FPN.yaml ├── COCO-PanopticSegmentation │ ├── panoptic_fpn_R_50_1x.yaml │ ├── panoptic_fpn_R_50_3x.yaml │ ├── panoptic_fpn_R_101_3x.yaml │ └── Base-Panoptic-FPN.yaml ├── COCO-InstanceSegmentation │ ├── mask_rcnn_R_50_C4_1x.yaml │ ├── mask_rcnn_R_50_FPN_1x.yaml │ ├── mask_rcnn_R_50_DC5_1x.yaml │ ├── mask_rcnn_R_50_C4_3x.yaml │ ├── mask_rcnn_R_50_FPN_3x.yaml │ ├── mask_rcnn_R_101_C4_3x.yaml │ ├── mask_rcnn_R_101_FPN_3x.yaml │ ├── mask_rcnn_R_101_DC5_3x.yaml │ ├── mask_rcnn_R_50_DC5_3x.yaml │ └── mask_rcnn_X_101_32x8d_FPN_3x.yaml ├── Misc │ ├── mask_rcnn_R_50_FPN_1x_dconv_c3-c5.yaml │ ├── mask_rcnn_R_50_FPN_1x_cls_agnostic.yaml │ ├── cascade_mask_rcnn_R_50_FPN_1x.yaml │ ├── mask_rcnn_R_50_FPN_3x_dconv_c3-c5.yaml │ ├── semantic_R_50_FPN_1x.yaml │ ├── cascade_mask_rcnn_R_50_FPN_3x.yaml │ ├── mask_rcnn_R_50_FPN_3x_gn.yaml │ ├── mask_rcnn_R_50_FPN_3x_syncbn.yaml │ ├── scratch_mask_rcnn_R_50_FPN_3x_gn.yaml │ ├── panoptic_fpn_R_101_dconv_cascade_gn_3x.yaml │ └── cascade_mask_rcnn_X_152_32x8d_FPN_IN5k_gn_dconv.yaml ├── Base-RCNN-C4.yaml ├── PascalVOC-Detection │ ├── faster_rcnn_R_50_C4.yaml │ └── faster_rcnn_R_50_FPN.yaml ├── Detectron1-Comparisons │ ├── faster_rcnn_R_50_FPN_noaug_1x.yaml │ ├── mask_rcnn_R_50_FPN_noaug_1x.yaml │ └── keypoint_rcnn_R_50_FPN_1x.yaml ├── LVIS-InstanceSegmentation │ ├── mask_rcnn_R_50_FPN_1x.yaml │ ├── mask_rcnn_R_101_FPN_1x.yaml │ └── mask_rcnn_X_101_32x8d_FPN_1x.yaml ├── Base-RetinaNet.yaml ├── Base-RCNN-DilatedC5.yaml ├── Cityscapes │ └── mask_rcnn_R_50_FPN.yaml └── Base-RCNN-FPN.yaml ├── dev ├── README.md ├── packaging │ ├── README.md │ ├── gen_wheel_index.sh │ ├── build_wheel.sh │ ├── build_all_wheels.sh │ └── pkg_helpers.bash ├── run_instant_tests.sh ├── linter.sh ├── run_inference_tests.sh └── parse_results.sh ├── .flake8 ├── demo └── README.md ├── docker ├── docker-compose.yml ├── Dockerfile-circleci ├── README.md └── Dockerfile ├── datasets ├── prepare_for_tests.sh └── README.md ├── .gitignore ├── setup.cfg ├── tools ├── README.md └── caffe2_converter.py └── .clang-format /docs/.gitignore: -------------------------------------------------------------------------------- 1 | _build 2 | -------------------------------------------------------------------------------- /docs/tutorials/install.md: -------------------------------------------------------------------------------- 1 | ../../INSTALL.md -------------------------------------------------------------------------------- /docs/notes/contributing.md: -------------------------------------------------------------------------------- 1 | ../../.github/CONTRIBUTING.md -------------------------------------------------------------------------------- /docs/tutorials/getting_started.md: -------------------------------------------------------------------------------- 1 | ../../GETTING_STARTED.md -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | -------------------------------------------------------------------------------- /projects/EQL/eql/__init__.py: -------------------------------------------------------------------------------- 1 | from .roi_heads import * 2 | from .cfg import add_eql_config -------------------------------------------------------------------------------- /tests/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | -------------------------------------------------------------------------------- /detectron2/utils/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | -------------------------------------------------------------------------------- /projects/TensorMask/tests/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | -------------------------------------------------------------------------------- /configs/quick_schedules/README.md: -------------------------------------------------------------------------------- 1 | These are quick configs for performance or accuracy regression tracking purposes. 2 | -------------------------------------------------------------------------------- /projects/DensePose/doc/images/res_bbox_dp_u.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tztztztztz/eql.detectron2/HEAD/projects/DensePose/doc/images/res_bbox_dp_u.jpg -------------------------------------------------------------------------------- /projects/DensePose/doc/images/res_bbox_dp_v.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tztztztztz/eql.detectron2/HEAD/projects/DensePose/doc/images/res_bbox_dp_v.jpg -------------------------------------------------------------------------------- /projects/DensePose/doc/images/vis_bbox_dp_i.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tztztztztz/eql.detectron2/HEAD/projects/DensePose/doc/images/vis_bbox_dp_i.jpg -------------------------------------------------------------------------------- /projects/DensePose/doc/images/vis_bbox_dp_u.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tztztztztz/eql.detectron2/HEAD/projects/DensePose/doc/images/vis_bbox_dp_u.jpg -------------------------------------------------------------------------------- /projects/DensePose/doc/images/vis_bbox_dp_v.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tztztztztz/eql.detectron2/HEAD/projects/DensePose/doc/images/vis_bbox_dp_v.jpg -------------------------------------------------------------------------------- /detectron2/export/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | 3 | from .api import * 4 | 5 | __all__ = [k for k in globals().keys() if not k.startswith("_")] 6 | -------------------------------------------------------------------------------- /projects/DensePose/doc/images/res_bbox_dp_segm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tztztztztz/eql.detectron2/HEAD/projects/DensePose/doc/images/res_bbox_dp_segm.jpg -------------------------------------------------------------------------------- /projects/DensePose/doc/images/vis_bbox_dp_pts.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tztztztztz/eql.detectron2/HEAD/projects/DensePose/doc/images/vis_bbox_dp_pts.jpg -------------------------------------------------------------------------------- /projects/DensePose/doc/images/vis_bbox_dp_segm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tztztztztz/eql.detectron2/HEAD/projects/DensePose/doc/images/vis_bbox_dp_segm.jpg -------------------------------------------------------------------------------- /projects/DensePose/doc/images/res_bbox_dp_contour.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/tztztztztz/eql.detectron2/HEAD/projects/DensePose/doc/images/res_bbox_dp_contour.jpg -------------------------------------------------------------------------------- /detectron2/layers/csrc/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | To add a new Op: 4 | 5 | 1. Create a new directory 6 | 2. Implement new ops there 7 | 3. Delcare its Python interface in `vision.cpp`. 8 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 2 | Please select an issue template from 3 | https://github.com/facebookresearch/detectron2/issues/new/choose . 4 | 5 | Otherwise your issue will be closed. 6 | -------------------------------------------------------------------------------- /configs/COCO-Detection/retinanet_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RetinaNet.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | -------------------------------------------------------------------------------- /docs/modules/export.rst: -------------------------------------------------------------------------------- 1 | detectron2.export package 2 | ========================= 3 | 4 | .. automodule:: detectron2.export 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/modules/layers.rst: -------------------------------------------------------------------------------- 1 | detectron2.layers package 2 | ========================= 3 | 4 | .. automodule:: detectron2.layers 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/modules/solver.rst: -------------------------------------------------------------------------------- 1 | detectron2.solver package 2 | ========================= 3 | 4 | .. automodule:: detectron2.solver 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /projects/TensorMask/configs/tensormask_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-TensorMask.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | -------------------------------------------------------------------------------- /projects/TensorMask/tensormask/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .config import add_tensormask_config 3 | from .arch import TensorMask 4 | -------------------------------------------------------------------------------- /configs/COCO-Keypoints/keypoint_rcnn_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-Keypoint-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | -------------------------------------------------------------------------------- /configs/COCO-PanopticSegmentation/panoptic_fpn_R_50_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-Panoptic-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | -------------------------------------------------------------------------------- /docs/modules/model_zoo.rst: -------------------------------------------------------------------------------- 1 | detectron2.model_zoo package 2 | ============================ 3 | 4 | .. automodule:: detectron2.model_zoo 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/notes/index.rst: -------------------------------------------------------------------------------- 1 | Notes 2 | ====================================== 3 | 4 | .. toctree:: 5 | :maxdepth: 2 6 | 7 | benchmarks 8 | compatibility 9 | contributing 10 | changelog 11 | -------------------------------------------------------------------------------- /configs/COCO-Detection/faster_rcnn_R_50_C4_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | -------------------------------------------------------------------------------- /docs/modules/checkpoint.rst: -------------------------------------------------------------------------------- 1 | detectron2.checkpoint package 2 | ============================= 3 | 4 | .. automodule:: detectron2.checkpoint 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/modules/evaluation.rst: -------------------------------------------------------------------------------- 1 | detectron2.evaluation package 2 | ============================= 3 | 4 | .. automodule:: detectron2.evaluation 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /docs/modules/structures.rst: -------------------------------------------------------------------------------- 1 | detectron2.structures package 2 | ============================= 3 | 4 | .. automodule:: detectron2.structures 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /configs/COCO-Detection/faster_rcnn_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | -------------------------------------------------------------------------------- /detectron2/utils/registry.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | 3 | # Keep this module for backward compatibility. 4 | from fvcore.common.registry import Registry # noqa 5 | -------------------------------------------------------------------------------- /configs/COCO-Detection/faster_rcnn_R_50_DC5_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-DilatedC5.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | -------------------------------------------------------------------------------- /configs/COCO-InstanceSegmentation/mask_rcnn_R_50_C4_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | -------------------------------------------------------------------------------- /configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | -------------------------------------------------------------------------------- /detectron2/utils/README.md: -------------------------------------------------------------------------------- 1 | # Utility functions 2 | 3 | This folder contain utility functions that are not used in the 4 | core library, but are useful for building models or training 5 | code using the config system. 6 | -------------------------------------------------------------------------------- /tests/README.md: -------------------------------------------------------------------------------- 1 | ## Unit Tests 2 | 3 | To run the unittests, do: 4 | ``` 5 | python -m unittest discover -v -s tests 6 | ``` 7 | 8 | There are also end-to-end inference & training tests, in [dev/run_*_tests.sh](../dev). 9 | -------------------------------------------------------------------------------- /configs/COCO-InstanceSegmentation/mask_rcnn_R_50_DC5_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-DilatedC5.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | -------------------------------------------------------------------------------- /projects/TridentNet/configs/tridentnet_fast_R_50_C4_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-TridentNet-Fast-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | -------------------------------------------------------------------------------- /configs/COCO-Detection/retinanet_R_50_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RetinaNet.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | SOLVER: 7 | STEPS: (210000, 250000) 8 | MAX_ITER: 270000 9 | -------------------------------------------------------------------------------- /docs/tutorials/README.md: -------------------------------------------------------------------------------- 1 | # Read the docs: 2 | 3 | The latest documentation built from this directory is available at [detectron2.readthedocs.io](https://detectron2.readthedocs.io/). 4 | Documents in this directory are not meant to be read on github. 5 | -------------------------------------------------------------------------------- /projects/PointRend/point_rend/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .config import add_pointrend_config 3 | from .coarse_mask_head import CoarseMaskHead 4 | from .roi_heads import PointRendROIHeads 5 | -------------------------------------------------------------------------------- /configs/COCO-Detection/retinanet_R_101_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RetinaNet.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | RESNETS: 5 | DEPTH: 101 6 | SOLVER: 7 | STEPS: (210000, 250000) 8 | MAX_ITER: 270000 9 | -------------------------------------------------------------------------------- /detectron2/modeling/proposal_generator/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .build import PROPOSAL_GENERATOR_REGISTRY, build_proposal_generator 3 | from .rpn import RPN_HEAD_REGISTRY, build_rpn_head, RPN 4 | -------------------------------------------------------------------------------- /projects/TensorMask/tensormask/layers/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .swap_align2nat import SwapAlign2Nat, swap_align2nat 3 | 4 | __all__ = [k for k in globals().keys() if not k.startswith("_")] 5 | -------------------------------------------------------------------------------- /configs/COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-Keypoint-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | SOLVER: 7 | STEPS: (210000, 250000) 8 | MAX_ITER: 270000 9 | -------------------------------------------------------------------------------- /configs/COCO-PanopticSegmentation/panoptic_fpn_R_50_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-Panoptic-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | SOLVER: 7 | STEPS: (210000, 250000) 8 | MAX_ITER: 270000 9 | -------------------------------------------------------------------------------- /configs/COCO-Keypoints/keypoint_rcnn_R_101_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-Keypoint-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | RESNETS: 5 | DEPTH: 101 6 | SOLVER: 7 | STEPS: (210000, 250000) 8 | MAX_ITER: 270000 9 | -------------------------------------------------------------------------------- /configs/COCO-PanopticSegmentation/panoptic_fpn_R_101_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-Panoptic-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | RESNETS: 5 | DEPTH: 101 6 | SOLVER: 7 | STEPS: (210000, 250000) 8 | MAX_ITER: 270000 9 | -------------------------------------------------------------------------------- /projects/DensePose/configs/densepose_rcnn_R_101_FPN_s1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | RESNETS: 5 | DEPTH: 101 6 | SOLVER: 7 | MAX_ITER: 130000 8 | STEPS: (100000, 120000) 9 | -------------------------------------------------------------------------------- /projects/DensePose/configs/densepose_rcnn_R_50_FPN_s1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | SOLVER: 7 | MAX_ITER: 130000 8 | STEPS: (100000, 120000) 9 | -------------------------------------------------------------------------------- /configs/COCO-Detection/faster_rcnn_R_50_C4_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /.github/CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- 1 | # Code of Conduct 2 | 3 | Facebook has adopted a Code of Conduct that we expect project participants to adhere to. 4 | Please read the [full text](https://code.fb.com/codeofconduct/) 5 | so that you can understand what actions will and will not be tolerated. 6 | -------------------------------------------------------------------------------- /configs/COCO-Detection/faster_rcnn_R_101_C4_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 101 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /configs/COCO-Detection/faster_rcnn_R_101_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 101 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /configs/COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /detectron2/layers/csrc/cuda_version.cu: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | #include 4 | 5 | namespace detectron2 { 6 | int get_cudart_version() { 7 | return CUDART_VERSION; 8 | } 9 | } // namespace detectron2 10 | -------------------------------------------------------------------------------- /configs/COCO-Detection/faster_rcnn_R_50_DC5_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-DilatedC5.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /configs/COCO-InstanceSegmentation/mask_rcnn_R_50_C4_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /detectron2/data/transforms/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .transform import * 3 | from fvcore.transforms.transform import * 4 | from .transform_gen import * 5 | 6 | __all__ = [k for k in globals().keys() if not k.startswith("_")] 7 | -------------------------------------------------------------------------------- /configs/COCO-Detection/faster_rcnn_R_101_DC5_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-DilatedC5.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 101 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /configs/COCO-InstanceSegmentation/mask_rcnn_R_101_C4_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 101 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /configs/COCO-InstanceSegmentation/mask_rcnn_R_101_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 101 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /detectron2/solver/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .build import build_lr_scheduler, build_optimizer 3 | from .lr_scheduler import WarmupCosineLR, WarmupMultiStepLR 4 | 5 | __all__ = [k for k in globals().keys() if not k.startswith("_")] 6 | -------------------------------------------------------------------------------- /configs/COCO-Detection/rpn_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | META_ARCHITECTURE: "ProposalNetwork" 4 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | RPN: 9 | POST_NMS_TOPK_TEST: 2000 10 | -------------------------------------------------------------------------------- /configs/COCO-InstanceSegmentation/mask_rcnn_R_101_DC5_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-DilatedC5.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 101 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /configs/COCO-InstanceSegmentation/mask_rcnn_R_50_DC5_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-DilatedC5.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /docs/modules/index.rst: -------------------------------------------------------------------------------- 1 | API Documentation 2 | ================== 3 | 4 | .. toctree:: 5 | 6 | checkpoint 7 | config 8 | data 9 | engine 10 | evaluation 11 | layers 12 | model_zoo 13 | modeling 14 | solver 15 | structures 16 | utils 17 | export 18 | -------------------------------------------------------------------------------- /projects/TridentNet/configs/tridentnet_fast_R_101_C4_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-TridentNet-Fast-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 101 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /projects/TridentNet/configs/tridentnet_fast_R_50_C4_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-TridentNet-Fast-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | -------------------------------------------------------------------------------- /dev/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Some scripts for developers to use, include: 3 | 4 | - `linter.sh`: lint the codebase before commit 5 | - `run_{inference,instant}_tests.sh`: run inference/training for a few iterations. 6 | Note that these tests require 2 GPUs. 7 | - `parse_results.sh`: parse results from a log file. 8 | -------------------------------------------------------------------------------- /configs/Misc/mask_rcnn_R_50_FPN_1x_dconv_c3-c5.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | DEFORM_ON_PER_STAGE: [False, True, True, True] # on Res3,Res4,Res5 8 | DEFORM_MODULATED: False 9 | -------------------------------------------------------------------------------- /projects/EQL/eql/cfg.py: -------------------------------------------------------------------------------- 1 | def add_eql_config(cfg): 2 | """ 3 | Add config for EQL. 4 | """ 5 | cfg.MODEL.ROI_HEADS.LAMBDA = 0.00177 6 | cfg.MODEL.ROI_HEADS.PRIOR_PROB = 0.001 7 | 8 | # legacy cfg key (make model compatible with previous ckpt) 9 | cfg.MODEL.ROI_HEADS.FREQ_INFO = "" 10 | -------------------------------------------------------------------------------- /.flake8: -------------------------------------------------------------------------------- 1 | # This is an example .flake8 config, used when developing *Black* itself. 2 | # Keep in sync with setup.cfg which is used for source packages. 3 | 4 | [flake8] 5 | ignore = W503, E203, E221, C901, C408 6 | max-line-length = 100 7 | max-complexity = 18 8 | select = B,C,E,F,W,T4,B9 9 | exclude = build,__init__.py 10 | -------------------------------------------------------------------------------- /configs/COCO-Detection/rpn_R_50_C4_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | META_ARCHITECTURE: "ProposalNetwork" 4 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | RPN: 9 | PRE_NMS_TOPK_TEST: 12000 10 | POST_NMS_TOPK_TEST: 2000 11 | -------------------------------------------------------------------------------- /configs/COCO-PanopticSegmentation/Base-Panoptic-FPN.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | META_ARCHITECTURE: "PanopticFPN" 4 | MASK_ON: True 5 | SEM_SEG_HEAD: 6 | LOSS_WEIGHT: 0.5 7 | DATASETS: 8 | TRAIN: ("coco_2017_train_panoptic_separated",) 9 | TEST: ("coco_2017_val_panoptic_separated",) 10 | -------------------------------------------------------------------------------- /configs/Misc/mask_rcnn_R_50_FPN_1x_cls_agnostic.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_BOX_HEAD: 8 | CLS_AGNOSTIC_BBOX_REG: True 9 | ROI_MASK_HEAD: 10 | CLS_AGNOSTIC_MASK: True 11 | -------------------------------------------------------------------------------- /docs/tutorials/index.rst: -------------------------------------------------------------------------------- 1 | Tutorials 2 | ====================================== 3 | 4 | .. toctree:: 5 | :maxdepth: 2 6 | 7 | install 8 | getting_started 9 | extend 10 | datasets 11 | data_loading 12 | models 13 | write-models 14 | training 15 | evaluation 16 | configs 17 | deployment 18 | -------------------------------------------------------------------------------- /configs/quick_schedules/rpn_R_50_FPN_inference_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-Detection/rpn_R_50_FPN_1x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://COCO-Detection/rpn_R_50_FPN_1x/137258492/model_final_02ce48.pkl" 4 | DATASETS: 5 | TEST: ("coco_2017_val_100",) 6 | TEST: 7 | EXPECTED_RESULTS: [["box_proposals", "AR@1000", 58.16, 0.02]] 8 | -------------------------------------------------------------------------------- /configs/quick_schedules/fast_rcnn_R_50_FPN_inference_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-Detection/fast_rcnn_R_50_FPN_1x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://COCO-Detection/fast_rcnn_R_50_FPN_1x/137635226/model_final_e5f7ce.pkl" 4 | DATASETS: 5 | TEST: ("coco_2017_val_100",) 6 | TEST: 7 | EXPECTED_RESULTS: [["bbox", "AP", 45.70, 0.02]] 8 | -------------------------------------------------------------------------------- /configs/quick_schedules/retinanet_R_50_FPN_inference_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-Detection/retinanet_R_50_FPN_3x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://COCO-Detection/retinanet_R_50_FPN_3x/137849486/model_final_4cafe0.pkl" 4 | DATASETS: 5 | TEST: ("coco_2017_val_100",) 6 | TEST: 7 | EXPECTED_RESULTS: [["bbox", "AP", 44.36, 0.02]] 8 | -------------------------------------------------------------------------------- /projects/DensePose/configs/densepose_rcnn_R_50_FPN_DL_s1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | ROI_DENSEPOSE_HEAD: 7 | NAME: "DensePoseDeepLabHead" 8 | SOLVER: 9 | MAX_ITER: 130000 10 | STEPS: (100000, 120000) 11 | -------------------------------------------------------------------------------- /projects/DensePose/dev/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Some scripts for developers to use, include: 3 | 4 | - `run_instant_tests.sh`: run training for a few iterations. 5 | - `run_inference_tests.sh`: run inference on a small dataset. 6 | - `../../dev/linter.sh`: lint the codebase before commit 7 | - `../../dev/parse_results.sh`: parse results from log file. 8 | -------------------------------------------------------------------------------- /detectron2/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | from .utils.env import setup_environment 4 | 5 | setup_environment() 6 | 7 | 8 | # This line will be programatically read/write by setup.py. 9 | # Leave them at the bottom of this file and don't touch them. 10 | __version__ = "0.1.1" 11 | -------------------------------------------------------------------------------- /projects/DensePose/configs/densepose_rcnn_R_101_FPN_DL_s1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | RESNETS: 5 | DEPTH: 101 6 | ROI_DENSEPOSE_HEAD: 7 | NAME: "DensePoseDeepLabHead" 8 | SOLVER: 9 | MAX_ITER: 130000 10 | STEPS: (100000, 120000) 11 | -------------------------------------------------------------------------------- /projects/TensorMask/configs/tensormask_R_50_FPN_6x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-TensorMask.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | SOLVER: 7 | STEPS: (480000, 520000) 8 | MAX_ITER: 540000 9 | INPUT: 10 | MIN_SIZE_TRAIN_SAMPLING: "range" 11 | MIN_SIZE_TRAIN: (640, 800) 12 | -------------------------------------------------------------------------------- /configs/Misc/cascade_mask_rcnn_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | NAME: CascadeROIHeads 9 | ROI_BOX_HEAD: 10 | CLS_AGNOSTIC_BBOX_REG: True 11 | RPN: 12 | POST_NMS_TOPK_TRAIN: 2000 13 | -------------------------------------------------------------------------------- /demo/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Detectron2 Demo 3 | 4 | We provide a command line tool to run a simple demo of builtin models. 5 | The usage is explained in [GETTING_STARTED.md](../GETTING_STARTED.md). 6 | 7 | See our [blog post](https://ai.facebook.com/blog/-detectron2-a-pytorch-based-modular-object-detection-library-) 8 | for a high-quality demo generated with this tool. 9 | -------------------------------------------------------------------------------- /projects/DensePose/configs/quick_schedules/densepose_rcnn_R_50_FPN_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | DATASETS: 5 | TRAIN: ("densepose_coco_2014_minival_100",) 6 | TEST: ("densepose_coco_2014_minival_100",) 7 | SOLVER: 8 | MAX_ITER: 40 9 | STEPS: (30,) 10 | -------------------------------------------------------------------------------- /configs/Misc/mask_rcnn_R_50_FPN_3x_dconv_c3-c5.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | DEFORM_ON_PER_STAGE: [False, True, True, True] # on Res3,Res4,Res5 8 | DEFORM_MODULATED: False 9 | SOLVER: 10 | STEPS: (210000, 250000) 11 | MAX_ITER: 270000 12 | -------------------------------------------------------------------------------- /configs/quick_schedules/mask_rcnn_R_50_C4_inference_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-InstanceSegmentation/mask_rcnn_R_50_C4_3x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_C4_3x/137849525/model_final_4ce675.pkl" 4 | DATASETS: 5 | TEST: ("coco_2017_val_100",) 6 | TEST: 7 | EXPECTED_RESULTS: [["bbox", "AP", 47.37, 0.02], ["segm", "AP", 40.99, 0.02]] 8 | -------------------------------------------------------------------------------- /configs/quick_schedules/mask_rcnn_R_50_DC5_inference_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-InstanceSegmentation/mask_rcnn_R_50_DC5_3x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_DC5_3x/137849551/model_final_84107b.pkl" 4 | DATASETS: 5 | TEST: ("coco_2017_val_100",) 6 | TEST: 7 | EXPECTED_RESULTS: [["bbox", "AP", 47.44, 0.02], ["segm", "AP", 42.94, 0.02]] 8 | -------------------------------------------------------------------------------- /configs/quick_schedules/keypoint_rcnn_R_50_FPN_inference_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://COCO-Keypoints/keypoint_rcnn_R_50_FPN_3x/137849621/model_final_a6e10b.pkl" 4 | DATASETS: 5 | TEST: ("keypoints_coco_2017_val_100",) 6 | TEST: 7 | EXPECTED_RESULTS: [["bbox", "AP", 52.47, 0.02], ["keypoints", "AP", 67.36, 0.02]] 8 | -------------------------------------------------------------------------------- /configs/quick_schedules/rpn_R_50_FPN_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-Detection/rpn_R_50_FPN_1x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | DATASETS: 5 | TRAIN: ("coco_2017_val_100",) 6 | TEST: ("coco_2017_val_100",) 7 | SOLVER: 8 | STEPS: (30,) 9 | MAX_ITER: 40 10 | BASE_LR: 0.005 11 | IMS_PER_BATCH: 4 12 | DATALOADER: 13 | NUM_WORKERS: 2 14 | -------------------------------------------------------------------------------- /detectron2/config/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .compat import downgrade_config, upgrade_config 3 | from .config import CfgNode, get_cfg, global_cfg, set_global_cfg 4 | 5 | __all__ = [ 6 | "CfgNode", 7 | "get_cfg", 8 | "global_cfg", 9 | "set_global_cfg", 10 | "downgrade_config", 11 | "upgrade_config", 12 | ] 13 | -------------------------------------------------------------------------------- /projects/PointRend/configs/InstanceSegmentation/pointrend_rcnn_R_50_FPN_1x_coco.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: Base-PointRend-RCNN-FPN.yaml 2 | MODEL: 3 | WEIGHTS: detectron2://ImageNetPretrained/MSRA/R-50.pkl 4 | MASK_ON: true 5 | RESNETS: 6 | DEPTH: 50 7 | # To add COCO AP evaluation against the higher-quality LVIS annotations. 8 | # DATASETS: 9 | # TEST: ("coco_2017_val", "lvis_v0.5_val_cocofied") 10 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | Thanks for your contribution! 2 | 3 | If you're sending a large PR (e.g., >50 lines), 4 | please open an issue first about the feature / bug, and indicate how you want to contribute. 5 | See more at https://detectron2.readthedocs.io/notes/contributing.html#pull-requests 6 | about how we handle PRs. 7 | 8 | Before submitting a PR, please run `dev/linter.sh` to lint the code. 9 | -------------------------------------------------------------------------------- /detectron2/modeling/backbone/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .build import build_backbone, BACKBONE_REGISTRY # noqa F401 isort:skip 3 | 4 | from .backbone import Backbone 5 | from .fpn import FPN 6 | from .resnet import ResNet, ResNetBlockBase, build_resnet_backbone, make_stage 7 | 8 | # TODO can expose more resnet blocks after careful consideration 9 | -------------------------------------------------------------------------------- /docs/modules/config.rst: -------------------------------------------------------------------------------- 1 | detectron2.config package 2 | ========================= 3 | 4 | .. automodule:: detectron2.config 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | :inherited-members: 9 | 10 | 11 | Config References 12 | ----------------- 13 | 14 | .. literalinclude:: ../../detectron2/config/defaults.py 15 | :language: python 16 | :linenos: 17 | :lines: 4- 18 | -------------------------------------------------------------------------------- /configs/Misc/semantic_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | META_ARCHITECTURE: "SemanticSegmentor" 4 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 5 | RESNETS: 6 | DEPTH: 50 7 | DATASETS: 8 | TRAIN: ("coco_2017_train_panoptic_stuffonly",) 9 | TEST: ("coco_2017_val_panoptic_stuffonly",) 10 | INPUT: 11 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 12 | -------------------------------------------------------------------------------- /configs/quick_schedules/mask_rcnn_R_50_C4_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | DATASETS: 6 | TRAIN: ("coco_2017_val_100",) 7 | TEST: ("coco_2017_val_100",) 8 | SOLVER: 9 | BASE_LR: 0.001 10 | STEPS: (30,) 11 | MAX_ITER: 40 12 | IMS_PER_BATCH: 4 13 | DATALOADER: 14 | NUM_WORKERS: 2 15 | -------------------------------------------------------------------------------- /configs/quick_schedules/mask_rcnn_R_50_FPN_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | DATASETS: 6 | TRAIN: ("coco_2017_val_100",) 7 | TEST: ("coco_2017_val_100",) 8 | SOLVER: 9 | BASE_LR: 0.005 10 | STEPS: (30,) 11 | MAX_ITER: 40 12 | IMS_PER_BATCH: 4 13 | DATALOADER: 14 | NUM_WORKERS: 2 15 | -------------------------------------------------------------------------------- /configs/quick_schedules/retinanet_R_50_FPN_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-Detection/retinanet_R_50_FPN_1x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | DATASETS: 5 | TRAIN: ("coco_2017_val_100",) 6 | TEST: ("coco_2017_val_100",) 7 | SOLVER: 8 | BASE_LR: 0.005 9 | STEPS: (30,) 10 | MAX_ITER: 40 11 | IMS_PER_BATCH: 4 12 | DATALOADER: 13 | NUM_WORKERS: 2 14 | -------------------------------------------------------------------------------- /detectron2/data/datasets/README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ### Common Datasets 4 | 5 | The dataset implemented here do not need to load the data into the final format. 6 | It should provide the minimal data structure needed to use the dataset, so it can be very efficient. 7 | 8 | For example, for an image dataset, just provide the file names and labels, but don't read the images. 9 | Let the downstream decide how to read. 10 | -------------------------------------------------------------------------------- /detectron2/data/samplers/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .distributed_sampler import InferenceSampler, RepeatFactorTrainingSampler, TrainingSampler 3 | from .grouped_batch_sampler import GroupedBatchSampler 4 | 5 | __all__ = [ 6 | "GroupedBatchSampler", 7 | "TrainingSampler", 8 | "InferenceSampler", 9 | "RepeatFactorTrainingSampler", 10 | ] 11 | -------------------------------------------------------------------------------- /projects/TridentNet/tridentnet/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .config import add_tridentnet_config 3 | from .trident_backbone import ( 4 | TridentBottleneckBlock, 5 | build_trident_resnet_backbone, 6 | make_trident_stage, 7 | ) 8 | from .trident_rpn import TridentRPN 9 | from .trident_rcnn import TridentRes5ROIHeads, TridentStandardROIHeads 10 | -------------------------------------------------------------------------------- /configs/COCO-Keypoints/keypoint_rcnn_X_101_32x8d_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-Keypoint-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/FAIR/X-101-32x8d.pkl" 4 | PIXEL_STD: [57.375, 57.120, 58.395] 5 | RESNETS: 6 | STRIDE_IN_1X1: False # this is a C2 model 7 | NUM_GROUPS: 32 8 | WIDTH_PER_GROUP: 8 9 | DEPTH: 101 10 | SOLVER: 11 | STEPS: (210000, 250000) 12 | MAX_ITER: 270000 13 | -------------------------------------------------------------------------------- /configs/Misc/cascade_mask_rcnn_R_50_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | NAME: CascadeROIHeads 9 | ROI_BOX_HEAD: 10 | CLS_AGNOSTIC_BBOX_REG: True 11 | RPN: 12 | POST_NMS_TOPK_TRAIN: 2000 13 | SOLVER: 14 | STEPS: (210000, 250000) 15 | MAX_ITER: 270000 16 | -------------------------------------------------------------------------------- /detectron2/export/README.md: -------------------------------------------------------------------------------- 1 | 2 | This directory contains code to prepare a detectron2 model for deployment. 3 | Currently it supports exporting a detectron2 model to Caffe2 format through ONNX. 4 | 5 | Please see [documentation](https://detectron2.readthedocs.io/tutorials/deployment.html) for its usage. 6 | 7 | 8 | ### Acknowledgements 9 | 10 | Thanks to Mobile Vision team at Facebook for developing the conversion tools. 11 | -------------------------------------------------------------------------------- /projects/DensePose/configs/quick_schedules/densepose_rcnn_R_50_FPN_DL_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | ROI_DENSEPOSE_HEAD: 5 | NAME: "DensePoseDeepLabHead" 6 | DATASETS: 7 | TRAIN: ("densepose_coco_2014_minival_100",) 8 | TEST: ("densepose_coco_2014_minival_100",) 9 | SOLVER: 10 | MAX_ITER: 40 11 | STEPS: (30,) 12 | -------------------------------------------------------------------------------- /configs/COCO-Detection/faster_rcnn_X_101_32x8d_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | MASK_ON: False 4 | WEIGHTS: "detectron2://ImageNetPretrained/FAIR/X-101-32x8d.pkl" 5 | PIXEL_STD: [57.375, 57.120, 58.395] 6 | RESNETS: 7 | STRIDE_IN_1X1: False # this is a C2 model 8 | NUM_GROUPS: 32 9 | WIDTH_PER_GROUP: 8 10 | DEPTH: 101 11 | SOLVER: 12 | STEPS: (210000, 250000) 13 | MAX_ITER: 270000 14 | -------------------------------------------------------------------------------- /configs/quick_schedules/keypoint_rcnn_R_50_FPN_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | KEYPOINT_ON: True 5 | DATASETS: 6 | TRAIN: ("keypoints_coco_2017_val_100",) 7 | TEST: ("keypoints_coco_2017_val_100",) 8 | SOLVER: 9 | BASE_LR: 0.005 10 | STEPS: (30,) 11 | MAX_ITER: 40 12 | IMS_PER_BATCH: 4 13 | DATALOADER: 14 | NUM_WORKERS: 2 15 | -------------------------------------------------------------------------------- /detectron2/checkpoint/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | # File: 4 | 5 | 6 | from . import catalog as _UNUSED # register the handler 7 | from .detection_checkpoint import DetectionCheckpointer 8 | from fvcore.common.checkpoint import Checkpointer, PeriodicCheckpointer 9 | 10 | __all__ = ["Checkpointer", "PeriodicCheckpointer", "DetectionCheckpointer"] 11 | -------------------------------------------------------------------------------- /detectron2/engine/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | from .launch import * 4 | from .train_loop import * 5 | 6 | __all__ = [k for k in globals().keys() if not k.startswith("_")] 7 | 8 | 9 | # prefer to let hooks and defaults live in separate namespaces (therefore not in __all__) 10 | # but still make them available here 11 | from .hooks import * 12 | from .defaults import * 13 | -------------------------------------------------------------------------------- /configs/COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | MASK_ON: True 4 | WEIGHTS: "detectron2://ImageNetPretrained/FAIR/X-101-32x8d.pkl" 5 | PIXEL_STD: [57.375, 57.120, 58.395] 6 | RESNETS: 7 | STRIDE_IN_1X1: False # this is a C2 model 8 | NUM_GROUPS: 32 9 | WIDTH_PER_GROUP: 8 10 | DEPTH: 101 11 | SOLVER: 12 | STEPS: (210000, 250000) 13 | MAX_ITER: 270000 14 | -------------------------------------------------------------------------------- /docs/index.rst: -------------------------------------------------------------------------------- 1 | .. detectron2 documentation master file, created by 2 | sphinx-quickstart on Sat Sep 21 13:46:45 2019. 3 | You can adapt this file completely to your liking, but it should at least 4 | contain the root `toctree` directive. 5 | 6 | Welcome to detectron2's documentation! 7 | ====================================== 8 | 9 | .. toctree:: 10 | :maxdepth: 2 11 | 12 | tutorials/index 13 | notes/index 14 | modules/index 15 | -------------------------------------------------------------------------------- /projects/DensePose/densepose/utils/logger.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import logging 3 | 4 | 5 | def verbosity_to_level(verbosity): 6 | if verbosity is not None: 7 | if verbosity == 0: 8 | return logging.WARNING 9 | elif verbosity == 1: 10 | return logging.INFO 11 | elif verbosity >= 2: 12 | return logging.DEBUG 13 | return logging.WARNING 14 | -------------------------------------------------------------------------------- /configs/quick_schedules/semantic_R_50_FPN_inference_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | META_ARCHITECTURE: "SemanticSegmentor" 4 | WEIGHTS: "detectron2://semantic_R_50_FPN_1x/111802073/model_final_c18079783c55a94968edc28b7101c5f0.pkl" 5 | RESNETS: 6 | DEPTH: 50 7 | DATASETS: 8 | TEST: ("coco_2017_val_100_panoptic_stuffonly",) 9 | TEST: 10 | EXPECTED_RESULTS: [["sem_seg", "mIoU", 39.53, 0.02], ["sem_seg", "mACC", 51.50, 0.02]] 11 | -------------------------------------------------------------------------------- /configs/Base-RCNN-C4.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "GeneralizedRCNN" 3 | RPN: 4 | PRE_NMS_TOPK_TEST: 6000 5 | POST_NMS_TOPK_TEST: 1000 6 | ROI_HEADS: 7 | NAME: "Res5ROIHeads" 8 | DATASETS: 9 | TRAIN: ("coco_2017_train",) 10 | TEST: ("coco_2017_val",) 11 | SOLVER: 12 | IMS_PER_BATCH: 16 13 | BASE_LR: 0.02 14 | STEPS: (60000, 80000) 15 | MAX_ITER: 90000 16 | INPUT: 17 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 18 | VERSION: 2 19 | -------------------------------------------------------------------------------- /configs/quick_schedules/panoptic_fpn_R_50_inference_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-PanopticSegmentation/panoptic_fpn_R_50_3x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://COCO-PanopticSegmentation/panoptic_fpn_R_50_3x/139514569/model_final_c10459.pkl" 4 | DATASETS: 5 | TEST: ("coco_2017_val_100_panoptic_separated",) 6 | TEST: 7 | EXPECTED_RESULTS: [["bbox", "AP", 46.47, 0.02], ["segm", "AP", 43.39, 0.02], ["sem_seg", "mIoU", 42.55, 0.02], ["panoptic_seg", "PQ", 38.99, 0.02]] 8 | -------------------------------------------------------------------------------- /projects/PointRend/configs/InstanceSegmentation/pointrend_rcnn_R_50_FPN_3x_coco.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: Base-PointRend-RCNN-FPN.yaml 2 | MODEL: 3 | WEIGHTS: detectron2://ImageNetPretrained/MSRA/R-50.pkl 4 | MASK_ON: true 5 | RESNETS: 6 | DEPTH: 50 7 | SOLVER: 8 | STEPS: (210000, 250000) 9 | MAX_ITER: 270000 10 | # To add COCO AP evaluation against the higher-quality LVIS annotations. 11 | # DATASETS: 12 | # TEST: ("coco_2017_val", "lvis_v0.5_val_cocofied") 13 | 14 | -------------------------------------------------------------------------------- /projects/DensePose/configs/quick_schedules/densepose_rcnn_R_50_FPN_inference_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../densepose_rcnn_R_50_FPN_s1x.yaml" 2 | MODEL: 3 | WEIGHTS: "https://dl.fbaipublicfiles.com/densepose/densepose_rcnn_R_50_FPN_s1x/143908701/model_final_dd99d2.pkl" 4 | DATASETS: 5 | TRAIN: () 6 | TEST: ("densepose_coco_2014_minival_100",) 7 | TEST: 8 | EXPECTED_RESULTS: [["bbox", "AP", 56.09, 0.025], ["densepose_gps", "AP", 46.76, 0.02], ["densepose_gpsm", "AP", 51.64, 0.02]] 9 | -------------------------------------------------------------------------------- /projects/DensePose/densepose/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from . import dataset # just to register data 3 | from .config import add_densepose_config 4 | from .dataset_mapper import DatasetMapper 5 | from .densepose_head import ROI_DENSEPOSE_HEAD_REGISTRY 6 | from .evaluator import DensePoseCOCOEvaluator 7 | from .roi_head import DensePoseROIHeads 8 | from .structures import DensePoseDataRelative, DensePoseList, DensePoseTransformData 9 | -------------------------------------------------------------------------------- /detectron2/model_zoo/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | """ 3 | Model Zoo API for Detectron2: a collection of functions to create common model architectures and 4 | optionally load pre-trained weights as released in 5 | `MODEL_ZOO.md `_. 6 | """ 7 | from .model_zoo import get, get_config_file, get_checkpoint_url 8 | 9 | __all__ = ["get_checkpoint_url", "get", "get_config_file"] 10 | -------------------------------------------------------------------------------- /projects/EQL/configs/eql_mask_rcnn_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-EQL-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | NUM_CLASSES: 1230 9 | SCORE_THRESH_TEST: 0.0001 10 | INPUT: 11 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 12 | DATASETS: 13 | TRAIN: ("lvis_v0.5_train",) 14 | TEST: ("lvis_v0.5_val",) 15 | TEST: 16 | DETECTIONS_PER_IMAGE: 300 # LVIS allows up to 300 17 | -------------------------------------------------------------------------------- /detectron2/modeling/meta_arch/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | from .build import META_ARCH_REGISTRY, build_model # isort:skip 5 | 6 | from .panoptic_fpn import PanopticFPN 7 | 8 | # import all the meta_arch, so they will be registered 9 | from .rcnn import GeneralizedRCNN, ProposalNetwork 10 | from .retinanet import RetinaNet 11 | from .semantic_seg import SEM_SEG_HEADS_REGISTRY, SemanticSegmentor, build_sem_seg_head 12 | -------------------------------------------------------------------------------- /configs/Misc/mask_rcnn_R_50_FPN_3x_gn.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "catalog://ImageNetPretrained/FAIR/R-50-GN" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | NORM: "GN" 8 | STRIDE_IN_1X1: False 9 | FPN: 10 | NORM: "GN" 11 | ROI_BOX_HEAD: 12 | NAME: "FastRCNNConvFCHead" 13 | NUM_CONV: 4 14 | NUM_FC: 1 15 | NORM: "GN" 16 | ROI_MASK_HEAD: 17 | NORM: "GN" 18 | SOLVER: 19 | # 3x schedule 20 | STEPS: (210000, 250000) 21 | MAX_ITER: 270000 22 | -------------------------------------------------------------------------------- /docs/requirements.txt: -------------------------------------------------------------------------------- 1 | termcolor 2 | numpy 3 | tqdm 4 | docutils>=0.14 5 | Sphinx>=1.7 6 | recommonmark==0.4.0 7 | sphinx_rtd_theme 8 | mock 9 | matplotlib 10 | termcolor 11 | yacs 12 | tabulate 13 | cloudpickle 14 | Pillow==6.2.2 15 | future 16 | requests 17 | six 18 | git+git://github.com/facebookresearch/fvcore.git 19 | https://download.pytorch.org/whl/nightly/cpu/torch-1.3.0.dev20191010%2Bcpu-cp37-cp37m-linux_x86_64.whl 20 | https://download.pytorch.org/whl/nightly/cpu/torchvision-0.5.0.dev20191008%2Bcpu-cp37-cp37m-linux_x86_64.whl 21 | -------------------------------------------------------------------------------- /projects/DensePose/configs/densepose_rcnn_R_101_FPN_s1x_legacy.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | RESNETS: 5 | DEPTH: 101 6 | ROI_DENSEPOSE_HEAD: 7 | NUM_COARSE_SEGM_CHANNELS: 15 8 | POOLER_RESOLUTION: 14 9 | HEATMAP_SIZE: 56 10 | INDEX_WEIGHTS: 2.0 11 | PART_WEIGHTS: 0.3 12 | POINT_REGRESSION_WEIGHTS: 0.1 13 | DECODER_ON: False 14 | SOLVER: 15 | BASE_LR: 0.002 16 | MAX_ITER: 130000 17 | STEPS: (100000, 120000) 18 | -------------------------------------------------------------------------------- /projects/DensePose/configs/densepose_rcnn_R_50_FPN_s1x_legacy.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | RESNETS: 5 | DEPTH: 50 6 | ROI_DENSEPOSE_HEAD: 7 | NUM_COARSE_SEGM_CHANNELS: 15 8 | POOLER_RESOLUTION: 14 9 | HEATMAP_SIZE: 56 10 | INDEX_WEIGHTS: 2.0 11 | PART_WEIGHTS: 0.3 12 | POINT_REGRESSION_WEIGHTS: 0.1 13 | DECODER_ON: False 14 | SOLVER: 15 | BASE_LR: 0.002 16 | MAX_ITER: 130000 17 | STEPS: (100000, 120000) 18 | -------------------------------------------------------------------------------- /projects/DensePose/configs/quick_schedules/densepose_rcnn_R_50_FPN_training_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | ROI_HEADS: 5 | NUM_CLASSES: 1 6 | DATASETS: 7 | TRAIN: ("densepose_coco_2014_minival",) 8 | TEST: ("densepose_coco_2014_minival",) 9 | SOLVER: 10 | MAX_ITER: 6000 11 | STEPS: (5500, 5800) 12 | TEST: 13 | EXPECTED_RESULTS: [["bbox", "AP", 58.27, 1.0], ["densepose_gps", "AP", 42.47, 1.5], ["densepose_gpsm", "AP", 49.20, 1.5]] 14 | 15 | -------------------------------------------------------------------------------- /detectron2/data/datasets/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .cityscapes import load_cityscapes_instances 3 | from .coco import load_coco_json, load_sem_seg 4 | from .lvis import load_lvis_json, register_lvis_instances, get_lvis_instances_meta 5 | from .register_coco import register_coco_instances, register_coco_panoptic_separated 6 | from . import builtin # ensure the builtin datasets are registered 7 | 8 | 9 | __all__ = [k for k in globals().keys() if "builtin" not in k and not k.startswith("_")] 10 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | # Read the docs: 2 | 3 | The latest documentation built from this directory is available at [detectron2.readthedocs.io](https://detectron2.readthedocs.io/). 4 | Documents in this directory are not meant to be read on github. 5 | 6 | # Build the docs: 7 | 8 | 1. Install detectron2 according to [INSTALL.md](INSTALL.md). 9 | 2. Install additional libraries required to build docs: 10 | - docutils>=0.14 11 | - Sphinx>=1.7 12 | - recommonmark==0.4.0 13 | - sphinx_rtd_theme 14 | - mock 15 | 16 | 3. Run `make html` from this directory. 17 | -------------------------------------------------------------------------------- /configs/quick_schedules/panoptic_fpn_R_50_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | META_ARCHITECTURE: "PanopticFPN" 4 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 5 | MASK_ON: True 6 | RESNETS: 7 | DEPTH: 50 8 | SEM_SEG_HEAD: 9 | LOSS_WEIGHT: 0.5 10 | DATASETS: 11 | TRAIN: ("coco_2017_val_100_panoptic_separated",) 12 | TEST: ("coco_2017_val_100_panoptic_separated",) 13 | SOLVER: 14 | BASE_LR: 0.005 15 | STEPS: (30,) 16 | MAX_ITER: 40 17 | IMS_PER_BATCH: 4 18 | DATALOADER: 19 | NUM_WORKERS: 1 20 | -------------------------------------------------------------------------------- /configs/quick_schedules/mask_rcnn_R_50_FPN_inference_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl" 4 | DATASETS: 5 | TEST: ("coco_2017_val_100",) 6 | TEST: 7 | EXPECTED_RESULTS: [["bbox", "AP", 47.34, 0.02], ["segm", "AP", 42.67, 0.02]] 8 | # expected results do not use test-time augmentation. TTA results are not verified. 9 | AUG: 10 | ENABLED: True 11 | MIN_SIZES: (400, 500) # to save some time 12 | -------------------------------------------------------------------------------- /configs/quick_schedules/semantic_R_50_FPN_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | META_ARCHITECTURE: "SemanticSegmentor" 4 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 5 | RESNETS: 6 | DEPTH: 50 7 | DATASETS: 8 | TRAIN: ("coco_2017_val_100_panoptic_stuffonly",) 9 | TEST: ("coco_2017_val_100_panoptic_stuffonly",) 10 | INPUT: 11 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 12 | SOLVER: 13 | BASE_LR: 0.005 14 | STEPS: (30,) 15 | MAX_ITER: 40 16 | IMS_PER_BATCH: 4 17 | DATALOADER: 18 | NUM_WORKERS: 2 19 | -------------------------------------------------------------------------------- /configs/PascalVOC-Detection/faster_rcnn_R_50_C4.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | NUM_CLASSES: 20 9 | INPUT: 10 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 11 | MIN_SIZE_TEST: 800 12 | DATASETS: 13 | TRAIN: ('voc_2007_trainval', 'voc_2012_trainval') 14 | TEST: ('voc_2007_test',) 15 | SOLVER: 16 | STEPS: (12000, 16000) 17 | MAX_ITER: 18000 # 17.4 epochs 18 | WARMUP_ITERS: 100 19 | -------------------------------------------------------------------------------- /configs/PascalVOC-Detection/faster_rcnn_R_50_FPN.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | NUM_CLASSES: 20 9 | INPUT: 10 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 11 | MIN_SIZE_TEST: 800 12 | DATASETS: 13 | TRAIN: ('voc_2007_trainval', 'voc_2012_trainval') 14 | TEST: ('voc_2007_test',) 15 | SOLVER: 16 | STEPS: (12000, 16000) 17 | MAX_ITER: 18000 # 17.4 epochs 18 | WARMUP_ITERS: 100 19 | -------------------------------------------------------------------------------- /detectron2/structures/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .boxes import Boxes, BoxMode, pairwise_iou 3 | from .image_list import ImageList 4 | from .instances import Instances 5 | from .keypoints import Keypoints, heatmaps_to_keypoints 6 | from .masks import BitMasks, PolygonMasks, rasterize_polygons_within_box, polygons_to_bitmask 7 | from .rotated_boxes import RotatedBoxes 8 | from .rotated_boxes import pairwise_iou as pairwise_iou_rotated 9 | 10 | __all__ = [k for k in globals().keys() if not k.startswith("_")] 11 | -------------------------------------------------------------------------------- /projects/TensorMask/tensormask/layers/csrc/vision.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | 3 | #include 4 | #include "SwapAlign2Nat/SwapAlign2Nat.h" 5 | 6 | namespace tensormask { 7 | 8 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { 9 | m.def( 10 | "swap_align2nat_forward", 11 | &SwapAlign2Nat_forward, 12 | "SwapAlign2Nat_forward"); 13 | m.def( 14 | "swap_align2nat_backward", 15 | &SwapAlign2Nat_backward, 16 | "SwapAlign2Nat_backward"); 17 | } 18 | 19 | } // namespace tensormask 20 | -------------------------------------------------------------------------------- /configs/Detectron1-Comparisons/faster_rcnn_R_50_FPN_noaug_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | # Detectron1 uses smooth L1 loss with some magic beta values. 8 | # The defaults are changed to L1 loss in Detectron2. 9 | RPN: 10 | SMOOTH_L1_BETA: 0.1111 11 | ROI_BOX_HEAD: 12 | SMOOTH_L1_BETA: 1.0 13 | POOLER_SAMPLING_RATIO: 2 14 | POOLER_TYPE: "ROIAlign" 15 | INPUT: 16 | # no scale augmentation 17 | MIN_SIZE_TRAIN: (800, ) 18 | -------------------------------------------------------------------------------- /configs/Misc/mask_rcnn_R_50_FPN_3x_syncbn.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | NORM: "SyncBN" 8 | STRIDE_IN_1X1: True 9 | FPN: 10 | NORM: "SyncBN" 11 | ROI_BOX_HEAD: 12 | NAME: "FastRCNNConvFCHead" 13 | NUM_CONV: 4 14 | NUM_FC: 1 15 | NORM: "SyncBN" 16 | ROI_MASK_HEAD: 17 | NORM: "SyncBN" 18 | SOLVER: 19 | # 3x schedule 20 | STEPS: (210000, 250000) 21 | MAX_ITER: 270000 22 | TEST: 23 | PRECISE_BN: 24 | ENABLED: True 25 | -------------------------------------------------------------------------------- /configs/LVIS-InstanceSegmentation/mask_rcnn_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | NUM_CLASSES: 1230 9 | SCORE_THRESH_TEST: 0.0001 10 | INPUT: 11 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 12 | DATASETS: 13 | TRAIN: ("lvis_v0.5_train",) 14 | TEST: ("lvis_v0.5_val",) 15 | TEST: 16 | DETECTIONS_PER_IMAGE: 300 # LVIS allows up to 300 17 | DATALOADER: 18 | SAMPLER_TRAIN: "RepeatFactorTrainingSampler" 19 | REPEAT_THRESHOLD: 0.001 20 | -------------------------------------------------------------------------------- /configs/LVIS-InstanceSegmentation/mask_rcnn_R_101_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-101.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 101 7 | ROI_HEADS: 8 | NUM_CLASSES: 1230 9 | SCORE_THRESH_TEST: 0.0001 10 | INPUT: 11 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 12 | DATASETS: 13 | TRAIN: ("lvis_v0.5_train",) 14 | TEST: ("lvis_v0.5_val",) 15 | TEST: 16 | DETECTIONS_PER_IMAGE: 300 # LVIS allows up to 300 17 | DATALOADER: 18 | SAMPLER_TRAIN: "RepeatFactorTrainingSampler" 19 | REPEAT_THRESHOLD: 0.001 20 | -------------------------------------------------------------------------------- /docs/modules/engine.rst: -------------------------------------------------------------------------------- 1 | detectron2.engine package 2 | ========================= 3 | 4 | 5 | .. automodule:: detectron2.engine 6 | :members: 7 | :undoc-members: 8 | :show-inheritance: 9 | 10 | 11 | detectron2.engine.defaults module 12 | --------------------------------- 13 | 14 | .. automodule:: detectron2.engine.defaults 15 | :members: 16 | :undoc-members: 17 | :show-inheritance: 18 | 19 | detectron2.engine.hooks module 20 | --------------------------------- 21 | 22 | .. automodule:: detectron2.engine.hooks 23 | :members: 24 | :undoc-members: 25 | :show-inheritance: 26 | -------------------------------------------------------------------------------- /projects/EQL/configs/eql_resampling_mask_rcnn_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-EQL-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | NUM_CLASSES: 1230 9 | SCORE_THRESH_TEST: 0.0001 10 | INPUT: 11 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 12 | DATASETS: 13 | TRAIN: ("lvis_v0.5_train",) 14 | TEST: ("lvis_v0.5_val",) 15 | TEST: 16 | DETECTIONS_PER_IMAGE: 300 # LVIS allows up to 300 17 | DATALOADER: 18 | SAMPLER_TRAIN: "RepeatFactorTrainingSampler" 19 | REPEAT_THRESHOLD: 0.0005 20 | -------------------------------------------------------------------------------- /dev/packaging/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## To build a cu101 wheel for release: 3 | 4 | ``` 5 | $ nvidia-docker run -it --storage-opt "size=20GB" --name pt pytorch/manylinux-cuda101 6 | # inside the container: 7 | # git clone https://github.com/facebookresearch/detectron2/ 8 | # cd detectron2 9 | # export CU_VERSION=cu101 D2_VERSION_SUFFIX= PYTHON_VERSION=3.7 PYTORCH_VERSION=1.4 10 | # ./dev/packaging/build_wheel.sh 11 | ``` 12 | 13 | ## To build all wheels for `CUDA {9.2,10.0,10.1}` x `Python {3.6,3.7,3.8}`: 14 | ``` 15 | ./dev/packaging/build_all_wheels.sh 16 | ./dev/packaging/gen_wheel_index.sh /path/to/wheels 17 | ``` 18 | -------------------------------------------------------------------------------- /docker/docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: "2.3" 2 | services: 3 | detectron2: 4 | build: 5 | context: . 6 | dockerfile: Dockerfile 7 | args: 8 | USER_ID: ${USER_ID:-1000} 9 | runtime: nvidia # TODO: Exchange with "gpu: all" in the future (see https://github.com/facebookresearch/detectron2/pull/197/commits/00545e1f376918db4a8ce264d427a07c1e896c5a). 10 | shm_size: "8gb" 11 | ulimits: 12 | memlock: -1 13 | stack: 67108864 14 | volumes: 15 | - /tmp/.X11-unix:/tmp/.X11-unix:ro 16 | environment: 17 | - DISPLAY=$DISPLAY 18 | - NVIDIA_VISIBLE_DEVICES=all 19 | -------------------------------------------------------------------------------- /configs/Misc/scratch_mask_rcnn_R_50_FPN_3x_gn.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "mask_rcnn_R_50_FPN_3x_gn.yaml" 2 | # INPUT: 3 | # It makes sense to divide by STD when training from scratch 4 | # But it seems to make no difference on the results and C2's models didn't do this. 5 | # So we keep things consistent with C2. 6 | # PIXEL_STD: [57.375, 57.12, 58.395] 7 | MODEL: 8 | # Train from random initialization. 9 | WEIGHTS: "" 10 | MASK_ON: True 11 | BACKBONE: 12 | FREEZE_AT: 0 13 | # NOTE: Please refer to Rethinking ImageNet Pre-training https://arxiv.org/abs/1811.08883 14 | # to learn what you need for training from scratch. 15 | -------------------------------------------------------------------------------- /configs/COCO-Keypoints/Base-Keypoint-RCNN-FPN.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | KEYPOINT_ON: True 4 | ROI_HEADS: 5 | NUM_CLASSES: 1 6 | ROI_BOX_HEAD: 7 | SMOOTH_L1_BETA: 0.5 # Keypoint AP degrades (though box AP improves) when using plain L1 loss 8 | RPN: 9 | # Detectron1 uses 2000 proposals per-batch, but this option is per-image in detectron2. 10 | # 1000 proposals per-image is found to hurt box AP. 11 | # Therefore we increase it to 1500 per-image. 12 | POST_NMS_TOPK_TRAIN: 1500 13 | DATASETS: 14 | TRAIN: ("keypoints_coco_2017_train",) 15 | TEST: ("keypoints_coco_2017_val",) 16 | -------------------------------------------------------------------------------- /configs/quick_schedules/mask_rcnn_R_50_FPN_training_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | ROI_HEADS: 5 | BATCH_SIZE_PER_IMAGE: 256 6 | MASK_ON: True 7 | DATASETS: 8 | TRAIN: ("coco_2017_val",) 9 | TEST: ("coco_2017_val",) 10 | INPUT: 11 | MIN_SIZE_TRAIN: (600,) 12 | MAX_SIZE_TRAIN: 1000 13 | MIN_SIZE_TEST: 800 14 | MAX_SIZE_TEST: 1000 15 | SOLVER: 16 | WARMUP_FACTOR: 0.3333333 17 | WARMUP_ITERS: 100 18 | STEPS: (5500, 5800) 19 | MAX_ITER: 6000 20 | TEST: 21 | EXPECTED_RESULTS: [["bbox", "AP", 42.0, 1.6], ["segm", "AP", 35.4, 1.25]] 22 | -------------------------------------------------------------------------------- /datasets/prepare_for_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | # Download some files needed for running tests. 5 | 6 | cd "${0%/*}" 7 | 8 | BASE=https://dl.fbaipublicfiles.com/detectron2 9 | mkdir -p coco/annotations 10 | 11 | for anno in instances_val2017_100 \ 12 | person_keypoints_val2017_100 \ 13 | instances_minival2014_100 \ 14 | person_keypoints_minival2014_100; do 15 | 16 | dest=coco/annotations/$anno.json 17 | [[ -s $dest ]] && { 18 | echo "$dest exists. Skipping ..." 19 | } || { 20 | wget $BASE/annotations/coco/$anno.json -O $dest 21 | } 22 | done 23 | -------------------------------------------------------------------------------- /detectron2/modeling/roi_heads/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .box_head import ROI_BOX_HEAD_REGISTRY, build_box_head 3 | from .keypoint_head import ROI_KEYPOINT_HEAD_REGISTRY, build_keypoint_head, BaseKeypointRCNNHead 4 | from .mask_head import ROI_MASK_HEAD_REGISTRY, build_mask_head, BaseMaskRCNNHead 5 | from .roi_heads import ( 6 | ROI_HEADS_REGISTRY, 7 | ROIHeads, 8 | Res5ROIHeads, 9 | StandardROIHeads, 10 | build_roi_heads, 11 | select_foreground_proposals, 12 | ) 13 | from .rotated_fast_rcnn import RROIHeads 14 | 15 | from . import cascade_rcnn # isort:skip 16 | -------------------------------------------------------------------------------- /dev/packaging/gen_wheel_index.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | 4 | root=$1 5 | if [[ -z "$root" ]]; then 6 | echo "Usage: ./gen_wheel_index.sh /path/to/wheels" 7 | exit 8 | fi 9 | 10 | index=$root/index.html 11 | 12 | cd "$root" 13 | for cu in cpu cu92 cu100 cu101; do 14 | cd $cu 15 | echo "Creating $PWD/index.html ..." 16 | for whl in *.whl; do 17 | echo "$whl
" 18 | done > index.html 19 | cd "$root" 20 | done 21 | 22 | echo "Creating $index ..." 23 | for whl in $(find . -type f -name '*.whl' -printf '%P\n' | sort); do 24 | echo "$whl
" 25 | done > "$index" 26 | 27 | -------------------------------------------------------------------------------- /configs/quick_schedules/fast_rcnn_R_50_FPN_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../COCO-Detection/fast_rcnn_R_50_FPN_1x.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | DATASETS: 5 | TRAIN: ("coco_2017_val_100",) 6 | PROPOSAL_FILES_TRAIN: ("detectron2://COCO-Detection/rpn_R_50_FPN_1x/137258492/coco_2017_val_box_proposals_ee0dad.pkl", ) 7 | TEST: ("coco_2017_val_100",) 8 | PROPOSAL_FILES_TEST: ("detectron2://COCO-Detection/rpn_R_50_FPN_1x/137258492/coco_2017_val_box_proposals_ee0dad.pkl", ) 9 | SOLVER: 10 | BASE_LR: 0.005 11 | STEPS: (30,) 12 | MAX_ITER: 40 13 | IMS_PER_BATCH: 4 14 | DATALOADER: 15 | NUM_WORKERS: 2 16 | -------------------------------------------------------------------------------- /detectron2/layers/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .batch_norm import FrozenBatchNorm2d, get_norm, NaiveSyncBatchNorm 3 | from .deform_conv import DeformConv, ModulatedDeformConv 4 | from .mask_ops import paste_masks_in_image 5 | from .nms import batched_nms, batched_nms_rotated, nms, nms_rotated 6 | from .roi_align import ROIAlign, roi_align 7 | from .roi_align_rotated import ROIAlignRotated, roi_align_rotated 8 | from .shape_spec import ShapeSpec 9 | from .wrappers import BatchNorm2d, Conv2d, ConvTranspose2d, cat, interpolate 10 | 11 | __all__ = [k for k in globals().keys() if not k.startswith("_")] 12 | -------------------------------------------------------------------------------- /configs/Detectron1-Comparisons/mask_rcnn_R_50_FPN_noaug_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | # Detectron1 uses smooth L1 loss with some magic beta values. 8 | # The defaults are changed to L1 loss in Detectron2. 9 | RPN: 10 | SMOOTH_L1_BETA: 0.1111 11 | ROI_BOX_HEAD: 12 | SMOOTH_L1_BETA: 1.0 13 | POOLER_SAMPLING_RATIO: 2 14 | POOLER_TYPE: "ROIAlign" 15 | ROI_MASK_HEAD: 16 | POOLER_SAMPLING_RATIO: 2 17 | POOLER_TYPE: "ROIAlign" 18 | INPUT: 19 | # no scale augmentation 20 | MIN_SIZE_TRAIN: (800, ) 21 | -------------------------------------------------------------------------------- /configs/quick_schedules/semantic_R_50_FPN_training_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | META_ARCHITECTURE: "SemanticSegmentor" 4 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 5 | RESNETS: 6 | DEPTH: 50 7 | DATASETS: 8 | TRAIN: ("coco_2017_val_panoptic_stuffonly",) 9 | TEST: ("coco_2017_val_panoptic_stuffonly",) 10 | SOLVER: 11 | BASE_LR: 0.01 12 | WARMUP_FACTOR: 0.001 13 | WARMUP_ITERS: 300 14 | STEPS: (5500,) 15 | MAX_ITER: 7000 16 | TEST: 17 | EXPECTED_RESULTS: [["sem_seg", "mIoU", 76.51, 1.0], ["sem_seg", "mACC", 83.25, 1.0]] 18 | INPUT: 19 | # no scale augmentation 20 | MIN_SIZE_TRAIN: (800, ) 21 | -------------------------------------------------------------------------------- /projects/README.md: -------------------------------------------------------------------------------- 1 | 2 | Here are a few research projects that are built on detectron2. 3 | They are examples of how to use detectron2 as a library, to make your projects more 4 | maintainable. 5 | 6 | Note that these are research projects, and therefore may not have the same level 7 | of support or stability of detectron2. 8 | 9 | + [DensePose: Dense Human Pose Estimation In The Wild](DensePose) 10 | + [Scale-Aware Trident Networks for Object Detection](TridentNet) 11 | + [TensorMask: A Foundation for Dense Object Segmentation](TensorMask) 12 | + [Mesh R-CNN](https://github.com/facebookresearch/meshrcnn) 13 | + [PointRend: Image Segmentation as Rendering](PointRend) 14 | -------------------------------------------------------------------------------- /projects/PointRend/configs/InstanceSegmentation/Base-PointRend-RCNN-FPN.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../../../../configs/Base-RCNN-FPN.yaml" 2 | MODEL: 3 | ROI_HEADS: 4 | NAME: "PointRendROIHeads" 5 | IN_FEATURES: ["p2", "p3", "p4", "p5"] 6 | ROI_BOX_HEAD: 7 | TRAIN_ON_PRED_BOXES: True 8 | ROI_MASK_HEAD: 9 | NAME: "CoarseMaskHead" 10 | FC_DIM: 1024 11 | NUM_FC: 2 12 | OUTPUT_SIDE_RESOLUTION: 7 13 | IN_FEATURES: ["p2"] 14 | POINT_HEAD_ON: True 15 | POINT_HEAD: 16 | FC_DIM: 256 17 | NUM_FC: 3 18 | IN_FEATURES: ["p2"] 19 | INPUT: 20 | # PointRend for instance segmenation does not work with "polygon" mask_format. 21 | MASK_FORMAT: "bitmask" 22 | -------------------------------------------------------------------------------- /configs/quick_schedules/mask_rcnn_R_50_C4_training_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | ROI_HEADS: 5 | BATCH_SIZE_PER_IMAGE: 256 6 | MASK_ON: True 7 | DATASETS: 8 | TRAIN: ("coco_2017_val",) 9 | TEST: ("coco_2017_val",) 10 | INPUT: 11 | MIN_SIZE_TRAIN: (600,) 12 | MAX_SIZE_TRAIN: 1000 13 | MIN_SIZE_TEST: 800 14 | MAX_SIZE_TEST: 1000 15 | SOLVER: 16 | IMS_PER_BATCH: 8 # base uses 16 17 | WARMUP_FACTOR: 0.33333 18 | WARMUP_ITERS: 100 19 | STEPS: (11000, 11600) 20 | MAX_ITER: 12000 21 | TEST: 22 | EXPECTED_RESULTS: [["bbox", "AP", 41.88, 0.7], ["segm", "AP", 33.79, 0.5]] 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # output dir 2 | output 3 | instant_test_output 4 | inference_test_output 5 | 6 | 7 | *.jpg 8 | *.png 9 | *.txt 10 | *.json 11 | *.diff 12 | 13 | # compilation and distribution 14 | __pycache__ 15 | _ext 16 | *.pyc 17 | *.so 18 | detectron2.egg-info/ 19 | build/ 20 | dist/ 21 | wheels/ 22 | 23 | # pytorch/python/numpy formats 24 | *.pth 25 | *.pkl 26 | *.npy 27 | 28 | # ipython/jupyter notebooks 29 | *.ipynb 30 | **/.ipynb_checkpoints/ 31 | 32 | # Editor temporaries 33 | *.swn 34 | *.swo 35 | *.swp 36 | *~ 37 | 38 | # editor settings 39 | .idea 40 | .vscode 41 | 42 | # project dirs 43 | /detectron2/model_zoo/configs 44 | /datasets 45 | /projects/*/datasets 46 | /models 47 | -------------------------------------------------------------------------------- /detectron2/data/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from . import transforms # isort:skip 3 | 4 | from .build import ( 5 | build_detection_test_loader, 6 | build_detection_train_loader, 7 | get_detection_dataset_dicts, 8 | load_proposals_into_dataset, 9 | print_instances_class_histogram, 10 | ) 11 | from .catalog import DatasetCatalog, MetadataCatalog 12 | from .common import DatasetFromList, MapDataset 13 | from .dataset_mapper import DatasetMapper 14 | 15 | # ensure the builtin datasets are registered 16 | from . import datasets, samplers # isort:skip 17 | 18 | __all__ = [k for k in globals().keys() if not k.startswith("_")] 19 | -------------------------------------------------------------------------------- /configs/quick_schedules/panoptic_fpn_R_50_training_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | META_ARCHITECTURE: "PanopticFPN" 4 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 5 | MASK_ON: True 6 | RESNETS: 7 | DEPTH: 50 8 | SEM_SEG_HEAD: 9 | LOSS_WEIGHT: 0.5 10 | DATASETS: 11 | TRAIN: ("coco_2017_val_panoptic_separated",) 12 | TEST: ("coco_2017_val_panoptic_separated",) 13 | SOLVER: 14 | BASE_LR: 0.01 15 | WARMUP_FACTOR: 0.001 16 | WARMUP_ITERS: 500 17 | STEPS: (5500,) 18 | MAX_ITER: 7000 19 | TEST: 20 | EXPECTED_RESULTS: [["bbox", "AP", 46.80, 1.1], ["segm", "AP", 38.93, 0.7], ["sem_seg", "mIoU", 64.53, 1.0], ["panoptic_seg", "PQ", 48.23, 0.8]] 21 | -------------------------------------------------------------------------------- /projects/PointRend/configs/InstanceSegmentation/pointrend_rcnn_R_50_FPN_1x_cityscapes.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: Base-PointRend-RCNN-FPN.yaml 2 | MODEL: 3 | WEIGHTS: detectron2://ImageNetPretrained/MSRA/R-50.pkl 4 | MASK_ON: true 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | NUM_CLASSES: 8 9 | POINT_HEAD: 10 | NUM_CLASSES: 8 11 | DATASETS: 12 | TEST: ("cityscapes_fine_instance_seg_val",) 13 | TRAIN: ("cityscapes_fine_instance_seg_train",) 14 | SOLVER: 15 | BASE_LR: 0.01 16 | IMS_PER_BATCH: 8 17 | MAX_ITER: 24000 18 | STEPS: (18000,) 19 | INPUT: 20 | MAX_SIZE_TEST: 2048 21 | MAX_SIZE_TRAIN: 2048 22 | MIN_SIZE_TEST: 1024 23 | MIN_SIZE_TRAIN: (800, 832, 864, 896, 928, 960, 992, 1024) 24 | -------------------------------------------------------------------------------- /docker/Dockerfile-circleci: -------------------------------------------------------------------------------- 1 | FROM nvidia/cuda:10.1-cudnn7-devel 2 | # This dockerfile only aims to provide an environment for unittest on CircleCI 3 | 4 | ENV DEBIAN_FRONTEND noninteractive 5 | RUN apt-get update && apt-get install -y \ 6 | python3-opencv ca-certificates python3-dev git wget sudo && \ 7 | rm -rf /var/lib/apt/lists/* 8 | 9 | RUN wget -q https://bootstrap.pypa.io/get-pip.py && \ 10 | python3 get-pip.py && \ 11 | rm get-pip.py 12 | 13 | # install dependencies 14 | # See https://pytorch.org/ for other options if you use a different version of CUDA 15 | RUN pip install torch torchvision tensorboard cython 16 | RUN pip install 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI' 17 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/questions-help-support.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "❓How to do something?" 3 | about: How to do X with detectron2? How detectron2 does X? 4 | 5 | --- 6 | 7 | ## ❓ How to use Detectron2 8 | 9 | Questions like: 10 | 11 | 1. How to do X with detectron2? 12 | 2. How detectron2 does X? 13 | 14 | NOTE: 15 | 16 | 1. If you met any unexpected issue when using detectron2 and wish to know why, 17 | please use the "Unexpected Problems / Bugs" issue template. 18 | 19 | 2. We do not answer general machine learning / computer vision questions that are not specific to 20 | detectron2, such as how a model works, how to improve your training/make it converge, or what algorithm/methods can be 21 | used to achieve X. 22 | -------------------------------------------------------------------------------- /detectron2/evaluation/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .cityscapes_evaluation import CityscapesEvaluator 3 | from .coco_evaluation import COCOEvaluator 4 | from .rotated_coco_evaluation import RotatedCOCOEvaluator 5 | from .evaluator import DatasetEvaluator, DatasetEvaluators, inference_context, inference_on_dataset 6 | from .lvis_evaluation import LVISEvaluator 7 | from .panoptic_evaluation import COCOPanopticEvaluator 8 | from .pascal_voc_evaluation import PascalVOCDetectionEvaluator 9 | from .sem_seg_evaluation import SemSegEvaluator 10 | from .testing import print_csv_format, verify_results 11 | 12 | __all__ = [k for k in globals().keys() if not k.startswith("_")] 13 | -------------------------------------------------------------------------------- /configs/COCO-Detection/fast_rcnn_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | MASK_ON: False 5 | LOAD_PROPOSALS: True 6 | RESNETS: 7 | DEPTH: 50 8 | PROPOSAL_GENERATOR: 9 | NAME: "PrecomputedProposals" 10 | DATASETS: 11 | TRAIN: ("coco_2017_train",) 12 | PROPOSAL_FILES_TRAIN: ("detectron2://COCO-Detection/rpn_R_50_FPN_1x/137258492/coco_2017_train_box_proposals_21bc3a.pkl", ) 13 | TEST: ("coco_2017_val",) 14 | PROPOSAL_FILES_TEST: ("detectron2://COCO-Detection/rpn_R_50_FPN_1x/137258492/coco_2017_val_box_proposals_ee0dad.pkl", ) 15 | DATALOADER: 16 | # proposals are part of the dataset_dicts, and take a lot of RAM 17 | NUM_WORKERS: 2 18 | -------------------------------------------------------------------------------- /docs/Makefile: -------------------------------------------------------------------------------- 1 | # Minimal makefile for Sphinx documentation 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | # You can set these variables from the command line. 5 | SPHINXOPTS = 6 | SPHINXBUILD = sphinx-build 7 | SOURCEDIR = . 8 | BUILDDIR = _build 9 | 10 | # Put it first so that "make" without argument is like "make help". 11 | help: 12 | @$(SPHINXBUILD) -M help "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 13 | 14 | .PHONY: help Makefile 15 | 16 | # Catch-all target: route all unknown targets to Sphinx using the new 17 | # "make mode" option. $(O) is meant as a shortcut for $(SPHINXOPTS). 18 | %: Makefile 19 | @$(SPHINXBUILD) -M $@ "$(SOURCEDIR)" "$(BUILDDIR)" $(SPHINXOPTS) $(O) 20 | -------------------------------------------------------------------------------- /detectron2/modeling/meta_arch/build.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from detectron2.utils.registry import Registry 3 | 4 | META_ARCH_REGISTRY = Registry("META_ARCH") # noqa F401 isort:skip 5 | META_ARCH_REGISTRY.__doc__ = """ 6 | Registry for meta-architectures, i.e. the whole model. 7 | 8 | The registered object will be called with `obj(cfg)` 9 | and expected to return a `nn.Module` object. 10 | """ 11 | 12 | 13 | def build_model(cfg): 14 | """ 15 | Build the whole model architecture, defined by ``cfg.MODEL.META_ARCHITECTURE``. 16 | Note that it does not load any weights from ``cfg``. 17 | """ 18 | meta_arch = cfg.MODEL.META_ARCHITECTURE 19 | return META_ARCH_REGISTRY.get(meta_arch)(cfg) 20 | -------------------------------------------------------------------------------- /projects/TensorMask/configs/Base-TensorMask.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "TensorMask" 3 | MASK_ON: True 4 | BACKBONE: 5 | NAME: "build_retinanet_resnet_fpn_backbone" 6 | RESNETS: 7 | OUT_FEATURES: ["res2", "res3", "res4", "res5"] 8 | ANCHOR_GENERATOR: 9 | SIZES: [[44, 60], [88, 120], [176, 240], [352, 480], [704, 960], [1408, 1920]] 10 | ASPECT_RATIOS: [[1.0]] 11 | FPN: 12 | IN_FEATURES: ["res2", "res3", "res4", "res5"] 13 | FUSE_TYPE: "avg" 14 | TENSOR_MASK: 15 | ALIGNED_ON: True 16 | BIPYRAMID_ON: True 17 | DATASETS: 18 | TRAIN: ("coco_2017_train",) 19 | TEST: ("coco_2017_val",) 20 | SOLVER: 21 | IMS_PER_BATCH: 16 22 | BASE_LR: 0.02 23 | STEPS: (60000, 80000) 24 | MAX_ITER: 90000 25 | VERSION: 2 26 | -------------------------------------------------------------------------------- /configs/LVIS-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/FAIR/X-101-32x8d.pkl" 4 | PIXEL_STD: [57.375, 57.120, 58.395] 5 | MASK_ON: True 6 | RESNETS: 7 | STRIDE_IN_1X1: False # this is a C2 model 8 | NUM_GROUPS: 32 9 | WIDTH_PER_GROUP: 8 10 | DEPTH: 101 11 | ROI_HEADS: 12 | NUM_CLASSES: 1230 13 | SCORE_THRESH_TEST: 0.0001 14 | INPUT: 15 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 16 | DATASETS: 17 | TRAIN: ("lvis_v0.5_train",) 18 | TEST: ("lvis_v0.5_val",) 19 | TEST: 20 | DETECTIONS_PER_IMAGE: 300 # LVIS allows up to 300 21 | DATALOADER: 22 | SAMPLER_TRAIN: "RepeatFactorTrainingSampler" 23 | REPEAT_THRESHOLD: 0.001 24 | -------------------------------------------------------------------------------- /detectron2/layers/shape_spec.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | from collections import namedtuple 4 | 5 | 6 | class ShapeSpec(namedtuple("_ShapeSpec", ["channels", "height", "width", "stride"])): 7 | """ 8 | A simple structure that contains basic shape specification about a tensor. 9 | It is often used as the auxiliary inputs/outputs of models, 10 | to obtain the shape inference ability among pytorch modules. 11 | 12 | Attributes: 13 | channels: 14 | height: 15 | width: 16 | stride: 17 | """ 18 | 19 | def __new__(cls, *, channels=None, height=None, width=None, stride=None): 20 | return super().__new__(cls, channels, height, width, stride) 21 | -------------------------------------------------------------------------------- /detectron2/layers/rotated_boxes.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from __future__ import absolute_import, division, print_function, unicode_literals 3 | 4 | # import torch 5 | from detectron2 import _C 6 | 7 | 8 | def pairwise_iou_rotated(boxes1, boxes2): 9 | """ 10 | Return intersection-over-union (Jaccard index) of boxes. 11 | 12 | Both sets of boxes are expected to be in 13 | (x_center, y_center, width, height, angle) format. 14 | 15 | Arguments: 16 | boxes1 (Tensor[N, 5]) 17 | boxes2 (Tensor[M, 5]) 18 | 19 | Returns: 20 | iou (Tensor[N, M]): the NxM matrix containing the pairwise 21 | IoU values for every element in boxes1 and boxes2 22 | """ 23 | return _C.box_iou_rotated(boxes1, boxes2) 24 | -------------------------------------------------------------------------------- /configs/Misc/panoptic_fpn_R_101_dconv_cascade_gn_3x.yaml: -------------------------------------------------------------------------------- 1 | # A large PanopticFPN for demo purposes. 2 | # Use GN on backbone to support semantic seg. 3 | # Use Cascade + Deform Conv to improve localization. 4 | _BASE_: "../COCO-PanopticSegmentation/Base-Panoptic-FPN.yaml" 5 | MODEL: 6 | WEIGHTS: "catalog://ImageNetPretrained/FAIR/R-101-GN" 7 | RESNETS: 8 | DEPTH: 101 9 | NORM: "GN" 10 | DEFORM_ON_PER_STAGE: [False, True, True, True] 11 | STRIDE_IN_1X1: False 12 | FPN: 13 | NORM: "GN" 14 | ROI_HEADS: 15 | NAME: CascadeROIHeads 16 | ROI_BOX_HEAD: 17 | CLS_AGNOSTIC_BBOX_REG: True 18 | ROI_MASK_HEAD: 19 | NORM: "GN" 20 | RPN: 21 | POST_NMS_TOPK_TRAIN: 2000 22 | SOLVER: 23 | STEPS: (105000, 125000) 24 | MAX_ITER: 135000 25 | IMS_PER_BATCH: 32 26 | BASE_LR: 0.04 27 | -------------------------------------------------------------------------------- /configs/Base-RetinaNet.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "RetinaNet" 3 | BACKBONE: 4 | NAME: "build_retinanet_resnet_fpn_backbone" 5 | RESNETS: 6 | OUT_FEATURES: ["res3", "res4", "res5"] 7 | ANCHOR_GENERATOR: 8 | SIZES: !!python/object/apply:eval ["[[x, x * 2**(1.0/3), x * 2**(2.0/3) ] for x in [32, 64, 128, 256, 512 ]]"] 9 | FPN: 10 | IN_FEATURES: ["res3", "res4", "res5"] 11 | RETINANET: 12 | IOU_THRESHOLDS: [0.4, 0.5] 13 | IOU_LABELS: [0, -1, 1] 14 | DATASETS: 15 | TRAIN: ("coco_2017_train",) 16 | TEST: ("coco_2017_val",) 17 | SOLVER: 18 | IMS_PER_BATCH: 16 19 | BASE_LR: 0.01 # Note that RetinaNet uses a different default learning rate 20 | STEPS: (60000, 80000) 21 | MAX_ITER: 90000 22 | INPUT: 23 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 24 | VERSION: 2 25 | -------------------------------------------------------------------------------- /projects/TridentNet/configs/Base-TridentNet-Fast-C4.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "GeneralizedRCNN" 3 | BACKBONE: 4 | NAME: "build_trident_resnet_backbone" 5 | ROI_HEADS: 6 | NAME: "TridentRes5ROIHeads" 7 | POSITIVE_FRACTION: 0.5 8 | BATCH_SIZE_PER_IMAGE: 128 9 | PROPOSAL_APPEND_GT: False 10 | PROPOSAL_GENERATOR: 11 | NAME: "TridentRPN" 12 | RPN: 13 | POST_NMS_TOPK_TRAIN: 500 14 | TRIDENT: 15 | NUM_BRANCH: 3 16 | BRANCH_DILATIONS: [1, 2, 3] 17 | TEST_BRANCH_IDX: 1 18 | TRIDENT_STAGE: "res4" 19 | DATASETS: 20 | TRAIN: ("coco_2017_train",) 21 | TEST: ("coco_2017_val",) 22 | SOLVER: 23 | IMS_PER_BATCH: 16 24 | BASE_LR: 0.02 25 | STEPS: (60000, 80000) 26 | MAX_ITER: 90000 27 | INPUT: 28 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 29 | VERSION: 2 30 | -------------------------------------------------------------------------------- /configs/Base-RCNN-DilatedC5.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "GeneralizedRCNN" 3 | RESNETS: 4 | OUT_FEATURES: ["res5"] 5 | RES5_DILATION: 2 6 | RPN: 7 | IN_FEATURES: ["res5"] 8 | PRE_NMS_TOPK_TEST: 6000 9 | POST_NMS_TOPK_TEST: 1000 10 | ROI_HEADS: 11 | NAME: "StandardROIHeads" 12 | IN_FEATURES: ["res5"] 13 | ROI_BOX_HEAD: 14 | NAME: "FastRCNNConvFCHead" 15 | NUM_FC: 2 16 | POOLER_RESOLUTION: 7 17 | ROI_MASK_HEAD: 18 | NAME: "MaskRCNNConvUpsampleHead" 19 | NUM_CONV: 4 20 | POOLER_RESOLUTION: 14 21 | DATASETS: 22 | TRAIN: ("coco_2017_train",) 23 | TEST: ("coco_2017_val",) 24 | SOLVER: 25 | IMS_PER_BATCH: 16 26 | BASE_LR: 0.02 27 | STEPS: (60000, 80000) 28 | MAX_ITER: 90000 29 | INPUT: 30 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 31 | VERSION: 2 32 | -------------------------------------------------------------------------------- /docker/README.md: -------------------------------------------------------------------------------- 1 | ## Run the container 2 | Change to the *docker* directory of this repository: 3 | ``` 4 | cd docker 5 | USER_ID=$UID docker-compose run detectron2 6 | ``` 7 | 8 | #### Using a persistent cache directory 9 | Prevents models to be re-downloaded on every run, by storing them in a cache directory. 10 | 11 | `docker-compose run --volume=/path/to/cache:/tmp:rw detectron2` 12 | 13 | ## Rebuild the container 14 | Rebuild the container by `USER_ID=$UID docker-compose build detectron2`. 15 | This is only necessary when `Dockerfile` has been changed. The initial build is done automatically. 16 | 17 | ## Install new dependencies 18 | Add the following to `Dockerfile` to make persistent changes. 19 | ``` 20 | RUN sudo apt-get update && sudo apt-get install -y \ 21 | nano vim emacs 22 | RUN pip install --user pandas 23 | ``` 24 | Or run them in the container to make temporary changes. 25 | -------------------------------------------------------------------------------- /docs/notes/changelog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ### Releases 4 | See release log at 5 | [https://github.com/facebookresearch/detectron2/releases](https://github.com/facebookresearch/detectron2/releases) 6 | 7 | ### Notable Backward Incompatible Changes: 8 | 9 | * 02/14/2020,02/18/2020: Mask head and keypoint head now include logic for losses & inference. Custom heads 10 | should overwrite the feature computation by `layers()` method. 11 | * 11/11/2019: `detectron2.data.detection_utils.read_image` transposes images with exif information. 12 | 13 | ### Config Version Change Log 14 | 15 | * v1: Rename `RPN_HEAD.NAME` to `RPN.HEAD_NAME`. 16 | * v2: A batch of rename of many configurations before release. 17 | 18 | ### Known Bugs in Historical Versions: 19 | * Dec 19 - Dec 26: Using aspect ratio grouping causes a drop in accuracy. 20 | * Oct 10 - Nov 9: Test time augmentation does not predict the last category. 21 | -------------------------------------------------------------------------------- /tests/test_sampler.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import unittest 3 | from torch.utils.data.sampler import SequentialSampler 4 | 5 | from detectron2.data.samplers import GroupedBatchSampler 6 | 7 | 8 | class TestGroupedBatchSampler(unittest.TestCase): 9 | def test_missing_group_id(self): 10 | sampler = SequentialSampler(list(range(100))) 11 | group_ids = [1] * 100 12 | samples = GroupedBatchSampler(sampler, group_ids, 2) 13 | 14 | for mini_batch in samples: 15 | self.assertEqual(len(mini_batch), 2) 16 | 17 | def test_groups(self): 18 | sampler = SequentialSampler(list(range(100))) 19 | group_ids = [1, 0] * 50 20 | samples = GroupedBatchSampler(sampler, group_ids, 2) 21 | 22 | for mini_batch in samples: 23 | self.assertEqual((mini_batch[0] + mini_batch[1]) % 2, 0) 24 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [isort] 2 | line_length=100 3 | multi_line_output=3 4 | include_trailing_comma=True 5 | known_standard_library=numpy,setuptools,mock 6 | skip=datasets,docs 7 | skip_glob=*/__init__.py 8 | known_myself=detectron2 9 | known_third_party=fvcore,matplotlib,cv2,torch,torchvision,PIL,pycocotools,yacs,termcolor,cityscapesscripts,tabulate,tqdm,scipy,lvis,psutil,pkg_resources,caffe2,onnx 10 | no_lines_before=STDLIB,THIRDPARTY 11 | sections=FUTURE,STDLIB,THIRDPARTY,myself,FIRSTPARTY,LOCALFOLDER 12 | default_section=FIRSTPARTY 13 | 14 | [mypy] 15 | python_version=3.6 16 | ignore_missing_imports = True 17 | warn_unused_configs = True 18 | disallow_untyped_defs = True 19 | check_untyped_defs = True 20 | warn_unused_ignores = True 21 | warn_redundant_casts = True 22 | show_column_numbers = True 23 | follow_imports = silent 24 | allow_redefinition = True 25 | ; Require all functions to be annotated 26 | disallow_incomplete_defs = True 27 | -------------------------------------------------------------------------------- /configs/quick_schedules/keypoint_rcnn_R_50_FPN_training_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | KEYPOINT_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | BATCH_SIZE_PER_IMAGE: 256 9 | NUM_CLASSES: 1 10 | ROI_KEYPOINT_HEAD: 11 | POOLER_RESOLUTION: 14 12 | POOLER_SAMPLING_RATIO: 2 13 | ROI_BOX_HEAD: 14 | SMOOTH_L1_BETA: 1.0 # Keypoint AP degrades when using plain L1 loss 15 | RPN: 16 | SMOOTH_L1_BETA: 0.2 # Keypoint AP degrades when using plain L1 loss 17 | DATASETS: 18 | TRAIN: ("keypoints_coco_2017_val",) 19 | TEST: ("keypoints_coco_2017_val",) 20 | INPUT: 21 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 22 | SOLVER: 23 | WARMUP_FACTOR: 0.33333333 24 | WARMUP_ITERS: 100 25 | STEPS: (5500, 5800) 26 | MAX_ITER: 6000 27 | TEST: 28 | EXPECTED_RESULTS: [["bbox", "AP", 53.5, 1.0], ["keypoints", "AP", 72.4, 1.0]] 29 | -------------------------------------------------------------------------------- /configs/Misc/cascade_mask_rcnn_X_152_32x8d_FPN_IN5k_gn_dconv.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | MASK_ON: True 4 | WEIGHTS: "catalog://ImageNetPretrained/FAIR/X-152-32x8d-IN5k" 5 | RESNETS: 6 | STRIDE_IN_1X1: False # this is a C2 model 7 | NUM_GROUPS: 32 8 | WIDTH_PER_GROUP: 8 9 | DEPTH: 152 10 | DEFORM_ON_PER_STAGE: [False, True, True, True] 11 | ROI_HEADS: 12 | NAME: "CascadeROIHeads" 13 | ROI_BOX_HEAD: 14 | NAME: "FastRCNNConvFCHead" 15 | NUM_CONV: 4 16 | NUM_FC: 1 17 | NORM: "GN" 18 | CLS_AGNOSTIC_BBOX_REG: True 19 | ROI_MASK_HEAD: 20 | NUM_CONV: 8 21 | NORM: "GN" 22 | RPN: 23 | POST_NMS_TOPK_TRAIN: 2000 24 | SOLVER: 25 | IMS_PER_BATCH: 128 26 | STEPS: (35000, 45000) 27 | MAX_ITER: 50000 28 | BASE_LR: 0.16 29 | INPUT: 30 | MIN_SIZE_TRAIN: (640, 864) 31 | MIN_SIZE_TRAIN_SAMPLING: "range" 32 | MAX_SIZE_TRAIN: 1440 33 | CROP: 34 | ENABLED: True 35 | TEST: 36 | EVAL_PERIOD: 2500 37 | -------------------------------------------------------------------------------- /detectron2/modeling/proposal_generator/build.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from detectron2.utils.registry import Registry 3 | 4 | PROPOSAL_GENERATOR_REGISTRY = Registry("PROPOSAL_GENERATOR") 5 | PROPOSAL_GENERATOR_REGISTRY.__doc__ = """ 6 | Registry for proposal generator, which produces object proposals from feature maps. 7 | 8 | The registered object will be called with `obj(cfg, input_shape)`. 9 | The call should return a `nn.Module` object. 10 | """ 11 | 12 | from . import rpn, rrpn # noqa F401 isort:skip 13 | 14 | 15 | def build_proposal_generator(cfg, input_shape): 16 | """ 17 | Build a proposal generator from `cfg.MODEL.PROPOSAL_GENERATOR.NAME`. 18 | The name can be "PrecomputedProposals" to use no proposal generator. 19 | """ 20 | name = cfg.MODEL.PROPOSAL_GENERATOR.NAME 21 | if name == "PrecomputedProposals": 22 | return None 23 | 24 | return PROPOSAL_GENERATOR_REGISTRY.get(name)(cfg, input_shape) 25 | -------------------------------------------------------------------------------- /dev/run_instant_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | BIN="python tools/train_net.py" 5 | OUTPUT="instant_test_output" 6 | NUM_GPUS=2 7 | 8 | CFG_LIST=( "${@:1}" ) 9 | if [ ${#CFG_LIST[@]} -eq 0 ]; then 10 | CFG_LIST=( ./configs/quick_schedules/*instant_test.yaml ) 11 | fi 12 | 13 | echo "========================================================================" 14 | echo "Configs to run:" 15 | echo "${CFG_LIST[@]}" 16 | echo "========================================================================" 17 | 18 | for cfg in "${CFG_LIST[@]}"; do 19 | echo "========================================================================" 20 | echo "Running $cfg ..." 21 | echo "========================================================================" 22 | $BIN --num-gpus $NUM_GPUS --config-file "$cfg" \ 23 | SOLVER.IMS_PER_BATCH $(($NUM_GPUS * 2)) \ 24 | OUTPUT_DIR "$OUTPUT" 25 | rm -rf "$OUTPUT" 26 | done 27 | 28 | -------------------------------------------------------------------------------- /configs/Detectron1-Comparisons/keypoint_rcnn_R_50_FPN_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | KEYPOINT_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | NUM_CLASSES: 1 9 | ROI_KEYPOINT_HEAD: 10 | POOLER_RESOLUTION: 14 11 | POOLER_SAMPLING_RATIO: 2 12 | POOLER_TYPE: "ROIAlign" 13 | # Detectron1 uses smooth L1 loss with some magic beta values. 14 | # The defaults are changed to L1 loss in Detectron2. 15 | ROI_BOX_HEAD: 16 | SMOOTH_L1_BETA: 1.0 17 | POOLER_SAMPLING_RATIO: 2 18 | POOLER_TYPE: "ROIAlign" 19 | RPN: 20 | SMOOTH_L1_BETA: 0.1111 21 | # Detectron1 uses 2000 proposals per-batch, but this option is per-image in detectron2 22 | # 1000 proposals per-image is found to hurt box AP. 23 | # Therefore we increase it to 1500 per-image. 24 | POST_NMS_TOPK_TRAIN: 1500 25 | DATASETS: 26 | TRAIN: ("keypoints_coco_2017_train",) 27 | TEST: ("keypoints_coco_2017_val",) 28 | -------------------------------------------------------------------------------- /projects/DensePose/dev/run_instant_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | BIN="python train_net.py" 5 | OUTPUT="instant_test_output" 6 | NUM_GPUS=2 7 | 8 | CFG_LIST=( "${@:1}" ) 9 | if [ ${#CFG_LIST[@]} -eq 0 ]; then 10 | CFG_LIST=( ./configs/quick_schedules/*instant_test.yaml ) 11 | fi 12 | 13 | echo "========================================================================" 14 | echo "Configs to run:" 15 | echo "${CFG_LIST[@]}" 16 | echo "========================================================================" 17 | 18 | for cfg in "${CFG_LIST[@]}"; do 19 | echo "========================================================================" 20 | echo "Running $cfg ..." 21 | echo "========================================================================" 22 | $BIN --num-gpus $NUM_GPUS --config-file "$cfg" \ 23 | SOLVER.IMS_PER_BATCH $(($NUM_GPUS * 2)) \ 24 | OUTPUT_DIR "$OUTPUT" 25 | rm -rf "$OUTPUT" 26 | done 27 | 28 | -------------------------------------------------------------------------------- /projects/DensePose/dev/run_inference_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | BIN="python train_net.py" 5 | OUTPUT="inference_test_output" 6 | NUM_GPUS=2 7 | 8 | CFG_LIST=( "${@:1}" ) 9 | 10 | if [ ${#CFG_LIST[@]} -eq 0 ]; then 11 | CFG_LIST=( ./configs/quick_schedules/*inference_acc_test.yaml ) 12 | fi 13 | 14 | echo "========================================================================" 15 | echo "Configs to run:" 16 | echo "${CFG_LIST[@]}" 17 | echo "========================================================================" 18 | 19 | for cfg in "${CFG_LIST[@]}"; do 20 | echo "========================================================================" 21 | echo "Running $cfg ..." 22 | echo "========================================================================" 23 | $BIN \ 24 | --eval-only \ 25 | --num-gpus $NUM_GPUS \ 26 | --config-file "$cfg" \ 27 | OUTPUT_DIR $OUTPUT 28 | rm -rf $OUTPUT 29 | done 30 | 31 | -------------------------------------------------------------------------------- /configs/Cityscapes/mask_rcnn_R_50_FPN.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | # WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | # For better, more stable performance initialize from COCO 5 | WEIGHTS: "detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl" 6 | MASK_ON: True 7 | ROI_HEADS: 8 | NUM_CLASSES: 8 9 | # This is similar to the setting used in Mask R-CNN paper, Appendix A 10 | # But there are some differences, e.g., we did not initialize the output 11 | # layer using the corresponding classes from COCO 12 | INPUT: 13 | MIN_SIZE_TRAIN: (800, 832, 864, 896, 928, 960, 992, 1024) 14 | MIN_SIZE_TRAIN_SAMPLING: "choice" 15 | MIN_SIZE_TEST: 1024 16 | MAX_SIZE_TRAIN: 2048 17 | MAX_SIZE_TEST: 2048 18 | DATASETS: 19 | TRAIN: ("cityscapes_fine_instance_seg_train",) 20 | TEST: ("cityscapes_fine_instance_seg_val",) 21 | SOLVER: 22 | BASE_LR: 0.01 23 | STEPS: (18000,) 24 | MAX_ITER: 24000 25 | IMS_PER_BATCH: 8 26 | TEST: 27 | EVAL_PERIOD: 8000 28 | -------------------------------------------------------------------------------- /docs/modules/data.rst: -------------------------------------------------------------------------------- 1 | detectron2.data package 2 | ======================= 3 | 4 | .. automodule:: detectron2.data 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | detectron2.data.detection\_utils module 10 | --------------------------------------- 11 | 12 | .. automodule:: detectron2.data.detection_utils 13 | :members: 14 | :undoc-members: 15 | :show-inheritance: 16 | 17 | detectron2.data.datasets module 18 | --------------------------------------- 19 | 20 | .. automodule:: detectron2.data.datasets 21 | :members: 22 | :undoc-members: 23 | :show-inheritance: 24 | 25 | detectron2.data.samplers module 26 | --------------------------------------- 27 | 28 | .. automodule:: detectron2.data.samplers 29 | :members: 30 | :undoc-members: 31 | :show-inheritance: 32 | 33 | 34 | detectron2.data.transforms module 35 | --------------------------------------- 36 | 37 | .. automodule:: detectron2.data.transforms 38 | :members: 39 | :undoc-members: 40 | :show-inheritance: 41 | -------------------------------------------------------------------------------- /configs/quick_schedules/keypoint_rcnn_R_50_FPN_normalized_training_acc_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 4 | KEYPOINT_ON: True 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | BATCH_SIZE_PER_IMAGE: 256 9 | NUM_CLASSES: 1 10 | ROI_KEYPOINT_HEAD: 11 | POOLER_RESOLUTION: 14 12 | POOLER_SAMPLING_RATIO: 2 13 | NORMALIZE_LOSS_BY_VISIBLE_KEYPOINTS: False 14 | LOSS_WEIGHT: 4.0 15 | ROI_BOX_HEAD: 16 | SMOOTH_L1_BETA: 1.0 # Keypoint AP degrades when using plain L1 loss 17 | RPN: 18 | SMOOTH_L1_BETA: 0.2 # Keypoint AP degrades when using plain L1 loss 19 | DATASETS: 20 | TRAIN: ("keypoints_coco_2017_val",) 21 | TEST: ("keypoints_coco_2017_val",) 22 | INPUT: 23 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 24 | SOLVER: 25 | WARMUP_FACTOR: 0.33333333 26 | WARMUP_ITERS: 100 27 | STEPS: (5500, 5800) 28 | MAX_ITER: 6000 29 | TEST: 30 | EXPECTED_RESULTS: [["bbox", "AP", 55.35, 1.0], ["keypoints", "AP", 76.91, 1.0]] 31 | -------------------------------------------------------------------------------- /projects/TridentNet/tridentnet/config.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | from detectron2.config import CfgNode as CN 5 | 6 | 7 | def add_tridentnet_config(cfg): 8 | """ 9 | Add config for tridentnet. 10 | """ 11 | _C = cfg 12 | 13 | _C.MODEL.TRIDENT = CN() 14 | 15 | # Number of branches for TridentNet. 16 | _C.MODEL.TRIDENT.NUM_BRANCH = 3 17 | # Specify the dilations for each branch. 18 | _C.MODEL.TRIDENT.BRANCH_DILATIONS = [1, 2, 3] 19 | # Specify the stage for applying trident blocks. Default stage is Res4 according to the 20 | # TridentNet paper. 21 | _C.MODEL.TRIDENT.TRIDENT_STAGE = "res4" 22 | # Specify the test branch index TridentNet Fast inference: 23 | # - use -1 to aggregate results of all branches during inference. 24 | # - otherwise, only using specified branch for fast inference. Recommended setting is 25 | # to use the middle branch. 26 | _C.MODEL.TRIDENT.TEST_BRANCH_IDX = 1 27 | -------------------------------------------------------------------------------- /detectron2/layers/csrc/box_iou_rotated/box_iou_rotated.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | #pragma once 3 | #include 4 | 5 | namespace detectron2 { 6 | 7 | at::Tensor box_iou_rotated_cpu( 8 | const at::Tensor& boxes1, 9 | const at::Tensor& boxes2); 10 | 11 | #ifdef WITH_CUDA 12 | at::Tensor box_iou_rotated_cuda( 13 | const at::Tensor& boxes1, 14 | const at::Tensor& boxes2); 15 | #endif 16 | 17 | // Interface for Python 18 | // inline is needed to prevent multiple function definitions when this header is 19 | // included by different cpps 20 | inline at::Tensor box_iou_rotated( 21 | const at::Tensor& boxes1, 22 | const at::Tensor& boxes2) { 23 | assert(boxes1.device().is_cuda() == boxes2.device().is_cuda()); 24 | if (boxes1.device().is_cuda()) { 25 | #ifdef WITH_CUDA 26 | return box_iou_rotated_cuda(boxes1, boxes2); 27 | #else 28 | AT_ERROR("Not compiled with GPU support"); 29 | #endif 30 | } 31 | 32 | return box_iou_rotated_cpu(boxes1, boxes2); 33 | } 34 | 35 | } // namespace detectron2 36 | -------------------------------------------------------------------------------- /detectron2/utils/serialize.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import cloudpickle 3 | 4 | 5 | class PicklableWrapper(object): 6 | """ 7 | Wrap an object to make it more picklable, note that it uses 8 | heavy weight serialization libraries that are slower than pickle. 9 | It's best to use it only on closures (which are usually not picklable). 10 | 11 | This is a simplified version of 12 | https://github.com/joblib/joblib/blob/master/joblib/externals/loky/cloudpickle_wrapper.py 13 | """ 14 | 15 | def __init__(self, obj): 16 | self._obj = obj 17 | 18 | def __reduce__(self): 19 | s = cloudpickle.dumps(self._obj) 20 | return cloudpickle.loads, (s,) 21 | 22 | def __call__(self, *args, **kwargs): 23 | return self._obj(*args, **kwargs) 24 | 25 | def __getattr__(self, attr): 26 | # Ensure that the wrapped object can be used seamlessly as the previous object. 27 | if attr not in ["_obj"]: 28 | return getattr(self._obj, attr) 29 | return getattr(self, attr) 30 | -------------------------------------------------------------------------------- /tests/test_model_zoo.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import logging 3 | import unittest 4 | 5 | from detectron2 import model_zoo 6 | from detectron2.modeling import FPN, GeneralizedRCNN 7 | 8 | logger = logging.getLogger(__name__) 9 | 10 | 11 | class TestModelZoo(unittest.TestCase): 12 | def test_get_returns_model(self): 13 | model = model_zoo.get("Misc/scratch_mask_rcnn_R_50_FPN_3x_gn.yaml", trained=False) 14 | self.assertIsInstance(model, GeneralizedRCNN) 15 | self.assertIsInstance(model.backbone, FPN) 16 | 17 | def test_get_invalid_model(self): 18 | self.assertRaises(RuntimeError, model_zoo.get, "Invalid/config.yaml") 19 | 20 | def test_get_url(self): 21 | url = model_zoo.get_checkpoint_url("Misc/scratch_mask_rcnn_R_50_FPN_3x_gn.yaml") 22 | self.assertEqual( 23 | url, 24 | "https://dl.fbaipublicfiles.com/detectron2/Misc/scratch_mask_rcnn_R_50_FPN_3x_gn/138602908/model_final_01ca85.pkl", # noqa 25 | ) 26 | 27 | 28 | if __name__ == "__main__": 29 | unittest.main() 30 | -------------------------------------------------------------------------------- /dev/packaging/build_wheel.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | set -ex 3 | 4 | ldconfig # https://github.com/NVIDIA/nvidia-docker/issues/854 5 | 6 | script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" 7 | . "$script_dir/pkg_helpers.bash" 8 | 9 | echo "Build Settings:" 10 | echo "CU_VERSION: $CU_VERSION" # e.g. cu100 11 | echo "D2_VERSION_SUFFIX: $D2_VERSION_SUFFIX" # e.g. +cu100 or "" 12 | echo "PYTHON_VERSION: $PYTHON_VERSION" # e.g. 3.6 13 | echo "PYTORCH_VERSION: $PYTORCH_VERSION" # e.g. 1.4 14 | 15 | setup_cuda 16 | setup_wheel_python 17 | 18 | export TORCH_VERSION_SUFFIX="+$CU_VERSION" 19 | if [[ "$CU_VERSION" == "cu101" ]]; then 20 | export TORCH_VERSION_SUFFIX="" 21 | fi 22 | pip_install pip numpy -U 23 | pip_install "torch==$PYTORCH_VERSION$TORCH_VERSION_SUFFIX" \ 24 | -f https://download.pytorch.org/whl/$CU_VERSION/torch_stable.html 25 | 26 | # use separate directories to allow parallel build 27 | BASE_BUILD_DIR=build/$CU_VERSION/$PYTHON_VERSION 28 | python setup.py \ 29 | build -b $BASE_BUILD_DIR \ 30 | bdist_wheel -b $BASE_BUILD_DIR/build_dist -d wheels/$CU_VERSION 31 | -------------------------------------------------------------------------------- /detectron2/layers/csrc/nms_rotated/nms_rotated.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | #pragma once 3 | #include 4 | 5 | namespace detectron2 { 6 | 7 | at::Tensor nms_rotated_cpu( 8 | const at::Tensor& dets, 9 | const at::Tensor& scores, 10 | const float iou_threshold); 11 | 12 | #ifdef WITH_CUDA 13 | at::Tensor nms_rotated_cuda( 14 | const at::Tensor& dets, 15 | const at::Tensor& scores, 16 | const float iou_threshold); 17 | #endif 18 | 19 | // Interface for Python 20 | // inline is needed to prevent multiple function definitions when this header is 21 | // included by different cpps 22 | inline at::Tensor nms_rotated( 23 | const at::Tensor& dets, 24 | const at::Tensor& scores, 25 | const float iou_threshold) { 26 | assert(dets.device().is_cuda() == scores.device().is_cuda()); 27 | if (dets.device().is_cuda()) { 28 | #ifdef WITH_CUDA 29 | return nms_rotated_cuda(dets, scores, iou_threshold); 30 | #else 31 | AT_ERROR("Not compiled with GPU support"); 32 | #endif 33 | } 34 | 35 | return nms_rotated_cpu(dets, scores, iou_threshold); 36 | } 37 | 38 | } // namespace detectron2 39 | -------------------------------------------------------------------------------- /detectron2/modeling/backbone/build.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from detectron2.layers import ShapeSpec 3 | from detectron2.utils.registry import Registry 4 | 5 | from .backbone import Backbone 6 | 7 | BACKBONE_REGISTRY = Registry("BACKBONE") 8 | BACKBONE_REGISTRY.__doc__ = """ 9 | Registry for backbones, which extract feature maps from images 10 | 11 | The registered object must be a callable that accepts two arguments: 12 | 13 | 1. A :class:`detectron2.config.CfgNode` 14 | 2. A :class:`detectron2.layers.ShapeSpec`, which contains the input shape specification. 15 | 16 | It must returns an instance of :class:`Backbone`. 17 | """ 18 | 19 | 20 | def build_backbone(cfg, input_shape=None): 21 | """ 22 | Build a backbone from `cfg.MODEL.BACKBONE.NAME`. 23 | 24 | Returns: 25 | an instance of :class:`Backbone` 26 | """ 27 | if input_shape is None: 28 | input_shape = ShapeSpec(channels=len(cfg.MODEL.PIXEL_MEAN)) 29 | 30 | backbone_name = cfg.MODEL.BACKBONE.NAME 31 | backbone = BACKBONE_REGISTRY.get(backbone_name)(cfg, input_shape) 32 | assert isinstance(backbone, Backbone) 33 | return backbone 34 | -------------------------------------------------------------------------------- /projects/TensorMask/tests/test_swap_align2nat.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import unittest 3 | import torch 4 | from torch.autograd import gradcheck 5 | 6 | from tensormask.layers.swap_align2nat import SwapAlign2Nat 7 | 8 | 9 | class SwapAlign2NatTest(unittest.TestCase): 10 | @unittest.skipIf(not torch.cuda.is_available(), "CUDA not available") 11 | def test_swap_align2nat_gradcheck_cuda(self): 12 | dtype = torch.float64 13 | device = torch.device("cuda") 14 | m = SwapAlign2Nat(2).to(dtype=dtype, device=device) 15 | x = torch.rand(2, 4, 10, 10, dtype=dtype, device=device, requires_grad=True) 16 | 17 | self.assertTrue(gradcheck(m, x), "gradcheck failed for SwapAlign2Nat CUDA") 18 | 19 | def _swap_align2nat(self, tensor, lambda_val): 20 | """ 21 | The basic setup for testing Swap_Align 22 | """ 23 | op = SwapAlign2Nat(lambda_val, pad_val=0.0) 24 | input = torch.from_numpy(tensor[None, :, :, :].astype("float32")) 25 | output = op.forward(input.cuda()).cpu().numpy() 26 | return output[0] 27 | 28 | 29 | if __name__ == "__main__": 30 | unittest.main() 31 | -------------------------------------------------------------------------------- /dev/linter.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | # Run this script at project root by "./dev/linter.sh" before you commit 5 | 6 | vergte() { 7 | [ "$2" = "$(echo -e "$1\n$2" | sort -V | head -n1)" ] 8 | } 9 | 10 | { 11 | black --version | grep "19.3b0" > /dev/null 12 | } || { 13 | echo "Linter requires black==19.3b0 !" 14 | exit 1 15 | } 16 | 17 | ISORT_TARGET_VERSION="4.3.21" 18 | ISORT_VERSION=$(isort -v | grep VERSION | awk '{print $2}') 19 | vergte "$ISORT_VERSION" "$ISORT_TARGET_VERSION" || { 20 | echo "Linter requires isort>=${ISORT_TARGET_VERSION} !" 21 | exit 1 22 | } 23 | 24 | set -v 25 | 26 | echo "Running isort ..." 27 | isort -y -sp . --atomic 28 | 29 | echo "Running black ..." 30 | black -l 100 . 31 | 32 | echo "Running flake8 ..." 33 | if [ -x "$(command -v flake8-3)" ]; then 34 | flake8-3 . 35 | else 36 | python3 -m flake8 . 37 | fi 38 | 39 | # echo "Running mypy ..." 40 | # Pytorch does not have enough type annotations 41 | # mypy detectron2/solver detectron2/structures detectron2/config 42 | 43 | echo "Running clang-format ..." 44 | find . -regex ".*\.\(cpp\|c\|cc\|cu\|cxx\|h\|hh\|hpp\|hxx\|tcc\|mm\|m\)" -print0 | xargs -0 clang-format -i 45 | 46 | command -v arc > /dev/null && arc lint 47 | -------------------------------------------------------------------------------- /projects/TridentNet/tridentnet/trident_rpn.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import torch 3 | 4 | from detectron2.modeling import PROPOSAL_GENERATOR_REGISTRY 5 | from detectron2.modeling.proposal_generator.rpn import RPN 6 | from detectron2.structures import ImageList 7 | 8 | 9 | @PROPOSAL_GENERATOR_REGISTRY.register() 10 | class TridentRPN(RPN): 11 | """ 12 | Trident RPN subnetwork. 13 | """ 14 | 15 | def __init__(self, cfg, input_shape): 16 | super(TridentRPN, self).__init__(cfg, input_shape) 17 | 18 | self.num_branch = cfg.MODEL.TRIDENT.NUM_BRANCH 19 | self.trident_fast = cfg.MODEL.TRIDENT.TEST_BRANCH_IDX != -1 20 | 21 | def forward(self, images, features, gt_instances=None): 22 | """ 23 | See :class:`RPN.forward`. 24 | """ 25 | num_branch = self.num_branch if self.training or not self.trident_fast else 1 26 | # Duplicate images and gt_instances for all branches in TridentNet. 27 | all_images = ImageList( 28 | torch.cat([images.tensor] * num_branch), images.image_sizes * num_branch 29 | ) 30 | all_gt_instances = gt_instances * num_branch if gt_instances is not None else None 31 | 32 | return super(TridentRPN, self).forward(all_images, features, all_gt_instances) 33 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature-request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "\U0001F680Feature Request" 3 | about: Submit a proposal/request for a new detectron2 feature 4 | 5 | --- 6 | 7 | ## 🚀 Feature 8 | 9 | 10 | ## Motivation 11 | 12 | 14 | 15 | ## Pitch 16 | 17 | 18 | 19 | Note that we only consider adding new features if they are relevant to many users. 20 | We do not take requests to implement recent research papers -- 21 | we only consider papers that have enough significance and prevalance. 22 | 23 | We don't take feature requests for most projects in the `projects/` directory, 24 | because they are research code release that is mainly for other researchers to reproduce results. 25 | 26 | Instead of adding features inside detectron2, 27 | you can implement many features by [extending detectron2](https://detectron2.readthedocs.io/tutorials/extend.html). 28 | The [projects/](https://github.com/facebookresearch/detectron2/tree/master/projects/) directory 29 | contains many of such examples. 30 | 31 | -------------------------------------------------------------------------------- /tools/README.md: -------------------------------------------------------------------------------- 1 | 2 | This directory contains a few scripts that use detectron2. 3 | 4 | 5 | * `train_net.py` 6 | 7 | An example training script that's made to train builtin models of detectron2. 8 | 9 | For usage, see [GETTING_STARTED.md](../GETTING_STARTED.md). 10 | 11 | * `plain_train_net.py` 12 | 13 | Similar to `train_net.py`, but implements a training loop instead of using `Trainer`. 14 | This script includes fewer features but it may be more friendly to hackers. 15 | 16 | * `benchmark.py` 17 | 18 | Benchmark the training speed, inference speed or data loading speed of a given config. 19 | 20 | Usage: 21 | ``` 22 | python benchmark.py --config-file config.yaml --task train/eval/data [optional DDP flags] 23 | ``` 24 | 25 | * `visualize_json_results.py` 26 | 27 | Visualize the json instance detection/segmentation results dumped by `COCOEvalutor` or `LVISEvaluator` 28 | 29 | Usage: 30 | ``` 31 | python visualize_json_results.py --input x.json --output dir/ --dataset coco_2017_val 32 | ``` 33 | If not using a builtin dataset, you'll need your own script or modify this script. 34 | 35 | * `visualize_data.py` 36 | 37 | Visualize ground truth raw annotations or training data (after preprocessing/augmentations). 38 | 39 | Usage: 40 | ``` 41 | python visualize_data.py --config-file config.yaml --source annotation/dataloader --output-dir dir/ [--show] 42 | ``` 43 | 44 | NOTE: the script does not stop by itself when using `--source dataloader` because a training 45 | dataloader is usually infinite. 46 | -------------------------------------------------------------------------------- /configs/Base-RCNN-FPN.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "GeneralizedRCNN" 3 | BACKBONE: 4 | NAME: "build_resnet_fpn_backbone" 5 | RESNETS: 6 | OUT_FEATURES: ["res2", "res3", "res4", "res5"] 7 | FPN: 8 | IN_FEATURES: ["res2", "res3", "res4", "res5"] 9 | ANCHOR_GENERATOR: 10 | SIZES: [[32], [64], [128], [256], [512]] # One size for each in feature map 11 | ASPECT_RATIOS: [[0.5, 1.0, 2.0]] # Three aspect ratios (same for all in feature maps) 12 | RPN: 13 | IN_FEATURES: ["p2", "p3", "p4", "p5", "p6"] 14 | PRE_NMS_TOPK_TRAIN: 2000 # Per FPN level 15 | PRE_NMS_TOPK_TEST: 1000 # Per FPN level 16 | # Detectron1 uses 2000 proposals per-batch, 17 | # (See "modeling/rpn/rpn_outputs.py" for details of this legacy issue) 18 | # which is approximately 1000 proposals per-image since the default batch size for FPN is 2. 19 | POST_NMS_TOPK_TRAIN: 1000 20 | POST_NMS_TOPK_TEST: 1000 21 | ROI_HEADS: 22 | NAME: "StandardROIHeads" 23 | IN_FEATURES: ["p2", "p3", "p4", "p5"] 24 | ROI_BOX_HEAD: 25 | NAME: "FastRCNNConvFCHead" 26 | NUM_FC: 2 27 | POOLER_RESOLUTION: 7 28 | ROI_MASK_HEAD: 29 | NAME: "MaskRCNNConvUpsampleHead" 30 | NUM_CONV: 4 31 | POOLER_RESOLUTION: 14 32 | DATASETS: 33 | TRAIN: ("coco_2017_train",) 34 | TEST: ("coco_2017_val",) 35 | SOLVER: 36 | IMS_PER_BATCH: 16 37 | BASE_LR: 0.02 38 | STEPS: (60000, 80000) 39 | MAX_ITER: 90000 40 | INPUT: 41 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 42 | VERSION: 2 43 | -------------------------------------------------------------------------------- /dev/run_inference_tests.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | BIN="python tools/train_net.py" 5 | OUTPUT="inference_test_output" 6 | NUM_GPUS=2 7 | 8 | CFG_LIST=( "${@:1}" ) 9 | 10 | if [ ${#CFG_LIST[@]} -eq 0 ]; then 11 | CFG_LIST=( ./configs/quick_schedules/*inference_acc_test.yaml ) 12 | fi 13 | 14 | echo "========================================================================" 15 | echo "Configs to run:" 16 | echo "${CFG_LIST[@]}" 17 | echo "========================================================================" 18 | 19 | 20 | for cfg in "${CFG_LIST[@]}"; do 21 | echo "========================================================================" 22 | echo "Running $cfg ..." 23 | echo "========================================================================" 24 | $BIN \ 25 | --eval-only \ 26 | --num-gpus $NUM_GPUS \ 27 | --config-file "$cfg" \ 28 | OUTPUT_DIR $OUTPUT 29 | rm -rf $OUTPUT 30 | done 31 | 32 | 33 | echo "========================================================================" 34 | echo "Running demo.py ..." 35 | echo "========================================================================" 36 | DEMO_BIN="python demo/demo.py" 37 | COCO_DIR=datasets/coco/val2014 38 | mkdir -pv $OUTPUT 39 | 40 | set -v 41 | 42 | $DEMO_BIN --config-file ./configs/quick_schedules/panoptic_fpn_R_50_inference_acc_test.yaml \ 43 | --input $COCO_DIR/COCO_val2014_0000001933* --output $OUTPUT 44 | rm -rf $OUTPUT 45 | -------------------------------------------------------------------------------- /projects/DensePose/README.md: -------------------------------------------------------------------------------- 1 | # DensePose in Detectron2 2 | **Dense Human Pose Estimation In The Wild** 3 | 4 | _Rıza Alp Güler, Natalia Neverova, Iasonas Kokkinos_ 5 | 6 | [[`densepose.org`](https://densepose.org)] [[`arXiv`](https://arxiv.org/abs/1802.00434)] [[`BibTeX`](#CitingDensePose)] 7 | 8 | Dense human pose estimation aims at mapping all human pixels of an RGB image to the 3D surface of the human body. 9 | 10 |
11 | 12 |
13 | 14 | In this repository, we provide the code to train and evaluate DensePose-RCNN. We also provide tools to visualize 15 | DensePose annotation and results. 16 | 17 | # Quick Start 18 | 19 | See [ Getting Started ](doc/GETTING_STARTED.md) 20 | 21 | # Model Zoo and Baselines 22 | 23 | We provide a number of baseline results and trained models available for download. See [Model Zoo](doc/MODEL_ZOO.md) for details. 24 | 25 | # License 26 | 27 | Detectron2 is released under the [Apache 2.0 license](../../LICENSE) 28 | 29 | ## Citing DensePose 30 | 31 | If you use DensePose, please use the following BibTeX entry. 32 | 33 | ``` 34 | @InProceedings{Guler2018DensePose, 35 | title={DensePose: Dense Human Pose Estimation In The Wild}, 36 | author={R\{i}za Alp G\"uler, Natalia Neverova, Iasonas Kokkinos}, 37 | journal={The IEEE Conference on Computer Vision and Pattern Recognition (CVPR)}, 38 | year={2018} 39 | } 40 | ``` 41 | 42 | -------------------------------------------------------------------------------- /projects/TensorMask/tensormask/layers/csrc/SwapAlign2Nat/SwapAlign2Nat.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | #pragma once 3 | #include 4 | 5 | namespace tensormask { 6 | 7 | #ifdef WITH_CUDA 8 | at::Tensor SwapAlign2Nat_forward_cuda( 9 | const at::Tensor& X, 10 | const int lambda_val, 11 | const float pad_val); 12 | 13 | at::Tensor SwapAlign2Nat_backward_cuda( 14 | const at::Tensor& gY, 15 | const int lambda_val, 16 | const int batch_size, 17 | const int channel, 18 | const int height, 19 | const int width); 20 | #endif 21 | 22 | inline at::Tensor SwapAlign2Nat_forward( 23 | const at::Tensor& X, 24 | const int lambda_val, 25 | const float pad_val) { 26 | if (X.type().is_cuda()) { 27 | #ifdef WITH_CUDA 28 | return SwapAlign2Nat_forward_cuda(X, lambda_val, pad_val); 29 | #else 30 | AT_ERROR("Not compiled with GPU support"); 31 | #endif 32 | } 33 | AT_ERROR("Not implemented on the CPU"); 34 | } 35 | 36 | inline at::Tensor SwapAlign2Nat_backward( 37 | const at::Tensor& gY, 38 | const int lambda_val, 39 | const int batch_size, 40 | const int channel, 41 | const int height, 42 | const int width) { 43 | if (gY.type().is_cuda()) { 44 | #ifdef WITH_CUDA 45 | return SwapAlign2Nat_backward_cuda( 46 | gY, lambda_val, batch_size, channel, height, width); 47 | #else 48 | AT_ERROR("Not compiled with GPU support"); 49 | #endif 50 | } 51 | AT_ERROR("Not implemented on the CPU"); 52 | } 53 | 54 | } // namespace tensormask 55 | -------------------------------------------------------------------------------- /projects/EQL/configs/Base-EQL-RCNN-FPN.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "GeneralizedRCNN" 3 | BACKBONE: 4 | NAME: "build_resnet_fpn_backbone" 5 | RESNETS: 6 | OUT_FEATURES: ["res2", "res3", "res4", "res5"] 7 | FPN: 8 | IN_FEATURES: ["res2", "res3", "res4", "res5"] 9 | ANCHOR_GENERATOR: 10 | SIZES: [[32], [64], [128], [256], [512]] # One size for each in feature map 11 | ASPECT_RATIOS: [[0.5, 1.0, 2.0]] # Three aspect ratios (same for all in feature maps) 12 | RPN: 13 | IN_FEATURES: ["p2", "p3", "p4", "p5", "p6"] 14 | PRE_NMS_TOPK_TRAIN: 2000 # Per FPN level 15 | PRE_NMS_TOPK_TEST: 1000 # Per FPN level 16 | # Detectron1 uses 2000 proposals per-batch, 17 | # (See "modeling/rpn/rpn_outputs.py" for details of this legacy issue) 18 | # which is approximately 1000 proposals per-image since the default batch size for FPN is 2. 19 | POST_NMS_TOPK_TRAIN: 1000 20 | POST_NMS_TOPK_TEST: 1000 21 | ROI_HEADS: 22 | NAME: "EQLROIHeads" 23 | IN_FEATURES: ["p2", "p3", "p4", "p5"] 24 | LAMBDA: 0.00177 25 | PRIOR_PROB: 0.001 26 | ROI_BOX_HEAD: 27 | NAME: "FastRCNNConvFCHead" 28 | NUM_FC: 2 29 | POOLER_RESOLUTION: 7 30 | ROI_MASK_HEAD: 31 | NAME: "MaskRCNNConvUpsampleHead" 32 | NUM_CONV: 4 33 | POOLER_RESOLUTION: 14 34 | DATASETS: 35 | TRAIN: ("coco_2017_train",) 36 | TEST: ("coco_2017_val",) 37 | SOLVER: 38 | IMS_PER_BATCH: 16 39 | BASE_LR: 0.02 40 | STEPS: (60000, 80000) 41 | MAX_ITER: 90000 42 | INPUT: 43 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 44 | VERSION: 2 45 | -------------------------------------------------------------------------------- /dev/packaging/build_all_wheels.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | PYTORCH_VERSION=1.4 4 | 5 | build_for_one_cuda() { 6 | cu=$1 7 | 8 | case "$cu" in 9 | cu*) 10 | container_name=manylinux-cuda${cu/cu/} 11 | ;; 12 | cpu) 13 | container_name=manylinux-cuda101 14 | ;; 15 | *) 16 | echo "Unrecognized cu=$cu" 17 | exit 1 18 | ;; 19 | esac 20 | 21 | echo "Launching container $container_name ..." 22 | 23 | for py in 3.6 3.7 3.8; do 24 | docker run -itd \ 25 | --name $container_name \ 26 | --mount type=bind,source="$(pwd)",target=/detectron2 \ 27 | pytorch/$container_name 28 | 29 | cat < 8 | void box_iou_rotated_cpu_kernel( 9 | const at::Tensor& boxes1, 10 | const at::Tensor& boxes2, 11 | at::Tensor& ious) { 12 | auto widths1 = boxes1.select(1, 2).contiguous(); 13 | auto heights1 = boxes1.select(1, 3).contiguous(); 14 | auto widths2 = boxes2.select(1, 2).contiguous(); 15 | auto heights2 = boxes2.select(1, 3).contiguous(); 16 | 17 | at::Tensor areas1 = widths1 * heights1; 18 | at::Tensor areas2 = widths2 * heights2; 19 | 20 | auto num_boxes1 = boxes1.size(0); 21 | auto num_boxes2 = boxes2.size(0); 22 | 23 | for (int i = 0; i < num_boxes1; i++) { 24 | for (int j = 0; j < num_boxes2; j++) { 25 | ious[i * num_boxes2 + j] = single_box_iou_rotated( 26 | boxes1[i].data_ptr(), boxes2[j].data_ptr()); 27 | } 28 | } 29 | } 30 | 31 | at::Tensor box_iou_rotated_cpu( 32 | const at::Tensor& boxes1, 33 | const at::Tensor& boxes2) { 34 | auto num_boxes1 = boxes1.size(0); 35 | auto num_boxes2 = boxes2.size(0); 36 | at::Tensor ious = 37 | at::empty({num_boxes1 * num_boxes2}, boxes1.options().dtype(at::kFloat)); 38 | 39 | box_iou_rotated_cpu_kernel(boxes1, boxes2, ious); 40 | 41 | // reshape from 1d array to 2d array 42 | auto shape = std::vector{num_boxes1, num_boxes2}; 43 | return ious.reshape(shape); 44 | } 45 | 46 | } // namespace detectron2 47 | -------------------------------------------------------------------------------- /dev/packaging/pkg_helpers.bash: -------------------------------------------------------------------------------- 1 | #!/bin/bash -e 2 | 3 | # Function to retry functions that sometimes timeout or have flaky failures 4 | retry () { 5 | $* || (sleep 1 && $*) || (sleep 2 && $*) || (sleep 4 && $*) || (sleep 8 && $*) 6 | } 7 | # Install with pip a bit more robustly than the default 8 | pip_install() { 9 | retry pip install --progress-bar off "$@" 10 | } 11 | 12 | 13 | setup_cuda() { 14 | # Now work out the CUDA settings 15 | # Like other torch domain libraries, we choose common GPU architectures only. 16 | export FORCE_CUDA=1 17 | case "$CU_VERSION" in 18 | cu101) 19 | export CUDA_HOME=/usr/local/cuda-10.1/ 20 | export TORCH_CUDA_ARCH_LIST="3.5;3.7;5.0;5.2;6.0+PTX;6.1+PTX;7.0+PTX;7.5+PTX" 21 | ;; 22 | cu100) 23 | export CUDA_HOME=/usr/local/cuda-10.0/ 24 | export TORCH_CUDA_ARCH_LIST="3.5;3.7;5.0;5.2;6.0+PTX;6.1+PTX;7.0+PTX;7.5+PTX" 25 | ;; 26 | cu92) 27 | export CUDA_HOME=/usr/local/cuda-9.2/ 28 | export TORCH_CUDA_ARCH_LIST="3.5;3.7;5.0;5.2;6.0+PTX;6.1+PTX;7.0+PTX" 29 | ;; 30 | cpu) 31 | unset FORCE_CUDA 32 | export CUDA_VISIBLE_DEVICES= 33 | ;; 34 | *) 35 | echo "Unrecognized CU_VERSION=$CU_VERSION" 36 | exit 1 37 | ;; 38 | esac 39 | } 40 | 41 | setup_wheel_python() { 42 | case "$PYTHON_VERSION" in 43 | 3.6) python_abi=cp36-cp36m ;; 44 | 3.7) python_abi=cp37-cp37m ;; 45 | 3.8) python_abi=cp38-cp38 ;; 46 | *) 47 | echo "Unrecognized PYTHON_VERSION=$PYTHON_VERSION" 48 | exit 1 49 | ;; 50 | esac 51 | export PATH="/opt/python/$python_abi/bin:$PATH" 52 | } 53 | -------------------------------------------------------------------------------- /tests/test_config.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | 5 | import os 6 | import tempfile 7 | import unittest 8 | 9 | from detectron2.config import downgrade_config, get_cfg, upgrade_config 10 | 11 | _V0_CFG = """ 12 | MODEL: 13 | RPN_HEAD: 14 | NAME: "TEST" 15 | VERSION: 0 16 | """ 17 | 18 | _V1_CFG = """ 19 | MODEL: 20 | WEIGHT: "/path/to/weight" 21 | """ 22 | 23 | 24 | class TestConfigVersioning(unittest.TestCase): 25 | def test_upgrade_downgrade_consistency(self): 26 | cfg = get_cfg() 27 | # check that custom is preserved 28 | cfg.USER_CUSTOM = 1 29 | 30 | down = downgrade_config(cfg, to_version=0) 31 | up = upgrade_config(down) 32 | self.assertTrue(up == cfg) 33 | 34 | def _merge_cfg_str(self, cfg, merge_str): 35 | f = tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) 36 | try: 37 | f.write(merge_str) 38 | f.close() 39 | cfg.merge_from_file(f.name) 40 | finally: 41 | os.remove(f.name) 42 | return cfg 43 | 44 | def test_auto_upgrade(self): 45 | cfg = get_cfg() 46 | latest_ver = cfg.VERSION 47 | cfg.USER_CUSTOM = 1 48 | 49 | self._merge_cfg_str(cfg, _V0_CFG) 50 | 51 | self.assertEqual(cfg.MODEL.RPN.HEAD_NAME, "TEST") 52 | self.assertEqual(cfg.VERSION, latest_ver) 53 | 54 | def test_guess_v1(self): 55 | cfg = get_cfg() 56 | latest_ver = cfg.VERSION 57 | self._merge_cfg_str(cfg, _V1_CFG) 58 | self.assertEqual(cfg.VERSION, latest_ver) 59 | -------------------------------------------------------------------------------- /detectron2/modeling/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import torch 3 | 4 | from detectron2.layers import ShapeSpec 5 | 6 | from .anchor_generator import build_anchor_generator, ANCHOR_GENERATOR_REGISTRY 7 | from .backbone import ( 8 | BACKBONE_REGISTRY, 9 | FPN, 10 | Backbone, 11 | ResNet, 12 | ResNetBlockBase, 13 | build_backbone, 14 | build_resnet_backbone, 15 | make_stage, 16 | ) 17 | from .meta_arch import ( 18 | META_ARCH_REGISTRY, 19 | SEM_SEG_HEADS_REGISTRY, 20 | GeneralizedRCNN, 21 | PanopticFPN, 22 | ProposalNetwork, 23 | RetinaNet, 24 | SemanticSegmentor, 25 | build_model, 26 | build_sem_seg_head, 27 | ) 28 | from .postprocessing import detector_postprocess 29 | from .proposal_generator import ( 30 | PROPOSAL_GENERATOR_REGISTRY, 31 | build_proposal_generator, 32 | RPN_HEAD_REGISTRY, 33 | build_rpn_head, 34 | ) 35 | from .roi_heads import ( 36 | ROI_BOX_HEAD_REGISTRY, 37 | ROI_HEADS_REGISTRY, 38 | ROI_KEYPOINT_HEAD_REGISTRY, 39 | ROI_MASK_HEAD_REGISTRY, 40 | ROIHeads, 41 | StandardROIHeads, 42 | BaseMaskRCNNHead, 43 | BaseKeypointRCNNHead, 44 | build_box_head, 45 | build_keypoint_head, 46 | build_mask_head, 47 | build_roi_heads, 48 | ) 49 | from .test_time_augmentation import DatasetMapperTTA, GeneralizedRCNNWithTTA 50 | 51 | _EXCLUDE = {"torch", "ShapeSpec"} 52 | __all__ = [k for k in globals().keys() if k not in _EXCLUDE and not k.startswith("_")] 53 | 54 | assert ( 55 | torch.Tensor([1]) == torch.Tensor([2]) 56 | ).dtype == torch.bool, "Your Pytorch is too old. Please update to contain https://github.com/pytorch/pytorch/pull/21113" 57 | -------------------------------------------------------------------------------- /projects/DensePose/densepose/vis/bounding_box.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from .base import RectangleVisualizer, TextVisualizer 3 | 4 | 5 | class BoundingBoxVisualizer(object): 6 | def __init__(self): 7 | self.rectangle_visualizer = RectangleVisualizer() 8 | 9 | def visualize(self, image_bgr, boxes_xywh): 10 | for bbox_xywh in boxes_xywh: 11 | image_bgr = self.rectangle_visualizer.visualize(image_bgr, bbox_xywh) 12 | return image_bgr 13 | 14 | 15 | class ScoredBoundingBoxVisualizer(object): 16 | def __init__(self, bbox_visualizer_params=None, score_visualizer_params=None): 17 | if bbox_visualizer_params is None: 18 | bbox_visualizer_params = {} 19 | if score_visualizer_params is None: 20 | score_visualizer_params = {} 21 | self.visualizer_bbox = RectangleVisualizer(**bbox_visualizer_params) 22 | self.visualizer_score = TextVisualizer(**score_visualizer_params) 23 | 24 | def visualize(self, image_bgr, scored_bboxes): 25 | boxes_xywh, box_scores = scored_bboxes 26 | assert len(boxes_xywh) == len(box_scores), ( 27 | "Number of bounding boxes {} should be equal to the number of " 28 | "scores".format(len(boxes_xywh), len(box_scores)) 29 | ) 30 | for i, box_xywh in enumerate(boxes_xywh): 31 | score_i = box_scores[i] 32 | image_bgr = self.visualizer_bbox.visualize(image_bgr, box_xywh) 33 | score_txt = "{0:6.4f}".format(score_i) 34 | topleft_xy = box_xywh[0], box_xywh[1] 35 | image_bgr = self.visualizer_score.visualize(image_bgr, score_txt, topleft_xy) 36 | return image_bgr 37 | -------------------------------------------------------------------------------- /projects/DensePose/configs/Base-DensePose-RCNN-FPN.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "GeneralizedRCNN" 3 | BACKBONE: 4 | NAME: "build_resnet_fpn_backbone" 5 | RESNETS: 6 | OUT_FEATURES: ["res2", "res3", "res4", "res5"] 7 | FPN: 8 | IN_FEATURES: ["res2", "res3", "res4", "res5"] 9 | ANCHOR_GENERATOR: 10 | SIZES: [[32], [64], [128], [256], [512]] # One size for each in feature map 11 | ASPECT_RATIOS: [[0.5, 1.0, 2.0]] # Three aspect ratios (same for all in feature maps) 12 | RPN: 13 | IN_FEATURES: ["p2", "p3", "p4", "p5", "p6"] 14 | PRE_NMS_TOPK_TRAIN: 2000 # Per FPN level 15 | PRE_NMS_TOPK_TEST: 1000 # Per FPN level 16 | # Detectron1 uses 2000 proposals per-batch, 17 | # (See "modeling/rpn/rpn_outputs.py" for details of this legacy issue) 18 | # which is approximately 1000 proposals per-image since the default batch size for FPN is 2. 19 | POST_NMS_TOPK_TRAIN: 1000 20 | POST_NMS_TOPK_TEST: 1000 21 | 22 | DENSEPOSE_ON: True 23 | ROI_HEADS: 24 | NAME: "DensePoseROIHeads" 25 | IN_FEATURES: ["p2", "p3", "p4", "p5"] 26 | NUM_CLASSES: 1 27 | ROI_BOX_HEAD: 28 | NAME: "FastRCNNConvFCHead" 29 | NUM_FC: 2 30 | POOLER_RESOLUTION: 7 31 | POOLER_SAMPLING_RATIO: 2 32 | POOLER_TYPE: "ROIAlign" 33 | ROI_DENSEPOSE_HEAD: 34 | NAME: "DensePoseV1ConvXHead" 35 | POOLER_TYPE: "ROIAlign" 36 | NUM_COARSE_SEGM_CHANNELS: 2 37 | DATASETS: 38 | TRAIN: ("densepose_coco_2014_train", "densepose_coco_2014_valminusminival") 39 | TEST: ("densepose_coco_2014_minival",) 40 | SOLVER: 41 | IMS_PER_BATCH: 16 42 | BASE_LR: 0.01 43 | STEPS: (60000, 80000) 44 | MAX_ITER: 90000 45 | WARMUP_FACTOR: 0.1 46 | INPUT: 47 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 48 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/unexpected-problems-bugs.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: "Unexpected behaviors / Bugs" 3 | about: Report unexpected behaviors or bugs in detectron2 4 | title: Please read & provide the following 5 | 6 | --- 7 | 8 | If you do not know the root cause of the problem / bug, and wish someone to help you, please 9 | post according to this template: 10 | 11 | ## Instructions To Reproduce the Issue: 12 | 13 | 1. what changes you made (`git diff`) or what code you wrote 14 | ``` 15 | 16 | ``` 17 | 2. what exact command you run: 18 | 3. what you observed (including the full logs): 19 | ``` 20 | 21 | ``` 22 | 4. please also simplify the steps as much as possible so they do not require additional resources to 23 | run, such as a private dataset. 24 | 25 | ## Expected behavior: 26 | 27 | If there are no obvious error in "what you observed" provided above, 28 | please tell us the expected behavior. 29 | 30 | If you expect the model to converge / work better, note that we do not give suggestions 31 | on how to train a new model. 32 | Only in one of the two conditions we will help with it: 33 | (1) You're unable to reproduce the results in detectron2 model zoo. 34 | (2) It indicates a detectron2 bug. 35 | 36 | ## Environment: 37 | 38 | Run `python -m detectron2.utils.collect_env` in the environment where you observerd the issue, and paste the output. 39 | If detectron2 hasn't been successfully installed, use `python detectron2/utils/collect_env.py` (after getting this file from github). 40 | 41 | If your issue looks like an installation issue / environment issue, 42 | please first try to solve it yourself with the instructions in 43 | https://github.com/facebookresearch/detectron2/blob/master/INSTALL.md#common-installation-issues 44 | -------------------------------------------------------------------------------- /detectron2/modeling/backbone/backbone.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from abc import ABCMeta, abstractmethod 3 | import torch.nn as nn 4 | 5 | from detectron2.layers import ShapeSpec 6 | 7 | __all__ = ["Backbone"] 8 | 9 | 10 | class Backbone(nn.Module, metaclass=ABCMeta): 11 | """ 12 | Abstract base class for network backbones. 13 | """ 14 | 15 | def __init__(self): 16 | """ 17 | The `__init__` method of any subclass can specify its own set of arguments. 18 | """ 19 | super().__init__() 20 | 21 | @abstractmethod 22 | def forward(self): 23 | """ 24 | Subclasses must override this method, but adhere to the same return type. 25 | 26 | Returns: 27 | dict[str->Tensor]: mapping from feature name (e.g., "res2") to tensor 28 | """ 29 | pass 30 | 31 | @property 32 | def size_divisibility(self): 33 | """ 34 | Some backbones require the input height and width to be divisible by a 35 | specific integer. This is typically true for encoder / decoder type networks 36 | with lateral connection (e.g., FPN) for which feature maps need to match 37 | dimension in the "bottom up" and "top down" paths. Set to 0 if no specific 38 | input size divisibility is required. 39 | """ 40 | return 0 41 | 42 | def output_shape(self): 43 | """ 44 | Returns: 45 | dict[str->ShapeSpec] 46 | """ 47 | # this is a backward-compatible default 48 | return { 49 | name: ShapeSpec( 50 | channels=self._out_feature_channels[name], stride=self._out_feature_strides[name] 51 | ) 52 | for name in self._out_features 53 | } 54 | -------------------------------------------------------------------------------- /tests/test_checkpoint.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import unittest 3 | from collections import OrderedDict 4 | import torch 5 | from torch import nn 6 | 7 | from detectron2.checkpoint.c2_model_loading import align_and_update_state_dicts 8 | from detectron2.utils.logger import setup_logger 9 | 10 | 11 | class TestCheckpointer(unittest.TestCase): 12 | def setUp(self): 13 | setup_logger() 14 | 15 | def create_complex_model(self): 16 | m = nn.Module() 17 | m.block1 = nn.Module() 18 | m.block1.layer1 = nn.Linear(2, 3) 19 | m.layer2 = nn.Linear(3, 2) 20 | m.res = nn.Module() 21 | m.res.layer2 = nn.Linear(3, 2) 22 | 23 | state_dict = OrderedDict() 24 | state_dict["layer1.weight"] = torch.rand(3, 2) 25 | state_dict["layer1.bias"] = torch.rand(3) 26 | state_dict["layer2.weight"] = torch.rand(2, 3) 27 | state_dict["layer2.bias"] = torch.rand(2) 28 | state_dict["res.layer2.weight"] = torch.rand(2, 3) 29 | state_dict["res.layer2.bias"] = torch.rand(2) 30 | return m, state_dict 31 | 32 | def test_complex_model_loaded(self): 33 | for add_data_parallel in [False, True]: 34 | model, state_dict = self.create_complex_model() 35 | if add_data_parallel: 36 | model = nn.DataParallel(model) 37 | model_sd = model.state_dict() 38 | 39 | align_and_update_state_dicts(model_sd, state_dict) 40 | for loaded, stored in zip(model_sd.values(), state_dict.values()): 41 | # different tensor references 42 | self.assertFalse(id(loaded) == id(stored)) 43 | # same content 44 | self.assertTrue(loaded.equal(stored)) 45 | 46 | 47 | if __name__ == "__main__": 48 | unittest.main() 49 | -------------------------------------------------------------------------------- /projects/TensorMask/tensormask/config.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | from detectron2.config import CfgNode as CN 5 | 6 | 7 | def add_tensormask_config(cfg): 8 | """ 9 | Add config for TensorMask. 10 | """ 11 | cfg.MODEL.TENSOR_MASK = CN() 12 | 13 | # Anchor parameters 14 | cfg.MODEL.TENSOR_MASK.IN_FEATURES = ["p2", "p3", "p4", "p5", "p6", "p7"] 15 | 16 | # Convolutions to use in the towers 17 | cfg.MODEL.TENSOR_MASK.NUM_CONVS = 4 18 | 19 | # Number of foreground classes. 20 | cfg.MODEL.TENSOR_MASK.NUM_CLASSES = 80 21 | # Channel size for the classification tower 22 | cfg.MODEL.TENSOR_MASK.CLS_CHANNELS = 256 23 | 24 | cfg.MODEL.TENSOR_MASK.SCORE_THRESH_TEST = 0.05 25 | # Only the top (1000 * #levels) candidate boxes across all levels are 26 | # considered jointly during test (to improve speed) 27 | cfg.MODEL.TENSOR_MASK.TOPK_CANDIDATES_TEST = 6000 28 | cfg.MODEL.TENSOR_MASK.NMS_THRESH_TEST = 0.5 29 | 30 | # Box parameters 31 | # Channel size for the box tower 32 | cfg.MODEL.TENSOR_MASK.BBOX_CHANNELS = 128 33 | # Weights on (dx, dy, dw, dh) 34 | cfg.MODEL.TENSOR_MASK.BBOX_REG_WEIGHTS = (1.5, 1.5, 0.75, 0.75) 35 | 36 | # Loss parameters 37 | cfg.MODEL.TENSOR_MASK.FOCAL_LOSS_GAMMA = 3.0 38 | cfg.MODEL.TENSOR_MASK.FOCAL_LOSS_ALPHA = 0.3 39 | 40 | # Mask parameters 41 | # Channel size for the mask tower 42 | cfg.MODEL.TENSOR_MASK.MASK_CHANNELS = 128 43 | # Mask loss weight 44 | cfg.MODEL.TENSOR_MASK.MASK_LOSS_WEIGHT = 2.0 45 | # weight on positive pixels within the mask 46 | cfg.MODEL.TENSOR_MASK.POSITIVE_WEIGHT = 1.5 47 | # Whether to predict in the aligned representation 48 | cfg.MODEL.TENSOR_MASK.ALIGNED_ON = False 49 | # Whether to use the bipyramid architecture 50 | cfg.MODEL.TENSOR_MASK.BIPYRAMID_ON = False 51 | -------------------------------------------------------------------------------- /projects/DensePose/densepose/dataset.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import os 3 | 4 | from detectron2.data import DatasetCatalog, MetadataCatalog 5 | from detectron2.data.datasets import load_coco_json 6 | 7 | _URL_PREFIX = "https://dl.fbaipublicfiles.com/densepose/data/" 8 | 9 | 10 | def get_densepose_metadata(): 11 | meta = { 12 | "thing_classes": ["person"], 13 | "densepose_transform_src": _URL_PREFIX + "UV_symmetry_transforms.mat", 14 | "densepose_smpl_subdiv": _URL_PREFIX + "SMPL_subdiv.mat", 15 | "densepose_smpl_subdiv_transform": _URL_PREFIX + "SMPL_SUBDIV_TRANSFORM.mat", 16 | } 17 | return meta 18 | 19 | 20 | SPLITS = { 21 | "densepose_coco_2014_train": ("coco/train2014", "coco/annotations/densepose_train2014.json"), 22 | "densepose_coco_2014_minival": ("coco/val2014", "coco/annotations/densepose_minival2014.json"), 23 | "densepose_coco_2014_minival_100": ( 24 | "coco/val2014", 25 | "coco/annotations/densepose_minival2014_100.json", 26 | ), 27 | "densepose_coco_2014_valminusminival": ( 28 | "coco/val2014", 29 | "coco/annotations/densepose_valminusminival2014.json", 30 | ), 31 | } 32 | 33 | DENSEPOSE_KEYS = ["dp_x", "dp_y", "dp_I", "dp_U", "dp_V", "dp_masks"] 34 | 35 | for key, (image_root, json_file) in SPLITS.items(): 36 | # Assume pre-defined datasets live in `./datasets`. 37 | json_file = os.path.join("datasets", json_file) 38 | image_root = os.path.join("datasets", image_root) 39 | 40 | DatasetCatalog.register( 41 | key, 42 | lambda key=key, json_file=json_file, image_root=image_root: load_coco_json( 43 | json_file, image_root, key, extra_annotation_keys=DENSEPOSE_KEYS 44 | ), 45 | ) 46 | 47 | MetadataCatalog.get(key).set( 48 | json_file=json_file, image_root=image_root, **get_densepose_metadata() 49 | ) 50 | -------------------------------------------------------------------------------- /docs/modules/utils.rst: -------------------------------------------------------------------------------- 1 | detectron2.utils package 2 | ======================== 3 | 4 | detectron2.utils.colormap module 5 | -------------------------------- 6 | 7 | .. automodule:: detectron2.utils.colormap 8 | :members: 9 | :undoc-members: 10 | :show-inheritance: 11 | 12 | detectron2.utils.comm module 13 | ---------------------------- 14 | 15 | .. automodule:: detectron2.utils.comm 16 | :members: 17 | :undoc-members: 18 | :show-inheritance: 19 | 20 | 21 | detectron2.utils.events module 22 | ------------------------------ 23 | 24 | .. automodule:: detectron2.utils.events 25 | :members: 26 | :undoc-members: 27 | :show-inheritance: 28 | 29 | 30 | detectron2.utils.logger module 31 | ------------------------------ 32 | 33 | .. automodule:: detectron2.utils.logger 34 | :members: 35 | :undoc-members: 36 | :show-inheritance: 37 | 38 | 39 | detectron2.utils.registry module 40 | -------------------------------- 41 | 42 | .. automodule:: detectron2.utils.registry 43 | :members: 44 | :undoc-members: 45 | :show-inheritance: 46 | 47 | detectron2.utils.memory module 48 | ---------------------------------- 49 | 50 | .. automodule:: detectron2.utils.memory 51 | :members: 52 | :undoc-members: 53 | :show-inheritance: 54 | 55 | 56 | detectron2.utils.analysis module 57 | ---------------------------------- 58 | 59 | .. automodule:: detectron2.utils.analysis 60 | :members: 61 | :undoc-members: 62 | :show-inheritance: 63 | 64 | 65 | detectron2.utils.visualizer module 66 | ---------------------------------- 67 | 68 | .. automodule:: detectron2.utils.visualizer 69 | :members: 70 | :undoc-members: 71 | :show-inheritance: 72 | 73 | detectron2.utils.video\_visualizer module 74 | ----------------------------------------- 75 | 76 | .. automodule:: detectron2.utils.video_visualizer 77 | :members: 78 | :undoc-members: 79 | :show-inheritance: 80 | 81 | -------------------------------------------------------------------------------- /docs/tutorials/evaluation.md: -------------------------------------------------------------------------------- 1 | 2 | # Evaluation 3 | 4 | Evaluation is a process that takes a number of inputs/outputs pairs and aggregate them. 5 | You can always [use the model](models.html) directly and just parse its inputs/outputs manually to perform 6 | evaluation. 7 | Alternatively, evaluation is implemented in detectron2 using the [DatasetEvaluator](../modules/evaluation.html#detectron2.evaluation.DatasetEvaluator) 8 | interface. 9 | 10 | Detectron2 includes a few `DatasetEvaluator` that computes metrics using standard dataset-specific 11 | APIs (e.g., COCO, LVIS). 12 | You can also implement your own `DatasetEvaluator` that performs some other jobs 13 | using the inputs/outputs pairs. 14 | For example, to count how many instances are detected on the validation set: 15 | 16 | ``` 17 | class Counter(DatasetEvaluator): 18 | def reset(self): 19 | self.count = 0 20 | def process(self, inputs, outputs): 21 | for output in outputs: 22 | self.count += len(output["instances"]) 23 | def evaluate(self): 24 | # save self.count somewhere, or print it, or return it. 25 | return {"count": self.count} 26 | ``` 27 | 28 | Once you have some `DatasetEvaluator`, you can run it with 29 | [inference_on_dataset](../modules/evaluation.html#detectron2.evaluation.inference_on_dataset). 30 | For example, 31 | 32 | ```python 33 | val_results = inference_on_dataset( 34 | model, 35 | val_data_loader, 36 | DatasetEvaluators([COCOEvaluator(...), Counter()])) 37 | ``` 38 | Compared to running the evaluation manually using the model, the benefit of this function is that 39 | you can merge evaluators together using [DatasetEvaluators](../modules/evaluation.html#detectron2.evaluation.DatasetEvaluators). 40 | In this way you can run all evaluations without having to go through the dataset multiple times. 41 | 42 | The `inference_on_dataset` function also provides accurate speed benchmarks for the 43 | given model and dataset. 44 | -------------------------------------------------------------------------------- /docs/modules/modeling.rst: -------------------------------------------------------------------------------- 1 | detectron2.modeling package 2 | =========================== 3 | 4 | .. automodule:: detectron2.modeling 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | 10 | detectron2.modeling.poolers module 11 | --------------------------------------- 12 | 13 | .. automodule:: detectron2.modeling.poolers 14 | :members: 15 | :undoc-members: 16 | :show-inheritance: 17 | 18 | 19 | detectron2.modeling.sampling module 20 | ------------------------------------ 21 | 22 | .. automodule:: detectron2.modeling.sampling 23 | :members: 24 | :undoc-members: 25 | :show-inheritance: 26 | 27 | 28 | detectron2.modeling.box_regression module 29 | ------------------------------------------ 30 | 31 | .. automodule:: detectron2.modeling.box_regression 32 | :members: 33 | :undoc-members: 34 | :show-inheritance: 35 | 36 | 37 | Model Registries 38 | ----------------- 39 | 40 | These are different registries provided in modeling. 41 | Each registry provide you the ability to replace it with your customized component, 42 | without having to modify detectron2's code. 43 | 44 | Note that it is impossible to allow users to customize any line of code directly. 45 | Even just to add one line at some place, 46 | you'll likely need to find out the smallest registry which contains that line, 47 | and register your component to that registry. 48 | 49 | 50 | .. autodata:: detectron2.modeling.META_ARCH_REGISTRY 51 | .. autodata:: detectron2.modeling.BACKBONE_REGISTRY 52 | .. autodata:: detectron2.modeling.PROPOSAL_GENERATOR_REGISTRY 53 | .. autodata:: detectron2.modeling.RPN_HEAD_REGISTRY 54 | .. autodata:: detectron2.modeling.ANCHOR_GENERATOR_REGISTRY 55 | .. autodata:: detectron2.modeling.ROI_HEADS_REGISTRY 56 | .. autodata:: detectron2.modeling.ROI_BOX_HEAD_REGISTRY 57 | .. autodata:: detectron2.modeling.ROI_MASK_HEAD_REGISTRY 58 | .. autodata:: detectron2.modeling.ROI_KEYPOINT_HEAD_REGISTRY 59 | -------------------------------------------------------------------------------- /docker/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM nvidia/cuda:10.1-cudnn7-devel 2 | 3 | ENV DEBIAN_FRONTEND noninteractive 4 | RUN apt-get update && apt-get install -y \ 5 | python3-opencv ca-certificates python3-dev git wget sudo && \ 6 | rm -rf /var/lib/apt/lists/* 7 | 8 | # create a non-root user 9 | ARG USER_ID=1000 10 | RUN useradd -m --no-log-init --system --uid ${USER_ID} appuser -g sudo 11 | RUN echo '%sudo ALL=(ALL) NOPASSWD:ALL' >> /etc/sudoers 12 | USER appuser 13 | WORKDIR /home/appuser 14 | 15 | ENV PATH="/home/appuser/.local/bin:${PATH}" 16 | RUN wget https://bootstrap.pypa.io/get-pip.py && \ 17 | python3 get-pip.py --user && \ 18 | rm get-pip.py 19 | 20 | # install dependencies 21 | # See https://pytorch.org/ for other options if you use a different version of CUDA 22 | RUN pip install --user torch torchvision tensorboard cython 23 | RUN pip install --user 'git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI' 24 | 25 | RUN pip install --user 'git+https://github.com/facebookresearch/fvcore' 26 | # install detectron2 27 | RUN git clone https://github.com/facebookresearch/detectron2 detectron2_repo 28 | ENV FORCE_CUDA="1" 29 | # This will build detectron2 for all common cuda architectures and take a lot more time, 30 | # because inside `docker build`, there is no way to tell which architecture will be used. 31 | ENV TORCH_CUDA_ARCH_LIST="Kepler;Kepler+Tesla;Maxwell;Maxwell+Tegra;Pascal;Volta;Turing" 32 | RUN pip install --user -e detectron2_repo 33 | 34 | # Set a fixed model cache directory. 35 | ENV FVCORE_CACHE="/tmp" 36 | WORKDIR /home/appuser/detectron2_repo 37 | 38 | # run it, for example: 39 | # wget http://images.cocodataset.org/val2017/000000439715.jpg -O input.jpg 40 | # python3 demo/demo.py \ 41 | #--config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \ 42 | #--input input.jpg --output outputs/ \ 43 | #--opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl 44 | -------------------------------------------------------------------------------- /docs/tutorials/write-models.md: -------------------------------------------------------------------------------- 1 | # Write Models 2 | 3 | If you are trying to do something completely new, you may wish to implement 4 | a model entirely from scratch within detectron2. However, in many situations you may 5 | be interested in modifying or extending some components of an existing model. 6 | Therefore, we also provide a registration mechanism that lets you override the 7 | behavior of certain internal components of standard models. 8 | 9 | For example, to add a new backbone, import this code in your code: 10 | ```python 11 | from detectron2.modeling import BACKBONE_REGISTRY, Backbone, ShapeSpec 12 | 13 | @BACKBONE_REGISTRY.register() 14 | class ToyBackBone(Backbone): 15 | def __init__(self, cfg, input_shape): 16 | # create your own backbone 17 | self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=16, padding=3) 18 | 19 | def forward(self, image): 20 | return {"conv1": self.conv1(image)} 21 | 22 | def output_shape(self): 23 | return {"conv1": ShapeSpec(channels=64, stride=16)} 24 | ``` 25 | Then, you can use `cfg.MODEL.BACKBONE.NAME = 'ToyBackBone'` in your config object. 26 | `build_model(cfg)` will then call your `ToyBackBone` instead. 27 | 28 | As another example, to add new abilities to the ROI heads in the Generalized R-CNN meta-architecture, 29 | you can implement a new 30 | [ROIHeads](../modules/modeling.html#detectron2.modeling.ROIHeads) subclass and put it in the `ROI_HEADS_REGISTRY`. 31 | See [densepose in detectron2](../../projects/DensePose) 32 | and [meshrcnn](https://github.com/facebookresearch/meshrcnn) 33 | for examples that implement new ROIHeads to perform new tasks. 34 | And [projects/](../../projects/) 35 | contains more examples that implement different architectures. 36 | 37 | A complete list of registries can be found in [API documentation](../modules/modeling.html#model-registries). 38 | You can register components in these registries to customize different parts of a model, or the 39 | entire model. 40 | -------------------------------------------------------------------------------- /docs/tutorials/training.md: -------------------------------------------------------------------------------- 1 | # Training 2 | 3 | From the previous tutorials, you may now have a custom model and data loader. 4 | 5 | You are free to create your own optimizer, and write the training logic: it's 6 | usually easy with PyTorch, and allow researchers to see the entire training 7 | logic more clearly and have full control. 8 | One such example is provided in [tools/plain_train_net.py](../../tools/plain_train_net.py). 9 | 10 | We also provide a standarized "trainer" abstraction with a 11 | [minimal hook system](../modules/engine.html#detectron2.engine.HookBase) 12 | that helps simplify the standard types of training. 13 | 14 | You can use 15 | [SimpleTrainer().train()](../modules/engine.html#detectron2.engine.SimpleTrainer) 16 | which provides minimal abstraction for single-cost single-optimizer single-data-source training. 17 | The builtin `train_net.py` script uses 18 | [DefaultTrainer().train()](../modules/engine.html#detectron2.engine.defaults.DefaultTrainer), 19 | which includes more standard default behavior that one might want to opt in. 20 | This also means that it's less likely to support some non-standard behavior 21 | you might want during research. 22 | 23 | To customize the training loops, you can either start 24 | from [tools/plain_train_net.py](../../tools/plain_train_net.py), 25 | or look at the source code of [DefaultTrainer](../../detectron2/engine/defaults.py) 26 | and overwrite some of its behaviors with new parameters or new hooks. 27 | 28 | 29 | ### Logging of Metrics 30 | 31 | During training, metrics are logged with a centralized [EventStorage](../modules/utils.html#detectron2.utils.events.EventStorage). 32 | You can use the following code to access it and log metrics to it: 33 | ``` 34 | from detectron2.utils.events import get_event_storage 35 | 36 | # inside the model: 37 | if self.training: 38 | value = # compute the value from inputs 39 | storage = get_event_storage() 40 | storage.put_scalar("some_accuracy", value) 41 | ``` 42 | 43 | Refer to its documentation for more details. 44 | -------------------------------------------------------------------------------- /projects/PointRend/point_rend/config.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | from detectron2.config import CfgNode as CN 5 | 6 | 7 | def add_pointrend_config(cfg): 8 | """ 9 | Add config for PointRend. 10 | """ 11 | # Names of the input feature maps to be used by a coarse mask head. 12 | cfg.MODEL.ROI_MASK_HEAD.IN_FEATURES = ("p2",) 13 | cfg.MODEL.ROI_MASK_HEAD.FC_DIM = 1024 14 | cfg.MODEL.ROI_MASK_HEAD.NUM_FC = 2 15 | # The side size of a coarse mask head prediction. 16 | cfg.MODEL.ROI_MASK_HEAD.OUTPUT_SIDE_RESOLUTION = 7 17 | # True if point head is used. 18 | cfg.MODEL.ROI_MASK_HEAD.POINT_HEAD_ON = False 19 | 20 | cfg.MODEL.POINT_HEAD = CN() 21 | cfg.MODEL.POINT_HEAD.NAME = "StandardPointHead" 22 | cfg.MODEL.POINT_HEAD.NUM_CLASSES = 80 23 | # Names of the input feature maps to be used by a mask point head. 24 | cfg.MODEL.POINT_HEAD.IN_FEATURES = ("p2",) 25 | # Number of points sampled during training for a mask point head. 26 | cfg.MODEL.POINT_HEAD.TRAIN_NUM_POINTS = 14 * 14 27 | # Oversampling parameter for PointRend point sampling during training. Parameter `k` in the 28 | # original paper. 29 | cfg.MODEL.POINT_HEAD.OVERSAMPLE_RATIO = 3 30 | # Importance sampling parameter for PointRend point sampling during training. Parametr `beta` in 31 | # the original paper. 32 | cfg.MODEL.POINT_HEAD.IMPORTANCE_SAMPLE_RATIO = 0.75 33 | # Number of subdivision steps during inference. 34 | cfg.MODEL.POINT_HEAD.SUBDIVISION_STEPS = 5 35 | # Maximum number of points selected at each subdivision step (N). 36 | cfg.MODEL.POINT_HEAD.SUBDIVISION_NUM_POINTS = 28 * 28 37 | cfg.MODEL.POINT_HEAD.FC_DIM = 256 38 | cfg.MODEL.POINT_HEAD.NUM_FC = 3 39 | cfg.MODEL.POINT_HEAD.CLS_AGNOSTIC_MASK = False 40 | # If True, then coarse prediction features are used as inout for each layer in PointRend's MLP. 41 | cfg.MODEL.POINT_HEAD.COARSE_PRED_EACH_LAYER = True 42 | -------------------------------------------------------------------------------- /projects/TridentNet/train_net.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | """ 3 | TridentNet Training Script. 4 | 5 | This script is a simplified version of the training script in detectron2/tools. 6 | """ 7 | 8 | import os 9 | 10 | from detectron2.checkpoint import DetectionCheckpointer 11 | from detectron2.config import get_cfg 12 | from detectron2.engine import DefaultTrainer, default_argument_parser, default_setup, launch 13 | from detectron2.evaluation import COCOEvaluator 14 | 15 | from tridentnet import add_tridentnet_config 16 | 17 | 18 | class Trainer(DefaultTrainer): 19 | @classmethod 20 | def build_evaluator(cls, cfg, dataset_name, output_folder=None): 21 | if output_folder is None: 22 | output_folder = os.path.join(cfg.OUTPUT_DIR, "inference") 23 | return COCOEvaluator(dataset_name, cfg, True, output_folder) 24 | 25 | 26 | def setup(args): 27 | """ 28 | Create configs and perform basic setups. 29 | """ 30 | cfg = get_cfg() 31 | add_tridentnet_config(cfg) 32 | cfg.merge_from_file(args.config_file) 33 | cfg.merge_from_list(args.opts) 34 | cfg.freeze() 35 | default_setup(cfg, args) 36 | return cfg 37 | 38 | 39 | def main(args): 40 | cfg = setup(args) 41 | 42 | if args.eval_only: 43 | model = Trainer.build_model(cfg) 44 | DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load( 45 | cfg.MODEL.WEIGHTS, resume=args.resume 46 | ) 47 | res = Trainer.test(cfg, model) 48 | return res 49 | 50 | trainer = Trainer(cfg) 51 | trainer.resume_or_load(resume=args.resume) 52 | return trainer.train() 53 | 54 | 55 | if __name__ == "__main__": 56 | args = default_argument_parser().parse_args() 57 | print("Command Line Args:", args) 58 | launch( 59 | main, 60 | args.num_gpus, 61 | num_machines=args.num_machines, 62 | machine_rank=args.machine_rank, 63 | dist_url=args.dist_url, 64 | args=(args,), 65 | ) 66 | -------------------------------------------------------------------------------- /projects/DensePose/densepose/config.py: -------------------------------------------------------------------------------- 1 | # -*- coding = utf-8 -*- 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | from detectron2.config import CfgNode as CN 5 | 6 | 7 | def add_densepose_config(cfg): 8 | """ 9 | Add config for densepose head. 10 | """ 11 | _C = cfg 12 | 13 | _C.MODEL.DENSEPOSE_ON = True 14 | 15 | _C.MODEL.ROI_DENSEPOSE_HEAD = CN() 16 | _C.MODEL.ROI_DENSEPOSE_HEAD.NAME = "" 17 | _C.MODEL.ROI_DENSEPOSE_HEAD.NUM_STACKED_CONVS = 8 18 | # Number of parts used for point labels 19 | _C.MODEL.ROI_DENSEPOSE_HEAD.NUM_PATCHES = 24 20 | _C.MODEL.ROI_DENSEPOSE_HEAD.DECONV_KERNEL = 4 21 | _C.MODEL.ROI_DENSEPOSE_HEAD.CONV_HEAD_DIM = 512 22 | _C.MODEL.ROI_DENSEPOSE_HEAD.CONV_HEAD_KERNEL = 3 23 | _C.MODEL.ROI_DENSEPOSE_HEAD.UP_SCALE = 2 24 | _C.MODEL.ROI_DENSEPOSE_HEAD.HEATMAP_SIZE = 112 25 | _C.MODEL.ROI_DENSEPOSE_HEAD.POOLER_TYPE = "ROIAlignV2" 26 | _C.MODEL.ROI_DENSEPOSE_HEAD.POOLER_RESOLUTION = 28 27 | _C.MODEL.ROI_DENSEPOSE_HEAD.POOLER_SAMPLING_RATIO = 2 28 | _C.MODEL.ROI_DENSEPOSE_HEAD.NUM_COARSE_SEGM_CHANNELS = 2 # 15 or 2 29 | # Overlap threshold for an RoI to be considered foreground (if >= FG_IOU_THRESHOLD) 30 | _C.MODEL.ROI_DENSEPOSE_HEAD.FG_IOU_THRESHOLD = 0.7 31 | # Loss weights for annotation masks.(14 Parts) 32 | _C.MODEL.ROI_DENSEPOSE_HEAD.INDEX_WEIGHTS = 5.0 33 | # Loss weights for surface parts. (24 Parts) 34 | _C.MODEL.ROI_DENSEPOSE_HEAD.PART_WEIGHTS = 1.0 35 | # Loss weights for UV regression. 36 | _C.MODEL.ROI_DENSEPOSE_HEAD.POINT_REGRESSION_WEIGHTS = 0.01 37 | # For Decoder 38 | _C.MODEL.ROI_DENSEPOSE_HEAD.DECODER_ON = True 39 | _C.MODEL.ROI_DENSEPOSE_HEAD.DECODER_NUM_CLASSES = 256 40 | _C.MODEL.ROI_DENSEPOSE_HEAD.DECODER_CONV_DIMS = 256 41 | _C.MODEL.ROI_DENSEPOSE_HEAD.DECODER_NORM = "" 42 | _C.MODEL.ROI_DENSEPOSE_HEAD.DECODER_COMMON_STRIDE = 4 43 | # For DeepLab head 44 | _C.MODEL.ROI_DENSEPOSE_HEAD.DEEPLAB = CN() 45 | _C.MODEL.ROI_DENSEPOSE_HEAD.DEEPLAB.NORM = "GN" 46 | _C.MODEL.ROI_DENSEPOSE_HEAD.DEEPLAB.NONLOCAL_ON = 0 47 | -------------------------------------------------------------------------------- /detectron2/data/samplers/grouped_batch_sampler.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import numpy as np 3 | from torch.utils.data.sampler import BatchSampler, Sampler 4 | 5 | 6 | class GroupedBatchSampler(BatchSampler): 7 | """ 8 | Wraps another sampler to yield a mini-batch of indices. 9 | It enforces that the batch only contain elements from the same group. 10 | It also tries to provide mini-batches which follows an ordering which is 11 | as close as possible to the ordering from the original sampler. 12 | """ 13 | 14 | def __init__(self, sampler, group_ids, batch_size): 15 | """ 16 | Args: 17 | sampler (Sampler): Base sampler. 18 | group_ids (list[int]): If the sampler produces indices in range [0, N), 19 | `group_ids` must be a list of `N` ints which contains the group id of each sample. 20 | The group ids must be a set of integers in the range [0, num_groups). 21 | batch_size (int): Size of mini-batch. 22 | """ 23 | if not isinstance(sampler, Sampler): 24 | raise ValueError( 25 | "sampler should be an instance of " 26 | "torch.utils.data.Sampler, but got sampler={}".format(sampler) 27 | ) 28 | self.sampler = sampler 29 | self.group_ids = np.asarray(group_ids) 30 | assert self.group_ids.ndim == 1 31 | self.batch_size = batch_size 32 | groups = np.unique(self.group_ids).tolist() 33 | 34 | # buffer the indices of each group until batch size is reached 35 | self.buffer_per_group = {k: [] for k in groups} 36 | 37 | def __iter__(self): 38 | for idx in self.sampler: 39 | group_id = self.group_ids[idx] 40 | group_buffer = self.buffer_per_group[group_id] 41 | group_buffer.append(idx) 42 | if len(group_buffer) == self.batch_size: 43 | yield group_buffer[:] # yield a copy of the list 44 | del group_buffer[:] 45 | 46 | def __len__(self): 47 | raise NotImplementedError("len() of GroupedBatchSampler is not well-defined.") 48 | -------------------------------------------------------------------------------- /projects/TensorMask/train_net.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | """ 3 | TensorMask Training Script. 4 | 5 | This script is a simplified version of the training script in detectron2/tools. 6 | """ 7 | 8 | import os 9 | 10 | import detectron2.utils.comm as comm 11 | from detectron2.checkpoint import DetectionCheckpointer 12 | from detectron2.config import get_cfg 13 | from detectron2.engine import DefaultTrainer, default_argument_parser, default_setup, launch 14 | from detectron2.evaluation import COCOEvaluator, verify_results 15 | 16 | from tensormask import add_tensormask_config 17 | 18 | 19 | class Trainer(DefaultTrainer): 20 | @classmethod 21 | def build_evaluator(cls, cfg, dataset_name, output_folder=None): 22 | if output_folder is None: 23 | output_folder = os.path.join(cfg.OUTPUT_DIR, "inference") 24 | return COCOEvaluator(dataset_name, cfg, True, output_folder) 25 | 26 | 27 | def setup(args): 28 | """ 29 | Create configs and perform basic setups. 30 | """ 31 | cfg = get_cfg() 32 | add_tensormask_config(cfg) 33 | cfg.merge_from_file(args.config_file) 34 | cfg.merge_from_list(args.opts) 35 | cfg.freeze() 36 | default_setup(cfg, args) 37 | return cfg 38 | 39 | 40 | def main(args): 41 | cfg = setup(args) 42 | 43 | if args.eval_only: 44 | model = Trainer.build_model(cfg) 45 | DetectionCheckpointer(model, save_dir=cfg.OUTPUT_DIR).resume_or_load( 46 | cfg.MODEL.WEIGHTS, resume=args.resume 47 | ) 48 | res = Trainer.test(cfg, model) 49 | if comm.is_main_process(): 50 | verify_results(cfg, res) 51 | return res 52 | 53 | trainer = Trainer(cfg) 54 | trainer.resume_or_load(resume=args.resume) 55 | return trainer.train() 56 | 57 | 58 | if __name__ == "__main__": 59 | args = default_argument_parser().parse_args() 60 | print("Command Line Args:", args) 61 | launch( 62 | main, 63 | args.num_gpus, 64 | num_machines=args.num_machines, 65 | machine_rank=args.machine_rank, 66 | dist_url=args.dist_url, 67 | args=(args,), 68 | ) 69 | -------------------------------------------------------------------------------- /tests/test_model_analysis.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | 4 | import unittest 5 | import torch 6 | 7 | import detectron2.model_zoo as model_zoo 8 | from detectron2.config import get_cfg 9 | from detectron2.modeling import build_model 10 | from detectron2.utils.analysis import flop_count_operators, parameter_count 11 | 12 | 13 | def get_model_zoo(config_path): 14 | """ 15 | Like model_zoo.get, but do not load any weights (even pretrained) 16 | """ 17 | cfg_file = model_zoo.get_config_file(config_path) 18 | cfg = get_cfg() 19 | cfg.merge_from_file(cfg_file) 20 | if not torch.cuda.is_available(): 21 | cfg.MODEL.DEVICE = "cpu" 22 | return build_model(cfg) 23 | 24 | 25 | class RetinaNetTest(unittest.TestCase): 26 | def setUp(self): 27 | self.model = get_model_zoo("COCO-Detection/retinanet_R_50_FPN_1x.yaml") 28 | 29 | def test_flop(self): 30 | # RetinaNet supports flop-counting with random inputs 31 | inputs = [{"image": torch.rand(3, 800, 800)}] 32 | res = flop_count_operators(self.model, inputs) 33 | self.assertTrue(int(res["conv"]), 146) # 146B flops 34 | 35 | def test_param_count(self): 36 | res = parameter_count(self.model) 37 | self.assertTrue(res[""], 37915572) 38 | self.assertTrue(res["backbone"], 31452352) 39 | 40 | 41 | class FasterRCNNTest(unittest.TestCase): 42 | def setUp(self): 43 | self.model = get_model_zoo("COCO-Detection/faster_rcnn_R_50_FPN_1x.yaml") 44 | 45 | def test_flop(self): 46 | # Faster R-CNN supports flop-counting with random inputs 47 | inputs = [{"image": torch.rand(3, 800, 800)}] 48 | res = flop_count_operators(self.model, inputs) 49 | 50 | # This only checks flops for backbone & proposal generator 51 | # Flops for box head is not conv, and depends on #proposals, which is 52 | # almost 0 for random inputs. 53 | self.assertTrue(int(res["conv"]), 117) 54 | 55 | def test_param_count(self): 56 | res = parameter_count(self.model) 57 | self.assertTrue(res[""], 41699936) 58 | self.assertTrue(res["backbone"], 26799296) 59 | -------------------------------------------------------------------------------- /detectron2/modeling/proposal_generator/proposal_utils.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import math 3 | import torch 4 | 5 | from detectron2.structures import Instances 6 | 7 | 8 | def add_ground_truth_to_proposals(gt_boxes, proposals): 9 | """ 10 | Call `add_ground_truth_to_proposals_single_image` for all images. 11 | 12 | Args: 13 | gt_boxes(list[Boxes]): list of N elements. Element i is a Boxes 14 | representing the gound-truth for image i. 15 | proposals (list[Instances]): list of N elements. Element i is a Instances 16 | representing the proposals for image i. 17 | 18 | Returns: 19 | list[Instances]: list of N Instances. Each is the proposals for the image, 20 | with field "proposal_boxes" and "objectness_logits". 21 | """ 22 | assert gt_boxes is not None 23 | 24 | assert len(proposals) == len(gt_boxes) 25 | if len(proposals) == 0: 26 | return proposals 27 | 28 | return [ 29 | add_ground_truth_to_proposals_single_image(gt_boxes_i, proposals_i) 30 | for gt_boxes_i, proposals_i in zip(gt_boxes, proposals) 31 | ] 32 | 33 | 34 | def add_ground_truth_to_proposals_single_image(gt_boxes, proposals): 35 | """ 36 | Augment `proposals` with ground-truth boxes from `gt_boxes`. 37 | 38 | Args: 39 | Same as `add_ground_truth_to_proposals`, but with gt_boxes and proposals 40 | per image. 41 | 42 | Returns: 43 | Same as `add_ground_truth_to_proposals`, but for only one image. 44 | """ 45 | device = proposals.objectness_logits.device 46 | # Concatenating gt_boxes with proposals requires them to have the same fields 47 | # Assign all ground-truth boxes an objectness logit corresponding to P(object) \approx 1. 48 | gt_logit_value = math.log((1.0 - 1e-10) / (1 - (1.0 - 1e-10))) 49 | 50 | gt_logits = gt_logit_value * torch.ones(len(gt_boxes), device=device) 51 | gt_proposal = Instances(proposals.image_size) 52 | 53 | gt_proposal.proposal_boxes = gt_boxes 54 | gt_proposal.objectness_logits = gt_logits 55 | new_proposals = Instances.cat([proposals, gt_proposal]) 56 | 57 | return new_proposals 58 | -------------------------------------------------------------------------------- /docs/tutorials/configs.md: -------------------------------------------------------------------------------- 1 | # Use Configs 2 | 3 | Detectron2's config system uses yaml and [yacs](https://github.com/rbgirshick/yacs). 4 | In addition to the basic operations that access and update a config, we provide 5 | the following extra functionalities: 6 | 7 | 1. The config can have `_BASE_: base.yaml` field, which will load a base config first. 8 | Values in the base config will be overwritten in sub-configs, if there are any conflicts. 9 | We provided several base configs for standard model architectures. 10 | 2. We provide config versioning, for backward compatibility. 11 | If your config file is versioned with a config line like `VERSION: 2`, 12 | detectron2 will still recognize it even if we rename some keys in the future. 13 | 14 | ### Use Configs 15 | 16 | Some basic usage of the `CfgNode` object is shown below: 17 | ```python 18 | from detectron2.config import get_cfg 19 | cfg = get_cfg() # obtain detectron2's default config 20 | cfg.xxx = yyy # add new configs for your own custom components 21 | cfg.merge_from_file("my_cfg.yaml") # load values from a file 22 | 23 | cfg.merge_from_list(["MODEL.WEIGHTS", "weights.pth"]) # can also load values from a list of str 24 | print(cfg.dump()) # print formatted configs 25 | ``` 26 | 27 | To see a list of available configs in detectron2, see [Config References](../modules/config.html#config-references) 28 | 29 | 30 | ### Best Practice with Configs 31 | 32 | 1. Treat the configs you write as "code": avoid copying them or duplicating them; use `_BASE_` 33 | to share common parts between configs. 34 | 35 | 2. Keep the configs you write simple: don't include keys that do not affect the experimental setting. 36 | 37 | 3. Keep a version number in your configs (or the base config), e.g., `VERSION: 2`, 38 | for backward compatibility. 39 | We print a warning when reading a config without version number. 40 | The official configs do not include version number because they are meant to 41 | be always up-to-date. 42 | 43 | 4. Save a full config together with a trained model, and use it to run inference. 44 | This is more robust to changes that may happen to the config definition 45 | (e.g., if a default value changed), although we will try to avoid such changes. 46 | -------------------------------------------------------------------------------- /dev/parse_results.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | # A shell script that parses metrics from the log file. 5 | # Make it easier for developers to track performance of models. 6 | 7 | LOG="$1" 8 | 9 | if [[ -z "$LOG" ]]; then 10 | echo "Usage: $0 /path/to/log/file" 11 | exit 1 12 | fi 13 | 14 | # [12/15 11:47:32] trainer INFO: Total training time: 12:15:04.446477 (0.4900 s / it) 15 | # [12/15 11:49:03] inference INFO: Total inference time: 0:01:25.326167 (0.13652186737060548 s / img per device, on 8 devices) 16 | # [12/15 11:49:03] inference INFO: Total inference pure compute time: ..... 17 | 18 | # training time 19 | trainspeed=$(grep -o 'Overall training.*' "$LOG" | grep -Eo '\(.*\)' | grep -o '[0-9\.]*') 20 | echo "Training speed: $trainspeed s/it" 21 | 22 | # inference time: there could be multiple inference during training 23 | inferencespeed=$(grep -o 'Total inference pure.*' "$LOG" | tail -n1 | grep -Eo '\(.*\)' | grep -o '[0-9\.]*' | head -n1) 24 | echo "Inference speed: $inferencespeed s/it" 25 | 26 | # [12/15 11:47:18] trainer INFO: eta: 0:00:00 iter: 90000 loss: 0.5407 (0.7256) loss_classifier: 0.1744 (0.2446) loss_box_reg: 0.0838 (0.1160) loss_mask: 0.2159 (0.2722) loss_objectness: 0.0244 (0.0429) loss_rpn_box_reg: 0.0279 (0.0500) time: 0.4487 (0.4899) data: 0.0076 (0.0975) lr: 0.000200 max mem: 4161 27 | memory=$(grep -o 'max[_ ]mem: [0-9]*' "$LOG" | tail -n1 | grep -o '[0-9]*') 28 | echo "Training memory: $memory MB" 29 | 30 | echo "Easy to copypaste:" 31 | echo "$trainspeed","$inferencespeed","$memory" 32 | 33 | echo "------------------------------" 34 | 35 | # [12/26 17:26:32] engine.coco_evaluation: copypaste: Task: bbox 36 | # [12/26 17:26:32] engine.coco_evaluation: copypaste: AP,AP50,AP75,APs,APm,APl 37 | # [12/26 17:26:32] engine.coco_evaluation: copypaste: 0.0017,0.0024,0.0017,0.0005,0.0019,0.0011 38 | # [12/26 17:26:32] engine.coco_evaluation: copypaste: Task: segm 39 | # [12/26 17:26:32] engine.coco_evaluation: copypaste: AP,AP50,AP75,APs,APm,APl 40 | # [12/26 17:26:32] engine.coco_evaluation: copypaste: 0.0014,0.0021,0.0016,0.0005,0.0016,0.0011 41 | 42 | echo "COCO Results:" 43 | num_tasks=$(grep -o 'copypaste:.*Task.*' "$LOG" | sort -u | wc -l) 44 | # each task has 3 lines 45 | grep -o 'copypaste:.*' "$LOG" | cut -d ' ' -f 2- | tail -n $((num_tasks * 3)) 46 | -------------------------------------------------------------------------------- /projects/TensorMask/tensormask/layers/swap_align2nat.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | from torch import nn 3 | from torch.autograd import Function 4 | from torch.autograd.function import once_differentiable 5 | 6 | from tensormask import _C 7 | 8 | 9 | class _SwapAlign2Nat(Function): 10 | @staticmethod 11 | def forward(ctx, X, lambda_val, pad_val): 12 | ctx.lambda_val = lambda_val 13 | ctx.input_shape = X.size() 14 | 15 | Y = _C.swap_align2nat_forward(X, lambda_val, pad_val) 16 | return Y 17 | 18 | @staticmethod 19 | @once_differentiable 20 | def backward(ctx, gY): 21 | lambda_val = ctx.lambda_val 22 | bs, ch, h, w = ctx.input_shape 23 | 24 | gX = _C.swap_align2nat_backward(gY, lambda_val, bs, ch, h, w) 25 | 26 | return gX, None, None 27 | 28 | 29 | swap_align2nat = _SwapAlign2Nat.apply 30 | 31 | 32 | class SwapAlign2Nat(nn.Module): 33 | """ 34 | The op `SwapAlign2Nat` described in https://arxiv.org/abs/1903.12174. 35 | Given an input tensor that predicts masks of shape (N, C=VxU, H, W), 36 | apply the op, it will return masks of shape (N, V'xU', H', W') where 37 | the unit lengths of (V, U) and (H, W) are swapped, and the mask representation 38 | is transformed from aligned to natural. 39 | Args: 40 | lambda_val (int): the relative unit length ratio between (V, U) and (H, W), 41 | as we always have larger unit lengths for (V, U) than (H, W), 42 | lambda_val is always >= 1. 43 | pad_val (float): padding value for the values falling outside of the input 44 | tensor, default set to -6 as sigmoid(-6) is ~0, indicating 45 | that is no masks outside of the tensor. 46 | """ 47 | 48 | def __init__(self, lambda_val, pad_val=-6.0): 49 | super(SwapAlign2Nat, self).__init__() 50 | self.lambda_val = lambda_val 51 | self.pad_val = pad_val 52 | 53 | def forward(self, X): 54 | return swap_align2nat(X, self.lambda_val, self.pad_val) 55 | 56 | def __repr__(self): 57 | tmpstr = self.__class__.__name__ + "(" 58 | tmpstr += "lambda_val=" + str(self.lambda_val) 59 | tmpstr += ", pad_val=" + str(self.pad_val) 60 | tmpstr += ")" 61 | return tmpstr 62 | -------------------------------------------------------------------------------- /projects/TensorMask/setup.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 3 | 4 | import glob 5 | import os 6 | from setuptools import find_packages, setup 7 | import torch 8 | from torch.utils.cpp_extension import CUDA_HOME, CppExtension, CUDAExtension 9 | 10 | torch_ver = [int(x) for x in torch.__version__.split(".")[:2]] 11 | assert torch_ver >= [1, 3], "Requires PyTorch >= 1.3" 12 | 13 | 14 | def get_extensions(): 15 | this_dir = os.path.dirname(os.path.abspath(__file__)) 16 | extensions_dir = os.path.join(this_dir, "tensormask", "layers", "csrc") 17 | 18 | main_source = os.path.join(extensions_dir, "vision.cpp") 19 | sources = glob.glob(os.path.join(extensions_dir, "**", "*.cpp")) 20 | source_cuda = glob.glob(os.path.join(extensions_dir, "**", "*.cu")) + glob.glob( 21 | os.path.join(extensions_dir, "*.cu") 22 | ) 23 | 24 | sources = [main_source] + sources 25 | 26 | extension = CppExtension 27 | 28 | extra_compile_args = {"cxx": []} 29 | define_macros = [] 30 | 31 | if (torch.cuda.is_available() and CUDA_HOME is not None) or os.getenv("FORCE_CUDA", "0") == "1": 32 | extension = CUDAExtension 33 | sources += source_cuda 34 | define_macros += [("WITH_CUDA", None)] 35 | extra_compile_args["nvcc"] = [ 36 | "-DCUDA_HAS_FP16=1", 37 | "-D__CUDA_NO_HALF_OPERATORS__", 38 | "-D__CUDA_NO_HALF_CONVERSIONS__", 39 | "-D__CUDA_NO_HALF2_OPERATORS__", 40 | ] 41 | 42 | # It's better if pytorch can do this by default .. 43 | CC = os.environ.get("CC", None) 44 | if CC is not None: 45 | extra_compile_args["nvcc"].append("-ccbin={}".format(CC)) 46 | 47 | sources = [os.path.join(extensions_dir, s) for s in sources] 48 | 49 | include_dirs = [extensions_dir] 50 | 51 | ext_modules = [ 52 | extension( 53 | "tensormask._C", 54 | sources, 55 | include_dirs=include_dirs, 56 | define_macros=define_macros, 57 | extra_compile_args=extra_compile_args, 58 | ) 59 | ] 60 | 61 | return ext_modules 62 | 63 | 64 | setup( 65 | name="tensormask", 66 | version="0.1", 67 | author="FAIR", 68 | packages=find_packages(exclude=("configs", "tests")), 69 | python_requires=">=3.6", 70 | ext_modules=get_extensions(), 71 | cmdclass={"build_ext": torch.utils.cpp_extension.BuildExtension}, 72 | ) 73 | -------------------------------------------------------------------------------- /detectron2/modeling/sampling.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import torch 3 | 4 | __all__ = ["subsample_labels"] 5 | 6 | 7 | def subsample_labels(labels, num_samples, positive_fraction, bg_label): 8 | """ 9 | Return `num_samples` (or fewer, if not enough found) 10 | random samples from `labels` which is a mixture of positives & negatives. 11 | It will try to return as many positives as possible without 12 | exceeding `positive_fraction * num_samples`, and then try to 13 | fill the remaining slots with negatives. 14 | 15 | Args: 16 | labels (Tensor): (N, ) label vector with values: 17 | * -1: ignore 18 | * bg_label: background ("negative") class 19 | * otherwise: one or more foreground ("positive") classes 20 | num_samples (int): The total number of labels with value >= 0 to return. 21 | Values that are not sampled will be filled with -1 (ignore). 22 | positive_fraction (float): The number of subsampled labels with values > 0 23 | is `min(num_positives, int(positive_fraction * num_samples))`. The number 24 | of negatives sampled is `min(num_negatives, num_samples - num_positives_sampled)`. 25 | In order words, if there are not enough positives, the sample is filled with 26 | negatives. If there are also not enough negatives, then as many elements are 27 | sampled as is possible. 28 | bg_label (int): label index of background ("negative") class. 29 | 30 | Returns: 31 | pos_idx, neg_idx (Tensor): 32 | 1D vector of indices. The total length of both is `num_samples` or fewer. 33 | """ 34 | positive = torch.nonzero((labels != -1) & (labels != bg_label)).squeeze(1) 35 | negative = torch.nonzero(labels == bg_label).squeeze(1) 36 | 37 | num_pos = int(num_samples * positive_fraction) 38 | # protect against not enough positive examples 39 | num_pos = min(positive.numel(), num_pos) 40 | num_neg = num_samples - num_pos 41 | # protect against not enough negative examples 42 | num_neg = min(negative.numel(), num_neg) 43 | 44 | # randomly select positive and negative examples 45 | perm1 = torch.randperm(positive.numel(), device=positive.device)[:num_pos] 46 | perm2 = torch.randperm(negative.numel(), device=negative.device)[:num_neg] 47 | 48 | pos_idx = positive[perm1] 49 | neg_idx = negative[perm2] 50 | return pos_idx, neg_idx 51 | -------------------------------------------------------------------------------- /.github/CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # Contributing to detectron2 2 | We want to make contributing to this project as easy and transparent as 3 | possible. 4 | 5 | ## Issues 6 | We use GitHub issues to track public bugs and questions. 7 | Please make sure to follow one of the 8 | [issue templates](https://github.com/facebookresearch/detectron2/issues/new/choose) 9 | when reporting any issues. 10 | 11 | Facebook has a [bounty program](https://www.facebook.com/whitehat/) for the safe 12 | disclosure of security bugs. In those cases, please go through the process 13 | outlined on that page and do not file a public issue. 14 | 15 | ## Pull Requests 16 | We actively welcome your pull requests. 17 | 18 | However, if you're adding any significant features, please 19 | make sure to have a corresponding issue to discuss your motivation and proposals, 20 | before sending a PR. We do not always accept new features, and we take the following 21 | factors into consideration: 22 | 23 | 1. Whether the same feature can be achieved without modifying detectron2. 24 | Detectron2 is designed so that you can implement many extensions from the outside, e.g. 25 | those in [projects](https://github.com/facebookresearch/detectron2/tree/master/projects). 26 | If some part is not as extensible, you can also bring up the issue to make it more extensible. 27 | 2. Whether the feature is potentially useful to a large audience, or only to a small portion of users. 28 | 3. Whether the proposed solution has a good design / interface. 29 | 4. Whether the proposed solution adds extra mental/practical overhead to users who don't 30 | need such feature. 31 | 5. Whether the proposed solution breaks existing APIs. 32 | 33 | When sending a PR, please do: 34 | 35 | 1. If a PR contains multiple orthogonal changes, split it to several PRs. 36 | 2. If you've added code that should be tested, add tests. 37 | 3. For PRs that need experiments (e.g. adding a new model), you don't need to update model zoo, 38 | but do provide experiment results in the description of the PR. 39 | 4. If APIs are changed, update the documentation. 40 | 5. Ensure the test suite passes. 41 | 6. Make sure your code lints with `./dev/linter.sh`. 42 | 43 | 44 | ## Contributor License Agreement ("CLA") 45 | In order to accept your pull request, we need you to submit a CLA. You only need 46 | to do this once to work on any of Facebook's open source projects. 47 | 48 | Complete your CLA here: 49 | 50 | ## License 51 | By contributing to detectron2, you agree that your contributions will be licensed 52 | under the LICENSE file in the root directory of this source tree. 53 | -------------------------------------------------------------------------------- /detectron2/layers/csrc/nms_rotated/nms_rotated_cpu.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | #include "../box_iou_rotated/box_iou_rotated_utils.h" 3 | #include "nms_rotated.h" 4 | 5 | namespace detectron2 { 6 | 7 | template 8 | at::Tensor nms_rotated_cpu_kernel( 9 | const at::Tensor& dets, 10 | const at::Tensor& scores, 11 | const float iou_threshold) { 12 | // nms_rotated_cpu_kernel is modified from torchvision's nms_cpu_kernel, 13 | // however, the code in this function is much shorter because 14 | // we delegate the IoU computation for rotated boxes to 15 | // the single_box_iou_rotated function in box_iou_rotated_utils.h 16 | AT_ASSERTM(!dets.type().is_cuda(), "dets must be a CPU tensor"); 17 | AT_ASSERTM(!scores.type().is_cuda(), "scores must be a CPU tensor"); 18 | AT_ASSERTM( 19 | dets.type() == scores.type(), "dets should have the same type as scores"); 20 | 21 | if (dets.numel() == 0) { 22 | return at::empty({0}, dets.options().dtype(at::kLong)); 23 | } 24 | 25 | auto order_t = std::get<1>(scores.sort(0, /* descending=*/true)); 26 | 27 | auto ndets = dets.size(0); 28 | at::Tensor suppressed_t = at::zeros({ndets}, dets.options().dtype(at::kByte)); 29 | at::Tensor keep_t = at::zeros({ndets}, dets.options().dtype(at::kLong)); 30 | 31 | auto suppressed = suppressed_t.data_ptr(); 32 | auto keep = keep_t.data_ptr(); 33 | auto order = order_t.data_ptr(); 34 | 35 | int64_t num_to_keep = 0; 36 | 37 | for (int64_t _i = 0; _i < ndets; _i++) { 38 | auto i = order[_i]; 39 | if (suppressed[i] == 1) { 40 | continue; 41 | } 42 | 43 | keep[num_to_keep++] = i; 44 | 45 | for (int64_t _j = _i + 1; _j < ndets; _j++) { 46 | auto j = order[_j]; 47 | if (suppressed[j] == 1) { 48 | continue; 49 | } 50 | 51 | auto ovr = single_box_iou_rotated( 52 | dets[i].data_ptr(), dets[j].data_ptr()); 53 | if (ovr >= iou_threshold) { 54 | suppressed[j] = 1; 55 | } 56 | } 57 | } 58 | return keep_t.narrow(/*dim=*/0, /*start=*/0, /*length=*/num_to_keep); 59 | } 60 | 61 | at::Tensor nms_rotated_cpu( 62 | const at::Tensor& dets, 63 | const at::Tensor& scores, 64 | const float iou_threshold) { 65 | auto result = at::empty({0}, dets.options()); 66 | 67 | AT_DISPATCH_FLOATING_TYPES(dets.type(), "nms_rotated", [&] { 68 | result = nms_rotated_cpu_kernel(dets, scores, iou_threshold); 69 | }); 70 | return result; 71 | } 72 | 73 | } // namespace detectron2 74 | -------------------------------------------------------------------------------- /docs/tutorials/extend.md: -------------------------------------------------------------------------------- 1 | # Extend Detectron2's Defaults 2 | 3 | __Research is about doing things in new ways__. 4 | This brings a tension in how to create abstractions in code, 5 | which is a challenge for any research engineering project of a significant size: 6 | 7 | 1. On one hand, it needs to have very thin abstractions to allow for the possibility of doing 8 | everything in new ways. It should be reasonably easy to break existing 9 | abstractions and replace them with new ones. 10 | 11 | 2. On the other hand, such a project also needs reasonably high-level 12 | abstractions, so that users can easily do things in standard ways, 13 | without worrying too much about the details that only certain researchers care about. 14 | 15 | In detectron2, there are two types of interfaces that address this tension together: 16 | 17 | 1. Functions and classes that take only a "config" argument (optionally with a minimal 18 | set of extra arguments in cases of mature interfaces). 19 | 20 | Such functions and classes implement 21 | the "standard default" behavior: it will read what it needs from the 22 | config and do the "standard" thing. 23 | Users only need to load a standard config and pass it around, without having to worry about 24 | which arguments are used and what they all mean. 25 | 26 | 2. Functions and classes that have well-defined explicit arguments. 27 | 28 | Each of these is a small building block of the entire system. 29 | They require users' effort to stitch together, but can be stitched together in more flexible ways. 30 | When you need to implement something different from the "standard defaults" 31 | included in detectron2, these well-defined components can be reused. 32 | 33 | 34 | If you only need the standard behavior, the [Beginner's Tutorial](getting_started.html) 35 | should suffice. If you need to extend detectron2 to your own needs, 36 | see the following tutorials for more details: 37 | 38 | * Detectron2 includes a few standard datasets, but you can use custom ones. See 39 | [Use Custom Datasets](datasets.html). 40 | * Detectron2 contains the standard logic that creates a data loader from a 41 | dataset, but you can write your own as well. See [Use Custom Data Loaders](data_loading.html). 42 | * Detectron2 implements many standard detection models, and provide ways for you 43 | to overwrite its behaviors. See [Use Models](models.html) and [Write Models](write-models.html). 44 | * Detectron2 provides a default training loop that is good for common training tasks. 45 | You can customize it with hooks, or write your own loop instead. See [training](training.html). 46 | -------------------------------------------------------------------------------- /datasets/README.md: -------------------------------------------------------------------------------- 1 | 2 | For a few datasets that detectron2 natively supports, 3 | the datasets are assumed to exist in a directory called 4 | "datasets/", under the directory where you launch the program. 5 | They need to have the following directory structure: 6 | 7 | ## Expected dataset structure for COCO instance/keypoint detection: 8 | 9 | ``` 10 | coco/ 11 | annotations/ 12 | instances_{train,val}2017.json 13 | person_keypoints_{train,val}2017.json 14 | {train,val}2017/ 15 | # image files that are mentioned in the corresponding json 16 | ``` 17 | 18 | You can use the 2014 version of the dataset as well. 19 | 20 | Some of the builtin tests (`dev/run_*_tests.sh`) uses a tiny version of the COCO dataset, 21 | which you can download with `./prepare_for_tests.sh`. 22 | 23 | ## Expected dataset structure for PanopticFPN: 24 | 25 | ``` 26 | coco/ 27 | annotations/ 28 | panoptic_{train,val}2017.json 29 | panoptic_{train,val}2017/ # png annotations 30 | panoptic_stuff_{train,val}2017/ # generated by the script mentioned below 31 | ``` 32 | 33 | Install panopticapi by: 34 | ``` 35 | pip install git+https://github.com/cocodataset/panopticapi.git 36 | ``` 37 | Then, run `python prepare_panoptic_fpn.py`, to extract semantic annotations from panoptic annotations. 38 | 39 | ## Expected dataset structure for LVIS instance segmentation: 40 | ``` 41 | coco/ 42 | {train,val,test}2017/ 43 | lvis/ 44 | lvis_v0.5_{train,val}.json 45 | lvis_v0.5_image_info_test.json 46 | ``` 47 | 48 | Install lvis-api by: 49 | ``` 50 | pip install git+https://github.com/lvis-dataset/lvis-api.git 51 | ``` 52 | 53 | Run `python prepare_cocofied_lvis.py` to prepare "cocofied" LVIS annotations for evaluation of models trained on the COCO dataset. 54 | 55 | ## Expected dataset structure for cityscapes: 56 | ``` 57 | cityscapes/ 58 | gtFine/ 59 | train/ 60 | aachen/ 61 | color.png, instanceIds.png, labelIds.png, polygons.json, 62 | labelTrainIds.png 63 | ... 64 | val/ 65 | test/ 66 | leftImg8bit/ 67 | train/ 68 | val/ 69 | test/ 70 | ``` 71 | Install cityscapes scripts by: 72 | ``` 73 | pip install git+https://github.com/mcordts/cityscapesScripts.git 74 | ``` 75 | 76 | Note: 77 | labelTrainIds.png are created by `cityscapesscripts/preparation/createTrainIdLabelImgs.py`. 78 | They are not needed for instance segmentation. 79 | 80 | ## Expected dataset structure for Pascal VOC: 81 | ``` 82 | VOC20{07,12}/ 83 | Annotations/ 84 | ImageSets/ 85 | Main/ 86 | trainval.txt 87 | test.txt 88 | # train.txt or val.txt, if you use these splits 89 | JPEGImages/ 90 | ``` 91 | -------------------------------------------------------------------------------- /tools/caffe2_converter.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import argparse 3 | import os 4 | 5 | from detectron2.checkpoint import DetectionCheckpointer 6 | from detectron2.config import get_cfg 7 | from detectron2.data import build_detection_test_loader 8 | from detectron2.evaluation import COCOEvaluator, inference_on_dataset, print_csv_format 9 | from detectron2.export import add_export_config, export_caffe2_model 10 | from detectron2.modeling import build_model 11 | from detectron2.utils.logger import setup_logger 12 | 13 | 14 | def setup_cfg(args): 15 | cfg = get_cfg() 16 | # cuda context is initialized before creating dataloader, so we don't fork anymore 17 | cfg.DATALOADER.NUM_WORKERS = 0 18 | cfg = add_export_config(cfg) 19 | cfg.merge_from_file(args.config_file) 20 | cfg.merge_from_list(args.opts) 21 | cfg.freeze() 22 | return cfg 23 | 24 | 25 | if __name__ == "__main__": 26 | parser = argparse.ArgumentParser(description="Convert a model to Caffe2") 27 | parser.add_argument("--config-file", default="", metavar="FILE", help="path to config file") 28 | parser.add_argument("--run-eval", action="store_true") 29 | parser.add_argument("--output", help="output directory for the converted caffe2 model") 30 | parser.add_argument( 31 | "opts", 32 | help="Modify config options using the command-line", 33 | default=None, 34 | nargs=argparse.REMAINDER, 35 | ) 36 | args = parser.parse_args() 37 | logger = setup_logger() 38 | logger.info("Command line arguments: " + str(args)) 39 | 40 | cfg = setup_cfg(args) 41 | 42 | # create a torch model 43 | torch_model = build_model(cfg) 44 | DetectionCheckpointer(torch_model).resume_or_load(cfg.MODEL.WEIGHTS) 45 | 46 | # get a sample data 47 | data_loader = build_detection_test_loader(cfg, cfg.DATASETS.TEST[0]) 48 | first_batch = next(iter(data_loader)) 49 | 50 | # convert and save caffe2 model 51 | caffe2_model = export_caffe2_model(cfg, torch_model, first_batch) 52 | caffe2_model.save_protobuf(args.output) 53 | # draw the caffe2 graph 54 | caffe2_model.save_graph(os.path.join(args.output, "model.svg"), inputs=first_batch) 55 | 56 | # run evaluation with the converted model 57 | if args.run_eval: 58 | dataset = cfg.DATASETS.TEST[0] 59 | data_loader = build_detection_test_loader(cfg, dataset) 60 | # NOTE: hard-coded evaluator. change to the evaluator for your dataset 61 | evaluator = COCOEvaluator(dataset, cfg, True, args.output) 62 | metrics = inference_on_dataset(caffe2_model, data_loader, evaluator) 63 | print_csv_format(metrics) 64 | -------------------------------------------------------------------------------- /detectron2/checkpoint/detection_checkpoint.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import pickle 3 | from fvcore.common.checkpoint import Checkpointer 4 | from fvcore.common.file_io import PathManager 5 | 6 | import detectron2.utils.comm as comm 7 | 8 | from .c2_model_loading import align_and_update_state_dicts 9 | 10 | 11 | class DetectionCheckpointer(Checkpointer): 12 | """ 13 | Same as :class:`Checkpointer`, but is able to handle models in detectron & detectron2 14 | model zoo, and apply conversions for legacy models. 15 | """ 16 | 17 | def __init__(self, model, save_dir="", *, save_to_disk=None, **checkpointables): 18 | is_main_process = comm.is_main_process() 19 | super().__init__( 20 | model, 21 | save_dir, 22 | save_to_disk=is_main_process if save_to_disk is None else save_to_disk, 23 | **checkpointables, 24 | ) 25 | 26 | def _load_file(self, filename): 27 | if filename.endswith(".pkl"): 28 | with PathManager.open(filename, "rb") as f: 29 | data = pickle.load(f, encoding="latin1") 30 | if "model" in data and "__author__" in data: 31 | # file is in Detectron2 model zoo format 32 | self.logger.info("Reading a file from '{}'".format(data["__author__"])) 33 | return data 34 | else: 35 | # assume file is from Caffe2 / Detectron1 model zoo 36 | if "blobs" in data: 37 | # Detection models have "blobs", but ImageNet models don't 38 | data = data["blobs"] 39 | data = {k: v for k, v in data.items() if not k.endswith("_momentum")} 40 | return {"model": data, "__author__": "Caffe2", "matching_heuristics": True} 41 | 42 | loaded = super()._load_file(filename) # load native pth checkpoint 43 | if "model" not in loaded: 44 | loaded = {"model": loaded} 45 | return loaded 46 | 47 | def _load_model(self, checkpoint): 48 | if checkpoint.get("matching_heuristics", False): 49 | self._convert_ndarray_to_tensor(checkpoint["model"]) 50 | # convert weights by name-matching heuristics 51 | model_state_dict = self.model.state_dict() 52 | align_and_update_state_dicts( 53 | model_state_dict, 54 | checkpoint["model"], 55 | c2_conversion=checkpoint.get("__author__", None) == "Caffe2", 56 | ) 57 | checkpoint["model"] = model_state_dict 58 | # for non-caffe2 models, use standard ways to load it 59 | super()._load_model(checkpoint) 60 | -------------------------------------------------------------------------------- /detectron2/evaluation/testing.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import logging 3 | import numpy as np 4 | import pprint 5 | import sys 6 | from collections import OrderedDict 7 | from collections.abc import Mapping 8 | 9 | 10 | def print_csv_format(results): 11 | """ 12 | Print main metrics in a format similar to Detectron, 13 | so that they are easy to copypaste into a spreadsheet. 14 | 15 | Args: 16 | results (OrderedDict[dict]): task_name -> {metric -> score} 17 | """ 18 | assert isinstance(results, OrderedDict), results # unordered results cannot be properly printed 19 | logger = logging.getLogger(__name__) 20 | for task, res in results.items(): 21 | # Don't print "AP-category" metrics since they are usually not tracked. 22 | important_res = [(k, v) for k, v in res.items() if "-" not in k] 23 | logger.info("copypaste: Task: {}".format(task)) 24 | logger.info("copypaste: " + ",".join([k[0] for k in important_res])) 25 | logger.info("copypaste: " + ",".join(["{0:.4f}".format(k[1]) for k in important_res])) 26 | 27 | 28 | def verify_results(cfg, results): 29 | """ 30 | Args: 31 | results (OrderedDict[dict]): task_name -> {metric -> score} 32 | 33 | Returns: 34 | bool: whether the verification succeeds or not 35 | """ 36 | expected_results = cfg.TEST.EXPECTED_RESULTS 37 | if not len(expected_results): 38 | return True 39 | 40 | ok = True 41 | for task, metric, expected, tolerance in expected_results: 42 | actual = results[task][metric] 43 | if not np.isfinite(actual): 44 | ok = False 45 | diff = abs(actual - expected) 46 | if diff > tolerance: 47 | ok = False 48 | 49 | logger = logging.getLogger(__name__) 50 | if not ok: 51 | logger.error("Result verification failed!") 52 | logger.error("Expected Results: " + str(expected_results)) 53 | logger.error("Actual Results: " + pprint.pformat(results)) 54 | 55 | sys.exit(1) 56 | else: 57 | logger.info("Results verification passed.") 58 | return ok 59 | 60 | 61 | def flatten_results_dict(results): 62 | """ 63 | Expand a hierarchical dict of scalars into a flat dict of scalars. 64 | If results[k1][k2][k3] = v, the returned dict will have the entry 65 | {"k1/k2/k3": v}. 66 | 67 | Args: 68 | results (dict): 69 | """ 70 | r = {} 71 | for k, v in results.items(): 72 | if isinstance(v, Mapping): 73 | v = flatten_results_dict(v) 74 | for kk, vv in v.items(): 75 | r[k + "/" + kk] = vv 76 | else: 77 | r[k] = v 78 | return r 79 | -------------------------------------------------------------------------------- /tests/test_box2box_transform.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import logging 3 | import unittest 4 | import torch 5 | 6 | from detectron2.modeling.box_regression import Box2BoxTransform, Box2BoxTransformRotated 7 | 8 | logger = logging.getLogger(__name__) 9 | 10 | 11 | def random_boxes(mean_box, stdev, N): 12 | return torch.rand(N, 4) * stdev + torch.tensor(mean_box, dtype=torch.float) 13 | 14 | 15 | class TestBox2BoxTransform(unittest.TestCase): 16 | def test_reconstruction(self): 17 | weights = (5, 5, 10, 10) 18 | b2b_tfm = Box2BoxTransform(weights=weights) 19 | src_boxes = random_boxes([10, 10, 20, 20], 1, 10) 20 | dst_boxes = random_boxes([10, 10, 20, 20], 1, 10) 21 | 22 | devices = [torch.device("cpu")] 23 | if torch.cuda.is_available(): 24 | devices.append(torch.device("cuda")) 25 | for device in devices: 26 | src_boxes = src_boxes.to(device=device) 27 | dst_boxes = dst_boxes.to(device=device) 28 | deltas = b2b_tfm.get_deltas(src_boxes, dst_boxes) 29 | dst_boxes_reconstructed = b2b_tfm.apply_deltas(deltas, src_boxes) 30 | assert torch.allclose(dst_boxes, dst_boxes_reconstructed) 31 | 32 | 33 | def random_rotated_boxes(mean_box, std_length, std_angle, N): 34 | return torch.cat( 35 | [torch.rand(N, 4) * std_length, torch.rand(N, 1) * std_angle], dim=1 36 | ) + torch.tensor(mean_box, dtype=torch.float) 37 | 38 | 39 | class TestBox2BoxTransformRotated(unittest.TestCase): 40 | def test_reconstruction(self): 41 | weights = (5, 5, 10, 10, 1) 42 | b2b_transform = Box2BoxTransformRotated(weights=weights) 43 | src_boxes = random_rotated_boxes([10, 10, 20, 20, -30], 5, 60.0, 10) 44 | dst_boxes = random_rotated_boxes([10, 10, 20, 20, -30], 5, 60.0, 10) 45 | 46 | devices = [torch.device("cpu")] 47 | if torch.cuda.is_available(): 48 | devices.append(torch.device("cuda")) 49 | for device in devices: 50 | src_boxes = src_boxes.to(device=device) 51 | dst_boxes = dst_boxes.to(device=device) 52 | deltas = b2b_transform.get_deltas(src_boxes, dst_boxes) 53 | dst_boxes_reconstructed = b2b_transform.apply_deltas(deltas, src_boxes) 54 | assert torch.allclose(dst_boxes[:, :4], dst_boxes_reconstructed[:, :4], atol=1e-5) 55 | # angle difference has to be normalized 56 | assert torch.allclose( 57 | (dst_boxes[:, 4] - dst_boxes_reconstructed[:, 4] + 180.0) % 360.0 - 180.0, 58 | torch.zeros_like(dst_boxes[:, 4]), 59 | atol=1e-4, 60 | ) 61 | 62 | 63 | if __name__ == "__main__": 64 | unittest.main() 65 | -------------------------------------------------------------------------------- /projects/DensePose/doc/GETTING_STARTED.md: -------------------------------------------------------------------------------- 1 | # Getting Started with DensePose 2 | 3 | ## Inference with Pre-trained Models 4 | 5 | 1. Pick a model and its config file from [Model Zoo](MODEL_ZOO.md), for example [densepose_rcnn_R_50_FPN_s1x.yaml](../configs/densepose_rcnn_R_50_FPN_s1x.yaml) 6 | 2. Run the [Apply Net](TOOL_APPLY_NET.md) tool to visualize the results or save the to disk. For example, to use contour visualization for DensePose, one can run: 7 | ```bash 8 | python apply_net.py show configs/densepose_rcnn_R_50_FPN_s1x.yaml densepose_rcnn_R_50_FPN_s1x.pkl image.jpg dp_contour,bbox --output image_densepose_contour.png 9 | ``` 10 | Please see [Apply Net](TOOL_APPLY_NET.md) for more details on the tool. 11 | 12 | ## Training 13 | 14 | First, prepare the [dataset](http://densepose.org/#dataset) into the following structure under the directory you'll run training scripts: 15 |
16 | datasets/coco/
17 |   annotations/
18 |     densepose_{train,minival,valminusminival}2014.json
19 |     densepose_minival2014_100.json   (optional, for testing only)
20 |   {train,val}2014/
21 |     # image files that are mentioned in the corresponding json
22 | 
23 | 24 | To train a model one can use the [train_net.py](../train_net.py) script. 25 | This script was used to train all DensePose models in [Model Zoo](MODEL_ZOO.md). 26 | For example, to launch end-to-end DensePose-RCNN training with ResNet-50 FPN backbone 27 | on 8 GPUs following the s1x schedule, one can run 28 | ```bash 29 | python train_net.py --config-file configs/densepose_rcnn_R_50_FPN_s1x.yaml --num-gpus 8 30 | ``` 31 | The configs are made for 8-GPU training. To train on 1 GPU, one can apply the 32 | [linear learning rate scaling rule](https://arxiv.org/abs/1706.02677): 33 | ```bash 34 | python train_net.py --config-file configs/densepose_rcnn_R_50_FPN_s1x.yaml \ 35 | SOLVER.IMS_PER_BATCH 2 SOLVER.BASE_LR 0.0025 36 | ``` 37 | 38 | ## Evaluation 39 | 40 | Model testing can be done in the same way as training, except for an additional flag `--eval-only` and 41 | model location specification through `MODEL.WEIGHTS model.pth` in the command line 42 | ```bash 43 | python train_net.py --config-file configs/densepose_rcnn_R_50_FPN_s1x.yaml \ 44 | --eval-only MODEL.WEIGHTS model.pth 45 | ``` 46 | 47 | ## Tools 48 | 49 | We provide tools which allow one to: 50 | - easily view DensePose annotated data in a dataset; 51 | - perform DensePose inference on a set of images; 52 | - visualize DensePose model results; 53 | 54 | `query_db` is a tool to print or visualize DensePose data in a dataset. 55 | Please refer to [Query DB](TOOL_QUERY_DB.md) for more details on this tool 56 | 57 | `apply_net` is a tool to print or visualize DensePose results. 58 | Please refer to [Apply Net](TOOL_APPLY_NET.md) for more details on this tool 59 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | AccessModifierOffset: -1 2 | AlignAfterOpenBracket: AlwaysBreak 3 | AlignConsecutiveAssignments: false 4 | AlignConsecutiveDeclarations: false 5 | AlignEscapedNewlinesLeft: true 6 | AlignOperands: false 7 | AlignTrailingComments: false 8 | AllowAllParametersOfDeclarationOnNextLine: false 9 | AllowShortBlocksOnASingleLine: false 10 | AllowShortCaseLabelsOnASingleLine: false 11 | AllowShortFunctionsOnASingleLine: Empty 12 | AllowShortIfStatementsOnASingleLine: false 13 | AllowShortLoopsOnASingleLine: false 14 | AlwaysBreakAfterReturnType: None 15 | AlwaysBreakBeforeMultilineStrings: true 16 | AlwaysBreakTemplateDeclarations: true 17 | BinPackArguments: false 18 | BinPackParameters: false 19 | BraceWrapping: 20 | AfterClass: false 21 | AfterControlStatement: false 22 | AfterEnum: false 23 | AfterFunction: false 24 | AfterNamespace: false 25 | AfterObjCDeclaration: false 26 | AfterStruct: false 27 | AfterUnion: false 28 | BeforeCatch: false 29 | BeforeElse: false 30 | IndentBraces: false 31 | BreakBeforeBinaryOperators: None 32 | BreakBeforeBraces: Attach 33 | BreakBeforeTernaryOperators: true 34 | BreakConstructorInitializersBeforeComma: false 35 | BreakAfterJavaFieldAnnotations: false 36 | BreakStringLiterals: false 37 | ColumnLimit: 80 38 | CommentPragmas: '^ IWYU pragma:' 39 | ConstructorInitializerAllOnOneLineOrOnePerLine: true 40 | ConstructorInitializerIndentWidth: 4 41 | ContinuationIndentWidth: 4 42 | Cpp11BracedListStyle: true 43 | DerivePointerAlignment: false 44 | DisableFormat: false 45 | ForEachMacros: [ FOR_EACH, FOR_EACH_ENUMERATE, FOR_EACH_KV, FOR_EACH_R, FOR_EACH_RANGE, ] 46 | IncludeCategories: 47 | - Regex: '^<.*\.h(pp)?>' 48 | Priority: 1 49 | - Regex: '^<.*' 50 | Priority: 2 51 | - Regex: '.*' 52 | Priority: 3 53 | IndentCaseLabels: true 54 | IndentWidth: 2 55 | IndentWrappedFunctionNames: false 56 | KeepEmptyLinesAtTheStartOfBlocks: false 57 | MacroBlockBegin: '' 58 | MacroBlockEnd: '' 59 | MaxEmptyLinesToKeep: 1 60 | NamespaceIndentation: None 61 | ObjCBlockIndentWidth: 2 62 | ObjCSpaceAfterProperty: false 63 | ObjCSpaceBeforeProtocolList: false 64 | PenaltyBreakBeforeFirstCallParameter: 1 65 | PenaltyBreakComment: 300 66 | PenaltyBreakFirstLessLess: 120 67 | PenaltyBreakString: 1000 68 | PenaltyExcessCharacter: 1000000 69 | PenaltyReturnTypeOnItsOwnLine: 200 70 | PointerAlignment: Left 71 | ReflowComments: true 72 | SortIncludes: true 73 | SpaceAfterCStyleCast: false 74 | SpaceBeforeAssignmentOperators: true 75 | SpaceBeforeParens: ControlStatements 76 | SpaceInEmptyParentheses: false 77 | SpacesBeforeTrailingComments: 1 78 | SpacesInAngles: false 79 | SpacesInContainerLiterals: true 80 | SpacesInCStyleCastParentheses: false 81 | SpacesInParentheses: false 82 | SpacesInSquareBrackets: false 83 | Standard: Cpp11 84 | TabWidth: 8 85 | UseTab: Never 86 | --------------------------------------------------------------------------------