已合并
[中国海洋大学][高校贡献][Pytorch迁移1.8][SOLOv2]-初次提交 #732
AtomGit-Bot创建于 2022年6月11日
[中国海洋大学][高校贡献][Pytorch迁移1.8][SOLOv2]-初次提交 #732
已合并
从refs/pull/732/head合入到master
共 164 个文件变更+1000-827
| @@ -1,19 +1,21 @@ | |||
| 1 | # SOLOv2 | 1 | # SOLOv2 |
| 2 | 2 | ||
| 3 | -This implements training of SOLOv2 on the Coco dataset, mainly modified from [pytorch/examples](https://github.com/WXinlong/SOLO). | 3 | +This implements training of SOLOv2 on the Coco dataset, mainly modified |
| 4 | +from [pytorch/examples](https://github.com/WXinlong/SOLO). | ||
| 4 | 5 | ||
| 5 | ## SOLOv2 Detail | 6 | ## SOLOv2 Detail |
| 6 | 7 | ||
| 7 | -As of the current date, Ascend-Pytorch is still inefficient for contiguous operations. | 8 | +As of the current date, Ascend-Pytorch is still inefficient for contiguous operations. Therefore, SOLOv2 model need to |
| 8 | -Therefore, SOLOv2 model need to be modified in the following aspects: | 9 | +be modified in the following aspects: |
| 9 | 10 | ||
| 10 | 1. Converting tensors with the dynamic shapes into tensors with fixed shapes. (This is the hardest one) | 11 | 1. Converting tensors with the dynamic shapes into tensors with fixed shapes. (This is the hardest one) |
| 11 | -2. Several operations, like the sum of `INT64`, are not supported on the NPU, so we modified tensors' `dtype` when needed | 12 | +2. Several operations, like the sum of `INT64`, are not supported on the NPU, so we modified tensors' `dtype` when |
| 12 | -3. Framework bottlenecks lead to poor performance, so we improve the original code to improve the performance of the model | 13 | + needed |
| 14 | +3. Framework bottlenecks lead to poor performance, so we improve the original code to improve the performance of the | ||
| 15 | + model | ||
| 13 | 4. We used Apex for mmdtection due to the hardware defects of the NPU | 16 | 4. We used Apex for mmdtection due to the hardware defects of the NPU |
| 14 | 5. ... | 17 | 5. ... |
| 15 | 18 | ||
| 16 | - | ||
| 17 | ## Requirements | 19 | ## Requirements |
| 18 | 20 | ||
| 19 | - NPU配套的run包安装 | 21 | - NPU配套的run包安装 |
| @@ -21,11 +23,14 @@ Therefore, SOLOv2 model need to be modified in the following aspects: | |||
| 21 | - PyTorch(NPU版本) | 23 | - PyTorch(NPU版本) |
| 22 | - apex(NPU版本) | 24 | - apex(NPU版本) |
| 23 | - MMCV v0.2.16 | 25 | - MMCV v0.2.16 |
| 26 | + | ||
| 24 | ### Document and data preparation | 27 | ### Document and data preparation |
| 28 | + | ||
| 25 | 1. 下载压缩modelzoo\contrib\PyTorch\cv\instance_segmentation\SOLOv2 文件夹 | 29 | 1. 下载压缩modelzoo\contrib\PyTorch\cv\instance_segmentation\SOLOv2 文件夹 |
| 26 | 2. 于npu服务器解压SOLOv2压缩包 | 30 | 2. 于npu服务器解压SOLOv2压缩包 |
| 27 | 3. 下载coco2017数据集 | 31 | 3. 下载coco2017数据集 |
| 28 | 4. 将coco数据集放于SOLOv2/data目录下,目录结构如下: | 32 | 4. 将coco数据集放于SOLOv2/data目录下,目录结构如下: |
| 33 | + | ||
| 29 | ``` | 34 | ``` |
| 30 | GFocalV2 | 35 | GFocalV2 |
| 31 | ├── configs | 36 | ├── configs |
| @@ -35,13 +40,17 @@ GFocalV2 | |||
| 35 | │ ├── train2017 19G | 40 | │ ├── train2017 19G |
| 36 | │ ├── val2017 788M | 41 | │ ├── val2017 788M |
| 37 | ``` | 42 | ``` |
| 43 | + | ||
| 38 | ### Configure the environment | 44 | ### Configure the environment |
| 45 | + | ||
| 39 | ``` | 46 | ``` |
| 40 | 进入SOLOv2目录,source环境变量 | 47 | 进入SOLOv2目录,source环境变量 |
| 41 | cd SOLOv2 | 48 | cd SOLOv2 |
| 42 | source test/env_npu.sh | 49 | source test/env_npu.sh |
| 43 | ``` | 50 | ``` |
| 51 | + | ||
| 44 | 1. 配置安装mmcv | 52 | 1. 配置安装mmcv |
| 53 | + | ||
| 45 | ``` | 54 | ``` |
| 46 | cd mmcv | 55 | cd mmcv |
| 47 | python3.7 setup.py build_ext | 56 | python3.7 setup.py build_ext |
| @@ -49,42 +58,56 @@ python3.7 setup.py develop | |||
| 49 | cd .. | 58 | cd .. |
| 50 | pip list | grep mmcv # 查看版本和路径 | 59 | pip list | grep mmcv # 查看版本和路径 |
| 51 | ``` | 60 | ``` |
| 61 | + | ||
| 52 | 2. 配置安装mmdet | 62 | 2. 配置安装mmdet |
| 63 | + | ||
| 53 | ``` | 64 | ``` |
| 54 | pip install -r requirements/build.txt | 65 | pip install -r requirements/build.txt |
| 55 | pip install "git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI" | 66 | pip install "git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI" |
| 56 | pip install -v -e . | 67 | pip install -v -e . |
| 57 | ``` | 68 | ``` |
| 69 | + | ||
| 58 | ## Train MODEL | 70 | ## Train MODEL |
| 71 | + | ||
| 59 | 进入SOLOv2目录下 | 72 | 进入SOLOv2目录下 |
| 73 | + | ||
| 60 | ### 1p | 74 | ### 1p |
| 75 | + | ||
| 61 | 导入环境变量,修改train_full_1p.sh权限并运行 | 76 | 导入环境变量,修改train_full_1p.sh权限并运行 |
| 77 | + | ||
| 62 | ``` | 78 | ``` |
| 63 | chmod +x ./test/train_full_1p.sh | 79 | chmod +x ./test/train_full_1p.sh |
| 64 | bash ./test/train_full_1p.sh --data_path=./data/coco | 80 | bash ./test/train_full_1p.sh --data_path=./data/coco |
| 65 | ``` | 81 | ``` |
| 66 | 82 | ||
| 67 | ### 8p | 83 | ### 8p |
| 84 | + | ||
| 68 | 导入环境变量,修改train_full_8p.sh权限并运行 | 85 | 导入环境变量,修改train_full_8p.sh权限并运行 |
| 86 | + | ||
| 69 | ``` | 87 | ``` |
| 70 | chmod +x ./test/train_full_8p.sh | 88 | chmod +x ./test/train_full_8p.sh |
| 71 | bash ./test/train_full_8p.sh --data_path=./data/coco | 89 | bash ./test/train_full_8p.sh --data_path=./data/coco |
| 72 | ``` | 90 | ``` |
| 73 | 91 | ||
| 74 | ### Eval | 92 | ### Eval |
| 93 | + | ||
| 75 | 修改train_eval_1p.sh权限并运行 | 94 | 修改train_eval_1p.sh权限并运行 |
| 95 | + | ||
| 76 | ``` | 96 | ``` |
| 77 | chmod +x ./test/train_eval_1p.sh | 97 | chmod +x ./test/train_eval_1p.sh |
| 78 | bash ./test/train_eval_1p.sh --data_path=./data/coco | 98 | bash ./test/train_eval_1p.sh --data_path=./data/coco |
| 79 | ``` | 99 | ``` |
| 100 | + | ||
| 80 | ### finetuning | 101 | ### finetuning |
| 102 | + | ||
| 81 | 修改train_finetune_1p.sh权限并运行 | 103 | 修改train_finetune_1p.sh权限并运行 |
| 104 | + | ||
| 82 | ``` | 105 | ``` |
| 83 | chmod +x ./test/train_eval_1p.sh | 106 | chmod +x ./test/train_eval_1p.sh |
| 84 | bash ./test/train_finetune_1p.sh --data_path=./data/coco | 107 | bash ./test/train_finetune_1p.sh --data_path=./data/coco |
| 85 | ``` | 108 | ``` |
| 86 | 109 | ||
| 87 | -## SOLOv2 training result | 110 | +## SOLOv2 training result |
| 88 | 111 | ||
| 89 | | Acc@1 | FPS | Npu/Gpu_nums | Epochs | AMP_Type | Loss_Scale | | 112 | | Acc@1 | FPS | Npu/Gpu_nums | Epochs | AMP_Type | Loss_Scale | |
| 90 | | :------: | :------: | :------: | :------: | :------: | :------: | | 113 | | :------: | :------: | :------: | :------: | :------: | :------: | |
| @@ -24,7 +24,7 @@ model = dict( | |||
| 24 | num_stages=3, | 24 | num_stages=3, |
| 25 | strides=(1, 2, 2), | 25 | strides=(1, 2, 2), |
| 26 | dilations=(1, 1, 1), | 26 | dilations=(1, 1, 1), |
| 27 | - out_indices=(2, ), | 27 | + out_indices=(2,), |
| 28 | frozen_stages=1, | 28 | frozen_stages=1, |
| 29 | norm_cfg=norm_cfg, | 29 | norm_cfg=norm_cfg, |
| 30 | norm_eval=True, | 30 | norm_eval=True, |
| @@ -24,7 +24,7 @@ model = dict( | |||
| 24 | num_stages=3, | 24 | num_stages=3, |
| 25 | strides=(1, 2, 2), | 25 | strides=(1, 2, 2), |
| 26 | dilations=(1, 1, 1), | 26 | dilations=(1, 1, 1), |
| 27 | - out_indices=(2, ), | 27 | + out_indices=(2,), |
| 28 | frozen_stages=1, | 28 | frozen_stages=1, |
| 29 | norm_cfg=norm_cfg, | 29 | norm_cfg=norm_cfg, |
| 30 | norm_eval=True, | 30 | norm_eval=True, |
| @@ -151,19 +151,19 @@ data = dict( | |||
| 151 | dataset=dict( | 151 | dataset=dict( |
| 152 | type=dataset_type, | 152 | type=dataset_type, |
| 153 | ann_file=data_root + | 153 | ann_file=data_root + |
| 154 | - 'annotations/instancesonly_filtered_gtFine_train.json', | 154 | + 'annotations/instancesonly_filtered_gtFine_train.json', |
| 155 | img_prefix=data_root + 'train/', | 155 | img_prefix=data_root + 'train/', |
| 156 | pipeline=train_pipeline)), | 156 | pipeline=train_pipeline)), |
| 157 | val=dict( | 157 | val=dict( |
| 158 | type=dataset_type, | 158 | type=dataset_type, |
| 159 | ann_file=data_root + | 159 | ann_file=data_root + |
| 160 | - 'annotations/instancesonly_filtered_gtFine_val.json', | 160 | + 'annotations/instancesonly_filtered_gtFine_val.json', |
| 161 | img_prefix=data_root + 'val/', | 161 | img_prefix=data_root + 'val/', |
| 162 | pipeline=test_pipeline), | 162 | pipeline=test_pipeline), |
| 163 | test=dict( | 163 | test=dict( |
| 164 | type=dataset_type, | 164 | type=dataset_type, |
| 165 | ann_file=data_root + | 165 | ann_file=data_root + |
| 166 | - 'annotations/instancesonly_filtered_gtFine_val.json', | 166 | + 'annotations/instancesonly_filtered_gtFine_val.json', |
| 167 | img_prefix=data_root + 'val/', | 167 | img_prefix=data_root + 'val/', |
| 168 | pipeline=test_pipeline)) | 168 | pipeline=test_pipeline)) |
| 169 | # optimizer | 169 | # optimizer |
| @@ -165,19 +165,19 @@ data = dict( | |||
| 165 | dataset=dict( | 165 | dataset=dict( |
| 166 | type=dataset_type, | 166 | type=dataset_type, |
| 167 | ann_file=data_root + | 167 | ann_file=data_root + |
| 168 | - 'annotations/instancesonly_filtered_gtFine_train.json', | 168 | + 'annotations/instancesonly_filtered_gtFine_train.json', |
| 169 | img_prefix=data_root + 'train/', | 169 | img_prefix=data_root + 'train/', |
| 170 | pipeline=train_pipeline)), | 170 | pipeline=train_pipeline)), |
| 171 | val=dict( | 171 | val=dict( |
| 172 | type=dataset_type, | 172 | type=dataset_type, |
| 173 | ann_file=data_root + | 173 | ann_file=data_root + |
| 174 | - 'annotations/instancesonly_filtered_gtFine_val.json', | 174 | + 'annotations/instancesonly_filtered_gtFine_val.json', |
| 175 | img_prefix=data_root + 'val/', | 175 | img_prefix=data_root + 'val/', |
| 176 | pipeline=test_pipeline), | 176 | pipeline=test_pipeline), |
| 177 | test=dict( | 177 | test=dict( |
| 178 | type=dataset_type, | 178 | type=dataset_type, |
| 179 | ann_file=data_root + | 179 | ann_file=data_root + |
| 180 | - 'annotations/instancesonly_filtered_gtFine_val.json', | 180 | + 'annotations/instancesonly_filtered_gtFine_val.json', |
| 181 | img_prefix=data_root + 'val/', | 181 | img_prefix=data_root + 'val/', |
| 182 | pipeline=test_pipeline)) | 182 | pipeline=test_pipeline)) |
| 183 | # optimizer | 183 | # optimizer |
| @@ -23,7 +23,7 @@ model = dict( | |||
| 23 | num_stages=3, | 23 | num_stages=3, |
| 24 | strides=(1, 2, 2), | 24 | strides=(1, 2, 2), |
| 25 | dilations=(1, 1, 1), | 25 | dilations=(1, 1, 1), |
| 26 | - out_indices=(2, ), | 26 | + out_indices=(2,), |
| 27 | frozen_stages=1, | 27 | frozen_stages=1, |
| 28 | norm_cfg=norm_cfg, | 28 | norm_cfg=norm_cfg, |
| 29 | norm_eval=True, | 29 | norm_eval=True, |
| @@ -23,7 +23,7 @@ model = dict( | |||
| 23 | num_stages=3, | 23 | num_stages=3, |
| 24 | strides=(1, 2, 2), | 24 | strides=(1, 2, 2), |
| 25 | dilations=(1, 1, 1), | 25 | dilations=(1, 1, 1), |
| 26 | - out_indices=(2, ), | 26 | + out_indices=(2,), |
| 27 | frozen_stages=1, | 27 | frozen_stages=1, |
| 28 | norm_cfg=norm_cfg, | 28 | norm_cfg=norm_cfg, |
| 29 | norm_eval=True, | 29 | norm_eval=True, |
| @@ -23,7 +23,7 @@ model = dict( | |||
| 23 | num_stages=3, | 23 | num_stages=3, |
| 24 | strides=(1, 2, 2), | 24 | strides=(1, 2, 2), |
| 25 | dilations=(1, 1, 1), | 25 | dilations=(1, 1, 1), |
| 26 | - out_indices=(2, ), | 26 | + out_indices=(2,), |
| 27 | frozen_stages=1, | 27 | frozen_stages=1, |
| 28 | norm_cfg=norm_cfg, | 28 | norm_cfg=norm_cfg, |
| 29 | norm_eval=True, | 29 | norm_eval=True, |
| @@ -24,8 +24,8 @@ model = dict( | |||
| 24 | num_modules=1, | 24 | num_modules=1, |
| 25 | num_branches=1, | 25 | num_branches=1, |
| 26 | block='BOTTLENECK', | 26 | block='BOTTLENECK', |
| 27 | - num_blocks=(4, ), | 27 | + num_blocks=(4,), |
| 28 | - num_channels=(64, )), | 28 | + num_channels=(64,)), |
| 29 | stage2=dict( | 29 | stage2=dict( |
| 30 | num_modules=1, | 30 | num_modules=1, |
| 31 | num_branches=2, | 31 | num_branches=2, |
| @@ -24,8 +24,8 @@ model = dict( | |||
| 24 | num_modules=1, | 24 | num_modules=1, |
| 25 | num_branches=1, | 25 | num_branches=1, |
| 26 | block='BOTTLENECK', | 26 | block='BOTTLENECK', |
| 27 | - num_blocks=(4, ), | 27 | + num_blocks=(4,), |
| 28 | - num_channels=(64, )), | 28 | + num_channels=(64,)), |
| 29 | stage2=dict( | 29 | stage2=dict( |
| 30 | num_modules=1, | 30 | num_modules=1, |
| 31 | num_branches=2, | 31 | num_branches=2, |
| @@ -23,8 +23,8 @@ model = dict( | |||
| 23 | num_modules=1, | 23 | num_modules=1, |
| 24 | num_branches=1, | 24 | num_branches=1, |
| 25 | block='BOTTLENECK', | 25 | block='BOTTLENECK', |
| 26 | - num_blocks=(4, ), | 26 | + num_blocks=(4,), |
| 27 | - num_channels=(64, )), | 27 | + num_channels=(64,)), |
| 28 | stage2=dict( | 28 | stage2=dict( |
| 29 | num_modules=1, | 29 | num_modules=1, |
| 30 | num_branches=2, | 30 | num_branches=2, |
| @@ -23,8 +23,8 @@ model = dict( | |||
| 23 | num_modules=1, | 23 | num_modules=1, |
| 24 | num_branches=1, | 24 | num_branches=1, |
| 25 | block='BOTTLENECK', | 25 | block='BOTTLENECK', |
| 26 | - num_blocks=(4, ), | 26 | + num_blocks=(4,), |
| 27 | - num_channels=(64, )), | 27 | + num_channels=(64,)), |
| 28 | stage2=dict( | 28 | stage2=dict( |
| 29 | num_modules=1, | 29 | num_modules=1, |
| 30 | num_branches=2, | 30 | num_branches=2, |
| @@ -23,8 +23,8 @@ model = dict( | |||
| 23 | num_modules=1, | 23 | num_modules=1, |
| 24 | num_branches=1, | 24 | num_branches=1, |
| 25 | block='BOTTLENECK', | 25 | block='BOTTLENECK', |
| 26 | - num_blocks=(4, ), | 26 | + num_blocks=(4,), |
| 27 | - num_channels=(64, )), | 27 | + num_channels=(64,)), |
| 28 | stage2=dict( | 28 | stage2=dict( |
| 29 | num_modules=1, | 29 | num_modules=1, |
| 30 | num_branches=2, | 30 | num_branches=2, |
| @@ -23,8 +23,8 @@ model = dict( | |||
| 23 | num_modules=1, | 23 | num_modules=1, |
| 24 | num_branches=1, | 24 | num_branches=1, |
| 25 | block='BOTTLENECK', | 25 | block='BOTTLENECK', |
| 26 | - num_blocks=(4, ), | 26 | + num_blocks=(4,), |
| 27 | - num_channels=(64, )), | 27 | + num_channels=(64,)), |
| 28 | stage2=dict( | 28 | stage2=dict( |
| 29 | num_modules=1, | 29 | num_modules=1, |
| 30 | num_branches=2, | 30 | num_branches=2, |
| @@ -26,8 +26,8 @@ model = dict( | |||
| 26 | num_modules=1, | 26 | num_modules=1, |
| 27 | num_branches=1, | 27 | num_branches=1, |
| 28 | block='BOTTLENECK', | 28 | block='BOTTLENECK', |
| 29 | - num_blocks=(4, ), | 29 | + num_blocks=(4,), |
| 30 | - num_channels=(64, )), | 30 | + num_channels=(64,)), |
| 31 | stage2=dict( | 31 | stage2=dict( |
| 32 | num_modules=1, | 32 | num_modules=1, |
| 33 | num_branches=2, | 33 | num_branches=2, |
| @@ -23,8 +23,8 @@ model = dict( | |||
| 23 | num_modules=1, | 23 | num_modules=1, |
| 24 | num_branches=1, | 24 | num_branches=1, |
| 25 | block='BOTTLENECK', | 25 | block='BOTTLENECK', |
| 26 | - num_blocks=(4, ), | 26 | + num_blocks=(4,), |
| 27 | - num_channels=(64, )), | 27 | + num_channels=(64,)), |
| 28 | stage2=dict( | 28 | stage2=dict( |
| 29 | num_modules=1, | 29 | num_modules=1, |
| 30 | num_branches=2, | 30 | num_branches=2, |
| @@ -23,8 +23,8 @@ model = dict( | |||
| 23 | num_modules=1, | 23 | num_modules=1, |
| 24 | num_branches=1, | 24 | num_branches=1, |
| 25 | block='BOTTLENECK', | 25 | block='BOTTLENECK', |
| 26 | - num_blocks=(4, ), | 26 | + num_blocks=(4,), |
| 27 | - num_channels=(64, )), | 27 | + num_channels=(64,)), |
| 28 | stage2=dict( | 28 | stage2=dict( |
| 29 | num_modules=1, | 29 | num_modules=1, |
| 30 | num_branches=2, | 30 | num_branches=2, |
| @@ -123,7 +123,7 @@ data = dict( | |||
| 123 | type=dataset_type, | 123 | type=dataset_type, |
| 124 | ann_file=data_root + 'annotations/instances_train2017.json', | 124 | ann_file=data_root + 'annotations/instances_train2017.json', |
| 125 | proposal_file=data_root + | 125 | proposal_file=data_root + |
| 126 | - 'libra_proposals/rpn_r50_fpn_1x_train2017.pkl', | 126 | + 'libra_proposals/rpn_r50_fpn_1x_train2017.pkl', |
| 127 | img_prefix=data_root + 'train2017/', | 127 | img_prefix=data_root + 'train2017/', |
| 128 | pipeline=train_pipeline), | 128 | pipeline=train_pipeline), |
| 129 | val=dict( | 129 | val=dict( |
| @@ -23,7 +23,7 @@ model = dict( | |||
| 23 | num_stages=3, | 23 | num_stages=3, |
| 24 | strides=(1, 2, 2), | 24 | strides=(1, 2, 2), |
| 25 | dilations=(1, 1, 1), | 25 | dilations=(1, 1, 1), |
| 26 | - out_indices=(2, ), | 26 | + out_indices=(2,), |
| 27 | frozen_stages=1, | 27 | frozen_stages=1, |
| 28 | norm_cfg=norm_cfg, | 28 | norm_cfg=norm_cfg, |
| 29 | norm_eval=True, | 29 | norm_eval=True, |
| @@ -22,7 +22,7 @@ model = dict( | |||
| 22 | num_stages=3, | 22 | num_stages=3, |
| 23 | strides=(1, 2, 2), | 23 | strides=(1, 2, 2), |
| 24 | dilations=(1, 1, 1), | 24 | dilations=(1, 1, 1), |
| 25 | - out_indices=(2, ), | 25 | + out_indices=(2,), |
| 26 | frozen_stages=1, | 26 | frozen_stages=1, |
| 27 | norm_cfg=dict(type='BN', requires_grad=False), | 27 | norm_cfg=dict(type='BN', requires_grad=False), |
| 28 | norm_eval=True, | 28 | norm_eval=True, |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=50, | 21 | depth=50, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch', | 25 | style='pytorch', |
| 26 | dcn=dict( | 26 | dcn=dict( |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=50, | 21 | depth=50, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=101, | 21 | depth=101, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=50, | 21 | depth=50, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=50, | 21 | depth=50, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=101, | 21 | depth=101, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=50, | 21 | depth=50, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=18, | 21 | depth=18, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -51,14 +51,14 @@ model = dict( | |||
| 51 | alpha=0.25, | 51 | alpha=0.25, |
| 52 | loss_weight=1.0)), | 52 | loss_weight=1.0)), |
| 53 | mask_feat_head=dict( | 53 | mask_feat_head=dict( |
| 54 | - type='MaskFeatHead', | 54 | + type='MaskFeatHead', |
| 55 | - in_channels=256, | 55 | + in_channels=256, |
| 56 | - out_channels=128, | 56 | + out_channels=128, |
| 57 | - start_level=0, | 57 | + start_level=0, |
| 58 | - end_level=3, | 58 | + end_level=3, |
| 59 | - num_classes=128, | 59 | + num_classes=128, |
| 60 | - norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), | 60 | + norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), |
| 61 | - ) | 61 | +) |
| 62 | # training and testing settings | 62 | # training and testing settings |
| 63 | train_cfg = dict() | 63 | train_cfg = dict() |
| 64 | test_cfg = dict( | 64 | test_cfg = dict( |
| @@ -79,7 +79,7 @@ train_pipeline = [ | |||
| 79 | dict(type='LoadAnnotations', with_bbox=True, with_mask=True), | 79 | dict(type='LoadAnnotations', with_bbox=True, with_mask=True), |
| 80 | dict(type='Resize', | 80 | dict(type='Resize', |
| 81 | img_scale=[(768, 512), (768, 480), (768, 448), | 81 | img_scale=[(768, 512), (768, 480), (768, 448), |
| 82 | - (768, 416), (768, 384), (768, 352)], | 82 | + (768, 416), (768, 384), (768, 352)], |
| 83 | multiscale_mode='value', | 83 | multiscale_mode='value', |
| 84 | keep_ratio=True), | 84 | keep_ratio=True), |
| 85 | dict(type='RandomFlip', flip_ratio=0.5), | 85 | dict(type='RandomFlip', flip_ratio=0.5), |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=34, | 21 | depth=34, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -51,14 +51,14 @@ model = dict( | |||
| 51 | alpha=0.25, | 51 | alpha=0.25, |
| 52 | loss_weight=1.0)), | 52 | loss_weight=1.0)), |
| 53 | mask_feat_head=dict( | 53 | mask_feat_head=dict( |
| 54 | - type='MaskFeatHead', | 54 | + type='MaskFeatHead', |
| 55 | - in_channels=256, | 55 | + in_channels=256, |
| 56 | - out_channels=128, | 56 | + out_channels=128, |
| 57 | - start_level=0, | 57 | + start_level=0, |
| 58 | - end_level=3, | 58 | + end_level=3, |
| 59 | - num_classes=128, | 59 | + num_classes=128, |
| 60 | - norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), | 60 | + norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), |
| 61 | - ) | 61 | +) |
| 62 | # training and testing settings | 62 | # training and testing settings |
| 63 | train_cfg = dict() | 63 | train_cfg = dict() |
| 64 | test_cfg = dict( | 64 | test_cfg = dict( |
| @@ -79,7 +79,7 @@ train_pipeline = [ | |||
| 79 | dict(type='LoadAnnotations', with_bbox=True, with_mask=True), | 79 | dict(type='LoadAnnotations', with_bbox=True, with_mask=True), |
| 80 | dict(type='Resize', | 80 | dict(type='Resize', |
| 81 | img_scale=[(768, 512), (768, 480), (768, 448), | 81 | img_scale=[(768, 512), (768, 480), (768, 448), |
| 82 | - (768, 416), (768, 384), (768, 352)], | 82 | + (768, 416), (768, 384), (768, 352)], |
| 83 | multiscale_mode='value', | 83 | multiscale_mode='value', |
| 84 | keep_ratio=True), | 84 | keep_ratio=True), |
| 85 | dict(type='RandomFlip', flip_ratio=0.5), | 85 | dict(type='RandomFlip', flip_ratio=0.5), |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=50, | 21 | depth=50, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -51,14 +51,14 @@ model = dict( | |||
| 51 | alpha=0.25, | 51 | alpha=0.25, |
| 52 | loss_weight=1.0)), | 52 | loss_weight=1.0)), |
| 53 | mask_feat_head=dict( | 53 | mask_feat_head=dict( |
| 54 | - type='MaskFeatHead', | 54 | + type='MaskFeatHead', |
| 55 | - in_channels=256, | 55 | + in_channels=256, |
| 56 | - out_channels=128, | 56 | + out_channels=128, |
| 57 | - start_level=0, | 57 | + start_level=0, |
| 58 | - end_level=3, | 58 | + end_level=3, |
| 59 | - num_classes=128, | 59 | + num_classes=128, |
| 60 | - norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), | 60 | + norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), |
| 61 | - ) | 61 | +) |
| 62 | # training and testing settings | 62 | # training and testing settings |
| 63 | train_cfg = dict() | 63 | train_cfg = dict() |
| 64 | test_cfg = dict( | 64 | test_cfg = dict( |
| @@ -79,7 +79,7 @@ train_pipeline = [ | |||
| 79 | dict(type='LoadAnnotations', with_bbox=True, with_mask=True), | 79 | dict(type='LoadAnnotations', with_bbox=True, with_mask=True), |
| 80 | dict(type='Resize', | 80 | dict(type='Resize', |
| 81 | img_scale=[(768, 512), (768, 480), (768, 448), | 81 | img_scale=[(768, 512), (768, 480), (768, 448), |
| 82 | - (768, 416), (768, 384), (768, 352)], | 82 | + (768, 416), (768, 384), (768, 352)], |
| 83 | multiscale_mode='value', | 83 | multiscale_mode='value', |
| 84 | keep_ratio=True), | 84 | keep_ratio=True), |
| 85 | dict(type='RandomFlip', flip_ratio=0.5), | 85 | dict(type='RandomFlip', flip_ratio=0.5), |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=50, | 21 | depth=50, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch', | 25 | style='pytorch', |
| 26 | dcn=dict( | 26 | dcn=dict( |
| @@ -58,14 +58,14 @@ model = dict( | |||
| 58 | alpha=0.25, | 58 | alpha=0.25, |
| 59 | loss_weight=1.0)), | 59 | loss_weight=1.0)), |
| 60 | mask_feat_head=dict( | 60 | mask_feat_head=dict( |
| 61 | - type='MaskFeatHead', | 61 | + type='MaskFeatHead', |
| 62 | - in_channels=256, | 62 | + in_channels=256, |
| 63 | - out_channels=128, | 63 | + out_channels=128, |
| 64 | - start_level=0, | 64 | + start_level=0, |
| 65 | - end_level=3, | 65 | + end_level=3, |
| 66 | - num_classes=128, | 66 | + num_classes=128, |
| 67 | - norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), | 67 | + norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), |
| 68 | - ) | 68 | +) |
| 69 | # training and testing settings | 69 | # training and testing settings |
| 70 | train_cfg = dict() | 70 | train_cfg = dict() |
| 71 | test_cfg = dict( | 71 | test_cfg = dict( |
| @@ -86,7 +86,7 @@ train_pipeline = [ | |||
| 86 | dict(type='LoadAnnotations', with_bbox=True, with_mask=True), | 86 | dict(type='LoadAnnotations', with_bbox=True, with_mask=True), |
| 87 | dict(type='Resize', | 87 | dict(type='Resize', |
| 88 | img_scale=[(852, 512), (852, 480), (852, 448), | 88 | img_scale=[(852, 512), (852, 480), (852, 448), |
| 89 | - (852, 416), (852, 384), (852, 352)], | 89 | + (852, 416), (852, 384), (852, 352)], |
| 90 | multiscale_mode='value', | 90 | multiscale_mode='value', |
| 91 | keep_ratio=True), | 91 | keep_ratio=True), |
| 92 | dict(type='RandomFlip', flip_ratio=0.5), | 92 | dict(type='RandomFlip', flip_ratio=0.5), |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=101, | 21 | depth=101, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch', | 25 | style='pytorch', |
| 26 | dcn=dict( | 26 | dcn=dict( |
| @@ -58,15 +58,15 @@ model = dict( | |||
| 58 | alpha=0.25, | 58 | alpha=0.25, |
| 59 | loss_weight=1.0)), | 59 | loss_weight=1.0)), |
| 60 | mask_feat_head=dict( | 60 | mask_feat_head=dict( |
| 61 | - type='MaskFeatHead', | 61 | + type='MaskFeatHead', |
| 62 | - in_channels=256, | 62 | + in_channels=256, |
| 63 | - out_channels=128, | 63 | + out_channels=128, |
| 64 | - start_level=0, | 64 | + start_level=0, |
| 65 | - end_level=3, | 65 | + end_level=3, |
| 66 | - num_classes=256, | 66 | + num_classes=256, |
| 67 | - conv_cfg=dict(type='DCNv2'), | 67 | + conv_cfg=dict(type='DCNv2'), |
| 68 | - norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), | 68 | + norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), |
| 69 | - ) | 69 | +) |
| 70 | # training and testing settings | 70 | # training and testing settings |
| 71 | train_cfg = dict() | 71 | train_cfg = dict() |
| 72 | test_cfg = dict( | 72 | test_cfg = dict( |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=101, | 21 | depth=101, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -51,14 +51,14 @@ model = dict( | |||
| 51 | alpha=0.25, | 51 | alpha=0.25, |
| 52 | loss_weight=1.0)), | 52 | loss_weight=1.0)), |
| 53 | mask_feat_head=dict( | 53 | mask_feat_head=dict( |
| 54 | - type='MaskFeatHead', | 54 | + type='MaskFeatHead', |
| 55 | - in_channels=256, | 55 | + in_channels=256, |
| 56 | - out_channels=128, | 56 | + out_channels=128, |
| 57 | - start_level=0, | 57 | + start_level=0, |
| 58 | - end_level=3, | 58 | + end_level=3, |
| 59 | - num_classes=256, | 59 | + num_classes=256, |
| 60 | - norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), | 60 | + norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), |
| 61 | - ) | 61 | +) |
| 62 | # training and testing settings | 62 | # training and testing settings |
| 63 | train_cfg = dict() | 63 | train_cfg = dict() |
| 64 | test_cfg = dict( | 64 | test_cfg = dict( |
| @@ -23,7 +23,7 @@ model = dict( | |||
| 23 | type='ResNet', | 23 | type='ResNet', |
| 24 | depth=50, | 24 | depth=50, |
| 25 | num_stages=4, | 25 | num_stages=4, |
| 26 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 26 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 27 | frozen_stages=1, | 27 | frozen_stages=1, |
| 28 | style='pytorch'), | 28 | style='pytorch'), |
| 29 | neck=dict( | 29 | neck=dict( |
| @@ -54,14 +54,14 @@ model = dict( | |||
| 54 | alpha=0.25, | 54 | alpha=0.25, |
| 55 | loss_weight=1.0)), | 55 | loss_weight=1.0)), |
| 56 | mask_feat_head=dict( | 56 | mask_feat_head=dict( |
| 57 | - type='MaskFeatHead', | 57 | + type='MaskFeatHead', |
| 58 | - in_channels=256, | 58 | + in_channels=256, |
| 59 | - out_channels=128, | 59 | + out_channels=128, |
| 60 | - start_level=0, | 60 | + start_level=0, |
| 61 | - end_level=3, | 61 | + end_level=3, |
| 62 | - num_classes=256, | 62 | + num_classes=256, |
| 63 | - norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), | 63 | + norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), |
| 64 | - ) | 64 | +) |
| 65 | # training and testing settings | 65 | # training and testing settings |
| 66 | train_cfg = dict() | 66 | train_cfg = dict() |
| 67 | test_cfg = dict( | 67 | test_cfg = dict( |
| @@ -83,7 +83,7 @@ train_pipeline = [ | |||
| 83 | dict(type='Resize', img_scale=(1333, 800), keep_ratio=True), | 83 | dict(type='Resize', img_scale=(1333, 800), keep_ratio=True), |
| 84 | dict(type='RandomFlip', flip_ratio=0.5), | 84 | dict(type='RandomFlip', flip_ratio=0.5), |
| 85 | dict(type='Normalize', **img_norm_cfg), | 85 | dict(type='Normalize', **img_norm_cfg), |
| 86 | - dict(type='Pad', size_divisor=1344), # diff | 86 | + dict(type='Pad', size_divisor=1344), # diff |
| 87 | dict(type='DefaultFormatBundle'), | 87 | dict(type='DefaultFormatBundle'), |
| 88 | dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']), | 88 | dict(type='Collect', keys=['img', 'gt_bboxes', 'gt_labels', 'gt_masks']), |
| 89 | ] | 89 | ] |
| @@ -20,7 +20,7 @@ model = dict( | |||
| 20 | type='ResNet', | 20 | type='ResNet', |
| 21 | depth=50, | 21 | depth=50, |
| 22 | num_stages=4, | 22 | num_stages=4, |
| 23 | - out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 | 23 | + out_indices=(0, 1, 2, 3), # C2, C3, C4, C5 |
| 24 | frozen_stages=1, | 24 | frozen_stages=1, |
| 25 | style='pytorch'), | 25 | style='pytorch'), |
| 26 | neck=dict( | 26 | neck=dict( |
| @@ -51,14 +51,14 @@ model = dict( | |||
| 51 | alpha=0.25, | 51 | alpha=0.25, |
| 52 | loss_weight=1.0)), | 52 | loss_weight=1.0)), |
| 53 | mask_feat_head=dict( | 53 | mask_feat_head=dict( |
| 54 | - type='MaskFeatHead', | 54 | + type='MaskFeatHead', |
| 55 | - in_channels=256, | 55 | + in_channels=256, |
| 56 | - out_channels=128, | 56 | + out_channels=128, |
| 57 | - start_level=0, | 57 | + start_level=0, |
| 58 | - end_level=3, | 58 | + end_level=3, |
| 59 | - num_classes=256, | 59 | + num_classes=256, |
| 60 | - norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), | 60 | + norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), |
| 61 | - ) | 61 | +) |
| 62 | # training and testing settings | 62 | # training and testing settings |
| 63 | train_cfg = dict() | 63 | train_cfg = dict() |
| 64 | test_cfg = dict( | 64 | test_cfg = dict( |
| @@ -60,15 +60,15 @@ model = dict( | |||
| 60 | alpha=0.25, | 60 | alpha=0.25, |
| 61 | loss_weight=1.0)), | 61 | loss_weight=1.0)), |
| 62 | mask_feat_head=dict( | 62 | mask_feat_head=dict( |
| 63 | - type='MaskFeatHead', | 63 | + type='MaskFeatHead', |
| 64 | - in_channels=256, | 64 | + in_channels=256, |
| 65 | - out_channels=128, | 65 | + out_channels=128, |
| 66 | - start_level=0, | 66 | + start_level=0, |
| 67 | - end_level=3, | 67 | + end_level=3, |
| 68 | - num_classes=256, | 68 | + num_classes=256, |
| 69 | - conv_cfg=dict(type='DCNv2'), | 69 | + conv_cfg=dict(type='DCNv2'), |
| 70 | - norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), | 70 | + norm_cfg=dict(type='GN', num_groups=32, requires_grad=True)), |
| 71 | - ) | 71 | +) |
| 72 | # training and testing settings | 72 | # training and testing settings |
| 73 | train_cfg = dict() | 73 | train_cfg = dict() |
| 74 | test_cfg = dict( | 74 | test_cfg = dict( |
| @@ -15,7 +15,6 @@ | |||
| 15 | from mmdet.apis import init_detector, inference_detector, show_result_pyplot, show_result_ins | 15 | from mmdet.apis import init_detector, inference_detector, show_result_pyplot, show_result_ins |
| 16 | import mmcv | 16 | import mmcv |
| 17 | 17 | ||
| 18 | - | ||
| 19 | config_file = '../configs/solo/decoupled_solo_r50_fpn_8gpu_3x.py' | 18 | config_file = '../configs/solo/decoupled_solo_r50_fpn_8gpu_3x.py' |
| 20 | # download the checkpoint from model zoo and put it in `checkpoints/` | 19 | # download the checkpoint from model zoo and put it in `checkpoints/` |
| 21 | checkpoint_file = '../checkpoints/DECOUPLED_SOLO_R50_3x.pth' | 20 | checkpoint_file = '../checkpoints/DECOUPLED_SOLO_R50_3x.pth' |
| @@ -28,6 +28,7 @@ | |||
| 28 | # | 28 | # |
| 29 | import os | 29 | import os |
| 30 | import sys | 30 | import sys |
| 31 | + | ||
| 31 | sys.path.insert(0, os.path.abspath('..')) | 32 | sys.path.insert(0, os.path.abspath('..')) |
| 32 | 33 | ||
| 33 | version_file = '../mmcv/version.py' | 34 | version_file = '../mmcv/version.py' |
| @@ -31,7 +31,7 @@ from mmcv import Config | |||
| 31 | from mmcv.runner import DistSamplerSeedHook, Runner | 31 | from mmcv.runner import DistSamplerSeedHook, Runner |
| 32 | 32 | ||
| 33 | 33 | ||
| 34 | -def accuracy(output, target, topk=(1, )): | 34 | +def accuracy(output, target, topk=(1,)): |
| 35 | """Computes the precision@k for the specified values of k""" | 35 | """Computes the precision@k for the specified values of k""" |
| 36 | with torch.no_grad(): | 36 | with torch.no_grad(): |
| 37 | maxk = max(topk) | 37 | maxk = max(topk) |
| @@ -259,7 +259,7 @@ class ResNet(nn.Module): | |||
| 259 | for i, num_blocks in enumerate(stage_blocks): | 259 | for i, num_blocks in enumerate(stage_blocks): |
| 260 | stride = strides[i] | 260 | stride = strides[i] |
| 261 | dilation = dilations[i] | 261 | dilation = dilations[i] |
| 262 | - planes = 64 * 2**i | 262 | + planes = 64 * 2 ** i |
| 263 | res_layer = make_res_layer( | 263 | res_layer = make_res_layer( |
| 264 | block, | 264 | block, |
| 265 | self.inplanes, | 265 | self.inplanes, |
| @@ -274,7 +274,7 @@ class ResNet(nn.Module): | |||
| 274 | self.add_module(layer_name, res_layer) | 274 | self.add_module(layer_name, res_layer) |
| 275 | self.res_layers.append(layer_name) | 275 | self.res_layers.append(layer_name) |
| 276 | 276 | ||
| 277 | - self.feat_dim = block.expansion * 64 * 2**(len(stage_blocks) - 1) | 277 | + self.feat_dim = block.expansion * 64 * 2 ** (len(stage_blocks) - 1) |
| 278 | 278 | ||
| 279 | def init_weights(self, pretrained=None): | 279 | def init_weights(self, pretrained=None): |
| 280 | if isinstance(pretrained, str): | 280 | if isinstance(pretrained, str): |
| @@ -108,7 +108,7 @@ class VGG(nn.Module): | |||
| 108 | num_modules = num_blocks * (2 + with_bn) + 1 | 108 | num_modules = num_blocks * (2 + with_bn) + 1 |
| 109 | end_idx = start_idx + num_modules | 109 | end_idx = start_idx + num_modules |
| 110 | dilation = dilations[i] | 110 | dilation = dilations[i] |
| 111 | - planes = 64 * 2**i if i < 4 else 512 | 111 | + planes = 64 * 2 ** i if i < 4 else 512 |
| 112 | vgg_layer = make_vgg_layer( | 112 | vgg_layer = make_vgg_layer( |
| 113 | self.inplanes, | 113 | self.inplanes, |
| 114 | planes, | 114 | planes, |
| @@ -17,7 +17,6 @@ from abc import ABCMeta, abstractmethod | |||
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | class BaseFileHandler(object): | 19 | class BaseFileHandler(object): |
| 20 | - | ||
| 21 | __metaclass__ = ABCMeta # python 2 compatibility | 20 | __metaclass__ = ABCMeta # python 2 compatibility |
| 22 | 21 | ||
| 23 | 22 | ||
| @@ -119,7 +119,6 @@ def _register_handler(handler, file_formats): | |||
| 119 | 119 | ||
| 120 | 120 | ||
| 121 | def register_handler(file_formats, **kwargs): | 121 | def register_handler(file_formats, **kwargs): |
| 122 | - | ||
| 123 | def wrap(cls): | 122 | def wrap(cls): |
| 124 | _register_handler(cls(**kwargs), file_formats) | 123 | _register_handler(cls(**kwargs), file_formats) |
| 125 | return cls | 124 | return cls |
| @@ -120,7 +120,6 @@ def gray2rgb(img): | |||
| 120 | 120 | ||
| 121 | 121 | ||
| 122 | def convert_color_factory(src, dst): | 122 | def convert_color_factory(src, dst): |
| 123 | - | ||
| 124 | code = getattr(cv2, 'COLOR_{}2{}'.format(src.upper(), dst.upper())) | 123 | code = getattr(cv2, 'COLOR_{}2{}'.format(src.upper(), dst.upper())) |
| 125 | 124 | ||
| 126 | def convert_color(img): | 125 | def convert_color(img): |
| @@ -163,13 +163,13 @@ def imcrop(img, bboxes, scale=1.0, pad_fill=None): | |||
| 163 | patch_shape = (_y2 - _y1 + 1, _x2 - _x1 + 1, chn) | 163 | patch_shape = (_y2 - _y1 + 1, _x2 - _x1 + 1, chn) |
| 164 | patch = np.array( | 164 | patch = np.array( |
| 165 | pad_fill, dtype=img.dtype) * np.ones( | 165 | pad_fill, dtype=img.dtype) * np.ones( |
| 166 | - patch_shape, dtype=img.dtype) | 166 | + patch_shape, dtype=img.dtype) |
| 167 | x_start = 0 if _x1 >= 0 else -_x1 | 167 | x_start = 0 if _x1 >= 0 else -_x1 |
| 168 | y_start = 0 if _y1 >= 0 else -_y1 | 168 | y_start = 0 if _y1 >= 0 else -_y1 |
| 169 | w = x2 - x1 + 1 | 169 | w = x2 - x1 + 1 |
| 170 | h = y2 - y1 + 1 | 170 | h = y2 - y1 + 1 |
| 171 | patch[y_start:y_start + h, x_start:x_start + w, | 171 | patch[y_start:y_start + h, x_start:x_start + w, |
| 172 | - ...] = img[y1:y1 + h, x1:x1 + w, ...] | 172 | + ...] = img[y1:y1 + h, x1:x1 + w, ...] |
| 173 | patches.append(patch) | 173 | patches.append(patch) |
| 174 | 174 | ||
| 175 | if bboxes.ndim == 1: | 175 | if bboxes.ndim == 1: |
| @@ -192,7 +192,7 @@ def impad(img, shape, pad_val=0): | |||
| 192 | if not isinstance(pad_val, (int, float)): | 192 | if not isinstance(pad_val, (int, float)): |
| 193 | assert len(pad_val) == img.shape[-1] | 193 | assert len(pad_val) == img.shape[-1] |
| 194 | if len(shape) < len(img.shape): | 194 | if len(shape) < len(img.shape): |
| 195 | - shape = shape + (img.shape[-1], ) | 195 | + shape = shape + (img.shape[-1],) |
| 196 | assert len(shape) == len(img.shape) | 196 | assert len(shape) == len(img.shape) |
| 197 | for i in range(len(shape)): | 197 | for i in range(len(shape)): |
| 198 | assert shape[i] >= img.shape[i] | 198 | assert shape[i] >= img.shape[i] |
| @@ -19,7 +19,6 @@ import torch | |||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | def assert_tensor_type(func): | 21 | def assert_tensor_type(func): |
| 22 | - | ||
| 23 | 22 | ||
| 24 | def wrapper(*args, **kwargs): | 23 | def wrapper(*args, **kwargs): |
| 25 | if not isinstance(args[0].data, torch.Tensor): | 24 | if not isinstance(args[0].data, torch.Tensor): |
| @@ -102,4 +102,3 @@ class MMDataParallel(DataParallel): | |||
| 102 | # | 102 | # |
| 103 | # inputs, kwargs = self.scatter(inputs, kwargs, self.device_ids) | 103 | # inputs, kwargs = self.scatter(inputs, kwargs, self.device_ids) |
| 104 | # return self.module.val_step(*inputs[0], **kwargs[0]) | 104 | # return self.module.val_step(*inputs[0], **kwargs[0]) |
| 105 | - | ||
| @@ -70,4 +70,4 @@ class MMDistributedDataParallel(nn.Module): | |||
| 70 | 70 | ||
| 71 | # npu_diff | 71 | # npu_diff |
| 72 | inputs, kwargs = self.scatter(inputs, kwargs, [-1]) | 72 | inputs, kwargs = self.scatter(inputs, kwargs, [-1]) |
| 73 | - return self.module(*inputs[0], **kwargs[0]) | 73 | + return self.module(*inputs[0], **kwargs[0]) |
| @@ -29,27 +29,48 @@ import mmcv | |||
| 29 | from .dist_utils import get_dist_info | 29 | from .dist_utils import get_dist_info |
| 30 | 30 | ||
| 31 | open_mmlab_model_urls = { | 31 | open_mmlab_model_urls = { |
| 32 | - 'vgg16_caffe': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/vgg16_caffe-292e1171.pth', # noqa: E501 | 32 | + 'vgg16_caffe': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/vgg16_caffe-292e1171.pth', |
| 33 | - 'resnet50_caffe': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet50_caffe-788b5fa3.pth', # noqa: E501 | 33 | + # noqa: E501 |
| 34 | - 'resnet101_caffe': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet101_caffe-3ad79236.pth', # noqa: E501 | 34 | + 'resnet50_caffe': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet50_caffe-788b5fa3.pth', |
| 35 | - 'resnext50_32x4d': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext50-32x4d-0ab1a123.pth', # noqa: E501 | 35 | + # noqa: E501 |
| 36 | - 'resnext101_32x4d': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext101_32x4d-a5af3160.pth', # noqa: E501 | 36 | + 'resnet101_caffe': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet101_caffe-3ad79236.pth', |
| 37 | - 'resnext101_64x4d': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext101_64x4d-ee2c6f71.pth', # noqa: E501 | 37 | + # noqa: E501 |
| 38 | - 'contrib/resnet50_gn': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet50_gn_thangvubk-ad1730dd.pth', # noqa: E501 | 38 | + 'resnext50_32x4d': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext50-32x4d-0ab1a123.pth', |
| 39 | - 'detectron/resnet50_gn': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet50_gn-9186a21c.pth', # noqa: E501 | 39 | + # noqa: E501 |
| 40 | - 'detectron/resnet101_gn': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet101_gn-cac0ab98.pth', # noqa: E501 | 40 | + 'resnext101_32x4d': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext101_32x4d-a5af3160.pth', |
| 41 | - 'jhu/resnet50_gn_ws': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet50_gn_ws-15beedd8.pth', # noqa: E501 | 41 | + # noqa: E501 |
| 42 | - 'jhu/resnet101_gn_ws': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet101_gn_ws-3e3c308c.pth', # noqa: E501 | 42 | + 'resnext101_64x4d': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext101_64x4d-ee2c6f71.pth', |
| 43 | - 'jhu/resnext50_32x4d_gn_ws': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext50_32x4d_gn_ws-0d87ac85.pth', # noqa: E501 | 43 | + # noqa: E501 |
| 44 | - 'jhu/resnext101_32x4d_gn_ws': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext101_32x4d_gn_ws-34ac1a9e.pth', # noqa: E501 | 44 | + 'contrib/resnet50_gn': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet50_gn_thangvubk-ad1730dd.pth', |
| 45 | - 'jhu/resnext50_32x4d_gn': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext50_32x4d_gn-c7e8b754.pth', # noqa: E501 | 45 | + # noqa: E501 |
| 46 | - 'jhu/resnext101_32x4d_gn': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext101_32x4d_gn-ac3bb84e.pth', # noqa: E501 | 46 | + 'detectron/resnet50_gn': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet50_gn-9186a21c.pth', |
| 47 | - 'msra/hrnetv2_w18': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/hrnetv2_w18-00eb2006.pth', # noqa: E501 | 47 | + # noqa: E501 |
| 48 | - 'msra/hrnetv2_w32': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/hrnetv2_w32-dc9eeb4f.pth', # noqa: E501 | 48 | + 'detectron/resnet101_gn': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet101_gn-cac0ab98.pth', |
| 49 | - 'msra/hrnetv2_w40': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/hrnetv2_w40-ed0b031c.pth', # noqa: E501 | 49 | + # noqa: E501 |
| 50 | - 'bninception_caffe': 'https://open-mmlab.s3.ap-northeast-2.amazonaws.com/pretrain/third_party/bn_inception_caffe-ed2e8665.pth', # noqa: E501 | 50 | + 'jhu/resnet50_gn_ws': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet50_gn_ws-15beedd8.pth', |
| 51 | - 'kin400/i3d_r50_f32s2_k400': 'https://open-mmlab.s3.ap-northeast-2.amazonaws.com/pretrain/third_party/i3d_r50_f32s2_k400-2c57e077.pth', # noqa: E501 | 51 | + # noqa: E501 |
| 52 | - 'kin400/nl3d_r50_f32s2_k400': 'https://open-mmlab.s3.ap-northeast-2.amazonaws.com/pretrain/third_party/nl3d_r50_f32s2_k400-fa7e7caa.pth', # noqa: E501 | 52 | + 'jhu/resnet101_gn_ws': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnet101_gn_ws-3e3c308c.pth', |
| 53 | + # noqa: E501 | ||
| 54 | + 'jhu/resnext50_32x4d_gn_ws': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext50_32x4d_gn_ws-0d87ac85.pth', | ||
| 55 | + # noqa: E501 | ||
| 56 | + 'jhu/resnext101_32x4d_gn_ws': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext101_32x4d_gn_ws-34ac1a9e.pth', | ||
| 57 | + # noqa: E501 | ||
| 58 | + 'jhu/resnext50_32x4d_gn': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext50_32x4d_gn-c7e8b754.pth', | ||
| 59 | + # noqa: E501 | ||
| 60 | + 'jhu/resnext101_32x4d_gn': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/resnext101_32x4d_gn-ac3bb84e.pth', | ||
| 61 | + # noqa: E501 | ||
| 62 | + 'msra/hrnetv2_w18': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/hrnetv2_w18-00eb2006.pth', | ||
| 63 | + # noqa: E501 | ||
| 64 | + 'msra/hrnetv2_w32': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/hrnetv2_w32-dc9eeb4f.pth', | ||
| 65 | + # noqa: E501 | ||
| 66 | + 'msra/hrnetv2_w40': 'https://s3.ap-northeast-2.amazonaws.com/open-mmlab/pretrain/third_party/hrnetv2_w40-ed0b031c.pth', | ||
| 67 | + # noqa: E501 | ||
| 68 | + 'bninception_caffe': 'https://open-mmlab.s3.ap-northeast-2.amazonaws.com/pretrain/third_party/bn_inception_caffe-ed2e8665.pth', | ||
| 69 | + # noqa: E501 | ||
| 70 | + 'kin400/i3d_r50_f32s2_k400': 'https://open-mmlab.s3.ap-northeast-2.amazonaws.com/pretrain/third_party/i3d_r50_f32s2_k400-2c57e077.pth', | ||
| 71 | + # noqa: E501 | ||
| 72 | + 'kin400/nl3d_r50_f32s2_k400': 'https://open-mmlab.s3.ap-northeast-2.amazonaws.com/pretrain/third_party/nl3d_r50_f32s2_k400-fa7e7caa.pth', | ||
| 73 | + # noqa: E501 | ||
| 53 | } # yapf: disable | 74 | } # yapf: disable |
| 54 | 75 | ||
| 55 | 76 | ||
| @@ -18,6 +18,9 @@ import os | |||
| 18 | import subprocess | 18 | import subprocess |
| 19 | 19 | ||
| 20 | import torch | 20 | import torch |
| 21 | + | ||
| 22 | +if torch.__version__ >= '1.8.1': | ||
| 23 | + import torch_npu | ||
| 21 | import torch.distributed as dist | 24 | import torch.distributed as dist |
| 22 | import torch.multiprocessing as mp | 25 | import torch.multiprocessing as mp |
| 23 | 26 | ||
| @@ -87,7 +90,6 @@ def get_dist_info(): | |||
| 87 | 90 | ||
| 88 | 91 | ||
| 89 | def master_only(func): | 92 | def master_only(func): |
| 90 | - | ||
| 91 | 93 | ||
| 92 | def wrapper(*args, **kwargs): | 94 | def wrapper(*args, **kwargs): |
| 93 | rank, _ = get_dist_info() | 95 | rank, _ = get_dist_info() |
| @@ -24,12 +24,13 @@ class IterTimerHook(Hook): | |||
| 24 | self.t = time.time() | 24 | self.t = time.time() |
| 25 | self.skip_step = 0 | 25 | self.skip_step = 0 |
| 26 | self.time_all = 0 | 26 | self.time_all = 0 |
| 27 | + | ||
| 27 | def before_iter(self, runner): | 28 | def before_iter(self, runner): |
| 28 | runner.log_buffer.update({'data_time': time.time() - self.t}) | 29 | runner.log_buffer.update({'data_time': time.time() - self.t}) |
| 29 | 30 | ||
| 30 | def after_iter(self, runner): | 31 | def after_iter(self, runner): |
| 31 | ## npu diff | 32 | ## npu diff |
| 32 | - #runner.log_buffer.update({'time': time.time() - self.t}) | 33 | + # runner.log_buffer.update({'time': time.time() - self.t}) |
| 33 | cur_time = time.time() | 34 | cur_time = time.time() |
| 34 | runner.log_buffer.update({'time': time.time() - self.t}) | 35 | runner.log_buffer.update({'time': time.time() - self.t}) |
| 35 | if self.skip_step >= 5: | 36 | if self.skip_step >= 5: |
| @@ -33,7 +33,7 @@ class LoggerHook(Hook): | |||
| 33 | def __init__(self, interval=10, ignore_last=True, reset_flag=False): | 33 | def __init__(self, interval=10, ignore_last=True, reset_flag=False): |
| 34 | self.interval = interval | 34 | self.interval = interval |
| 35 | self.ignore_last = ignore_last | 35 | self.ignore_last = ignore_last |
| 36 | - self.reset_flag = reset_flag | 36 | + self.reset_flag = False # reset_flag |
| 37 | 37 | ||
| 38 | 38 | ||
| 39 | def log(self, runner): | 39 | def log(self, runner): |
| @@ -47,7 +47,7 @@ class PaviClient(object): | |||
| 47 | if not var: | 47 | if not var: |
| 48 | raise ValueError( | 48 | raise ValueError( |
| 49 | '"{}" is neither specified nor defined as env variables'. | 49 | '"{}" is neither specified nor defined as env variables'. |
| 50 | - format(env_var)) | 50 | + format(env_var)) |
| 51 | return var | 51 | return var |
| 52 | 52 | ||
| 53 | def _print_log(self, msg, level=logging.INFO, *args, **kwargs): | 53 | def _print_log(self, msg, level=logging.INFO, *args, **kwargs): |
| @@ -129,7 +129,7 @@ class PaviClient(object): | |||
| 129 | else: | 129 | else: |
| 130 | self._print_log( | 130 | self._print_log( |
| 131 | 'unexpected status code: {}, err msg: {}'. | 131 | 'unexpected status code: {}, err msg: {}'. |
| 132 | - format(status_code, response.reason), | 132 | + format(status_code, response.reason), |
| 133 | level=logging.ERROR) | 133 | level=logging.ERROR) |
| 134 | retry += 1 | 134 | retry += 1 |
| 135 | if retry == max_retry: | 135 | if retry == max_retry: |
| @@ -53,7 +53,7 @@ class TextLoggerHook(LoggerHook): | |||
| 53 | if 'time' in log_dict.keys(): | 53 | if 'time' in log_dict.keys(): |
| 54 | self.time_sec_tot += (log_dict['time'] * self.interval) | 54 | self.time_sec_tot += (log_dict['time'] * self.interval) |
| 55 | time_sec_avg = self.time_sec_tot / ( | 55 | time_sec_avg = self.time_sec_tot / ( |
| 56 | - runner.iter - self.start_iter + 1) | 56 | + runner.iter - self.start_iter + 1) |
| 57 | eta_sec = time_sec_avg * (runner.max_iters - runner.iter - 1) | 57 | eta_sec = time_sec_avg * (runner.max_iters - runner.iter - 1) |
| 58 | eta_str = str(datetime.timedelta(seconds=int(eta_sec))) | 58 | eta_str = str(datetime.timedelta(seconds=int(eta_sec))) |
| 59 | log_str += 'eta: {}, '.format(eta_str) | 59 | log_str += 'eta: {}, '.format(eta_str) |
| @@ -71,8 +71,8 @@ class TextLoggerHook(LoggerHook): | |||
| 71 | # TODO: resolve this hack | 71 | # TODO: resolve this hack |
| 72 | # these items have been in log_str | 72 | # these items have been in log_str |
| 73 | if name in [ | 73 | if name in [ |
| 74 | - 'mode', 'Epoch', 'iter', 'lr', 'time', 'data_time', | 74 | + 'mode', 'Epoch', 'iter', 'lr', 'time', 'data_time', |
| 75 | - 'memory', 'epoch' | 75 | + 'memory', 'epoch' |
| 76 | ]: | 76 | ]: |
| 77 | continue | 77 | continue |
| 78 | if isinstance(val, float): | 78 | if isinstance(val, float): |
| @@ -64,7 +64,7 @@ class LrUpdaterHook(Hook): | |||
| 64 | k = (1 - cur_iters / self.warmup_iters) * (1 - self.warmup_ratio) | 64 | k = (1 - cur_iters / self.warmup_iters) * (1 - self.warmup_ratio) |
| 65 | warmup_lr = [_lr * (1 - k) for _lr in self.regular_lr] | 65 | warmup_lr = [_lr * (1 - k) for _lr in self.regular_lr] |
| 66 | elif self.warmup == 'exp': | 66 | elif self.warmup == 'exp': |
| 67 | - k = self.warmup_ratio**(1 - cur_iters / self.warmup_iters) | 67 | + k = self.warmup_ratio ** (1 - cur_iters / self.warmup_iters) |
| 68 | warmup_lr = [_lr * k for _lr in self.regular_lr] | 68 | warmup_lr = [_lr * k for _lr in self.regular_lr] |
| 69 | return warmup_lr | 69 | return warmup_lr |
| 70 | 70 | ||
| @@ -130,14 +130,14 @@ class StepLrUpdaterHook(LrUpdaterHook): | |||
| 130 | progress = runner.epoch if self.by_epoch else runner.iter | 130 | progress = runner.epoch if self.by_epoch else runner.iter |
| 131 | 131 | ||
| 132 | if isinstance(self.step, int): | 132 | if isinstance(self.step, int): |
| 133 | - return base_lr * (self.gamma**(progress // self.step)) | 133 | + return base_lr * (self.gamma ** (progress // self.step)) |
| 134 | 134 | ||
| 135 | exp = len(self.step) | 135 | exp = len(self.step) |
| 136 | for i, s in enumerate(self.step): | 136 | for i, s in enumerate(self.step): |
| 137 | if progress < s: | 137 | if progress < s: |
| 138 | exp = i | 138 | exp = i |
| 139 | break | 139 | break |
| 140 | - return base_lr * self.gamma**exp | 140 | + return base_lr * self.gamma ** exp |
| 141 | 141 | ||
| 142 | 142 | ||
| 143 | class ExpLrUpdaterHook(LrUpdaterHook): | 143 | class ExpLrUpdaterHook(LrUpdaterHook): |
| @@ -148,7 +148,7 @@ class ExpLrUpdaterHook(LrUpdaterHook): | |||
| 148 | 148 | ||
| 149 | def get_lr(self, runner, base_lr): | 149 | def get_lr(self, runner, base_lr): |
| 150 | progress = runner.epoch if self.by_epoch else runner.iter | 150 | progress = runner.epoch if self.by_epoch else runner.iter |
| 151 | - return base_lr * self.gamma**progress | 151 | + return base_lr * self.gamma ** progress |
| 152 | 152 | ||
| 153 | 153 | ||
| 154 | class PolyLrUpdaterHook(LrUpdaterHook): | 154 | class PolyLrUpdaterHook(LrUpdaterHook): |
| @@ -165,7 +165,7 @@ class PolyLrUpdaterHook(LrUpdaterHook): | |||
| 165 | else: | 165 | else: |
| 166 | progress = runner.iter | 166 | progress = runner.iter |
| 167 | max_progress = runner.max_iters | 167 | max_progress = runner.max_iters |
| 168 | - coeff = (1 - progress / max_progress)**self.power | 168 | + coeff = (1 - progress / max_progress) ** self.power |
| 169 | return (base_lr - self.min_lr) * coeff + self.min_lr | 169 | return (base_lr - self.min_lr) * coeff + self.min_lr |
| 170 | 170 | ||
| 171 | 171 | ||
| @@ -178,7 +178,7 @@ class InvLrUpdaterHook(LrUpdaterHook): | |||
| 178 | 178 | ||
| 179 | def get_lr(self, runner, base_lr): | 179 | def get_lr(self, runner, base_lr): |
| 180 | progress = runner.epoch if self.by_epoch else runner.iter | 180 | progress = runner.epoch if self.by_epoch else runner.iter |
| 181 | - return base_lr * (1 + self.gamma * progress)**(-self.power) | 181 | + return base_lr * (1 + self.gamma * progress) ** (-self.power) |
| 182 | 182 | ||
| 183 | 183 | ||
| 184 | class CosineLrUpdaterHook(LrUpdaterHook): | 184 | class CosineLrUpdaterHook(LrUpdaterHook): |
| @@ -195,4 +195,4 @@ class CosineLrUpdaterHook(LrUpdaterHook): | |||
| 195 | progress = runner.iter | 195 | progress = runner.iter |
| 196 | max_progress = runner.max_iters | 196 | max_progress = runner.max_iters |
| 197 | return self.target_lr + 0.5 * (base_lr - self.target_lr) * \ | 197 | return self.target_lr + 0.5 * (base_lr - self.target_lr) * \ |
| 198 | - (1 + cos(pi * (progress / max_progress))) | 198 | + (1 + cos(pi * (progress / max_progress))) |
| @@ -81,10 +81,10 @@ class Runner(object): | |||
| 81 | 81 | ||
| 82 | self._rank, self._world_size = get_dist_info() | 82 | self._rank, self._world_size = get_dist_info() |
| 83 | self.timestamp = get_time_str() | 83 | self.timestamp = get_time_str() |
| 84 | - if logger is None: | 84 | + # if logger is None: |
| 85 | - self.logger = self.init_logger(work_dir, log_level) | 85 | + self.logger = self.init_logger(work_dir, log_level) |
| 86 | - else: | 86 | + # else: |
| 87 | - self.logger = logger | 87 | + # self.logger = logger |
| 88 | self.log_buffer = LogBuffer() | 88 | self.log_buffer = LogBuffer() |
| 89 | 89 | ||
| 90 | self.mode = None | 90 | self.mode = None |
| @@ -94,9 +94,11 @@ class Runner(object): | |||
| 94 | self._inner_iter = 0 | 94 | self._inner_iter = 0 |
| 95 | self._max_epochs = 0 | 95 | self._max_epochs = 0 |
| 96 | self._max_iters = 0 | 96 | self._max_iters = 0 |
| 97 | + self.train_performance = False | ||
| 97 | self.samples_per_gpu = samples_per_gpu | 98 | self.samples_per_gpu = samples_per_gpu |
| 98 | self.num_of_gpus = num_of_gpus | 99 | self.num_of_gpus = num_of_gpus |
| 99 | self.iter_time_hook = IterTimerHook() | 100 | self.iter_time_hook = IterTimerHook() |
| 101 | + | ||
| 100 | 102 | ||
| 101 | def model_name(self): | 103 | def model_name(self): |
| 102 | """str: Name of the model, usually the module class name.""" | 104 | """str: Name of the model, usually the module class name.""" |
| @@ -194,6 +196,9 @@ class Runner(object): | |||
| 194 | logging.basicConfig( | 196 | logging.basicConfig( |
| 195 | format='%(asctime)s - %(levelname)s - %(message)s', level=level) | 197 | format='%(asctime)s - %(levelname)s - %(message)s', level=level) |
| 196 | logger = logging.getLogger(__name__) | 198 | logger = logging.getLogger(__name__) |
| 199 | + if self.rank == 0: | ||
| 200 | + logger.addHandler(logging.StreamHandler()) | ||
| 201 | + logger.setLevel(logging.INFO) | ||
| 197 | if log_dir and self.rank == 0: | 202 | if log_dir and self.rank == 0: |
| 198 | filename = '{}.log'.format(self.timestamp) | 203 | filename = '{}.log'.format(self.timestamp) |
| 199 | log_file = osp.join(log_dir, filename) | 204 | log_file = osp.join(log_dir, filename) |
| @@ -325,11 +330,14 @@ class Runner(object): | |||
| 325 | self.outputs = outputs | 330 | self.outputs = outputs |
| 326 | self.call_hook('after_train_iter') | 331 | self.call_hook('after_train_iter') |
| 327 | self._iter += 1 | 332 | self._iter += 1 |
| 333 | + if i >= 500 and self.train_performance: | ||
| 334 | + exit(0) | ||
| 328 | if i % 200 == 0 and i: | 335 | if i % 200 == 0 and i: |
| 329 | self.logger.info('FPS: %02f' % (self.samples_per_gpu * self.num_of_gpus * (i - 5) / | 336 | self.logger.info('FPS: %02f' % (self.samples_per_gpu * self.num_of_gpus * (i - 5) / |
| 330 | - self.iter_time_hook.time_all)) | 337 | + self.iter_time_hook.time_all)) |
| 331 | 338 | ||
| 332 | - self.logger.info('FPS: ' + str(self.samples_per_gpu * self.num_of_gpus / self.iter_time_hook.time_all * (len(self.data_loader) - 5))) | 339 | + self.logger.info('FPS: ' + str( |
| 340 | + self.samples_per_gpu * self.num_of_gpus / self.iter_time_hook.time_all * (len(self.data_loader) - 5))) | ||
| 333 | self.call_hook('after_train_epoch') | 341 | self.call_hook('after_train_epoch') |
| 334 | self._epoch += 1 | 342 | self._epoch += 1 |
| 335 | 343 | ||
| @@ -405,14 +413,14 @@ class Runner(object): | |||
| 405 | if not hasattr(self, mode): | 413 | if not hasattr(self, mode): |
| 406 | raise ValueError( | 414 | raise ValueError( |
| 407 | 'runner has no method named "{}" to run an epoch'. | 415 | 'runner has no method named "{}" to run an epoch'. |
| 408 | - format(mode)) | 416 | + format(mode)) |
| 409 | epoch_runner = getattr(self, mode) | 417 | epoch_runner = getattr(self, mode) |
| 410 | elif callable(mode): # custom train() | 418 | elif callable(mode): # custom train() |
| 411 | epoch_runner = mode | 419 | epoch_runner = mode |
| 412 | else: | 420 | else: |
| 413 | raise TypeError('mode in workflow must be a str or ' | 421 | raise TypeError('mode in workflow must be a str or ' |
| 414 | 'callable function, not {}'.format( | 422 | 'callable function, not {}'.format( |
| 415 | - type(mode))) | 423 | + type(mode))) |
| 416 | for _ in range(epochs): | 424 | for _ in range(epochs): |
| 417 | if mode == 'train' and self.epoch >= max_epochs: | 425 | if mode == 'train' and self.epoch >= max_epochs: |
| 418 | return | 426 | return |
| @@ -158,7 +158,7 @@ def check_prerequisites( | |||
| 158 | prerequisites, | 158 | prerequisites, |
| 159 | checker, | 159 | checker, |
| 160 | msg_tmpl='Prerequisites "{}" are required in method "{}" but not ' | 160 | msg_tmpl='Prerequisites "{}" are required in method "{}" but not ' |
| 161 | - 'found, please install them first.'): # yapf: disable | 161 | + 'found, please install them first.'): # yapf: disable |
| 162 | """A decorator factory to check if prerequisites are satisfied. | 162 | """A decorator factory to check if prerequisites are satisfied. |
| 163 | 163 | ||
| 164 | Args: | 164 | Args: |
| @@ -94,7 +94,7 @@ def scandir(dir_path, suffix=None): | |||
| 94 | return _scandir_py(dir_path, suffix) | 94 | return _scandir_py(dir_path, suffix) |
| 95 | 95 | ||
| 96 | 96 | ||
| 97 | -def find_vcs_root(path, markers=('.git', )): | 97 | +def find_vcs_root(path, markers=('.git',)): |
| 98 | """Finds the root directory (including itself) of specified markers. | 98 | """Finds the root directory (including itself) of specified markers. |
| 99 | 99 | ||
| 100 | Args: | 100 | Args: |
| @@ -13,13 +13,3 @@ | |||
| 13 | # limitations under the License. | 13 | # limitations under the License. |
| 14 | 14 | ||
| 15 | # Copyright (c) Open-MMLab. All rights reserved. | 15 | # Copyright (c) Open-MMLab. All rights reserved. |
| 16 | -from .io import Cache, VideoReader, frames2video | ||
| 17 | -from .optflow import (dequantize_flow, flow_warp, flowread, flowwrite, | ||
| 18 | - quantize_flow) | ||
| 19 | -from .processing import concat_video, convert_video, cut_video, resize_video | ||
| 20 | - | ||
| 21 | -__all__ = [ | ||
| 22 | - 'Cache', 'VideoReader', 'frames2video', 'convert_video', 'resize_video', | ||
| 23 | - 'cut_video', 'concat_video', 'flowread', 'flowwrite', 'quantize_flow', | ||
| 24 | - 'dequantize_flow', 'flow_warp' | ||
| 25 | -] | ||
| @@ -54,7 +54,7 @@ def flowread(flow_or_path, quantize=False, concat_axis=0, *args, **kwargs): | |||
| 54 | if header != 'PIEH': | 54 | if header != 'PIEH': |
| 55 | raise IOError( | 55 | raise IOError( |
| 56 | 'Invalid flow file: {}, header does not contain PIEH'. | 56 | 'Invalid flow file: {}, header does not contain PIEH'. |
| 57 | - format(flow_or_path)) | 57 | + format(flow_or_path)) |
| 58 | 58 | ||
| 59 | w = np.fromfile(f, np.int32, 1).squeeze() | 59 | w = np.fromfile(f, np.int32, 1).squeeze() |
| 60 | h = np.fromfile(f, np.int32, 1).squeeze() | 60 | h = np.fromfile(f, np.int32, 1).squeeze() |
| @@ -65,7 +65,7 @@ def flowread(flow_or_path, quantize=False, concat_axis=0, *args, **kwargs): | |||
| 65 | if cat_flow.ndim != 2: | 65 | if cat_flow.ndim != 2: |
| 66 | raise IOError( | 66 | raise IOError( |
| 67 | '{} is not a valid quantized flow file, its dimension is {}.'. | 67 | '{} is not a valid quantized flow file, its dimension is {}.'. |
| 68 | - format(flow_or_path, cat_flow.ndim)) | 68 | + format(flow_or_path, cat_flow.ndim)) |
| 69 | assert cat_flow.shape[concat_axis] % 2 == 0 | 69 | assert cat_flow.shape[concat_axis] % 2 == 0 |
| 70 | dx, dy = np.split(cat_flow, 2, axis=concat_axis) | 70 | dx, dy = np.split(cat_flow, 2, axis=concat_axis) |
| 71 | flow = dequantize_flow(dx, dy, *args, **kwargs) | 71 | flow = dequantize_flow(dx, dy, *args, **kwargs) |
| @@ -18,7 +18,7 @@ from __future__ import division | |||
| 18 | import numpy as np | 18 | import numpy as np |
| 19 | 19 | ||
| 20 | from mmcv.image import rgb2bgr | 20 | from mmcv.image import rgb2bgr |
| 21 | -from mmcv.video import flowread | 21 | +# from mmcv.video import flowread |
| 22 | from .image import imshow | 22 | from .image import imshow |
| 23 | 23 | ||
| 24 | 24 | ||
| @@ -58,12 +58,12 @@ def flow2rgb(flow, color_wheel=None, unknown_thr=1e6): | |||
| 58 | dy = flow[:, :, 1].copy() | 58 | dy = flow[:, :, 1].copy() |
| 59 | 59 | ||
| 60 | ignore_inds = ( | 60 | ignore_inds = ( |
| 61 | - np.isnan(dx) | np.isnan(dy) | (np.abs(dx) > unknown_thr) | | 61 | + np.isnan(dx) | np.isnan(dy) | (np.abs(dx) > unknown_thr) | |
| 62 | - (np.abs(dy) > unknown_thr)) | 62 | + (np.abs(dy) > unknown_thr)) |
| 63 | dx[ignore_inds] = 0 | 63 | dx[ignore_inds] = 0 |
| 64 | dy[ignore_inds] = 0 | 64 | dy[ignore_inds] = 0 |
| 65 | 65 | ||
| 66 | - rad = np.sqrt(dx**2 + dy**2) | 66 | + rad = np.sqrt(dx ** 2 + dy ** 2) |
| 67 | if np.any(rad > np.finfo(float).eps): | 67 | if np.any(rad > np.finfo(float).eps): |
| 68 | max_rad = np.max(rad) | 68 | max_rad = np.max(rad) |
| 69 | dx /= max_rad | 69 | dx /= max_rad |
| @@ -71,7 +71,7 @@ def flow2rgb(flow, color_wheel=None, unknown_thr=1e6): | |||
| 71 | 71 | ||
| 72 | [h, w] = dx.shape | 72 | [h, w] = dx.shape |
| 73 | 73 | ||
| 74 | - rad = np.sqrt(dx**2 + dy**2) | 74 | + rad = np.sqrt(dx ** 2 + dy ** 2) |
| 75 | angle = np.arctan2(-dy, -dx) / np.pi | 75 | angle = np.arctan2(-dy, -dx) / np.pi |
| 76 | 76 | ||
| 77 | bin_real = (angle + 1) / 2 * (num_bins - 1) | 77 | bin_real = (angle + 1) / 2 * (num_bins - 1) |
| @@ -79,17 +79,6 @@ else: | |||
| 79 | extra_link_args = [] | 79 | extra_link_args = [] |
| 80 | 80 | ||
| 81 | EXT_MODULES = [ | 81 | EXT_MODULES = [ |
| 82 | - Extension( | ||
| 83 | - name='mmcv._ext', | ||
| 84 | - sources=[ | ||
| 85 | - './mmcv/video/optflow_warp/flow_warp.cpp', | ||
| 86 | - './mmcv/video/optflow_warp/flow_warp_module.pyx' | ||
| 87 | - ], | ||
| 88 | - include_dirs=[numpy.get_include()], | ||
| 89 | - language='c++', | ||
| 90 | - extra_compile_args=extra_compile_args, | ||
| 91 | - extra_link_args=extra_link_args, | ||
| 92 | - ), | ||
| 93 | ] | 82 | ] |
| 94 | 83 | ||
| 95 | setup( | 84 | setup( |
| @@ -11,4 +11,3 @@ | |||
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and | 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. | 13 | # limitations under the License. |
| 14 | - | ||
| @@ -59,7 +59,6 @@ obj_for_test = [{'a': 'abc', 'b': 1}, 2, 'c'] | |||
| 59 | 59 | ||
| 60 | 60 | ||
| 61 | def test_json(): | 61 | def test_json(): |
| 62 | - | ||
| 63 | def json_checker(dump_str): | 62 | def json_checker(dump_str): |
| 64 | assert dump_str in [ | 63 | assert dump_str in [ |
| 65 | '[{"a": "abc", "b": 1}, 2, "c"]', '[{"b": 1, "a": "abc"}, 2, "c"]' | 64 | '[{"a": "abc", "b": 1}, 2, "c"]', '[{"b": 1, "a": "abc"}, 2, "c"]' |
| @@ -69,7 +68,6 @@ def test_json(): | |||
| 69 | 68 | ||
| 70 | 69 | ||
| 71 | def test_yaml(): | 70 | def test_yaml(): |
| 72 | - | ||
| 73 | def yaml_checker(dump_str): | 71 | def yaml_checker(dump_str): |
| 74 | assert dump_str in [ | 72 | assert dump_str in [ |
| 75 | '- {a: abc, b: 1}\n- 2\n- c\n', '- {b: 1, a: abc}\n- 2\n- c\n', | 73 | '- {a: abc, b: 1}\n- 2\n- c\n', '- {b: 1, a: abc}\n- 2\n- c\n', |
| @@ -80,7 +78,6 @@ def test_yaml(): | |||
| 80 | 78 | ||
| 81 | 79 | ||
| 82 | def test_pickle(): | 80 | def test_pickle(): |
| 83 | - | ||
| 84 | def pickle_checker(dump_str): | 81 | def pickle_checker(dump_str): |
| 85 | import pickle | 82 | import pickle |
| 86 | assert pickle.loads(dump_str) == obj_for_test | 83 | assert pickle.loads(dump_str) == obj_for_test |
| @@ -99,7 +96,6 @@ def test_exception(): | |||
| 99 | 96 | ||
| 100 | 97 | ||
| 101 | def test_register_handler(): | 98 | def test_register_handler(): |
| 102 | - | ||
| 103 | 99 | ||
| 104 | class TxtHandler1(mmcv.BaseFileHandler): | 100 | class TxtHandler1(mmcv.BaseFileHandler): |
| 105 | 101 | ||
| @@ -74,8 +74,8 @@ class TestImage(object): | |||
| 74 | in_img = np.random.rand(10, 10, 3).astype(np.float32) | 74 | in_img = np.random.rand(10, 10, 3).astype(np.float32) |
| 75 | out_img = mmcv.bgr2gray(in_img) | 75 | out_img = mmcv.bgr2gray(in_img) |
| 76 | computed_gray = ( | 76 | computed_gray = ( |
| 77 | - in_img[:, :, 0] * 0.114 + in_img[:, :, 1] * 0.587 + | 77 | + in_img[:, :, 0] * 0.114 + in_img[:, :, 1] * 0.587 + |
| 78 | - in_img[:, :, 2] * 0.299) | 78 | + in_img[:, :, 2] * 0.299) |
| 79 | assert_array_almost_equal(out_img, computed_gray, decimal=4) | 79 | assert_array_almost_equal(out_img, computed_gray, decimal=4) |
| 80 | out_img_3d = mmcv.bgr2gray(in_img, True) | 80 | out_img_3d = mmcv.bgr2gray(in_img, True) |
| 81 | assert out_img_3d.shape == (10, 10, 1) | 81 | assert out_img_3d.shape == (10, 10, 1) |
| @@ -85,8 +85,8 @@ class TestImage(object): | |||
| 85 | in_img = np.random.rand(10, 10, 3).astype(np.float32) | 85 | in_img = np.random.rand(10, 10, 3).astype(np.float32) |
| 86 | out_img = mmcv.rgb2gray(in_img) | 86 | out_img = mmcv.rgb2gray(in_img) |
| 87 | computed_gray = ( | 87 | computed_gray = ( |
| 88 | - in_img[:, :, 0] * 0.299 + in_img[:, :, 1] * 0.587 + | 88 | + in_img[:, :, 0] * 0.299 + in_img[:, :, 1] * 0.587 + |
| 89 | - in_img[:, :, 2] * 0.114) | 89 | + in_img[:, :, 2] * 0.114) |
| 90 | assert_array_almost_equal(out_img, computed_gray, decimal=4) | 90 | assert_array_almost_equal(out_img, computed_gray, decimal=4) |
| 91 | out_img_3d = mmcv.rgb2gray(in_img, True) | 91 | out_img_3d = mmcv.rgb2gray(in_img, True) |
| 92 | assert out_img_3d.shape == (10, 10, 1) | 92 | assert out_img_3d.shape == (10, 10, 1) |
| @@ -324,7 +324,7 @@ class TestImage(object): | |||
| 324 | (15, 2, 3), dtype='uint8'), padded_img[:, 10:, :]) | 324 | (15, 2, 3), dtype='uint8'), padded_img[:, 10:, :]) |
| 325 | 325 | ||
| 326 | with pytest.raises(AssertionError): | 326 | with pytest.raises(AssertionError): |
| 327 | - mmcv.impad(img, (15, ), 0) | 327 | + mmcv.impad(img, (15,), 0) |
| 328 | with pytest.raises(AssertionError): | 328 | with pytest.raises(AssertionError): |
| 329 | mmcv.impad(img, (5, 5), 0) | 329 | mmcv.impad(img, (5, 5), 0) |
| 330 | with pytest.raises(AssertionError): | 330 | with pytest.raises(AssertionError): |
| @@ -32,7 +32,7 @@ def test_iter_cast(): | |||
| 32 | 32 | ||
| 33 | def test_is_seq_of(): | 33 | def test_is_seq_of(): |
| 34 | assert mmcv.is_seq_of([1.0, 2.0, 3.0], float) | 34 | assert mmcv.is_seq_of([1.0, 2.0, 3.0], float) |
| 35 | - assert mmcv.is_seq_of([(1, ), (2, ), (3, )], tuple) | 35 | + assert mmcv.is_seq_of([(1,), (2,), (3,)], tuple) |
| 36 | assert mmcv.is_seq_of((1.0, 2.0, 3.0), float) | 36 | assert mmcv.is_seq_of((1.0, 2.0, 3.0), float) |
| 37 | assert mmcv.is_list_of([1.0, 2.0, 3.0], float) | 37 | assert mmcv.is_list_of([1.0, 2.0, 3.0], float) |
| 38 | assert not mmcv.is_seq_of((1.0, 2.0, 3.0), float, seq_type=list) | 38 | assert not mmcv.is_seq_of((1.0, 2.0, 3.0), float, seq_type=list) |
| @@ -57,7 +57,6 @@ def test_concat_list(): | |||
| 57 | 57 | ||
| 58 | 58 | ||
| 59 | def test_requires_package(capsys): | 59 | def test_requires_package(capsys): |
| 60 | - | ||
| 61 | 60 | ||
| 62 | def func_a(): | 61 | def func_a(): |
| 63 | pass | 62 | pass |
| @@ -87,7 +86,6 @@ def test_requires_package(capsys): | |||
| 87 | 86 | ||
| 88 | 87 | ||
| 89 | def test_requires_executable(capsys): | 88 | def test_requires_executable(capsys): |
| 90 | - | ||
| 91 | 89 | ||
| 92 | def func_a(): | 90 | def func_a(): |
| 93 | pass | 91 | pass |
| @@ -158,7 +158,6 @@ def test_flow2rgb(): | |||
| 158 | 158 | ||
| 159 | 159 | ||
| 160 | def test_flow_warp(): | 160 | def test_flow_warp(): |
| 161 | - | ||
| 162 | def np_flow_warp(flow, img): | 161 | def np_flow_warp(flow, img): |
| 163 | output = np.zeros_like(img, dtype=img.dtype) | 162 | output = np.zeros_like(img, dtype=img.dtype) |
| 164 | height = flow.shape[0] | 163 | height = flow.shape[0] |
| @@ -172,7 +171,7 @@ def test_flow_warp(): | |||
| 172 | valid = (sx >= 0) & (sx < height - 1) & (sy >= 0) & (sy < width - 1) | 171 | valid = (sx >= 0) & (sx < height - 1) & (sy >= 0) & (sy < width - 1) |
| 173 | 172 | ||
| 174 | output[valid, :] = img[dx[valid].round().astype(int), | 173 | output[valid, :] = img[dx[valid].round().astype(int), |
| 175 | - dy[valid].round().astype(int), :] | 174 | + dy[valid].round().astype(int), :] |
| 176 | 175 | ||
| 177 | return output | 176 | return output |
| 178 | 177 | ||
| @@ -202,74 +201,74 @@ def test_make_color_wheel(): | |||
| 202 | color_wheel = mmcv.make_color_wheel([2, 2, 2, 2, 2, 2]) | 201 | color_wheel = mmcv.make_color_wheel([2, 2, 2, 2, 2, 2]) |
| 203 | # yapf: disable | 202 | # yapf: disable |
| 204 | assert_array_equal(default_color_wheel, np.array( | 203 | assert_array_equal(default_color_wheel, np.array( |
| 205 | - [[1. , 0. , 0. ], | 204 | + [[1., 0., 0.], |
| 206 | - [1. , 0.06666667, 0. ], | 205 | + [1., 0.06666667, 0.], |
| 207 | - [1. , 0.13333334, 0. ], | 206 | + [1., 0.13333334, 0.], |
| 208 | - [1. , 0.2 , 0. ], | 207 | + [1., 0.2, 0.], |
| 209 | - [1. , 0.26666668, 0. ], | 208 | + [1., 0.26666668, 0.], |
| 210 | - [1. , 0.33333334, 0. ], | 209 | + [1., 0.33333334, 0.], |
| 211 | - [1. , 0.4 , 0. ], | 210 | + [1., 0.4, 0.], |
| 212 | - [1. , 0.46666667, 0. ], | 211 | + [1., 0.46666667, 0.], |
| 213 | - [1. , 0.53333336, 0. ], | 212 | + [1., 0.53333336, 0.], |
| 214 | - [1. , 0.6 , 0. ], | 213 | + [1., 0.6, 0.], |
| 215 | - [1. , 0.6666667 , 0. ], | 214 | + [1., 0.6666667, 0.], |
| 216 | - [1. , 0.73333335, 0. ], | 215 | + [1., 0.73333335, 0.], |
| 217 | - [1. , 0.8 , 0. ], | 216 | + [1., 0.8, 0.], |
| 218 | - [1. , 0.8666667 , 0. ], | 217 | + [1., 0.8666667, 0.], |
| 219 | - [1. , 0.93333334, 0. ], | 218 | + [1., 0.93333334, 0.], |
| 220 | - [1. , 1. , 0. ], | 219 | + [1., 1., 0.], |
| 221 | - [0.8333333 , 1. , 0. ], | 220 | + [0.8333333, 1., 0.], |
| 222 | - [0.6666667 , 1. , 0. ], | 221 | + [0.6666667, 1., 0.], |
| 223 | - [0.5 , 1. , 0. ], | 222 | + [0.5, 1., 0.], |
| 224 | - [0.33333334, 1. , 0. ], | 223 | + [0.33333334, 1., 0.], |
| 225 | - [0.16666667, 1. , 0. ], | 224 | + [0.16666667, 1., 0.], |
| 226 | - [0. , 1. , 0. ], | 225 | + [0., 1., 0.], |
| 227 | - [0. , 1. , 0.25 ], | 226 | + [0., 1., 0.25], |
| 228 | - [0. , 1. , 0.5 ], | 227 | + [0., 1., 0.5], |
| 229 | - [0. , 1. , 0.75 ], | 228 | + [0., 1., 0.75], |
| 230 | - [0. , 1. , 1. ], | 229 | + [0., 1., 1.], |
| 231 | - [0. , 0.90909094, 1. ], | 230 | + [0., 0.90909094, 1.], |
| 232 | - [0. , 0.8181818 , 1. ], | 231 | + [0., 0.8181818, 1.], |
| 233 | - [0. , 0.72727275, 1. ], | 232 | + [0., 0.72727275, 1.], |
| 234 | - [0. , 0.6363636 , 1. ], | 233 | + [0., 0.6363636, 1.], |
| 235 | - [0. , 0.54545456, 1. ], | 234 | + [0., 0.54545456, 1.], |
| 236 | - [0. , 0.45454547, 1. ], | 235 | + [0., 0.45454547, 1.], |
| 237 | - [0. , 0.36363637, 1. ], | 236 | + [0., 0.36363637, 1.], |
| 238 | - [0. , 0.27272728, 1. ], | 237 | + [0., 0.27272728, 1.], |
| 239 | - [0. , 0.18181819, 1. ], | 238 | + [0., 0.18181819, 1.], |
| 240 | - [0. , 0.09090909, 1. ], | 239 | + [0., 0.09090909, 1.], |
| 241 | - [0. , 0. , 1. ], | 240 | + [0., 0., 1.], |
| 242 | - [0.07692308, 0. , 1. ], | 241 | + [0.07692308, 0., 1.], |
| 243 | - [0.15384616, 0. , 1. ], | 242 | + [0.15384616, 0., 1.], |
| 244 | - [0.23076923, 0. , 1. ], | 243 | + [0.23076923, 0., 1.], |
| 245 | - [0.30769232, 0. , 1. ], | 244 | + [0.30769232, 0., 1.], |
| 246 | - [0.3846154 , 0. , 1. ], | 245 | + [0.3846154, 0., 1.], |
| 247 | - [0.46153846, 0. , 1. ], | 246 | + [0.46153846, 0., 1.], |
| 248 | - [0.53846157, 0. , 1. ], | 247 | + [0.53846157, 0., 1.], |
| 249 | - [0.61538464, 0. , 1. ], | 248 | + [0.61538464, 0., 1.], |
| 250 | - [0.6923077 , 0. , 1. ], | 249 | + [0.6923077, 0., 1.], |
| 251 | - [0.7692308 , 0. , 1. ], | 250 | + [0.7692308, 0., 1.], |
| 252 | - [0.84615386, 0. , 1. ], | 251 | + [0.84615386, 0., 1.], |
| 253 | - [0.9230769 , 0. , 1. ], | 252 | + [0.9230769, 0., 1.], |
| 254 | - [1. , 0. , 1. ], | 253 | + [1., 0., 1.], |
| 255 | - [1. , 0. , 0.8333333 ], | 254 | + [1., 0., 0.8333333], |
| 256 | - [1. , 0. , 0.6666667 ], | 255 | + [1., 0., 0.6666667], |
| 257 | - [1. , 0. , 0.5 ], | 256 | + [1., 0., 0.5], |
| 258 | - [1. , 0. , 0.33333334], | 257 | + [1., 0., 0.33333334], |
| 259 | - [1. , 0. , 0.16666667]], dtype=np.float32)) | 258 | + [1., 0., 0.16666667]], dtype=np.float32)) |
| 260 | 259 | ||
| 261 | assert_array_equal( | 260 | assert_array_equal( |
| 262 | color_wheel, | 261 | color_wheel, |
| 263 | - np.array([[1., 0. , 0. ], | 262 | + np.array([[1., 0., 0.], |
| 264 | - [1. , 0.5, 0. ], | 263 | + [1., 0.5, 0.], |
| 265 | - [1. , 1. , 0. ], | 264 | + [1., 1., 0.], |
| 266 | - [0.5, 1. , 0. ], | 265 | + [0.5, 1., 0.], |
| 267 | - [0. , 1. , 0. ], | 266 | + [0., 1., 0.], |
| 268 | - [0. , 1. , 0.5], | 267 | + [0., 1., 0.5], |
| 269 | - [0. , 1. , 1. ], | 268 | + [0., 1., 1.], |
| 270 | - [0. , 0.5, 1. ], | 269 | + [0., 0.5, 1.], |
| 271 | - [0. , 0. , 1. ], | 270 | + [0., 0., 1.], |
| 272 | - [0.5, 0. , 1. ], | 271 | + [0.5, 0., 1.], |
| 273 | - [1. , 0. , 1. ], | 272 | + [1., 0., 1.], |
| 274 | - [1. , 0. , 0.5]], dtype=np.float32)) | 273 | + [1., 0., 0.5]], dtype=np.float32)) |
| 275 | # yapf: enable | 274 | # yapf: enable |
| @@ -29,6 +29,7 @@ from mmdet.models import build_detector | |||
| 29 | import cv2 | 29 | import cv2 |
| 30 | from scipy import ndimage | 30 | from scipy import ndimage |
| 31 | 31 | ||
| 32 | + | ||
| 32 | def init_detector(config, checkpoint=None, device='cuda:0'): | 33 | def init_detector(config, checkpoint=None, device='cuda:0'): |
| 33 | """Initialize a detector from config file. | 34 | """Initialize a detector from config file. |
| 34 | 35 | ||
| @@ -280,7 +281,7 @@ def show_result_ins(img, | |||
| 280 | for _ in range(num_mask) | 281 | for _ in range(num_mask) |
| 281 | ] | 282 | ] |
| 282 | for idx in range(num_mask): | 283 | for idx in range(num_mask): |
| 283 | - idx = -(idx+1) | 284 | + idx = -(idx + 1) |
| 284 | cur_mask = seg_label[idx, :, :] | 285 | cur_mask = seg_label[idx, :, :] |
| 285 | cur_mask = mmcv.imresize(cur_mask, (w, h)) | 286 | cur_mask = mmcv.imresize(cur_mask, (w, h)) |
| 286 | cur_mask = (cur_mask > 0.5).astype(np.uint8) | 287 | cur_mask = (cur_mask > 0.5).astype(np.uint8) |
| @@ -293,11 +294,11 @@ def show_result_ins(img, | |||
| 293 | cur_cate = cate_label[idx] | 294 | cur_cate = cate_label[idx] |
| 294 | cur_score = cate_score[idx] | 295 | cur_score = cate_score[idx] |
| 295 | label_text = class_names[cur_cate] | 296 | label_text = class_names[cur_cate] |
| 296 | - #label_text += '|{:.02f}'.format(cur_score) | 297 | + # label_text += '|{:.02f}'.format(cur_score) |
| 297 | center_y, center_x = ndimage.measurements.center_of_mass(cur_mask) | 298 | center_y, center_x = ndimage.measurements.center_of_mass(cur_mask) |
| 298 | vis_pos = (max(int(center_x) - 10, 0), int(center_y)) | 299 | vis_pos = (max(int(center_x) - 10, 0), int(center_y)) |
| 299 | cv2.putText(img_show, label_text, vis_pos, | 300 | cv2.putText(img_show, label_text, vis_pos, |
| 300 | - cv2.FONT_HERSHEY_COMPLEX, 0.3, (255, 255, 255)) # green | 301 | + cv2.FONT_HERSHEY_COMPLEX, 0.3, (255, 255, 255)) # green |
| 301 | if out_file is None: | 302 | if out_file is None: |
| 302 | return img_show | 303 | return img_show |
| 303 | else: | 304 | else: |
| @@ -30,6 +30,8 @@ from mmdet.datasets import DATASETS, build_dataloader | |||
| 30 | from mmdet.utils import get_root_logger | 30 | from mmdet.utils import get_root_logger |
| 31 | from apex import amp | 31 | from apex import amp |
| 32 | import apex | 32 | import apex |
| 33 | + | ||
| 34 | + | ||
| 33 | def set_random_seed(seed, deterministic=False): | 35 | def set_random_seed(seed, deterministic=False): |
| 34 | """Set random seed. | 36 | """Set random seed. |
| 35 | 37 | ||
| @@ -103,7 +105,8 @@ def train_detector(model, | |||
| 103 | cfg, | 105 | cfg, |
| 104 | distributed=False, | 106 | distributed=False, |
| 105 | validate=False, | 107 | validate=False, |
| 106 | - timestamp=None): | 108 | + timestamp=None, |
| 109 | + train_performance=False): | ||
| 107 | logger = get_root_logger(cfg.log_level) | 110 | logger = get_root_logger(cfg.log_level) |
| 108 | 111 | ||
| 109 | # start training | 112 | # start training |
| @@ -114,7 +117,8 @@ def train_detector(model, | |||
| 114 | cfg, | 117 | cfg, |
| 115 | validate=validate, | 118 | validate=validate, |
| 116 | logger=logger, | 119 | logger=logger, |
| 117 | - timestamp=timestamp) | 120 | + timestamp=timestamp, |
| 121 | + train_performance=train_performance) | ||
| 118 | else: | 122 | else: |
| 119 | _non_dist_train( | 123 | _non_dist_train( |
| 120 | model, | 124 | model, |
| @@ -122,7 +126,8 @@ def train_detector(model, | |||
| 122 | cfg, | 126 | cfg, |
| 123 | validate=validate, | 127 | validate=validate, |
| 124 | logger=logger, | 128 | logger=logger, |
| 125 | - timestamp=timestamp) | 129 | + timestamp=timestamp, |
| 130 | + train_performance=train_performance) | ||
| 126 | 131 | ||
| 127 | 132 | ||
| 128 | def build_optimizer(model, optimizer_cfg): | 133 | def build_optimizer(model, optimizer_cfg): |
| @@ -210,7 +215,8 @@ def _dist_train(model, | |||
| 210 | cfg, | 215 | cfg, |
| 211 | validate=False, | 216 | validate=False, |
| 212 | logger=None, | 217 | logger=None, |
| 213 | - timestamp=None): | 218 | + timestamp=None, |
| 219 | + train_performance=False): | ||
| 214 | # prepare data loaders | 220 | # prepare data loaders |
| 215 | dataset = dataset if isinstance(dataset, (list, tuple)) else [dataset] | 221 | dataset = dataset if isinstance(dataset, (list, tuple)) else [dataset] |
| 216 | data_loaders = [ | 222 | data_loaders = [ |
| @@ -237,7 +243,8 @@ def _dist_train(model, | |||
| 237 | # ) | 243 | # ) |
| 238 | # build runner | 244 | # build runner |
| 239 | runner = Runner( | 245 | runner = Runner( |
| 240 | - model, batch_processor, optimizer, cfg.work_dir, logger=logger, samples_per_gpu=cfg.data.imgs_per_gpu, num_of_gpus = cfg.gpus) | 246 | + model, batch_processor, optimizer, cfg.work_dir, logger=logger, samples_per_gpu=cfg.data.imgs_per_gpu, |
| 247 | + num_of_gpus=cfg.gpus) | ||
| 241 | # an ugly walkaround to make the .log and .log.json filenames the same | 248 | # an ugly walkaround to make the .log and .log.json filenames the same |
| 242 | runner.timestamp = timestamp | 249 | runner.timestamp = timestamp |
| 243 | 250 | ||
| @@ -284,6 +291,7 @@ def _dist_train(model, | |||
| 284 | runner.resume(cfg.resume_from) | 291 | runner.resume(cfg.resume_from) |
| 285 | elif cfg.load_from: | 292 | elif cfg.load_from: |
| 286 | runner.load_checkpoint(cfg.load_from) | 293 | runner.load_checkpoint(cfg.load_from) |
| 294 | + runner.train_performance = train_performance | ||
| 287 | runner.run(data_loaders, cfg.workflow, cfg.total_epochs) | 295 | runner.run(data_loaders, cfg.workflow, cfg.total_epochs) |
| 288 | 296 | ||
| 289 | 297 | ||
| @@ -292,7 +300,8 @@ def _non_dist_train(model, | |||
| 292 | cfg, | 300 | cfg, |
| 293 | validate=False, | 301 | validate=False, |
| 294 | logger=None, | 302 | logger=None, |
| 295 | - timestamp=None): | 303 | + timestamp=None, |
| 304 | + train_performance=False): | ||
| 296 | if validate: | 305 | if validate: |
| 297 | raise NotImplementedError('Built-in validation is not implemented ' | 306 | raise NotImplementedError('Built-in validation is not implemented ' |
| 298 | 'yet in not-distributed training. Use ' | 307 | 'yet in not-distributed training. Use ' |
| @@ -318,9 +327,10 @@ def _non_dist_train(model, | |||
| 318 | model = MMDataParallel(model.npu(), device_ids=range(cfg.gpus)) | 327 | model = MMDataParallel(model.npu(), device_ids=range(cfg.gpus)) |
| 319 | 328 | ||
| 320 | # build runner | 329 | # build runner |
| 321 | - #optimizer = build_optimizer(model, cfg.optimizer) | 330 | + # optimizer = build_optimizer(model, cfg.optimizer) |
| 322 | runner = Runner( | 331 | runner = Runner( |
| 323 | - model, batch_processor, optimizer, cfg.work_dir, logger=logger, samples_per_gpu=cfg.data.imgs_per_gpu, num_of_gpus = cfg.gpus) | 332 | + model, batch_processor, optimizer, cfg.work_dir, logger=logger, samples_per_gpu=cfg.data.imgs_per_gpu, |
| 333 | + num_of_gpus=cfg.gpus) | ||
| 324 | # an ugly walkaround to make the .log and .log.json filenames the same | 334 | # an ugly walkaround to make the .log and .log.json filenames the same |
| 325 | runner.timestamp = timestamp | 335 | runner.timestamp = timestamp |
| 326 | # fp16 setting | 336 | # fp16 setting |
| @@ -337,4 +347,5 @@ def _non_dist_train(model, | |||
| 337 | runner.resume(cfg.resume_from) | 347 | runner.resume(cfg.resume_from) |
| 338 | elif cfg.load_from: | 348 | elif cfg.load_from: |
| 339 | runner.load_checkpoint(cfg.load_from) | 349 | runner.load_checkpoint(cfg.load_from) |
| 340 | - runner.run(data_loaders, cfg.workflow, cfg.total_epochs) | 350 | + runner.train_performance = train_performance |
| 351 | + runner.run(data_loaders, cfg.workflow, cfg.total_epochs) | ||
| @@ -107,6 +107,6 @@ class AnchorGenerator(object): | |||
| 107 | valid_xx, valid_yy = self._meshgrid(valid_x, valid_y) | 107 | valid_xx, valid_yy = self._meshgrid(valid_x, valid_y) |
| 108 | valid = valid_xx & valid_yy | 108 | valid = valid_xx & valid_yy |
| 109 | valid = valid[:, | 109 | valid = valid[:, |
| 110 | - None].expand(valid.size(0), | 110 | + None].expand(valid.size(0), |
| 111 | - self.num_base_anchors).contiguous().view(-1) | 111 | + self.num_base_anchors).contiguous().view(-1) |
| 112 | return valid | 112 | return valid |
| @@ -62,19 +62,19 @@ def anchor_target(anchor_list, | |||
| 62 | gt_labels_list = [None for _ in range(num_imgs)] | 62 | gt_labels_list = [None for _ in range(num_imgs)] |
| 63 | (all_labels, all_label_weights, all_bbox_targets, all_bbox_weights, | 63 | (all_labels, all_label_weights, all_bbox_targets, all_bbox_weights, |
| 64 | pos_inds_list, neg_inds_list) = multi_apply( | 64 | pos_inds_list, neg_inds_list) = multi_apply( |
| 65 | - anchor_target_single, | 65 | + anchor_target_single, |
| 66 | - anchor_list, | 66 | + anchor_list, |
| 67 | - valid_flag_list, | 67 | + valid_flag_list, |
| 68 | - gt_bboxes_list, | 68 | + gt_bboxes_list, |
| 69 | - gt_bboxes_ignore_list, | 69 | + gt_bboxes_ignore_list, |
| 70 | - gt_labels_list, | 70 | + gt_labels_list, |
| 71 | - img_metas, | 71 | + img_metas, |
| 72 | - target_means=target_means, | 72 | + target_means=target_means, |
| 73 | - target_stds=target_stds, | 73 | + target_stds=target_stds, |
| 74 | - cfg=cfg, | 74 | + cfg=cfg, |
| 75 | - label_channels=label_channels, | 75 | + label_channels=label_channels, |
| 76 | - sampling=sampling, | 76 | + sampling=sampling, |
| 77 | - unmap_outputs=unmap_outputs) | 77 | + unmap_outputs=unmap_outputs) |
| 78 | # no valid anchors | 78 | # no valid anchors |
| 79 | if any([labels is None for labels in all_labels]): | 79 | if any([labels is None for labels in all_labels]): |
| 80 | return None | 80 | return None |
| @@ -121,7 +121,7 @@ def anchor_target_single(flat_anchors, | |||
| 121 | img_meta['img_shape'][:2], | 121 | img_meta['img_shape'][:2], |
| 122 | cfg.allowed_border) | 122 | cfg.allowed_border) |
| 123 | if not inside_flags.any(): | 123 | if not inside_flags.any(): |
| 124 | - return (None, ) * 6 | 124 | + return (None,) * 6 |
| 125 | # assign gt and sample anchors | 125 | # assign gt and sample anchors |
| 126 | anchors = flat_anchors[inside_flags, :] | 126 | anchors = flat_anchors[inside_flags, :] |
| 127 | 127 | ||
| @@ -180,10 +180,10 @@ def anchor_inside_flags(flat_anchors, | |||
| 180 | img_h, img_w = img_shape[:2] | 180 | img_h, img_w = img_shape[:2] |
| 181 | if allowed_border >= 0: | 181 | if allowed_border >= 0: |
| 182 | inside_flags = valid_flags & \ | 182 | inside_flags = valid_flags & \ |
| 183 | - (flat_anchors[:, 0] >= -allowed_border).type(torch.uint8) & \ | 183 | + (flat_anchors[:, 0] >= -allowed_border).type(torch.uint8) & \ |
| 184 | - (flat_anchors[:, 1] >= -allowed_border).type(torch.uint8) & \ | 184 | + (flat_anchors[:, 1] >= -allowed_border).type(torch.uint8) & \ |
| 185 | - (flat_anchors[:, 2] < img_w + allowed_border).type(torch.uint8) & \ | 185 | + (flat_anchors[:, 2] < img_w + allowed_border).type(torch.uint8) & \ |
| 186 | - (flat_anchors[:, 3] < img_h + allowed_border).type(torch.uint8) | 186 | + (flat_anchors[:, 3] < img_h + allowed_border).type(torch.uint8) |
| 187 | else: | 187 | else: |
| 188 | inside_flags = valid_flags | 188 | inside_flags = valid_flags |
| 189 | return inside_flags | 189 | return inside_flags |
| @@ -193,10 +193,10 @@ def unmap(data, count, inds, fill=0): | |||
| 193 | """ Unmap a subset of item (data) back to the original set of items (of | 193 | """ Unmap a subset of item (data) back to the original set of items (of |
| 194 | size count) """ | 194 | size count) """ |
| 195 | if data.dim() == 1: | 195 | if data.dim() == 1: |
| 196 | - ret = data.new_full((count, ), fill) | 196 | + ret = data.new_full((count,), fill) |
| 197 | ret[inds] = data | 197 | ret[inds] = data |
| 198 | else: | 198 | else: |
| 199 | - new_size = (count, ) + data.size()[1:] | 199 | + new_size = (count,) + data.size()[1:] |
| 200 | ret = data.new_full(new_size, fill) | 200 | ret = data.new_full(new_size, fill) |
| 201 | ret[inds, :] = data | 201 | ret[inds, :] = data |
| 202 | return ret | 202 | return ret |
| @@ -93,7 +93,7 @@ def ga_loc_target(gt_bboxes_list, | |||
| 93 | scale = torch.sqrt((gt_bboxes[:, 2] - gt_bboxes[:, 0] + 1) * | 93 | scale = torch.sqrt((gt_bboxes[:, 2] - gt_bboxes[:, 0] + 1) * |
| 94 | (gt_bboxes[:, 3] - gt_bboxes[:, 1] + 1)) | 94 | (gt_bboxes[:, 3] - gt_bboxes[:, 1] + 1)) |
| 95 | min_anchor_size = scale.new_full( | 95 | min_anchor_size = scale.new_full( |
| 96 | - (1, ), float(anchor_scale * anchor_strides[0])) | 96 | + (1,), float(anchor_scale * anchor_strides[0])) |
| 97 | # assign gt bboxes to different feature levels w.r.t. their scales | 97 | # assign gt bboxes to different feature levels w.r.t. their scales |
| 98 | target_lvls = torch.floor( | 98 | target_lvls = torch.floor( |
| 99 | torch.log2(scale) - torch.log2(min_anchor_size) + 0.5) | 99 | torch.log2(scale) - torch.log2(min_anchor_size) + 0.5) |
| @@ -109,11 +109,11 @@ def ga_loc_target(gt_bboxes_list, | |||
| 109 | ctr_x1, ctr_y1, ctr_x2, ctr_y2 = calc_region( | 109 | ctr_x1, ctr_y1, ctr_x2, ctr_y2 = calc_region( |
| 110 | gt_, r1, featmap_sizes[lvl]) | 110 | gt_, r1, featmap_sizes[lvl]) |
| 111 | all_loc_targets[lvl][img_id, 0, ctr_y1:ctr_y2 + 1, | 111 | all_loc_targets[lvl][img_id, 0, ctr_y1:ctr_y2 + 1, |
| 112 | - ctr_x1:ctr_x2 + 1] = 1 | 112 | + ctr_x1:ctr_x2 + 1] = 1 |
| 113 | all_loc_weights[lvl][img_id, 0, ignore_y1:ignore_y2 + 1, | 113 | all_loc_weights[lvl][img_id, 0, ignore_y1:ignore_y2 + 1, |
| 114 | - ignore_x1:ignore_x2 + 1] = 0 | 114 | + ignore_x1:ignore_x2 + 1] = 0 |
| 115 | all_loc_weights[lvl][img_id, 0, ctr_y1:ctr_y2 + 1, | 115 | all_loc_weights[lvl][img_id, 0, ctr_y1:ctr_y2 + 1, |
| 116 | - ctr_x1:ctr_x2 + 1] = 1 | 116 | + ctr_x1:ctr_x2 + 1] = 1 |
| 117 | # calculate ignore map on nearby low level feature | 117 | # calculate ignore map on nearby low level feature |
| 118 | if lvl > 0: | 118 | if lvl > 0: |
| 119 | d_lvl = lvl - 1 | 119 | d_lvl = lvl - 1 |
| @@ -122,7 +122,7 @@ def ga_loc_target(gt_bboxes_list, | |||
| 122 | ignore_x1, ignore_y1, ignore_x2, ignore_y2 = calc_region( | 122 | ignore_x1, ignore_y1, ignore_x2, ignore_y2 = calc_region( |
| 123 | gt_, r2, featmap_sizes[d_lvl]) | 123 | gt_, r2, featmap_sizes[d_lvl]) |
| 124 | all_ignore_map[d_lvl][img_id, 0, ignore_y1:ignore_y2 + 1, | 124 | all_ignore_map[d_lvl][img_id, 0, ignore_y1:ignore_y2 + 1, |
| 125 | - ignore_x1:ignore_x2 + 1] = 1 | 125 | + ignore_x1:ignore_x2 + 1] = 1 |
| 126 | # calculate ignore map on nearby high level feature | 126 | # calculate ignore map on nearby high level feature |
| 127 | if lvl < num_lvls - 1: | 127 | if lvl < num_lvls - 1: |
| 128 | u_lvl = lvl + 1 | 128 | u_lvl = lvl + 1 |
| @@ -131,7 +131,7 @@ def ga_loc_target(gt_bboxes_list, | |||
| 131 | ignore_x1, ignore_y1, ignore_x2, ignore_y2 = calc_region( | 131 | ignore_x1, ignore_y1, ignore_x2, ignore_y2 = calc_region( |
| 132 | gt_, r2, featmap_sizes[u_lvl]) | 132 | gt_, r2, featmap_sizes[u_lvl]) |
| 133 | all_ignore_map[u_lvl][img_id, 0, ignore_y1:ignore_y2 + 1, | 133 | all_ignore_map[u_lvl][img_id, 0, ignore_y1:ignore_y2 + 1, |
| 134 | - ignore_x1:ignore_x2 + 1] = 1 | 134 | + ignore_x1:ignore_x2 + 1] = 1 |
| 135 | for lvl_id in range(num_lvls): | 135 | for lvl_id in range(num_lvls): |
| 136 | # ignore negative regions w.r.t. ignore map | 136 | # ignore negative regions w.r.t. ignore map |
| 137 | all_loc_weights[lvl_id][(all_loc_weights[lvl_id] < 0) | 137 | all_loc_weights[lvl_id][(all_loc_weights[lvl_id] < 0) |
| @@ -191,17 +191,17 @@ def ga_shape_target(approx_list, | |||
| 191 | gt_bboxes_ignore_list = [None for _ in range(num_imgs)] | 191 | gt_bboxes_ignore_list = [None for _ in range(num_imgs)] |
| 192 | (all_bbox_anchors, all_bbox_gts, all_bbox_weights, pos_inds_list, | 192 | (all_bbox_anchors, all_bbox_gts, all_bbox_weights, pos_inds_list, |
| 193 | neg_inds_list) = multi_apply( | 193 | neg_inds_list) = multi_apply( |
| 194 | - ga_shape_target_single, | 194 | + ga_shape_target_single, |
| 195 | - approx_flat_list, | 195 | + approx_flat_list, |
| 196 | - inside_flag_flat_list, | 196 | + inside_flag_flat_list, |
| 197 | - square_flat_list, | 197 | + square_flat_list, |
| 198 | - gt_bboxes_list, | 198 | + gt_bboxes_list, |
| 199 | - gt_bboxes_ignore_list, | 199 | + gt_bboxes_ignore_list, |
| 200 | - img_metas, | 200 | + img_metas, |
| 201 | - approxs_per_octave=approxs_per_octave, | 201 | + approxs_per_octave=approxs_per_octave, |
| 202 | - cfg=cfg, | 202 | + cfg=cfg, |
| 203 | - sampling=sampling, | 203 | + sampling=sampling, |
| 204 | - unmap_outputs=unmap_outputs) | 204 | + unmap_outputs=unmap_outputs) |
| 205 | # no valid anchors | 205 | # no valid anchors |
| 206 | if any([bbox_anchors is None for bbox_anchors in all_bbox_anchors]): | 206 | if any([bbox_anchors is None for bbox_anchors in all_bbox_anchors]): |
| 207 | return None | 207 | return None |
| @@ -264,7 +264,7 @@ def ga_shape_target_single(flat_approxs, | |||
| 264 | tuple | 264 | tuple |
| 265 | """ | 265 | """ |
| 266 | if not inside_flags.any(): | 266 | if not inside_flags.any(): |
| 267 | - return (None, ) * 5 | 267 | + return (None,) * 5 |
| 268 | # assign gt and sample anchors | 268 | # assign gt and sample anchors |
| 269 | expand_inside_flags = inside_flags[:, None].expand( | 269 | expand_inside_flags = inside_flags[:, None].expand( |
| 270 | -1, approxs_per_octave).reshape(-1) | 270 | -1, approxs_per_octave).reshape(-1) |
| @@ -30,7 +30,7 @@ class PointGenerator(object): | |||
| 30 | shift_x = torch.arange(0., feat_w, device=device) * stride | 30 | shift_x = torch.arange(0., feat_w, device=device) * stride |
| 31 | shift_y = torch.arange(0., feat_h, device=device) * stride | 31 | shift_y = torch.arange(0., feat_h, device=device) * stride |
| 32 | shift_xx, shift_yy = self._meshgrid(shift_x, shift_y) | 32 | shift_xx, shift_yy = self._meshgrid(shift_x, shift_y) |
| 33 | - stride = shift_x.new_full((shift_xx.shape[0], ), stride) | 33 | + stride = shift_x.new_full((shift_xx.shape[0],), stride) |
| 34 | shifts = torch.stack([shift_xx, shift_yy, stride], dim=-1) | 34 | shifts = torch.stack([shift_xx, shift_yy, stride], dim=-1) |
| 35 | all_points = shifts.to(device) | 35 | all_points = shifts.to(device) |
| 36 | return all_points | 36 | return all_points |
| @@ -59,16 +59,16 @@ def point_target(proposals_list, | |||
| 59 | gt_labels_list = [None for _ in range(num_imgs)] | 59 | gt_labels_list = [None for _ in range(num_imgs)] |
| 60 | (all_labels, all_label_weights, all_bbox_gt, all_proposals, | 60 | (all_labels, all_label_weights, all_bbox_gt, all_proposals, |
| 61 | all_proposal_weights, pos_inds_list, neg_inds_list) = multi_apply( | 61 | all_proposal_weights, pos_inds_list, neg_inds_list) = multi_apply( |
| 62 | - point_target_single, | 62 | + point_target_single, |
| 63 | - proposals_list, | 63 | + proposals_list, |
| 64 | - valid_flag_list, | 64 | + valid_flag_list, |
| 65 | - gt_bboxes_list, | 65 | + gt_bboxes_list, |
| 66 | - gt_bboxes_ignore_list, | 66 | + gt_bboxes_ignore_list, |
| 67 | - gt_labels_list, | 67 | + gt_labels_list, |
| 68 | - cfg=cfg, | 68 | + cfg=cfg, |
| 69 | - label_channels=label_channels, | 69 | + label_channels=label_channels, |
| 70 | - sampling=sampling, | 70 | + sampling=sampling, |
| 71 | - unmap_outputs=unmap_outputs) | 71 | + unmap_outputs=unmap_outputs) |
| 72 | # no valid points | 72 | # no valid points |
| 73 | if any([labels is None for labels in all_labels]): | 73 | if any([labels is None for labels in all_labels]): |
| 74 | return None | 74 | return None |
| @@ -112,7 +112,7 @@ def point_target_single(flat_proposals, | |||
| 112 | unmap_outputs=True): | 112 | unmap_outputs=True): |
| 113 | inside_flags = valid_flags | 113 | inside_flags = valid_flags |
| 114 | if not inside_flags.any(): | 114 | if not inside_flags.any(): |
| 115 | - return (None, ) * 7 | 115 | + return (None,) * 7 |
| 116 | # assign gt and sample proposals | 116 | # assign gt and sample proposals |
| 117 | proposals = flat_proposals[inside_flags, :] | 117 | proposals = flat_proposals[inside_flags, :] |
| 118 | 118 | ||
| @@ -170,10 +170,10 @@ def unmap(data, count, inds, fill=0): | |||
| 170 | """ Unmap a subset of item (data) back to the original set of items (of | 170 | """ Unmap a subset of item (data) back to the original set of items (of |
| 171 | size count) """ | 171 | size count) """ |
| 172 | if data.dim() == 1: | 172 | if data.dim() == 1: |
| 173 | - ret = data.new_full((count, ), fill) | 173 | + ret = data.new_full((count,), fill) |
| 174 | ret[inds] = data | 174 | ret[inds] = data |
| 175 | else: | 175 | else: |
| 176 | - new_size = (count, ) + data.size()[1:] | 176 | + new_size = (count,) + data.size()[1:] |
| 177 | ret = data.new_full(new_size, fill) | 177 | ret = data.new_full(new_size, fill) |
| 178 | ret[inds, :] = data | 178 | ret[inds, :] = data |
| 179 | return ret | 179 | return ret |
| @@ -114,7 +114,7 @@ class ApproxMaxIoUAssigner(MaxIoUAssigner): | |||
| 114 | approxs.view(num_squares, approxs_per_octave, 4), 0, | 114 | approxs.view(num_squares, approxs_per_octave, 4), 0, |
| 115 | 1).contiguous().view(-1, 4) | 115 | 1).contiguous().view(-1, 4) |
| 116 | assign_on_cpu = True if (self.gpu_assign_thr > 0) and ( | 116 | assign_on_cpu = True if (self.gpu_assign_thr > 0) and ( |
| 117 | - num_gts > self.gpu_assign_thr) else False | 117 | + num_gts > self.gpu_assign_thr) else False |
| 118 | # compute overlap and assign gt on CPU when number of GT is large | 118 | # compute overlap and assign gt on CPU when number of GT is large |
| 119 | if assign_on_cpu: | 119 | if assign_on_cpu: |
| 120 | device = approxs.device | 120 | device = approxs.device |
| @@ -78,20 +78,20 @@ class ATSSAssigner(BaseAssigner): | |||
| 78 | overlaps = bbox_overlaps(bboxes, gt_bboxes) | 78 | overlaps = bbox_overlaps(bboxes, gt_bboxes) |
| 79 | 79 | ||
| 80 | # assign 0 by default | 80 | # assign 0 by default |
| 81 | - assigned_gt_inds = overlaps.new_full((num_bboxes, ), | 81 | + assigned_gt_inds = overlaps.new_full((num_bboxes,), |
| 82 | 0, | 82 | 0, |
| 83 | dtype=torch.long) | 83 | dtype=torch.long) |
| 84 | 84 | ||
| 85 | if num_gt == 0 or num_bboxes == 0: | 85 | if num_gt == 0 or num_bboxes == 0: |
| 86 | # No ground truth or boxes, return empty assignment | 86 | # No ground truth or boxes, return empty assignment |
| 87 | - max_overlaps = overlaps.new_zeros((num_bboxes, )) | 87 | + max_overlaps = overlaps.new_zeros((num_bboxes,)) |
| 88 | if num_gt == 0: | 88 | if num_gt == 0: |
| 89 | # No truth, assign everything to background | 89 | # No truth, assign everything to background |
| 90 | assigned_gt_inds[:] = 0 | 90 | assigned_gt_inds[:] = 0 |
| 91 | if gt_labels is None: | 91 | if gt_labels is None: |
| 92 | assigned_labels = None | 92 | assigned_labels = None |
| 93 | else: | 93 | else: |
| 94 | - assigned_labels = overlaps.new_zeros((num_bboxes, ), | 94 | + assigned_labels = overlaps.new_zeros((num_bboxes,), |
| 95 | dtype=torch.long) | 95 | dtype=torch.long) |
| 96 | return AssignResult( | 96 | return AssignResult( |
| 97 | num_gt, assigned_gt_inds, max_overlaps, labels=assigned_labels) | 97 | num_gt, assigned_gt_inds, max_overlaps, labels=assigned_labels) |
| @@ -162,7 +162,7 @@ class ATSSAssigner(BaseAssigner): | |||
| 162 | max_overlaps != -INF] = argmax_overlaps[max_overlaps != -INF] + 1 | 162 | max_overlaps != -INF] = argmax_overlaps[max_overlaps != -INF] + 1 |
| 163 | 163 | ||
| 164 | if gt_labels is not None: | 164 | if gt_labels is not None: |
| 165 | - assigned_labels = assigned_gt_inds.new_zeros((num_bboxes, )) | 165 | + assigned_labels = assigned_gt_inds.new_zeros((num_bboxes,)) |
| 166 | pos_inds = torch.nonzero(assigned_gt_inds > 0).squeeze() | 166 | pos_inds = torch.nonzero(assigned_gt_inds > 0).squeeze() |
| 167 | if pos_inds.numel() > 0: | 167 | if pos_inds.numel() > 0: |
| 168 | assigned_labels[pos_inds] = gt_labels[ | 168 | assigned_labels[pos_inds] = gt_labels[ |
| @@ -98,7 +98,7 @@ class MaxIoUAssigner(BaseAssigner): | |||
| 98 | >>> assert torch.all(assign_result.gt_inds == expected_gt_inds) | 98 | >>> assert torch.all(assign_result.gt_inds == expected_gt_inds) |
| 99 | """ | 99 | """ |
| 100 | assign_on_cpu = True if (self.gpu_assign_thr > 0) and ( | 100 | assign_on_cpu = True if (self.gpu_assign_thr > 0) and ( |
| 101 | - gt_bboxes.shape[0] > self.gpu_assign_thr) else False | 101 | + gt_bboxes.shape[0] > self.gpu_assign_thr) else False |
| 102 | # compute overlap and assign gt on CPU when number of GT is large | 102 | # compute overlap and assign gt on CPU when number of GT is large |
| 103 | if assign_on_cpu: | 103 | if assign_on_cpu: |
| 104 | device = bboxes.device | 104 | device = bboxes.device |
| @@ -146,20 +146,20 @@ class MaxIoUAssigner(BaseAssigner): | |||
| 146 | num_gts, num_bboxes = overlaps.size(0), overlaps.size(1) | 146 | num_gts, num_bboxes = overlaps.size(0), overlaps.size(1) |
| 147 | 147 | ||
| 148 | # 1. assign -1 by default | 148 | # 1. assign -1 by default |
| 149 | - assigned_gt_inds = overlaps.new_full((num_bboxes, ), | 149 | + assigned_gt_inds = overlaps.new_full((num_bboxes,), |
| 150 | -1, | 150 | -1, |
| 151 | dtype=torch.long) | 151 | dtype=torch.long) |
| 152 | 152 | ||
| 153 | if num_gts == 0 or num_bboxes == 0: | 153 | if num_gts == 0 or num_bboxes == 0: |
| 154 | # No ground truth or boxes, return empty assignment | 154 | # No ground truth or boxes, return empty assignment |
| 155 | - max_overlaps = overlaps.new_zeros((num_bboxes, )) | 155 | + max_overlaps = overlaps.new_zeros((num_bboxes,)) |
| 156 | if num_gts == 0: | 156 | if num_gts == 0: |
| 157 | # No truth, assign everything to background | 157 | # No truth, assign everything to background |
| 158 | assigned_gt_inds[:] = 0 | 158 | assigned_gt_inds[:] = 0 |
| 159 | if gt_labels is None: | 159 | if gt_labels is None: |
| 160 | assigned_labels = None | 160 | assigned_labels = None |
| 161 | else: | 161 | else: |
| 162 | - assigned_labels = overlaps.new_zeros((num_bboxes, ), | 162 | + assigned_labels = overlaps.new_zeros((num_bboxes,), |
| 163 | dtype=torch.long) | 163 | dtype=torch.long) |
| 164 | return AssignResult( | 164 | return AssignResult( |
| 165 | num_gts, | 165 | num_gts, |
| @@ -197,7 +197,7 @@ class MaxIoUAssigner(BaseAssigner): | |||
| 197 | assigned_gt_inds[gt_argmax_overlaps[i]] = i + 1 | 197 | assigned_gt_inds[gt_argmax_overlaps[i]] = i + 1 |
| 198 | 198 | ||
| 199 | if gt_labels is not None: | 199 | if gt_labels is not None: |
| 200 | - assigned_labels = assigned_gt_inds.new_zeros((num_bboxes, )) | 200 | + assigned_labels = assigned_gt_inds.new_zeros((num_bboxes,)) |
| 201 | pos_inds = torch.nonzero(assigned_gt_inds > 0).squeeze() | 201 | pos_inds = torch.nonzero(assigned_gt_inds > 0).squeeze() |
| 202 | if pos_inds.numel() > 0: | 202 | if pos_inds.numel() > 0: |
| 203 | assigned_labels[pos_inds] = gt_labels[ | 203 | assigned_labels[pos_inds] = gt_labels[ |
| @@ -65,13 +65,13 @@ class PointAssigner(BaseAssigner): | |||
| 65 | 65 | ||
| 66 | if num_gts == 0 or num_points == 0: | 66 | if num_gts == 0 or num_points == 0: |
| 67 | # If no truth assign everything to the background | 67 | # If no truth assign everything to the background |
| 68 | - assigned_gt_inds = points.new_full((num_points, ), | 68 | + assigned_gt_inds = points.new_full((num_points,), |
| 69 | 0, | 69 | 0, |
| 70 | dtype=torch.long) | 70 | dtype=torch.long) |
| 71 | if gt_labels is None: | 71 | if gt_labels is None: |
| 72 | assigned_labels = None | 72 | assigned_labels = None |
| 73 | else: | 73 | else: |
| 74 | - assigned_labels = points.new_zeros((num_points, ), | 74 | + assigned_labels = points.new_zeros((num_points,), |
| 75 | dtype=torch.long) | 75 | dtype=torch.long) |
| 76 | return AssignResult( | 76 | return AssignResult( |
| 77 | num_gts, assigned_gt_inds, None, labels=assigned_labels) | 77 | num_gts, assigned_gt_inds, None, labels=assigned_labels) |
| @@ -91,9 +91,9 @@ class PointAssigner(BaseAssigner): | |||
| 91 | gt_bboxes_lvl = torch.clamp(gt_bboxes_lvl, min=lvl_min, max=lvl_max) | 91 | gt_bboxes_lvl = torch.clamp(gt_bboxes_lvl, min=lvl_min, max=lvl_max) |
| 92 | 92 | ||
| 93 | # stores the assigned gt index of each point | 93 | # stores the assigned gt index of each point |
| 94 | - assigned_gt_inds = points.new_zeros((num_points, ), dtype=torch.long) | 94 | + assigned_gt_inds = points.new_zeros((num_points,), dtype=torch.long) |
| 95 | # stores the assigned gt dist (to this point) of each point | 95 | # stores the assigned gt dist (to this point) of each point |
| 96 | - assigned_gt_dist = points.new_full((num_points, ), float('inf')) | 96 | + assigned_gt_dist = points.new_full((num_points,), float('inf')) |
| 97 | points_range = torch.arange(points.shape[0]) | 97 | points_range = torch.arange(points.shape[0]) |
| 98 | 98 | ||
| 99 | for idx in range(num_gts): | 99 | for idx in range(num_gts): |
| @@ -132,7 +132,7 @@ class PointAssigner(BaseAssigner): | |||
| 132 | less_than_recorded_index] | 132 | less_than_recorded_index] |
| 133 | 133 | ||
| 134 | if gt_labels is not None: | 134 | if gt_labels is not None: |
| 135 | - assigned_labels = assigned_gt_inds.new_zeros((num_points, )) | 135 | + assigned_labels = assigned_gt_inds.new_zeros((num_points,)) |
| 136 | pos_inds = torch.nonzero(assigned_gt_inds > 0).squeeze() | 136 | pos_inds = torch.nonzero(assigned_gt_inds > 0).squeeze() |
| 137 | if pos_inds.numel() > 0: | 137 | if pos_inds.numel() > 0: |
| 138 | assigned_labels[pos_inds] = gt_labels[ | 138 | assigned_labels[pos_inds] = gt_labels[ |
| @@ -75,11 +75,11 @@ def bbox_overlaps(bboxes1, bboxes2, mode='iou', is_aligned=False): | |||
| 75 | wh = (rb - lt + 1).clamp(min=0) # [rows, 2] | 75 | wh = (rb - lt + 1).clamp(min=0) # [rows, 2] |
| 76 | overlap = wh[:, 0] * wh[:, 1] | 76 | overlap = wh[:, 0] * wh[:, 1] |
| 77 | area1 = (bboxes1[:, 2] - bboxes1[:, 0] + 1) * ( | 77 | area1 = (bboxes1[:, 2] - bboxes1[:, 0] + 1) * ( |
| 78 | - bboxes1[:, 3] - bboxes1[:, 1] + 1) | 78 | + bboxes1[:, 3] - bboxes1[:, 1] + 1) |
| 79 | 79 | ||
| 80 | if mode == 'iou': | 80 | if mode == 'iou': |
| 81 | area2 = (bboxes2[:, 2] - bboxes2[:, 0] + 1) * ( | 81 | area2 = (bboxes2[:, 2] - bboxes2[:, 0] + 1) * ( |
| 82 | - bboxes2[:, 3] - bboxes2[:, 1] + 1) | 82 | + bboxes2[:, 3] - bboxes2[:, 1] + 1) |
| 83 | ious = overlap / (area1 + area2 - overlap) | 83 | ious = overlap / (area1 + area2 - overlap) |
| 84 | else: | 84 | else: |
| 85 | ious = overlap / area1 | 85 | ious = overlap / area1 |
| @@ -90,11 +90,11 @@ def bbox_overlaps(bboxes1, bboxes2, mode='iou', is_aligned=False): | |||
| 90 | wh = (rb - lt + 1).clamp(min=0) # [rows, cols, 2] | 90 | wh = (rb - lt + 1).clamp(min=0) # [rows, cols, 2] |
| 91 | overlap = wh[:, :, 0] * wh[:, :, 1] | 91 | overlap = wh[:, :, 0] * wh[:, :, 1] |
| 92 | area1 = (bboxes1[:, 2] - bboxes1[:, 0] + 1) * ( | 92 | area1 = (bboxes1[:, 2] - bboxes1[:, 0] + 1) * ( |
| 93 | - bboxes1[:, 3] - bboxes1[:, 1] + 1) | 93 | + bboxes1[:, 3] - bboxes1[:, 1] + 1) |
| 94 | 94 | ||
| 95 | if mode == 'iou': | 95 | if mode == 'iou': |
| 96 | area2 = (bboxes2[:, 2] - bboxes2[:, 0] + 1) * ( | 96 | area2 = (bboxes2[:, 2] - bboxes2[:, 0] + 1) * ( |
| 97 | - bboxes2[:, 3] - bboxes2[:, 1] + 1) | 97 | + bboxes2[:, 3] - bboxes2[:, 1] + 1) |
| 98 | ious = overlap / (area1[:, None] + area2 - overlap) | 98 | ious = overlap / (area1[:, None] + area2 - overlap) |
| 99 | else: | 99 | else: |
| 100 | ious = overlap / (area1[:, None]) | 100 | ious = overlap / (area1[:, None]) |
| @@ -80,7 +80,7 @@ class BaseSampler(metaclass=ABCMeta): | |||
| 80 | 80 | ||
| 81 | bboxes = bboxes[:, :4] | 81 | bboxes = bboxes[:, :4] |
| 82 | 82 | ||
| 83 | - gt_flags = bboxes.new_zeros((bboxes.shape[0], ), dtype=torch.uint8) | 83 | + gt_flags = bboxes.new_zeros((bboxes.shape[0],), dtype=torch.uint8) |
| 84 | if self.add_gt_as_proposals and len(gt_bboxes) > 0: | 84 | if self.add_gt_as_proposals and len(gt_bboxes) > 0: |
| 85 | if gt_labels is None: | 85 | if gt_labels is None: |
| 86 | raise ValueError( | 86 | raise ValueError( |
| @@ -43,9 +43,9 @@ def bbox_overlaps(bboxes1, bboxes2, mode='iou'): | |||
| 43 | ious = np.zeros((cols, rows), dtype=np.float32) | 43 | ious = np.zeros((cols, rows), dtype=np.float32) |
| 44 | exchange = True | 44 | exchange = True |
| 45 | area1 = (bboxes1[:, 2] - bboxes1[:, 0] + 1) * ( | 45 | area1 = (bboxes1[:, 2] - bboxes1[:, 0] + 1) * ( |
| 46 | - bboxes1[:, 3] - bboxes1[:, 1] + 1) | 46 | + bboxes1[:, 3] - bboxes1[:, 1] + 1) |
| 47 | area2 = (bboxes2[:, 2] - bboxes2[:, 0] + 1) * ( | 47 | area2 = (bboxes2[:, 2] - bboxes2[:, 0] + 1) * ( |
| 48 | - bboxes2[:, 3] - bboxes2[:, 1] + 1) | 48 | + bboxes2[:, 3] - bboxes2[:, 1] + 1) |
| 49 | for i in range(bboxes1.shape[0]): | 49 | for i in range(bboxes1.shape[0]): |
| 50 | x_start = np.maximum(bboxes1[i, 0], bboxes2[:, 0]) | 50 | x_start = np.maximum(bboxes1[i, 0], bboxes2[:, 0]) |
| 51 | y_start = np.maximum(bboxes1[i, 1], bboxes2[:, 1]) | 51 | y_start = np.maximum(bboxes1[i, 1], bboxes2[:, 1]) |
| @@ -105,7 +105,7 @@ def fast_eval_recall(results, | |||
| 105 | elif not isinstance(results, list): | 105 | elif not isinstance(results, list): |
| 106 | raise TypeError( | 106 | raise TypeError( |
| 107 | 'results must be a list of numpy arrays or a filename, not {}'. | 107 | 'results must be a list of numpy arrays or a filename, not {}'. |
| 108 | - format(type(results))) | 108 | + format(type(results))) |
| 109 | 109 | ||
| 110 | gt_bboxes = [] | 110 | gt_bboxes = [] |
| 111 | img_ids = coco.getImgIds() | 111 | img_ids = coco.getImgIds() |
| @@ -113,7 +113,7 @@ def tpfp_imagenet(det_bboxes, | |||
| 113 | fp[...] = 1 | 113 | fp[...] = 1 |
| 114 | else: | 114 | else: |
| 115 | det_areas = (det_bboxes[:, 2] - det_bboxes[:, 0] + 1) * ( | 115 | det_areas = (det_bboxes[:, 2] - det_bboxes[:, 0] + 1) * ( |
| 116 | - det_bboxes[:, 3] - det_bboxes[:, 1] + 1) | 116 | + det_bboxes[:, 3] - det_bboxes[:, 1] + 1) |
| 117 | for i, (min_area, max_area) in enumerate(area_ranges): | 117 | for i, (min_area, max_area) in enumerate(area_ranges): |
| 118 | fp[i, (det_areas >= min_area) & (det_areas < max_area)] = 1 | 118 | fp[i, (det_areas >= min_area) & (det_areas < max_area)] = 1 |
| 119 | return tp, fp | 119 | return tp, fp |
| @@ -209,7 +209,7 @@ def tpfp_default(det_bboxes, | |||
| 209 | fp[...] = 1 | 209 | fp[...] = 1 |
| 210 | else: | 210 | else: |
| 211 | det_areas = (det_bboxes[:, 2] - det_bboxes[:, 0] + 1) * ( | 211 | det_areas = (det_bboxes[:, 2] - det_bboxes[:, 0] + 1) * ( |
| 212 | - det_bboxes[:, 3] - det_bboxes[:, 1] + 1) | 212 | + det_bboxes[:, 3] - det_bboxes[:, 1] + 1) |
| 213 | for i, (min_area, max_area) in enumerate(area_ranges): | 213 | for i, (min_area, max_area) in enumerate(area_ranges): |
| 214 | fp[i, (det_areas >= min_area) & (det_areas < max_area)] = 1 | 214 | fp[i, (det_areas >= min_area) & (det_areas < max_area)] = 1 |
| 215 | return tp, fp | 215 | return tp, fp |
| @@ -228,7 +228,7 @@ def tpfp_default(det_bboxes, | |||
| 228 | gt_area_ignore = np.zeros_like(gt_ignore_inds, dtype=bool) | 228 | gt_area_ignore = np.zeros_like(gt_ignore_inds, dtype=bool) |
| 229 | else: | 229 | else: |
| 230 | gt_areas = (gt_bboxes[:, 2] - gt_bboxes[:, 0] + 1) * ( | 230 | gt_areas = (gt_bboxes[:, 2] - gt_bboxes[:, 0] + 1) * ( |
| 231 | - gt_bboxes[:, 3] - gt_bboxes[:, 1] + 1) | 231 | + gt_bboxes[:, 3] - gt_bboxes[:, 1] + 1) |
| 232 | gt_area_ignore = (gt_areas < min_area) | (gt_areas >= max_area) | 232 | gt_area_ignore = (gt_areas < min_area) | (gt_areas >= max_area) |
| 233 | for i in sort_inds: | 233 | for i in sort_inds: |
| 234 | if ious_max[i] >= iou_thr: | 234 | if ious_max[i] >= iou_thr: |
| @@ -318,7 +318,7 @@ def eval_map(det_results, | |||
| 318 | num_imgs = len(det_results) | 318 | num_imgs = len(det_results) |
| 319 | num_scales = len(scale_ranges) if scale_ranges is not None else 1 | 319 | num_scales = len(scale_ranges) if scale_ranges is not None else 1 |
| 320 | num_classes = len(det_results[0]) # positive class num | 320 | num_classes = len(det_results[0]) # positive class num |
| 321 | - area_ranges = ([(rg[0]**2, rg[1]**2) for rg in scale_ranges] | 321 | + area_ranges = ([(rg[0] ** 2, rg[1] ** 2) for rg in scale_ranges] |
| 322 | if scale_ranges is not None else None) | 322 | if scale_ranges is not None else None) |
| 323 | 323 | ||
| 324 | pool = Pool(nproc) | 324 | pool = Pool(nproc) |
| @@ -347,7 +347,7 @@ def eval_map(det_results, | |||
| 347 | num_gts[0] += bbox.shape[0] | 347 | num_gts[0] += bbox.shape[0] |
| 348 | else: | 348 | else: |
| 349 | gt_areas = (bbox[:, 2] - bbox[:, 0] + 1) * ( | 349 | gt_areas = (bbox[:, 2] - bbox[:, 0] + 1) * ( |
| 350 | - bbox[:, 3] - bbox[:, 1] + 1) | 350 | + bbox[:, 3] - bbox[:, 1] + 1) |
| 351 | for k, (min_area, max_area) in enumerate(area_ranges): | 351 | for k, (min_area, max_area) in enumerate(area_ranges): |
| 352 | num_gts[k] += np.sum((gt_areas >= min_area) | 352 | num_gts[k] += np.sum((gt_areas >= min_area) |
| 353 | & (gt_areas < max_area)) | 353 | & (gt_areas < max_area)) |
| @@ -19,7 +19,6 @@ from .bbox_overlaps import bbox_overlaps | |||
| 19 | 19 | ||
| 20 | 20 | ||
| 21 | def _recalls(all_ious, proposal_nums, thrs): | 21 | def _recalls(all_ious, proposal_nums, thrs): |
| 22 | - | ||
| 23 | img_num = all_ious.shape[0] | 22 | img_num = all_ious.shape[0] |
| 24 | total_gt_num = sum([ious.shape[0] for ious in all_ious]) | 23 | total_gt_num = sum([ious.shape[0] for ious in all_ious]) |
| 25 | 24 | ||
| @@ -51,5 +51,5 @@ def mask_target_single(pos_proposals, pos_assigned_gt_inds, gt_masks, cfg): | |||
| 51 | mask_targets = torch.from_numpy(np.stack(mask_targets)).float().to( | 51 | mask_targets = torch.from_numpy(np.stack(mask_targets)).float().to( |
| 52 | pos_proposals.device) | 52 | pos_proposals.device) |
| 53 | else: | 53 | else: |
| 54 | - mask_targets = pos_proposals.new_zeros((0, ) + mask_size) | 54 | + mask_targets = pos_proposals.new_zeros((0,) + mask_size) |
| 55 | return mask_targets | 55 | return mask_targets |
| @@ -14,6 +14,7 @@ | |||
| 14 | 14 | ||
| 15 | import torch | 15 | import torch |
| 16 | 16 | ||
| 17 | + | ||
| 17 | # from mmdet.ops.nms import nms_wrapper | 18 | # from mmdet.ops.nms import nms_wrapper |
| 18 | 19 | ||
| 19 | 20 | ||
| @@ -60,7 +61,7 @@ def multiclass_nms(multi_bboxes, | |||
| 60 | _scores *= score_factors[cls_inds] | 61 | _scores *= score_factors[cls_inds] |
| 61 | cls_dets = torch.cat([_bboxes, _scores[:, None]], dim=1) | 62 | cls_dets = torch.cat([_bboxes, _scores[:, None]], dim=1) |
| 62 | cls_dets, _ = nms_op(cls_dets, **nms_cfg_) | 63 | cls_dets, _ = nms_op(cls_dets, **nms_cfg_) |
| 63 | - cls_labels = multi_bboxes.new_full((cls_dets.shape[0], ), | 64 | + cls_labels = multi_bboxes.new_full((cls_dets.shape[0],), |
| 64 | i - 1, | 65 | i - 1, |
| 65 | dtype=torch.long) | 66 | dtype=torch.long) |
| 66 | bboxes.append(cls_dets) | 67 | bboxes.append(cls_dets) |
| @@ -75,6 +76,6 @@ def multiclass_nms(multi_bboxes, | |||
| 75 | labels = labels[inds] | 76 | labels = labels[inds] |
| 76 | else: | 77 | else: |
| 77 | bboxes = multi_bboxes.new_zeros((0, 5)) | 78 | bboxes = multi_bboxes.new_zeros((0, 5)) |
| 78 | - labels = multi_bboxes.new_zeros((0, ), dtype=torch.long) | 79 | + labels = multi_bboxes.new_zeros((0,), dtype=torch.long) |
| 79 | 80 | ||
| 80 | return bboxes, labels | 81 | return bboxes, labels |
| @@ -58,7 +58,7 @@ def matrix_nms(seg_masks, cate_labels, cate_scores, kernel='gaussian', sigma=2.0 | |||
| 58 | compensate_matrix = torch.exp(-1 * sigma * (compensate_iou ** 2)) | 58 | compensate_matrix = torch.exp(-1 * sigma * (compensate_iou ** 2)) |
| 59 | decay_coefficient, _ = (decay_matrix / compensate_matrix).min(0) | 59 | decay_coefficient, _ = (decay_matrix / compensate_matrix).min(0) |
| 60 | elif kernel == 'linear': | 60 | elif kernel == 'linear': |
| 61 | - decay_matrix = (1-decay_iou)/(1-compensate_iou) | 61 | + decay_matrix = (1 - decay_iou) / (1 - compensate_iou) |
| 62 | decay_coefficient, _ = decay_matrix.min(0) | 62 | decay_coefficient, _ = decay_matrix.min(0) |
| 63 | else: | 63 | else: |
| 64 | raise NotImplementedError | 64 | raise NotImplementedError |
| @@ -111,7 +111,7 @@ def multiclass_nms(multi_bboxes, | |||
| 111 | _scores *= score_factors[cls_inds] | 111 | _scores *= score_factors[cls_inds] |
| 112 | cls_dets = torch.cat([_bboxes, _scores[:, None]], dim=1) | 112 | cls_dets = torch.cat([_bboxes, _scores[:, None]], dim=1) |
| 113 | cls_dets, _ = nms_op(cls_dets, **nms_cfg_) | 113 | cls_dets, _ = nms_op(cls_dets, **nms_cfg_) |
| 114 | - cls_labels = multi_bboxes.new_full((cls_dets.shape[0], ), | 114 | + cls_labels = multi_bboxes.new_full((cls_dets.shape[0],), |
| 115 | i - 1, | 115 | i - 1, |
| 116 | dtype=torch.long) | 116 | dtype=torch.long) |
| 117 | bboxes.append(cls_dets) | 117 | bboxes.append(cls_dets) |
| @@ -126,6 +126,6 @@ def multiclass_nms(multi_bboxes, | |||
| 126 | labels = labels[inds] | 126 | labels = labels[inds] |
| 127 | else: | 127 | else: |
| 128 | bboxes = multi_bboxes.new_zeros((0, 5)) | 128 | bboxes = multi_bboxes.new_zeros((0, 5)) |
| 129 | - labels = multi_bboxes.new_zeros((0, ), dtype=torch.long) | 129 | + labels = multi_bboxes.new_zeros((0,), dtype=torch.long) |
| 130 | 130 | ||
| 131 | return bboxes, labels | 131 | return bboxes, labels |
| @@ -42,10 +42,10 @@ def unmap(data, count, inds, fill=0): | |||
| 42 | """ Unmap a subset of item (data) back to the original set of items (of | 42 | """ Unmap a subset of item (data) back to the original set of items (of |
| 43 | size count) """ | 43 | size count) """ |
| 44 | if data.dim() == 1: | 44 | if data.dim() == 1: |
| 45 | - ret = data.new_full((count, ), fill) | 45 | + ret = data.new_full((count,), fill) |
| 46 | ret[inds] = data | 46 | ret[inds] = data |
| 47 | else: | 47 | else: |
| 48 | - new_size = (count, ) + data.size()[1:] | 48 | + new_size = (count,) + data.size()[1:] |
| 49 | ret = data.new_full(new_size, fill) | 49 | ret = data.new_full(new_size, fill) |
| 50 | ret[inds, :] = data | 50 | ret[inds, :] = data |
| 51 | return ret | 51 | return ret |
| @@ -18,6 +18,5 @@ from .registry import DATASETS | |||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | class CityscapesDataset(CocoDataset): | 20 | class CityscapesDataset(CocoDataset): |
| 21 | - | ||
| 22 | CLASSES = ('person', 'rider', 'car', 'truck', 'bus', 'train', 'motorcycle', | 21 | CLASSES = ('person', 'rider', 'car', 'truck', 'bus', 'train', 'motorcycle', |
| 23 | 'bicycle') | 22 | 'bicycle') |
| @@ -21,7 +21,6 @@ from .registry import DATASETS | |||
| 21 | 21 | ||
| 22 | 22 | ||
| 23 | class CocoDataset(CustomDataset): | 23 | class CocoDataset(CustomDataset): |
| 24 | - | ||
| 25 | CLASSES = ('person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', | 24 | CLASSES = ('person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', |
| 26 | 'train', 'truck', 'boat', 'traffic_light', 'fire_hydrant', | 25 | 'train', 'truck', 'boat', 'traffic_light', 'fire_hydrant', |
| 27 | 'stop_sign', 'parking_meter', 'bench', 'bird', 'cat', 'dog', | 26 | 'stop_sign', 'parking_meter', 'bench', 'bird', 'cat', 'dog', |
| @@ -24,6 +24,7 @@ from .sampler import DistributedGroupSampler, DistributedSampler, GroupSampler | |||
| 24 | if platform.system() != 'Windows': | 24 | if platform.system() != 'Windows': |
| 25 | # https://github.com/pytorch/pytorch/issues/973 | 25 | # https://github.com/pytorch/pytorch/issues/973 |
| 26 | import resource | 26 | import resource |
| 27 | + | ||
| 27 | rlimit = resource.getrlimit(resource.RLIMIT_NOFILE) | 28 | rlimit = resource.getrlimit(resource.RLIMIT_NOFILE) |
| 28 | resource.setrlimit(resource.RLIMIT_NOFILE, (4096, rlimit[1])) | 29 | resource.setrlimit(resource.RLIMIT_NOFILE, (4096, rlimit[1])) |
| 29 | 30 | ||
| @@ -317,7 +317,7 @@ class Pad(object): | |||
| 317 | if padded_masks: | 317 | if padded_masks: |
| 318 | results[key] = np.stack(padded_masks, axis=0) | 318 | results[key] = np.stack(padded_masks, axis=0) |
| 319 | else: | 319 | else: |
| 320 | - results[key] = np.empty((0, ) + pad_shape, dtype=np.uint8) | 320 | + results[key] = np.empty((0,) + pad_shape, dtype=np.uint8) |
| 321 | 321 | ||
| 322 | def _pad_seg(self, results): | 322 | def _pad_seg(self, results): |
| 323 | for key in results.get('seg_fields', []): | 323 | for key in results.get('seg_fields', []): |
| @@ -409,7 +409,7 @@ class RandomCrop(object): | |||
| 409 | if 'gt_bboxes' in results: | 409 | if 'gt_bboxes' in results: |
| 410 | gt_bboxes = results['gt_bboxes'] | 410 | gt_bboxes = results['gt_bboxes'] |
| 411 | valid_inds = (gt_bboxes[:, 2] > gt_bboxes[:, 0]) & ( | 411 | valid_inds = (gt_bboxes[:, 2] > gt_bboxes[:, 0]) & ( |
| 412 | - gt_bboxes[:, 3] > gt_bboxes[:, 1]) | 412 | + gt_bboxes[:, 3] > gt_bboxes[:, 1]) |
| 413 | # if no gt bbox remains after cropping, just skip this image | 413 | # if no gt bbox remains after cropping, just skip this image |
| 414 | if not np.any(valid_inds): | 414 | if not np.any(valid_inds): |
| 415 | return None | 415 | return None |
| @@ -422,7 +422,7 @@ class RandomCrop(object): | |||
| 422 | valid_gt_masks = [] | 422 | valid_gt_masks = [] |
| 423 | for i in np.where(valid_inds)[0]: | 423 | for i in np.where(valid_inds)[0]: |
| 424 | gt_mask = results['gt_masks'][i][crop_y1:crop_y2, | 424 | gt_mask = results['gt_masks'][i][crop_y1:crop_y2, |
| 425 | - crop_x1:crop_x2] | 425 | + crop_x1:crop_x2] |
| 426 | valid_gt_masks.append(gt_mask) | 426 | valid_gt_masks.append(gt_mask) |
| 427 | results['gt_masks'] = np.stack(valid_gt_masks) | 427 | results['gt_masks'] = np.stack(valid_gt_masks) |
| 428 | 428 | ||
| @@ -540,8 +540,8 @@ class PhotoMetricDistortion(object): | |||
| 540 | repr_str = self.__class__.__name__ | 540 | repr_str = self.__class__.__name__ |
| 541 | repr_str += ('(brightness_delta={}, contrast_range={}, ' | 541 | repr_str += ('(brightness_delta={}, contrast_range={}, ' |
| 542 | 'saturation_range={}, hue_delta={})').format( | 542 | 'saturation_range={}, hue_delta={})').format( |
| 543 | - self.brightness_delta, self.contrast_range, | 543 | + self.brightness_delta, self.contrast_range, |
| 544 | - self.saturation_range, self.hue_delta) | 544 | + self.saturation_range, self.hue_delta) |
| 545 | return repr_str | 545 | return repr_str |
| 546 | 546 | ||
| 547 | 547 | ||
| @@ -616,8 +616,8 @@ class Expand(object): | |||
| 616 | repr_str = self.__class__.__name__ | 616 | repr_str = self.__class__.__name__ |
| 617 | repr_str += '(mean={}, to_rgb={}, ratio_range={}, ' \ | 617 | repr_str += '(mean={}, to_rgb={}, ratio_range={}, ' \ |
| 618 | 'seg_ignore_label={})'.format( | 618 | 'seg_ignore_label={})'.format( |
| 619 | - self.mean, self.to_rgb, self.ratio_range, | 619 | + self.mean, self.to_rgb, self.ratio_range, |
| 620 | - self.seg_ignore_label) | 620 | + self.seg_ignore_label) |
| 621 | return repr_str | 621 | return repr_str |
| 622 | 622 | ||
| 623 | 623 | ||
| @@ -700,7 +700,7 @@ class MinIoURandomCrop(object): | |||
| 700 | # not tested | 700 | # not tested |
| 701 | if 'gt_semantic_seg' in results: | 701 | if 'gt_semantic_seg' in results: |
| 702 | results['gt_semantic_seg'] = results['gt_semantic_seg'][ | 702 | results['gt_semantic_seg'] = results['gt_semantic_seg'][ |
| 703 | - patch[1]:patch[3], patch[0]:patch[2]] | 703 | + patch[1]:patch[3], patch[0]:patch[2]] |
| 704 | return results | 704 | return results |
| 705 | 705 | ||
| 706 | def __repr__(self): | 706 | def __repr__(self): |
| @@ -18,7 +18,6 @@ from .xml_style import XMLDataset | |||
| 18 | 18 | ||
| 19 | 19 | ||
| 20 | class VOCDataset(XMLDataset): | 20 | class VOCDataset(XMLDataset): |
| 21 | - | ||
| 22 | CLASSES = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', | 21 | CLASSES = ('aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', |
| 23 | 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', | 22 | 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', |
| 24 | 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', | 23 | 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', |
| @@ -28,7 +28,7 @@ class WIDERFaceDataset(XMLDataset): | |||
| 28 | Conversion scripts can be found in | 28 | Conversion scripts can be found in |
| 29 | https://github.com/sovrasov/wider-face-pascal-voc-annotations | 29 | https://github.com/sovrasov/wider-face-pascal-voc-annotations |
| 30 | """ | 30 | """ |
| 31 | - CLASSES = ('face', ) | 31 | + CLASSES = ('face',) |
| 32 | 32 | ||
| 33 | def __init__(self, **kwargs): | 33 | def __init__(self, **kwargs): |
| 34 | super(WIDERFaceDataset, self).__init__(**kwargs) | 34 | super(WIDERFaceDataset, self).__init__(**kwargs) |
| @@ -82,13 +82,13 @@ class XMLDataset(CustomDataset): | |||
| 82 | labels.append(label) | 82 | labels.append(label) |
| 83 | if not bboxes: | 83 | if not bboxes: |
| 84 | bboxes = np.zeros((0, 4)) | 84 | bboxes = np.zeros((0, 4)) |
| 85 | - labels = np.zeros((0, )) | 85 | + labels = np.zeros((0,)) |
| 86 | else: | 86 | else: |
| 87 | bboxes = np.array(bboxes, ndmin=2) - 1 | 87 | bboxes = np.array(bboxes, ndmin=2) - 1 |
| 88 | labels = np.array(labels) | 88 | labels = np.array(labels) |
| 89 | if not bboxes_ignore: | 89 | if not bboxes_ignore: |
| 90 | bboxes_ignore = np.zeros((0, 4)) | 90 | bboxes_ignore = np.zeros((0, 4)) |
| 91 | - labels_ignore = np.zeros((0, )) | 91 | + labels_ignore = np.zeros((0,)) |
| 92 | else: | 92 | else: |
| 93 | bboxes_ignore = np.array(bboxes_ignore, ndmin=2) - 1 | 93 | bboxes_ignore = np.array(bboxes_ignore, ndmin=2) - 1 |
| 94 | labels_ignore = np.array(labels_ignore) | 94 | labels_ignore = np.array(labels_ignore) |
| @@ -23,6 +23,7 @@ from .mask_heads import * # noqa: F401,F403 | |||
| 23 | from .necks import * # noqa: F401,F403 | 23 | from .necks import * # noqa: F401,F403 |
| 24 | from .registry import (BACKBONES, DETECTORS, HEADS, LOSSES, NECKS, | 24 | from .registry import (BACKBONES, DETECTORS, HEADS, LOSSES, NECKS, |
| 25 | ROI_EXTRACTORS, SHARED_HEADS) | 25 | ROI_EXTRACTORS, SHARED_HEADS) |
| 26 | + | ||
| 26 | # from .roi_extractors import * # noqa: F401,F403 | 27 | # from .roi_extractors import * # noqa: F401,F403 |
| 27 | # from .shared_heads import * # noqa: F401,F403 | 28 | # from .shared_heads import * # noqa: F401,F403 |
| 28 | 29 | ||
| @@ -27,6 +27,7 @@ | |||
| 27 | # from .ssd_head import SSDHead | 27 | # from .ssd_head import SSDHead |
| 28 | from .solo_head import SOLOHead | 28 | from .solo_head import SOLOHead |
| 29 | from .solov2_head import SOLOv2Head | 29 | from .solov2_head import SOLOv2Head |
| 30 | + | ||
| 30 | # from .solov2_light_head import SOLOv2LightHead | 31 | # from .solov2_light_head import SOLOv2LightHead |
| 31 | # from .decoupled_solo_head import DecoupledSOLOHead | 32 | # from .decoupled_solo_head import DecoupledSOLOHead |
| 32 | # from .decoupled_solo_light_head import DecoupledSOLOLightHead | 33 | # from .decoupled_solo_light_head import DecoupledSOLOLightHead |
| @@ -67,7 +67,7 @@ class ATSSHead(AnchorHead): | |||
| 67 | self.norm_cfg = norm_cfg | 67 | self.norm_cfg = norm_cfg |
| 68 | 68 | ||
| 69 | octave_scales = np.array( | 69 | octave_scales = np.array( |
| 70 | - [2**(i / scales_per_octave) for i in range(scales_per_octave)]) | 70 | + [2 ** (i / scales_per_octave) for i in range(scales_per_octave)]) |
| 71 | anchor_scales = octave_scales * octave_base_scale | 71 | anchor_scales = octave_scales * octave_base_scale |
| 72 | super(ATSSHead, self).__init__( | 72 | super(ATSSHead, self).__init__( |
| 73 | num_classes, in_channels, anchor_scales=anchor_scales, **kwargs) | 73 | num_classes, in_channels, anchor_scales=anchor_scales, **kwargs) |
| @@ -226,18 +226,18 @@ class ATSSHead(AnchorHead): | |||
| 226 | torch.tensor(num_total_pos).cuda()).item() | 226 | torch.tensor(num_total_pos).cuda()).item() |
| 227 | num_total_samples = max(num_total_samples, 1.0) | 227 | num_total_samples = max(num_total_samples, 1.0) |
| 228 | 228 | ||
| 229 | - losses_cls, losses_bbox, loss_centerness,\ | 229 | + losses_cls, losses_bbox, loss_centerness, \ |
| 230 | - bbox_avg_factor = multi_apply( | 230 | + bbox_avg_factor = multi_apply( |
| 231 | - self.loss_single, | 231 | + self.loss_single, |
| 232 | - anchor_list, | 232 | + anchor_list, |
| 233 | - cls_scores, | 233 | + cls_scores, |
| 234 | - bbox_preds, | 234 | + bbox_preds, |
| 235 | - centernesses, | 235 | + centernesses, |
| 236 | - labels_list, | 236 | + labels_list, |
| 237 | - label_weights_list, | 237 | + label_weights_list, |
| 238 | - bbox_targets_list, | 238 | + bbox_targets_list, |
| 239 | - num_total_samples=num_total_samples, | 239 | + num_total_samples=num_total_samples, |
| 240 | - cfg=cfg) | 240 | + cfg=cfg) |
| 241 | 241 | ||
| 242 | bbox_avg_factor = sum(bbox_avg_factor) | 242 | bbox_avg_factor = sum(bbox_avg_factor) |
| 243 | bbox_avg_factor = reduce_mean(bbox_avg_factor).item() | 243 | bbox_avg_factor = reduce_mean(bbox_avg_factor).item() |
| @@ -394,17 +394,17 @@ class ATSSHead(AnchorHead): | |||
| 394 | gt_labels_list = [None for _ in range(num_imgs)] | 394 | gt_labels_list = [None for _ in range(num_imgs)] |
| 395 | (all_anchors, all_labels, all_label_weights, all_bbox_targets, | 395 | (all_anchors, all_labels, all_label_weights, all_bbox_targets, |
| 396 | all_bbox_weights, pos_inds_list, neg_inds_list) = multi_apply( | 396 | all_bbox_weights, pos_inds_list, neg_inds_list) = multi_apply( |
| 397 | - self.atss_target_single, | 397 | + self.atss_target_single, |
| 398 | - anchor_list, | 398 | + anchor_list, |
| 399 | - valid_flag_list, | 399 | + valid_flag_list, |
| 400 | - num_level_anchors_list, | 400 | + num_level_anchors_list, |
| 401 | - gt_bboxes_list, | 401 | + gt_bboxes_list, |
| 402 | - gt_bboxes_ignore_list, | 402 | + gt_bboxes_ignore_list, |
| 403 | - gt_labels_list, | 403 | + gt_labels_list, |
| 404 | - img_metas, | 404 | + img_metas, |
| 405 | - cfg=cfg, | 405 | + cfg=cfg, |
| 406 | - label_channels=label_channels, | 406 | + label_channels=label_channels, |
| 407 | - unmap_outputs=unmap_outputs) | 407 | + unmap_outputs=unmap_outputs) |
| 408 | # no valid anchors | 408 | # no valid anchors |
| 409 | if any([labels is None for labels in all_labels]): | 409 | if any([labels is None for labels in all_labels]): |
| 410 | return None | 410 | return None |
| @@ -439,7 +439,7 @@ class ATSSHead(AnchorHead): | |||
| 439 | img_meta['img_shape'][:2], | 439 | img_meta['img_shape'][:2], |
| 440 | cfg.allowed_border) | 440 | cfg.allowed_border) |
| 441 | if not inside_flags.any(): | 441 | if not inside_flags.any(): |
| 442 | - return (None, ) * 6 | 442 | + return (None,) * 6 |
| 443 | # assign gt and sample anchors | 443 | # assign gt and sample anchors |
| 444 | anchors = flat_anchors[inside_flags, :] | 444 | anchors = flat_anchors[inside_flags, :] |
| 445 | 445 | ||
| @@ -25,6 +25,7 @@ from ..utils import bias_init_with_prob, ConvModule | |||
| 25 | 25 | ||
| 26 | INF = 1e8 | 26 | INF = 1e8 |
| 27 | 27 | ||
| 28 | + | ||
| 28 | def center_of_mass(bitmasks): | 29 | def center_of_mass(bitmasks): |
| 29 | _, h, w = bitmasks.size() | 30 | _, h, w = bitmasks.size() |
| 30 | ys = torch.arange(0, h, dtype=torch.float32, device=bitmasks.device) | 31 | ys = torch.arange(0, h, dtype=torch.float32, device=bitmasks.device) |
| @@ -37,6 +38,7 @@ def center_of_mass(bitmasks): | |||
| 37 | center_y = m01 / m00 | 38 | center_y = m01 / m00 |
| 38 | return center_x, center_y | 39 | return center_x, center_y |
| 39 | 40 | ||
| 41 | + | ||
| 40 | def points_nms(heat, kernel=2): | 42 | def points_nms(heat, kernel=2): |
| 41 | # kernel must be 2 | 43 | # kernel must be 2 |
| 42 | hmax = nn.functional.max_pool2d( | 44 | hmax = nn.functional.max_pool2d( |
| @@ -44,6 +46,7 @@ def points_nms(heat, kernel=2): | |||
| 44 | keep = (hmax[:, :, :-1, :-1] == heat).float() | 46 | keep = (hmax[:, :, :-1, :-1] == heat).float() |
| 45 | return heat * keep | 47 | return heat * keep |
| 46 | 48 | ||
| 49 | + | ||
| 47 | def dice_loss(input, target): | 50 | def dice_loss(input, target): |
| 48 | input = input.contiguous().view(input.size()[0], -1) | 51 | input = input.contiguous().view(input.size()[0], -1) |
| 49 | target = target.contiguous().view(target.size()[0], -1).float() | 52 | target = target.contiguous().view(target.size()[0], -1).float() |
| @@ -52,7 +55,8 @@ def dice_loss(input, target): | |||
| 52 | b = torch.sum(input * input, 1) + 0.001 | 55 | b = torch.sum(input * input, 1) + 0.001 |
| 53 | c = torch.sum(target * target, 1) + 0.001 | 56 | c = torch.sum(target * target, 1) + 0.001 |
| 54 | d = (2 * a) / (b + c) | 57 | d = (2 * a) / (b + c) |
| 55 | - return 1-d | 58 | + return 1 - d |
| 59 | + | ||
| 56 | 60 | ||
| 57 | 61 | ||
| 58 | class DecoupledSOLOHead(nn.Module): | 62 | class DecoupledSOLOHead(nn.Module): |
| @@ -166,10 +170,10 @@ class DecoupledSOLOHead(nn.Module): | |||
| 166 | return ins_pred_x, ins_pred_y, cate_pred | 170 | return ins_pred_x, ins_pred_y, cate_pred |
| 167 | 171 | ||
| 168 | def split_feats(self, feats): | 172 | def split_feats(self, feats): |
| 169 | - return (F.interpolate(feats[0], scale_factor=0.5, mode='bilinear'), | 173 | + return (F.interpolate(feats[0], scale_factor=0.5, mode='bilinear'), |
| 170 | - feats[1], | 174 | + feats[1], |
| 171 | - feats[2], | 175 | + feats[2], |
| 172 | - feats[3], | 176 | + feats[3], |
| 173 | F.interpolate(feats[4], size=feats[3].shape[-2:], mode='bilinear')) | 177 | F.interpolate(feats[4], size=feats[3].shape[-2:], mode='bilinear')) |
| 174 | 178 | ||
| 175 | def forward_single(self, x, idx, eval=False, upsampled_size=None): | 179 | def forward_single(self, x, idx, eval=False, upsampled_size=None): |
| @@ -198,7 +202,7 @@ class DecoupledSOLOHead(nn.Module): | |||
| 198 | # cate branch | 202 | # cate branch |
| 199 | for i, cate_layer in enumerate(self.cate_convs): | 203 | for i, cate_layer in enumerate(self.cate_convs): |
| 200 | if i == self.cate_down_pos: | 204 | if i == self.cate_down_pos: |
| 201 | - seg_num_grid = self.seg_num_grids[idx] | 205 | + seg_num_grid = self.seg_num_grids[idx] |
| 202 | cate_feat = F.interpolate(cate_feat, size=seg_num_grid, mode='bilinear') | 206 | cate_feat = F.interpolate(cate_feat, size=seg_num_grid, mode='bilinear') |
| 203 | cate_feat = cate_layer(cate_feat) | 207 | cate_feat = cate_layer(cate_feat) |
| 204 | 208 | ||
| @@ -236,16 +240,16 @@ class DecoupledSOLOHead(nn.Module): | |||
| 236 | for ins_labels_level, ins_ind_labels_level in zip(zip(*ins_label_list), zip(*ins_ind_label_list))] | 240 | for ins_labels_level, ins_ind_labels_level in zip(zip(*ins_label_list), zip(*ins_ind_label_list))] |
| 237 | 241 | ||
| 238 | ins_preds_x_final = [torch.cat([ins_preds_level_img_x[ins_ind_labels_level_img[:, 1], ...] | 242 | ins_preds_x_final = [torch.cat([ins_preds_level_img_x[ins_ind_labels_level_img[:, 1], ...] |
| 239 | - for ins_preds_level_img_x, ins_ind_labels_level_img in | 243 | + for ins_preds_level_img_x, ins_ind_labels_level_img in |
| 240 | - zip(ins_preds_level_x, ins_ind_labels_level)], 0) | 244 | + zip(ins_preds_level_x, ins_ind_labels_level)], 0) |
| 241 | - for ins_preds_level_x, ins_ind_labels_level in | 245 | + for ins_preds_level_x, ins_ind_labels_level in |
| 242 | - zip(ins_preds_x, zip(*ins_ind_label_list_xy))] | 246 | + zip(ins_preds_x, zip(*ins_ind_label_list_xy))] |
| 243 | 247 | ||
| 244 | ins_preds_y_final = [torch.cat([ins_preds_level_img_y[ins_ind_labels_level_img[:, 0], ...] | 248 | ins_preds_y_final = [torch.cat([ins_preds_level_img_y[ins_ind_labels_level_img[:, 0], ...] |
| 245 | - for ins_preds_level_img_y, ins_ind_labels_level_img in | 249 | + for ins_preds_level_img_y, ins_ind_labels_level_img in |
| 246 | - zip(ins_preds_level_y, ins_ind_labels_level)], 0) | 250 | + zip(ins_preds_level_y, ins_ind_labels_level)], 0) |
| 247 | - for ins_preds_level_y, ins_ind_labels_level in | 251 | + for ins_preds_level_y, ins_ind_labels_level in |
| 248 | - zip(ins_preds_y, zip(*ins_ind_label_list_xy))] | 252 | + zip(ins_preds_y, zip(*ins_ind_label_list_xy))] |
| 249 | 253 | ||
| 250 | num_ins = 0. | 254 | num_ins = 0. |
| 251 | # dice loss | 255 | # dice loss |
| @@ -255,7 +259,7 @@ class DecoupledSOLOHead(nn.Module): | |||
| 255 | if mask_n == 0: | 259 | if mask_n == 0: |
| 256 | continue | 260 | continue |
| 257 | num_ins += mask_n | 261 | num_ins += mask_n |
| 258 | - input = (input_x.sigmoid())*(input_y.sigmoid()) | 262 | + input = (input_x.sigmoid()) * (input_y.sigmoid()) |
| 259 | loss_ins.append(dice_loss(input, target)) | 263 | loss_ins.append(dice_loss(input, target)) |
| 260 | 264 | ||
| 261 | loss_ins = torch.cat(loss_ins).mean() * self.ins_loss_weight | 265 | loss_ins = torch.cat(loss_ins).mean() * self.ins_loss_weight |
| @@ -280,10 +284,10 @@ class DecoupledSOLOHead(nn.Module): | |||
| 280 | loss_cate=loss_cate) | 284 | loss_cate=loss_cate) |
| 281 | 285 | ||
| 282 | def solo_target_single(self, | 286 | def solo_target_single(self, |
| 283 | - gt_bboxes_raw, | 287 | + gt_bboxes_raw, |
| 284 | - gt_labels_raw, | 288 | + gt_labels_raw, |
| 285 | - gt_masks_raw, | 289 | + gt_masks_raw, |
| 286 | - featmap_sizes=None): | 290 | + featmap_sizes=None): |
| 287 | 291 | ||
| 288 | device = gt_labels_raw[0].device | 292 | device = gt_labels_raw[0].device |
| 289 | # ins | 293 | # ins |
| @@ -296,9 +300,9 @@ class DecoupledSOLOHead(nn.Module): | |||
| 296 | for (lower_bound, upper_bound), stride, featmap_size, num_grid \ | 300 | for (lower_bound, upper_bound), stride, featmap_size, num_grid \ |
| 297 | in zip(self.scale_ranges, self.strides, featmap_sizes, self.seg_num_grids): | 301 | in zip(self.scale_ranges, self.strides, featmap_sizes, self.seg_num_grids): |
| 298 | 302 | ||
| 299 | - ins_label = torch.zeros([num_grid**2, featmap_size[0], featmap_size[1]], dtype=torch.uint8, device=device) | 303 | + ins_label = torch.zeros([num_grid ** 2, featmap_size[0], featmap_size[1]], dtype=torch.uint8, device=device) |
| 300 | cate_label = torch.zeros([num_grid, num_grid], dtype=torch.int64, device=device) | 304 | cate_label = torch.zeros([num_grid, num_grid], dtype=torch.int64, device=device) |
| 301 | - ins_ind_label = torch.zeros([num_grid**2], dtype=torch.bool, device=device) | 305 | + ins_ind_label = torch.zeros([num_grid ** 2], dtype=torch.bool, device=device) |
| 302 | 306 | ||
| 303 | hit_indices = ((gt_areas >= lower_bound) & (gt_areas <= upper_bound)).nonzero().flatten() | 307 | hit_indices = ((gt_areas >= lower_bound) & (gt_areas <= upper_bound)).nonzero().flatten() |
| 304 | 308 | ||
| @@ -324,9 +328,12 @@ class DecoupledSOLOHead(nn.Module): | |||
| 324 | valid_mask_flags = gt_masks_pt.sum(dim=-1).sum(dim=-1) > 0 | 328 | valid_mask_flags = gt_masks_pt.sum(dim=-1).sum(dim=-1) > 0 |
| 325 | 329 | ||
| 326 | output_stride = stride / 2 | 330 | output_stride = stride / 2 |
| 327 | - for seg_mask, gt_label, half_h, half_w, center_h, center_w, valid_mask_flag in zip(gt_masks, gt_labels, half_hs, half_ws, center_hs, center_ws, valid_mask_flags): | 331 | + for seg_mask, gt_label, half_h, half_w, center_h, center_w, valid_mask_flag in zip(gt_masks, gt_labels, |
| 332 | + half_hs, half_ws, | ||
| 333 | + center_hs, center_ws, | ||
| 334 | + valid_mask_flags): | ||
| 328 | if not valid_mask_flag: | 335 | if not valid_mask_flag: |
| 329 | - continue | 336 | + continue |
| 330 | upsampled_size = (featmap_sizes[0][0] * 4, featmap_sizes[0][1] * 4) | 337 | upsampled_size = (featmap_sizes[0][0] * 4, featmap_sizes[0][1] * 4) |
| 331 | coord_w = int((center_w / upsampled_size[1]) // (1. / num_grid)) | 338 | coord_w = int((center_w / upsampled_size[1]) // (1. / num_grid)) |
| 332 | coord_h = int((center_h / upsampled_size[0]) // (1. / num_grid)) | 339 | coord_h = int((center_h / upsampled_size[0]) // (1. / num_grid)) |
| @@ -337,18 +344,18 @@ class DecoupledSOLOHead(nn.Module): | |||
| 337 | left_box = max(0, int(((center_w - half_w) / upsampled_size[1]) // (1. / num_grid))) | 344 | left_box = max(0, int(((center_w - half_w) / upsampled_size[1]) // (1. / num_grid))) |
| 338 | right_box = min(num_grid - 1, int(((center_w + half_w) / upsampled_size[1]) // (1. / num_grid))) | 345 | right_box = min(num_grid - 1, int(((center_w + half_w) / upsampled_size[1]) // (1. / num_grid))) |
| 339 | 346 | ||
| 340 | - top = max(top_box, coord_h-1) | 347 | + top = max(top_box, coord_h - 1) |
| 341 | - down = min(down_box, coord_h+1) | 348 | + down = min(down_box, coord_h + 1) |
| 342 | - left = max(coord_w-1, left_box) | 349 | + left = max(coord_w - 1, left_box) |
| 343 | - right = min(right_box, coord_w+1) | 350 | + right = min(right_box, coord_w + 1) |
| 344 | 351 | ||
| 345 | # squared | 352 | # squared |
| 346 | - cate_label[top:(down+1), left:(right+1)] = gt_label | 353 | + cate_label[top:(down + 1), left:(right + 1)] = gt_label |
| 347 | # ins | 354 | # ins |
| 348 | seg_mask = mmcv.imrescale(seg_mask, scale=1. / output_stride) | 355 | seg_mask = mmcv.imrescale(seg_mask, scale=1. / output_stride) |
| 349 | seg_mask = torch.from_numpy(seg_mask).to(device=device) | 356 | seg_mask = torch.from_numpy(seg_mask).to(device=device) |
| 350 | - for i in range(top, down+1): | 357 | + for i in range(top, down + 1): |
| 351 | - for j in range(left, right+1): | 358 | + for j in range(left, right + 1): |
| 352 | label = int(i * num_grid + j) | 359 | label = int(i * num_grid + j) |
| 353 | ins_label[label, :seg_mask.shape[0], :seg_mask.shape[1]] = seg_mask | 360 | ins_label[label, :seg_mask.shape[0], :seg_mask.shape[1]] = seg_mask |
| 354 | ins_ind_label[label] = True | 361 | ins_ind_label[label] = True |
| @@ -404,7 +411,6 @@ class DecoupledSOLOHead(nn.Module): | |||
| 404 | cfg, | 411 | cfg, |
| 405 | rescale=False, debug=False): | 412 | rescale=False, debug=False): |
| 406 | 413 | ||
| 407 | - | ||
| 408 | # overall info. | 414 | # overall info. |
| 409 | h, w, _ = img_shape | 415 | h, w, _ = img_shape |
| 410 | upsampled_size_out = (featmap_size[0] * 4, featmap_size[1] * 4) | 416 | upsampled_size_out = (featmap_size[0] * 4, featmap_size[1] * 4) |
| @@ -489,10 +495,10 @@ class DecoupledSOLOHead(nn.Module): | |||
| 489 | cate_labels = cate_labels[sort_inds] | 495 | cate_labels = cate_labels[sort_inds] |
| 490 | 496 | ||
| 491 | seg_masks_soft = F.interpolate(seg_masks_soft.unsqueeze(0), | 497 | seg_masks_soft = F.interpolate(seg_masks_soft.unsqueeze(0), |
| 492 | - size=upsampled_size_out, | 498 | + size=upsampled_size_out, |
| 493 | - mode='bilinear')[:, :, :h, :w] | 499 | + mode='bilinear')[:, :, :h, :w] |
| 494 | seg_masks = F.interpolate(seg_masks_soft, | 500 | seg_masks = F.interpolate(seg_masks_soft, |
| 495 | - size=ori_shape[:2], | 501 | + size=ori_shape[:2], |
| 496 | - mode='bilinear').squeeze(0) | 502 | + mode='bilinear').squeeze(0) |
| 497 | seg_masks = seg_masks > cfg.mask_thr | 503 | seg_masks = seg_masks > cfg.mask_thr |
| 498 | return seg_masks, cate_labels, cate_scores | 504 | return seg_masks, cate_labels, cate_scores |
| @@ -25,6 +25,7 @@ from ..utils import bias_init_with_prob, ConvModule | |||
| 25 | 25 | ||
| 26 | INF = 1e8 | 26 | INF = 1e8 |
| 27 | 27 | ||
| 28 | + | ||
| 28 | def center_of_mass(bitmasks): | 29 | def center_of_mass(bitmasks): |
| 29 | _, h, w = bitmasks.size() | 30 | _, h, w = bitmasks.size() |
| 30 | ys = torch.arange(0, h, dtype=torch.float32, device=bitmasks.device) | 31 | ys = torch.arange(0, h, dtype=torch.float32, device=bitmasks.device) |
| @@ -37,6 +38,7 @@ def center_of_mass(bitmasks): | |||
| 37 | center_y = m01 / m00 | 38 | center_y = m01 / m00 |
| 38 | return center_x, center_y | 39 | return center_x, center_y |
| 39 | 40 | ||
| 41 | + | ||
| 40 | def points_nms(heat, kernel=2): | 42 | def points_nms(heat, kernel=2): |
| 41 | # kernel must be 2 | 43 | # kernel must be 2 |
| 42 | hmax = nn.functional.max_pool2d( | 44 | hmax = nn.functional.max_pool2d( |
| @@ -44,6 +46,7 @@ def points_nms(heat, kernel=2): | |||
| 44 | keep = (hmax[:, :, :-1, :-1] == heat).float() | 46 | keep = (hmax[:, :, :-1, :-1] == heat).float() |
| 45 | return heat * keep | 47 | return heat * keep |
| 46 | 48 | ||
| 49 | + | ||
| 47 | def dice_loss(input, target): | 50 | def dice_loss(input, target): |
| 48 | input = input.contiguous().view(input.size()[0], -1) | 51 | input = input.contiguous().view(input.size()[0], -1) |
| 49 | target = target.contiguous().view(target.size()[0], -1).float() | 52 | target = target.contiguous().view(target.size()[0], -1).float() |
| @@ -52,7 +55,8 @@ def dice_loss(input, target): | |||
| 52 | b = torch.sum(input * input, 1) + 0.001 | 55 | b = torch.sum(input * input, 1) + 0.001 |
| 53 | c = torch.sum(target * target, 1) + 0.001 | 56 | c = torch.sum(target * target, 1) + 0.001 |
| 54 | d = (2 * a) / (b + c) | 57 | d = (2 * a) / (b + c) |
| 55 | - return 1-d | 58 | + return 1 - d |
| 59 | + | ||
| 56 | 60 | ||
| 57 | 61 | ||
| 58 | class DecoupledSOLOLightHead(nn.Module): | 62 | class DecoupledSOLOLightHead(nn.Module): |
| @@ -163,10 +167,10 @@ class DecoupledSOLOLightHead(nn.Module): | |||
| 163 | return ins_pred_x, ins_pred_y, cate_pred | 167 | return ins_pred_x, ins_pred_y, cate_pred |
| 164 | 168 | ||
| 165 | def split_feats(self, feats): | 169 | def split_feats(self, feats): |
| 166 | - return (F.interpolate(feats[0], scale_factor=0.5, mode='bilinear'), | 170 | + return (F.interpolate(feats[0], scale_factor=0.5, mode='bilinear'), |
| 167 | - feats[1], | 171 | + feats[1], |
| 168 | - feats[2], | 172 | + feats[2], |
| 169 | - feats[3], | 173 | + feats[3], |
| 170 | F.interpolate(feats[4], size=feats[3].shape[-2:], mode='bilinear')) | 174 | F.interpolate(feats[4], size=feats[3].shape[-2:], mode='bilinear')) |
| 171 | 175 | ||
| 172 | def forward_single(self, x, idx, eval=False, upsampled_size=None): | 176 | def forward_single(self, x, idx, eval=False, upsampled_size=None): |
| @@ -193,7 +197,7 @@ class DecoupledSOLOLightHead(nn.Module): | |||
| 193 | # cate branch | 197 | # cate branch |
| 194 | for i, cate_layer in enumerate(self.cate_convs): | 198 | for i, cate_layer in enumerate(self.cate_convs): |
| 195 | if i == self.cate_down_pos: | 199 | if i == self.cate_down_pos: |
| 196 | - seg_num_grid = self.seg_num_grids[idx] | 200 | + seg_num_grid = self.seg_num_grids[idx] |
| 197 | cate_feat = F.interpolate(cate_feat, size=seg_num_grid, mode='bilinear') | 201 | cate_feat = F.interpolate(cate_feat, size=seg_num_grid, mode='bilinear') |
| 198 | cate_feat = cate_layer(cate_feat) | 202 | cate_feat = cate_layer(cate_feat) |
| 199 | 203 | ||
| @@ -231,16 +235,16 @@ class DecoupledSOLOLightHead(nn.Module): | |||
| 231 | for ins_labels_level, ins_ind_labels_level in zip(zip(*ins_label_list), zip(*ins_ind_label_list))] | 235 | for ins_labels_level, ins_ind_labels_level in zip(zip(*ins_label_list), zip(*ins_ind_label_list))] |
| 232 | 236 | ||
| 233 | ins_preds_x_final = [torch.cat([ins_preds_level_img_x[ins_ind_labels_level_img[:, 1], ...] | 237 | ins_preds_x_final = [torch.cat([ins_preds_level_img_x[ins_ind_labels_level_img[:, 1], ...] |
| 234 | - for ins_preds_level_img_x, ins_ind_labels_level_img in | 238 | + for ins_preds_level_img_x, ins_ind_labels_level_img in |
| 235 | - zip(ins_preds_level_x, ins_ind_labels_level)], 0) | 239 | + zip(ins_preds_level_x, ins_ind_labels_level)], 0) |
| 236 | - for ins_preds_level_x, ins_ind_labels_level in | 240 | + for ins_preds_level_x, ins_ind_labels_level in |
| 237 | - zip(ins_preds_x, zip(*ins_ind_label_list_xy))] | 241 | + zip(ins_preds_x, zip(*ins_ind_label_list_xy))] |
| 238 | 242 | ||
| 239 | ins_preds_y_final = [torch.cat([ins_preds_level_img_y[ins_ind_labels_level_img[:, 0], ...] | 243 | ins_preds_y_final = [torch.cat([ins_preds_level_img_y[ins_ind_labels_level_img[:, 0], ...] |
| 240 | - for ins_preds_level_img_y, ins_ind_labels_level_img in | 244 | + for ins_preds_level_img_y, ins_ind_labels_level_img in |
| 241 | - zip(ins_preds_level_y, ins_ind_labels_level)], 0) | 245 | + zip(ins_preds_level_y, ins_ind_labels_level)], 0) |
| 242 | - for ins_preds_level_y, ins_ind_labels_level in | 246 | + for ins_preds_level_y, ins_ind_labels_level in |
| 243 | - zip(ins_preds_y, zip(*ins_ind_label_list_xy))] | 247 | + zip(ins_preds_y, zip(*ins_ind_label_list_xy))] |
| 244 | 248 | ||
| 245 | num_ins = 0. | 249 | num_ins = 0. |
| 246 | # dice loss | 250 | # dice loss |
| @@ -250,7 +254,7 @@ class DecoupledSOLOLightHead(nn.Module): | |||
| 250 | if mask_n == 0: | 254 | if mask_n == 0: |
| 251 | continue | 255 | continue |
| 252 | num_ins += mask_n | 256 | num_ins += mask_n |
| 253 | - input = (input_x.sigmoid())*(input_y.sigmoid()) | 257 | + input = (input_x.sigmoid()) * (input_y.sigmoid()) |
| 254 | loss_ins.append(dice_loss(input, target)) | 258 | loss_ins.append(dice_loss(input, target)) |
| 255 | 259 | ||
| 256 | loss_ins = torch.cat(loss_ins).mean() * self.ins_loss_weight | 260 | loss_ins = torch.cat(loss_ins).mean() * self.ins_loss_weight |
| @@ -275,10 +279,10 @@ class DecoupledSOLOLightHead(nn.Module): | |||
| 275 | loss_cate=loss_cate) | 279 | loss_cate=loss_cate) |
| 276 | 280 | ||
| 277 | def solo_target_single(self, | 281 | def solo_target_single(self, |
| 278 | - gt_bboxes_raw, | 282 | + gt_bboxes_raw, |
| 279 | - gt_labels_raw, | 283 | + gt_labels_raw, |
| 280 | - gt_masks_raw, | 284 | + gt_masks_raw, |
| 281 | - featmap_sizes=None): | 285 | + featmap_sizes=None): |
| 282 | 286 | ||
| 283 | device = gt_labels_raw[0].device | 287 | device = gt_labels_raw[0].device |
| 284 | # ins | 288 | # ins |
| @@ -291,9 +295,9 @@ class DecoupledSOLOLightHead(nn.Module): | |||
| 291 | for (lower_bound, upper_bound), stride, featmap_size, num_grid \ | 295 | for (lower_bound, upper_bound), stride, featmap_size, num_grid \ |
| 292 | in zip(self.scale_ranges, self.strides, featmap_sizes, self.seg_num_grids): | 296 | in zip(self.scale_ranges, self.strides, featmap_sizes, self.seg_num_grids): |
| 293 | 297 | ||
| 294 | - ins_label = torch.zeros([num_grid**2, featmap_size[0], featmap_size[1]], dtype=torch.uint8, device=device) | 298 | + ins_label = torch.zeros([num_grid ** 2, featmap_size[0], featmap_size[1]], dtype=torch.uint8, device=device) |
| 295 | cate_label = torch.zeros([num_grid, num_grid], dtype=torch.int64, device=device) | 299 | cate_label = torch.zeros([num_grid, num_grid], dtype=torch.int64, device=device) |
| 296 | - ins_ind_label = torch.zeros([num_grid**2], dtype=torch.bool, device=device) | 300 | + ins_ind_label = torch.zeros([num_grid ** 2], dtype=torch.bool, device=device) |
| 297 | 301 | ||
| 298 | hit_indices = ((gt_areas >= lower_bound) & (gt_areas <= upper_bound)).nonzero().flatten() | 302 | hit_indices = ((gt_areas >= lower_bound) & (gt_areas <= upper_bound)).nonzero().flatten() |
| 299 | 303 | ||
| @@ -319,9 +323,12 @@ class DecoupledSOLOLightHead(nn.Module): | |||
| 319 | valid_mask_flags = gt_masks_pt.sum(dim=-1).sum(dim=-1) > 0 | 323 | valid_mask_flags = gt_masks_pt.sum(dim=-1).sum(dim=-1) > 0 |
| 320 | 324 | ||
| 321 | output_stride = stride / 2 | 325 | output_stride = stride / 2 |
| 322 | - for seg_mask, gt_label, half_h, half_w, center_h, center_w, valid_mask_flag in zip(gt_masks, gt_labels, half_hs, half_ws, center_hs, center_ws, valid_mask_flags): | 326 | + for seg_mask, gt_label, half_h, half_w, center_h, center_w, valid_mask_flag in zip(gt_masks, gt_labels, |
| 327 | + half_hs, half_ws, | ||
| 328 | + center_hs, center_ws, | ||
| 329 | + valid_mask_flags): | ||
| 323 | if not valid_mask_flag: | 330 | if not valid_mask_flag: |
| 324 | - continue | 331 | + continue |
| 325 | upsampled_size = (featmap_sizes[0][0] * 4, featmap_sizes[0][1] * 4) | 332 | upsampled_size = (featmap_sizes[0][0] * 4, featmap_sizes[0][1] * 4) |
| 326 | coord_w = int((center_w / upsampled_size[1]) // (1. / num_grid)) | 333 | coord_w = int((center_w / upsampled_size[1]) // (1. / num_grid)) |
| 327 | coord_h = int((center_h / upsampled_size[0]) // (1. / num_grid)) | 334 | coord_h = int((center_h / upsampled_size[0]) // (1. / num_grid)) |
| @@ -332,18 +339,18 @@ class DecoupledSOLOLightHead(nn.Module): | |||
| 332 | left_box = max(0, int(((center_w - half_w) / upsampled_size[1]) // (1. / num_grid))) | 339 | left_box = max(0, int(((center_w - half_w) / upsampled_size[1]) // (1. / num_grid))) |
| 333 | right_box = min(num_grid - 1, int(((center_w + half_w) / upsampled_size[1]) // (1. / num_grid))) | 340 | right_box = min(num_grid - 1, int(((center_w + half_w) / upsampled_size[1]) // (1. / num_grid))) |
| 334 | 341 | ||
| 335 | - top = max(top_box, coord_h-1) | 342 | + top = max(top_box, coord_h - 1) |
| 336 | - down = min(down_box, coord_h+1) | 343 | + down = min(down_box, coord_h + 1) |
| 337 | - left = max(coord_w-1, left_box) | 344 | + left = max(coord_w - 1, left_box) |
| 338 | - right = min(right_box, coord_w+1) | 345 | + right = min(right_box, coord_w + 1) |
| 339 | 346 | ||
| 340 | # squared | 347 | # squared |
| 341 | - cate_label[top:(down+1), left:(right+1)] = gt_label | 348 | + cate_label[top:(down + 1), left:(right + 1)] = gt_label |
| 342 | # ins | 349 | # ins |
| 343 | seg_mask = mmcv.imrescale(seg_mask, scale=1. / output_stride) | 350 | seg_mask = mmcv.imrescale(seg_mask, scale=1. / output_stride) |
| 344 | seg_mask = torch.from_numpy(seg_mask).to(device=device) | 351 | seg_mask = torch.from_numpy(seg_mask).to(device=device) |
| 345 | - for i in range(top, down+1): | 352 | + for i in range(top, down + 1): |
| 346 | - for j in range(left, right+1): | 353 | + for j in range(left, right + 1): |
| 347 | label = int(i * num_grid + j) | 354 | label = int(i * num_grid + j) |
| 348 | ins_label[label, :seg_mask.shape[0], :seg_mask.shape[1]] = seg_mask | 355 | ins_label[label, :seg_mask.shape[0], :seg_mask.shape[1]] = seg_mask |
| 349 | ins_ind_label[label] = True | 356 | ins_ind_label[label] = True |
| @@ -399,7 +406,6 @@ class DecoupledSOLOLightHead(nn.Module): | |||
| 399 | cfg, | 406 | cfg, |
| 400 | rescale=False, debug=False): | 407 | rescale=False, debug=False): |
| 401 | 408 | ||
| 402 | - | ||
| 403 | # overall info. | 409 | # overall info. |
| 404 | h, w, _ = img_shape | 410 | h, w, _ = img_shape |
| 405 | upsampled_size_out = (featmap_size[0] * 4, featmap_size[1] * 4) | 411 | upsampled_size_out = (featmap_size[0] * 4, featmap_size[1] * 4) |
| @@ -484,10 +490,10 @@ class DecoupledSOLOLightHead(nn.Module): | |||
| 484 | cate_labels = cate_labels[sort_inds] | 490 | cate_labels = cate_labels[sort_inds] |
| 485 | 491 | ||
| 486 | seg_masks_soft = F.interpolate(seg_masks_soft.unsqueeze(0), | 492 | seg_masks_soft = F.interpolate(seg_masks_soft.unsqueeze(0), |
| 487 | - size=upsampled_size_out, | 493 | + size=upsampled_size_out, |
| 488 | - mode='bilinear')[:, :, :h, :w] | 494 | + mode='bilinear')[:, :, :h, :w] |
| 489 | seg_masks = F.interpolate(seg_masks_soft, | 495 | seg_masks = F.interpolate(seg_masks_soft, |
| 490 | - size=ori_shape[:2], | 496 | + size=ori_shape[:2], |
| 491 | - mode='bilinear').squeeze(0) | 497 | + mode='bilinear').squeeze(0) |
| 492 | seg_masks = seg_masks > cfg.mask_thr | 498 | seg_masks = seg_masks > cfg.mask_thr |
| 493 | return seg_masks, cate_labels, cate_scores | 499 | return seg_masks, cate_labels, cate_scores |
| @@ -374,7 +374,7 @@ class FCOSHead(nn.Module): | |||
| 374 | gt_bboxes.new_zeros((num_points, 4)) | 374 | gt_bboxes.new_zeros((num_points, 4)) |
| 375 | 375 | ||
| 376 | areas = (gt_bboxes[:, 2] - gt_bboxes[:, 0] + 1) * ( | 376 | areas = (gt_bboxes[:, 2] - gt_bboxes[:, 0] + 1) * ( |
| 377 | - gt_bboxes[:, 3] - gt_bboxes[:, 1] + 1) | 377 | + gt_bboxes[:, 3] - gt_bboxes[:, 1] + 1) |
| 378 | # TODO: figure out why these two are different | 378 | # TODO: figure out why these two are different |
| 379 | # areas = areas[None].expand(num_points, num_gts) | 379 | # areas = areas[None].expand(num_points, num_gts) |
| 380 | areas = areas[None].repeat(num_points, 1) | 380 | areas = areas[None].repeat(num_points, 1) |
| @@ -397,8 +397,8 @@ class FCOSHead(nn.Module): | |||
| 397 | # condition2: limit the regression range for each location | 397 | # condition2: limit the regression range for each location |
| 398 | max_regress_distance = bbox_targets.max(-1)[0] | 398 | max_regress_distance = bbox_targets.max(-1)[0] |
| 399 | inside_regress_range = ( | 399 | inside_regress_range = ( |
| 400 | - max_regress_distance >= regress_ranges[..., 0]) & ( | 400 | + max_regress_distance >= regress_ranges[..., 0]) & ( |
| 401 | - max_regress_distance <= regress_ranges[..., 1]) | 401 | + max_regress_distance <= regress_ranges[..., 1]) |
| 402 | 402 | ||
| 403 | # if there are still more than one objects for a location, | 403 | # if there are still more than one objects for a location, |
| 404 | # we choose the one with minimal area | 404 | # we choose the one with minimal area |
| @@ -417,6 +417,6 @@ class FCOSHead(nn.Module): | |||
| 417 | left_right = pos_bbox_targets[:, [0, 2]] | 417 | left_right = pos_bbox_targets[:, [0, 2]] |
| 418 | top_bottom = pos_bbox_targets[:, [1, 3]] | 418 | top_bottom = pos_bbox_targets[:, [1, 3]] |
| 419 | centerness_targets = ( | 419 | centerness_targets = ( |
| 420 | - left_right.min(dim=-1)[0] / left_right.max(dim=-1)[0]) * ( | 420 | + left_right.min(dim=-1)[0] / left_right.max(dim=-1)[0]) * ( |
| 421 | - top_bottom.min(dim=-1)[0] / top_bottom.max(dim=-1)[0]) | 421 | + top_bottom.min(dim=-1)[0] / top_bottom.max(dim=-1)[0]) |
| 422 | return torch.sqrt(centerness_targets) | 422 | return torch.sqrt(centerness_targets) |
| @@ -297,16 +297,16 @@ class FoveaHead(nn.Module): | |||
| 297 | half_h = 0.5 * (gt_bboxes[:, 3] - gt_bboxes[:, 1]) | 297 | half_h = 0.5 * (gt_bboxes[:, 3] - gt_bboxes[:, 1]) |
| 298 | # valid fovea area: left, right, top, down | 298 | # valid fovea area: left, right, top, down |
| 299 | pos_left = torch.ceil( | 299 | pos_left = torch.ceil( |
| 300 | - gt_bboxes[:, 0] + (1 - self.sigma) * half_w - 0.5).long().\ | 300 | + gt_bboxes[:, 0] + (1 - self.sigma) * half_w - 0.5).long(). \ |
| 301 | clamp(0, featmap_size[1] - 1) | 301 | clamp(0, featmap_size[1] - 1) |
| 302 | pos_right = torch.floor( | 302 | pos_right = torch.floor( |
| 303 | - gt_bboxes[:, 0] + (1 + self.sigma) * half_w - 0.5).long().\ | 303 | + gt_bboxes[:, 0] + (1 + self.sigma) * half_w - 0.5).long(). \ |
| 304 | clamp(0, featmap_size[1] - 1) | 304 | clamp(0, featmap_size[1] - 1) |
| 305 | pos_top = torch.ceil( | 305 | pos_top = torch.ceil( |
| 306 | - gt_bboxes[:, 1] + (1 - self.sigma) * half_h - 0.5).long().\ | 306 | + gt_bboxes[:, 1] + (1 - self.sigma) * half_h - 0.5).long(). \ |
| 307 | clamp(0, featmap_size[0] - 1) | 307 | clamp(0, featmap_size[0] - 1) |
| 308 | pos_down = torch.floor( | 308 | pos_down = torch.floor( |
| 309 | - gt_bboxes[:, 1] + (1 + self.sigma) * half_h - 0.5).long().\ | 309 | + gt_bboxes[:, 1] + (1 + self.sigma) * half_h - 0.5).long(). \ |
| 310 | clamp(0, featmap_size[0] - 1) | 310 | clamp(0, featmap_size[0] - 1) |
| 311 | for px1, py1, px2, py2, label, (gt_x1, gt_y1, gt_x2, gt_y2) in \ | 311 | for px1, py1, px2, py2, label, (gt_x1, gt_y1, gt_x2, gt_y2) in \ |
| 312 | zip(pos_left, pos_top, pos_right, pos_down, gt_labels, | 312 | zip(pos_left, pos_top, pos_right, pos_down, gt_labels, |
| @@ -378,13 +378,13 @@ class FoveaHead(nn.Module): | |||
| 378 | scores = scores[topk_inds, :] | 378 | scores = scores[topk_inds, :] |
| 379 | y = y[topk_inds] | 379 | y = y[topk_inds] |
| 380 | x = x[topk_inds] | 380 | x = x[topk_inds] |
| 381 | - x1 = (stride * x - base_len * bbox_pred[:, 0]).\ | 381 | + x1 = (stride * x - base_len * bbox_pred[:, 0]). \ |
| 382 | clamp(min=0, max=img_shape[1] - 1) | 382 | clamp(min=0, max=img_shape[1] - 1) |
| 383 | - y1 = (stride * y - base_len * bbox_pred[:, 1]).\ | 383 | + y1 = (stride * y - base_len * bbox_pred[:, 1]). \ |
| 384 | clamp(min=0, max=img_shape[0] - 1) | 384 | clamp(min=0, max=img_shape[0] - 1) |
| 385 | - x2 = (stride * x + base_len * bbox_pred[:, 2]).\ | 385 | + x2 = (stride * x + base_len * bbox_pred[:, 2]). \ |
| 386 | clamp(min=0, max=img_shape[1] - 1) | 386 | clamp(min=0, max=img_shape[1] - 1) |
| 387 | - y2 = (stride * y + base_len * bbox_pred[:, 3]).\ | 387 | + y2 = (stride * y + base_len * bbox_pred[:, 3]). \ |
| 388 | clamp(min=0, max=img_shape[0] - 1) | 388 | clamp(min=0, max=img_shape[0] - 1) |
| 389 | bboxes = torch.stack([x1, y1, x2, y2], -1) | 389 | bboxes = torch.stack([x1, y1, x2, y2], -1) |
| 390 | det_bboxes.append(bboxes) | 390 | det_bboxes.append(bboxes) |
| @@ -79,7 +79,7 @@ class FreeAnchorRetinaHead(RetinaHead): | |||
| 79 | positive_losses = [] | 79 | positive_losses = [] |
| 80 | for _, (anchors_, gt_labels_, gt_bboxes_, cls_prob_, | 80 | for _, (anchors_, gt_labels_, gt_bboxes_, cls_prob_, |
| 81 | bbox_preds_) in enumerate( | 81 | bbox_preds_) in enumerate( |
| 82 | - zip(anchors, gt_labels, gt_bboxes, cls_prob, bbox_preds)): | 82 | + zip(anchors, gt_labels, gt_bboxes, cls_prob, bbox_preds)): |
| 83 | gt_labels_ -= 1 | 83 | gt_labels_ -= 1 |
| 84 | 84 | ||
| 85 | with torch.no_grad(): | 85 | with torch.no_grad(): |
| @@ -197,6 +197,6 @@ class FreeAnchorRetinaHead(RetinaHead): | |||
| 197 | 197 | ||
| 198 | def negative_bag_loss(self, cls_prob, box_prob): | 198 | def negative_bag_loss(self, cls_prob, box_prob): |
| 199 | prob = cls_prob * (1 - box_prob) | 199 | prob = cls_prob * (1 - box_prob) |
| 200 | - negative_bag_loss = prob**self.gamma * F.binary_cross_entropy( | 200 | + negative_bag_loss = prob ** self.gamma * F.binary_cross_entropy( |
| 201 | prob, torch.zeros_like(prob), reduction='none') | 201 | prob, torch.zeros_like(prob), reduction='none') |
| 202 | return (1 - self.alpha) * negative_bag_loss | 202 | return (1 - self.alpha) * negative_bag_loss |
| @@ -108,32 +108,32 @@ class GuidedAnchorHead(AnchorHead): | |||
| 108 | """ | 108 | """ |
| 109 | 109 | ||
| 110 | def __init__( | 110 | def __init__( |
| 111 | - self, | 111 | + self, |
| 112 | - num_classes, | 112 | + num_classes, |
| 113 | - in_channels, | 113 | + in_channels, |
| 114 | - feat_channels=256, | 114 | + feat_channels=256, |
| 115 | - octave_base_scale=8, | 115 | + octave_base_scale=8, |
| 116 | - scales_per_octave=3, | 116 | + scales_per_octave=3, |
| 117 | - octave_ratios=[0.5, 1.0, 2.0], | 117 | + octave_ratios=[0.5, 1.0, 2.0], |
| 118 | - anchor_strides=[4, 8, 16, 32, 64], | 118 | + anchor_strides=[4, 8, 16, 32, 64], |
| 119 | - anchor_base_sizes=None, | 119 | + anchor_base_sizes=None, |
| 120 | - anchoring_means=(.0, .0, .0, .0), | 120 | + anchoring_means=(.0, .0, .0, .0), |
| 121 | - anchoring_stds=(1.0, 1.0, 1.0, 1.0), | 121 | + anchoring_stds=(1.0, 1.0, 1.0, 1.0), |
| 122 | - target_means=(.0, .0, .0, .0), | 122 | + target_means=(.0, .0, .0, .0), |
| 123 | - target_stds=(1.0, 1.0, 1.0, 1.0), | 123 | + target_stds=(1.0, 1.0, 1.0, 1.0), |
| 124 | - deformable_groups=4, | 124 | + deformable_groups=4, |
| 125 | - loc_filter_thr=0.01, | 125 | + loc_filter_thr=0.01, |
| 126 | - loss_loc=dict( | 126 | + loss_loc=dict( |
| 127 | - type='FocalLoss', | 127 | + type='FocalLoss', |
| 128 | - use_sigmoid=True, | 128 | + use_sigmoid=True, |
| 129 | - gamma=2.0, | 129 | + gamma=2.0, |
| 130 | - alpha=0.25, | 130 | + alpha=0.25, |
| 131 | - loss_weight=1.0), | 131 | + loss_weight=1.0), |
| 132 | - loss_shape=dict(type='BoundedIoULoss', beta=0.2, loss_weight=1.0), | 132 | + loss_shape=dict(type='BoundedIoULoss', beta=0.2, loss_weight=1.0), |
| 133 | - loss_cls=dict( | 133 | + loss_cls=dict( |
| 134 | - type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0), | 134 | + type='CrossEntropyLoss', use_sigmoid=True, loss_weight=1.0), |
| 135 | - loss_bbox=dict(type='SmoothL1Loss', beta=1.0, | 135 | + loss_bbox=dict(type='SmoothL1Loss', beta=1.0, |
| 136 | - loss_weight=1.0)): # yapf: disable | 136 | + loss_weight=1.0)): # yapf: disable |
| 137 | super(AnchorHead, self).__init__() | 137 | super(AnchorHead, self).__init__() |
| 138 | self.in_channels = in_channels | 138 | self.in_channels = in_channels |
| 139 | self.num_classes = num_classes | 139 | self.num_classes = num_classes |
| @@ -141,7 +141,7 @@ class GuidedAnchorHead(AnchorHead): | |||
| 141 | self.octave_base_scale = octave_base_scale | 141 | self.octave_base_scale = octave_base_scale |
| 142 | self.scales_per_octave = scales_per_octave | 142 | self.scales_per_octave = scales_per_octave |
| 143 | self.octave_scales = octave_base_scale * np.array( | 143 | self.octave_scales = octave_base_scale * np.array( |
| 144 | - [2**(i / scales_per_octave) for i in range(scales_per_octave)]) | 144 | + [2 ** (i / scales_per_octave) for i in range(scales_per_octave)]) |
| 145 | self.approxs_per_octave = len(self.octave_scales) * len(octave_ratios) | 145 | self.approxs_per_octave = len(self.octave_scales) * len(octave_ratios) |
| 146 | self.octave_ratios = octave_ratios | 146 | self.octave_ratios = octave_ratios |
| 147 | self.anchor_strides = anchor_strides | 147 | self.anchor_strides = anchor_strides |
| @@ -277,7 +277,7 @@ class GuidedAnchorHead(AnchorHead): | |||
| 277 | # inside_flag for a position is true if any anchor in this | 277 | # inside_flag for a position is true if any anchor in this |
| 278 | # position is true | 278 | # position is true |
| 279 | inside_flags = ( | 279 | inside_flags = ( |
| 280 | - torch.stack(inside_flags_list, 0).sum(dim=0) > 0) | 280 | + torch.stack(inside_flags_list, 0).sum(dim=0) > 0) |
| 281 | multi_level_flags.append(inside_flags) | 281 | multi_level_flags.append(inside_flags) |
| 282 | inside_flag_list.append(multi_level_flags) | 282 | inside_flag_list.append(multi_level_flags) |
| 283 | return approxs_list, inside_flag_list | 283 | return approxs_list, inside_flag_list |
| @@ -483,7 +483,7 @@ class GuidedAnchorHead(AnchorHead): | |||
| 483 | num_total_pos, num_total_neg) = cls_reg_targets | 483 | num_total_pos, num_total_neg) = cls_reg_targets |
| 484 | num_total_samples = ( | 484 | num_total_samples = ( |
| 485 | num_total_pos if self.cls_focal_loss else num_total_pos + | 485 | num_total_pos if self.cls_focal_loss else num_total_pos + |
| 486 | - num_total_neg) | 486 | + num_total_neg) |
| 487 | 487 | ||
| 488 | # get classification and bbox regression losses | 488 | # get classification and bbox regression losses |
| 489 | losses_cls, losses_bbox = multi_apply( | 489 | losses_cls, losses_bbox = multi_apply( |
| @@ -187,9 +187,9 @@ class RepPointsHead(nn.Module): | |||
| 187 | """ | 187 | """ |
| 188 | pts_reshape = pts.view(pts.shape[0], -1, 2, *pts.shape[2:]) | 188 | pts_reshape = pts.view(pts.shape[0], -1, 2, *pts.shape[2:]) |
| 189 | pts_y = pts_reshape[:, :, 0, ...] if y_first else pts_reshape[:, :, 1, | 189 | pts_y = pts_reshape[:, :, 0, ...] if y_first else pts_reshape[:, :, 1, |
| 190 | - ...] | 190 | + ...] |
| 191 | pts_x = pts_reshape[:, :, 1, ...] if y_first else pts_reshape[:, :, 0, | 191 | pts_x = pts_reshape[:, :, 1, ...] if y_first else pts_reshape[:, :, 0, |
| 192 | - ...] | 192 | + ...] |
| 193 | if self.transform_method == 'minmax': | 193 | if self.transform_method == 'minmax': |
| 194 | bbox_left = pts_x.min(dim=1, keepdim=True)[0] | 194 | bbox_left = pts_x.min(dim=1, keepdim=True)[0] |
| 195 | bbox_right = pts_x.max(dim=1, keepdim=True)[0] | 195 | bbox_right = pts_x.max(dim=1, keepdim=True)[0] |
| @@ -212,7 +212,7 @@ class RepPointsHead(nn.Module): | |||
| 212 | pts_y_std = torch.std(pts_y - pts_y_mean, dim=1, keepdim=True) | 212 | pts_y_std = torch.std(pts_y - pts_y_mean, dim=1, keepdim=True) |
| 213 | pts_x_std = torch.std(pts_x - pts_x_mean, dim=1, keepdim=True) | 213 | pts_x_std = torch.std(pts_x - pts_x_mean, dim=1, keepdim=True) |
| 214 | moment_transfer = (self.moment_transfer * self.moment_mul) + ( | 214 | moment_transfer = (self.moment_transfer * self.moment_mul) + ( |
| 215 | - self.moment_transfer.detach() * (1 - self.moment_mul)) | 215 | + self.moment_transfer.detach() * (1 - self.moment_mul)) |
| 216 | moment_width_transfer = moment_transfer[0] | 216 | moment_width_transfer = moment_transfer[0] |
| 217 | moment_height_transfer = moment_transfer[1] | 217 | moment_height_transfer = moment_transfer[1] |
| 218 | half_width = pts_x_std * torch.exp(moment_width_transfer) | 218 | half_width = pts_x_std * torch.exp(moment_width_transfer) |
| @@ -221,7 +221,7 @@ class RepPointsHead(nn.Module): | |||
| 221 | pts_x_mean - half_width, pts_y_mean - half_height, | 221 | pts_x_mean - half_width, pts_y_mean - half_height, |
| 222 | pts_x_mean + half_width, pts_y_mean + half_height | 222 | pts_x_mean + half_width, pts_y_mean + half_height |
| 223 | ], | 223 | ], |
| 224 | - dim=1) | 224 | + dim=1) |
| 225 | else: | 225 | else: |
| 226 | raise NotImplementedError | 226 | raise NotImplementedError |
| 227 | return bbox | 227 | return bbox |
| @@ -59,7 +59,7 @@ class RetinaHead(AnchorHead): | |||
| 59 | self.conv_cfg = conv_cfg | 59 | self.conv_cfg = conv_cfg |
| 60 | self.norm_cfg = norm_cfg | 60 | self.norm_cfg = norm_cfg |
| 61 | octave_scales = np.array( | 61 | octave_scales = np.array( |
| 62 | - [2**(i / scales_per_octave) for i in range(scales_per_octave)]) | 62 | + [2 ** (i / scales_per_octave) for i in range(scales_per_octave)]) |
| 63 | anchor_scales = octave_scales * octave_base_scale | 63 | anchor_scales = octave_scales * octave_base_scale |
| 64 | super(RetinaHead, self).__init__( | 64 | super(RetinaHead, self).__init__( |
| 65 | num_classes, in_channels, anchor_scales=anchor_scales, **kwargs) | 65 | num_classes, in_channels, anchor_scales=anchor_scales, **kwargs) |
| @@ -47,7 +47,7 @@ class RetinaSepBNHead(AnchorHead): | |||
| 47 | self.norm_cfg = norm_cfg | 47 | self.norm_cfg = norm_cfg |
| 48 | self.num_ins = num_ins | 48 | self.num_ins = num_ins |
| 49 | octave_scales = np.array( | 49 | octave_scales = np.array( |
| 50 | - [2**(i / scales_per_octave) for i in range(scales_per_octave)]) | 50 | + [2 ** (i / scales_per_octave) for i in range(scales_per_octave)]) |
| 51 | anchor_scales = octave_scales * octave_base_scale | 51 | anchor_scales = octave_scales * octave_base_scale |
| 52 | super(RetinaSepBNHead, self).__init__( | 52 | super(RetinaSepBNHead, self).__init__( |
| 53 | num_classes, in_channels, anchor_scales=anchor_scales, **kwargs) | 53 | num_classes, in_channels, anchor_scales=anchor_scales, **kwargs) |
| @@ -25,6 +25,7 @@ from ..utils import bias_init_with_prob, ConvModule | |||
| 25 | 25 | ||
| 26 | INF = 1e8 | 26 | INF = 1e8 |
| 27 | 27 | ||
| 28 | + | ||
| 28 | def center_of_mass(bitmasks): | 29 | def center_of_mass(bitmasks): |
| 29 | _, h, w = bitmasks.size() | 30 | _, h, w = bitmasks.size() |
| 30 | ys = torch.arange(0, h, dtype=torch.float32, device=bitmasks.device) | 31 | ys = torch.arange(0, h, dtype=torch.float32, device=bitmasks.device) |
| @@ -37,6 +38,7 @@ def center_of_mass(bitmasks): | |||
| 37 | center_y = m01 / m00 | 38 | center_y = m01 / m00 |
| 38 | return center_x, center_y | 39 | return center_x, center_y |
| 39 | 40 | ||
| 41 | + | ||
| 40 | def points_nms(heat, kernel=2): | 42 | def points_nms(heat, kernel=2): |
| 41 | # kernel must be 2 | 43 | # kernel must be 2 |
| 42 | hmax = nn.functional.max_pool2d( | 44 | hmax = nn.functional.max_pool2d( |
| @@ -44,6 +46,7 @@ def points_nms(heat, kernel=2): | |||
| 44 | keep = (hmax[:, :, :-1, :-1] == heat).float() | 46 | keep = (hmax[:, :, :-1, :-1] == heat).float() |
| 45 | return heat * keep | 47 | return heat * keep |
| 46 | 48 | ||
| 49 | + | ||
| 47 | def dice_loss(input, target): | 50 | def dice_loss(input, target): |
| 48 | input = input.contiguous().view(input.size()[0], -1) | 51 | input = input.contiguous().view(input.size()[0], -1) |
| 49 | target = target.contiguous().view(target.size()[0], -1).float() | 52 | target = target.contiguous().view(target.size()[0], -1).float() |
| @@ -52,7 +55,8 @@ def dice_loss(input, target): | |||
| 52 | b = torch.sum(input * input, 1) + 0.001 | 55 | b = torch.sum(input * input, 1) + 0.001 |
| 53 | c = torch.sum(target * target, 1) + 0.001 | 56 | c = torch.sum(target * target, 1) + 0.001 |
| 54 | d = (2 * a) / (b + c) | 57 | d = (2 * a) / (b + c) |
| 55 | - return 1-d | 58 | + return 1 - d |
| 59 | + | ||
| 56 | 60 | ||
| 57 | 61 | ||
| 58 | class SOLOHead(nn.Module): | 62 | class SOLOHead(nn.Module): |
| @@ -123,7 +127,7 @@ class SOLOHead(nn.Module): | |||
| 123 | for seg_num_grid in self.seg_num_grids: | 127 | for seg_num_grid in self.seg_num_grids: |
| 124 | self.solo_ins_list.append( | 128 | self.solo_ins_list.append( |
| 125 | nn.Conv2d( | 129 | nn.Conv2d( |
| 126 | - self.seg_feat_channels, seg_num_grid**2, 1)) | 130 | + self.seg_feat_channels, seg_num_grid ** 2, 1)) |
| 127 | 131 | ||
| 128 | self.solo_cate = nn.Conv2d( | 132 | self.solo_cate = nn.Conv2d( |
| 129 | self.seg_feat_channels, self.cate_out_channels, 3, padding=1) | 133 | self.seg_feat_channels, self.cate_out_channels, 3, padding=1) |
| @@ -134,7 +138,7 @@ class SOLOHead(nn.Module): | |||
| 134 | for m in self.cate_convs: | 138 | for m in self.cate_convs: |
| 135 | normal_init(m.conv, std=0.01) | 139 | normal_init(m.conv, std=0.01) |
| 136 | bias_ins = bias_init_with_prob(0.01) | 140 | bias_ins = bias_init_with_prob(0.01) |
| 137 | - for m in self.solo_ins_list: | 141 | + for m in self.solo_ins_list: |
| 138 | normal_init(m, std=0.01, bias=bias_ins) | 142 | normal_init(m, std=0.01, bias=bias_ins) |
| 139 | bias_cate = bias_init_with_prob(0.01) | 143 | bias_cate = bias_init_with_prob(0.01) |
| 140 | normal_init(self.solo_cate, std=0.01, bias=bias_cate) | 144 | normal_init(self.solo_cate, std=0.01, bias=bias_cate) |
| @@ -143,16 +147,16 @@ class SOLOHead(nn.Module): | |||
| 143 | new_feats = self.split_feats(feats) | 147 | new_feats = self.split_feats(feats) |
| 144 | featmap_sizes = [featmap.size()[-2:] for featmap in new_feats] | 148 | featmap_sizes = [featmap.size()[-2:] for featmap in new_feats] |
| 145 | upsampled_size = (featmap_sizes[0][0] * 2, featmap_sizes[0][1] * 2) | 149 | upsampled_size = (featmap_sizes[0][0] * 2, featmap_sizes[0][1] * 2) |
| 146 | - ins_pred, cate_pred = multi_apply(self.forward_single, new_feats, | 150 | + ins_pred, cate_pred = multi_apply(self.forward_single, new_feats, |
| 147 | list(range(len(self.seg_num_grids))), | 151 | list(range(len(self.seg_num_grids))), |
| 148 | eval=eval, upsampled_size=upsampled_size) | 152 | eval=eval, upsampled_size=upsampled_size) |
| 149 | return ins_pred, cate_pred | 153 | return ins_pred, cate_pred |
| 150 | 154 | ||
| 151 | def split_feats(self, feats): | 155 | def split_feats(self, feats): |
| 152 | - return (F.interpolate(feats[0], scale_factor=0.5, mode='bilinear'), | 156 | + return (F.interpolate(feats[0], scale_factor=0.5, mode='bilinear'), |
| 153 | - feats[1], | 157 | + feats[1], |
| 154 | - feats[2], | 158 | + feats[2], |
| 155 | - feats[3], | 159 | + feats[3], |
| 156 | F.interpolate(feats[4], size=feats[3].shape[-2:], mode='bilinear')) | 160 | F.interpolate(feats[4], size=feats[3].shape[-2:], mode='bilinear')) |
| 157 | 161 | ||
| 158 | def forward_single(self, x, idx, eval=False, upsampled_size=None): | 162 | def forward_single(self, x, idx, eval=False, upsampled_size=None): |
| @@ -216,7 +220,6 @@ class SOLOHead(nn.Module): | |||
| 216 | zip(ins_preds_level, ins_ind_labels_level)], 0) | 220 | zip(ins_preds_level, ins_ind_labels_level)], 0) |
| 217 | for ins_preds_level, ins_ind_labels_level in zip(ins_preds, zip(*ins_ind_label_list))] | 221 | for ins_preds_level, ins_ind_labels_level in zip(ins_preds, zip(*ins_ind_label_list))] |
| 218 | 222 | ||
| 219 | - | ||
| 220 | ins_ind_labels = [ | 223 | ins_ind_labels = [ |
| 221 | torch.cat([ins_ind_labels_level_img.flatten() | 224 | torch.cat([ins_ind_labels_level_img.flatten() |
| 222 | for ins_ind_labels_level_img in ins_ind_labels_level]) | 225 | for ins_ind_labels_level_img in ins_ind_labels_level]) |
| @@ -256,10 +259,10 @@ class SOLOHead(nn.Module): | |||
| 256 | loss_cate=loss_cate) | 259 | loss_cate=loss_cate) |
| 257 | 260 | ||
| 258 | def solo_target_single(self, | 261 | def solo_target_single(self, |
| 259 | - gt_bboxes_raw, | 262 | + gt_bboxes_raw, |
| 260 | - gt_labels_raw, | 263 | + gt_labels_raw, |
| 261 | - gt_masks_raw, | 264 | + gt_masks_raw, |
| 262 | - featmap_sizes=None): | 265 | + featmap_sizes=None): |
| 263 | 266 | ||
| 264 | device = gt_labels_raw[0].device | 267 | device = gt_labels_raw[0].device |
| 265 | 268 | ||
| @@ -296,9 +299,12 @@ class SOLOHead(nn.Module): | |||
| 296 | valid_mask_flags = gt_masks_pt.sum(dim=-1).sum(dim=-1) > 0 | 299 | valid_mask_flags = gt_masks_pt.sum(dim=-1).sum(dim=-1) > 0 |
| 297 | 300 | ||
| 298 | output_stride = stride / 2 | 301 | output_stride = stride / 2 |
| 299 | - for seg_mask, gt_label, half_h, half_w, center_h, center_w, valid_mask_flag in zip(gt_masks, gt_labels, half_hs, half_ws, center_hs, center_ws, valid_mask_flags): | 302 | + for seg_mask, gt_label, half_h, half_w, center_h, center_w, valid_mask_flag in zip(gt_masks, gt_labels, |
| 303 | + half_hs, half_ws, | ||
| 304 | + center_hs, center_ws, | ||
| 305 | + valid_mask_flags): | ||
| 300 | if not valid_mask_flag: | 306 | if not valid_mask_flag: |
| 301 | - continue | 307 | + continue |
| 302 | upsampled_size = (featmap_sizes[0][0] * 4, featmap_sizes[0][1] * 4) | 308 | upsampled_size = (featmap_sizes[0][0] * 4, featmap_sizes[0][1] * 4) |
| 303 | coord_w = int((center_w / upsampled_size[1]) // (1. / num_grid)) | 309 | coord_w = int((center_w / upsampled_size[1]) // (1. / num_grid)) |
| 304 | coord_h = int((center_h / upsampled_size[0]) // (1. / num_grid)) | 310 | coord_h = int((center_h / upsampled_size[0]) // (1. / num_grid)) |
| @@ -309,17 +315,17 @@ class SOLOHead(nn.Module): | |||
| 309 | left_box = max(0, int(((center_w - half_w) / upsampled_size[1]) // (1. / num_grid))) | 315 | left_box = max(0, int(((center_w - half_w) / upsampled_size[1]) // (1. / num_grid))) |
| 310 | right_box = min(num_grid - 1, int(((center_w + half_w) / upsampled_size[1]) // (1. / num_grid))) | 316 | right_box = min(num_grid - 1, int(((center_w + half_w) / upsampled_size[1]) // (1. / num_grid))) |
| 311 | 317 | ||
| 312 | - top = max(top_box, coord_h-1) | 318 | + top = max(top_box, coord_h - 1) |
| 313 | - down = min(down_box, coord_h+1) | 319 | + down = min(down_box, coord_h + 1) |
| 314 | - left = max(coord_w-1, left_box) | 320 | + left = max(coord_w - 1, left_box) |
| 315 | - right = min(right_box, coord_w+1) | 321 | + right = min(right_box, coord_w + 1) |
| 316 | 322 | ||
| 317 | - cate_label[top:(down+1), left:(right+1)] = gt_label | 323 | + cate_label[top:(down + 1), left:(right + 1)] = gt_label |
| 318 | # ins | 324 | # ins |
| 319 | seg_mask = mmcv.imrescale(seg_mask, scale=1. / output_stride) | 325 | seg_mask = mmcv.imrescale(seg_mask, scale=1. / output_stride) |
| 320 | seg_mask = torch.from_numpy(seg_mask).to(device=device) | 326 | seg_mask = torch.from_numpy(seg_mask).to(device=device) |
| 321 | - for i in range(top, down+1): | 327 | + for i in range(top, down + 1): |
| 322 | - for j in range(left, right+1): | 328 | + for j in range(left, right + 1): |
| 323 | label = int(i * num_grid + j) | 329 | label = int(i * num_grid + j) |
| 324 | ins_label[label, :seg_mask.shape[0], :seg_mask.shape[1]] = seg_mask | 330 | ins_label[label, :seg_mask.shape[0], :seg_mask.shape[1]] = seg_mask |
| 325 | ins_ind_label[label] = True | 331 | ins_ind_label[label] = True |
| @@ -23,8 +23,10 @@ from ..builder import build_loss | |||
| 23 | from ..registry import HEADS | 23 | from ..registry import HEADS |
| 24 | from ..utils import bias_init_with_prob, ConvModule | 24 | from ..utils import bias_init_with_prob, ConvModule |
| 25 | import numpy as np | 25 | import numpy as np |
| 26 | + | ||
| 26 | INF = 1e8 | 27 | INF = 1e8 |
| 27 | 28 | ||
| 29 | + | ||
| 28 | def center_of_mass(bitmasks): | 30 | def center_of_mass(bitmasks): |
| 29 | _, h, w = bitmasks.size() | 31 | _, h, w = bitmasks.size() |
| 30 | ys = torch.arange(0, h, dtype=torch.float32, device=bitmasks.device) | 32 | ys = torch.arange(0, h, dtype=torch.float32, device=bitmasks.device) |
| @@ -37,6 +39,7 @@ def center_of_mass(bitmasks): | |||
| 37 | center_y = m01 / m00 | 39 | center_y = m01 / m00 |
| 38 | return center_x, center_y | 40 | return center_x, center_y |
| 39 | 41 | ||
| 42 | + | ||
| 40 | def points_nms(heat, kernel=2): | 43 | def points_nms(heat, kernel=2): |
| 41 | # kernel must be 2 | 44 | # kernel must be 2 |
| 42 | hmax = nn.functional.max_pool2d( | 45 | hmax = nn.functional.max_pool2d( |
| @@ -44,6 +47,7 @@ def points_nms(heat, kernel=2): | |||
| 44 | keep = (hmax[:, :, :-1, :-1] == heat).float() | 47 | keep = (hmax[:, :, :-1, :-1] == heat).float() |
| 45 | return heat * keep | 48 | return heat * keep |
| 46 | 49 | ||
| 50 | + | ||
| 47 | def dice_loss(input, target): | 51 | def dice_loss(input, target): |
| 48 | input = input.contiguous().view(input.size()[0], -1) | 52 | input = input.contiguous().view(input.size()[0], -1) |
| 49 | target = target.contiguous().view(target.size()[0], -1).float() | 53 | target = target.contiguous().view(target.size()[0], -1).float() |
| @@ -52,7 +56,8 @@ def dice_loss(input, target): | |||
| 52 | b = torch.sum(input * input, 1) + 0.001 | 56 | b = torch.sum(input * input, 1) + 0.001 |
| 53 | c = torch.sum(target * target, 1) + 0.001 | 57 | c = torch.sum(target * target, 1) + 0.001 |
| 54 | d = (2 * a) / (b + c) | 58 | d = (2 * a) / (b + c) |
| 55 | - return 1-d | 59 | + return 1 - d |
| 60 | + | ||
| 56 | 61 | ||
| 57 | 62 | ||
| 58 | class SOLOv2Head(nn.Module): | 63 | class SOLOv2Head(nn.Module): |
| @@ -150,8 +155,8 @@ class SOLOv2Head(nn.Module): | |||
| 150 | featmap_sizes = [featmap.size()[-2:] for featmap in new_feats] | 155 | featmap_sizes = [featmap.size()[-2:] for featmap in new_feats] |
| 151 | upsampled_size = (featmap_sizes[0][0] * 2, featmap_sizes[0][1] * 2) | 156 | upsampled_size = (featmap_sizes[0][0] * 2, featmap_sizes[0][1] * 2) |
| 152 | cate_pred, kernel_pred = multi_apply(self.forward_single, new_feats, | 157 | cate_pred, kernel_pred = multi_apply(self.forward_single, new_feats, |
| 153 | - list(range(len(self.seg_num_grids))), | 158 | + list(range(len(self.seg_num_grids))), |
| 154 | - eval=eval, upsampled_size=upsampled_size) | 159 | + eval=eval, upsampled_size=upsampled_size) |
| 155 | return cate_pred, kernel_pred | 160 | return cate_pred, kernel_pred |
| 156 | 161 | ||
| 157 | def split_feats(self, feats): | 162 | def split_feats(self, feats): |
| @@ -172,7 +177,7 @@ class SOLOv2Head(nn.Module): | |||
| 172 | x = x.expand([ins_kernel_feat.shape[0], 1, -1, -1]) | 177 | x = x.expand([ins_kernel_feat.shape[0], 1, -1, -1]) |
| 173 | coord_feat = torch.cat([x, y], 1) | 178 | coord_feat = torch.cat([x, y], 1) |
| 174 | ins_kernel_feat = torch.cat([ins_kernel_feat, coord_feat], 1) | 179 | ins_kernel_feat = torch.cat([ins_kernel_feat, coord_feat], 1) |
| 175 | - | 180 | + |
| 176 | # kernel branch | 181 | # kernel branch |
| 177 | kernel_feat = ins_kernel_feat | 182 | kernel_feat = ins_kernel_feat |
| 178 | seg_num_grid = self.seg_num_grids[idx] | 183 | seg_num_grid = self.seg_num_grids[idx] |
| @@ -195,7 +200,7 @@ class SOLOv2Head(nn.Module): | |||
| 195 | return cate_pred, kernel_pred | 200 | return cate_pred, kernel_pred |
| 196 | 201 | ||
| 197 | def loss(self, | 202 | def loss(self, |
| 198 | - cate_preds, | 203 | + cate_preds, |
| 199 | kernel_preds, | 204 | kernel_preds, |
| 200 | ins_pred, | 205 | ins_pred, |
| 201 | gt_bbox_list, | 206 | gt_bbox_list, |
| @@ -203,7 +208,7 @@ class SOLOv2Head(nn.Module): | |||
| 203 | gt_mask_list, | 208 | gt_mask_list, |
| 204 | img_metas, | 209 | img_metas, |
| 205 | cfg, | 210 | cfg, |
| 206 | - gt_bboxes_ignore=None): | 211 | + gt_bboxes_ignore=None): |
| 207 | 212 | ||
| 208 | # diff | 213 | # diff |
| 209 | MAX_LEN = 90 | 214 | MAX_LEN = 90 |
| @@ -258,9 +263,9 @@ class SOLOv2Head(nn.Module): | |||
| 258 | continue | 263 | continue |
| 259 | cur_ins_pred = ins_pred[idx, ...] # this img‘s pred | 264 | cur_ins_pred = ins_pred[idx, ...] # this img‘s pred |
| 260 | H, W = cur_ins_pred.shape[-2:] | 265 | H, W = cur_ins_pred.shape[-2:] |
| 261 | - N, I = kernel_pred.shape | 266 | + N, I = kernel_pred.shape |
| 262 | cur_ins_pred = cur_ins_pred.unsqueeze(0) # [1, c, msk_pre_h, msk_pre_w] c = N | 267 | cur_ins_pred = cur_ins_pred.unsqueeze(0) # [1, c, msk_pre_h, msk_pre_w] c = N |
| 263 | - kernel_pred = kernel_pred.permute(1, 0).view(I, -1, 1, 1) | 268 | + kernel_pred = kernel_pred.permute(1, 0).view(I, -1, 1, 1) |
| 264 | cur_ins_pred = F.conv2d(cur_ins_pred, kernel_pred, stride=1).view(-1, H, W) # (n, msk_pre_h, msk_pre_w) | 269 | cur_ins_pred = F.conv2d(cur_ins_pred, kernel_pred, stride=1).view(-1, H, W) # (n, msk_pre_h, msk_pre_w) |
| 265 | b_mask_pred.append(cur_ins_pred) | 270 | b_mask_pred.append(cur_ins_pred) |
| 266 | if len(b_mask_pred) == 0: | 271 | if len(b_mask_pred) == 0: |
| @@ -420,7 +425,7 @@ class SOLOv2Head(nn.Module): | |||
| 420 | seg_pred_list = seg_pred[img_id, ...].unsqueeze(0).float().cpu() | 425 | seg_pred_list = seg_pred[img_id, ...].unsqueeze(0).float().cpu() |
| 421 | kernel_pred_list = [ | 426 | kernel_pred_list = [ |
| 422 | kernel_preds[i][img_id].permute(1, 2, 0).view(-1, self.kernel_out_channels).detach() | 427 | kernel_preds[i][img_id].permute(1, 2, 0).view(-1, self.kernel_out_channels).detach() |
| 423 | - for i in range(num_levels) | 428 | + for i in range(num_levels) |
| 424 | ] | 429 | ] |
| 425 | img_shape = img_metas[img_id]['img_shape'] | 430 | img_shape = img_metas[img_id]['img_shape'] |
| 426 | scale_factor = img_metas[img_id]['scale_factor'] | 431 | scale_factor = img_metas[img_id]['scale_factor'] |
| @@ -467,13 +472,18 @@ class SOLOv2Head(nn.Module): | |||
| 467 | n_stage = len(self.seg_num_grids) | 472 | n_stage = len(self.seg_num_grids) |
| 468 | strides[:size_trans[0]] *= self.strides[0] | 473 | strides[:size_trans[0]] *= self.strides[0] |
| 469 | for ind_ in range(1, n_stage): | 474 | for ind_ in range(1, n_stage): |
| 470 | - strides[size_trans[ind_-1]:size_trans[ind_]] *= self.strides[ind_] | 475 | + strides[size_trans[ind_ - 1]:size_trans[ind_]] *= self.strides[ind_] |
| 471 | strides = strides[inds[:, 0]] | 476 | strides = strides[inds[:, 0]] |
| 472 | 477 | ||
| 473 | # mask encoding. | 478 | # mask encoding. |
| 474 | I, N = kernel_preds.shape | 479 | I, N = kernel_preds.shape |
| 475 | kernel_preds = kernel_preds.view(I, N, 1, 1) | 480 | kernel_preds = kernel_preds.view(I, N, 1, 1) |
| 476 | - seg_preds = F.conv2d(seg_preds, kernel_preds, stride=1).squeeze(0).sigmoid() | 481 | + new_I = ((I // 100) + 1) * 100 |
| 482 | + | ||
| 483 | + new_kernel_preds = torch.zeros(new_I, N, 1, 1) | ||
| 484 | + new_kernel_preds[:I] = kernel_preds | ||
| 485 | + seg_preds = F.conv2d(seg_preds.npu().half(), new_kernel_preds.npu().half(), stride=1).squeeze( | ||
| 486 | + 0).sigmoid().cpu().float()[:I, :, :] | ||
| 477 | # mask. | 487 | # mask. |
| 478 | seg_masks = seg_preds > cfg.mask_thr | 488 | seg_masks = seg_preds > cfg.mask_thr |
| 479 | sum_masks = seg_masks.sum((1, 2)).float() | 489 | sum_masks = seg_masks.sum((1, 2)).float() |
| @@ -505,7 +515,7 @@ class SOLOv2Head(nn.Module): | |||
| 505 | 515 | ||
| 506 | # Matrix NMS | 516 | # Matrix NMS |
| 507 | cate_scores = matrix_nms(seg_masks, cate_labels, cate_scores, | 517 | cate_scores = matrix_nms(seg_masks, cate_labels, cate_scores, |
| 508 | - kernel=cfg.kernel,sigma=cfg.sigma, sum_masks=sum_masks) | 518 | + kernel=cfg.kernel, sigma=cfg.sigma, sum_masks=sum_masks) |
| 509 | 519 | ||
| 510 | # filter. | 520 | # filter. |
| 511 | keep = cate_scores >= cfg.update_thr | 521 | keep = cate_scores >= cfg.update_thr |
| @@ -524,10 +534,10 @@ class SOLOv2Head(nn.Module): | |||
| 524 | cate_labels = cate_labels[sort_inds] | 534 | cate_labels = cate_labels[sort_inds] |
| 525 | 535 | ||
| 526 | seg_preds = F.interpolate(seg_preds.unsqueeze(0), | 536 | seg_preds = F.interpolate(seg_preds.unsqueeze(0), |
| 527 | - size=upsampled_size_out, | 537 | + size=upsampled_size_out, |
| 528 | - mode='bilinear')[:, :, :h, :w] | 538 | + mode='bilinear')[:, :, :h, :w] |
| 529 | seg_masks = F.interpolate(seg_preds, | 539 | seg_masks = F.interpolate(seg_preds, |
| 530 | - size=ori_shape[:2], | 540 | + size=ori_shape[:2], |
| 531 | - mode='bilinear').squeeze(0) | 541 | + mode='bilinear').squeeze(0) |
| 532 | seg_masks = seg_masks > cfg.mask_thr | 542 | seg_masks = seg_masks > cfg.mask_thr |
| 533 | return seg_masks, cate_labels, cate_scores | 543 | return seg_masks, cate_labels, cate_scores |
| @@ -25,6 +25,7 @@ from ..utils import bias_init_with_prob, ConvModule | |||
| 25 | 25 | ||
| 26 | INF = 1e8 | 26 | INF = 1e8 |
| 27 | 27 | ||
| 28 | + | ||
| 28 | def center_of_mass(bitmasks): | 29 | def center_of_mass(bitmasks): |
| 29 | _, h, w = bitmasks.size() | 30 | _, h, w = bitmasks.size() |
| 30 | ys = torch.arange(0, h, dtype=torch.float32, device=bitmasks.device) | 31 | ys = torch.arange(0, h, dtype=torch.float32, device=bitmasks.device) |
| @@ -37,6 +38,7 @@ def center_of_mass(bitmasks): | |||
| 37 | center_y = m01 / m00 | 38 | center_y = m01 / m00 |
| 38 | return center_x, center_y | 39 | return center_x, center_y |
| 39 | 40 | ||
| 41 | + | ||
| 40 | def points_nms(heat, kernel=2): | 42 | def points_nms(heat, kernel=2): |
| 41 | # kernel must be 2 | 43 | # kernel must be 2 |
| 42 | hmax = nn.functional.max_pool2d( | 44 | hmax = nn.functional.max_pool2d( |
| @@ -44,6 +46,7 @@ def points_nms(heat, kernel=2): | |||
| 44 | keep = (hmax[:, :, :-1, :-1] == heat).float() | 46 | keep = (hmax[:, :, :-1, :-1] == heat).float() |
| 45 | return heat * keep | 47 | return heat * keep |
| 46 | 48 | ||
| 49 | + | ||
| 47 | def dice_loss(input, target): | 50 | def dice_loss(input, target): |
| 48 | input = input.contiguous().view(input.size()[0], -1) | 51 | input = input.contiguous().view(input.size()[0], -1) |
| 49 | target = target.contiguous().view(target.size()[0], -1).float() | 52 | target = target.contiguous().view(target.size()[0], -1).float() |
| @@ -52,7 +55,8 @@ def dice_loss(input, target): | |||
| 52 | b = torch.sum(input * input, 1) + 0.001 | 55 | b = torch.sum(input * input, 1) + 0.001 |
| 53 | c = torch.sum(target * target, 1) + 0.001 | 56 | c = torch.sum(target * target, 1) + 0.001 |
| 54 | d = (2 * a) / (b + c) | 57 | d = (2 * a) / (b + c) |
| 55 | - return 1-d | 58 | + return 1 - d |
| 59 | + | ||
| 56 | 60 | ||
| 57 | 61 | ||
| 58 | class SOLOv2LightHead(nn.Module): | 62 | class SOLOv2LightHead(nn.Module): |
| @@ -150,8 +154,8 @@ class SOLOv2LightHead(nn.Module): | |||
| 150 | featmap_sizes = [featmap.size()[-2:] for featmap in new_feats] | 154 | featmap_sizes = [featmap.size()[-2:] for featmap in new_feats] |
| 151 | upsampled_size = (featmap_sizes[0][0] * 2, featmap_sizes[0][1] * 2) | 155 | upsampled_size = (featmap_sizes[0][0] * 2, featmap_sizes[0][1] * 2) |
| 152 | cate_pred, kernel_pred = multi_apply(self.forward_single, new_feats, | 156 | cate_pred, kernel_pred = multi_apply(self.forward_single, new_feats, |
| 153 | - list(range(len(self.seg_num_grids))), | 157 | + list(range(len(self.seg_num_grids))), |
| 154 | - eval=eval, upsampled_size=upsampled_size) | 158 | + eval=eval, upsampled_size=upsampled_size) |
| 155 | return cate_pred, kernel_pred | 159 | return cate_pred, kernel_pred |
| 156 | 160 | ||
| 157 | def split_feats(self, feats): | 161 | def split_feats(self, feats): |
| @@ -172,7 +176,7 @@ class SOLOv2LightHead(nn.Module): | |||
| 172 | x = x.expand([ins_kernel_feat.shape[0], 1, -1, -1]) | 176 | x = x.expand([ins_kernel_feat.shape[0], 1, -1, -1]) |
| 173 | coord_feat = torch.cat([x, y], 1) | 177 | coord_feat = torch.cat([x, y], 1) |
| 174 | ins_kernel_feat = torch.cat([ins_kernel_feat, coord_feat], 1) | 178 | ins_kernel_feat = torch.cat([ins_kernel_feat, coord_feat], 1) |
| 175 | - | 179 | + |
| 176 | # kernel branch | 180 | # kernel branch |
| 177 | kernel_feat = ins_kernel_feat | 181 | kernel_feat = ins_kernel_feat |
| 178 | seg_num_grid = self.seg_num_grids[idx] | 182 | seg_num_grid = self.seg_num_grids[idx] |
| @@ -210,7 +214,7 @@ class SOLOv2LightHead(nn.Module): | |||
| 210 | self.solov2_target_single, | 214 | self.solov2_target_single, |
| 211 | gt_bbox_list, | 215 | gt_bbox_list, |
| 212 | gt_label_list, | 216 | gt_label_list, |
| 213 | - gt_mask_list, | 217 | + gt_mask_list, |
| 214 | mask_feat_size=mask_feat_size) | 218 | mask_feat_size=mask_feat_size) |
| 215 | 219 | ||
| 216 | # ins | 220 | # ins |
| @@ -283,10 +287,10 @@ class SOLOv2LightHead(nn.Module): | |||
| 283 | loss_cate=loss_cate) | 287 | loss_cate=loss_cate) |
| 284 | 288 | ||
| 285 | def solov2_target_single(self, | 289 | def solov2_target_single(self, |
| 286 | - gt_bboxes_raw, | 290 | + gt_bboxes_raw, |
| 287 | - gt_labels_raw, | 291 | + gt_labels_raw, |
| 288 | - gt_masks_raw, | 292 | + gt_masks_raw, |
| 289 | - mask_feat_size): | 293 | + mask_feat_size): |
| 290 | 294 | ||
| 291 | device = gt_labels_raw[0].device | 295 | device = gt_labels_raw[0].device |
| 292 | 296 | ||
| @@ -328,9 +332,12 @@ class SOLOv2LightHead(nn.Module): | |||
| 328 | center_ws, center_hs = center_of_mass(gt_masks_pt) | 332 | center_ws, center_hs = center_of_mass(gt_masks_pt) |
| 329 | valid_mask_flags = gt_masks_pt.sum(dim=-1).sum(dim=-1) > 0 | 333 | valid_mask_flags = gt_masks_pt.sum(dim=-1).sum(dim=-1) > 0 |
| 330 | output_stride = 4 | 334 | output_stride = 4 |
| 331 | - for seg_mask, gt_label, half_h, half_w, center_h, center_w, valid_mask_flag in zip(gt_masks, gt_labels, half_hs, half_ws, center_hs, center_ws, valid_mask_flags): | 335 | + for seg_mask, gt_label, half_h, half_w, center_h, center_w, valid_mask_flag in zip(gt_masks, gt_labels, |
| 336 | + half_hs, half_ws, | ||
| 337 | + center_hs, center_ws, | ||
| 338 | + valid_mask_flags): | ||
| 332 | if not valid_mask_flag: | 339 | if not valid_mask_flag: |
| 333 | - continue | 340 | + continue |
| 334 | upsampled_size = (mask_feat_size[0] * 4, mask_feat_size[1] * 4) | 341 | upsampled_size = (mask_feat_size[0] * 4, mask_feat_size[1] * 4) |
| 335 | coord_w = int((center_w / upsampled_size[1]) // (1. / num_grid)) | 342 | coord_w = int((center_w / upsampled_size[1]) // (1. / num_grid)) |
| 336 | coord_h = int((center_h / upsampled_size[0]) // (1. / num_grid)) | 343 | coord_h = int((center_h / upsampled_size[0]) // (1. / num_grid)) |
| @@ -341,16 +348,16 @@ class SOLOv2LightHead(nn.Module): | |||
| 341 | left_box = max(0, int(((center_w - half_w) / upsampled_size[1]) // (1. / num_grid))) | 348 | left_box = max(0, int(((center_w - half_w) / upsampled_size[1]) // (1. / num_grid))) |
| 342 | right_box = min(num_grid - 1, int(((center_w + half_w) / upsampled_size[1]) // (1. / num_grid))) | 349 | right_box = min(num_grid - 1, int(((center_w + half_w) / upsampled_size[1]) // (1. / num_grid))) |
| 343 | 350 | ||
| 344 | - top = max(top_box, coord_h-1) | 351 | + top = max(top_box, coord_h - 1) |
| 345 | - down = min(down_box, coord_h+1) | 352 | + down = min(down_box, coord_h + 1) |
| 346 | - left = max(coord_w-1, left_box) | 353 | + left = max(coord_w - 1, left_box) |
| 347 | - right = min(right_box, coord_w+1) | 354 | + right = min(right_box, coord_w + 1) |
| 348 | 355 | ||
| 349 | - cate_label[top:(down+1), left:(right+1)] = gt_label | 356 | + cate_label[top:(down + 1), left:(right + 1)] = gt_label |
| 350 | seg_mask = mmcv.imrescale(seg_mask, scale=1. / output_stride) | 357 | seg_mask = mmcv.imrescale(seg_mask, scale=1. / output_stride) |
| 351 | seg_mask = torch.from_numpy(seg_mask).to(device=device) | 358 | seg_mask = torch.from_numpy(seg_mask).to(device=device) |
| 352 | - for i in range(top, down+1): | 359 | + for i in range(top, down + 1): |
| 353 | - for j in range(left, right+1): | 360 | + for j in range(left, right + 1): |
| 354 | label = int(i * num_grid + j) | 361 | label = int(i * num_grid + j) |
| 355 | 362 | ||
| 356 | cur_ins_label = torch.zeros([mask_feat_size[0], mask_feat_size[1]], dtype=torch.uint8, | 363 | cur_ins_label = torch.zeros([mask_feat_size[0], mask_feat_size[1]], dtype=torch.uint8, |
| @@ -381,7 +388,7 @@ class SOLOv2LightHead(nn.Module): | |||
| 381 | seg_pred_list = seg_pred[img_id, ...].unsqueeze(0) | 388 | seg_pred_list = seg_pred[img_id, ...].unsqueeze(0) |
| 382 | kernel_pred_list = [ | 389 | kernel_pred_list = [ |
| 383 | kernel_preds[i][img_id].permute(1, 2, 0).view(-1, self.kernel_out_channels).detach() | 390 | kernel_preds[i][img_id].permute(1, 2, 0).view(-1, self.kernel_out_channels).detach() |
| 384 | - for i in range(num_levels) | 391 | + for i in range(num_levels) |
| 385 | ] | 392 | ] |
| 386 | img_shape = img_metas[img_id]['img_shape'] | 393 | img_shape = img_metas[img_id]['img_shape'] |
| 387 | scale_factor = img_metas[img_id]['scale_factor'] | 394 | scale_factor = img_metas[img_id]['scale_factor'] |
| @@ -430,7 +437,7 @@ class SOLOv2LightHead(nn.Module): | |||
| 430 | n_stage = len(self.seg_num_grids) | 437 | n_stage = len(self.seg_num_grids) |
| 431 | strides[:size_trans[0]] *= self.strides[0] | 438 | strides[:size_trans[0]] *= self.strides[0] |
| 432 | for ind_ in range(1, n_stage): | 439 | for ind_ in range(1, n_stage): |
| 433 | - strides[size_trans[ind_-1]:size_trans[ind_]] *= self.strides[ind_] | 440 | + strides[size_trans[ind_ - 1]:size_trans[ind_]] *= self.strides[ind_] |
| 434 | strides = strides[inds[:, 0]] | 441 | strides = strides[inds[:, 0]] |
| 435 | 442 | ||
| 436 | # mask encoding. | 443 | # mask encoding. |
| @@ -468,7 +475,7 @@ class SOLOv2LightHead(nn.Module): | |||
| 468 | 475 | ||
| 469 | # Matrix NMS | 476 | # Matrix NMS |
| 470 | cate_scores = matrix_nms(seg_masks, cate_labels, cate_scores, | 477 | cate_scores = matrix_nms(seg_masks, cate_labels, cate_scores, |
| 471 | - kernel=cfg.kernel,sigma=cfg.sigma, sum_masks=sum_masks) | 478 | + kernel=cfg.kernel, sigma=cfg.sigma, sum_masks=sum_masks) |
| 472 | 479 | ||
| 473 | # filter. | 480 | # filter. |
| 474 | keep = cate_scores >= cfg.update_thr | 481 | keep = cate_scores >= cfg.update_thr |
| @@ -487,10 +494,10 @@ class SOLOv2LightHead(nn.Module): | |||
| 487 | cate_labels = cate_labels[sort_inds] | 494 | cate_labels = cate_labels[sort_inds] |
| 488 | 495 | ||
| 489 | seg_preds = F.interpolate(seg_preds.unsqueeze(0), | 496 | seg_preds = F.interpolate(seg_preds.unsqueeze(0), |
| 490 | - size=upsampled_size_out, | 497 | + size=upsampled_size_out, |
| 491 | - mode='bilinear')[:, :, :h, :w] | 498 | + mode='bilinear')[:, :, :h, :w] |
| 492 | seg_masks = F.interpolate(seg_preds, | 499 | seg_masks = F.interpolate(seg_preds, |
| 493 | - size=ori_shape[:2], | 500 | + size=ori_shape[:2], |
| 494 | - mode='bilinear').squeeze(0) | 501 | + mode='bilinear').squeeze(0) |
| 495 | seg_masks = seg_masks > cfg.mask_thr | 502 | seg_masks = seg_masks > cfg.mask_thr |
| 496 | return seg_masks, cate_labels, cate_scores | 503 | return seg_masks, cate_labels, cate_scores |
| @@ -148,7 +148,7 @@ class HRModule(nn.Module): | |||
| 148 | bias=False), | 148 | bias=False), |
| 149 | build_norm_layer(self.norm_cfg, in_channels[i])[1], | 149 | build_norm_layer(self.norm_cfg, in_channels[i])[1], |
| 150 | nn.Upsample( | 150 | nn.Upsample( |
| 151 | - scale_factor=2**(j - i), mode='nearest'))) | 151 | + scale_factor=2 ** (j - i), mode='nearest'))) |
| 152 | elif j == i: | 152 | elif j == i: |
| 153 | fuse_layer.append(None) | 153 | fuse_layer.append(None) |
| 154 | else: | 154 | else: |
| @@ -424,7 +424,7 @@ class ResNet(nn.Module): | |||
| 424 | dilation = dilations[i] | 424 | dilation = dilations[i] |
| 425 | dcn = self.dcn if self.stage_with_dcn[i] else None | 425 | dcn = self.dcn if self.stage_with_dcn[i] else None |
| 426 | gcb = self.gcb if self.stage_with_gcb[i] else None | 426 | gcb = self.gcb if self.stage_with_gcb[i] else None |
| 427 | - planes = 64 * 2**i | 427 | + planes = 64 * 2 ** i |
| 428 | res_layer = make_res_layer( | 428 | res_layer = make_res_layer( |
| 429 | self.block, | 429 | self.block, |
| 430 | self.inplanes, | 430 | self.inplanes, |
| @@ -447,8 +447,8 @@ class ResNet(nn.Module): | |||
| 447 | 447 | ||
| 448 | self._freeze_stages() | 448 | self._freeze_stages() |
| 449 | 449 | ||
| 450 | - self.feat_dim = self.block.expansion * 64 * 2**( | 450 | + self.feat_dim = self.block.expansion * 64 * 2 ** ( |
| 451 | - len(self.stage_blocks) - 1) | 451 | + len(self.stage_blocks) - 1) |
| 452 | 452 | ||
| 453 | 453 | ||
| 454 | def norm1(self): | 454 | def norm1(self): |
| @@ -212,7 +212,7 @@ class ResNeXt(ResNet): | |||
| 212 | dilation = self.dilations[i] | 212 | dilation = self.dilations[i] |
| 213 | dcn = self.dcn if self.stage_with_dcn[i] else None | 213 | dcn = self.dcn if self.stage_with_dcn[i] else None |
| 214 | gcb = self.gcb if self.stage_with_gcb[i] else None | 214 | gcb = self.gcb if self.stage_with_gcb[i] else None |
| 215 | - planes = 64 * 2**i | 215 | + planes = 64 * 2 ** i |
| 216 | res_layer = make_res_layer( | 216 | res_layer = make_res_layer( |
| 217 | self.block, | 217 | self.block, |
| 218 | self.inplanes, | 218 | self.inplanes, |
| @@ -186,7 +186,7 @@ class BBoxHead(nn.Module): | |||
| 186 | 186 | ||
| 187 | return det_bboxes, det_labels | 187 | return det_bboxes, det_labels |
| 188 | 188 | ||
| 189 | - @force_fp32(apply_to=('bbox_preds', )) | 189 | + @force_fp32(apply_to=('bbox_preds',)) |
| 190 | def refine_bboxes(self, rois, labels, bbox_preds, pos_is_gts, img_metas): | 190 | def refine_bboxes(self, rois, labels, bbox_preds, pos_is_gts, img_metas): |
| 191 | """Refine bboxes during training. | 191 | """Refine bboxes during training. |
| 192 | 192 | ||
| @@ -264,7 +264,7 @@ class BBoxHead(nn.Module): | |||
| 264 | 264 | ||
| 265 | return bboxes_list | 265 | return bboxes_list |
| 266 | 266 | ||
| 267 | - @force_fp32(apply_to=('bbox_pred', )) | 267 | + @force_fp32(apply_to=('bbox_pred',)) |
| 268 | def regress_by_class(self, rois, label, bbox_pred, img_meta): | 268 | def regress_by_class(self, rois, label, bbox_pred, img_meta): |
| 269 | """Regress the bbox for the predicted class. Used in Cascade R-CNN. | 269 | """Regress the bbox for the predicted class. Used in Cascade R-CNN. |
| 270 | 270 | ||
| @@ -91,7 +91,7 @@ class ConvFCBBoxHead(BBoxHead): | |||
| 91 | self.fc_cls = nn.Linear(self.cls_last_dim, self.num_classes) | 91 | self.fc_cls = nn.Linear(self.cls_last_dim, self.num_classes) |
| 92 | if self.with_reg: | 92 | if self.with_reg: |
| 93 | out_dim_reg = (4 if self.reg_class_agnostic else 4 * | 93 | out_dim_reg = (4 if self.reg_class_agnostic else 4 * |
| 94 | - self.num_classes) | 94 | + self.num_classes) |
| 95 | self.fc_reg = nn.Linear(self.reg_last_dim, out_dim_reg) | 95 | self.fc_reg = nn.Linear(self.reg_last_dim, out_dim_reg) |
| 96 | 96 | ||
| 97 | def _add_conv_fc_branch(self, | 97 | def _add_conv_fc_branch(self, |
| @@ -125,7 +125,7 @@ class ConvFCBBoxHead(BBoxHead): | |||
| 125 | # for shared branch, only consider self.with_avg_pool | 125 | # for shared branch, only consider self.with_avg_pool |
| 126 | # for separated branches, also consider self.num_shared_fcs | 126 | # for separated branches, also consider self.num_shared_fcs |
| 127 | if (is_shared | 127 | if (is_shared |
| 128 | - or self.num_shared_fcs == 0) and not self.with_avg_pool: | 128 | + or self.num_shared_fcs == 0) and not self.with_avg_pool: |
| 129 | last_layer_dim *= self.roi_feat_area | 129 | last_layer_dim *= self.roi_feat_area |
| 130 | for i in range(num_branch_fcs): | 130 | for i in range(num_branch_fcs): |
| 131 | fc_in_channels = ( | 131 | fc_in_channels = ( |
| @@ -37,7 +37,7 @@ class BaseDetector(nn.Module, metaclass=ABCMeta): | |||
| 37 | 37 | ||
| 38 | def with_mask_feat_head(self): | 38 | def with_mask_feat_head(self): |
| 39 | return hasattr(self, 'mask_feat_head') and \ | 39 | return hasattr(self, 'mask_feat_head') and \ |
| 40 | - self.mask_feat_head is not None | 40 | + self.mask_feat_head is not None |
| 41 | 41 | ||
| 42 | 42 | ||
| 43 | def with_shared_head(self): | 43 | def with_shared_head(self): |
| @@ -147,7 +147,7 @@ class BaseDetector(nn.Module, metaclass=ABCMeta): | |||
| 147 | else: | 147 | else: |
| 148 | return self.aug_test(imgs, img_metas, **kwargs) | 148 | return self.aug_test(imgs, img_metas, **kwargs) |
| 149 | 149 | ||
| 150 | - @auto_fp16(apply_to=('img', )) | 150 | + @auto_fp16(apply_to=('img',)) |
| 151 | def forward(self, img, img_meta, return_loss=True, **kwargs): | 151 | def forward(self, img, img_meta, return_loss=True, **kwargs): |
| 152 | """ | 152 | """ |
| 153 | Calls either forward_train or forward_test depending on whether | 153 | Calls either forward_train or forward_test depending on whether |
| @@ -139,7 +139,7 @@ class CascadeRCNN(BaseDetector, RPNTestMixin): | |||
| 139 | # rpn | 139 | # rpn |
| 140 | if self.with_rpn: | 140 | if self.with_rpn: |
| 141 | rpn_outs = self.rpn_head(x) | 141 | rpn_outs = self.rpn_head(x) |
| 142 | - outs = outs + (rpn_outs, ) | 142 | + outs = outs + (rpn_outs,) |
| 143 | proposals = torch.randn(1000, 4).cuda() | 143 | proposals = torch.randn(1000, 4).cuda() |
| 144 | # bbox heads | 144 | # bbox heads |
| 145 | rois = bbox2roi([proposals]) | 145 | rois = bbox2roi([proposals]) |
| @@ -160,7 +160,7 @@ class CascadeRCNN(BaseDetector, RPNTestMixin): | |||
| 160 | if self.with_shared_head: | 160 | if self.with_shared_head: |
| 161 | mask_feats = self.shared_head(mask_feats) | 161 | mask_feats = self.shared_head(mask_feats) |
| 162 | mask_pred = self.mask_head[i](mask_feats) | 162 | mask_pred = self.mask_head[i](mask_feats) |
| 163 | - outs = outs + (mask_pred, ) | 163 | + outs = outs + (mask_pred,) |
| 164 | return outs | 164 | return outs |
| 165 | 165 | ||
| 166 | def forward_train(self, | 166 | def forward_train(self, |
| @@ -33,7 +33,7 @@ class DoubleHeadRCNN(TwoStageDetector): | |||
| 33 | # rpn | 33 | # rpn |
| 34 | if self.with_rpn: | 34 | if self.with_rpn: |
| 35 | rpn_outs = self.rpn_head(x) | 35 | rpn_outs = self.rpn_head(x) |
| 36 | - outs = outs + (rpn_outs, ) | 36 | + outs = outs + (rpn_outs,) |
| 37 | proposals = torch.randn(1000, 4).cuda() | 37 | proposals = torch.randn(1000, 4).cuda() |
| 38 | # bbox head | 38 | # bbox head |
| 39 | rois = bbox2roi([proposals]) | 39 | rois = bbox2roi([proposals]) |
| @@ -101,7 +101,7 @@ class GridRCNN(TwoStageDetector): | |||
| 101 | # rpn | 101 | # rpn |
| 102 | if self.with_rpn: | 102 | if self.with_rpn: |
| 103 | rpn_outs = self.rpn_head(x) | 103 | rpn_outs = self.rpn_head(x) |
| 104 | - outs = outs + (rpn_outs, ) | 104 | + outs = outs + (rpn_outs,) |
| 105 | proposals = torch.randn(1000, 4).cuda() | 105 | proposals = torch.randn(1000, 4).cuda() |
| 106 | # bbox head | 106 | # bbox head |
| 107 | rois = bbox2roi([proposals]) | 107 | rois = bbox2roi([proposals]) |
| @@ -175,7 +175,7 @@ class HybridTaskCascade(CascadeRCNN): | |||
| 175 | # rpn | 175 | # rpn |
| 176 | if self.with_rpn: | 176 | if self.with_rpn: |
| 177 | rpn_outs = self.rpn_head(x) | 177 | rpn_outs = self.rpn_head(x) |
| 178 | - outs = outs + (rpn_outs, ) | 178 | + outs = outs + (rpn_outs,) |
| 179 | proposals = torch.randn(1000, 4).cuda() | 179 | proposals = torch.randn(1000, 4).cuda() |
| 180 | # semantic head | 180 | # semantic head |
| 181 | if self.with_semantic: | 181 | if self.with_semantic: |
| @@ -205,7 +205,7 @@ class HybridTaskCascade(CascadeRCNN): | |||
| 205 | mask_pred, last_feat = mask_head(mask_feats, last_feat) | 205 | mask_pred, last_feat = mask_head(mask_feats, last_feat) |
| 206 | else: | 206 | else: |
| 207 | mask_pred = mask_head(mask_feats) | 207 | mask_pred = mask_head(mask_feats) |
| 208 | - outs = outs + (mask_pred, ) | 208 | + outs = outs + (mask_pred,) |
| 209 | return outs | 209 | return outs |
| 210 | 210 | ||
| 211 | def forward_train(self, | 211 | def forward_train(self, |
| @@ -499,7 +499,7 @@ class HybridTaskCascade(CascadeRCNN): | |||
| 499 | mask_semantic_feat = self.semantic_roi_extractor( | 499 | mask_semantic_feat = self.semantic_roi_extractor( |
| 500 | [semantic_feat], mask_rois) | 500 | [semantic_feat], mask_rois) |
| 501 | if mask_semantic_feat.shape[-2:] != mask_feats.shape[ | 501 | if mask_semantic_feat.shape[-2:] != mask_feats.shape[ |
| 502 | - -2:]: | 502 | + -2:]: |
| 503 | mask_semantic_feat = F.adaptive_avg_pool2d( | 503 | mask_semantic_feat = F.adaptive_avg_pool2d( |
| 504 | mask_semantic_feat, mask_feats.shape[-2:]) | 504 | mask_semantic_feat, mask_feats.shape[-2:]) |
| 505 | mask_feats += mask_semantic_feat | 505 | mask_feats += mask_semantic_feat |
| @@ -91,7 +91,7 @@ class SingleStageInsDetector(BaseDetector): | |||
| 91 | if self.with_mask_feat_head: | 91 | if self.with_mask_feat_head: |
| 92 | mask_feat_pred = self.mask_feat_head( | 92 | mask_feat_pred = self.mask_feat_head( |
| 93 | x[self.mask_feat_head. | 93 | x[self.mask_feat_head. |
| 94 | - start_level:self.mask_feat_head.end_level + 1]) | 94 | + start_level:self.mask_feat_head.end_level + 1]) |
| 95 | loss_inputs = outs + (mask_feat_pred, gt_bboxes, gt_labels, gt_masks, img_metas, self.train_cfg) | 95 | loss_inputs = outs + (mask_feat_pred, gt_bboxes, gt_labels, gt_masks, img_metas, self.train_cfg) |
| 96 | else: | 96 | else: |
| 97 | loss_inputs = outs + (gt_bboxes, gt_labels, gt_masks, img_metas, self.train_cfg) | 97 | loss_inputs = outs + (gt_bboxes, gt_labels, gt_masks, img_metas, self.train_cfg) |
| @@ -102,19 +102,18 @@ class SingleStageInsDetector(BaseDetector): | |||
| 102 | def simple_test(self, img, img_meta, rescale=False): | 102 | def simple_test(self, img, img_meta, rescale=False): |
| 103 | # diff | 103 | # diff |
| 104 | img = img.npu() | 104 | img = img.npu() |
| 105 | - | ||
| 106 | x = self.extract_feat(img) | 105 | x = self.extract_feat(img) |
| 107 | outs = self.bbox_head(x, eval=True) | 106 | outs = self.bbox_head(x, eval=True) |
| 108 | if self.with_mask_feat_head: | 107 | if self.with_mask_feat_head: |
| 109 | mask_feat_pred = self.mask_feat_head( | 108 | mask_feat_pred = self.mask_feat_head( |
| 110 | x[self.mask_feat_head. | 109 | x[self.mask_feat_head. |
| 111 | - start_level:self.mask_feat_head.end_level + 1]) | 110 | + start_level:self.mask_feat_head.end_level + 1]) |
| 112 | seg_inputs = outs + (mask_feat_pred, img_meta, self.test_cfg, rescale) | 111 | seg_inputs = outs + (mask_feat_pred, img_meta, self.test_cfg, rescale) |
| 113 | else: | 112 | else: |
| 114 | seg_inputs = outs + (img_meta, self.test_cfg, rescale) | 113 | seg_inputs = outs + (img_meta, self.test_cfg, rescale) |
| 115 | 114 | ||
| 116 | seg_result = self.bbox_head.get_seg(*seg_inputs) | 115 | seg_result = self.bbox_head.get_seg(*seg_inputs) |
| 117 | - return seg_result | 116 | + return seg_result |
| 118 | 117 | ||
| 119 | def aug_test(self, imgs, img_metas, rescale=False): | 118 | def aug_test(self, imgs, img_metas, rescale=False): |
| 120 | raise NotImplementedError | 119 | raise NotImplementedError |
| @@ -28,4 +28,4 @@ class SOLOv2(SingleStageInsDetector): | |||
| 28 | test_cfg=None, | 28 | test_cfg=None, |
| 29 | pretrained=None): | 29 | pretrained=None): |
| 30 | super(SOLOv2, self).__init__(backbone, neck, bbox_head, mask_feat_head, train_cfg, | 30 | super(SOLOv2, self).__init__(backbone, neck, bbox_head, mask_feat_head, train_cfg, |
| 31 | - test_cfg, pretrained) | 31 | + test_cfg, pretrained) |
| @@ -27,9 +27,7 @@ if sys.version_info >= (3, 7): | |||
| 27 | 27 | ||
| 28 | 28 | ||
| 29 | class RPNTestMixin(object): | 29 | class RPNTestMixin(object): |
| 30 | - | ||
| 31 | if sys.version_info >= (3, 7): | 30 | if sys.version_info >= (3, 7): |
| 32 | - | ||
| 33 | async def async_test_rpn(self, x, img_meta, rpn_test_cfg): | 31 | async def async_test_rpn(self, x, img_meta, rpn_test_cfg): |
| 34 | sleep_interval = rpn_test_cfg.pop("async_sleep_interval", 0.025) | 32 | sleep_interval = rpn_test_cfg.pop("async_sleep_interval", 0.025) |
| 35 | async with completed( | 33 | async with completed( |
| @@ -72,7 +70,6 @@ class RPNTestMixin(object): | |||
| 72 | 70 | ||
| 73 | 71 | ||
| 74 | class BBoxTestMixin(object): | 72 | class BBoxTestMixin(object): |
| 75 | - | ||
| 76 | if sys.version_info >= (3, 7): | 73 | if sys.version_info >= (3, 7): |
| 77 | 74 | ||
| 78 | async def async_test_bboxes(self, | 75 | async def async_test_bboxes(self, |
| @@ -172,7 +169,6 @@ class BBoxTestMixin(object): | |||
| 172 | 169 | ||
| 173 | 170 | ||
| 174 | class MaskTestMixin(object): | 171 | class MaskTestMixin(object): |
| 175 | - | ||
| 176 | if sys.version_info >= (3, 7): | 172 | if sys.version_info >= (3, 7): |
| 177 | 173 | ||
| 178 | async def async_test_mask(self, | 174 | async def async_test_mask(self, |
| @@ -119,7 +119,7 @@ class TwoStageDetector(BaseDetector, RPNTestMixin, BBoxTestMixin, | |||
| 119 | # rpn | 119 | # rpn |
| 120 | if self.with_rpn: | 120 | if self.with_rpn: |
| 121 | rpn_outs = self.rpn_head(x) | 121 | rpn_outs = self.rpn_head(x) |
| 122 | - outs = outs + (rpn_outs, ) | 122 | + outs = outs + (rpn_outs,) |
| 123 | proposals = torch.randn(1000, 4).cuda() | 123 | proposals = torch.randn(1000, 4).cuda() |
| 124 | # bbox head | 124 | # bbox head |
| 125 | rois = bbox2roi([proposals]) | 125 | rois = bbox2roi([proposals]) |
| @@ -138,7 +138,7 @@ class TwoStageDetector(BaseDetector, RPNTestMixin, BBoxTestMixin, | |||
| 138 | if self.with_shared_head: | 138 | if self.with_shared_head: |
| 139 | mask_feats = self.shared_head(mask_feats) | 139 | mask_feats = self.shared_head(mask_feats) |
| 140 | mask_pred = self.mask_head(mask_feats) | 140 | mask_pred = self.mask_head(mask_feats) |
| 141 | - outs = outs + (mask_pred, ) | 141 | + outs = outs + (mask_pred,) |
| 142 | return outs | 142 | return outs |
| 143 | 143 | ||
| 144 | def forward_train(self, | 144 | def forward_train(self, |
| @@ -18,7 +18,7 @@ import torch.nn as nn | |||
| 18 | def accuracy(pred, target, topk=1): | 18 | def accuracy(pred, target, topk=1): |
| 19 | assert isinstance(topk, (int, tuple)) | 19 | assert isinstance(topk, (int, tuple)) |
| 20 | if isinstance(topk, int): | 20 | if isinstance(topk, int): |
| 21 | - topk = (topk, ) | 21 | + topk = (topk,) |
| 22 | return_single = True | 22 | return_single = True |
| 23 | else: | 23 | else: |
| 24 | return_single = False | 24 | return_single = False |
| @@ -37,7 +37,7 @@ def accuracy(pred, target, topk=1): | |||
| 37 | 37 | ||
| 38 | class Accuracy(nn.Module): | 38 | class Accuracy(nn.Module): |
| 39 | 39 | ||
| 40 | - def __init__(self, topk=(1, )): | 40 | + def __init__(self, topk=(1,)): |
| 41 | super().__init__() | 41 | super().__init__() |
| 42 | self.topk = topk | 42 | self.topk = topk |
| 43 | 43 | ||
| @@ -31,7 +31,7 @@ def balanced_l1_loss(pred, | |||
| 31 | assert pred.size() == target.size() and target.numel() > 0 | 31 | assert pred.size() == target.size() and target.numel() > 0 |
| 32 | 32 | ||
| 33 | diff = torch.abs(pred - target) | 33 | diff = torch.abs(pred - target) |
| 34 | - b = np.e**(gamma / alpha) - 1 | 34 | + b = np.e ** (gamma / alpha) - 1 |
| 35 | loss = torch.where( | 35 | loss = torch.where( |
| 36 | diff < beta, alpha / b * | 36 | diff < beta, alpha / b * |
| 37 | (b * diff + 1) * torch.log(b * diff / beta + 1) - alpha * diff, | 37 | (b * diff + 1) * torch.log(b * diff / beta + 1) - alpha * diff, |
| @@ -15,7 +15,7 @@ | |||
| 15 | import torch | 15 | import torch |
| 16 | import torch.nn as nn | 16 | import torch.nn as nn |
| 17 | import torch.nn.functional as F | 17 | import torch.nn.functional as F |
| 18 | -#from mmcv.ops import sigmoid_focal_loss as _sigmoid_focal_loss | 18 | +# from mmcv.ops import sigmoid_focal_loss as _sigmoid_focal_loss |
| 19 | 19 | ||
| 20 | from ..builder import LOSSES | 20 | from ..builder import LOSSES |
| 21 | from .utils import weight_reduce_loss | 21 | from .utils import weight_reduce_loss |
| @@ -55,6 +55,7 @@ def py_sigmoid_focal_loss(pred, | |||
| 55 | loss = weight_reduce_loss(loss, weight, reduction, avg_factor) | 55 | loss = weight_reduce_loss(loss, weight, reduction, avg_factor) |
| 56 | return loss | 56 | return loss |
| 57 | 57 | ||
| 58 | + | ||
| 58 | def sigmoid_focal_loss(pred, | 59 | def sigmoid_focal_loss(pred, |
| 59 | target, | 60 | target, |
| 60 | weight=None, | 61 | weight=None, |
| @@ -94,7 +94,7 @@ class GHMC(nn.Module): | |||
| 94 | if num_in_bin > 0: | 94 | if num_in_bin > 0: |
| 95 | if mmt > 0: | 95 | if mmt > 0: |
| 96 | self.acc_sum[i] = mmt * self.acc_sum[i] \ | 96 | self.acc_sum[i] = mmt * self.acc_sum[i] \ |
| 97 | - + (1 - mmt) * num_in_bin | 97 | + + (1 - mmt) * num_in_bin |
| 98 | weights[inds] = tot / self.acc_sum[i] | 98 | weights[inds] = tot / self.acc_sum[i] |
| 99 | else: | 99 | else: |
| 100 | weights[inds] = tot / num_in_bin | 100 | weights[inds] = tot / num_in_bin |
| @@ -173,7 +173,7 @@ class GHMR(nn.Module): | |||
| 173 | n += 1 | 173 | n += 1 |
| 174 | if mmt > 0: | 174 | if mmt > 0: |
| 175 | self.acc_sum[i] = mmt * self.acc_sum[i] \ | 175 | self.acc_sum[i] = mmt * self.acc_sum[i] \ |
| 176 | - + (1 - mmt) * num_in_bin | 176 | + + (1 - mmt) * num_in_bin |
| 177 | weights[inds] = tot / self.acc_sum[i] | 177 | weights[inds] = tot / self.acc_sum[i] |
| 178 | else: | 178 | else: |
| 179 | weights[inds] = tot / num_in_bin | 179 | weights[inds] = tot / num_in_bin |
| @@ -125,7 +125,7 @@ class FCNMaskHead(nn.Module): | |||
| 125 | gt_masks, rcnn_train_cfg) | 125 | gt_masks, rcnn_train_cfg) |
| 126 | return mask_targets | 126 | return mask_targets |
| 127 | 127 | ||
| 128 | - @force_fp32(apply_to=('mask_pred', )) | 128 | + @force_fp32(apply_to=('mask_pred',)) |
| 129 | def loss(self, mask_pred, mask_targets, labels): | 129 | def loss(self, mask_pred, mask_targets, labels): |
| 130 | loss = dict() | 130 | loss = dict() |
| 131 | if self.class_agnostic: | 131 | if self.class_agnostic: |
| @@ -112,7 +112,7 @@ class FusedSemanticHead(nn.Module): | |||
| 112 | x = self.conv_embedding(x) | 112 | x = self.conv_embedding(x) |
| 113 | return mask_pred, x | 113 | return mask_pred, x |
| 114 | 114 | ||
| 115 | - @force_fp32(apply_to=('mask_pred', )) | 115 | + @force_fp32(apply_to=('mask_pred',)) |
| 116 | def loss(self, mask_pred, labels): | 116 | def loss(self, mask_pred, labels): |
| 117 | labels = labels.squeeze(1).long() | 117 | labels = labels.squeeze(1).long() |
| 118 | loss_semantic_seg = self.criterion(mask_pred, labels) | 118 | loss_semantic_seg = self.criterion(mask_pred, labels) |
| @@ -267,7 +267,7 @@ class GridHead(nn.Module): | |||
| 267 | 1 - y_idx / (self.grid_size - 1))) | 267 | 1 - y_idx / (self.grid_size - 1))) |
| 268 | 268 | ||
| 269 | radius = rcnn_train_cfg.pos_radius | 269 | radius = rcnn_train_cfg.pos_radius |
| 270 | - radius2 = radius**2 | 270 | + radius2 = radius ** 2 |
| 271 | for i in range(num_rois): | 271 | for i in range(num_rois): |
| 272 | # ignore small bboxes | 272 | # ignore small bboxes |
| 273 | if (pos_bbox_ws[i] <= self.grid_size | 273 | if (pos_bbox_ws[i] <= self.grid_size |
| @@ -277,9 +277,9 @@ class GridHead(nn.Module): | |||
| 277 | for j in range(self.grid_points): | 277 | for j in range(self.grid_points): |
| 278 | factor_x, factor_y = factors[j] | 278 | factor_x, factor_y = factors[j] |
| 279 | gridpoint_x = factor_x * pos_gt_bboxes[i, 0] + ( | 279 | gridpoint_x = factor_x * pos_gt_bboxes[i, 0] + ( |
| 280 | - 1 - factor_x) * pos_gt_bboxes[i, 2] | 280 | + 1 - factor_x) * pos_gt_bboxes[i, 2] |
| 281 | gridpoint_y = factor_y * pos_gt_bboxes[i, 1] + ( | 281 | gridpoint_y = factor_y * pos_gt_bboxes[i, 1] + ( |
| 282 | - 1 - factor_y) * pos_gt_bboxes[i, 3] | 282 | + 1 - factor_y) * pos_gt_bboxes[i, 3] |
| 283 | 283 | ||
| 284 | cx = int((gridpoint_x - pos_bboxes[i, 0]) / pos_bbox_ws[i] * | 284 | cx = int((gridpoint_x - pos_bboxes[i, 0]) / pos_bbox_ws[i] * |
| 285 | map_size) | 285 | map_size) |
| @@ -289,7 +289,7 @@ class GridHead(nn.Module): | |||
| 289 | for x in range(cx - radius, cx + radius + 1): | 289 | for x in range(cx - radius, cx + radius + 1): |
| 290 | for y in range(cy - radius, cy + radius + 1): | 290 | for y in range(cy - radius, cy + radius + 1): |
| 291 | if x >= 0 and x < map_size and y >= 0 and y < map_size: | 291 | if x >= 0 and x < map_size and y >= 0 and y < map_size: |
| 292 | - if (x - cx)**2 + (y - cy)**2 <= radius2: | 292 | + if (x - cx) ** 2 + (y - cy) ** 2 <= radius2: |
| 293 | targets[i, j, y, x] = 1 | 293 | targets[i, j, y, x] = 1 |
| 294 | # reduce the target heatmap size by a half | 294 | # reduce the target heatmap size by a half |
| 295 | # proposed in Grid R-CNN Plus (https://arxiv.org/abs/1906.05688). | 295 | # proposed in Grid R-CNN Plus (https://arxiv.org/abs/1906.05688). |
| @@ -356,16 +356,16 @@ class GridHead(nn.Module): | |||
| 356 | # voting of all grid points on some boundary | 356 | # voting of all grid points on some boundary |
| 357 | bboxes_x1 = (abs_xs[:, x1_inds] * pred_scores[:, x1_inds]).sum( | 357 | bboxes_x1 = (abs_xs[:, x1_inds] * pred_scores[:, x1_inds]).sum( |
| 358 | dim=1, keepdim=True) / ( | 358 | dim=1, keepdim=True) / ( |
| 359 | - pred_scores[:, x1_inds].sum(dim=1, keepdim=True)) | 359 | + pred_scores[:, x1_inds].sum(dim=1, keepdim=True)) |
| 360 | bboxes_y1 = (abs_ys[:, y1_inds] * pred_scores[:, y1_inds]).sum( | 360 | bboxes_y1 = (abs_ys[:, y1_inds] * pred_scores[:, y1_inds]).sum( |
| 361 | dim=1, keepdim=True) / ( | 361 | dim=1, keepdim=True) / ( |
| 362 | - pred_scores[:, y1_inds].sum(dim=1, keepdim=True)) | 362 | + pred_scores[:, y1_inds].sum(dim=1, keepdim=True)) |
| 363 | bboxes_x2 = (abs_xs[:, x2_inds] * pred_scores[:, x2_inds]).sum( | 363 | bboxes_x2 = (abs_xs[:, x2_inds] * pred_scores[:, x2_inds]).sum( |
| 364 | dim=1, keepdim=True) / ( | 364 | dim=1, keepdim=True) / ( |
| 365 | - pred_scores[:, x2_inds].sum(dim=1, keepdim=True)) | 365 | + pred_scores[:, x2_inds].sum(dim=1, keepdim=True)) |
| 366 | bboxes_y2 = (abs_ys[:, y2_inds] * pred_scores[:, y2_inds]).sum( | 366 | bboxes_y2 = (abs_ys[:, y2_inds] * pred_scores[:, y2_inds]).sum( |
| 367 | dim=1, keepdim=True) / ( | 367 | dim=1, keepdim=True) / ( |
| 368 | - pred_scores[:, y2_inds].sum(dim=1, keepdim=True)) | 368 | + pred_scores[:, y2_inds].sum(dim=1, keepdim=True)) |
| 369 | 369 | ||
| 370 | bbox_res = torch.cat( | 370 | bbox_res = torch.cat( |
| 371 | [bboxes_x1, bboxes_y1, bboxes_x2, bboxes_y2, cls_scores], dim=1) | 371 | [bboxes_x1, bboxes_y1, bboxes_x2, bboxes_y2, cls_scores], dim=1) |
| @@ -63,7 +63,7 @@ class MaskFeatHead(nn.Module): | |||
| 63 | 63 | ||
| 64 | for j in range(i): | 64 | for j in range(i): |
| 65 | if j == 0: | 65 | if j == 0: |
| 66 | - chn = self.in_channels+2 if i==3 else self.in_channels | 66 | + chn = self.in_channels + 2 if i == 3 else self.in_channels |
| 67 | one_conv = ConvModule( | 67 | one_conv = ConvModule( |
| 68 | chn, | 68 | chn, |
| 69 | self.out_channels, | 69 | self.out_channels, |
| @@ -126,7 +126,7 @@ class MaskFeatHead(nn.Module): | |||
| 126 | x = x.expand([input_feat.shape[0], 1, -1, -1]) | 126 | x = x.expand([input_feat.shape[0], 1, -1, -1]) |
| 127 | coord_feat = torch.cat([x, y], 1) | 127 | coord_feat = torch.cat([x, y], 1) |
| 128 | input_p = torch.cat([input_p, coord_feat], 1) | 128 | input_p = torch.cat([input_p, coord_feat], 1) |
| 129 | - | 129 | + |
| 130 | feature_add_all_level += self.convs_all_levels[i](input_p) | 130 | feature_add_all_level += self.convs_all_levels[i](input_p) |
| 131 | 131 | ||
| 132 | feature_pred = self.conv_pred(feature_add_all_level) | 132 | feature_pred = self.conv_pred(feature_add_all_level) |
| @@ -102,7 +102,7 @@ class MaskIoUHead(nn.Module): | |||
| 102 | mask_iou = self.fc_mask_iou(x) | 102 | mask_iou = self.fc_mask_iou(x) |
| 103 | return mask_iou | 103 | return mask_iou |
| 104 | 104 | ||
| 105 | - @force_fp32(apply_to=('mask_iou_pred', )) | 105 | + @force_fp32(apply_to=('mask_iou_pred',)) |
| 106 | def loss(self, mask_iou_pred, mask_iou_targets): | 106 | def loss(self, mask_iou_pred, mask_iou_targets): |
| 107 | pos_inds = mask_iou_targets > 0 | 107 | pos_inds = mask_iou_targets > 0 |
| 108 | if pos_inds.sum() > 0: | 108 | if pos_inds.sum() > 0: |
| @@ -112,7 +112,7 @@ class MaskIoUHead(nn.Module): | |||
| 112 | loss_mask_iou = mask_iou_pred * 0 | 112 | loss_mask_iou = mask_iou_pred * 0 |
| 113 | return dict(loss_mask_iou=loss_mask_iou) | 113 | return dict(loss_mask_iou=loss_mask_iou) |
| 114 | 114 | ||
| 115 | - @force_fp32(apply_to=('mask_pred', )) | 115 | + @force_fp32(apply_to=('mask_pred',)) |
| 116 | def get_target(self, sampling_results, gt_masks, mask_pred, mask_targets, | 116 | def get_target(self, sampling_results, gt_masks, mask_pred, mask_targets, |
| 117 | rcnn_train_cfg): | 117 | rcnn_train_cfg): |
| 118 | """Compute target of mask IoU. | 118 | """Compute target of mask IoU. |
| @@ -159,7 +159,7 @@ class MaskIoUHead(nn.Module): | |||
| 159 | gt_full_areas = mask_targets.sum((-1, -2)) / (area_ratios + 1e-7) | 159 | gt_full_areas = mask_targets.sum((-1, -2)) / (area_ratios + 1e-7) |
| 160 | 160 | ||
| 161 | mask_iou_targets = overlap_areas / ( | 161 | mask_iou_targets = overlap_areas / ( |
| 162 | - mask_pred_areas + gt_full_areas - overlap_areas) | 162 | + mask_pred_areas + gt_full_areas - overlap_areas) |
| 163 | return mask_iou_targets | 163 | return mask_iou_targets |
| 164 | 164 | ||
| 165 | def _get_area_ratio(self, pos_proposals, pos_assigned_gt_inds, gt_masks): | 165 | def _get_area_ratio(self, pos_proposals, pos_assigned_gt_inds, gt_masks): |
| @@ -180,15 +180,15 @@ class MaskIoUHead(nn.Module): | |||
| 180 | gt_mask_in_proposal = gt_mask[y1:y2 + 1, x1:x2 + 1] | 180 | gt_mask_in_proposal = gt_mask[y1:y2 + 1, x1:x2 + 1] |
| 181 | 181 | ||
| 182 | ratio = gt_mask_in_proposal.sum() / ( | 182 | ratio = gt_mask_in_proposal.sum() / ( |
| 183 | - gt_instance_mask_area[pos_assigned_gt_inds[i]] + 1e-7) | 183 | + gt_instance_mask_area[pos_assigned_gt_inds[i]] + 1e-7) |
| 184 | area_ratios.append(ratio) | 184 | area_ratios.append(ratio) |
| 185 | area_ratios = torch.from_numpy(np.stack(area_ratios)).float().to( | 185 | area_ratios = torch.from_numpy(np.stack(area_ratios)).float().to( |
| 186 | pos_proposals.device) | 186 | pos_proposals.device) |
| 187 | else: | 187 | else: |
| 188 | - area_ratios = pos_proposals.new_zeros((0, )) | 188 | + area_ratios = pos_proposals.new_zeros((0,)) |
| 189 | return area_ratios | 189 | return area_ratios |
| 190 | 190 | ||
| 191 | - @force_fp32(apply_to=('mask_iou_pred', )) | 191 | + @force_fp32(apply_to=('mask_iou_pred',)) |
| 192 | def get_mask_scores(self, mask_iou_pred, det_bboxes, det_labels): | 192 | def get_mask_scores(self, mask_iou_pred, det_bboxes, det_labels): |
| 193 | """Get the mask scores. | 193 | """Get the mask scores. |
| 194 | 194 | ||
| @@ -94,7 +94,7 @@ class HRFPN(nn.Module): | |||
| 94 | outs = [inputs[0]] | 94 | outs = [inputs[0]] |
| 95 | for i in range(1, self.num_ins): | 95 | for i in range(1, self.num_ins): |
| 96 | outs.append( | 96 | outs.append( |
| 97 | - F.interpolate(inputs[i], scale_factor=2**i, mode='bilinear')) | 97 | + F.interpolate(inputs[i], scale_factor=2 ** i, mode='bilinear')) |
| 98 | out = torch.cat(outs, dim=1) | 98 | out = torch.cat(outs, dim=1) |
| 99 | if out.requires_grad and self.with_cp: | 99 | if out.requires_grad and self.with_cp: |
| 100 | out = checkpoint(self.reduction_conv, out) | 100 | out = checkpoint(self.reduction_conv, out) |
| @@ -102,7 +102,7 @@ class HRFPN(nn.Module): | |||
| 102 | out = self.reduction_conv(out) | 102 | out = self.reduction_conv(out) |
| 103 | outs = [out] | 103 | outs = [out] |
| 104 | for i in range(1, self.num_outs): | 104 | for i in range(1, self.num_outs): |
| 105 | - outs.append(self.pooling(out, kernel_size=2**i, stride=2**i)) | 105 | + outs.append(self.pooling(out, kernel_size=2 ** i, stride=2 ** i)) |
| 106 | outputs = [] | 106 | outputs = [] |
| 107 | 107 | ||
| 108 | for i in range(self.num_outs): | 108 | for i in range(self.num_outs): |
| @@ -135,15 +135,15 @@ class GeneralizedAttention(nn.Module): | |||
| 135 | for iy in range(max_len): | 135 | for iy in range(max_len): |
| 136 | for ix in range(max_len): | 136 | for ix in range(max_len): |
| 137 | local_constraint_map[ | 137 | local_constraint_map[ |
| 138 | - iy, ix, | 138 | + iy, ix, |
| 139 | - max((iy - self.spatial_range) // | 139 | + max((iy - self.spatial_range) // |
| 140 | - self.kv_stride, 0):min((iy + self.spatial_range + | 140 | + self.kv_stride, 0):min((iy + self.spatial_range + |
| 141 | - 1) // self.kv_stride + | 141 | + 1) // self.kv_stride + |
| 142 | - 1, max_len), | 142 | + 1, max_len), |
| 143 | - max((ix - self.spatial_range) // | 143 | + max((ix - self.spatial_range) // |
| 144 | - self.kv_stride, 0):min((ix + self.spatial_range + | 144 | + self.kv_stride, 0):min((ix + self.spatial_range + |
| 145 | - 1) // self.kv_stride + | 145 | + 1) // self.kv_stride + |
| 146 | - 1, max_len)] = 0 | 146 | + 1, max_len)] = 0 |
| 147 | 147 | ||
| 148 | self.local_constraint_map = nn.Parameter( | 148 | self.local_constraint_map = nn.Parameter( |
| 149 | torch.from_numpy(local_constraint_map).byte(), | 149 | torch.from_numpy(local_constraint_map).byte(), |
| @@ -196,7 +196,7 @@ class GeneralizedAttention(nn.Module): | |||
| 196 | feat_range = torch.arange(0, feat_dim / 4).cuda(device) | 196 | feat_range = torch.arange(0, feat_dim / 4).cuda(device) |
| 197 | 197 | ||
| 198 | dim_mat = torch.Tensor([wave_length]).cuda(device) | 198 | dim_mat = torch.Tensor([wave_length]).cuda(device) |
| 199 | - dim_mat = dim_mat**((4. / feat_dim) * feat_range) | 199 | + dim_mat = dim_mat ** ((4. / feat_dim) * feat_range) |
| 200 | dim_mat = dim_mat.view((1, 1, -1)) | 200 | dim_mat = dim_mat.view((1, 1, -1)) |
| 201 | 201 | ||
| 202 | embedding_x = torch.cat( | 202 | embedding_x = torch.cat( |
| @@ -237,15 +237,15 @@ class GeneralizedAttention(nn.Module): | |||
| 237 | h, w, h_kv, w_kv, self.q_stride, self.kv_stride, | 237 | h, w, h_kv, w_kv, self.q_stride, self.kv_stride, |
| 238 | x_input.device, self.position_embedding_dim) | 238 | x_input.device, self.position_embedding_dim) |
| 239 | # (n, num_heads, w, w_kv, dim) | 239 | # (n, num_heads, w, w_kv, dim) |
| 240 | - position_feat_x = self.appr_geom_fc_x(position_embed_x).\ | 240 | + position_feat_x = self.appr_geom_fc_x(position_embed_x). \ |
| 241 | - view(1, w, w_kv, num_heads, self.qk_embed_dim).\ | 241 | + view(1, w, w_kv, num_heads, self.qk_embed_dim). \ |
| 242 | - permute(0, 3, 1, 2, 4).\ | 242 | + permute(0, 3, 1, 2, 4). \ |
| 243 | repeat(n, 1, 1, 1, 1) | 243 | repeat(n, 1, 1, 1, 1) |
| 244 | 244 | ||
| 245 | # (n, num_heads, h, h_kv, dim) | 245 | # (n, num_heads, h, h_kv, dim) |
| 246 | - position_feat_y = self.appr_geom_fc_y(position_embed_y).\ | 246 | + position_feat_y = self.appr_geom_fc_y(position_embed_y). \ |
| 247 | - view(1, h, h_kv, num_heads, self.qk_embed_dim).\ | 247 | + view(1, h, h_kv, num_heads, self.qk_embed_dim). \ |
| 248 | - permute(0, 3, 1, 2, 4).\ | 248 | + permute(0, 3, 1, 2, 4). \ |
| 249 | repeat(n, 1, 1, 1, 1) | 249 | repeat(n, 1, 1, 1, 1) |
| 250 | 250 | ||
| 251 | position_feat_x /= math.sqrt(2) | 251 | position_feat_x /= math.sqrt(2) |
| @@ -253,11 +253,11 @@ class GeneralizedAttention(nn.Module): | |||
| 253 | 253 | ||
| 254 | # accelerate for saliency only | 254 | # accelerate for saliency only |
| 255 | if (np.sum(self.attention_type) == 1) and self.attention_type[2]: | 255 | if (np.sum(self.attention_type) == 1) and self.attention_type[2]: |
| 256 | - appr_bias = self.appr_bias.\ | 256 | + appr_bias = self.appr_bias. \ |
| 257 | - view(1, num_heads, 1, self.qk_embed_dim).\ | 257 | + view(1, num_heads, 1, self.qk_embed_dim). \ |
| 258 | repeat(n, 1, 1, 1) | 258 | repeat(n, 1, 1, 1) |
| 259 | 259 | ||
| 260 | - energy = torch.matmul(appr_bias, proj_key).\ | 260 | + energy = torch.matmul(appr_bias, proj_key). \ |
| 261 | view(n, num_heads, 1, h_kv * w_kv) | 261 | view(n, num_heads, 1, h_kv * w_kv) |
| 262 | 262 | ||
| 263 | h = 1 | 263 | h = 1 |
| @@ -281,35 +281,35 @@ class GeneralizedAttention(nn.Module): | |||
| 281 | # attention_type[3]: bias - position | 281 | # attention_type[3]: bias - position |
| 282 | if self.attention_type[0] or self.attention_type[2]: | 282 | if self.attention_type[0] or self.attention_type[2]: |
| 283 | if self.attention_type[0] and self.attention_type[2]: | 283 | if self.attention_type[0] and self.attention_type[2]: |
| 284 | - appr_bias = self.appr_bias.\ | 284 | + appr_bias = self.appr_bias. \ |
| 285 | view(1, num_heads, 1, self.qk_embed_dim) | 285 | view(1, num_heads, 1, self.qk_embed_dim) |
| 286 | - energy = torch.matmul(proj_query + appr_bias, proj_key).\ | 286 | + energy = torch.matmul(proj_query + appr_bias, proj_key). \ |
| 287 | view(n, num_heads, h, w, h_kv, w_kv) | 287 | view(n, num_heads, h, w, h_kv, w_kv) |
| 288 | 288 | ||
| 289 | elif self.attention_type[0]: | 289 | elif self.attention_type[0]: |
| 290 | - energy = torch.matmul(proj_query, proj_key).\ | 290 | + energy = torch.matmul(proj_query, proj_key). \ |
| 291 | view(n, num_heads, h, w, h_kv, w_kv) | 291 | view(n, num_heads, h, w, h_kv, w_kv) |
| 292 | 292 | ||
| 293 | elif self.attention_type[2]: | 293 | elif self.attention_type[2]: |
| 294 | - appr_bias = self.appr_bias.\ | 294 | + appr_bias = self.appr_bias. \ |
| 295 | - view(1, num_heads, 1, self.qk_embed_dim).\ | 295 | + view(1, num_heads, 1, self.qk_embed_dim). \ |
| 296 | repeat(n, 1, 1, 1) | 296 | repeat(n, 1, 1, 1) |
| 297 | 297 | ||
| 298 | - energy += torch.matmul(appr_bias, proj_key).\ | 298 | + energy += torch.matmul(appr_bias, proj_key). \ |
| 299 | view(n, num_heads, 1, 1, h_kv, w_kv) | 299 | view(n, num_heads, 1, 1, h_kv, w_kv) |
| 300 | 300 | ||
| 301 | if self.attention_type[1] or self.attention_type[3]: | 301 | if self.attention_type[1] or self.attention_type[3]: |
| 302 | if self.attention_type[1] and self.attention_type[3]: | 302 | if self.attention_type[1] and self.attention_type[3]: |
| 303 | - geom_bias = self.geom_bias.\ | 303 | + geom_bias = self.geom_bias. \ |
| 304 | view(1, num_heads, 1, self.qk_embed_dim) | 304 | view(1, num_heads, 1, self.qk_embed_dim) |
| 305 | 305 | ||
| 306 | - proj_query_reshape = (proj_query + geom_bias).\ | 306 | + proj_query_reshape = (proj_query + geom_bias). \ |
| 307 | view(n, num_heads, h, w, self.qk_embed_dim) | 307 | view(n, num_heads, h, w, self.qk_embed_dim) |
| 308 | 308 | ||
| 309 | energy_x = torch.matmul( | 309 | energy_x = torch.matmul( |
| 310 | proj_query_reshape.permute(0, 1, 3, 2, 4), | 310 | proj_query_reshape.permute(0, 1, 3, 2, 4), |
| 311 | position_feat_x.permute(0, 1, 2, 4, 3)) | 311 | position_feat_x.permute(0, 1, 2, 4, 3)) |
| 312 | - energy_x = energy_x.\ | 312 | + energy_x = energy_x. \ |
| 313 | permute(0, 1, 3, 2, 4).unsqueeze(4) | 313 | permute(0, 1, 3, 2, 4).unsqueeze(4) |
| 314 | 314 | ||
| 315 | energy_y = torch.matmul( | 315 | energy_y = torch.matmul( |
| @@ -320,13 +320,13 @@ class GeneralizedAttention(nn.Module): | |||
| 320 | energy += energy_x + energy_y | 320 | energy += energy_x + energy_y |
| 321 | 321 | ||
| 322 | elif self.attention_type[1]: | 322 | elif self.attention_type[1]: |
| 323 | - proj_query_reshape = proj_query.\ | 323 | + proj_query_reshape = proj_query. \ |
| 324 | view(n, num_heads, h, w, self.qk_embed_dim) | 324 | view(n, num_heads, h, w, self.qk_embed_dim) |
| 325 | - proj_query_reshape = proj_query_reshape.\ | 325 | + proj_query_reshape = proj_query_reshape. \ |
| 326 | permute(0, 1, 3, 2, 4) | 326 | permute(0, 1, 3, 2, 4) |
| 327 | - position_feat_x_reshape = position_feat_x.\ | 327 | + position_feat_x_reshape = position_feat_x. \ |
| 328 | permute(0, 1, 2, 4, 3) | 328 | permute(0, 1, 2, 4, 3) |
| 329 | - position_feat_y_reshape = position_feat_y.\ | 329 | + position_feat_y_reshape = position_feat_y. \ |
| 330 | permute(0, 1, 2, 4, 3) | 330 | permute(0, 1, 2, 4, 3) |
| 331 | 331 | ||
| 332 | energy_x = torch.matmul(proj_query_reshape, | 332 | energy_x = torch.matmul(proj_query_reshape, |
| @@ -340,14 +340,14 @@ class GeneralizedAttention(nn.Module): | |||
| 340 | energy += energy_x + energy_y | 340 | energy += energy_x + energy_y |
| 341 | 341 | ||
| 342 | elif self.attention_type[3]: | 342 | elif self.attention_type[3]: |
| 343 | - geom_bias = self.geom_bias.\ | 343 | + geom_bias = self.geom_bias. \ |
| 344 | - view(1, num_heads, self.qk_embed_dim, 1).\ | 344 | + view(1, num_heads, self.qk_embed_dim, 1). \ |
| 345 | repeat(n, 1, 1, 1) | 345 | repeat(n, 1, 1, 1) |
| 346 | 346 | ||
| 347 | - position_feat_x_reshape = position_feat_x.\ | 347 | + position_feat_x_reshape = position_feat_x. \ |
| 348 | - view(n, num_heads, w*w_kv, self.qk_embed_dim) | 348 | + view(n, num_heads, w * w_kv, self.qk_embed_dim) |
| 349 | 349 | ||
| 350 | - position_feat_y_reshape = position_feat_y.\ | 350 | + position_feat_y_reshape = position_feat_y. \ |
| 351 | view(n, num_heads, h * h_kv, self.qk_embed_dim) | 351 | view(n, num_heads, h * h_kv, self.qk_embed_dim) |
| 352 | 352 | ||
| 353 | energy_x = torch.matmul(position_feat_x_reshape, geom_bias) | 353 | energy_x = torch.matmul(position_feat_x_reshape, geom_bias) |
| @@ -362,9 +362,9 @@ class GeneralizedAttention(nn.Module): | |||
| 362 | 362 | ||
| 363 | if self.spatial_range >= 0: | 363 | if self.spatial_range >= 0: |
| 364 | cur_local_constraint_map = \ | 364 | cur_local_constraint_map = \ |
| 365 | - self.local_constraint_map[:h, :w, :h_kv, :w_kv].\ | 365 | + self.local_constraint_map[:h, :w, :h_kv, :w_kv]. \ |
| 366 | - contiguous().\ | 366 | + contiguous(). \ |
| 367 | - view(1, 1, h*w, h_kv*w_kv) | 367 | + view(1, 1, h * w, h_kv * w_kv) |
| 368 | 368 | ||
| 369 | energy = energy.masked_fill_(cur_local_constraint_map, | 369 | energy = energy.masked_fill_(cur_local_constraint_map, |
| 370 | float('-inf')) | 370 | float('-inf')) |
| @@ -372,13 +372,13 @@ class GeneralizedAttention(nn.Module): | |||
| 372 | attention = F.softmax(energy, 3) | 372 | attention = F.softmax(energy, 3) |
| 373 | 373 | ||
| 374 | proj_value = self.value_conv(x_kv) | 374 | proj_value = self.value_conv(x_kv) |
| 375 | - proj_value_reshape = proj_value.\ | 375 | + proj_value_reshape = proj_value. \ |
| 376 | - view((n, num_heads, self.v_dim, h_kv * w_kv)).\ | 376 | + view((n, num_heads, self.v_dim, h_kv * w_kv)). \ |
| 377 | permute(0, 1, 3, 2) | 377 | permute(0, 1, 3, 2) |
| 378 | 378 | ||
| 379 | - out = torch.matmul(attention, proj_value_reshape).\ | 379 | + out = torch.matmul(attention, proj_value_reshape). \ |
| 380 | - permute(0, 1, 3, 2).\ | 380 | + permute(0, 1, 3, 2). \ |
| 381 | - contiguous().\ | 381 | + contiguous(). \ |
| 382 | view(n, self.v_dim * self.num_heads, h, w) | 382 | view(n, self.v_dim * self.num_heads, h, w) |
| 383 | 383 | ||
| 384 | out = self.proj_conv(out) | 384 | out = self.proj_conv(out) |
| @@ -90,7 +90,7 @@ class NonLocal2D(nn.Module): | |||
| 90 | pairwise_weight = torch.matmul(theta_x, phi_x) | 90 | pairwise_weight = torch.matmul(theta_x, phi_x) |
| 91 | if self.use_scale: | 91 | if self.use_scale: |
| 92 | # theta_x.shape[-1] is `self.inter_channels` | 92 | # theta_x.shape[-1] is `self.inter_channels` |
| 93 | - pairwise_weight /= theta_x.shape[-1]**0.5 | 93 | + pairwise_weight /= theta_x.shape[-1] ** 0.5 |
| 94 | pairwise_weight = pairwise_weight.softmax(dim=-1) | 94 | pairwise_weight = pairwise_weight.softmax(dim=-1) |
| 95 | return pairwise_weight | 95 | return pairwise_weight |
| 96 | 96 | ||
| @@ -100,7 +100,7 @@ class SingleRoIExtractor(nn.Module): | |||
| 100 | new_rois = torch.stack((rois[:, 0], x1, y1, x2, y2), dim=-1) | 100 | new_rois = torch.stack((rois[:, 0], x1, y1, x2, y2), dim=-1) |
| 101 | return new_rois | 101 | return new_rois |
| 102 | 102 | ||
| 103 | - @force_fp32(apply_to=('feats', ), out_fp16=True) | 103 | + @force_fp32(apply_to=('feats',), out_fp16=True) |
| 104 | def forward(self, feats, rois, roi_scale_factor=None): | 104 | def forward(self, feats, rois, roi_scale_factor=None): |
| 105 | if len(feats) == 1: | 105 | if len(feats) == 1: |
| 106 | return self.roi_layers[0](feats[0], rois) | 106 | return self.roi_layers[0](feats[0], rois) |
| @@ -30,7 +30,7 @@ class ContextBlock(nn.Module): | |||
| 30 | inplanes, | 30 | inplanes, |
| 31 | ratio, | 31 | ratio, |
| 32 | pooling_type='att', | 32 | pooling_type='att', |
| 33 | - fusion_types=('channel_add', )): | 33 | + fusion_types=('channel_add',)): |
| 34 | super(ContextBlock, self).__init__() | 34 | super(ContextBlock, self).__init__() |
| 35 | assert pooling_type in ['avg', 'att'] | 35 | assert pooling_type in ['avg', 'att'] |
| 36 | assert isinstance(fusion_types, (list, tuple)) | 36 | assert isinstance(fusion_types, (list, tuple)) |
| @@ -18,6 +18,7 @@ from torch.autograd import Function | |||
| 18 | from torch.nn.modules.utils import _pair, _single | 18 | from torch.nn.modules.utils import _pair, _single |
| 19 | import math | 19 | import math |
| 20 | 20 | ||
| 21 | + | ||
| 21 | class ModulatedDeformConv2dFunction(Function): | 22 | class ModulatedDeformConv2dFunction(Function): |
| 22 | 23 | ||
| 23 | 24 | ||
| @@ -198,10 +199,11 @@ class ModulatedDeformConvPack(ModulatedDeformConv2d): | |||
| 198 | self.stride, self.padding, self.dilation, | 199 | self.stride, self.padding, self.dilation, |
| 199 | self.groups, self.deformable_groups) | 200 | self.groups, self.deformable_groups) |
| 200 | 201 | ||
| 202 | + | ||
| 201 | DCNv2 = ModulatedDeformConvPack | 203 | DCNv2 = ModulatedDeformConvPack |
| 202 | 204 | ||
| 203 | if __name__ == "__main__": | 205 | if __name__ == "__main__": |
| 204 | - x = torch.randn(2,32,4,4) | 206 | + x = torch.randn(2, 32, 4, 4) |
| 205 | model = DCNv2(32, 32, 1) | 207 | model = DCNv2(32, 32, 1) |
| 206 | 208 | ||
| 207 | torch.npu.set_device(0) | 209 | torch.npu.set_device(0) |
| @@ -69,7 +69,7 @@ class MaskedConv2dFunction(Function): | |||
| 69 | 69 | ||
| 70 | 70 | ||
| 71 | def backward(ctx, grad_output): | 71 | def backward(ctx, grad_output): |
| 72 | - return (None, ) * 5 | 72 | + return (None,) * 5 |
| 73 | 73 | ||
| 74 | 74 | ||
| 75 | masked_conv2d = MaskedConv2dFunction.apply | 75 | masked_conv2d = MaskedConv2dFunction.apply |
| @@ -15,6 +15,7 @@ | |||
| 15 | import numpy as np | 15 | import numpy as np |
| 16 | import torch | 16 | import torch |
| 17 | 17 | ||
| 18 | + | ||
| 18 | # from . import nms_cpu, nms_cuda | 19 | # from . import nms_cpu, nms_cuda |
| 19 | # from .soft_nms_cpu import soft_nms_cpu | 20 | # from .soft_nms_cpu import soft_nms_cpu |
| 20 | 21 | ||
| @@ -82,21 +82,21 @@ def get_model_complexity_info(model, | |||
| 82 | 82 | ||
| 83 | def flops_to_string(flops, units='GMac', precision=2): | 83 | def flops_to_string(flops, units='GMac', precision=2): |
| 84 | if units is None: | 84 | if units is None: |
| 85 | - if flops // 10**9 > 0: | 85 | + if flops // 10 ** 9 > 0: |
| 86 | - return str(round(flops / 10.**9, precision)) + ' GMac' | 86 | + return str(round(flops / 10. ** 9, precision)) + ' GMac' |
| 87 | - elif flops // 10**6 > 0: | 87 | + elif flops // 10 ** 6 > 0: |
| 88 | - return str(round(flops / 10.**6, precision)) + ' MMac' | 88 | + return str(round(flops / 10. ** 6, precision)) + ' MMac' |
| 89 | - elif flops // 10**3 > 0: | 89 | + elif flops // 10 ** 3 > 0: |
| 90 | - return str(round(flops / 10.**3, precision)) + ' KMac' | 90 | + return str(round(flops / 10. ** 3, precision)) + ' KMac' |
| 91 | else: | 91 | else: |
| 92 | return str(flops) + ' Mac' | 92 | return str(flops) + ' Mac' |
| 93 | else: | 93 | else: |
| 94 | if units == 'GMac': | 94 | if units == 'GMac': |
| 95 | - return str(round(flops / 10.**9, precision)) + ' ' + units | 95 | + return str(round(flops / 10. ** 9, precision)) + ' ' + units |
| 96 | elif units == 'MMac': | 96 | elif units == 'MMac': |
| 97 | - return str(round(flops / 10.**6, precision)) + ' ' + units | 97 | + return str(round(flops / 10. ** 6, precision)) + ' ' + units |
| 98 | elif units == 'KMac': | 98 | elif units == 'KMac': |
| 99 | - return str(round(flops / 10.**3, precision)) + ' ' + units | 99 | + return str(round(flops / 10. ** 3, precision)) + ' ' + units |
| 100 | else: | 100 | else: |
| 101 | return str(flops) + ' Mac' | 101 | return str(flops) + ' Mac' |
| 102 | 102 | ||
| @@ -114,10 +114,10 @@ def params_to_string(params_num): | |||
| 114 | >>> params_to_string(3e-9) | 114 | >>> params_to_string(3e-9) |
| 115 | '3e-09' | 115 | '3e-09' |
| 116 | """ | 116 | """ |
| 117 | - if params_num // 10**6 > 0: | 117 | + if params_num // 10 ** 6 > 0: |
| 118 | - return str(round(params_num / 10**6, 2)) + ' M' | 118 | + return str(round(params_num / 10 ** 6, 2)) + ' M' |
| 119 | - elif params_num // 10**3: | 119 | + elif params_num // 10 ** 3: |
| 120 | - return str(round(params_num / 10**3, 2)) + ' k' | 120 | + return str(round(params_num / 10 ** 3, 2)) + ' k' |
| 121 | else: | 121 | else: |
| 122 | return str(params_num) | 122 | return str(params_num) |
| 123 | 123 | ||
| @@ -237,7 +237,6 @@ def reset_flops_count(self): | |||
| 237 | 237 | ||
| 238 | 238 | ||
| 239 | def add_flops_mask(module, mask): | 239 | def add_flops_mask(module, mask): |
| 240 | - | ||
| 241 | def add_flops_mask_func(module): | 240 | def add_flops_mask_func(module): |
| 242 | if isinstance(module, torch.nn.Conv2d): | 241 | if isinstance(module, torch.nn.Conv2d): |
| 243 | module.__mask__ = mask | 242 | module.__mask__ = mask |
| @@ -319,7 +318,7 @@ def deconv_flops_counter_hook(conv_module, input, output): | |||
| 319 | 318 | ||
| 320 | filters_per_channel = out_channels // groups | 319 | filters_per_channel = out_channels // groups |
| 321 | conv_per_position_flops = ( | 320 | conv_per_position_flops = ( |
| 322 | - kernel_height * kernel_width * in_channels * filters_per_channel) | 321 | + kernel_height * kernel_width * in_channels * filters_per_channel) |
| 323 | 322 | ||
| 324 | active_elements_count = batch_size * input_height * input_width | 323 | active_elements_count = batch_size * input_height * input_width |
| 325 | overall_conv_flops = conv_per_position_flops * active_elements_count | 324 | overall_conv_flops = conv_per_position_flops * active_elements_count |
| @@ -362,7 +361,6 @@ def conv_flops_counter_hook(conv_module, input, output): | |||
| 362 | bias_flops = 0 | 361 | bias_flops = 0 |
| 363 | 362 | ||
| 364 | if conv_module.bias is not None: | 363 | if conv_module.bias is not None: |
| 365 | - | ||
| 366 | bias_flops = out_channels * active_elements_count | 364 | bias_flops = out_channels * active_elements_count |
| 367 | 365 | ||
| 368 | overall_flops = overall_conv_flops + bias_flops | 366 | overall_flops = overall_conv_flops + bias_flops |
| @@ -15,4 +15,4 @@ | |||
| 15 | # Author: Acer Zhang | 15 | # Author: Acer Zhang |
| 16 | # Datetime: 2021/9/1 | 16 | # Datetime: 2021/9/1 |
| 17 | # Copyright belongs to the author. | 17 | # Copyright belongs to the author. |
| 18 | -# Please indicate the source for reprinting. | 18 | +# Please indicate the source for reprinting. |
| @@ -12,7 +12,7 @@ | |||
| 12 | # See the License for the specific language governing permissions and | 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. | 13 | # limitations under the License. |
| 14 | 14 | ||
| 15 | -#!/usr/bin/env python | 15 | +# !/usr/bin/env python |
| 16 | # -*- coding: utf-8 -*- | 16 | # -*- coding: utf-8 -*- |
| 17 | import os | 17 | import os |
| 18 | import platform | 18 | import platform |
| @@ -48,7 +48,6 @@ version_file = 'mmdet/version.py' | |||
| 48 | 48 | ||
| 49 | 49 | ||
| 50 | def get_git_hash(): | 50 | def get_git_hash(): |
| 51 | - | ||
| 52 | def _minimal_ext_cmd(cmd): | 51 | def _minimal_ext_cmd(cmd): |
| 53 | # construct minimal environment | 52 | # construct minimal environment |
| 54 | env = {} | 53 | env = {} |
| @@ -131,7 +130,6 @@ def make_cuda_ext(name, module, sources): | |||
| 131 | define_macros=define_macros, | 130 | define_macros=define_macros, |
| 132 | extra_compile_args=extra_compile_args) | 131 | extra_compile_args=extra_compile_args) |
| 133 | 132 | ||
| 134 | - | ||
| 135 | # define_macros = [] | 133 | # define_macros = [] |
| 136 | # extra_compile_args = {'cxx': []} | 134 | # extra_compile_args = {'cxx': []} |
| 137 | # | 135 | # |
| @@ -11,7 +11,7 @@ batch_size=1 | |||
| 11 | # 训练使用的npu卡数 | 11 | # 训练使用的npu卡数 |
| 12 | export RANK_SIZE=1 | 12 | export RANK_SIZE=1 |
| 13 | data_path="" | 13 | data_path="" |
| 14 | -MODEL="./work_dirs/solov2_release_r50_fpn_8gpu_1x/latest.pth" | 14 | +MODEL="./work_dirs/solov2_release_r50_fpn_8gpu_1x/epoch_12.pth" |
| 15 | device_id=0 | 15 | device_id=0 |
| 16 | 16 | ||
| 17 | #参数校验,不需要修改 | 17 | #参数校验,不需要修改 |
| @@ -70,7 +70,7 @@ end_time=$(date +%s) | |||
| 70 | e2e_time=$(( $end_time - $start_time )) | 70 | e2e_time=$(( $end_time - $start_time )) |
| 71 | 71 | ||
| 72 | # 输出训练精度,需要模型审视修改 # eval.log | awk -F ',' '{print $1}' | awk '{print $2}' | awk ' END {print}' | 72 | # 输出训练精度,需要模型审视修改 # eval.log | awk -F ',' '{print $1}' | awk '{print $2}' | awk ' END {print}' |
| 73 | -train_accuracy=`grep -a 'maxDets' $cur_path/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log|awk -F " " '{print $13}'|head -n 1` | 73 | +train_accuracy=`grep -a 'maxDets' $cur_path/output/${ASCEND_DEVICE_ID}/eval_${ASCEND_DEVICE_ID}.log|awk -F " " '{print $13}'|head -n 1` |
| 74 | # 打印,不需要修改 | 74 | # 打印,不需要修改 |
| 75 | echo "Final Train Accuracy : ${train_accuracy}" | 75 | echo "Final Train Accuracy : ${train_accuracy}" |
| 76 | echo "E2E Training Duration sec : $e2e_time" | 76 | echo "E2E Training Duration sec : $e2e_time" |
| @@ -14,7 +14,7 @@ Network="SOLOv2" | |||
| 14 | 14 | ||
| 15 | #训练batch_size,,需要模型审视修改 | 15 | #训练batch_size,,需要模型审视修改 |
| 16 | batch_size=2 | 16 | batch_size=2 |
| 17 | -device_id=1 | 17 | +device_id=0 |
| 18 | 18 | ||
| 19 | #参数校验,不需要修改 | 19 | #参数校验,不需要修改 |
| 20 | for para in $* | 20 | for para in $* |
| @@ -14,7 +14,7 @@ Network="SOLOv2" | |||
| 14 | 14 | ||
| 15 | #训练batch_size,,需要模型审视修改 | 15 | #训练batch_size,,需要模型审视修改 |
| 16 | batch_size=16 | 16 | batch_size=16 |
| 17 | -device_id=1 | 17 | +device_id=0 |
| 18 | 18 | ||
| 19 | #参数校验,不需要修改 | 19 | #参数校验,不需要修改 |
| 20 | for para in $* | 20 | for para in $* |
| @@ -14,7 +14,7 @@ Network="SOLOv2" | |||
| 14 | 14 | ||
| 15 | #训练batch_size,,需要模型审视修改 | 15 | #训练batch_size,,需要模型审视修改 |
| 16 | batch_size=2 | 16 | batch_size=2 |
| 17 | -device_id=1 | 17 | +device_id=0 |
| 18 | 18 | ||
| 19 | #参数校验,不需要修改 | 19 | #参数校验,不需要修改 |
| 20 | for para in $* | 20 | for para in $* |
| @@ -68,10 +68,7 @@ fi | |||
| 68 | export NPUID=0 | 68 | export NPUID=0 |
| 69 | export RANK=0 | 69 | export RANK=0 |
| 70 | python3.7 tools/train.py configs/solov2/solov2_r50_fpn_8gpu_1x.py --opt-level $apex --autoscale-lr --seed 0 --total_epochs 1 \ | 70 | python3.7 tools/train.py configs/solov2/solov2_r50_fpn_8gpu_1x.py --opt-level $apex --autoscale-lr --seed 0 --total_epochs 1 \ |
| 71 | - --data_root=$data_path --gpu-ids 0 > ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & | 71 | + --data_root=$data_path --gpu-ids 0 --train_performance=True > ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & |
| 72 | -wait | ||
| 73 | -python3.7 tools/test_ins.py configs/solov2/solov2_r50_fpn_8gpu_1x.py work_dirs/solov2_release_r50_fpn_8gpu_1x/latest.pth --show \ | ||
| 74 | - --out results_solo.pkl --eval segm --data_root=$data_path >> ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & | ||
| 75 | wait | 72 | wait |
| 76 | 73 | ||
| 77 | #训练结束时间,不需要修改 | 74 | #训练结束时间,不需要修改 |
| @@ -14,7 +14,7 @@ Network="SOLOv2" | |||
| 14 | 14 | ||
| 15 | #训练batch_size,,需要模型审视修改 | 15 | #训练batch_size,,需要模型审视修改 |
| 16 | batch_size=16 | 16 | batch_size=16 |
| 17 | -device_id=1 | 17 | +device_id=0 |
| 18 | 18 | ||
| 19 | #参数校验,不需要修改 | 19 | #参数校验,不需要修改 |
| 20 | for para in $* | 20 | for para in $* |
| @@ -82,7 +82,7 @@ do | |||
| 82 | --gpus 8 \ | 82 | --gpus 8 \ |
| 83 | --autoscale-lr \ | 83 | --autoscale-lr \ |
| 84 | --seed 0 \ | 84 | --seed 0 \ |
| 85 | - --data_root=$data_path \ | 85 | + --data_root=$data_path --train_performance=True \ |
| 86 | --total_epochs 1 > ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & | 86 | --total_epochs 1 > ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & |
| 87 | else | 87 | else |
| 88 | python3.7 ./tools/train.py configs/solov2/solov2_r50_fpn_8gpu_1x.py \ | 88 | python3.7 ./tools/train.py configs/solov2/solov2_r50_fpn_8gpu_1x.py \ |
| @@ -91,14 +91,11 @@ do | |||
| 91 | --gpus 8 \ | 91 | --gpus 8 \ |
| 92 | --autoscale-lr \ | 92 | --autoscale-lr \ |
| 93 | --seed 0 \ | 93 | --seed 0 \ |
| 94 | - --data_root=$data_path \ | 94 | + --data_root=$data_path --train_performance=True \ |
| 95 | --total_epochs 1 > ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & | 95 | --total_epochs 1 > ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & |
| 96 | fi | 96 | fi |
| 97 | done | 97 | done |
| 98 | wait | 98 | wait |
| 99 | -python3.7 tools/test_ins.py configs/solov2/solov2_r50_fpn_8gpu_1x.py work_dirs/solov2_release_r50_fpn_8gpu_1x/latest.pth --show \ | ||
| 100 | - --out results_solo.pkl --eval segm --data_root=$data_path >> ${cur_path}/output/${ASCEND_DEVICE_ID}/train_${ASCEND_DEVICE_ID}.log 2>&1 & | ||
| 101 | -wait | ||
| 102 | #训练结束时间,不需要修改 | 99 | #训练结束时间,不需要修改 |
| 103 | end_time=$(date +%s) | 100 | end_time=$(date +%s) |
| 104 | e2e_time=$(( $end_time - $start_time )) | 101 | e2e_time=$(( $end_time - $start_time )) |
| @@ -118,7 +118,7 @@ def test_max_iou_assigner_with_empty_boxes(): | |||
| 118 | # Test with gt_labels | 118 | # Test with gt_labels |
| 119 | assign_result = self.assign(bboxes, gt_bboxes, gt_labels=gt_labels) | 119 | assign_result = self.assign(bboxes, gt_bboxes, gt_labels=gt_labels) |
| 120 | assert len(assign_result.gt_inds) == 0 | 120 | assert len(assign_result.gt_inds) == 0 |
| 121 | - assert tuple(assign_result.labels.shape) == (0, ) | 121 | + assert tuple(assign_result.labels.shape) == (0,) |
| 122 | 122 | ||
| 123 | # Test without gt_labels | 123 | # Test without gt_labels |
| 124 | assign_result = self.assign(bboxes, gt_bboxes, gt_labels=None) | 124 | assign_result = self.assign(bboxes, gt_bboxes, gt_labels=None) |
| @@ -73,7 +73,6 @@ class MaskRCNNDetector: | |||
| 73 | 73 | ||
| 74 | 74 | ||
| 75 | class AsyncInferenceTestCase(AsyncTestCase): | 75 | class AsyncInferenceTestCase(AsyncTestCase): |
| 76 | - | ||
| 77 | if sys.version_info >= (3, 7): | 76 | if sys.version_info >= (3, 7): |
| 78 | 77 | ||
| 79 | async def test_simple_inference(self): | 78 | async def test_simple_inference(self): |
| @@ -54,7 +54,7 @@ def test_anchor_head_loss(): | |||
| 54 | 54 | ||
| 55 | # Anchor head expects a multiple levels of features per image | 55 | # Anchor head expects a multiple levels of features per image |
| 56 | feat = [ | 56 | feat = [ |
| 57 | - torch.rand(1, 1, s // (2**(i + 2)), s // (2**(i + 2))) | 57 | + torch.rand(1, 1, s // (2 ** (i + 2)), s // (2 ** (i + 2))) |
| 58 | for i in range(len(self.anchor_generators)) | 58 | for i in range(len(self.anchor_generators)) |
| 59 | ] | 59 | ] |
| 60 | cls_scores, bbox_preds = self.forward(feat) | 60 | cls_scores, bbox_preds = self.forward(feat) |
| @@ -330,14 +330,14 @@ def _demodata_refine_boxes(n_roi, n_img, rng=0): | |||
| 330 | roi_boxes = random_boxes(n_roi, scale=scale, rng=rng) | 330 | roi_boxes = random_boxes(n_roi, scale=scale, rng=rng) |
| 331 | if n_img == 0: | 331 | if n_img == 0: |
| 332 | assert n_roi == 0, 'cannot have any rois if there are no images' | 332 | assert n_roi == 0, 'cannot have any rois if there are no images' |
| 333 | - img_ids = torch.empty((0, ), dtype=torch.long) | 333 | + img_ids = torch.empty((0,), dtype=torch.long) |
| 334 | roi_boxes = torch.empty((0, 4), dtype=torch.float32) | 334 | roi_boxes = torch.empty((0, 4), dtype=torch.float32) |
| 335 | else: | 335 | else: |
| 336 | - img_ids = rng.randint(0, n_img, (n_roi, )) | 336 | + img_ids = rng.randint(0, n_img, (n_roi,)) |
| 337 | img_ids = torch.from_numpy(img_ids) | 337 | img_ids = torch.from_numpy(img_ids) |
| 338 | rois = torch.cat([img_ids[:, None].float(), roi_boxes], dim=1) | 338 | rois = torch.cat([img_ids[:, None].float(), roi_boxes], dim=1) |
| 339 | # Create other args | 339 | # Create other args |
| 340 | - labels = rng.randint(0, 2, (n_roi, )) | 340 | + labels = rng.randint(0, 2, (n_roi,)) |
| 341 | labels = torch.from_numpy(labels).long() | 341 | labels = torch.from_numpy(labels).long() |
| 342 | bbox_preds = random_boxes(n_roi, scale=scale, rng=rng) | 342 | bbox_preds = random_boxes(n_roi, scale=scale, rng=rng) |
| 343 | # For each image, pretend random positive boxes are gts | 343 | # For each image, pretend random positive boxes are gts |
| @@ -346,7 +346,7 @@ def _demodata_refine_boxes(n_roi, n_img, rng=0): | |||
| 346 | pos_per_img = [sum(lbl_per_img.get(gid, [])) for gid in range(n_img)] | 346 | pos_per_img = [sum(lbl_per_img.get(gid, [])) for gid in range(n_img)] |
| 347 | # randomly generate with numpy then sort with torch | 347 | # randomly generate with numpy then sort with torch |
| 348 | _pos_is_gts = [ | 348 | _pos_is_gts = [ |
| 349 | - rng.randint(0, 2, (npos, )).astype(np.uint8) for npos in pos_per_img | 349 | + rng.randint(0, 2, (npos,)).astype(np.uint8) for npos in pos_per_img |
| 350 | ] | 350 | ] |
| 351 | pos_is_gts = [ | 351 | pos_is_gts = [ |
| 352 | torch.from_numpy(p).sort(descending=True)[0] for p in _pos_is_gts | 352 | torch.from_numpy(p).sort(descending=True)[0] for p in _pos_is_gts |
| @@ -124,7 +124,6 @@ def _context_for_ohem(): | |||
| 124 | 124 | ||
| 125 | 125 | ||
| 126 | def test_ohem_sampler(): | 126 | def test_ohem_sampler(): |
| 127 | - | ||
| 128 | assigner = MaxIoUAssigner( | 127 | assigner = MaxIoUAssigner( |
| 129 | pos_iou_thr=0.5, | 128 | pos_iou_thr=0.5, |
| 130 | neg_iou_thr=0.5, | 129 | neg_iou_thr=0.5, |
| @@ -160,7 +159,7 @@ def test_ohem_sampler(): | |||
| 160 | neg_pos_ub=-1, | 159 | neg_pos_ub=-1, |
| 161 | add_gt_as_proposals=True) | 160 | add_gt_as_proposals=True) |
| 162 | 161 | ||
| 163 | - feats = [torch.rand(1, 256, int(2**i), int(2**i)) for i in [6, 5, 4, 3, 2]] | 162 | + feats = [torch.rand(1, 256, int(2 ** i), int(2 ** i)) for i in [6, 5, 4, 3, 2]] |
| 164 | sample_result = sampler.sample( | 163 | sample_result = sampler.sample( |
| 165 | assign_result, bboxes, gt_bboxes, gt_labels, feats=feats) | 164 | assign_result, bboxes, gt_bboxes, gt_labels, feats=feats) |
| 166 | 165 | ||
| @@ -169,7 +168,6 @@ def test_ohem_sampler(): | |||
| 169 | 168 | ||
| 170 | 169 | ||
| 171 | def test_ohem_sampler_empty_gt(): | 170 | def test_ohem_sampler_empty_gt(): |
| 172 | - | ||
| 173 | assigner = MaxIoUAssigner( | 171 | assigner = MaxIoUAssigner( |
| 174 | pos_iou_thr=0.5, | 172 | pos_iou_thr=0.5, |
| 175 | neg_iou_thr=0.5, | 173 | neg_iou_thr=0.5, |
| @@ -200,7 +198,7 @@ def test_ohem_sampler_empty_gt(): | |||
| 200 | neg_pos_ub=-1, | 198 | neg_pos_ub=-1, |
| 201 | add_gt_as_proposals=True) | 199 | add_gt_as_proposals=True) |
| 202 | 200 | ||
| 203 | - feats = [torch.rand(1, 256, int(2**i), int(2**i)) for i in [6, 5, 4, 3, 2]] | 201 | + feats = [torch.rand(1, 256, int(2 ** i), int(2 ** i)) for i in [6, 5, 4, 3, 2]] |
| 204 | 202 | ||
| 205 | sample_result = sampler.sample( | 203 | sample_result = sampler.sample( |
| 206 | assign_result, bboxes, gt_bboxes, gt_labels, feats=feats) | 204 | assign_result, bboxes, gt_bboxes, gt_labels, feats=feats) |
| @@ -240,7 +238,7 @@ def test_ohem_sampler_empty_pred(): | |||
| 240 | neg_pos_ub=-1, | 238 | neg_pos_ub=-1, |
| 241 | add_gt_as_proposals=True) | 239 | add_gt_as_proposals=True) |
| 242 | 240 | ||
| 243 | - feats = [torch.rand(1, 256, int(2**i), int(2**i)) for i in [6, 5, 4, 3, 2]] | 241 | + feats = [torch.rand(1, 256, int(2 ** i), int(2 ** i)) for i in [6, 5, 4, 3, 2]] |
| 244 | 242 | ||
| 245 | sample_result = sampler.sample( | 243 | sample_result = sampler.sample( |
| 246 | assign_result, bboxes, gt_bboxes, gt_labels, feats=feats) | 244 | assign_result, bboxes, gt_bboxes, gt_labels, feats=feats) |
| @@ -146,7 +146,7 @@ def add_time_parser(subparsers): | |||
| 146 | '--include-outliers', | 146 | '--include-outliers', |
| 147 | action='store_true', | 147 | action='store_true', |
| 148 | help='include the first value of every epoch when computing ' | 148 | help='include the first value of every epoch when computing ' |
| 149 | - 'the average time') | 149 | + 'the average time') |
| 150 | 150 | ||
| 151 | 151 | ||
| 152 | def parse_args(): | 152 | def parse_args(): |
| @@ -54,13 +54,13 @@ def parse_xml(args): | |||
| 54 | labels.append(label) | 54 | labels.append(label) |
| 55 | if not bboxes: | 55 | if not bboxes: |
| 56 | bboxes = np.zeros((0, 4)) | 56 | bboxes = np.zeros((0, 4)) |
| 57 | - labels = np.zeros((0, )) | 57 | + labels = np.zeros((0,)) |
| 58 | else: | 58 | else: |
| 59 | bboxes = np.array(bboxes, ndmin=2) - 1 | 59 | bboxes = np.array(bboxes, ndmin=2) - 1 |
| 60 | labels = np.array(labels) | 60 | labels = np.array(labels) |
| 61 | if not bboxes_ignore: | 61 | if not bboxes_ignore: |
| 62 | bboxes_ignore = np.zeros((0, 4)) | 62 | bboxes_ignore = np.zeros((0, 4)) |
| 63 | - labels_ignore = np.zeros((0, )) | 63 | + labels_ignore = np.zeros((0,)) |
| 64 | else: | 64 | else: |
| 65 | bboxes_ignore = np.array(bboxes_ignore, ndmin=2) - 1 | 65 | bboxes_ignore = np.array(bboxes_ignore, ndmin=2) - 1 |
| 66 | labels_ignore = np.array(labels_ignore) | 66 | labels_ignore = np.array(labels_ignore) |
| @@ -14,7 +14,7 @@ | |||
| 14 | 14 | ||
| 15 | #!/usr/bin/env bash | 15 | #!/usr/bin/env bash |
| 16 | 16 | ||
| 17 | -PYTHON=${PYTHON:-"python"} | 17 | +PYTHON=${PYTHON:-"python3.7"} |
| 18 | 18 | ||
| 19 | CONFIG=$1 | 19 | CONFIG=$1 |
| 20 | CHECKPOINT=$2 | 20 | CHECKPOINT=$2 |
| @@ -34,13 +34,12 @@ def parse_args(): | |||
| 34 | 34 | ||
| 35 | 35 | ||
| 36 | def main(): | 36 | def main(): |
| 37 | - | ||
| 38 | args = parse_args() | 37 | args = parse_args() |
| 39 | 38 | ||
| 40 | if len(args.shape) == 1: | 39 | if len(args.shape) == 1: |
| 41 | input_shape = (3, args.shape[0], args.shape[0]) | 40 | input_shape = (3, args.shape[0], args.shape[0]) |
| 42 | elif len(args.shape) == 2: | 41 | elif len(args.shape) == 2: |
| 43 | - input_shape = (3, ) + tuple(args.shape) | 42 | + input_shape = (3,) + tuple(args.shape) |
| 44 | else: | 43 | else: |
| 45 | raise ValueError('invalid input shape') | 44 | raise ValueError('invalid input shape') |
| 46 | 45 | ||
| @@ -54,7 +53,7 @@ def main(): | |||
| 54 | else: | 53 | else: |
| 55 | raise NotImplementedError( | 54 | raise NotImplementedError( |
| 56 | 'FLOPs counter is currently not currently supported with {}'. | 55 | 'FLOPs counter is currently not currently supported with {}'. |
| 57 | - format(model.__class__.__name__)) | 56 | + format(model.__class__.__name__)) |
| 58 | 57 | ||
| 59 | flops, params = get_model_complexity_info(model, input_shape) | 58 | flops, params = get_model_complexity_info(model, input_shape) |
| 60 | split_line = '=' * 30 | 59 | split_line = '=' * 30 |
| @@ -20,7 +20,6 @@ import numpy as np | |||
| 20 | 20 | ||
| 21 | 21 | ||
| 22 | def print_coco_results(results): | 22 | def print_coco_results(results): |
| 23 | - | ||
| 24 | def _print(result, ap=1, iouThr=None, areaRng='all', maxDets=100): | 23 | def _print(result, ap=1, iouThr=None, areaRng='all', maxDets=100): |
| 25 | iStr = ' {:<18} {} @[ IoU={:<9} | \ | 24 | iStr = ' {:<18} {} @[ IoU={:<9} | \ |
| 26 | area={:>6s} | maxDets={:>3d} ] = {:0.3f}' | 25 | area={:>6s} | maxDets={:>3d} ] = {:0.3f}' |
| @@ -31,7 +30,7 @@ def print_coco_results(results): | |||
| 31 | if iouThr is None else '{:0.2f}'.format(iouThr) | 30 | if iouThr is None else '{:0.2f}'.format(iouThr) |
| 32 | print(iStr.format(titleStr, typeStr, iouStr, areaRng, maxDets, result)) | 31 | print(iStr.format(titleStr, typeStr, iouStr, areaRng, maxDets, result)) |
| 33 | 32 | ||
| 34 | - stats = np.zeros((12, )) | 33 | + stats = np.zeros((12,)) |
| 35 | stats[0] = _print(results[0], 1) | 34 | stats[0] = _print(results[0], 1) |
| 36 | stats[1] = _print(results[1], 1, iouThr=.5) | 35 | stats[1] = _print(results[1], 1, iouThr=.5) |
| 37 | stats[2] = _print(results[2], 1, iouThr=.75) | 36 | stats[2] = _print(results[2], 1, iouThr=.75) |
| @@ -51,7 +50,6 @@ def get_coco_style_results(filename, | |||
| 51 | metric=None, | 50 | metric=None, |
| 52 | prints='mPC', | 51 | prints='mPC', |
| 53 | aggregate='benchmark'): | 52 | aggregate='benchmark'): |
| 54 | - | ||
| 55 | assert aggregate in ['benchmark', 'all'] | 53 | assert aggregate in ['benchmark', 'all'] |
| 56 | 54 | ||
| 57 | if prints == 'all': | 55 | if prints == 'all': |
| @@ -127,7 +125,6 @@ def get_coco_style_results(filename, | |||
| 127 | 125 | ||
| 128 | 126 | ||
| 129 | def get_voc_style_results(filename, prints='mPC', aggregate='benchmark'): | 127 | def get_voc_style_results(filename, prints='mPC', aggregate='benchmark'): |
| 130 | - | ||
| 131 | assert aggregate in ['benchmark', 'all'] | 128 | assert aggregate in ['benchmark', 'all'] |
| 132 | 129 | ||
| 133 | if prints == 'all': | 130 | if prints == 'all': |
| @@ -201,7 +198,6 @@ def get_results(filename, | |||
| 201 | 198 | ||
| 202 | 199 | ||
| 203 | def get_distortions_from_file(filename): | 200 | def get_distortions_from_file(filename): |
| 204 | - | ||
| 205 | eval_output = mmcv.load(filename) | 201 | eval_output = mmcv.load(filename) |
| 206 | 202 | ||
| 207 | return get_distortions_from_results(eval_output) | 203 | return get_distortions_from_results(eval_output) |
| @@ -98,7 +98,7 @@ def collect_results_cpu(result_part, size, tmpdir=None): | |||
| 98 | if tmpdir is None: | 98 | if tmpdir is None: |
| 99 | MAX_LEN = 512 | 99 | MAX_LEN = 512 |
| 100 | # 32 is whitespace | 100 | # 32 is whitespace |
| 101 | - dir_tensor = torch.full((MAX_LEN, ), | 101 | + dir_tensor = torch.full((MAX_LEN,), |
| 102 | 32, | 102 | 32, |
| 103 | dtype=torch.uint8, | 103 | dtype=torch.uint8, |
| 104 | device='cuda') | 104 | device='cuda') |
| @@ -15,15 +15,23 @@ | |||
| 15 | import argparse | 15 | import argparse |
| 16 | import os | 16 | import os |
| 17 | import os.path as osp | 17 | import os.path as osp |
| 18 | +import pickle | ||
| 18 | import shutil | 19 | import shutil |
| 19 | import tempfile | 20 | import tempfile |
| 20 | 21 | ||
| 21 | import mmcv | 22 | import mmcv |
| 23 | +import apex | ||
| 24 | +import time | ||
| 25 | +from apex import amp | ||
| 22 | import torch | 26 | import torch |
| 27 | + | ||
| 28 | +if torch.__version__ >= '1.8.1': | ||
| 29 | + import torch_npu | ||
| 23 | import torch.nn.functional as F | 30 | import torch.nn.functional as F |
| 24 | import torch.distributed as dist | 31 | import torch.distributed as dist |
| 25 | from mmcv.parallel import MMDataParallel, MMDistributedDataParallel | 32 | from mmcv.parallel import MMDataParallel, MMDistributedDataParallel |
| 26 | from mmcv.runner import init_dist, get_dist_info, load_checkpoint | 33 | from mmcv.runner import init_dist, get_dist_info, load_checkpoint |
| 34 | +from mmcv.runner import DistSamplerSeedHook, Runner, obj_from_dict | ||
| 27 | 35 | ||
| 28 | from mmdet.core import coco_eval, results2json, results2json_segm, wrap_fp16_model, tensor2imgs, get_classes | 36 | from mmdet.core import coco_eval, results2json, results2json_segm, wrap_fp16_model, tensor2imgs, get_classes |
| 29 | from mmdet.datasets import build_dataloader, build_dataset | 37 | from mmdet.datasets import build_dataloader, build_dataset |
| @@ -65,7 +73,7 @@ def single_gpu_test(model, data_loader, show=False, verbose=True): | |||
| 65 | seg_result = model(return_loss=False, rescale=not show, **data) | 73 | seg_result = model(return_loss=False, rescale=not show, **data) |
| 66 | result = get_masks(seg_result, num_classes=num_classes) | 74 | result = get_masks(seg_result, num_classes=num_classes) |
| 67 | results.append(result) | 75 | results.append(result) |
| 68 | - | 76 | + |
| 69 | batch_size = data['img'][0].size(0) | 77 | batch_size = data['img'][0].size(0) |
| 70 | for _ in range(batch_size): | 78 | for _ in range(batch_size): |
| 71 | prog_bar.update() | 79 | prog_bar.update() |
| @@ -80,10 +88,13 @@ def multi_gpu_test(model, data_loader, tmpdir=None): | |||
| 80 | 88 | ||
| 81 | rank, world_size = get_dist_info() | 89 | rank, world_size = get_dist_info() |
| 82 | if rank == 0: | 90 | if rank == 0: |
| 83 | - prog_bar = mmcv.ProgressBar(len(dataset)) | 91 | + prog_bar = mmcv.ProgressBar(16) |
| 84 | for i, data in enumerate(data_loader): | 92 | for i, data in enumerate(data_loader): |
| 93 | + if i >= 2: | ||
| 94 | + break | ||
| 85 | with torch.no_grad(): | 95 | with torch.no_grad(): |
| 86 | seg_result = model(return_loss=False, rescale=True, **data) | 96 | seg_result = model(return_loss=False, rescale=True, **data) |
| 97 | + torch.npu.synchronize() | ||
| 87 | result = get_masks(seg_result, num_classes=num_classes) | 98 | result = get_masks(seg_result, num_classes=num_classes) |
| 88 | results.append(result) | 99 | results.append(result) |
| 89 | 100 | ||
| @@ -93,7 +104,7 @@ def multi_gpu_test(model, data_loader, tmpdir=None): | |||
| 93 | prog_bar.update() | 104 | prog_bar.update() |
| 94 | 105 | ||
| 95 | # collect results from all ranks | 106 | # collect results from all ranks |
| 96 | - results = collect_results(results, len(dataset), tmpdir) | 107 | + results = collect_results(results, 16, tmpdir) |
| 97 | 108 | ||
| 98 | return results | 109 | return results |
| 99 | 110 | ||
| @@ -104,14 +115,14 @@ def collect_results(result_part, size, tmpdir=None): | |||
| 104 | if tmpdir is None: | 115 | if tmpdir is None: |
| 105 | MAX_LEN = 512 | 116 | MAX_LEN = 512 |
| 106 | # 32 is whitespace | 117 | # 32 is whitespace |
| 107 | - dir_tensor = torch.full((MAX_LEN, ), | 118 | + dir_tensor = torch.full((MAX_LEN,), |
| 108 | 32, | 119 | 32, |
| 109 | dtype=torch.uint8, | 120 | dtype=torch.uint8, |
| 110 | - device='cuda') | 121 | + device='npu') |
| 111 | if rank == 0: | 122 | if rank == 0: |
| 112 | tmpdir = tempfile.mkdtemp() | 123 | tmpdir = tempfile.mkdtemp() |
| 113 | tmpdir = torch.tensor( | 124 | tmpdir = torch.tensor( |
| 114 | - bytearray(tmpdir.encode()), dtype=torch.uint8, device='cuda') | 125 | + bytearray(tmpdir.encode()), dtype=torch.uint8, device='npu') |
| 115 | dir_tensor[:len(tmpdir)] = tmpdir | 126 | dir_tensor[:len(tmpdir)] = tmpdir |
| 116 | dist.broadcast(dir_tensor, 0) | 127 | dist.broadcast(dir_tensor, 0) |
| 117 | tmpdir = dir_tensor.cpu().numpy().tobytes().decode().rstrip() | 128 | tmpdir = dir_tensor.cpu().numpy().tobytes().decode().rstrip() |
| @@ -179,19 +190,104 @@ def parse_args(): | |||
| 179 | return args | 190 | return args |
| 180 | 191 | ||
| 181 | 192 | ||
| 193 | +def build_optimizer(model, optimizer_cfg): | ||
| 194 | + """Build optimizer from configs. | ||
| 195 | + | ||
| 196 | + Args: | ||
| 197 | + model (:obj:`nn.Module`): The model with parameters to be optimized. | ||
| 198 | + optimizer_cfg (dict): The config dict of the optimizer. | ||
| 199 | + Positional fields are: | ||
| 200 | + - type: class name of the optimizer. | ||
| 201 | + - lr: base learning rate. | ||
| 202 | + Optional fields are: | ||
| 203 | + - any arguments of the corresponding optimizer type, e.g., | ||
| 204 | + weight_decay, momentum, etc. | ||
| 205 | + - paramwise_options: a dict with 3 accepted fileds | ||
| 206 | + (bias_lr_mult, bias_decay_mult, norm_decay_mult). | ||
| 207 | + `bias_lr_mult` and `bias_decay_mult` will be multiplied to | ||
| 208 | + the lr and weight decay respectively for all bias parameters | ||
| 209 | + (except for the normalization layers), and | ||
| 210 | + `norm_decay_mult` will be multiplied to the weight decay | ||
| 211 | + for all weight and bias parameters of normalization layers. | ||
| 212 | + | ||
| 213 | + Returns: | ||
| 214 | + torch.optim.Optimizer: The initialized optimizer. | ||
| 215 | + | ||
| 216 | + Example: | ||
| 217 | + >>> model = torch.nn.modules.Conv1d(1, 1, 1) | ||
| 218 | + >>> optimizer_cfg = dict(type='SGD', lr=0.01, momentum=0.9, | ||
| 219 | + >>> weight_decay=0.0001) | ||
| 220 | + >>> optimizer = build_optimizer(model, optimizer_cfg) | ||
| 221 | + """ | ||
| 222 | + if hasattr(model, 'module'): | ||
| 223 | + model = model.module | ||
| 224 | + | ||
| 225 | + optimizer_cfg = optimizer_cfg.copy() | ||
| 226 | + paramwise_options = optimizer_cfg.pop('paramwise_options', None) | ||
| 227 | + # if no paramwise option is specified, just use the global setting | ||
| 228 | + if paramwise_options is None: | ||
| 229 | + return obj_from_dict(optimizer_cfg, torch.optim, | ||
| 230 | + dict(params=model.parameters())) | ||
| 231 | + else: | ||
| 232 | + assert isinstance(paramwise_options, dict) | ||
| 233 | + # get base lr and weight decay | ||
| 234 | + base_lr = optimizer_cfg['lr'] | ||
| 235 | + base_wd = optimizer_cfg.get('weight_decay', None) | ||
| 236 | + # weight_decay must be explicitly specified if mult is specified | ||
| 237 | + if ('bias_decay_mult' in paramwise_options | ||
| 238 | + or 'norm_decay_mult' in paramwise_options): | ||
| 239 | + assert base_wd is not None | ||
| 240 | + # get param-wise options | ||
| 241 | + bias_lr_mult = paramwise_options.get('bias_lr_mult', 1.) | ||
| 242 | + bias_decay_mult = paramwise_options.get('bias_decay_mult', 1.) | ||
| 243 | + norm_decay_mult = paramwise_options.get('norm_decay_mult', 1.) | ||
| 244 | + # set param-wise lr and weight decay | ||
| 245 | + params = [] | ||
| 246 | + for name, param in model.named_parameters(): | ||
| 247 | + param_group = {'params': [param]} | ||
| 248 | + if not param.requires_grad: | ||
| 249 | + # FP16 training needs to copy gradient/weight between master | ||
| 250 | + # weight copy and model weight, it is convenient to keep all | ||
| 251 | + # parameters here to align with model.parameters() | ||
| 252 | + params.append(param_group) | ||
| 253 | + continue | ||
| 254 | + | ||
| 255 | + # for norm layers, overwrite the weight decay of weight and bias | ||
| 256 | + # TODO: obtain the norm layer prefixes dynamically | ||
| 257 | + if re.search(r'(bn|gn)(\d+)?.(weight|bias)', name): | ||
| 258 | + if base_wd is not None: | ||
| 259 | + param_group['weight_decay'] = base_wd * norm_decay_mult | ||
| 260 | + # for other layers, overwrite both lr and weight decay of bias | ||
| 261 | + elif name.endswith('.bias'): | ||
| 262 | + param_group['lr'] = base_lr * bias_lr_mult | ||
| 263 | + if base_wd is not None: | ||
| 264 | + param_group['weight_decay'] = base_wd * bias_decay_mult | ||
| 265 | + # otherwise use the global settings | ||
| 266 | + | ||
| 267 | + params.append(param_group) | ||
| 268 | + | ||
| 269 | + optimizer_cls = getattr(torch.optim, optimizer_cfg.pop('type')) | ||
| 270 | + return optimizer_cls(params, **optimizer_cfg) | ||
| 271 | + | ||
| 272 | + | ||
| 182 | def main(): | 273 | def main(): |
| 183 | args = parse_args() | 274 | args = parse_args() |
| 275 | + option = {} | ||
| 276 | + option["ACL_OP_COMPILER_CACHE_MODE"] = 'enable' | ||
| 277 | + option["ACL_OP_COMPILER_CACHE_DIR"] = './cache' | ||
| 184 | 278 | ||
| 279 | + option["ACL_OP_SELECT_IMPL_MODE"] = 'high_precision' | ||
| 280 | + option['ACL_OPTYPELIST_FOR_IMPLMODE'] = 'Sqrt' | ||
| 281 | + print('option', option) | ||
| 282 | + torch.npu.set_option(option) | ||
| 185 | assert args.out or args.show or args.json_out, \ | 283 | assert args.out or args.show or args.json_out, \ |
| 186 | ('Please specify at least one operation (save or show the results) ' | 284 | ('Please specify at least one operation (save or show the results) ' |
| 187 | 'with the argument "--out" or "--show" or "--json_out"') | 285 | 'with the argument "--out" or "--show" or "--json_out"') |
| 188 | - | ||
| 189 | if args.out is not None and not args.out.endswith(('.pkl', '.pickle')): | 286 | if args.out is not None and not args.out.endswith(('.pkl', '.pickle')): |
| 190 | raise ValueError('The output file must be a pkl file.') | 287 | raise ValueError('The output file must be a pkl file.') |
| 191 | 288 | ||
| 192 | if args.json_out is not None and args.json_out.endswith('.json'): | 289 | if args.json_out is not None and args.json_out.endswith('.json'): |
| 193 | args.json_out = args.json_out[:-5] | 290 | args.json_out = args.json_out[:-5] |
| 194 | - | ||
| 195 | cfg = mmcv.Config.fromfile(args.config) | 291 | cfg = mmcv.Config.fromfile(args.config) |
| 196 | if args.data_root: | 292 | if args.data_root: |
| 197 | cfg.data_root = args.data_root | 293 | cfg.data_root = args.data_root |
| @@ -205,12 +301,15 @@ def main(): | |||
| 205 | if args.gpu_ids is not None: | 301 | if args.gpu_ids is not None: |
| 206 | torch.npu.set_device(args.gpu_ids[0]) | 302 | torch.npu.set_device(args.gpu_ids[0]) |
| 207 | # init distributed env first, since logger depends on the dist info. | 303 | # init distributed env first, since logger depends on the dist info. |
| 208 | - if args.launcher == 'none': | 304 | + if "WORLD_SIZE" in os.environ and int(os.environ["WORLD_SIZE"]) > 1: |
| 209 | - distributed = False | ||
| 210 | - else: | ||
| 211 | distributed = True | 305 | distributed = True |
| 212 | - init_dist(args.launcher, **cfg.dist_params) | 306 | + rank = int(os.environ['RANK']) |
| 213 | - | 307 | + num_gpus = torch.npu.device_count() |
| 308 | + torch.npu.set_device(rank % num_gpus) | ||
| 309 | + torch.distributed.init_process_group(backend='hccl', init_method='env://', | ||
| 310 | + world_size=int(os.environ['WORLD_SIZE']), rank=args.local_rank) | ||
| 311 | + else: | ||
| 312 | + distributed = False | ||
| 214 | # build the dataloader | 313 | # build the dataloader |
| 215 | # TODO: support multiple images per gpu (only minor changes are needed) | 314 | # TODO: support multiple images per gpu (only minor changes are needed) |
| 216 | print('arg data root', cfg.data_root) | 315 | print('arg data root', cfg.data_root) |
| @@ -225,10 +324,11 @@ def main(): | |||
| 225 | # build the model and load checkpoint | 324 | # build the model and load checkpoint |
| 226 | model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg) | 325 | model = build_detector(cfg.model, train_cfg=None, test_cfg=cfg.test_cfg) |
| 227 | fp16_cfg = cfg.get('fp16', None) | 326 | fp16_cfg = cfg.get('fp16', None) |
| 228 | - if fp16_cfg is not None: | 327 | + # if fp16_cfg is not None: |
| 229 | - wrap_fp16_model(model) | 328 | + # wrap_fp16_model(model) |
| 230 | 329 | ||
| 231 | while not osp.isfile(args.checkpoint): | 330 | while not osp.isfile(args.checkpoint): |
| 331 | + print(args.checkpoint) | ||
| 232 | print('Waiting for {} to exist...'.format(args.checkpoint)) | 332 | print('Waiting for {} to exist...'.format(args.checkpoint)) |
| 233 | time.sleep(60) | 333 | time.sleep(60) |
| 234 | 334 | ||
| @@ -241,10 +341,18 @@ def main(): | |||
| 241 | model.CLASSES = dataset.CLASSES | 341 | model.CLASSES = dataset.CLASSES |
| 242 | 342 | ||
| 243 | if not distributed: | 343 | if not distributed: |
| 244 | - model = MMDataParallel(model.npu(), device_ids=[0]) | 344 | + amp.register_float_function(torch, 'sigmoid') |
| 345 | + optimizer = build_optimizer(model, cfg.optimizer) | ||
| 346 | + model, optimizer = amp.initialize(model.npu(), optimizer, | ||
| 347 | + opt_level='O1', loss_scale=128.0, combine_grad=True) | ||
| 348 | + model = MMDataParallel(model, device_ids=[0]) | ||
| 245 | outputs = single_gpu_test(model, data_loader) | 349 | outputs = single_gpu_test(model, data_loader) |
| 246 | else: | 350 | else: |
| 247 | - model = MMDistributedDataParallel(model.npu()) | 351 | + amp.register_float_function(torch, 'sigmoid') |
| 352 | + optimizer = build_optimizer(model, cfg.optimizer) | ||
| 353 | + model, optimizer = amp.initialize(model.npu(), optimizer, | ||
| 354 | + opt_level='O1', loss_scale=128.0, combine_grad=True) | ||
| 355 | + model = MMDistributedDataParallel(model) | ||
| 248 | outputs = multi_gpu_test(model, data_loader, args.tmpdir) | 356 | outputs = multi_gpu_test(model, data_loader, args.tmpdir) |
| 249 | 357 | ||
| 250 | rank, _ = get_dist_info() | 358 | rank, _ = get_dist_info() |
| @@ -32,6 +32,7 @@ import cv2 | |||
| 32 | import numpy as np | 32 | import numpy as np |
| 33 | import matplotlib.cm as cm | 33 | import matplotlib.cm as cm |
| 34 | 34 | ||
| 35 | + | ||
| 35 | def vis_seg(data, result, img_norm_cfg, data_id, colors, score_thr, save_dir): | 36 | def vis_seg(data, result, img_norm_cfg, data_id, colors, score_thr, save_dir): |
| 36 | img_tensor = data['img'][0] | 37 | img_tensor = data['img'][0] |
| 37 | img_metas = data['img_meta'][0].data[0] | 38 | img_metas = data['img_meta'][0].data[0] |
| @@ -44,7 +45,7 @@ def vis_seg(data, result, img_norm_cfg, data_id, colors, score_thr, save_dir): | |||
| 44 | continue | 45 | continue |
| 45 | h, w, _ = img_meta['img_shape'] | 46 | h, w, _ = img_meta['img_shape'] |
| 46 | img_show = img[:h, :w, :] | 47 | img_show = img[:h, :w, :] |
| 47 | - | 48 | + |
| 48 | seg_label = cur_result[0] | 49 | seg_label = cur_result[0] |
| 49 | seg_label = seg_label.cpu().numpy().astype(np.uint8) | 50 | seg_label = seg_label.cpu().numpy().astype(np.uint8) |
| 50 | cate_label = cur_result[1] | 51 | cate_label = cur_result[1] |
| @@ -70,12 +71,12 @@ def vis_seg(data, result, img_norm_cfg, data_id, colors, score_thr, save_dir): | |||
| 70 | 71 | ||
| 71 | seg_show = img_show.copy() | 72 | seg_show = img_show.copy() |
| 72 | for idx in range(num_mask): | 73 | for idx in range(num_mask): |
| 73 | - idx = -(idx+1) | 74 | + idx = -(idx + 1) |
| 74 | - cur_mask = seg_label[idx, :,:] | 75 | + cur_mask = seg_label[idx, :, :] |
| 75 | cur_mask = mmcv.imresize(cur_mask, (w, h)) | 76 | cur_mask = mmcv.imresize(cur_mask, (w, h)) |
| 76 | cur_mask = (cur_mask > 0.5).astype(np.uint8) | 77 | cur_mask = (cur_mask > 0.5).astype(np.uint8) |
| 77 | if cur_mask.sum() == 0: | 78 | if cur_mask.sum() == 0: |
| 78 | - continue | 79 | + continue |
| 79 | color_mask = np.random.randint( | 80 | color_mask = np.random.randint( |
| 80 | 0, 256, (1, 3), dtype=np.uint8) | 81 | 0, 256, (1, 3), dtype=np.uint8) |
| 81 | cur_mask_bool = cur_mask.astype(np.bool) | 82 | cur_mask_bool = cur_mask.astype(np.bool) |
| @@ -85,7 +86,7 @@ def vis_seg(data, result, img_norm_cfg, data_id, colors, score_thr, save_dir): | |||
| 85 | cur_score = cate_score[idx] | 86 | cur_score = cate_score[idx] |
| 86 | 87 | ||
| 87 | label_text = class_names[cur_cate] | 88 | label_text = class_names[cur_cate] |
| 88 | - #label_text += '|{:.02f}'.format(cur_score) | 89 | + # label_text += '|{:.02f}'.format(cur_score) |
| 89 | # center | 90 | # center |
| 90 | center_y, center_x = ndimage.measurements.center_of_mass(cur_mask) | 91 | center_y, center_x = ndimage.measurements.center_of_mass(cur_mask) |
| 91 | vis_pos = (max(int(center_x) - 10, 0), int(center_y)) | 92 | vis_pos = (max(int(center_x) - 10, 0), int(center_y)) |
| @@ -99,8 +100,8 @@ def single_gpu_test(model, data_loader, args, cfg=None, verbose=True): | |||
| 99 | results = [] | 100 | results = [] |
| 100 | dataset = data_loader.dataset | 101 | dataset = data_loader.dataset |
| 101 | 102 | ||
| 102 | - class_num = 1000 # ins | 103 | + class_num = 1000 # ins |
| 103 | - colors = [(np.random.random((1, 3)) * 255).tolist()[0] for i in range(class_num)] | 104 | + colors = [(np.random.random((1, 3)) * 255).tolist()[0] for i in range(class_num)] |
| 104 | 105 | ||
| 105 | prog_bar = mmcv.ProgressBar(len(dataset)) | 106 | prog_bar = mmcv.ProgressBar(len(dataset)) |
| 106 | for i, data in enumerate(data_loader): | 107 | for i, data in enumerate(data_loader): |
| @@ -110,7 +111,8 @@ def single_gpu_test(model, data_loader, args, cfg=None, verbose=True): | |||
| 110 | results.append(result) | 111 | results.append(result) |
| 111 | 112 | ||
| 112 | if verbose: | 113 | if verbose: |
| 113 | - vis_seg(data, seg_result, cfg.img_norm_cfg, data_id=i, colors=colors, score_thr=args.score_thr, save_dir=args.save_dir) | 114 | + vis_seg(data, seg_result, cfg.img_norm_cfg, data_id=i, colors=colors, score_thr=args.score_thr, |
| 115 | + save_dir=args.save_dir) | ||
| 114 | 116 | ||
| 115 | batch_size = data['img'][0].size(0) | 117 | batch_size = data['img'][0].size(0) |
| 116 | for _ in range(batch_size): | 118 | for _ in range(batch_size): |
| @@ -147,7 +149,7 @@ def collect_results(result_part, size, tmpdir=None): | |||
| 147 | if tmpdir is None: | 149 | if tmpdir is None: |
| 148 | MAX_LEN = 512 | 150 | MAX_LEN = 512 |
| 149 | # 32 is whitespace | 151 | # 32 is whitespace |
| 150 | - dir_tensor = torch.full((MAX_LEN, ), | 152 | + dir_tensor = torch.full((MAX_LEN,), |
| 151 | 32, | 153 | 32, |
| 152 | dtype=torch.uint8, | 154 | dtype=torch.uint8, |
| 153 | device='cuda') | 155 | device='cuda') |
| @@ -162,7 +162,7 @@ def collect_results(result_part, size, tmpdir=None): | |||
| 162 | if tmpdir is None: | 162 | if tmpdir is None: |
| 163 | MAX_LEN = 512 | 163 | MAX_LEN = 512 |
| 164 | # 32 is whitespace | 164 | # 32 is whitespace |
| 165 | - dir_tensor = torch.full((MAX_LEN, ), | 165 | + dir_tensor = torch.full((MAX_LEN,), |
| 166 | 32, | 166 | 32, |
| 167 | dtype=torch.uint8, | 167 | dtype=torch.uint8, |
| 168 | device='cuda') | 168 | device='cuda') |
| @@ -395,8 +395,8 @@ def main(): | |||
| 395 | rank, _ = get_dist_info() | 395 | rank, _ = get_dist_info() |
| 396 | if args.out and rank == 0: | 396 | if args.out and rank == 0: |
| 397 | eval_results_filename = ( | 397 | eval_results_filename = ( |
| 398 | - osp.splitext(args.out)[0] + '_results' + | 398 | + osp.splitext(args.out)[0] + '_results' + |
| 399 | - osp.splitext(args.out)[1]) | 399 | + osp.splitext(args.out)[1]) |
| 400 | mmcv.dump(outputs, args.out) | 400 | mmcv.dump(outputs, args.out) |
| 401 | eval_types = args.eval | 401 | eval_types = args.eval |
| 402 | if cfg.dataset_type == 'VOCDataset': | 402 | if cfg.dataset_type == 'VOCDataset': |
| @@ -20,6 +20,9 @@ import time | |||
| 20 | 20 | ||
| 21 | import mmcv | 21 | import mmcv |
| 22 | import torch | 22 | import torch |
| 23 | + | ||
| 24 | +if torch.__version__ >= '1.8.1': | ||
| 25 | + import torch_npu | ||
| 23 | from mmcv import Config | 26 | from mmcv import Config |
| 24 | from mmcv.runner import init_dist, load_state_dict | 27 | from mmcv.runner import init_dist, load_state_dict |
| 25 | 28 | ||
| @@ -29,6 +32,7 @@ from mmdet.datasets import build_dataset | |||
| 29 | from mmdet.models import build_detector | 32 | from mmdet.models import build_detector |
| 30 | from mmdet.utils import get_root_logger | 33 | from mmdet.utils import get_root_logger |
| 31 | 34 | ||
| 35 | + | ||
| 32 | def parse_args(): | 36 | def parse_args(): |
| 33 | parser = argparse.ArgumentParser(description='Train a detector') | 37 | parser = argparse.ArgumentParser(description='Train a detector') |
| 34 | parser.add_argument('config', help='train config file path') | 38 | parser.add_argument('config', help='train config file path') |
| @@ -44,7 +48,7 @@ def parse_args(): | |||
| 44 | type=int, | 48 | type=int, |
| 45 | default=1, | 49 | default=1, |
| 46 | help='number of gpus to use ' | 50 | help='number of gpus to use ' |
| 47 | - '(only applicable to non-distributed training)') | 51 | + '(only applicable to non-distributed training)') |
| 48 | parser.add_argument( | 52 | parser.add_argument( |
| 49 | '--data_root', | 53 | '--data_root', |
| 50 | help='the path of dataset', | 54 | help='the path of dataset', |
| @@ -61,6 +65,7 @@ def parse_args(): | |||
| 61 | action='store_true', | 65 | action='store_true', |
| 62 | help='whether fine-tune model, change class num + 1') | 66 | help='whether fine-tune model, change class num + 1') |
| 63 | parser.add_argument('--total_epochs', type=int, default=12, help='random seed') | 67 | parser.add_argument('--total_epochs', type=int, default=12, help='random seed') |
| 68 | + parser.add_argument('--train_performance', type=bool, default=False, help='train performace') | ||
| 64 | parser.add_argument( | 69 | parser.add_argument( |
| 65 | '--deterministic', | 70 | '--deterministic', |
| 66 | action='store_true', | 71 | action='store_true', |
| @@ -88,6 +93,14 @@ def parse_args(): | |||
| 88 | 93 | ||
| 89 | 94 | ||
| 90 | def main(): | 95 | def main(): |
| 96 | + option = {} | ||
| 97 | + option["ACL_OP_COMPILER_CACHE_MODE"] = 'enable' | ||
| 98 | + option["ACL_OP_COMPILER_CACHE_DIR"] = './cache' | ||
| 99 | + | ||
| 100 | + option["ACL_OP_SELECT_IMPL_MODE"] = 'high_precision' | ||
| 101 | + option['ACL_OPTYPELIST_FOR_IMPLMODE'] = 'Sqrt' | ||
| 102 | + print('option', option) | ||
| 103 | + torch.npu.set_option(option) | ||
| 91 | os.environ['MASTER_ADDR'] = '127.0.0.1' | 104 | os.environ['MASTER_ADDR'] = '127.0.0.1' |
| 92 | os.environ['MASTER_PORT'] = '29688' | 105 | os.environ['MASTER_PORT'] = '29688' |
| 93 | args = parse_args() | 106 | args = parse_args() |
| @@ -177,8 +190,9 @@ def main(): | |||
| 177 | cfg, | 190 | cfg, |
| 178 | distributed=distributed, | 191 | distributed=distributed, |
| 179 | validate=args.validate, | 192 | validate=args.validate, |
| 180 | - timestamp=timestamp) | 193 | + timestamp=timestamp, |
| 194 | + train_performance=args.train_performance) | ||
| 181 | 195 | ||
| 182 | 196 | ||
| 183 | if __name__ == '__main__': | 197 | if __name__ == '__main__': |
| 184 | - main() | 198 | + main() |