├── track2 ├── test.sh ├── train.sh ├── model │ ├── modules │ │ ├── backbones │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── hrnet.cpython-36.pyc │ │ │ │ ├── hrnet.cpython-37.pyc │ │ │ │ ├── senet.cpython-36.pyc │ │ │ │ ├── helpers.cpython-36.pyc │ │ │ │ ├── resnest.cpython-36.pyc │ │ │ │ ├── resnest.cpython-37.pyc │ │ │ │ ├── resnet.cpython-36.pyc │ │ │ │ ├── resnet.cpython-37.pyc │ │ │ │ ├── se_ibn.cpython-36.pyc │ │ │ │ ├── se_ibn.cpython-37.pyc │ │ │ │ ├── __init__.cpython-36.pyc │ │ │ │ ├── __init__.cpython-37.pyc │ │ │ │ ├── registry.cpython-36.pyc │ │ │ │ ├── resnet_ibn.cpython-36.pyc │ │ │ │ ├── resnet_ibn.cpython-37.pyc │ │ │ │ ├── resnetvd.cpython-36.pyc │ │ │ │ ├── sw_resnet.cpython-36.pyc │ │ │ │ ├── xception.cpython-36.pyc │ │ │ │ ├── efficientnet.cpython-36.pyc │ │ │ │ ├── efficientnet.cpython-37.pyc │ │ │ │ ├── res2net_dal.cpython-36.pyc │ │ │ │ ├── resnext_ibn.cpython-36.pyc │ │ │ │ ├── resnext_ibn.cpython-37.pyc │ │ │ │ └── resnet_dilated.cpython-36.pyc │ │ │ ├── resnet_dilated.py │ │ │ └── registry.py │ │ ├── utils.pyc │ │ ├── __init__.pyc │ │ ├── inplace_abn.zip │ │ ├── __pycache__ │ │ │ ├── cc.cpython-36.pyc │ │ │ ├── cc.cpython-37.pyc │ │ │ ├── mish.cpython-36.pyc │ │ │ ├── mish.cpython-37.pyc │ │ │ ├── scse.cpython-36.pyc │ │ │ ├── scse.cpython-37.pyc │ │ │ ├── utils.cpython-36.pyc │ │ │ ├── utils.cpython-37.pyc │ │ │ ├── dblock.cpython-36.pyc │ │ │ ├── dblock.cpython-37.pyc │ │ │ ├── pooling.cpython-36.pyc │ │ │ ├── __init__.cpython-36.pyc │ │ │ ├── __init__.cpython-37.pyc │ │ │ ├── fcn_head.cpython-36.pyc │ │ │ ├── strippooling.cpython-36.pyc │ │ │ ├── base_ocr_model.cpython-36.pyc │ │ │ ├── dbb_transforms.cpython-36.pyc │ │ │ ├── sep_aspp_head.cpython-36.pyc │ │ │ └── diversebranchblock.cpython-36.pyc │ │ ├── inplace_abn │ │ │ ├── __pycache__ │ │ │ │ ├── abn.cpython-36.pyc │ │ │ │ └── __init__.cpython-36.pyc │ │ │ ├── __init__.py │ │ │ ├── src │ │ │ │ ├── inplace_abn.h │ │ │ │ ├── inplace_abn.cpp │ │ │ │ ├── common.h │ │ │ │ └── inplace_abn_cpu.cpp │ │ │ ├── abn.py │ │ │ └── bn.py │ │ ├── mish.py │ │ ├── __init__.py │ │ ├── scse.py │ │ ├── dbb_transforms.py │ │ └── cc.py │ ├── unet.pyc │ ├── __init__.pyc │ ├── __pycache__ │ │ ├── msc.cpython-36.pyc │ │ ├── msc.cpython-37.pyc │ │ ├── unet.cpython-36.pyc │ │ ├── unet.cpython-37.pyc │ │ ├── __init__.cpython-36.pyc │ │ ├── __init__.cpython-37.pyc │ │ ├── dlinknet.cpython-36.pyc │ │ ├── dlinknet.cpython-37.pyc │ │ ├── hr_seg.cpython-36.pyc │ │ ├── hr_seg.cpython-37.pyc │ │ ├── res_unet.cpython-36.pyc │ │ ├── res_unet.cpython-37.pyc │ │ ├── res_unet_dbb.cpython-36.pyc │ │ ├── deeplab_v3_plus.cpython-36.pyc │ │ ├── deeplab_v3_plus.cpython-37.pyc │ │ ├── efficient_unet.cpython-36.pyc │ │ ├── efficient_unet.cpython-37.pyc │ │ ├── msc_hierarchical.cpython-36.pyc │ │ └── msc_hierarchical.cpython-37.pyc │ ├── .ipynb_checkpoints │ │ └── __init__-checkpoint.py │ ├── __init__.py │ ├── msc.py │ ├── hr_seg.py │ └── efficient_unet.py ├── run.sh ├── edgeBCE_Dice_loss.py ├── style_transfer.py ├── edge_utils.py ├── data_agu.py └── copy_paste.py ├── track1 ├── pytorch │ └── code │ │ ├── nets │ │ ├── __init__.py │ │ ├── backbone │ │ │ ├── __init__.py │ │ │ ├── __pycache__ │ │ │ │ ├── hrnet.cpython-37.pyc │ │ │ │ ├── modules.cpython-37.pyc │ │ │ │ ├── __init__.cpython-37.pyc │ │ │ │ ├── backbone.cpython-37.pyc │ │ │ │ └── hrnet_ibn.cpython-37.pyc │ │ │ └── modules.py │ │ ├── __pycache__ │ │ │ ├── hrnet.cpython-37.pyc │ │ │ ├── __init__.cpython-37.pyc │ │ │ ├── hrnet_ibn.cpython-37.pyc │ │ │ ├── net_training.cpython-37.pyc │ │ │ └── centernet_training.cpython-37.pyc │ │ ├── net_training.py │ │ └── hrnet.py │ │ ├── utils │ │ ├── __pycache__ │ │ │ ├── utils.cpython-37.pyc │ │ │ ├── dataloader.cpython-37.pyc │ │ │ └── utils_bbox.cpython-37.pyc │ │ └── utils.py │ │ ├── pre │ │ ├── cal_mean_std.py │ │ ├── json2txt.py │ │ ├── gen_train_txt.py │ │ ├── draw_box.py │ │ └── data_clip.py │ │ ├── readme.md │ │ ├── train.py │ │ └── test_vim.py └── mindspore │ └── code │ ├── src │ ├── __pycache__ │ │ ├── hrnet.cpython-37.pyc │ │ ├── loss.cpython-37.pyc │ │ ├── utils.cpython-37.pyc │ │ ├── config.cpython-37.pyc │ │ ├── decode.cpython-37.pyc │ │ ├── callback.cpython-37.pyc │ │ ├── utils_bbox.cpython-37.pyc │ │ ├── basedataset.cpython-37.pyc │ │ ├── post_process.cpython-37.pyc │ │ ├── road_dataset.cpython-37.pyc │ │ ├── seg_hrnet_ocr.cpython-37.pyc │ │ ├── utils_bbox_20221025.cpython-37.pyc │ │ └── utils_bbox_20221026.cpython-37.pyc │ ├── model_utils │ │ ├── __pycache__ │ │ │ └── moxing_adapter.cpython-37.pyc │ │ ├── moxing_adapter.py │ │ └── .ipynb_checkpoints │ │ │ └── moxing_adapter-checkpoint.py │ ├── post_process.py │ ├── .ipynb_checkpoints │ │ ├── post_process-checkpoint.py │ │ └── callback-checkpoint.py │ ├── callback.py │ └── decode.py │ ├── pre │ ├── __pycache__ │ │ ├── data_clip.cpython-37.pyc │ │ ├── json2txt.cpython-37.pyc │ │ ├── gdal_imread.cpython-37.pyc │ │ └── gen_train_txt.cpython-37.pyc │ ├── json2txt.py │ ├── .ipynb_checkpoints │ │ ├── json2txt-checkpoint.py │ │ ├── pre_main-checkpoint.py │ │ ├── gdal_imread-checkpoint.py │ │ └── gen_train_txt-checkpoint.py │ ├── pre_main.py │ ├── gdal_imread.py │ └── gen_train_txt.py │ ├── run.sh │ ├── pre_train.sh │ ├── .ipynb_checkpoints │ ├── run-checkpoint.sh │ ├── pre_train-checkpoint.sh │ └── torch2mindspore-checkpoint.py │ ├── scripts │ ├── run_eval.sh │ ├── run_standalone_train.sh │ ├── .ipynb_checkpoints │ │ └── run_standalone_train-checkpoint.sh │ ├── run_distribute_train.sh │ └── ascend310_inference.sh │ └── torch2mindspore.py └── README.md /track2/test.sh: -------------------------------------------------------------------------------- 1 | python infer.py -------------------------------------------------------------------------------- /track2/train.sh: -------------------------------------------------------------------------------- 1 | python train.py -------------------------------------------------------------------------------- /track1/pytorch/code/nets/__init__.py: -------------------------------------------------------------------------------- 1 | # -------------------------------------------------------------------------------- /track2/model/modules/backbones/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /track1/pytorch/code/nets/backbone/__init__.py: -------------------------------------------------------------------------------- 1 | # -------------------------------------------------------------------------------- /track2/run.sh: -------------------------------------------------------------------------------- 1 | python train.py 2 | python infer.py 3 | -------------------------------------------------------------------------------- /track2/model/unet.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/unet.pyc -------------------------------------------------------------------------------- /track2/model/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__init__.pyc -------------------------------------------------------------------------------- /track2/model/modules/utils.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/utils.pyc -------------------------------------------------------------------------------- /track2/model/modules/__init__.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__init__.pyc -------------------------------------------------------------------------------- /track2/model/modules/inplace_abn.zip: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/inplace_abn.zip -------------------------------------------------------------------------------- /track2/model/__pycache__/msc.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/msc.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/msc.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/msc.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/unet.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/unet.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/unet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/unet.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/dlinknet.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/dlinknet.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/dlinknet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/dlinknet.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/hr_seg.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/hr_seg.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/hr_seg.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/hr_seg.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/res_unet.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/res_unet.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/res_unet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/res_unet.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/cc.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/cc.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/cc.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/cc.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/res_unet_dbb.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/res_unet_dbb.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/mish.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/mish.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/mish.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/mish.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/scse.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/scse.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/scse.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/scse.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/utils.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/utils.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/utils.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/deeplab_v3_plus.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/deeplab_v3_plus.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/deeplab_v3_plus.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/deeplab_v3_plus.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/efficient_unet.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/efficient_unet.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/efficient_unet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/efficient_unet.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/dblock.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/dblock.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/dblock.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/dblock.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/pooling.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/pooling.cpython-36.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/hrnet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/hrnet.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/loss.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/loss.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/utils.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/nets/__pycache__/hrnet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/nets/__pycache__/hrnet.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/utils/__pycache__/utils.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/utils/__pycache__/utils.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/msc_hierarchical.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/msc_hierarchical.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/__pycache__/msc_hierarchical.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/__pycache__/msc_hierarchical.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/fcn_head.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/fcn_head.cpython-36.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/config.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/config.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/decode.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/decode.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/nets/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/nets/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/strippooling.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/strippooling.cpython-36.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/pre/__pycache__/data_clip.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/pre/__pycache__/data_clip.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/pre/__pycache__/json2txt.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/pre/__pycache__/json2txt.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/callback.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/callback.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/utils_bbox.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/utils_bbox.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/nets/__pycache__/hrnet_ibn.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/nets/__pycache__/hrnet_ibn.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/utils/__pycache__/dataloader.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/utils/__pycache__/dataloader.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/utils/__pycache__/utils_bbox.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/utils/__pycache__/utils_bbox.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/base_ocr_model.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/base_ocr_model.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/dbb_transforms.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/dbb_transforms.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/sep_aspp_head.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/sep_aspp_head.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/hrnet.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/hrnet.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/hrnet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/hrnet.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/senet.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/senet.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/inplace_abn/__pycache__/abn.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/inplace_abn/__pycache__/abn.cpython-36.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/pre/__pycache__/gdal_imread.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/pre/__pycache__/gdal_imread.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/basedataset.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/basedataset.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/post_process.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/post_process.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/road_dataset.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/road_dataset.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/nets/__pycache__/net_training.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/nets/__pycache__/net_training.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/helpers.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/helpers.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/resnest.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/resnest.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/resnest.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/resnest.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/resnet.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/resnet.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/resnet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/resnet.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/se_ibn.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/se_ibn.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/se_ibn.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/se_ibn.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/pre/__pycache__/gen_train_txt.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/pre/__pycache__/gen_train_txt.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/seg_hrnet_ocr.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/seg_hrnet_ocr.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/nets/backbone/__pycache__/hrnet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/nets/backbone/__pycache__/hrnet.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/nets/backbone/__pycache__/modules.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/nets/backbone/__pycache__/modules.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/__pycache__/diversebranchblock.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/__pycache__/diversebranchblock.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/registry.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/registry.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/resnet_ibn.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/resnet_ibn.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/resnet_ibn.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/resnet_ibn.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/resnetvd.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/resnetvd.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/sw_resnet.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/sw_resnet.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/xception.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/xception.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/inplace_abn/__pycache__/__init__.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/inplace_abn/__pycache__/__init__.cpython-36.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/nets/__pycache__/centernet_training.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/nets/__pycache__/centernet_training.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/nets/backbone/__pycache__/__init__.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/nets/backbone/__pycache__/__init__.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/nets/backbone/__pycache__/backbone.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/nets/backbone/__pycache__/backbone.cpython-37.pyc -------------------------------------------------------------------------------- /track1/pytorch/code/nets/backbone/__pycache__/hrnet_ibn.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/pytorch/code/nets/backbone/__pycache__/hrnet_ibn.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/efficientnet.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/efficientnet.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/efficientnet.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/efficientnet.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/res2net_dal.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/res2net_dal.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/resnext_ibn.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/resnext_ibn.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/resnext_ibn.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/resnext_ibn.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/utils_bbox_20221025.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/utils_bbox_20221025.cpython-37.pyc -------------------------------------------------------------------------------- /track1/mindspore/code/src/__pycache__/utils_bbox_20221026.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/__pycache__/utils_bbox_20221026.cpython-37.pyc -------------------------------------------------------------------------------- /track2/model/modules/backbones/__pycache__/resnet_dilated.cpython-36.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track2/model/modules/backbones/__pycache__/resnet_dilated.cpython-36.pyc -------------------------------------------------------------------------------- /track2/model/modules/inplace_abn/__init__.py: -------------------------------------------------------------------------------- 1 | 2 | from .abn import ABN as ActivatedBatchNorm 3 | #from .bn import InPlaceABN as ActivatedBatchNorm 4 | #from .bn import InPlaceABNSync as ActivatedBatchNorm 5 | -------------------------------------------------------------------------------- /track1/mindspore/code/src/model_utils/__pycache__/moxing_adapter.cpython-37.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/WangZhenqing-RS/2022rsipac/HEAD/track1/mindspore/code/src/model_utils/__pycache__/moxing_adapter.cpython-37.pyc -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # [2022年遥感影像智能处理算法大赛rsipac](http://rsipac.whu.edu.cn/index) 2 | 3 | ## 赛道一和赛道二 4 | 5 | ### 线下决赛赛道1:道路提取和交叉口识别 6 | 7 | 选手根据提供的遥感影像识别出道路的分割掩膜和两种交叉口类型区域的定位。 8 | 9 | 决赛二等奖。 10 | 11 | ### 线下决赛赛道2:遥感影像变化检测 12 | 13 | 选手根据前后两时相的遥感影像,提取出地物发生变化的对象级别的斑块。 14 | 15 | 决赛三等奖。 16 | -------------------------------------------------------------------------------- /track2/model/modules/mish.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | 6 | class Mish(nn.Module): 7 | def __init__(self): 8 | super(Mish, self).__init__() 9 | def forward(self, input): 10 | return input*torch.tanh(F.softplus(input)) -------------------------------------------------------------------------------- /track2/model/modules/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | ''' 3 | @File : __init__.py.py 4 | @Contact : whut.hexin@foxmail.com 5 | @License : (C)Copyright 2017-2018, HeXin 6 | 7 | @Modify Time @Author @Version @Desciption 8 | ------------ ------- -------- ----------- 9 | 2020/1/28 13:19 xin 1.0 None 10 | ''' 11 | 12 | -------------------------------------------------------------------------------- /track1/mindspore/code/run.sh: -------------------------------------------------------------------------------- 1 | # python code_mindspore/pre/pre_main.py 2 | # python code_mindspore/train.py --workers 12 --end_epoch 25 --batchsize 8 --total_epoch 100 --end_epoch 23 --lr 0.00005 3 | python code_mindspore/infer_20221114.py --checkpoint_path "./model/epoch_22.ckpt" --checkpoint_path2 "./model/epoch_23.ckpt" 4 | # python code_mindspore/infer_20221120.py --checkpoint_path "./model/epoch_22.ckpt" --checkpoint_path2 "./model/epoch_23.ckpt" --checkpoint_path3 "./model/epoch_21.ckpt" -------------------------------------------------------------------------------- /track1/mindspore/code/pre_train.sh: -------------------------------------------------------------------------------- 1 | python code_mindspore/pre/pre_main.py 2 | python code_mindspore/train.py --workers 12 --end_epoch 25 --batchsize 8 --total_epoch 100 --end_epoch 23 --lr 0.00005 3 | # python code_mindspore/infer_20221114.py --checkpoint_path "./model/epoch_22.ckpt" --checkpoint_path2 "./model/epoch_23.ckpt" 4 | # python code_mindspore/infer_20221120.py --checkpoint_path "./model/epoch_22.ckpt" --checkpoint_path2 "./model/epoch_23.ckpt" --checkpoint_path3 "./model/epoch_21.ckpt" -------------------------------------------------------------------------------- /track1/mindspore/code/.ipynb_checkpoints/run-checkpoint.sh: -------------------------------------------------------------------------------- 1 | python code_mindspore/pre/pre_main.py 2 | python code_mindspore/train.py --workers 12 --end_epoch 25 --batchsize 8 --total_epoch 100 --end_epoch 23 --lr 0.00005 3 | python code_mindspore/infer_20221114.py --checkpoint_path "./model/epoch_22.ckpt" --checkpoint_path2 "./model/epoch_23.ckpt" 4 | # python code_mindspore/infer_20221120.py --checkpoint_path "./model/epoch_22.ckpt" --checkpoint_path2 "./model/epoch_23.ckpt" --checkpoint_path3 "./model/epoch_21.ckpt" -------------------------------------------------------------------------------- /track1/mindspore/code/.ipynb_checkpoints/pre_train-checkpoint.sh: -------------------------------------------------------------------------------- 1 | python code_mindspore/pre/pre_main.py 2 | python code_mindspore/train.py --workers 12 --end_epoch 25 --batchsize 8 --total_epoch 100 --end_epoch 23 --lr 0.00005 3 | # python code_mindspore/infer_20221114.py --checkpoint_path "./model/epoch_22.ckpt" --checkpoint_path2 "./model/epoch_23.ckpt" 4 | # python code_mindspore/infer_20221120.py --checkpoint_path "./model/epoch_22.ckpt" --checkpoint_path2 "./model/epoch_23.ckpt" --checkpoint_path3 "./model/epoch_21.ckpt" -------------------------------------------------------------------------------- /track2/model/.ipynb_checkpoints/__init__-checkpoint.py: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | ''' 3 | @File : __init__.py.py 4 | @Contact : whut.hexin@foxmail.com 5 | @License : (C)Copyright 2017-2018, HeXin 6 | 7 | @Modify Time @Author @Version @Desciption 8 | ------------ ------- -------- ----------- 9 | 2020/1/28 13:18 xin 1.0 None 10 | ''' 11 | 12 | from .unet import Unet 13 | from .res_unet import Res_Unet 14 | from .mf_unet import MfUnet 15 | 16 | 17 | def build_model(cfg): 18 | if cfg.MODEL.NAME == "unet": 19 | model = Unet(cfg.MODEL.N_CHANNEL, cfg.MODEL.N_CLASS, cfg.MODEL.BILINEAR, cfg.MODEL.ENCODE_DIM, cfg.MODEL.DROPOUT) 20 | elif cfg.MODEL.NAME == "res_unet": 21 | model = Res_Unet(cfg.MODEL.N_CHANNEL, cfg.MODEL.N_CLASS, cfg.MODEL.DROPOUT) 22 | elif cfg.MODEL.NAME == "mf_unet": 23 | model = MfUnet(cfg.MODEL.N_CHANNEL, cfg.MODEL.N_CLASS, cfg.MODEL.BILINEAR, cfg.MODEL.ENCODE_DIM, 24 | cfg.MODEL.DROPOUT) 25 | return model 26 | -------------------------------------------------------------------------------- /track1/pytorch/code/pre/cal_mean_std.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Sep 5 18:04:40 2022 4 | 5 | @author: DELL 6 | """ 7 | import cv2 8 | import numpy as np 9 | import glob 10 | import tqdm 11 | 12 | image_paths = glob.glob(r"E:\WangZhenQing\2022HongTu\project_road\data\chusai_release\train\images/*.tif") 13 | image_num = len(image_paths) 14 | images = [] 15 | for image_path in tqdm.tqdm(image_paths): 16 | image = cv2.imread(image_path) 17 | image = cv2.resize(image,(int(image.shape[0]/4),int(image.shape[1]/4))) 18 | # BGR2RGB 19 | image = image.astype(np.float32)[:, :, ::-1] 20 | # HWC2CHW 21 | image = image.transpose((2, 0, 1)) 22 | image = image / 255.0 23 | # 图像尺寸不一,需要拉直后再拼接 24 | image = image.reshape(3,image.shape[1]*image.shape[2]) 25 | images.append(image) 26 | 27 | images = np.concatenate(images,axis=1) 28 | 29 | images_mean = [np.mean(images[0]), np.mean(images[1]), np.mean(images[2])] 30 | image_std = [np.std(images[0]), np.std(images[1]), np.std(images[2])] 31 | print("images_mean") 32 | # [0.38983384, 0.41159928, 0.3616227] 33 | print(images_mean) 34 | print("image_std") 35 | # [0.20442073, 0.17412226, 0.16636926] 36 | print(image_std) -------------------------------------------------------------------------------- /track1/pytorch/code/readme.md: -------------------------------------------------------------------------------- 1 | # [线下决赛赛道1:道路提取和交叉口识别](http://rsipac.whu.edu.cn/subject_one) 2 | 3 | ** 初赛TOP2 ** 4 | 5 | ## 数据预处理 6 | 7 | ### 数据可视化 8 | 9 | pre/draw_box.py 用来可视化训练数据集 10 | 11 | ### json转txt 12 | 13 | pre/json2txt.py 为每个image配置一个内容为[xmin,ymin,xmax,ymax,category_id]的txt。方便后面进行数据裁剪。 14 | 15 | ### 数据裁剪 16 | 17 | pre/data_clip.py 图像尺寸太大,需要进行裁剪,使用滑动窗口裁剪为1024*1024大小。 18 | 19 | ### 生成训练txt 20 | 21 | pre/gen_train_txt.py 生成训练txt,由[image_paths, label_paths, annotations]组成。 22 | 23 | ## 模型训练 24 | 25 | train.py 使用hrnet作为backbone,fcn head作为道路分割头, centernet head作为交叉口检测头。 26 | 27 | ## 模型推理 28 | 29 | infer.py 30 | 31 | ## 结果可视化 32 | 33 | test_vim.py 34 | 35 | 36 | 37 | ## 预训练模型下载地址 38 | model_urls = { 39 | 'hrnetv2_w18' : "https://github.com/bubbliiiing/hrnet-pytorch/releases/download/v1.0/hrnetv2_w18_imagenet_pretrained.pth", 40 | 'hrnetv2_w32' : "https://github.com/bubbliiiing/hrnet-pytorch/releases/download/v1.0/hrnetv2_w32_imagenet_pretrained.pth", 41 | 'hrnetv2_w48' : "https://github.com/bubbliiiing/hrnet-pytorch/releases/download/v1.0/hrnetv2_w48_imagenet_pretrained.pth", 42 | } 43 | 44 | ## 参考 45 | 46 | https://github.com/bubbliiiing/hrnet-pytorch 47 | https://github.com/bubbliiiing/centernet-pytorch 48 | 49 | -------------------------------------------------------------------------------- /track2/edgeBCE_Dice_loss.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Sep 7 22:02:34 2021 4 | 5 | @author: DELL 6 | """ 7 | 8 | import torch 9 | import torch.nn as nn 10 | import torch.nn.functional as F 11 | import numpy as np 12 | import torch.nn as nn 13 | from segmentation_models_pytorch.losses import DiceLoss, SoftCrossEntropyLoss, LovaszLoss 14 | 15 | 16 | def edgeBCE_Dice_loss(pred, target, edge): 17 | 18 | # diceloss在一定程度上可以缓解类别不平衡,但是训练容易不稳定 19 | DiceLoss_fn = DiceLoss(mode='binary', 20 | from_logits=False) 21 | # 交叉熵 22 | BinaryCrossEntropy_fn = nn.BCELoss(reduction='none') 23 | 24 | # 25 | edge_weight = 4. 26 | loss_bce = BinaryCrossEntropy_fn(pred, target) 27 | loss_dice = DiceLoss_fn(pred, target) 28 | edge[edge == 0] = 1. 29 | edge[edge == 255] = edge_weight 30 | loss_bce *= edge 31 | # OHEM 32 | loss_bce_,ind = loss_bce.contiguous().view(-1).sort() 33 | min_value = loss_bce_[int(0.5*loss_bce.numel())] 34 | loss_bce = loss_bce[loss_bce>=min_value] 35 | loss_bce = loss_bce.mean() 36 | loss = loss_bce + loss_dice 37 | return loss 38 | 39 | 40 | # if __name__ == '__main__': 41 | # target=torch.ones((2,1,256,256),dtype=torch.float32) 42 | # input=(torch.ones((2,1,256,256))*0.9) 43 | # input[0,0,0,0] = 0.99 44 | # loss=edgeBCE_Dice_loss(input,target,target*255) 45 | # print(loss) 46 | 47 | -------------------------------------------------------------------------------- /track2/model/modules/scse.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | from functools import partial 6 | 7 | nonlinearity = partial(F.relu,inplace=True) 8 | 9 | class sSE(nn.Module): 10 | def __init__(self, out_channels): 11 | super(sSE, self).__init__() 12 | self.conv = nn.Sequential( 13 | nn.Conv2d(in_channels=out_channels,out_channels=1,kernel_size=1,padding=0), 14 | nn.BatchNorm2d(1)) 15 | def forward(self,x): 16 | x=self.conv(x) 17 | #print('spatial',x.size()) 18 | x=F.sigmoid(x) 19 | return x 20 | 21 | class cSE(nn.Module): 22 | def __init__(self, out_channels, act_fun=nonlinearity): 23 | super(cSE, self).__init__() 24 | self.conv1 = nn.Sequential( 25 | nn.Conv2d(in_channels=out_channels,out_channels=int(out_channels/2),kernel_size=1,padding=0), 26 | nn.BatchNorm2d(int(out_channels/2)) 27 | ) 28 | self.conv2 = nn.Sequential( 29 | nn.Conv2d(in_channels=int(out_channels/2),out_channels=out_channels,kernel_size=1,padding=0), 30 | nn.BatchNorm2d(out_channels) 31 | ) 32 | self.act_fun = act_fun 33 | def forward(self,x): 34 | x=nn.AvgPool2d(x.size()[2:])(x) 35 | #print('channel',x.size()) 36 | x=self.conv1(x) 37 | x=self.act_fun(x) 38 | x=self.conv2(x) 39 | x=F.sigmoid(x) 40 | return x -------------------------------------------------------------------------------- /track2/model/modules/inplace_abn/src/inplace_abn.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | #include 6 | 7 | std::vector mean_var_cpu(at::Tensor x); 8 | std::vector mean_var_cuda(at::Tensor x); 9 | 10 | at::Tensor forward_cpu(at::Tensor x, at::Tensor mean, at::Tensor var, at::Tensor weight, at::Tensor bias, 11 | bool affine, float eps); 12 | at::Tensor forward_cuda(at::Tensor x, at::Tensor mean, at::Tensor var, at::Tensor weight, at::Tensor bias, 13 | bool affine, float eps); 14 | 15 | std::vector edz_eydz_cpu(at::Tensor z, at::Tensor dz, at::Tensor weight, at::Tensor bias, 16 | bool affine, float eps); 17 | std::vector edz_eydz_cuda(at::Tensor z, at::Tensor dz, at::Tensor weight, at::Tensor bias, 18 | bool affine, float eps); 19 | 20 | std::vector backward_cpu(at::Tensor z, at::Tensor dz, at::Tensor var, at::Tensor weight, at::Tensor bias, 21 | at::Tensor edz, at::Tensor eydz, bool affine, float eps); 22 | std::vector backward_cuda(at::Tensor z, at::Tensor dz, at::Tensor var, at::Tensor weight, at::Tensor bias, 23 | at::Tensor edz, at::Tensor eydz, bool affine, float eps); 24 | 25 | void leaky_relu_backward_cpu(at::Tensor z, at::Tensor dz, float slope); 26 | void leaky_relu_backward_cuda(at::Tensor z, at::Tensor dz, float slope); 27 | 28 | void elu_backward_cpu(at::Tensor z, at::Tensor dz); 29 | void elu_backward_cuda(at::Tensor z, at::Tensor dz); -------------------------------------------------------------------------------- /track1/pytorch/code/nets/backbone/modules.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | 4 | 5 | class IBN(nn.Module): 6 | r"""Instance-Batch Normalization layer from 7 | `"Two at Once: Enhancing Learning and Generalization Capacities via IBN-Net" 8 | ` 9 | 10 | Args: 11 | planes (int): Number of channels for the input tensor 12 | ratio (float): Ratio of instance normalization in the IBN layer 13 | """ 14 | def __init__(self, planes, ratio=0.5): 15 | super(IBN, self).__init__() 16 | self.half = int(planes * ratio) 17 | self.IN = nn.InstanceNorm2d(self.half, affine=True) 18 | self.BN = nn.BatchNorm2d(planes - self.half) 19 | 20 | def forward(self, x): 21 | split = torch.split(x, self.half, 1) 22 | out1 = self.IN(split[0].contiguous()) 23 | out2 = self.BN(split[1].contiguous()) 24 | out = torch.cat((out1, out2), 1) 25 | return out 26 | 27 | 28 | class SELayer(nn.Module): 29 | def __init__(self, channel, reduction=16): 30 | super(SELayer, self).__init__() 31 | self.avg_pool = nn.AdaptiveAvgPool2d(1) 32 | self.fc = nn.Sequential( 33 | nn.Linear(channel, int(channel/reduction), bias=False), 34 | nn.ReLU(inplace=True), 35 | nn.Linear(int(channel/reduction), channel, bias=False), 36 | nn.Sigmoid() 37 | ) 38 | 39 | def forward(self, x): 40 | b, c, _, _ = x.size() 41 | y = self.avg_pool(x).view(b, c) 42 | y = self.fc(y).view(b, c, 1, 1) 43 | return x * y.expand_as(x) 44 | -------------------------------------------------------------------------------- /track1/pytorch/code/pre/json2txt.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Aug 25 09:14:23 2022 4 | 5 | @author: DELL 6 | """ 7 | import os 8 | import json 9 | import tqdm 10 | import shutil 11 | 12 | def get_img_annos(img_id, annos): 13 | 14 | anno_list = [] 15 | for anno_info in annos["annotations"]: 16 | if anno_info['image_id'] == img_id: 17 | anno_bbox = anno_info["bbox"] 18 | xmin = anno_bbox[0] 19 | ymin = anno_bbox[1] 20 | w = anno_bbox[2] 21 | h = anno_bbox[3] 22 | xmax = xmin + w 23 | ymax = ymin + h 24 | # 1,2 -> 0,1 25 | category_id = anno_info["category_id"]-1 26 | anno_list.append([xmin,ymin,xmax,ymax,category_id]) 27 | else: 28 | continue 29 | return anno_list 30 | 31 | 32 | json_path = "../../data/chusai_release/train/instances_train.json" 33 | 34 | save_folder = "../../data/chusai_release/train/annotations" 35 | 36 | if os.path.exists(save_folder): 37 | shutil.rmtree(save_folder) 38 | os.makedirs(save_folder) 39 | 40 | with open(json_path, encoding="utf-8") as f: 41 | annotations_train = json.load(f) 42 | 43 | 44 | for img_info in tqdm.tqdm(annotations_train["images"]): 45 | image_name = img_info['file_name'] 46 | image_id = img_info['id'] 47 | image_annos = get_img_annos(image_id, annotations_train) 48 | 49 | image_anno_path = os.path.join(save_folder,image_name.replace("tif","txt")) 50 | with open(image_anno_path, 'a') as f: 51 | for image_anno in image_annos: 52 | f.write(f"{image_anno[0]} {image_anno[1]} {image_anno[2]} {image_anno[3]} {image_anno[4]}\n") 53 | 54 | # break 55 | 56 | -------------------------------------------------------------------------------- /track1/mindspore/code/scripts/run_eval.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2021 Huawei Technologies Co., Ltd 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================ 16 | 17 | if [ $# != 3 ] 18 | then 19 | echo "Using: bash scripts/run_eval.sh [DEVICE_ID] [DATASET_PATH] [CHECKPOINT_PATH]" 20 | exit 1 21 | fi 22 | 23 | get_real_path(){ 24 | if [ "${1:0:1}" == "/" ]; then 25 | echo "$1" 26 | else 27 | echo "$(realpath -m $PWD/$1)" 28 | fi 29 | } 30 | 31 | PATH1=$(get_real_path $2) 32 | PATH2=$(get_real_path $3) 33 | 34 | if [ ! -d $PATH1 ] 35 | then 36 | echo "error: DATASET_PATH=$PATH1 is not a dictionary." 37 | exit 1 38 | fi 39 | 40 | if [ ! -f $PATH2 ] 41 | then 42 | echo "error: CHECKPOINT_PATH=$PATH2 is not a file." 43 | exit 1 44 | fi 45 | 46 | export DEVICE_NUM=1 47 | export DEVICE_ID=$1 48 | export RANK_SIZE=$DEVICE_NUM 49 | export RANK_ID=0 50 | 51 | if [ -d "eval" ]; 52 | then 53 | rm -rf ./eval 54 | fi 55 | mkdir ./eval 56 | cp ./eval.py ./eval 57 | cp -r ./src ./eval 58 | cd ./eval || exit 59 | env > env.log 60 | echo "start evaluation for device $DEVICE_ID" 61 | python3 eval.py --data_path $PATH1 --device_target Ascend --modelarts False --checkpoint_path $PATH2 &> log & 62 | cd .. 63 | -------------------------------------------------------------------------------- /track1/mindspore/code/pre/json2txt.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Aug 25 09:14:23 2022 4 | 5 | @author: DELL 6 | """ 7 | import os 8 | import json 9 | import tqdm 10 | import shutil 11 | 12 | def get_img_annos(img_id, annos): 13 | 14 | anno_list = [] 15 | for anno_info in annos["annotations"]: 16 | if anno_info['image_id'] == img_id: 17 | anno_bbox = anno_info["bbox"] 18 | xmin = anno_bbox[0] 19 | ymin = anno_bbox[1] 20 | w = anno_bbox[2] 21 | h = anno_bbox[3] 22 | xmax = xmin + w 23 | ymax = ymin + h 24 | # 1,2 -> 0,1 25 | category_id = anno_info["category_id"]-1 26 | anno_list.append([xmin,ymin,xmax,ymax,category_id]) 27 | else: 28 | continue 29 | return anno_list 30 | 31 | 32 | def json2txt(json_path, save_folder): 33 | 34 | if os.path.exists(save_folder): 35 | shutil.rmtree(save_folder) 36 | os.makedirs(save_folder) 37 | 38 | with open(json_path, encoding="utf-8") as f: 39 | annotations_train = json.load(f) 40 | 41 | for img_info in tqdm.tqdm(annotations_train["images"]): 42 | image_name = img_info['file_name'] 43 | image_id = img_info['id'] 44 | image_annos = get_img_annos(image_id, annotations_train) 45 | 46 | image_anno_path = os.path.join(save_folder,image_name.replace("tif","txt")) 47 | with open(image_anno_path, 'a') as f: 48 | for image_anno in image_annos: 49 | f.write(f"{image_anno[0]} {image_anno[1]} {image_anno[2]} {image_anno[3]} {image_anno[4]}\n") 50 | 51 | # break 52 | 53 | if __name__ == '__main__': 54 | json_path = "../../data/fusai_release/train/instances_train.json" 55 | save_folder = "../../data/fusai_release/train/annotations" 56 | json2txt(json_path, save_folder) -------------------------------------------------------------------------------- /track1/mindspore/code/pre/.ipynb_checkpoints/json2txt-checkpoint.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Aug 25 09:14:23 2022 4 | 5 | @author: DELL 6 | """ 7 | import os 8 | import json 9 | import tqdm 10 | import shutil 11 | 12 | def get_img_annos(img_id, annos): 13 | 14 | anno_list = [] 15 | for anno_info in annos["annotations"]: 16 | if anno_info['image_id'] == img_id: 17 | anno_bbox = anno_info["bbox"] 18 | xmin = anno_bbox[0] 19 | ymin = anno_bbox[1] 20 | w = anno_bbox[2] 21 | h = anno_bbox[3] 22 | xmax = xmin + w 23 | ymax = ymin + h 24 | # 1,2 -> 0,1 25 | category_id = anno_info["category_id"]-1 26 | anno_list.append([xmin,ymin,xmax,ymax,category_id]) 27 | else: 28 | continue 29 | return anno_list 30 | 31 | 32 | def json2txt(json_path, save_folder): 33 | 34 | if os.path.exists(save_folder): 35 | shutil.rmtree(save_folder) 36 | os.makedirs(save_folder) 37 | 38 | with open(json_path, encoding="utf-8") as f: 39 | annotations_train = json.load(f) 40 | 41 | for img_info in tqdm.tqdm(annotations_train["images"]): 42 | image_name = img_info['file_name'] 43 | image_id = img_info['id'] 44 | image_annos = get_img_annos(image_id, annotations_train) 45 | 46 | image_anno_path = os.path.join(save_folder,image_name.replace("tif","txt")) 47 | with open(image_anno_path, 'a') as f: 48 | for image_anno in image_annos: 49 | f.write(f"{image_anno[0]} {image_anno[1]} {image_anno[2]} {image_anno[3]} {image_anno[4]}\n") 50 | 51 | # break 52 | 53 | if __name__ == '__main__': 54 | json_path = "../../data/fusai_release/train/instances_train.json" 55 | save_folder = "../../data/fusai_release/train/annotations" 56 | json2txt(json_path, save_folder) -------------------------------------------------------------------------------- /track2/model/modules/dbb_transforms.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | import torch.nn.functional as F 4 | 5 | def transI_fusebn(kernel, bn): 6 | gamma = bn.weight 7 | std = (bn.running_var + bn.eps).sqrt() 8 | return kernel * ((gamma / std).reshape(-1, 1, 1, 1)), bn.bias - bn.running_mean * gamma / std 9 | 10 | def transII_addbranch(kernels, biases): 11 | return sum(kernels), sum(biases) 12 | 13 | def transIII_1x1_kxk(k1, b1, k2, b2, groups): 14 | if groups == 1: 15 | k = F.conv2d(k2, k1.permute(1, 0, 2, 3)) # 16 | b_hat = (k2 * b1.reshape(1, -1, 1, 1)).sum((1, 2, 3)) 17 | else: 18 | k_slices = [] 19 | b_slices = [] 20 | k1_T = k1.permute(1, 0, 2, 3) 21 | k1_group_width = k1.size(0) // groups 22 | k2_group_width = k2.size(0) // groups 23 | for g in range(groups): 24 | k1_T_slice = k1_T[:, g*k1_group_width:(g+1)*k1_group_width, :, :] 25 | k2_slice = k2[g*k2_group_width:(g+1)*k2_group_width, :, :, :] 26 | k_slices.append(F.conv2d(k2_slice, k1_T_slice)) 27 | b_slices.append((k2_slice * b1[g*k1_group_width:(g+1)*k1_group_width].reshape(1, -1, 1, 1)).sum((1, 2, 3))) 28 | k, b_hat = transIV_depthconcat(k_slices, b_slices) 29 | return k, b_hat + b2 30 | 31 | def transIV_depthconcat(kernels, biases): 32 | return torch.cat(kernels, dim=0), torch.cat(biases) 33 | 34 | def transV_avg(channels, kernel_size, groups): 35 | input_dim = channels // groups 36 | k = torch.zeros((channels, input_dim, kernel_size, kernel_size)) 37 | k[np.arange(channels), np.tile(np.arange(input_dim), groups), :, :] = 1.0 / kernel_size ** 2 38 | return k 39 | 40 | # This has not been tested with non-square kernels (kernel.size(2) != kernel.size(3)) nor even-size kernels 41 | def transVI_multiscale(kernel, target_kernel_size): 42 | H_pixels_to_pad = (target_kernel_size - kernel.size(2)) // 2 43 | W_pixels_to_pad = (target_kernel_size - kernel.size(3)) // 2 44 | return F.pad(kernel, [H_pixels_to_pad, H_pixels_to_pad, W_pixels_to_pad, W_pixels_to_pad]) -------------------------------------------------------------------------------- /track2/style_transfer.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Aug 13 20:08:26 2021 4 | 5 | @author: DELL 6 | """ 7 | 8 | import cv2 9 | import numpy as np 10 | import os 11 | import string 12 | import random 13 | 14 | def style_transfer(source_image, target_image): 15 | h, w, c = source_image.shape 16 | out = [] 17 | for i in range(c): 18 | source_image_f = np.fft.fft2(source_image[:,:,i]) 19 | source_image_fshift = np.fft.fftshift(source_image_f) 20 | target_image_f = np.fft.fft2(target_image[:,:,i]) 21 | target_image_fshift = np.fft.fftshift(target_image_f) 22 | 23 | change_length = 1 24 | source_image_fshift[int(h/2)-change_length:int(h/2)+change_length, 25 | int(h/2)-change_length:int(h/2)+change_length] = \ 26 | target_image_fshift[int(h/2)-change_length:int(h/2)+change_length, 27 | int(h/2)-change_length:int(h/2)+change_length] 28 | 29 | source_image_ifshift = np.fft.ifftshift(source_image_fshift) 30 | source_image_if = np.fft.ifft2(source_image_ifshift) 31 | source_image_if = np.abs(source_image_if) 32 | 33 | source_image_if[source_image_if>255] = np.max(source_image[:,:,i]) 34 | out.append(source_image_if) 35 | out = np.array(out) 36 | out = out.swapaxes(1,0).swapaxes(1,2) 37 | 38 | # # 结果中含有>255的值,拉伸或者强制=255效果都不好,但是cv2.imwrite再read效果好 39 | # # 有时间探究一下原因 40 | # # 生成数字+字母 41 | # token = string.ascii_letters + string.digits 42 | # # 随机选择指定长度随机码 43 | # token = random.sample(token,15) 44 | # token_str = ''.join(token) 45 | # temp_path = "temp_{}.png".format(token_str) 46 | # if not os.path.exists(temp_path): cv2.imwrite(temp_path, out) 47 | # out = cv2.imread(temp_path) 48 | # if os.path.exists(temp_path): os.remove(temp_path) 49 | out = out.astype(np.uint8) 50 | return out 51 | 52 | 53 | # target_image = cv2.imread(r"15_B.tif") 54 | # source_image = cv2.imread(r"15_A.tif") 55 | 56 | 57 | # out = style_transfer(source_image, target_image) 58 | 59 | # cv2.imwrite("15_a_out.png",out) 60 | -------------------------------------------------------------------------------- /track2/model/modules/inplace_abn/abn.py: -------------------------------------------------------------------------------- 1 | 2 | import torch.nn as nn 3 | import torch.nn.functional as functional 4 | 5 | 6 | class ABN(nn.BatchNorm2d): 7 | """Activated Batch Normalization 8 | 9 | This gathers a `BatchNorm2d` and an activation function in a single module 10 | """ 11 | 12 | def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True, 13 | activation="leaky_relu", slope=0.01): 14 | """Creates an Activated Batch Normalization module 15 | 16 | Parameters 17 | ---------- 18 | num_features : int 19 | Number of feature channels in the input and output. 20 | eps : float 21 | Small constant to prevent numerical issues. 22 | momentum : float 23 | Momentum factor applied to compute running statistics as. 24 | affine : bool 25 | If `True` apply learned scale and shift transformation after normalization. 26 | activation : str 27 | Name of the activation functions, one of: `leaky_relu`, `elu` or `none`. 28 | slope : float 29 | Negative slope for the `leaky_relu` activation. 30 | """ 31 | super().__init__(num_features=num_features, eps=eps, momentum=momentum, affine=affine, 32 | track_running_stats=track_running_stats) 33 | 34 | if activation not in ("leaky_relu", "elu", "none"): 35 | raise NotImplementedError(activation) 36 | 37 | self.activation = activation 38 | self.slope = slope 39 | 40 | def forward(self, x): 41 | x = super().forward(x) 42 | 43 | if self.activation == "leaky_relu": 44 | return functional.leaky_relu(x, negative_slope=self.slope, inplace=True) 45 | elif self.activation == "elu": 46 | return functional.elu(x, inplace=True) 47 | else: 48 | return x 49 | 50 | def extra_repr(self): 51 | rep = super().extra_repr() 52 | rep += ', activation={activation}'.format(**self.__dict__) 53 | if self.activation == "leaky_relu": 54 | rep += ', slope={slope}'.format(**self.__dict__) 55 | return rep -------------------------------------------------------------------------------- /track1/pytorch/code/pre/gen_train_txt.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Aug 26 09:14:11 2022 4 | 5 | @author: DELL 6 | """ 7 | import cv2 8 | import glob 9 | import tqdm 10 | import os 11 | 12 | 13 | ''' 14 | 生成train.txt val.txt 15 | ''' 16 | 17 | txt_paths = sorted(glob.glob(r"E:\WangZhenQing\2022HongTu\project_road\data\chusai_release\train\annotations_crop\*.txt")) 18 | 19 | fold = 0 20 | train_txt_path = f'../train_{fold}.txt' 21 | val_txt_path = f'../val_{fold}.txt' 22 | train_all_txt_path = '../train_all.txt' 23 | 24 | if os.path.exists(train_txt_path): os.remove(train_txt_path) 25 | if os.path.exists(val_txt_path): os.remove(val_txt_path) 26 | if os.path.exists(train_all_txt_path): os.remove(train_all_txt_path) 27 | 28 | # 打开要写的train_txt_path 29 | train_txt_f = open(train_txt_path, 'a') 30 | val_txt_f = open(val_txt_path, 'a') 31 | train_all_txt_f = open(train_all_txt_path, 'a') 32 | 33 | for i, txt_path in enumerate(txt_paths): 34 | image_path = txt_path.replace("annotations_crop", "images_crop").replace("txt", "tif") 35 | label_path = txt_path.replace("annotations_crop", "masks_crop").replace("txt", "png") 36 | 37 | train_all_txt_f.write(image_path+" "+label_path) 38 | if i%5!=fold: 39 | # 在train_txt_path中写入图像路径 40 | train_txt_f.write(image_path+" "+label_path) 41 | else: 42 | val_txt_f.write(image_path+" "+label_path) 43 | 44 | with open(txt_path, "r") as f: # 打开文件 45 | data = f.read() # 读取文件 46 | annotations = data.split("\n") 47 | for annotation in annotations: 48 | 49 | annotation = annotation.split(" ") 50 | 51 | if len(annotation)<5: 52 | continue 53 | min_x,min_y,max_x,max_y,category = annotation 54 | 55 | train_all_txt_f.write(f' {min_x},{min_y},{max_x},{max_y},{category}') 56 | if i%5!=fold: 57 | train_txt_f.write(f' {min_x},{min_y},{max_x},{max_y},{category}') 58 | else: 59 | val_txt_f.write(f' {min_x},{min_y},{max_x},{max_y},{category}') 60 | train_all_txt_f.write('\n') 61 | if i%5!=fold: 62 | train_txt_f.write('\n') 63 | else: 64 | val_txt_f.write('\n') -------------------------------------------------------------------------------- /track2/model/__init__.py: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | ''' 3 | @File : __init__.py.py 4 | @Contact : whut.hexin@foxmail.com 5 | @License : (C)Copyright 2017-2018, HeXin 6 | 7 | @Modify Time @Author @Version @Desciption 8 | ------------ ------- -------- ----------- 9 | 2020/1/28 13:18 xin 1.0 None 10 | ''' 11 | 12 | from .unet import Unet 13 | from .res_unet import Res_Unet 14 | 15 | from .efficient_unet import EfficientUnet 16 | 17 | from .hr_seg import HRNet 18 | from .deeplab_v3_plus import DeepV3Plus 19 | from .dlinknet import DLinkNet 20 | from .hr_seg import HRNet 21 | 22 | from .msc import MSC 23 | from .msc_hierarchical import Basic 24 | # from .double_res_unet import Double_Res_Unet 25 | 26 | 27 | def build_model(cfg): 28 | if cfg.MODEL.NAME == "unet": 29 | model = Unet(cfg.MODEL.N_CHANNEL, cfg.MODEL.N_CLASS, cfg.MODEL.UNET.BILINEAR, cfg.MODEL.UNET.ENCODE_DIM, cfg.MODEL.DROPOUT, cfg.MODEL.UNET.SCSE, cfg.MODEL.UNET.DBLOCK, cfg.MODEL.UNET.ATTENTION_BLOCK, cfg.MODEL.UNET.RRCNN_BLOCK, cfg.MODEL.UNET.RRCNN_BLOCK_T) 30 | elif cfg.MODEL.NAME == "efficient_unet": 31 | model = EfficientUnet(cfg.MODEL.N_CHANNEL, cfg.MODEL.N_CLASS, cfg.MODEL.EFFICIENT_UNET.BACKBONE_NAME, cfg.MODEL.WEIGHT, cfg.MODEL.DROPOUT, cfg.MODEL.EFFICIENT_UNET.CONCAT_INPUT, cfg.MODEL.EFFICIENT_UNET.ATTENTION_BLOCK, cfg.MODEL.EFFICIENT_UNET.SCSE) 32 | elif cfg.MODEL.NAME == "deeplab_v3_plus": 33 | model = DeepV3Plus(cfg.MODEL.N_CHANNEL, cfg.MODEL.N_CLASS, cfg.MODEL.DEEPLAB_V3_PLUS.BACKBONE_NAME, cfg.MODEL.WEIGHT) 34 | elif cfg.MODEL.NAME == "dlinknet": 35 | model = DLinkNet(cfg.MODEL.N_CHANNEL, cfg.MODEL.N_CLASS, cfg.MODEL.DLINKNET.BACKBONE_NAME, cfg.MODEL.WEIGHT, cfg.MODEL.DLINKNET.SCSE, cfg.MODEL.DLINKNET.MISH) 36 | elif cfg.MODEL.NAME == "hrnet": 37 | model = HRNet(cfg.MODEL.N_CHANNEL, cfg.MODEL.N_CLASS, cfg.MODEL.HRNET.BACKBONE_NAME, cfg.MODEL.WEIGHT, cfg.MODEL.DROPOUT) 38 | elif cfg.MODEL.NAME == "res_unet": 39 | model = Res_Unet(cfg.MODEL.N_CHANNEL, cfg.MODEL.N_CLASS, cfg.MODEL.RES_UNET.BACKBONE_NAME, cfg.MODEL.WEIGHT, cfg.MODEL.DROPOUT, cfg.MODEL.RES_UNET.SCSE, cfg.MODEL.RES_UNET.MISH, cfg.MODEL.RES_UNET.DB_BLOCK, cfg.MODEL.RES_UNET.HYPERCOLUMN) 40 | 41 | if cfg.SOLVER.MULTI_SCALE: 42 | model = MSC(base=model, scales=cfg.SOLVER.MULTI_SCALE) 43 | # model = Basic(base=model) 44 | 45 | return model 46 | 47 | -------------------------------------------------------------------------------- /track2/model/msc.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | # coding: utf-8 3 | # 4 | # Author: Kazuto Nakashima 5 | # URL: http://kazuto1011.github.io 6 | # Created: 2018-03-26 7 | 8 | import torch 9 | import torch.nn as nn 10 | import torch.nn.functional as F 11 | 12 | 13 | class MSC(nn.Module): 14 | """ 15 | Multi-scale inputs 16 | """ 17 | 18 | def __init__(self, base, scales=None): 19 | super(MSC, self).__init__() 20 | self.base = base 21 | if scales: 22 | self.scales = scales 23 | else: 24 | self.scales = [0.5, 0.75] 25 | 26 | def forward(self, x): 27 | # Original 28 | logits = self.base(x) 29 | if isinstance(logits, list) or isinstance(logits, tuple): 30 | _, _, H, W = logits[0].shape 31 | interp = lambda l: F.interpolate(l, size=(H, W), mode="bilinear", align_corners=False) 32 | new_logits_pyramid = [[]]*len(logits) 33 | for p in self.scales: 34 | h = F.interpolate(x, scale_factor=p, mode="bilinear", align_corners=False) 35 | pyramid_res = self.base(h) 36 | for i, pyramid_item in enumerate(pyramid_res): 37 | new_logits_pyramid[i].append(pyramid_item) 38 | 39 | 40 | # Pixel-wise max 41 | #logits_all = [logits] + [interp(l) for l in logits_pyramid] 42 | #logits_max = torch.max(torch.stack(logits_all), dim=0)[0] 43 | logits_all = [] 44 | logits_max = [] 45 | for _logits, logits_pyramid in zip(logits, new_logits_pyramid): 46 | _logits_all = [_logits] + [interp(l) for l in logits_pyramid] 47 | _logits_max = torch.max(torch.stack(_logits_all), dim=0)[0] 48 | logits_all.append([_logits]+logits_pyramid+[_logits_max]) 49 | logits_max.append(_logits_max) 50 | if self.training: 51 | return logits_all 52 | else: 53 | return logits_max 54 | else: 55 | _, _, H, W = logits.shape 56 | interp = lambda l: F.interpolate(l, size=(H, W), mode="bilinear", align_corners=False) 57 | # Scaled 58 | logits_pyramid = [] 59 | # Pixel-wise max 60 | logits_all = [logits] + [interp(l) for l in logits_pyramid] 61 | logits_max = torch.max(torch.stack(logits_all), dim=0)[0] 62 | if self.training: 63 | return [logits_max]+[logits] + logits_pyramid 64 | else: 65 | return logits_max 66 | 67 | 68 | 69 | 70 | -------------------------------------------------------------------------------- /track1/pytorch/code/utils/utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Aug 25 20:39:02 2022 4 | 5 | @author: DELL 6 | """ 7 | 8 | import cv2 9 | import numpy as np 10 | 11 | 12 | def randomHueSaturationValue(image, hue_shift_limit=(-180, 180), 13 | sat_shift_limit=(-255, 255), 14 | val_shift_limit=(-255, 255), u=0.5): 15 | if np.random.random() < u: 16 | image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV) 17 | h, s, v = cv2.split(image) 18 | hue_shift = np.random.randint(hue_shift_limit[0], hue_shift_limit[1]+1) 19 | hue_shift = np.uint8(hue_shift) 20 | h += hue_shift 21 | sat_shift = np.random.uniform(sat_shift_limit[0], sat_shift_limit[1]) 22 | s = cv2.add(s, sat_shift) 23 | val_shift = np.random.uniform(val_shift_limit[0], val_shift_limit[1]) 24 | v = cv2.add(v, val_shift) 25 | image = cv2.merge((h, s, v)) 26 | image = cv2.cvtColor(image, cv2.COLOR_HSV2BGR) 27 | return image 28 | 29 | 30 | def randomHorizontalFlip(image, label, batch_hm, batch_wh, batch_reg, batch_reg_mask, u=0.5): 31 | if np.random.random() < u: 32 | image = cv2.flip(image, 1) 33 | label = cv2.flip(label, 1) 34 | batch_hm = cv2.flip(batch_hm, 1) 35 | batch_wh = cv2.flip(batch_wh, 1) 36 | batch_reg = cv2.flip(batch_reg, 1) 37 | batch_reg_mask = cv2.flip(batch_reg_mask, 1) 38 | 39 | return image, label, batch_hm, batch_wh, batch_reg, batch_reg_mask 40 | 41 | 42 | def randomVerticleFlip(image, label, batch_hm, batch_wh, batch_reg, batch_reg_mask, u=0.5): 43 | if np.random.random() < u: 44 | image = cv2.flip(image, 0) 45 | label = cv2.flip(label, 0) 46 | batch_hm = cv2.flip(batch_hm, 0) 47 | batch_wh = cv2.flip(batch_wh, 0) 48 | batch_reg = cv2.flip(batch_reg, 0) 49 | batch_reg_mask = cv2.flip(batch_reg_mask, 0) 50 | 51 | return image, label, batch_hm, batch_wh, batch_reg, batch_reg_mask 52 | 53 | 54 | def randomRotate90(image, label, batch_hm, batch_wh, batch_reg, batch_reg_mask, u=0.5): 55 | if np.random.random() < u: 56 | image=np.rot90(image) 57 | label=np.rot90(label) 58 | batch_hm=np.rot90(batch_hm) 59 | batch_wh=np.rot90(batch_wh) 60 | batch_reg=np.rot90(batch_reg) 61 | batch_reg_mask=np.rot90(batch_reg_mask) 62 | 63 | return image, label, batch_hm, batch_wh, batch_reg, batch_reg_mask 64 | 65 | 66 | def preprocess_input(image): 67 | mean = [0.38983384, 0.41159928, 0.3616227] 68 | std = [0.20442073, 0.17412226, 0.1663693] 69 | return (image / 255. - mean) / std 70 | 71 | -------------------------------------------------------------------------------- /track2/edge_utils.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Tue Sep 7 20:24:41 2021 4 | 5 | @author: DELL 6 | """ 7 | 8 | import cv2 9 | import os 10 | import numpy as np 11 | import torch 12 | import torch.nn as nn 13 | from tqdm import tqdm 14 | from scipy.ndimage.morphology import distance_transform_edt 15 | 16 | 17 | def onehot_to_multiclass_edges(mask, radius, num_classes): 18 | """ 19 | Converts a segmentation mask (K,H,W) to an edgemap (K,H,W) 20 | """ 21 | if radius < 0: 22 | return mask 23 | 24 | # We need to pad the borders for boundary conditions 25 | mask_pad = np.pad(mask, ((0, 0), (1, 1), (1, 1)), mode='constant', constant_values=0) 26 | 27 | channels = [] 28 | for i in range(num_classes): 29 | dist = distance_transform_edt(mask_pad[i, :]) + distance_transform_edt(1.0 - mask_pad[i, :]) 30 | dist = dist[1:-1, 1:-1] 31 | dist[dist > radius] = 0 32 | dist = (dist > 0).astype(np.uint8) 33 | channels.append(dist) 34 | 35 | return np.array(channels) 36 | 37 | 38 | def onehot_to_binary_edges(mask, radius, num_classes): 39 | """ 40 | Converts a segmentation mask (K,H,W) to a binary edgemap (H,W) 41 | """ 42 | 43 | if radius < 0: 44 | return mask 45 | 46 | # We need to pad the borders for boundary conditions 47 | mask_pad = np.pad(mask, ((0, 0), (1, 1), (1, 1)), mode='constant', constant_values=0) 48 | 49 | edgemap = np.zeros(mask.shape[1:]) 50 | for i in range(num_classes): 51 | # 提取轮廓 52 | dist = distance_transform_edt(mask_pad[i, :]) + distance_transform_edt(1.0 - mask_pad[i, :]) 53 | dist = dist[1:-1, 1:-1] 54 | dist[dist > radius] = 0 55 | edgemap += dist 56 | edgemap = (edgemap > 0).astype(np.uint8)*255 57 | return edgemap 58 | 59 | 60 | def mask_to_onehot(mask, num_classes): 61 | """ 62 | Converts a segmentation mask (H,W) to (K,H,W) where the last dim is a one 63 | hot encoding vector 64 | """ 65 | _mask = [mask == (i) for i in range(num_classes)] 66 | return np.array(_mask).astype(np.uint8) 67 | 68 | # if __name__ == '__main__': 69 | # label = cv2.imread(r"E:\WangZhenQing\2021GaoFen\code\41_1_label.png",0) 70 | # label[label==255] = 1 71 | # print(np.unique(label)) 72 | # img = cv2.imread(r"E:\WangZhenQing\2021GaoFen\code\41_1.png") 73 | # oneHot_label = mask_to_onehot(label, 2) 74 | # edge = onehot_to_binary_edges(oneHot_label, 2, 2) # #edge=255,background=0 75 | # edge[:2, :] = 0 76 | # edge[-2:, :] = 0 77 | # edge[:, :2] = 0 78 | # edge[:, -2:] = 0 79 | # # print(edge) 80 | # print(np.unique(edge)) 81 | # print(edge.shape) 82 | # cv2.imwrite('test.png',edge) -------------------------------------------------------------------------------- /track2/model/modules/inplace_abn/src/inplace_abn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include "inplace_abn.h" 6 | 7 | std::vector mean_var(at::Tensor x) { 8 | if (x.is_cuda()) { 9 | return mean_var_cuda(x); 10 | } else { 11 | return mean_var_cpu(x); 12 | } 13 | } 14 | 15 | at::Tensor forward(at::Tensor x, at::Tensor mean, at::Tensor var, at::Tensor weight, at::Tensor bias, 16 | bool affine, float eps) { 17 | if (x.is_cuda()) { 18 | return forward_cuda(x, mean, var, weight, bias, affine, eps); 19 | } else { 20 | return forward_cpu(x, mean, var, weight, bias, affine, eps); 21 | } 22 | } 23 | 24 | std::vector edz_eydz(at::Tensor z, at::Tensor dz, at::Tensor weight, at::Tensor bias, 25 | bool affine, float eps) { 26 | if (z.is_cuda()) { 27 | return edz_eydz_cuda(z, dz, weight, bias, affine, eps); 28 | } else { 29 | return edz_eydz_cpu(z, dz, weight, bias, affine, eps); 30 | } 31 | } 32 | 33 | std::vector backward(at::Tensor z, at::Tensor dz, at::Tensor var, at::Tensor weight, at::Tensor bias, 34 | at::Tensor edz, at::Tensor eydz, bool affine, float eps) { 35 | if (z.is_cuda()) { 36 | return backward_cuda(z, dz, var, weight, bias, edz, eydz, affine, eps); 37 | } else { 38 | return backward_cpu(z, dz, var, weight, bias, edz, eydz, affine, eps); 39 | } 40 | } 41 | 42 | void leaky_relu_forward(at::Tensor z, float slope) { 43 | at::leaky_relu_(z, slope); 44 | } 45 | 46 | void leaky_relu_backward(at::Tensor z, at::Tensor dz, float slope) { 47 | if (z.is_cuda()) { 48 | return leaky_relu_backward_cuda(z, dz, slope); 49 | } else { 50 | return leaky_relu_backward_cpu(z, dz, slope); 51 | } 52 | } 53 | 54 | void elu_forward(at::Tensor z) { 55 | at::elu_(z); 56 | } 57 | 58 | void elu_backward(at::Tensor z, at::Tensor dz) { 59 | if (z.is_cuda()) { 60 | return elu_backward_cuda(z, dz); 61 | } else { 62 | return elu_backward_cpu(z, dz); 63 | } 64 | } 65 | 66 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { 67 | m.def("mean_var", &mean_var, "Mean and variance computation"); 68 | m.def("forward", &forward, "In-place forward computation"); 69 | m.def("edz_eydz", &edz_eydz, "First part of backward computation"); 70 | m.def("backward", &backward, "Second part of backward computation"); 71 | m.def("leaky_relu_forward", &leaky_relu_forward, "Leaky relu forward computation"); 72 | m.def("leaky_relu_backward", &leaky_relu_backward, "Leaky relu backward computation and inversion"); 73 | m.def("elu_forward", &elu_forward, "Elu forward computation"); 74 | m.def("elu_backward", &elu_backward, "Elu backward computation and inversion"); 75 | } -------------------------------------------------------------------------------- /track1/mindspore/code/scripts/run_standalone_train.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2021 Huawei Technologies Co., Ltd 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================ 16 | 17 | if [ $# != 5 ] && [ $# != 6 ] 18 | then 19 | echo "Using: bash scripts/run_standalone_train.sh [DEVICE_ID] [DATASET_PATH] [TRAIN_OUTPUT_PATH] [CHECKPOINT_PATH] [EVAL_CALLBACK]" 20 | echo "or" 21 | echo "Using: bash scripts/run_standalone_train.sh [DEVICE_ID] [DATASET_PATH] [TRAIN_OUTPUT_PATH] [CHECKPOINT_PATH] [BEGIN_EPOCH] [EVAL_CALLBACK]" 22 | exit 1 23 | fi 24 | 25 | get_real_path(){ 26 | if [ "${1:0:1}" == "/" ]; then 27 | echo "$1" 28 | else 29 | echo "$(realpath -m $PWD/$1)" 30 | fi 31 | } 32 | 33 | PATH1=$(get_real_path $2) # dataset_path 34 | PATH2=$(get_real_path $3) # train_output_path 35 | PATH3=$(get_real_path $4) # pretrained or resume ckpt_path 36 | 37 | if [ ! -d $PATH1 ] 38 | then 39 | echo "error: DATASET_PATH=$PATH1 is not a directory." 40 | exit 1 41 | fi 42 | 43 | if [ ! -d $PATH2 ] 44 | then 45 | echo "error: TRAIN_OUTPUT_PATH=$PATH2 is not a directory." 46 | fi 47 | 48 | if [ ! -f $PATH3 ] 49 | then 50 | echo "error: CHECKPOINT_PATH=$PATH3 is not a file." 51 | exit 1 52 | fi 53 | 54 | export DEVICE_NUM=1 55 | export DEVICE_ID=$1 56 | export RANK_ID=0 57 | export RANK_SIZE=1 58 | 59 | if [ -d "./train" ] 60 | then 61 | rm -rf ./train 62 | echo "Remove dir ./train." 63 | fi 64 | mkdir ./train 65 | echo "Create a dir ./train." 66 | cp ./train.py ./train 67 | cp -r ./src ./train 68 | cd ./train || exit 69 | echo "Start training for device $DEVICE_ID" 70 | env > env.log 71 | if [ $# == 5 ] 72 | then 73 | python3 train.py --data_path $PATH1 --output_path $PATH2 --checkpoint_path $PATH3 --modelarts False --run_distribute False --device_target Ascend --lr 0.0017 --lr_power 6e-10 --begin_epoch 0 --eval_callback $5 &> log & 74 | else 75 | python3 train.py --data_path $PATH1 --output_path $PATH2 --checkpoint_path $PATH3 --modelarts False --run_distribute False --device_target Ascend --lr 0.0017 --lr_power 6e-10 --begin_epoch $5 --eval_callback $6 &> log & 76 | fi 77 | cd .. 78 | 79 | -------------------------------------------------------------------------------- /track1/mindspore/code/scripts/.ipynb_checkpoints/run_standalone_train-checkpoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2021 Huawei Technologies Co., Ltd 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================ 16 | 17 | if [ $# != 5 ] && [ $# != 6 ] 18 | then 19 | echo "Using: bash scripts/run_standalone_train.sh [DEVICE_ID] [DATASET_PATH] [TRAIN_OUTPUT_PATH] [CHECKPOINT_PATH] [EVAL_CALLBACK]" 20 | echo "or" 21 | echo "Using: bash scripts/run_standalone_train.sh [DEVICE_ID] [DATASET_PATH] [TRAIN_OUTPUT_PATH] [CHECKPOINT_PATH] [BEGIN_EPOCH] [EVAL_CALLBACK]" 22 | exit 1 23 | fi 24 | 25 | get_real_path(){ 26 | if [ "${1:0:1}" == "/" ]; then 27 | echo "$1" 28 | else 29 | echo "$(realpath -m $PWD/$1)" 30 | fi 31 | } 32 | 33 | PATH1=$(get_real_path $2) # dataset_path 34 | PATH2=$(get_real_path $3) # train_output_path 35 | PATH3=$(get_real_path $4) # pretrained or resume ckpt_path 36 | 37 | if [ ! -d $PATH1 ] 38 | then 39 | echo "error: DATASET_PATH=$PATH1 is not a directory." 40 | exit 1 41 | fi 42 | 43 | if [ ! -d $PATH2 ] 44 | then 45 | echo "error: TRAIN_OUTPUT_PATH=$PATH2 is not a directory." 46 | fi 47 | 48 | if [ ! -f $PATH3 ] 49 | then 50 | echo "error: CHECKPOINT_PATH=$PATH3 is not a file." 51 | exit 1 52 | fi 53 | 54 | export DEVICE_NUM=1 55 | export DEVICE_ID=$1 56 | export RANK_ID=0 57 | export RANK_SIZE=1 58 | 59 | if [ -d "./train" ] 60 | then 61 | rm -rf ./train 62 | echo "Remove dir ./train." 63 | fi 64 | mkdir ./train 65 | echo "Create a dir ./train." 66 | cp ./train.py ./train 67 | cp -r ./src ./train 68 | cd ./train || exit 69 | echo "Start training for device $DEVICE_ID" 70 | env > env.log 71 | if [ $# == 5 ] 72 | then 73 | python3 train.py --data_path $PATH1 --output_path $PATH2 --checkpoint_path $PATH3 --modelarts False --run_distribute False --device_target Ascend --lr 0.0017 --lr_power 6e-10 --begin_epoch 0 --eval_callback $5 &> log & 74 | else 75 | python3 train.py --data_path $PATH1 --output_path $PATH2 --checkpoint_path $PATH3 --modelarts False --run_distribute False --device_target Ascend --lr 0.0017 --lr_power 6e-10 --begin_epoch $5 --eval_callback $6 &> log & 76 | fi 77 | cd .. 78 | 79 | -------------------------------------------------------------------------------- /track2/model/modules/cc.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | from torch.nn import Softmax 5 | 6 | 7 | 8 | def INF(B,H,W): 9 | return -torch.diag(torch.tensor(float("inf")).cuda().repeat(H),0).unsqueeze(0).repeat(B*W,1,1) 10 | 11 | 12 | 13 | class CC_module(nn.Module): 14 | 15 | def __init__(self,in_dim): 16 | super(CC_module, self).__init__() 17 | self.query_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1) 18 | self.key_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1) 19 | self.value_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim, kernel_size=1) 20 | self.softmax = Softmax(dim=3) 21 | self.INF = INF 22 | self.gamma = nn.Parameter(torch.zeros(1)) 23 | 24 | def forward(self, x): 25 | m_batchsize, _, height, width = x.size() 26 | proj_query = self.query_conv(x) 27 | proj_query_H = proj_query.permute(0,3,1,2).contiguous().view(m_batchsize*width,-1,height).permute(0, 2, 1) 28 | proj_query_W = proj_query.permute(0,2,1,3).contiguous().view(m_batchsize*height,-1,width).permute(0, 2, 1) 29 | proj_key = self.key_conv(x) 30 | proj_key_H = proj_key.permute(0,3,1,2).contiguous().view(m_batchsize*width,-1,height) 31 | proj_key_W = proj_key.permute(0,2,1,3).contiguous().view(m_batchsize*height,-1,width) 32 | proj_value = self.value_conv(x) 33 | proj_value_H = proj_value.permute(0,3,1,2).contiguous().view(m_batchsize*width,-1,height) 34 | proj_value_W = proj_value.permute(0,2,1,3).contiguous().view(m_batchsize*height,-1,width) 35 | energy_H = (torch.bmm(proj_query_H, proj_key_H)+self.INF(m_batchsize, height, width)).view(m_batchsize,width,height,height).permute(0,2,1,3) 36 | energy_W = torch.bmm(proj_query_W, proj_key_W).view(m_batchsize,height,width,width) 37 | concate = self.softmax(torch.cat([energy_H, energy_W], 3)) 38 | #concate = concate * (concate>torch.mean(concate,dim=3,keepdim=True)).float() 39 | att_H = concate[:,:,:,0:height].permute(0,2,1,3).contiguous().view(m_batchsize*width,height,height) 40 | #print(concate) 41 | #print(att_H) 42 | att_W = concate[:,:,:,height:height+width].contiguous().view(m_batchsize*height,width,width) 43 | out_H = torch.bmm(proj_value_H, att_H.permute(0, 2, 1)).view(m_batchsize,width,-1,height).permute(0,2,3,1) 44 | out_W = torch.bmm(proj_value_W, att_W.permute(0, 2, 1)).view(m_batchsize,height,-1,width).permute(0,2,1,3) 45 | #print(out_H.size(),out_W.size()) 46 | return self.gamma*(out_H + out_W) + x 47 | 48 | 49 | 50 | if __name__ == '__main__': 51 | model = CC_module(2048) 52 | x = torch.randn(4, 2048, 41, 41) 53 | out = model(x) 54 | print(out.shape) -------------------------------------------------------------------------------- /track1/mindspore/code/pre/pre_main.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Nov 2 22:36:34 2022 4 | 5 | @author: DELL 6 | """ 7 | 8 | import argparse 9 | 10 | from json2txt import json2txt 11 | from data_clip import data_clip 12 | from gen_train_txt import gen_train_txt 13 | 14 | 15 | 16 | def parse_args(): 17 | """ 18 | Get arguments from command-line. 19 | """ 20 | parser = argparse.ArgumentParser(description='data preprocess.') 21 | parser.add_argument("--chusai_train_data_dir", type=str, default="././data/chusai_release/train") 22 | parser.add_argument("--fusai_train_data_dir", type=str, default="././data/fusai_release/train") 23 | parser.add_argument("--train_all_txt_path", type=str, default="code_mindspore/train_all.txt") 24 | 25 | return parser.parse_args(args=[]) 26 | 27 | if __name__ == '__main__': 28 | 29 | args = parse_args() 30 | chusai_train_data_dir = args.chusai_train_data_dir 31 | fusai_train_data_dir = args.fusai_train_data_dir 32 | train_all_txt_path = args.train_all_txt_path 33 | 34 | chusai_json_path = chusai_train_data_dir + "/instances_train.json" 35 | chusai_ann_folder = chusai_train_data_dir + "/annotations" 36 | chusai_img_floder = chusai_train_data_dir +"/images" 37 | chusai_label_folder = chusai_train_data_dir + "/masks" 38 | 39 | chusai_out_img_floder = chusai_train_data_dir + "/images_crop" 40 | chusai_out_label_floder = chusai_train_data_dir + "/masks_crop" 41 | chusai_out_anno_floder = chusai_train_data_dir + "/annotations_crop" 42 | 43 | fusai_json_path = fusai_train_data_dir + "/instances_train.json" 44 | fusai_ann_folder = fusai_train_data_dir + "/annotations" 45 | fusai_img_floder = fusai_train_data_dir +"/images" 46 | fusai_label_folder = fusai_train_data_dir + "/masks" 47 | 48 | fusai_out_img_floder = fusai_train_data_dir + "/images_crop" 49 | fusai_out_label_floder = fusai_train_data_dir + "/masks_crop" 50 | fusai_out_anno_floder = fusai_train_data_dir + "/annotations_crop" 51 | 52 | 53 | print("step 1: chusai json to txt...") 54 | json2txt(chusai_json_path, chusai_ann_folder) 55 | 56 | print("step 2: chusai data clip...") 57 | data_clip(chusai_json_path, chusai_img_floder, chusai_label_folder, chusai_ann_folder, 58 | chusai_out_img_floder, chusai_out_label_floder, chusai_out_anno_floder) 59 | 60 | print("step 3: fusai json to txt...") 61 | json2txt(fusai_json_path, fusai_ann_folder) 62 | 63 | print("step 4: fusai data clip...") 64 | data_clip(fusai_json_path, fusai_img_floder, fusai_label_folder, fusai_ann_folder, 65 | fusai_out_img_floder, fusai_out_label_floder, fusai_out_anno_floder) 66 | 67 | print("step 5: gen train txt...") 68 | gen_train_txt(chusai_out_anno_floder, fusai_out_anno_floder, train_all_txt_path) -------------------------------------------------------------------------------- /track2/model/modules/backbones/resnet_dilated.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | from .resnet import ResNet 4 | 5 | class ResnetDilated(nn.Module): 6 | def __init__(self, orig_resnet, dilate_scale=8): 7 | super(ResnetDilated, self).__init__() 8 | from functools import partial 9 | 10 | if dilate_scale == 8: 11 | orig_resnet.layer3.apply( 12 | partial(self._nostride_dilate, dilate=2)) 13 | orig_resnet.layer4.apply( 14 | partial(self._nostride_dilate, dilate=4)) 15 | elif dilate_scale == 16: 16 | orig_resnet.layer4.apply( 17 | partial(self._nostride_dilate, dilate=2)) 18 | 19 | # take pretrained resnet, except AvgPool and FC 20 | self.conv1 = orig_resnet.conv1 21 | self.bn1 = orig_resnet.bn1 22 | self.relu = orig_resnet.relu 23 | 24 | self.maxpool = orig_resnet.maxpool 25 | self.layer1 = orig_resnet.layer1 26 | self.layer2 = orig_resnet.layer2 27 | self.layer3 = orig_resnet.layer3 28 | self.layer4 = orig_resnet.layer4 29 | 30 | def _nostride_dilate(self, m, dilate): 31 | classname = m.__class__.__name__ 32 | if classname.find('Conv') != -1: 33 | # the convolution with stride 34 | if m.stride == (2, 2): 35 | m.stride = (1, 1) 36 | if m.kernel_size == (3, 3): 37 | m.dilation = (dilate//2, dilate//2) 38 | m.padding = (dilate//2, dilate//2) 39 | # other convoluions 40 | else: 41 | if m.kernel_size == (3, 3): 42 | m.dilation = (dilate, dilate) 43 | m.padding = (dilate, dilate) 44 | 45 | 46 | def load_param(self, model_path): 47 | param_dict = torch.load(model_path, map_location=lambda storage, loc: storage) 48 | if 'state_dict' in param_dict.keys(): 49 | param_dict = param_dict['state_dict'] 50 | 51 | 52 | start_with_module = False 53 | for k in param_dict.keys(): 54 | if k.startswith('module.'): 55 | start_with_module = True 56 | break 57 | if start_with_module: 58 | param_dict = {k[7:] : v for k, v in param_dict.items() } 59 | 60 | print('ignore_param:') 61 | print([k for k, v in param_dict.items() if k not in self.state_dict() or self.state_dict()[k].size() != v.size()]) 62 | print('unload_param:') 63 | print([k for k, v in self.state_dict().items() if k not in param_dict.keys() or param_dict[k].size() != v.size()] ) 64 | 65 | param_dict = {k: v for k, v in param_dict.items() if k in self.state_dict() and self.state_dict()[k].size() == v.size()} 66 | for i in param_dict: 67 | self.state_dict()[i].copy_(param_dict[i]) -------------------------------------------------------------------------------- /track1/mindspore/code/pre/.ipynb_checkpoints/pre_main-checkpoint.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Nov 2 22:36:34 2022 4 | 5 | @author: DELL 6 | """ 7 | 8 | import argparse 9 | 10 | from json2txt import json2txt 11 | from data_clip import data_clip 12 | from gen_train_txt import gen_train_txt 13 | 14 | 15 | 16 | def parse_args(): 17 | """ 18 | Get arguments from command-line. 19 | """ 20 | parser = argparse.ArgumentParser(description='data preprocess.') 21 | parser.add_argument("--chusai_train_data_dir", type=str, default="././data/fusai_release/train") 22 | parser.add_argument("--fusai_train_data_dir", type=str, default="././data/fusai_release/train") 23 | parser.add_argument("--train_all_txt_path", type=str, default="code_mindspore/train_all.txt") 24 | 25 | return parser.parse_args(args=[]) 26 | 27 | if __name__ == '__main__': 28 | 29 | args = parse_args() 30 | chusai_train_data_dir = args.chusai_train_data_dir 31 | fusai_train_data_dir = args.fusai_train_data_dir 32 | train_all_txt_path = args.train_all_txt_path 33 | 34 | chusai_json_path = chusai_train_data_dir + "/instances_train.json" 35 | chusai_ann_folder = chusai_train_data_dir + "/annotations" 36 | chusai_img_floder = chusai_train_data_dir +"/images" 37 | chusai_label_folder = chusai_train_data_dir + "/masks" 38 | 39 | chusai_out_img_floder = chusai_train_data_dir + "/images_crop" 40 | chusai_out_label_floder = chusai_train_data_dir + "/masks_crop" 41 | chusai_out_anno_floder = chusai_train_data_dir + "/annotations_crop" 42 | 43 | fusai_json_path = fusai_train_data_dir + "/instances_train.json" 44 | fusai_ann_folder = fusai_train_data_dir + "/annotations" 45 | fusai_img_floder = fusai_train_data_dir +"/images" 46 | fusai_label_folder = fusai_train_data_dir + "/masks" 47 | 48 | fusai_out_img_floder = fusai_train_data_dir + "/images_crop" 49 | fusai_out_label_floder = fusai_train_data_dir + "/masks_crop" 50 | fusai_out_anno_floder = fusai_train_data_dir + "/annotations_crop" 51 | 52 | 53 | print("step 1: chusai json to txt...") 54 | json2txt(chusai_json_path, chusai_ann_folder) 55 | 56 | print("step 2: chusai data clip...") 57 | data_clip(chusai_json_path, chusai_img_floder, chusai_label_folder, chusai_ann_folder, 58 | chusai_out_img_floder, chusai_out_label_floder, chusai_out_anno_floder) 59 | 60 | print("step 3: fusai json to txt...") 61 | json2txt(fusai_json_path, fusai_ann_folder) 62 | 63 | print("step 4: fusai data clip...") 64 | data_clip(fusai_json_path, fusai_img_floder, fusai_label_folder, fusai_ann_folder, 65 | fusai_out_img_floder, fusai_out_label_floder, fusai_out_anno_floder) 66 | 67 | print("step 5: gen train txt...") 68 | gen_train_txt(chusai_out_anno_floder, fusai_out_anno_floder, train_all_txt_path) -------------------------------------------------------------------------------- /track1/mindspore/code/pre/gdal_imread.py: -------------------------------------------------------------------------------- 1 | import os 2 | # import cv2 3 | # import tqdm 4 | import numpy as np 5 | try: 6 | import gdal 7 | except: 8 | from osgeo import gdal 9 | 10 | 11 | def imread_gdal(image_path): 12 | dataset = gdal.Open(image_path) 13 | width = dataset.RasterXSize 14 | height = dataset.RasterYSize 15 | nodata = dataset.GetRasterBand(1).GetNoDataValue() 16 | # print(nodata) 17 | image_h_s = [] 18 | image_w_s = [] 19 | step = 100 20 | # for height_i in tqdm.tqdm(range(0,height,step)): 21 | for height_i in range(0,height,step): 22 | try: 23 | image_i = dataset.ReadAsArray(0, height_i, width, min(step,height-height_i)) 24 | image_i[image_i==nodata] = 0 25 | image_i = image_i.astype(np.uint8) 26 | except: 27 | image_i = np.zeros((3,step,width), np.uint8) 28 | image_h_s.append(image_i) 29 | image_h = np.concatenate(image_h_s,1) 30 | 31 | # for width_i in tqdm.tqdm(range(0,width,step)): 32 | for width_i in range(0,width,step): 33 | try: 34 | image_i = dataset.ReadAsArray(width_i, 0, min(step,width-width_i), height) 35 | image_i[image_i==nodata] = 0 36 | image_i = image_i.astype(np.uint8) 37 | except: 38 | image_i = np.zeros((3,height,step), np.uint8) 39 | image_w_s.append(image_i) 40 | image_w = np.concatenate(image_w_s,2) 41 | 42 | image = np.where(image_h>image_w, image_h, image_w) 43 | return image 44 | 45 | def imwrite_gdal(path, im_data): 46 | if 'int8' in im_data.dtype.name: 47 | datatype = gdal.GDT_Byte 48 | elif 'int16' in im_data.dtype.name: 49 | datatype = gdal.GDT_UInt16 50 | else: 51 | datatype = gdal.GDT_Float32 52 | if len(im_data.shape) == 3: 53 | im_bands, im_height, im_width = im_data.shape 54 | elif len(im_data.shape) == 2: 55 | im_data = np.array([im_data]) 56 | im_bands, im_height, im_width = im_data.shape 57 | 58 | #创建文件 59 | driver = gdal.GetDriverByName("GTiff") 60 | dataset = driver.Create(path, int(im_width), int(im_height), int(im_bands), datatype, 61 | options=["TILED=YES", "COMPRESS=LZW"]) 62 | if(dataset!= None): 63 | dataset.SetGeoTransform((0,0,0,0,0,0)) #写入仿射变换参数 64 | dataset.SetProjection("") #写入投影 65 | for i in range(im_bands): 66 | dataset.GetRasterBand(i+1).WriteArray(im_data[i]) 67 | del dataset 68 | 69 | if __name__ == "__main__": 70 | 71 | error_tif_names = ["148.tif","156.tif","157.tif","235.tif","262.tif"] 72 | error_tif_dir = "././data/fusai_release/testA/images" 73 | correct_tif_dir = error_tif_dir.replace("images","images_correct") 74 | if not os.path.exists(correct_tif_dir): os.makedirs(correct_tif_dir) 75 | for error_tif_name in error_tif_names: 76 | image_path = error_tif_dir + "/" + error_tif_name 77 | img = imread_gdal(image_path) 78 | # img = np.transpose(img,(1,2,0)) 79 | # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 80 | correct_tif_path = correct_tif_dir + "/" + error_tif_name 81 | imwrite_gdal(correct_tif_path, img) -------------------------------------------------------------------------------- /track1/mindspore/code/pre/.ipynb_checkpoints/gdal_imread-checkpoint.py: -------------------------------------------------------------------------------- 1 | import os 2 | # import cv2 3 | # import tqdm 4 | import numpy as np 5 | try: 6 | import gdal 7 | except: 8 | from osgeo import gdal 9 | 10 | 11 | def imread_gdal(image_path): 12 | dataset = gdal.Open(image_path) 13 | width = dataset.RasterXSize 14 | height = dataset.RasterYSize 15 | nodata = dataset.GetRasterBand(1).GetNoDataValue() 16 | print(nodata) 17 | image_h_s = [] 18 | image_w_s = [] 19 | step = 100 20 | # for height_i in tqdm.tqdm(range(0,height,step)): 21 | for height_i in range(0,height,step): 22 | try: 23 | image_i = dataset.ReadAsArray(0, height_i, width, min(step,height-height_i)) 24 | image_i[image_i==nodata] = 0 25 | image_i = image_i.astype(np.uint8) 26 | except: 27 | image_i = np.zeros((3,step,width), np.uint8) 28 | image_h_s.append(image_i) 29 | image_h = np.concatenate(image_h_s,1) 30 | 31 | # for width_i in tqdm.tqdm(range(0,width,step)): 32 | for width_i in range(0,width,step): 33 | try: 34 | image_i = dataset.ReadAsArray(width_i, 0, min(step,width-width_i), height) 35 | image_i[image_i==nodata] = 0 36 | image_i = image_i.astype(np.uint8) 37 | except: 38 | image_i = np.zeros((3,height,step), np.uint8) 39 | image_w_s.append(image_i) 40 | image_w = np.concatenate(image_w_s,2) 41 | 42 | image = np.where(image_h>image_w, image_h, image_w) 43 | return image 44 | 45 | def imwrite_gdal(path, im_data): 46 | if 'int8' in im_data.dtype.name: 47 | datatype = gdal.GDT_Byte 48 | elif 'int16' in im_data.dtype.name: 49 | datatype = gdal.GDT_UInt16 50 | else: 51 | datatype = gdal.GDT_Float32 52 | if len(im_data.shape) == 3: 53 | im_bands, im_height, im_width = im_data.shape 54 | elif len(im_data.shape) == 2: 55 | im_data = np.array([im_data]) 56 | im_bands, im_height, im_width = im_data.shape 57 | 58 | #创建文件 59 | driver = gdal.GetDriverByName("GTiff") 60 | dataset = driver.Create(path, int(im_width), int(im_height), int(im_bands), datatype, 61 | options=["TILED=YES", "COMPRESS=LZW"]) 62 | if(dataset!= None): 63 | dataset.SetGeoTransform((0,0,0,0,0,0)) #写入仿射变换参数 64 | dataset.SetProjection("") #写入投影 65 | for i in range(im_bands): 66 | dataset.GetRasterBand(i+1).WriteArray(im_data[i]) 67 | del dataset 68 | 69 | if __name__ == "__main__": 70 | 71 | error_tif_names = ["148.tif","156.tif","157.tif","235.tif","262.tif"] 72 | error_tif_dir = "././data/fusai_release/testA/images" 73 | correct_tif_dir = error_tif_dir.replace("images","images_correct") 74 | if not os.path.exists(correct_tif_dir): os.makedirs(correct_tif_dir) 75 | for error_tif_name in error_tif_names: 76 | image_path = error_tif_dir + "/" + error_tif_name 77 | img = imread_gdal(image_path) 78 | # img = np.transpose(img,(1,2,0)) 79 | # img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 80 | correct_tif_path = correct_tif_dir + "/" + error_tif_name 81 | imwrite_gdal(correct_tif_path, img) -------------------------------------------------------------------------------- /track1/mindspore/code/scripts/run_distribute_train.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2021 Huawei Technologies Co., Ltd 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================ 16 | 17 | if [ $# != 5 ] && [ $# != 6 ] 18 | then 19 | echo "Using: bash scripts/run_distribute_train.sh [RANK_TABLE_FILE] [DATASET_PATH] [TRAIN_OUTPUT_PATH] [CHECKPOINT_PATH] [EVAL_CALLBACK]" 20 | echo "or" 21 | echo "Using: bash scripts/run_distribute_train.sh [RANK_TABLE_FILE] [DATASET_PATH] [TRAIN_OUTPUT_PATH] [CHECKPOINT_PATH] [BEGIN_EPOCH] [EVAL_CALLBACK]" 22 | exit 1 23 | fi 24 | 25 | get_real_path(){ 26 | if [ "${1:0:1}" == "/" ]; then 27 | echo "$1" 28 | else 29 | echo "$(realpath -m $PWD/$1)" 30 | fi 31 | } 32 | 33 | PATH1=$(get_real_path $1) # rank_table_file 34 | PATH2=$(get_real_path $2) # dataset_path 35 | PATH3=$(get_real_path $3) # train_output_path 36 | PATH4=$(get_real_path $4) # pretrained or resume ckpt_path 37 | 38 | if [ ! -f $PATH1 ] 39 | then 40 | echo "error: RANK_TABLE_FILE=$PATH1 is not a file." 41 | exit 1 42 | fi 43 | 44 | if [ ! -d $PATH2 ] 45 | then 46 | echo "error: DATASET_PATH=$PATH2 is not a directory." 47 | exit 1 48 | fi 49 | 50 | if [ ! -d $PATH3 ] 51 | then 52 | echo "error: TRAIN_OUTPUT_PATH=$PATH3 is not a directory." 53 | fi 54 | 55 | if [ ! -f $PATH4 ] 56 | then 57 | echo "error: CHECKPOINT_PATH=$PATH4 is not a file." 58 | exit 1 59 | fi 60 | 61 | export DEVICE_NUM=4 62 | export RANK_SIZE=4 63 | export RANK_TABLE_FILE=$PATH1 64 | 65 | export SERVER_ID=0 66 | rank_start=$((DEVICE_NUM * SERVER_ID)) 67 | 68 | 69 | for((i=0; i<${DEVICE_NUM}; i++)) 70 | do 71 | export DEVICE_ID=${i} 72 | export RANK_ID=$((rank_start + i)) 73 | rm -rf ./train_parallel$i 74 | mkdir ./train_parallel$i 75 | cp ./train.py ./train_parallel$i 76 | cp -r ./src ./train_parallel$i 77 | cd ./train_parallel$i || exit 78 | echo "Start training for rank $RANK_ID, device $DEVICE_ID." 79 | env > env.log 80 | if [ $# == 5 ] 81 | then 82 | python3 train.py --data_path $PATH2 --output_path $PATH3 --checkpoint_path $PATH4 --modelarts False --run_distribute True --device_target Ascend --lr 0.0012 --lr_power 6e-10 --begin_epoch 0 --end_epoch 1000 --eval_callback $5 --eval_interval 50 &> log & 83 | elif [ $# == 6 ] 84 | then 85 | python3 train.py --data_path $PATH2 --output_path $PATH3 --checkpoint_path $PATH4 --modelarts False --run_distribute True --device_target Ascend --lr 0.0012 --lr_power 6e-10 --begin_epoch $5 --end_epoch 1000 --eval_callback $6 --eval_interval 50 &> log & 86 | fi 87 | cd .. 88 | done 89 | -------------------------------------------------------------------------------- /track2/model/modules/inplace_abn/src/common.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | #include 4 | 5 | /* 6 | * General settings 7 | */ 8 | const int WARP_SIZE = 32; 9 | const int MAX_BLOCK_SIZE = 512; 10 | 11 | template 12 | struct Pair { 13 | T v1, v2; 14 | __device__ Pair() {} 15 | __device__ Pair(T _v1, T _v2) : v1(_v1), v2(_v2) {} 16 | __device__ Pair(T v) : v1(v), v2(v) {} 17 | __device__ Pair(int v) : v1(v), v2(v) {} 18 | __device__ Pair &operator+=(const Pair &a) { 19 | v1 += a.v1; 20 | v2 += a.v2; 21 | return *this; 22 | } 23 | }; 24 | 25 | /* 26 | * Utility functions 27 | */ 28 | template 29 | __device__ __forceinline__ T WARP_SHFL_XOR(T value, int laneMask, int width = warpSize, 30 | unsigned int mask = 0xffffffff) { 31 | #if CUDART_VERSION >= 9000 32 | return __shfl_xor_sync(mask, value, laneMask, width); 33 | #else 34 | return __shfl_xor(value, laneMask, width); 35 | #endif 36 | } 37 | 38 | __device__ __forceinline__ int getMSB(int val) { return 31 - __clz(val); } 39 | 40 | static int getNumThreads(int nElem) { 41 | int threadSizes[5] = {32, 64, 128, 256, MAX_BLOCK_SIZE}; 42 | for (int i = 0; i != 5; ++i) { 43 | if (nElem <= threadSizes[i]) { 44 | return threadSizes[i]; 45 | } 46 | } 47 | return MAX_BLOCK_SIZE; 48 | } 49 | 50 | template 51 | static __device__ __forceinline__ T warpSum(T val) { 52 | #if __CUDA_ARCH__ >= 300 53 | for (int i = 0; i < getMSB(WARP_SIZE); ++i) { 54 | val += WARP_SHFL_XOR(val, 1 << i, WARP_SIZE); 55 | } 56 | #else 57 | __shared__ T values[MAX_BLOCK_SIZE]; 58 | values[threadIdx.x] = val; 59 | __threadfence_block(); 60 | const int base = (threadIdx.x / WARP_SIZE) * WARP_SIZE; 61 | for (int i = 1; i < WARP_SIZE; i++) { 62 | val += values[base + ((i + threadIdx.x) % WARP_SIZE)]; 63 | } 64 | #endif 65 | return val; 66 | } 67 | 68 | template 69 | static __device__ __forceinline__ Pair warpSum(Pair value) { 70 | value.v1 = warpSum(value.v1); 71 | value.v2 = warpSum(value.v2); 72 | return value; 73 | } 74 | 75 | template 76 | __device__ T reduce(Op op, int plane, int N, int C, int S) { 77 | T sum = (T)0; 78 | for (int batch = 0; batch < N; ++batch) { 79 | for (int x = threadIdx.x; x < S; x += blockDim.x) { 80 | sum += op(batch, plane, x); 81 | } 82 | } 83 | 84 | // sum over NumThreads within a warp 85 | sum = warpSum(sum); 86 | 87 | // 'transpose', and reduce within warp again 88 | __shared__ T shared[32]; 89 | __syncthreads(); 90 | if (threadIdx.x % WARP_SIZE == 0) { 91 | shared[threadIdx.x / WARP_SIZE] = sum; 92 | } 93 | if (threadIdx.x >= blockDim.x / WARP_SIZE && threadIdx.x < WARP_SIZE) { 94 | // zero out the other entries in shared 95 | shared[threadIdx.x] = (T)0; 96 | } 97 | __syncthreads(); 98 | if (threadIdx.x / WARP_SIZE == 0) { 99 | sum = warpSum(shared[threadIdx.x]); 100 | if (threadIdx.x == 0) { 101 | shared[0] = sum; 102 | } 103 | } 104 | __syncthreads(); 105 | 106 | // Everyone picks it up, should be broadcast into the whole gradInput 107 | return shared[0]; 108 | } -------------------------------------------------------------------------------- /track1/mindspore/code/torch2mindspore.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Huawei Technologies Co., Ltd 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================ 15 | """Transform torch model to mindspore format.""" 16 | import argparse 17 | import torch 18 | from mindspore import Tensor 19 | from mindspore.train.serialization import load_checkpoint, load_param_into_net, save_checkpoint 20 | 21 | from src.hrnet import get_seg_model 22 | from src.config import config_hrnetv2_w18 as model_config 23 | # from src.config import hrnetw18_config as model_config 24 | 25 | 26 | def parse_args(): 27 | """Get parameters from command line.""" 28 | parser = argparse.ArgumentParser(description="Transform torch model to mindspore format.") 29 | parser.add_argument("--pth_path", type=str, default="pre_model/hrnetv2_w18_imagenet_pretrained.pth", help="Torch model path.") 30 | parser.add_argument("--ckpt_path", type=str, default="pre_model/hrnetv2_w18_imagenet_pretrained.ckpt", help="Output checkpoint storage path.") 31 | 32 | return parser.parse_args() 33 | 34 | 35 | def convert(pth_path, ckpt_path): 36 | """Model converter.""" 37 | params_dict = torch.load(pth_path, map_location=torch.device('cpu')) 38 | keys = list(params_dict.keys()) 39 | print(list(params_dict.keys())) 40 | compare = {} 41 | i = 0 42 | while i < len(keys): 43 | temp = keys[i] 44 | if "model." in keys[i]: 45 | temp = temp.replace("model.", "") 46 | print(temp) 47 | if "running_mean" in temp: 48 | temp = temp.replace("running_mean", "moving_mean") 49 | if "running_var" in temp: 50 | temp = temp.replace("running_var", "moving_variance") 51 | if "weight" in temp: 52 | if i+2 < len(keys) and "running" in keys[i+2]: 53 | temp = temp.replace("weight", "gamma") 54 | if "bias" in temp: 55 | if i+2 < len(keys) and "running" in keys[i+2]: 56 | temp = temp.replace("bias", "beta") 57 | compare[keys[i]] = temp 58 | i += 1 59 | ms_params = [] 60 | for item in compare.items(): 61 | pair = {} 62 | tk = item[0] 63 | mk = item[1] 64 | if "tracked" in tk or "loss.criterion.weight" in tk: 65 | continue 66 | pair['name'] = mk 67 | pair['data'] = Tensor(params_dict[tk].numpy()) 68 | ms_params.append(pair) 69 | save_checkpoint(ms_params, ckpt_path) 70 | 71 | 72 | if __name__ == "__main__": 73 | args = parse_args() 74 | convert(args.pth_path, args.ckpt_path) 75 | net = get_seg_model(model_config) 76 | params = load_checkpoint(args.ckpt_path) 77 | load_param_into_net(net, params, strict_load=True) 78 | print("Transform successfully!") 79 | -------------------------------------------------------------------------------- /track1/pytorch/code/nets/net_training.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Aug 25 20:54:59 2022 4 | 5 | @author: DELL 6 | """ 7 | # import math 8 | # from functools import partial 9 | import torch 10 | import torch.nn.functional as F 11 | import torch.nn as nn 12 | # from segmentation_models_pytorch.losses import DiceLoss 13 | 14 | def focal_loss(pred, target): 15 | pred = pred.permute(0, 2, 3, 1) 16 | 17 | #-------------------------------------------------------------------------# 18 | # 找到每张图片的正样本和负样本 19 | # 一个真实框对应一个正样本 20 | # 除去正样本的特征点,其余为负样本 21 | #-------------------------------------------------------------------------# 22 | pos_inds = target.eq(1).float() 23 | neg_inds = target.lt(1).float() 24 | #-------------------------------------------------------------------------# 25 | # 正样本特征点附近的负样本的权值更小一些 26 | #-------------------------------------------------------------------------# 27 | neg_weights = torch.pow(1 - target, 4) 28 | 29 | pred = torch.clamp(pred, 1e-6, 1 - 1e-6) 30 | #-------------------------------------------------------------------------# 31 | # 计算focal loss。难分类样本权重大,易分类样本权重小。 32 | #-------------------------------------------------------------------------# 33 | pos_loss = torch.log(pred) * torch.pow(1 - pred, 2) * pos_inds 34 | neg_loss = torch.log(1 - pred) * torch.pow(pred, 2) * neg_weights * neg_inds 35 | 36 | # -------------------------------------------------------------------------# 37 | # 进行损失的归一化 38 | # -------------------------------------------------------------------------# 39 | num_pos = pos_inds.float().sum() 40 | pos_loss = pos_loss.sum() 41 | neg_loss = neg_loss.sum() 42 | 43 | if num_pos == 0: 44 | loss = -neg_loss 45 | else: 46 | loss = -(pos_loss + neg_loss) / num_pos 47 | return loss 48 | 49 | def reg_l1_loss(pred, target, mask): 50 | #--------------------------------# 51 | # 计算l1_loss 52 | #--------------------------------# 53 | pred = pred.permute(0,2,3,1) 54 | expand_mask = torch.unsqueeze(mask,-1).repeat(1,1,1,2) 55 | 56 | loss = F.l1_loss(pred * expand_mask, target * expand_mask, reduction='sum') 57 | loss = loss / (mask.sum() + 1e-4) 58 | return loss 59 | 60 | 61 | def dice_loss(pred, target): 62 | 63 | epsilon = 1e-5 64 | num = pred.size(0) 65 | 66 | pred = pred.view(num, -1) 67 | target = target.view(num, -1) 68 | 69 | # 利用预测值与标签相乘当作交集 70 | intersection = (pred * target).sum(-1).sum() 71 | union = (pred + target).sum(-1).sum() 72 | 73 | score = 1 - 2 * (intersection + epsilon) / (union + epsilon) 74 | 75 | return score 76 | 77 | def ohem_bce_dice_loss(pred, target): 78 | 79 | # diceloss在一定程度上可以缓解类别不平衡,但是训练容易不稳定 80 | # dice_loss = DiceLoss(mode='binary', 81 | # from_logits=False) 82 | # 交叉熵 83 | bce_loss = nn.BCELoss(reduction='none') 84 | 85 | loss_bce = bce_loss(pred, target) 86 | loss_dice = dice_loss(pred, target) 87 | 88 | # OHEM 89 | loss_bce_,ind = loss_bce.contiguous().view(-1).sort() 90 | min_value = loss_bce_[int(0.5*loss_bce.numel())] 91 | loss_bce = loss_bce[loss_bce>=min_value] 92 | loss_bce = loss_bce.mean() 93 | loss = loss_bce + loss_dice 94 | 95 | return loss 96 | -------------------------------------------------------------------------------- /track1/mindspore/code/.ipynb_checkpoints/torch2mindspore-checkpoint.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Huawei Technologies Co., Ltd 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================ 15 | """Transform torch model to mindspore format.""" 16 | import argparse 17 | import torch 18 | from mindspore import Tensor 19 | from mindspore.train.serialization import load_checkpoint, load_param_into_net, save_checkpoint 20 | 21 | from src.hrnet import get_seg_model 22 | from src.config import config_hrnetv2_w18 as model_config 23 | # from src.config import hrnetw18_config as model_config 24 | 25 | 26 | def parse_args(): 27 | """Get parameters from command line.""" 28 | parser = argparse.ArgumentParser(description="Transform torch model to mindspore format.") 29 | parser.add_argument("--pth_path", type=str, default="pre_model/hrnetv2_w18_imagenet_pretrained.pth", help="Torch model path.") 30 | parser.add_argument("--ckpt_path", type=str, default="pre_model/hrnetv2_w18_imagenet_pretrained.ckpt", help="Output checkpoint storage path.") 31 | 32 | return parser.parse_args() 33 | 34 | 35 | def convert(pth_path, ckpt_path): 36 | """Model converter.""" 37 | params_dict = torch.load(pth_path, map_location=torch.device('cpu')) 38 | keys = list(params_dict.keys()) 39 | print(list(params_dict.keys())) 40 | compare = {} 41 | i = 0 42 | while i < len(keys): 43 | temp = keys[i] 44 | if "model." in keys[i]: 45 | temp = temp.replace("model.", "") 46 | print(temp) 47 | if "running_mean" in temp: 48 | temp = temp.replace("running_mean", "moving_mean") 49 | if "running_var" in temp: 50 | temp = temp.replace("running_var", "moving_variance") 51 | if "weight" in temp: 52 | if i+2 < len(keys) and "running" in keys[i+2]: 53 | temp = temp.replace("weight", "gamma") 54 | if "bias" in temp: 55 | if i+2 < len(keys) and "running" in keys[i+2]: 56 | temp = temp.replace("bias", "beta") 57 | compare[keys[i]] = temp 58 | i += 1 59 | ms_params = [] 60 | for item in compare.items(): 61 | pair = {} 62 | tk = item[0] 63 | mk = item[1] 64 | if "tracked" in tk or "loss.criterion.weight" in tk: 65 | continue 66 | pair['name'] = mk 67 | pair['data'] = Tensor(params_dict[tk].numpy()) 68 | ms_params.append(pair) 69 | save_checkpoint(ms_params, ckpt_path) 70 | 71 | 72 | if __name__ == "__main__": 73 | args = parse_args() 74 | convert(args.pth_path, args.ckpt_path) 75 | net = get_seg_model(model_config) 76 | params = load_checkpoint(args.ckpt_path) 77 | load_param_into_net(net, params, strict_load=True) 78 | print("Transform successfully!") 79 | -------------------------------------------------------------------------------- /track1/mindspore/code/src/post_process.py: -------------------------------------------------------------------------------- 1 | import math 2 | import random 3 | import numpy as np 4 | import cv2 5 | 6 | def transform_preds(coords, center, scale, output_size): 7 | """transform prediction to new coords""" 8 | target_coords = np.zeros(coords.shape) 9 | trans = get_affine_transform(center, scale, 0, output_size, inv=1) 10 | for p in range(coords.shape[0]): 11 | target_coords[p, 0:2] = affine_transform(coords[p, 0:2], trans) 12 | return target_coords 13 | 14 | def get_affine_transform(center, 15 | scale, 16 | rot, 17 | output_size, 18 | shift=np.array([0, 0], dtype=np.float32), 19 | inv=0): 20 | """get affine matrix""" 21 | if not isinstance(scale, np.ndarray) and not isinstance(scale, list): 22 | scale = np.array([scale, scale], dtype=np.float32) 23 | 24 | scale_tmp = scale 25 | src_w = scale_tmp[0] 26 | dst_w = output_size[0] 27 | dst_h = output_size[1] 28 | 29 | rot_rad = np.pi * rot / 180 30 | src_dir = get_dir([0, src_w * -0.5], rot_rad) 31 | dst_dir = np.array([0, dst_w * -0.5], np.float32) 32 | 33 | src = np.zeros((3, 2), dtype=np.float32) 34 | dst = np.zeros((3, 2), dtype=np.float32) 35 | src[0, :] = center + scale_tmp * shift 36 | src[1, :] = center + src_dir + scale_tmp * shift 37 | dst[0, :] = [dst_w * 0.5, dst_h * 0.5] 38 | dst[1, :] = np.array([dst_w * 0.5, dst_h * 0.5], np.float32) + dst_dir 39 | 40 | src[2:, :] = get_3rd_point(src[0, :], src[1, :]) 41 | dst[2:, :] = get_3rd_point(dst[0, :], dst[1, :]) 42 | 43 | if inv: 44 | trans = cv2.getAffineTransform(np.float32(dst), np.float32(src)) 45 | else: 46 | trans = cv2.getAffineTransform(np.float32(src), np.float32(dst)) 47 | 48 | return trans 49 | 50 | def affine_transform(pt, t): 51 | """get new position after affine""" 52 | new_pt = np.array([pt[0], pt[1], 1.], dtype=np.float32).T 53 | new_pt = np.dot(t, new_pt) 54 | return new_pt[:2] 55 | 56 | def get_3rd_point(a, b): 57 | """get the third point to calculate affine matrix""" 58 | direct = a - b 59 | return b + np.array([-direct[1], direct[0]], dtype=np.float32) 60 | 61 | 62 | def get_dir(src_point, rot_rad): 63 | """get new pos after rotate""" 64 | sn, cs = np.sin(rot_rad), np.cos(rot_rad) 65 | 66 | src_result = [0, 0] 67 | src_result[0] = src_point[0] * cs - src_point[1] * sn 68 | src_result[1] = src_point[0] * sn + src_point[1] * cs 69 | 70 | return src_result 71 | 72 | def post_process(dets, c, s, h, w, num_classes): 73 | """rescale detection to original scale""" 74 | # c, s, h, w = meta['c'], meta['s'], meta['out_height'], meta['out_width'] 75 | # ret = [] 76 | for i in range(dets.shape[0]): 77 | top_preds = {} 78 | dets[i, :, :2] = transform_preds( 79 | dets[i, :, 0:2], c, s, (w, h)) 80 | dets[i, :, 2:4] = transform_preds( 81 | dets[i, :, 2:4], c, s, (w, h)) 82 | # classes = dets[i, :, -1] 83 | # for j in range(num_classes): 84 | # inds = (classes == j) 85 | # top_preds[j + 1] = np.concatenate([ 86 | # dets[i, inds, :4].astype(np.float32), 87 | # dets[i, inds, 4:5].astype(np.float32)], axis=1).tolist() 88 | # ret.append(top_preds) 89 | 90 | # print("++++++++++++") 91 | # print(dets) 92 | # print("++++++++++++") 93 | # for j in range(1, num_classes + 1): 94 | # ret[0][j] = np.array(ret[0][j], dtype=np.float32).reshape(-1, 5) 95 | # ret[0][j][:, :4] /= scale 96 | # return ret[0] 97 | return dets -------------------------------------------------------------------------------- /track1/mindspore/code/src/.ipynb_checkpoints/post_process-checkpoint.py: -------------------------------------------------------------------------------- 1 | import math 2 | import random 3 | import numpy as np 4 | import cv2 5 | 6 | def transform_preds(coords, center, scale, output_size): 7 | """transform prediction to new coords""" 8 | target_coords = np.zeros(coords.shape) 9 | trans = get_affine_transform(center, scale, 0, output_size, inv=1) 10 | for p in range(coords.shape[0]): 11 | target_coords[p, 0:2] = affine_transform(coords[p, 0:2], trans) 12 | return target_coords 13 | 14 | def get_affine_transform(center, 15 | scale, 16 | rot, 17 | output_size, 18 | shift=np.array([0, 0], dtype=np.float32), 19 | inv=0): 20 | """get affine matrix""" 21 | if not isinstance(scale, np.ndarray) and not isinstance(scale, list): 22 | scale = np.array([scale, scale], dtype=np.float32) 23 | 24 | scale_tmp = scale 25 | src_w = scale_tmp[0] 26 | dst_w = output_size[0] 27 | dst_h = output_size[1] 28 | 29 | rot_rad = np.pi * rot / 180 30 | src_dir = get_dir([0, src_w * -0.5], rot_rad) 31 | dst_dir = np.array([0, dst_w * -0.5], np.float32) 32 | 33 | src = np.zeros((3, 2), dtype=np.float32) 34 | dst = np.zeros((3, 2), dtype=np.float32) 35 | src[0, :] = center + scale_tmp * shift 36 | src[1, :] = center + src_dir + scale_tmp * shift 37 | dst[0, :] = [dst_w * 0.5, dst_h * 0.5] 38 | dst[1, :] = np.array([dst_w * 0.5, dst_h * 0.5], np.float32) + dst_dir 39 | 40 | src[2:, :] = get_3rd_point(src[0, :], src[1, :]) 41 | dst[2:, :] = get_3rd_point(dst[0, :], dst[1, :]) 42 | 43 | if inv: 44 | trans = cv2.getAffineTransform(np.float32(dst), np.float32(src)) 45 | else: 46 | trans = cv2.getAffineTransform(np.float32(src), np.float32(dst)) 47 | 48 | return trans 49 | 50 | def affine_transform(pt, t): 51 | """get new position after affine""" 52 | new_pt = np.array([pt[0], pt[1], 1.], dtype=np.float32).T 53 | new_pt = np.dot(t, new_pt) 54 | return new_pt[:2] 55 | 56 | def get_3rd_point(a, b): 57 | """get the third point to calculate affine matrix""" 58 | direct = a - b 59 | return b + np.array([-direct[1], direct[0]], dtype=np.float32) 60 | 61 | 62 | def get_dir(src_point, rot_rad): 63 | """get new pos after rotate""" 64 | sn, cs = np.sin(rot_rad), np.cos(rot_rad) 65 | 66 | src_result = [0, 0] 67 | src_result[0] = src_point[0] * cs - src_point[1] * sn 68 | src_result[1] = src_point[0] * sn + src_point[1] * cs 69 | 70 | return src_result 71 | 72 | def post_process(dets, c, s, h, w, num_classes): 73 | """rescale detection to original scale""" 74 | # c, s, h, w = meta['c'], meta['s'], meta['out_height'], meta['out_width'] 75 | # ret = [] 76 | for i in range(dets.shape[0]): 77 | top_preds = {} 78 | dets[i, :, :2] = transform_preds( 79 | dets[i, :, 0:2], c, s, (w, h)) 80 | dets[i, :, 2:4] = transform_preds( 81 | dets[i, :, 2:4], c, s, (w, h)) 82 | # classes = dets[i, :, -1] 83 | # for j in range(num_classes): 84 | # inds = (classes == j) 85 | # top_preds[j + 1] = np.concatenate([ 86 | # dets[i, inds, :4].astype(np.float32), 87 | # dets[i, inds, 4:5].astype(np.float32)], axis=1).tolist() 88 | # ret.append(top_preds) 89 | 90 | # print("++++++++++++") 91 | # print(dets) 92 | # print("++++++++++++") 93 | # for j in range(1, num_classes + 1): 94 | # ret[0][j] = np.array(ret[0][j], dtype=np.float32).reshape(-1, 5) 95 | # ret[0][j][:, :4] /= scale 96 | # return ret[0] 97 | return dets -------------------------------------------------------------------------------- /track2/model/modules/inplace_abn/bn.py: -------------------------------------------------------------------------------- 1 | 2 | import torch 3 | from queue import Queue 4 | 5 | from .functions import * 6 | from .abn import ABN 7 | 8 | 9 | class InPlaceABN(ABN): 10 | """InPlace Activated Batch Normalization""" 11 | 12 | def forward(self, x): 13 | exponential_average_factor = 0.0 14 | 15 | if self.training and self.track_running_stats: 16 | self.num_batches_tracked += 1 17 | if self.momentum is None: # use cumulative moving average 18 | exponential_average_factor = 1.0 / self.num_batches_tracked.item() 19 | else: # use exponential moving average 20 | exponential_average_factor = self.momentum 21 | 22 | return inplace_abn(x, self.weight, self.bias, self.running_mean, self.running_var, 23 | self.training or not self.track_running_stats, 24 | exponential_average_factor, self.eps, self.activation, self.slope) 25 | 26 | 27 | class InPlaceABNSync(ABN): 28 | """InPlace Activated Batch Normalization with cross-GPU synchronization 29 | 30 | This assumes that it will be replicated across GPUs using the same mechanism as in `nn.DataParallel`. 31 | """ 32 | 33 | def __init__(self, num_features, eps=1e-5, momentum=0.1, affine=True, track_running_stats=True, 34 | activation="leaky_relu", slope=0.01, devices=None): 35 | """Creates a synchronized, InPlace Activated Batch Normalization module 36 | 37 | Parameters 38 | ---------- 39 | num_features : int 40 | Number of feature channels in the input and output. 41 | devices : list of int or None 42 | IDs of the GPUs that will run the replicas of this module. 43 | eps : float 44 | Small constant to prevent numerical issues. 45 | momentum : float 46 | Momentum factor applied to compute running statistics as. 47 | affine : bool 48 | If `True` apply learned scale and shift transformation after normalization. 49 | activation : str 50 | Name of the activation functions, one of: `leaky_relu`, `elu` or `none`. 51 | slope : float 52 | Negative slope for the `leaky_relu` activation. 53 | """ 54 | super().__init__(num_features=num_features, eps=eps, momentum=momentum, affine=affine, 55 | track_running_stats=track_running_stats, activation=activation, slope=slope) 56 | self.devices = devices if devices else list(range(torch.cuda.device_count())) 57 | 58 | # Initialize queues 59 | self.worker_ids = self.devices[1:] 60 | self.master_queue = Queue(len(self.worker_ids)) 61 | self.worker_queues = [Queue(1) for _ in self.worker_ids] 62 | 63 | def forward(self, x): 64 | if len(self.devices) < 2: 65 | # fallback for CPU mode or single GPU mode 66 | return super().forward(x) 67 | 68 | if x.get_device() == self.devices[0]: 69 | # Master mode 70 | extra = { 71 | "is_master": True, 72 | "master_queue": self.master_queue, 73 | "worker_queues": self.worker_queues, 74 | "worker_ids": self.worker_ids 75 | } 76 | else: 77 | # Worker mode 78 | extra = { 79 | "is_master": False, 80 | "master_queue": self.master_queue, 81 | "worker_queue": self.worker_queues[self.worker_ids.index(x.get_device())] 82 | } 83 | 84 | return inplace_abn_sync(x, self.weight, self.bias, self.running_mean, self.running_var, 85 | extra, self.training, self.momentum, self.eps, self.activation, self.slope) 86 | 87 | def extra_repr(self): 88 | rep = super().extra_repr() 89 | rep += ', devices={devices}'.format(**self.__dict__) 90 | return rep 91 | -------------------------------------------------------------------------------- /track2/model/modules/inplace_abn/src/inplace_abn_cpu.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include 4 | 5 | #include "inplace_abn.h" 6 | 7 | at::Tensor reduce_sum(at::Tensor x) { 8 | if (x.ndimension() == 2) { 9 | return x.sum(0); 10 | } else { 11 | auto x_view = x.view({x.size(0), x.size(1), -1}); 12 | return x_view.sum(-1).sum(0); 13 | } 14 | } 15 | 16 | at::Tensor broadcast_to(at::Tensor v, at::Tensor x) { 17 | if (x.ndimension() == 2) { 18 | return v; 19 | } else { 20 | std::vector broadcast_size = {1, -1}; 21 | for (int64_t i = 2; i < x.ndimension(); ++i) 22 | broadcast_size.push_back(1); 23 | 24 | return v.view(broadcast_size); 25 | } 26 | } 27 | 28 | int64_t count(at::Tensor x) { 29 | int64_t count = x.size(0); 30 | for (int64_t i = 2; i < x.ndimension(); ++i) 31 | count *= x.size(i); 32 | 33 | return count; 34 | } 35 | 36 | at::Tensor invert_affine(at::Tensor z, at::Tensor weight, at::Tensor bias, bool affine, float eps) { 37 | if (affine) { 38 | return (z - broadcast_to(bias, z)) / broadcast_to(at::abs(weight) + eps, z); 39 | } else { 40 | return z; 41 | } 42 | } 43 | 44 | std::vector mean_var_cpu(at::Tensor x) { 45 | auto num = count(x); 46 | auto mean = reduce_sum(x) / num; 47 | auto diff = x - broadcast_to(mean, x); 48 | auto var = reduce_sum(diff.pow(2)) / num; 49 | 50 | return {mean, var}; 51 | } 52 | 53 | at::Tensor forward_cpu(at::Tensor x, at::Tensor mean, at::Tensor var, at::Tensor weight, at::Tensor bias, 54 | bool affine, float eps) { 55 | auto gamma = affine ? at::abs(weight) + eps : at::ones_like(var); 56 | auto mul = at::rsqrt(var + eps) * gamma; 57 | 58 | x.sub_(broadcast_to(mean, x)); 59 | x.mul_(broadcast_to(mul, x)); 60 | if (affine) x.add_(broadcast_to(bias, x)); 61 | 62 | return x; 63 | } 64 | 65 | std::vector edz_eydz_cpu(at::Tensor z, at::Tensor dz, at::Tensor weight, at::Tensor bias, 66 | bool affine, float eps) { 67 | auto edz = reduce_sum(dz); 68 | auto y = invert_affine(z, weight, bias, affine, eps); 69 | auto eydz = reduce_sum(y * dz); 70 | 71 | return {edz, eydz}; 72 | } 73 | 74 | std::vector backward_cpu(at::Tensor z, at::Tensor dz, at::Tensor var, at::Tensor weight, at::Tensor bias, 75 | at::Tensor edz, at::Tensor eydz, bool affine, float eps) { 76 | auto y = invert_affine(z, weight, bias, affine, eps); 77 | auto mul = affine ? at::rsqrt(var + eps) * (at::abs(weight) + eps) : at::rsqrt(var + eps); 78 | 79 | auto num = count(z); 80 | auto dx = (dz - broadcast_to(edz / num, dz) - y * broadcast_to(eydz / num, dz)) * broadcast_to(mul, dz); 81 | 82 | auto dweight = at::empty(z.type(), {0}); 83 | auto dbias = at::empty(z.type(), {0}); 84 | if (affine) { 85 | dweight = eydz * at::sign(weight); 86 | dbias = edz; 87 | } 88 | 89 | return {dx, dweight, dbias}; 90 | } 91 | 92 | void leaky_relu_backward_cpu(at::Tensor z, at::Tensor dz, float slope) { 93 | AT_DISPATCH_FLOATING_TYPES(z.type(), "leaky_relu_backward_cpu", ([&] { 94 | int64_t count = z.numel(); 95 | auto *_z = z.data(); 96 | auto *_dz = dz.data(); 97 | 98 | for (int64_t i = 0; i < count; ++i) { 99 | if (_z[i] < 0) { 100 | _z[i] *= 1 / slope; 101 | _dz[i] *= slope; 102 | } 103 | } 104 | })); 105 | } 106 | 107 | void elu_backward_cpu(at::Tensor z, at::Tensor dz) { 108 | AT_DISPATCH_FLOATING_TYPES(z.type(), "elu_backward_cpu", ([&] { 109 | int64_t count = z.numel(); 110 | auto *_z = z.data(); 111 | auto *_dz = dz.data(); 112 | 113 | for (int64_t i = 0; i < count; ++i) { 114 | if (_z[i] < 0) { 115 | _z[i] = log1p(_z[i]); 116 | _dz[i] *= (_z[i] + 1.f); 117 | } 118 | } 119 | })); 120 | } -------------------------------------------------------------------------------- /track1/mindspore/code/pre/gen_train_txt.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Aug 26 09:14:11 2022 4 | 5 | @author: DELL 6 | """ 7 | import cv2 8 | import glob 9 | import tqdm 10 | import os 11 | 12 | 13 | # ''' 14 | # 生成train.txt val.txt 15 | # ''' 16 | # def gen_train_txt(annotations_crop_dir, train_all_txt_path): 17 | 18 | # txt_paths = sorted(glob.glob(annotations_crop_dir+"/*.txt")) 19 | 20 | # if os.path.exists(train_all_txt_path): os.remove(train_all_txt_path) 21 | 22 | # # 打开要写的train_all_txt_path 23 | # train_all_txt_f = open(train_all_txt_path, 'a') 24 | 25 | # for i, txt_path in tqdm.tqdm(enumerate(txt_paths)): 26 | # txt_path = os.path.abspath(txt_path) 27 | # image_path = txt_path.replace("annotations_crop", "images_crop").replace("txt", "tif") 28 | # label_path = txt_path.replace("annotations_crop", "masks_crop").replace("txt", "png") 29 | # train_all_txt_f.write(image_path+" "+label_path) 30 | 31 | # with open(txt_path, "r") as f: # 打开文件 32 | # data = f.read() # 读取文件 33 | # annotations = data.split("\n") 34 | # for annotation in annotations: 35 | # annotation = annotation.split(" ") 36 | # if len(annotation)<5: 37 | # continue 38 | # min_x,min_y,max_x,max_y,category = annotation 39 | # train_all_txt_f.write(f' {min_x},{min_y},{max_x},{max_y},{category}') 40 | # train_all_txt_f.write('\n') 41 | 42 | # train_all_txt_f.close() 43 | 44 | 45 | ''' 46 | 生成train.txt val.txt 47 | ''' 48 | def gen_train_txt(annotations_crop_dir, annotations_crop_dir2, train_all_txt_path): 49 | 50 | txt_paths = sorted(glob.glob(annotations_crop_dir+"/*.txt")) 51 | 52 | if os.path.exists(train_all_txt_path): os.remove(train_all_txt_path) 53 | 54 | # 打开要写的train_all_txt_path 55 | train_all_txt_f = open(train_all_txt_path, 'a') 56 | 57 | for i, txt_path in tqdm.tqdm(enumerate(txt_paths)): 58 | txt_path = os.path.abspath(txt_path) 59 | image_path = txt_path.replace("annotations_crop", "images_crop").replace("txt", "tif") 60 | label_path = txt_path.replace("annotations_crop", "masks_crop").replace("txt", "png") 61 | train_all_txt_f.write(image_path+" "+label_path) 62 | 63 | with open(txt_path, "r") as f: # 打开文件 64 | data = f.read() # 读取文件 65 | annotations = data.split("\n") 66 | for annotation in annotations: 67 | annotation = annotation.split(" ") 68 | if len(annotation)<5: 69 | continue 70 | min_x,min_y,max_x,max_y,category = annotation 71 | train_all_txt_f.write(f' {min_x},{min_y},{max_x},{max_y},{category}') 72 | train_all_txt_f.write('\n') 73 | 74 | txt_paths = sorted(glob.glob(annotations_crop_dir2+"/*.txt")) 75 | for i, txt_path in tqdm.tqdm(enumerate(txt_paths)): 76 | txt_path = os.path.abspath(txt_path) 77 | image_path = txt_path.replace("annotations_crop", "images_crop").replace("txt", "tif") 78 | label_path = txt_path.replace("annotations_crop", "masks_crop").replace("txt", "png") 79 | train_all_txt_f.write(image_path+" "+label_path) 80 | 81 | with open(txt_path, "r") as f: # 打开文件 82 | data = f.read() # 读取文件 83 | annotations = data.split("\n") 84 | for annotation in annotations: 85 | annotation = annotation.split(" ") 86 | if len(annotation)<5: 87 | continue 88 | min_x,min_y,max_x,max_y,category = annotation 89 | train_all_txt_f.write(f' {min_x},{min_y},{max_x},{max_y},{category}') 90 | train_all_txt_f.write('\n') 91 | 92 | train_all_txt_f.close() 93 | 94 | if __name__ == "__main__": 95 | 96 | 97 | annotations_crop_dir = '../../data/fusai_release/train/annotations_crop' 98 | train_all_txt_path = 'code_mindspore/train_all.txt' 99 | gen_train_txt(annotations_crop_dir, train_all_txt_path) -------------------------------------------------------------------------------- /track1/mindspore/code/pre/.ipynb_checkpoints/gen_train_txt-checkpoint.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Fri Aug 26 09:14:11 2022 4 | 5 | @author: DELL 6 | """ 7 | import cv2 8 | import glob 9 | import tqdm 10 | import os 11 | 12 | 13 | # ''' 14 | # 生成train.txt val.txt 15 | # ''' 16 | # def gen_train_txt(annotations_crop_dir, train_all_txt_path): 17 | 18 | # txt_paths = sorted(glob.glob(annotations_crop_dir+"/*.txt")) 19 | 20 | # if os.path.exists(train_all_txt_path): os.remove(train_all_txt_path) 21 | 22 | # # 打开要写的train_all_txt_path 23 | # train_all_txt_f = open(train_all_txt_path, 'a') 24 | 25 | # for i, txt_path in tqdm.tqdm(enumerate(txt_paths)): 26 | # txt_path = os.path.abspath(txt_path) 27 | # image_path = txt_path.replace("annotations_crop", "images_crop").replace("txt", "tif") 28 | # label_path = txt_path.replace("annotations_crop", "masks_crop").replace("txt", "png") 29 | # train_all_txt_f.write(image_path+" "+label_path) 30 | 31 | # with open(txt_path, "r") as f: # 打开文件 32 | # data = f.read() # 读取文件 33 | # annotations = data.split("\n") 34 | # for annotation in annotations: 35 | # annotation = annotation.split(" ") 36 | # if len(annotation)<5: 37 | # continue 38 | # min_x,min_y,max_x,max_y,category = annotation 39 | # train_all_txt_f.write(f' {min_x},{min_y},{max_x},{max_y},{category}') 40 | # train_all_txt_f.write('\n') 41 | 42 | # train_all_txt_f.close() 43 | 44 | 45 | ''' 46 | 生成train.txt val.txt 47 | ''' 48 | def gen_train_txt(annotations_crop_dir, annotations_crop_dir2, train_all_txt_path): 49 | 50 | txt_paths = sorted(glob.glob(annotations_crop_dir+"/*.txt")) 51 | 52 | if os.path.exists(train_all_txt_path): os.remove(train_all_txt_path) 53 | 54 | # 打开要写的train_all_txt_path 55 | train_all_txt_f = open(train_all_txt_path, 'a') 56 | 57 | for i, txt_path in tqdm.tqdm(enumerate(txt_paths)): 58 | txt_path = os.path.abspath(txt_path) 59 | image_path = txt_path.replace("annotations_crop", "images_crop").replace("txt", "tif") 60 | label_path = txt_path.replace("annotations_crop", "masks_crop").replace("txt", "png") 61 | train_all_txt_f.write(image_path+" "+label_path) 62 | 63 | with open(txt_path, "r") as f: # 打开文件 64 | data = f.read() # 读取文件 65 | annotations = data.split("\n") 66 | for annotation in annotations: 67 | annotation = annotation.split(" ") 68 | if len(annotation)<5: 69 | continue 70 | min_x,min_y,max_x,max_y,category = annotation 71 | train_all_txt_f.write(f' {min_x},{min_y},{max_x},{max_y},{category}') 72 | train_all_txt_f.write('\n') 73 | 74 | txt_paths = sorted(glob.glob(annotations_crop_dir2+"/*.txt")) 75 | for i, txt_path in tqdm.tqdm(enumerate(txt_paths)): 76 | txt_path = os.path.abspath(txt_path) 77 | image_path = txt_path.replace("annotations_crop", "images_crop").replace("txt", "tif") 78 | label_path = txt_path.replace("annotations_crop", "masks_crop").replace("txt", "png") 79 | train_all_txt_f.write(image_path+" "+label_path) 80 | 81 | with open(txt_path, "r") as f: # 打开文件 82 | data = f.read() # 读取文件 83 | annotations = data.split("\n") 84 | for annotation in annotations: 85 | annotation = annotation.split(" ") 86 | if len(annotation)<5: 87 | continue 88 | min_x,min_y,max_x,max_y,category = annotation 89 | train_all_txt_f.write(f' {min_x},{min_y},{max_x},{max_y},{category}') 90 | train_all_txt_f.write('\n') 91 | 92 | train_all_txt_f.close() 93 | 94 | if __name__ == "__main__": 95 | 96 | 97 | annotations_crop_dir = '../../data/fusai_release/train/annotations_crop' 98 | train_all_txt_path = 'code_mindspore/train_all.txt' 99 | gen_train_txt(annotations_crop_dir, train_all_txt_path) -------------------------------------------------------------------------------- /track1/mindspore/code/scripts/ascend310_inference.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # Copyright 2021 Huawei Technologies Co., Ltd 3 | # 4 | # Licensed under the Apache License, Version 2.0 (the "License"); 5 | # you may not use this file except in compliance with the License. 6 | # You may obtain a copy of the License at 7 | # 8 | # http://www.apache.org/licenses/LICENSE-2.0 9 | # 10 | # Unless required by applicable law or agreed to in writing, software 11 | # distributed under the License is distributed on an "AS IS" BASIS, 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | # See the License for the specific language governing permissions and 14 | # limitations under the License. 15 | # ============================================================================ 16 | 17 | if [[ $# != 3 ]]; then 18 | echo "Usage:" 19 | echo "bash scripts/ascend310_inference.sh [MINDIR_PATH] [DATA_PATH] [DEVICE_ID]" 20 | exit 1 21 | fi 22 | 23 | get_real_path() { 24 | if [ "${1:0:1}" == "/" ]; then 25 | echo "$1" 26 | else 27 | echo "$(realpath -m $PWD/$1)" 28 | fi 29 | } 30 | 31 | model=$(get_real_path $1) 32 | data_path=$(get_real_path $2) 33 | device_id=$3 34 | 35 | echo "mindir name: "$model 36 | echo "dataset path: "$data_path 37 | echo "device id: "$device_id 38 | 39 | export ASCEND_HOME=/usr/local/Ascend/ 40 | if [ -d ${ASCEND_HOME}/ascend-toolkit ]; then 41 | export PATH=$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/ccec_compiler/bin:$ASCEND_HOME/ascend-toolkit/latest/atc/bin:$PATH 42 | export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/ascend-toolkit/latest/atc/lib64:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH 43 | export TBE_IMPL_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp/op_impl/built-in/ai_core/tbe 44 | export PYTHONPATH=${TBE_IMPL_PATH}:$ASCEND_HOME/ascend-toolkit/latest/fwkacllib/python/site-packages:$PYTHONPATH 45 | export ASCEND_OPP_PATH=$ASCEND_HOME/ascend-toolkit/latest/opp 46 | else 47 | export PATH=$ASCEND_HOME/atc/ccec_compiler/bin:$ASCEND_HOME/atc/bin:$PATH 48 | export LD_LIBRARY_PATH=/usr/local/lib:$ASCEND_HOME/atc/lib64:$ASCEND_HOME/acllib/lib64:$ASCEND_HOME/driver/lib64:$ASCEND_HOME/add-ons:$LD_LIBRARY_PATH 49 | export PYTHONPATH=$ASCEND_HOME/atc/python/site-packages:$PYTHONPATH 50 | export ASCEND_OPP_PATH=$ASCEND_HOME/opp 51 | fi 52 | 53 | function compile_app() 54 | { 55 | cd ./ascend310_infer/src/ || exit 56 | if [ -f "Makefile" ]; then 57 | make clean 58 | fi 59 | bash build.sh &> build.log 60 | } 61 | 62 | function preprocess_data() 63 | { 64 | if [ -d preprocess_Result ]; then 65 | rm -rf ./preprocess_Result 66 | fi 67 | mkdir preprocess_Result 68 | python3.7 ./preprocess.py --data_path=$data_path --train_path=./preprocess_Result &> preprocess.log 69 | } 70 | 71 | function infer() 72 | { 73 | cd - || exit 74 | if [ -d result_Files ]; then 75 | rm -rf ./result_Files 76 | fi 77 | if [ -d time_Result ]; then 78 | rm -rf ./time_Result 79 | fi 80 | mkdir result_Files 81 | mkdir time_Result 82 | ./ascend310_infer/src/main --gmindir_path=$model --gdataset_path=./preprocess_Result/image --gdevice_id=$device_id &> infer.log 83 | } 84 | 85 | function cal_acc() 86 | { 87 | python3.7 ./postprocess.py --result_path=./result_Files --label_path=./preprocess_Result/label &> acc.log 88 | if [ $? -ne 0 ]; then 89 | echo "Calculate accuracy failed." 90 | exit 1 91 | fi 92 | } 93 | 94 | preprocess_data 95 | if [ $? -ne 0 ]; then 96 | echo "Dataset preprocessing failed." 97 | exit 1 98 | fi 99 | 100 | compile_app 101 | if [ $? -ne 0 ]; then 102 | echo "Compile app code failed." 103 | exit 1 104 | fi 105 | 106 | infer 107 | if [ $? -ne 0 ]; then 108 | echo "Execute inference failed." 109 | exit 1 110 | fi 111 | 112 | cal_acc 113 | if [ $? -ne 0 ]; then 114 | echo "Calculate mIoU failed." 115 | exit 1 116 | fi 117 | -------------------------------------------------------------------------------- /track2/model/modules/backbones/registry.py: -------------------------------------------------------------------------------- 1 | """ Model Registry 2 | Hacked together by / Copyright 2020 Ross Wightman 3 | """ 4 | 5 | import sys 6 | import re 7 | import fnmatch 8 | from collections import defaultdict 9 | 10 | __all__ = ['list_models', 'is_model', 'model_entrypoint', 'list_modules', 'is_model_in_modules'] 11 | 12 | _module_to_models = defaultdict(set) # dict of sets to check membership of model in module 13 | _model_to_module = {} # mapping of model names to module names 14 | _model_entrypoints = {} # mapping of model names to entrypoint fns 15 | _model_has_pretrained = set() # set of model names that have pretrained weight url present 16 | 17 | 18 | def register_model(fn): 19 | # lookup containing module 20 | mod = sys.modules[fn.__module__] 21 | module_name_split = fn.__module__.split('.') 22 | module_name = module_name_split[-1] if len(module_name_split) else '' 23 | 24 | # add model to __all__ in module 25 | model_name = fn.__name__ 26 | if hasattr(mod, '__all__'): 27 | mod.__all__.append(model_name) 28 | else: 29 | mod.__all__ = [model_name] 30 | 31 | # add entries to registry dict/sets 32 | _model_entrypoints[model_name] = fn 33 | _model_to_module[model_name] = module_name 34 | _module_to_models[module_name].add(model_name) 35 | has_pretrained = False # check if model has a pretrained url to allow filtering on this 36 | if hasattr(mod, 'default_cfgs') and model_name in mod.default_cfgs: 37 | # this will catch all models that have entrypoint matching cfg key, but miss any aliasing 38 | # entrypoints or non-matching combos 39 | has_pretrained = 'url' in mod.default_cfgs[model_name] and 'http' in mod.default_cfgs[model_name]['url'] 40 | if has_pretrained: 41 | _model_has_pretrained.add(model_name) 42 | return fn 43 | 44 | 45 | def _natural_key(string_): 46 | return [int(s) if s.isdigit() else s for s in re.split(r'(\d+)', string_.lower())] 47 | 48 | 49 | def list_models(filter='', module='', pretrained=False, exclude_filters=''): 50 | """ Return list of available model names, sorted alphabetically 51 | Args: 52 | filter (str) - Wildcard filter string that works with fnmatch 53 | module (str) - Limit model selection to a specific sub-module (ie 'gen_efficientnet') 54 | pretrained (bool) - Include only models with pretrained weights if True 55 | exclude_filters (str or list[str]) - Wildcard filters to exclude models after including them with filter 56 | Example: 57 | model_list('gluon_resnet*') -- returns all models starting with 'gluon_resnet' 58 | model_list('*resnext*, 'resnet') -- returns all models with 'resnext' in 'resnet' module 59 | """ 60 | if module: 61 | models = list(_module_to_models[module]) 62 | else: 63 | models = _model_entrypoints.keys() 64 | if filter: 65 | models = fnmatch.filter(models, filter) # include these models 66 | if exclude_filters: 67 | if not isinstance(exclude_filters, list): 68 | exclude_filters = [exclude_filters] 69 | for xf in exclude_filters: 70 | exclude_models = fnmatch.filter(models, xf) # exclude these models 71 | if len(exclude_models): 72 | models = set(models).difference(exclude_models) 73 | if pretrained: 74 | models = _model_has_pretrained.intersection(models) 75 | return list(sorted(models, key=_natural_key)) 76 | 77 | 78 | def is_model(model_name): 79 | """ Check if a model name exists 80 | """ 81 | return model_name in _model_entrypoints 82 | 83 | 84 | def model_entrypoint(model_name): 85 | """Fetch a model entrypoint for specified model name 86 | """ 87 | return _model_entrypoints[model_name] 88 | 89 | 90 | def list_modules(): 91 | """ Return list of module names that contain models / model entrypoints 92 | """ 93 | modules = _module_to_models.keys() 94 | return list(sorted(modules)) 95 | 96 | 97 | def is_model_in_modules(model_name, module_names): 98 | """Check if a model exists within a subset of modules 99 | Args: 100 | model_name (str) - name of model to check 101 | module_names (tuple, list, set) - names of modules to search in 102 | """ 103 | assert isinstance(module_names, (tuple, list, set)) 104 | return any(model_name in _module_to_models[n] for n in module_names) -------------------------------------------------------------------------------- /track1/mindspore/code/src/callback.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Huawei Technologies Co., Ltd 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================ 15 | 16 | """Callback for inference while training.""" 17 | import os 18 | import mindspore.ops as P 19 | from mindspore.train.callback import Callback 20 | from mindspore import save_checkpoint 21 | import numpy as np 22 | 23 | 24 | def get_confusion_matrix(label, pred, shape, num_classes, ignore_label): 25 | """Calcute the confusion matrix by given label and pred.""" 26 | output = pred.transpose(0, 2, 3, 1) # NHWC 27 | seg_pred = np.asarray(np.argmax(output, axis=3), dtype=np.uint8) # NHW 28 | seg_gt = np.asarray(label[:, :shape[-2], :shape[-1]], dtype=np.int32) # NHW 29 | 30 | ignore_index = seg_gt != ignore_label # NHW 31 | seg_gt = seg_gt[ignore_index] # NHW 32 | seg_pred = seg_pred[ignore_index] # NHW 33 | 34 | index = (seg_gt * num_classes + seg_pred).astype(np.int32) 35 | label_count = np.bincount(index) 36 | confusion_matrix = np.zeros((num_classes, num_classes)) 37 | 38 | for i_label in range(num_classes): 39 | for i_pred in range(num_classes): 40 | cur_index = i_label * num_classes + i_pred 41 | if cur_index < len(label_count): 42 | confusion_matrix[i_label, i_pred] = label_count[cur_index] 43 | return confusion_matrix 44 | 45 | 46 | def evaluate_model(net, data_helper, num_classes, ignore_label): 47 | """Inference function.""" 48 | net.set_train(False) 49 | confusion_matrix = np.zeros((num_classes, num_classes)) 50 | for item in data_helper: 51 | image = item[0] 52 | label = item[1].asnumpy() 53 | shape = label.shape 54 | pred = net(image) 55 | pred = pred[-1] 56 | pred = P.ResizeBilinear((shape[-2], shape[-1]))(pred).asnumpy() 57 | confusion_matrix += get_confusion_matrix(label, pred, shape, num_classes, ignore_label) 58 | pos = confusion_matrix.sum(1) 59 | res = confusion_matrix.sum(0) 60 | tp = np.diag(confusion_matrix) 61 | IoU_array = (tp / np.maximum(1.0, pos + res - tp)) 62 | mean_IoU = IoU_array.mean() 63 | return IoU_array, mean_IoU 64 | 65 | 66 | class EvalCallback(Callback): 67 | """Callback for inference while training.""" 68 | def __init__(self, network, eval_data, num_classes, ignore_label, train_url, eval_interval=1): 69 | self.network = network 70 | self.eval_data = eval_data 71 | self.best_iouarray = None 72 | self.best_miou = 0 73 | self.best_epoch = 0 74 | self.num_classes = num_classes 75 | self.ignore_label = ignore_label 76 | self.eval_interval = eval_interval 77 | self.train_url = train_url 78 | 79 | def epoch_end(self, run_context): 80 | """Executions after each epoch.""" 81 | cb_param = run_context.original_args() 82 | cur_epoch = cb_param.cur_epoch_num 83 | device_id = int(os.getenv("DEVICE_ID")) 84 | if cur_epoch % self.eval_interval == 0: 85 | iou_array, miou = evaluate_model(self.network, self.eval_data, self.num_classes, self.ignore_label) 86 | if miou > self.best_miou: 87 | self.best_miou = miou 88 | self.best_iouarray = iou_array 89 | self.best_epoch = cur_epoch 90 | save_checkpoint(self.network, self.train_url + "/best_card%d.ckpt" % device_id) 91 | 92 | log_text1 = 'EPOCH: %d, mIoU: %.4f\n' % (cur_epoch, miou) 93 | log_text2 = 'BEST EPOCH: %s, BEST mIoU: %0.4f\n' % (self.best_epoch, self.best_miou) 94 | log_text3 = 'DEVICE_ID: %d\n' % device_id 95 | print("==================================================\n", 96 | log_text3, 97 | log_text1, 98 | log_text2, 99 | "==================================================") 100 | self.network.set_train(True) 101 | -------------------------------------------------------------------------------- /track1/mindspore/code/src/.ipynb_checkpoints/callback-checkpoint.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Huawei Technologies Co., Ltd 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================ 15 | 16 | """Callback for inference while training.""" 17 | import os 18 | import mindspore.ops as P 19 | from mindspore.train.callback import Callback 20 | from mindspore import save_checkpoint 21 | import numpy as np 22 | 23 | 24 | def get_confusion_matrix(label, pred, shape, num_classes, ignore_label): 25 | """Calcute the confusion matrix by given label and pred.""" 26 | output = pred.transpose(0, 2, 3, 1) # NHWC 27 | seg_pred = np.asarray(np.argmax(output, axis=3), dtype=np.uint8) # NHW 28 | seg_gt = np.asarray(label[:, :shape[-2], :shape[-1]], dtype=np.int32) # NHW 29 | 30 | ignore_index = seg_gt != ignore_label # NHW 31 | seg_gt = seg_gt[ignore_index] # NHW 32 | seg_pred = seg_pred[ignore_index] # NHW 33 | 34 | index = (seg_gt * num_classes + seg_pred).astype(np.int32) 35 | label_count = np.bincount(index) 36 | confusion_matrix = np.zeros((num_classes, num_classes)) 37 | 38 | for i_label in range(num_classes): 39 | for i_pred in range(num_classes): 40 | cur_index = i_label * num_classes + i_pred 41 | if cur_index < len(label_count): 42 | confusion_matrix[i_label, i_pred] = label_count[cur_index] 43 | return confusion_matrix 44 | 45 | 46 | def evaluate_model(net, data_helper, num_classes, ignore_label): 47 | """Inference function.""" 48 | net.set_train(False) 49 | confusion_matrix = np.zeros((num_classes, num_classes)) 50 | for item in data_helper: 51 | image = item[0] 52 | label = item[1].asnumpy() 53 | shape = label.shape 54 | pred = net(image) 55 | pred = pred[-1] 56 | pred = P.ResizeBilinear((shape[-2], shape[-1]))(pred).asnumpy() 57 | confusion_matrix += get_confusion_matrix(label, pred, shape, num_classes, ignore_label) 58 | pos = confusion_matrix.sum(1) 59 | res = confusion_matrix.sum(0) 60 | tp = np.diag(confusion_matrix) 61 | IoU_array = (tp / np.maximum(1.0, pos + res - tp)) 62 | mean_IoU = IoU_array.mean() 63 | return IoU_array, mean_IoU 64 | 65 | 66 | class EvalCallback(Callback): 67 | """Callback for inference while training.""" 68 | def __init__(self, network, eval_data, num_classes, ignore_label, train_url, eval_interval=1): 69 | self.network = network 70 | self.eval_data = eval_data 71 | self.best_iouarray = None 72 | self.best_miou = 0 73 | self.best_epoch = 0 74 | self.num_classes = num_classes 75 | self.ignore_label = ignore_label 76 | self.eval_interval = eval_interval 77 | self.train_url = train_url 78 | 79 | def epoch_end(self, run_context): 80 | """Executions after each epoch.""" 81 | cb_param = run_context.original_args() 82 | cur_epoch = cb_param.cur_epoch_num 83 | device_id = int(os.getenv("DEVICE_ID")) 84 | if cur_epoch % self.eval_interval == 0: 85 | iou_array, miou = evaluate_model(self.network, self.eval_data, self.num_classes, self.ignore_label) 86 | if miou > self.best_miou: 87 | self.best_miou = miou 88 | self.best_iouarray = iou_array 89 | self.best_epoch = cur_epoch 90 | save_checkpoint(self.network, self.train_url + "/best_card%d.ckpt" % device_id) 91 | 92 | log_text1 = 'EPOCH: %d, mIoU: %.4f\n' % (cur_epoch, miou) 93 | log_text2 = 'BEST EPOCH: %s, BEST mIoU: %0.4f\n' % (self.best_epoch, self.best_miou) 94 | log_text3 = 'DEVICE_ID: %d\n' % device_id 95 | print("==================================================\n", 96 | log_text3, 97 | log_text1, 98 | log_text2, 99 | "==================================================") 100 | self.network.set_train(True) 101 | -------------------------------------------------------------------------------- /track1/mindspore/code/src/model_utils/moxing_adapter.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Huawei Technologies Co., Ltd 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================ 15 | 16 | """Moxing adapter for ModelArts""" 17 | import os 18 | import functools 19 | from mindspore import context 20 | from src.config import show_config 21 | 22 | 23 | _global_sync_count = 0 24 | 25 | 26 | def get_device_id(): 27 | device_id = os.getenv('DEVICE_ID', '0') 28 | return int(device_id) 29 | 30 | 31 | def get_device_num(): 32 | device_num = os.getenv('RANK_SIZE', '1') 33 | return int(device_num) 34 | 35 | 36 | def get_rank_id(): 37 | global_rank_id = os.getenv('RANK_ID', '0') 38 | return int(global_rank_id) 39 | 40 | 41 | def get_job_id(): 42 | job_id = os.getenv('JOB_ID') 43 | job_id = job_id if job_id != "" else "default" 44 | return job_id 45 | 46 | 47 | def sync_data(from_path, to_path): 48 | """ 49 | Download data from remote obs to local directory if the first url is remote url and the second one is local path 50 | Upload data from local directory to remote obs in contrast. 51 | """ 52 | import moxing as mox 53 | import time 54 | global _global_sync_count 55 | sync_lock = "/tmp/copy_sync.lock" + str(_global_sync_count) 56 | _global_sync_count += 1 57 | 58 | # Each server contains 8 devices as most. 59 | if get_device_id() % min(get_device_num(), 8) == 0 and not os.path.exists(sync_lock): 60 | print("from path: ", from_path) 61 | print("to path: ", to_path) 62 | mox.file.copy_parallel(from_path, to_path) 63 | print("===finish data synchronization===") 64 | try: 65 | os.mknod(sync_lock) 66 | except IOError: 67 | pass 68 | print("===save flag===") 69 | 70 | while True: 71 | if os.path.exists(sync_lock): 72 | break 73 | time.sleep(1) 74 | 75 | print("Finish sync data from {} to {}.".format(from_path, to_path)) 76 | 77 | 78 | def moxing_wrapper(config, pre_process=None, post_process=None): 79 | """ 80 | Moxing wrapper to download dataset and upload outputs. 81 | """ 82 | def wrapper(run_func): 83 | @functools.wraps(run_func) 84 | def wrapped_func(*args, **kwargs): 85 | # Download data from data_url 86 | if config.modelarts: 87 | if config.data_url: 88 | config.data_path = "/cache/train_data_path" 89 | sync_data(config.data_url, config.data_path) 90 | print("Dataset downloaded: ", os.listdir(config.data_path)) 91 | if config.checkpoint_url: 92 | config.checkpoint_path = "/cache/load_checkpoint.ckpt" 93 | sync_data(config.checkpoint_url, config.checkpoint_path) 94 | print("Preload downloaded: ", config.checkpoint_path) 95 | if config.train_url: 96 | config.output_path = "/cache/output_path" 97 | sync_data(config.train_url, config.output_path) 98 | print("Workspace downloaded: ", os.listdir(config.output_path)) 99 | if config.eval_data_url: 100 | config.eval_data_path = "/cache/eval_data_path" 101 | sync_data(config.eval_data_url, config.eval_data_path) 102 | print("Workspace downloaded: ", os.listdir(config.eval_data_path)) 103 | 104 | context.set_context(save_graphs_path=os.path.join(config.output_path, str(get_rank_id()))) 105 | config.device_num = get_device_num() 106 | config.device_id = get_device_id() 107 | if not os.path.exists(config.output_path): 108 | os.makedirs(config.output_path) 109 | 110 | if pre_process: 111 | pre_process() 112 | 113 | show_config(config) 114 | run_func(*args, **kwargs) 115 | 116 | # Upload data to train_url 117 | if config.modelarts: 118 | if post_process: 119 | post_process() 120 | 121 | if config.train_url: 122 | print("Start to copy output directory") 123 | sync_data(config.output_path, config.train_url) 124 | return wrapped_func 125 | return wrapper 126 | -------------------------------------------------------------------------------- /track1/mindspore/code/src/model_utils/.ipynb_checkpoints/moxing_adapter-checkpoint.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Huawei Technologies Co., Ltd 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================ 15 | 16 | """Moxing adapter for ModelArts""" 17 | import os 18 | import functools 19 | from mindspore import context 20 | from src.config import show_config 21 | 22 | 23 | _global_sync_count = 0 24 | 25 | 26 | def get_device_id(): 27 | device_id = os.getenv('DEVICE_ID', '0') 28 | return int(device_id) 29 | 30 | 31 | def get_device_num(): 32 | device_num = os.getenv('RANK_SIZE', '1') 33 | return int(device_num) 34 | 35 | 36 | def get_rank_id(): 37 | global_rank_id = os.getenv('RANK_ID', '0') 38 | return int(global_rank_id) 39 | 40 | 41 | def get_job_id(): 42 | job_id = os.getenv('JOB_ID') 43 | job_id = job_id if job_id != "" else "default" 44 | return job_id 45 | 46 | 47 | def sync_data(from_path, to_path): 48 | """ 49 | Download data from remote obs to local directory if the first url is remote url and the second one is local path 50 | Upload data from local directory to remote obs in contrast. 51 | """ 52 | import moxing as mox 53 | import time 54 | global _global_sync_count 55 | sync_lock = "/tmp/copy_sync.lock" + str(_global_sync_count) 56 | _global_sync_count += 1 57 | 58 | # Each server contains 8 devices as most. 59 | if get_device_id() % min(get_device_num(), 8) == 0 and not os.path.exists(sync_lock): 60 | print("from path: ", from_path) 61 | print("to path: ", to_path) 62 | mox.file.copy_parallel(from_path, to_path) 63 | print("===finish data synchronization===") 64 | try: 65 | os.mknod(sync_lock) 66 | except IOError: 67 | pass 68 | print("===save flag===") 69 | 70 | while True: 71 | if os.path.exists(sync_lock): 72 | break 73 | time.sleep(1) 74 | 75 | print("Finish sync data from {} to {}.".format(from_path, to_path)) 76 | 77 | 78 | def moxing_wrapper(config, pre_process=None, post_process=None): 79 | """ 80 | Moxing wrapper to download dataset and upload outputs. 81 | """ 82 | def wrapper(run_func): 83 | @functools.wraps(run_func) 84 | def wrapped_func(*args, **kwargs): 85 | # Download data from data_url 86 | if config.modelarts: 87 | if config.data_url: 88 | config.data_path = "/cache/train_data_path" 89 | sync_data(config.data_url, config.data_path) 90 | print("Dataset downloaded: ", os.listdir(config.data_path)) 91 | if config.checkpoint_url: 92 | config.checkpoint_path = "/cache/load_checkpoint.ckpt" 93 | sync_data(config.checkpoint_url, config.checkpoint_path) 94 | print("Preload downloaded: ", config.checkpoint_path) 95 | if config.train_url: 96 | config.output_path = "/cache/output_path" 97 | sync_data(config.train_url, config.output_path) 98 | print("Workspace downloaded: ", os.listdir(config.output_path)) 99 | if config.eval_data_url: 100 | config.eval_data_path = "/cache/eval_data_path" 101 | sync_data(config.eval_data_url, config.eval_data_path) 102 | print("Workspace downloaded: ", os.listdir(config.eval_data_path)) 103 | 104 | context.set_context(save_graphs_path=os.path.join(config.output_path, str(get_rank_id()))) 105 | config.device_num = get_device_num() 106 | config.device_id = get_device_id() 107 | if not os.path.exists(config.output_path): 108 | os.makedirs(config.output_path) 109 | 110 | if pre_process: 111 | pre_process() 112 | 113 | show_config(config) 114 | run_func(*args, **kwargs) 115 | 116 | # Upload data to train_url 117 | if config.modelarts: 118 | if post_process: 119 | post_process() 120 | 121 | if config.train_url: 122 | print("Start to copy output directory") 123 | sync_data(config.output_path, config.train_url) 124 | return wrapped_func 125 | return wrapper 126 | -------------------------------------------------------------------------------- /track1/pytorch/code/train.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Thu Aug 25 20:57:34 2022 4 | 5 | @author: DELL 6 | """ 7 | import numpy as np 8 | import torch 9 | import warnings 10 | import time 11 | import os 12 | import random 13 | import timm.scheduler as timm_scheduler 14 | from nets.net_training import focal_loss, reg_l1_loss, ohem_bce_dice_loss 15 | from utils.dataloader import RoadDataset, road_dataset_collate 16 | from torch.utils.data import DataLoader 17 | from nets.hrnet import HRnet 18 | 19 | # 忽略警告信息 20 | warnings.filterwarnings('ignore') 21 | 22 | # 将模型加载到指定设备DEVICE上 23 | DEVICE = 'cuda:0' if torch.cuda.is_available() else 'cpu' 24 | 25 | # 设置随机种子 26 | def seed_it(seed): 27 | os.environ["PYTHONSEED"] = str(seed) 28 | random.seed(seed) 29 | np.random.seed(seed) 30 | torch.cuda.manual_seed(seed) 31 | torch.cuda.manual_seed_all(seed) 32 | torch.backends.cudnn.deterministic = True 33 | torch.backends.cudnn.benchmark = False 34 | torch.manual_seed(seed) 35 | 36 | 37 | def train(model, optimizer, scheduler, train_loader, epoches, batch_size, model_path): 38 | 39 | header = r'Epoch/EpochNum | SegLoss | DetLoss | Time(m)' 40 | raw_line = r'{:5d}/{:8d} | {:9.5f} | {:9.5f} | {:9.2f}' 41 | print(header) 42 | 43 | # # 记录当前验证集最优IoU,以判定是否保存当前模型 44 | # best_iou = 0 45 | # best_iou_epoch = 0 46 | # train_loss_epochs, val_iou_epochs, lr_epochs = [], [], [] 47 | # 开始训练 48 | model.train() 49 | for epoch in range(1, epoches+1): 50 | # 存储训练集每个batch的loss 51 | losses = [] 52 | seg_losses = [] 53 | det_losses = [] 54 | 55 | start_time = time.time() 56 | for batch_index, batch in enumerate(train_loader): 57 | 58 | batch = [ann.to(DEVICE) for ann in batch] 59 | batch_images, batch_label, batch_hms, batch_whs, batch_regs, batch_reg_masks = batch 60 | 61 | # 在反向传播前要手动将梯度清零 62 | optimizer.zero_grad() 63 | # 模型推理得到输出 64 | seg, hm, wh, offset = model(batch_images) 65 | # 求解该batch的loss 66 | seg = seg.to(torch.float32) 67 | batch_label = batch_label.to(torch.float32) 68 | seg_loss = ohem_bce_dice_loss(seg, batch_label) 69 | c_loss = focal_loss(hm, batch_hms) 70 | wh_loss = 0.1 * reg_l1_loss(wh, batch_whs, batch_reg_masks) 71 | off_loss = reg_l1_loss(offset, batch_regs, batch_reg_masks) 72 | det_loss = 0.1*(c_loss + wh_loss + off_loss) 73 | loss = seg_loss + det_loss 74 | losses.append(loss.item()) 75 | seg_losses.append(seg_loss.item()) 76 | det_losses.append(det_loss.item()) 77 | 78 | # 反向传播求解梯度 79 | loss.backward() 80 | # 更新权重参数 81 | optimizer.step() 82 | 83 | scheduler.step(epoch-1) 84 | # 输出进程 85 | print(raw_line.format(epoch, epoches, np.array(seg_losses).mean(), 86 | np.array(det_losses).mean(), 87 | (time.time()-start_time)/60)) 88 | 89 | torch.save(model.state_dict(), model_path.replace(".pth",f"_{epoch}.pth")) 90 | 91 | 92 | if __name__ == '__main__': 93 | 94 | seed_it(1) 95 | epoches = 50 96 | batch_size = 4 97 | input_shape = (1024,1024) 98 | seg_classes = 1 99 | det_classes = 2 100 | num_workers = 8 101 | train_annotation_path = "train_all.txt" 102 | 103 | with open(train_annotation_path) as f: 104 | train_lines = f.readlines() 105 | 106 | train_dataset = RoadDataset(train_lines, input_shape, seg_classes, 107 | det_classes, train = True) 108 | 109 | train_loader = DataLoader(train_dataset, shuffle = True, batch_size = batch_size, 110 | num_workers = num_workers, pin_memory=True, 111 | drop_last=True, collate_fn=road_dataset_collate) 112 | 113 | model = HRnet(seg_classes = 1, det_classes = 2, 114 | backbone = 'hrnetv2_w48', pretrained = True) 115 | 116 | model.to(DEVICE) 117 | model_path = "../model/hr_w48_fcn_center_1024.pth" 118 | 119 | # 采用AdamM优化器 120 | optimizer = torch.optim.AdamW(model.parameters(), 121 | lr=1e-3, weight_decay=1e-3) 122 | # # 余弦退火调整学习率 123 | # scheduler = torch.optim.lr_scheduler.CosineAnnealingWarmRestarts( 124 | # optimizer, 125 | # T_0=10, # T_0就是初始restart的epoch数目 126 | # T_mult=2, # T_mult就是重启之后因子,即每个restart后,T_0 = T_0 * T_mult 127 | # eta_min=1e-5, # 最低学习率 128 | # ) 129 | 130 | scheduler = timm_scheduler.CosineLRScheduler(optimizer, 131 | t_initial=100, 132 | lr_min=1e-5, 133 | warmup_t=5, 134 | warmup_lr_init=1e-5) 135 | 136 | train(model, optimizer, scheduler, train_loader, epoches, batch_size, model_path) -------------------------------------------------------------------------------- /track1/pytorch/code/nets/hrnet.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | import torch 3 | import torch.nn as nn 4 | import torch.nn.functional as F 5 | 6 | from .backbone.hrnet import BN_MOMENTUM, hrnet_classification 7 | 8 | 9 | class HRnet_Backbone(nn.Module): 10 | def __init__(self, backbone = 'hrnetv2_w18', pretrained = False): 11 | super(HRnet_Backbone, self).__init__() 12 | self.model = hrnet_classification(backbone = backbone, pretrained = pretrained) 13 | del self.model.incre_modules 14 | del self.model.downsamp_modules 15 | del self.model.final_layer 16 | del self.model.classifier 17 | 18 | 19 | def forward(self, x): 20 | x = self.model.conv1(x) 21 | x = self.model.bn1(x) 22 | x = self.model.relu(x) 23 | x = self.model.conv2(x) 24 | x = self.model.bn2(x) 25 | x = self.model.relu(x) 26 | x = self.model.layer1(x) 27 | 28 | x_list = [] 29 | for i in range(2): 30 | if self.model.transition1[i] is not None: 31 | x_list.append(self.model.transition1[i](x)) 32 | else: 33 | x_list.append(x) 34 | y_list = self.model.stage2(x_list) 35 | 36 | x_list = [] 37 | for i in range(3): 38 | if self.model.transition2[i] is not None: 39 | if i < 2: 40 | x_list.append(self.model.transition2[i](y_list[i])) 41 | else: 42 | x_list.append(self.model.transition2[i](y_list[-1])) 43 | else: 44 | x_list.append(y_list[i]) 45 | y_list = self.model.stage3(x_list) 46 | 47 | x_list = [] 48 | for i in range(4): 49 | if self.model.transition3[i] is not None: 50 | if i < 3: 51 | x_list.append(self.model.transition3[i](y_list[i])) 52 | else: 53 | x_list.append(self.model.transition3[i](y_list[-1])) 54 | else: 55 | x_list.append(y_list[i]) 56 | y_list = self.model.stage4(x_list) 57 | 58 | return y_list 59 | 60 | class HRnet(nn.Module): 61 | def __init__(self, seg_classes = 1, det_classes = 2, backbone = 'hrnetv2_w18', pretrained = True): 62 | super(HRnet, self).__init__() 63 | self.backbone = HRnet_Backbone(backbone = backbone, pretrained = pretrained) 64 | 65 | in_channels = np.int(np.sum(self.backbone.model.pre_stage_channels)) 66 | inter_channels = in_channels // 4 67 | 68 | dropout_pro = 0.1 69 | 70 | # 分割部分 71 | self.fcn_head = nn.Sequential( 72 | nn.Conv2d(in_channels=in_channels, out_channels=inter_channels, kernel_size=1, stride=1, padding=0), 73 | nn.BatchNorm2d(inter_channels, momentum=BN_MOMENTUM), 74 | nn.ReLU(inplace=True), 75 | nn.Dropout(dropout_pro, False), 76 | nn.Conv2d(in_channels=inter_channels, out_channels=seg_classes, kernel_size=1, stride=1, padding=0) 77 | ) 78 | 79 | # 热力图预测部分 80 | self.cls_head = nn.Sequential( 81 | nn.Conv2d(in_channels, inter_channels, kernel_size=3, padding=1, bias=False), 82 | nn.BatchNorm2d(inter_channels, momentum=BN_MOMENTUM), 83 | nn.ReLU(inplace=True), 84 | nn.Dropout(dropout_pro, False), 85 | nn.Conv2d(inter_channels, det_classes, kernel_size=1, stride=1, padding=0)) 86 | 87 | # 宽高预测的部分 88 | self.wh_head = nn.Sequential( 89 | nn.Conv2d(in_channels, inter_channels, kernel_size=3, padding=1, bias=False), 90 | nn.BatchNorm2d(inter_channels, momentum=BN_MOMENTUM), 91 | nn.ReLU(inplace=True), 92 | nn.Dropout(dropout_pro, False), 93 | nn.Conv2d(inter_channels, 2, kernel_size=1, stride=1, padding=0)) 94 | 95 | 96 | # 中心点预测的部分 97 | self.reg_head = nn.Sequential( 98 | nn.Conv2d(in_channels, inter_channels, kernel_size=3, padding=1, bias=False), 99 | nn.BatchNorm2d(inter_channels, momentum=BN_MOMENTUM), 100 | nn.ReLU(inplace=True), 101 | nn.Dropout(dropout_pro, False), 102 | nn.Conv2d(inter_channels, 2, kernel_size=1, stride=1, padding=0)) 103 | 104 | 105 | 106 | def forward(self, inputs): 107 | H, W = inputs.size(2), inputs.size(3) 108 | x = self.backbone(inputs) 109 | 110 | # Upsampling 111 | x0_h, x0_w = x[0].size(2), x[0].size(3) 112 | 113 | # print(x[3].shape) 114 | 115 | x1 = F.interpolate(x[1], size=(x0_h, x0_w), mode='bilinear', align_corners=True) 116 | x2 = F.interpolate(x[2], size=(x0_h, x0_w), mode='bilinear', align_corners=True) 117 | x3 = F.interpolate(x[3], size=(x0_h, x0_w), mode='bilinear', align_corners=True) 118 | 119 | 120 | x = torch.cat([x[0], x1, x2, x3], 1) 121 | 122 | seg = self.fcn_head(x).sigmoid_() 123 | 124 | seg = F.interpolate(seg, size=(H, W), mode='bilinear', align_corners=True) 125 | 126 | 127 | hm = self.cls_head(x).sigmoid_() 128 | wh = self.wh_head(x) 129 | offset = self.reg_head(x) 130 | 131 | return seg, hm, wh, offset 132 | -------------------------------------------------------------------------------- /track1/pytorch/code/pre/draw_box.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Aug 22 09:16:32 2022 4 | 5 | @author: DELL 6 | """ 7 | """ 8 | Created on Sat May 15 14:17:42 2021 9 | 10 | @author: Ryan 11 | """ 12 | 13 | import os 14 | import json 15 | import numpy as np 16 | import cv2 17 | import glob 18 | import tqdm 19 | 20 | def cv_imread(file_name): 21 | # 用于解决 cv2 无法直接读取路径含有中文的图片 22 | 23 | cv_img = cv2.imdecode(np.fromfile(file_name, dtype=np.uint8), -1) 24 | return cv_img 25 | 26 | 27 | # ----------------------------------------------------------------------------- 28 | # --------------------- COCO 标注的大字典里找那张图片的信息 --------------------- 29 | # ----------------------------------------------------------------------------- 30 | def get_COCO_img_info(img_name, all_coco_ann): 31 | 32 | # 从 COCO 标注的那个大字典里 找 img_name 的名字 33 | # 找到了就返回, 没找到就 return False 34 | 35 | for img_info in all_coco_ann["images"]: 36 | if img_info['file_name'] == img_name: 37 | return img_info 38 | else: 39 | continue 40 | return False 41 | 42 | 43 | # ----------------------------------------------------------------------------- 44 | # --------------------- COCO 标注的大字典里找那张图片的标注 --------------------- 45 | # ----------------------------------------------------------------------------- 46 | def get_COCO_img_anno(img_id, all_coco_ann): 47 | 48 | # 根据图片的 id 找标注的信息 49 | # 找到了就返回那个列表, 没找到就 return [] 50 | 51 | ann_list = [] 52 | for ann_info in all_coco_ann["annotations"]: 53 | if ann_info['image_id'] == img_id: 54 | ann_list.append(ann_info) 55 | else: 56 | continue 57 | return ann_list 58 | 59 | 60 | # ----------------------------------------------------------------------------- 61 | # ------------------------- 获取你想要的的类别的类别id ------------------------ 62 | # ----------------------------------------------------------------------------- 63 | def get_categories_needed(category, all_coco_ann): 64 | 65 | # category 可以使一个类(字符串) 也可以是好几个类(字符串的列表) 66 | if isinstance(category, str): 67 | category = [category] 68 | 69 | cls_id2name = {} 70 | cls_name2id = {} 71 | for cls_info in all_coco_ann["categories"]: 72 | if cls_info['name'] in category: 73 | cls_id2name[cls_info['id']] = cls_info['name'] 74 | cls_name2id[cls_info['name']] = cls_info['id'] 75 | 76 | return cls_id2name, cls_name2id 77 | 78 | 79 | 80 | # ----------------------------------------------------------------------------- 81 | # ---------------------- 根据已选择的类别挑选已获得的标注 ---------------------- 82 | # ----------------------------------------------------------------------------- 83 | def get_ann_needed(ann_list, cls_id2name): 84 | 85 | # 根据标注列表 ann_list 和 需要的类别字典 cls_id2name 86 | 87 | ann_you_want = [] 88 | for ann in ann_list: 89 | if ann['category_id'] in cls_id2name: 90 | ann_you_want.append( (cls_id2name[ann['category_id']], ann['bbox']) ) 91 | return ann_you_want 92 | 93 | 94 | 95 | # ----------------------------------------------------------------------------- 96 | # -------------------------------- 读图绘制bbox ------------------------------- 97 | # ----------------------------------------------------------------------------- 98 | def drawBbox(img_array, ann_needed, color=(0, 255, 0)): 99 | 100 | # 在图片上绘制 bbox 101 | 102 | # 我没想到下面这句话这么重要!! 后面由于是传的引用会直接在原图上操作 103 | img_array = img_array.copy() 104 | 105 | for name, (x_lt, y_lt, w, h) in ann_needed: 106 | img_array = cv2.rectangle(img_array, 107 | (int(x_lt), int(y_lt)), 108 | (int(x_lt+w), int(y_lt+h)), 109 | color, # 这里可以根据类别自己换颜色 110 | 3) 111 | 112 | # import matplotlib.pyplot as plt 113 | # # plt.figure(dpi=1000) 114 | # plt.imshow(img_array[:, :, ::-1]) 115 | # plt.show() 116 | 117 | return img_array 118 | 119 | 120 | json_path = "../../data/chusai_release/train/instances_train.json" 121 | 122 | image_paths = glob.glob("../../data/chusai_release/train/images/*.tif") 123 | save_folder = "../../data/chusai_release/train/image_ann" 124 | if not os.path.exists(save_folder): os.makedirs(save_folder) 125 | 126 | with open(json_path, encoding="utf-8") as f: 127 | all_coco_ann = json.load(f) 128 | 129 | for image_path in tqdm.tqdm(image_paths): 130 | img_name = os.path.basename(image_path) 131 | 132 | img_info = get_COCO_img_info(img_name, all_coco_ann) 133 | img_id = img_info['id'] 134 | 135 | category = ['cross'] 136 | cls_id2name, cls_name2id = get_categories_needed(category, all_coco_ann) 137 | ann_list = get_COCO_img_anno(img_id, all_coco_ann) 138 | ann_needed = get_ann_needed(ann_list, cls_id2name) 139 | img_array = cv_imread(image_path) 140 | img_array = drawBbox(img_array, ann_needed) 141 | 142 | 143 | category = ['uncross'] 144 | cls_id2name, cls_name2id = get_categories_needed(category, all_coco_ann) 145 | ann_list = get_COCO_img_anno(img_id, all_coco_ann) 146 | ann_needed = get_ann_needed(ann_list, cls_id2name) 147 | img_array = drawBbox(img_array, ann_needed, color=(0,0,255)) 148 | 149 | cv2.imwrite(os.path.join(save_folder, img_name),img_array) 150 | # break 151 | -------------------------------------------------------------------------------- /track2/data_agu.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Sep 5 20:11:24 2021 4 | 5 | @author: wangzhenqing 6 | 7 | ref: 8 | https://github.com/liaochengcsu/road_segmentation_pytorch/blob/main/data_agu.py 9 | """ 10 | import cv2 11 | import numpy as np 12 | 13 | # 随机调节色调、饱和度值 14 | def randomHueSaturationValue(image_A, image_B, hue_shift_limit=(-30, 30), 15 | sat_shift_limit=(-5, 5), 16 | val_shift_limit=(-15, 15), ratio=1): 17 | if np.random.random() < ratio: 18 | image_A = cv2.cvtColor(image_A, cv2.COLOR_BGR2HSV) 19 | image_B = cv2.cvtColor(image_B, cv2.COLOR_BGR2HSV) 20 | h_A, s_A, v_A = cv2.split(image_A) 21 | h_B, s_B, v_B = cv2.split(image_B) 22 | # hue_shift = np.random.randint(hue_shift_limit[0], hue_shift_limit[1]+1) 23 | # hue_shift = np.uint8(hue_shift) 24 | # h_A += hue_shift 25 | # h_B += hue_shift 26 | sat_shift = np.random.uniform(sat_shift_limit[0], sat_shift_limit[1]) 27 | s_A = cv2.add(s_A, sat_shift) 28 | s_B = cv2.add(s_B, sat_shift) 29 | val_shift = np.random.uniform(val_shift_limit[0], val_shift_limit[1]) 30 | v_A = cv2.add(v_A, val_shift) 31 | v_B = cv2.add(v_B, val_shift) 32 | image_A = cv2.merge((h_A, s_A, v_A)) 33 | image_B = cv2.merge((h_B, s_B, v_B)) 34 | image_A = cv2.cvtColor(image_A, cv2.COLOR_HSV2BGR) 35 | image_B = cv2.cvtColor(image_B, cv2.COLOR_HSV2BGR) 36 | return image_A, image_B 37 | 38 | # 随机移位旋转 39 | def randomShiftScaleRotate(image_A, image_B, mask, 40 | shift_limit=(-0.1, 0.1), 41 | scale_limit=(-0.1, 0.1), 42 | aspect_limit=(-0.1, 0.1), 43 | rotate_limit=(-0, 0), 44 | borderMode=cv2.BORDER_CONSTANT, ratio=0.5): 45 | if np.random.random() < ratio: 46 | height, width, channel = image_A.shape 47 | 48 | angle = np.random.uniform(rotate_limit[0], rotate_limit[1]) 49 | scale = np.random.uniform(1 + scale_limit[0], 1 + scale_limit[1]) 50 | aspect = np.random.uniform(1 + aspect_limit[0], 1 + aspect_limit[1]) 51 | sx = scale * aspect / (aspect ** 0.5) 52 | sy = scale / (aspect ** 0.5) 53 | dx = round(np.random.uniform(shift_limit[0], shift_limit[1]) * width) 54 | dy = round(np.random.uniform(shift_limit[0], shift_limit[1]) * height) 55 | 56 | cc = np.math.cos(angle / 180 * np.math.pi) * sx 57 | ss = np.math.sin(angle / 180 * np.math.pi) * sy 58 | rotate_matrix = np.array([[cc, -ss], [ss, cc]]) 59 | 60 | box0 = np.array([[0, 0], [width, 0], [width, height], [0, height], ]) 61 | box1 = box0 - np.array([width / 2, height / 2]) 62 | box1 = np.dot(box1, rotate_matrix.T) + np.array([width / 2 + dx, height / 2 + dy]) 63 | 64 | box0 = box0.astype(np.float32) 65 | box1 = box1.astype(np.float32) 66 | mat = cv2.getPerspectiveTransform(box0, box1) 67 | image_A = cv2.warpPerspective(image_A, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode, 68 | borderValue=( 69 | 0, 0, 70 | 0,)) 71 | image_B = cv2.warpPerspective(image_B, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode, 72 | borderValue=( 73 | 0, 0, 74 | 0,)) 75 | mask = cv2.warpPerspective(mask, mat, (width, height), flags=cv2.INTER_LINEAR, borderMode=borderMode, 76 | borderValue=( 77 | 0, 0, 78 | 0,)) 79 | 80 | return image_A, image_B, mask 81 | 82 | 83 | def randomHorizontalFlip(image_A, image_B, mask, ratio=0.5): 84 | if np.random.random() < ratio: 85 | image_A = cv2.flip(image_A, 1) 86 | image_B = cv2.flip(image_B, 1) 87 | mask = cv2.flip(mask, 1) 88 | 89 | return image_A, image_B, mask 90 | 91 | 92 | def randomVerticleFlip(image_A, image_B, mask, ratio=0.5): 93 | if np.random.random() < ratio: 94 | image_A = cv2.flip(image_A, 0) 95 | image_B = cv2.flip(image_B, 0) 96 | mask = cv2.flip(mask, 0) 97 | 98 | return image_A, image_B, mask 99 | 100 | 101 | def randomRotate90(image_A, image_B, mask, ratio=0.5): 102 | if np.random.random() < ratio: 103 | image_A = np.rot90(image_A).copy() 104 | image_B = np.rot90(image_B).copy() 105 | mask = np.rot90(mask).copy() 106 | 107 | return image_A, image_B, mask 108 | 109 | 110 | def data_agu(image_A, image_B, label): 111 | 112 | # image_A = cv2.imread("15_A.tif") 113 | # image_B = cv2.imread("15_B.tif") 114 | # label = cv2.imread("15.png") 115 | 116 | image_A, image_B = randomHueSaturationValue(image_A, image_B) 117 | 118 | image_A, image_B, label = randomShiftScaleRotate(image_A, image_B, label) 119 | 120 | image_A, image_B, label = randomHorizontalFlip(image_A, image_B, label) 121 | image_A, image_B, label = randomVerticleFlip(image_A, image_B, label) 122 | image_A, image_B, label = randomRotate90(image_A, image_B, label) 123 | 124 | # label[label==1] = 255 125 | # image_a_b_label = np.hstack((image_A, image_B, label)) 126 | # cv2.imwrite("15_a_b_1.png", image_a_b_label) 127 | 128 | return image_A, image_B, label -------------------------------------------------------------------------------- /track1/pytorch/code/test_vim.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Sun Aug 28 09:26:04 2022 4 | 5 | @author: DELL 6 | """ 7 | 8 | import os 9 | import json 10 | import numpy as np 11 | import cv2 12 | import glob 13 | import tqdm 14 | 15 | def cv_imread(file_name): 16 | # 用于解决 cv2 无法直接读取路径含有中文的图片 17 | 18 | cv_img = cv2.imdecode(np.fromfile(file_name, dtype=np.uint8), -1) 19 | return cv_img 20 | 21 | 22 | # ----------------------------------------------------------------------------- 23 | # --------------------- COCO 标注的大字典里找那张图片的信息 --------------------- 24 | # ----------------------------------------------------------------------------- 25 | def get_COCO_img_info(img_name, all_coco_ann): 26 | 27 | # 从 COCO 标注的那个大字典里 找 img_name 的名字 28 | # 找到了就返回, 没找到就 return False 29 | 30 | for img_info in all_coco_ann["images"]: 31 | if img_info['file_name'] == img_name: 32 | return img_info 33 | else: 34 | continue 35 | return False 36 | 37 | 38 | # ----------------------------------------------------------------------------- 39 | # --------------------- COCO 标注的大字典里找那张图片的标注 --------------------- 40 | # ----------------------------------------------------------------------------- 41 | def get_COCO_img_anno(img_id, all_coco_ann): 42 | 43 | # 根据图片的 id 找标注的信息 44 | # 找到了就返回那个列表, 没找到就 return [] 45 | 46 | ann_list = [] 47 | for ann_info in all_coco_ann["annotations"]: 48 | if ann_info['image_id'] == img_id: 49 | ann_list.append(ann_info) 50 | else: 51 | continue 52 | return ann_list 53 | 54 | 55 | # ----------------------------------------------------------------------------- 56 | # ------------------------- 获取你想要的的类别的类别id ------------------------ 57 | # ----------------------------------------------------------------------------- 58 | def get_categories_needed(category, all_coco_ann): 59 | 60 | # category 可以使一个类(字符串) 也可以是好几个类(字符串的列表) 61 | if isinstance(category, str): 62 | category = [category] 63 | 64 | cls_id2name = {} 65 | cls_name2id = {} 66 | for cls_info in all_coco_ann["categories"]: 67 | if cls_info['name'] in category: 68 | cls_id2name[cls_info['id']] = cls_info['name'] 69 | cls_name2id[cls_info['name']] = cls_info['id'] 70 | 71 | return cls_id2name, cls_name2id 72 | 73 | 74 | 75 | # ----------------------------------------------------------------------------- 76 | # ---------------------- 根据已选择的类别挑选已获得的标注 ---------------------- 77 | # ----------------------------------------------------------------------------- 78 | def get_ann_needed(ann_list, cls_id2name): 79 | 80 | # 根据标注列表 ann_list 和 需要的类别字典 cls_id2name 81 | 82 | ann_you_want = [] 83 | for ann in ann_list: 84 | if ann['category_id'] in cls_id2name: 85 | ann_you_want.append( (cls_id2name[ann['category_id']], ann['bbox']) ) 86 | return ann_you_want 87 | 88 | 89 | 90 | # ----------------------------------------------------------------------------- 91 | # -------------------------------- 读图绘制bbox ------------------------------- 92 | # ----------------------------------------------------------------------------- 93 | def drawBbox(img_array, ann_needed, color=(0, 255, 0)): 94 | 95 | # 在图片上绘制 bbox 96 | 97 | # 我没想到下面这句话这么重要!! 后面由于是传的引用会直接在原图上操作 98 | img_array = img_array.copy() 99 | 100 | for name, (x_lt, y_lt, w, h) in ann_needed: 101 | img_array = cv2.rectangle(img_array, 102 | (int(x_lt), int(y_lt)), 103 | (int(x_lt+w), int(y_lt+h)), 104 | # (int(y_lt), int(x_lt)), 105 | # (int(y_lt+h), int(x_lt+w)), 106 | color, # 这里可以根据类别自己换颜色 107 | 3) 108 | 109 | return img_array 110 | 111 | with open(r"E:\WangZhenQing\2022HongTu\project_road\data\chusai_release\test\instances_test.json", encoding="utf-8") as f: 112 | test_image_infos = json.loads(f.read()) 113 | with open(r"E:\WangZhenQing\2022HongTu\project_road\data\chusai_release\test\results\test.bbox.json", encoding="utf-8") as f: 114 | test_annos = json.loads(f.read()) 115 | 116 | 117 | test_image_infos["annotations"] = test_annos 118 | 119 | 120 | 121 | image_paths = glob.glob(r"E:\WangZhenQing\2022HongTu\project_road\data\chusai_release\test\images/*.tif") 122 | save_folder = r"E:\WangZhenQing\2022HongTu\project_road\data\chusai_release\test\images_ann__" 123 | if not os.path.exists(save_folder): os.makedirs(save_folder) 124 | 125 | 126 | for image_path in tqdm.tqdm(image_paths): 127 | img_name = os.path.basename(image_path) 128 | 129 | img_info = get_COCO_img_info(img_name, test_image_infos) 130 | img_id = img_info['id'] 131 | 132 | category = ['cross'] 133 | cls_id2name, cls_name2id = get_categories_needed(category, test_image_infos) 134 | ann_list = get_COCO_img_anno(img_id, test_image_infos) 135 | ann_needed = get_ann_needed(ann_list, cls_id2name) 136 | img_array = cv_imread(image_path) 137 | img_array = drawBbox(img_array, ann_needed) 138 | 139 | 140 | category = ['uncross'] 141 | cls_id2name, cls_name2id = get_categories_needed(category, test_image_infos) 142 | ann_list = get_COCO_img_anno(img_id, test_image_infos) 143 | ann_needed = get_ann_needed(ann_list, cls_id2name) 144 | img_array = drawBbox(img_array, ann_needed, color=(0,0,255)) 145 | 146 | cv2.imwrite(os.path.join(save_folder, img_name),img_array) 147 | # break 148 | -------------------------------------------------------------------------------- /track2/copy_paste.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Wed Aug 11 21:11:54 2021 4 | 5 | @author: DELL 6 | """ 7 | 8 | 9 | import cv2 10 | import numpy as np 11 | from style_transfer import style_transfer 12 | import os 13 | # import random 14 | import string 15 | 16 | 17 | def avoid_overwrite(copy_mask, target_mask, kernel): 18 | 19 | copy_contours, _ = cv2.findContours(copy_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 20 | target_contours, _ = cv2.findContours(target_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) 21 | 22 | overwrite_contours = [] 23 | # print(len(copy_contours)) 24 | for copy_contour in copy_contours: 25 | 26 | copy_contour_mask = np.zeros(copy_mask.shape, np.uint8) 27 | cv2.fillPoly(copy_contour_mask, [copy_contour], (255)) 28 | 29 | copy_contour_mask = cv2.dilate(copy_contour_mask, kernel, iterations = 1) 30 | 31 | copy_index = np.where(copy_contour_mask==255) 32 | copy_index_XmergeY = copy_index[0]*1.0+copy_index[1]*0.001 33 | 34 | for target_contour in target_contours: 35 | 36 | target_contour_mask = np.zeros(copy_mask.shape, np.uint8) 37 | cv2.fillPoly(target_contour_mask, [target_contour], (255)) 38 | target_contour_mask = cv2.dilate(target_contour_mask, kernel, iterations = 1) 39 | 40 | target_index = np.where(target_contour_mask==255) 41 | target_index_XmergeY = target_index[0]*1.0+target_index[1]*0.001 42 | 43 | # 若contour1和contour2相交 44 | if True in np.isin(copy_index_XmergeY,target_index_XmergeY): 45 | overwrite_contours.append(copy_contour) 46 | # print(1) 47 | break 48 | 49 | cv2.fillPoly(copy_mask, overwrite_contours, (0)) 50 | # cv2.imwrite("22222.png",copy_mask) 51 | return copy_mask 52 | 53 | def img_add(img_src, img_main, mask_src, isdilate=False): 54 | if len(img_main.shape) == 3: 55 | h, w, c = img_main.shape 56 | elif len(img_main.shape) == 2: 57 | h, w = img_main.shape 58 | 59 | sub_img_src = cv2.add(img_src, np.zeros(np.shape(img_src), dtype=np.uint8), mask=mask_src) 60 | sub_img_src_wh = cv2.resize(sub_img_src, (w, h),interpolation=cv2.INTER_NEAREST) 61 | mask_src_wh = cv2.resize(mask_src, (w, h), interpolation=cv2.INTER_NEAREST) 62 | sub_img_main = cv2.add(img_main, np.zeros(np.shape(img_main), dtype=np.uint8), mask=mask_src_wh) 63 | img_main = img_main - sub_img_main + sub_img_src_wh 64 | return img_main 65 | 66 | 67 | def copy_paste_self(img, mask, random): 68 | 69 | 70 | if random<0.25 : 71 | # 水平翻转 72 | img_filp = cv2.flip(img, 1) 73 | mask_filp = cv2.flip(mask, 1) 74 | elif (random>=0.25) and (random<0.5) : 75 | # 垂直翻转 76 | img_filp = cv2.flip(img, 0) 77 | mask_filp = cv2.flip(mask, 0) 78 | elif (random>=0.5) and (random<0.75) : 79 | # 对角翻转 80 | img_filp = cv2.flip(img, -1) 81 | mask_filp = cv2.flip(mask, -1) 82 | else: 83 | img_filp=np.rot90(img).copy() 84 | mask_filp=np.rot90(mask).copy() 85 | 86 | # print(mask.shape, mask_filp.shape) 87 | # 膨胀核 88 | kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10)) 89 | # 若和目标有重叠,则不进行粘贴 90 | mask_filp = avoid_overwrite(mask_filp, mask, kernel) 91 | # cv2.imwrite("11111.png",mask_filp) 92 | mask_filp_dilate = cv2.dilate(mask_filp, kernel, iterations = 1) 93 | 94 | img = img_add(img_filp, img, mask_filp_dilate, isdilate=True) 95 | mask = img_add(mask_filp, mask, mask_filp, isdilate=False) 96 | 97 | return mask, img 98 | 99 | 100 | def copy_paste(img_copy, mask_copy, img_target, mask_target, random): 101 | 102 | img_copy = style_transfer(img_copy, img_target) 103 | 104 | if random<0.25 : 105 | # 水平翻转 106 | img_copy = cv2.flip(img_copy, 1) 107 | mask_copy = cv2.flip(mask_copy, 1) 108 | elif (random>=0.25) and (random<0.5) : 109 | # 垂直翻转 110 | img_copy = cv2.flip(img_copy, 0) 111 | mask_copy = cv2.flip(mask_copy, 0) 112 | elif (random>=0.5) and (random<0.75) : 113 | # 对角翻转 114 | img_copy = cv2.flip(img_copy, -1) 115 | mask_copy = cv2.flip(mask_copy, -1) 116 | else: 117 | img_copy=np.rot90(img_copy).copy() 118 | mask_copy=np.rot90(mask_copy).copy() 119 | 120 | # print(mask.shape, mask_filp.shape) 121 | # 膨胀核 122 | kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (10,10)) 123 | # 若和目标有重叠,则不进行粘贴 124 | mask_copy = avoid_overwrite(mask_copy, mask_target, kernel) 125 | # cv2.imwrite("11111.png",mask_filp) 126 | mask_copy_dilate = cv2.dilate(mask_copy, kernel, iterations = 1) 127 | 128 | img = img_add(img_copy, img_target, mask_copy_dilate, isdilate=True) 129 | mask = img_add(mask_copy, mask_target, mask_copy, isdilate=False) 130 | 131 | return mask, img 132 | 133 | # # 复制粘贴的图像和标签 134 | # mask_src = cv2.imread(r"41_1_label.png",0) 135 | # img_src = cv2.imread(r"41_1.png") 136 | 137 | # # 粘贴背景图像和标签 138 | # mask_main = cv2.imread(r"23_2_label.png",0) 139 | # img_main = cv2.imread(r"23_2.png") 140 | 141 | # random = np.random.random() 142 | # # 复制粘贴数据增强 143 | # mask, img = copy_paste(img_src, mask_src, img_main, mask_main, random) 144 | 145 | # cv2.imwrite("copy_paste_label.png", mask) 146 | # cv2.imwrite("copy_paste.png",img) 147 | 148 | 149 | 150 | 151 | # mask = cv2.imread(r"41_1_label.png",0) 152 | # # mask[mask==255]=1 153 | # img = cv2.imread(r"41_1.png") 154 | 155 | # mask, img = copy_paste_self(img, mask) 156 | # cv2.imwrite("41_1_label_.png", mask) 157 | # cv2.imwrite("41_1_.png",img) -------------------------------------------------------------------------------- /track2/model/hr_seg.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | import numpy as np 4 | import torch.nn.functional as F 5 | 6 | from .modules.backbones.hrnet import hrnetv2_18, hrnetv2_32, hrnetv2_48 7 | 8 | 9 | class HRNet(nn.Module): 10 | def __init__(self, in_channels, n_classes, backbone, model_path, dropout_rate=0.0): 11 | super().__init__() 12 | 13 | if backbone == 'hrnetv2_18': 14 | 15 | self.base_model = hrnetv2_18(pretrained=False) 16 | 17 | elif backbone == 'hrnetv2_32': 18 | 19 | self.base_model = hrnetv2_32(pretrained=False) 20 | 21 | elif backbone == 'hrnetv2_48': 22 | 23 | self.base_model = hrnetv2_48(pretrained=False) 24 | 25 | if model_path: 26 | self.base_model.load_param(model_path) 27 | 28 | # input_dim >3 29 | if in_channels > 3: 30 | with torch.no_grad(): 31 | pretrained_conv1 = self.base_model.conv1.weight.clone() 32 | self.base_model.conv1 = torch.nn.Conv2d(in_channels, 64, 3, 2, 1, bias=False) 33 | torch.nn.init.kaiming_normal_( 34 | self.base_model.conv1.weight, mode='fan_out', nonlinearity='relu') 35 | # Re-assign pretraiend weights to first 3 channels 36 | # (assuming alpha channel is last in your input data) 37 | self.base_model.conv1.weight[:, :3] = pretrained_conv1 38 | 39 | last_inp_channels = np.int(np.sum(self.base_model.final_stage_channels)) 40 | 41 | self.last_layer = nn.Sequential( 42 | nn.Conv2d( 43 | in_channels=last_inp_channels, 44 | out_channels=last_inp_channels, 45 | kernel_size=1, 46 | stride=1, 47 | padding=0), 48 | nn.BatchNorm2d(last_inp_channels, momentum=0.1), 49 | nn.ReLU(inplace=True), 50 | nn.Conv2d( 51 | in_channels=last_inp_channels, 52 | out_channels=n_classes, 53 | kernel_size=1, 54 | stride=1, 55 | padding=0) 56 | ) 57 | self.use_dropout = False 58 | if dropout_rate > 0: 59 | self.use_dropout = True 60 | self.dropout = nn.Dropout2d(dropout_rate) 61 | 62 | def freeze_bn(self): 63 | print("freeze bacth normalization successfully!") 64 | for m in self.modules(): 65 | if isinstance(m, nn.BatchNorm2d) or isinstance(m, nn.InstanceNorm2d): 66 | m.eval() 67 | 68 | def forward(self, input): 69 | x = self.base_model.conv1(input) 70 | x = self.base_model.bn1(x) 71 | x = self.base_model.relu(x) 72 | x = self.base_model.conv2(x) 73 | x = self.base_model.bn2(x) 74 | x = self.base_model.relu(x) 75 | x = self.base_model.layer1(x) 76 | 77 | x_list = [] 78 | for i in range(self.base_model.stage2_cfg['NUM_BRANCHES']): 79 | if self.base_model.transition1[i] is not None: 80 | x_list.append(self.base_model.transition1[i](x)) 81 | else: 82 | x_list.append(x) 83 | y_list = self.base_model.stage2(x_list) 84 | 85 | x_list = [] 86 | for i in range(self.base_model.stage3_cfg['NUM_BRANCHES']): 87 | if self.base_model.transition2[i] is not None: 88 | if i < self.base_model.stage2_cfg['NUM_BRANCHES']: 89 | x_list.append(self.base_model.transition2[i](y_list[i])) 90 | else: 91 | x_list.append(self.base_model.transition2[i](y_list[-1])) 92 | else: 93 | x_list.append(y_list[i]) 94 | y_list = self.base_model.stage3(x_list) 95 | 96 | x_list = [] 97 | for i in range(self.base_model.stage4_cfg['NUM_BRANCHES']): 98 | if self.base_model.transition3[i] is not None: 99 | if i < self.base_model.stage3_cfg['NUM_BRANCHES']: 100 | x_list.append(self.base_model.transition3[i](y_list[i])) 101 | else: 102 | x_list.append(self.base_model.transition3[i](y_list[-1])) 103 | else: 104 | x_list.append(y_list[i]) 105 | x = self.base_model.stage4(x_list) 106 | 107 | # Upsampling 108 | x0_h, x0_w = x[0].size(2), x[0].size(3) 109 | x1 = F.interpolate(x[1], size=(x0_h, x0_w), mode='bilinear', align_corners=False) 110 | x2 = F.interpolate(x[2], size=(x0_h, x0_w), mode='bilinear', align_corners=False) 111 | x3 = F.interpolate(x[3], size=(x0_h, x0_w), mode='bilinear', align_corners=False) 112 | 113 | x = torch.cat([x[0], x1, x2, x3], 1) 114 | 115 | if self.use_dropout: 116 | x = self.dropout(x) 117 | 118 | x = self.last_layer(x) 119 | x = F.interpolate(input=x, scale_factor=4, mode='bilinear', align_corners=False) 120 | 121 | return x 122 | 123 | def load_param(self, model_path): 124 | param_dict = torch.load(model_path, map_location=lambda storage, loc: storage) 125 | if 'state_dict' in param_dict.keys(): 126 | param_dict = param_dict['state_dict'] 127 | 128 | start_with_module = False 129 | for k in param_dict.keys(): 130 | if k.startswith('module.'): 131 | start_with_module = True 132 | break 133 | if start_with_module: 134 | param_dict = {k[7:]: v for k, v in param_dict.items()} 135 | print('ignore_param:') 136 | print( 137 | [k for k, v in param_dict.items() if k not in self.state_dict() or self.state_dict()[k].size() != v.size()]) 138 | print('unload_param:') 139 | print( 140 | [k for k, v in self.state_dict().items() if k not in param_dict.keys() or param_dict[k].size() != v.size()]) 141 | 142 | param_dict = {k: v for k, v in param_dict.items() if 143 | k in self.state_dict() and self.state_dict()[k].size() == v.size()} 144 | for i in param_dict: 145 | self.state_dict()[i].copy_(param_dict[i]) -------------------------------------------------------------------------------- /track1/pytorch/code/pre/data_clip.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | """ 3 | Created on Mon Aug 22 20:51:41 2022 4 | 5 | @author: DELL 6 | """ 7 | import cv2 8 | import os 9 | import glob 10 | import shutil 11 | import tqdm 12 | import numpy as np 13 | 14 | # 不删除全背景,裁剪10637张 15 | # 删除全背景,裁剪张 16 | 17 | # # 图像宽不足裁剪宽度,填充至裁剪宽度 18 | # def fill_right(img, size_w): 19 | # size = img.shape 20 | # img_fill_right = cv2.copyMakeBorder(img, 0, 0, 0, size_w - size[1], 21 | # cv2.BORDER_CONSTANT, value = (0, 0, 0)) 22 | # return img_fill_right 23 | 24 | # # 图像高不足裁剪高度,填充至裁剪高度 25 | # def fill_bottom(img, size_h): 26 | # size = img.shape 27 | # img_fill_bottom = cv2.copyMakeBorder(img, 0, size_h - size[0], 0, 0, 28 | # cv2.BORDER_CONSTANT, value = (0, 0, 0)) 29 | # return img_fill_bottom 30 | 31 | # 图像宽高不足裁剪宽高度,填充至裁剪宽高度 32 | def fill_right_bottom(img, size_w, size_h, value=(0,0,0)): 33 | size = img.shape 34 | img_fill_right_bottom = cv2.copyMakeBorder(img, 0, max(0,size_h - size[0]), 0, max(0,size_w - size[1]), 35 | cv2.BORDER_CONSTANT, value = value) 36 | return img_fill_right_bottom 37 | 38 | 39 | def data_crop(img_paths, label_folder, anno_folder, out_img_floder, 40 | out_label_floder, out_anno_floder, 41 | size_w = 1024, size_h = 1024, step = 900): 42 | 43 | count = 0 44 | for img_path in tqdm.tqdm(img_paths): 45 | 46 | number = 0 47 | 48 | img_name = os.path.basename(img_path)[:-4] 49 | label_path = os.path.join(label_folder,img_name+".png") 50 | anno_path = os.path.join(anno_folder,img_name+".txt") 51 | img = cv2.imread(img_path) 52 | label = cv2.imread(label_path,0) 53 | anno_file = open(anno_path, 'r') 54 | anno_lines = anno_file.readlines() 55 | 56 | size = img.shape 57 | 58 | # 59 | if size[0] < size_h or size[1] < size_w: 60 | print(f'图片{img_name}需要补齐') 61 | img = fill_right_bottom(img, size_w, size_h) 62 | label = fill_right_bottom(label, size_w, size_h, value=(0)) 63 | 64 | size = img.shape 65 | 66 | count = count + 1 67 | for h in range(0, size[0] - 1, step): 68 | start_h = h 69 | for w in range(0, size[1] - 1, step): 70 | 71 | start_w = w 72 | end_h = start_h + size_h 73 | if end_h > size[0]: 74 | start_h = size[0] - size_h 75 | end_h = start_h + size_h 76 | end_w = start_w + size_w 77 | if end_w > size[1]: 78 | start_w = size[1] - size_w 79 | end_w = start_w + size_w 80 | 81 | img_cropped = img[start_h : end_h, start_w : end_w] 82 | label_cropped = label[start_h : end_h, start_w : end_w] 83 | 84 | # 含有正例才保存 85 | if np.max(label_cropped)==255: 86 | # 用起始坐标来命名切割得到的图像,为的是方便后续标签数据抓取 87 | img_name_cropped = img_name + '_'+ str(start_h) +'_' + str(start_w) 88 | 89 | cv2.imwrite(os.path.join(out_img_floder,img_name_cropped+".tif"), img_cropped) 90 | cv2.imwrite(os.path.join(out_label_floder,img_name_cropped+".png"), label_cropped) 91 | 92 | 93 | anno_cropped_file = open(os.path.join(out_anno_floder,img_name_cropped+".txt"), 'a') 94 | 95 | 96 | # 逐行读取 97 | for anno_line in anno_lines: 98 | anno_line_split = anno_line.split(' ') 99 | min_x,min_y,max_x,max_y,category = anno_line_split 100 | 101 | # 裁剪后的box对应原图的坐标 102 | cropped_min_x = max(start_w,int(min_x)) 103 | cropped_max_x = min(end_w,int(max_x)) 104 | cropped_min_y = max(start_h,int(min_y)) 105 | cropped_max_y = min(end_h,int(max_y)) 106 | 107 | area_cropped = (cropped_max_y-cropped_min_y)*(cropped_max_x-cropped_min_x) 108 | area_box = (int(max_y)-int(min_y))*(int(max_x)-int(min_x)) 109 | 110 | if cropped_max_y-cropped_min_y>0 and area_cropped/area_box>=0.5: 111 | # print(area_cropped, area_box, area_cropped/area_box) 112 | min_x_cropped = cropped_min_x-start_w 113 | min_y_cropped = cropped_min_y-start_h 114 | max_x_cropped = cropped_max_x-start_w 115 | max_y_cropped = cropped_max_y-start_h 116 | anno_cropped_file.write(f"{min_x_cropped} {min_y_cropped} {max_x_cropped} {max_y_cropped} {category}") 117 | 118 | number = number + 1 119 | 120 | anno_file.close() 121 | anno_cropped_file.close() 122 | 123 | 124 | 125 | print('{}.png切割成{}张.'.format(img_name,number)) 126 | print('共完成{}张图片'.format(count)) 127 | 128 | 129 | # 图像、标签和注解 130 | img_floder = '../../data/chusai_release/train/images' 131 | img_paths = glob.glob(f"{img_floder}/*.tif") 132 | # img_paths = [r"E:\WangZhenQing\2022HongTu\project_road\data\chusai_release\train\images\122.tif"] 133 | label_folder = '../../data/chusai_release/train/masks' 134 | anno_folder = '../../data/chusai_release/train/annotations' 135 | 136 | # 裁剪的图像、标签和注解 137 | out_img_floder = '../../data/chusai_release/train/images_crop' 138 | out_label_floder = '../../data/chusai_release/train/masks_crop' 139 | out_anno_floder = '../../data/chusai_release/train/annotations_crop' 140 | 141 | if os.path.exists(out_img_floder): 142 | shutil.rmtree(out_img_floder) 143 | os.makedirs(out_img_floder) 144 | if os.path.exists(out_label_floder): 145 | shutil.rmtree(out_label_floder) 146 | os.makedirs(out_label_floder) 147 | if os.path.exists(out_anno_floder): 148 | shutil.rmtree(out_anno_floder) 149 | os.makedirs(out_anno_floder) 150 | 151 | # 切割图像宽 152 | size_w = 1024 153 | # 切割图像高 154 | size_h = size_w 155 | # 切割步长,重叠度为size_w - step 156 | step = int(size_h*0.9) 157 | 158 | data_crop(img_paths, label_folder, anno_folder, out_img_floder, 159 | out_label_floder, out_anno_floder, 160 | size_w = 1024, size_h = 1024, step = 900) -------------------------------------------------------------------------------- /track2/model/efficient_unet.py: -------------------------------------------------------------------------------- 1 | from collections import OrderedDict 2 | from .modules.utils import * 3 | from .modules.backbones.efficientnet import EfficientNet 4 | 5 | from .modules.scse import cSE, sSE 6 | 7 | 8 | __all__ = ['EfficientUnet', 'get_efficientunet_b0', 'get_efficientunet_b1', 'get_efficientunet_b2', 9 | 'get_efficientunet_b3', 'get_efficientunet_b4', 'get_efficientunet_b5', 'get_efficientunet_b6', 10 | 'get_efficientunet_b7'] 11 | 12 | 13 | def get_blocks_to_be_concat(model, x): 14 | shapes = set() 15 | blocks = OrderedDict() 16 | hooks = [] 17 | count = 0 18 | 19 | def register_hook(module): 20 | 21 | def hook(module, input, output): 22 | try: 23 | nonlocal count 24 | if module.name == f'blocks_{count}_output_batch_norm': 25 | count += 1 26 | shape = output.size()[-2:] 27 | if shape not in shapes: 28 | shapes.add(shape) 29 | blocks[module.name] = output 30 | 31 | elif module.name == 'head_swish': 32 | # when module.name == 'head_swish', it means the program has already got all necessary blocks for 33 | # concatenation. In my dynamic unet implementation, I first upscale the output of the backbone, 34 | # (in this case it's the output of 'head_swish') concatenate it with a block which has the same 35 | # Height & Width (image size). Therefore, after upscaling, the output of 'head_swish' has bigger 36 | # image size. The last block has the same image size as 'head_swish' before upscaling. So we don't 37 | # really need the last block for concatenation. That's why I wrote `blocks.popitem()`. 38 | blocks.popitem() 39 | blocks[module.name] = output 40 | 41 | except AttributeError: 42 | pass 43 | 44 | if ( 45 | not isinstance(module, nn.Sequential) 46 | and not isinstance(module, nn.ModuleList) 47 | and not (module == model) 48 | ): 49 | hooks.append(module.register_forward_hook(hook)) 50 | 51 | # register hook 52 | model.apply(register_hook) 53 | 54 | # make a forward pass to trigger the hooks 55 | model(x) 56 | 57 | # remove these hooks 58 | for h in hooks: 59 | h.remove() 60 | 61 | return blocks 62 | 63 | 64 | class EfficientUnet(nn.Module): 65 | def __init__(self, in_channels, n_classes, model_name, model_path, dropout_rate=0.5, concat_input=True, use_attention_block=False, use_scse=False): 66 | super().__init__() 67 | 68 | # self.encoder = encoder 69 | self.encoder = EfficientNet.encoder(model_name, model_path=model_path, in_channels=in_channels, drop_connect_rate=dropout_rate) 70 | self.concat_input = concat_input 71 | 72 | self.up_conv1 = up_conv(self.n_channels, 512) 73 | self.double_conv1 = conv_block(self.size[0], 512) 74 | self.up_conv2 = up_conv(512, 256) 75 | self.double_conv2 = conv_block(self.size[1], 256) 76 | self.up_conv3 = up_conv(256, 128) 77 | self.double_conv3 = conv_block(self.size[2], 128) 78 | self.up_conv4 = up_conv(128, 64) 79 | self.double_conv4 = conv_block(self.size[3], 64) 80 | 81 | self.use_attention_block = use_attention_block 82 | 83 | 84 | self.use_scse = use_scse 85 | if self.use_scse: 86 | self.spatial_gate1 = sSE(512) 87 | self.channel_gate1 = cSE(512) 88 | 89 | self.spatial_gate2 = sSE(256) 90 | self.channel_gate2 = cSE(256) 91 | 92 | 93 | self.spatial_gate3 = sSE(128) 94 | self.channel_gate3 = cSE(128) 95 | 96 | self.spatial_gate4 = sSE(64) 97 | self.channel_gate4 = cSE(64) 98 | 99 | if self.concat_input: 100 | self.up_conv_input = up_conv(64, 32) 101 | self.double_conv_input = conv_block(self.size[4], 32) 102 | 103 | self.final_conv = nn.Conv2d(self.size[5], n_classes, kernel_size=1) 104 | 105 | @property 106 | def n_channels(self): 107 | n_channels_dict = {'efficientnet-b0': 1280, 'efficientnet-b1': 1280, 'efficientnet-b2': 1408, 108 | 'efficientnet-b3': 1536, 'efficientnet-b4': 1792, 'efficientnet-b5': 2048, 109 | 'efficientnet-b6': 2304, 'efficientnet-b7': 2560} 110 | return n_channels_dict[self.encoder.name] 111 | 112 | @property 113 | def size(self): 114 | size_dict = {'efficientnet-b0': [592, 296, 152, 80, 35, 32], 'efficientnet-b1': [592, 296, 152, 80, 35, 32], 115 | 'efficientnet-b2': [600, 304, 152, 80, 35, 32], 'efficientnet-b3': [608, 304, 160, 88, 35, 32], 116 | 'efficientnet-b4': [624, 312, 160, 88, 35, 32], 'efficientnet-b5': [640, 320, 168, 88, 35, 32], 117 | 'efficientnet-b6': [656, 328, 168, 96, 35, 32], 'efficientnet-b7': [672, 336, 176, 96, 35, 32]} 118 | return size_dict[self.encoder.name] 119 | 120 | def forward(self, x): 121 | input_ = x 122 | 123 | blocks = get_blocks_to_be_concat(self.encoder, x) 124 | _, x = blocks.popitem() 125 | 126 | x = self.up_conv1(x) 127 | x4 = blocks.popitem()[1] 128 | # print(x4.shape) 129 | 130 | x = torch.cat([x, x4], dim=1) 131 | x = self.double_conv1(x) 132 | if self.use_scse: 133 | x = self.spatial_gate1(x)*x+self.channel_gate1(x)*x 134 | 135 | x = self.up_conv2(x) 136 | x3 = blocks.popitem()[1] 137 | # print(x3.shape) 138 | 139 | x = torch.cat([x, x3], dim=1) 140 | x = self.double_conv2(x) 141 | if self.use_scse: 142 | x = self.spatial_gate2(x)*x+self.channel_gate2(x)*x 143 | 144 | x = self.up_conv3(x) 145 | x2 = blocks.popitem()[1] 146 | # print(x2.shape) 147 | 148 | x = torch.cat([x, x2], dim=1) 149 | x = self.double_conv3(x) 150 | if self.use_scse: 151 | x = self.spatial_gate3(x)*x+self.channel_gate3(x)*x 152 | 153 | x = self.up_conv4(x) 154 | x1 = blocks.popitem()[1] 155 | # print(x1.shape) 156 | 157 | x = torch.cat([x, x1], dim=1) 158 | x = self.double_conv4(x) 159 | if self.use_scse: 160 | x = self.spatial_gate4(x)*x+self.channel_gate4(x)*x 161 | 162 | if self.concat_input: 163 | x = self.up_conv_input(x) 164 | x = torch.cat([x, input_], dim=1) 165 | x = self.double_conv_input(x) 166 | 167 | x = self.final_conv(x) 168 | 169 | return x -------------------------------------------------------------------------------- /track1/mindspore/code/src/decode.py: -------------------------------------------------------------------------------- 1 | # Copyright 2021 Huawei Technologies Co., Ltd 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | # ============================================================================ 15 | """ 16 | Decode from heads for evaluation 17 | """ 18 | 19 | import mindspore as ms 20 | import mindspore.nn as nn 21 | import mindspore.ops as ops 22 | from mindspore.ops import operations as P 23 | from mindspore.common import dtype as mstype 24 | from .utils import GatherFeature, TransposeGatherFeature 25 | 26 | 27 | class NMS(nn.Cell): 28 | """ 29 | Non-maximum suppression 30 | 31 | Args: 32 | kernel(int): Maxpooling kernel size. Default: 3. 33 | enable_nms_fp16(bool): Use float16 data for max_pool, adaption for CPU. Default: False. 34 | 35 | Returns: 36 | Tensor, heatmap after non-maximum suppression. 37 | """ 38 | def __init__(self, kernel=3, enable_nms_fp16=False): 39 | super(NMS, self).__init__() 40 | self.cast = ops.Cast() 41 | self.dtype = ops.DType() 42 | self.equal = ops.Equal() 43 | self.Abs = P.Abs() 44 | self.max_pool_ = nn.MaxPool2d(kernel, stride=1, pad_mode="same") 45 | self.max_pool = P.MaxPoolWithArgmax(kernel_size=kernel, strides=1, pad_mode='same') 46 | self.enable_fp16 = enable_nms_fp16 47 | 48 | def construct(self, heat): 49 | """Non-maximum suppression""" 50 | dtype = self.dtype(heat) 51 | if self.enable_fp16: 52 | heat = self.cast(heat, mstype.float16) 53 | heat_max = self.max_pool_(heat) 54 | keep = self.equal(heat, heat_max) 55 | keep = self.cast(keep, dtype) 56 | heat = self.cast(heat, dtype) 57 | else: 58 | heat_max, _ = self.max_pool(heat) 59 | error = self.cast((heat - heat_max), mstype.float32) 60 | abs_error = self.Abs(error) 61 | abs_out = self.Abs(heat) 62 | error = abs_error / (abs_out + 1e-12) 63 | keep = P.Select()(P.LessEqual()(error, 1e-3), 64 | P.Fill()(ms.float32, P.Shape()(error), 1.0), 65 | P.Fill()(ms.float32, P.Shape()(error), 0.0)) 66 | heat = heat * keep 67 | return heat 68 | 69 | 70 | class GatherTopK(nn.Cell): 71 | """ 72 | Gather topk features through all channels 73 | 74 | Args: None 75 | 76 | Returns: 77 | Tuple of Tensors, top_k scores, indexes, category ids, and the indexes in height and width direcction. 78 | """ 79 | def __init__(self): 80 | super(GatherTopK, self).__init__() 81 | self.shape = ops.Shape() 82 | self.reshape = ops.Reshape() 83 | self.topk = ops.TopK(sorted=True) 84 | self.cast = ops.Cast() 85 | self.dtype = ops.DType() 86 | self.gather_feat = GatherFeature() 87 | # The ops.Mod() operator will produce errors on the Ascend 310 88 | self.mod = P.FloorMod() 89 | self.div = ops.Div() 90 | 91 | def construct(self, scores, K=40): 92 | """gather top_k""" 93 | b, c, _, w = self.shape(scores) 94 | scores = self.reshape(scores, (b, c, -1)) 95 | # (b, c, K) 96 | topk_scores, topk_inds = self.topk(scores, K) 97 | topk_ys = self.div(topk_inds, w) 98 | topk_xs = self.mod(topk_inds, w) 99 | # (b, K) 100 | topk_score, topk_ind = self.topk(self.reshape(topk_scores, (b, -1)), K) 101 | topk_clses = self.cast(self.div(topk_ind, K), self.dtype(scores)) 102 | topk_inds = self.gather_feat(self.reshape(topk_inds, (b, -1, 1)), topk_ind) 103 | topk_inds = self.reshape(topk_inds, (b, K)) 104 | topk_ys = self.gather_feat(self.reshape(topk_ys, (b, -1, 1)), topk_ind) 105 | topk_ys = self.cast(self.reshape(topk_ys, (b, K)), self.dtype(scores)) 106 | topk_xs = self.gather_feat(self.reshape(topk_xs, (b, -1, 1)), topk_ind) 107 | topk_xs = self.cast(self.reshape(topk_xs, (b, K)), self.dtype(scores)) 108 | return topk_score, topk_inds, topk_clses, topk_ys, topk_xs 109 | 110 | 111 | class DetectionDecode(nn.Cell): 112 | """ 113 | Decode from heads to gather multi-objects info. 114 | 115 | Args: 116 | K(int): maximum objects number. Default: 100. 117 | enable_nms_fp16(bool): Use float16 data for max_pool, adaption for CPU. Default: True. 118 | 119 | Returns: 120 | Tensor, multi-objects detections. 121 | """ 122 | def __init__(self, K=100, enable_nms_fp16=False): 123 | super(DetectionDecode, self).__init__() 124 | self.K = K 125 | self.nms = NMS(enable_nms_fp16=enable_nms_fp16) 126 | self.shape = ops.Shape() 127 | self.gather_topk = GatherTopK() 128 | self.half = ops.Split(axis=-1, output_num=2) 129 | self.add = ops.TensorAdd() 130 | self.concat_a2 = ops.Concat(axis=2) 131 | self.trans_gather_feature = TransposeGatherFeature() 132 | self.expand_dims = ops.ExpandDims() 133 | self.reshape = ops.Reshape() 134 | self.reg_offset = True 135 | # self.Sigmoid = nn.Sigmoid() 136 | 137 | def construct(self, heat, wh, reg): 138 | """gather detections""" 139 | # heat = self.Sigmoid(heat) 140 | K = self.K 141 | b, _, _, _ = self.shape(heat) 142 | heat = self.nms(heat) 143 | scores, inds, clses, ys, xs = self.gather_topk(heat, K=K) 144 | ys = self.reshape(ys, (b, K, 1)) 145 | xs = self.reshape(xs, (b, K, 1)) 146 | 147 | wh = self.trans_gather_feature(wh, inds) 148 | ws, hs = self.half(wh) 149 | 150 | if self.reg_offset: 151 | reg = self.trans_gather_feature(reg, inds) 152 | reg = self.reshape(reg, (b, K, 2)) 153 | reg_w, reg_h = self.half(reg) 154 | ys = self.add(ys, reg_h) 155 | xs = self.add(xs, reg_w) 156 | else: 157 | ys = ys + 0.5 158 | xs = xs + 0.5 159 | 160 | bboxes = self.concat_a2((xs - ws / 2, ys - hs / 2, xs + ws / 2, ys + hs / 2)) 161 | clses = self.expand_dims(clses, 2) 162 | scores = self.expand_dims(scores, 2) 163 | detection = self.concat_a2((bboxes, scores, clses)) 164 | return detection 165 | --------------------------------------------------------------------------------