├── datasets ├── epfl2vnc │ ├── semgt │ │ └── tmp │ ├── annotations │ │ └── tmp │ ├── source_images │ │ └── tmp │ └── target_images │ │ └── tmp ├── fluo2tnbc │ ├── semgt │ │ └── tmp │ ├── annotations │ │ └── tmp │ ├── source_images │ │ └── tmp │ └── target_images │ │ └── tmp └── fluo2tcga │ ├── semgt │ ├── 1_img.png │ ├── 2_img.png │ ├── 3_img.png │ ├── 4_img.png │ ├── 5_img.png │ ├── 6_img.png │ ├── 7_img.png │ ├── 8_img.png │ ├── 9_img.png │ ├── 10_img.png │ ├── 11_img.png │ ├── 12_img.png │ ├── 13_img.png │ ├── 14_img.png │ ├── 15_img.png │ ├── 16_img.png │ ├── 17_img.png │ ├── 18_img.png │ ├── 19_img.png │ └── 20_img.png │ ├── source_images │ ├── 1_img.png │ ├── 2_img.png │ ├── 3_img.png │ ├── 4_img.png │ ├── 5_img.png │ ├── 6_img.png │ ├── 7_img.png │ ├── 8_img.png │ ├── 9_img.png │ ├── 10_img.png │ ├── 11_img.png │ ├── 12_img.png │ ├── 13_img.png │ ├── 14_img.png │ ├── 15_img.png │ ├── 16_img.png │ ├── 17_img.png │ ├── 18_img.png │ ├── 19_img.png │ └── 20_img.png │ ├── target_images │ ├── 1_img.png │ ├── 2_img.png │ ├── 3_img.png │ ├── 4_img.png │ ├── 5_img.png │ ├── 6_img.png │ ├── 7_img.png │ ├── 8_img.png │ ├── 9_img.png │ ├── 10_img.png │ ├── 11_img.png │ ├── 12_img.png │ ├── 13_img.png │ ├── 14_img.png │ ├── 15_img.png │ ├── 16_img.png │ ├── 17_img.png │ ├── 18_img.png │ ├── 19_img.png │ └── 20_img.png │ └── raw_source_images │ ├── 10_img.png │ ├── 11_img.png │ ├── 12_img.png │ ├── 13_img.png │ ├── 14_img.png │ ├── 15_img.png │ ├── 16_img.png │ ├── 17_img.png │ ├── 18_img.png │ ├── 19_img.png │ ├── 1_img.png │ ├── 20_img.png │ ├── 2_img.png │ ├── 3_img.png │ ├── 4_img.png │ ├── 5_img.png │ ├── 6_img.png │ ├── 7_img.png │ ├── 8_img.png │ └── 9_img.png ├── maskrcnn_benchmark ├── utils │ ├── __init__.py │ ├── README.md │ ├── collect_env.py │ ├── cv2_util.py │ ├── logger.py │ ├── imports.py │ ├── miscellaneous.py │ ├── timer.py │ ├── env.py │ ├── registry.py │ ├── metric_logger.py │ ├── model_zoo.py │ ├── model_serialization.py │ └── comm.py ├── modeling │ ├── __init__.py │ ├── domain_adaption │ │ ├── _init_.py │ │ ├── BuildLabel.py │ │ └── LabelResizeLayer.py │ ├── roi_heads │ │ ├── __init__.py │ │ ├── box_head │ │ │ ├── __init__.py │ │ │ ├── roi_box_predictors.py │ │ │ └── box_head.py │ │ ├── keypoint_head │ │ │ ├── __init__.py │ │ │ ├── roi_keypoint_predictors.py │ │ │ ├── roi_keypoint_feature_extractors.py │ │ │ └── keypoint_head.py │ │ ├── mask_head │ │ │ ├── __init__.py │ │ │ ├── roi_mask_predictors.py │ │ │ └── roi_mask_feature_extractors.py │ │ └── roi_heads.py │ ├── rpn │ │ ├── retinanet │ │ │ ├── __init__.py │ │ │ └── loss.py │ │ ├── __init__.py │ │ └── utils.py │ ├── detector │ │ ├── __init__.py │ │ └── detectors.py │ ├── backbone │ │ ├── __init__.py │ │ ├── pafpn.py │ │ └── backbone.py │ ├── registry.py │ ├── utils.py │ ├── balanced_positive_negative_sampler.py │ └── box_coder.py ├── structures │ ├── __init__.py │ └── image_list.py ├── __init__.py ├── engine │ └── __init__.py ├── config │ └── __init__.py ├── data │ ├── __init__.py │ ├── transforms │ │ ├── __init__.py │ │ └── build.py │ ├── samplers │ │ ├── __init__.py │ │ ├── iteration_based_batch_sampler.py │ │ └── distributed.py │ ├── datasets │ │ ├── __init__.py │ │ ├── evaluation │ │ │ ├── voc │ │ │ │ └── __init__.py │ │ │ ├── cityscapes │ │ │ │ └── __init__.py │ │ │ ├── __init__.py │ │ │ └── coco │ │ │ │ ├── __init__.py │ │ │ │ └── coco_eval_wrapper.py │ │ ├── concat_dataset.py │ │ ├── list_dataset.py │ │ └── abstract.py │ ├── collate_batch.py │ └── README.md ├── layers │ ├── dcn │ │ ├── __init__.py │ │ └── deform_pool_func.py │ ├── nms.py │ ├── smooth_l1_loss.py │ ├── batch_norm.py │ ├── _utils.py │ ├── __init__.py │ ├── roi_pool.py │ ├── roi_align.py │ └── sigmoid_focal_loss.py ├── solver │ ├── __init__.py │ ├── build.py │ └── lr_scheduler.py └── csrc │ ├── cpu │ ├── vision.h │ └── nms_cpu.cpp │ ├── nms.h │ ├── SigmoidFocalLoss.h │ ├── vision.cpp │ ├── ROIPool.h │ ├── ROIAlign.h │ ├── deform_pool.h │ └── cuda │ └── deform_pool_cuda.cu ├── requirements.txt ├── nuclei_inpaint ├── synthesized_labels │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ ├── 9.png │ ├── 10.png │ ├── 11.png │ └── 12.png ├── raw_synthesized_patches │ ├── 1.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ ├── 9.png │ ├── 10.png │ ├── 11.png │ └── 12.png └── inpaint_synthesized_patches │ ├── 1.png │ ├── 10.png │ ├── 11.png │ ├── 12.png │ ├── 2.png │ ├── 3.png │ ├── 4.png │ ├── 5.png │ ├── 6.png │ ├── 7.png │ ├── 8.png │ └── 9.png ├── tests ├── env_tests │ └── env.py ├── test_configs.py ├── test_metric_logger.py ├── utils.py ├── test_backbones.py ├── test_rpn_heads.py ├── test_segmentation_mask.py ├── test_fbnet.py ├── test_feature_extractors.py ├── test_predictors.py └── test_box_coder.py ├── train_gn_pdam.sh ├── LICENSE ├── inference ├── color_instance.py ├── epfl2vnc_infer.py ├── fluo2tnbc_infer.py ├── fluo2tcga_infer.py ├── epfl2vnc_eva.py ├── fluo2tnbc_eva.py └── fluo2tcga_eva.py ├── auxiliary_nuclei_inpaint.py ├── setup.py └── configs └── uda_nuclei_seg ├── e2e_mask_rcnn_R_101_FPN_1x_gn_fluo2tcga.yaml ├── e2e_mask_rcnn_R_101_FPN_1x_gn_epfl2vnc.yaml └── e2e_mask_rcnn_R_101_FPN_1x_gn_fluo2tnbc.yaml /datasets/epfl2vnc/semgt/tmp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /datasets/fluo2tnbc/semgt/tmp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /datasets/epfl2vnc/annotations/tmp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /datasets/epfl2vnc/source_images/tmp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /datasets/epfl2vnc/target_images/tmp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /datasets/fluo2tnbc/annotations/tmp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /datasets/fluo2tnbc/source_images/tmp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /datasets/fluo2tnbc/target_images/tmp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/structures/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/domain_adaption/_init_.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/rpn/retinanet/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/box_head/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/keypoint_head/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/mask_head/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | ninja 2 | yacs 3 | cython 4 | matplotlib 5 | tqdm 6 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/engine/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/1_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/1_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/2_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/2_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/3_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/3_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/4_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/4_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/5_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/5_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/6_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/6_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/7_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/7_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/8_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/8_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/9_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/9_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/10_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/10_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/11_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/11_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/12_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/12_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/13_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/13_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/14_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/14_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/15_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/15_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/16_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/16_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/17_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/17_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/18_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/18_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/19_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/19_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/semgt/20_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/semgt/20_img.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/1.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/2.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/3.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/4.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/5.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/6.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/7.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/8.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/9.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/1_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/1_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/2_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/2_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/3_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/3_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/4_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/4_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/5_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/5_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/6_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/6_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/7_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/7_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/8_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/8_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/9_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/9_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/1_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/1_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/2_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/2_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/3_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/3_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/4_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/4_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/5_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/5_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/6_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/6_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/7_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/7_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/8_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/8_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/9_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/9_img.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/10.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/11.png -------------------------------------------------------------------------------- /nuclei_inpaint/synthesized_labels/12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/synthesized_labels/12.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/10_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/10_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/11_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/11_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/12_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/12_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/13_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/13_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/14_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/14_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/15_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/15_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/16_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/16_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/17_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/17_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/18_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/18_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/19_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/19_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/source_images/20_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/source_images/20_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/10_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/10_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/11_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/11_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/12_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/12_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/13_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/13_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/14_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/14_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/15_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/15_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/16_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/16_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/17_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/17_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/18_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/18_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/19_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/19_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/target_images/20_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/target_images/20_img.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/1.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/2.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/3.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/4.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/5.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/6.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/7.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/8.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/9.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/10_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/10_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/11_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/11_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/12_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/12_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/13_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/13_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/14_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/14_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/15_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/15_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/16_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/16_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/17_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/17_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/18_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/18_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/19_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/19_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/1_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/1_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/20_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/20_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/2_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/2_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/3_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/3_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/4_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/4_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/5_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/5_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/6_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/6_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/7_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/7_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/8_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/8_img.png -------------------------------------------------------------------------------- /datasets/fluo2tcga/raw_source_images/9_img.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/datasets/fluo2tcga/raw_source_images/9_img.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/10.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/11.png -------------------------------------------------------------------------------- /nuclei_inpaint/raw_synthesized_patches/12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/raw_synthesized_patches/12.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/1.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/10.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/10.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/11.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/11.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/12.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/12.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/2.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/3.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/4.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/5.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/6.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/6.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/7.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/7.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/8.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/8.png -------------------------------------------------------------------------------- /nuclei_inpaint/inpaint_synthesized_patches/9.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dliu5812/PDAM/HEAD/nuclei_inpaint/inpaint_synthesized_patches/9.png -------------------------------------------------------------------------------- /maskrcnn_benchmark/config/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from .defaults import _C as cfg 3 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from .build import make_data_loader 3 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/rpn/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | # from .rpn import build_rpn 3 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/layers/dcn/__init__.py: -------------------------------------------------------------------------------- 1 | # 2 | # Copied From [mmdetection](https://github.com/open-mmlab/mmdetection/tree/master/mmdet/ops/dcn) 3 | # 4 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/detector/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from .detectors import build_detection_model 3 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/backbone/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from .backbone import build_backbone 3 | from . import fbnet 4 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/README.md: -------------------------------------------------------------------------------- 1 | # Utility functions 2 | 3 | This folder contain utility functions that are not used in the 4 | core library, but are useful for building models or training 5 | code using the config system. 6 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/solver/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from .build import make_optimizer 3 | from .build import make_lr_scheduler 4 | from .lr_scheduler import WarmupMultiStepLR 5 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/transforms/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from .transforms import Compose 3 | from .transforms import Resize 4 | from .transforms import RandomHorizontalFlip 5 | from .transforms import ToTensor 6 | from .transforms import Normalize 7 | 8 | from .build import build_transforms 9 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/samplers/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from .distributed import DistributedSampler 3 | from .grouped_batch_sampler import GroupedBatchSampler 4 | from .iteration_based_batch_sampler import IterationBasedBatchSampler 5 | 6 | __all__ = ["DistributedSampler", "GroupedBatchSampler", "IterationBasedBatchSampler"] 7 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/layers/nms.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | # from ._utils import _C 3 | from maskrcnn_benchmark import _C 4 | 5 | from apex import amp 6 | 7 | # Only valid with fp32 inputs - give AMP the hint 8 | nms = amp.float_function(_C.nms) 9 | 10 | # nms.__doc__ = """ 11 | # This function performs Non-maximum suppresion""" 12 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/detector/detectors.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from .generalized_rcnn import GeneralizedRCNN 3 | 4 | 5 | _DETECTION_META_ARCHITECTURES = {"GeneralizedRCNN": GeneralizedRCNN} 6 | 7 | 8 | def build_detection_model(cfg): 9 | meta_arch = _DETECTION_META_ARCHITECTURES[cfg.MODEL.META_ARCHITECTURE] 10 | return meta_arch(cfg) 11 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/collect_env.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import PIL 3 | 4 | from torch.utils.collect_env import get_pretty_env_info 5 | 6 | 7 | def get_pil_version(): 8 | return "\n Pillow ({})".format(PIL.__version__) 9 | 10 | 11 | def collect_env_info(): 12 | env_str = get_pretty_env_info() 13 | env_str += get_pil_version() 14 | return env_str 15 | -------------------------------------------------------------------------------- /tests/env_tests/env.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | import os 4 | 5 | 6 | def get_config_root_path(): 7 | ''' Path to configs for unit tests ''' 8 | # cur_file_dir is root/tests/env_tests 9 | cur_file_dir = os.path.dirname(os.path.abspath(os.path.realpath(__file__))) 10 | ret = os.path.dirname(os.path.dirname(cur_file_dir)) 11 | ret = os.path.join(ret, "configs") 12 | return ret 13 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/registry.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | from maskrcnn_benchmark.utils.registry import Registry 4 | 5 | BACKBONES = Registry() 6 | RPN_HEADS = Registry() 7 | ROI_BOX_FEATURE_EXTRACTORS = Registry() 8 | ROI_BOX_PREDICTOR = Registry() 9 | ROI_KEYPOINT_FEATURE_EXTRACTORS = Registry() 10 | ROI_KEYPOINT_PREDICTOR = Registry() 11 | ROI_MASK_FEATURE_EXTRACTORS = Registry() 12 | ROI_MASK_PREDICTOR = Registry() 13 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/datasets/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | from .coco import COCODataset 4 | from .voc import PascalVOCDataset 5 | from .concat_dataset import ConcatDataset 6 | from .abstract import AbstractDataset 7 | from .cityscapes import CityScapesDataset 8 | 9 | __all__ = [ 10 | "COCODataset", 11 | "ConcatDataset", 12 | "PascalVOCDataset", 13 | "AbstractDataset", 14 | "CityScapesDataset", 15 | ] 16 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | """ 3 | Miscellaneous utility functions 4 | """ 5 | 6 | import torch 7 | 8 | 9 | def cat(tensors, dim=0): 10 | """ 11 | Efficient version of torch.cat that avoids a copy if there is only a single element in a list 12 | """ 13 | assert isinstance(tensors, (list, tuple)) 14 | if len(tensors) == 1: 15 | return tensors[0] 16 | return torch.cat(tensors, dim) 17 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/layers/smooth_l1_loss.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | 4 | 5 | # TODO maybe push this to nn? 6 | def smooth_l1_loss(input, target, beta=1. / 9, size_average=True): 7 | """ 8 | very similar to the smooth_l1_loss from pytorch, but with 9 | the extra beta parameter 10 | """ 11 | n = torch.abs(input - target) 12 | cond = n < beta 13 | loss = torch.where(cond, 0.5 * n ** 2 / beta, n - 0.5 * beta) 14 | if size_average: 15 | return loss.mean() 16 | return loss.sum() 17 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/datasets/evaluation/voc/__init__.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | from .voc_eval import do_voc_evaluation 4 | 5 | 6 | def voc_evaluation(dataset, predictions, output_folder, box_only, **_): 7 | logger = logging.getLogger("maskrcnn_benchmark.inference") 8 | if box_only: 9 | logger.warning("voc evaluation doesn't support box_only, ignored.") 10 | logger.info("performing voc evaluation, ignored iou_types.") 11 | return do_voc_evaluation( 12 | dataset=dataset, 13 | predictions=predictions, 14 | output_folder=output_folder, 15 | logger=logger, 16 | ) 17 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/datasets/evaluation/cityscapes/__init__.py: -------------------------------------------------------------------------------- 1 | from .cityscapes_eval import do_cityscapes_evaluation 2 | 3 | 4 | def abs_cityscapes_evaluation( 5 | dataset, 6 | predictions, 7 | box_only, 8 | output_folder, 9 | iou_types, 10 | expected_results, 11 | expected_results_sigma_tol, 12 | ): 13 | return do_cityscapes_evaluation( 14 | dataset=dataset, 15 | predictions=predictions, 16 | box_only=box_only, 17 | output_folder=output_folder, 18 | iou_types=iou_types, 19 | expected_results=expected_results, 20 | expected_results_sigma_tol=expected_results_sigma_tol, 21 | ) 22 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/csrc/cpu/vision.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | #pragma once 3 | #include 4 | 5 | 6 | at::Tensor ROIAlign_forward_cpu(const at::Tensor& input, 7 | const at::Tensor& rois, 8 | const float spatial_scale, 9 | const int pooled_height, 10 | const int pooled_width, 11 | const int sampling_ratio); 12 | 13 | 14 | at::Tensor nms_cpu(const at::Tensor& dets, 15 | const at::Tensor& scores, 16 | const float threshold); 17 | -------------------------------------------------------------------------------- /train_gn_pdam.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | 4 | 5 | # fluo2tnbc 6 | 7 | # 8 | #CUDA_VISIBLE_DEVICES=0 python tools/train_net.py \ 9 | # --config-file "configs/uda_nuclei_seg/e2e_mask_rcnn_R_101_FPN_1x_gn_fluo2tnbc.yaml" \ 10 | # OUTPUT_DIR ./fluo2tnbc-models/pdam 11 | 12 | # fluo2tcga 13 | 14 | CUDA_VISIBLE_DEVICES=0 python tools/train_net.py \ 15 | --config-file "./configs/uda_nuclei_seg/e2e_mask_rcnn_R_101_FPN_1x_gn_fluo2tcga.yaml" \ 16 | OUTPUT_DIR ./fluo2tcga-models/pdam 17 | 18 | 19 | # em epfl2vnc 20 | # 21 | #CUDA_VISIBLE_DEVICES=0 python tools/train_net.py \ 22 | # --config-file "configs/uda_nuclei_seg/e2e_mask_rcnn_R_101_FPN_1x_gn_epfl2vnc.yaml" \ 23 | # OUTPUT_DIR ./epfl2vnc-models/pdam -------------------------------------------------------------------------------- /tests/test_configs.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | import unittest 4 | import glob 5 | import os 6 | import utils 7 | 8 | 9 | class TestConfigs(unittest.TestCase): 10 | def test_configs_load(self): 11 | ''' Make sure configs are loadable ''' 12 | 13 | cfg_root_path = utils.get_config_root_path() 14 | files = glob.glob( 15 | os.path.join(cfg_root_path, "./**/*.yaml"), recursive=True) 16 | self.assertGreater(len(files), 0) 17 | 18 | for fn in files: 19 | print('Loading {}...'.format(fn)) 20 | utils.load_config_from_file(fn) 21 | 22 | 23 | if __name__ == "__main__": 24 | unittest.main() 25 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/cv2_util.py: -------------------------------------------------------------------------------- 1 | """ 2 | Module for cv2 utility functions and maintaining version compatibility 3 | between 3.x and 4.x 4 | """ 5 | import cv2 6 | 7 | 8 | def findContours(*args, **kwargs): 9 | """ 10 | Wraps cv2.findContours to maintain compatiblity between versions 11 | 3 and 4 12 | 13 | Returns: 14 | contours, hierarchy 15 | """ 16 | if cv2.__version__.startswith('4'): 17 | contours, hierarchy = cv2.findContours(*args, **kwargs) 18 | elif cv2.__version__.startswith('3'): 19 | _, contours, hierarchy = cv2.findContours(*args, **kwargs) 20 | else: 21 | raise AssertionError( 22 | 'cv2 must be either version 3 or 4 to call this method') 23 | 24 | return contours, hierarchy 25 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/csrc/nms.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | #pragma once 3 | #include "cpu/vision.h" 4 | 5 | #ifdef WITH_CUDA 6 | #include "cuda/vision.h" 7 | #endif 8 | 9 | 10 | at::Tensor nms(const at::Tensor& dets, 11 | const at::Tensor& scores, 12 | const float threshold) { 13 | 14 | if (dets.type().is_cuda()) { 15 | #ifdef WITH_CUDA 16 | // TODO raise error if not compiled with CUDA 17 | if (dets.numel() == 0) 18 | return at::empty({0}, dets.options().dtype(at::kLong).device(at::kCPU)); 19 | auto b = at::cat({dets, scores.unsqueeze(1)}, 1); 20 | return nms_cuda(b, threshold); 21 | #else 22 | AT_ERROR("Not compiled with GPU support"); 23 | #endif 24 | } 25 | 26 | at::Tensor result = nms_cpu(dets, scores, threshold); 27 | return result; 28 | } 29 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/datasets/concat_dataset.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import bisect 3 | 4 | from torch.utils.data.dataset import ConcatDataset as _ConcatDataset 5 | 6 | 7 | class ConcatDataset(_ConcatDataset): 8 | """ 9 | Same as torch.utils.data.dataset.ConcatDataset, but exposes an extra 10 | method for querying the sizes of the image 11 | """ 12 | 13 | def get_idxs(self, idx): 14 | dataset_idx = bisect.bisect_right(self.cumulative_sizes, idx) 15 | if dataset_idx == 0: 16 | sample_idx = idx 17 | else: 18 | sample_idx = idx - self.cumulative_sizes[dataset_idx - 1] 19 | return dataset_idx, sample_idx 20 | 21 | def get_img_info(self, idx): 22 | dataset_idx, sample_idx = self.get_idxs(idx) 23 | return self.datasets[dataset_idx].get_img_info(sample_idx) 24 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/logger.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import logging 3 | import os 4 | import sys 5 | 6 | 7 | def setup_logger(name, save_dir, distributed_rank, filename="log.txt"): 8 | logger = logging.getLogger(name) 9 | logger.setLevel(logging.DEBUG) 10 | # don't log results for the non-master process 11 | if distributed_rank > 0: 12 | return logger 13 | ch = logging.StreamHandler(stream=sys.stdout) 14 | ch.setLevel(logging.DEBUG) 15 | formatter = logging.Formatter("%(asctime)s %(name)s %(levelname)s: %(message)s") 16 | ch.setFormatter(formatter) 17 | logger.addHandler(ch) 18 | 19 | if save_dir: 20 | fh = logging.FileHandler(os.path.join(save_dir, filename)) 21 | fh.setLevel(logging.DEBUG) 22 | fh.setFormatter(formatter) 23 | logger.addHandler(fh) 24 | 25 | return logger 26 | -------------------------------------------------------------------------------- /tests/test_metric_logger.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import unittest 3 | 4 | from maskrcnn_benchmark.utils.metric_logger import MetricLogger 5 | 6 | 7 | class TestMetricLogger(unittest.TestCase): 8 | def test_update(self): 9 | meter = MetricLogger() 10 | for i in range(10): 11 | meter.update(metric=float(i)) 12 | 13 | m = meter.meters["metric"] 14 | self.assertEqual(m.count, 10) 15 | self.assertEqual(m.total, 45) 16 | self.assertEqual(m.median, 4) 17 | self.assertEqual(m.avg, 4.5) 18 | 19 | def test_no_attr(self): 20 | meter = MetricLogger() 21 | _ = meter.meters 22 | _ = meter.delimiter 23 | def broken(): 24 | _ = meter.not_existent 25 | self.assertRaises(AttributeError, broken) 26 | 27 | if __name__ == "__main__": 28 | unittest.main() 29 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/imports.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | 4 | if torch._six.PY3: 5 | import importlib 6 | import importlib.util 7 | import sys 8 | 9 | 10 | # from https://stackoverflow.com/questions/67631/how-to-import-a-module-given-the-full-path?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa 11 | def import_file(module_name, file_path, make_importable=False): 12 | spec = importlib.util.spec_from_file_location(module_name, file_path) 13 | module = importlib.util.module_from_spec(spec) 14 | spec.loader.exec_module(module) 15 | if make_importable: 16 | sys.modules[module_name] = module 17 | return module 18 | else: 19 | import imp 20 | 21 | def import_file(module_name, file_path, make_importable=None): 22 | module = imp.load_source(module_name, file_path) 23 | return module 24 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/domain_adaption/BuildLabel.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | from torch.autograd import Variable 4 | 5 | 6 | def build_source_label(cfg): 7 | need_backprop = torch.FloatTensor(1) 8 | device = torch.device(cfg.MODEL.DEVICE) 9 | if device is 'cuda': 10 | need_backprop = need_backprop.cuda() 11 | need_backprop = Variable(need_backprop) 12 | np_need_backprop = np.ones((1,),dtype=np.float32) 13 | need_backprop.data.resize_(np_need_backprop.size()).copy_(np_need_backprop) 14 | 15 | return need_backprop 16 | 17 | 18 | def build_target_label(cfg): 19 | need_backprop = torch.FloatTensor(1) 20 | device = torch.device(cfg.MODEL.DEVICE) 21 | if device is 'cuda': 22 | need_backprop = need_backprop.cuda() 23 | need_backprop = Variable(need_backprop) 24 | np_need_backprop = np.zeros((1,), dtype=np.float32) 25 | need_backprop.data.resize_(np_need_backprop.size()).copy_(np_need_backprop) 26 | 27 | return need_backprop -------------------------------------------------------------------------------- /tests/utils.py: -------------------------------------------------------------------------------- 1 | from __future__ import absolute_import, division, print_function, unicode_literals 2 | 3 | # Set up custom environment before nearly anything else is imported 4 | # NOTE: this should be the first import (no not reorder) 5 | from maskrcnn_benchmark.utils.env import setup_environment # noqa F401 isort:skip 6 | import env_tests.env as env_tests 7 | 8 | import os 9 | import copy 10 | 11 | from maskrcnn_benchmark.config import cfg as g_cfg 12 | 13 | 14 | def get_config_root_path(): 15 | return env_tests.get_config_root_path() 16 | 17 | 18 | def load_config(rel_path): 19 | ''' Load config from file path specified as path relative to config_root ''' 20 | cfg_path = os.path.join(env_tests.get_config_root_path(), rel_path) 21 | return load_config_from_file(cfg_path) 22 | 23 | 24 | def load_config_from_file(file_path): 25 | ''' Load config from file path specified as absolute path ''' 26 | ret = copy.deepcopy(g_cfg) 27 | ret.merge_from_file(file_path) 28 | return ret 29 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/collate_batch.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from maskrcnn_benchmark.structures.image_list import to_image_list 3 | 4 | 5 | class BatchCollator(object): 6 | """ 7 | From a list of samples from the dataset, 8 | returns the batched images and targets. 9 | This should be passed to the DataLoader 10 | """ 11 | 12 | def __init__(self, size_divisible=0): 13 | self.size_divisible = size_divisible 14 | 15 | def __call__(self, batch): 16 | transposed_batch = list(zip(*batch)) 17 | images = to_image_list(transposed_batch[0], self.size_divisible) 18 | targets = transposed_batch[1] 19 | img_ids = transposed_batch[2] 20 | return images, targets, img_ids 21 | 22 | 23 | class BBoxAugCollator(object): 24 | """ 25 | From a list of samples from the dataset, 26 | returns the images and targets. 27 | Images should be converted to batched images in `im_detect_bbox_aug` 28 | """ 29 | 30 | def __call__(self, batch): 31 | return list(zip(*batch)) 32 | 33 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/solver/build.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | 4 | from .lr_scheduler import WarmupMultiStepLR 5 | 6 | 7 | def make_optimizer(cfg, model): 8 | params = [] 9 | for key, value in model.named_parameters(): 10 | if not value.requires_grad: 11 | continue 12 | lr = cfg.SOLVER.BASE_LR 13 | weight_decay = cfg.SOLVER.WEIGHT_DECAY 14 | if "bias" in key: 15 | lr = cfg.SOLVER.BASE_LR * cfg.SOLVER.BIAS_LR_FACTOR 16 | weight_decay = cfg.SOLVER.WEIGHT_DECAY_BIAS 17 | params += [{"params": [value], "lr": lr, "weight_decay": weight_decay}] 18 | 19 | optimizer = torch.optim.SGD(params, lr, momentum=cfg.SOLVER.MOMENTUM) 20 | return optimizer 21 | 22 | 23 | def make_lr_scheduler(cfg, optimizer): 24 | return WarmupMultiStepLR( 25 | optimizer, 26 | cfg.SOLVER.STEPS, 27 | cfg.SOLVER.GAMMA, 28 | warmup_factor=cfg.SOLVER.WARMUP_FACTOR, 29 | warmup_iters=cfg.SOLVER.WARMUP_ITERS, 30 | warmup_method=cfg.SOLVER.WARMUP_METHOD, 31 | ) 32 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/datasets/list_dataset.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | """ 3 | Simple dataset class that wraps a list of path names 4 | """ 5 | 6 | from PIL import Image 7 | 8 | from maskrcnn_benchmark.structures.bounding_box import BoxList 9 | 10 | 11 | class ListDataset(object): 12 | def __init__(self, image_lists, transforms=None): 13 | self.image_lists = image_lists 14 | self.transforms = transforms 15 | 16 | def __getitem__(self, item): 17 | img = Image.open(self.image_lists[item]).convert("RGB") 18 | 19 | # dummy target 20 | w, h = img.size 21 | target = BoxList([[0, 0, w, h]], img.size, mode="xyxy") 22 | 23 | if self.transforms is not None: 24 | img, target = self.transforms(img, target) 25 | 26 | return img, target 27 | 28 | def __len__(self): 29 | return len(self.image_lists) 30 | 31 | def get_img_info(self, item): 32 | """ 33 | Return the image dimensions for the image, without 34 | loading and pre-processing it 35 | """ 36 | pass 37 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Dongnan Liu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/csrc/SigmoidFocalLoss.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include "cpu/vision.h" 4 | 5 | #ifdef WITH_CUDA 6 | #include "cuda/vision.h" 7 | #endif 8 | 9 | // Interface for Python 10 | at::Tensor SigmoidFocalLoss_forward( 11 | const at::Tensor& logits, 12 | const at::Tensor& targets, 13 | const int num_classes, 14 | const float gamma, 15 | const float alpha) { 16 | if (logits.type().is_cuda()) { 17 | #ifdef WITH_CUDA 18 | return SigmoidFocalLoss_forward_cuda(logits, targets, num_classes, gamma, alpha); 19 | #else 20 | AT_ERROR("Not compiled with GPU support"); 21 | #endif 22 | } 23 | AT_ERROR("Not implemented on the CPU"); 24 | } 25 | 26 | at::Tensor SigmoidFocalLoss_backward( 27 | const at::Tensor& logits, 28 | const at::Tensor& targets, 29 | const at::Tensor& d_losses, 30 | const int num_classes, 31 | const float gamma, 32 | const float alpha) { 33 | if (logits.type().is_cuda()) { 34 | #ifdef WITH_CUDA 35 | return SigmoidFocalLoss_backward_cuda(logits, targets, d_losses, num_classes, gamma, alpha); 36 | #else 37 | AT_ERROR("Not compiled with GPU support"); 38 | #endif 39 | } 40 | AT_ERROR("Not implemented on the CPU"); 41 | } 42 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/layers/batch_norm.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | from torch import nn 4 | 5 | 6 | class FrozenBatchNorm2d(nn.Module): 7 | """ 8 | BatchNorm2d where the batch statistics and the affine parameters 9 | are fixed 10 | """ 11 | 12 | def __init__(self, n): 13 | super(FrozenBatchNorm2d, self).__init__() 14 | self.register_buffer("weight", torch.ones(n)) 15 | self.register_buffer("bias", torch.zeros(n)) 16 | self.register_buffer("running_mean", torch.zeros(n)) 17 | self.register_buffer("running_var", torch.ones(n)) 18 | 19 | def forward(self, x): 20 | # Cast all fixed parameters to half() if necessary 21 | if x.dtype == torch.float16: 22 | self.weight = self.weight.half() 23 | self.bias = self.bias.half() 24 | self.running_mean = self.running_mean.half() 25 | self.running_var = self.running_var.half() 26 | 27 | scale = self.weight * self.running_var.rsqrt() 28 | bias = self.bias - self.running_mean * scale 29 | scale = scale.reshape(1, -1, 1, 1) 30 | bias = bias.reshape(1, -1, 1, 1) 31 | return x * scale + bias 32 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/datasets/evaluation/__init__.py: -------------------------------------------------------------------------------- 1 | from maskrcnn_benchmark.data import datasets 2 | 3 | from .coco import coco_evaluation 4 | from .voc import voc_evaluation 5 | from .cityscapes import abs_cityscapes_evaluation 6 | 7 | def evaluate(dataset, predictions, output_folder, **kwargs): 8 | """evaluate dataset using different methods based on dataset type. 9 | Args: 10 | dataset: Dataset object 11 | predictions(list[BoxList]): each item in the list represents the 12 | prediction results for one image. 13 | output_folder: output folder, to save evaluation files or results. 14 | **kwargs: other args. 15 | Returns: 16 | evaluation result 17 | """ 18 | args = dict( 19 | dataset=dataset, predictions=predictions, output_folder=output_folder, **kwargs 20 | ) 21 | if isinstance(dataset, datasets.COCODataset): 22 | return coco_evaluation(**args) 23 | elif isinstance(dataset, datasets.PascalVOCDataset): 24 | return voc_evaluation(**args) 25 | elif isinstance(dataset, datasets.AbstractDataset): 26 | return abs_cityscapes_evaluation(**args) 27 | else: 28 | dataset_name = dataset.__class__.__name__ 29 | raise NotImplementedError("Unsupported dataset type {}.".format(dataset_name)) 30 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/samplers/iteration_based_batch_sampler.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from torch.utils.data.sampler import BatchSampler 3 | 4 | 5 | class IterationBasedBatchSampler(BatchSampler): 6 | """ 7 | Wraps a BatchSampler, resampling from it until 8 | a specified number of iterations have been sampled 9 | """ 10 | 11 | def __init__(self, batch_sampler, num_iterations, start_iter=0): 12 | self.batch_sampler = batch_sampler 13 | self.num_iterations = num_iterations 14 | self.start_iter = start_iter 15 | 16 | def __iter__(self): 17 | iteration = self.start_iter 18 | while iteration <= self.num_iterations: 19 | # if the underlying sampler has a set_epoch method, like 20 | # DistributedSampler, used for making each process see 21 | # a different split of the dataset, then set it 22 | if hasattr(self.batch_sampler.sampler, "set_epoch"): 23 | self.batch_sampler.sampler.set_epoch(iteration) 24 | for batch in self.batch_sampler: 25 | iteration += 1 26 | if iteration > self.num_iterations: 27 | break 28 | yield batch 29 | 30 | def __len__(self): 31 | return self.num_iterations 32 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/miscellaneous.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import errno 3 | import json 4 | import logging 5 | import os 6 | from .comm import is_main_process 7 | 8 | 9 | def mkdir(path): 10 | try: 11 | os.makedirs(path) 12 | except OSError as e: 13 | if e.errno != errno.EEXIST: 14 | raise 15 | 16 | 17 | def save_labels(dataset_list, output_dir): 18 | if is_main_process(): 19 | logger = logging.getLogger(__name__) 20 | 21 | ids_to_labels = {} 22 | for dataset in dataset_list: 23 | if hasattr(dataset, 'categories'): 24 | ids_to_labels.update(dataset.categories) 25 | else: 26 | logger.warning("Dataset [{}] has no categories attribute, labels.json file won't be created".format( 27 | dataset.__class__.__name__)) 28 | 29 | if ids_to_labels: 30 | labels_file = os.path.join(output_dir, 'labels.json') 31 | logger.info("Saving labels mapping into {}".format(labels_file)) 32 | with open(labels_file, 'w') as f: 33 | json.dump(ids_to_labels, f, indent=2) 34 | 35 | 36 | def save_config(cfg, path): 37 | if is_main_process(): 38 | with open(path, 'w') as f: 39 | f.write(cfg.dump()) 40 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/timer.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | 4 | import time 5 | import datetime 6 | 7 | 8 | class Timer(object): 9 | def __init__(self): 10 | self.reset() 11 | 12 | @property 13 | def average_time(self): 14 | return self.total_time / self.calls if self.calls > 0 else 0.0 15 | 16 | def tic(self): 17 | # using time.time instead of time.clock because time time.clock 18 | # does not normalize for multithreading 19 | self.start_time = time.time() 20 | 21 | def toc(self, average=True): 22 | self.add(time.time() - self.start_time) 23 | if average: 24 | return self.average_time 25 | else: 26 | return self.diff 27 | 28 | def add(self, time_diff): 29 | self.diff = time_diff 30 | self.total_time += self.diff 31 | self.calls += 1 32 | 33 | def reset(self): 34 | self.total_time = 0.0 35 | self.calls = 0 36 | self.start_time = 0.0 37 | self.diff = 0.0 38 | 39 | def avg_time_str(self): 40 | time_str = str(datetime.timedelta(seconds=self.average_time)) 41 | return time_str 42 | 43 | 44 | def get_time_str(time_diff): 45 | time_str = str(datetime.timedelta(seconds=time_diff)) 46 | return time_str 47 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/layers/_utils.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import glob 3 | import os.path 4 | 5 | import torch 6 | 7 | try: 8 | from torch.utils.cpp_extension import load as load_ext 9 | from torch.utils.cpp_extension import CUDA_HOME 10 | except ImportError: 11 | raise ImportError("The cpp layer extensions requires PyTorch 0.4 or higher") 12 | 13 | 14 | def _load_C_extensions(): 15 | this_dir = os.path.dirname(os.path.abspath(__file__)) 16 | this_dir = os.path.dirname(this_dir) 17 | this_dir = os.path.join(this_dir, "csrc") 18 | 19 | main_file = glob.glob(os.path.join(this_dir, "*.cpp")) 20 | source_cpu = glob.glob(os.path.join(this_dir, "cpu", "*.cpp")) 21 | source_cuda = glob.glob(os.path.join(this_dir, "cuda", "*.cu")) 22 | 23 | source = main_file + source_cpu 24 | 25 | extra_cflags = [] 26 | if torch.cuda.is_available() and CUDA_HOME is not None: 27 | source.extend(source_cuda) 28 | extra_cflags = ["-DWITH_CUDA"] 29 | source = [os.path.join(this_dir, s) for s in source] 30 | extra_include_paths = [this_dir] 31 | return load_ext( 32 | "torchvision", 33 | source, 34 | extra_cflags=extra_cflags, 35 | extra_include_paths=extra_include_paths, 36 | ) 37 | 38 | 39 | _C = _load_C_extensions() 40 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/env.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import os 3 | 4 | from maskrcnn_benchmark.utils.imports import import_file 5 | 6 | 7 | def setup_environment(): 8 | """Perform environment setup work. The default setup is a no-op, but this 9 | function allows the user to specify a Python source file that performs 10 | custom setup work that may be necessary to their computing environment. 11 | """ 12 | custom_module_path = os.environ.get("TORCH_DETECTRON_ENV_MODULE") 13 | if custom_module_path: 14 | setup_custom_environment(custom_module_path) 15 | else: 16 | # The default setup is a no-op 17 | pass 18 | 19 | 20 | def setup_custom_environment(custom_module_path): 21 | """Load custom environment setup from a Python source file and run the setup 22 | function. 23 | """ 24 | module = import_file("maskrcnn_benchmark.utils.env.custom_module", custom_module_path) 25 | assert hasattr(module, "setup_environment") and callable( 26 | module.setup_environment 27 | ), ( 28 | "Custom environment module defined in {} does not have the " 29 | "required callable attribute 'setup_environment'." 30 | ).format( 31 | custom_module_path 32 | ) 33 | module.setup_environment() 34 | 35 | 36 | # Force environment setup when this module is imported 37 | setup_environment() 38 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/keypoint_head/roi_keypoint_predictors.py: -------------------------------------------------------------------------------- 1 | from torch import nn 2 | 3 | from maskrcnn_benchmark import layers 4 | from maskrcnn_benchmark.modeling import registry 5 | 6 | 7 | @registry.ROI_KEYPOINT_PREDICTOR.register("KeypointRCNNPredictor") 8 | class KeypointRCNNPredictor(nn.Module): 9 | def __init__(self, cfg, in_channels): 10 | super(KeypointRCNNPredictor, self).__init__() 11 | input_features = in_channels 12 | num_keypoints = cfg.MODEL.ROI_KEYPOINT_HEAD.NUM_CLASSES 13 | deconv_kernel = 4 14 | self.kps_score_lowres = layers.ConvTranspose2d( 15 | input_features, 16 | num_keypoints, 17 | deconv_kernel, 18 | stride=2, 19 | padding=deconv_kernel // 2 - 1, 20 | ) 21 | nn.init.kaiming_normal_( 22 | self.kps_score_lowres.weight, mode="fan_out", nonlinearity="relu" 23 | ) 24 | nn.init.constant_(self.kps_score_lowres.bias, 0) 25 | self.up_scale = 2 26 | self.out_channels = num_keypoints 27 | 28 | def forward(self, x): 29 | x = self.kps_score_lowres(x) 30 | x = layers.interpolate( 31 | x, scale_factor=self.up_scale, mode="bilinear", align_corners=False 32 | ) 33 | return x 34 | 35 | 36 | def make_roi_keypoint_predictor(cfg, in_channels): 37 | func = registry.ROI_KEYPOINT_PREDICTOR[cfg.MODEL.ROI_KEYPOINT_HEAD.PREDICTOR] 38 | return func(cfg, in_channels) 39 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/layers/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | 4 | from .batch_norm import FrozenBatchNorm2d 5 | from .misc import Conv2d 6 | from .misc import DFConv2d 7 | from .misc import ConvTranspose2d 8 | from .misc import BatchNorm2d 9 | from .misc import interpolate 10 | from .nms import nms 11 | from .roi_align import ROIAlign 12 | from .roi_align import roi_align 13 | from .roi_pool import ROIPool 14 | from .roi_pool import roi_pool 15 | from .smooth_l1_loss import smooth_l1_loss 16 | from .sigmoid_focal_loss import SigmoidFocalLoss 17 | from .dcn.deform_conv_func import deform_conv, modulated_deform_conv 18 | from .dcn.deform_conv_module import DeformConv, ModulatedDeformConv, ModulatedDeformConvPack 19 | from .dcn.deform_pool_func import deform_roi_pooling 20 | from .dcn.deform_pool_module import DeformRoIPooling, DeformRoIPoolingPack, ModulatedDeformRoIPoolingPack 21 | 22 | 23 | __all__ = [ 24 | "nms", 25 | "roi_align", 26 | "ROIAlign", 27 | "roi_pool", 28 | "ROIPool", 29 | "smooth_l1_loss", 30 | "Conv2d", 31 | "DFConv2d", 32 | "ConvTranspose2d", 33 | "interpolate", 34 | "BatchNorm2d", 35 | "FrozenBatchNorm2d", 36 | "SigmoidFocalLoss", 37 | 'deform_conv', 38 | 'modulated_deform_conv', 39 | 'DeformConv', 40 | 'ModulatedDeformConv', 41 | 'ModulatedDeformConvPack', 42 | 'deform_roi_pooling', 43 | 'DeformRoIPooling', 44 | 'DeformRoIPoolingPack', 45 | 'ModulatedDeformRoIPoolingPack', 46 | ] 47 | 48 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/registry.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | 4 | def _register_generic(module_dict, module_name, module): 5 | assert module_name not in module_dict 6 | module_dict[module_name] = module 7 | 8 | 9 | class Registry(dict): 10 | ''' 11 | A helper class for managing registering modules, it extends a dictionary 12 | and provides a register functions. 13 | 14 | Eg. creeting a registry: 15 | some_registry = Registry({"default": default_module}) 16 | 17 | There're two ways of registering new modules: 18 | 1): normal way is just calling register function: 19 | def foo(): 20 | ... 21 | some_registry.register("foo_module", foo) 22 | 2): used as decorator when declaring the module: 23 | @some_registry.register("foo_module") 24 | @some_registry.register("foo_modeul_nickname") 25 | def foo(): 26 | ... 27 | 28 | Access of module is just like using a dictionary, eg: 29 | f = some_registry["foo_modeul"] 30 | ''' 31 | def __init__(self, *args, **kwargs): 32 | super(Registry, self).__init__(*args, **kwargs) 33 | 34 | def register(self, module_name, module=None): 35 | # used as function call 36 | if module is not None: 37 | _register_generic(self, module_name, module) 38 | return 39 | 40 | # used as decorator 41 | def register_fn(fn): 42 | _register_generic(self, module_name, fn) 43 | return fn 44 | 45 | return register_fn 46 | -------------------------------------------------------------------------------- /inference/color_instance.py: -------------------------------------------------------------------------------- 1 | from inference.colormap import colormap 2 | import cv2 3 | import numpy as np 4 | import os 5 | import tifffile as tiff 6 | 7 | 8 | def mkdir(out_folder): 9 | try: 10 | os.stat(os.path.dirname(out_folder + '/')) 11 | except: 12 | os.mkdir(os.path.dirname(out_folder + '/')) 13 | 14 | def color_instance(img_folder, ins_folder, out_folder): 15 | 16 | mkdir(out_folder) 17 | imglist = os.listdir(img_folder) 18 | 19 | for imgname in imglist: 20 | 21 | imgpath = os.path.join(img_folder, imgname) 22 | inspath = os.path.join(ins_folder, imgname.split('.')[0] + '.tif') 23 | 24 | ins_seg = tiff.imread(inspath) 25 | img = cv2.imread(imgpath) 26 | 27 | masknum = np.amax(ins_seg) 28 | #print(masknum) 29 | 30 | color_list = colormap() 31 | 32 | for idx in range(1, masknum + 1): 33 | color_mask = color_list[idx % len(color_list), 0:3] 34 | 35 | # bi_mask_map = (ins_seg == idx).astype(np.uint8) 36 | bi_mask_map = (ins_seg == idx) 37 | 38 | ins = np.nonzero(bi_mask_map) 39 | 40 | img = img.astype(np.float64) 41 | img[ins[0], ins[1], :] *= 0.3 42 | img[ins[0], ins[1], :] += 0.7 * color_mask 43 | 44 | out_name = imgname 45 | cv2.imwrite(os.path.join(out_folder, out_name), img.astype(np.uint8)) 46 | 47 | 48 | 49 | if __name__ == "__main__": 50 | 51 | img_folder = '' 52 | ins_folder = '' 53 | out_folder = '' 54 | 55 | color_instance(img_folder, ins_folder, out_folder) 56 | 57 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/csrc/vision.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | #include "nms.h" 3 | #include "ROIAlign.h" 4 | #include "ROIPool.h" 5 | #include "SigmoidFocalLoss.h" 6 | #include "deform_conv.h" 7 | #include "deform_pool.h" 8 | 9 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { 10 | m.def("nms", &nms, "non-maximum suppression"); 11 | m.def("roi_align_forward", &ROIAlign_forward, "ROIAlign_forward"); 12 | m.def("roi_align_backward", &ROIAlign_backward, "ROIAlign_backward"); 13 | m.def("roi_pool_forward", &ROIPool_forward, "ROIPool_forward"); 14 | m.def("roi_pool_backward", &ROIPool_backward, "ROIPool_backward"); 15 | m.def("sigmoid_focalloss_forward", &SigmoidFocalLoss_forward, "SigmoidFocalLoss_forward"); 16 | m.def("sigmoid_focalloss_backward", &SigmoidFocalLoss_backward, "SigmoidFocalLoss_backward"); 17 | // dcn-v2 18 | m.def("deform_conv_forward", &deform_conv_forward, "deform_conv_forward"); 19 | m.def("deform_conv_backward_input", &deform_conv_backward_input, "deform_conv_backward_input"); 20 | m.def("deform_conv_backward_parameters", &deform_conv_backward_parameters, "deform_conv_backward_parameters"); 21 | m.def("modulated_deform_conv_forward", &modulated_deform_conv_forward, "modulated_deform_conv_forward"); 22 | m.def("modulated_deform_conv_backward", &modulated_deform_conv_backward, "modulated_deform_conv_backward"); 23 | m.def("deform_psroi_pooling_forward", &deform_psroi_pooling_forward, "deform_psroi_pooling_forward"); 24 | m.def("deform_psroi_pooling_backward", &deform_psroi_pooling_backward, "deform_psroi_pooling_backward"); 25 | } -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/datasets/evaluation/coco/__init__.py: -------------------------------------------------------------------------------- 1 | from .coco_eval import do_coco_evaluation as do_orig_coco_evaluation 2 | from .coco_eval_wrapper import do_coco_evaluation as do_wrapped_coco_evaluation 3 | from maskrcnn_benchmark.data.datasets import AbstractDataset, COCODataset 4 | 5 | 6 | def coco_evaluation( 7 | dataset, 8 | predictions, 9 | output_folder, 10 | box_only, 11 | iou_types, 12 | expected_results, 13 | expected_results_sigma_tol, 14 | ): 15 | if isinstance(dataset, COCODataset): 16 | return do_orig_coco_evaluation( 17 | dataset=dataset, 18 | predictions=predictions, 19 | box_only=box_only, 20 | output_folder=output_folder, 21 | iou_types=iou_types, 22 | expected_results=expected_results, 23 | expected_results_sigma_tol=expected_results_sigma_tol, 24 | ) 25 | elif isinstance(dataset, AbstractDataset): 26 | return do_wrapped_coco_evaluation( 27 | dataset=dataset, 28 | predictions=predictions, 29 | box_only=box_only, 30 | output_folder=output_folder, 31 | iou_types=iou_types, 32 | expected_results=expected_results, 33 | expected_results_sigma_tol=expected_results_sigma_tol, 34 | ) 35 | else: 36 | raise NotImplementedError( 37 | ( 38 | "Ground truth dataset is not a COCODataset, " 39 | "nor it is derived from AbstractDataset: type(dataset)=" 40 | "%s" % type(dataset) 41 | ) 42 | ) 43 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/transforms/build.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from . import transforms as T 3 | 4 | 5 | def build_transforms(cfg, is_train=True): 6 | if is_train: 7 | min_size = cfg.INPUT.MIN_SIZE_TRAIN 8 | max_size = cfg.INPUT.MAX_SIZE_TRAIN 9 | flip_horizontal_prob = cfg.INPUT.HORIZONTAL_FLIP_PROB_TRAIN 10 | flip_vertical_prob = cfg.INPUT.VERTICAL_FLIP_PROB_TRAIN 11 | brightness = cfg.INPUT.BRIGHTNESS 12 | contrast = cfg.INPUT.CONTRAST 13 | saturation = cfg.INPUT.SATURATION 14 | hue = cfg.INPUT.HUE 15 | else: 16 | min_size = cfg.INPUT.MIN_SIZE_TEST 17 | max_size = cfg.INPUT.MAX_SIZE_TEST 18 | flip_horizontal_prob = 0.0 19 | flip_vertical_prob = 0.0 20 | brightness = 0.0 21 | contrast = 0.0 22 | saturation = 0.0 23 | hue = 0.0 24 | 25 | to_bgr255 = cfg.INPUT.TO_BGR255 26 | normalize_transform = T.Normalize( 27 | mean=cfg.INPUT.PIXEL_MEAN, std=cfg.INPUT.PIXEL_STD, to_bgr255=to_bgr255 28 | ) 29 | color_jitter = T.ColorJitter( 30 | brightness=brightness, 31 | contrast=contrast, 32 | saturation=saturation, 33 | hue=hue, 34 | ) 35 | 36 | transform = T.Compose( 37 | [ 38 | color_jitter, 39 | T.Resize(min_size, max_size), 40 | T.RandomHorizontalFlip(flip_horizontal_prob), 41 | T.RandomVerticalFlip(flip_vertical_prob), 42 | T.ToTensor(), 43 | normalize_transform, 44 | ] 45 | ) 46 | return transform 47 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/datasets/evaluation/coco/coco_eval_wrapper.py: -------------------------------------------------------------------------------- 1 | # COCO style evaluation for custom datasets derived from AbstractDataset 2 | # by botcs@github 3 | 4 | import logging 5 | import os 6 | import json 7 | 8 | from maskrcnn_benchmark.data.datasets.coco import COCODataset 9 | from .coco_eval import do_coco_evaluation as orig_evaluation 10 | from .abs_to_coco import convert_abstract_to_coco 11 | 12 | 13 | def do_coco_evaluation( 14 | dataset, 15 | predictions, 16 | box_only, 17 | output_folder, 18 | iou_types, 19 | expected_results, 20 | expected_results_sigma_tol, 21 | ): 22 | 23 | logger = logging.getLogger("maskrcnn_benchmark.inference") 24 | logger.info("Converting annotations to COCO format...") 25 | coco_annotation_dict = convert_abstract_to_coco(dataset) 26 | 27 | dataset_name = dataset.__class__.__name__ 28 | coco_annotation_path = os.path.join(output_folder, dataset_name + ".json") 29 | logger.info("Saving annotations to %s" % coco_annotation_path) 30 | with open(coco_annotation_path, "w") as f: 31 | json.dump(coco_annotation_dict, f, indent=2) 32 | 33 | logger.info("Loading annotations as COCODataset") 34 | coco_dataset = COCODataset( 35 | ann_file=coco_annotation_path, 36 | root="", 37 | remove_images_without_annotations=False, 38 | transforms=None, # transformations should be already saved to the json 39 | ) 40 | 41 | return orig_evaluation( 42 | dataset=coco_dataset, 43 | predictions=predictions, 44 | box_only=box_only, 45 | output_folder=output_folder, 46 | iou_types=iou_types, 47 | expected_results=expected_results, 48 | expected_results_sigma_tol=expected_results, 49 | ) 50 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/csrc/ROIPool.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | #pragma once 3 | 4 | #include "cpu/vision.h" 5 | 6 | #ifdef WITH_CUDA 7 | #include "cuda/vision.h" 8 | #endif 9 | 10 | 11 | std::tuple ROIPool_forward(const at::Tensor& input, 12 | const at::Tensor& rois, 13 | const float spatial_scale, 14 | const int pooled_height, 15 | const int pooled_width) { 16 | if (input.type().is_cuda()) { 17 | #ifdef WITH_CUDA 18 | return ROIPool_forward_cuda(input, rois, spatial_scale, pooled_height, pooled_width); 19 | #else 20 | AT_ERROR("Not compiled with GPU support"); 21 | #endif 22 | } 23 | AT_ERROR("Not implemented on the CPU"); 24 | } 25 | 26 | at::Tensor ROIPool_backward(const at::Tensor& grad, 27 | const at::Tensor& input, 28 | const at::Tensor& rois, 29 | const at::Tensor& argmax, 30 | const float spatial_scale, 31 | const int pooled_height, 32 | const int pooled_width, 33 | const int batch_size, 34 | const int channels, 35 | const int height, 36 | const int width) { 37 | if (grad.type().is_cuda()) { 38 | #ifdef WITH_CUDA 39 | return ROIPool_backward_cuda(grad, input, rois, argmax, spatial_scale, pooled_height, pooled_width, batch_size, channels, height, width); 40 | #else 41 | AT_ERROR("Not compiled with GPU support"); 42 | #endif 43 | } 44 | AT_ERROR("Not implemented on the CPU"); 45 | } 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/csrc/ROIAlign.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | #pragma once 3 | 4 | #include "cpu/vision.h" 5 | 6 | #ifdef WITH_CUDA 7 | #include "cuda/vision.h" 8 | #endif 9 | 10 | // Interface for Python 11 | at::Tensor ROIAlign_forward(const at::Tensor& input, 12 | const at::Tensor& rois, 13 | const float spatial_scale, 14 | const int pooled_height, 15 | const int pooled_width, 16 | const int sampling_ratio) { 17 | if (input.type().is_cuda()) { 18 | #ifdef WITH_CUDA 19 | return ROIAlign_forward_cuda(input, rois, spatial_scale, pooled_height, pooled_width, sampling_ratio); 20 | #else 21 | AT_ERROR("Not compiled with GPU support"); 22 | #endif 23 | } 24 | return ROIAlign_forward_cpu(input, rois, spatial_scale, pooled_height, pooled_width, sampling_ratio); 25 | } 26 | 27 | at::Tensor ROIAlign_backward(const at::Tensor& grad, 28 | const at::Tensor& rois, 29 | const float spatial_scale, 30 | const int pooled_height, 31 | const int pooled_width, 32 | const int batch_size, 33 | const int channels, 34 | const int height, 35 | const int width, 36 | const int sampling_ratio) { 37 | if (grad.type().is_cuda()) { 38 | #ifdef WITH_CUDA 39 | return ROIAlign_backward_cuda(grad, rois, spatial_scale, pooled_height, pooled_width, batch_size, channels, height, width, sampling_ratio); 40 | #else 41 | AT_ERROR("Not compiled with GPU support"); 42 | #endif 43 | } 44 | AT_ERROR("Not implemented on the CPU"); 45 | } 46 | 47 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/rpn/utils.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | """ 3 | Utility functions minipulating the prediction layers 4 | """ 5 | 6 | from ..utils import cat 7 | 8 | import torch 9 | 10 | def permute_and_flatten(layer, N, A, C, H, W): 11 | layer = layer.view(N, -1, C, H, W) 12 | layer = layer.permute(0, 3, 4, 1, 2) 13 | layer = layer.reshape(N, -1, C) 14 | return layer 15 | 16 | 17 | def concat_box_prediction_layers(box_cls, box_regression): 18 | box_cls_flattened = [] 19 | box_regression_flattened = [] 20 | # for each feature level, permute the outputs to make them be in the 21 | # same format as the labels. Note that the labels are computed for 22 | # all feature levels concatenated, so we keep the same representation 23 | # for the objectness and the box_regression 24 | for box_cls_per_level, box_regression_per_level in zip( 25 | box_cls, box_regression 26 | ): 27 | N, AxC, H, W = box_cls_per_level.shape 28 | Ax4 = box_regression_per_level.shape[1] 29 | A = Ax4 // 4 30 | C = AxC // A 31 | box_cls_per_level = permute_and_flatten( 32 | box_cls_per_level, N, A, C, H, W 33 | ) 34 | box_cls_flattened.append(box_cls_per_level) 35 | 36 | box_regression_per_level = permute_and_flatten( 37 | box_regression_per_level, N, A, 4, H, W 38 | ) 39 | box_regression_flattened.append(box_regression_per_level) 40 | # concatenate on the first dimension (representing the feature levels), to 41 | # take into account the way the labels were generated (with all feature maps 42 | # being concatenated as well) 43 | box_cls = cat(box_cls_flattened, dim=1).reshape(-1, C) 44 | box_regression = cat(box_regression_flattened, dim=1).reshape(-1, 4) 45 | return box_cls, box_regression 46 | -------------------------------------------------------------------------------- /inference/epfl2vnc_infer.py: -------------------------------------------------------------------------------- 1 | 2 | import cv2.cv2 as cv2 3 | from maskrcnn_benchmark.utils.miscellaneous import mkdir 4 | import tifffile as tiff 5 | from inference.cell_predictor import CellDemo 6 | from inference.metrics import mask2out, removeoverlap 7 | from maskrcnn_benchmark.config import cfg 8 | import os 9 | import numpy as np 10 | from maskrcnn_benchmark.modeling.detector import build_detection_model 11 | 12 | 13 | 14 | def infer_epfl2vnc(wts_root, out_pred_root): 15 | 16 | 17 | config_file = "../configs/uda_nuclei_seg/e2e_mask_rcnn_R_101_FPN_1x_gn_epfl2vnc.yaml" 18 | 19 | cfg.merge_from_file(config_file) 20 | 21 | cfg.merge_from_list(["MODEL.DEVICE", "cuda"]) 22 | model = build_detection_model(cfg) 23 | 24 | cell_demo = CellDemo( 25 | cfg, 26 | min_image_size=1024, 27 | confidence_threshold=0.5, 28 | weight= wts_root, 29 | model=model 30 | ) 31 | 32 | test_root_name = '' # with testing images 33 | 34 | mkdir(out_pred_root) 35 | 36 | test_imgs = os.listdir(test_root_name) 37 | for img_name in test_imgs: 38 | 39 | if img_name.endswith(".png"): 40 | image = cv2.imread(os.path.join(test_root_name, img_name)) 41 | 42 | # compute predictions 43 | predictions, mask_list = cell_demo.run_on_opencv_image(image) 44 | masks_no_overlap, bi_map, num_mask = removeoverlap(mask_list) 45 | pred_ins = mask2out(masks_no_overlap, num_mask) 46 | 47 | 48 | cv2.imwrite(os.path.join(out_pred_root, img_name), predictions) 49 | cv2.imwrite(os.path.join(out_pred_root, 'bi_mask_' + img_name), (bi_map*255).astype(np.uint8)) 50 | 51 | pred_ins_name = os.path.join(out_pred_root, img_name.split('.')[0] + '.tif') 52 | tiff.imsave(pred_ins_name, pred_ins) 53 | 54 | 55 | 56 | if __name__ == "__main__": 57 | wts_root = '' 58 | out_pred_root = '' 59 | 60 | infer_epfl2vnc(wts_root, out_pred_root) -------------------------------------------------------------------------------- /inference/fluo2tnbc_infer.py: -------------------------------------------------------------------------------- 1 | 2 | import cv2.cv2 as cv2 3 | from maskrcnn_benchmark.utils.miscellaneous import mkdir 4 | import tifffile as tiff 5 | from inference.cell_predictor import CellDemo 6 | from inference.metrics import mask2out, removeoverlap 7 | from maskrcnn_benchmark.config import cfg 8 | import os 9 | import numpy as np 10 | from maskrcnn_benchmark.modeling.detector import build_detection_model 11 | 12 | 13 | 14 | def infer_fluo2tnbc(wts_root, out_pred_root): 15 | 16 | 17 | config_file = "../configs/uda_nuclei_seg/e2e_mask_rcnn_R_101_FPN_1x_gn_fluo2tnbc.yaml" 18 | 19 | cfg.merge_from_file(config_file) 20 | 21 | cfg.merge_from_list(["MODEL.DEVICE", "cuda"]) 22 | model = build_detection_model(cfg) 23 | 24 | cell_demo = CellDemo( 25 | cfg, 26 | min_image_size=512, 27 | confidence_threshold=0.5, 28 | weight= wts_root, 29 | model=model 30 | ) 31 | 32 | test_root_name = '' # with testing images 33 | 34 | mkdir(out_pred_root) 35 | 36 | test_imgs = os.listdir(test_root_name) 37 | for img_name in test_imgs: 38 | 39 | if img_name.endswith(".png"): 40 | image = cv2.imread(os.path.join(test_root_name, img_name)) 41 | 42 | # compute predictions 43 | predictions, mask_list = cell_demo.run_on_opencv_image(image) 44 | masks_no_overlap, bi_map, num_mask = removeoverlap(mask_list) 45 | pred_ins = mask2out(masks_no_overlap, num_mask) 46 | 47 | 48 | cv2.imwrite(os.path.join(out_pred_root, img_name), predictions) 49 | cv2.imwrite(os.path.join(out_pred_root, 'bi_mask_' + img_name), (bi_map*255).astype(np.uint8)) 50 | 51 | pred_ins_name = os.path.join(out_pred_root, img_name.split('.')[0] + '.tif') 52 | tiff.imsave(pred_ins_name, pred_ins) 53 | 54 | 55 | 56 | if __name__ == "__main__": 57 | wts_root = '' 58 | out_pred_root = '' 59 | 60 | infer_fluo2tnbc(wts_root, out_pred_root) -------------------------------------------------------------------------------- /inference/fluo2tcga_infer.py: -------------------------------------------------------------------------------- 1 | 2 | import cv2 3 | import os 4 | import numpy as np 5 | from maskrcnn_benchmark.utils.miscellaneous import mkdir 6 | import tifffile as tiff 7 | from inference.metrics import mask2out, removeoverlap 8 | from maskrcnn_benchmark.config import cfg 9 | from inference.cell_predictor import CellDemo 10 | from maskrcnn_benchmark.modeling.detector import build_detection_model 11 | 12 | def infer_fluo2tcga(wts_root, out_pred_root): 13 | 14 | config_file = "../configs/uda_nuclei_seg/e2e_mask_rcnn_R_101_FPN_1x_gn_fluo2tcga.yaml" 15 | 16 | # update the config options with the config file 17 | cfg.merge_from_file(config_file) 18 | # manual override some options 19 | cfg.merge_from_list(["MODEL.DEVICE", "cuda"]) 20 | model = build_detection_model(cfg) 21 | 22 | cell_demo = CellDemo( 23 | cfg, 24 | min_image_size=1000, 25 | confidence_threshold=0.5, 26 | weight= wts_root, 27 | model=model 28 | ) 29 | 30 | # put your testing images 31 | test_root_name = '' 32 | 33 | 34 | # output saving root 35 | mkdir(out_pred_root) 36 | 37 | test_imgs = os.listdir(test_root_name) 38 | for img_name in test_imgs: 39 | 40 | if img_name.endswith(".png"): 41 | image = cv2.imread(os.path.join(test_root_name, img_name)) 42 | 43 | predictions, mask_list = cell_demo.run_on_opencv_image(image) 44 | 45 | masks_no_overlap, bi_map, num_mask = removeoverlap(mask_list) 46 | 47 | out_name = os.path.join(out_pred_root, img_name.split('.')[0] + '.tif') 48 | pred_ins = mask2out(masks_no_overlap, num_mask) 49 | 50 | cv2.imwrite(os.path.join(out_pred_root, img_name), predictions) 51 | cv2.imwrite(os.path.join(out_pred_root, 'bi_mask_' + img_name), (bi_map * 255).astype(np.uint8)) 52 | tiff.imsave(out_name, pred_ins) 53 | 54 | 55 | if __name__ == "__main__": 56 | wts_root = '' 57 | out_pred_root = '' 58 | 59 | infer_fluo2tcga(wts_root, out_pred_root) 60 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/solver/lr_scheduler.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from bisect import bisect_right 3 | 4 | import torch 5 | 6 | 7 | # FIXME ideally this would be achieved with a CombinedLRScheduler, 8 | # separating MultiStepLR with WarmupLR 9 | # but the current LRScheduler design doesn't allow it 10 | class WarmupMultiStepLR(torch.optim.lr_scheduler._LRScheduler): 11 | def __init__( 12 | self, 13 | optimizer, 14 | milestones, 15 | gamma=0.1, 16 | warmup_factor=1.0 / 3, 17 | warmup_iters=500, 18 | warmup_method="linear", 19 | last_epoch=-1, 20 | ): 21 | if not list(milestones) == sorted(milestones): 22 | raise ValueError( 23 | "Milestones should be a list of" " increasing integers. Got {}", 24 | milestones, 25 | ) 26 | 27 | if warmup_method not in ("constant", "linear"): 28 | raise ValueError( 29 | "Only 'constant' or 'linear' warmup_method accepted" 30 | "got {}".format(warmup_method) 31 | ) 32 | self.milestones = milestones 33 | self.gamma = gamma 34 | self.warmup_factor = warmup_factor 35 | self.warmup_iters = warmup_iters 36 | self.warmup_method = warmup_method 37 | super(WarmupMultiStepLR, self).__init__(optimizer, last_epoch) 38 | 39 | def get_lr(self): 40 | warmup_factor = 1 41 | if self.last_epoch < self.warmup_iters: 42 | if self.warmup_method == "constant": 43 | warmup_factor = self.warmup_factor 44 | elif self.warmup_method == "linear": 45 | alpha = float(self.last_epoch) / self.warmup_iters 46 | warmup_factor = self.warmup_factor * (1 - alpha) + alpha 47 | return [ 48 | base_lr 49 | * warmup_factor 50 | * self.gamma ** bisect_right(self.milestones, self.last_epoch) 51 | for base_lr in self.base_lrs 52 | ] 53 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/csrc/deform_pool.h: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | #pragma once 3 | #include "cpu/vision.h" 4 | 5 | #ifdef WITH_CUDA 6 | #include "cuda/vision.h" 7 | #endif 8 | 9 | 10 | // Interface for Python 11 | void deform_psroi_pooling_forward( 12 | at::Tensor input, 13 | at::Tensor bbox, 14 | at::Tensor trans, 15 | at::Tensor out, 16 | at::Tensor top_count, 17 | const int no_trans, 18 | const float spatial_scale, 19 | const int output_dim, 20 | const int group_size, 21 | const int pooled_size, 22 | const int part_size, 23 | const int sample_per_part, 24 | const float trans_std) 25 | { 26 | if (input.type().is_cuda()) { 27 | #ifdef WITH_CUDA 28 | return deform_psroi_pooling_cuda_forward( 29 | input, bbox, trans, out, top_count, 30 | no_trans, spatial_scale, output_dim, group_size, 31 | pooled_size, part_size, sample_per_part, trans_std 32 | ); 33 | #else 34 | AT_ERROR("Not compiled with GPU support"); 35 | #endif 36 | } 37 | AT_ERROR("Not implemented on the CPU"); 38 | } 39 | 40 | 41 | void deform_psroi_pooling_backward( 42 | at::Tensor out_grad, 43 | at::Tensor input, 44 | at::Tensor bbox, 45 | at::Tensor trans, 46 | at::Tensor top_count, 47 | at::Tensor input_grad, 48 | at::Tensor trans_grad, 49 | const int no_trans, 50 | const float spatial_scale, 51 | const int output_dim, 52 | const int group_size, 53 | const int pooled_size, 54 | const int part_size, 55 | const int sample_per_part, 56 | const float trans_std) 57 | { 58 | if (input.type().is_cuda()) { 59 | #ifdef WITH_CUDA 60 | return deform_psroi_pooling_cuda_backward( 61 | out_grad, input, bbox, trans, top_count, input_grad, trans_grad, 62 | no_trans, spatial_scale, output_dim, group_size, pooled_size, 63 | part_size, sample_per_part, trans_std 64 | ); 65 | #else 66 | AT_ERROR("Not compiled with GPU support"); 67 | #endif 68 | } 69 | AT_ERROR("Not implemented on the CPU"); 70 | } 71 | -------------------------------------------------------------------------------- /auxiliary_nuclei_inpaint.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | import numpy as np 4 | from PIL import Image 5 | from skimage.color import rgb2hed 6 | from scipy import stats 7 | 8 | 9 | 10 | 11 | def nuclei_inpaint(img_path, lbl_path, out_path): 12 | 13 | try: 14 | os.stat(os.path.dirname(out_path + '/')) 15 | except: 16 | os.mkdir(os.path.dirname(out_path + '/')) 17 | 18 | 19 | img_list = os.listdir(img_path) 20 | 21 | for image_name in img_list: 22 | if not image_name.endswith('.png'): 23 | continue 24 | img_abs_path = os.path.join(img_path, image_name) 25 | img_rgb = np.array(Image.open(img_abs_path).convert('RGB')) 26 | img_hed = rgb2hed((img_rgb.astype(np.float32)/255.0).astype(np.float32)) 27 | img_gray = (255 * (img_hed[:,:,0] - img_hed[:,:,0].min()) / (img_hed[:,:,0].max() - img_hed[:,:,0].min())).astype(np.uint8) 28 | nuclei_color_threshold, thresholded = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY+cv2.THRESH_OTSU) 29 | 30 | nuclei_color_threshold_per = stats.percentileofscore(img_gray.flatten(), nuclei_color_threshold) 31 | nuclei_mask_pred = (img_hed[:, :, 0] > np.percentile(img_hed[:, :, 0], nuclei_color_threshold_per)) 32 | 33 | 34 | gt_abs_path = os.path.join(lbl_path, image_name.split('.')[0] + '.png') 35 | gt = cv2.imread(gt_abs_path)[:,:,0] 36 | gt_bi = (gt > 0).astype(np.uint8) 37 | 38 | nuclei_mask_extra = (nuclei_mask_pred - gt_bi) == 1 39 | 40 | img_inpaint = cv2.inpaint(img_rgb, nuclei_mask_extra.astype(np.uint8), 3, cv2.INPAINT_TELEA) 41 | img_inpaint = cv2.cvtColor(img_inpaint, cv2.COLOR_BGR2RGB) 42 | 43 | inpaint_abs_name = os.path.join(out_path, image_name) 44 | cv2.imwrite(inpaint_abs_name, img_inpaint) 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | if __name__ == "__main__": 53 | 54 | img_path = 'nuclei_inpaint/raw_synthesized_patches' 55 | lbl_path = 'nuclei_inpaint/synthesized_labels' 56 | out_path = 'nuclei_inpaint/inpaint_synthesized_patches' 57 | 58 | nuclei_inpaint(img_path, lbl_path, out_path) -------------------------------------------------------------------------------- /tests/test_backbones.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | import unittest 4 | import copy 5 | import torch 6 | # import modules to to register backbones 7 | from maskrcnn_benchmark.modeling.backbone import build_backbone # NoQA 8 | from maskrcnn_benchmark.modeling import registry 9 | from maskrcnn_benchmark.config import cfg as g_cfg 10 | from utils import load_config 11 | 12 | 13 | # overwrite configs if specified, otherwise default config is used 14 | BACKBONE_CFGS = { 15 | "R-50-FPN": "e2e_faster_rcnn_R_50_FPN_1x.yaml", 16 | "R-101-FPN": "e2e_faster_rcnn_R_101_FPN_1x.yaml", 17 | "R-152-FPN": "e2e_faster_rcnn_R_101_FPN_1x.yaml", 18 | "R-50-FPN-RETINANET": "retinanet/retinanet_R-50-FPN_1x.yaml", 19 | "R-101-FPN-RETINANET": "retinanet/retinanet_R-101-FPN_1x.yaml", 20 | } 21 | 22 | 23 | class TestBackbones(unittest.TestCase): 24 | def test_build_backbones(self): 25 | ''' Make sure backbones run ''' 26 | 27 | self.assertGreater(len(registry.BACKBONES), 0) 28 | 29 | for name, backbone_builder in registry.BACKBONES.items(): 30 | print('Testing {}...'.format(name)) 31 | if name in BACKBONE_CFGS: 32 | cfg = load_config(BACKBONE_CFGS[name]) 33 | else: 34 | # Use default config if config file is not specified 35 | cfg = copy.deepcopy(g_cfg) 36 | backbone = backbone_builder(cfg) 37 | 38 | # make sures the backbone has `out_channels` 39 | self.assertIsNotNone( 40 | getattr(backbone, 'out_channels', None), 41 | 'Need to provide out_channels for backbone {}'.format(name) 42 | ) 43 | 44 | N, C_in, H, W = 2, 3, 224, 256 45 | input = torch.rand([N, C_in, H, W], dtype=torch.float32) 46 | out = backbone(input) 47 | for cur_out in out: 48 | self.assertEqual( 49 | cur_out.shape[:2], 50 | torch.Size([N, backbone.out_channels]) 51 | ) 52 | 53 | 54 | if __name__ == "__main__": 55 | unittest.main() 56 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/keypoint_head/roi_keypoint_feature_extractors.py: -------------------------------------------------------------------------------- 1 | from torch import nn 2 | from torch.nn import functional as F 3 | 4 | from maskrcnn_benchmark.modeling import registry 5 | from maskrcnn_benchmark.modeling.poolers import Pooler 6 | 7 | from maskrcnn_benchmark.layers import Conv2d 8 | 9 | 10 | @registry.ROI_KEYPOINT_FEATURE_EXTRACTORS.register("KeypointRCNNFeatureExtractor") 11 | class KeypointRCNNFeatureExtractor(nn.Module): 12 | def __init__(self, cfg, in_channels): 13 | super(KeypointRCNNFeatureExtractor, self).__init__() 14 | 15 | resolution = cfg.MODEL.ROI_KEYPOINT_HEAD.POOLER_RESOLUTION 16 | scales = cfg.MODEL.ROI_KEYPOINT_HEAD.POOLER_SCALES 17 | sampling_ratio = cfg.MODEL.ROI_KEYPOINT_HEAD.POOLER_SAMPLING_RATIO 18 | pooler = Pooler( 19 | output_size=(resolution, resolution), 20 | scales=scales, 21 | sampling_ratio=sampling_ratio, 22 | ) 23 | self.pooler = pooler 24 | 25 | input_features = in_channels 26 | layers = cfg.MODEL.ROI_KEYPOINT_HEAD.CONV_LAYERS 27 | next_feature = input_features 28 | self.blocks = [] 29 | for layer_idx, layer_features in enumerate(layers, 1): 30 | layer_name = "conv_fcn{}".format(layer_idx) 31 | module = Conv2d(next_feature, layer_features, 3, stride=1, padding=1) 32 | nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu") 33 | nn.init.constant_(module.bias, 0) 34 | self.add_module(layer_name, module) 35 | next_feature = layer_features 36 | self.blocks.append(layer_name) 37 | self.out_channels = layer_features 38 | 39 | def forward(self, x, proposals): 40 | x = self.pooler(x, proposals) 41 | for layer_name in self.blocks: 42 | x = F.relu(getattr(self, layer_name)(x)) 43 | return x 44 | 45 | 46 | def make_roi_keypoint_feature_extractor(cfg, in_channels): 47 | func = registry.ROI_KEYPOINT_FEATURE_EXTRACTORS[ 48 | cfg.MODEL.ROI_KEYPOINT_HEAD.FEATURE_EXTRACTOR 49 | ] 50 | return func(cfg, in_channels) 51 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/metric_logger.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from collections import defaultdict 3 | from collections import deque 4 | 5 | import torch 6 | 7 | 8 | class SmoothedValue(object): 9 | """Track a series of values and provide access to smoothed values over a 10 | window or the global series average. 11 | """ 12 | 13 | def __init__(self, window_size=20): 14 | self.deque = deque(maxlen=window_size) 15 | self.series = [] 16 | self.total = 0.0 17 | self.count = 0 18 | 19 | def update(self, value): 20 | self.deque.append(value) 21 | self.series.append(value) 22 | self.count += 1 23 | self.total += value 24 | 25 | @property 26 | def median(self): 27 | d = torch.tensor(list(self.deque)) 28 | return d.median().item() 29 | 30 | @property 31 | def avg(self): 32 | d = torch.tensor(list(self.deque)) 33 | return d.mean().item() 34 | 35 | @property 36 | def global_avg(self): 37 | return self.total / self.count 38 | 39 | 40 | class MetricLogger(object): 41 | def __init__(self, delimiter="\t"): 42 | self.meters = defaultdict(SmoothedValue) 43 | self.delimiter = delimiter 44 | 45 | def update(self, **kwargs): 46 | for k, v in kwargs.items(): 47 | if isinstance(v, torch.Tensor): 48 | v = v.item() 49 | assert isinstance(v, (float, int)) 50 | self.meters[k].update(v) 51 | 52 | def __getattr__(self, attr): 53 | if attr in self.meters: 54 | return self.meters[attr] 55 | if attr in self.__dict__: 56 | return self.__dict__[attr] 57 | raise AttributeError("'{}' object has no attribute '{}'".format( 58 | type(self).__name__, attr)) 59 | 60 | def __str__(self): 61 | loss_str = [] 62 | for name, meter in self.meters.items(): 63 | loss_str.append( 64 | "{}: {:.4f} ({:.4f})".format(name, meter.median, meter.global_avg) 65 | ) 66 | return self.delimiter.join(loss_str) 67 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/layers/roi_pool.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | from torch import nn 4 | from torch.autograd import Function 5 | from torch.autograd.function import once_differentiable 6 | from torch.nn.modules.utils import _pair 7 | 8 | from maskrcnn_benchmark import _C 9 | 10 | from apex import amp 11 | 12 | class _ROIPool(Function): 13 | @staticmethod 14 | def forward(ctx, input, roi, output_size, spatial_scale): 15 | ctx.output_size = _pair(output_size) 16 | ctx.spatial_scale = spatial_scale 17 | ctx.input_shape = input.size() 18 | output, argmax = _C.roi_pool_forward( 19 | input, roi, spatial_scale, output_size[0], output_size[1] 20 | ) 21 | ctx.save_for_backward(input, roi, argmax) 22 | return output 23 | 24 | @staticmethod 25 | @once_differentiable 26 | def backward(ctx, grad_output): 27 | input, rois, argmax = ctx.saved_tensors 28 | output_size = ctx.output_size 29 | spatial_scale = ctx.spatial_scale 30 | bs, ch, h, w = ctx.input_shape 31 | grad_input = _C.roi_pool_backward( 32 | grad_output, 33 | input, 34 | rois, 35 | argmax, 36 | spatial_scale, 37 | output_size[0], 38 | output_size[1], 39 | bs, 40 | ch, 41 | h, 42 | w, 43 | ) 44 | return grad_input, None, None, None 45 | 46 | 47 | roi_pool = _ROIPool.apply 48 | 49 | 50 | class ROIPool(nn.Module): 51 | def __init__(self, output_size, spatial_scale): 52 | super(ROIPool, self).__init__() 53 | self.output_size = output_size 54 | self.spatial_scale = spatial_scale 55 | 56 | @amp.float_function 57 | def forward(self, input, rois): 58 | return roi_pool(input, rois, self.output_size, self.spatial_scale) 59 | 60 | def __repr__(self): 61 | tmpstr = self.__class__.__name__ + "(" 62 | tmpstr += "output_size=" + str(self.output_size) 63 | tmpstr += ", spatial_scale=" + str(self.spatial_scale) 64 | tmpstr += ")" 65 | return tmpstr 66 | -------------------------------------------------------------------------------- /tests/test_rpn_heads.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | import unittest 4 | import copy 5 | import torch 6 | # import modules to to register rpn heads 7 | from maskrcnn_benchmark.modeling.backbone import build_backbone # NoQA 8 | from maskrcnn_benchmark.modeling.rpn.rpn import build_rpn # NoQA 9 | from maskrcnn_benchmark.modeling import registry 10 | from maskrcnn_benchmark.config import cfg as g_cfg 11 | from utils import load_config 12 | 13 | 14 | # overwrite configs if specified, otherwise default config is used 15 | RPN_CFGS = { 16 | } 17 | 18 | 19 | class TestRPNHeads(unittest.TestCase): 20 | def test_build_rpn_heads(self): 21 | ''' Make sure rpn heads run ''' 22 | 23 | self.assertGreater(len(registry.RPN_HEADS), 0) 24 | 25 | in_channels = 64 26 | num_anchors = 10 27 | 28 | for name, builder in registry.RPN_HEADS.items(): 29 | print('Testing {}...'.format(name)) 30 | if name in RPN_CFGS: 31 | cfg = load_config(RPN_CFGS[name]) 32 | else: 33 | # Use default config if config file is not specified 34 | cfg = copy.deepcopy(g_cfg) 35 | 36 | rpn = builder(cfg, in_channels, num_anchors) 37 | 38 | N, C_in, H, W = 2, in_channels, 24, 32 39 | input = torch.rand([N, C_in, H, W], dtype=torch.float32) 40 | LAYERS = 3 41 | out = rpn([input] * LAYERS) 42 | self.assertEqual(len(out), 2) 43 | logits, bbox_reg = out 44 | for idx in range(LAYERS): 45 | self.assertEqual( 46 | logits[idx].shape, 47 | torch.Size([ 48 | input.shape[0], num_anchors, 49 | input.shape[2], input.shape[3], 50 | ]) 51 | ) 52 | self.assertEqual( 53 | bbox_reg[idx].shape, 54 | torch.Size([ 55 | logits[idx].shape[0], num_anchors * 4, 56 | logits[idx].shape[2], logits[idx].shape[3], 57 | ]), 58 | ) 59 | 60 | 61 | if __name__ == "__main__": 62 | unittest.main() 63 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/keypoint_head/keypoint_head.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | from .roi_keypoint_feature_extractors import make_roi_keypoint_feature_extractor 4 | from .roi_keypoint_predictors import make_roi_keypoint_predictor 5 | from .inference import make_roi_keypoint_post_processor 6 | from .loss import make_roi_keypoint_loss_evaluator 7 | 8 | 9 | class ROIKeypointHead(torch.nn.Module): 10 | def __init__(self, cfg, in_channels): 11 | super(ROIKeypointHead, self).__init__() 12 | self.cfg = cfg.clone() 13 | self.feature_extractor = make_roi_keypoint_feature_extractor(cfg, in_channels) 14 | self.predictor = make_roi_keypoint_predictor( 15 | cfg, self.feature_extractor.out_channels) 16 | self.post_processor = make_roi_keypoint_post_processor(cfg) 17 | self.loss_evaluator = make_roi_keypoint_loss_evaluator(cfg) 18 | 19 | def forward(self, features, proposals, targets=None): 20 | """ 21 | Arguments: 22 | features (list[Tensor]): feature-maps from possibly several levels 23 | proposals (list[BoxList]): proposal boxes 24 | targets (list[BoxList], optional): the ground-truth targets. 25 | 26 | Returns: 27 | x (Tensor): the result of the feature extractor 28 | proposals (list[BoxList]): during training, the original proposals 29 | are returned. During testing, the predicted boxlists are returned 30 | with the `mask` field set 31 | losses (dict[Tensor]): During training, returns the losses for the 32 | head. During testing, returns an empty dict. 33 | """ 34 | if self.training: 35 | with torch.no_grad(): 36 | proposals = self.loss_evaluator.subsample(proposals, targets) 37 | 38 | x = self.feature_extractor(features, proposals) 39 | kp_logits = self.predictor(x) 40 | 41 | if not self.training: 42 | result = self.post_processor(kp_logits, proposals) 43 | return x, result, {} 44 | 45 | loss_kp = self.loss_evaluator(proposals, kp_logits) 46 | 47 | return x, proposals, dict(loss_kp=loss_kp) 48 | 49 | 50 | def build_roi_keypoint_head(cfg, in_channels): 51 | return ROIKeypointHead(cfg, in_channels) 52 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | #!/usr/bin/env python 3 | 4 | import glob 5 | import os 6 | 7 | import torch 8 | from setuptools import find_packages 9 | from setuptools import setup 10 | from torch.utils.cpp_extension import CUDA_HOME 11 | from torch.utils.cpp_extension import CppExtension 12 | from torch.utils.cpp_extension import CUDAExtension 13 | 14 | requirements = ["torch", "torchvision"] 15 | 16 | 17 | def get_extensions(): 18 | this_dir = os.path.dirname(os.path.abspath(__file__)) 19 | extensions_dir = os.path.join(this_dir, "maskrcnn_benchmark", "csrc") 20 | 21 | main_file = glob.glob(os.path.join(extensions_dir, "*.cpp")) 22 | source_cpu = glob.glob(os.path.join(extensions_dir, "cpu", "*.cpp")) 23 | source_cuda = glob.glob(os.path.join(extensions_dir, "cuda", "*.cu")) 24 | 25 | sources = main_file + source_cpu 26 | extension = CppExtension 27 | 28 | extra_compile_args = {"cxx": []} 29 | define_macros = [] 30 | 31 | if (torch.cuda.is_available() and CUDA_HOME is not None) or os.getenv("FORCE_CUDA", "0") == "1": 32 | extension = CUDAExtension 33 | sources += source_cuda 34 | define_macros += [("WITH_CUDA", None)] 35 | extra_compile_args["nvcc"] = [ 36 | "-DCUDA_HAS_FP16=1", 37 | "-D__CUDA_NO_HALF_OPERATORS__", 38 | "-D__CUDA_NO_HALF_CONVERSIONS__", 39 | "-D__CUDA_NO_HALF2_OPERATORS__", 40 | ] 41 | 42 | sources = [os.path.join(extensions_dir, s) for s in sources] 43 | 44 | include_dirs = [extensions_dir] 45 | 46 | ext_modules = [ 47 | extension( 48 | "maskrcnn_benchmark._C", 49 | sources, 50 | include_dirs=include_dirs, 51 | define_macros=define_macros, 52 | extra_compile_args=extra_compile_args, 53 | ) 54 | ] 55 | 56 | return ext_modules 57 | 58 | 59 | setup( 60 | name="maskrcnn_benchmark", 61 | version="0.1", 62 | author="fmassa", 63 | url="https://github.com/facebookresearch/maskrcnn-benchmark", 64 | description="object detection in pytorch", 65 | packages=find_packages(exclude=("configs", "tests",)), 66 | # install_requires=requirements, 67 | ext_modules=get_extensions(), 68 | cmdclass={"build_ext": torch.utils.cpp_extension.BuildExtension}, 69 | ) 70 | -------------------------------------------------------------------------------- /configs/uda_nuclei_seg/e2e_mask_rcnn_R_101_FPN_1x_gn_fluo2tcga.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "GeneralizedRCNN" 3 | WEIGHT: "catalog://ImageNetPretrained/MSRA/R-101-GN" 4 | BACKBONE: 5 | CONV_BODY: "R-101-FPN" 6 | RESNETS: 7 | BACKBONE_OUT_CHANNELS: 256 8 | STRIDE_IN_1X1: False 9 | TRANS_FUNC: "BottleneckWithGN" 10 | STEM_FUNC: "StemWithGN" 11 | FPN: 12 | USE_GN: True 13 | RPN: 14 | USE_FPN: True 15 | ANCHOR_STRIDE: (4, 8, 16, 32, 64) 16 | PRE_NMS_TOP_N_TRAIN: 2000 17 | POST_NMS_TOP_N_TRAIN: 1000 18 | PRE_NMS_TOP_N_TEST: 30000 19 | POST_NMS_TOP_N_TEST: 15000 20 | FPN_POST_NMS_TOP_N_TEST: 8000 21 | ROI_HEADS: 22 | USE_FPN: True 23 | BATCH_SIZE_PER_IMAGE: 512 24 | POSITIVE_FRACTION: 0.25 25 | DETECTIONS_PER_IMG: 8000 26 | ROI_BOX_HEAD: 27 | USE_GN: True # use GN for bbox head 28 | POOLER_RESOLUTION: 7 29 | POOLER_SCALES: (0.25, 0.125, 0.0625, 0.03125) 30 | POOLER_SAMPLING_RATIO: 2 31 | FEATURE_EXTRACTOR: "FPN2MLPFeatureExtractor" 32 | PREDICTOR: "FPNPredictor" 33 | NUM_CLASSES: 2 34 | ROI_MASK_HEAD: 35 | USE_GN: True # use GN for mask head 36 | POOLER_SCALES: (0.25, 0.125, 0.0625, 0.03125) 37 | CONV_LAYERS: (256, 256, 256, 256) 38 | FEATURE_EXTRACTOR: "MaskRCNNFPNFeatureExtractor" 39 | PREDICTOR: "MaskRCNNC4Predictor" 40 | POOLER_RESOLUTION: 14 41 | POOLER_SAMPLING_RATIO: 2 42 | RESOLUTION: 28 43 | SHARE_BOX_FEATURE_EXTRACTOR: False 44 | MASK_ON: True 45 | PANOPTIC: 46 | PS_ON: True 47 | SEM_PATH: "./datasets/fluo2tcga/semgt" 48 | USE_GN: True 49 | DOMAIN_ADAPTION: 50 | ENABLE_DA: True 51 | ENABLE_SEMDA: True 52 | SEM_TYPE: 'RES4' # RES4 OR CNN6 53 | ENABLE_INSDA: True 54 | ENABLE_IMGDA: True 55 | LOSS_AVG: True 56 | MI_MAX: True 57 | FEATURE_CHANNEL: 64 58 | DATASETS: 59 | TRAIN_SOURCE: ("cell_fluo2tcga_syn_train",) 60 | TRAIN_TARGET: ("cell_tcga",) 61 | TEST: ("cell_tcga",) 62 | DATALOADER: 63 | SIZE_DIVISIBILITY: 32 64 | SOLVER: 65 | # Assume 1 gpus 66 | BASE_LR: 0.001 67 | WEIGHT_DECAY: 0.0001 68 | STEPS: (16000, 24000) 69 | MAX_ITER: 24000 70 | IMS_PER_BATCH: 1 71 | EPOCH_PERIOD: 2400 72 | CHECKPOINT_PERIOD: 30000 73 | TEST: 74 | IMS_PER_BATCH: 1 75 | INPUT: 76 | MAX_SIZE_TRAIN: 256 77 | MAX_SIZE_TEST: 256 78 | PIXEL_MEAN: [128.0, 128.0, 128.0] 79 | HORIZONTAL_FLIP_PROB_TRAIN: 0.5 80 | VERTICAL_FLIP_PROB_TRAIN: 0.5 81 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/layers/roi_align.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | from torch import nn 4 | from torch.autograd import Function 5 | from torch.autograd.function import once_differentiable 6 | from torch.nn.modules.utils import _pair 7 | 8 | from maskrcnn_benchmark import _C 9 | 10 | from apex import amp 11 | 12 | class _ROIAlign(Function): 13 | @staticmethod 14 | def forward(ctx, input, roi, output_size, spatial_scale, sampling_ratio): 15 | ctx.save_for_backward(roi) 16 | ctx.output_size = _pair(output_size) 17 | ctx.spatial_scale = spatial_scale 18 | ctx.sampling_ratio = sampling_ratio 19 | ctx.input_shape = input.size() 20 | output = _C.roi_align_forward( 21 | input, roi, spatial_scale, output_size[0], output_size[1], sampling_ratio 22 | ) 23 | return output 24 | 25 | @staticmethod 26 | @once_differentiable 27 | def backward(ctx, grad_output): 28 | rois, = ctx.saved_tensors 29 | output_size = ctx.output_size 30 | spatial_scale = ctx.spatial_scale 31 | sampling_ratio = ctx.sampling_ratio 32 | bs, ch, h, w = ctx.input_shape 33 | grad_input = _C.roi_align_backward( 34 | grad_output, 35 | rois, 36 | spatial_scale, 37 | output_size[0], 38 | output_size[1], 39 | bs, 40 | ch, 41 | h, 42 | w, 43 | sampling_ratio, 44 | ) 45 | return grad_input, None, None, None, None 46 | 47 | 48 | roi_align = _ROIAlign.apply 49 | 50 | class ROIAlign(nn.Module): 51 | def __init__(self, output_size, spatial_scale, sampling_ratio): 52 | super(ROIAlign, self).__init__() 53 | self.output_size = output_size 54 | self.spatial_scale = spatial_scale 55 | self.sampling_ratio = sampling_ratio 56 | 57 | @amp.float_function 58 | def forward(self, input, rois): 59 | return roi_align( 60 | input, rois, self.output_size, self.spatial_scale, self.sampling_ratio 61 | ) 62 | 63 | def __repr__(self): 64 | tmpstr = self.__class__.__name__ + "(" 65 | tmpstr += "output_size=" + str(self.output_size) 66 | tmpstr += ", spatial_scale=" + str(self.spatial_scale) 67 | tmpstr += ", sampling_ratio=" + str(self.sampling_ratio) 68 | tmpstr += ")" 69 | return tmpstr 70 | -------------------------------------------------------------------------------- /configs/uda_nuclei_seg/e2e_mask_rcnn_R_101_FPN_1x_gn_epfl2vnc.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "GeneralizedRCNN" 3 | WEIGHT: "catalog://ImageNetPretrained/MSRA/R-101-GN" 4 | BACKBONE: 5 | CONV_BODY: "R-101-FPN" 6 | RESNETS: # use GN for backbone 7 | BACKBONE_OUT_CHANNELS: 256 8 | STRIDE_IN_1X1: False 9 | TRANS_FUNC: "BottleneckWithGN" 10 | STEM_FUNC: "StemWithGN" 11 | FPN: 12 | USE_GN: True # use GN for FPN 13 | RPN: 14 | USE_FPN: True 15 | ANCHOR_STRIDE: (4, 8, 16, 32, 64) 16 | PRE_NMS_TOP_N_TRAIN: 500 17 | POST_NMS_TOP_N_TRAIN: 200 18 | PRE_NMS_TOP_N_TEST: 1000 19 | POST_NMS_TOP_N_TEST: 500 20 | FPN_POST_NMS_TOP_N_TEST: 200 21 | ROI_HEADS: 22 | USE_FPN: True 23 | BATCH_SIZE_PER_IMAGE: 128 24 | POSITIVE_FRACTION: 0.25 25 | NMS: 0.3 26 | DETECTIONS_PER_IMG: 60 27 | ROI_BOX_HEAD: 28 | USE_GN: True # use GN for bbox head 29 | POOLER_RESOLUTION: 7 30 | POOLER_SCALES: (0.25, 0.125, 0.0625, 0.03125) 31 | POOLER_SAMPLING_RATIO: 2 32 | FEATURE_EXTRACTOR: "FPN2MLPFeatureExtractor" 33 | PREDICTOR: "FPNPredictor" 34 | NUM_CLASSES: 2 35 | ROI_MASK_HEAD: 36 | USE_GN: True # use GN for mask head 37 | POOLER_SCALES: (0.25, 0.125, 0.0625, 0.03125) 38 | CONV_LAYERS: (256, 256, 256, 256) 39 | FEATURE_EXTRACTOR: "MaskRCNNFPNFeatureExtractor" 40 | PREDICTOR: "MaskRCNNC4Predictor" 41 | POOLER_RESOLUTION: 14 42 | POOLER_SAMPLING_RATIO: 2 43 | RESOLUTION: 28 44 | SHARE_BOX_FEATURE_EXTRACTOR: False 45 | MASK_ON: True 46 | PANOPTIC: 47 | PS_ON: True 48 | SEM_PATH: "./datasets/epfl2vnc/semgt" 49 | USE_GN: True 50 | DOMAIN_ADAPTION: 51 | ENABLE_DA: True 52 | ENABLE_SEMDA: True 53 | SEM_TYPE: 'RES4' # RES4 OR CNN6 54 | ENABLE_INSDA: True 55 | ENABLE_IMGDA: True 56 | LOSS_AVG: True 57 | MI_MAX: True 58 | FEATURE_CHANNEL: 64 59 | DATASETS: 60 | TRAIN_SOURCE: ("em_epfl2vnc_syn_train",) 61 | TRAIN_TARGET: ("em_vnc_raw_train",) 62 | TEST: ("em_vnc_raw_train",) 63 | DATALOADER: 64 | SIZE_DIVISIBILITY: 32 65 | SOLVER: 66 | # Assume 1 gpu 67 | BASE_LR: 0.001 68 | WEIGHT_DECAY: 0.0001 69 | STEPS: (4000, 5000) 70 | MAX_ITER: 5000 71 | IMS_PER_BATCH: 1 72 | EPOCH_PERIOD: 500 73 | CHECKPOINT_PERIOD: 30000 74 | TEST: 75 | IMS_PER_BATCH: 1 76 | INPUT: 77 | MAX_SIZE_TRAIN: 512 78 | MAX_SIZE_TEST: 512 79 | PIXEL_MEAN: [128.0, 128.0, 128.0] 80 | HORIZONTAL_FLIP_PROB_TRAIN: 0.5 81 | VERTICAL_FLIP_PROB_TRAIN: 0.5 82 | -------------------------------------------------------------------------------- /inference/epfl2vnc_eva.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | import os 4 | import numpy as np 5 | import tifffile as tiff 6 | from inference.metrics import agg_jc_index, pixel_f1, remap_label, get_fast_pq 7 | import xlwt 8 | 9 | 10 | def evaluate_epfl2vnc(pred_root, gt_root): 11 | 12 | aji_list = [] 13 | f1_list = [] 14 | pq_list = [] 15 | 16 | wb = xlwt.Workbook() 17 | ws = wb.add_sheet('Test Sheet') 18 | 19 | counter = 0 20 | 21 | ws.write(0, 0, 'img_name') 22 | ws.write(0, 1, 'aji') 23 | ws.write(0, 2, 'f1') 24 | ws.write(0, 3, 'pq') 25 | 26 | 27 | test_imgs = os.listdir(pred_root) 28 | 29 | 30 | #for img_name in img_names: 31 | 32 | for img_name in test_imgs: 33 | if img_name.endswith(".tif"): 34 | gt_name = img_name 35 | pred_ins = tiff.imread(os.path.join(pred_root, img_name)) 36 | gt_ins = tiff.imread(os.path.join(gt_root, gt_name)) 37 | 38 | aji_cur = agg_jc_index(gt_ins, pred_ins) 39 | aji_list.append(aji_cur) 40 | 41 | f1_cur = pixel_f1(gt_ins, pred_ins) 42 | f1_list.append(f1_cur) 43 | 44 | gt = remap_label(gt_ins, by_size=False) 45 | pred = remap_label(pred_ins, by_size=False) 46 | 47 | pq_info_cur = get_fast_pq(gt, pred, match_iou=0.5)[0] 48 | pq_cur = pq_info_cur[2] 49 | pq_list.append(pq_cur) 50 | 51 | counter = counter + 1 52 | 53 | ws.write(counter, 0, img_name) 54 | ws.write(counter, 1, aji_cur) 55 | ws.write(counter, 2, f1_cur) 56 | ws.write(counter, 3, pq_cur) 57 | 58 | 59 | wb.save(pred_root + '.xls') 60 | 61 | 62 | aji_array = np.asarray(aji_list, dtype= np.float32) 63 | f1_array = np.asarray(f1_list, dtype= np.float32) 64 | pq_array = np.asarray(pq_list, dtype= np.float32) 65 | 66 | 67 | aji_avg = np.average(aji_array) 68 | aji_std = np.std(aji_array) 69 | 70 | f1_avg = np.average(f1_array) 71 | f1_std = np.std(f1_array) 72 | 73 | pq_avg = np.average(pq_array) 74 | pq_std = np.std(pq_array) 75 | 76 | print(pred_root) 77 | 78 | print('average aji score of this method is: ', aji_avg, ' ', aji_std) 79 | print('average f1 score of this method is: ', f1_avg, ' ',f1_std) 80 | print('average pq score of this method is: ', pq_avg, ' ',pq_std) 81 | 82 | 83 | 84 | if __name__ == "__main__": 85 | pred_root = '' 86 | gt_root = '' 87 | 88 | evaluate_epfl2vnc(pred_root, gt_root) 89 | -------------------------------------------------------------------------------- /inference/fluo2tnbc_eva.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | import os 4 | import numpy as np 5 | import tifffile as tiff 6 | from inference.metrics import agg_jc_index, pixel_f1, remap_label, get_fast_pq 7 | import xlwt 8 | 9 | 10 | def evaluate_fluo2tnbc(pred_root, gt_root): 11 | 12 | aji_list = [] 13 | f1_list = [] 14 | pq_list = [] 15 | 16 | wb = xlwt.Workbook() 17 | ws = wb.add_sheet('Test Sheet') 18 | 19 | counter = 0 20 | 21 | ws.write(0, 0, 'img_name') 22 | ws.write(0, 1, 'aji') 23 | ws.write(0, 2, 'f1') 24 | ws.write(0, 3, 'pq') 25 | 26 | 27 | test_imgs = os.listdir(pred_root) 28 | 29 | 30 | #for img_name in img_names: 31 | 32 | for img_name in test_imgs: 33 | if img_name.endswith(".tif"): 34 | gt_name = img_name 35 | pred_ins = tiff.imread(os.path.join(pred_root, img_name)) 36 | gt_ins = tiff.imread(os.path.join(gt_root, gt_name)) 37 | 38 | aji_cur = agg_jc_index(gt_ins, pred_ins) 39 | aji_list.append(aji_cur) 40 | 41 | f1_cur = pixel_f1(gt_ins, pred_ins) 42 | f1_list.append(f1_cur) 43 | 44 | gt = remap_label(gt_ins, by_size=False) 45 | pred = remap_label(pred_ins, by_size=False) 46 | 47 | pq_info_cur = get_fast_pq(gt, pred, match_iou=0.5)[0] 48 | pq_cur = pq_info_cur[2] 49 | pq_list.append(pq_cur) 50 | 51 | counter = counter + 1 52 | 53 | ws.write(counter, 0, img_name) 54 | ws.write(counter, 1, aji_cur) 55 | ws.write(counter, 2, f1_cur) 56 | ws.write(counter, 3, pq_cur) 57 | 58 | 59 | wb.save(pred_root + '.xls') 60 | 61 | 62 | aji_array = np.asarray(aji_list, dtype= np.float32) 63 | f1_array = np.asarray(f1_list, dtype= np.float32) 64 | pq_array = np.asarray(pq_list, dtype= np.float32) 65 | 66 | 67 | aji_avg = np.average(aji_array) 68 | aji_std = np.std(aji_array) 69 | 70 | f1_avg = np.average(f1_array) 71 | f1_std = np.std(f1_array) 72 | 73 | pq_avg = np.average(pq_array) 74 | pq_std = np.std(pq_array) 75 | 76 | print(pred_root) 77 | 78 | print('average aji score of this method is: ', aji_avg, ' ', aji_std) 79 | print('average f1 score of this method is: ', f1_avg, ' ',f1_std) 80 | print('average pq score of this method is: ', pq_avg, ' ',pq_std) 81 | 82 | 83 | 84 | if __name__ == "__main__": 85 | pred_root = '' 86 | gt_root = '' 87 | 88 | evaluate_fluo2tnbc(pred_root, gt_root) 89 | -------------------------------------------------------------------------------- /configs/uda_nuclei_seg/e2e_mask_rcnn_R_101_FPN_1x_gn_fluo2tnbc.yaml: -------------------------------------------------------------------------------- 1 | MODEL: 2 | META_ARCHITECTURE: "GeneralizedRCNN" 3 | WEIGHT: "catalog://ImageNetPretrained/MSRA/R-101-GN" 4 | BACKBONE: 5 | CONV_BODY: "R-101-FPN" 6 | RESNETS: # use GN for backbone 7 | BACKBONE_OUT_CHANNELS: 256 8 | STRIDE_IN_1X1: False 9 | TRANS_FUNC: "BottleneckWithGN" 10 | STEM_FUNC: "StemWithGN" 11 | FPN: 12 | USE_GN: True # use GN for FPN 13 | RPN: 14 | USE_FPN: True 15 | ANCHOR_STRIDE: (4, 8, 16, 32, 64) 16 | PRE_NMS_TOP_N_TRAIN: 2000 17 | POST_NMS_TOP_N_TRAIN: 1000 18 | PRE_NMS_TOP_N_TEST: 4000 19 | POST_NMS_TOP_N_TEST: 2000 20 | FPN_POST_NMS_TOP_N_TEST: 1500 21 | ROI_HEADS: 22 | USE_FPN: True 23 | BATCH_SIZE_PER_IMAGE: 512 24 | POSITIVE_FRACTION: 0.25 25 | NMS: 0.3 26 | DETECTIONS_PER_IMG: 800 27 | ROI_BOX_HEAD: 28 | USE_GN: True # use GN for bbox head 29 | POOLER_RESOLUTION: 7 30 | POOLER_SCALES: (0.25, 0.125, 0.0625, 0.03125) 31 | POOLER_SAMPLING_RATIO: 2 32 | FEATURE_EXTRACTOR: "FPN2MLPFeatureExtractor" 33 | PREDICTOR: "FPNPredictor" 34 | NUM_CLASSES: 2 35 | ROI_MASK_HEAD: 36 | USE_GN: True # use GN for mask head 37 | POOLER_SCALES: (0.25, 0.125, 0.0625, 0.03125) 38 | CONV_LAYERS: (256, 256, 256, 256) 39 | FEATURE_EXTRACTOR: "MaskRCNNFPNFeatureExtractor" 40 | PREDICTOR: "MaskRCNNC4Predictor" 41 | POOLER_RESOLUTION: 14 42 | POOLER_SAMPLING_RATIO: 2 43 | RESOLUTION: 28 44 | SHARE_BOX_FEATURE_EXTRACTOR: False 45 | MASK_ON: True 46 | PANOPTIC: 47 | PS_ON: True 48 | SEM_PATH: "./datasets/fluo2tnbc/semgt" 49 | USE_GN: True 50 | DOMAIN_ADAPTION: 51 | ENABLE_DA: True 52 | ENABLE_SEMDA: True 53 | SEM_TYPE: 'RES4' # RES4 OR CNN6 54 | ENABLE_INSDA: True 55 | ENABLE_IMGDA: True 56 | LOSS_AVG: True 57 | MI_MAX: True 58 | FEATURE_CHANNEL: 64 59 | DATASETS: 60 | TRAIN_SOURCE: ("cell_fluo2tnbc_syn_train",) 61 | TRAIN_TARGET: ("celltnbc_tgt_train40",) 62 | TEST: ("celltnbc_tgt_train40",) 63 | DATALOADER: 64 | SIZE_DIVISIBILITY: 32 65 | SOLVER: 66 | # Assume 1 gpu 67 | BASE_LR: 0.001 68 | WEIGHT_DECAY: 0.0001 69 | STEPS: (9000, 12000) 70 | MAX_ITER: 12000 71 | IMS_PER_BATCH: 1 72 | EPOCH_PERIOD: 1200 73 | CHECKPOINT_PERIOD: 30000 74 | TEST: 75 | IMS_PER_BATCH: 1 76 | INPUT: 77 | MAX_SIZE_TRAIN: 256 78 | MAX_SIZE_TEST: 256 79 | PIXEL_MEAN: [128.0, 128.0, 128.0] 80 | HORIZONTAL_FLIP_PROB_TRAIN: 0.5 81 | VERTICAL_FLIP_PROB_TRAIN: 0.5 82 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/mask_head/roi_mask_predictors.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from torch import nn 3 | from torch.nn import functional as F 4 | 5 | from maskrcnn_benchmark.layers import Conv2d 6 | from maskrcnn_benchmark.layers import ConvTranspose2d 7 | from maskrcnn_benchmark.modeling import registry 8 | 9 | 10 | @registry.ROI_MASK_PREDICTOR.register("MaskRCNNC4Predictor") 11 | class MaskRCNNC4Predictor(nn.Module): 12 | def __init__(self, cfg, in_channels): 13 | super(MaskRCNNC4Predictor, self).__init__() 14 | num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES 15 | dim_reduced = cfg.MODEL.ROI_MASK_HEAD.CONV_LAYERS[-1] 16 | num_inputs = in_channels 17 | 18 | 19 | self.conv5_mask = ConvTranspose2d(num_inputs, dim_reduced, 2, 2, 0) 20 | self.mask_fcn_logits = Conv2d(dim_reduced, num_classes, 1, 1, 0) 21 | 22 | for name, param in self.named_parameters(): 23 | if "bias" in name: 24 | nn.init.constant_(param, 0) 25 | elif "weight" in name: 26 | # Caffe2 implementation uses MSRAFill, which in fact 27 | # corresponds to kaiming_normal_ in PyTorch 28 | nn.init.kaiming_normal_(param, mode="fan_out", nonlinearity="relu") 29 | 30 | def forward(self, x): 31 | x = F.relu(self.conv5_mask(x)) 32 | return self.mask_fcn_logits(x) 33 | 34 | 35 | @registry.ROI_MASK_PREDICTOR.register("MaskRCNNConv1x1Predictor") 36 | class MaskRCNNConv1x1Predictor(nn.Module): 37 | def __init__(self, cfg, in_channels): 38 | super(MaskRCNNConv1x1Predictor, self).__init__() 39 | num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES 40 | num_inputs = in_channels 41 | 42 | self.mask_fcn_logits = Conv2d(num_inputs, num_classes, 1, 1, 0) 43 | 44 | for name, param in self.named_parameters(): 45 | if "bias" in name: 46 | nn.init.constant_(param, 0) 47 | elif "weight" in name: 48 | # Caffe2 implementation uses MSRAFill, which in fact 49 | # corresponds to kaiming_normal_ in PyTorch 50 | nn.init.kaiming_normal_(param, mode="fan_out", nonlinearity="relu") 51 | 52 | def forward(self, x): 53 | return self.mask_fcn_logits(x) 54 | 55 | 56 | def make_roi_mask_predictor(cfg, in_channels): 57 | func = registry.ROI_MASK_PREDICTOR[cfg.MODEL.ROI_MASK_HEAD.PREDICTOR] 58 | return func(cfg, in_channels) 59 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/datasets/abstract.py: -------------------------------------------------------------------------------- 1 | import torch 2 | 3 | class AbstractDataset(torch.utils.data.Dataset): 4 | """ 5 | Serves as a common interface to reduce boilerplate and help dataset 6 | customization 7 | 8 | A generic Dataset for the maskrcnn_benchmark must have the following 9 | non-trivial fields / methods implemented: 10 | CLASSES - list/tuple: 11 | A list of strings representing the classes. It must have 12 | "__background__" as its 0th element for correct id mapping. 13 | 14 | __getitem__ - function(idx): 15 | This has to return three things: img, target, idx. 16 | img is the input image, which has to be load as a PIL Image object 17 | implementing the target requires the most effort, since it must have 18 | multiple fields: the size, bounding boxes, labels (contiguous), and 19 | masks (either COCO-style Polygons, RLE or torch BinaryMask). 20 | Usually the target is a BoxList instance with extra fields. 21 | Lastly, idx is simply the input argument of the function. 22 | 23 | also the following is required: 24 | __len__ - function(): 25 | return the size of the dataset 26 | get_img_info - function(idx): 27 | return metadata, at least width and height of the input image 28 | """ 29 | 30 | def __init__(self, *args, **kwargs): 31 | self.name_to_id = None 32 | self.id_to_name = None 33 | 34 | 35 | def __getitem__(self, idx): 36 | raise NotImplementedError 37 | 38 | 39 | def initMaps(self): 40 | """ 41 | Can be called optionally to initialize the id<->category name mapping 42 | 43 | 44 | Initialize default mapping between: 45 | class <==> index 46 | class: this is a string that represents the class 47 | index: positive int, used directly by the ROI heads. 48 | 49 | 50 | NOTE: 51 | make sure that the background is always indexed by 0. 52 | "__background__" <==> 0 53 | 54 | if initialized by hand, double check that the indexing is correct. 55 | """ 56 | assert isinstance(self.CLASSES, (list, tuple)) 57 | assert self.CLASSES[0] == "__background__" 58 | cls = self.CLASSES 59 | self.name_to_id = dict(zip(cls, range(len(cls)))) 60 | self.id_to_name = dict(zip(range(len(cls)), cls)) 61 | 62 | 63 | def get_img_info(self, index): 64 | raise NotImplementedError 65 | 66 | 67 | def __len__(self): 68 | raise NotImplementedError 69 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/domain_adaption/LabelResizeLayer.py: -------------------------------------------------------------------------------- 1 | # 2 | # from __future__ import absolute_import 3 | # from __future__ import division 4 | # from __future__ import print_function 5 | import random 6 | import torch 7 | import torch.nn.functional as F 8 | import torchvision.models as models 9 | from torch.autograd import Variable 10 | import numpy as np 11 | import torch.nn as nn 12 | from torch.autograd import Function 13 | import cv2 14 | 15 | 16 | class ImageLabelResizeLayer(nn.Module): 17 | """ 18 | Resize label to be the same size with the samples 19 | """ 20 | def __init__(self): 21 | super(ImageLabelResizeLayer, self).__init__() 22 | 23 | 24 | def forward(self,x, need_backprop): 25 | 26 | feats = x.detach().cpu().numpy() 27 | lbs = need_backprop.detach().cpu().numpy() 28 | gt_blob = np.zeros((lbs.shape[0], feats.shape[2], feats.shape[3], 1), dtype=np.float32) 29 | for i in range(lbs.shape[0]): 30 | lb=np.array([lbs[i]]) 31 | lbs_resize = cv2.resize(lb, (feats.shape[3] ,feats.shape[2]), interpolation=cv2.INTER_NEAREST) 32 | gt_blob[i, 0:lbs_resize.shape[0], 0:lbs_resize.shape[1], 0] = lbs_resize 33 | 34 | channel_swap = (0, 3, 1, 2) 35 | gt_blob = gt_blob.transpose(channel_swap) 36 | y=Variable(torch.from_numpy(gt_blob)).cuda() 37 | y=y.squeeze(1).long() 38 | return y 39 | 40 | 41 | class InstanceLabelResizeLayer(nn.Module): 42 | 43 | 44 | def __init__(self): 45 | super(InstanceLabelResizeLayer, self).__init__() 46 | self.minibatch=256 47 | 48 | def forward(self, x,need_backprop): 49 | feats = x.data.cpu().numpy() 50 | lbs = need_backprop.data.cpu().numpy() 51 | 52 | resized_lbs = np.ones((feats.shape[0], 1), dtype=np.float32) 53 | for i in range(lbs.shape[0]): 54 | resized_lbs[i*self.minibatch:(i+1)*self.minibatch] = lbs[i] 55 | 56 | y=torch.from_numpy(resized_lbs).cuda() 57 | 58 | return y 59 | 60 | class FcLabelResizeLayer(nn.Module): 61 | 62 | 63 | def __init__(self): 64 | super(FcLabelResizeLayer, self).__init__() 65 | self.minibatch = 1 66 | 67 | def forward(self, x,need_backprop): 68 | feats = x.data.cpu().numpy() 69 | lbs = need_backprop.data.cpu().numpy() 70 | 71 | resized_lbs = np.ones((feats.shape[0], 1), dtype=np.float32) 72 | for i in range(lbs.shape[0]): 73 | resized_lbs[i*self.minibatch:(i+1)*self.minibatch] = lbs[i] 74 | 75 | y=torch.from_numpy(resized_lbs).cuda().long() 76 | 77 | return y -------------------------------------------------------------------------------- /maskrcnn_benchmark/layers/sigmoid_focal_loss.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | from torch.autograd import Function 4 | from torch.autograd.function import once_differentiable 5 | 6 | from maskrcnn_benchmark import _C 7 | 8 | # TODO: Use JIT to replace CUDA implementation in the future. 9 | class _SigmoidFocalLoss(Function): 10 | @staticmethod 11 | def forward(ctx, logits, targets, gamma, alpha): 12 | ctx.save_for_backward(logits, targets) 13 | num_classes = logits.shape[1] 14 | ctx.num_classes = num_classes 15 | ctx.gamma = gamma 16 | ctx.alpha = alpha 17 | 18 | losses = _C.sigmoid_focalloss_forward( 19 | logits, targets, num_classes, gamma, alpha 20 | ) 21 | return losses 22 | 23 | @staticmethod 24 | @once_differentiable 25 | def backward(ctx, d_loss): 26 | logits, targets = ctx.saved_tensors 27 | num_classes = ctx.num_classes 28 | gamma = ctx.gamma 29 | alpha = ctx.alpha 30 | d_loss = d_loss.contiguous() 31 | d_logits = _C.sigmoid_focalloss_backward( 32 | logits, targets, d_loss, num_classes, gamma, alpha 33 | ) 34 | return d_logits, None, None, None, None 35 | 36 | 37 | sigmoid_focal_loss_cuda = _SigmoidFocalLoss.apply 38 | 39 | 40 | def sigmoid_focal_loss_cpu(logits, targets, gamma, alpha): 41 | num_classes = logits.shape[1] 42 | dtype = targets.dtype 43 | device = targets.device 44 | class_range = torch.arange(1, num_classes+1, dtype=dtype, device=device).unsqueeze(0) 45 | 46 | t = targets.unsqueeze(1) 47 | p = torch.sigmoid(logits) 48 | term1 = (1 - p) ** gamma * torch.log(p) 49 | term2 = p ** gamma * torch.log(1 - p) 50 | return -(t == class_range).float() * term1 * alpha - ((t != class_range) * (t >= 0)).float() * term2 * (1 - alpha) 51 | 52 | 53 | class SigmoidFocalLoss(nn.Module): 54 | def __init__(self, gamma, alpha): 55 | super(SigmoidFocalLoss, self).__init__() 56 | self.gamma = gamma 57 | self.alpha = alpha 58 | 59 | def forward(self, logits, targets): 60 | device = logits.device 61 | if logits.is_cuda: 62 | loss_func = sigmoid_focal_loss_cuda 63 | else: 64 | loss_func = sigmoid_focal_loss_cpu 65 | 66 | loss = loss_func(logits, targets, self.gamma, self.alpha) 67 | return loss.sum() 68 | 69 | def __repr__(self): 70 | tmpstr = self.__class__.__name__ + "(" 71 | tmpstr += "gamma=" + str(self.gamma) 72 | tmpstr += ", alpha=" + str(self.alpha) 73 | tmpstr += ")" 74 | return tmpstr 75 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/box_head/roi_box_predictors.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from maskrcnn_benchmark.modeling import registry 3 | from torch import nn 4 | 5 | 6 | @registry.ROI_BOX_PREDICTOR.register("FastRCNNPredictor") 7 | class FastRCNNPredictor(nn.Module): 8 | def __init__(self, config, in_channels): 9 | super(FastRCNNPredictor, self).__init__() 10 | assert in_channels is not None 11 | 12 | num_inputs = in_channels 13 | 14 | num_classes = config.MODEL.ROI_BOX_HEAD.NUM_CLASSES 15 | self.avgpool = nn.AdaptiveAvgPool2d(1) 16 | self.cls_score = nn.Linear(num_inputs, num_classes) 17 | num_bbox_reg_classes = 2 if config.MODEL.CLS_AGNOSTIC_BBOX_REG else num_classes 18 | self.bbox_pred = nn.Linear(num_inputs, num_bbox_reg_classes * 4) 19 | 20 | nn.init.normal_(self.cls_score.weight, mean=0, std=0.01) 21 | nn.init.constant_(self.cls_score.bias, 0) 22 | 23 | nn.init.normal_(self.bbox_pred.weight, mean=0, std=0.001) 24 | nn.init.constant_(self.bbox_pred.bias, 0) 25 | 26 | def forward(self, x): 27 | x = self.avgpool(x) 28 | x = x.view(x.size(0), -1) 29 | cls_logit = self.cls_score(x) 30 | bbox_pred = self.bbox_pred(x) 31 | return cls_logit, bbox_pred 32 | 33 | 34 | @registry.ROI_BOX_PREDICTOR.register("FPNPredictor") 35 | class FPNPredictor(nn.Module): 36 | def __init__(self, cfg, in_channels): 37 | super(FPNPredictor, self).__init__() 38 | num_classes = cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES 39 | representation_size = in_channels 40 | 41 | self.cls_score = nn.Linear(representation_size, num_classes) 42 | num_bbox_reg_classes = 2 if cfg.MODEL.CLS_AGNOSTIC_BBOX_REG else num_classes 43 | self.bbox_pred = nn.Linear(representation_size, num_bbox_reg_classes * 4) 44 | 45 | nn.init.normal_(self.cls_score.weight, std=0.01) 46 | nn.init.normal_(self.bbox_pred.weight, std=0.001) 47 | for l in [self.cls_score, self.bbox_pred]: 48 | nn.init.constant_(l.bias, 0) 49 | 50 | def forward(self, x): 51 | if x.ndimension() == 4: 52 | assert list(x.shape[2:]) == [1, 1] 53 | x = x.view(x.size(0), -1) 54 | # print('box final logit', x.size()) 55 | scores = self.cls_score(x) 56 | bbox_deltas = self.bbox_pred(x) 57 | 58 | return scores, bbox_deltas 59 | 60 | 61 | def make_roi_box_predictor(cfg, in_channels): 62 | func = registry.ROI_BOX_PREDICTOR[cfg.MODEL.ROI_BOX_HEAD.PREDICTOR] 63 | return func(cfg, in_channels) 64 | -------------------------------------------------------------------------------- /tests/test_segmentation_mask.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import unittest 3 | import torch 4 | from maskrcnn_benchmark.structures.segmentation_mask import SegmentationMask 5 | 6 | 7 | class TestSegmentationMask(unittest.TestCase): 8 | def __init__(self, method_name='runTest'): 9 | super(TestSegmentationMask, self).__init__(method_name) 10 | poly = [[[423.0, 306.5, 406.5, 277.0, 400.0, 271.5, 389.5, 277.0, 11 | 387.5, 292.0, 384.5, 295.0, 374.5, 220.0, 378.5, 210.0, 12 | 391.0, 200.5, 404.0, 199.5, 414.0, 203.5, 425.5, 221.0, 13 | 438.5, 297.0, 423.0, 306.5], 14 | [100, 100, 200, 100, 200, 200, 100, 200], 15 | ]] 16 | width = 640 17 | height = 480 18 | size = width, height 19 | 20 | self.P = SegmentationMask(poly, size, 'poly') 21 | self.M = SegmentationMask(poly, size, 'poly').convert('mask') 22 | 23 | def L1(self, A, B): 24 | diff = A.get_mask_tensor() - B.get_mask_tensor() 25 | diff = torch.sum(torch.abs(diff.float())).item() 26 | return diff 27 | 28 | def test_convert(self): 29 | M_hat = self.M.convert('poly').convert('mask') 30 | P_hat = self.P.convert('mask').convert('poly') 31 | 32 | diff_mask = self.L1(self.M, M_hat) 33 | diff_poly = self.L1(self.P, P_hat) 34 | self.assertTrue(diff_mask == diff_poly) 35 | self.assertTrue(diff_mask <= 8169.) 36 | self.assertTrue(diff_poly <= 8169.) 37 | 38 | def test_crop(self): 39 | box = [400, 250, 500, 300] # xyxy 40 | diff = self.L1(self.M.crop(box), self.P.crop(box)) 41 | self.assertTrue(diff <= 1.) 42 | 43 | def test_resize(self): 44 | new_size = 50, 25 45 | M_hat = self.M.resize(new_size) 46 | P_hat = self.P.resize(new_size) 47 | diff = self.L1(M_hat, P_hat) 48 | 49 | self.assertTrue(self.M.size == self.P.size) 50 | self.assertTrue(M_hat.size == P_hat.size) 51 | self.assertTrue(self.M.size != M_hat.size) 52 | self.assertTrue(diff <= 255.) 53 | 54 | def test_transpose(self): 55 | FLIP_LEFT_RIGHT = 0 56 | FLIP_TOP_BOTTOM = 1 57 | diff_hor = self.L1(self.M.transpose(FLIP_LEFT_RIGHT), 58 | self.P.transpose(FLIP_LEFT_RIGHT)) 59 | 60 | diff_ver = self.L1(self.M.transpose(FLIP_TOP_BOTTOM), 61 | self.P.transpose(FLIP_TOP_BOTTOM)) 62 | 63 | self.assertTrue(diff_hor <= 53250.) 64 | self.assertTrue(diff_ver <= 42494.) 65 | 66 | 67 | if __name__ == "__main__": 68 | 69 | unittest.main() 70 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/csrc/cpu/nms_cpu.cpp: -------------------------------------------------------------------------------- 1 | // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | #include "cpu/vision.h" 3 | 4 | 5 | template 6 | at::Tensor nms_cpu_kernel(const at::Tensor& dets, 7 | const at::Tensor& scores, 8 | const float threshold) { 9 | AT_ASSERTM(!dets.type().is_cuda(), "dets must be a CPU tensor"); 10 | AT_ASSERTM(!scores.type().is_cuda(), "scores must be a CPU tensor"); 11 | AT_ASSERTM(dets.type() == scores.type(), "dets should have the same type as scores"); 12 | 13 | if (dets.numel() == 0) { 14 | return at::empty({0}, dets.options().dtype(at::kLong).device(at::kCPU)); 15 | } 16 | 17 | auto x1_t = dets.select(1, 0).contiguous(); 18 | auto y1_t = dets.select(1, 1).contiguous(); 19 | auto x2_t = dets.select(1, 2).contiguous(); 20 | auto y2_t = dets.select(1, 3).contiguous(); 21 | 22 | at::Tensor areas_t = (x2_t - x1_t + 1) * (y2_t - y1_t + 1); 23 | 24 | auto order_t = std::get<1>(scores.sort(0, /* descending=*/true)); 25 | 26 | auto ndets = dets.size(0); 27 | at::Tensor suppressed_t = at::zeros({ndets}, dets.options().dtype(at::kByte).device(at::kCPU)); 28 | 29 | auto suppressed = suppressed_t.data(); 30 | auto order = order_t.data(); 31 | auto x1 = x1_t.data(); 32 | auto y1 = y1_t.data(); 33 | auto x2 = x2_t.data(); 34 | auto y2 = y2_t.data(); 35 | auto areas = areas_t.data(); 36 | 37 | for (int64_t _i = 0; _i < ndets; _i++) { 38 | auto i = order[_i]; 39 | if (suppressed[i] == 1) 40 | continue; 41 | auto ix1 = x1[i]; 42 | auto iy1 = y1[i]; 43 | auto ix2 = x2[i]; 44 | auto iy2 = y2[i]; 45 | auto iarea = areas[i]; 46 | 47 | for (int64_t _j = _i + 1; _j < ndets; _j++) { 48 | auto j = order[_j]; 49 | if (suppressed[j] == 1) 50 | continue; 51 | auto xx1 = std::max(ix1, x1[j]); 52 | auto yy1 = std::max(iy1, y1[j]); 53 | auto xx2 = std::min(ix2, x2[j]); 54 | auto yy2 = std::min(iy2, y2[j]); 55 | 56 | auto w = std::max(static_cast(0), xx2 - xx1 + 1); 57 | auto h = std::max(static_cast(0), yy2 - yy1 + 1); 58 | auto inter = w * h; 59 | auto ovr = inter / (iarea + areas[j] - inter); 60 | if (ovr >= threshold) 61 | suppressed[j] = 1; 62 | } 63 | } 64 | return at::nonzero(suppressed_t == 0).squeeze(1); 65 | } 66 | 67 | at::Tensor nms_cpu(const at::Tensor& dets, 68 | const at::Tensor& scores, 69 | const float threshold) { 70 | at::Tensor result; 71 | AT_DISPATCH_FLOATING_TYPES(dets.type(), "nms", [&] { 72 | result = nms_cpu_kernel(dets, scores, threshold); 73 | }); 74 | return result; 75 | } 76 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/structures/image_list.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from __future__ import division 3 | 4 | import torch 5 | 6 | 7 | class ImageList(object): 8 | """ 9 | Structure that holds a list of images (of possibly 10 | varying sizes) as a single tensor. 11 | This works by padding the images to the same size, 12 | and storing in a field the original sizes of each image 13 | """ 14 | 15 | def __init__(self, tensors, image_sizes): 16 | """ 17 | Arguments: 18 | tensors (tensor) 19 | image_sizes (list[tuple[int, int]]) 20 | """ 21 | self.tensors = tensors 22 | self.image_sizes = image_sizes 23 | 24 | def to(self, *args, **kwargs): 25 | cast_tensor = self.tensors.to(*args, **kwargs) 26 | return ImageList(cast_tensor, self.image_sizes) 27 | 28 | 29 | def to_image_list(tensors, size_divisible=0): 30 | """ 31 | tensors can be an ImageList, a torch.Tensor or 32 | an iterable of Tensors. It can't be a numpy array. 33 | When tensors is an iterable of Tensors, it pads 34 | the Tensors with zeros so that they have the same 35 | shape 36 | """ 37 | if isinstance(tensors, torch.Tensor) and size_divisible > 0: 38 | tensors = [tensors] 39 | 40 | if isinstance(tensors, ImageList): 41 | return tensors 42 | elif isinstance(tensors, torch.Tensor): 43 | # single tensor shape can be inferred 44 | if tensors.dim() == 3: 45 | tensors = tensors[None] 46 | assert tensors.dim() == 4 47 | image_sizes = [tensor.shape[-2:] for tensor in tensors] 48 | return ImageList(tensors, image_sizes) 49 | elif isinstance(tensors, (tuple, list)): 50 | max_size = tuple(max(s) for s in zip(*[img.shape for img in tensors])) 51 | 52 | # TODO Ideally, just remove this and let me model handle arbitrary 53 | # input sizs 54 | if size_divisible > 0: 55 | import math 56 | 57 | stride = size_divisible 58 | max_size = list(max_size) 59 | max_size[1] = int(math.ceil(max_size[1] / stride) * stride) 60 | max_size[2] = int(math.ceil(max_size[2] / stride) * stride) 61 | max_size = tuple(max_size) 62 | 63 | batch_shape = (len(tensors),) + max_size 64 | batched_imgs = tensors[0].new(*batch_shape).zero_() 65 | for img, pad_img in zip(tensors, batched_imgs): 66 | pad_img[: img.shape[0], : img.shape[1], : img.shape[2]].copy_(img) 67 | 68 | image_sizes = [im.shape[-2:] for im in tensors] 69 | 70 | return ImageList(batched_imgs, image_sizes) 71 | else: 72 | raise TypeError("Unsupported type for to_image_list: {}".format(type(tensors))) 73 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/roi_heads.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | 4 | from .box_head.box_head import build_roi_box_head 5 | from .mask_head.mask_head import build_roi_mask_head 6 | from .keypoint_head.keypoint_head import build_roi_keypoint_head 7 | 8 | 9 | class CombinedROIHeads(torch.nn.ModuleDict): 10 | """ 11 | Combines a set of individual heads (for box prediction or masks) into a single 12 | head. 13 | """ 14 | 15 | def __init__(self, cfg, heads): 16 | super(CombinedROIHeads, self).__init__(heads) 17 | self.cfg = cfg.clone() 18 | if cfg.MODEL.MASK_ON and cfg.MODEL.ROI_MASK_HEAD.SHARE_BOX_FEATURE_EXTRACTOR: 19 | self.mask.feature_extractor = self.box.feature_extractor 20 | if cfg.MODEL.KEYPOINT_ON and cfg.MODEL.ROI_KEYPOINT_HEAD.SHARE_BOX_FEATURE_EXTRACTOR: 21 | self.keypoint.feature_extractor = self.box.feature_extractor 22 | 23 | def forward(self, features, proposals, targets=None, is_source = True): 24 | losses = {} 25 | # TODO rename x to roi_box_features, if it doesn't increase memory consumption 26 | x, detections, loss_box = self.box(features, proposals, targets, is_source) 27 | losses.update(loss_box) 28 | if self.cfg.MODEL.MASK_ON: 29 | mask_features = features 30 | # optimization: during training, if we share the feature extractor between 31 | # the box and the mask heads, then we can reuse the features already computed 32 | if ( 33 | self.training 34 | and self.cfg.MODEL.ROI_MASK_HEAD.SHARE_BOX_FEATURE_EXTRACTOR 35 | ): 36 | mask_features = x 37 | # During training, self.box() will return the unaltered proposals as "detections" 38 | # this makes the API consistent during training and testing 39 | x, detections, loss_mask, ins_logits_da, roi_features \ 40 | = self.mask(mask_features, detections,targets, box_logits = x, is_source = is_source) 41 | losses.update(loss_mask) 42 | 43 | return x, detections, losses, ins_logits_da, roi_features 44 | 45 | 46 | def build_roi_heads(cfg, in_channels): 47 | # individually create the heads, that will be combined together 48 | # afterwards 49 | roi_heads = [] 50 | if cfg.MODEL.RETINANET_ON: 51 | return [] 52 | 53 | if not cfg.MODEL.RPN_ONLY: 54 | roi_heads.append(("box", build_roi_box_head(cfg, in_channels))) 55 | if cfg.MODEL.MASK_ON: 56 | roi_heads.append(("mask", build_roi_mask_head(cfg, in_channels))) 57 | 58 | # combine individual heads in a single module 59 | if roi_heads: 60 | roi_heads = CombinedROIHeads(cfg, roi_heads) 61 | 62 | return roi_heads 63 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/samplers/distributed.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | # Code is copy-pasted exactly as in torch.utils.data.distributed. 3 | # FIXME remove this once c10d fixes the bug it has 4 | import math 5 | import torch 6 | import torch.distributed as dist 7 | from torch.utils.data.sampler import Sampler 8 | 9 | 10 | class DistributedSampler(Sampler): 11 | """Sampler that restricts data loading to a subset of the dataset. 12 | It is especially useful in conjunction with 13 | :class:`torch.nn.parallel.DistributedDataParallel`. In such case, each 14 | process can pass a DistributedSampler instance as a DataLoader sampler, 15 | and load a subset of the original dataset that is exclusive to it. 16 | .. note:: 17 | Dataset is assumed to be of constant size. 18 | Arguments: 19 | dataset: Dataset used for sampling. 20 | num_replicas (optional): Number of processes participating in 21 | distributed training. 22 | rank (optional): Rank of the current process within num_replicas. 23 | """ 24 | 25 | def __init__(self, dataset, num_replicas=None, rank=None, shuffle=True): 26 | if num_replicas is None: 27 | if not dist.is_available(): 28 | raise RuntimeError("Requires distributed package to be available") 29 | num_replicas = dist.get_world_size() 30 | if rank is None: 31 | if not dist.is_available(): 32 | raise RuntimeError("Requires distributed package to be available") 33 | rank = dist.get_rank() 34 | self.dataset = dataset 35 | self.num_replicas = num_replicas 36 | self.rank = rank 37 | self.epoch = 0 38 | self.num_samples = int(math.ceil(len(self.dataset) * 1.0 / self.num_replicas)) 39 | self.total_size = self.num_samples * self.num_replicas 40 | self.shuffle = shuffle 41 | 42 | def __iter__(self): 43 | if self.shuffle: 44 | # deterministically shuffle based on epoch 45 | g = torch.Generator() 46 | g.manual_seed(self.epoch) 47 | indices = torch.randperm(len(self.dataset), generator=g).tolist() 48 | else: 49 | indices = torch.arange(len(self.dataset)).tolist() 50 | 51 | # add extra samples to make it evenly divisible 52 | indices += indices[: (self.total_size - len(indices))] 53 | assert len(indices) == self.total_size 54 | 55 | # subsample 56 | offset = self.num_samples * self.rank 57 | indices = indices[offset : offset + self.num_samples] 58 | assert len(indices) == self.num_samples 59 | 60 | return iter(indices) 61 | 62 | def __len__(self): 63 | return self.num_samples 64 | 65 | def set_epoch(self, epoch): 66 | self.epoch = epoch 67 | -------------------------------------------------------------------------------- /inference/fluo2tcga_eva.py: -------------------------------------------------------------------------------- 1 | 2 | import os 3 | import numpy as np 4 | import tifffile as tiff 5 | from inference.metrics import agg_jc_index, pixel_f1, remap_label, get_fast_pq 6 | import xlwt 7 | 8 | 9 | def evaluate_fluo2tcga(pred_root, gt_root): 10 | 11 | aji_list = [] 12 | f1_list = [] 13 | pq_list = [] 14 | 15 | wb = xlwt.Workbook() 16 | ws = wb.add_sheet('Test Sheet') 17 | 18 | counter = 0 19 | 20 | ws.write(0, 0, 'img_name') 21 | ws.write(0, 1, 'aji') 22 | ws.write(0, 2, 'f1') 23 | ws.write(0, 3, 'pq') 24 | 25 | organ_list = ['breast', 'kidney', 'liver', 'prostate', 'bladder', 'colon', 'stomach'] 26 | 27 | #for img_name in img_names: 28 | 29 | for organ in organ_list: 30 | for i in range (1,3): 31 | gt_name = organ + '_' + str(i) + '.tif' 32 | img_name = organ + '_' + str(i) + '.tif' 33 | pred_ins = tiff.imread(os.path.join(pred_root, img_name)) 34 | gt_ins = tiff.imread(os.path.join(gt_root, gt_name)) 35 | 36 | # compute predictions 37 | 38 | gt = remap_label(gt_ins, by_size=False) 39 | pred = remap_label(pred_ins, by_size=False) 40 | 41 | # object level pq 42 | pq_info_cur = get_fast_pq(gt, pred, match_iou=0.5)[0] 43 | pq_cur = pq_info_cur[2] 44 | pq_list.append(pq_cur) 45 | 46 | # object-level aji 47 | aji_cur = agg_jc_index(gt_ins, pred_ins) 48 | aji_list.append(aji_cur) 49 | 50 | # pixel level dice/f1 score 51 | f1_cur = pixel_f1(gt_ins, pred_ins) 52 | f1_list.append(f1_cur) 53 | 54 | counter = counter + 1 55 | 56 | ws.write(counter, 0, img_name) 57 | ws.write(counter, 1, aji_cur) 58 | ws.write(counter, 2, f1_cur) 59 | ws.write(counter, 3, pq_cur) 60 | 61 | print('The evaluation for current image:', img_name, 'is: aji score: ', aji_cur, 'f1 score: ', f1_cur) 62 | 63 | wb.save(pred_root + '.xls') 64 | 65 | aji_array = np.asarray(aji_list, dtype= np.float16) 66 | f1_array = np.asarray(f1_list, dtype= np.float16) 67 | pq_array = np.asarray(pq_list, dtype= np.float16) 68 | 69 | aji_avg = np.average(aji_array) 70 | aji_std = np.std(aji_array) 71 | 72 | f1_avg = np.average(f1_array) 73 | f1_std = np.std(f1_array) 74 | 75 | pq_avg = np.average(pq_array) 76 | pq_std = np.std(pq_array) 77 | 78 | print(pred_root) 79 | 80 | print('average aji score of this method is: ', aji_avg, ' ', aji_std) 81 | print('average f1 score of this method is: ', f1_avg, ' ',f1_std) 82 | print('average pq score of this method is: ', pq_avg, ' ',pq_std) 83 | 84 | 85 | 86 | if __name__ == "__main__": 87 | pred_root = '' 88 | gt_root = '' 89 | 90 | evaluate_fluo2tcga(pred_root, gt_root) 91 | 92 | 93 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/mask_head/roi_mask_feature_extractors.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from torch import nn 3 | from torch.nn import functional as F 4 | 5 | from ..box_head.roi_box_feature_extractors import ResNet50Conv5ROIFeatureExtractor 6 | from maskrcnn_benchmark.modeling import registry 7 | from maskrcnn_benchmark.modeling.poolers import Pooler 8 | from maskrcnn_benchmark.modeling.make_layers import make_conv3x3 9 | 10 | 11 | registry.ROI_MASK_FEATURE_EXTRACTORS.register( 12 | "ResNet50Conv5ROIFeatureExtractor", ResNet50Conv5ROIFeatureExtractor 13 | ) 14 | 15 | 16 | @registry.ROI_MASK_FEATURE_EXTRACTORS.register("MaskRCNNFPNFeatureExtractor") 17 | class MaskRCNNFPNFeatureExtractor(nn.Module): 18 | """ 19 | Heads for FPN for classification 20 | """ 21 | 22 | def __init__(self, cfg, in_channels): 23 | """ 24 | Arguments: 25 | num_classes (int): number of output classes 26 | input_size (int): number of channels of the input once it's flattened 27 | representation_size (int): size of the intermediate representation 28 | """ 29 | super(MaskRCNNFPNFeatureExtractor, self).__init__() 30 | 31 | resolution = cfg.MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION 32 | scales = cfg.MODEL.ROI_MASK_HEAD.POOLER_SCALES 33 | sampling_ratio = cfg.MODEL.ROI_MASK_HEAD.POOLER_SAMPLING_RATIO 34 | pooler = Pooler( 35 | output_size=(resolution, resolution), 36 | scales=scales, 37 | sampling_ratio=sampling_ratio, 38 | ) 39 | input_size = in_channels 40 | self.pooler = pooler 41 | 42 | use_gn = cfg.MODEL.ROI_MASK_HEAD.USE_GN 43 | layers = cfg.MODEL.ROI_MASK_HEAD.CONV_LAYERS 44 | dilation = cfg.MODEL.ROI_MASK_HEAD.DILATION 45 | 46 | next_feature = input_size 47 | self.blocks = [] 48 | for layer_idx, layer_features in enumerate(layers, 1): 49 | layer_name = "mask_fcn{}".format(layer_idx) 50 | module = make_conv3x3( 51 | next_feature, layer_features, 52 | dilation=dilation, stride=1, use_gn=use_gn 53 | ) 54 | self.add_module(layer_name, module) 55 | next_feature = layer_features 56 | self.blocks.append(layer_name) 57 | self.out_channels = layer_features 58 | 59 | def forward(self, x, proposals): 60 | # print('proposals', proposals) 61 | x = self.pooler(x, proposals) 62 | roi_feature = x 63 | 64 | for layer_name in self.blocks: 65 | x = F.relu(getattr(self, layer_name)(x)) 66 | 67 | return x, roi_feature 68 | 69 | 70 | def make_roi_mask_feature_extractor(cfg, in_channels): 71 | func = registry.ROI_MASK_FEATURE_EXTRACTORS[ 72 | cfg.MODEL.ROI_MASK_HEAD.FEATURE_EXTRACTOR 73 | ] 74 | return func(cfg, in_channels) 75 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/layers/dcn/deform_pool_func.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch.autograd import Function 3 | from torch.autograd.function import once_differentiable 4 | 5 | from maskrcnn_benchmark import _C 6 | 7 | 8 | class DeformRoIPoolingFunction(Function): 9 | 10 | @staticmethod 11 | def forward( 12 | ctx, 13 | data, 14 | rois, 15 | offset, 16 | spatial_scale, 17 | out_size, 18 | out_channels, 19 | no_trans, 20 | group_size=1, 21 | part_size=None, 22 | sample_per_part=4, 23 | trans_std=.0 24 | ): 25 | ctx.spatial_scale = spatial_scale 26 | ctx.out_size = out_size 27 | ctx.out_channels = out_channels 28 | ctx.no_trans = no_trans 29 | ctx.group_size = group_size 30 | ctx.part_size = out_size if part_size is None else part_size 31 | ctx.sample_per_part = sample_per_part 32 | ctx.trans_std = trans_std 33 | 34 | assert 0.0 <= ctx.trans_std <= 1.0 35 | if not data.is_cuda: 36 | raise NotImplementedError 37 | 38 | n = rois.shape[0] 39 | output = data.new_empty(n, out_channels, out_size, out_size) 40 | output_count = data.new_empty(n, out_channels, out_size, out_size) 41 | _C.deform_psroi_pooling_forward( 42 | data, 43 | rois, 44 | offset, 45 | output, 46 | output_count, 47 | ctx.no_trans, 48 | ctx.spatial_scale, 49 | ctx.out_channels, 50 | ctx.group_size, 51 | ctx.out_size, 52 | ctx.part_size, 53 | ctx.sample_per_part, 54 | ctx.trans_std 55 | ) 56 | 57 | if data.requires_grad or rois.requires_grad or offset.requires_grad: 58 | ctx.save_for_backward(data, rois, offset) 59 | ctx.output_count = output_count 60 | 61 | return output 62 | 63 | @staticmethod 64 | @once_differentiable 65 | def backward(ctx, grad_output): 66 | if not grad_output.is_cuda: 67 | raise NotImplementedError 68 | 69 | data, rois, offset = ctx.saved_tensors 70 | output_count = ctx.output_count 71 | grad_input = torch.zeros_like(data) 72 | grad_rois = None 73 | grad_offset = torch.zeros_like(offset) 74 | 75 | _C.deform_psroi_pooling_backward( 76 | grad_output, 77 | data, 78 | rois, 79 | offset, 80 | output_count, 81 | grad_input, 82 | grad_offset, 83 | ctx.no_trans, 84 | ctx.spatial_scale, 85 | ctx.out_channels, 86 | ctx.group_size, 87 | ctx.out_size, 88 | ctx.part_size, 89 | ctx.sample_per_part, 90 | ctx.trans_std 91 | ) 92 | return (grad_input, grad_rois, grad_offset, None, None, None, None, None, None, None, None) 93 | 94 | 95 | deform_roi_pooling = DeformRoIPoolingFunction.apply 96 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/balanced_positive_negative_sampler.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | 4 | 5 | class BalancedPositiveNegativeSampler(object): 6 | """ 7 | This class samples batches, ensuring that they contain a fixed proportion of positives 8 | """ 9 | 10 | def __init__(self, batch_size_per_image, positive_fraction): 11 | """ 12 | Arguments: 13 | batch_size_per_image (int): number of elements to be selected per image 14 | positive_fraction (float): percentage of positive elements per batch 15 | """ 16 | self.batch_size_per_image = batch_size_per_image 17 | self.positive_fraction = positive_fraction 18 | 19 | def __call__(self, matched_idxs): 20 | """ 21 | Arguments: 22 | matched idxs: list of tensors containing -1, 0 or positive values. 23 | Each tensor corresponds to a specific image. 24 | -1 values are ignored, 0 are considered as negatives and > 0 as 25 | positives. 26 | 27 | Returns: 28 | pos_idx (list[tensor]) 29 | neg_idx (list[tensor]) 30 | 31 | Returns two lists of binary masks for each image. 32 | The first list contains the positive elements that were selected, 33 | and the second list the negative example. 34 | """ 35 | pos_idx = [] 36 | neg_idx = [] 37 | for matched_idxs_per_image in matched_idxs: 38 | positive = torch.nonzero(matched_idxs_per_image >= 1).squeeze(1) 39 | negative = torch.nonzero(matched_idxs_per_image == 0).squeeze(1) 40 | 41 | num_pos = int(self.batch_size_per_image * self.positive_fraction) 42 | # protect against not enough positive examples 43 | num_pos = min(positive.numel(), num_pos) 44 | num_neg = self.batch_size_per_image - num_pos 45 | # protect against not enough negative examples 46 | num_neg = min(negative.numel(), num_neg) 47 | 48 | # randomly select positive and negative examples 49 | perm1 = torch.randperm(positive.numel(), device=positive.device)[:num_pos] 50 | perm2 = torch.randperm(negative.numel(), device=negative.device)[:num_neg] 51 | 52 | pos_idx_per_image = positive[perm1] 53 | neg_idx_per_image = negative[perm2] 54 | 55 | # create binary mask from indices 56 | pos_idx_per_image_mask = torch.zeros_like( 57 | matched_idxs_per_image, dtype=torch.bool 58 | ) 59 | neg_idx_per_image_mask = torch.zeros_like( 60 | matched_idxs_per_image, dtype=torch.bool 61 | ) 62 | pos_idx_per_image_mask[pos_idx_per_image] = 1 63 | neg_idx_per_image_mask[neg_idx_per_image] = 1 64 | 65 | pos_idx.append(pos_idx_per_image_mask) 66 | neg_idx.append(neg_idx_per_image_mask) 67 | 68 | return pos_idx, neg_idx 69 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/data/README.md: -------------------------------------------------------------------------------- 1 | # Setting Up Datasets 2 | This file describes how to perform training on other datasets. 3 | 4 | Only Pascal VOC dataset can be loaded from its original format and be outputted to Pascal style results currently. 5 | 6 | We expect the annotations from other datasets be converted to COCO json format, and 7 | the output will be in COCO-style. (i.e. AP, AP50, AP75, APs, APm, APl for bbox and segm) 8 | 9 | ## Creating Symlinks for PASCAL VOC 10 | 11 | We assume that your symlinked `datasets/voc/VOC` directory has the following structure: 12 | 13 | ``` 14 | VOC 15 | |_ JPEGImages 16 | | |_ .jpg 17 | | |_ ... 18 | | |_ .jpg 19 | |_ Annotations 20 | | |_ pascal_train.json (optional) 21 | | |_ pascal_val.json (optional) 22 | | |_ pascal_test.json (optional) 23 | | |_ .xml 24 | | |_ ... 25 | | |_ .xml 26 | |_ VOCdevkit 27 | ``` 28 | 29 | Create symlinks for `voc/VOC`: 30 | 31 | ``` 32 | cd ~/github/maskrcnn-benchmark 33 | mkdir -p datasets/voc/VOC 34 | ln -s /path/to/VOC /datasets/voc/VOC 35 | ``` 36 | Example configuration files for PASCAL VOC could be found [here](https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/configs/pascal_voc/). 37 | 38 | ### PASCAL VOC Annotations in COCO Format 39 | To output COCO-style evaluation result, PASCAL VOC annotations in COCO json format is required and could be downloaded from [here](https://storage.googleapis.com/coco-dataset/external/PASCAL_VOC.zip) 40 | via http://cocodataset.org/#external. 41 | 42 | ## Creating Symlinks for Cityscapes: 43 | 44 | We assume that your symlinked `datasets/cityscapes` directory has the following structure: 45 | 46 | ``` 47 | cityscapes 48 | |_ images 49 | | |_ .jpg 50 | | |_ ... 51 | | |_ .jpg 52 | |_ annotations 53 | | |_ instanceonly_gtFile_train.json 54 | | |_ ... 55 | |_ raw 56 | |_ gtFine 57 | |_ ... 58 | |_ README.md 59 | ``` 60 | 61 | Create symlinks for `cityscapes`: 62 | 63 | ``` 64 | cd ~/github/maskrcnn-benchmark 65 | mkdir -p datasets/cityscapes 66 | ln -s /path/to/cityscapes datasets/data/cityscapes 67 | ``` 68 | 69 | ### Steps to convert Cityscapes Annotations to COCO Format 70 | 1. Download gtFine_trainvaltest.zip from https://www.cityscapes-dataset.com/downloads/ (login required) 71 | 2. Extract it to /path/to/gtFine_trainvaltest 72 | ``` 73 | cityscapes 74 | |_ gtFine_trainvaltest.zip 75 | |_ gtFine_trainvaltest 76 | |_ gtFine 77 | ``` 78 | 3. Run the below commands to convert the annotations 79 | 80 | ``` 81 | cd ~/github 82 | git clone https://github.com/mcordts/cityscapesScripts.git 83 | cd cityscapesScripts 84 | cp ~/github/maskrcnn-benchmark/tools/cityscapes/instances2dict_with_polygons.py cityscapesscripts/evaluation 85 | python setup.py install 86 | cd ~/github/maskrcnn-benchmark 87 | python tools/cityscapes/convert_cityscapes_to_coco.py --datadir /path/to/cityscapes --outdir /path/to/cityscapes/annotations 88 | ``` 89 | 90 | Example configuration files for Cityscapes could be found [here](https://github.com/facebookresearch/maskrcnn-benchmark/blob/master/configs/cityscapes/). 91 | -------------------------------------------------------------------------------- /tests/test_fbnet.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | import unittest 4 | 5 | import numpy as np 6 | import torch 7 | import maskrcnn_benchmark.modeling.backbone.fbnet_builder as fbnet_builder 8 | 9 | 10 | TEST_CUDA = torch.cuda.is_available() 11 | 12 | 13 | def _test_primitive(self, device, op_name, op_func, N, C_in, C_out, expand, stride): 14 | op = op_func(C_in, C_out, expand, stride).to(device) 15 | input = torch.rand([N, C_in, 7, 7], dtype=torch.float32).to(device) 16 | output = op(input) 17 | self.assertEqual( 18 | output.shape[:2], torch.Size([N, C_out]), 19 | 'Primitive {} failed for shape {}.'.format(op_name, input.shape) 20 | ) 21 | 22 | 23 | class TestFBNetBuilder(unittest.TestCase): 24 | def test_identity(self): 25 | id_op = fbnet_builder.Identity(20, 20, 1) 26 | input = torch.rand([10, 20, 7, 7], dtype=torch.float32) 27 | output = id_op(input) 28 | np.testing.assert_array_equal(np.array(input), np.array(output)) 29 | 30 | id_op = fbnet_builder.Identity(20, 40, 2) 31 | input = torch.rand([10, 20, 7, 7], dtype=torch.float32) 32 | output = id_op(input) 33 | np.testing.assert_array_equal(output.shape, [10, 40, 4, 4]) 34 | 35 | def test_primitives(self): 36 | ''' Make sures the primitives runs ''' 37 | for op_name, op_func in fbnet_builder.PRIMITIVES.items(): 38 | print('Testing {}'.format(op_name)) 39 | 40 | _test_primitive( 41 | self, "cpu", 42 | op_name, op_func, 43 | N=20, C_in=16, C_out=32, expand=4, stride=1 44 | ) 45 | 46 | @unittest.skipIf(not TEST_CUDA, "no CUDA detected") 47 | def test_primitives_cuda(self): 48 | ''' Make sures the primitives runs on cuda ''' 49 | for op_name, op_func in fbnet_builder.PRIMITIVES.items(): 50 | print('Testing {}'.format(op_name)) 51 | 52 | _test_primitive( 53 | self, "cuda", 54 | op_name, op_func, 55 | N=20, C_in=16, C_out=32, expand=4, stride=1 56 | ) 57 | 58 | def test_primitives_empty_batch(self): 59 | ''' Make sures the primitives runs ''' 60 | for op_name, op_func in fbnet_builder.PRIMITIVES.items(): 61 | print('Testing {}'.format(op_name)) 62 | 63 | # test empty batch size 64 | _test_primitive( 65 | self, "cpu", 66 | op_name, op_func, 67 | N=0, C_in=16, C_out=32, expand=4, stride=1 68 | ) 69 | 70 | @unittest.skipIf(not TEST_CUDA, "no CUDA detected") 71 | def test_primitives_cuda_empty_batch(self): 72 | ''' Make sures the primitives runs ''' 73 | for op_name, op_func in fbnet_builder.PRIMITIVES.items(): 74 | print('Testing {}'.format(op_name)) 75 | 76 | # test empty batch size 77 | _test_primitive( 78 | self, "cuda", 79 | op_name, op_func, 80 | N=0, C_in=16, C_out=32, expand=4, stride=1 81 | ) 82 | 83 | if __name__ == "__main__": 84 | unittest.main() 85 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/model_zoo.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import os 3 | import sys 4 | 5 | try: 6 | from torch.hub import _download_url_to_file 7 | from torch.hub import urlparse 8 | from torch.hub import HASH_REGEX 9 | except ImportError: 10 | from torch.utils.model_zoo import _download_url_to_file 11 | from torch.utils.model_zoo import urlparse 12 | from torch.utils.model_zoo import HASH_REGEX 13 | 14 | from maskrcnn_benchmark.utils.comm import is_main_process 15 | from maskrcnn_benchmark.utils.comm import synchronize 16 | 17 | 18 | # very similar to https://github.com/pytorch/pytorch/blob/master/torch/utils/model_zoo.py 19 | # but with a few improvements and modifications 20 | def cache_url(url, model_dir=None, progress=True): 21 | r"""Loads the Torch serialized object at the given URL. 22 | If the object is already present in `model_dir`, it's deserialized and 23 | returned. The filename part of the URL should follow the naming convention 24 | ``filename-.ext`` where ```` is the first eight or more 25 | digits of the SHA256 hash of the contents of the file. The hash is used to 26 | ensure unique names and to verify the contents of the file. 27 | The default value of `model_dir` is ``$TORCH_HOME/models`` where 28 | ``$TORCH_HOME`` defaults to ``~/.torch``. The default directory can be 29 | overridden with the ``$TORCH_MODEL_ZOO`` environment variable. 30 | Args: 31 | url (string): URL of the object to download 32 | model_dir (string, optional): directory in which to save the object 33 | progress (bool, optional): whether or not to display a progress bar to stderr 34 | Example: 35 | >>> cached_file = maskrcnn_benchmark.utils.model_zoo.cache_url('https://s3.amazonaws.com/pytorch/models/resnet18-5c106cde.pth') 36 | """ 37 | if model_dir is None: 38 | torch_home = os.path.expanduser(os.getenv("TORCH_HOME", "~/.torch")) 39 | model_dir = os.getenv("TORCH_MODEL_ZOO", os.path.join(torch_home, "models")) 40 | if not os.path.exists(model_dir): 41 | os.makedirs(model_dir) 42 | parts = urlparse(url) 43 | filename = os.path.basename(parts.path) 44 | if filename == "model_final.pkl": 45 | # workaround as pre-trained Caffe2 models from Detectron have all the same filename 46 | # so make the full path the filename by replacing / with _ 47 | filename = parts.path.replace("/", "_") 48 | cached_file = os.path.join(model_dir, filename) 49 | if not os.path.exists(cached_file) and is_main_process(): 50 | sys.stderr.write('Downloading: "{}" to {}\n'.format(url, cached_file)) 51 | hash_prefix = HASH_REGEX.search(filename) 52 | if hash_prefix is not None: 53 | hash_prefix = hash_prefix.group(1) 54 | # workaround: Caffe2 models don't have a hash, but follow the R-50 convention, 55 | # which matches the hash PyTorch uses. So we skip the hash matching 56 | # if the hash_prefix is less than 6 characters 57 | if len(hash_prefix) < 6: 58 | hash_prefix = None 59 | _download_url_to_file(url, cached_file, hash_prefix, progress=progress) 60 | synchronize() 61 | return cached_file 62 | -------------------------------------------------------------------------------- /tests/test_feature_extractors.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | import unittest 4 | import copy 5 | import torch 6 | # import modules to to register feature extractors 7 | from maskrcnn_benchmark.modeling.backbone import build_backbone # NoQA 8 | from maskrcnn_benchmark.modeling.roi_heads.roi_heads import build_roi_heads # NoQA 9 | from maskrcnn_benchmark.modeling import registry 10 | from maskrcnn_benchmark.structures.bounding_box import BoxList 11 | from maskrcnn_benchmark.config import cfg as g_cfg 12 | from utils import load_config 13 | 14 | # overwrite configs if specified, otherwise default config is used 15 | FEATURE_EXTRACTORS_CFGS = { 16 | } 17 | 18 | # overwrite configs if specified, otherwise default config is used 19 | FEATURE_EXTRACTORS_INPUT_CHANNELS = { 20 | # in_channels was not used, load through config 21 | "ResNet50Conv5ROIFeatureExtractor": 1024, 22 | } 23 | 24 | 25 | def _test_feature_extractors( 26 | self, extractors, overwrite_cfgs, overwrite_in_channels 27 | ): 28 | ''' Make sure roi box feature extractors run ''' 29 | 30 | self.assertGreater(len(extractors), 0) 31 | 32 | in_channels_default = 64 33 | 34 | for name, builder in extractors.items(): 35 | print('Testing {}...'.format(name)) 36 | if name in overwrite_cfgs: 37 | cfg = load_config(overwrite_cfgs[name]) 38 | else: 39 | # Use default config if config file is not specified 40 | cfg = copy.deepcopy(g_cfg) 41 | 42 | in_channels = overwrite_in_channels.get( 43 | name, in_channels_default) 44 | 45 | fe = builder(cfg, in_channels) 46 | self.assertIsNotNone( 47 | getattr(fe, 'out_channels', None), 48 | 'Need to provide out_channels for feature extractor {}'.format(name) 49 | ) 50 | 51 | N, C_in, H, W = 2, in_channels, 24, 32 52 | input = torch.rand([N, C_in, H, W], dtype=torch.float32) 53 | bboxes = [[1, 1, 10, 10], [5, 5, 8, 8], [2, 2, 3, 4]] 54 | img_size = [384, 512] 55 | box_list = BoxList(bboxes, img_size, "xyxy") 56 | out = fe([input], [box_list] * N) 57 | self.assertEqual( 58 | out.shape[:2], 59 | torch.Size([N * len(bboxes), fe.out_channels]) 60 | ) 61 | 62 | 63 | class TestFeatureExtractors(unittest.TestCase): 64 | def test_roi_box_feature_extractors(self): 65 | ''' Make sure roi box feature extractors run ''' 66 | _test_feature_extractors( 67 | self, 68 | registry.ROI_BOX_FEATURE_EXTRACTORS, 69 | FEATURE_EXTRACTORS_CFGS, 70 | FEATURE_EXTRACTORS_INPUT_CHANNELS, 71 | ) 72 | 73 | def test_roi_keypoints_feature_extractors(self): 74 | ''' Make sure roi keypoints feature extractors run ''' 75 | _test_feature_extractors( 76 | self, 77 | registry.ROI_KEYPOINT_FEATURE_EXTRACTORS, 78 | FEATURE_EXTRACTORS_CFGS, 79 | FEATURE_EXTRACTORS_INPUT_CHANNELS, 80 | ) 81 | 82 | def test_roi_mask_feature_extractors(self): 83 | ''' Make sure roi mask feature extractors run ''' 84 | _test_feature_extractors( 85 | self, 86 | registry.ROI_MASK_FEATURE_EXTRACTORS, 87 | FEATURE_EXTRACTORS_CFGS, 88 | FEATURE_EXTRACTORS_INPUT_CHANNELS, 89 | ) 90 | 91 | 92 | if __name__ == "__main__": 93 | unittest.main() 94 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/roi_heads/box_head/box_head.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | from torch import nn 4 | 5 | from .roi_box_feature_extractors import make_roi_box_feature_extractor 6 | from .roi_box_predictors import make_roi_box_predictor 7 | from .inference import make_roi_box_post_processor 8 | from .loss import make_roi_box_loss_evaluator 9 | 10 | 11 | class ROIBoxHead(torch.nn.Module): 12 | """ 13 | Generic Box Head class. 14 | """ 15 | 16 | def __init__(self, cfg, in_channels): 17 | super(ROIBoxHead, self).__init__() 18 | self.feature_extractor = make_roi_box_feature_extractor(cfg, in_channels) 19 | self.predictor = make_roi_box_predictor( 20 | cfg, self.feature_extractor.out_channels) 21 | self.post_processor = make_roi_box_post_processor(cfg) 22 | self.loss_evaluator = make_roi_box_loss_evaluator(cfg) 23 | 24 | def forward(self, features, proposals, targets=None, is_source = True): 25 | """ 26 | Arguments: 27 | features (list[Tensor]): feature-maps from possibly several levels 28 | proposals (list[BoxList]): proposal boxes 29 | targets (list[BoxList], optional): the ground-truth targets. 30 | 31 | Returns: 32 | x (Tensor): the result of the feature extractor 33 | proposals (list[BoxList]): during training, the subsampled proposals 34 | are returned. During testing, the predicted boxlists are returned 35 | losses (dict[Tensor]): During training, returns the losses for the 36 | head. During testing, returns an empty dict. 37 | """ 38 | 39 | if self.training and is_source: 40 | # Faster R-CNN subsamples during training the proposals with a fixed 41 | # positive / negative ratio 42 | with torch.no_grad(): 43 | proposals = self.loss_evaluator.subsample(proposals, targets) 44 | 45 | # extract features that will be fed to the final classifier. The 46 | # feature_extractor generally corresponds to the pooler + heads 47 | box_logits = self.feature_extractor(features, proposals) 48 | # final classifier that converts the features into predictions 49 | class_logits, box_regression = self.predictor(box_logits) 50 | 51 | if not self.training: 52 | result, keep_list, keep_list2 = self.post_processor((class_logits, box_regression), proposals) 53 | return box_logits, result, {} 54 | 55 | if not is_source: 56 | result, keep_list, keep_list2 = self.post_processor((class_logits, box_regression), proposals) 57 | box_logits_tgt = box_logits[keep_list] 58 | if keep_list2 is not None: 59 | box_logits_tgt = box_logits_tgt[keep_list2] 60 | return box_logits_tgt, result, {} 61 | 62 | loss_classifier, loss_box_reg = self.loss_evaluator( 63 | [class_logits], [box_regression] 64 | ) 65 | return ( 66 | box_logits, 67 | proposals, 68 | dict(loss_classifier=loss_classifier, loss_box_reg=loss_box_reg), 69 | ) 70 | 71 | 72 | def build_roi_box_head(cfg, in_channels): 73 | """ 74 | Constructs a new box head. 75 | By default, uses ROIBoxHead, but if it turns out not to be enough, just register a new class 76 | and make it a parameter in the config 77 | """ 78 | return ROIBoxHead(cfg, in_channels) 79 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/backbone/pafpn.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import torch 3 | import torch.nn.functional as F 4 | from torch import nn 5 | from maskrcnn_benchmark.modeling.make_layers import conv_with_kaiming_uniform 6 | import numpy as np 7 | 8 | 9 | def prob_2_entropy(prob): 10 | """ convert probabilistic prediction maps to weighted self-information maps 11 | """ 12 | n, c, h, w = prob.size() 13 | return -torch.mul(prob, torch.log2(prob + 1e-30)) / np.log2(c) 14 | 15 | 16 | class PAFPN(nn.Module): 17 | """ 18 | Module that adds FPN on top of a list of feature maps. 19 | The feature maps are currently supposed to be in increasing depth 20 | order, and must be consecutive 21 | """ 22 | 23 | def __init__( 24 | self, cfg, in_channels, out_channels, conv_block, middle_channels = 128 25 | ): 26 | """ 27 | Arguments: 28 | 29 | """ 30 | super(PAFPN, self).__init__() 31 | 32 | # When defining each conv, its name wil be like conv_{scale}_{No. of conv for this scale} 33 | 34 | self.conv_8_1 = conv_block(in_channels, middle_channels, 3, 1) 35 | self.conv_8_2 = conv_block(middle_channels, middle_channels, 3, 1) 36 | self.conv_8_3 = conv_block(middle_channels, middle_channels, 3, 1) 37 | 38 | self.conv_4_1 = conv_block(in_channels, middle_channels, 3, 1) 39 | self.conv_4_2 = conv_block(middle_channels, middle_channels, 3, 1) 40 | 41 | self.conv_2_1 = conv_block(in_channels, middle_channels, 3, 1) 42 | 43 | self.conv_1_1 = conv_block(in_channels, middle_channels, 3, 1) 44 | 45 | self.conv_final = conv_with_kaiming_uniform()(middle_channels, out_channels, 1) 46 | 47 | self.relu = nn.ReLU(inplace=True) 48 | 49 | 50 | def forward(self, x): 51 | """ 52 | 53 | """ 54 | 55 | feature_1x = x[0] 56 | feature_2x = x[1] 57 | feature_4x = x[2] 58 | feature_8x = x[3] 59 | 60 | feature_1x_out = self.conv_1_1(feature_1x) 61 | 62 | feature_2x_out_1 = self.conv_2_1(feature_2x) 63 | feature_2x_out = F.interpolate(feature_2x_out_1, scale_factor=2, mode="nearest") 64 | 65 | feature_4x_out_1 = self.conv_4_1(feature_4x) 66 | feature_4x_out_1 = F.interpolate(feature_4x_out_1, scale_factor=2, mode="nearest") 67 | feature_4x_out_2 = self.conv_4_2(feature_4x_out_1) 68 | feature_4x_out = F.interpolate(feature_4x_out_2, scale_factor=2, mode="nearest") 69 | 70 | feature_8x_out_1 = self.conv_8_1(feature_8x) 71 | feature_8x_out_1 = F.interpolate(feature_8x_out_1, scale_factor=2, mode="nearest") 72 | feature_8x_out_2 = self.conv_8_2(feature_8x_out_1) 73 | feature_8x_out_2 = F.interpolate(feature_8x_out_2, scale_factor=2, mode="nearest") 74 | feature_8x_out_3 = self.conv_8_3(feature_8x_out_2) 75 | feature_8x_out = F.interpolate(feature_8x_out_3, scale_factor=2, mode="nearest") 76 | 77 | feature_out_1248 = feature_1x_out + feature_2x_out + feature_4x_out + feature_8x_out 78 | final_out_1248 = self.conv_final(feature_out_1248) 79 | semseg_pred = F.interpolate(final_out_1248, scale_factor=4, mode="nearest") 80 | semseg_entropy = prob_2_entropy(F.softmax(semseg_pred, dim=1)) 81 | # print('semseg pred, ', semseg_pred.size()) 82 | # print('feature out 1248 size', feature_out_1248.size()) 83 | 84 | # return x, semseg_pred 85 | 86 | return semseg_pred, semseg_entropy 87 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/backbone/backbone.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from collections import OrderedDict 3 | 4 | from torch import nn 5 | 6 | from maskrcnn_benchmark.modeling import registry 7 | from maskrcnn_benchmark.modeling.make_layers import conv_with_kaiming_uniform 8 | from . import fpn as fpn_module 9 | from . import resnet 10 | from . import pafpn as pafpn_body 11 | 12 | 13 | @registry.BACKBONES.register("R-50-C4") 14 | @registry.BACKBONES.register("R-50-C5") 15 | @registry.BACKBONES.register("R-101-C4") 16 | @registry.BACKBONES.register("R-101-C5") 17 | def build_resnet_backbone(cfg): 18 | body = resnet.ResNet(cfg) 19 | model = nn.Sequential(OrderedDict([("body", body)])) 20 | model.out_channels = cfg.MODEL.RESNETS.BACKBONE_OUT_CHANNELS 21 | return model 22 | 23 | 24 | @registry.BACKBONES.register("R-50-FPN") 25 | @registry.BACKBONES.register("R-101-FPN") 26 | @registry.BACKBONES.register("R-152-FPN") 27 | def build_resnet_fpn_backbone(cfg): 28 | body = resnet.ResNet(cfg) 29 | in_channels_stage2 = cfg.MODEL.RESNETS.RES2_OUT_CHANNELS 30 | out_channels = cfg.MODEL.RESNETS.BACKBONE_OUT_CHANNELS 31 | fpn = fpn_module.FPN( 32 | in_channels_list=[ 33 | in_channels_stage2, 34 | in_channels_stage2 * 2, 35 | in_channels_stage2 * 4, 36 | in_channels_stage2 * 8, 37 | ], 38 | out_channels=out_channels, 39 | conv_block=conv_with_kaiming_uniform( 40 | cfg.MODEL.FPN.USE_GN, cfg.MODEL.FPN.USE_RELU 41 | ), 42 | top_blocks=fpn_module.LastLevelMaxPool(), 43 | ) 44 | model = nn.Sequential(OrderedDict([("body", body), ("fpn", fpn)])) 45 | model.out_channels = out_channels 46 | return model 47 | 48 | 49 | @registry.BACKBONES.register("R-50-FPN-RETINANET") 50 | @registry.BACKBONES.register("R-101-FPN-RETINANET") 51 | def build_resnet_fpn_p3p7_backbone(cfg): 52 | body = resnet.ResNet(cfg) 53 | in_channels_stage2 = cfg.MODEL.RESNETS.RES2_OUT_CHANNELS 54 | out_channels = cfg.MODEL.RESNETS.BACKBONE_OUT_CHANNELS 55 | in_channels_p6p7 = in_channels_stage2 * 8 if cfg.MODEL.RETINANET.USE_C5 \ 56 | else out_channels 57 | fpn = fpn_module.FPN( 58 | in_channels_list=[ 59 | 0, 60 | in_channels_stage2 * 2, 61 | in_channels_stage2 * 4, 62 | in_channels_stage2 * 8, 63 | ], 64 | out_channels=out_channels, 65 | conv_block=conv_with_kaiming_uniform( 66 | cfg.MODEL.FPN.USE_GN, cfg.MODEL.FPN.USE_RELU 67 | ), 68 | top_blocks=fpn_module.LastLevelP6P7(in_channels_p6p7, out_channels), 69 | ) 70 | model = nn.Sequential(OrderedDict([("body", body), ("fpn", fpn)])) 71 | model.out_channels = out_channels 72 | return model 73 | 74 | 75 | def build_backbone(cfg): 76 | assert cfg.MODEL.BACKBONE.CONV_BODY in registry.BACKBONES, \ 77 | "cfg.MODEL.BACKBONE.CONV_BODY: {} are not registered in registry".format( 78 | cfg.MODEL.BACKBONE.CONV_BODY 79 | ) 80 | return registry.BACKBONES[cfg.MODEL.BACKBONE.CONV_BODY](cfg) 81 | 82 | 83 | 84 | def build_panoptic_fpn(cfg): 85 | pafpn = pafpn_body.PAFPN( 86 | cfg, 87 | in_channels=cfg.MODEL.RESNETS.BACKBONE_OUT_CHANNELS, 88 | out_channels=cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES, 89 | conv_block=conv_with_kaiming_uniform( 90 | cfg.MODEL.PANOPTIC.USE_GN, cfg.MODEL.PANOPTIC.USE_RELU 91 | ), 92 | ) 93 | model = nn.Sequential(OrderedDict([("pafpn", pafpn)])) 94 | return model 95 | -------------------------------------------------------------------------------- /tests/test_predictors.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | import unittest 4 | import copy 5 | import torch 6 | # import modules to to register predictors 7 | from maskrcnn_benchmark.modeling.backbone import build_backbone # NoQA 8 | from maskrcnn_benchmark.modeling.roi_heads.roi_heads import build_roi_heads # NoQA 9 | from maskrcnn_benchmark.modeling import registry 10 | from maskrcnn_benchmark.config import cfg as g_cfg 11 | from utils import load_config 12 | 13 | 14 | # overwrite configs if specified, otherwise default config is used 15 | PREDICTOR_CFGS = { 16 | } 17 | 18 | # overwrite configs if specified, otherwise default config is used 19 | PREDICTOR_INPUT_CHANNELS = { 20 | } 21 | 22 | 23 | def _test_predictors( 24 | self, predictors, overwrite_cfgs, overwrite_in_channels, 25 | hwsize, 26 | ): 27 | ''' Make sure predictors run ''' 28 | 29 | self.assertGreater(len(predictors), 0) 30 | 31 | in_channels_default = 64 32 | 33 | for name, builder in predictors.items(): 34 | print('Testing {}...'.format(name)) 35 | if name in overwrite_cfgs: 36 | cfg = load_config(overwrite_cfgs[name]) 37 | else: 38 | # Use default config if config file is not specified 39 | cfg = copy.deepcopy(g_cfg) 40 | 41 | in_channels = overwrite_in_channels.get( 42 | name, in_channels_default) 43 | 44 | fe = builder(cfg, in_channels) 45 | 46 | N, C_in, H, W = 2, in_channels, hwsize, hwsize 47 | input = torch.rand([N, C_in, H, W], dtype=torch.float32) 48 | out = fe(input) 49 | yield input, out, cfg 50 | 51 | 52 | class TestPredictors(unittest.TestCase): 53 | def test_roi_box_predictors(self): 54 | ''' Make sure roi box predictors run ''' 55 | for cur_in, cur_out, cur_cfg in _test_predictors( 56 | self, 57 | registry.ROI_BOX_PREDICTOR, 58 | PREDICTOR_CFGS, 59 | PREDICTOR_INPUT_CHANNELS, 60 | hwsize=1, 61 | ): 62 | self.assertEqual(len(cur_out), 2) 63 | scores, bbox_deltas = cur_out[0], cur_out[1] 64 | self.assertEqual( 65 | scores.shape[1], cur_cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES) 66 | self.assertEqual(scores.shape[0], cur_in.shape[0]) 67 | self.assertEqual(scores.shape[0], bbox_deltas.shape[0]) 68 | self.assertEqual(scores.shape[1] * 4, bbox_deltas.shape[1]) 69 | 70 | def test_roi_keypoints_predictors(self): 71 | ''' Make sure roi keypoint predictors run ''' 72 | for cur_in, cur_out, cur_cfg in _test_predictors( 73 | self, 74 | registry.ROI_KEYPOINT_PREDICTOR, 75 | PREDICTOR_CFGS, 76 | PREDICTOR_INPUT_CHANNELS, 77 | hwsize=14, 78 | ): 79 | self.assertEqual(cur_out.shape[0], cur_in.shape[0]) 80 | self.assertEqual( 81 | cur_out.shape[1], cur_cfg.MODEL.ROI_KEYPOINT_HEAD.NUM_CLASSES) 82 | 83 | def test_roi_mask_predictors(self): 84 | ''' Make sure roi mask predictors run ''' 85 | for cur_in, cur_out, cur_cfg in _test_predictors( 86 | self, 87 | registry.ROI_MASK_PREDICTOR, 88 | PREDICTOR_CFGS, 89 | PREDICTOR_INPUT_CHANNELS, 90 | hwsize=14, 91 | ): 92 | self.assertEqual(cur_out.shape[0], cur_in.shape[0]) 93 | self.assertEqual( 94 | cur_out.shape[1], cur_cfg.MODEL.ROI_BOX_HEAD.NUM_CLASSES) 95 | 96 | 97 | if __name__ == "__main__": 98 | unittest.main() 99 | -------------------------------------------------------------------------------- /tests/test_box_coder.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | 3 | import unittest 4 | 5 | import numpy as np 6 | import torch 7 | from maskrcnn_benchmark.modeling.box_coder import BoxCoder 8 | 9 | 10 | class TestBoxCoder(unittest.TestCase): 11 | def test_box_decoder(self): 12 | """ Match unit test UtilsBoxesTest.TestBboxTransformRandom in 13 | caffe2/operators/generate_proposals_op_util_boxes_test.cc 14 | """ 15 | box_coder = BoxCoder(weights=(1.0, 1.0, 1.0, 1.0)) 16 | bbox = torch.from_numpy( 17 | np.array( 18 | [ 19 | 175.62031555, 20 | 20.91103172, 21 | 253.352005, 22 | 155.0145874, 23 | 169.24636841, 24 | 4.85241556, 25 | 228.8605957, 26 | 105.02092743, 27 | 181.77426147, 28 | 199.82876587, 29 | 192.88427734, 30 | 214.0255127, 31 | 174.36262512, 32 | 186.75761414, 33 | 296.19091797, 34 | 231.27906799, 35 | 22.73153877, 36 | 92.02596283, 37 | 135.5695343, 38 | 208.80291748, 39 | ] 40 | ) 41 | .astype(np.float32) 42 | .reshape(-1, 4) 43 | ) 44 | 45 | deltas = torch.from_numpy( 46 | np.array( 47 | [ 48 | 0.47861834, 49 | 0.13992102, 50 | 0.14961673, 51 | 0.71495209, 52 | 0.29915856, 53 | -0.35664671, 54 | 0.89018666, 55 | 0.70815367, 56 | -0.03852064, 57 | 0.44466892, 58 | 0.49492538, 59 | 0.71409376, 60 | 0.28052918, 61 | 0.02184832, 62 | 0.65289006, 63 | 1.05060139, 64 | -0.38172557, 65 | -0.08533806, 66 | -0.60335309, 67 | 0.79052375, 68 | ] 69 | ) 70 | .astype(np.float32) 71 | .reshape(-1, 4) 72 | ) 73 | 74 | gt_bbox = ( 75 | np.array( 76 | [ 77 | 206.949539, 78 | -30.715202, 79 | 297.387665, 80 | 244.448486, 81 | 143.871216, 82 | -83.342888, 83 | 290.502289, 84 | 121.053398, 85 | 177.430283, 86 | 198.666245, 87 | 196.295273, 88 | 228.703079, 89 | 152.251892, 90 | 145.431564, 91 | 387.215454, 92 | 274.594238, 93 | 5.062420, 94 | 11.040955, 95 | 66.328903, 96 | 269.686218, 97 | ] 98 | ) 99 | .astype(np.float32) 100 | .reshape(-1, 4) 101 | ) 102 | 103 | results = box_coder.decode(deltas, bbox) 104 | 105 | np.testing.assert_allclose(results.detach().numpy(), gt_bbox, atol=1e-4) 106 | 107 | 108 | if __name__ == "__main__": 109 | unittest.main() 110 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/box_coder.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | import math 3 | 4 | import torch 5 | 6 | 7 | class BoxCoder(object): 8 | """ 9 | This class encodes and decodes a set of bounding boxes into 10 | the representation used for training the regressors. 11 | """ 12 | 13 | def __init__(self, weights, bbox_xform_clip=math.log(1000. / 16)): 14 | """ 15 | Arguments: 16 | weights (4-element tuple) 17 | bbox_xform_clip (float) 18 | """ 19 | self.weights = weights 20 | self.bbox_xform_clip = bbox_xform_clip 21 | 22 | def encode(self, reference_boxes, proposals): 23 | """ 24 | Encode a set of proposals with respect to some 25 | reference boxes 26 | 27 | Arguments: 28 | reference_boxes (Tensor): reference boxes 29 | proposals (Tensor): boxes to be encoded 30 | """ 31 | 32 | TO_REMOVE = 1 # TODO remove 33 | ex_widths = proposals[:, 2] - proposals[:, 0] + TO_REMOVE 34 | ex_heights = proposals[:, 3] - proposals[:, 1] + TO_REMOVE 35 | ex_ctr_x = proposals[:, 0] + 0.5 * ex_widths 36 | ex_ctr_y = proposals[:, 1] + 0.5 * ex_heights 37 | 38 | gt_widths = reference_boxes[:, 2] - reference_boxes[:, 0] + TO_REMOVE 39 | gt_heights = reference_boxes[:, 3] - reference_boxes[:, 1] + TO_REMOVE 40 | gt_ctr_x = reference_boxes[:, 0] + 0.5 * gt_widths 41 | gt_ctr_y = reference_boxes[:, 1] + 0.5 * gt_heights 42 | 43 | wx, wy, ww, wh = self.weights 44 | targets_dx = wx * (gt_ctr_x - ex_ctr_x) / ex_widths 45 | targets_dy = wy * (gt_ctr_y - ex_ctr_y) / ex_heights 46 | targets_dw = ww * torch.log(gt_widths / ex_widths) 47 | targets_dh = wh * torch.log(gt_heights / ex_heights) 48 | 49 | targets = torch.stack((targets_dx, targets_dy, targets_dw, targets_dh), dim=1) 50 | return targets 51 | 52 | def decode(self, rel_codes, boxes): 53 | """ 54 | From a set of original boxes and encoded relative box offsets, 55 | get the decoded boxes. 56 | 57 | Arguments: 58 | rel_codes (Tensor): encoded boxes 59 | boxes (Tensor): reference boxes. 60 | """ 61 | 62 | boxes = boxes.to(rel_codes.dtype) 63 | 64 | TO_REMOVE = 1 # TODO remove 65 | widths = boxes[:, 2] - boxes[:, 0] + TO_REMOVE 66 | heights = boxes[:, 3] - boxes[:, 1] + TO_REMOVE 67 | ctr_x = boxes[:, 0] + 0.5 * widths 68 | ctr_y = boxes[:, 1] + 0.5 * heights 69 | 70 | wx, wy, ww, wh = self.weights 71 | dx = rel_codes[:, 0::4] / wx 72 | dy = rel_codes[:, 1::4] / wy 73 | dw = rel_codes[:, 2::4] / ww 74 | dh = rel_codes[:, 3::4] / wh 75 | 76 | # Prevent sending too large values into torch.exp() 77 | dw = torch.clamp(dw, max=self.bbox_xform_clip) 78 | dh = torch.clamp(dh, max=self.bbox_xform_clip) 79 | 80 | pred_ctr_x = dx * widths[:, None] + ctr_x[:, None] 81 | pred_ctr_y = dy * heights[:, None] + ctr_y[:, None] 82 | pred_w = torch.exp(dw) * widths[:, None] 83 | pred_h = torch.exp(dh) * heights[:, None] 84 | 85 | pred_boxes = torch.zeros_like(rel_codes) 86 | # x1 87 | pred_boxes[:, 0::4] = pred_ctr_x - 0.5 * pred_w 88 | # y1 89 | pred_boxes[:, 1::4] = pred_ctr_y - 0.5 * pred_h 90 | # x2 (note: "- 1" is correct; don't be fooled by the asymmetry) 91 | pred_boxes[:, 2::4] = pred_ctr_x + 0.5 * pred_w - 1 92 | # y2 (note: "- 1" is correct; don't be fooled by the asymmetry) 93 | pred_boxes[:, 3::4] = pred_ctr_y + 0.5 * pred_h - 1 94 | 95 | return pred_boxes 96 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/model_serialization.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 2 | from collections import OrderedDict 3 | import logging 4 | 5 | import torch 6 | 7 | from maskrcnn_benchmark.utils.imports import import_file 8 | 9 | 10 | def align_and_update_state_dicts(model_state_dict, loaded_state_dict): 11 | """ 12 | Strategy: suppose that the models that we will create will have prefixes appended 13 | to each of its keys, for example due to an extra level of nesting that the original 14 | pre-trained weights from ImageNet won't contain. For example, model.state_dict() 15 | might return backbone[0].body.res2.conv1.weight, while the pre-trained model contains 16 | res2.conv1.weight. We thus want to match both parameters together. 17 | For that, we look for each model weight, look among all loaded keys if there is one 18 | that is a suffix of the current weight name, and use it if that's the case. 19 | If multiple matches exist, take the one with longest size 20 | of the corresponding name. For example, for the same model as before, the pretrained 21 | weight file can contain both res2.conv1.weight, as well as conv1.weight. In this case, 22 | we want to match backbone[0].body.conv1.weight to conv1.weight, and 23 | backbone[0].body.res2.conv1.weight to res2.conv1.weight. 24 | """ 25 | current_keys = sorted(list(model_state_dict.keys())) 26 | loaded_keys = sorted(list(loaded_state_dict.keys())) 27 | # get a matrix of string matches, where each (i, j) entry correspond to the size of the 28 | # loaded_key string, if it matches 29 | match_matrix = [ 30 | len(j) if i.endswith(j) else 0 for i in current_keys for j in loaded_keys 31 | ] 32 | match_matrix = torch.as_tensor(match_matrix).view( 33 | len(current_keys), len(loaded_keys) 34 | ) 35 | max_match_size, idxs = match_matrix.max(1) 36 | # remove indices that correspond to no-match 37 | idxs[max_match_size == 0] = -1 38 | 39 | # used for logging 40 | max_size = max([len(key) for key in current_keys]) if current_keys else 1 41 | max_size_loaded = max([len(key) for key in loaded_keys]) if loaded_keys else 1 42 | log_str_template = "{: <{}} loaded from {: <{}} of shape {}" 43 | logger = logging.getLogger(__name__) 44 | for idx_new, idx_old in enumerate(idxs.tolist()): 45 | if idx_old == -1: 46 | continue 47 | key = current_keys[idx_new] 48 | key_old = loaded_keys[idx_old] 49 | model_state_dict[key] = loaded_state_dict[key_old] 50 | logger.info( 51 | log_str_template.format( 52 | key, 53 | max_size, 54 | key_old, 55 | max_size_loaded, 56 | tuple(loaded_state_dict[key_old].shape), 57 | ) 58 | ) 59 | 60 | 61 | def strip_prefix_if_present(state_dict, prefix): 62 | keys = sorted(state_dict.keys()) 63 | if not all(key.startswith(prefix) for key in keys): 64 | return state_dict 65 | stripped_state_dict = OrderedDict() 66 | for key, value in state_dict.items(): 67 | stripped_state_dict[key.replace(prefix, "")] = value 68 | return stripped_state_dict 69 | 70 | 71 | def load_state_dict(model, loaded_state_dict): 72 | model_state_dict = model.state_dict() 73 | # if the state_dict comes from a model that was wrapped in a 74 | # DataParallel or DistributedDataParallel during serialization, 75 | # remove the "module" prefix before performing the matching 76 | loaded_state_dict = strip_prefix_if_present(loaded_state_dict, prefix="module.") 77 | align_and_update_state_dicts(model_state_dict, loaded_state_dict) 78 | 79 | # use strict loading 80 | model.load_state_dict(model_state_dict) 81 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/utils/comm.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file contains primitives for multi-gpu communication. 3 | This is useful when doing distributed training. 4 | """ 5 | 6 | import pickle 7 | import time 8 | 9 | import torch 10 | import torch.distributed as dist 11 | 12 | 13 | def get_world_size(): 14 | if not dist.is_available(): 15 | return 1 16 | if not dist.is_initialized(): 17 | return 1 18 | return dist.get_world_size() 19 | 20 | 21 | def get_rank(): 22 | if not dist.is_available(): 23 | return 0 24 | if not dist.is_initialized(): 25 | return 0 26 | return dist.get_rank() 27 | 28 | 29 | def is_main_process(): 30 | return get_rank() == 0 31 | 32 | 33 | def synchronize(): 34 | """ 35 | Helper function to synchronize (barrier) among all processes when 36 | using distributed training 37 | """ 38 | if not dist.is_available(): 39 | return 40 | if not dist.is_initialized(): 41 | return 42 | world_size = dist.get_world_size() 43 | if world_size == 1: 44 | return 45 | dist.barrier() 46 | 47 | 48 | def all_gather(data): 49 | """ 50 | Run all_gather on arbitrary picklable data (not necessarily tensors) 51 | Args: 52 | data: any picklable object 53 | Returns: 54 | list[data]: list of data gathered from each rank 55 | """ 56 | world_size = get_world_size() 57 | if world_size == 1: 58 | return [data] 59 | 60 | # serialized to a Tensor 61 | buffer = pickle.dumps(data) 62 | storage = torch.ByteStorage.from_buffer(buffer) 63 | tensor = torch.ByteTensor(storage).to("cuda") 64 | 65 | # obtain Tensor size of each rank 66 | local_size = torch.LongTensor([tensor.numel()]).to("cuda") 67 | size_list = [torch.LongTensor([0]).to("cuda") for _ in range(world_size)] 68 | dist.all_gather(size_list, local_size) 69 | size_list = [int(size.item()) for size in size_list] 70 | max_size = max(size_list) 71 | 72 | # receiving Tensor from all ranks 73 | # we pad the tensor because torch all_gather does not support 74 | # gathering tensors of different shapes 75 | tensor_list = [] 76 | for _ in size_list: 77 | tensor_list.append(torch.ByteTensor(size=(max_size,)).to("cuda")) 78 | if local_size != max_size: 79 | padding = torch.ByteTensor(size=(max_size - local_size,)).to("cuda") 80 | tensor = torch.cat((tensor, padding), dim=0) 81 | dist.all_gather(tensor_list, tensor) 82 | 83 | data_list = [] 84 | for size, tensor in zip(size_list, tensor_list): 85 | buffer = tensor.cpu().numpy().tobytes()[:size] 86 | data_list.append(pickle.loads(buffer)) 87 | 88 | return data_list 89 | 90 | 91 | def reduce_dict(input_dict, average=True): 92 | """ 93 | Args: 94 | input_dict (dict): all the values will be reduced 95 | average (bool): whether to do average or sum 96 | Reduce the values in the dictionary from all processes so that process with rank 97 | 0 has the averaged results. Returns a dict with the same fields as 98 | input_dict, after reduction. 99 | """ 100 | world_size = get_world_size() 101 | if world_size < 2: 102 | return input_dict 103 | with torch.no_grad(): 104 | names = [] 105 | values = [] 106 | # sort the keys so that they are consistent across processes 107 | for k in sorted(input_dict.keys()): 108 | names.append(k) 109 | values.append(input_dict[k]) 110 | values = torch.stack(values, dim=0) 111 | dist.reduce(values, dst=0) 112 | if dist.get_rank() == 0 and average: 113 | # only main process gets accumulated, so only divide by 114 | # world_size in this case 115 | values /= world_size 116 | reduced_dict = {k: v for k, v in zip(names, values)} 117 | return reduced_dict 118 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/modeling/rpn/retinanet/loss.py: -------------------------------------------------------------------------------- 1 | """ 2 | This file contains specific functions for computing losses on the RetinaNet 3 | file 4 | """ 5 | 6 | import torch 7 | from torch.nn import functional as F 8 | 9 | from ..utils import concat_box_prediction_layers 10 | 11 | from maskrcnn_benchmark.layers import smooth_l1_loss 12 | from maskrcnn_benchmark.layers import SigmoidFocalLoss 13 | from maskrcnn_benchmark.modeling.matcher import Matcher 14 | from maskrcnn_benchmark.modeling.utils import cat 15 | from maskrcnn_benchmark.structures.boxlist_ops import boxlist_iou 16 | from maskrcnn_benchmark.structures.boxlist_ops import cat_boxlist 17 | from maskrcnn_benchmark.modeling.rpn.loss import RPNLossComputation 18 | 19 | class RetinaNetLossComputation(RPNLossComputation): 20 | """ 21 | This class computes the RetinaNet loss. 22 | """ 23 | 24 | def __init__(self, proposal_matcher, box_coder, 25 | generate_labels_func, 26 | sigmoid_focal_loss, 27 | bbox_reg_beta=0.11, 28 | regress_norm=1.0): 29 | """ 30 | Arguments: 31 | proposal_matcher (Matcher) 32 | box_coder (BoxCoder) 33 | """ 34 | self.proposal_matcher = proposal_matcher 35 | self.box_coder = box_coder 36 | self.box_cls_loss_func = sigmoid_focal_loss 37 | self.bbox_reg_beta = bbox_reg_beta 38 | self.copied_fields = ['labels'] 39 | self.generate_labels_func = generate_labels_func 40 | self.discard_cases = ['between_thresholds'] 41 | self.regress_norm = regress_norm 42 | 43 | def __call__(self, anchors, box_cls, box_regression, targets): 44 | """ 45 | Arguments: 46 | anchors (list[BoxList]) 47 | box_cls (list[Tensor]) 48 | box_regression (list[Tensor]) 49 | targets (list[BoxList]) 50 | 51 | Returns: 52 | retinanet_cls_loss (Tensor) 53 | retinanet_regression_loss (Tensor 54 | """ 55 | anchors = [cat_boxlist(anchors_per_image) for anchors_per_image in anchors] 56 | labels, regression_targets = self.prepare_targets(anchors, targets) 57 | 58 | N = len(labels) 59 | box_cls, box_regression = \ 60 | concat_box_prediction_layers(box_cls, box_regression) 61 | 62 | labels = torch.cat(labels, dim=0) 63 | regression_targets = torch.cat(regression_targets, dim=0) 64 | pos_inds = torch.nonzero(labels > 0).squeeze(1) 65 | 66 | retinanet_regression_loss = smooth_l1_loss( 67 | box_regression[pos_inds], 68 | regression_targets[pos_inds], 69 | beta=self.bbox_reg_beta, 70 | size_average=False, 71 | ) / (max(1, pos_inds.numel() * self.regress_norm)) 72 | 73 | labels = labels.int() 74 | 75 | retinanet_cls_loss = self.box_cls_loss_func( 76 | box_cls, 77 | labels 78 | ) / (pos_inds.numel() + N) 79 | 80 | return retinanet_cls_loss, retinanet_regression_loss 81 | 82 | 83 | def generate_retinanet_labels(matched_targets): 84 | labels_per_image = matched_targets.get_field("labels") 85 | return labels_per_image 86 | 87 | 88 | def make_retinanet_loss_evaluator(cfg, box_coder): 89 | matcher = Matcher( 90 | cfg.MODEL.RETINANET.FG_IOU_THRESHOLD, 91 | cfg.MODEL.RETINANET.BG_IOU_THRESHOLD, 92 | allow_low_quality_matches=True, 93 | ) 94 | sigmoid_focal_loss = SigmoidFocalLoss( 95 | cfg.MODEL.RETINANET.LOSS_GAMMA, 96 | cfg.MODEL.RETINANET.LOSS_ALPHA 97 | ) 98 | 99 | loss_evaluator = RetinaNetLossComputation( 100 | matcher, 101 | box_coder, 102 | generate_retinanet_labels, 103 | sigmoid_focal_loss, 104 | bbox_reg_beta = cfg.MODEL.RETINANET.BBOX_REG_BETA, 105 | regress_norm = cfg.MODEL.RETINANET.BBOX_REG_WEIGHT, 106 | ) 107 | return loss_evaluator 108 | -------------------------------------------------------------------------------- /maskrcnn_benchmark/csrc/cuda/deform_pool_cuda.cu: -------------------------------------------------------------------------------- 1 | // modify from 2 | // https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/blob/mmdetection/mmdet/ops/dcn/src/modulated_dcn_cuda.c 3 | 4 | // based on 5 | // author: Charles Shang 6 | // https://github.com/torch/cunn/blob/master/lib/THCUNN/generic/SpatialConvolutionMM.cu 7 | 8 | #include 9 | #include 10 | 11 | #include 12 | #include 13 | 14 | #include 15 | #include 16 | #include 17 | 18 | 19 | void DeformablePSROIPoolForward( 20 | const at::Tensor data, const at::Tensor bbox, const at::Tensor trans, 21 | at::Tensor out, at::Tensor top_count, const int batch, const int channels, 22 | const int height, const int width, const int num_bbox, 23 | const int channels_trans, const int no_trans, const float spatial_scale, 24 | const int output_dim, const int group_size, const int pooled_size, 25 | const int part_size, const int sample_per_part, const float trans_std); 26 | 27 | void DeformablePSROIPoolBackwardAcc( 28 | const at::Tensor out_grad, const at::Tensor data, const at::Tensor bbox, 29 | const at::Tensor trans, const at::Tensor top_count, at::Tensor in_grad, 30 | at::Tensor trans_grad, const int batch, const int channels, 31 | const int height, const int width, const int num_bbox, 32 | const int channels_trans, const int no_trans, const float spatial_scale, 33 | const int output_dim, const int group_size, const int pooled_size, 34 | const int part_size, const int sample_per_part, const float trans_std); 35 | 36 | void deform_psroi_pooling_cuda_forward( 37 | at::Tensor input, at::Tensor bbox, at::Tensor trans, at::Tensor out, 38 | at::Tensor top_count, const int no_trans, const float spatial_scale, 39 | const int output_dim, const int group_size, const int pooled_size, 40 | const int part_size, const int sample_per_part, const float trans_std) 41 | { 42 | AT_CHECK(input.is_contiguous(), "input tensor has to be contiguous"); 43 | 44 | const int batch = input.size(0); 45 | const int channels = input.size(1); 46 | const int height = input.size(2); 47 | const int width = input.size(3); 48 | const int channels_trans = no_trans ? 2 : trans.size(1); 49 | 50 | const int num_bbox = bbox.size(0); 51 | if (num_bbox != out.size(0)) 52 | AT_ERROR("Output shape and bbox number wont match: (%d vs %d).", 53 | out.size(0), num_bbox); 54 | 55 | DeformablePSROIPoolForward( 56 | input, bbox, trans, out, top_count, batch, channels, height, width, 57 | num_bbox, channels_trans, no_trans, spatial_scale, output_dim, group_size, 58 | pooled_size, part_size, sample_per_part, trans_std); 59 | } 60 | 61 | void deform_psroi_pooling_cuda_backward( 62 | at::Tensor out_grad, at::Tensor input, at::Tensor bbox, at::Tensor trans, 63 | at::Tensor top_count, at::Tensor input_grad, at::Tensor trans_grad, 64 | const int no_trans, const float spatial_scale, const int output_dim, 65 | const int group_size, const int pooled_size, const int part_size, 66 | const int sample_per_part, const float trans_std) 67 | { 68 | AT_CHECK(out_grad.is_contiguous(), "out_grad tensor has to be contiguous"); 69 | AT_CHECK(input.is_contiguous(), "input tensor has to be contiguous"); 70 | 71 | const int batch = input.size(0); 72 | const int channels = input.size(1); 73 | const int height = input.size(2); 74 | const int width = input.size(3); 75 | const int channels_trans = no_trans ? 2 : trans.size(1); 76 | 77 | const int num_bbox = bbox.size(0); 78 | if (num_bbox != out_grad.size(0)) 79 | AT_ERROR("Output shape and bbox number wont match: (%d vs %d).", 80 | out_grad.size(0), num_bbox); 81 | 82 | DeformablePSROIPoolBackwardAcc( 83 | out_grad, input, bbox, trans, top_count, input_grad, trans_grad, batch, 84 | channels, height, width, num_bbox, channels_trans, no_trans, 85 | spatial_scale, output_dim, group_size, pooled_size, part_size, 86 | sample_per_part, trans_std); 87 | } 88 | --------------------------------------------------------------------------------