├── classification ├── utils │ ├── __init__.py │ ├── incremental │ │ ├── __init__.py │ │ └── compute_features.py │ ├── imagenet │ │ ├── __init__.py │ │ ├── utils_dataset.py │ │ └── setup_data │ │ │ ├── generate_imagenet.py │ │ │ └── generate_imagenet_subset.py │ └── gpu_tools.py ├── trainer │ └── __init__.py ├── scripts │ ├── cifar_100_10_task.sh │ ├── cifar_100_25_task.sh │ ├── cifar_100_5_task.sh │ ├── mini_imagenet_5_task.sh │ ├── mini_imagenet_10_task.sh │ └── mini_imagenet_25_task.sh └── LICENSE ├── detection ├── docs │ ├── .gitignore │ ├── tutorials │ │ ├── install.md │ │ ├── getting_started.md │ │ ├── index.rst │ │ ├── training.md │ │ ├── write-models.md │ │ └── configs.md │ ├── notes │ │ ├── contributing.md │ │ ├── index.rst │ │ └── changelog.md │ ├── modules │ │ ├── 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 │ ├── README.md │ ├── requirements.txt │ └── Makefile ├── tests │ ├── __init__.py │ ├── test_model_zoo.py │ ├── test_sampler.py │ ├── test_config.py │ └── test_checkpoint.py ├── detectron2 │ ├── utils │ │ ├── __init__.py │ │ ├── registry.py │ │ ├── README.md │ │ ├── pickling.py │ │ ├── serialize.py │ │ └── store.py │ ├── layers │ │ ├── csrc │ │ │ ├── README.md │ │ │ ├── cuda_version.cu │ │ │ ├── box_iou_rotated │ │ │ │ ├── box_iou_rotated.h │ │ │ │ └── box_iou_rotated_cpu.cpp │ │ │ └── nms_rotated │ │ │ │ └── nms_rotated.h │ │ ├── __init__.py │ │ ├── shape_spec.py │ │ └── rotated_boxes.py │ ├── __init__.py │ ├── modeling │ │ ├── proposal_generator │ │ │ ├── __init__.py │ │ │ ├── build.py │ │ │ └── proposal_utils.py │ │ ├── backbone │ │ │ ├── __init__.py │ │ │ └── build.py │ │ ├── meta_arch │ │ │ ├── __init__.py │ │ │ └── build.py │ │ ├── roi_heads │ │ │ └── __init__.py │ │ ├── __init__.py │ │ └── distillation_loss.py │ ├── data │ │ ├── transforms │ │ │ └── __init__.py │ │ ├── datasets │ │ │ ├── README.md │ │ │ ├── __init__.py │ │ │ └── finetune_dataset.py │ │ ├── samplers │ │ │ ├── __init__.py │ │ │ └── grouped_batch_sampler.py │ │ └── __init__.py │ ├── solver │ │ └── __init__.py │ ├── config │ │ └── __init__.py │ ├── distiller_zoo │ │ ├── FitNet.py │ │ ├── __init__.py │ │ ├── KD.py │ │ ├── SP.py │ │ ├── AT.py │ │ ├── FT.py │ │ ├── AB.py │ │ ├── NST.py │ │ ├── CC.py │ │ ├── FSP.py │ │ ├── PKT.py │ │ ├── RKD.py │ │ └── VID.py │ ├── checkpoint │ │ └── __init__.py │ ├── engine │ │ └── __init__.py │ ├── model_zoo │ │ └── __init__.py │ ├── structures │ │ └── __init__.py │ └── evaluation │ │ └── __init__.py ├── projects │ ├── 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 │ │ │ └── config.py │ │ └── train_net.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 │ │ ├── configs │ │ │ ├── densepose_rcnn_R_50_FPN_s1x.yaml │ │ │ ├── densepose_rcnn_R_101_FPN_s1x.yaml │ │ │ ├── quick_schedules │ │ │ │ ├── densepose_rcnn_R_50_FPN_instant_test.yaml │ │ │ │ ├── densepose_rcnn_R_50_FPN_inference_acc_test.yaml │ │ │ │ └── densepose_rcnn_R_50_FPN_training_acc_test.yaml │ │ │ └── Base-DensePose-RCNN-FPN.yaml │ │ ├── dev │ │ │ ├── README.md │ │ │ ├── run_instant_tests.sh │ │ │ └── run_inference_tests.sh │ │ ├── densepose │ │ │ ├── utils │ │ │ │ └── logger.py │ │ │ ├── __init__.py │ │ │ ├── config.py │ │ │ ├── vis │ │ │ │ └── bounding_box.py │ │ │ └── dataset.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 │ └── README.md ├── 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_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 │ │ ├── warp_faster_rcnn_R_50_C4_1x.yaml │ │ ├── warp_eval_faster_rcnn_R_50_C4_1x.yaml │ │ └── warp_finetune_faster_rcnn_R_50_C4_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 │ │ ├── scratch_mask_rcnn_R_50_FPN_3x_gn.yaml │ │ ├── mask_rcnn_R_50_FPN_3x_gn.yaml │ │ ├── mask_rcnn_R_50_FPN_3x_syncbn.yaml │ │ ├── panoptic_fpn_R_101_dconv_cascade_gn_3x.yaml │ │ └── cascade_mask_rcnn_X_152_32x8d_FPN_IN5k_gn_dconv.yaml │ ├── PascalVOC-Detection │ │ ├── retinanet_R_50_FPN_3x.yaml │ │ ├── faster_rcnn_R_50_FPN.yaml │ │ ├── faster_rcnn_R_50_C4.yaml │ │ └── iOD │ │ │ ├── align_10_p_10.yaml │ │ │ ├── align_19_p_1.yaml │ │ │ ├── align_15_p_5.yaml │ │ │ ├── eval.yaml │ │ │ ├── base_15.yaml │ │ │ ├── base_19.yaml │ │ │ ├── base_10.yaml │ │ │ ├── 10_p_10.yaml │ │ │ ├── 15_p_5.yaml │ │ │ ├── 19_p_1.yaml │ │ │ ├── ft_15_p_5.yaml │ │ │ ├── ft_10_p_10.yaml │ │ │ └── ft_19_p_1.yaml │ ├── Base-RCNN-C4.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 │ ├── linter.sh │ ├── run_instant_tests.sh │ └── run_inference_tests.sh ├── demo │ └── README.md ├── docker │ ├── docker-compose.yml │ ├── Dockerfile-circleci │ ├── README.md │ └── Dockerfile ├── datasets │ └── prepare_for_tests.sh ├── setup.cfg ├── demo.py ├── tools │ └── README.md └── README.md └── LICENSE /classification/utils/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /classification/trainer/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /detection/docs/.gitignore: -------------------------------------------------------------------------------- 1 | _build 2 | -------------------------------------------------------------------------------- /detection/docs/tutorials/install.md: -------------------------------------------------------------------------------- 1 | ../../INSTALL.md -------------------------------------------------------------------------------- /detection/docs/notes/contributing.md: -------------------------------------------------------------------------------- 1 | ../../.github/CONTRIBUTING.md -------------------------------------------------------------------------------- /detection/docs/tutorials/getting_started.md: -------------------------------------------------------------------------------- 1 | ../../GETTING_STARTED.md -------------------------------------------------------------------------------- /detection/tests/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | -------------------------------------------------------------------------------- /detection/detectron2/utils/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | -------------------------------------------------------------------------------- /detection/projects/TensorMask/tests/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | -------------------------------------------------------------------------------- /classification/utils/incremental/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # for incremental train and eval 4 | -------------------------------------------------------------------------------- /classification/utils/imagenet/__init__.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding=utf-8 3 | # for incremental-class train and eval 4 | -------------------------------------------------------------------------------- /detection/configs/quick_schedules/README.md: -------------------------------------------------------------------------------- 1 | These are quick configs for performance or accuracy regression tracking purposes. 2 | -------------------------------------------------------------------------------- /detection/projects/DensePose/doc/images/res_bbox_dp_u.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JosephKJ/ELI/HEAD/detection/projects/DensePose/doc/images/res_bbox_dp_u.jpg -------------------------------------------------------------------------------- /detection/projects/DensePose/doc/images/res_bbox_dp_v.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JosephKJ/ELI/HEAD/detection/projects/DensePose/doc/images/res_bbox_dp_v.jpg -------------------------------------------------------------------------------- /detection/projects/DensePose/doc/images/vis_bbox_dp_i.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JosephKJ/ELI/HEAD/detection/projects/DensePose/doc/images/vis_bbox_dp_i.jpg -------------------------------------------------------------------------------- /detection/projects/DensePose/doc/images/vis_bbox_dp_u.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JosephKJ/ELI/HEAD/detection/projects/DensePose/doc/images/vis_bbox_dp_u.jpg -------------------------------------------------------------------------------- /detection/projects/DensePose/doc/images/vis_bbox_dp_v.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JosephKJ/ELI/HEAD/detection/projects/DensePose/doc/images/vis_bbox_dp_v.jpg -------------------------------------------------------------------------------- /detection/projects/DensePose/doc/images/res_bbox_dp_segm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JosephKJ/ELI/HEAD/detection/projects/DensePose/doc/images/res_bbox_dp_segm.jpg -------------------------------------------------------------------------------- /detection/projects/DensePose/doc/images/vis_bbox_dp_pts.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JosephKJ/ELI/HEAD/detection/projects/DensePose/doc/images/vis_bbox_dp_pts.jpg -------------------------------------------------------------------------------- /detection/projects/DensePose/doc/images/vis_bbox_dp_segm.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JosephKJ/ELI/HEAD/detection/projects/DensePose/doc/images/vis_bbox_dp_segm.jpg -------------------------------------------------------------------------------- /detection/projects/DensePose/doc/images/res_bbox_dp_contour.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/JosephKJ/ELI/HEAD/detection/projects/DensePose/doc/images/res_bbox_dp_contour.jpg -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/docs/modules/layers.rst: -------------------------------------------------------------------------------- 1 | detectron2.layers package 2 | ========================= 3 | 4 | .. automodule:: detectron2.layers 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /detection/docs/modules/solver.rst: -------------------------------------------------------------------------------- 1 | detectron2.solver package 2 | ========================= 3 | 4 | .. automodule:: detectron2.solver 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/detectron2/layers/csrc/cuda_version.cu: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | namespace detectron2 { 4 | int get_cudart_version() { 5 | return CUDART_VERSION; 6 | } 7 | } // namespace detectron2 8 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/docs/notes/index.rst: -------------------------------------------------------------------------------- 1 | Notes 2 | ====================================== 3 | 4 | .. toctree:: 5 | :maxdepth: 2 6 | 7 | benchmarks 8 | compatibility 9 | contributing 10 | changelog 11 | -------------------------------------------------------------------------------- /detection/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 | __version__ = "0.1" 9 | -------------------------------------------------------------------------------- /detection/docs/modules/checkpoint.rst: -------------------------------------------------------------------------------- 1 | detectron2.checkpoint package 2 | ============================= 3 | 4 | .. automodule:: detectron2.checkpoint 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /detection/docs/modules/evaluation.rst: -------------------------------------------------------------------------------- 1 | detectron2.evaluation package 2 | ============================= 3 | 4 | .. automodule:: detectron2.evaluation 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /detection/docs/modules/structures.rst: -------------------------------------------------------------------------------- 1 | detectron2.structures package 2 | ============================= 3 | 4 | .. automodule:: detectron2.structures 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 4 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | - `parse_results.sh`: parse results from log file. 7 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/projects/DensePose/configs/densepose_rcnn_R_50_FPN_s1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "catalog://ImageNetPretrained/MSRA/R-50" 4 | RESNETS: 5 | DEPTH: 50 6 | SOLVER: 7 | MAX_ITER: 130000 8 | STEPS: (100000, 120000) 9 | 10 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/projects/DensePose/configs/densepose_rcnn_R_101_FPN_s1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "catalog://ImageNetPretrained/MSRA/R-101" 4 | RESNETS: 5 | DEPTH: 101 6 | SOLVER: 7 | MAX_ITER: 130000 8 | STEPS: (100000, 120000) 9 | 10 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | configs 16 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/retinanet_R_50_FPN_3x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RetinaNet.yaml" 2 | MODEL: 3 | WEIGHTS: "/tmp/pycharm_project_652/output/model_0049999.pth" 4 | RESNETS: 5 | DEPTH: 50 6 | DATASETS: 7 | TRAIN: ('voc_2007_trainval', 'voc_2012_trainval') 8 | TEST: ('voc_2007_test',) 9 | OUTPUT_DIR: ./output/retina -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/demo/README.md: -------------------------------------------------------------------------------- 1 | 2 | ## Detectron2 Demo 3 | 4 | We provide a command line tools for running a simple demo. 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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/projects/DensePose/configs/quick_schedules/densepose_rcnn_R_50_FPN_instant_test.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-DensePose-RCNN-FPN.yaml" 2 | MODEL: 3 | WEIGHTS: "catalog://ImageNetPretrained/MSRA/R-50" 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 | -------------------------------------------------------------------------------- /detection/docs/modules/config.rst: -------------------------------------------------------------------------------- 1 | detectron2.config package 2 | ========================= 3 | 4 | .. automodule:: detectron2.config 5 | :members: 6 | :undoc-members: 7 | :show-inheritance: 8 | 9 | 10 | Config References 11 | ----------------- 12 | 13 | .. literalinclude:: ../../detectron2/config/defaults.py 14 | :language: python 15 | :linenos: 16 | :lines: 4- 17 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/docs/notes/changelog.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | 4 | ### Notable Changes: 5 | 6 | * 2019-11-11: `detectron2.data.detection_utils.read_image` transposes images with exif information. 7 | * 2019-10-10: initial release. 8 | 9 | ### Config Version Change Log 10 | 11 | * v1: Rename `RPN_HEAD.NAME` to `RPN.HEAD_NAME`. 12 | * v2: A batch of rename of many configurations before release. 13 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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: "detectron2://e2e_densepose_R_50_FPN_s1x/124238535/model_final_5f3d7f.pkl" 4 | DATASETS: 5 | TRAIN: () 6 | TEST: ("densepose_coco_2014_minival_100",) 7 | TEST: 8 | EXPECTED_RESULTS: [["bbox", "AP", 56.05, 0.025], ["densepose", "AP", 46.54, 0.02]] 9 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/FitNet.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch.nn as nn 4 | 5 | 6 | class HintLoss(nn.Module): 7 | """Fitnets: hints for thin deep nets, ICLR 2015""" 8 | def __init__(self): 9 | super(HintLoss, self).__init__() 10 | self.crit = nn.MSELoss() 11 | 12 | def forward(self, f_s, f_t): 13 | loss = self.crit(f_s, f_t) 14 | return loss 15 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/__init__.py: -------------------------------------------------------------------------------- 1 | from .AB import ABLoss 2 | from .AT import Attention 3 | from .CC import Correlation 4 | from .FitNet import HintLoss 5 | from .FSP import FSP 6 | from .FT import FactorTransfer 7 | from .KD import DistillKL 8 | from .KDSVD import KDSVD 9 | from .NST import NSTLoss 10 | from .PKT import PKT 11 | from .RKD import RKDLoss 12 | from .SP import Similarity 13 | from .VID import VIDLoss 14 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | WEIGHTS: "" 9 | MASK_ON: True 10 | BACKBONE: 11 | FREEZE_AT: 0 12 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | 19 | -------------------------------------------------------------------------------- /detection/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 8 | 9 | __all__ = ["get", "get_config_file"] 10 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | 5 | # Build the docs: 6 | 7 | 1. Install detectron2 according to [INSTALL.md](INSTALL.md). 8 | 2. Install additional libraries required to build docs: 9 | - docutils>=0.14 10 | - Sphinx>=1.7 11 | - recommonmark==0.4.0 12 | - sphinx_rtd_theme 13 | - mock 14 | 15 | 3. Run `make html` from this directory. 16 | -------------------------------------------------------------------------------- /detection/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 15 | git+git://github.com/facebookresearch/fvcore.git 16 | https://download.pytorch.org/whl/nightly/cpu/torch-1.3.0.dev20191010%2Bcpu-cp37-cp37m-linux_x86_64.whl 17 | https://download.pytorch.org/whl/nightly/cpu/torchvision-0.5.0.dev20191008%2Bcpu-cp37-cp37m-linux_x86_64.whl 18 | -------------------------------------------------------------------------------- /detection/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: "catalog://ImageNetPretrained/MSRA/R-50" 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", "AP", 42.47, 1.5]] 14 | 15 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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: 2 20 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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',) 14 | TEST: ('voc_2007_test',) 15 | SOLVER: 16 | STEPS: (7000, 8000) 17 | MAX_ITER: 9000 # 17.4 epochs 18 | WARMUP_ITERS: 100 19 | OUTPUT_DIR: ./output/all_20_std_cfg_new -------------------------------------------------------------------------------- /detection/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 4 | from .mask_head import ROI_MASK_HEAD_REGISTRY, build_mask_head 5 | from .roi_heads import ( 6 | ROI_HEADS_REGISTRY, 7 | ROIHeads, 8 | StandardROIHeads, 9 | build_roi_heads, 10 | select_foreground_proposals, 11 | ) 12 | from .rotated_fast_rcnn import RROIHeads 13 | 14 | from . import cascade_rcnn # isort:skip 15 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/KD.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | 7 | class DistillKL(nn.Module): 8 | """Distilling the Knowledge in a Neural Network""" 9 | def __init__(self, T): 10 | super(DistillKL, self).__init__() 11 | self.T = T 12 | 13 | def forward(self, y_s, y_t): 14 | p_s = F.log_softmax(y_s/self.T, dim=1) 15 | p_t = F.softmax(y_t/self.T, dim=1) 16 | loss = F.kl_div(p_s, p_t, size_average=False) * (self.T**2) / y_s.shape[0] 17 | return loss 18 | -------------------------------------------------------------------------------- /detection/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: False 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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/detectron2/data/datasets/finetune_dataset.py: -------------------------------------------------------------------------------- 1 | import os 2 | import torch 3 | import torch.utils.data as data 4 | from fvcore.common.file_io import PathManager 5 | 6 | 7 | class ImageStoreDataset(data.Dataset): 8 | def __init__(self, cfg): 9 | path = os.path.join(cfg.WG.IMAGE_STORE_LOC, 'image_store.pth') 10 | with PathManager.open(path, 'rb') as f: 11 | image_store = torch.load(f) 12 | self.images = image_store.retrieve() 13 | 14 | def __getitem__(self, index): 15 | return self.images[index] 16 | 17 | def __len__(self): 18 | return len(self.images) 19 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 (Coming Soon) 13 | + PointRend: Image Segmentation as Rendering (Coming Soon) 14 | -------------------------------------------------------------------------------- /detection/detectron2/utils/pickling.py: -------------------------------------------------------------------------------- 1 | from store import Store 2 | import torch 3 | import os 4 | import pickle 5 | 6 | store = Store(10, 3) 7 | store.add(('a', 'b', 'c', 'd', 'e', 'f'), (1, 1, 9, 1, 0, 1)) 8 | store.add(('h',), (4,)) 9 | print(store.retrieve()) 10 | 11 | IMAGE_STORE_LOC = '/home/joseph/detectron2' 12 | 13 | file_path = os.path.join(IMAGE_STORE_LOC, "image_store.pth.726047903") 14 | 15 | # torch.save(store, file_path) 16 | obj = torch.load(file_path) 17 | 18 | # with open(file_path, 'wb') as f: 19 | # pickle.dump(store, f) 20 | 21 | # with open(file_path, 'rb') as f: 22 | # obj = pickle.load(f) 23 | 24 | print(len(obj)) 25 | -------------------------------------------------------------------------------- /detection/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.8, 0.8], ["segm", "AP", 35.7, 0.8]] 22 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 .evaluator import DatasetEvaluator, DatasetEvaluators, inference_context, inference_on_dataset 5 | from .lvis_evaluation import LVISEvaluator 6 | from .panoptic_evaluation import COCOPanopticEvaluator 7 | from .pascal_voc_evaluation import PascalVOCDetectionEvaluator 8 | from .sem_seg_evaluation import SemSegEvaluator 9 | from .testing import print_csv_format, verify_results 10 | 11 | __all__ = [k for k in globals().keys() if not k.startswith("_")] 12 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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", 63.99, 0.9], ["panoptic_seg", "PQ", 48.23, 0.8]] 21 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | assert isinstance(model, GeneralizedRCNN), model 15 | assert isinstance(model.backbone, FPN), model.backbone 16 | 17 | def test_get_invalid_model(self): 18 | self.assertRaises(RuntimeError, model_zoo.get, "Invalid/config.yaml") 19 | 20 | 21 | if __name__ == "__main__": 22 | unittest.main() 23 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/tests/test_sampler.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 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 | s = GroupedBatchSampler(sampler, group_ids, 2) 13 | 14 | for k in s: 15 | self.assertEqual(len(k), 2) 16 | 17 | def test_groups(self): 18 | sampler = SequentialSampler(list(range(100))) 19 | group_ids = [1, 0] * 50 20 | s = GroupedBatchSampler(sampler, group_ids, 2) 21 | 22 | for k in s: 23 | self.assertTrue((k[0] + k[1]) % 2 == 0) 24 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | 24 | return _C.box_iou_rotated(boxes1, boxes2) 25 | -------------------------------------------------------------------------------- /classification/scripts/cifar_100_10_task.sh: -------------------------------------------------------------------------------- 1 | python main.py --nb_cl_fg=50 --nb_cl=5 --gpu=1 --random_seed=1993 --baseline=icarl --branch_mode=single --branch_1=free --dataset=cifar100 --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1012_114011 --evaluate_with_ebm | tee cifar_100_10_task_icarl.txt 2 | 3 | python main.py --nb_cl_fg=50 --nb_cl=5 --gpu=1 --random_seed=1993 --baseline=lucir --branch_mode=single --branch_1=free --dataset=cifar100 --resume_fg --evaluate_with_ebm --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1011_214547 | tee cifar_100_10_task_lucir.txt 4 | 5 | python main.py --nb_cl_fg=50 --nb_cl=5 --gpu=1 --random_seed=1993 --baseline=lucir --branch_mode=dual --branch_1=free --branch_2=ss --dataset=cifar100 --resume_fg --evaluate_with_ebm --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1027_114226 | tee cifar_100_10_task_aanet.txt 6 | -------------------------------------------------------------------------------- /classification/scripts/cifar_100_25_task.sh: -------------------------------------------------------------------------------- 1 | python main.py --nb_cl_fg=50 --nb_cl=2 --gpu=2 --random_seed=1993 --baseline=icarl --branch_mode=single --branch_1=free --dataset=cifar100 --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1012_153758 --evaluate_with_ebm | tee cifar_100_25_task_icarl.txt 2 | 3 | python main.py --nb_cl_fg=50 --nb_cl=2 --gpu=2 --random_seed=1993 --baseline=lucir --branch_mode=single --branch_1=free --dataset=cifar100 --resume_fg --evaluate_with_ebm --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1012_014006 | tee cifar_100_25_task_lucir.txt 4 | 5 | python main.py --nb_cl_fg=50 --nb_cl=2 --gpu=2 --random_seed=1993 --baseline=lucir --branch_mode=dual --branch_1=free --branch_2=ss --dataset=cifar100 --resume_fg --evaluate_with_ebm --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1027_114241 | tee cifar_100_25_task_aanet.txt 6 | -------------------------------------------------------------------------------- /classification/scripts/cifar_100_5_task.sh: -------------------------------------------------------------------------------- 1 | python main.py --nb_cl_fg=50 --nb_cl=10 --gpu=3 --random_seed=1993 --baseline=icarl --branch_mode=single --branch_1=free --dataset=cifar100 --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1011_081348 --evaluate_with_ebm | tee cifar_100_5_task_icarl.txt 2 | 3 | python main.py --nb_cl_fg=50 --nb_cl=10 --gpu=3 --random_seed=1993 --baseline=lucir --branch_mode=single --branch_1=free --dataset=cifar100 --resume_fg --evaluate_with_ebm --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1011_073810 | tee cifar_100_5_task_lucir.txt 4 | 5 | python main.py --nb_cl_fg=50 --nb_cl=10 --gpu=3 --random_seed=1993 --baseline=lucir --branch_mode=dual --branch_1=free --branch_2=ss --dataset=cifar100 --resume_fg --evaluate_with_ebm --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1027_104241 | tee cifar_100_5_task_aanet.txt 6 | -------------------------------------------------------------------------------- /detection/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 the setting used in Mask R-CNN paper, Appendix A 10 | INPUT: 11 | MIN_SIZE_TRAIN: (800, 832, 864, 896, 928, 960, 992, 1024) 12 | MIN_SIZE_TRAIN_SAMPLING: "choice" 13 | MIN_SIZE_TEST: 1024 14 | MAX_SIZE_TRAIN: 2048 15 | MAX_SIZE_TEST: 2048 16 | DATASETS: 17 | TRAIN: ("cityscapes_fine_instance_seg_train",) 18 | TEST: ("cityscapes_fine_instance_seg_val",) 19 | SOLVER: 20 | BASE_LR: 0.01 21 | STEPS: (18000,) 22 | MAX_ITER: 24000 23 | IMS_PER_BATCH: 8 24 | TEST: 25 | EVAL_PERIOD: 8000 26 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/setup.cfg: -------------------------------------------------------------------------------- 1 | [isort] 2 | line_length=100 3 | multi_line_output=3 4 | include_trailing_comma=True 5 | known_standard_library=numpy,setuptools 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 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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | { 7 | black --version | grep "19.3b0" > /dev/null 8 | } || { 9 | echo "Linter requires black==19.3b0 !" 10 | exit 1 11 | } 12 | 13 | set -v 14 | 15 | echo "Running isort ..." 16 | isort -y -sp . --atomic 17 | 18 | echo "Running black ..." 19 | black -l 100 . 20 | 21 | echo "Running flake8 ..." 22 | if [ -x "$(command -v flake8-3)" ]; then 23 | flake8-3 . 24 | else 25 | python3 -m flake8 . 26 | fi 27 | 28 | # echo "Running mypy ..." 29 | # Pytorch does not have enough type annotations 30 | # mypy detectron2/solver detectron2/structures detectron2/config 31 | 32 | echo "Running clang-format ..." 33 | find . -regex ".*\.\(cpp\|c\|cc\|cu\|cxx\|h\|hh\|hpp\|hxx\|tcc\|mm\|m\)" -print0 | xargs -0 clang-format -i 34 | 35 | command -v arc > /dev/null && arc lint 36 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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. 8 | One such example is provided in [tools/plain_train_net.py](https://github.com/facebookresearch/detectron2/blob/master/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 does single-cost single-optimizer single-data-source training. 17 | Or use [DefaultTrainer().train()](../modules/engine.html#detectron2.engine.defaults.DefaultTrainer) 18 | which includes more standard behavior that one might want to opt in. 19 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/demo.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | from detectron2.utils.logger import setup_logger 3 | setup_logger() 4 | 5 | from detectron2.config import get_cfg 6 | from detectron2.engine import DefaultPredictor 7 | from detectron2.utils.visualizer import Visualizer 8 | from detectron2.data import MetadataCatalog 9 | 10 | # Get image 11 | im = cv2.imread("/home/joseph/workspace/eb-iOD/datasets/VOC2007/JPEGImages/000112.jpg") 12 | 13 | # Get the configuration ready 14 | cfg = get_cfg() 15 | cfg.merge_from_file("configs/PascalVOC-Detection/iOD/align_15_p_5.yaml") 16 | # cfg.MODEL.WEIGHTS = "/home/joseph/PycharmProjects/detectron2/output/model_final.pth" 17 | cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 18 | 19 | predictor = DefaultPredictor(cfg) 20 | outputs = predictor(im) 21 | 22 | print(outputs["instances"].pred_classes) 23 | 24 | v = Visualizer(im[:,:,::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2) 25 | v = v.draw_instance_predictions(outputs['instances'].to('cpu')) 26 | img = v.get_image()[:, :, ::-1] 27 | cv2.imwrite('output.jpg', img) 28 | 29 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/SP.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch 4 | import torch.nn as nn 5 | import torch.nn.functional as F 6 | 7 | 8 | class Similarity(nn.Module): 9 | """Similarity-Preserving Knowledge Distillation, ICCV2019, verified by original author""" 10 | def __init__(self): 11 | super(Similarity, self).__init__() 12 | 13 | def forward(self, g_s, g_t): 14 | return [self.similarity_loss(f_s, f_t) for f_s, f_t in zip(g_s, g_t)] 15 | 16 | def similarity_loss(self, f_s, f_t): 17 | bsz = f_s.shape[0] 18 | f_s = f_s.view(bsz, -1) 19 | f_t = f_t.view(bsz, -1) 20 | 21 | G_s = torch.mm(f_s, torch.t(f_s)) 22 | # G_s = G_s / G_s.norm(2) 23 | G_s = torch.nn.functional.normalize(G_s) 24 | G_t = torch.mm(f_t, torch.t(f_t)) 25 | # G_t = G_t / G_t.norm(2) 26 | G_t = torch.nn.functional.normalize(G_t) 27 | 28 | G_diff = G_t - G_s 29 | loss = (G_diff * G_diff).view(-1, 1).sum(0) / (bsz * bsz) 30 | return loss 31 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/AT.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | 7 | class Attention(nn.Module): 8 | """Paying More Attention to Attention: Improving the Performance of Convolutional Neural Networks 9 | via Attention Transfer 10 | code: https://github.com/szagoruyko/attention-transfer""" 11 | def __init__(self, p=2): 12 | super(Attention, self).__init__() 13 | self.p = p 14 | 15 | def forward(self, g_s, g_t): 16 | return [self.at_loss(f_s, f_t) for f_s, f_t in zip(g_s, g_t)] 17 | 18 | def at_loss(self, f_s, f_t): 19 | s_H, t_H = f_s.shape[2], f_t.shape[2] 20 | if s_H > t_H: 21 | f_s = F.adaptive_avg_pool2d(f_s, (t_H, t_H)) 22 | elif s_H < t_H: 23 | f_t = F.adaptive_avg_pool2d(f_t, (s_H, s_H)) 24 | else: 25 | pass 26 | return (self.at(f_s) - self.at(f_t)).pow(2).mean() 27 | 28 | def at(self, f): 29 | return F.normalize(f.pow(self.p).mean(1).view(f.size(0), -1)) 30 | -------------------------------------------------------------------------------- /classification/utils/imagenet/utils_dataset.py: -------------------------------------------------------------------------------- 1 | ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 | ## Copied from: https://github.com/hshustc/CVPR19_Incremental_Learning 3 | ## 4 | ## This source code is licensed under the MIT-style license found in the 5 | ## LICENSE file in the root directory of this source tree 6 | ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 7 | """ Tools for ImageNet """ 8 | import argparse 9 | import os 10 | import shutil 11 | import time 12 | import numpy as np 13 | 14 | def split_images_labels(imgs): 15 | images = [] 16 | labels = [] 17 | for item in imgs: 18 | images.append(item[0]) 19 | labels.append(item[1]) 20 | 21 | return np.array(images), np.array(labels) 22 | 23 | def merge_images_labels(images, labels): 24 | images = list(images) 25 | labels = list(labels) 26 | assert(len(images)==len(labels)) 27 | imgs = [] 28 | for i in range(len(images)): 29 | item = (images[i], labels[i]) 30 | imgs.append(item) 31 | 32 | return imgs 33 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/FT.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | 7 | class FactorTransfer(nn.Module): 8 | """Paraphrasing Complex Network: Network Compression via Factor Transfer, NeurIPS 2018""" 9 | def __init__(self, p1=2, p2=1): 10 | super(FactorTransfer, self).__init__() 11 | self.p1 = p1 12 | self.p2 = p2 13 | 14 | def forward(self, f_s, f_t): 15 | return self.factor_loss(f_s, f_t) 16 | 17 | def factor_loss(self, f_s, f_t): 18 | s_H, t_H = f_s.shape[2], f_t.shape[2] 19 | if s_H > t_H: 20 | f_s = F.adaptive_avg_pool2d(f_s, (t_H, t_H)) 21 | elif s_H < t_H: 22 | f_t = F.adaptive_avg_pool2d(f_t, (s_H, s_H)) 23 | else: 24 | pass 25 | if self.p2 == 1: 26 | return (self.factor(f_s) - self.factor(f_t)).abs().mean() 27 | else: 28 | return (self.factor(f_s) - self.factor(f_t)).pow(self.p2).mean() 29 | 30 | def factor(self, f): 31 | return F.normalize(f.pow(self.p1).mean(1).view(f.size(0), -1)) 32 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Joseph K J 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /classification/LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Joseph K J 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 unavailable") 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 | assert 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 | -------------------------------------------------------------------------------- /classification/scripts/mini_imagenet_5_task.sh: -------------------------------------------------------------------------------- 1 | python main.py --nb_cl_fg=50 --nb_cl=10 --gpu=0 --random_seed=1993 --baseline=icarl --branch_mode=single --branch_1=free --dataset=imagenet_sub --evaluate_with_ebm --ebm_latent_dim 512 --num_workers 16 --epochs 90 --custom_weight_decay 1e-4 --ckpt_dir_fg=./base_50_imagenet_sub.pt --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1025_093429 | tee imagenet_subset_5_task_icarl.log 2 | 3 | python main.py --nb_cl_fg=50 --nb_cl=10 --gpu=0 --random_seed=1993 --baseline=lucir --branch_mode=single --branch_1=free --dataset=imagenet_sub --evaluate_with_ebm --ebm_latent_dim 512 --num_workers 16 --epochs 90 --custom_weight_decay 1e-4 --ckpt_dir_fg=./base_50_imagenet_sub.pt --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1026_042951 | tee imagenet_subset_5_task_lucir.log 4 | 5 | python main.py --nb_cl_fg=50 --nb_cl=10 --gpu=0 --random_seed=1993 --baseline=lucir --branch_mode=dual --branch_1=ss --branch_2=fixed --dataset=imagenet_sub --evaluate_with_ebm --ebm_latent_dim 512 --num_workers 16 --epochs 90 --custom_weight_decay 1e-4 --ckpt_dir_fg=./base_50_imagenet_sub.pt --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1105_033111 | tee imagenet_subset_5_task_aanet.log -------------------------------------------------------------------------------- /detection/configs/COCO-Detection/warp_faster_rcnn_R_50_C4_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "/home/joseph/detectron2/output/coco_first_40/model_final.pth" 4 | BASE_WEIGHTS: "/home/joseph/detectron2/output/coco_first_40/model_final.pth" 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | ROI_HEADS: 9 | NUM_CLASSES: 80 10 | LEARN_INCREMENTALLY: True 11 | TRAIN_ON_BASE_CLASSES: False 12 | NUM_BASE_CLASSES: 40 13 | NUM_NOVEL_CLASSES: 40 14 | NMS_THRESH_TEST: 0.4 15 | DATASETS: 16 | TRAIN: ("coco_2014_train",) 17 | TEST: ("coco_2014_val",) 18 | 19 | OUTPUT_DIR: ./output/coco_next_40 20 | SEED: 3074309 21 | VERSION: 2 22 | 23 | SOLVER: 24 | IMS_PER_BATCH: 16 25 | BASE_LR: 0.02 26 | STEPS: (140000, 160000) 27 | MAX_ITER: 180000 28 | 29 | WG: 30 | ENABLE: True 31 | TRAIN_WARP_AT_ITR_NO: 20 32 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 33 | NUM_FEATURES_PER_CLASS: 100 34 | NUM_IMAGES_PER_CLASS: 10 35 | BATCH_SIZE: 2 36 | USE_FEATURE_STORE: True 37 | IMAGE_STORE_LOC: '/home/joseph/detectron2/40_image_store.pth' 38 | 39 | DISTILL: 40 | ENABLE: True 41 | BACKBONE: True 42 | RPN: False 43 | ROI_HEADS: True 44 | ONLY_FG_ROIS: False 45 | LOSS_WEIGHT: 0.2 -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/AB.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch 4 | import torch.nn as nn 5 | 6 | 7 | class ABLoss(nn.Module): 8 | """Knowledge Transfer via Distillation of Activation Boundaries Formed by Hidden Neurons 9 | code: https://github.com/bhheo/AB_distillation 10 | """ 11 | def __init__(self, feat_num, margin=1.0): 12 | super(ABLoss, self).__init__() 13 | self.w = [2**(i-feat_num+1) for i in range(feat_num)] 14 | self.margin = margin 15 | 16 | def forward(self, g_s, g_t): 17 | bsz = g_s[0].shape[0] 18 | losses = [self.criterion_alternative_l2(s, t) for s, t in zip(g_s, g_t)] 19 | losses = [w * l for w, l in zip(self.w, losses)] 20 | # loss = sum(losses) / bsz 21 | # loss = loss / 1000 * 3 22 | losses = [l / bsz for l in losses] 23 | losses = [l / 1000 * 3 for l in losses] 24 | return losses 25 | 26 | def criterion_alternative_l2(self, source, target): 27 | loss = ((source + self.margin) ** 2 * ((source > -self.margin) & (target <= 0)).float() + 28 | (source - self.margin) ** 2 * ((source <= self.margin) & (target > 0)).float()) 29 | return torch.abs(loss).sum() 30 | -------------------------------------------------------------------------------- /classification/utils/imagenet/setup_data/generate_imagenet.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import torchvision.datasets as datasets 4 | from PIL import Image 5 | 6 | src_root_dir = 'data/imagenet/data/' 7 | des_root_dir = 'data/imagenet_resized_256/data/' 8 | if not os.path.exists(des_root_dir): 9 | os.makedirs(des_root_dir) 10 | 11 | phase_list = ['train', 'val'] 12 | for phase in phase_list: 13 | if not os.path.exists(os.path.join(des_root_dir, phase)): 14 | os.mkdir(os.path.join(des_root_dir, phase)) 15 | data_dir = os.path.join(src_root_dir, phase) 16 | tg_dataset = datasets.ImageFolder(data_dir) 17 | for cls_name in tg_dataset.classes: 18 | if not os.path.exists(os.path.join(des_root_dir, phase, cls_name)): 19 | os.mkdir(os.path.join(des_root_dir, phase, cls_name)) 20 | cnt = 0 21 | for item in tg_dataset.imgs: 22 | img_path = item[0] 23 | img = Image.open(img_path) 24 | img = img.convert('RGB') 25 | save_path = img_path.replace('imagenet', 'imagenet_resized_256') 26 | resized_img = img.resize((256,256), Image.BILINEAR) 27 | resized_img.save(save_path) 28 | cnt = cnt+1 29 | if cnt % 1000 == 0: 30 | print(cnt, save_path) 31 | 32 | print("Generation finished.") -------------------------------------------------------------------------------- /classification/scripts/mini_imagenet_10_task.sh: -------------------------------------------------------------------------------- 1 | # 10 Task iCaRL 2 | python main.py --nb_cl_fg=50 --nb_cl=5 --gpu=0 --random_seed=1993 --baseline=icarl --branch_mode=single --branch_1=free --dataset=imagenet_sub --evaluate_with_ebm --ebm_latent_dim 512 --num_workers 16 --epochs 90 --custom_weight_decay 1e-4 --ckpt_dir_fg=./base_50_imagenet_sub.pt --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1026_045637 | tee imagenet_subset_10_task_icarl.log 3 | 4 | # 10 Task LUCIR 5 | python main.py --nb_cl_fg=50 --nb_cl=5 --gpu=0 --random_seed=1993 --baseline=lucir --branch_mode=single --branch_1=free --dataset=imagenet_sub --evaluate_with_ebm --ebm_latent_dim 512 --num_workers 16 --epochs 90 --custom_weight_decay 1e-4 --ckpt_dir_fg=./base_50_imagenet_sub.pt --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1026_050528 | tee imagenet_subset_10_task_lucir.log 6 | 7 | python main.py --nb_cl_fg=50 --nb_cl=5 --gpu=0 --random_seed=1993 --baseline=lucir --branch_mode=dual --branch_1=ss --branch_2=fixed --dataset=imagenet_sub --evaluate_with_ebm --ebm_latent_dim 512 --num_workers 16 --epochs 90 --custom_weight_decay 1e-4 --ebm_n_layers 0 --ckpt_dir_fg=./base_50_imagenet_sub.pt --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1105_033045 | tee imagenet_subset_10_task_aanet.log 8 | -------------------------------------------------------------------------------- /detection/configs/COCO-Detection/warp_eval_faster_rcnn_R_50_C4_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "/home/joseph/detectron2/output/coco_next_40_FT/model_final.pth" 4 | BASE_WEIGHTS: "/home/joseph/detectron2/output/coco_next_40_FT/model_final.pth" # coco_next_40_FT 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | ROI_HEADS: 9 | NUM_CLASSES: 80 10 | LEARN_INCREMENTALLY: False 11 | TRAIN_ON_BASE_CLASSES: False 12 | NUM_BASE_CLASSES: 40 13 | NUM_NOVEL_CLASSES: 40 14 | NMS_THRESH_TEST: 0.4 15 | DATASETS: 16 | TRAIN: ("coco_2014_train",) 17 | # TEST: ("coco_2014_minival",) 18 | TEST: ("coco_2014_val",) 19 | 20 | OUTPUT_DIR: ./output/coco_base_minival 21 | SEED: 3074309 22 | VERSION: 2 23 | 24 | SOLVER: 25 | IMS_PER_BATCH: 16 26 | BASE_LR: 0.02 27 | STEPS: (140000, 160000) 28 | MAX_ITER: 180000 29 | 30 | WG: 31 | ENABLE: False 32 | TRAIN_WARP_AT_ITR_NO: 20 33 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 34 | NUM_FEATURES_PER_CLASS: 100 35 | NUM_IMAGES_PER_CLASS: 10 36 | BATCH_SIZE: 2 37 | USE_FEATURE_STORE: True 38 | IMAGE_STORE_LOC: '/home/joseph/detectron2/40_image_store.pth' 39 | 40 | DISTILL: 41 | ENABLE: False 42 | BACKBONE: True 43 | RPN: False 44 | ROI_HEADS: True 45 | ONLY_FG_ROIS: False 46 | LOSS_WEIGHT: 0.2 -------------------------------------------------------------------------------- /classification/scripts/mini_imagenet_25_task.sh: -------------------------------------------------------------------------------- 1 | # 25 Task iCaRL 2 | python main.py --nb_cl_fg=50 --nb_cl=2 --gpu=0 --random_seed=1993 --baseline=icarl --branch_mode=single --branch_1=free --dataset=imagenet_sub --evaluate_with_ebm --ebm_latent_dim 512 --num_workers 16 --epochs 90 --custom_weight_decay 1e-4 --ckpt_dir_fg=./base_50_imagenet_sub.pt --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1026_050900 | tee imagenet_subset_25_task_icarl.log 3 | 4 | # 25 Task LUCIR 5 | python main.py --nb_cl_fg=50 --nb_cl=2 --gpu=0 --random_seed=1993 --baseline=lucir --branch_mode=single --branch_1=free --dataset=imagenet_sub --evaluate_with_ebm --ebm_latent_dim 512 --num_workers 16 --epochs 90 --custom_weight_decay 1e-4 --ckpt_dir_fg=./base_50_imagenet_sub.pt --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1114_025033 | tee imagenet_subset_25_task_lucir.log 6 | 7 | # 25 Task AANET 8 | python main.py --nb_cl_fg=50 --nb_cl=2 --gpu=0 --random_seed=1993 --baseline=lucir --branch_mode=dual --branch_1=ss --branch_2=fixed --dataset=imagenet_sub --evaluate_with_ebm --ebm_latent_dim 512 --num_workers 16 --epochs 90 --custom_weight_decay 1e-4 --ebm_n_layers 0 --ckpt_dir_fg=./base_50_imagenet_sub.pt --resume_fg --resume_with_ebm_training --ckpt_loc=./model_checkpoints/1105_033050 | tee imagenet_subset_25_task_aanet.log 9 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /classification/utils/imagenet/setup_data/generate_imagenet_subset.py: -------------------------------------------------------------------------------- 1 | import os 2 | import numpy as np 3 | 4 | import torchvision.datasets as datasets 5 | 6 | data_dir = '/proj/vot2021/x_fahkh/joseph/imagenet' 7 | 8 | # Data loading code 9 | traindir = os.path.join(data_dir, 'train') 10 | train_dataset = datasets.ImageFolder(traindir, None) 11 | classes = train_dataset.classes 12 | print("the number of total classes: {}".format(len(classes))) 13 | 14 | seed = 1993 15 | np.random.seed(seed) 16 | subset_num = 100 17 | subset_classes = np.random.choice(classes, subset_num, replace=False) 18 | print("the number of subset classes: {}".format(len(subset_classes))) 19 | print(subset_classes) 20 | 21 | des_root_dir = '/proj/vot2021/x_fahkh/joseph/xLantern/data/seed_{}_subset_{}_imagenet/data/'.format(seed, subset_num) 22 | if not os.path.exists(des_root_dir): 23 | os.makedirs(des_root_dir) 24 | phase_list = ['train', 'val'] 25 | for phase in phase_list: 26 | if not os.path.exists(os.path.join(des_root_dir, phase)): 27 | os.mkdir(os.path.join(des_root_dir, phase)) 28 | for sc in subset_classes: 29 | src_dir = os.path.join(data_dir, phase, sc) 30 | des_dir = os.path.join(des_root_dir, phase, sc) 31 | cmd = "cp -r {} {}".format(src_dir, des_dir) 32 | print(cmd) 33 | os.system(cmd) 34 | 35 | print("Generation finished.") -------------------------------------------------------------------------------- /detection/configs/COCO-Detection/warp_finetune_faster_rcnn_R_50_C4_1x.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "/home/joseph/detectron2/output/coco_next_40/model_0144999.pth" 4 | BASE_WEIGHTS: "/home/joseph/detectron2/output/coco_next_40/model_0144999.pth" 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | ROI_HEADS: 9 | NUM_CLASSES: 80 10 | LEARN_INCREMENTALLY: False 11 | TRAIN_ON_BASE_CLASSES: False 12 | NUM_BASE_CLASSES: 40 13 | NUM_NOVEL_CLASSES: 40 14 | NMS_THRESH_TEST: 0.4 15 | DATASETS: 16 | TRAIN: ("coco_2014_train",) 17 | TEST: ("coco_2014_val",) 18 | 19 | OUTPUT_DIR: ./output/coco_next_40_144999_FT 20 | SEED: 3074309 21 | VERSION: 2 22 | 23 | SOLVER: 24 | IMS_PER_BATCH: 16 25 | BASE_LR: 0.02 26 | STEPS: (140000, 160000) 27 | MAX_ITER: 160100 28 | 29 | WG: 30 | ENABLE: False 31 | TRAIN_WARP_AT_ITR_NO: 20 32 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 33 | NUM_FEATURES_PER_CLASS: 100 34 | NUM_IMAGES_PER_CLASS: 10 35 | BATCH_SIZE: 2 36 | USE_FEATURE_STORE: True 37 | IMAGE_STORE_LOC: '/home/joseph/detectron2/40_image_store_ft.pth' 38 | 39 | DISTILL: 40 | ENABLE: False 41 | BACKBONE: True 42 | RPN: False 43 | ROI_HEADS: True 44 | ONLY_FG_ROIS: False 45 | LOSS_WEIGHT: 0.2 46 | 47 | FINETUNE: 48 | MIN_NUM_IMG_PER_CLASS: 10 49 | BATCH_SIZE: 2 50 | USE_IMAGE_STORE: False -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/align_10_p_10.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "./output/10_p_10_ft/model_final.pth" 4 | BASE_WEIGHTS: "./output/first_10/model_final.pth" 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | ROI_HEADS: 9 | # Maximum number of foreground classes to expect 10 | NUM_CLASSES: 20 11 | # Flag to turn on/off Incremental Learning 12 | LEARN_INCREMENTALLY: True 13 | # Flag to select whether to learn base classes or iOD expanded classes 14 | TRAIN_ON_BASE_CLASSES: False 15 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 16 | NUM_BASE_CLASSES: 10 17 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 18 | NUM_NOVEL_CLASSES: 10 19 | POSITIVE_FRACTION: 0.25 20 | NMS_THRESH_TEST: 0.4 21 | RPN: 22 | FREEZE_WEIGHTS: False 23 | ROI_BOX_HEAD: 24 | CLS_AGNOSTIC_BBOX_REG: True 25 | INPUT: 26 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 27 | MIN_SIZE_TEST: 800 28 | DATASETS: 29 | TRAIN: ('voc_2007_trainval',) 30 | TEST: ('voc_2007_test',) 31 | OUTPUT_DIR: ./output/align_10_p_10 32 | SEED: 9999 33 | VERSION: 2 34 | SOLVER: 35 | MAX_ITER: 41450 36 | EBM_ALIGNER: 37 | ENABLE: True 38 | FEAT_SAVE_LOCATION: 'features' 39 | LANGEVIN_LR: 0.01 -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/align_19_p_1.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "./output/19_p_1_ft/model_final.pth" 4 | BASE_WEIGHTS: "./output/first_19/model_final.pth" 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | ROI_HEADS: 9 | # Maximum number of foreground classes to expect 10 | NUM_CLASSES: 20 11 | # Flag to turn on/off Incremental Learning 12 | LEARN_INCREMENTALLY: True 13 | # Flag to select whether to learn base classes or iOD expanded classes 14 | TRAIN_ON_BASE_CLASSES: False 15 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 16 | NUM_BASE_CLASSES: 19 17 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 18 | NUM_NOVEL_CLASSES: 1 19 | POSITIVE_FRACTION: 0.25 20 | NMS_THRESH_TEST: 0.3 21 | RPN: 22 | FREEZE_WEIGHTS: False 23 | ROI_BOX_HEAD: 24 | CLS_AGNOSTIC_BBOX_REG: True 25 | INPUT: 26 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 27 | MIN_SIZE_TEST: 800 28 | DATASETS: 29 | TRAIN: ('voc_2007_trainval',) 30 | TEST: ('voc_2007_test',) 31 | OUTPUT_DIR: ./output/align_19_p_1 32 | SEED: 9999 33 | VERSION: 2 34 | SOLVER: 35 | MAX_ITER: 23450 36 | EBM_ALIGNER: 37 | ENABLE: True 38 | FEAT_SAVE_LOCATION: 'features' 39 | EBM_N_LAYERS: 0 40 | LANGEVIN_LR: 0.0001 -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/align_15_p_5.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "/home/joseph/workspace/eb-iOD/output/15_p_5_ft/model_final.pth" 4 | BASE_WEIGHTS: "/home/joseph/workspace/eb-iOD/output/first_15/model_final.pth" 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | ROI_HEADS: 9 | # Maximum number of foreground classes to expect 10 | NUM_CLASSES: 20 11 | # Flag to turn on/off Incremental Learning 12 | LEARN_INCREMENTALLY: True 13 | # Flag to select whether to learn base classes or iOD expanded classes 14 | TRAIN_ON_BASE_CLASSES: False 15 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 16 | NUM_BASE_CLASSES: 15 17 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 18 | NUM_NOVEL_CLASSES: 5 19 | POSITIVE_FRACTION: 0.25 20 | NMS_THRESH_TEST: 0.2 21 | RPN: 22 | FREEZE_WEIGHTS: False 23 | ROI_BOX_HEAD: 24 | CLS_AGNOSTIC_BBOX_REG: True 25 | INPUT: 26 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 27 | MIN_SIZE_TEST: 800 28 | DATASETS: 29 | TRAIN: ('voc_2007_trainval',) 30 | TEST: ('voc_2007_test',) 31 | OUTPUT_DIR: ./output/align_15_p_5_demo 32 | SEED: 9999 33 | VIS_PERIOD: 30450 34 | VERSION: 2 35 | SOLVER: 36 | MAX_ITER: 30450 37 | EBM_ALIGNER: 38 | ENABLE: True 39 | FEAT_SAVE_LOCATION: 'features' -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 = 56 25 | _C.MODEL.ROI_DENSEPOSE_HEAD.POOLER_TYPE = "ROIAlignV2" 26 | _C.MODEL.ROI_DENSEPOSE_HEAD.POOLER_RESOLUTION = 14 27 | _C.MODEL.ROI_DENSEPOSE_HEAD.POOLER_SAMPLING_RATIO = 2 28 | # Overlap threshold for an RoI to be considered foreground (if >= FG_IOU_THRESHOLD) 29 | _C.MODEL.ROI_DENSEPOSE_HEAD.FG_IOU_THRESHOLD = 0.7 30 | # Loss weights for annotation masks.(14 Parts) 31 | _C.MODEL.ROI_DENSEPOSE_HEAD.INDEX_WEIGHTS = 2.0 32 | # Loss weights for surface parts. (24 Parts) 33 | _C.MODEL.ROI_DENSEPOSE_HEAD.PART_WEIGHTS = 0.3 34 | # Loss weights for UV regression. 35 | _C.MODEL.ROI_DENSEPOSE_HEAD.POINT_REGRESSION_WEIGHTS = 0.1 36 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/NST.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | 7 | class NSTLoss(nn.Module): 8 | """like what you like: knowledge distill via neuron selectivity transfer""" 9 | def __init__(self): 10 | super(NSTLoss, self).__init__() 11 | pass 12 | 13 | def forward(self, g_s, g_t): 14 | return [self.nst_loss(f_s, f_t) for f_s, f_t in zip(g_s, g_t)] 15 | 16 | def nst_loss(self, f_s, f_t): 17 | s_H, t_H = f_s.shape[2], f_t.shape[2] 18 | if s_H > t_H: 19 | f_s = F.adaptive_avg_pool2d(f_s, (t_H, t_H)) 20 | elif s_H < t_H: 21 | f_t = F.adaptive_avg_pool2d(f_t, (s_H, s_H)) 22 | else: 23 | pass 24 | 25 | f_s = f_s.view(f_s.shape[0], f_s.shape[1], -1) 26 | f_s = F.normalize(f_s, dim=2) 27 | f_t = f_t.view(f_t.shape[0], f_t.shape[1], -1) 28 | f_t = F.normalize(f_t, dim=2) 29 | 30 | # set full_loss as False to avoid unnecessary computation 31 | full_loss = True 32 | if full_loss: 33 | return (self.poly_kernel(f_t, f_t).mean().detach() + self.poly_kernel(f_s, f_s).mean() 34 | - 2 * self.poly_kernel(f_s, f_t).mean()) 35 | else: 36 | return self.poly_kernel(f_s, f_s).mean() - 2 * self.poly_kernel(f_s, f_t).mean() 37 | 38 | def poly_kernel(self, a, b): 39 | a = a.unsqueeze(1) 40 | b = b.unsqueeze(2) 41 | res = (a * b).sum(-1).pow(2) 42 | return res -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/CC.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch 4 | import torch.nn as nn 5 | 6 | 7 | class Correlation(nn.Module): 8 | """Correlation Congruence for Knowledge Distillation, ICCV 2019. 9 | The authors nicely shared the code with me. I restructured their code to be 10 | compatible with my running framework. Credits go to the original author""" 11 | def __init__(self): 12 | super(Correlation, self).__init__() 13 | 14 | def forward(self, f_s, f_t): 15 | delta = torch.abs(f_s - f_t) 16 | loss = torch.mean((delta[:-1] * delta[1:]).sum(1)) 17 | return loss 18 | 19 | 20 | # class Correlation(nn.Module): 21 | # """Similarity-preserving loss. My origianl own reimplementation 22 | # based on the paper before emailing the original authors.""" 23 | # def __init__(self): 24 | # super(Correlation, self).__init__() 25 | # 26 | # def forward(self, f_s, f_t): 27 | # return self.similarity_loss(f_s, f_t) 28 | # # return [self.similarity_loss(f_s, f_t) for f_s, f_t in zip(g_s, g_t)] 29 | # 30 | # def similarity_loss(self, f_s, f_t): 31 | # bsz = f_s.shape[0] 32 | # f_s = f_s.view(bsz, -1) 33 | # f_t = f_t.view(bsz, -1) 34 | # 35 | # G_s = torch.mm(f_s, torch.t(f_s)) 36 | # G_s = G_s / G_s.norm(2) 37 | # G_t = torch.mm(f_t, torch.t(f_t)) 38 | # G_t = G_t / G_t.norm(2) 39 | # 40 | # G_diff = G_t - G_s 41 | # loss = (G_diff * G_diff).view(-1, 1).sum(0) / (bsz * bsz) 42 | # return loss 43 | -------------------------------------------------------------------------------- /detection/detectron2/layers/csrc/box_iou_rotated/box_iou_rotated_cpu.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | #include "box_iou_rotated.h" 3 | #include "box_iou_rotated_utils.h" 4 | 5 | namespace detectron2 { 6 | 7 | template 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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/detectron2/utils/store.py: -------------------------------------------------------------------------------- 1 | import random 2 | from collections import deque 3 | 4 | 5 | class Store: 6 | def __init__(self, total_num_classes, items_per_class, shuffle=True): 7 | self.shuffle = shuffle 8 | self.items_per_class = items_per_class 9 | self.total_num_classes = total_num_classes 10 | self.store = [deque(maxlen=self.items_per_class) for _ in range(self.total_num_classes)] 11 | 12 | def add(self, items, class_ids): 13 | for idx, class_id in enumerate(class_ids): 14 | self.store[class_id].append(items[idx]) 15 | 16 | def retrieve(self): 17 | items = [] 18 | for item in self.store: 19 | items.extend(list(item)) 20 | if self.shuffle: 21 | random.shuffle(items) 22 | return items 23 | 24 | def reset(self): 25 | self.store = [deque(maxlen=self.items_per_class) for _ in range(self.total_num_classes)] 26 | 27 | def __str__(self): 28 | s = self.__class__.__name__ + '(' 29 | for idx, item in enumerate(self.store): 30 | s += '\n Class ' + str(idx) + ' --> ' + str(len(list(item))) + ' items' 31 | s = s + ' )' 32 | return s 33 | 34 | def __repr__(self): 35 | return self.__str__() 36 | 37 | def __len__(self): 38 | return sum([len(s) for s in self.store]) 39 | 40 | 41 | if __name__ == "__main__": 42 | store = Store(10, 3) 43 | store.add(('a', 'b', 'c', 'd', 'e', 'f'), (1, 1, 9, 1, 0, 1)) 44 | store.add(('h',), (4,)) 45 | print(store.retrieve()) 46 | print(len(store)) 47 | store.reset() 48 | print(store.retrieve()) 49 | print(len(store)) 50 | -------------------------------------------------------------------------------- /detection/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 | build_box_head, 43 | build_keypoint_head, 44 | build_mask_head, 45 | build_roi_heads, 46 | ) 47 | from .test_time_augmentation import DatasetMapperTTA, GeneralizedRCNNWithTTA 48 | 49 | _EXCLUDE = {"torch", "ShapeSpec"} 50 | __all__ = [k for k in globals().keys() if k not in _EXCLUDE and not k.startswith("_")] 51 | 52 | assert ( 53 | torch.Tensor([1]) == torch.Tensor([2]) 54 | ).dtype == torch.bool, "Your Pytorch is too old. Please update to contain https://github.com/pytorch/pytorch/pull/21113" 55 | -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/eval.yaml: -------------------------------------------------------------------------------- 1 | # Use this config for evaluating the a trained model. 2 | # Usage: python tools/train_net.py --num-gpus 4 --config-file ./configs/PascalVOC-Detection/iOD/eval.yaml --eval-only 3 | # Do change: 4 | # -- WEIGHTS 5 | # -- NUM_BASE_CLASSES 6 | # -- NUM_NOVEL_CLASSES 7 | 8 | 9 | _BASE_: "../../Base-RCNN-C4.yaml" 10 | MODEL: 11 | # WEIGHTS: "detectron2://ImageNetPretrained/MSRA/R-50.pkl" 12 | # WEIGHTS: "./output/10_p_10_ft/model_final.pth" 13 | WEIGHTS: "./output/plus_10/10_p_10_ft_with_extras/model_final.pth" 14 | # WEIGHTS: "/home/joseph/workspace/detectron2/output/15_p_5/model_0019999.pth" 15 | MASK_ON: False 16 | RESNETS: 17 | DEPTH: 50 18 | ROI_HEADS: 19 | # Maximum number of foreground classes to expect 20 | NUM_CLASSES: 20 21 | # Flag to turn on/off Incremental Learning 22 | LEARN_INCREMENTALLY: False 23 | # Flag to select whether to learn base classes or iOD expanded classes 24 | TRAIN_ON_BASE_CLASSES: False 25 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 26 | NUM_BASE_CLASSES: 10 27 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 28 | NUM_NOVEL_CLASSES: 10 29 | POSITIVE_FRACTION: 0.25 30 | NMS_THRESH_TEST: 0.4 31 | RPN: 32 | FREEZE_WEIGHTS: False 33 | ROI_BOX_HEAD: 34 | CLS_AGNOSTIC_BBOX_REG: True 35 | INPUT: 36 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 37 | MIN_SIZE_TEST: 800 38 | DATASETS: 39 | TRAIN: ('voc_2007_trainval',) 40 | TEST: ('voc_2007_test',) 41 | OUTPUT_DIR: ./output/eval 42 | SEED: 9999 43 | VERSION: 2 -------------------------------------------------------------------------------- /detection/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 | DATASETS: 37 | TRAIN: ("densepose_coco_2014_train", "densepose_coco_2014_valminusminival") 38 | TEST: ("densepose_coco_2014_minival",) 39 | SOLVER: 40 | IMS_PER_BATCH: 16 41 | BASE_LR: 0.002 42 | STEPS: (60000, 80000) 43 | MAX_ITER: 90000 44 | WARMUP_FACTOR: 0.1 45 | INPUT: 46 | MIN_SIZE_TRAIN: (640, 672, 704, 736, 768, 800) 47 | -------------------------------------------------------------------------------- /detection/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.video\_visualizer module 57 | ----------------------------------------- 58 | 59 | .. automodule:: detectron2.utils.video_visualizer 60 | :members: 61 | :undoc-members: 62 | :show-inheritance: 63 | 64 | detectron2.utils.visualizer module 65 | ---------------------------------- 66 | 67 | .. automodule:: detectron2.utils.visualizer 68 | :members: 69 | :undoc-members: 70 | :show-inheritance: 71 | 72 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | ENV TORCH_CUDA_ARCH_LIST="Kepler;Kepler+Tesla;Maxwell;Maxwell+Tegra;Pascal;Volta;Turing" 30 | RUN pip install --user -e detectron2_repo 31 | 32 | # Set a fixed model cache directory. 33 | ENV FVCORE_CACHE="/tmp" 34 | WORKDIR /home/appuser/detectron2_repo 35 | 36 | # run it, for example: 37 | # wget http://images.cocodataset.org/val2017/000000439715.jpg -O input.jpg 38 | # python3 demo/demo.py \ 39 | #--config-file configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml \ 40 | #--input input.jpg --output outputs/ \ 41 | #--opts MODEL.WEIGHTS detectron2://COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x/137849600/model_final_f10217.pkl 42 | -------------------------------------------------------------------------------- /detection/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](https://github.com/facebookresearch/detectron2/tree/master/projects/DensePose) 32 | for an example that implements new ROIHeads. 33 | 34 | Other registries can be found in [API documentation](../modules/modeling.html#model-registries). 35 | You can register components in these registries to customize different parts of a model, or the 36 | entire model. 37 | -------------------------------------------------------------------------------- /classification/utils/gpu_tools.py: -------------------------------------------------------------------------------- 1 | ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 | ## Created by: Yaoyao Liu 3 | ## Max Planck Institute for Informatics 4 | ## yaoyao.liu@mpi-inf.mpg.de 5 | ## Copyright (c) 2021 6 | ## 7 | ## This source code is licensed under the MIT-style license found in the 8 | ## LICENSE file in the root directory of this source tree 9 | ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 10 | """ GPU tools. """ 11 | import os 12 | import torch 13 | import time 14 | 15 | def check_memory(cuda_device): 16 | """ Check the total memory and occupied memory for GPU """ 17 | devices_info = os.popen('"/usr/bin/nvidia-smi" --query-gpu=memory.total,memory.used --format=csv,nounits,noheader').read().strip().split("\n") 18 | total, used = devices_info[int(cuda_device)].split(',') 19 | return total,used 20 | 21 | def occupy_memory(cuda_device): 22 | """ Create a large tensor and delete it. 23 | This operation occupies the GPU memory, so other processes cannot use the occupied memory. 24 | It is used to ensure that this process won't be stopped when it requires additional GPU memory. 25 | Be careful with this operation. It will influence other people when you are sharing GPUs with others. 26 | """ 27 | total, used = check_memory(cuda_device) 28 | total = int(total) 29 | used = int(used) 30 | max_mem = int(total * 0.90) 31 | print('Total memory: ' + str(total) + ', used memory: ' + str(used)) 32 | block_mem = max_mem - used 33 | if block_mem > 0: 34 | x = torch.cuda.FloatTensor(256, 1024, block_mem) 35 | del x 36 | 37 | def set_gpu(x): 38 | """ Set up which GPU we use for this process """ 39 | os.environ['CUDA_VISIBLE_DEVICES'] = x 40 | print('Using gpu:', x) 41 | 42 | 43 | -------------------------------------------------------------------------------- /detection/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.modeilng.poolers module 11 | --------------------------------------- 12 | 13 | .. automodule:: detectron2.modeling.poolers 14 | :members: 15 | :undoc-members: 16 | :show-inheritance: 17 | 18 | 19 | detectron2.modeilng.sampling module 20 | ------------------------------------ 21 | 22 | .. automodule:: detectron2.modeling.sampling 23 | :members: 24 | :undoc-members: 25 | :show-inheritance: 26 | 27 | 28 | detectron2.modeilng.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.ROI_HEADS_REGISTRY 54 | .. autodata:: detectron2.modeling.ROI_BOX_HEAD_REGISTRY 55 | .. autodata:: detectron2.modeling.ROI_MASK_HEAD_REGISTRY 56 | .. autodata:: detectron2.modeling.ROI_KEYPOINT_HEAD_REGISTRY 57 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/FSP.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import numpy as np 4 | import torch.nn as nn 5 | import torch.nn.functional as F 6 | 7 | 8 | class FSP(nn.Module): 9 | """A Gift from Knowledge Distillation: 10 | Fast Optimization, Network Minimization and Transfer Learning""" 11 | def __init__(self, s_shapes, t_shapes): 12 | super(FSP, self).__init__() 13 | assert len(s_shapes) == len(t_shapes), 'unequal length of feat list' 14 | s_c = [s[1] for s in s_shapes] 15 | t_c = [t[1] for t in t_shapes] 16 | if np.any(np.asarray(s_c) != np.asarray(t_c)): 17 | raise ValueError('num of channels not equal (error in FSP)') 18 | 19 | def forward(self, g_s, g_t): 20 | s_fsp = self.compute_fsp(g_s) 21 | t_fsp = self.compute_fsp(g_t) 22 | loss_group = [self.compute_loss(s, t) for s, t in zip(s_fsp, t_fsp)] 23 | return loss_group 24 | 25 | @staticmethod 26 | def compute_loss(s, t): 27 | return (s - t).pow(2).mean() 28 | 29 | @staticmethod 30 | def compute_fsp(g): 31 | fsp_list = [] 32 | for i in range(len(g) - 1): 33 | bot, top = g[i], g[i + 1] 34 | b_H, t_H = bot.shape[2], top.shape[2] 35 | if b_H > t_H: 36 | bot = F.adaptive_avg_pool2d(bot, (t_H, t_H)) 37 | elif b_H < t_H: 38 | top = F.adaptive_avg_pool2d(top, (b_H, b_H)) 39 | else: 40 | pass 41 | bot = bot.unsqueeze(1) 42 | top = top.unsqueeze(2) 43 | bot = bot.view(bot.shape[0], bot.shape[1], bot.shape[2], -1) 44 | top = top.view(top.shape[0], top.shape[1], top.shape[2], -1) 45 | 46 | fsp = (bot * top).mean(-1) 47 | fsp_list.append(fsp) 48 | return fsp_list 49 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/PKT.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch 4 | import torch.nn as nn 5 | 6 | 7 | class PKT(nn.Module): 8 | """Probabilistic Knowledge Transfer for deep representation learning 9 | Code from author: https://github.com/passalis/probabilistic_kt""" 10 | def __init__(self): 11 | super(PKT, self).__init__() 12 | 13 | def forward(self, f_s, f_t): 14 | return self.cosine_similarity_loss(f_s, f_t) 15 | 16 | @staticmethod 17 | def cosine_similarity_loss(output_net, target_net, eps=0.0000001): 18 | # Normalize each vector by its norm 19 | output_net_norm = torch.sqrt(torch.sum(output_net ** 2, dim=1, keepdim=True)) 20 | output_net = output_net / (output_net_norm + eps) 21 | output_net[output_net != output_net] = 0 22 | 23 | target_net_norm = torch.sqrt(torch.sum(target_net ** 2, dim=1, keepdim=True)) 24 | target_net = target_net / (target_net_norm + eps) 25 | target_net[target_net != target_net] = 0 26 | 27 | # Calculate the cosine similarity 28 | model_similarity = torch.mm(output_net, output_net.transpose(0, 1)) 29 | target_similarity = torch.mm(target_net, target_net.transpose(0, 1)) 30 | 31 | # Scale cosine similarity to 0..1 32 | model_similarity = (model_similarity + 1.0) / 2.0 33 | target_similarity = (target_similarity + 1.0) / 2.0 34 | 35 | # Transform them into probabilities 36 | model_similarity = model_similarity / torch.sum(model_similarity, dim=1, keepdim=True) 37 | target_similarity = target_similarity / torch.sum(target_similarity, dim=1, keepdim=True) 38 | 39 | # Calculate the KL-divergence 40 | loss = torch.mean(target_similarity * torch.log((target_similarity + eps) / (model_similarity + eps))) 41 | 42 | return loss 43 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/README.md: -------------------------------------------------------------------------------- 1 | # ELI for Object Detection (CVPR 2022) 2 | 3 | ## Installation and Setup 4 | - Install the Detectron2 library that is packages along with this code base. See [INSTALL.md](INSTALL.md). 5 | - Download and extract Pascal VOC 2007 to `./datasets/VOC2007/` 6 | - ELI enhances already trained incremental models. You can download trained incremental models from [iOD repository](https://github.com/JosephKJ/iOD). Here are the direct links: [19+1](https://drive.google.com/file/d/1sW-aZ9crRFjgbErtgXNQ8hO67WLKYAAn/view?usp=sharing) | [15+5](https://drive.google.com/file/d/1E8m4VrrKmNYT1Zba0MwaI3ZjztrLobcA/view?usp=sharing) | [10+10](https://drive.google.com/file/d/1LH7OY-uMifl2gwCFEgm6U5h_Xfh1nPcH/view?usp=sharing) 7 | - Use the script: `run.sh` (You may uncomment the other commands in this scrips to train the base and incremetal models.) 8 | 9 | 10 | ## Environment Configurations 11 | - Python version: 3.6.7 12 | - PyTorch version: 1.3.0 13 | - CUDA version: 11.0 14 | - GPUs: 4 x NVIDIA GTX 1080-ti 15 | 16 | ## Acknowledgement 17 | The code is build on top of Detectron2 library. 18 | 19 | ## Citation 20 | If you find our research useful, please consider citing us: 21 | 22 | ```BibTeX 23 | 24 | @inproceedings{joseph2022Energy, 25 | title={Energy-based Latent Aligner for Incremental Learning}, 26 | author={Joseph, KJ and Khan, Salman and Khan, Fahad Shahbaz and Anwar, Rao Muhammad and Balasubramanian, Vineeth}, 27 | booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition}, 28 | year={2022} 29 | } 30 | 31 | @article{joseph2021incremental, 32 | title={Incremental object detection via meta-learning}, 33 | author={Joseph, KJ and Rajasegaran, Jathushan and Khan, Salman and Khan, Fahad Shahbaz and Balasubramanian, Vineeth}, 34 | journal={IEEE Transactions on Pattern Analysis and Machine Intelligence}, 35 | year={2021} 36 | } 37 | ``` 38 | -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/base_15.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 | # Maximum number of foreground classes to expect 9 | NUM_CLASSES: 20 10 | # Flag to turn on/off Incremental Learning 11 | LEARN_INCREMENTALLY: True 12 | # Flag to select whether to learn base classes or iOD expanded classes 13 | TRAIN_ON_BASE_CLASSES: True 14 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 15 | NUM_BASE_CLASSES: 15 16 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 17 | NUM_NOVEL_CLASSES: 5 18 | POSITIVE_FRACTION: 0.25 19 | NMS_THRESH_TEST: 0.3 20 | RPN: 21 | FREEZE_WEIGHTS: False 22 | ROI_BOX_HEAD: 23 | CLS_AGNOSTIC_BBOX_REG: True 24 | INPUT: 25 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 26 | MIN_SIZE_TEST: 800 27 | DATASETS: 28 | TRAIN: ('voc_2007_trainval',) 29 | TEST: ('voc_2007_test',) 30 | SOLVER: 31 | STEPS: (12000, 16000) 32 | MAX_ITER: 18000 # 17.4 epochs 33 | WARMUP_ITERS: 100 # 100 34 | LR_SCHEDULER_NAME: WarmupMultiStepLR 35 | OUTPUT_DIR: ./output/first_15 36 | VIS_PERIOD: 17000 37 | DISTILL: 38 | ENABLE: False 39 | BACKBONE: True 40 | RPN: False 41 | ROI_HEADS: True 42 | ONLY_FG_ROIS: False 43 | # (1-LOSS_WEIGHT) (CLF / REG loss) + (LOSS_WEIGHT) ROI-Distillation 44 | LOSS_WEIGHT: 0.2 45 | # Warp Grad 46 | WG: 47 | ENABLE: False 48 | TRAIN_WARP_AT_ITR_NO: 20 49 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 50 | NUM_FEATURES_PER_CLASS: 100 51 | NUM_IMAGES_PER_CLASS: 10 52 | BATCH_SIZE: 2 53 | USE_FEATURE_STORE: True 54 | IMAGE_STORE_LOC: './image_store_15_no_wg.pth' 55 | SEED: 9999 56 | VERSION: 2 -------------------------------------------------------------------------------- /detection/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 | 8 | def get_densepose_metadata(): 9 | meta = { 10 | "thing_classes": ["person"], 11 | "densepose_transform_src": "detectron2://densepose/UV_symmetry_transforms.mat", 12 | "densepose_smpl_subdiv": "detectron2://densepose/SMPL_subdiv.mat", 13 | "densepose_smpl_subdiv_transform": "detectron2://densepose/SMPL_SUBDIV_TRANSFORM.mat", 14 | } 15 | return meta 16 | 17 | 18 | SPLITS = { 19 | "densepose_coco_2014_train": ("coco/train2014", "coco/annotations/densepose_train2014.json"), 20 | "densepose_coco_2014_minival": ("coco/val2014", "coco/annotations/densepose_minival2014.json"), 21 | "densepose_coco_2014_minival_100": ( 22 | "coco/val2014", 23 | "coco/annotations/densepose_minival2014_100.json", 24 | ), 25 | "densepose_coco_2014_valminusminival": ( 26 | "coco/val2014", 27 | "coco/annotations/densepose_valminusminival2014.json", 28 | ), 29 | } 30 | 31 | DENSEPOSE_KEYS = ["dp_x", "dp_y", "dp_I", "dp_U", "dp_V", "dp_masks"] 32 | 33 | for key, (image_root, json_file) in SPLITS.items(): 34 | # Assume pre-defined datasets live in `./datasets`. 35 | json_file = os.path.join("datasets", json_file) 36 | image_root = os.path.join("datasets", image_root) 37 | 38 | DatasetCatalog.register( 39 | key, 40 | lambda key=key, json_file=json_file, image_root=image_root: load_coco_json( 41 | json_file, image_root, key, extra_annotation_keys=DENSEPOSE_KEYS 42 | ), 43 | ) 44 | 45 | MetadataCatalog.get(key).set( 46 | json_file=json_file, image_root=image_root, **get_densepose_metadata() 47 | ) 48 | -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/base_19.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 | # Maximum number of foreground classes to expect 9 | NUM_CLASSES: 20 10 | # Flag to turn on/off Incremental Learning 11 | LEARN_INCREMENTALLY: True 12 | # Flag to select whether to learn base classes or iOD expanded classes 13 | TRAIN_ON_BASE_CLASSES: True 14 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 15 | NUM_BASE_CLASSES: 19 16 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 17 | NUM_NOVEL_CLASSES: 1 18 | POSITIVE_FRACTION: 0.25 19 | NMS_THRESH_TEST: 0.3 20 | RPN: 21 | FREEZE_WEIGHTS: False 22 | ROI_BOX_HEAD: 23 | CLS_AGNOSTIC_BBOX_REG: True 24 | INPUT: 25 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 26 | MIN_SIZE_TEST: 800 27 | DATASETS: 28 | TRAIN: ('voc_2007_trainval',) 29 | TEST: ('voc_2007_test',) 30 | SOLVER: 31 | STEPS: (12000, 16000) 32 | MAX_ITER: 18000 # 17.4 epochs 33 | WARMUP_ITERS: 100 34 | LR_SCHEDULER_NAME: WarmupMultiStepLR 35 | CHECKPOINT_PERIOD: 2000 36 | OUTPUT_DIR: ./output/first_19 37 | VIS_PERIOD: 17000 38 | DISTILL: 39 | ENABLE: False 40 | BACKBONE: True 41 | RPN: False 42 | ROI_HEADS: True 43 | ONLY_FG_ROIS: False 44 | # (1-LOSS_WEIGHT) (CLF / REG loss) + (LOSS_WEIGHT) ROI-Distillation 45 | LOSS_WEIGHT: 0.2 46 | # Warp Grad 47 | WG: 48 | ENABLE: False 49 | TRAIN_WARP_AT_ITR_NO: 20 50 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 51 | NUM_FEATURES_PER_CLASS: 100 52 | NUM_IMAGES_PER_CLASS: 10 53 | BATCH_SIZE: 2 54 | USE_FEATURE_STORE: True 55 | IMAGE_STORE_LOC: './image_store_19.pth' 56 | 57 | SEED: 9999 58 | VERSION: 2 -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/base_10.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 | # Maximum number of foreground classes to expect 9 | NUM_CLASSES: 20 10 | # Flag to turn on/off Incremental Learning 11 | LEARN_INCREMENTALLY: True 12 | # Flag to select whether to learn base classes or iOD expanded classes 13 | TRAIN_ON_BASE_CLASSES: True 14 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 15 | NUM_BASE_CLASSES: 10 16 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 17 | NUM_NOVEL_CLASSES: 10 18 | POSITIVE_FRACTION: 0.25 19 | NMS_THRESH_TEST: 0.3 20 | RPN: 21 | FREEZE_WEIGHTS: False 22 | ROI_BOX_HEAD: 23 | CLS_AGNOSTIC_BBOX_REG: True 24 | INPUT: 25 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 26 | MIN_SIZE_TEST: 800 27 | DATASETS: 28 | TRAIN: ('voc_2007_trainval',) 29 | TEST: ('voc_2007_test',) 30 | SOLVER: 31 | STEPS: (12000, 16000) 32 | MAX_ITER: 18000 # 17.4 epochs 33 | WARMUP_ITERS: 100 # 100 34 | LR_SCHEDULER_NAME: WarmupMultiStepLR 35 | CHECKPOINT_PERIOD: 2000 36 | OUTPUT_DIR: ./output/first_10 37 | VIS_PERIOD: 17000 38 | DISTILL: 39 | ENABLE: False 40 | BACKBONE: True 41 | RPN: False 42 | ROI_HEADS: True 43 | ONLY_FG_ROIS: False 44 | # (1-LOSS_WEIGHT) (CLF / REG loss) + (LOSS_WEIGHT) ROI-Distillation 45 | LOSS_WEIGHT: 0.2 46 | # Warp Grad 47 | WG: 48 | ENABLE: False 49 | TRAIN_WARP_AT_ITR_NO: 20 50 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 51 | NUM_FEATURES_PER_CLASS: 100 52 | NUM_IMAGES_PER_CLASS: 10 53 | BATCH_SIZE: 2 54 | USE_FEATURE_STORE: True 55 | IMAGE_STORE_LOC: './image_store_10.pth' 56 | 57 | SEED: 9999 58 | VERSION: 2 -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/10_p_10.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "./output/first_10/model_final.pth" 4 | BASE_WEIGHTS: "./output/first_10/model_final.pth" 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | ROI_HEADS: 9 | # Maximum number of foreground classes to expect 10 | NUM_CLASSES: 20 11 | # Flag to turn on/off Incremental Learning 12 | LEARN_INCREMENTALLY: True 13 | # Flag to select whether to learn base classes or iOD expanded classes 14 | TRAIN_ON_BASE_CLASSES: False 15 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 16 | NUM_BASE_CLASSES: 10 17 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 18 | NUM_NOVEL_CLASSES: 10 19 | POSITIVE_FRACTION: 0.25 20 | NMS_THRESH_TEST: 0.3 21 | RPN: 22 | FREEZE_WEIGHTS: False 23 | ROI_BOX_HEAD: 24 | CLS_AGNOSTIC_BBOX_REG: True 25 | INPUT: 26 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 27 | MIN_SIZE_TEST: 800 28 | DATASETS: 29 | TRAIN: ('voc_2007_trainval',) 30 | TEST: ('voc_2007_test',) 31 | SOLVER: 32 | STEPS: (30000, 34000) # 21000, 22000 33 | MAX_ITER: 36000 # 36000 34 | WARMUP_ITERS: 100 # 100 35 | LR_SCHEDULER_NAME: WarmupMultiStepLR 36 | OUTPUT_DIR: ./output/10_p_10 37 | VIS_PERIOD: 17000 38 | DISTILL: 39 | ENABLE: True 40 | BACKBONE: True 41 | RPN: False 42 | ROI_HEADS: True 43 | ONLY_FG_ROIS: False 44 | # (1-LOSS_WEIGHT) (CLF / REG loss) + (LOSS_WEIGHT) ROI-Distillation 45 | LOSS_WEIGHT: 0.2 46 | # Warp Grad 47 | WG: 48 | ENABLE: True 49 | TRAIN_WARP_AT_ITR_NO: 20 50 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 51 | NUM_FEATURES_PER_CLASS: 100 52 | NUM_IMAGES_PER_CLASS: 10 53 | BATCH_SIZE: 2 54 | USE_FEATURE_STORE: True 55 | IMAGE_STORE_LOC: './10_p_10.pth' 56 | 57 | SEED: 9999 58 | VERSION: 2 -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/15_p_5.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "./output/first_15/model_final.pth" 4 | BASE_WEIGHTS: "./output/first_15/model_final.pth" 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | ROI_HEADS: 9 | # Maximum number of foreground classes to expect 10 | NUM_CLASSES: 20 11 | # Flag to turn on/off Incremental Learning 12 | LEARN_INCREMENTALLY: True 13 | # Flag to select whether to learn base classes or iOD expanded classes 14 | TRAIN_ON_BASE_CLASSES: False 15 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 16 | NUM_BASE_CLASSES: 15 17 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 18 | NUM_NOVEL_CLASSES: 5 19 | POSITIVE_FRACTION: 0.25 20 | NMS_THRESH_TEST: 0.3 21 | RPN: 22 | FREEZE_WEIGHTS: False 23 | ROI_BOX_HEAD: 24 | CLS_AGNOSTIC_BBOX_REG: True 25 | INPUT: 26 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 27 | MIN_SIZE_TEST: 800 28 | DATASETS: 29 | TRAIN: ('voc_2007_trainval',) 30 | TEST: ('voc_2007_test',) 31 | SOLVER: 32 | STEPS: (30000, 34000) # 21000, 22000 33 | MAX_ITER: 20000 # 36000 34 | WARMUP_ITERS: 100 # 100 35 | LR_SCHEDULER_NAME: WarmupMultiStepLR 36 | OUTPUT_DIR: ./output/15_p_5 37 | VIS_PERIOD: 17000 38 | DISTILL: 39 | ENABLE: True 40 | BACKBONE: True 41 | RPN: False 42 | ROI_HEADS: True 43 | ONLY_FG_ROIS: False 44 | # (1-LOSS_WEIGHT) (CLF / REG loss) + (LOSS_WEIGHT) ROI-Distillation 45 | LOSS_WEIGHT: 0.2 46 | # Warp Grad 47 | WG: 48 | ENABLE: True 49 | TRAIN_WARP_AT_ITR_NO: 20 50 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 51 | NUM_FEATURES_PER_CLASS: 100 52 | NUM_IMAGES_PER_CLASS: 10 53 | BATCH_SIZE: 2 54 | USE_FEATURE_STORE: True 55 | IMAGE_STORE_LOC: './15_p_5.pth' 56 | 57 | SEED: 9999 58 | VERSION: 2 -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/19_p_1.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "./output/first_19/model_final.pth" 4 | BASE_WEIGHTS: "./output/first_19/model_final.pth" 5 | MASK_ON: False 6 | RESNETS: 7 | DEPTH: 50 8 | ROI_HEADS: 9 | # Maximum number of foreground classes to expect 10 | NUM_CLASSES: 20 11 | # Flag to turn on/off Incremental Learning 12 | LEARN_INCREMENTALLY: True 13 | # Flag to select whether to learn base classes or iOD expanded classes 14 | TRAIN_ON_BASE_CLASSES: False 15 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 16 | NUM_BASE_CLASSES: 19 17 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 18 | NUM_NOVEL_CLASSES: 1 19 | POSITIVE_FRACTION: 0.25 20 | NMS_THRESH_TEST: 0.3 21 | RPN: 22 | FREEZE_WEIGHTS: False 23 | ROI_BOX_HEAD: 24 | CLS_AGNOSTIC_BBOX_REG: True 25 | INPUT: 26 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 27 | MIN_SIZE_TEST: 800 28 | DATASETS: 29 | TRAIN: ('voc_2007_trainval',) 30 | TEST: ('voc_2007_test',) 31 | SOLVER: 32 | STEPS: (30000, 34000) # 21000, 22000 33 | MAX_ITER: 20000 # 36000 34 | WARMUP_ITERS: 100 # 100 35 | LR_SCHEDULER_NAME: WarmupMultiStepLR 36 | OUTPUT_DIR: ./output/19_p_1 37 | VIS_PERIOD: 17000 38 | DISTILL: 39 | ENABLE: True 40 | BACKBONE: True 41 | RPN: False 42 | ROI_HEADS: True 43 | ONLY_FG_ROIS: False 44 | # (1-LOSS_WEIGHT) (CLF / REG loss) + (LOSS_WEIGHT) ROI-Distillation 45 | LOSS_WEIGHT: 0.2 46 | # Warp Grad 47 | WG: 48 | ENABLE: True 49 | TRAIN_WARP_AT_ITR_NO: 20 50 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 51 | NUM_FEATURES_PER_CLASS: 100 52 | NUM_IMAGES_PER_CLASS: 10 53 | BATCH_SIZE: 2 54 | USE_FEATURE_STORE: True 55 | IMAGE_STORE_LOC: './19_p_1.pth' 56 | 57 | SEED: 9999 58 | VERSION: 2 -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/RKD.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch 4 | import torch.nn as nn 5 | import torch.nn.functional as F 6 | 7 | 8 | class RKDLoss(nn.Module): 9 | """Relational Knowledge Disitllation, CVPR2019""" 10 | def __init__(self, w_d=25, w_a=50): 11 | super(RKDLoss, self).__init__() 12 | self.w_d = w_d 13 | self.w_a = w_a 14 | 15 | def forward(self, f_s, f_t): 16 | student = f_s.view(f_s.shape[0], -1) 17 | teacher = f_t.view(f_t.shape[0], -1) 18 | 19 | # RKD distance loss 20 | with torch.no_grad(): 21 | t_d = self.pdist(teacher, squared=False) 22 | mean_td = t_d[t_d > 0].mean() 23 | t_d = t_d / mean_td 24 | 25 | d = self.pdist(student, squared=False) 26 | mean_d = d[d > 0].mean() 27 | d = d / mean_d 28 | 29 | loss_d = F.smooth_l1_loss(d, t_d) 30 | 31 | # RKD Angle loss 32 | with torch.no_grad(): 33 | td = (teacher.unsqueeze(0) - teacher.unsqueeze(1)) 34 | norm_td = F.normalize(td, p=2, dim=2) 35 | t_angle = torch.bmm(norm_td, norm_td.transpose(1, 2)).view(-1) 36 | 37 | sd = (student.unsqueeze(0) - student.unsqueeze(1)) 38 | norm_sd = F.normalize(sd, p=2, dim=2) 39 | s_angle = torch.bmm(norm_sd, norm_sd.transpose(1, 2)).view(-1) 40 | 41 | loss_a = F.smooth_l1_loss(s_angle, t_angle) 42 | 43 | loss = self.w_d * loss_d + self.w_a * loss_a 44 | 45 | return loss 46 | 47 | @staticmethod 48 | def pdist(e, squared=False, eps=1e-12): 49 | e_square = e.pow(2).sum(dim=1) 50 | prod = e @ e.t() 51 | res = (e_square.unsqueeze(1) + e_square.unsqueeze(0) - 2 * prod).clamp(min=eps) 52 | 53 | if not squared: 54 | res = res.sqrt() 55 | 56 | res = res.clone() 57 | res[range(len(e)), range(len(e))] = 0 58 | return res 59 | -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/ft_15_p_5.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "./output/15_p_5/model_final.pth" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | # Maximum number of foreground classes to expect 9 | NUM_CLASSES: 20 10 | # Flag to turn on/off Incremental Learning 11 | LEARN_INCREMENTALLY: False 12 | # Flag to select whether to learn base classes or iOD expanded classes 13 | TRAIN_ON_BASE_CLASSES: False 14 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 15 | NUM_BASE_CLASSES: 15 16 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 17 | NUM_NOVEL_CLASSES: 5 18 | POSITIVE_FRACTION: 0.25 19 | NMS_THRESH_TEST: 0.3 20 | RPN: 21 | FREEZE_WEIGHTS: False 22 | ROI_BOX_HEAD: 23 | CLS_AGNOSTIC_BBOX_REG: True 24 | INPUT: 25 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 26 | MIN_SIZE_TEST: 800 27 | DATASETS: 28 | TRAIN: ('voc_2007_trainval',) 29 | TEST: ('voc_2007_test',) 30 | SOLVER: 31 | STEPS: (30000, 34000) # 21000, 22000 32 | MAX_ITER: 30000 # 36000 33 | WARMUP_ITERS: 100 # 100 34 | LR_SCHEDULER_NAME: FixedLR 35 | EXPLICIT_LR: 0.0001 36 | CHECKPOINT_PERIOD: 1000 37 | OUTPUT_DIR: ./output/15_p_5_ft 38 | VIS_PERIOD: 17000 39 | 40 | DISTILL: 41 | ENABLE: False 42 | BACKBONE: True 43 | RPN: False 44 | ROI_HEADS: True 45 | ONLY_FG_ROIS: False 46 | # (1-LOSS_WEIGHT) (CLF / REG loss) + (LOSS_WEIGHT) ROI-Distillation 47 | LOSS_WEIGHT: 0.2 48 | 49 | # Warp Grad 50 | WG: 51 | ENABLE: False 52 | TRAIN_WARP_AT_ITR_NO: 20 53 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 54 | NUM_FEATURES_PER_CLASS: 100 55 | NUM_IMAGES_PER_CLASS: 10 56 | BATCH_SIZE: 2 57 | USE_FEATURE_STORE: True 58 | IMAGE_STORE_LOC: './15_p_5.pth' 59 | 60 | SEED: 9999 61 | VERSION: 2 62 | 63 | FINETUNE: 64 | MIN_NUM_IMG_PER_CLASS: 10 65 | BATCH_SIZE: 2 -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/ft_10_p_10.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "./output/10_p_10/model_final.pth" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | # Maximum number of foreground classes to expect 9 | NUM_CLASSES: 20 10 | # Flag to turn on/off Incremental Learning 11 | LEARN_INCREMENTALLY: False 12 | # Flag to select whether to learn base classes or iOD expanded classes 13 | TRAIN_ON_BASE_CLASSES: False 14 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 15 | NUM_BASE_CLASSES: 10 16 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 17 | NUM_NOVEL_CLASSES: 10 18 | POSITIVE_FRACTION: 0.25 19 | NMS_THRESH_TEST: 0.4 20 | RPN: 21 | FREEZE_WEIGHTS: False 22 | ROI_BOX_HEAD: 23 | CLS_AGNOSTIC_BBOX_REG: True 24 | INPUT: 25 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 26 | MIN_SIZE_TEST: 800 27 | DATASETS: 28 | TRAIN: ('voc_2007_trainval',) 29 | TEST: ('voc_2007_test',) 30 | SOLVER: 31 | STEPS: (30000, 34000) # 21000, 22000 32 | MAX_ITER: 41000 # 36000 33 | WARMUP_ITERS: 100 # 100 34 | LR_SCHEDULER_NAME: FixedLR 35 | EXPLICIT_LR: 0.0001 36 | CHECKPOINT_PERIOD: 1000 37 | OUTPUT_DIR: ./output/10_p_10_ft 38 | VIS_PERIOD: 17000 39 | 40 | DISTILL: 41 | ENABLE: False 42 | BACKBONE: True 43 | RPN: False 44 | ROI_HEADS: True 45 | ONLY_FG_ROIS: False 46 | # (1-LOSS_WEIGHT) (CLF / REG loss) + (LOSS_WEIGHT) ROI-Distillation 47 | LOSS_WEIGHT: 0.2 48 | 49 | # Warp Grad 50 | WG: 51 | ENABLE: False 52 | TRAIN_WARP_AT_ITR_NO: 20 53 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 54 | NUM_FEATURES_PER_CLASS: 100 55 | NUM_IMAGES_PER_CLASS: 10 56 | BATCH_SIZE: 2 57 | USE_FEATURE_STORE: True 58 | IMAGE_STORE_LOC: './10_p_10.pth' 59 | 60 | SEED: 9999 61 | VERSION: 2 62 | 63 | FINETUNE: 64 | MIN_NUM_IMG_PER_CLASS: 10 65 | BATCH_SIZE: 2 -------------------------------------------------------------------------------- /detection/configs/PascalVOC-Detection/iOD/ft_19_p_1.yaml: -------------------------------------------------------------------------------- 1 | _BASE_: "../../Base-RCNN-C4.yaml" 2 | MODEL: 3 | WEIGHTS: "./output/19_p_1/model_final.pth" 4 | MASK_ON: False 5 | RESNETS: 6 | DEPTH: 50 7 | ROI_HEADS: 8 | # Maximum number of foreground classes to expect 9 | NUM_CLASSES: 20 10 | # Flag to turn on/off Incremental Learning 11 | LEARN_INCREMENTALLY: False 12 | # Flag to select whether to learn base classes or iOD expanded classes 13 | TRAIN_ON_BASE_CLASSES: False 14 | # Number of base classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to True 15 | NUM_BASE_CLASSES: 19 16 | # Number of novel classes; these classes would be trained if TRAIN_ON_BASE_CLASSES is set to False 17 | NUM_NOVEL_CLASSES: 1 18 | POSITIVE_FRACTION: 0.25 19 | NMS_THRESH_TEST: 0.3 20 | RPN: 21 | FREEZE_WEIGHTS: False 22 | ROI_BOX_HEAD: 23 | CLS_AGNOSTIC_BBOX_REG: True 24 | INPUT: 25 | MIN_SIZE_TRAIN: (480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800) 26 | MIN_SIZE_TEST: 800 27 | DATASETS: 28 | TRAIN: ('voc_2007_trainval',) 29 | TEST: ('voc_2007_test',) 30 | SOLVER: 31 | STEPS: (30000, 34000) # 21000, 22000 32 | MAX_ITER: 23000 # 36000 33 | WARMUP_ITERS: 100 # 100 34 | LR_SCHEDULER_NAME: FixedLR 35 | EXPLICIT_LR: 0.0001 36 | CHECKPOINT_PERIOD: 1000 37 | 38 | OUTPUT_DIR: ./output/19_p_1_ft 39 | VIS_PERIOD: 17000 40 | 41 | DISTILL: 42 | ENABLE: False 43 | BACKBONE: True 44 | RPN: False 45 | ROI_HEADS: True 46 | ONLY_FG_ROIS: False 47 | # (1-LOSS_WEIGHT) (CLF / REG loss) + (LOSS_WEIGHT) ROI-Distillation 48 | LOSS_WEIGHT: 0.2 49 | 50 | # Warp Grad 51 | WG: 52 | ENABLE: False 53 | TRAIN_WARP_AT_ITR_NO: 20 54 | WARP_LAYERS: ("module.roi_heads.res5.2.conv3.weight",) 55 | NUM_FEATURES_PER_CLASS: 100 56 | NUM_IMAGES_PER_CLASS: 10 57 | BATCH_SIZE: 2 58 | USE_FEATURE_STORE: True 59 | IMAGE_STORE_LOC: './19_p_1.pth' 60 | 61 | SEED: 9999 62 | VERSION: 2 63 | 64 | FINETUNE: 65 | MIN_NUM_IMG_PER_CLASS: 10 66 | BATCH_SIZE: 2 -------------------------------------------------------------------------------- /detection/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 iOD 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 | ``` 25 | 26 | To see a list of available configs in detectron2, see [Config References](../modules/config.html#config-references) 27 | 28 | 29 | ### Best Practice with Configs 30 | 31 | 1. Treat the configs you write as "code": avoid copying them or duplicating them; use "_BASE_" 32 | instead to share common parts between configs. 33 | 34 | 2. Keep the configs you write simple: don't include keys that do not affect the experimental setting. 35 | 36 | 3. Keep a version number in your configs (or the base config), e.g., `VERSION: 2`, 37 | for backward compatibility. 38 | The builtin configs do not include version number because they are meant to 39 | be always up-to-date. 40 | 41 | 4. Save a full config together with a trained model, and use it to run inference. 42 | This is more robust to changes that may happen to the config definition 43 | (e.g., if a default value changed). 44 | -------------------------------------------------------------------------------- /detection/detectron2/distiller_zoo/VID.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch 4 | import torch.nn as nn 5 | import torch.nn.functional as F 6 | import numpy as np 7 | 8 | 9 | class VIDLoss(nn.Module): 10 | """Variational Information Distillation for Knowledge Transfer (CVPR 2019), 11 | code from author: https://github.com/ssahn0215/variational-information-distillation""" 12 | def __init__(self, 13 | num_input_channels, 14 | num_mid_channel, 15 | num_target_channels, 16 | init_pred_var=5.0, 17 | eps=1e-5): 18 | super(VIDLoss, self).__init__() 19 | 20 | def conv1x1(in_channels, out_channels, stride=1): 21 | return nn.Conv2d( 22 | in_channels, out_channels, 23 | kernel_size=1, padding=0, 24 | bias=False, stride=stride) 25 | 26 | self.regressor = nn.Sequential( 27 | conv1x1(num_input_channels, num_mid_channel), 28 | nn.ReLU(), 29 | conv1x1(num_mid_channel, num_mid_channel), 30 | nn.ReLU(), 31 | conv1x1(num_mid_channel, num_target_channels), 32 | ) 33 | self.log_scale = torch.nn.Parameter( 34 | np.log(np.exp(init_pred_var-eps)-1.0) * torch.ones(num_target_channels) 35 | ) 36 | self.eps = eps 37 | 38 | def forward(self, input, target): 39 | # pool for dimentsion match 40 | s_H, t_H = input.shape[2], target.shape[2] 41 | if s_H > t_H: 42 | input = F.adaptive_avg_pool2d(input, (t_H, t_H)) 43 | elif s_H < t_H: 44 | target = F.adaptive_avg_pool2d(target, (s_H, s_H)) 45 | else: 46 | pass 47 | pred_mean = self.regressor(input) 48 | pred_var = torch.log(1.0+torch.exp(self.log_scale))+self.eps 49 | pred_var = pred_var.view(1, -1, 1, 1) 50 | neg_log_prob = 0.5*( 51 | (pred_mean-target)**2/pred_var+torch.log(pred_var) 52 | ) 53 | loss = torch.mean(neg_log_prob) 54 | return loss 55 | -------------------------------------------------------------------------------- /detection/detectron2/modeling/distillation_loss.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn.functional as F 3 | from fvcore.nn import smooth_l1_loss 4 | from detectron2.distiller_zoo import * 5 | 6 | 7 | def rpn_loss(pred_objectness_logits, pred_anchor_deltas, prev_pred_objectness_logits, prev_pred_anchor_deltas): 8 | loss = logit_distillation(pred_objectness_logits[0], prev_pred_objectness_logits[0]) 9 | loss += anchor_delta_distillation(pred_anchor_deltas[0], prev_pred_anchor_deltas[0]) 10 | return {"loss_dist_rpn": loss} 11 | 12 | 13 | def backbone_loss(features, prev_features): 14 | loss = feature_distillation(features['res4'], prev_features['res4']) 15 | return {"loss_dist_backbone": loss} 16 | 17 | 18 | def roi_head_loss(pred_class_logits, pred_proposal_deltas, prev_pred_class_logits, prev_pred_proposal_deltas, dist_loss_weight=0.5): 19 | loss = logit_distillation(pred_class_logits, prev_pred_class_logits) 20 | # loss = feature_distillation(pred_class_logits, prev_pred_class_logits) 21 | loss += anchor_delta_distillation(pred_proposal_deltas, prev_pred_proposal_deltas) 22 | return {"loss_dist_roi_head": dist_loss_weight * loss} 23 | 24 | 25 | def logit_distillation(current_logits, prev_logits, T=6.0): 26 | p = F.log_softmax(current_logits / T, dim=1) 27 | q = F.softmax(prev_logits / T, dim=1) 28 | kl_div = torch.sum(F.kl_div(p, q, reduction='none').clamp(min=0.0) * (T**2)) / current_logits.shape[0] 29 | return kl_div 30 | 31 | 32 | def anchor_delta_distillation(current_delta, prev_delta): 33 | # return smooth_l1_loss(current_delta, prev_delta, beta=0.1, reduction='mean') 34 | return F.mse_loss(current_delta, prev_delta) 35 | 36 | 37 | def feature_distillation(features, prev_features): 38 | # return smooth_l1_loss(features, prev_features, beta=0.1, reduction='mean') 39 | return F.mse_loss(features, prev_features) 40 | # criterion_kd = Attention() 41 | # # criterion_kd = NSTLoss() 42 | # # criterion_kd = DistillKL(T=4) 43 | # # criterion_kd = FactorTransfer() 44 | # loss = criterion_kd(features, prev_features) 45 | # loss = torch.stack(loss, dim=0).sum() 46 | # return loss 47 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /detection/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 | 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 tridentnet import add_tridentnet_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_tridentnet_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 | -------------------------------------------------------------------------------- /detection/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 | -------------------------------------------------------------------------------- /classification/utils/incremental/compute_features.py: -------------------------------------------------------------------------------- 1 | ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 | ## Created by: Yaoyao Liu 3 | ## Modified from: https://github.com/hshustc/CVPR19_Incremental_Learning 4 | ## Max Planck Institute for Informatics 5 | ## yaoyao.liu@mpi-inf.mpg.de 6 | ## Copyright (c) 2021 7 | ## 8 | ## This source code is licensed under the MIT-style license found in the 9 | ## LICENSE file in the root directory of this source tree 10 | ##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 11 | """ The functions that compute the features """ 12 | import torch 13 | import torch.nn as nn 14 | import torch.nn.functional as F 15 | import torch.optim as optim 16 | from torch.optim import lr_scheduler 17 | import torchvision 18 | from torchvision import datasets, models, transforms 19 | from torch.autograd import Variable 20 | import numpy as np 21 | import time 22 | import os 23 | import copy 24 | import argparse 25 | from PIL import Image 26 | from scipy.spatial.distance import cdist 27 | from sklearn.metrics import confusion_matrix 28 | from utils.misc import * 29 | from utils.process_fp import process_inputs_fp 30 | 31 | def compute_features(the_args, fusion_vars, tg_model, free_model, tg_feature_model, \ 32 | is_start_iteration, evalloader, num_samples, num_features, device=None): 33 | if device is None: 34 | device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu") 35 | tg_feature_model.eval() 36 | tg_model.eval() 37 | if free_model is not None: 38 | free_model.eval() 39 | 40 | features = np.zeros([num_samples, num_features]) 41 | start_idx = 0 42 | with torch.no_grad(): 43 | for inputs, targets in evalloader: 44 | inputs = inputs.to(device) 45 | if is_start_iteration: 46 | the_feature = tg_feature_model(inputs) 47 | else: 48 | the_feature = process_inputs_fp(the_args, fusion_vars, tg_model, free_model, inputs, feature_mode=True) 49 | features[start_idx:start_idx+inputs.shape[0], :] = np.squeeze(the_feature.cpu()) 50 | start_idx = start_idx+inputs.shape[0] 51 | assert(start_idx==num_samples) 52 | return features 53 | --------------------------------------------------------------------------------