├── .gitignore ├── .isort.cfg ├── .pre-commit-config.yaml ├── .style.yapf ├── .travis.yml ├── LICENSE ├── README.md ├── coco_test_12510.jpg ├── configs ├── albu_example │ └── mask_rcnn_r50_fpn_1x.py ├── atss │ ├── README.md │ └── atss_r50_fpn_1x.py ├── bingzao │ └── cascade_rcnn_r101_fpn_1x.py ├── carafe │ ├── README.md │ ├── faster_rcnn_r50_fpn_carafe_1x.py │ └── mask_rcnn_r50_fpn_carafe_1x.py ├── cascade_mask_rcnn_r101_fpn_1x.py ├── cascade_mask_rcnn_r50_caffe_c4_1x.py ├── cascade_mask_rcnn_r50_fpn_1x.py ├── cascade_mask_rcnn_x101_32x4d_fpn_1x.py ├── cascade_mask_rcnn_x101_64x4d_fpn_1x.py ├── cascade_rcnn_r101_fpn_1x.py ├── cascade_rcnn_r50_caffe_c4_1x.py ├── cascade_rcnn_r50_fpn_1x.py ├── cascade_rcnn_x101_32x4d_fpn_1x.py ├── cascade_rcnn_x101_64x4d_fpn_1x.py ├── cityscapes │ ├── README.md │ ├── faster_rcnn_r50_fpn_1x_cityscapes.py │ └── mask_rcnn_r50_fpn_1x_cityscapes.py ├── dcn │ ├── README.md │ ├── cascade_mask_rcnn_dconv_c3-c5_r50_fpn_1x.py │ ├── cascade_rcnn_dconv_c3-c5_r50_fpn_1x.py │ ├── faster_rcnn_dconv_c3-c5_r50_fpn_1x.py │ ├── faster_rcnn_dconv_c3-c5_x101_32x4d_fpn_1x.py │ ├── faster_rcnn_dpool_r50_fpn_1x.py │ ├── faster_rcnn_mdconv_c3-c5_group4_r50_fpn_1x.py │ ├── faster_rcnn_mdconv_c3-c5_r50_fpn_1x.py │ ├── faster_rcnn_mdpool_r50_fpn_1x.py │ ├── mask_rcnn_dconv_c3-c5_r50_fpn_1x.py │ └── mask_rcnn_mdconv_c3-c5_r50_fpn_1x.py ├── double_heads │ └── dh_faster_rcnn_r50_fpn_1x.py ├── empirical_attention │ ├── README.md │ ├── faster_rcnn_r50_fpn_attention_0010_1x.py │ ├── faster_rcnn_r50_fpn_attention_0010_dcn_1x.py │ ├── faster_rcnn_r50_fpn_attention_1111_1x.py │ └── faster_rcnn_r50_fpn_attention_1111_dcn_1x.py ├── fast_mask_rcnn_r101_fpn_1x.py ├── fast_mask_rcnn_r50_caffe_c4_1x.py ├── fast_mask_rcnn_r50_fpn_1x.py ├── fast_rcnn_r101_fpn_1x.py ├── fast_rcnn_r50_caffe_c4_1x.py ├── fast_rcnn_r50_fpn_1x.py ├── faster_rcnn_ohem_r50_fpn_1x.py ├── faster_rcnn_r101_fpn_1x.py ├── faster_rcnn_r50_caffe_c4_1x.py ├── faster_rcnn_r50_fpn_1x.py ├── faster_rcnn_x101_32x4d_fpn_1x.py ├── faster_rcnn_x101_64x4d_fpn_1x.py ├── fcos │ ├── README.md │ ├── fcos_center_r50_caffe_fpn_gn_1x_4gpu.py.py │ ├── fcos_mstrain_640_800_r101_caffe_fpn_gn_2x_4gpu.py │ ├── fcos_mstrain_640_800_x101_64x4d_fpn_gn_2x.py │ └── fcos_r50_caffe_fpn_gn_1x_4gpu.py ├── foveabox │ ├── README.md │ ├── fovea_align_gn_ms_r101_fpn_4gpu_2x.py │ ├── fovea_align_gn_ms_r50_fpn_4gpu_2x.py │ ├── fovea_align_gn_r101_fpn_4gpu_2x.py │ ├── fovea_align_gn_r50_fpn_4gpu_2x.py │ └── fovea_r50_fpn_4gpu_1x.py ├── fp16 │ ├── faster_rcnn_r50_fpn_fp16_1x.py │ ├── mask_rcnn_r50_fpn_fp16_1x.py │ └── retinanet_r50_fpn_fp16_1x.py ├── free_anchor │ ├── README.md │ ├── retinanet_free_anchor_r101_fpn_1x.py │ ├── retinanet_free_anchor_r50_fpn_1x.py │ └── retinanet_free_anchor_x101-32x4d_fpn_1x.py ├── gcnet │ ├── README.md │ ├── mask_rcnn_r16_gcb_c3-c5_r50_fpn_1x.py │ ├── mask_rcnn_r16_gcb_c3-c5_r50_fpn_syncbn_1x.py │ ├── mask_rcnn_r4_gcb_c3-c5_r50_fpn_1x.py │ ├── mask_rcnn_r4_gcb_c3-c5_r50_fpn_syncbn_1x.py │ └── mask_rcnn_r50_fpn_sbn_1x.py ├── ghm │ ├── README.md │ └── retinanet_ghm_r50_fpn_1x.py ├── gn+ws │ ├── README.md │ ├── faster_rcnn_r50_fpn_gn_ws_1x.py │ ├── mask_rcnn_r50_fpn_gn_ws_20_23_24e.py │ ├── mask_rcnn_r50_fpn_gn_ws_2x.py │ └── mask_rcnn_x101_32x4d_fpn_gn_ws_2x.py ├── gn │ ├── README.md │ ├── mask_rcnn_r101_fpn_gn_2x.py │ ├── mask_rcnn_r50_fpn_gn_2x.py │ └── mask_rcnn_r50_fpn_gn_contrib_2x.py ├── grid_rcnn │ ├── README.md │ ├── grid_rcnn_gn_head_r50_fpn_2x.py │ └── grid_rcnn_gn_head_x101_32x4d_fpn_2x.py ├── guided_anchoring │ ├── README.md │ ├── ga_fast_r50_caffe_fpn_1x.py │ ├── ga_faster_r50_caffe_fpn_1x.py │ ├── ga_faster_x101_32x4d_fpn_1x.py │ ├── ga_retinanet_r50_caffe_fpn_1x.py │ ├── ga_retinanet_x101_32x4d_fpn_1x.py │ ├── ga_rpn_r101_caffe_rpn_1x.py │ ├── ga_rpn_r50_caffe_fpn_1x.py │ └── ga_rpn_x101_32x4d_fpn_1x.py ├── hrnet │ ├── README.md │ ├── cascade_mask_rcnn_hrnetv2p_w32_20e.py │ ├── cascade_rcnn_hrnetv2p_w32_20e.py │ ├── faster_rcnn_hrnetv2p_w18_1x.py │ ├── faster_rcnn_hrnetv2p_w32_1x.py │ ├── faster_rcnn_hrnetv2p_w40_1x.py │ ├── fcos_hrnetv2p_w32_gn_1x_4gpu.py │ ├── htc_hrnetv2p_w32_20e.py │ ├── mask_rcnn_hrnetv2p_w18_1x.py │ └── mask_rcnn_hrnetv2p_w32_1x.py ├── htc │ ├── README.md │ ├── htc_dconv_c3-c5_mstrain_400_1400_x101_64x4d_fpn_20e.py │ ├── htc_r101_fpn_20e.py │ ├── htc_r50_fpn_1x.py │ ├── htc_r50_fpn_20e.py │ ├── htc_without_semantic_r50_fpn_1x.py │ ├── htc_x101_32x4d_fpn_20e_16gpu.py │ └── htc_x101_64x4d_fpn_20e_16gpu.py ├── instaboost │ ├── README.md │ ├── cascade_mask_rcnn_r50_fpn_instaboost_4x.py │ ├── mask_rcnn_r50_fpn_instaboost_4x.py │ └── ssd300_coco_instaboost_4x.py ├── libra_rcnn │ ├── README.md │ ├── libra_fast_rcnn_r50_fpn_1x.py │ ├── libra_faster_rcnn_r101_fpn_1x.py │ ├── libra_faster_rcnn_r50_fpn_1x.py │ ├── libra_faster_rcnn_x101_64x4d_fpn_1x.py │ └── libra_retinanet_r50_fpn_1x.py ├── mask_rcnn_r101_fpn_1x.py ├── mask_rcnn_r50_caffe_c4_1x.py ├── mask_rcnn_r50_fpn_1x.py ├── mask_rcnn_x101_32x4d_fpn_1x.py ├── mask_rcnn_x101_64x4d_fpn_1x.py ├── ms_rcnn │ ├── README.md │ ├── ms_rcnn_r101_caffe_fpn_1x.py │ ├── ms_rcnn_r50_caffe_fpn_1x.py │ └── ms_rcnn_x101_64x4d_fpn_1x.py ├── nas_fpn │ ├── README.md │ ├── retinanet_crop640_r50_fpn_50e.py │ └── retinanet_crop640_r50_nasfpn_50e.py ├── pascal_voc │ ├── README.md │ ├── faster_rcnn_r50_fpn_1x_voc0712.py │ ├── ssd300_voc.py │ └── ssd512_voc.py ├── reppoints │ ├── README.md │ ├── bbox_r50_grid_center_fpn_1x.py │ ├── bbox_r50_grid_fpn_1x.py │ ├── reppoints.png │ ├── reppoints_minmax_r50_fpn_1x.py │ ├── reppoints_moment_r101_dcn_fpn_2x.py │ ├── reppoints_moment_r101_dcn_fpn_2x_mt.py │ ├── reppoints_moment_r101_fpn_2x.py │ ├── reppoints_moment_r101_fpn_2x_mt.py │ ├── reppoints_moment_r50_fpn_1x.py │ ├── reppoints_moment_r50_fpn_2x.py │ ├── reppoints_moment_r50_fpn_2x_mt.py │ ├── reppoints_moment_r50_no_gn_fpn_1x.py │ ├── reppoints_moment_x101_dcn_fpn_2x.py │ ├── reppoints_moment_x101_dcn_fpn_2x_mt.py │ └── reppoints_partial_minmax_r50_fpn_1x.py ├── retinanet_r101_fpn_1x.py ├── retinanet_r50_fpn_1x.py ├── retinanet_x101_32x4d_fpn_1x.py ├── retinanet_x101_64x4d_fpn_1x.py ├── rpn_r101_fpn_1x.py ├── rpn_r50_caffe_c4_1x.py ├── rpn_r50_fpn_1x.py ├── rpn_x101_32x4d_fpn_1x.py ├── rpn_x101_64x4d_fpn_1x.py ├── scratch │ ├── README.md │ ├── scratch_faster_rcnn_r50_fpn_gn_6x.py │ └── scratch_mask_rcnn_r50_fpn_gn_6x.py ├── ssd300_coco.py ├── ssd512_coco.py └── wider_face │ ├── README.md │ └── ssd300_wider_face.py ├── demo.py ├── demo ├── coco_test_12510.jpg ├── corruptions_sev_3.png ├── data_pipeline.png ├── demo.jpg ├── inference_demo.ipynb ├── loss_curve.png └── webcam_demo.py ├── mmdet ├── __init__.py ├── apis │ ├── __init__.py │ ├── inference.py │ ├── test.py │ └── train.py ├── core │ ├── __init__.py │ ├── anchor │ │ ├── __init__.py │ │ ├── anchor_generator.py │ │ ├── anchor_target.py │ │ ├── guided_anchor_target.py │ │ ├── point_generator.py │ │ └── point_target.py │ ├── bbox │ │ ├── __init__.py │ │ ├── assign_sampling.py │ │ ├── assigners │ │ │ ├── __init__.py │ │ │ ├── approx_max_iou_assigner.py │ │ │ ├── assign_result.py │ │ │ ├── atss_assigner.py │ │ │ ├── base_assigner.py │ │ │ ├── max_iou_assigner.py │ │ │ └── point_assigner.py │ │ ├── bbox_target.py │ │ ├── demodata.py │ │ ├── geometry.py │ │ ├── samplers │ │ │ ├── __init__.py │ │ │ ├── base_sampler.py │ │ │ ├── combined_sampler.py │ │ │ ├── instance_balanced_pos_sampler.py │ │ │ ├── iou_balanced_neg_sampler.py │ │ │ ├── ohem_sampler.py │ │ │ ├── pseudo_sampler.py │ │ │ ├── random_sampler.py │ │ │ └── sampling_result.py │ │ └── transforms.py │ ├── evaluation │ │ ├── __init__.py │ │ ├── bbox_overlaps.py │ │ ├── class_names.py │ │ ├── eval_hooks.py │ │ ├── mean_ap.py │ │ └── recall.py │ ├── fp16 │ │ ├── __init__.py │ │ ├── decorators.py │ │ ├── hooks.py │ │ └── utils.py │ ├── mask │ │ ├── __init__.py │ │ ├── mask_target.py │ │ └── utils.py │ ├── optimizer │ │ ├── __init__.py │ │ ├── builder.py │ │ ├── copy_of_sgd.py │ │ └── registry.py │ ├── post_processing │ │ ├── __init__.py │ │ ├── bbox_nms.py │ │ └── merge_augs.py │ └── utils │ │ ├── __init__.py │ │ ├── dist_utils.py │ │ └── misc.py ├── datasets │ ├── __init__.py │ ├── bingzao.py │ ├── builder.py │ ├── cityscapes.py │ ├── coco.py │ ├── custom.py │ ├── dataset_wrappers.py │ ├── loader │ │ ├── __init__.py │ │ ├── build_loader.py │ │ └── sampler.py │ ├── pipelines │ │ ├── __init__.py │ │ ├── compose.py │ │ ├── formating.py │ │ ├── instaboost.py │ │ ├── loading.py │ │ ├── test_aug.py │ │ └── transforms.py │ ├── registry.py │ ├── voc.py │ ├── wider_face.py │ └── xml_style.py ├── models │ ├── __init__.py │ ├── anchor_heads │ │ ├── __init__.py │ │ ├── anchor_head.py │ │ ├── atss_head.py │ │ ├── fcos_head.py │ │ ├── fovea_head.py │ │ ├── free_anchor_retina_head.py │ │ ├── ga_retina_head.py │ │ ├── ga_rpn_head.py │ │ ├── guided_anchor_head.py │ │ ├── reppoints_head.py │ │ ├── retina_head.py │ │ ├── retina_sepbn_head.py │ │ ├── rpn_head.py │ │ └── ssd_head.py │ ├── backbones │ │ ├── __init__.py │ │ ├── hrnet.py │ │ ├── resnet.py │ │ ├── resnext.py │ │ └── ssd_vgg.py │ ├── bbox_heads │ │ ├── __init__.py │ │ ├── bbox_head.py │ │ ├── convfc_bbox_head.py │ │ └── double_bbox_head.py │ ├── builder.py │ ├── detectors │ │ ├── __init__.py │ │ ├── atss.py │ │ ├── base.py │ │ ├── cascade_rcnn.py │ │ ├── double_head_rcnn.py │ │ ├── fast_rcnn.py │ │ ├── faster_rcnn.py │ │ ├── fcos.py │ │ ├── fovea.py │ │ ├── grid_rcnn.py │ │ ├── htc.py │ │ ├── mask_rcnn.py │ │ ├── mask_scoring_rcnn.py │ │ ├── reppoints_detector.py │ │ ├── retinanet.py │ │ ├── rpn.py │ │ ├── single_stage.py │ │ ├── test_mixins.py │ │ └── two_stage.py │ ├── losses │ │ ├── __init__.py │ │ ├── accuracy.py │ │ ├── balanced_l1_loss.py │ │ ├── cross_entropy_loss.py │ │ ├── focal_loss.py │ │ ├── ghm_loss.py │ │ ├── iou_loss.py │ │ ├── mse_loss.py │ │ ├── smooth_l1_loss.py │ │ └── utils.py │ ├── mask_heads │ │ ├── __init__.py │ │ ├── fcn_mask_head.py │ │ ├── fused_semantic_head.py │ │ ├── grid_head.py │ │ ├── htc_mask_head.py │ │ └── maskiou_head.py │ ├── necks │ │ ├── __init__.py │ │ ├── bfp.py │ │ ├── fpn.py │ │ ├── fpn_carafe.py │ │ ├── hrfpn.py │ │ └── nas_fpn.py │ ├── registry.py │ ├── roi_extractors │ │ ├── __init__.py │ │ └── single_level.py │ ├── shared_heads │ │ ├── __init__.py │ │ └── res_layer.py │ └── utils │ │ ├── __init__.py │ │ └── weight_init.py ├── ops │ ├── __init__.py │ ├── activation.py │ ├── affine_grid │ │ ├── __init__.py │ │ ├── affine_grid.py │ │ └── src │ │ │ └── affine_grid_cuda.cpp │ ├── carafe │ │ ├── __init__.py │ │ ├── carafe.py │ │ ├── grad_check.py │ │ ├── setup.py │ │ └── src │ │ │ ├── carafe_cuda.cpp │ │ │ ├── carafe_cuda_kernel.cu │ │ │ ├── carafe_naive_cuda.cpp │ │ │ └── carafe_naive_cuda_kernel.cu │ ├── context_block.py │ ├── conv.py │ ├── conv_module.py │ ├── conv_ws.py │ ├── dcn │ │ ├── __init__.py │ │ ├── deform_conv.py │ │ ├── deform_pool.py │ │ └── src │ │ │ ├── deform_conv_cuda.cpp │ │ │ ├── deform_conv_cuda_kernel.cu │ │ │ ├── deform_pool_cuda.cpp │ │ │ └── deform_pool_cuda_kernel.cu │ ├── generalized_attention.py │ ├── grid_sampler │ │ ├── __init__.py │ │ ├── grid_sampler.py │ │ └── src │ │ │ ├── cpu │ │ │ ├── grid_sampler_cpu.cpp │ │ │ └── grid_sampler_cpu.h │ │ │ ├── cuda │ │ │ ├── grid_sampler_cuda.cu │ │ │ └── grid_sampler_cuda.cuh │ │ │ ├── cudnn │ │ │ └── grid_sampler_cudnn.cpp │ │ │ └── grid_sampler.cpp │ ├── masked_conv │ │ ├── __init__.py │ │ ├── masked_conv.py │ │ └── src │ │ │ ├── masked_conv2d_cuda.cpp │ │ │ └── masked_conv2d_kernel.cu │ ├── nms │ │ ├── __init__.py │ │ ├── nms_wrapper.py │ │ └── src │ │ │ ├── nms_cpu.cpp │ │ │ ├── nms_cuda.cpp │ │ │ └── nms_kernel.cu │ ├── non_local.py │ ├── norm.py │ ├── roi_align │ │ ├── __init__.py │ │ ├── gradcheck.py │ │ ├── roi_align.py │ │ └── src │ │ │ ├── roi_align_cuda.cpp │ │ │ ├── roi_align_kernel.cu │ │ │ └── roi_align_kernel_v2.cu │ ├── roi_pool │ │ ├── __init__.py │ │ ├── gradcheck.py │ │ ├── roi_pool.py │ │ └── src │ │ │ ├── roi_pool_cuda.cpp │ │ │ └── roi_pool_kernel.cu │ ├── scale.py │ ├── sigmoid_focal_loss │ │ ├── __init__.py │ │ ├── sigmoid_focal_loss.py │ │ └── src │ │ │ ├── sigmoid_focal_loss.cpp │ │ │ └── sigmoid_focal_loss_cuda.cu │ ├── upsample.py │ └── utils │ │ ├── __init__.py │ │ └── src │ │ └── compiling_info.cpp └── utils │ ├── __init__.py │ ├── collect_env.py │ ├── contextmanagers.py │ ├── flops_counter.py │ ├── logger.py │ ├── profiling.py │ ├── registry.py │ └── util_mixins.py ├── pytest.ini ├── requirements.txt ├── requirements ├── build.txt ├── optional.txt ├── runtime.txt └── tests.txt ├── setup.py ├── test_fast_rcnn.jpg ├── tests ├── async_benchmark.py ├── test_assigner.py ├── test_async.py ├── test_config.py ├── test_forward.py ├── test_heads.py ├── test_nms.py ├── test_sampler.py ├── test_soft_nms.py └── test_utils.py └── tools ├── analyze_logs.py ├── browse_dataset.py ├── coco_error_analysis.py ├── convert_datasets ├── cityscapes.py └── pascal_voc.py ├── data_process ├── 00_img_rename.py ├── 01_check_img.py ├── 02_check_box.py ├── 03_xml2coco.py ├── generate_test_json.py └── readme ├── detectron2pytorch.py ├── dist_test.sh ├── dist_train.sh ├── get_flops.py ├── publish_model.py ├── pytorch2onnx.py ├── robustness_eval.py ├── slurm_test.sh ├── slurm_train.sh ├── test.py ├── test_robustness.py ├── train.py └── upgrade_model_version.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | *.spec 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .coverage 42 | .coverage.* 43 | .cache 44 | nosetests.xml 45 | coverage.xml 46 | *.cover 47 | .hypothesis/ 48 | .pytest_cache/ 49 | 50 | # Translations 51 | *.mo 52 | *.pot 53 | 54 | # Django stuff: 55 | *.log 56 | local_settings.py 57 | db.sqlite3 58 | 59 | # Flask stuff: 60 | instance/ 61 | .webassets-cache 62 | 63 | # Scrapy stuff: 64 | .scrapy 65 | 66 | # Sphinx documentation 67 | docs/_build/ 68 | 69 | # PyBuilder 70 | target/ 71 | 72 | # Jupyter Notebook 73 | .ipynb_checkpoints 74 | 75 | # pyenv 76 | .python-version 77 | 78 | # celery beat schedule file 79 | celerybeat-schedule 80 | 81 | # SageMath parsed files 82 | *.sage.py 83 | 84 | # Environments 85 | .env 86 | .venv 87 | env/ 88 | venv/ 89 | ENV/ 90 | env.bak/ 91 | venv.bak/ 92 | 93 | # Spyder project settings 94 | .spyderproject 95 | .spyproject 96 | 97 | # Rope project settings 98 | .ropeproject 99 | 100 | # mkdocs documentation 101 | /site 102 | 103 | # mypy 104 | .mypy_cache/ 105 | 106 | mmdet/version.py 107 | data 108 | .vscode 109 | .idea 110 | 111 | # custom 112 | *.pkl 113 | *.pkl.json 114 | *.log.json 115 | work_dirs/ 116 | 117 | # Pytorch 118 | *.pth 119 | -------------------------------------------------------------------------------- /.isort.cfg: -------------------------------------------------------------------------------- 1 | [isort] 2 | line_length = 79 3 | multi_line_output = 0 4 | known_standard_library = setuptools 5 | known_first_party = mmdet 6 | known_third_party = asynctest,cityscapesscripts,cv2,matplotlib,mmcv,numpy,onnx,pycocotools,robustness_eval,roi_align,roi_pool,seaborn,six,terminaltables,torch,torchvision 7 | no_lines_before = STDLIB,LOCALFOLDER 8 | default_section = THIRDPARTY 9 | -------------------------------------------------------------------------------- /.pre-commit-config.yaml: -------------------------------------------------------------------------------- 1 | repos: 2 | - repo: https://gitlab.com/pycqa/flake8.git 3 | rev: 3.7.9 4 | hooks: 5 | - id: flake8 6 | - repo: https://github.com/asottile/seed-isort-config 7 | rev: v2.1.0 8 | hooks: 9 | - id: seed-isort-config 10 | - repo: https://github.com/timothycrosley/isort 11 | rev: 4.3.21 12 | hooks: 13 | - id: isort 14 | - repo: https://github.com/pre-commit/mirrors-yapf 15 | rev: v0.29.0 16 | hooks: 17 | - id: yapf 18 | - repo: https://github.com/pre-commit/pre-commit-hooks 19 | rev: v2.5.0 20 | hooks: 21 | - id: trailing-whitespace 22 | - id: check-yaml 23 | - id: end-of-file-fixer 24 | - id: requirements-txt-fixer 25 | - id: double-quote-string-fixer 26 | - id: fix-encoding-pragma 27 | args: ["--remove"] 28 | -------------------------------------------------------------------------------- /.style.yapf: -------------------------------------------------------------------------------- 1 | [style] 2 | BASED_ON_STYLE = pep8 3 | BLANK_LINE_BEFORE_NESTED_CLASS_OR_DEF = true 4 | SPLIT_BEFORE_EXPRESSION_AFTER_OPENING_PAREN = true 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | dist: bionic # ubuntu 18.04 2 | language: python 3 | 4 | python: 5 | - "3.5" 6 | - "3.6" 7 | - "3.7" 8 | 9 | env: CUDA=10.1.105-1 CUDA_SHORT=10.1 UBUNTU_VERSION=ubuntu1804 FORCE_CUDA=1 10 | cache: pip 11 | 12 | # Ref to CUDA installation in Travis: https://github.com/jeremad/cuda-travis 13 | before_install: 14 | - INSTALLER=cuda-repo-${UBUNTU_VERSION}_${CUDA}_amd64.deb 15 | - wget http://developer.download.nvidia.com/compute/cuda/repos/${UBUNTU_VERSION}/x86_64/${INSTALLER} 16 | - sudo dpkg -i ${INSTALLER} 17 | - wget https://developer.download.nvidia.com/compute/cuda/repos/${UBUNTU_VERSION}/x86_64/7fa2af80.pub 18 | - sudo apt-key add 7fa2af80.pub 19 | - sudo apt update -qq 20 | - sudo apt install -y cuda-${CUDA_SHORT/./-} cuda-cufft-dev-${CUDA_SHORT/./-} 21 | - sudo apt clean 22 | - CUDA_HOME=/usr/local/cuda-${CUDA_SHORT} 23 | - LD_LIBRARY_PATH=${CUDA_HOME}/lib64:${CUDA_HOME}/include:${LD_LIBRARY_PATH} 24 | - PATH=${CUDA_HOME}/bin:${PATH} 25 | 26 | install: 27 | - pip install Pillow==6.2.2 # remove this line when torchvision>=0.5 28 | - pip install torch==1.2 torchvision==0.4.0 # TODO: fix CI for pytorch>1.2 29 | - pip install "git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI" 30 | - pip install -r requirements.txt 31 | 32 | before_script: 33 | - flake8 . 34 | - isort -rc --check-only --diff mmdet/ tools/ tests/ 35 | - yapf -r -d --style .style.yapf mmdet/ tools/ tests/ configs/ 36 | 37 | script: 38 | - python setup.py check -m -s 39 | - python setup.py build_ext --inplace 40 | - coverage run --source mmdet -m py.test -v --xdoctest-modules tests mmdet 41 | 42 | after_success: 43 | - coverage report 44 | -------------------------------------------------------------------------------- /coco_test_12510.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataXujing/Cascade_RCNN_mmdetection/da1cfb8e5b36ba63ae444b8d3c011623bc175970/coco_test_12510.jpg -------------------------------------------------------------------------------- /configs/atss/README.md: -------------------------------------------------------------------------------- 1 | # Bridging the Gap Between Anchor-based and Anchor-free Detection via Adaptive Training Sample Selection 2 | 3 | 4 | ## Introduction 5 | 6 | ``` 7 | @article{zhang2019bridging, 8 | title = {Bridging the Gap Between Anchor-based and Anchor-free Detection via Adaptive Training Sample Selection}, 9 | author = {Zhang, Shifeng and Chi, Cheng and Yao, Yongqiang and Lei, Zhen and Li, Stan Z.}, 10 | journal = {arXiv preprint arXiv:1912.02424}, 11 | year = {2019} 12 | } 13 | ``` 14 | 15 | 16 | ## Results and Models 17 | 18 | | Backbone | Style | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | Download | 19 | |:---------:|:-------:|:-------:|:--------:|:-------------------:|:--------------:|:------:|:--------:| 20 | | R-50 | pytorch | 1x | 3.6 | 0.357 | 12.8 | 39.2 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/atss/atss_r50_fpn_1x_20200113-a7aa251e.pth)| 21 | -------------------------------------------------------------------------------- /configs/carafe/README.md: -------------------------------------------------------------------------------- 1 | # CARAFE: Content-Aware ReAssembly of FEatures 2 | 3 | ## Introduction 4 | 5 | We provide config files to reproduce the object detection & instance segmentation results in the ICCV 2019 Oral paper for [CARAFE: Content-Aware ReAssembly of FEatures](https://arxiv.org/abs/1905.02188). 6 | 7 | ``` 8 | @inproceedings{Wang_2019_ICCV, 9 | title = {CARAFE: Content-Aware ReAssembly of FEatures}, 10 | author = {Wang, Jiaqi and Chen, Kai and Xu, Rui and Liu, Ziwei and Loy, Chen Change and Lin, Dahua}, 11 | booktitle = {The IEEE International Conference on Computer Vision (ICCV)}, 12 | month = {October}, 13 | year = {2019} 14 | } 15 | ``` 16 | 17 | ## Results and Models 18 | 19 | The results on COCO 2017 val is shown in the below table. 20 | 21 | | Method | Backbone | Style | Lr schd | Test Proposal Num| Box AP | Mask AP | Download | 22 | | :--------------------: | :-------------: | :-----: | :-----: | :--------------: | :----: | :--------: |:----------------------------------------------------------------------------------------------------: | 23 | | Faster R-CNN w/ CARAFE | R-50-FPN | pytorch | 1x | 1000 | 37.8 | - | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/carafe/faster_rcnn_r50_fpn_carafe_1x-2ca2d094.pth) | 24 | | - | - | - | - | 2000 | 37.9 | - | - | 25 | | Mask R-CNN w/ CARAFE | R-50-FPN | pytorch | 1x | 1000 | 38.6 | 35.6| [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/carafe/mask_rcnn_r50_fpn_carafe_1x-2cc4b9fe.pth) | 26 | | - | - | - | - | 2000 | 38.6 | 35.7| - | 27 | 28 | ## Implementation 29 | 30 | The CUDA implementation of CARAFE can be find at `mmdet/ops/carafe` under this repository. 31 | 32 | ## Setup CARAFE 33 | 34 | a. Use CARAFE in mmdetection. 35 | 36 | Install mmdetection following the official guide. 37 | 38 | b. Use CARAFE in your own project. 39 | 40 | Git clone mmdetection. 41 | ```shell 42 | git clone https://github.com/open-mmlab/mmdetection.git 43 | cd mmdetection 44 | ``` 45 | Setup CARAFE in our project. 46 | ```shell 47 | cp -r ./mmdet/ops/carafe $Your_Project_Path$ 48 | cd $Your_Project_Path$/carafe 49 | python setup.py develop 50 | # or "pip install -v -e ." 51 | cd .. 52 | python ./carafe/grad_check.py 53 | ``` 54 | -------------------------------------------------------------------------------- /configs/cityscapes/README.md: -------------------------------------------------------------------------------- 1 | ## Common settings 2 | 3 | - All baselines were trained using 8 GPU with a batch size of 8 (1 images per GPU) using the [linear scaling rule](https://arxiv.org/abs/1706.02677) to scale the learning rate. 4 | - All models were trained on `cityscapes_train`, and tested on `cityscapes_val`. 5 | - 1x training schedule indicates 64 epochs which corresponds to slightly less than the 24k iterations reported in the original schedule from the [Mask R-CNN paper](https://arxiv.org/abs/1703.06870) 6 | - COCO pre-trained weights are used to initialize. 7 | - A conversion [script](../../tools/convert_datasets/cityscapes.py) is provided to convert Cityscapes into COCO format. Please refer to [INSTALL.md](../../docs/INSTALL.md#prepare-datasets) for details. 8 | - `CityscapesDataset` implemented three evaluation methods. `bbox` and `segm` are standard COCO bbox/mask AP. `cityscapes` is the cityscapes dataset official evaluation, which may be slightly higher than COCO. 9 | 10 | 11 | ### Faster R-CNN 12 | 13 | | Backbone | Style | Lr schd | Scale | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | Download | 14 | | :-------------: | :-----: | :-----: | :---: | :------: | :-----------------: | :------------: | :----: | :------: | 15 | | R-50-FPN | pytorch | 1x | 800-1024 | 4.9 | - | - | 41.6 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/cityscapes/faster_rcnn_r50_fpn_1x_cityscapes_20200227-362cfbbf.pth) | 16 | 17 | ### Mask R-CNN 18 | 19 | | Backbone | Style | Lr schd | Scale | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | mask AP | Download | 20 | | :-------------: | :-----: | :-----: | :------: | :------: | :-----------------: | :------------: | :----: | :-----: | :------: | 21 | | R-50-FPN | pytorch | 1x | 800-1024 | 4.9 | - | - | 41.9 | 37.1 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/cityscapes/mask_rcnn_r50_fpn_1x_cityscapes_20200227-afe51d5a.pth) | 22 | -------------------------------------------------------------------------------- /configs/empirical_attention/README.md: -------------------------------------------------------------------------------- 1 | # An Empirical Study of Spatial Attention Mechanisms in Deep Networks 2 | 3 | ## Introduction 4 | 5 | ``` 6 | @article{zhu2019empirical, 7 | title={An Empirical Study of Spatial Attention Mechanisms in Deep Networks}, 8 | author={Zhu, Xizhou and Cheng, Dazhi and Zhang, Zheng and Lin, Stephen and Dai, Jifeng}, 9 | journal={arXiv preprint arXiv:1904.05873}, 10 | year={2019} 11 | } 12 | ``` 13 | 14 | 15 | ## Results and Models 16 | 17 | | Backbone | Attention Component | DCN | Lr schd | box AP | Download | 18 | |:---------:|:-------------------:|:----:|:-------:|:------:|:--------:| 19 | | R-50 | 1111 | N | 1x | 38.6 | - | 20 | | R-50 | 0010 | N | 1x | 38.2 | - | 21 | | R-50 | 1111 | Y | 1x | 41.0 | - | 22 | | R-50 | 0010 | Y | 1x | 40.8 | - | 23 | -------------------------------------------------------------------------------- /configs/fcos/README.md: -------------------------------------------------------------------------------- 1 | # FCOS: Fully Convolutional One-Stage Object Detection 2 | 3 | ## Introduction 4 | 5 | ``` 6 | @article{tian2019fcos, 7 | title={FCOS: Fully Convolutional One-Stage Object Detection}, 8 | author={Tian, Zhi and Shen, Chunhua and Chen, Hao and He, Tong}, 9 | journal={arXiv preprint arXiv:1904.01355}, 10 | year={2019} 11 | } 12 | ``` 13 | 14 | ## Results and Models 15 | 16 | | Backbone | Style | GN | MS train | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | Download | 17 | |:---------:|:-------:|:-------:|:-------:|:-------:|:--------:|:-------------------:|:--------------:|:------:|:--------:| 18 | | R-50 | caffe | N | N | 1x | 5.5 | 0.373 | 13.7 | 35.7 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/fcos/fcos_r50_caffe_fpn_1x_4gpu_20190516-a7cac5ff.pth) | 19 | | R-50 | caffe | Y | N | 1x | 6.9 | 0.396 | 13.6 | 36.7 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/fcos/fcos_r50_caffe_fpn_gn_1x_4gpu_20190516-9f253a93.pth) | 20 | | R-50 | caffe | Y | N | 2x | - | - | - | 36.9 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/fcos/fcos_r50_caffe_fpn_gn_2x_4gpu_20190516_-93484354.pth) | 21 | | R-101 | caffe | Y | N | 1x | 10.4 | 0.558 | 11.6 | 39.1 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/fcos/fcos_r101_caffe_fpn_gn_1x_4gpu_20190516-e4889733.pth) | 22 | | R-101 | caffe | Y | N | 2x | - | - | - | 39.1 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/fcos/fcos_r101_caffe_fpn_gn_2x_4gpu_20190516-c03af97b.pth) | 23 | 24 | 25 | | Backbone | Style | GN | MS train | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | Download | 26 | |:---------:|:-------:|:-------:|:-------:|:-------:|:--------:|:-------------------:|:--------------:|:------:|:--------:| 27 | | R-50 | caffe | Y | Y | 2x | - | - | - | 38.7 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/fcos/fcos_mstrain_640_800_r50_caffe_fpn_gn_2x_4gpu_20190516-f7329d80.pth) | 28 | | R-101 | caffe | Y | Y | 2x | - | - | - | 40.8 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/fcos/fcos_mstrain_640_800_r101_caffe_fpn_gn_2x_4gpu_20190516-42e6f62d.pth) | 29 | | X-101 | caffe | Y | Y | 2x | 9.7 | 0.892 | 7.0 | 42.8 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/fcos/fcos_mstrain_640_800_x101_64x4d_fpn_gn_2x_20190516-a36c0872.pth) | 30 | 31 | **Notes:** 32 | - To be consistent with the author's implementation, we use 4 GPUs with 4 images/GPU for R-50 and R-101 models, and 8 GPUs with 2 image/GPU for X-101 models. 33 | - The X-101 backbone is X-101-64x4d. 34 | -------------------------------------------------------------------------------- /configs/free_anchor/README.md: -------------------------------------------------------------------------------- 1 | # FreeAnchor: Learning to Match Anchors for Visual Object Detection 2 | 3 | ## Introduction 4 | 5 | ``` 6 | @inproceedings{zhang2019freeanchor, 7 | title = {{FreeAnchor}: Learning to Match Anchors for Visual Object Detection}, 8 | author = {Zhang, Xiaosong and Wan, Fang and Liu, Chang and Ji, Rongrong and Ye, Qixiang}, 9 | booktitle = {Neural Information Processing Systems}, 10 | year = {2019} 11 | } 12 | ``` 13 | 14 | ## Results and Models 15 | 16 | | Backbone | Style | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | Download | 17 | |:---------:|:-------:|:-------:|:--------:|:-------------------:|:--------------:|:------:|:--------:| 18 | | R-50 | pytorch | 1x | 4.7 | 0.322 | 12.0 | 38.4 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/free_anchor/retinanet_free_anchor_r50_fpn_1x_20190914-84db6585.pth) | 19 | | R-101 | pytorch | 1x | 6.6 | 0.437 | 9.7 | 40.3 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/free_anchor/retinanet_free_anchor_r101_fpn_1x_20190914-c4e4db81.pth) | 20 | | X-101-32x4d | pytorch | 1x | 7.8 | 0.640 | 8.4 | 42.0 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/free_anchor/retinanet_free_anchor_x101-32x4d_fpn_1x_20190914-eb73b804.pth) | 21 | 22 | **Notes:** 23 | - We use 8 GPUs with 2 images/GPU. 24 | - For more settings and models, please refer to the [official repo](https://github.com/zhangxiaosong18/FreeAnchor). 25 | -------------------------------------------------------------------------------- /configs/ghm/README.md: -------------------------------------------------------------------------------- 1 | # Gradient Harmonized Single-stage Detector 2 | 3 | ## Introduction 4 | 5 | ``` 6 | @inproceedings{li2019gradient, 7 | title={Gradient Harmonized Single-stage Detector}, 8 | author={Li, Buyu and Liu, Yu and Wang, Xiaogang}, 9 | booktitle={AAAI Conference on Artificial Intelligence}, 10 | year={2019} 11 | } 12 | ``` 13 | 14 | ## Results and Models 15 | 16 | | Backbone | Style | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | Download | 17 | | :-------------: | :-----: | :-----: | :------: | :-----------------: | :------------: | :----: | :------: | 18 | | R-50-FPN | pytorch | 1x | 3.9 | 0.500 | 9.4 | 36.9 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/ghm/retinanet_ghm_r50_fpn_1x_20190608-b9aa5862.pth) | 19 | | R-101-FPN | pytorch | 1x | 5.8 | 0.625 | 8.5 | 39.0 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/ghm/retinanet_ghm_r101_fpn_1x_20190608-b885b74a.pth) | 20 | | X-101-32x4d-FPN | pytorch | 1x | 7.0 | 0.818 | 7.6 | 40.5 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/ghm/retinanet_ghm_x101_32x4d_fpn_1x_20190608-ed295d22.pth) | 21 | | X-101-64x4d-FPN | pytorch | 1x | 9.9 | 1.191 | 6.1 | 41.6 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/ghm/retinanet_ghm_x101_64x4d_fpn_1x_20190608-7f2037ce.pth) | 22 | -------------------------------------------------------------------------------- /configs/gn/README.md: -------------------------------------------------------------------------------- 1 | # Group Normalization 2 | 3 | ## Introduction 4 | 5 | ``` 6 | @inproceedings{wu2018group, 7 | title={Group Normalization}, 8 | author={Wu, Yuxin and He, Kaiming}, 9 | booktitle={Proceedings of the European Conference on Computer Vision (ECCV)}, 10 | year={2018} 11 | } 12 | ``` 13 | 14 | ## Results and Models 15 | 16 | | Backbone | model | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | mask AP | Download | 17 | |:-------------:|:----------:|:-------:|:--------:|:-------------------:|:--------------:|:------:|:-------:|:--------:| 18 | | R-50-FPN (d) | Mask R-CNN | 2x | 7.2 | 0.806 | 5.4 | 39.8 | 36.1 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/gn/mask_rcnn_r50_fpn_gn_2x_20180113-86832cf2.pth) | 19 | | R-50-FPN (d) | Mask R-CNN | 3x | 7.2 | 0.806 | 5.4 | 40.1 | 36.4 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/gn/mask_rcnn_r50_fpn_gn_3x_20180113-8e82f48d.pth) | 20 | | R-101-FPN (d) | Mask R-CNN | 2x | 9.9 | 0.970 | 4.8 | 41.5 | 37.0 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/gn/mask_rcnn_r101_fpn_gn_2x_20180113-9598649c.pth) | 21 | | R-101-FPN (d) | Mask R-CNN | 3x | 9.9 | 0.970 | 4.8 | 41.6 | 37.3 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/gn/mask_rcnn_r101_fpn_gn_3x_20180113-a14ffb96.pth) | 22 | | R-50-FPN (c) | Mask R-CNN | 2x | 7.2 | 0.806 | 5.4 | 39.7 | 35.9 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/gn/mask_rcnn_r50_fpn_gn_contrib_2x_20180113-ec93305c.pth) | 23 | | R-50-FPN (c) | Mask R-CNN | 3x | 7.2 | 0.806 | 5.4 | 40.0 | 36.2 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/gn/mask_rcnn_r50_fpn_gn_contrib_3x_20180113-9d230cab.pth) | 24 | 25 | **Notes:** 26 | - (d) means pretrained model converted from Detectron, and (c) means the contributed model pretrained by [@thangvubk](https://github.com/thangvubk). 27 | - The `3x` schedule is epoch [28, 34, 36]. 28 | - **Memory, Train/Inf time is outdated.** 29 | -------------------------------------------------------------------------------- /configs/grid_rcnn/README.md: -------------------------------------------------------------------------------- 1 | # Grid R-CNN 2 | 3 | ## Introduction 4 | 5 | ``` 6 | @inproceedings{lu2019grid, 7 | title={Grid r-cnn}, 8 | author={Lu, Xin and Li, Buyu and Yue, Yuxin and Li, Quanquan and Yan, Junjie}, 9 | booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition}, 10 | year={2019} 11 | } 12 | 13 | @article{lu2019grid, 14 | title={Grid R-CNN Plus: Faster and Better}, 15 | author={Lu, Xin and Li, Buyu and Yue, Yuxin and Li, Quanquan and Yan, Junjie}, 16 | journal={arXiv preprint arXiv:1906.05688}, 17 | year={2019} 18 | } 19 | ``` 20 | 21 | ## Results and Models 22 | 23 | | Backbone | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | Download | 24 | |:-----------:|:-------:|:--------:|:-------------------:|:--------------:|:------:|:--------:| 25 | | R-50 | 2x | 4.8 | 1.172 | 10.9 | 40.3 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/grid_rcnn/grid_rcnn_gn_head_r50_fpn_2x_20190619-5b29cf9d.pth) | 26 | | R-101 | 2x | 6.7 | 1.214 | 10.0 | 41.7 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/grid_rcnn/grid_rcnn_gn_head_r101_fpn_2x_20190619-a4b61645.pth) | 27 | | X-101-32x4d | 2x | 8.0 | 1.335 | 8.5 | 43.0 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/grid_rcnn/grid_rcnn_gn_head_x101_32x4d_fpn_2x_20190619-0bbfd87a.pth) | 28 | | X-101-64x4d | 2x | 10.9 | 1.753 | 6.4 | 43.1 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/grid_rcnn/grid_rcnn_gn_head_x101_64x4d_fpn_2x_20190619-8f4e20bb.pth) | 29 | 30 | **Notes:** 31 | - All models are trained with 8 GPUs instead of 32 GPUs in the original paper. 32 | - The warming up lasts for 1 epoch and `2x` here indicates 25 epochs. 33 | -------------------------------------------------------------------------------- /configs/htc/README.md: -------------------------------------------------------------------------------- 1 | # Hybrid Task Cascade for Instance Segmentation 2 | 3 | ## Introduction 4 | 5 | We provide config files to reproduce the results in the CVPR 2019 paper for [Hybrid Task Cascade](https://arxiv.org/abs/1901.07518). 6 | 7 | ``` 8 | @inproceedings{chen2019hybrid, 9 | title={Hybrid task cascade for instance segmentation}, 10 | author={Chen, Kai and Pang, Jiangmiao and Wang, Jiaqi and Xiong, Yu and Li, Xiaoxiao and Sun, Shuyang and Feng, Wansen and Liu, Ziwei and Shi, Jianping and Ouyang, Wanli and Chen Change Loy and Dahua Lin}, 11 | booktitle={IEEE Conference on Computer Vision and Pattern Recognition}, 12 | year={2019} 13 | } 14 | ``` 15 | 16 | ## Dataset 17 | 18 | HTC requires COCO and COCO-stuff dataset for training. You need to download and extract it in the COCO dataset path. 19 | The directory should be like this. 20 | 21 | ``` 22 | mmdetection 23 | ├── mmdet 24 | ├── tools 25 | ├── configs 26 | ├── data 27 | │ ├── coco 28 | │ │ ├── annotations 29 | │ │ ├── train2017 30 | │ │ ├── val2017 31 | │ │ ├── test2017 32 | | | ├── stuffthingmaps 33 | ``` 34 | 35 | ## Results and Models 36 | 37 | The results on COCO 2017val are shown in the below table. (results on test-dev are usually slightly higher than val) 38 | 39 | | Backbone | Style | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | mask AP | Download | 40 | |:---------:|:-------:|:-------:|:--------:|:-------------------:|:--------------:|:------:|:-------:|:--------:| 41 | | R-50-FPN | pytorch | 1x | 7.4 | 0.936 | 4.1 | 42.1 | 37.3 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/htc/htc_r50_fpn_1x_20190408-878c1712.pth) | 42 | | R-50-FPN | pytorch | 20e | - | - | - | 43.2 | 38.1 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/htc/htc_r50_fpn_20e_20190408-c03b7015.pth) | 43 | | R-101-FPN | pytorch | 20e | 9.3 | 1.051 | 4.0 | 44.9 | 39.4 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/htc/htc_r101_fpn_20e_20190408-a2e586db.pth) | 44 | | X-101-32x4d-FPN | pytorch |20e| 5.8 | 0.769 | 3.8 | 46.1 | 40.3 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/htc/htc_x101_32x4d_fpn_20e_20190408-9eae4d0b.pth) | 45 | | X-101-64x4d-FPN | pytorch |20e| 7.5 | 1.120 | 3.5 | 46.9 | 40.8 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/htc/htc_x101_64x4d_fpn_20e_20190408-497f2561.pth) | 46 | 47 | - In the HTC paper and COCO 2018 Challenge, `score_thr` is set to 0.001 for both baselines and HTC. 48 | - We use 8 GPUs with 2 images/GPU for R-50 and R-101 models, and 16 GPUs with 1 image/GPU for X-101 models. 49 | If you would like to train X-101 HTC with 8 GPUs, you need to change the lr from 0.02 to 0.01. 50 | 51 | We also provide a powerful HTC with DCN and multi-scale training model. No testing augmentation is used. 52 | 53 | | Backbone | Style | DCN | training scales | Lr schd | box AP | mask AP | Download | 54 | |:----------------:|:-------:|:-----:|:---------------:|:-------:|:------:|:-------:|:--------:| 55 | | X-101-64x4d-FPN | pytorch | c3-c5 | 400~1400 | 20e | 50.7 | 43.9 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/htc/htc_dconv_c3-c5_mstrain_400_1400_x101_64x4d_fpn_20e_20190408-0e50669c.pth) | 56 | -------------------------------------------------------------------------------- /configs/libra_rcnn/README.md: -------------------------------------------------------------------------------- 1 | # Libra R-CNN: Towards Balanced Learning for Object Detection 2 | 3 | ## Introduction 4 | 5 | We provide config files to reproduce the results in the CVPR 2019 paper [Libra R-CNN](https://arxiv.org/pdf/1904.02701.pdf). 6 | 7 | ``` 8 | @inproceedings{pang2019libra, 9 | title={Libra R-CNN: Towards Balanced Learning for Object Detection}, 10 | author={Pang, Jiangmiao and Chen, Kai and Shi, Jianping and Feng, Huajun and Ouyang, Wanli and Dahua Lin}, 11 | booktitle={IEEE Conference on Computer Vision and Pattern Recognition}, 12 | year={2019} 13 | } 14 | ``` 15 | 16 | ## Results and models 17 | 18 | The results on COCO 2017val are shown in the below table. (results on test-dev are usually slightly higher than val) 19 | 20 | | Architecture | Backbone | Style | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | Download | 21 | |:---------:|:-------:|:-------:|:--------:|:-------------------:|:--------------:|:------:|:-------:|:--------:| 22 | | Faster R-CNN | R-50-FPN | pytorch | 1x | 4.2 | 0.375 | 12.0 | 38.5 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/libra_rcnn/libra_faster_rcnn_r50_fpn_1x_20190610-bf0ea559.pth) | 23 | | Fast R-CNN | R-50-FPN | pytorch | 1x | 3.7 | 0.272 | 16.3 | 38.5 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/libra_rcnn/libra_fast_rcnn_r50_fpn_1x_20190525-a43f88b5.pth) | 24 | | Faster R-CNN | R-101-FPN | pytorch | 1x | 6.0 | 0.495 | 10.4 | 40.3 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/libra_rcnn/libra_faster_rcnn_r101_fpn_1x_20190525-94e94051.pth) | 25 | | Faster R-CNN | X-101-64x4d-FPN | pytorch | 1x | 10.1 | 1.050 | 6.8 | 42.7 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/libra_rcnn/libra_faster_rcnn_x101_64x4d_fpn_1x_20190525-359c134a.pth) | 26 | | RetinaNet | R-50-FPN | pytorch | 1x | 3.7 | 0.328 | 11.8 | 37.7 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/libra_rcnn/libra_retinanet_r50_fpn_1x_20190525-ead2a6bb.pth) | 27 | -------------------------------------------------------------------------------- /configs/ms_rcnn/README.md: -------------------------------------------------------------------------------- 1 | # Mask Scoring R-CNN 2 | 3 | ## Introduction 4 | 5 | ``` 6 | @inproceedings{huang2019msrcnn, 7 | title={Mask Scoring R-CNN}, 8 | author={Zhaojin Huang and Lichao Huang and Yongchao Gong and Chang Huang and Xinggang Wang}, 9 | booktitle={IEEE Conference on Computer Vision and Pattern Recognition}, 10 | year={2019}, 11 | } 12 | ``` 13 | 14 | ## Results and Models 15 | 16 | | Backbone | style | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | mask AP | Download | 17 | |:-------------:|:----------:|:-------:|:--------:|:-------------------:|:--------------:|:------:|:-------:|:--------:| 18 | | R-50-FPN | caffe | 1x | 4.3 | 0.537 | 10.1 | 37.4 | 35.5 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/ms-rcnn/ms_rcnn_r50_caffe_fpn_1x_20190624-619934b5.pth) | 19 | | R-50-FPN | caffe | 2x | - | - | - | 38.2 | 35.9 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/ms-rcnn/ms_rcnn_r50_caffe_fpn_2x_20190525-a07be31e.pth) | 20 | | R-101-FPN | caffe | 1x | 6.2 | 0.682 | 9.1 | 39.8 | 37.2 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/ms-rcnn/ms_rcnn_r101_caffe_fpn_1x_20190624-677a5548.pth) | 21 | | R-101-FPN | caffe | 2x | - | - | - | 40.7 | 37.8 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/ms-rcnn/ms_rcnn_r101_caffe_fpn_2x_20190525-4aee1528.pth) | 22 | | R-X101-32x4d | pytorch | 2x | 7.6 | 0.844 | 8.0 | 41.7 | 38.5 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/ms-rcnn/ms_rcnn_x101_32x4d_fpn_2x_20190628-ab454d07.pth) | 23 | | R-X101-64x4d | pytorch | 1x | 10.5 | 1.214 | 6.4 | 42.0 | 39.1 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/ms-rcnn/ms_rcnn_x101_64x4d_fpn_1x_20190628-dec32bda.pth) | 24 | | R-X101-64x4d | pytorch | 2x | - | - | - | 42.2 | 38.9 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/ms-rcnn/ms_rcnn_x101_64x4d_fpn_2x_20190525-c044c25a.pth) | 25 | -------------------------------------------------------------------------------- /configs/nas_fpn/README.md: -------------------------------------------------------------------------------- 1 | # NAS-FPN: Learning Scalable Feature Pyramid Architecture for Object Detection 2 | 3 | ## Introduction 4 | 5 | ``` 6 | @inproceedings{ghiasi2019fpn, 7 | title={Nas-fpn: Learning scalable feature pyramid architecture for object detection}, 8 | author={Ghiasi, Golnaz and Lin, Tsung-Yi and Le, Quoc V}, 9 | booktitle={Proceedings of the IEEE Conference on Computer Vision and Pattern Recognition}, 10 | pages={7036--7045}, 11 | year={2019} 12 | } 13 | ``` 14 | 15 | ## Results and Models 16 | 17 | We benchmark the new training schedule (crop training, large batch, unfrozen BN, 50 epochs) introduced in NAS-FPN. RetinaNet is used in the paper. 18 | 19 | | Backbone | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | Download | 20 | |:-----------:|:-------:|:--------:|:-------------------:|:--------------:|:------:|:--------:| 21 | | R-50-FPN | 50e | 12.8 | 0.513 | 15.3 | 37.0 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/nas_fpn/retinanet_crop640_r50_fpn_50e_190824-4d75bfa0.pth) | 22 | | R-50-NASFPN | 50e | 14.8 | 0.662 | 13.1 | 39.8 | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/nas_fpn/retinanet_crop640_r50_nasfpn_50e_20191225-b82d3a86.pth) | 23 | 24 | 25 | **Note**: We find that it is unstable to train NAS-FPN and there is a small chance that results can be 3% mAP lower. 26 | -------------------------------------------------------------------------------- /configs/pascal_voc/README.md: -------------------------------------------------------------------------------- 1 | ### SSD 2 | 3 | | Backbone | Size | Style | Lr schd | Mem (GB) | Train time (s/iter) | Inf time (fps) | box AP | Download | 4 | | :------: | :---: | :---: | :-----: | :------: | :-----------------: | :------------: | :----: | :------------------------------------------------------------------------------------------------------------------------------: | 5 | | VGG16 | 300 | caffe | 240e | 2.5 | 0.159 | 35.7 / 53.6 | 77.5 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/ssd300_voc_vgg16_caffe_240e_20190501-7160d09a.pth) | 6 | | VGG16 | 512 | caffe | 240e | 4.3 | 0.214 | 27.5 / 35.9 | 80.0 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/ssd512_voc_vgg16_caffe_240e_20190501-ff194be1.pth) | 7 | -------------------------------------------------------------------------------- /configs/reppoints/reppoints.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataXujing/Cascade_RCNN_mmdetection/da1cfb8e5b36ba63ae444b8d3c011623bc175970/configs/reppoints/reppoints.png -------------------------------------------------------------------------------- /configs/scratch/README.md: -------------------------------------------------------------------------------- 1 | # Rethinking ImageNet Pre-training 2 | 3 | ## Introduction 4 | 5 | ``` 6 | @article{he2018rethinking, 7 | title={Rethinking imagenet pre-training}, 8 | author={He, Kaiming and Girshick, Ross and Doll{\'a}r, Piotr}, 9 | journal={arXiv preprint arXiv:1811.08883}, 10 | year={2018} 11 | } 12 | ``` 13 | 14 | ## Results and Models 15 | 16 | | Model | Backbone | Style | Lr schd | box AP | mask AP | Download | 17 | |:------------:|:---------:|:-------:|:-------:|:------:|:-------:|:--------:| 18 | | Faster R-CNN | R-50-FPN | pytorch | 6x | 40.1 | - | [model](https://open-mmlab.s3.ap-northeast-2.amazonaws.com/mmdetection/models/scratch/scratch_faster_rcnn_r50_fpn_gn_6x_20190515-ff554978.pth) | 19 | | Mask R-CNN | R-50-FPN | pytorch | 6x | 41.0 | 37.4 | [model](https://s3.ap-northeast-2.amazonaws.com/open-mmlab/mmdetection/models/scratch/scratch_mask_rcnn_r50_fpn_gn_6x_20190515-96743f5e.pth) | 20 | 21 | Note: 22 | - The above models are trained with 16 GPUs. 23 | -------------------------------------------------------------------------------- /configs/wider_face/README.md: -------------------------------------------------------------------------------- 1 | ## WIDER Face Dataset 2 | 3 | To use the WIDER Face dataset you need to download it 4 | and extract to the `data/WIDERFace` folder. Annotation in the VOC format 5 | can be found in this [repo](https://github.com/sovrasov/wider-face-pascal-voc-annotations.git). 6 | You should move the annotation files from `WIDER_train_annotations` and `WIDER_val_annotations` folders 7 | to the `Annotation` folders inside the corresponding directories `WIDER_train` and `WIDER_val`. 8 | Also annotation lists `val.txt` and `train.txt` should be copied to `data/WIDERFace` from `WIDER_train_annotations` and `WIDER_val_annotations`. 9 | The directory should be like this: 10 | 11 | ``` 12 | mmdetection 13 | ├── mmdet 14 | ├── tools 15 | ├── configs 16 | ├── data 17 | │ ├── WIDERFace 18 | │ │ ├── WIDER_train 19 | │ | │ ├──0--Parade 20 | │ | │ ├── ... 21 | │ | │ ├── Annotations 22 | │ │ ├── WIDER_val 23 | │ | │ ├──0--Parade 24 | │ | │ ├── ... 25 | │ | │ ├── Annotations 26 | │ │ ├── val.txt 27 | │ │ ├── train.txt 28 | 29 | ``` 30 | 31 | After that you can train the SSD300 on WIDER by launching training with the `ssd300_wider_face.py` config or 32 | create your own config based on the presented one. 33 | -------------------------------------------------------------------------------- /demo.py: -------------------------------------------------------------------------------- 1 | from mmdet.apis import init_detector, inference_detector, show_result 2 | import mmcv 3 | 4 | config_file = 'configs/faster_rcnn_r50_fpn_1x.py' 5 | checkpoint_file = 'checkpoints/faster_rcnn_r50_fpn_1x_20181010-3d1b3351.pth' 6 | 7 | # build the model from a config file and a checkpoint file 8 | model = init_detector(config_file, checkpoint_file, device='cuda:0') 9 | 10 | # test a single image and show the results 11 | img = 'test.jpg' # or img = mmcv.imread(img), which will only load it once 12 | result = inference_detector(model, img) 13 | # visualize the results in a new window 14 | # show_result(img, result, model.CLASSES) 15 | # or save the visualization results to image files 16 | show_result(img, result, model.CLASSES,score_thr=0.90,show=False,out_file='result.jpg') -------------------------------------------------------------------------------- /demo/coco_test_12510.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataXujing/Cascade_RCNN_mmdetection/da1cfb8e5b36ba63ae444b8d3c011623bc175970/demo/coco_test_12510.jpg -------------------------------------------------------------------------------- /demo/corruptions_sev_3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataXujing/Cascade_RCNN_mmdetection/da1cfb8e5b36ba63ae444b8d3c011623bc175970/demo/corruptions_sev_3.png -------------------------------------------------------------------------------- /demo/data_pipeline.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataXujing/Cascade_RCNN_mmdetection/da1cfb8e5b36ba63ae444b8d3c011623bc175970/demo/data_pipeline.png -------------------------------------------------------------------------------- /demo/demo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataXujing/Cascade_RCNN_mmdetection/da1cfb8e5b36ba63ae444b8d3c011623bc175970/demo/demo.jpg -------------------------------------------------------------------------------- /demo/loss_curve.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataXujing/Cascade_RCNN_mmdetection/da1cfb8e5b36ba63ae444b8d3c011623bc175970/demo/loss_curve.png -------------------------------------------------------------------------------- /demo/webcam_demo.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | import cv2 4 | import torch 5 | 6 | from mmdet.apis import inference_detector, init_detector, show_result 7 | 8 | 9 | def parse_args(): 10 | parser = argparse.ArgumentParser(description='MMDetection webcam demo') 11 | parser.add_argument('config', help='test config file path') 12 | parser.add_argument('checkpoint', help='checkpoint file') 13 | parser.add_argument('--device', type=int, default=0, help='CUDA device id') 14 | parser.add_argument( 15 | '--camera-id', type=int, default=0, help='camera device id') 16 | parser.add_argument( 17 | '--score-thr', type=float, default=0.5, help='bbox score threshold') 18 | args = parser.parse_args() 19 | return args 20 | 21 | 22 | def main(): 23 | args = parse_args() 24 | 25 | model = init_detector( 26 | args.config, args.checkpoint, device=torch.device('cuda', args.device)) 27 | 28 | camera = cv2.VideoCapture(args.camera_id) 29 | 30 | print('Press "Esc", "q" or "Q" to exit.') 31 | while True: 32 | ret_val, img = camera.read() 33 | result = inference_detector(model, img) 34 | 35 | ch = cv2.waitKey(1) 36 | if ch == 27 or ch == ord('q') or ch == ord('Q'): 37 | break 38 | 39 | show_result( 40 | img, result, model.CLASSES, score_thr=args.score_thr, wait_time=1) 41 | 42 | 43 | if __name__ == '__main__': 44 | main() 45 | -------------------------------------------------------------------------------- /mmdet/__init__.py: -------------------------------------------------------------------------------- 1 | from .version import __version__, short_version 2 | 3 | __all__ = ['__version__', 'short_version'] 4 | -------------------------------------------------------------------------------- /mmdet/apis/__init__.py: -------------------------------------------------------------------------------- 1 | from .inference import (async_inference_detector, inference_detector, 2 | init_detector, show_result, show_result_pyplot) 3 | from .test import multi_gpu_test, single_gpu_test 4 | from .train import get_root_logger, set_random_seed, train_detector 5 | 6 | __all__ = [ 7 | 'get_root_logger', 'set_random_seed', 'train_detector', 'init_detector', 8 | 'async_inference_detector', 'inference_detector', 'show_result', 9 | 'show_result_pyplot', 'multi_gpu_test', 'single_gpu_test' 10 | ] 11 | -------------------------------------------------------------------------------- /mmdet/core/__init__.py: -------------------------------------------------------------------------------- 1 | from .anchor import * # noqa: F401, F403 2 | from .bbox import * # noqa: F401, F403 3 | from .evaluation import * # noqa: F401, F403 4 | from .fp16 import * # noqa: F401, F403 5 | from .mask import * # noqa: F401, F403 6 | from .optimizer import * # noqa: F401, F403 7 | from .post_processing import * # noqa: F401, F403 8 | from .utils import * # noqa: F401, F403 9 | -------------------------------------------------------------------------------- /mmdet/core/anchor/__init__.py: -------------------------------------------------------------------------------- 1 | from .anchor_generator import AnchorGenerator 2 | from .anchor_target import (anchor_inside_flags, anchor_target, 3 | images_to_levels, unmap) 4 | from .guided_anchor_target import ga_loc_target, ga_shape_target 5 | from .point_generator import PointGenerator 6 | from .point_target import point_target 7 | 8 | __all__ = [ 9 | 'AnchorGenerator', 'anchor_target', 'anchor_inside_flags', 'ga_loc_target', 10 | 'ga_shape_target', 'PointGenerator', 'point_target', 'images_to_levels', 11 | 'unmap' 12 | ] 13 | -------------------------------------------------------------------------------- /mmdet/core/anchor/point_generator.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | 4 | class PointGenerator(object): 5 | 6 | def _meshgrid(self, x, y, row_major=True): 7 | xx = x.repeat(len(y)) 8 | yy = y.view(-1, 1).repeat(1, len(x)).view(-1) 9 | if row_major: 10 | return xx, yy 11 | else: 12 | return yy, xx 13 | 14 | def grid_points(self, featmap_size, stride=16, device='cuda'): 15 | feat_h, feat_w = featmap_size 16 | shift_x = torch.arange(0., feat_w, device=device) * stride 17 | shift_y = torch.arange(0., feat_h, device=device) * stride 18 | shift_xx, shift_yy = self._meshgrid(shift_x, shift_y) 19 | stride = shift_x.new_full((shift_xx.shape[0], ), stride) 20 | shifts = torch.stack([shift_xx, shift_yy, stride], dim=-1) 21 | all_points = shifts.to(device) 22 | return all_points 23 | 24 | def valid_flags(self, featmap_size, valid_size, device='cuda'): 25 | feat_h, feat_w = featmap_size 26 | valid_h, valid_w = valid_size 27 | assert valid_h <= feat_h and valid_w <= feat_w 28 | valid_x = torch.zeros(feat_w, dtype=torch.uint8, device=device) 29 | valid_y = torch.zeros(feat_h, dtype=torch.uint8, device=device) 30 | valid_x[:valid_w] = 1 31 | valid_y[:valid_h] = 1 32 | valid_xx, valid_yy = self._meshgrid(valid_x, valid_y) 33 | valid = valid_xx & valid_yy 34 | return valid 35 | -------------------------------------------------------------------------------- /mmdet/core/bbox/__init__.py: -------------------------------------------------------------------------------- 1 | from .assigners import AssignResult, BaseAssigner, MaxIoUAssigner 2 | from .bbox_target import bbox_target 3 | from .geometry import bbox_overlaps 4 | from .samplers import (BaseSampler, CombinedSampler, 5 | InstanceBalancedPosSampler, IoUBalancedNegSampler, 6 | PseudoSampler, RandomSampler, SamplingResult) 7 | from .transforms import (bbox2delta, bbox2result, bbox2roi, bbox_flip, 8 | bbox_mapping, bbox_mapping_back, delta2bbox, 9 | distance2bbox, roi2bbox) 10 | 11 | from .assign_sampling import ( # isort:skip, avoid recursive imports 12 | assign_and_sample, build_assigner, build_sampler) 13 | 14 | __all__ = [ 15 | 'bbox_overlaps', 'BaseAssigner', 'MaxIoUAssigner', 'AssignResult', 16 | 'BaseSampler', 'PseudoSampler', 'RandomSampler', 17 | 'InstanceBalancedPosSampler', 'IoUBalancedNegSampler', 'CombinedSampler', 18 | 'SamplingResult', 'build_assigner', 'build_sampler', 'assign_and_sample', 19 | 'bbox2delta', 'delta2bbox', 'bbox_flip', 'bbox_mapping', 20 | 'bbox_mapping_back', 'bbox2roi', 'roi2bbox', 'bbox2result', 21 | 'distance2bbox', 'bbox_target' 22 | ] 23 | -------------------------------------------------------------------------------- /mmdet/core/bbox/assign_sampling.py: -------------------------------------------------------------------------------- 1 | import mmcv 2 | 3 | from . import assigners, samplers 4 | 5 | 6 | def build_assigner(cfg, **kwargs): 7 | if isinstance(cfg, assigners.BaseAssigner): 8 | return cfg 9 | elif isinstance(cfg, dict): 10 | return mmcv.runner.obj_from_dict(cfg, assigners, default_args=kwargs) 11 | else: 12 | raise TypeError('Invalid type {} for building a sampler'.format( 13 | type(cfg))) 14 | 15 | 16 | def build_sampler(cfg, **kwargs): 17 | if isinstance(cfg, samplers.BaseSampler): 18 | return cfg 19 | elif isinstance(cfg, dict): 20 | return mmcv.runner.obj_from_dict(cfg, samplers, default_args=kwargs) 21 | else: 22 | raise TypeError('Invalid type {} for building a sampler'.format( 23 | type(cfg))) 24 | 25 | 26 | def assign_and_sample(bboxes, gt_bboxes, gt_bboxes_ignore, gt_labels, cfg): 27 | bbox_assigner = build_assigner(cfg.assigner) 28 | bbox_sampler = build_sampler(cfg.sampler) 29 | assign_result = bbox_assigner.assign(bboxes, gt_bboxes, gt_bboxes_ignore, 30 | gt_labels) 31 | sampling_result = bbox_sampler.sample(assign_result, bboxes, gt_bboxes, 32 | gt_labels) 33 | return assign_result, sampling_result 34 | -------------------------------------------------------------------------------- /mmdet/core/bbox/assigners/__init__.py: -------------------------------------------------------------------------------- 1 | from .approx_max_iou_assigner import ApproxMaxIoUAssigner 2 | from .assign_result import AssignResult 3 | from .atss_assigner import ATSSAssigner 4 | from .base_assigner import BaseAssigner 5 | from .max_iou_assigner import MaxIoUAssigner 6 | from .point_assigner import PointAssigner 7 | 8 | __all__ = [ 9 | 'BaseAssigner', 'MaxIoUAssigner', 'ApproxMaxIoUAssigner', 'AssignResult', 10 | 'PointAssigner', 'ATSSAssigner' 11 | ] 12 | -------------------------------------------------------------------------------- /mmdet/core/bbox/assigners/base_assigner.py: -------------------------------------------------------------------------------- 1 | from abc import ABCMeta, abstractmethod 2 | 3 | 4 | class BaseAssigner(metaclass=ABCMeta): 5 | 6 | @abstractmethod 7 | def assign(self, bboxes, gt_bboxes, gt_bboxes_ignore=None, gt_labels=None): 8 | pass 9 | -------------------------------------------------------------------------------- /mmdet/core/bbox/bbox_target.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | from ..utils import multi_apply 4 | from .transforms import bbox2delta 5 | 6 | 7 | def bbox_target(pos_bboxes_list, 8 | neg_bboxes_list, 9 | pos_gt_bboxes_list, 10 | pos_gt_labels_list, 11 | cfg, 12 | reg_classes=1, 13 | target_means=[.0, .0, .0, .0], 14 | target_stds=[1.0, 1.0, 1.0, 1.0], 15 | concat=True): 16 | labels, label_weights, bbox_targets, bbox_weights = multi_apply( 17 | bbox_target_single, 18 | pos_bboxes_list, 19 | neg_bboxes_list, 20 | pos_gt_bboxes_list, 21 | pos_gt_labels_list, 22 | cfg=cfg, 23 | reg_classes=reg_classes, 24 | target_means=target_means, 25 | target_stds=target_stds) 26 | 27 | if concat: 28 | labels = torch.cat(labels, 0) 29 | label_weights = torch.cat(label_weights, 0) 30 | bbox_targets = torch.cat(bbox_targets, 0) 31 | bbox_weights = torch.cat(bbox_weights, 0) 32 | return labels, label_weights, bbox_targets, bbox_weights 33 | 34 | 35 | def bbox_target_single(pos_bboxes, 36 | neg_bboxes, 37 | pos_gt_bboxes, 38 | pos_gt_labels, 39 | cfg, 40 | reg_classes=1, 41 | target_means=[.0, .0, .0, .0], 42 | target_stds=[1.0, 1.0, 1.0, 1.0]): 43 | num_pos = pos_bboxes.size(0) 44 | num_neg = neg_bboxes.size(0) 45 | num_samples = num_pos + num_neg 46 | labels = pos_bboxes.new_zeros(num_samples, dtype=torch.long) 47 | label_weights = pos_bboxes.new_zeros(num_samples) 48 | bbox_targets = pos_bboxes.new_zeros(num_samples, 4) 49 | bbox_weights = pos_bboxes.new_zeros(num_samples, 4) 50 | if num_pos > 0: 51 | labels[:num_pos] = pos_gt_labels 52 | pos_weight = 1.0 if cfg.pos_weight <= 0 else cfg.pos_weight 53 | label_weights[:num_pos] = pos_weight 54 | pos_bbox_targets = bbox2delta(pos_bboxes, pos_gt_bboxes, target_means, 55 | target_stds) 56 | bbox_targets[:num_pos, :] = pos_bbox_targets 57 | bbox_weights[:num_pos, :] = 1 58 | if num_neg > 0: 59 | label_weights[-num_neg:] = 1.0 60 | 61 | return labels, label_weights, bbox_targets, bbox_weights 62 | 63 | 64 | def expand_target(bbox_targets, bbox_weights, labels, num_classes): 65 | bbox_targets_expand = bbox_targets.new_zeros( 66 | (bbox_targets.size(0), 4 * num_classes)) 67 | bbox_weights_expand = bbox_weights.new_zeros( 68 | (bbox_weights.size(0), 4 * num_classes)) 69 | for i in torch.nonzero(labels > 0).squeeze(-1): 70 | start, end = labels[i] * 4, (labels[i] + 1) * 4 71 | bbox_targets_expand[i, start:end] = bbox_targets[i, :] 72 | bbox_weights_expand[i, start:end] = bbox_weights[i, :] 73 | return bbox_targets_expand, bbox_weights_expand 74 | -------------------------------------------------------------------------------- /mmdet/core/bbox/demodata.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | 4 | 5 | def ensure_rng(rng=None): 6 | """ 7 | Simple version of the ``kwarray.ensure_rng`` 8 | 9 | Args: 10 | rng (int | numpy.random.RandomState | None): 11 | if None, then defaults to the global rng. Otherwise this can be an 12 | integer or a RandomState class 13 | Returns: 14 | (numpy.random.RandomState) : rng - 15 | a numpy random number generator 16 | 17 | References: 18 | https://gitlab.kitware.com/computer-vision/kwarray/blob/master/kwarray/util_random.py#L270 19 | """ 20 | 21 | if rng is None: 22 | rng = np.random.mtrand._rand 23 | elif isinstance(rng, int): 24 | rng = np.random.RandomState(rng) 25 | else: 26 | rng = rng 27 | return rng 28 | 29 | 30 | def random_boxes(num=1, scale=1, rng=None): 31 | """ 32 | Simple version of ``kwimage.Boxes.random`` 33 | 34 | Returns: 35 | Tensor: shape (n, 4) in x1, y1, x2, y2 format. 36 | 37 | References: 38 | https://gitlab.kitware.com/computer-vision/kwimage/blob/master/kwimage/structs/boxes.py#L1390 39 | 40 | Example: 41 | >>> num = 3 42 | >>> scale = 512 43 | >>> rng = 0 44 | >>> boxes = random_boxes(num, scale, rng) 45 | >>> print(boxes) 46 | tensor([[280.9925, 278.9802, 308.6148, 366.1769], 47 | [216.9113, 330.6978, 224.0446, 456.5878], 48 | [405.3632, 196.3221, 493.3953, 270.7942]]) 49 | """ 50 | rng = ensure_rng(rng) 51 | 52 | tlbr = rng.rand(num, 4).astype(np.float32) 53 | 54 | tl_x = np.minimum(tlbr[:, 0], tlbr[:, 2]) 55 | tl_y = np.minimum(tlbr[:, 1], tlbr[:, 3]) 56 | br_x = np.maximum(tlbr[:, 0], tlbr[:, 2]) 57 | br_y = np.maximum(tlbr[:, 1], tlbr[:, 3]) 58 | 59 | tlbr[:, 0] = tl_x * scale 60 | tlbr[:, 1] = tl_y * scale 61 | tlbr[:, 2] = br_x * scale 62 | tlbr[:, 3] = br_y * scale 63 | 64 | boxes = torch.from_numpy(tlbr) 65 | return boxes 66 | -------------------------------------------------------------------------------- /mmdet/core/bbox/geometry.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | 4 | def bbox_overlaps(bboxes1, bboxes2, mode='iou', is_aligned=False): 5 | """Calculate overlap between two set of bboxes. 6 | 7 | If ``is_aligned`` is ``False``, then calculate the ious between each bbox 8 | of bboxes1 and bboxes2, otherwise the ious between each aligned pair of 9 | bboxes1 and bboxes2. 10 | 11 | Args: 12 | bboxes1 (Tensor): shape (m, 4) in format. 13 | bboxes2 (Tensor): shape (n, 4) in format. 14 | If is_aligned is ``True``, then m and n must be equal. 15 | mode (str): "iou" (intersection over union) or iof (intersection over 16 | foreground). 17 | 18 | Returns: 19 | ious(Tensor): shape (m, n) if is_aligned == False else shape (m, 1) 20 | 21 | Example: 22 | >>> bboxes1 = torch.FloatTensor([ 23 | >>> [0, 0, 10, 10], 24 | >>> [10, 10, 20, 20], 25 | >>> [32, 32, 38, 42], 26 | >>> ]) 27 | >>> bboxes2 = torch.FloatTensor([ 28 | >>> [0, 0, 10, 20], 29 | >>> [0, 10, 10, 19], 30 | >>> [10, 10, 20, 20], 31 | >>> ]) 32 | >>> bbox_overlaps(bboxes1, bboxes2) 33 | tensor([[0.5238, 0.0500, 0.0041], 34 | [0.0323, 0.0452, 1.0000], 35 | [0.0000, 0.0000, 0.0000]]) 36 | 37 | Example: 38 | >>> empty = torch.FloatTensor([]) 39 | >>> nonempty = torch.FloatTensor([ 40 | >>> [0, 0, 10, 9], 41 | >>> ]) 42 | >>> assert tuple(bbox_overlaps(empty, nonempty).shape) == (0, 1) 43 | >>> assert tuple(bbox_overlaps(nonempty, empty).shape) == (1, 0) 44 | >>> assert tuple(bbox_overlaps(empty, empty).shape) == (0, 0) 45 | """ 46 | 47 | assert mode in ['iou', 'iof'] 48 | 49 | rows = bboxes1.size(0) 50 | cols = bboxes2.size(0) 51 | if is_aligned: 52 | assert rows == cols 53 | 54 | if rows * cols == 0: 55 | return bboxes1.new(rows, 1) if is_aligned else bboxes1.new(rows, cols) 56 | 57 | if is_aligned: 58 | lt = torch.max(bboxes1[:, :2], bboxes2[:, :2]) # [rows, 2] 59 | rb = torch.min(bboxes1[:, 2:], bboxes2[:, 2:]) # [rows, 2] 60 | 61 | wh = (rb - lt + 1).clamp(min=0) # [rows, 2] 62 | overlap = wh[:, 0] * wh[:, 1] 63 | area1 = (bboxes1[:, 2] - bboxes1[:, 0] + 1) * ( 64 | bboxes1[:, 3] - bboxes1[:, 1] + 1) 65 | 66 | if mode == 'iou': 67 | area2 = (bboxes2[:, 2] - bboxes2[:, 0] + 1) * ( 68 | bboxes2[:, 3] - bboxes2[:, 1] + 1) 69 | ious = overlap / (area1 + area2 - overlap) 70 | else: 71 | ious = overlap / area1 72 | else: 73 | lt = torch.max(bboxes1[:, None, :2], bboxes2[:, :2]) # [rows, cols, 2] 74 | rb = torch.min(bboxes1[:, None, 2:], bboxes2[:, 2:]) # [rows, cols, 2] 75 | 76 | wh = (rb - lt + 1).clamp(min=0) # [rows, cols, 2] 77 | overlap = wh[:, :, 0] * wh[:, :, 1] 78 | area1 = (bboxes1[:, 2] - bboxes1[:, 0] + 1) * ( 79 | bboxes1[:, 3] - bboxes1[:, 1] + 1) 80 | 81 | if mode == 'iou': 82 | area2 = (bboxes2[:, 2] - bboxes2[:, 0] + 1) * ( 83 | bboxes2[:, 3] - bboxes2[:, 1] + 1) 84 | ious = overlap / (area1[:, None] + area2 - overlap) 85 | else: 86 | ious = overlap / (area1[:, None]) 87 | 88 | return ious 89 | -------------------------------------------------------------------------------- /mmdet/core/bbox/samplers/__init__.py: -------------------------------------------------------------------------------- 1 | from .base_sampler import BaseSampler 2 | from .combined_sampler import CombinedSampler 3 | from .instance_balanced_pos_sampler import InstanceBalancedPosSampler 4 | from .iou_balanced_neg_sampler import IoUBalancedNegSampler 5 | from .ohem_sampler import OHEMSampler 6 | from .pseudo_sampler import PseudoSampler 7 | from .random_sampler import RandomSampler 8 | from .sampling_result import SamplingResult 9 | 10 | __all__ = [ 11 | 'BaseSampler', 'PseudoSampler', 'RandomSampler', 12 | 'InstanceBalancedPosSampler', 'IoUBalancedNegSampler', 'CombinedSampler', 13 | 'OHEMSampler', 'SamplingResult' 14 | ] 15 | -------------------------------------------------------------------------------- /mmdet/core/bbox/samplers/combined_sampler.py: -------------------------------------------------------------------------------- 1 | from ..assign_sampling import build_sampler 2 | from .base_sampler import BaseSampler 3 | 4 | 5 | class CombinedSampler(BaseSampler): 6 | 7 | def __init__(self, pos_sampler, neg_sampler, **kwargs): 8 | super(CombinedSampler, self).__init__(**kwargs) 9 | self.pos_sampler = build_sampler(pos_sampler, **kwargs) 10 | self.neg_sampler = build_sampler(neg_sampler, **kwargs) 11 | 12 | def _sample_pos(self, **kwargs): 13 | raise NotImplementedError 14 | 15 | def _sample_neg(self, **kwargs): 16 | raise NotImplementedError 17 | -------------------------------------------------------------------------------- /mmdet/core/bbox/samplers/instance_balanced_pos_sampler.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | 4 | from .random_sampler import RandomSampler 5 | 6 | 7 | class InstanceBalancedPosSampler(RandomSampler): 8 | 9 | def _sample_pos(self, assign_result, num_expected, **kwargs): 10 | pos_inds = torch.nonzero(assign_result.gt_inds > 0) 11 | if pos_inds.numel() != 0: 12 | pos_inds = pos_inds.squeeze(1) 13 | if pos_inds.numel() <= num_expected: 14 | return pos_inds 15 | else: 16 | unique_gt_inds = assign_result.gt_inds[pos_inds].unique() 17 | num_gts = len(unique_gt_inds) 18 | num_per_gt = int(round(num_expected / float(num_gts)) + 1) 19 | sampled_inds = [] 20 | for i in unique_gt_inds: 21 | inds = torch.nonzero(assign_result.gt_inds == i.item()) 22 | if inds.numel() != 0: 23 | inds = inds.squeeze(1) 24 | else: 25 | continue 26 | if len(inds) > num_per_gt: 27 | inds = self.random_choice(inds, num_per_gt) 28 | sampled_inds.append(inds) 29 | sampled_inds = torch.cat(sampled_inds) 30 | if len(sampled_inds) < num_expected: 31 | num_extra = num_expected - len(sampled_inds) 32 | extra_inds = np.array( 33 | list(set(pos_inds.cpu()) - set(sampled_inds.cpu()))) 34 | if len(extra_inds) > num_extra: 35 | extra_inds = self.random_choice(extra_inds, num_extra) 36 | extra_inds = torch.from_numpy(extra_inds).to( 37 | assign_result.gt_inds.device).long() 38 | sampled_inds = torch.cat([sampled_inds, extra_inds]) 39 | elif len(sampled_inds) > num_expected: 40 | sampled_inds = self.random_choice(sampled_inds, num_expected) 41 | return sampled_inds 42 | -------------------------------------------------------------------------------- /mmdet/core/bbox/samplers/ohem_sampler.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | from ..transforms import bbox2roi 4 | from .base_sampler import BaseSampler 5 | 6 | 7 | class OHEMSampler(BaseSampler): 8 | """ 9 | Online Hard Example Mining Sampler described in [1]_. 10 | 11 | References: 12 | .. [1] https://arxiv.org/pdf/1604.03540.pdf 13 | """ 14 | 15 | def __init__(self, 16 | num, 17 | pos_fraction, 18 | context, 19 | neg_pos_ub=-1, 20 | add_gt_as_proposals=True, 21 | **kwargs): 22 | super(OHEMSampler, self).__init__(num, pos_fraction, neg_pos_ub, 23 | add_gt_as_proposals) 24 | if not hasattr(context, 'num_stages'): 25 | self.bbox_roi_extractor = context.bbox_roi_extractor 26 | self.bbox_head = context.bbox_head 27 | else: 28 | self.bbox_roi_extractor = context.bbox_roi_extractor[ 29 | context.current_stage] 30 | self.bbox_head = context.bbox_head[context.current_stage] 31 | 32 | def hard_mining(self, inds, num_expected, bboxes, labels, feats): 33 | with torch.no_grad(): 34 | rois = bbox2roi([bboxes]) 35 | bbox_feats = self.bbox_roi_extractor( 36 | feats[:self.bbox_roi_extractor.num_inputs], rois) 37 | cls_score, _ = self.bbox_head(bbox_feats) 38 | loss = self.bbox_head.loss( 39 | cls_score=cls_score, 40 | bbox_pred=None, 41 | labels=labels, 42 | label_weights=cls_score.new_ones(cls_score.size(0)), 43 | bbox_targets=None, 44 | bbox_weights=None, 45 | reduction_override='none')['loss_cls'] 46 | _, topk_loss_inds = loss.topk(num_expected) 47 | return inds[topk_loss_inds] 48 | 49 | def _sample_pos(self, 50 | assign_result, 51 | num_expected, 52 | bboxes=None, 53 | feats=None, 54 | **kwargs): 55 | # Sample some hard positive samples 56 | pos_inds = torch.nonzero(assign_result.gt_inds > 0) 57 | if pos_inds.numel() != 0: 58 | pos_inds = pos_inds.squeeze(1) 59 | if pos_inds.numel() <= num_expected: 60 | return pos_inds 61 | else: 62 | return self.hard_mining(pos_inds, num_expected, bboxes[pos_inds], 63 | assign_result.labels[pos_inds], feats) 64 | 65 | def _sample_neg(self, 66 | assign_result, 67 | num_expected, 68 | bboxes=None, 69 | feats=None, 70 | **kwargs): 71 | # Sample some hard negative samples 72 | neg_inds = torch.nonzero(assign_result.gt_inds == 0) 73 | if neg_inds.numel() != 0: 74 | neg_inds = neg_inds.squeeze(1) 75 | if len(neg_inds) <= num_expected: 76 | return neg_inds 77 | else: 78 | return self.hard_mining(neg_inds, num_expected, bboxes[neg_inds], 79 | assign_result.labels[neg_inds], feats) 80 | -------------------------------------------------------------------------------- /mmdet/core/bbox/samplers/pseudo_sampler.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | from .base_sampler import BaseSampler 4 | from .sampling_result import SamplingResult 5 | 6 | 7 | class PseudoSampler(BaseSampler): 8 | 9 | def __init__(self, **kwargs): 10 | pass 11 | 12 | def _sample_pos(self, **kwargs): 13 | raise NotImplementedError 14 | 15 | def _sample_neg(self, **kwargs): 16 | raise NotImplementedError 17 | 18 | def sample(self, assign_result, bboxes, gt_bboxes, **kwargs): 19 | pos_inds = torch.nonzero( 20 | assign_result.gt_inds > 0).squeeze(-1).unique() 21 | neg_inds = torch.nonzero( 22 | assign_result.gt_inds == 0).squeeze(-1).unique() 23 | gt_flags = bboxes.new_zeros(bboxes.shape[0], dtype=torch.uint8) 24 | sampling_result = SamplingResult(pos_inds, neg_inds, bboxes, gt_bboxes, 25 | assign_result, gt_flags) 26 | return sampling_result 27 | -------------------------------------------------------------------------------- /mmdet/core/bbox/samplers/random_sampler.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | from .base_sampler import BaseSampler 4 | 5 | 6 | class RandomSampler(BaseSampler): 7 | 8 | def __init__(self, 9 | num, 10 | pos_fraction, 11 | neg_pos_ub=-1, 12 | add_gt_as_proposals=True, 13 | **kwargs): 14 | from mmdet.core.bbox import demodata 15 | super(RandomSampler, self).__init__(num, pos_fraction, neg_pos_ub, 16 | add_gt_as_proposals) 17 | self.rng = demodata.ensure_rng(kwargs.get('rng', None)) 18 | 19 | def random_choice(self, gallery, num): 20 | """Random select some elements from the gallery. 21 | 22 | If `gallery` is a Tensor, the returned indices will be a Tensor; 23 | If `gallery` is a ndarray or list, the returned indices will be a 24 | ndarray. 25 | 26 | Args: 27 | gallery (Tensor | ndarray | list): indices pool. 28 | num (int): expected sample num. 29 | 30 | Returns: 31 | Tensor or ndarray: sampled indices. 32 | """ 33 | assert len(gallery) >= num 34 | 35 | is_tensor = isinstance(gallery, torch.Tensor) 36 | if not is_tensor: 37 | gallery = torch.tensor( 38 | gallery, dtype=torch.long, device=torch.cuda.current_device()) 39 | perm = torch.randperm(gallery.numel(), device=gallery.device)[:num] 40 | rand_inds = gallery[perm] 41 | if not is_tensor: 42 | rand_inds = rand_inds.cpu().numpy() 43 | return rand_inds 44 | 45 | def _sample_pos(self, assign_result, num_expected, **kwargs): 46 | """Randomly sample some positive samples.""" 47 | pos_inds = torch.nonzero(assign_result.gt_inds > 0) 48 | if pos_inds.numel() != 0: 49 | pos_inds = pos_inds.squeeze(1) 50 | if pos_inds.numel() <= num_expected: 51 | return pos_inds 52 | else: 53 | return self.random_choice(pos_inds, num_expected) 54 | 55 | def _sample_neg(self, assign_result, num_expected, **kwargs): 56 | """Randomly sample some negative samples.""" 57 | neg_inds = torch.nonzero(assign_result.gt_inds == 0) 58 | if neg_inds.numel() != 0: 59 | neg_inds = neg_inds.squeeze(1) 60 | if len(neg_inds) <= num_expected: 61 | return neg_inds 62 | else: 63 | return self.random_choice(neg_inds, num_expected) 64 | -------------------------------------------------------------------------------- /mmdet/core/evaluation/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | from .class_names import (cityscapes_classes, coco_classes, dataset_aliases, 3 | get_classes, imagenet_det_classes, 4 | imagenet_vid_classes, voc_classes,bingzao_classes) 5 | from .eval_hooks import DistEvalHook 6 | from .mean_ap import average_precision, eval_map, print_map_summary 7 | from .recall import (eval_recalls, plot_iou_recall, plot_num_recall, 8 | print_recall_summary) 9 | 10 | __all__ = [ 11 | 'voc_classes', 'imagenet_det_classes', 'imagenet_vid_classes', 12 | 'coco_classes', 'cityscapes_classes', 'dataset_aliases', 'get_classes', 13 | 'DistEvalHook', 'average_precision', 'eval_map', 'print_map_summary', 14 | 'eval_recalls', 'print_recall_summary', 'plot_num_recall', 15 | 'plot_iou_recall',"bingzao_classes" 16 | ] -------------------------------------------------------------------------------- /mmdet/core/evaluation/bbox_overlaps.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def bbox_overlaps(bboxes1, bboxes2, mode='iou'): 5 | """Calculate the ious between each bbox of bboxes1 and bboxes2. 6 | 7 | Args: 8 | bboxes1(ndarray): shape (n, 4) 9 | bboxes2(ndarray): shape (k, 4) 10 | mode(str): iou (intersection over union) or iof (intersection 11 | over foreground) 12 | 13 | Returns: 14 | ious(ndarray): shape (n, k) 15 | """ 16 | 17 | assert mode in ['iou', 'iof'] 18 | 19 | bboxes1 = bboxes1.astype(np.float32) 20 | bboxes2 = bboxes2.astype(np.float32) 21 | rows = bboxes1.shape[0] 22 | cols = bboxes2.shape[0] 23 | ious = np.zeros((rows, cols), dtype=np.float32) 24 | if rows * cols == 0: 25 | return ious 26 | exchange = False 27 | if bboxes1.shape[0] > bboxes2.shape[0]: 28 | bboxes1, bboxes2 = bboxes2, bboxes1 29 | ious = np.zeros((cols, rows), dtype=np.float32) 30 | exchange = True 31 | area1 = (bboxes1[:, 2] - bboxes1[:, 0] + 1) * ( 32 | bboxes1[:, 3] - bboxes1[:, 1] + 1) 33 | area2 = (bboxes2[:, 2] - bboxes2[:, 0] + 1) * ( 34 | bboxes2[:, 3] - bboxes2[:, 1] + 1) 35 | for i in range(bboxes1.shape[0]): 36 | x_start = np.maximum(bboxes1[i, 0], bboxes2[:, 0]) 37 | y_start = np.maximum(bboxes1[i, 1], bboxes2[:, 1]) 38 | x_end = np.minimum(bboxes1[i, 2], bboxes2[:, 2]) 39 | y_end = np.minimum(bboxes1[i, 3], bboxes2[:, 3]) 40 | overlap = np.maximum(x_end - x_start + 1, 0) * np.maximum( 41 | y_end - y_start + 1, 0) 42 | if mode == 'iou': 43 | union = area1[i] + area2 - overlap 44 | else: 45 | union = area1[i] if not exchange else area2 46 | ious[i, :] = overlap / union 47 | if exchange: 48 | ious = ious.T 49 | return ious 50 | -------------------------------------------------------------------------------- /mmdet/core/evaluation/eval_hooks.py: -------------------------------------------------------------------------------- 1 | import os.path as osp 2 | 3 | from mmcv.runner import Hook 4 | from torch.utils.data import DataLoader 5 | 6 | 7 | class DistEvalHook(Hook): 8 | """Distributed evaluation hook. 9 | 10 | Attributes: 11 | dataloader (DataLoader): A PyTorch dataloader. 12 | interval (int): Evaluation interval (by epochs). Default: 1. 13 | tmpdir (str | None): Temporary directory to save the results of all 14 | processes. Default: None. 15 | gpu_collect (bool): Whether to use gpu or cpu to collect results. 16 | Default: False. 17 | """ 18 | 19 | def __init__(self, 20 | dataloader, 21 | interval=1, 22 | gpu_collect=False, 23 | **eval_kwargs): 24 | if not isinstance(dataloader, DataLoader): 25 | raise TypeError( 26 | 'dataloader must be a pytorch DataLoader, but got {}'.format( 27 | type(dataloader))) 28 | self.dataloader = dataloader 29 | self.interval = interval 30 | self.gpu_collect = gpu_collect 31 | self.eval_kwargs = eval_kwargs 32 | 33 | def after_train_epoch(self, runner): 34 | if not self.every_n_epochs(runner, self.interval): 35 | return 36 | from mmdet.apis import multi_gpu_test 37 | results = multi_gpu_test( 38 | runner.model, 39 | self.dataloader, 40 | tmpdir=osp.join(runner.work_dir, '.eval_hook'), 41 | gpu_collect=self.gpu_collect) 42 | if runner.rank == 0: 43 | print('\n') 44 | self.evaluate(runner, results) 45 | 46 | def evaluate(self, runner, results): 47 | eval_res = self.dataloader.dataset.evaluate( 48 | results, logger=runner.logger, **self.eval_kwargs) 49 | for name, val in eval_res.items(): 50 | runner.log_buffer.output[name] = val 51 | runner.log_buffer.ready = True 52 | -------------------------------------------------------------------------------- /mmdet/core/fp16/__init__.py: -------------------------------------------------------------------------------- 1 | from .decorators import auto_fp16, force_fp32 2 | from .hooks import Fp16OptimizerHook, wrap_fp16_model 3 | 4 | __all__ = ['auto_fp16', 'force_fp32', 'Fp16OptimizerHook', 'wrap_fp16_model'] 5 | -------------------------------------------------------------------------------- /mmdet/core/fp16/utils.py: -------------------------------------------------------------------------------- 1 | from collections import abc 2 | 3 | import numpy as np 4 | import torch 5 | 6 | 7 | def cast_tensor_type(inputs, src_type, dst_type): 8 | if isinstance(inputs, torch.Tensor): 9 | return inputs.to(dst_type) 10 | elif isinstance(inputs, str): 11 | return inputs 12 | elif isinstance(inputs, np.ndarray): 13 | return inputs 14 | elif isinstance(inputs, abc.Mapping): 15 | return type(inputs)({ 16 | k: cast_tensor_type(v, src_type, dst_type) 17 | for k, v in inputs.items() 18 | }) 19 | elif isinstance(inputs, abc.Iterable): 20 | return type(inputs)( 21 | cast_tensor_type(item, src_type, dst_type) for item in inputs) 22 | else: 23 | return inputs 24 | -------------------------------------------------------------------------------- /mmdet/core/mask/__init__.py: -------------------------------------------------------------------------------- 1 | from .mask_target import mask_target 2 | from .utils import split_combined_polys 3 | 4 | __all__ = ['split_combined_polys', 'mask_target'] 5 | -------------------------------------------------------------------------------- /mmdet/core/mask/mask_target.py: -------------------------------------------------------------------------------- 1 | import mmcv 2 | import numpy as np 3 | import torch 4 | from torch.nn.modules.utils import _pair 5 | 6 | 7 | def mask_target(pos_proposals_list, pos_assigned_gt_inds_list, gt_masks_list, 8 | cfg): 9 | cfg_list = [cfg for _ in range(len(pos_proposals_list))] 10 | mask_targets = map(mask_target_single, pos_proposals_list, 11 | pos_assigned_gt_inds_list, gt_masks_list, cfg_list) 12 | mask_targets = torch.cat(list(mask_targets)) 13 | return mask_targets 14 | 15 | 16 | def mask_target_single(pos_proposals, pos_assigned_gt_inds, gt_masks, cfg): 17 | mask_size = _pair(cfg.mask_size) 18 | num_pos = pos_proposals.size(0) 19 | mask_targets = [] 20 | if num_pos > 0: 21 | proposals_np = pos_proposals.cpu().numpy() 22 | _, maxh, maxw = gt_masks.shape 23 | proposals_np[:, [0, 2]] = np.clip(proposals_np[:, [0, 2]], 0, maxw - 1) 24 | proposals_np[:, [1, 3]] = np.clip(proposals_np[:, [1, 3]], 0, maxh - 1) 25 | pos_assigned_gt_inds = pos_assigned_gt_inds.cpu().numpy() 26 | for i in range(num_pos): 27 | gt_mask = gt_masks[pos_assigned_gt_inds[i]] 28 | bbox = proposals_np[i, :].astype(np.int32) 29 | x1, y1, x2, y2 = bbox 30 | w = np.maximum(x2 - x1 + 1, 1) 31 | h = np.maximum(y2 - y1 + 1, 1) 32 | # mask is uint8 both before and after resizing 33 | # mask_size (h, w) to (w, h) 34 | target = mmcv.imresize(gt_mask[y1:y1 + h, x1:x1 + w], 35 | mask_size[::-1]) 36 | mask_targets.append(target) 37 | mask_targets = torch.from_numpy(np.stack(mask_targets)).float().to( 38 | pos_proposals.device) 39 | else: 40 | mask_targets = pos_proposals.new_zeros((0, ) + mask_size) 41 | return mask_targets 42 | -------------------------------------------------------------------------------- /mmdet/core/mask/utils.py: -------------------------------------------------------------------------------- 1 | import mmcv 2 | 3 | 4 | def split_combined_polys(polys, poly_lens, polys_per_mask): 5 | """Split the combined 1-D polys into masks. 6 | 7 | A mask is represented as a list of polys, and a poly is represented as 8 | a 1-D array. In dataset, all masks are concatenated into a single 1-D 9 | tensor. Here we need to split the tensor into original representations. 10 | 11 | Args: 12 | polys (list): a list (length = image num) of 1-D tensors 13 | poly_lens (list): a list (length = image num) of poly length 14 | polys_per_mask (list): a list (length = image num) of poly number 15 | of each mask 16 | 17 | Returns: 18 | list: a list (length = image num) of list (length = mask num) of 19 | list (length = poly num) of numpy array 20 | """ 21 | mask_polys_list = [] 22 | for img_id in range(len(polys)): 23 | polys_single = polys[img_id] 24 | polys_lens_single = poly_lens[img_id].tolist() 25 | polys_per_mask_single = polys_per_mask[img_id].tolist() 26 | 27 | split_polys = mmcv.slice_list(polys_single, polys_lens_single) 28 | mask_polys = mmcv.slice_list(split_polys, polys_per_mask_single) 29 | mask_polys_list.append(mask_polys) 30 | return mask_polys_list 31 | -------------------------------------------------------------------------------- /mmdet/core/optimizer/__init__.py: -------------------------------------------------------------------------------- 1 | from .builder import build_optimizer 2 | from .copy_of_sgd import CopyOfSGD 3 | from .registry import OPTIMIZERS 4 | 5 | __all__ = ['OPTIMIZERS', 'build_optimizer', 'CopyOfSGD'] 6 | -------------------------------------------------------------------------------- /mmdet/core/optimizer/copy_of_sgd.py: -------------------------------------------------------------------------------- 1 | from torch.optim import SGD 2 | 3 | from .registry import OPTIMIZERS 4 | 5 | 6 | @OPTIMIZERS.register_module 7 | class CopyOfSGD(SGD): 8 | """A clone of torch.optim.SGD. 9 | 10 | A customized optimizer could be defined like CopyOfSGD. 11 | You may derive from built-in optimizers in torch.optim, 12 | or directly implement a new optimizer. 13 | """ 14 | -------------------------------------------------------------------------------- /mmdet/core/optimizer/registry.py: -------------------------------------------------------------------------------- 1 | import inspect 2 | 3 | import torch 4 | 5 | from mmdet.utils import Registry 6 | 7 | OPTIMIZERS = Registry('optimizer') 8 | 9 | 10 | def register_torch_optimizers(): 11 | torch_optimizers = [] 12 | for module_name in dir(torch.optim): 13 | if module_name.startswith('__'): 14 | continue 15 | _optim = getattr(torch.optim, module_name) 16 | if inspect.isclass(_optim) and issubclass(_optim, 17 | torch.optim.Optimizer): 18 | OPTIMIZERS.register_module(_optim) 19 | torch_optimizers.append(module_name) 20 | return torch_optimizers 21 | 22 | 23 | TORCH_OPTIMIZERS = register_torch_optimizers() 24 | -------------------------------------------------------------------------------- /mmdet/core/post_processing/__init__.py: -------------------------------------------------------------------------------- 1 | from .bbox_nms import multiclass_nms 2 | from .merge_augs import (merge_aug_bboxes, merge_aug_masks, 3 | merge_aug_proposals, merge_aug_scores) 4 | 5 | __all__ = [ 6 | 'multiclass_nms', 'merge_aug_proposals', 'merge_aug_bboxes', 7 | 'merge_aug_scores', 'merge_aug_masks' 8 | ] 9 | -------------------------------------------------------------------------------- /mmdet/core/post_processing/bbox_nms.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | from mmdet.ops.nms import nms_wrapper 4 | 5 | 6 | def multiclass_nms(multi_bboxes, 7 | multi_scores, 8 | score_thr, 9 | nms_cfg, 10 | max_num=-1, 11 | score_factors=None): 12 | """NMS for multi-class bboxes. 13 | 14 | Args: 15 | multi_bboxes (Tensor): shape (n, #class*4) or (n, 4) 16 | multi_scores (Tensor): shape (n, #class), where the 0th column 17 | contains scores of the background class, but this will be ignored. 18 | score_thr (float): bbox threshold, bboxes with scores lower than it 19 | will not be considered. 20 | nms_thr (float): NMS IoU threshold 21 | max_num (int): if there are more than max_num bboxes after NMS, 22 | only top max_num will be kept. 23 | score_factors (Tensor): The factors multiplied to scores before 24 | applying NMS 25 | 26 | Returns: 27 | tuple: (bboxes, labels), tensors of shape (k, 5) and (k, 1). Labels 28 | are 0-based. 29 | """ 30 | num_classes = multi_scores.size(1) - 1 31 | # exclude background category 32 | if multi_bboxes.shape[1] > 4: 33 | bboxes = multi_bboxes.view(multi_scores.size(0), -1, 4)[:, 1:] 34 | else: 35 | bboxes = multi_bboxes[:, None].expand(-1, num_classes, 4) 36 | scores = multi_scores[:, 1:] 37 | 38 | # filter out boxes with low scores 39 | valid_mask = scores > score_thr 40 | bboxes = bboxes[valid_mask] 41 | if score_factors is not None: 42 | scores = scores * score_factors[:, None] 43 | scores = scores[valid_mask] 44 | labels = valid_mask.nonzero()[:, 1] 45 | 46 | if bboxes.numel() == 0: 47 | bboxes = multi_bboxes.new_zeros((0, 5)) 48 | labels = multi_bboxes.new_zeros((0, ), dtype=torch.long) 49 | return bboxes, labels 50 | 51 | # Modified from https://github.com/pytorch/vision/blob 52 | # /505cd6957711af790211896d32b40291bea1bc21/torchvision/ops/boxes.py#L39. 53 | # strategy: in order to perform NMS independently per class. 54 | # we add an offset to all the boxes. The offset is dependent 55 | # only on the class idx, and is large enough so that boxes 56 | # from different classes do not overlap 57 | max_coordinate = bboxes.max() 58 | offsets = labels.to(bboxes) * (max_coordinate + 1) 59 | bboxes_for_nms = bboxes + offsets[:, None] 60 | nms_cfg_ = nms_cfg.copy() 61 | nms_type = nms_cfg_.pop('type', 'nms') 62 | nms_op = getattr(nms_wrapper, nms_type) 63 | dets, keep = nms_op( 64 | torch.cat([bboxes_for_nms, scores[:, None]], 1), **nms_cfg_) 65 | bboxes = bboxes[keep] 66 | scores = dets[:, -1] # soft_nms will modify scores 67 | labels = labels[keep] 68 | 69 | if keep.size(0) > max_num: 70 | _, inds = scores.sort(descending=True) 71 | inds = inds[:max_num] 72 | bboxes = bboxes[inds] 73 | scores = scores[inds] 74 | labels = labels[inds] 75 | 76 | return torch.cat([bboxes, scores[:, None]], 1), labels 77 | -------------------------------------------------------------------------------- /mmdet/core/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from .dist_utils import DistOptimizerHook, allreduce_grads 2 | from .misc import multi_apply, tensor2imgs, unmap 3 | 4 | __all__ = [ 5 | 'allreduce_grads', 'DistOptimizerHook', 'tensor2imgs', 'unmap', 6 | 'multi_apply' 7 | ] 8 | -------------------------------------------------------------------------------- /mmdet/core/utils/dist_utils.py: -------------------------------------------------------------------------------- 1 | from collections import OrderedDict 2 | 3 | import torch.distributed as dist 4 | from mmcv.runner import OptimizerHook 5 | from torch._utils import (_flatten_dense_tensors, _take_tensors, 6 | _unflatten_dense_tensors) 7 | 8 | 9 | def _allreduce_coalesced(tensors, world_size, bucket_size_mb=-1): 10 | if bucket_size_mb > 0: 11 | bucket_size_bytes = bucket_size_mb * 1024 * 1024 12 | buckets = _take_tensors(tensors, bucket_size_bytes) 13 | else: 14 | buckets = OrderedDict() 15 | for tensor in tensors: 16 | tp = tensor.type() 17 | if tp not in buckets: 18 | buckets[tp] = [] 19 | buckets[tp].append(tensor) 20 | buckets = buckets.values() 21 | 22 | for bucket in buckets: 23 | flat_tensors = _flatten_dense_tensors(bucket) 24 | dist.all_reduce(flat_tensors) 25 | flat_tensors.div_(world_size) 26 | for tensor, synced in zip( 27 | bucket, _unflatten_dense_tensors(flat_tensors, bucket)): 28 | tensor.copy_(synced) 29 | 30 | 31 | def allreduce_grads(params, coalesce=True, bucket_size_mb=-1): 32 | grads = [ 33 | param.grad.data for param in params 34 | if param.requires_grad and param.grad is not None 35 | ] 36 | world_size = dist.get_world_size() 37 | if coalesce: 38 | _allreduce_coalesced(grads, world_size, bucket_size_mb) 39 | else: 40 | for tensor in grads: 41 | dist.all_reduce(tensor.div_(world_size)) 42 | 43 | 44 | class DistOptimizerHook(OptimizerHook): 45 | 46 | def __init__(self, grad_clip=None, coalesce=True, bucket_size_mb=-1): 47 | self.grad_clip = grad_clip 48 | self.coalesce = coalesce 49 | self.bucket_size_mb = bucket_size_mb 50 | 51 | def after_train_iter(self, runner): 52 | runner.optimizer.zero_grad() 53 | runner.outputs['loss'].backward() 54 | if self.grad_clip is not None: 55 | self.clip_grads(runner.model.parameters()) 56 | runner.optimizer.step() 57 | -------------------------------------------------------------------------------- /mmdet/core/utils/misc.py: -------------------------------------------------------------------------------- 1 | from functools import partial 2 | 3 | import mmcv 4 | import numpy as np 5 | from six.moves import map, zip 6 | 7 | 8 | def tensor2imgs(tensor, mean=(0, 0, 0), std=(1, 1, 1), to_rgb=True): 9 | num_imgs = tensor.size(0) 10 | mean = np.array(mean, dtype=np.float32) 11 | std = np.array(std, dtype=np.float32) 12 | imgs = [] 13 | for img_id in range(num_imgs): 14 | img = tensor[img_id, ...].cpu().numpy().transpose(1, 2, 0) 15 | img = mmcv.imdenormalize( 16 | img, mean, std, to_bgr=to_rgb).astype(np.uint8) 17 | imgs.append(np.ascontiguousarray(img)) 18 | return imgs 19 | 20 | 21 | def multi_apply(func, *args, **kwargs): 22 | pfunc = partial(func, **kwargs) if kwargs else func 23 | map_results = map(pfunc, *args) 24 | return tuple(map(list, zip(*map_results))) 25 | 26 | 27 | def unmap(data, count, inds, fill=0): 28 | """ Unmap a subset of item (data) back to the original set of items (of 29 | size count) """ 30 | if data.dim() == 1: 31 | ret = data.new_full((count, ), fill) 32 | ret[inds] = data 33 | else: 34 | new_size = (count, ) + data.size()[1:] 35 | ret = data.new_full(new_size, fill) 36 | ret[inds, :] = data 37 | return ret 38 | -------------------------------------------------------------------------------- /mmdet/datasets/__init__.py: -------------------------------------------------------------------------------- 1 | from .builder import build_dataset 2 | from .cityscapes import CityscapesDataset 3 | from .coco import CocoDataset 4 | from .custom import CustomDataset 5 | from .dataset_wrappers import ConcatDataset, RepeatDataset 6 | from .loader import DistributedGroupSampler, GroupSampler, build_dataloader 7 | from .registry import DATASETS 8 | from .voc import VOCDataset 9 | from .wider_face import WIDERFaceDataset 10 | from .xml_style import XMLDataset 11 | from .bingzao import bingzao 12 | 13 | __all__ = [ 14 | 'CustomDataset', 'XMLDataset', 'CocoDataset', 'VOCDataset', 15 | 'CityscapesDataset', 'GroupSampler', 'DistributedGroupSampler', 16 | 'build_dataloader', 'ConcatDataset', 'RepeatDataset', 'WIDERFaceDataset', 17 | 'DATASETS', 'build_dataset',"bingzao" 18 | ] 19 | -------------------------------------------------------------------------------- /mmdet/datasets/builder.py: -------------------------------------------------------------------------------- 1 | import copy 2 | 3 | from mmdet.utils import build_from_cfg 4 | from .dataset_wrappers import ConcatDataset, RepeatDataset 5 | from .registry import DATASETS 6 | 7 | 8 | def _concat_dataset(cfg, default_args=None): 9 | ann_files = cfg['ann_file'] 10 | img_prefixes = cfg.get('img_prefix', None) 11 | seg_prefixes = cfg.get('seg_prefix', None) 12 | proposal_files = cfg.get('proposal_file', None) 13 | 14 | datasets = [] 15 | num_dset = len(ann_files) 16 | for i in range(num_dset): 17 | data_cfg = copy.deepcopy(cfg) 18 | data_cfg['ann_file'] = ann_files[i] 19 | if isinstance(img_prefixes, (list, tuple)): 20 | data_cfg['img_prefix'] = img_prefixes[i] 21 | if isinstance(seg_prefixes, (list, tuple)): 22 | data_cfg['seg_prefix'] = seg_prefixes[i] 23 | if isinstance(proposal_files, (list, tuple)): 24 | data_cfg['proposal_file'] = proposal_files[i] 25 | datasets.append(build_dataset(data_cfg, default_args)) 26 | 27 | return ConcatDataset(datasets) 28 | 29 | 30 | def build_dataset(cfg, default_args=None): 31 | if isinstance(cfg, (list, tuple)): 32 | dataset = ConcatDataset([build_dataset(c, default_args) for c in cfg]) 33 | elif cfg['type'] == 'RepeatDataset': 34 | dataset = RepeatDataset( 35 | build_dataset(cfg['dataset'], default_args), cfg['times']) 36 | elif isinstance(cfg.get('ann_file'), (list, tuple)): 37 | dataset = _concat_dataset(cfg, default_args) 38 | else: 39 | dataset = build_from_cfg(cfg, DATASETS, default_args) 40 | 41 | return dataset 42 | -------------------------------------------------------------------------------- /mmdet/datasets/dataset_wrappers.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | from torch.utils.data.dataset import ConcatDataset as _ConcatDataset 3 | 4 | from .registry import DATASETS 5 | 6 | 7 | @DATASETS.register_module 8 | class ConcatDataset(_ConcatDataset): 9 | """A wrapper of concatenated dataset. 10 | 11 | Same as :obj:`torch.utils.data.dataset.ConcatDataset`, but 12 | concat the group flag for image aspect ratio. 13 | 14 | Args: 15 | datasets (list[:obj:`Dataset`]): A list of datasets. 16 | """ 17 | 18 | def __init__(self, datasets): 19 | super(ConcatDataset, self).__init__(datasets) 20 | self.CLASSES = datasets[0].CLASSES 21 | if hasattr(datasets[0], 'flag'): 22 | flags = [] 23 | for i in range(0, len(datasets)): 24 | flags.append(datasets[i].flag) 25 | self.flag = np.concatenate(flags) 26 | 27 | 28 | @DATASETS.register_module 29 | class RepeatDataset(object): 30 | """A wrapper of repeated dataset. 31 | 32 | The length of repeated dataset will be `times` larger than the original 33 | dataset. This is useful when the data loading time is long but the dataset 34 | is small. Using RepeatDataset can reduce the data loading time between 35 | epochs. 36 | 37 | Args: 38 | dataset (:obj:`Dataset`): The dataset to be repeated. 39 | times (int): Repeat times. 40 | """ 41 | 42 | def __init__(self, dataset, times): 43 | self.dataset = dataset 44 | self.times = times 45 | self.CLASSES = dataset.CLASSES 46 | if hasattr(self.dataset, 'flag'): 47 | self.flag = np.tile(self.dataset.flag, times) 48 | 49 | self._ori_len = len(self.dataset) 50 | 51 | def __getitem__(self, idx): 52 | return self.dataset[idx % self._ori_len] 53 | 54 | def __len__(self): 55 | return self.times * self._ori_len 56 | -------------------------------------------------------------------------------- /mmdet/datasets/loader/__init__.py: -------------------------------------------------------------------------------- 1 | from .build_loader import build_dataloader 2 | from .sampler import DistributedGroupSampler, GroupSampler 3 | 4 | __all__ = ['GroupSampler', 'DistributedGroupSampler', 'build_dataloader'] 5 | -------------------------------------------------------------------------------- /mmdet/datasets/loader/build_loader.py: -------------------------------------------------------------------------------- 1 | import platform 2 | import random 3 | from functools import partial 4 | 5 | import numpy as np 6 | from mmcv.parallel import collate 7 | from mmcv.runner import get_dist_info 8 | from torch.utils.data import DataLoader 9 | 10 | from .sampler import DistributedGroupSampler, DistributedSampler, GroupSampler 11 | 12 | if platform.system() != 'Windows': 13 | # https://github.com/pytorch/pytorch/issues/973 14 | import resource 15 | rlimit = resource.getrlimit(resource.RLIMIT_NOFILE) 16 | hard_limit = rlimit[1] 17 | soft_limit = min(4096, hard_limit) 18 | resource.setrlimit(resource.RLIMIT_NOFILE, (soft_limit, hard_limit)) 19 | 20 | 21 | def build_dataloader(dataset, 22 | imgs_per_gpu, 23 | workers_per_gpu, 24 | num_gpus=1, 25 | dist=True, 26 | shuffle=True, 27 | seed=None, 28 | **kwargs): 29 | """Build PyTorch DataLoader. 30 | 31 | In distributed training, each GPU/process has a dataloader. 32 | In non-distributed training, there is only one dataloader for all GPUs. 33 | 34 | Args: 35 | dataset (Dataset): A PyTorch dataset. 36 | imgs_per_gpu (int): Number of images on each GPU, i.e., batch size of 37 | each GPU. 38 | workers_per_gpu (int): How many subprocesses to use for data loading 39 | for each GPU. 40 | num_gpus (int): Number of GPUs. Only used in non-distributed training. 41 | dist (bool): Distributed training/test or not. Default: True. 42 | shuffle (bool): Whether to shuffle the data at every epoch. 43 | Default: True. 44 | kwargs: any keyword argument to be used to initialize DataLoader 45 | 46 | Returns: 47 | DataLoader: A PyTorch dataloader. 48 | """ 49 | rank, world_size = get_dist_info() 50 | if dist: 51 | # DistributedGroupSampler will definitely shuffle the data to satisfy 52 | # that images on each GPU are in the same group 53 | if shuffle: 54 | sampler = DistributedGroupSampler(dataset, imgs_per_gpu, 55 | world_size, rank) 56 | else: 57 | sampler = DistributedSampler( 58 | dataset, world_size, rank, shuffle=False) 59 | batch_size = imgs_per_gpu 60 | num_workers = workers_per_gpu 61 | else: 62 | sampler = GroupSampler(dataset, imgs_per_gpu) if shuffle else None 63 | batch_size = num_gpus * imgs_per_gpu 64 | num_workers = num_gpus * workers_per_gpu 65 | 66 | init_fn = partial( 67 | worker_init_fn, num_workers=num_workers, rank=rank, 68 | seed=seed) if seed is not None else None 69 | 70 | data_loader = DataLoader( 71 | dataset, 72 | batch_size=batch_size, 73 | sampler=sampler, 74 | num_workers=num_workers, 75 | collate_fn=partial(collate, samples_per_gpu=imgs_per_gpu), 76 | pin_memory=False, 77 | worker_init_fn=init_fn, 78 | **kwargs) 79 | 80 | return data_loader 81 | 82 | 83 | def worker_init_fn(worker_id, num_workers, rank, seed): 84 | # The seed of each worker equals to 85 | # num_worker * rank + worker_id + user_seed 86 | worker_seed = num_workers * rank + worker_id + seed 87 | np.random.seed(worker_seed) 88 | random.seed(worker_seed) 89 | -------------------------------------------------------------------------------- /mmdet/datasets/pipelines/__init__.py: -------------------------------------------------------------------------------- 1 | from .compose import Compose 2 | from .formating import (Collect, ImageToTensor, ToDataContainer, ToTensor, 3 | Transpose, to_tensor) 4 | from .instaboost import InstaBoost 5 | from .loading import LoadAnnotations, LoadImageFromFile, LoadProposals 6 | from .test_aug import MultiScaleFlipAug 7 | from .transforms import (Albu, Expand, MinIoURandomCrop, Normalize, Pad, 8 | PhotoMetricDistortion, RandomCrop, RandomFlip, Resize, 9 | SegRescale) 10 | 11 | __all__ = [ 12 | 'Compose', 'to_tensor', 'ToTensor', 'ImageToTensor', 'ToDataContainer', 13 | 'Transpose', 'Collect', 'LoadAnnotations', 'LoadImageFromFile', 14 | 'LoadProposals', 'MultiScaleFlipAug', 'Resize', 'RandomFlip', 'Pad', 15 | 'RandomCrop', 'Normalize', 'SegRescale', 'MinIoURandomCrop', 'Expand', 16 | 'PhotoMetricDistortion', 'Albu', 'InstaBoost' 17 | ] 18 | -------------------------------------------------------------------------------- /mmdet/datasets/pipelines/compose.py: -------------------------------------------------------------------------------- 1 | import collections 2 | 3 | from mmdet.utils import build_from_cfg 4 | from ..registry import PIPELINES 5 | 6 | 7 | @PIPELINES.register_module 8 | class Compose(object): 9 | 10 | def __init__(self, transforms): 11 | assert isinstance(transforms, collections.abc.Sequence) 12 | self.transforms = [] 13 | for transform in transforms: 14 | if isinstance(transform, dict): 15 | transform = build_from_cfg(transform, PIPELINES) 16 | self.transforms.append(transform) 17 | elif callable(transform): 18 | self.transforms.append(transform) 19 | else: 20 | raise TypeError('transform must be callable or a dict') 21 | 22 | def __call__(self, data): 23 | for t in self.transforms: 24 | data = t(data) 25 | if data is None: 26 | return None 27 | return data 28 | 29 | def __repr__(self): 30 | format_string = self.__class__.__name__ + '(' 31 | for t in self.transforms: 32 | format_string += '\n' 33 | format_string += ' {0}'.format(t) 34 | format_string += '\n)' 35 | return format_string 36 | -------------------------------------------------------------------------------- /mmdet/datasets/pipelines/instaboost.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | from ..registry import PIPELINES 4 | 5 | 6 | @PIPELINES.register_module 7 | class InstaBoost(object): 8 | """ 9 | Data augmentation method in paper "InstaBoost: Boosting Instance 10 | Segmentation Via Probability Map Guided Copy-Pasting" 11 | Implementation details can refer to https://github.com/GothicAi/Instaboost. 12 | """ 13 | 14 | def __init__(self, 15 | action_candidate=('normal', 'horizontal', 'skip'), 16 | action_prob=(1, 0, 0), 17 | scale=(0.8, 1.2), 18 | dx=15, 19 | dy=15, 20 | theta=(-1, 1), 21 | color_prob=0.5, 22 | hflag=False, 23 | aug_ratio=0.5): 24 | try: 25 | import instaboostfast as instaboost 26 | except ImportError: 27 | raise ImportError( 28 | 'Please run "pip install instaboostfast" ' 29 | 'to install instaboostfast first for instaboost augmentation.') 30 | self.cfg = instaboost.InstaBoostConfig(action_candidate, action_prob, 31 | scale, dx, dy, theta, 32 | color_prob, hflag) 33 | self.aug_ratio = aug_ratio 34 | 35 | def _load_anns(self, results): 36 | labels = results['ann_info']['labels'] 37 | masks = results['ann_info']['masks'] 38 | bboxes = results['ann_info']['bboxes'] 39 | n = len(labels) 40 | 41 | anns = [] 42 | for i in range(n): 43 | label = labels[i] 44 | bbox = bboxes[i] 45 | mask = masks[i] 46 | x1, y1, x2, y2 = bbox 47 | bbox = [x1, y1, x2 - x1 + 1, y2 - y1 + 1] 48 | anns.append({ 49 | 'category_id': label, 50 | 'segmentation': mask, 51 | 'bbox': bbox 52 | }) 53 | 54 | return anns 55 | 56 | def _parse_anns(self, results, anns, img): 57 | gt_bboxes = [] 58 | gt_labels = [] 59 | gt_masks_ann = [] 60 | for ann in anns: 61 | x1, y1, w, h = ann['bbox'] 62 | bbox = [x1, y1, x1 + w - 1, y1 + h - 1] 63 | gt_bboxes.append(bbox) 64 | gt_labels.append(ann['category_id']) 65 | gt_masks_ann.append(ann['segmentation']) 66 | gt_bboxes = np.array(gt_bboxes, dtype=np.float32) 67 | gt_labels = np.array(gt_labels, dtype=np.int64) 68 | results['ann_info']['labels'] = gt_labels 69 | results['ann_info']['bboxes'] = gt_bboxes 70 | results['ann_info']['masks'] = gt_masks_ann 71 | results['img'] = img 72 | return results 73 | 74 | def __call__(self, results): 75 | img = results['img'] 76 | anns = self._load_anns(results) 77 | if np.random.choice([0, 1], p=[1 - self.aug_ratio, self.aug_ratio]): 78 | try: 79 | import instaboostfast as instaboost 80 | except ImportError: 81 | raise ImportError('Please run "pip install instaboostfast" ' 82 | 'to install instaboostfast first.') 83 | anns, img = instaboost.get_new_data( 84 | anns, img, self.cfg, background=None) 85 | results = self._parse_anns(results, anns, img) 86 | return results 87 | 88 | def __repr__(self): 89 | repr_str = self.__class__.__name__ 90 | repr_str += ('(cfg={}, aug_ratio={})').format(self.cfg, self.aug_ratio) 91 | return repr_str 92 | -------------------------------------------------------------------------------- /mmdet/datasets/pipelines/test_aug.py: -------------------------------------------------------------------------------- 1 | import mmcv 2 | 3 | from ..registry import PIPELINES 4 | from .compose import Compose 5 | 6 | 7 | @PIPELINES.register_module 8 | class MultiScaleFlipAug(object): 9 | 10 | def __init__(self, transforms, img_scale, flip=False): 11 | self.transforms = Compose(transforms) 12 | self.img_scale = img_scale if isinstance(img_scale, 13 | list) else [img_scale] 14 | assert mmcv.is_list_of(self.img_scale, tuple) 15 | self.flip = flip 16 | 17 | def __call__(self, results): 18 | aug_data = [] 19 | flip_aug = [False, True] if self.flip else [False] 20 | for scale in self.img_scale: 21 | for flip in flip_aug: 22 | _results = results.copy() 23 | _results['scale'] = scale 24 | _results['flip'] = flip 25 | data = self.transforms(_results) 26 | aug_data.append(data) 27 | # list of dict to dict of list 28 | aug_data_dict = {key: [] for key in aug_data[0]} 29 | for data in aug_data: 30 | for key, val in data.items(): 31 | aug_data_dict[key].append(val) 32 | return aug_data_dict 33 | 34 | def __repr__(self): 35 | repr_str = self.__class__.__name__ 36 | repr_str += '(transforms={}, img_scale={}, flip={})'.format( 37 | self.transforms, self.img_scale, self.flip) 38 | return repr_str 39 | -------------------------------------------------------------------------------- /mmdet/datasets/registry.py: -------------------------------------------------------------------------------- 1 | from mmdet.utils import Registry 2 | 3 | DATASETS = Registry('dataset') 4 | PIPELINES = Registry('pipeline') 5 | -------------------------------------------------------------------------------- /mmdet/datasets/voc.py: -------------------------------------------------------------------------------- 1 | from mmdet.core import eval_map, eval_recalls 2 | from .registry import DATASETS 3 | from .xml_style import XMLDataset 4 | 5 | 6 | @DATASETS.register_module 7 | class VOCDataset(XMLDataset): 8 | 9 | CLASSES = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 10 | 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 11 | 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 12 | 'tvmonitor') 13 | 14 | def __init__(self, **kwargs): 15 | super(VOCDataset, self).__init__(**kwargs) 16 | if 'VOC2007' in self.img_prefix: 17 | self.year = 2007 18 | elif 'VOC2012' in self.img_prefix: 19 | self.year = 2012 20 | else: 21 | raise ValueError('Cannot infer dataset year from img_prefix') 22 | 23 | def evaluate(self, 24 | results, 25 | metric='mAP', 26 | logger=None, 27 | proposal_nums=(100, 300, 1000), 28 | iou_thr=0.5, 29 | scale_ranges=None): 30 | if not isinstance(metric, str): 31 | assert len(metric) == 1 32 | metric = metric[0] 33 | allowed_metrics = ['mAP', 'recall'] 34 | if metric not in allowed_metrics: 35 | raise KeyError('metric {} is not supported'.format(metric)) 36 | annotations = [self.get_ann_info(i) for i in range(len(self))] 37 | eval_results = {} 38 | if metric == 'mAP': 39 | assert isinstance(iou_thr, float) 40 | if self.year == 2007: 41 | ds_name = 'voc07' 42 | else: 43 | ds_name = self.dataset.CLASSES 44 | mean_ap, _ = eval_map( 45 | results, 46 | annotations, 47 | scale_ranges=None, 48 | iou_thr=iou_thr, 49 | dataset=ds_name, 50 | logger=logger) 51 | eval_results['mAP'] = mean_ap 52 | elif metric == 'recall': 53 | gt_bboxes = [ann['bboxes'] for ann in annotations] 54 | if isinstance(iou_thr, float): 55 | iou_thr = [iou_thr] 56 | recalls = eval_recalls( 57 | gt_bboxes, results, proposal_nums, iou_thr, logger=logger) 58 | for i, num in enumerate(proposal_nums): 59 | for j, iou in enumerate(iou_thr): 60 | eval_results['recall@{}@{}'.format(num, iou)] = recalls[i, 61 | j] 62 | if recalls.shape[1] > 1: 63 | ar = recalls.mean(axis=1) 64 | for i, num in enumerate(proposal_nums): 65 | eval_results['AR@{}'.format(num)] = ar[i] 66 | return eval_results 67 | -------------------------------------------------------------------------------- /mmdet/datasets/wider_face.py: -------------------------------------------------------------------------------- 1 | import os.path as osp 2 | import xml.etree.ElementTree as ET 3 | 4 | import mmcv 5 | 6 | from .registry import DATASETS 7 | from .xml_style import XMLDataset 8 | 9 | 10 | @DATASETS.register_module 11 | class WIDERFaceDataset(XMLDataset): 12 | """ 13 | Reader for the WIDER Face dataset in PASCAL VOC format. 14 | Conversion scripts can be found in 15 | https://github.com/sovrasov/wider-face-pascal-voc-annotations 16 | """ 17 | CLASSES = ('face', ) 18 | 19 | def __init__(self, **kwargs): 20 | super(WIDERFaceDataset, self).__init__(**kwargs) 21 | 22 | def load_annotations(self, ann_file): 23 | img_infos = [] 24 | img_ids = mmcv.list_from_file(ann_file) 25 | for img_id in img_ids: 26 | filename = '{}.jpg'.format(img_id) 27 | xml_path = osp.join(self.img_prefix, 'Annotations', 28 | '{}.xml'.format(img_id)) 29 | tree = ET.parse(xml_path) 30 | root = tree.getroot() 31 | size = root.find('size') 32 | width = int(size.find('width').text) 33 | height = int(size.find('height').text) 34 | folder = root.find('folder').text 35 | img_infos.append( 36 | dict( 37 | id=img_id, 38 | filename=osp.join(folder, filename), 39 | width=width, 40 | height=height)) 41 | 42 | return img_infos 43 | -------------------------------------------------------------------------------- /mmdet/datasets/xml_style.py: -------------------------------------------------------------------------------- 1 | import os.path as osp 2 | import xml.etree.ElementTree as ET 3 | 4 | import mmcv 5 | import numpy as np 6 | 7 | from .custom import CustomDataset 8 | from .registry import DATASETS 9 | 10 | 11 | @DATASETS.register_module 12 | class XMLDataset(CustomDataset): 13 | 14 | def __init__(self, min_size=None, **kwargs): 15 | super(XMLDataset, self).__init__(**kwargs) 16 | self.cat2label = {cat: i + 1 for i, cat in enumerate(self.CLASSES)} 17 | self.min_size = min_size 18 | 19 | def load_annotations(self, ann_file): 20 | img_infos = [] 21 | img_ids = mmcv.list_from_file(ann_file) 22 | for img_id in img_ids: 23 | filename = 'JPEGImages/{}.jpg'.format(img_id) 24 | xml_path = osp.join(self.img_prefix, 'Annotations', 25 | '{}.xml'.format(img_id)) 26 | tree = ET.parse(xml_path) 27 | root = tree.getroot() 28 | size = root.find('size') 29 | width = int(size.find('width').text) 30 | height = int(size.find('height').text) 31 | img_infos.append( 32 | dict(id=img_id, filename=filename, width=width, height=height)) 33 | return img_infos 34 | 35 | def get_ann_info(self, idx): 36 | img_id = self.img_infos[idx]['id'] 37 | xml_path = osp.join(self.img_prefix, 'Annotations', 38 | '{}.xml'.format(img_id)) 39 | tree = ET.parse(xml_path) 40 | root = tree.getroot() 41 | bboxes = [] 42 | labels = [] 43 | bboxes_ignore = [] 44 | labels_ignore = [] 45 | for obj in root.findall('object'): 46 | name = obj.find('name').text 47 | label = self.cat2label[name] 48 | difficult = int(obj.find('difficult').text) 49 | bnd_box = obj.find('bndbox') 50 | bbox = [ 51 | int(bnd_box.find('xmin').text), 52 | int(bnd_box.find('ymin').text), 53 | int(bnd_box.find('xmax').text), 54 | int(bnd_box.find('ymax').text) 55 | ] 56 | ignore = False 57 | if self.min_size: 58 | assert not self.test_mode 59 | w = bbox[2] - bbox[0] 60 | h = bbox[3] - bbox[1] 61 | if w < self.min_size or h < self.min_size: 62 | ignore = True 63 | if difficult or ignore: 64 | bboxes_ignore.append(bbox) 65 | labels_ignore.append(label) 66 | else: 67 | bboxes.append(bbox) 68 | labels.append(label) 69 | if not bboxes: 70 | bboxes = np.zeros((0, 4)) 71 | labels = np.zeros((0, )) 72 | else: 73 | bboxes = np.array(bboxes, ndmin=2) - 1 74 | labels = np.array(labels) 75 | if not bboxes_ignore: 76 | bboxes_ignore = np.zeros((0, 4)) 77 | labels_ignore = np.zeros((0, )) 78 | else: 79 | bboxes_ignore = np.array(bboxes_ignore, ndmin=2) - 1 80 | labels_ignore = np.array(labels_ignore) 81 | ann = dict( 82 | bboxes=bboxes.astype(np.float32), 83 | labels=labels.astype(np.int64), 84 | bboxes_ignore=bboxes_ignore.astype(np.float32), 85 | labels_ignore=labels_ignore.astype(np.int64)) 86 | return ann 87 | -------------------------------------------------------------------------------- /mmdet/models/__init__.py: -------------------------------------------------------------------------------- 1 | from .anchor_heads import * # noqa: F401,F403 2 | from .backbones import * # noqa: F401,F403 3 | from .bbox_heads import * # noqa: F401,F403 4 | from .builder import (build_backbone, build_detector, build_head, build_loss, 5 | build_neck, build_roi_extractor, build_shared_head) 6 | from .detectors import * # noqa: F401,F403 7 | from .losses import * # noqa: F401,F403 8 | from .mask_heads import * # noqa: F401,F403 9 | from .necks import * # noqa: F401,F403 10 | from .registry import (BACKBONES, DETECTORS, HEADS, LOSSES, NECKS, 11 | ROI_EXTRACTORS, SHARED_HEADS) 12 | from .roi_extractors import * # noqa: F401,F403 13 | from .shared_heads import * # noqa: F401,F403 14 | 15 | __all__ = [ 16 | 'BACKBONES', 'NECKS', 'ROI_EXTRACTORS', 'SHARED_HEADS', 'HEADS', 'LOSSES', 17 | 'DETECTORS', 'build_backbone', 'build_neck', 'build_roi_extractor', 18 | 'build_shared_head', 'build_head', 'build_loss', 'build_detector' 19 | ] 20 | -------------------------------------------------------------------------------- /mmdet/models/anchor_heads/__init__.py: -------------------------------------------------------------------------------- 1 | from .anchor_head import AnchorHead 2 | from .atss_head import ATSSHead 3 | from .fcos_head import FCOSHead 4 | from .fovea_head import FoveaHead 5 | from .free_anchor_retina_head import FreeAnchorRetinaHead 6 | from .ga_retina_head import GARetinaHead 7 | from .ga_rpn_head import GARPNHead 8 | from .guided_anchor_head import FeatureAdaption, GuidedAnchorHead 9 | from .reppoints_head import RepPointsHead 10 | from .retina_head import RetinaHead 11 | from .retina_sepbn_head import RetinaSepBNHead 12 | from .rpn_head import RPNHead 13 | from .ssd_head import SSDHead 14 | 15 | __all__ = [ 16 | 'AnchorHead', 'GuidedAnchorHead', 'FeatureAdaption', 'RPNHead', 17 | 'GARPNHead', 'RetinaHead', 'RetinaSepBNHead', 'GARetinaHead', 'SSDHead', 18 | 'FCOSHead', 'RepPointsHead', 'FoveaHead', 'FreeAnchorRetinaHead', 19 | 'ATSSHead' 20 | ] 21 | -------------------------------------------------------------------------------- /mmdet/models/backbones/__init__.py: -------------------------------------------------------------------------------- 1 | from .hrnet import HRNet 2 | from .resnet import ResNet, make_res_layer 3 | from .resnext import ResNeXt 4 | from .ssd_vgg import SSDVGG 5 | 6 | __all__ = ['ResNet', 'make_res_layer', 'ResNeXt', 'SSDVGG', 'HRNet'] 7 | -------------------------------------------------------------------------------- /mmdet/models/bbox_heads/__init__.py: -------------------------------------------------------------------------------- 1 | from .bbox_head import BBoxHead 2 | from .convfc_bbox_head import ConvFCBBoxHead, SharedFCBBoxHead 3 | from .double_bbox_head import DoubleConvFCBBoxHead 4 | 5 | __all__ = [ 6 | 'BBoxHead', 'ConvFCBBoxHead', 'SharedFCBBoxHead', 'DoubleConvFCBBoxHead' 7 | ] 8 | -------------------------------------------------------------------------------- /mmdet/models/builder.py: -------------------------------------------------------------------------------- 1 | from torch import nn 2 | 3 | from mmdet.utils import build_from_cfg 4 | from .registry import (BACKBONES, DETECTORS, HEADS, LOSSES, NECKS, 5 | ROI_EXTRACTORS, SHARED_HEADS) 6 | 7 | 8 | def build(cfg, registry, default_args=None): 9 | if isinstance(cfg, list): 10 | modules = [ 11 | build_from_cfg(cfg_, registry, default_args) for cfg_ in cfg 12 | ] 13 | return nn.Sequential(*modules) 14 | else: 15 | return build_from_cfg(cfg, registry, default_args) 16 | 17 | 18 | def build_backbone(cfg): 19 | return build(cfg, BACKBONES) 20 | 21 | 22 | def build_neck(cfg): 23 | return build(cfg, NECKS) 24 | 25 | 26 | def build_roi_extractor(cfg): 27 | return build(cfg, ROI_EXTRACTORS) 28 | 29 | 30 | def build_shared_head(cfg): 31 | return build(cfg, SHARED_HEADS) 32 | 33 | 34 | def build_head(cfg): 35 | return build(cfg, HEADS) 36 | 37 | 38 | def build_loss(cfg): 39 | return build(cfg, LOSSES) 40 | 41 | 42 | def build_detector(cfg, train_cfg=None, test_cfg=None): 43 | return build(cfg, DETECTORS, dict(train_cfg=train_cfg, test_cfg=test_cfg)) 44 | -------------------------------------------------------------------------------- /mmdet/models/detectors/__init__.py: -------------------------------------------------------------------------------- 1 | from .atss import ATSS 2 | from .base import BaseDetector 3 | from .cascade_rcnn import CascadeRCNN 4 | from .double_head_rcnn import DoubleHeadRCNN 5 | from .fast_rcnn import FastRCNN 6 | from .faster_rcnn import FasterRCNN 7 | from .fcos import FCOS 8 | from .fovea import FOVEA 9 | from .grid_rcnn import GridRCNN 10 | from .htc import HybridTaskCascade 11 | from .mask_rcnn import MaskRCNN 12 | from .mask_scoring_rcnn import MaskScoringRCNN 13 | from .reppoints_detector import RepPointsDetector 14 | from .retinanet import RetinaNet 15 | from .rpn import RPN 16 | from .single_stage import SingleStageDetector 17 | from .two_stage import TwoStageDetector 18 | 19 | __all__ = [ 20 | 'ATSS', 'BaseDetector', 'SingleStageDetector', 'TwoStageDetector', 'RPN', 21 | 'FastRCNN', 'FasterRCNN', 'MaskRCNN', 'CascadeRCNN', 'HybridTaskCascade', 22 | 'DoubleHeadRCNN', 'RetinaNet', 'FCOS', 'GridRCNN', 'MaskScoringRCNN', 23 | 'RepPointsDetector', 'FOVEA' 24 | ] 25 | -------------------------------------------------------------------------------- /mmdet/models/detectors/atss.py: -------------------------------------------------------------------------------- 1 | from ..registry import DETECTORS 2 | from .single_stage import SingleStageDetector 3 | 4 | 5 | @DETECTORS.register_module 6 | class ATSS(SingleStageDetector): 7 | 8 | def __init__(self, 9 | backbone, 10 | neck, 11 | bbox_head, 12 | train_cfg=None, 13 | test_cfg=None, 14 | pretrained=None): 15 | super(ATSS, self).__init__(backbone, neck, bbox_head, train_cfg, 16 | test_cfg, pretrained) 17 | -------------------------------------------------------------------------------- /mmdet/models/detectors/fast_rcnn.py: -------------------------------------------------------------------------------- 1 | from ..registry import DETECTORS 2 | from .two_stage import TwoStageDetector 3 | 4 | 5 | @DETECTORS.register_module 6 | class FastRCNN(TwoStageDetector): 7 | 8 | def __init__(self, 9 | backbone, 10 | bbox_roi_extractor, 11 | bbox_head, 12 | train_cfg, 13 | test_cfg, 14 | neck=None, 15 | shared_head=None, 16 | mask_roi_extractor=None, 17 | mask_head=None, 18 | pretrained=None): 19 | super(FastRCNN, self).__init__( 20 | backbone=backbone, 21 | neck=neck, 22 | shared_head=shared_head, 23 | bbox_roi_extractor=bbox_roi_extractor, 24 | bbox_head=bbox_head, 25 | train_cfg=train_cfg, 26 | test_cfg=test_cfg, 27 | mask_roi_extractor=mask_roi_extractor, 28 | mask_head=mask_head, 29 | pretrained=pretrained) 30 | 31 | def forward_test(self, imgs, img_metas, proposals, **kwargs): 32 | """ 33 | Args: 34 | imgs (List[Tensor]): the outer list indicates test-time 35 | augmentations and inner Tensor should have a shape NxCxHxW, 36 | which contains all images in the batch. 37 | img_metas (List[List[dict]]): the outer list indicates test-time 38 | augs (multiscale, flip, etc.) and the inner list indicates 39 | images in a batch. 40 | proposals (List[List[Tensor]]): the outer list indicates test-time 41 | augs (multiscale, flip, etc.) and the inner list indicates 42 | images in a batch. The Tensor should have a shape Px4, where 43 | P is the number of proposals. 44 | """ 45 | for var, name in [(imgs, 'imgs'), (img_metas, 'img_metas')]: 46 | if not isinstance(var, list): 47 | raise TypeError('{} must be a list, but got {}'.format( 48 | name, type(var))) 49 | 50 | num_augs = len(imgs) 51 | if num_augs != len(img_metas): 52 | raise ValueError( 53 | 'num of augmentations ({}) != num of image meta ({})'.format( 54 | len(imgs), len(img_metas))) 55 | # TODO: remove the restriction of imgs_per_gpu == 1 when prepared 56 | imgs_per_gpu = imgs[0].size(0) 57 | assert imgs_per_gpu == 1 58 | 59 | if num_augs == 1: 60 | return self.simple_test(imgs[0], img_metas[0], proposals[0], 61 | **kwargs) 62 | else: 63 | # TODO: support test-time augmentation 64 | assert NotImplementedError 65 | -------------------------------------------------------------------------------- /mmdet/models/detectors/faster_rcnn.py: -------------------------------------------------------------------------------- 1 | from ..registry import DETECTORS 2 | from .two_stage import TwoStageDetector 3 | 4 | 5 | @DETECTORS.register_module 6 | class FasterRCNN(TwoStageDetector): 7 | 8 | def __init__(self, 9 | backbone, 10 | rpn_head, 11 | bbox_roi_extractor, 12 | bbox_head, 13 | train_cfg, 14 | test_cfg, 15 | neck=None, 16 | shared_head=None, 17 | pretrained=None): 18 | super(FasterRCNN, self).__init__( 19 | backbone=backbone, 20 | neck=neck, 21 | shared_head=shared_head, 22 | rpn_head=rpn_head, 23 | bbox_roi_extractor=bbox_roi_extractor, 24 | bbox_head=bbox_head, 25 | train_cfg=train_cfg, 26 | test_cfg=test_cfg, 27 | pretrained=pretrained) 28 | -------------------------------------------------------------------------------- /mmdet/models/detectors/fcos.py: -------------------------------------------------------------------------------- 1 | from ..registry import DETECTORS 2 | from .single_stage import SingleStageDetector 3 | 4 | 5 | @DETECTORS.register_module 6 | class FCOS(SingleStageDetector): 7 | 8 | def __init__(self, 9 | backbone, 10 | neck, 11 | bbox_head, 12 | train_cfg=None, 13 | test_cfg=None, 14 | pretrained=None): 15 | super(FCOS, self).__init__(backbone, neck, bbox_head, train_cfg, 16 | test_cfg, pretrained) 17 | -------------------------------------------------------------------------------- /mmdet/models/detectors/fovea.py: -------------------------------------------------------------------------------- 1 | from ..registry import DETECTORS 2 | from .single_stage import SingleStageDetector 3 | 4 | 5 | @DETECTORS.register_module 6 | class FOVEA(SingleStageDetector): 7 | 8 | def __init__(self, 9 | backbone, 10 | neck, 11 | bbox_head, 12 | train_cfg=None, 13 | test_cfg=None, 14 | pretrained=None): 15 | super(FOVEA, self).__init__(backbone, neck, bbox_head, train_cfg, 16 | test_cfg, pretrained) 17 | -------------------------------------------------------------------------------- /mmdet/models/detectors/mask_rcnn.py: -------------------------------------------------------------------------------- 1 | from ..registry import DETECTORS 2 | from .two_stage import TwoStageDetector 3 | 4 | 5 | @DETECTORS.register_module 6 | class MaskRCNN(TwoStageDetector): 7 | 8 | def __init__(self, 9 | backbone, 10 | rpn_head, 11 | bbox_roi_extractor, 12 | bbox_head, 13 | mask_roi_extractor, 14 | mask_head, 15 | train_cfg, 16 | test_cfg, 17 | neck=None, 18 | shared_head=None, 19 | pretrained=None): 20 | super(MaskRCNN, self).__init__( 21 | backbone=backbone, 22 | neck=neck, 23 | shared_head=shared_head, 24 | rpn_head=rpn_head, 25 | bbox_roi_extractor=bbox_roi_extractor, 26 | bbox_head=bbox_head, 27 | mask_roi_extractor=mask_roi_extractor, 28 | mask_head=mask_head, 29 | train_cfg=train_cfg, 30 | test_cfg=test_cfg, 31 | pretrained=pretrained) 32 | -------------------------------------------------------------------------------- /mmdet/models/detectors/reppoints_detector.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | from mmdet.core import bbox2result, bbox_mapping_back, multiclass_nms 4 | from ..registry import DETECTORS 5 | from .single_stage import SingleStageDetector 6 | 7 | 8 | @DETECTORS.register_module 9 | class RepPointsDetector(SingleStageDetector): 10 | """RepPoints: Point Set Representation for Object Detection. 11 | 12 | This detector is the implementation of: 13 | - RepPoints detector (https://arxiv.org/pdf/1904.11490) 14 | """ 15 | 16 | def __init__(self, 17 | backbone, 18 | neck, 19 | bbox_head, 20 | train_cfg=None, 21 | test_cfg=None, 22 | pretrained=None): 23 | super(RepPointsDetector, 24 | self).__init__(backbone, neck, bbox_head, train_cfg, test_cfg, 25 | pretrained) 26 | 27 | def merge_aug_results(self, aug_bboxes, aug_scores, img_metas): 28 | """Merge augmented detection bboxes and scores. 29 | 30 | Args: 31 | aug_bboxes (list[Tensor]): shape (n, 4*#class) 32 | aug_scores (list[Tensor] or None): shape (n, #class) 33 | img_shapes (list[Tensor]): shape (3, ). 34 | 35 | Returns: 36 | tuple: (bboxes, scores) 37 | """ 38 | recovered_bboxes = [] 39 | for bboxes, img_info in zip(aug_bboxes, img_metas): 40 | img_shape = img_info[0]['img_shape'] 41 | scale_factor = img_info[0]['scale_factor'] 42 | flip = img_info[0]['flip'] 43 | bboxes = bbox_mapping_back(bboxes, img_shape, scale_factor, flip) 44 | recovered_bboxes.append(bboxes) 45 | bboxes = torch.cat(recovered_bboxes, dim=0) 46 | if aug_scores is None: 47 | return bboxes 48 | else: 49 | scores = torch.cat(aug_scores, dim=0) 50 | return bboxes, scores 51 | 52 | def aug_test(self, imgs, img_metas, rescale=False): 53 | # recompute feats to save memory 54 | feats = self.extract_feats(imgs) 55 | 56 | aug_bboxes = [] 57 | aug_scores = [] 58 | for x, img_meta in zip(feats, img_metas): 59 | # only one image in the batch 60 | outs = self.bbox_head(x) 61 | bbox_inputs = outs + (img_metas, self.test_cfg, False, False) 62 | det_bboxes, det_scores = self.bbox_head.get_bboxes(*bbox_inputs)[0] 63 | aug_bboxes.append(det_bboxes) 64 | aug_scores.append(det_scores) 65 | 66 | # after merging, bboxes will be rescaled to the original image size 67 | merged_bboxes, merged_scores = self.merge_aug_results( 68 | aug_bboxes, aug_scores, img_metas) 69 | det_bboxes, det_labels = multiclass_nms(merged_bboxes, merged_scores, 70 | self.test_cfg.score_thr, 71 | self.test_cfg.nms, 72 | self.test_cfg.max_per_img) 73 | 74 | if rescale: 75 | _det_bboxes = det_bboxes 76 | else: 77 | _det_bboxes = det_bboxes.clone() 78 | _det_bboxes[:, :4] *= img_metas[0][0]['scale_factor'] 79 | bbox_results = bbox2result(_det_bboxes, det_labels, 80 | self.bbox_head.num_classes) 81 | return bbox_results 82 | -------------------------------------------------------------------------------- /mmdet/models/detectors/retinanet.py: -------------------------------------------------------------------------------- 1 | from ..registry import DETECTORS 2 | from .single_stage import SingleStageDetector 3 | 4 | 5 | @DETECTORS.register_module 6 | class RetinaNet(SingleStageDetector): 7 | 8 | def __init__(self, 9 | backbone, 10 | neck, 11 | bbox_head, 12 | train_cfg=None, 13 | test_cfg=None, 14 | pretrained=None): 15 | super(RetinaNet, self).__init__(backbone, neck, bbox_head, train_cfg, 16 | test_cfg, pretrained) 17 | -------------------------------------------------------------------------------- /mmdet/models/detectors/single_stage.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | 3 | from mmdet.core import bbox2result 4 | from .. import builder 5 | from ..registry import DETECTORS 6 | from .base import BaseDetector 7 | 8 | 9 | @DETECTORS.register_module 10 | class SingleStageDetector(BaseDetector): 11 | """Base class for single-stage detectors. 12 | 13 | Single-stage detectors directly and densely predict bounding boxes on the 14 | output features of the backbone+neck. 15 | """ 16 | 17 | def __init__(self, 18 | backbone, 19 | neck=None, 20 | bbox_head=None, 21 | train_cfg=None, 22 | test_cfg=None, 23 | pretrained=None): 24 | super(SingleStageDetector, self).__init__() 25 | self.backbone = builder.build_backbone(backbone) 26 | if neck is not None: 27 | self.neck = builder.build_neck(neck) 28 | self.bbox_head = builder.build_head(bbox_head) 29 | self.train_cfg = train_cfg 30 | self.test_cfg = test_cfg 31 | self.init_weights(pretrained=pretrained) 32 | 33 | def init_weights(self, pretrained=None): 34 | super(SingleStageDetector, self).init_weights(pretrained) 35 | self.backbone.init_weights(pretrained=pretrained) 36 | if self.with_neck: 37 | if isinstance(self.neck, nn.Sequential): 38 | for m in self.neck: 39 | m.init_weights() 40 | else: 41 | self.neck.init_weights() 42 | self.bbox_head.init_weights() 43 | 44 | def extract_feat(self, img): 45 | """Directly extract features from the backbone+neck 46 | """ 47 | x = self.backbone(img) 48 | if self.with_neck: 49 | x = self.neck(x) 50 | return x 51 | 52 | def forward_dummy(self, img): 53 | """Used for computing network flops. 54 | 55 | See `mmdetection/tools/get_flops.py` 56 | """ 57 | x = self.extract_feat(img) 58 | outs = self.bbox_head(x) 59 | return outs 60 | 61 | def forward_train(self, 62 | img, 63 | img_metas, 64 | gt_bboxes, 65 | gt_labels, 66 | gt_bboxes_ignore=None): 67 | x = self.extract_feat(img) 68 | outs = self.bbox_head(x) 69 | loss_inputs = outs + (gt_bboxes, gt_labels, img_metas, self.train_cfg) 70 | losses = self.bbox_head.loss( 71 | *loss_inputs, gt_bboxes_ignore=gt_bboxes_ignore) 72 | return losses 73 | 74 | def simple_test(self, img, img_metas, rescale=False): 75 | x = self.extract_feat(img) 76 | outs = self.bbox_head(x) 77 | bbox_inputs = outs + (img_metas, self.test_cfg, rescale) 78 | bbox_list = self.bbox_head.get_bboxes(*bbox_inputs) 79 | bbox_results = [ 80 | bbox2result(det_bboxes, det_labels, self.bbox_head.num_classes) 81 | for det_bboxes, det_labels in bbox_list 82 | ] 83 | return bbox_results[0] 84 | 85 | def aug_test(self, imgs, img_metas, rescale=False): 86 | raise NotImplementedError 87 | -------------------------------------------------------------------------------- /mmdet/models/losses/__init__.py: -------------------------------------------------------------------------------- 1 | from .accuracy import Accuracy, accuracy 2 | from .balanced_l1_loss import BalancedL1Loss, balanced_l1_loss 3 | from .cross_entropy_loss import (CrossEntropyLoss, binary_cross_entropy, 4 | cross_entropy, mask_cross_entropy) 5 | from .focal_loss import FocalLoss, sigmoid_focal_loss 6 | from .ghm_loss import GHMC, GHMR 7 | from .iou_loss import (BoundedIoULoss, GIoULoss, IoULoss, bounded_iou_loss, 8 | iou_loss) 9 | from .mse_loss import MSELoss, mse_loss 10 | from .smooth_l1_loss import SmoothL1Loss, smooth_l1_loss 11 | from .utils import reduce_loss, weight_reduce_loss, weighted_loss 12 | 13 | __all__ = [ 14 | 'accuracy', 'Accuracy', 'cross_entropy', 'binary_cross_entropy', 15 | 'mask_cross_entropy', 'CrossEntropyLoss', 'sigmoid_focal_loss', 16 | 'FocalLoss', 'smooth_l1_loss', 'SmoothL1Loss', 'balanced_l1_loss', 17 | 'BalancedL1Loss', 'mse_loss', 'MSELoss', 'iou_loss', 'bounded_iou_loss', 18 | 'IoULoss', 'BoundedIoULoss', 'GIoULoss', 'GHMC', 'GHMR', 'reduce_loss', 19 | 'weight_reduce_loss', 'weighted_loss' 20 | ] 21 | -------------------------------------------------------------------------------- /mmdet/models/losses/accuracy.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | 3 | 4 | def accuracy(pred, target, topk=1): 5 | assert isinstance(topk, (int, tuple)) 6 | if isinstance(topk, int): 7 | topk = (topk, ) 8 | return_single = True 9 | else: 10 | return_single = False 11 | 12 | maxk = max(topk) 13 | _, pred_label = pred.topk(maxk, dim=1) 14 | pred_label = pred_label.t() 15 | correct = pred_label.eq(target.view(1, -1).expand_as(pred_label)) 16 | 17 | res = [] 18 | for k in topk: 19 | correct_k = correct[:k].view(-1).float().sum(0, keepdim=True) 20 | res.append(correct_k.mul_(100.0 / pred.size(0))) 21 | return res[0] if return_single else res 22 | 23 | 24 | class Accuracy(nn.Module): 25 | 26 | def __init__(self, topk=(1, )): 27 | super().__init__() 28 | self.topk = topk 29 | 30 | def forward(self, pred, target): 31 | return accuracy(pred, target, self.topk) 32 | -------------------------------------------------------------------------------- /mmdet/models/losses/balanced_l1_loss.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | 5 | from ..registry import LOSSES 6 | from .utils import weighted_loss 7 | 8 | 9 | @weighted_loss 10 | def balanced_l1_loss(pred, 11 | target, 12 | beta=1.0, 13 | alpha=0.5, 14 | gamma=1.5, 15 | reduction='mean'): 16 | assert beta > 0 17 | assert pred.size() == target.size() and target.numel() > 0 18 | 19 | diff = torch.abs(pred - target) 20 | b = np.e**(gamma / alpha) - 1 21 | loss = torch.where( 22 | diff < beta, alpha / b * 23 | (b * diff + 1) * torch.log(b * diff / beta + 1) - alpha * diff, 24 | gamma * diff + gamma / b - alpha * beta) 25 | 26 | return loss 27 | 28 | 29 | @LOSSES.register_module 30 | class BalancedL1Loss(nn.Module): 31 | """Balanced L1 Loss 32 | 33 | arXiv: https://arxiv.org/pdf/1904.02701.pdf (CVPR 2019) 34 | """ 35 | 36 | def __init__(self, 37 | alpha=0.5, 38 | gamma=1.5, 39 | beta=1.0, 40 | reduction='mean', 41 | loss_weight=1.0): 42 | super(BalancedL1Loss, self).__init__() 43 | self.alpha = alpha 44 | self.gamma = gamma 45 | self.beta = beta 46 | self.reduction = reduction 47 | self.loss_weight = loss_weight 48 | 49 | def forward(self, 50 | pred, 51 | target, 52 | weight=None, 53 | avg_factor=None, 54 | reduction_override=None, 55 | **kwargs): 56 | assert reduction_override in (None, 'none', 'mean', 'sum') 57 | reduction = ( 58 | reduction_override if reduction_override else self.reduction) 59 | loss_bbox = self.loss_weight * balanced_l1_loss( 60 | pred, 61 | target, 62 | weight, 63 | alpha=self.alpha, 64 | gamma=self.gamma, 65 | beta=self.beta, 66 | reduction=reduction, 67 | avg_factor=avg_factor, 68 | **kwargs) 69 | return loss_bbox 70 | -------------------------------------------------------------------------------- /mmdet/models/losses/focal_loss.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch.nn.functional as F 3 | 4 | from mmdet.ops import sigmoid_focal_loss as _sigmoid_focal_loss 5 | from ..registry import LOSSES 6 | from .utils import weight_reduce_loss 7 | 8 | 9 | # This method is only for debugging 10 | def py_sigmoid_focal_loss(pred, 11 | target, 12 | weight=None, 13 | gamma=2.0, 14 | alpha=0.25, 15 | reduction='mean', 16 | avg_factor=None): 17 | pred_sigmoid = pred.sigmoid() 18 | target = target.type_as(pred) 19 | pt = (1 - pred_sigmoid) * target + pred_sigmoid * (1 - target) 20 | focal_weight = (alpha * target + (1 - alpha) * 21 | (1 - target)) * pt.pow(gamma) 22 | loss = F.binary_cross_entropy_with_logits( 23 | pred, target, reduction='none') * focal_weight 24 | loss = weight_reduce_loss(loss, weight, reduction, avg_factor) 25 | return loss 26 | 27 | 28 | def sigmoid_focal_loss(pred, 29 | target, 30 | weight=None, 31 | gamma=2.0, 32 | alpha=0.25, 33 | reduction='mean', 34 | avg_factor=None): 35 | # Function.apply does not accept keyword arguments, so the decorator 36 | # "weighted_loss" is not applicable 37 | loss = _sigmoid_focal_loss(pred, target, gamma, alpha) 38 | # TODO: find a proper way to handle the shape of weight 39 | if weight is not None: 40 | weight = weight.view(-1, 1) 41 | loss = weight_reduce_loss(loss, weight, reduction, avg_factor) 42 | return loss 43 | 44 | 45 | @LOSSES.register_module 46 | class FocalLoss(nn.Module): 47 | 48 | def __init__(self, 49 | use_sigmoid=True, 50 | gamma=2.0, 51 | alpha=0.25, 52 | reduction='mean', 53 | loss_weight=1.0): 54 | super(FocalLoss, self).__init__() 55 | assert use_sigmoid is True, 'Only sigmoid focal loss supported now.' 56 | self.use_sigmoid = use_sigmoid 57 | self.gamma = gamma 58 | self.alpha = alpha 59 | self.reduction = reduction 60 | self.loss_weight = loss_weight 61 | 62 | def forward(self, 63 | pred, 64 | target, 65 | weight=None, 66 | avg_factor=None, 67 | reduction_override=None): 68 | assert reduction_override in (None, 'none', 'mean', 'sum') 69 | reduction = ( 70 | reduction_override if reduction_override else self.reduction) 71 | if self.use_sigmoid: 72 | loss_cls = self.loss_weight * sigmoid_focal_loss( 73 | pred, 74 | target, 75 | weight, 76 | gamma=self.gamma, 77 | alpha=self.alpha, 78 | reduction=reduction, 79 | avg_factor=avg_factor) 80 | else: 81 | raise NotImplementedError 82 | return loss_cls 83 | -------------------------------------------------------------------------------- /mmdet/models/losses/mse_loss.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch.nn.functional as F 3 | 4 | from ..registry import LOSSES 5 | from .utils import weighted_loss 6 | 7 | 8 | @weighted_loss 9 | def mse_loss(pred, target): 10 | return F.mse_loss(pred, target, reduction='none') 11 | 12 | 13 | @LOSSES.register_module 14 | class MSELoss(nn.Module): 15 | 16 | def __init__(self, reduction='mean', loss_weight=1.0): 17 | super().__init__() 18 | self.reduction = reduction 19 | self.loss_weight = loss_weight 20 | 21 | def forward(self, pred, target, weight=None, avg_factor=None): 22 | loss = self.loss_weight * mse_loss( 23 | pred, 24 | target, 25 | weight, 26 | reduction=self.reduction, 27 | avg_factor=avg_factor) 28 | return loss 29 | -------------------------------------------------------------------------------- /mmdet/models/losses/smooth_l1_loss.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | 4 | from ..registry import LOSSES 5 | from .utils import weighted_loss 6 | 7 | 8 | @weighted_loss 9 | def smooth_l1_loss(pred, target, beta=1.0): 10 | assert beta > 0 11 | assert pred.size() == target.size() and target.numel() > 0 12 | diff = torch.abs(pred - target) 13 | loss = torch.where(diff < beta, 0.5 * diff * diff / beta, 14 | diff - 0.5 * beta) 15 | return loss 16 | 17 | 18 | @LOSSES.register_module 19 | class SmoothL1Loss(nn.Module): 20 | 21 | def __init__(self, beta=1.0, reduction='mean', loss_weight=1.0): 22 | super(SmoothL1Loss, self).__init__() 23 | self.beta = beta 24 | self.reduction = reduction 25 | self.loss_weight = loss_weight 26 | 27 | def forward(self, 28 | pred, 29 | target, 30 | weight=None, 31 | avg_factor=None, 32 | reduction_override=None, 33 | **kwargs): 34 | assert reduction_override in (None, 'none', 'mean', 'sum') 35 | reduction = ( 36 | reduction_override if reduction_override else self.reduction) 37 | loss_bbox = self.loss_weight * smooth_l1_loss( 38 | pred, 39 | target, 40 | weight, 41 | beta=self.beta, 42 | reduction=reduction, 43 | avg_factor=avg_factor, 44 | **kwargs) 45 | return loss_bbox 46 | -------------------------------------------------------------------------------- /mmdet/models/losses/utils.py: -------------------------------------------------------------------------------- 1 | import functools 2 | 3 | import torch.nn.functional as F 4 | 5 | 6 | def reduce_loss(loss, reduction): 7 | """Reduce loss as specified. 8 | 9 | Args: 10 | loss (Tensor): Elementwise loss tensor. 11 | reduction (str): Options are "none", "mean" and "sum". 12 | 13 | Return: 14 | Tensor: Reduced loss tensor. 15 | """ 16 | reduction_enum = F._Reduction.get_enum(reduction) 17 | # none: 0, elementwise_mean:1, sum: 2 18 | if reduction_enum == 0: 19 | return loss 20 | elif reduction_enum == 1: 21 | return loss.mean() 22 | elif reduction_enum == 2: 23 | return loss.sum() 24 | 25 | 26 | def weight_reduce_loss(loss, weight=None, reduction='mean', avg_factor=None): 27 | """Apply element-wise weight and reduce loss. 28 | 29 | Args: 30 | loss (Tensor): Element-wise loss. 31 | weight (Tensor): Element-wise weights. 32 | reduction (str): Same as built-in losses of PyTorch. 33 | avg_factor (float): Avarage factor when computing the mean of losses. 34 | 35 | Returns: 36 | Tensor: Processed loss values. 37 | """ 38 | # if weight is specified, apply element-wise weight 39 | if weight is not None: 40 | loss = loss * weight 41 | 42 | # if avg_factor is not specified, just reduce the loss 43 | if avg_factor is None: 44 | loss = reduce_loss(loss, reduction) 45 | else: 46 | # if reduction is mean, then average the loss by avg_factor 47 | if reduction == 'mean': 48 | loss = loss.sum() / avg_factor 49 | # if reduction is 'none', then do nothing, otherwise raise an error 50 | elif reduction != 'none': 51 | raise ValueError('avg_factor can not be used with reduction="sum"') 52 | return loss 53 | 54 | 55 | def weighted_loss(loss_func): 56 | """Create a weighted version of a given loss function. 57 | 58 | To use this decorator, the loss function must have the signature like 59 | `loss_func(pred, target, **kwargs)`. The function only needs to compute 60 | element-wise loss without any reduction. This decorator will add weight 61 | and reduction arguments to the function. The decorated function will have 62 | the signature like `loss_func(pred, target, weight=None, reduction='mean', 63 | avg_factor=None, **kwargs)`. 64 | 65 | :Example: 66 | 67 | >>> import torch 68 | >>> @weighted_loss 69 | >>> def l1_loss(pred, target): 70 | >>> return (pred - target).abs() 71 | 72 | >>> pred = torch.Tensor([0, 2, 3]) 73 | >>> target = torch.Tensor([1, 1, 1]) 74 | >>> weight = torch.Tensor([1, 0, 1]) 75 | 76 | >>> l1_loss(pred, target) 77 | tensor(1.3333) 78 | >>> l1_loss(pred, target, weight) 79 | tensor(1.) 80 | >>> l1_loss(pred, target, reduction='none') 81 | tensor([1., 1., 2.]) 82 | >>> l1_loss(pred, target, weight, avg_factor=2) 83 | tensor(1.5000) 84 | """ 85 | 86 | @functools.wraps(loss_func) 87 | def wrapper(pred, 88 | target, 89 | weight=None, 90 | reduction='mean', 91 | avg_factor=None, 92 | **kwargs): 93 | # get element-wise loss 94 | loss = loss_func(pred, target, **kwargs) 95 | loss = weight_reduce_loss(loss, weight, reduction, avg_factor) 96 | return loss 97 | 98 | return wrapper 99 | -------------------------------------------------------------------------------- /mmdet/models/mask_heads/__init__.py: -------------------------------------------------------------------------------- 1 | from .fcn_mask_head import FCNMaskHead 2 | from .fused_semantic_head import FusedSemanticHead 3 | from .grid_head import GridHead 4 | from .htc_mask_head import HTCMaskHead 5 | from .maskiou_head import MaskIoUHead 6 | 7 | __all__ = [ 8 | 'FCNMaskHead', 'HTCMaskHead', 'FusedSemanticHead', 'GridHead', 9 | 'MaskIoUHead' 10 | ] 11 | -------------------------------------------------------------------------------- /mmdet/models/mask_heads/htc_mask_head.py: -------------------------------------------------------------------------------- 1 | from mmdet.ops import ConvModule 2 | from ..registry import HEADS 3 | from .fcn_mask_head import FCNMaskHead 4 | 5 | 6 | @HEADS.register_module 7 | class HTCMaskHead(FCNMaskHead): 8 | 9 | def __init__(self, with_conv_res=True, *args, **kwargs): 10 | super(HTCMaskHead, self).__init__(*args, **kwargs) 11 | self.with_conv_res = with_conv_res 12 | if self.with_conv_res: 13 | self.conv_res = ConvModule( 14 | self.conv_out_channels, 15 | self.conv_out_channels, 16 | 1, 17 | conv_cfg=self.conv_cfg, 18 | norm_cfg=self.norm_cfg) 19 | 20 | def init_weights(self): 21 | super(HTCMaskHead, self).init_weights() 22 | if self.with_conv_res: 23 | self.conv_res.init_weights() 24 | 25 | def forward(self, x, res_feat=None, return_logits=True, return_feat=True): 26 | if res_feat is not None: 27 | assert self.with_conv_res 28 | res_feat = self.conv_res(res_feat) 29 | x = x + res_feat 30 | for conv in self.convs: 31 | x = conv(x) 32 | res_feat = x 33 | outs = [] 34 | if return_logits: 35 | x = self.upsample(x) 36 | if self.upsample_method == 'deconv': 37 | x = self.relu(x) 38 | mask_pred = self.conv_logits(x) 39 | outs.append(mask_pred) 40 | if return_feat: 41 | outs.append(res_feat) 42 | return outs if len(outs) > 1 else outs[0] 43 | -------------------------------------------------------------------------------- /mmdet/models/necks/__init__.py: -------------------------------------------------------------------------------- 1 | from .bfp import BFP 2 | from .fpn import FPN 3 | from .fpn_carafe import FPN_CARAFE 4 | from .hrfpn import HRFPN 5 | from .nas_fpn import NASFPN 6 | 7 | __all__ = ['FPN', 'BFP', 'HRFPN', 'NASFPN', 'FPN_CARAFE'] 8 | -------------------------------------------------------------------------------- /mmdet/models/registry.py: -------------------------------------------------------------------------------- 1 | from mmdet.utils import Registry 2 | 3 | BACKBONES = Registry('backbone') 4 | NECKS = Registry('neck') 5 | ROI_EXTRACTORS = Registry('roi_extractor') 6 | SHARED_HEADS = Registry('shared_head') 7 | HEADS = Registry('head') 8 | LOSSES = Registry('loss') 9 | DETECTORS = Registry('detector') 10 | -------------------------------------------------------------------------------- /mmdet/models/roi_extractors/__init__.py: -------------------------------------------------------------------------------- 1 | from .single_level import SingleRoIExtractor 2 | 3 | __all__ = ['SingleRoIExtractor'] 4 | -------------------------------------------------------------------------------- /mmdet/models/shared_heads/__init__.py: -------------------------------------------------------------------------------- 1 | from .res_layer import ResLayer 2 | 3 | __all__ = ['ResLayer'] 4 | -------------------------------------------------------------------------------- /mmdet/models/shared_heads/res_layer.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | from mmcv.cnn import constant_init, kaiming_init 3 | from mmcv.runner import load_checkpoint 4 | 5 | from mmdet.core import auto_fp16 6 | from mmdet.utils import get_root_logger 7 | from ..backbones import ResNet, make_res_layer 8 | from ..registry import SHARED_HEADS 9 | 10 | 11 | @SHARED_HEADS.register_module 12 | class ResLayer(nn.Module): 13 | 14 | def __init__(self, 15 | depth, 16 | stage=3, 17 | stride=2, 18 | dilation=1, 19 | style='pytorch', 20 | norm_cfg=dict(type='BN', requires_grad=True), 21 | norm_eval=True, 22 | with_cp=False, 23 | dcn=None): 24 | super(ResLayer, self).__init__() 25 | self.norm_eval = norm_eval 26 | self.norm_cfg = norm_cfg 27 | self.stage = stage 28 | self.fp16_enabled = False 29 | block, stage_blocks = ResNet.arch_settings[depth] 30 | stage_block = stage_blocks[stage] 31 | planes = 64 * 2**stage 32 | inplanes = 64 * 2**(stage - 1) * block.expansion 33 | 34 | res_layer = make_res_layer( 35 | block, 36 | inplanes, 37 | planes, 38 | stage_block, 39 | stride=stride, 40 | dilation=dilation, 41 | style=style, 42 | with_cp=with_cp, 43 | norm_cfg=self.norm_cfg, 44 | dcn=dcn) 45 | self.add_module('layer{}'.format(stage + 1), res_layer) 46 | 47 | def init_weights(self, pretrained=None): 48 | if isinstance(pretrained, str): 49 | logger = get_root_logger() 50 | load_checkpoint(self, pretrained, strict=False, logger=logger) 51 | elif pretrained is None: 52 | for m in self.modules(): 53 | if isinstance(m, nn.Conv2d): 54 | kaiming_init(m) 55 | elif isinstance(m, nn.BatchNorm2d): 56 | constant_init(m, 1) 57 | else: 58 | raise TypeError('pretrained must be a str or None') 59 | 60 | @auto_fp16() 61 | def forward(self, x): 62 | res_layer = getattr(self, 'layer{}'.format(self.stage + 1)) 63 | out = res_layer(x) 64 | return out 65 | 66 | def train(self, mode=True): 67 | super(ResLayer, self).train(mode) 68 | if self.norm_eval: 69 | for m in self.modules(): 70 | if isinstance(m, nn.BatchNorm2d): 71 | m.eval() 72 | -------------------------------------------------------------------------------- /mmdet/models/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from .weight_init import bias_init_with_prob 2 | 3 | __all__ = ['bias_init_with_prob'] 4 | -------------------------------------------------------------------------------- /mmdet/models/utils/weight_init.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | 3 | 4 | def bias_init_with_prob(prior_prob): 5 | """ initialize conv/fc bias value according to giving probablity""" 6 | bias_init = float(-np.log((1 - prior_prob) / prior_prob)) 7 | return bias_init 8 | -------------------------------------------------------------------------------- /mmdet/ops/__init__.py: -------------------------------------------------------------------------------- 1 | from .context_block import ContextBlock 2 | from .conv import build_conv_layer 3 | from .conv_module import ConvModule 4 | from .conv_ws import ConvWS2d, conv_ws_2d 5 | from .dcn import (DeformConv, DeformConvPack, DeformRoIPooling, 6 | DeformRoIPoolingPack, ModulatedDeformConv, 7 | ModulatedDeformConvPack, ModulatedDeformRoIPoolingPack, 8 | deform_conv, deform_roi_pooling, modulated_deform_conv) 9 | from .generalized_attention import GeneralizedAttention 10 | from .masked_conv import MaskedConv2d 11 | from .nms import nms, soft_nms 12 | from .non_local import NonLocal2D 13 | from .norm import build_norm_layer 14 | from .roi_align import RoIAlign, roi_align 15 | from .roi_pool import RoIPool, roi_pool 16 | from .scale import Scale 17 | from .sigmoid_focal_loss import SigmoidFocalLoss, sigmoid_focal_loss 18 | from .upsample import build_upsample_layer 19 | from .utils import get_compiler_version, get_compiling_cuda_version 20 | 21 | __all__ = [ 22 | 'nms', 'soft_nms', 'RoIAlign', 'roi_align', 'RoIPool', 'roi_pool', 23 | 'DeformConv', 'DeformConvPack', 'DeformRoIPooling', 'DeformRoIPoolingPack', 24 | 'ModulatedDeformRoIPoolingPack', 'ModulatedDeformConv', 25 | 'ModulatedDeformConvPack', 'deform_conv', 'modulated_deform_conv', 26 | 'deform_roi_pooling', 'SigmoidFocalLoss', 'sigmoid_focal_loss', 27 | 'MaskedConv2d', 'ContextBlock', 'GeneralizedAttention', 'NonLocal2D', 28 | 'get_compiler_version', 'get_compiling_cuda_version', 'build_conv_layer', 29 | 'ConvModule', 'ConvWS2d', 'conv_ws_2d', 'build_norm_layer', 'Scale', 30 | 'build_upsample_layer' 31 | ] 32 | -------------------------------------------------------------------------------- /mmdet/ops/activation.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | 3 | activation_cfg = { 4 | # layer_abbreviation: module 5 | 'ReLU': nn.ReLU, 6 | 'LeakyReLU': nn.LeakyReLU, 7 | 'PReLU': nn.PReLU, 8 | 'RReLU': nn.RReLU, 9 | 'ReLU6': nn.ReLU6, 10 | 'SELU': nn.SELU, 11 | 'CELU': nn.CELU 12 | } 13 | 14 | 15 | def build_activation_layer(cfg): 16 | """ Build activation layer 17 | 18 | Args: 19 | cfg (dict): cfg should contain: 20 | type (str): Identify activation layer type. 21 | layer args: args needed to instantiate a activation layer. 22 | 23 | Returns: 24 | layer (nn.Module): Created activation layer 25 | """ 26 | assert isinstance(cfg, dict) and 'type' in cfg 27 | cfg_ = cfg.copy() 28 | 29 | layer_type = cfg_.pop('type') 30 | if layer_type not in activation_cfg: 31 | raise KeyError('Unrecognized activation type {}'.format(layer_type)) 32 | else: 33 | activation = activation_cfg[layer_type] 34 | if activation is None: 35 | raise NotImplementedError 36 | 37 | layer = activation(**cfg_) 38 | return layer 39 | -------------------------------------------------------------------------------- /mmdet/ops/affine_grid/__init__.py: -------------------------------------------------------------------------------- 1 | from .affine_grid import affine_grid 2 | 3 | __all__ = ['affine_grid'] 4 | -------------------------------------------------------------------------------- /mmdet/ops/affine_grid/affine_grid.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn.functional as F 3 | from torch.autograd import Function 4 | from torch.autograd.function import once_differentiable 5 | 6 | from . import affine_grid_cuda 7 | 8 | 9 | class _AffineGridGenerator(Function): 10 | 11 | @staticmethod 12 | def forward(ctx, theta, size, align_corners): 13 | 14 | ctx.save_for_backward(theta) 15 | ctx.size = size 16 | ctx.align_corners = align_corners 17 | 18 | func = affine_grid_cuda.affine_grid_generator_forward 19 | 20 | output = func(theta, size, align_corners) 21 | 22 | return output 23 | 24 | @staticmethod 25 | @once_differentiable 26 | def backward(ctx, grad_output): 27 | theta = ctx.saved_tensors 28 | size = ctx.size 29 | align_corners = ctx.align_corners 30 | 31 | func = affine_grid_cuda.affine_grid_generator_backward 32 | 33 | grad_input = func(grad_output, theta, size, align_corners) 34 | 35 | return grad_input, None, None 36 | 37 | 38 | def affine_grid(theta, size, align_corners=False): 39 | if torch.__version__ >= '1.3': 40 | return F.affine_grid(theta, size, align_corners) 41 | elif align_corners: 42 | return F.affine_grid(theta, size) 43 | else: 44 | # enforce floating point dtype on theta 45 | if not theta.is_floating_point(): 46 | raise ValueError( 47 | 'Expected theta to have floating point type, but got {}'. 48 | format(theta.dtype)) 49 | # check that shapes and sizes match 50 | if len(size) == 4: 51 | if theta.dim() != 3 or theta.size(-2) != 2 or theta.size(-1) != 3: 52 | raise ValueError( 53 | 'Expected a batch of 2D affine matrices of shape Nx2x3 ' 54 | 'for size {}. Got {}.'.format(size, theta.shape)) 55 | elif len(size) == 5: 56 | if theta.dim() != 3 or theta.size(-2) != 3 or theta.size(-1) != 4: 57 | raise ValueError( 58 | 'Expected a batch of 3D affine matrices of shape Nx3x4 ' 59 | 'for size {}. Got {}.'.format(size, theta.shape)) 60 | else: 61 | raise NotImplementedError( 62 | 'affine_grid only supports 4D and 5D sizes, ' 63 | 'for 2D and 3D affine transforms, respectively. ' 64 | 'Got size {}.'.format(size)) 65 | if min(size) <= 0: 66 | raise ValueError( 67 | 'Expected non-zero, positive output size. Got {}'.format(size)) 68 | return _AffineGridGenerator.apply(theta, size, align_corners) 69 | -------------------------------------------------------------------------------- /mmdet/ops/carafe/__init__.py: -------------------------------------------------------------------------------- 1 | from .carafe import CARAFE, CARAFENaive, CARAFEPack, carafe, carafe_naive 2 | 3 | __all__ = ['carafe', 'carafe_naive', 'CARAFE', 'CARAFENaive', 'CARAFEPack'] 4 | -------------------------------------------------------------------------------- /mmdet/ops/carafe/grad_check.py: -------------------------------------------------------------------------------- 1 | import os.path as osp 2 | import sys 3 | 4 | import mmcv 5 | import torch 6 | from torch.autograd import gradcheck 7 | 8 | sys.path.append(osp.abspath(osp.join(__file__, '../../'))) 9 | from mmdet.ops.carafe import CARAFE, CARAFENaive # noqa: E402, isort:skip 10 | from mmdet.ops.carafe import carafe, carafe_naive # noqa: E402, isort:skip 11 | 12 | feat = torch.randn(2, 64, 3, 3, requires_grad=True, device='cuda:0').double() 13 | mask = torch.randn( 14 | 2, 100, 6, 6, requires_grad=True, device='cuda:0').sigmoid().double() 15 | 16 | print('Gradcheck for carafe...') 17 | test = gradcheck(CARAFE(5, 4, 2), (feat, mask), atol=1e-4, eps=1e-4) 18 | print(test) 19 | 20 | print('Gradcheck for carafe naive...') 21 | test = gradcheck(CARAFENaive(5, 4, 2), (feat, mask), atol=1e-4, eps=1e-4) 22 | print(test) 23 | 24 | feat = torch.randn( 25 | 2, 1024, 100, 100, requires_grad=True, device='cuda:0').float() 26 | mask = torch.randn( 27 | 2, 25, 200, 200, requires_grad=True, device='cuda:0').sigmoid().float() 28 | loop_num = 500 29 | 30 | time_forward = 0 31 | time_backward = 0 32 | bar = mmcv.ProgressBar(loop_num) 33 | timer = mmcv.Timer() 34 | for i in range(loop_num): 35 | x = carafe(feat.clone(), mask.clone(), 5, 1, 2) 36 | torch.cuda.synchronize() 37 | time_forward += timer.since_last_check() 38 | x.sum().backward(retain_graph=True) 39 | torch.cuda.synchronize() 40 | time_backward += timer.since_last_check() 41 | bar.update() 42 | print('\nCARAFE time forward: {} ms/iter | time backward: {} ms/iter'.format( 43 | (time_forward + 1e-3) * 1e3 / loop_num, 44 | (time_backward + 1e-3) * 1e3 / loop_num)) 45 | 46 | time_naive_forward = 0 47 | time_naive_backward = 0 48 | bar = mmcv.ProgressBar(loop_num) 49 | timer = mmcv.Timer() 50 | for i in range(loop_num): 51 | x = carafe_naive(feat.clone(), mask.clone(), 5, 1, 2) 52 | torch.cuda.synchronize() 53 | time_naive_forward += timer.since_last_check() 54 | x.sum().backward(retain_graph=True) 55 | torch.cuda.synchronize() 56 | time_naive_backward += timer.since_last_check() 57 | bar.update() 58 | print('\nCARAFE naive time forward: {} ms/iter | time backward: {} ms/iter'. 59 | format((time_naive_forward + 1e-3) * 1e3 / loop_num, 60 | (time_naive_backward + 1e-3) * 1e3 / loop_num)) 61 | -------------------------------------------------------------------------------- /mmdet/ops/carafe/setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | from torch.utils.cpp_extension import BuildExtension, CUDAExtension 4 | 5 | NVCC_ARGS = [ 6 | '-D__CUDA_NO_HALF_OPERATORS__', 7 | '-D__CUDA_NO_HALF_CONVERSIONS__', 8 | '-D__CUDA_NO_HALF2_OPERATORS__', 9 | ] 10 | 11 | setup( 12 | name='carafe', 13 | ext_modules=[ 14 | CUDAExtension( 15 | 'carafe_cuda', 16 | ['src/carafe_cuda.cpp', 'src/carafe_cuda_kernel.cu'], 17 | extra_compile_args={ 18 | 'cxx': [], 19 | 'nvcc': NVCC_ARGS 20 | }), 21 | CUDAExtension( 22 | 'carafe_naive_cuda', 23 | ['src/carafe_naive_cuda.cpp', 'src/carafe_naive_cuda_kernel.cu'], 24 | extra_compile_args={ 25 | 'cxx': [], 26 | 'nvcc': NVCC_ARGS 27 | }) 28 | ], 29 | cmdclass={'build_ext': BuildExtension}) 30 | -------------------------------------------------------------------------------- /mmdet/ops/carafe/src/carafe_naive_cuda.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include 5 | #include 6 | 7 | int CARAFENAIVEForwardLaucher(const at::Tensor features, const at::Tensor masks, 8 | const int kernel_size, const int group_size, 9 | const int scale_factor, const int batch_size, 10 | const int channels, const int height, 11 | const int width, at::Tensor output); 12 | 13 | int CARAFENAIVEBackwardLaucher(const at::Tensor top_grad, 14 | const at::Tensor features, 15 | const at::Tensor masks, const int kernel_size, 16 | const int group_size, const int scale_factor, 17 | const int batch_size, const int channels, 18 | const int height, const int width, 19 | at::Tensor bottom_grad, at::Tensor mask_grad); 20 | 21 | #define CHECK_CUDA(x) AT_CHECK(x.type().is_cuda(), #x, " must be a CUDAtensor ") 22 | #define CHECK_CONTIGUOUS(x) \ 23 | AT_CHECK(x.is_contiguous(), #x, " must be contiguous ") 24 | #define CHECK_INPUT(x) \ 25 | CHECK_CUDA(x); \ 26 | CHECK_CONTIGUOUS(x) 27 | 28 | int carafe_naive_forward_cuda(at::Tensor features, at::Tensor masks, 29 | int kernel_size, int group_size, int scale_factor, 30 | at::Tensor output) { 31 | CHECK_INPUT(features); 32 | CHECK_INPUT(masks); 33 | CHECK_INPUT(output); 34 | at::DeviceGuard guard(features.device()); 35 | 36 | int batch_size = output.size(0); 37 | int num_channels = output.size(1); 38 | int data_height = output.size(2); 39 | int data_width = output.size(3); 40 | 41 | CARAFENAIVEForwardLaucher(features, masks, kernel_size, group_size, 42 | scale_factor, batch_size, num_channels, data_height, 43 | data_width, output); 44 | 45 | return 1; 46 | } 47 | 48 | int carafe_naive_backward_cuda(at::Tensor top_grad, at::Tensor features, 49 | at::Tensor masks, int kernel_size, 50 | int group_size, int scale_factor, 51 | at::Tensor bottom_grad, at::Tensor mask_grad) { 52 | CHECK_INPUT(top_grad); 53 | CHECK_INPUT(features); 54 | CHECK_INPUT(masks); 55 | CHECK_INPUT(bottom_grad); 56 | CHECK_INPUT(mask_grad); 57 | at::DeviceGuard guard(top_grad.device()); 58 | 59 | int batch_size = top_grad.size(0); 60 | int num_channels = top_grad.size(1); 61 | int data_height = top_grad.size(2); 62 | int data_width = top_grad.size(3); 63 | 64 | CARAFENAIVEBackwardLaucher(top_grad, features, masks, kernel_size, group_size, 65 | scale_factor, batch_size, num_channels, 66 | data_height, data_width, bottom_grad, mask_grad); 67 | 68 | return 1; 69 | } 70 | 71 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { 72 | m.def("forward", &carafe_naive_forward_cuda, "carafe_naive forward (CUDA)"); 73 | m.def("backward", &carafe_naive_backward_cuda, 74 | "carafe_naive backward (CUDA)"); 75 | } 76 | -------------------------------------------------------------------------------- /mmdet/ops/conv.py: -------------------------------------------------------------------------------- 1 | from torch import nn as nn 2 | 3 | from .conv_ws import ConvWS2d 4 | from .dcn import DeformConvPack, ModulatedDeformConvPack 5 | 6 | conv_cfg = { 7 | 'Conv': nn.Conv2d, 8 | 'ConvWS': ConvWS2d, 9 | 'DCN': DeformConvPack, 10 | 'DCNv2': ModulatedDeformConvPack, 11 | # TODO: octave conv 12 | } 13 | 14 | 15 | def build_conv_layer(cfg, *args, **kwargs): 16 | """ Build convolution layer 17 | 18 | Args: 19 | cfg (None or dict): cfg should contain: 20 | type (str): identify conv layer type. 21 | layer args: args needed to instantiate a conv layer. 22 | 23 | Returns: 24 | layer (nn.Module): created conv layer 25 | """ 26 | if cfg is None: 27 | cfg_ = dict(type='Conv') 28 | else: 29 | assert isinstance(cfg, dict) and 'type' in cfg 30 | cfg_ = cfg.copy() 31 | 32 | layer_type = cfg_.pop('type') 33 | if layer_type not in conv_cfg: 34 | raise KeyError('Unrecognized norm type {}'.format(layer_type)) 35 | else: 36 | conv_layer = conv_cfg[layer_type] 37 | 38 | layer = conv_layer(*args, **kwargs, **cfg_) 39 | 40 | return layer 41 | -------------------------------------------------------------------------------- /mmdet/ops/conv_ws.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch.nn.functional as F 3 | 4 | 5 | def conv_ws_2d(input, 6 | weight, 7 | bias=None, 8 | stride=1, 9 | padding=0, 10 | dilation=1, 11 | groups=1, 12 | eps=1e-5): 13 | c_in = weight.size(0) 14 | weight_flat = weight.view(c_in, -1) 15 | mean = weight_flat.mean(dim=1, keepdim=True).view(c_in, 1, 1, 1) 16 | std = weight_flat.std(dim=1, keepdim=True).view(c_in, 1, 1, 1) 17 | weight = (weight - mean) / (std + eps) 18 | return F.conv2d(input, weight, bias, stride, padding, dilation, groups) 19 | 20 | 21 | class ConvWS2d(nn.Conv2d): 22 | 23 | def __init__(self, 24 | in_channels, 25 | out_channels, 26 | kernel_size, 27 | stride=1, 28 | padding=0, 29 | dilation=1, 30 | groups=1, 31 | bias=True, 32 | eps=1e-5): 33 | super(ConvWS2d, self).__init__( 34 | in_channels, 35 | out_channels, 36 | kernel_size, 37 | stride=stride, 38 | padding=padding, 39 | dilation=dilation, 40 | groups=groups, 41 | bias=bias) 42 | self.eps = eps 43 | 44 | def forward(self, x): 45 | return conv_ws_2d(x, self.weight, self.bias, self.stride, self.padding, 46 | self.dilation, self.groups, self.eps) 47 | -------------------------------------------------------------------------------- /mmdet/ops/dcn/__init__.py: -------------------------------------------------------------------------------- 1 | from .deform_conv import (DeformConv, DeformConvPack, ModulatedDeformConv, 2 | ModulatedDeformConvPack, deform_conv, 3 | modulated_deform_conv) 4 | from .deform_pool import (DeformRoIPooling, DeformRoIPoolingPack, 5 | ModulatedDeformRoIPoolingPack, deform_roi_pooling) 6 | 7 | __all__ = [ 8 | 'DeformConv', 'DeformConvPack', 'ModulatedDeformConv', 9 | 'ModulatedDeformConvPack', 'DeformRoIPooling', 'DeformRoIPoolingPack', 10 | 'ModulatedDeformRoIPoolingPack', 'deform_conv', 'modulated_deform_conv', 11 | 'deform_roi_pooling' 12 | ] 13 | -------------------------------------------------------------------------------- /mmdet/ops/grid_sampler/__init__.py: -------------------------------------------------------------------------------- 1 | from .grid_sampler import grid_sample 2 | 3 | __all__ = ['grid_sample'] 4 | -------------------------------------------------------------------------------- /mmdet/ops/masked_conv/__init__.py: -------------------------------------------------------------------------------- 1 | from .masked_conv import MaskedConv2d, masked_conv2d 2 | 3 | __all__ = ['masked_conv2d', 'MaskedConv2d'] 4 | -------------------------------------------------------------------------------- /mmdet/ops/masked_conv/src/masked_conv2d_cuda.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | int MaskedIm2colForwardLaucher(const at::Tensor im, const int height, 7 | const int width, const int channels, 8 | const int kernel_h, const int kernel_w, 9 | const int pad_h, const int pad_w, 10 | const at::Tensor mask_h_idx, 11 | const at::Tensor mask_w_idx, const int mask_cnt, 12 | at::Tensor col); 13 | 14 | int MaskedCol2imForwardLaucher(const at::Tensor col, const int height, 15 | const int width, const int channels, 16 | const at::Tensor mask_h_idx, 17 | const at::Tensor mask_w_idx, const int mask_cnt, 18 | at::Tensor im); 19 | 20 | #define CHECK_CUDA(x) AT_CHECK(x.type().is_cuda(), #x, " must be a CUDAtensor ") 21 | #define CHECK_CONTIGUOUS(x) \ 22 | AT_CHECK(x.is_contiguous(), #x, " must be contiguous ") 23 | #define CHECK_INPUT(x) \ 24 | CHECK_CUDA(x); \ 25 | CHECK_CONTIGUOUS(x) 26 | 27 | int masked_im2col_forward_cuda(const at::Tensor im, const at::Tensor mask_h_idx, 28 | const at::Tensor mask_w_idx, const int kernel_h, 29 | const int kernel_w, const int pad_h, 30 | const int pad_w, at::Tensor col) { 31 | CHECK_INPUT(im); 32 | CHECK_INPUT(mask_h_idx); 33 | CHECK_INPUT(mask_w_idx); 34 | CHECK_INPUT(col); 35 | // im: (n, ic, h, w), kernel size (kh, kw) 36 | // kernel: (oc, ic * kh * kw), col: (kh * kw * ic, ow * oh) 37 | at::DeviceGuard guard(im.device()); 38 | 39 | int channels = im.size(1); 40 | int height = im.size(2); 41 | int width = im.size(3); 42 | int mask_cnt = mask_h_idx.size(0); 43 | 44 | MaskedIm2colForwardLaucher(im, height, width, channels, kernel_h, kernel_w, 45 | pad_h, pad_w, mask_h_idx, mask_w_idx, mask_cnt, 46 | col); 47 | 48 | return 1; 49 | } 50 | 51 | int masked_col2im_forward_cuda(const at::Tensor col, 52 | const at::Tensor mask_h_idx, 53 | const at::Tensor mask_w_idx, int height, 54 | int width, int channels, at::Tensor im) { 55 | CHECK_INPUT(col); 56 | CHECK_INPUT(mask_h_idx); 57 | CHECK_INPUT(mask_w_idx); 58 | CHECK_INPUT(im); 59 | // im: (n, ic, h, w), kernel size (kh, kw) 60 | // kernel: (oc, ic * kh * kh), col: (kh * kw * ic, ow * oh) 61 | at::DeviceGuard guard(col.device()); 62 | 63 | int mask_cnt = mask_h_idx.size(0); 64 | 65 | MaskedCol2imForwardLaucher(col, height, width, channels, mask_h_idx, 66 | mask_w_idx, mask_cnt, im); 67 | 68 | return 1; 69 | } 70 | 71 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { 72 | m.def("masked_im2col_forward", &masked_im2col_forward_cuda, 73 | "masked_im2col forward (CUDA)"); 74 | m.def("masked_col2im_forward", &masked_col2im_forward_cuda, 75 | "masked_col2im forward (CUDA)"); 76 | } 77 | -------------------------------------------------------------------------------- /mmdet/ops/nms/__init__.py: -------------------------------------------------------------------------------- 1 | from .nms_wrapper import nms, soft_nms 2 | 3 | __all__ = ['nms', 'soft_nms'] 4 | -------------------------------------------------------------------------------- /mmdet/ops/nms/src/nms_cuda.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | #include 3 | 4 | #define CHECK_CUDA(x) AT_CHECK(x.type().is_cuda(), #x, " must be a CUDAtensor ") 5 | 6 | at::Tensor nms_cuda(const at::Tensor boxes, float nms_overlap_thresh); 7 | 8 | at::Tensor nms(const at::Tensor& dets, const float threshold) { 9 | CHECK_CUDA(dets); 10 | if (dets.numel() == 0) 11 | return at::empty({0}, dets.options().dtype(at::kLong).device(at::kCPU)); 12 | return nms_cuda(dets, threshold); 13 | } 14 | 15 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { 16 | m.def("nms", &nms, "non-maximum suppression"); 17 | } 18 | -------------------------------------------------------------------------------- /mmdet/ops/norm.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | 3 | norm_cfg = { 4 | # format: layer_type: (abbreviation, module) 5 | 'BN': ('bn', nn.BatchNorm2d), 6 | 'SyncBN': ('bn', nn.SyncBatchNorm), 7 | 'GN': ('gn', nn.GroupNorm), 8 | # and potentially 'SN' 9 | } 10 | 11 | 12 | def build_norm_layer(cfg, num_features, postfix=''): 13 | """ Build normalization layer 14 | 15 | Args: 16 | cfg (dict): cfg should contain: 17 | type (str): identify norm layer type. 18 | layer args: args needed to instantiate a norm layer. 19 | requires_grad (bool): [optional] whether stop gradient updates 20 | num_features (int): number of channels from input. 21 | postfix (int, str): appended into norm abbreviation to 22 | create named layer. 23 | 24 | Returns: 25 | name (str): abbreviation + postfix 26 | layer (nn.Module): created norm layer 27 | """ 28 | assert isinstance(cfg, dict) and 'type' in cfg 29 | cfg_ = cfg.copy() 30 | 31 | layer_type = cfg_.pop('type') 32 | if layer_type not in norm_cfg: 33 | raise KeyError('Unrecognized norm type {}'.format(layer_type)) 34 | else: 35 | abbr, norm_layer = norm_cfg[layer_type] 36 | if norm_layer is None: 37 | raise NotImplementedError 38 | 39 | assert isinstance(postfix, (int, str)) 40 | name = abbr + str(postfix) 41 | 42 | requires_grad = cfg_.pop('requires_grad', True) 43 | cfg_.setdefault('eps', 1e-5) 44 | if layer_type != 'GN': 45 | layer = norm_layer(num_features, **cfg_) 46 | if layer_type == 'SyncBN': 47 | layer._specify_ddp_gpu_num(1) 48 | else: 49 | assert 'num_groups' in cfg_ 50 | layer = norm_layer(num_channels=num_features, **cfg_) 51 | 52 | for param in layer.parameters(): 53 | param.requires_grad = requires_grad 54 | 55 | return name, layer 56 | -------------------------------------------------------------------------------- /mmdet/ops/roi_align/__init__.py: -------------------------------------------------------------------------------- 1 | from .roi_align import RoIAlign, roi_align 2 | 3 | __all__ = ['roi_align', 'RoIAlign'] 4 | -------------------------------------------------------------------------------- /mmdet/ops/roi_align/gradcheck.py: -------------------------------------------------------------------------------- 1 | import os.path as osp 2 | import sys 3 | 4 | import numpy as np 5 | import torch 6 | from torch.autograd import gradcheck 7 | 8 | sys.path.append(osp.abspath(osp.join(__file__, '../../'))) 9 | from roi_align import RoIAlign # noqa: E402, isort:skip 10 | 11 | feat_size = 15 12 | spatial_scale = 1.0 / 8 13 | img_size = feat_size / spatial_scale 14 | num_imgs = 2 15 | num_rois = 20 16 | 17 | batch_ind = np.random.randint(num_imgs, size=(num_rois, 1)) 18 | rois = np.random.rand(num_rois, 4) * img_size * 0.5 19 | rois[:, 2:] += img_size * 0.5 20 | rois = np.hstack((batch_ind, rois)) 21 | 22 | feat = torch.randn( 23 | num_imgs, 16, feat_size, feat_size, requires_grad=True, device='cuda:0') 24 | rois = torch.from_numpy(rois).float().cuda() 25 | inputs = (feat, rois) 26 | print('Gradcheck for roi align...') 27 | test = gradcheck(RoIAlign(3, spatial_scale), inputs, atol=1e-3, eps=1e-3) 28 | print(test) 29 | test = gradcheck(RoIAlign(3, spatial_scale, 2), inputs, atol=1e-3, eps=1e-3) 30 | print(test) 31 | -------------------------------------------------------------------------------- /mmdet/ops/roi_pool/__init__.py: -------------------------------------------------------------------------------- 1 | from .roi_pool import RoIPool, roi_pool 2 | 3 | __all__ = ['roi_pool', 'RoIPool'] 4 | -------------------------------------------------------------------------------- /mmdet/ops/roi_pool/gradcheck.py: -------------------------------------------------------------------------------- 1 | import os.path as osp 2 | import sys 3 | 4 | import torch 5 | from torch.autograd import gradcheck 6 | 7 | sys.path.append(osp.abspath(osp.join(__file__, '../../'))) 8 | from roi_pool import RoIPool # noqa: E402, isort:skip 9 | 10 | feat = torch.randn(4, 16, 15, 15, requires_grad=True).cuda() 11 | rois = torch.Tensor([[0, 0, 0, 50, 50], [0, 10, 30, 43, 55], 12 | [1, 67, 40, 110, 120]]).cuda() 13 | inputs = (feat, rois) 14 | print('Gradcheck for roi pooling...') 15 | test = gradcheck(RoIPool(4, 1.0 / 8), inputs, eps=1e-5, atol=1e-3) 16 | print(test) 17 | -------------------------------------------------------------------------------- /mmdet/ops/roi_pool/roi_pool.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from torch.autograd import Function 4 | from torch.autograd.function import once_differentiable 5 | from torch.nn.modules.utils import _pair 6 | 7 | from . import roi_pool_cuda 8 | 9 | 10 | class RoIPoolFunction(Function): 11 | 12 | @staticmethod 13 | def forward(ctx, features, rois, out_size, spatial_scale): 14 | assert features.is_cuda 15 | out_h, out_w = _pair(out_size) 16 | assert isinstance(out_h, int) and isinstance(out_w, int) 17 | ctx.save_for_backward(rois) 18 | num_channels = features.size(1) 19 | num_rois = rois.size(0) 20 | out_size = (num_rois, num_channels, out_h, out_w) 21 | output = features.new_zeros(out_size) 22 | argmax = features.new_zeros(out_size, dtype=torch.int) 23 | roi_pool_cuda.forward(features, rois, out_h, out_w, spatial_scale, 24 | output, argmax) 25 | ctx.spatial_scale = spatial_scale 26 | ctx.feature_size = features.size() 27 | ctx.argmax = argmax 28 | 29 | return output 30 | 31 | @staticmethod 32 | @once_differentiable 33 | def backward(ctx, grad_output): 34 | assert grad_output.is_cuda 35 | spatial_scale = ctx.spatial_scale 36 | feature_size = ctx.feature_size 37 | argmax = ctx.argmax 38 | rois = ctx.saved_tensors[0] 39 | assert feature_size is not None 40 | 41 | grad_input = grad_rois = None 42 | if ctx.needs_input_grad[0]: 43 | grad_input = grad_output.new_zeros(feature_size) 44 | roi_pool_cuda.backward(grad_output.contiguous(), rois, argmax, 45 | spatial_scale, grad_input) 46 | 47 | return grad_input, grad_rois, None, None 48 | 49 | 50 | roi_pool = RoIPoolFunction.apply 51 | 52 | 53 | class RoIPool(nn.Module): 54 | 55 | def __init__(self, out_size, spatial_scale, use_torchvision=False): 56 | super(RoIPool, self).__init__() 57 | 58 | self.out_size = _pair(out_size) 59 | self.spatial_scale = float(spatial_scale) 60 | self.use_torchvision = use_torchvision 61 | 62 | def forward(self, features, rois): 63 | if self.use_torchvision: 64 | from torchvision.ops import roi_pool as tv_roi_pool 65 | return tv_roi_pool(features, rois, self.out_size, 66 | self.spatial_scale) 67 | else: 68 | return roi_pool(features, rois, self.out_size, self.spatial_scale) 69 | 70 | def __repr__(self): 71 | format_str = self.__class__.__name__ 72 | format_str += '(out_size={}, spatial_scale={}'.format( 73 | self.out_size, self.spatial_scale) 74 | format_str += ', use_torchvision={})'.format(self.use_torchvision) 75 | return format_str 76 | -------------------------------------------------------------------------------- /mmdet/ops/roi_pool/src/roi_pool_cuda.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | #include 5 | 6 | int ROIPoolForwardLaucher(const at::Tensor features, const at::Tensor rois, 7 | const float spatial_scale, const int channels, 8 | const int height, const int width, const int num_rois, 9 | const int pooled_h, const int pooled_w, 10 | at::Tensor output, at::Tensor argmax); 11 | 12 | int ROIPoolBackwardLaucher(const at::Tensor top_grad, const at::Tensor rois, 13 | const at::Tensor argmax, const float spatial_scale, 14 | const int batch_size, const int channels, 15 | const int height, const int width, 16 | const int num_rois, const int pooled_h, 17 | const int pooled_w, at::Tensor bottom_grad); 18 | 19 | #define CHECK_CUDA(x) AT_CHECK(x.type().is_cuda(), #x, " must be a CUDAtensor ") 20 | #define CHECK_CONTIGUOUS(x) \ 21 | AT_CHECK(x.is_contiguous(), #x, " must be contiguous ") 22 | #define CHECK_INPUT(x) \ 23 | CHECK_CUDA(x); \ 24 | CHECK_CONTIGUOUS(x) 25 | 26 | int roi_pooling_forward_cuda(at::Tensor features, at::Tensor rois, 27 | int pooled_height, int pooled_width, 28 | float spatial_scale, at::Tensor output, 29 | at::Tensor argmax) { 30 | CHECK_INPUT(features); 31 | CHECK_INPUT(rois); 32 | CHECK_INPUT(output); 33 | CHECK_INPUT(argmax); 34 | at::DeviceGuard guard(features.device()); 35 | 36 | // Number of ROIs 37 | int num_rois = rois.size(0); 38 | int size_rois = rois.size(1); 39 | 40 | if (size_rois != 5) { 41 | printf("wrong roi size\n"); 42 | return 0; 43 | } 44 | 45 | int channels = features.size(1); 46 | int height = features.size(2); 47 | int width = features.size(3); 48 | 49 | ROIPoolForwardLaucher(features, rois, spatial_scale, channels, height, width, 50 | num_rois, pooled_height, pooled_width, output, argmax); 51 | 52 | return 1; 53 | } 54 | 55 | int roi_pooling_backward_cuda(at::Tensor top_grad, at::Tensor rois, 56 | at::Tensor argmax, float spatial_scale, 57 | at::Tensor bottom_grad) { 58 | CHECK_INPUT(top_grad); 59 | CHECK_INPUT(rois); 60 | CHECK_INPUT(argmax); 61 | CHECK_INPUT(bottom_grad); 62 | at::DeviceGuard guard(top_grad.device()); 63 | 64 | int pooled_height = top_grad.size(2); 65 | int pooled_width = top_grad.size(3); 66 | int num_rois = rois.size(0); 67 | int size_rois = rois.size(1); 68 | 69 | if (size_rois != 5) { 70 | printf("wrong roi size\n"); 71 | return 0; 72 | } 73 | int batch_size = bottom_grad.size(0); 74 | int channels = bottom_grad.size(1); 75 | int height = bottom_grad.size(2); 76 | int width = bottom_grad.size(3); 77 | 78 | ROIPoolBackwardLaucher(top_grad, rois, argmax, spatial_scale, batch_size, 79 | channels, height, width, num_rois, pooled_height, 80 | pooled_width, bottom_grad); 81 | 82 | return 1; 83 | } 84 | 85 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { 86 | m.def("forward", &roi_pooling_forward_cuda, "Roi_Pooling forward (CUDA)"); 87 | m.def("backward", &roi_pooling_backward_cuda, "Roi_Pooling backward (CUDA)"); 88 | } 89 | -------------------------------------------------------------------------------- /mmdet/ops/scale.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | 4 | 5 | class Scale(nn.Module): 6 | """ 7 | A learnable scale parameter 8 | """ 9 | 10 | def __init__(self, scale=1.0): 11 | super(Scale, self).__init__() 12 | self.scale = nn.Parameter(torch.tensor(scale, dtype=torch.float)) 13 | 14 | def forward(self, x): 15 | return x * self.scale 16 | -------------------------------------------------------------------------------- /mmdet/ops/sigmoid_focal_loss/__init__.py: -------------------------------------------------------------------------------- 1 | from .sigmoid_focal_loss import SigmoidFocalLoss, sigmoid_focal_loss 2 | 3 | __all__ = ['SigmoidFocalLoss', 'sigmoid_focal_loss'] 4 | -------------------------------------------------------------------------------- /mmdet/ops/sigmoid_focal_loss/sigmoid_focal_loss.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | from torch.autograd import Function 3 | from torch.autograd.function import once_differentiable 4 | 5 | from . import sigmoid_focal_loss_cuda 6 | 7 | 8 | class SigmoidFocalLossFunction(Function): 9 | 10 | @staticmethod 11 | def forward(ctx, input, target, gamma=2.0, alpha=0.25): 12 | ctx.save_for_backward(input, target) 13 | num_classes = input.shape[1] 14 | ctx.num_classes = num_classes 15 | ctx.gamma = gamma 16 | ctx.alpha = alpha 17 | 18 | loss = sigmoid_focal_loss_cuda.forward(input, target, num_classes, 19 | gamma, alpha) 20 | return loss 21 | 22 | @staticmethod 23 | @once_differentiable 24 | def backward(ctx, d_loss): 25 | input, target = ctx.saved_tensors 26 | num_classes = ctx.num_classes 27 | gamma = ctx.gamma 28 | alpha = ctx.alpha 29 | d_loss = d_loss.contiguous() 30 | d_input = sigmoid_focal_loss_cuda.backward(input, target, d_loss, 31 | num_classes, gamma, alpha) 32 | return d_input, None, None, None, None 33 | 34 | 35 | sigmoid_focal_loss = SigmoidFocalLossFunction.apply 36 | 37 | 38 | # TODO: remove this module 39 | class SigmoidFocalLoss(nn.Module): 40 | 41 | def __init__(self, gamma, alpha): 42 | super(SigmoidFocalLoss, self).__init__() 43 | self.gamma = gamma 44 | self.alpha = alpha 45 | 46 | def forward(self, logits, targets): 47 | assert logits.is_cuda 48 | loss = sigmoid_focal_loss(logits, targets, self.gamma, self.alpha) 49 | return loss.sum() 50 | 51 | def __repr__(self): 52 | tmpstr = self.__class__.__name__ + '(gamma={}, alpha={})'.format( 53 | self.gamma, self.alpha) 54 | return tmpstr 55 | -------------------------------------------------------------------------------- /mmdet/ops/sigmoid_focal_loss/src/sigmoid_focal_loss.cpp: -------------------------------------------------------------------------------- 1 | // modify from 2 | // https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/maskrcnn_benchmark/csrc/SigmoidFocalLoss.h 3 | #include 4 | 5 | at::Tensor SigmoidFocalLoss_forward_cuda(const at::Tensor &logits, 6 | const at::Tensor &targets, 7 | const int num_classes, 8 | const float gamma, const float alpha); 9 | 10 | at::Tensor SigmoidFocalLoss_backward_cuda(const at::Tensor &logits, 11 | const at::Tensor &targets, 12 | const at::Tensor &d_losses, 13 | const int num_classes, 14 | const float gamma, const float alpha); 15 | 16 | // Interface for Python 17 | at::Tensor SigmoidFocalLoss_forward(const at::Tensor &logits, 18 | const at::Tensor &targets, 19 | const int num_classes, const float gamma, 20 | const float alpha) { 21 | if (logits.type().is_cuda()) { 22 | at::DeviceGuard guard(logits.device()); 23 | return SigmoidFocalLoss_forward_cuda(logits, targets, num_classes, gamma, 24 | alpha); 25 | } 26 | AT_ERROR("SigmoidFocalLoss is not implemented on the CPU"); 27 | } 28 | 29 | at::Tensor SigmoidFocalLoss_backward(const at::Tensor &logits, 30 | const at::Tensor &targets, 31 | const at::Tensor &d_losses, 32 | const int num_classes, const float gamma, 33 | const float alpha) { 34 | if (logits.type().is_cuda()) { 35 | at::DeviceGuard guard(logits.device()); 36 | return SigmoidFocalLoss_backward_cuda(logits, targets, d_losses, 37 | num_classes, gamma, alpha); 38 | } 39 | AT_ERROR("SigmoidFocalLoss is not implemented on the CPU"); 40 | } 41 | 42 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { 43 | m.def("forward", &SigmoidFocalLoss_forward, 44 | "SigmoidFocalLoss forward (CUDA)"); 45 | m.def("backward", &SigmoidFocalLoss_backward, 46 | "SigmoidFocalLoss backward (CUDA)"); 47 | } 48 | -------------------------------------------------------------------------------- /mmdet/ops/upsample.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch.nn.functional as F 3 | from mmcv.cnn import xavier_init 4 | 5 | from .carafe import CARAFEPack 6 | 7 | 8 | class PixelShufflePack(nn.Module): 9 | """ Pixel Shuffle upsample layer 10 | 11 | Args: 12 | in_channels (int): Number of input channels 13 | out_channels (int): Number of output channels 14 | scale_factor (int): Upsample ratio 15 | upsample_kernel (int): Kernel size of Conv layer to expand the channels 16 | 17 | Returns: 18 | upsampled feature map 19 | """ 20 | 21 | def __init__(self, in_channels, out_channels, scale_factor, 22 | upsample_kernel): 23 | super(PixelShufflePack, self).__init__() 24 | self.in_channels = in_channels 25 | self.out_channels = out_channels 26 | self.scale_factor = scale_factor 27 | self.upsample_kernel = upsample_kernel 28 | self.upsample_conv = nn.Conv2d( 29 | self.in_channels, 30 | self.out_channels * scale_factor * scale_factor, 31 | self.upsample_kernel, 32 | padding=(self.upsample_kernel - 1) // 2) 33 | self.init_weights() 34 | 35 | def init_weights(self): 36 | xavier_init(self.upsample_conv, distribution='uniform') 37 | 38 | def forward(self, x): 39 | x = self.upsample_conv(x) 40 | x = F.pixel_shuffle(x, self.scale_factor) 41 | return x 42 | 43 | 44 | upsample_cfg = { 45 | # layer_abbreviation: module 46 | 'nearest': nn.Upsample, 47 | 'bilinear': nn.Upsample, 48 | 'deconv': nn.ConvTranspose2d, 49 | 'pixel_shuffle': PixelShufflePack, 50 | 'carafe': CARAFEPack 51 | } 52 | 53 | 54 | def build_upsample_layer(cfg): 55 | """ Build upsample layer 56 | 57 | Args: 58 | cfg (dict): cfg should contain: 59 | type (str): Identify upsample layer type. 60 | upsample ratio (int): Upsample ratio 61 | layer args: args needed to instantiate a upsample layer. 62 | 63 | Returns: 64 | layer (nn.Module): Created upsample layer 65 | """ 66 | assert isinstance(cfg, dict) and 'type' in cfg 67 | cfg_ = cfg.copy() 68 | 69 | layer_type = cfg_.pop('type') 70 | if layer_type not in upsample_cfg: 71 | raise KeyError('Unrecognized upsample type {}'.format(layer_type)) 72 | else: 73 | upsample = upsample_cfg[layer_type] 74 | if upsample is None: 75 | raise NotImplementedError 76 | 77 | layer = upsample(**cfg_) 78 | return layer 79 | -------------------------------------------------------------------------------- /mmdet/ops/utils/__init__.py: -------------------------------------------------------------------------------- 1 | # from . import compiling_info 2 | from .compiling_info import get_compiler_version, get_compiling_cuda_version 3 | 4 | # get_compiler_version = compiling_info.get_compiler_version 5 | # get_compiling_cuda_version = compiling_info.get_compiling_cuda_version 6 | 7 | __all__ = ['get_compiler_version', 'get_compiling_cuda_version'] 8 | -------------------------------------------------------------------------------- /mmdet/ops/utils/src/compiling_info.cpp: -------------------------------------------------------------------------------- 1 | // modified from 2 | // https://github.com/facebookresearch/detectron2/blob/master/detectron2/layers/csrc/vision.cpp 3 | #include 4 | #include 5 | 6 | #ifdef WITH_CUDA 7 | int get_cudart_version() { return CUDART_VERSION; } 8 | #endif 9 | 10 | std::string get_compiling_cuda_version() { 11 | #ifdef WITH_CUDA 12 | std::ostringstream oss; 13 | 14 | // copied from 15 | // https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/cuda/detail/CUDAHooks.cpp#L231 16 | auto printCudaStyleVersion = [&](int v) { 17 | oss << (v / 1000) << "." << (v / 10 % 100); 18 | if (v % 10 != 0) { 19 | oss << "." << (v % 10); 20 | } 21 | }; 22 | printCudaStyleVersion(get_cudart_version()); 23 | return oss.str(); 24 | #else 25 | return std::string("not available"); 26 | #endif 27 | } 28 | 29 | // similar to 30 | // https://github.com/pytorch/pytorch/blob/master/aten/src/ATen/Version.cpp 31 | std::string get_compiler_version() { 32 | std::ostringstream ss; 33 | #if defined(__GNUC__) 34 | #ifndef __clang__ 35 | { ss << "GCC " << __GNUC__ << "." << __GNUC_MINOR__; } 36 | #endif 37 | #endif 38 | 39 | #if defined(__clang_major__) 40 | { 41 | ss << "clang " << __clang_major__ << "." << __clang_minor__ << "." 42 | << __clang_patchlevel__; 43 | } 44 | #endif 45 | 46 | #if defined(_MSC_VER) 47 | { ss << "MSVC " << _MSC_FULL_VER; } 48 | #endif 49 | return ss.str(); 50 | } 51 | 52 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { 53 | m.def("get_compiler_version", &get_compiler_version, "get_compiler_version"); 54 | m.def("get_compiling_cuda_version", &get_compiling_cuda_version, 55 | "get_compiling_cuda_version"); 56 | } 57 | -------------------------------------------------------------------------------- /mmdet/utils/__init__.py: -------------------------------------------------------------------------------- 1 | from .collect_env import collect_env 2 | from .flops_counter import get_model_complexity_info 3 | from .logger import get_root_logger, print_log 4 | from .registry import Registry, build_from_cfg 5 | 6 | __all__ = [ 7 | 'Registry', 'build_from_cfg', 'get_model_complexity_info', 8 | 'get_root_logger', 'print_log', 'collect_env' 9 | ] 10 | -------------------------------------------------------------------------------- /mmdet/utils/collect_env.py: -------------------------------------------------------------------------------- 1 | import os.path as osp 2 | import subprocess 3 | import sys 4 | from collections import defaultdict 5 | 6 | import cv2 7 | import mmcv 8 | import torch 9 | import torchvision 10 | 11 | import mmdet 12 | 13 | 14 | def collect_env(): 15 | env_info = {} 16 | env_info['sys.platform'] = sys.platform 17 | env_info['Python'] = sys.version.replace('\n', '') 18 | 19 | cuda_available = torch.cuda.is_available() 20 | env_info['CUDA available'] = cuda_available 21 | 22 | if cuda_available: 23 | from torch.utils.cpp_extension import CUDA_HOME 24 | env_info['CUDA_HOME'] = CUDA_HOME 25 | 26 | if CUDA_HOME is not None and osp.isdir(CUDA_HOME): 27 | try: 28 | nvcc = osp.join(CUDA_HOME, 'bin/nvcc') 29 | nvcc = subprocess.check_output( 30 | '"{}" -V | tail -n1'.format(nvcc), shell=True) 31 | nvcc = nvcc.decode('utf-8').strip() 32 | except subprocess.SubprocessError: 33 | nvcc = 'Not Available' 34 | env_info['NVCC'] = nvcc 35 | 36 | devices = defaultdict(list) 37 | for k in range(torch.cuda.device_count()): 38 | devices[torch.cuda.get_device_name(k)].append(str(k)) 39 | for name, devids in devices.items(): 40 | env_info['GPU ' + ','.join(devids)] = name 41 | 42 | gcc = subprocess.check_output('gcc --version | head -n1', shell=True) 43 | gcc = gcc.decode('utf-8').strip() 44 | env_info['GCC'] = gcc 45 | 46 | env_info['PyTorch'] = torch.__version__ 47 | env_info['PyTorch compiling details'] = torch.__config__.show() 48 | 49 | env_info['TorchVision'] = torchvision.__version__ 50 | 51 | env_info['OpenCV'] = cv2.__version__ 52 | 53 | env_info['MMCV'] = mmcv.__version__ 54 | env_info['MMDetection'] = mmdet.__version__ 55 | from mmdet.ops import get_compiler_version, get_compiling_cuda_version 56 | env_info['MMDetection Compiler'] = get_compiler_version() 57 | env_info['MMDetection CUDA Compiler'] = get_compiling_cuda_version() 58 | return env_info 59 | 60 | 61 | if __name__ == '__main__': 62 | for name, val in collect_env().items(): 63 | print('{}: {}'.format(name, val)) 64 | -------------------------------------------------------------------------------- /mmdet/utils/logger.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | from mmcv.runner import get_dist_info 4 | 5 | 6 | def get_root_logger(log_file=None, log_level=logging.INFO): 7 | """Get the root logger. 8 | 9 | The logger will be initialized if it has not been initialized. By default a 10 | StreamHandler will be added. If `log_file` is specified, a FileHandler will 11 | also be added. The name of the root logger is the top-level package name, 12 | e.g., "mmdet". 13 | 14 | Args: 15 | log_file (str | None): The log filename. If specified, a FileHandler 16 | will be added to the root logger. 17 | log_level (int): The root logger level. Note that only the process of 18 | rank 0 is affected, while other processes will set the level to 19 | "Error" and be silent most of the time. 20 | 21 | Returns: 22 | logging.Logger: The root logger. 23 | """ 24 | logger = logging.getLogger(__name__.split('.')[0]) # i.e., mmdet 25 | # if the logger has been initialized, just return it 26 | if logger.hasHandlers(): 27 | return logger 28 | 29 | format_str = '%(asctime)s - %(name)s - %(levelname)s - %(message)s' 30 | logging.basicConfig(format=format_str, level=log_level) 31 | rank, _ = get_dist_info() 32 | if rank != 0: 33 | logger.setLevel('ERROR') 34 | elif log_file is not None: 35 | file_handler = logging.FileHandler(log_file, 'w') 36 | file_handler.setFormatter(logging.Formatter(format_str)) 37 | file_handler.setLevel(log_level) 38 | logger.addHandler(file_handler) 39 | 40 | return logger 41 | 42 | 43 | def print_log(msg, logger=None, level=logging.INFO): 44 | """Print a log message. 45 | 46 | Args: 47 | msg (str): The message to be logged. 48 | logger (logging.Logger | str | None): The logger to be used. Some 49 | special loggers are: 50 | - "root": the root logger obtained with `get_root_logger()`. 51 | - "silent": no message will be printed. 52 | - None: The `print()` method will be used to print log messages. 53 | level (int): Logging level. Only available when `logger` is a Logger 54 | object or "root". 55 | """ 56 | if logger is None: 57 | print(msg) 58 | elif logger == 'root': 59 | _logger = get_root_logger() 60 | _logger.log(level, msg) 61 | elif isinstance(logger, logging.Logger): 62 | logger.log(level, msg) 63 | elif logger != 'silent': 64 | raise TypeError( 65 | 'logger should be either a logging.Logger object, "root", ' 66 | '"silent" or None, but got {}'.format(logger)) 67 | -------------------------------------------------------------------------------- /mmdet/utils/profiling.py: -------------------------------------------------------------------------------- 1 | import contextlib 2 | import sys 3 | import time 4 | 5 | import torch 6 | 7 | if sys.version_info >= (3, 7): 8 | 9 | @contextlib.contextmanager 10 | def profile_time(trace_name, 11 | name, 12 | enabled=True, 13 | stream=None, 14 | end_stream=None): 15 | """Print time spent by CPU and GPU. 16 | 17 | Useful as a temporary context manager to find sweet spots of 18 | code suitable for async implementation. 19 | 20 | """ 21 | if (not enabled) or not torch.cuda.is_available(): 22 | yield 23 | return 24 | stream = stream if stream else torch.cuda.current_stream() 25 | end_stream = end_stream if end_stream else stream 26 | start = torch.cuda.Event(enable_timing=True) 27 | end = torch.cuda.Event(enable_timing=True) 28 | stream.record_event(start) 29 | try: 30 | cpu_start = time.monotonic() 31 | yield 32 | finally: 33 | cpu_end = time.monotonic() 34 | end_stream.record_event(end) 35 | end.synchronize() 36 | cpu_time = (cpu_end - cpu_start) * 1000 37 | gpu_time = start.elapsed_time(end) 38 | msg = '{} {} cpu_time {:.2f} ms '.format(trace_name, name, 39 | cpu_time) 40 | msg += 'gpu_time {:.2f} ms stream {}'.format(gpu_time, stream) 41 | print(msg, end_stream) 42 | -------------------------------------------------------------------------------- /mmdet/utils/registry.py: -------------------------------------------------------------------------------- 1 | import inspect 2 | from functools import partial 3 | 4 | import mmcv 5 | 6 | 7 | class Registry(object): 8 | 9 | def __init__(self, name): 10 | self._name = name 11 | self._module_dict = dict() 12 | 13 | def __repr__(self): 14 | format_str = self.__class__.__name__ + '(name={}, items={})'.format( 15 | self._name, list(self._module_dict.keys())) 16 | return format_str 17 | 18 | @property 19 | def name(self): 20 | return self._name 21 | 22 | @property 23 | def module_dict(self): 24 | return self._module_dict 25 | 26 | def get(self, key): 27 | return self._module_dict.get(key, None) 28 | 29 | def _register_module(self, module_class, force=False): 30 | """Register a module. 31 | 32 | Args: 33 | module (:obj:`nn.Module`): Module to be registered. 34 | """ 35 | if not inspect.isclass(module_class): 36 | raise TypeError('module must be a class, but got {}'.format( 37 | type(module_class))) 38 | module_name = module_class.__name__ 39 | if not force and module_name in self._module_dict: 40 | raise KeyError('{} is already registered in {}'.format( 41 | module_name, self.name)) 42 | self._module_dict[module_name] = module_class 43 | 44 | def register_module(self, cls=None, force=False): 45 | if cls is None: 46 | return partial(self.register_module, force=force) 47 | self._register_module(cls, force=force) 48 | return cls 49 | 50 | 51 | def build_from_cfg(cfg, registry, default_args=None): 52 | """Build a module from config dict. 53 | 54 | Args: 55 | cfg (dict): Config dict. It should at least contain the key "type". 56 | registry (:obj:`Registry`): The registry to search the type from. 57 | default_args (dict, optional): Default initialization arguments. 58 | 59 | Returns: 60 | obj: The constructed object. 61 | """ 62 | assert isinstance(cfg, dict) and 'type' in cfg 63 | assert isinstance(default_args, dict) or default_args is None 64 | args = cfg.copy() 65 | obj_type = args.pop('type') 66 | if mmcv.is_str(obj_type): 67 | obj_cls = registry.get(obj_type) 68 | if obj_cls is None: 69 | raise KeyError('{} is not in the {} registry'.format( 70 | obj_type, registry.name)) 71 | elif inspect.isclass(obj_type): 72 | obj_cls = obj_type 73 | else: 74 | raise TypeError('type must be a str or valid type, but got {}'.format( 75 | type(obj_type))) 76 | if default_args is not None: 77 | for name, value in default_args.items(): 78 | args.setdefault(name, value) 79 | return obj_cls(**args) 80 | -------------------------------------------------------------------------------- /pytest.ini: -------------------------------------------------------------------------------- 1 | [pytest] 2 | addopts = --xdoctest --xdoctest-style=auto 3 | norecursedirs = .git ignore build __pycache__ data docker docs .eggs 4 | 5 | filterwarnings= default 6 | ignore:.*No cfgstr given in Cacher constructor or call.*:Warning 7 | ignore:.*Define the __nice__ method for.*:Warning 8 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | -r requirements/build.txt 2 | -r requirements/optional.txt 3 | -r requirements/runtime.txt 4 | -r requirements/tests.txt 5 | -------------------------------------------------------------------------------- /requirements/build.txt: -------------------------------------------------------------------------------- 1 | # These must be installed before building mmdetection 2 | numpy 3 | torch>=1.1 4 | -------------------------------------------------------------------------------- /requirements/optional.txt: -------------------------------------------------------------------------------- 1 | albumentations>=0.3.2 2 | cityscapesscripts 3 | imagecorruptions 4 | -------------------------------------------------------------------------------- /requirements/runtime.txt: -------------------------------------------------------------------------------- 1 | matplotlib 2 | mmcv>=0.3.1 3 | numpy 4 | # need older pillow until torchvision is fixed 5 | Pillow<=6.2.2 6 | six 7 | terminaltables 8 | torch>=1.1 9 | torchvision 10 | -------------------------------------------------------------------------------- /requirements/tests.txt: -------------------------------------------------------------------------------- 1 | asynctest 2 | codecov 3 | flake8 4 | isort 5 | # Note: used for kwarray.group_items, this may be ported to mmcv in the future. 6 | kwarray 7 | pytest 8 | pytest-cov 9 | pytest-runner 10 | ubelt 11 | xdoctest >= 0.10.0 12 | yapf 13 | -------------------------------------------------------------------------------- /test_fast_rcnn.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/DataXujing/Cascade_RCNN_mmdetection/da1cfb8e5b36ba63ae444b8d3c011623bc175970/test_fast_rcnn.jpg -------------------------------------------------------------------------------- /tests/test_async.py: -------------------------------------------------------------------------------- 1 | """Tests for async interface.""" 2 | 3 | import asyncio 4 | import os 5 | import sys 6 | 7 | import asynctest 8 | import mmcv 9 | import torch 10 | 11 | from mmdet.apis import async_inference_detector, init_detector 12 | 13 | if sys.version_info >= (3, 7): 14 | from mmdet.utils.contextmanagers import concurrent 15 | 16 | 17 | class AsyncTestCase(asynctest.TestCase): 18 | use_default_loop = False 19 | forbid_get_event_loop = True 20 | 21 | TEST_TIMEOUT = int(os.getenv('ASYNCIO_TEST_TIMEOUT', '30')) 22 | 23 | def _run_test_method(self, method): 24 | result = method() 25 | if asyncio.iscoroutine(result): 26 | self.loop.run_until_complete( 27 | asyncio.wait_for(result, timeout=self.TEST_TIMEOUT)) 28 | 29 | 30 | class MaskRCNNDetector: 31 | 32 | def __init__(self, 33 | model_config, 34 | checkpoint=None, 35 | streamqueue_size=3, 36 | device='cuda:0'): 37 | 38 | self.streamqueue_size = streamqueue_size 39 | self.device = device 40 | # build the model and load checkpoint 41 | self.model = init_detector( 42 | model_config, checkpoint=None, device=self.device) 43 | self.streamqueue = None 44 | 45 | async def init(self): 46 | self.streamqueue = asyncio.Queue() 47 | for _ in range(self.streamqueue_size): 48 | stream = torch.cuda.Stream(device=self.device) 49 | self.streamqueue.put_nowait(stream) 50 | 51 | if sys.version_info >= (3, 7): 52 | 53 | async def apredict(self, img): 54 | if isinstance(img, str): 55 | img = mmcv.imread(img) 56 | async with concurrent(self.streamqueue): 57 | result = await async_inference_detector(self.model, img) 58 | return result 59 | 60 | 61 | class AsyncInferenceTestCase(AsyncTestCase): 62 | 63 | if sys.version_info >= (3, 7): 64 | 65 | async def test_simple_inference(self): 66 | if not torch.cuda.is_available(): 67 | import pytest 68 | 69 | pytest.skip('test requires GPU and torch+cuda') 70 | 71 | root_dir = os.path.dirname(os.path.dirname(__name__)) 72 | model_config = os.path.join(root_dir, 73 | 'configs/mask_rcnn_r50_fpn_1x.py') 74 | detector = MaskRCNNDetector(model_config) 75 | await detector.init() 76 | img_path = os.path.join(root_dir, 'demo/demo.jpg') 77 | bboxes, _ = await detector.apredict(img_path) 78 | self.assertTrue(bboxes) 79 | -------------------------------------------------------------------------------- /tests/test_nms.py: -------------------------------------------------------------------------------- 1 | """ 2 | CommandLine: 3 | pytest tests/test_nms.py 4 | """ 5 | import numpy as np 6 | import torch 7 | 8 | from mmdet.ops.nms.nms_wrapper import nms 9 | 10 | 11 | def test_nms_device_and_dtypes_cpu(): 12 | """ 13 | CommandLine: 14 | xdoctest -m tests/test_nms.py test_nms_device_and_dtypes_cpu 15 | """ 16 | iou_thr = 0.7 17 | base_dets = np.array([[49.1, 32.4, 51.0, 35.9, 0.9], 18 | [49.3, 32.9, 51.0, 35.3, 0.9], 19 | [35.3, 11.5, 39.9, 14.5, 0.4], 20 | [35.2, 11.7, 39.7, 15.7, 0.3]]) 21 | 22 | # CPU can handle float32 and float64 23 | dets = base_dets.astype(np.float32) 24 | supressed, inds = nms(dets, iou_thr) 25 | assert dets.dtype == supressed.dtype 26 | assert len(inds) == len(supressed) == 3 27 | 28 | dets = torch.FloatTensor(base_dets) 29 | surpressed, inds = nms(dets, iou_thr) 30 | assert dets.dtype == surpressed.dtype 31 | assert len(inds) == len(surpressed) == 3 32 | 33 | dets = base_dets.astype(np.float64) 34 | supressed, inds = nms(dets, iou_thr) 35 | assert dets.dtype == supressed.dtype 36 | assert len(inds) == len(supressed) == 3 37 | 38 | dets = torch.DoubleTensor(base_dets) 39 | surpressed, inds = nms(dets, iou_thr) 40 | assert dets.dtype == surpressed.dtype 41 | assert len(inds) == len(surpressed) == 3 42 | 43 | 44 | def test_nms_device_and_dtypes_gpu(): 45 | """ 46 | CommandLine: 47 | xdoctest -m tests/test_nms.py test_nms_device_and_dtypes_gpu 48 | """ 49 | if not torch.cuda.is_available(): 50 | import pytest 51 | pytest.skip('test requires GPU and torch+cuda') 52 | 53 | iou_thr = 0.7 54 | base_dets = np.array([[49.1, 32.4, 51.0, 35.9, 0.9], 55 | [49.3, 32.9, 51.0, 35.3, 0.9], 56 | [35.3, 11.5, 39.9, 14.5, 0.4], 57 | [35.2, 11.7, 39.7, 15.7, 0.3]]) 58 | 59 | for device_id in range(torch.cuda.device_count()): 60 | print('Run NMS on device_id = {!r}'.format(device_id)) 61 | # GPU can handle float32 but not float64 62 | dets = base_dets.astype(np.float32) 63 | supressed, inds = nms(dets, iou_thr, device_id) 64 | assert dets.dtype == supressed.dtype 65 | assert len(inds) == len(supressed) == 3 66 | 67 | dets = torch.FloatTensor(base_dets).to(device_id) 68 | surpressed, inds = nms(dets, iou_thr) 69 | assert dets.dtype == surpressed.dtype 70 | assert len(inds) == len(surpressed) == 3 71 | -------------------------------------------------------------------------------- /tests/test_soft_nms.py: -------------------------------------------------------------------------------- 1 | """ 2 | CommandLine: 3 | pytest tests/test_soft_nms.py 4 | """ 5 | import numpy as np 6 | import torch 7 | 8 | from mmdet.ops.nms.nms_wrapper import soft_nms 9 | 10 | 11 | def test_soft_nms_device_and_dtypes_cpu(): 12 | """ 13 | CommandLine: 14 | xdoctest -m tests/test_soft_nms.py test_soft_nms_device_and_dtypes_cpu 15 | """ 16 | iou_thr = 0.7 17 | base_dets = np.array([[49.1, 32.4, 51.0, 35.9, 0.9], 18 | [49.3, 32.9, 51.0, 35.3, 0.9], 19 | [35.3, 11.5, 39.9, 14.5, 0.4], 20 | [35.2, 11.7, 39.7, 15.7, 0.3]]) 21 | 22 | # CPU can handle float32 and float64 23 | dets = base_dets.astype(np.float32) 24 | new_dets, inds = soft_nms(dets, iou_thr) 25 | assert dets.dtype == new_dets.dtype 26 | assert len(inds) == len(new_dets) == 4 27 | 28 | dets = torch.FloatTensor(base_dets) 29 | new_dets, inds = soft_nms(dets, iou_thr) 30 | assert dets.dtype == new_dets.dtype 31 | assert len(inds) == len(new_dets) == 4 32 | 33 | dets = base_dets.astype(np.float64) 34 | new_dets, inds = soft_nms(dets, iou_thr) 35 | assert dets.dtype == new_dets.dtype 36 | assert len(inds) == len(new_dets) == 4 37 | 38 | dets = torch.DoubleTensor(base_dets) 39 | new_dets, inds = soft_nms(dets, iou_thr) 40 | assert dets.dtype == new_dets.dtype 41 | assert len(inds) == len(new_dets) == 4 42 | -------------------------------------------------------------------------------- /tests/test_utils.py: -------------------------------------------------------------------------------- 1 | import numpy.testing as npt 2 | 3 | from mmdet.utils.flops_counter import params_to_string 4 | 5 | 6 | def test_params_to_string(): 7 | npt.assert_equal(params_to_string(1e9), '1000.0 M') 8 | npt.assert_equal(params_to_string(2e5), '200.0 k') 9 | npt.assert_equal(params_to_string(3e-9), '3e-09') 10 | -------------------------------------------------------------------------------- /tools/browse_dataset.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import os 3 | from pathlib import Path 4 | 5 | import mmcv 6 | from mmcv import Config 7 | 8 | from mmdet.datasets.builder import build_dataset 9 | 10 | 11 | def parse_args(): 12 | parser = argparse.ArgumentParser(description='Browse a dataset') 13 | parser.add_argument('config', help='train config file path') 14 | parser.add_argument( 15 | '--skip-type', 16 | type=str, 17 | nargs='+', 18 | default=['DefaultFormatBundle', 'Normalize', 'Collect'], 19 | help='skip some useless pipeline') 20 | parser.add_argument( 21 | '--output-dir', 22 | default=None, 23 | type=str, 24 | help='If there is no display interface, you can save it') 25 | parser.add_argument('--not-show', default=False, action='store_true') 26 | parser.add_argument( 27 | '--show-interval', 28 | type=int, 29 | default=999, 30 | help='the interval of show (ms)') 31 | args = parser.parse_args() 32 | return args 33 | 34 | 35 | def retrieve_data_cfg(config_path, skip_type): 36 | cfg = Config.fromfile(config_path) 37 | train_data_cfg = cfg.data.train 38 | train_data_cfg['pipeline'] = [ 39 | x for x in train_data_cfg.pipeline if x['type'] not in skip_type 40 | ] 41 | 42 | return cfg 43 | 44 | 45 | def main(): 46 | args = parse_args() 47 | cfg = retrieve_data_cfg(args.config, args.skip_type) 48 | 49 | dataset = build_dataset(cfg.data.train) 50 | 51 | progress_bar = mmcv.ProgressBar(len(dataset)) 52 | for item in dataset: 53 | filename = os.path.join(args.output_dir, 54 | Path(item['filename']).name 55 | ) if args.output_dir is not None else None 56 | mmcv.imshow_det_bboxes( 57 | item['img'], 58 | item['gt_bboxes'], 59 | item['gt_labels'] - 1, 60 | class_names=dataset.CLASSES, 61 | show=not args.not_show, 62 | out_file=filename, 63 | wait_time=args.show_interval) 64 | progress_bar.update() 65 | 66 | 67 | if __name__ == '__main__': 68 | main() 69 | -------------------------------------------------------------------------------- /tools/data_process/00_img_rename.py: -------------------------------------------------------------------------------- 1 | import os 2 | import shutil 3 | import uuid 4 | 5 | def img_rename(img_path,anno_path,mode="train"): 6 | 7 | ''' 8 | src: image path 9 | ''' 10 | img_files = os.listdir(img_path) 11 | anno_files = os.listdir(anno_path) 12 | 13 | count = 0 14 | for img_file in img_files: 15 | file_name = img_file.rstrip("jpg") 16 | if file_name+"xml" in anno_files: 17 | new_file = str(uuid.uuid1()).replace("-","") 18 | src_img = os.path.join(img_path,img_file) 19 | dst_img = os.path.join("./data/coco/{}/JPEGImages".format(mode),new_file+".jpg") 20 | src_anno = os.path.join(anno_path,file_name+"xml") 21 | dst_anno = os.path.join("./data/coco/{}/annotations".format(mode),new_file+".xml") 22 | os.rename(src_img,dst_img) 23 | os.rename(src_anno,dst_anno) 24 | count += 1 25 | else: 26 | print(img_file) 27 | os.remove(os.path.join(img_path,img_file)) 28 | 29 | print("[info] Rename Done: {} images".format(count)) 30 | 31 | 32 | if __name__ == "__main__": 33 | img_rename("./data/source/train/JPEGImages","./data/source/train/annotations",mode="train") 34 | img_rename("./data/source/test/JPEGImages","./data/source/test/annotations",mode="test") 35 | 36 | 37 | -------------------------------------------------------------------------------- /tools/data_process/01_check_img.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | import shutil 4 | 5 | def check_img(img_path): 6 | imgs = os.listdir(img_path) 7 | for img in imgs: 8 | if img.split(".")[-1] !="jpg": 9 | print(img) 10 | shutil.move(img_path+"/"+img,"./error/"+img) 11 | 12 | def check_anno(anno_path): 13 | anno_files = os.listdir(anno_path) 14 | for file in anno_files: 15 | if file.split(".")[-1] !="xml": 16 | print(file) 17 | shutil.move(anno_path+"/"+file,"./error/"+file) 18 | 19 | def ckeck_img_label(img_path,anno_path): 20 | imgs = os.listdir(img_path) 21 | anno_files = os.listdir(anno_path) 22 | 23 | files = [i.split(".")[0] for i in anno_files] 24 | 25 | 26 | for img in imgs: 27 | if img.split(".")[0] not in files: 28 | print(img) 29 | shutil.move(img_path+"/"+img,"./error/"+img) 30 | 31 | imgs = os.listdir(img_path) 32 | images = [j.split(".")[0] for j in imgs] 33 | 34 | for file in anno_files: 35 | if file.split(".")[0] not in images: 36 | print(file) 37 | shutil.move(anno_path+"/"+file,"./error/"+file) 38 | 39 | 40 | if __name__ == "__main__": 41 | img_path = "./data/coco/train/JPEGImages" 42 | anno_path = "./data/coco/train/annotations" 43 | 44 | print("============check image=========") 45 | check_img(img_path) 46 | 47 | print("============check anno==========") 48 | check_anno(anno_path) 49 | print("============check both==========") 50 | ckeck_img_label(img_path,anno_path) 51 | -------------------------------------------------------------------------------- /tools/data_process/02_check_box.py: -------------------------------------------------------------------------------- 1 | import xml.etree.ElementTree as xml_tree 2 | import pandas as pd 3 | import numpy as np 4 | import os 5 | import shutil 6 | 7 | 8 | def check_box(path): 9 | files = os.listdir(path) 10 | i = 0 11 | for anna_file in files: 12 | tree = xml_tree.parse(path+"/"+anna_file) 13 | root = tree.getroot() 14 | 15 | # Image shape. 16 | size = root.find('size') 17 | shape = [int(size.find('height').text), 18 | int(size.find('width').text), 19 | int(size.find('depth').text)] 20 | # Find annotations. 21 | bboxes = [] 22 | labels = [] 23 | labels_text = [] 24 | difficult = [] 25 | truncated = [] 26 | 27 | for obj in root.findall('object'): 28 | # label = obj.find('name').text 29 | # labels.append(int(dataset_common.VOC_LABELS[label][0])) 30 | # # labels_text.append(label.encode('ascii')) 31 | # labels_text.append(label.encode('utf-8')) 32 | 33 | 34 | # isdifficult = obj.find('difficult') 35 | # if isdifficult is not None: 36 | # difficult.append(int(isdifficult.text)) 37 | # else: 38 | # difficult.append(0) 39 | 40 | # istruncated = obj.find('truncated') 41 | # if istruncated is not None: 42 | # truncated.append(int(istruncated.text)) 43 | # else: 44 | # truncated.append(0) 45 | 46 | bbox = obj.find('bndbox') 47 | # bboxes.append((float(bbox.find('ymin').text) / shape[0], 48 | # float(bbox.find('xmin').text) / shape[1], 49 | # float(bbox.find('ymax').text) / shape[0], 50 | # float(bbox.find('xmax').text) / shape[1] 51 | # )) 52 | if (float(bbox.find('ymin').text) >= float(bbox.find('ymax').text)) or (float(bbox.find('xmin').text) >= float(bbox.find('xmax').text)): 53 | print(anna_file) 54 | i += 1 55 | try: 56 | shutil.move(path+"/"+anna_file,"./data/coco/error/"+anna_file) 57 | shutil.move("./data/coco/train/JPEGImages/"+anna_file.split(".")[0]+".jpg","./data/coco/error/"+anna_file.split(".")[0]+".jpg") 58 | except: 59 | pass 60 | 61 | print(i) 62 | 63 | 64 | 65 | if __name__ == "__main__": 66 | check_box("./data/coco/train/annotations") -------------------------------------------------------------------------------- /tools/data_process/generate_test_json.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | from glob import glob 4 | from tqdm import tqdm 5 | from PIL import Image 6 | from mmdet.core import underwater_classes 7 | label_ids = {name: i + 1 for i, name in enumerate(underwater_classes())} 8 | 9 | 10 | def save(images, annotations): 11 | ann = {} 12 | ann['type'] = 'instances' 13 | ann['images'] = images 14 | ann['annotations'] = annotations 15 | 16 | categories = [] 17 | for k, v in label_ids.items(): 18 | categories.append({"name": k, "id": v}) 19 | ann['categories'] = categories 20 | json.dump(ann, open('data/coco/annotations/test.json', 'w')) 21 | 22 | 23 | def test_dataset(im_dir): 24 | im_list = glob(os.path.join(im_dir, '*.jpg')) 25 | idx = 1 26 | image_id = 1 27 | images = [] 28 | annotations = [] 29 | for im_path in tqdm(im_list): 30 | image_id += 1 31 | im = Image.open(im_path) 32 | w, h = im.size 33 | image = {'file_name': os.path.basename(im_path), 'width': w, 'height': h, 'id': image_id} 34 | images.append(image) 35 | labels = [[10, 10, 20, 20]] 36 | for label in labels: 37 | bbox = [label[0], label[1], label[2] - label[0], label[3] - label[1]] 38 | seg = [] 39 | ann = {'segmentation': [seg], 'area': bbox[2] * bbox[3], 'iscrowd': 0, 'image_id': image_id, 40 | 'bbox': bbox, 'category_id': 1, 'id': idx, 'ignore': 0} 41 | idx += 1 42 | annotations.append(ann) 43 | save(images, annotations) 44 | 45 | 46 | if __name__ == '__main__': 47 | test_dir = 'data/source/test/JPEGImages' 48 | print("generate test json label file.") 49 | test_dataset(test_dir) -------------------------------------------------------------------------------- /tools/data_process/readme: -------------------------------------------------------------------------------- 1 | 存放在mmdetection/tools下! -------------------------------------------------------------------------------- /tools/dist_test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PYTHON=${PYTHON:-"python"} 4 | 5 | CONFIG=$1 6 | CHECKPOINT=$2 7 | GPUS=$3 8 | PORT=${PORT:-29500} 9 | 10 | $PYTHON -m torch.distributed.launch --nproc_per_node=$GPUS --master_port=$PORT \ 11 | $(dirname "$0")/test.py $CONFIG $CHECKPOINT --launcher pytorch ${@:4} 12 | -------------------------------------------------------------------------------- /tools/dist_train.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | PYTHON=${PYTHON:-"python"} 4 | 5 | CONFIG=$1 6 | GPUS=$2 7 | PORT=${PORT:-29500} 8 | 9 | $PYTHON -m torch.distributed.launch --nproc_per_node=$GPUS --master_port=$PORT \ 10 | $(dirname "$0")/train.py $CONFIG --launcher pytorch ${@:3} 11 | -------------------------------------------------------------------------------- /tools/get_flops.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | 3 | from mmcv import Config 4 | 5 | from mmdet.models import build_detector 6 | from mmdet.utils import get_model_complexity_info 7 | 8 | 9 | def parse_args(): 10 | parser = argparse.ArgumentParser(description='Train a detector') 11 | parser.add_argument('config', help='train config file path') 12 | parser.add_argument( 13 | '--shape', 14 | type=int, 15 | nargs='+', 16 | default=[1280, 800], 17 | help='input image size') 18 | args = parser.parse_args() 19 | return args 20 | 21 | 22 | def main(): 23 | 24 | args = parse_args() 25 | 26 | if len(args.shape) == 1: 27 | input_shape = (3, args.shape[0], args.shape[0]) 28 | elif len(args.shape) == 2: 29 | input_shape = (3, ) + tuple(args.shape) 30 | else: 31 | raise ValueError('invalid input shape') 32 | 33 | cfg = Config.fromfile(args.config) 34 | model = build_detector( 35 | cfg.model, train_cfg=cfg.train_cfg, test_cfg=cfg.test_cfg).cuda() 36 | model.eval() 37 | 38 | if hasattr(model, 'forward_dummy'): 39 | model.forward = model.forward_dummy 40 | else: 41 | raise NotImplementedError( 42 | 'FLOPs counter is currently not currently supported with {}'. 43 | format(model.__class__.__name__)) 44 | 45 | flops, params = get_model_complexity_info(model, input_shape) 46 | split_line = '=' * 30 47 | print('{0}\nInput shape: {1}\nFlops: {2}\nParams: {3}\n{0}'.format( 48 | split_line, input_shape, flops, params)) 49 | print('!!!Please be cautious if you use the results in papers. ' 50 | 'You may need to check if all ops are supported and verify that the ' 51 | 'flops computation is correct.') 52 | 53 | 54 | if __name__ == '__main__': 55 | main() 56 | -------------------------------------------------------------------------------- /tools/publish_model.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import subprocess 3 | 4 | import torch 5 | 6 | 7 | def parse_args(): 8 | parser = argparse.ArgumentParser( 9 | description='Process a checkpoint to be published') 10 | parser.add_argument('in_file', help='input checkpoint filename') 11 | parser.add_argument('out_file', help='output checkpoint filename') 12 | args = parser.parse_args() 13 | return args 14 | 15 | 16 | def process_checkpoint(in_file, out_file): 17 | checkpoint = torch.load(in_file, map_location='cpu') 18 | # remove optimizer for smaller file size 19 | if 'optimizer' in checkpoint: 20 | del checkpoint['optimizer'] 21 | # if it is necessary to remove some sensitive data in checkpoint['meta'], 22 | # add the code here. 23 | torch.save(checkpoint, out_file) 24 | sha = subprocess.check_output(['sha256sum', out_file]).decode() 25 | final_file = out_file.rstrip('.pth') + '-{}.pth'.format(sha[:8]) 26 | subprocess.Popen(['mv', out_file, final_file]) 27 | 28 | 29 | def main(): 30 | args = parse_args() 31 | process_checkpoint(args.in_file, args.out_file) 32 | 33 | 34 | if __name__ == '__main__': 35 | main() 36 | -------------------------------------------------------------------------------- /tools/slurm_test.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -x 4 | 5 | PARTITION=$1 6 | JOB_NAME=$2 7 | CONFIG=$3 8 | CHECKPOINT=$4 9 | GPUS=${GPUS:-8} 10 | GPUS_PER_NODE=${GPUS_PER_NODE:-8} 11 | CPUS_PER_TASK=${CPUS_PER_TASK:-5} 12 | PY_ARGS=${@:5} 13 | SRUN_ARGS=${SRUN_ARGS:-""} 14 | 15 | srun -p ${PARTITION} \ 16 | --job-name=${JOB_NAME} \ 17 | --gres=gpu:${GPUS_PER_NODE} \ 18 | --ntasks=${GPUS} \ 19 | --ntasks-per-node=${GPUS_PER_NODE} \ 20 | --cpus-per-task=${CPUS_PER_TASK} \ 21 | --kill-on-bad-exit=1 \ 22 | ${SRUN_ARGS} \ 23 | python -u tools/test.py ${CONFIG} ${CHECKPOINT} --launcher="slurm" ${PY_ARGS} 24 | -------------------------------------------------------------------------------- /tools/slurm_train.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | set -x 4 | 5 | PARTITION=$1 6 | JOB_NAME=$2 7 | CONFIG=$3 8 | WORK_DIR=$4 9 | GPUS=${5:-8} 10 | GPUS_PER_NODE=${GPUS_PER_NODE:-8} 11 | CPUS_PER_TASK=${CPUS_PER_TASK:-5} 12 | SRUN_ARGS=${SRUN_ARGS:-""} 13 | PY_ARGS=${PY_ARGS:-"--validate"} 14 | 15 | srun -p ${PARTITION} \ 16 | --job-name=${JOB_NAME} \ 17 | --gres=gpu:${GPUS_PER_NODE} \ 18 | --ntasks=${GPUS} \ 19 | --ntasks-per-node=${GPUS_PER_NODE} \ 20 | --cpus-per-task=${CPUS_PER_TASK} \ 21 | --kill-on-bad-exit=1 \ 22 | ${SRUN_ARGS} \ 23 | python -u tools/train.py ${CONFIG} --work_dir=${WORK_DIR} --launcher="slurm" ${PY_ARGS} 24 | -------------------------------------------------------------------------------- /tools/upgrade_model_version.py: -------------------------------------------------------------------------------- 1 | import argparse 2 | import re 3 | from collections import OrderedDict 4 | 5 | import torch 6 | 7 | 8 | def convert(in_file, out_file): 9 | """Convert keys in checkpoints. 10 | 11 | There can be some breaking changes during the development of mmdetection, 12 | and this tool is used for upgrading checkpoints trained with old versions 13 | to the latest one. 14 | """ 15 | checkpoint = torch.load(in_file) 16 | in_state_dict = checkpoint.pop('state_dict') 17 | out_state_dict = OrderedDict() 18 | for key, val in in_state_dict.items(): 19 | # Use ConvModule instead of nn.Conv2d in RetinaNet 20 | # cls_convs.0.weight -> cls_convs.0.conv.weight 21 | m = re.search(r'(cls_convs|reg_convs).\d.(weight|bias)', key) 22 | if m is not None: 23 | param = m.groups()[1] 24 | new_key = key.replace(param, 'conv.{}'.format(param)) 25 | out_state_dict[new_key] = val 26 | continue 27 | 28 | out_state_dict[key] = val 29 | checkpoint['state_dict'] = out_state_dict 30 | torch.save(checkpoint, out_file) 31 | 32 | 33 | def main(): 34 | parser = argparse.ArgumentParser(description='Upgrade model version') 35 | parser.add_argument('in_file', help='input checkpoint file') 36 | parser.add_argument('out_file', help='output checkpoint file') 37 | args = parser.parse_args() 38 | convert(args.in_file, args.out_file) 39 | 40 | 41 | if __name__ == '__main__': 42 | main() 43 | --------------------------------------------------------------------------------