├── .gitignore ├── DINO ├── .gitignore ├── LICENSE ├── README.md ├── config │ └── DINO │ │ ├── DINO_4scale.py │ │ ├── DINO_4scale_convnext.py │ │ ├── DINO_4scale_increased_backbone_lr.py │ │ ├── DINO_4scale_modified.py │ │ ├── DINO_4scale_modified_300_queries.py │ │ ├── DINO_4scale_swin.py │ │ ├── DINO_5scale.py │ │ └── coco_transformer.py ├── datasets │ ├── __init__.py │ ├── coco.py │ ├── coco_eval.py │ ├── coco_panoptic.py │ ├── data_util.py │ ├── dataset.py │ ├── panoptic_eval.py │ ├── partimagenet.py │ ├── random_crop.py │ ├── sltransform.py │ └── transforms.py ├── engine.py ├── figs │ ├── idea.jpg │ └── sota.jpg ├── main.py ├── models │ ├── __init__.py │ ├── dino │ │ ├── __init__.py │ │ ├── attention.py │ │ ├── backbone.py │ │ ├── convnext.py │ │ ├── deformable_transformer.py │ │ ├── dino.py │ │ ├── dn_components.py │ │ ├── matcher.py │ │ ├── ops │ │ │ ├── functions │ │ │ │ ├── __init__.py │ │ │ │ └── ms_deform_attn_func.py │ │ │ ├── make.sh │ │ │ ├── modules │ │ │ │ ├── __init__.py │ │ │ │ └── ms_deform_attn.py │ │ │ ├── setup.py │ │ │ ├── src │ │ │ │ ├── cpu │ │ │ │ │ ├── ms_deform_attn_cpu.cpp │ │ │ │ │ └── ms_deform_attn_cpu.h │ │ │ │ ├── cuda │ │ │ │ │ ├── ms_deform_attn_cuda.cu │ │ │ │ │ ├── ms_deform_attn_cuda.h │ │ │ │ │ └── ms_deform_im2col_cuda.cuh │ │ │ │ ├── ms_deform_attn.h │ │ │ │ └── vision.cpp │ │ │ └── test.py │ │ ├── position_encoding.py │ │ ├── segmentation.py │ │ ├── swin_transformer.py │ │ ├── transformer_deformable.py │ │ └── utils.py │ └── registry.py ├── requirements.txt ├── run_with_submitit.py ├── scripts │ ├── DINO_eval.sh │ ├── DINO_eval_dist.sh │ ├── DINO_eval_submitit.sh │ ├── DINO_eval_submitit_5scale.sh │ ├── DINO_train.sh │ ├── DINO_train_convnext.sh │ ├── DINO_train_dist.sh │ ├── DINO_train_submitit.sh │ ├── DINO_train_submitit_5scale.sh │ ├── DINO_train_submitit_convnext.sh │ ├── DINO_train_submitit_swin.sh │ └── DINO_train_swin.sh └── util │ ├── __init__.py │ ├── box_loss.py │ ├── box_ops.py │ ├── coco_id2name.json │ ├── get_param_dicts.py │ ├── logger.py │ ├── misc.py │ ├── plot_utils.py │ ├── slconfig.py │ ├── slio.py │ ├── static_data_path.py │ ├── time_counter.py │ ├── utils.py │ ├── vis_utils.py │ └── visualizer.py ├── LICENSE ├── LOC_synset_mapping.txt ├── README.md ├── atta_main.py ├── autoattack_modified ├── __init__.py ├── autoattack.py ├── autopgd_base.py ├── autopgd_pt.py ├── autopgd_tf.py ├── fab_base.py ├── fab_projections.py ├── fab_pt.py ├── fab_tf.py ├── flags.py ├── other_utils.py ├── square.py ├── utils_tf.py └── utils_tf2.py ├── calculate_background.py ├── coco └── coco.py ├── convert_seg_mask_to_bbox.ipynb ├── custom_seg_attack_main.py ├── environment.yml ├── main.py ├── main_pseudo.py ├── panoptic_parts ├── __init__.py ├── specs │ ├── dataset_spec.py │ ├── dataset_specs │ │ ├── cpp_datasetspec.yaml │ │ └── ppp_datasetspec.yaml │ ├── eval_spec.py │ └── eval_specs │ │ ├── ppq_cpp_19_23_cvpr21_default_evalspec.yaml │ │ ├── ppq_cpp_19_23_cvpr21_grouped_evalspec.yaml │ │ └── ppq_ppp_59_57_cvpr21_default_evalspec.yaml └── utils │ ├── __init__.py │ ├── evaluation_PartPQ.py │ ├── experimental_evaluation_IOU.py │ ├── format.py │ ├── internal │ ├── convert_annotations_v1_to_v2.py │ ├── populate_ppp_official_evalspec.py │ └── ppq_ppp_20_58_part_groupings.yaml │ ├── utils.py │ └── visualization.py ├── part_model ├── __init__.py ├── attack │ ├── __init__.py │ ├── _hsja │ │ ├── __init__.py │ │ ├── hop_skip_jump.py │ │ └── hsja.py │ ├── auto.py │ ├── auto_square.py │ ├── base.py │ ├── corruption_benchmark.py │ ├── hsj.py │ ├── masked_pgd.py │ ├── mat.py │ ├── none.py │ ├── pgd.py │ ├── rays │ │ ├── __init__.py │ │ ├── general_torch_model.py │ │ ├── pgbar.py │ │ └── rays.py │ ├── seg_guide.py │ ├── seg_inverse.py │ ├── seg_pgd.py │ └── trades.py ├── dataloader │ ├── __init__.py │ ├── cityscapes.py │ ├── hdf5_dataset.py │ ├── imagenet.py │ ├── part_imagenet.py │ ├── part_imagenet_bbox.py │ ├── part_imagenet_corrupt.py │ ├── part_imagenet_geirhos.py │ ├── part_imagenet_imagenet_class.py │ ├── part_imagenet_mixed_next.py │ ├── part_imagenet_pseudo.py │ ├── part_imagenet_pseudo_imagenet_class.py │ ├── pascal_part.py │ ├── pascal_voc.py │ ├── segmentation_transforms.py │ └── util.py ├── models │ ├── __init__.py │ ├── common.py │ ├── det_part_models │ │ ├── __init__.py │ │ ├── backbone.py │ │ ├── dino.py │ │ ├── multi_head_dino_model.py │ │ └── seq_dino_model.py │ ├── model.py │ ├── seg_part_models │ │ ├── bbox_model.py │ │ ├── clean_mask_model.py │ │ ├── groundtruth_mask_model.py │ │ ├── part_fc_model.py │ │ ├── part_mask_model.py │ │ ├── part_seg_cat_model.py │ │ ├── part_seg_model.py │ │ ├── pixel_count_model.py │ │ ├── pooling_model.py │ │ ├── two_head_model.py │ │ ├── util.py │ │ └── weighted_bbox_model.py │ └── toy_part_model.py └── utils │ ├── __init__.py │ ├── argparse.py │ ├── atta.py │ ├── dataloader_visualizer.py │ ├── distributed.py │ ├── eval_sampler.py │ ├── image.py │ ├── loss.py │ ├── metric.py │ └── types.py ├── partimagenet_hparams.py ├── post_prediction.py ├── post_prediction_imagenet.py ├── pre_prediction.py ├── pre_prediction_imagenet.py ├── prepare_cityscapes.py ├── prepare_part_imagenet.py ├── prepare_pascal_part.py ├── print_metrics.py ├── requirements.txt ├── scripts ├── download_cityscapes.sh ├── prepare_data.sh ├── prepare_data_slurm.sh ├── run.sh ├── run1-1.sh ├── run1-2.sh ├── run1-3.sh ├── run1-4.sh ├── run1.sh ├── run2-1.sh ├── run2-2.sh ├── run2-3.sh ├── run2-4.sh ├── run2.sh ├── run3-1.sh ├── run3-2.sh ├── run3-3.sh ├── run3-4.sh ├── run3.sh ├── run4.sh ├── run_custom_seg_attack.sh ├── run_dino.sh ├── run_dino_hyp_search.sh ├── run_example.sh ├── run_example_gen_new_dataset.sh ├── run_example_gen_new_dataset_imagenet.sh ├── run_gcp.sh ├── run_reds.sh ├── run_slurm_1080ti.sh ├── run_slurm_2080ti.sh ├── run_slurm_cpu.sh ├── run_slurm_v100.sh └── run_test.sh ├── tif_to_png.py ├── to_class_specific.py └── train_semantic_seg.py /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | .github 6 | 7 | # C extensions 8 | *.so 9 | 10 | # Distribution / packaging 11 | .Python 12 | build/ 13 | develop-eggs/ 14 | dist/ 15 | downloads/ 16 | eggs/ 17 | .eggs/ 18 | lib/ 19 | lib64/ 20 | parts/ 21 | sdist/ 22 | var/ 23 | wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | 29 | # Lightning /research 30 | test_tube_exp/ 31 | tests/tests_tt_dir/ 32 | tests/save_dir 33 | default/ 34 | data/ 35 | test_tube_logs/ 36 | test_tube_data/ 37 | model_weights/ 38 | tests/save_dir 39 | tests/tests_tt_dir/ 40 | processed/ 41 | raw/ 42 | wandb/ 43 | 44 | # PyInstaller 45 | # Usually these files are written by a python script from a template 46 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 47 | *.manifest 48 | *.spec 49 | 50 | # Installer logs 51 | pip-log.txt 52 | pip-delete-this-directory.txt 53 | 54 | # Unit test / coverage reports 55 | htmlcov/ 56 | .tox/ 57 | .coverage 58 | .coverage.* 59 | .cache 60 | nosetests.xml 61 | coverage.xml 62 | *.cover 63 | .hypothesis/ 64 | .pytest_cache/ 65 | 66 | # Translations 67 | *.mo 68 | *.pot 69 | 70 | # Django stuff: 71 | *.log 72 | local_settings.py 73 | db.sqlite3 74 | 75 | # Flask stuff: 76 | instance/ 77 | .webassets-cache 78 | 79 | # Scrapy stuff: 80 | .scrapy 81 | 82 | # Sphinx documentation 83 | docs/_build/ 84 | 85 | # PyBuilder 86 | target/ 87 | 88 | # Jupyter Notebook 89 | .ipynb_checkpoints 90 | 91 | # pyenv 92 | .python-version 93 | 94 | # celery beat schedule file 95 | celerybeat-schedule 96 | 97 | # SageMath parsed files 98 | *.sage.py 99 | 100 | # Environments 101 | .env 102 | .venv 103 | env/ 104 | venv/ 105 | ENV/ 106 | env.bak/ 107 | venv.bak/ 108 | 109 | # Spyder project settings 110 | .spyderproject 111 | .spyproject 112 | 113 | # Rope project settings 114 | .ropeproject 115 | 116 | # mkdocs documentation 117 | /site 118 | 119 | # mypy 120 | .mypy_cache/ 121 | 122 | # IDEs 123 | .idea 124 | .vscode 125 | 126 | # seed project 127 | lightning_logs/ 128 | MNIST 129 | .DS_Store 130 | 131 | *.png 132 | *.pt 133 | *.out 134 | results/ 135 | .fuse_hidden* 136 | log/ 137 | results 138 | tmp* 139 | temp* 140 | log.txt 141 | /models/ -------------------------------------------------------------------------------- /DINO/.gitignore: -------------------------------------------------------------------------------- 1 | .nfs* 2 | *.ipynb 3 | *.pyc 4 | .dumbo.json 5 | .DS_Store 6 | .*.swp 7 | *.pth 8 | **/__pycache__/** 9 | .ipynb_checkpoints/ 10 | experiment-* 11 | *.tmp 12 | *.pkl 13 | **/.mypy_cache/* 14 | .mypy_cache/* 15 | not_tracked_dir/ 16 | .vscode 17 | logs 18 | jobs 19 | subs 20 | tmp 21 | *.sub 22 | vis/ 23 | model_zoo/ 24 | model_zoo_old/ -------------------------------------------------------------------------------- /DINO/config/DINO/DINO_4scale.py: -------------------------------------------------------------------------------- 1 | _base_ = ['coco_transformer.py'] 2 | 3 | num_classes=91 4 | 5 | lr = 0.0001 6 | param_dict_type = 'default' 7 | lr_backbone = 1e-05 8 | lr_backbone_names = ['backbone.0'] 9 | lr_linear_proj_names = ['reference_points', 'sampling_offsets'] 10 | lr_linear_proj_mult = 0.1 11 | ddetr_lr_param = False 12 | batch_size = 2 13 | weight_decay = 0.0001 14 | epochs = 12 15 | lr_drop = 11 16 | save_checkpoint_interval = 1 17 | clip_max_norm = 0.1 18 | onecyclelr = False 19 | multi_step_lr = False 20 | lr_drop_list = [33, 45] 21 | 22 | 23 | modelname = 'dino' 24 | frozen_weights = None 25 | backbone = 'resnet50' 26 | use_checkpoint = False 27 | 28 | dilation = False 29 | position_embedding = 'sine' 30 | pe_temperatureH = 20 31 | pe_temperatureW = 20 32 | return_interm_indices = [1, 2, 3] 33 | backbone_freeze_keywords = None 34 | enc_layers = 6 35 | dec_layers = 6 36 | unic_layers = 0 37 | pre_norm = False 38 | dim_feedforward = 2048 39 | hidden_dim = 256 40 | dropout = 0.0 41 | nheads = 8 42 | num_queries = 900 43 | query_dim = 4 44 | num_patterns = 0 45 | pdetr3_bbox_embed_diff_each_layer = False 46 | pdetr3_refHW = -1 47 | random_refpoints_xy = False 48 | fix_refpoints_hw = -1 49 | dabdetr_yolo_like_anchor_update = False 50 | dabdetr_deformable_encoder = False 51 | dabdetr_deformable_decoder = False 52 | use_deformable_box_attn = False 53 | box_attn_type = 'roi_align' 54 | dec_layer_number = None 55 | num_feature_levels = 4 56 | enc_n_points = 4 57 | dec_n_points = 4 58 | decoder_layer_noise = False 59 | dln_xy_noise = 0.2 60 | dln_hw_noise = 0.2 61 | add_channel_attention = False 62 | add_pos_value = False 63 | two_stage_type = 'standard' 64 | two_stage_pat_embed = 0 65 | two_stage_add_query_num = 0 66 | two_stage_bbox_embed_share = False 67 | two_stage_class_embed_share = False 68 | two_stage_learn_wh = False 69 | two_stage_default_hw = 0.05 70 | two_stage_keep_all_tokens = False 71 | num_select = 300 72 | transformer_activation = 'relu' 73 | batch_norm_type = 'FrozenBatchNorm2d' 74 | masks = False 75 | aux_loss = True 76 | set_cost_class = 2.0 77 | set_cost_bbox = 5.0 78 | set_cost_giou = 2.0 79 | cls_loss_coef = 1.0 80 | mask_loss_coef = 1.0 81 | dice_loss_coef = 1.0 82 | bbox_loss_coef = 5.0 83 | giou_loss_coef = 2.0 84 | enc_loss_coef = 1.0 85 | interm_loss_coef = 1.0 86 | no_interm_box_loss = False 87 | focal_alpha = 0.25 88 | 89 | decoder_sa_type = 'sa' # ['sa', 'ca_label', 'ca_content'] 90 | matcher_type = 'HungarianMatcher' # or SimpleMinsumMatcher 91 | decoder_module_seq = ['sa', 'ca', 'ffn'] 92 | nms_iou_threshold = -1 93 | 94 | dec_pred_bbox_embed_share = True 95 | dec_pred_class_embed_share = True 96 | 97 | # for dn 98 | use_dn = True 99 | dn_number = 100 100 | dn_box_noise_scale = 0.4 101 | dn_label_noise_ratio = 0.5 102 | embed_init_tgt = True 103 | dn_labelbook_size = 91 104 | 105 | match_unstable_error = True 106 | 107 | # for ema 108 | use_ema = False 109 | ema_decay = 0.9997 110 | ema_epoch = 0 111 | 112 | use_detached_boxes_dec_out = False 113 | 114 | -------------------------------------------------------------------------------- /DINO/config/DINO/DINO_4scale_convnext.py: -------------------------------------------------------------------------------- 1 | _base_ = ['coco_transformer.py'] 2 | 3 | num_classes=91 4 | 5 | lr = 0.0001 6 | param_dict_type = 'default' 7 | lr_backbone = 1e-05 8 | lr_backbone_names = ['backbone.0'] 9 | lr_linear_proj_names = ['reference_points', 'sampling_offsets'] 10 | lr_linear_proj_mult = 0.1 11 | ddetr_lr_param = False 12 | batch_size = 2 13 | weight_decay = 0.0001 14 | epochs = 12 15 | lr_drop = 11 16 | save_checkpoint_interval = 1 17 | clip_max_norm = 0.1 18 | onecyclelr = False 19 | multi_step_lr = False 20 | lr_drop_list = [33, 45] 21 | 22 | 23 | modelname = 'dino' 24 | frozen_weights = None 25 | backbone = 'convnext_xlarge_22k' 26 | use_checkpoint = False 27 | 28 | dilation = False 29 | position_embedding = 'sine' 30 | pe_temperatureH = 20 31 | pe_temperatureW = 20 32 | return_interm_indices = [1, 2, 3] 33 | backbone_freeze_keywords = None 34 | enc_layers = 6 35 | dec_layers = 6 36 | unic_layers = 0 37 | pre_norm = False 38 | dim_feedforward = 2048 39 | hidden_dim = 256 40 | dropout = 0.0 41 | nheads = 8 42 | num_queries = 900 43 | query_dim = 4 44 | num_patterns = 0 45 | pdetr3_bbox_embed_diff_each_layer = False 46 | pdetr3_refHW = -1 47 | random_refpoints_xy = False 48 | fix_refpoints_hw = -1 49 | dabdetr_yolo_like_anchor_update = False 50 | dabdetr_deformable_encoder = False 51 | dabdetr_deformable_decoder = False 52 | use_deformable_box_attn = False 53 | box_attn_type = 'roi_align' 54 | dec_layer_number = None 55 | num_feature_levels = 4 56 | enc_n_points = 4 57 | dec_n_points = 4 58 | decoder_layer_noise = False 59 | dln_xy_noise = 0.2 60 | dln_hw_noise = 0.2 61 | add_channel_attention = False 62 | add_pos_value = False 63 | two_stage_type = 'standard' 64 | two_stage_pat_embed = 0 65 | two_stage_add_query_num = 0 66 | two_stage_bbox_embed_share = False 67 | two_stage_class_embed_share = False 68 | two_stage_learn_wh = False 69 | two_stage_default_hw = 0.05 70 | two_stage_keep_all_tokens = False 71 | num_select = 300 72 | transformer_activation = 'relu' 73 | batch_norm_type = 'FrozenBatchNorm2d' 74 | masks = False 75 | aux_loss = True 76 | set_cost_class = 2.0 77 | set_cost_bbox = 5.0 78 | set_cost_giou = 2.0 79 | cls_loss_coef = 1.0 80 | mask_loss_coef = 1.0 81 | dice_loss_coef = 1.0 82 | bbox_loss_coef = 5.0 83 | giou_loss_coef = 2.0 84 | enc_loss_coef = 1.0 85 | interm_loss_coef = 1.0 86 | no_interm_box_loss = False 87 | focal_alpha = 0.25 88 | 89 | decoder_sa_type = 'sa' # ['sa', 'ca_label', 'ca_content'] 90 | matcher_type = 'HungarianMatcher' # or SimpleMinsumMatcher 91 | decoder_module_seq = ['sa', 'ca', 'ffn'] 92 | nms_iou_threshold = -1 93 | 94 | dec_pred_bbox_embed_share = True 95 | dec_pred_class_embed_share = True 96 | 97 | # for dn 98 | use_dn = True 99 | dn_number = 100 100 | dn_box_noise_scale = 0.4 101 | dn_label_noise_ratio = 0.5 102 | embed_init_tgt = True 103 | dn_labelbook_size = 91 104 | 105 | match_unstable_error = True 106 | 107 | # for ema 108 | use_ema = False 109 | ema_decay = 0.9997 110 | ema_epoch = 0 111 | 112 | use_detached_boxes_dec_out = False 113 | 114 | -------------------------------------------------------------------------------- /DINO/config/DINO/DINO_4scale_increased_backbone_lr.py: -------------------------------------------------------------------------------- 1 | _base_ = ['coco_transformer.py'] 2 | 3 | # num_classes=91 4 | 5 | # lr = 0.0001 6 | param_dict_type = 'default' 7 | lr_backbone = 1e-02 8 | lr_backbone_names = ['backbone.0'] 9 | lr_linear_proj_names = ['reference_points', 'sampling_offsets'] 10 | lr_linear_proj_mult = 0.1 11 | ddetr_lr_param = False 12 | # batch_size = 2 13 | weight_decay = 0.0001 14 | # epochs = 12 15 | lr_drop = 11 16 | save_checkpoint_interval = 1 17 | clip_max_norm = 0.1 18 | onecyclelr = False 19 | multi_step_lr = False 20 | lr_drop_list = [33, 45] 21 | 22 | 23 | modelname = 'dino' 24 | frozen_weights = None 25 | backbone = 'resnet50' 26 | use_checkpoint = False 27 | 28 | dilation = False 29 | position_embedding = 'sine' 30 | pe_temperatureH = 20 31 | pe_temperatureW = 20 32 | return_interm_indices = [1, 2, 3] 33 | backbone_freeze_keywords = None 34 | enc_layers = 6 35 | dec_layers = 6 36 | unic_layers = 0 37 | pre_norm = False 38 | dim_feedforward = 2048 39 | hidden_dim = 256 40 | dropout = 0.0 41 | nheads = 8 42 | num_queries = 900 43 | query_dim = 4 44 | num_patterns = 0 45 | pdetr3_bbox_embed_diff_each_layer = False 46 | pdetr3_refHW = -1 47 | random_refpoints_xy = False 48 | fix_refpoints_hw = -1 49 | dabdetr_yolo_like_anchor_update = False 50 | dabdetr_deformable_encoder = False 51 | dabdetr_deformable_decoder = False 52 | use_deformable_box_attn = False 53 | box_attn_type = 'roi_align' 54 | dec_layer_number = None 55 | num_feature_levels = 4 56 | enc_n_points = 4 57 | dec_n_points = 4 58 | decoder_layer_noise = False 59 | dln_xy_noise = 0.2 60 | dln_hw_noise = 0.2 61 | add_channel_attention = False 62 | add_pos_value = False 63 | two_stage_type = 'standard' 64 | two_stage_pat_embed = 0 65 | two_stage_add_query_num = 0 66 | two_stage_bbox_embed_share = False 67 | two_stage_class_embed_share = False 68 | two_stage_learn_wh = False 69 | two_stage_default_hw = 0.05 70 | two_stage_keep_all_tokens = False 71 | num_select = 300 72 | transformer_activation = 'relu' 73 | batch_norm_type = 'FrozenBatchNorm2d' 74 | masks = False 75 | aux_loss = True 76 | set_cost_class = 2.0 77 | set_cost_bbox = 5.0 78 | set_cost_giou = 2.0 79 | cls_loss_coef = 1.0 80 | mask_loss_coef = 1.0 81 | dice_loss_coef = 1.0 82 | bbox_loss_coef = 5.0 83 | giou_loss_coef = 2.0 84 | enc_loss_coef = 1.0 85 | interm_loss_coef = 1.0 86 | no_interm_box_loss = False 87 | focal_alpha = 0.25 88 | 89 | decoder_sa_type = 'sa' # ['sa', 'ca_label', 'ca_content'] 90 | matcher_type = 'HungarianMatcher' # or SimpleMinsumMatcher 91 | decoder_module_seq = ['sa', 'ca', 'ffn'] 92 | nms_iou_threshold = -1 93 | 94 | dec_pred_bbox_embed_share = True 95 | dec_pred_class_embed_share = True 96 | 97 | # for dn 98 | use_dn = True 99 | dn_number = 100 100 | dn_box_noise_scale = 0.4 101 | dn_label_noise_ratio = 0.5 102 | embed_init_tgt = True 103 | dn_labelbook_size = 91 104 | 105 | match_unstable_error = True 106 | 107 | # for ema 108 | use_ema = False 109 | ema_decay = 0.9997 110 | ema_epoch = 0 111 | 112 | use_detached_boxes_dec_out = False 113 | 114 | -------------------------------------------------------------------------------- /DINO/config/DINO/DINO_4scale_modified.py: -------------------------------------------------------------------------------- 1 | _base_ = ['coco_transformer.py'] 2 | 3 | # num_classes=91 4 | 5 | # lr = 0.0001 6 | param_dict_type = 'default' 7 | lr_backbone = 1e-02 8 | lr_backbone_names = ['backbone.0'] 9 | lr_linear_proj_names = ['reference_points', 'sampling_offsets'] 10 | lr_linear_proj_mult = 0.1 11 | ddetr_lr_param = False 12 | # batch_size = 2 13 | weight_decay = 0.0001 14 | # epochs = 12 15 | lr_drop = 11 16 | save_checkpoint_interval = 1 17 | clip_max_norm = 0.1 18 | onecyclelr = False 19 | multi_step_lr = False 20 | lr_drop_list = [33, 45] 21 | 22 | 23 | modelname = 'dino' 24 | frozen_weights = None 25 | backbone = 'resnet50' 26 | use_checkpoint = False 27 | 28 | dilation = False 29 | position_embedding = 'sine' 30 | pe_temperatureH = 20 31 | pe_temperatureW = 20 32 | return_interm_indices = [1, 2, 3] 33 | backbone_freeze_keywords = None 34 | enc_layers = 6 35 | dec_layers = 6 36 | unic_layers = 0 37 | pre_norm = False 38 | dim_feedforward = 2048 39 | hidden_dim = 256 40 | dropout = 0.0 41 | nheads = 8 42 | num_queries = 900 43 | query_dim = 4 44 | num_patterns = 0 45 | pdetr3_bbox_embed_diff_each_layer = False 46 | pdetr3_refHW = -1 47 | random_refpoints_xy = False 48 | fix_refpoints_hw = -1 49 | dabdetr_yolo_like_anchor_update = False 50 | dabdetr_deformable_encoder = False 51 | dabdetr_deformable_decoder = False 52 | use_deformable_box_attn = False 53 | box_attn_type = 'roi_align' 54 | dec_layer_number = None 55 | num_feature_levels = 4 56 | enc_n_points = 4 57 | dec_n_points = 4 58 | decoder_layer_noise = False 59 | dln_xy_noise = 0.2 60 | dln_hw_noise = 0.2 61 | add_channel_attention = False 62 | add_pos_value = False 63 | two_stage_type = 'standard' 64 | two_stage_pat_embed = 0 65 | two_stage_add_query_num = 0 66 | two_stage_bbox_embed_share = False 67 | two_stage_class_embed_share = False 68 | two_stage_learn_wh = False 69 | two_stage_default_hw = 0.05 70 | two_stage_keep_all_tokens = False 71 | num_select = 300 72 | transformer_activation = 'relu' 73 | batch_norm_type = 'FrozenBatchNorm2d' 74 | masks = False 75 | aux_loss = True 76 | set_cost_class = 2.0 77 | set_cost_bbox = 5.0 78 | set_cost_giou = 2.0 79 | cls_loss_coef = 1.0 80 | mask_loss_coef = 1.0 81 | dice_loss_coef = 1.0 82 | bbox_loss_coef = 5.0 83 | giou_loss_coef = 2.0 84 | enc_loss_coef = 1.0 85 | interm_loss_coef = 1.0 86 | no_interm_box_loss = False 87 | focal_alpha = 0.25 88 | 89 | decoder_sa_type = 'sa' # ['sa', 'ca_label', 'ca_content'] 90 | matcher_type = 'HungarianMatcher' # or SimpleMinsumMatcher 91 | decoder_module_seq = ['sa', 'ca', 'ffn'] 92 | nms_iou_threshold = -1 93 | 94 | dec_pred_bbox_embed_share = True 95 | dec_pred_class_embed_share = True 96 | 97 | # for dn 98 | use_dn = True 99 | dn_number = 100 100 | dn_box_noise_scale = 0.4 101 | dn_label_noise_ratio = 0.5 102 | embed_init_tgt = True 103 | dn_labelbook_size = 41 104 | # dn_labelbook_size = 91 105 | 106 | match_unstable_error = True 107 | 108 | # for ema 109 | use_ema = False 110 | ema_decay = 0.9997 111 | ema_epoch = 0 112 | 113 | use_detached_boxes_dec_out = False 114 | 115 | -------------------------------------------------------------------------------- /DINO/config/DINO/DINO_4scale_modified_300_queries.py: -------------------------------------------------------------------------------- 1 | _base_ = ['coco_transformer.py'] 2 | 3 | # num_classes=91 4 | 5 | # lr = 0.0001 6 | param_dict_type = 'default' 7 | lr_backbone = 1e-05 8 | lr_backbone_names = ['backbone.0'] 9 | lr_linear_proj_names = ['reference_points', 'sampling_offsets'] 10 | lr_linear_proj_mult = 0.1 11 | ddetr_lr_param = False 12 | # batch_size = 2 13 | weight_decay = 0.0001 14 | # epochs = 12 15 | lr_drop = 11 16 | save_checkpoint_interval = 1 17 | clip_max_norm = 0.1 18 | onecyclelr = False 19 | multi_step_lr = False 20 | lr_drop_list = [33, 45] 21 | 22 | 23 | modelname = 'dino' 24 | frozen_weights = None 25 | backbone = 'resnet50' 26 | use_checkpoint = False 27 | 28 | dilation = False 29 | position_embedding = 'sine' 30 | pe_temperatureH = 20 31 | pe_temperatureW = 20 32 | return_interm_indices = [1, 2, 3] 33 | backbone_freeze_keywords = None 34 | enc_layers = 6 35 | dec_layers = 6 36 | unic_layers = 0 37 | pre_norm = False 38 | dim_feedforward = 2048 39 | hidden_dim = 256 40 | dropout = 0.0 41 | nheads = 8 42 | num_queries = 300 43 | query_dim = 4 44 | num_patterns = 0 45 | pdetr3_bbox_embed_diff_each_layer = False 46 | pdetr3_refHW = -1 47 | random_refpoints_xy = False 48 | fix_refpoints_hw = -1 49 | dabdetr_yolo_like_anchor_update = False 50 | dabdetr_deformable_encoder = False 51 | dabdetr_deformable_decoder = False 52 | use_deformable_box_attn = False 53 | box_attn_type = 'roi_align' 54 | dec_layer_number = None 55 | num_feature_levels = 4 56 | enc_n_points = 4 57 | dec_n_points = 4 58 | decoder_layer_noise = False 59 | dln_xy_noise = 0.2 60 | dln_hw_noise = 0.2 61 | add_channel_attention = False 62 | add_pos_value = False 63 | two_stage_type = 'standard' 64 | two_stage_pat_embed = 0 65 | two_stage_add_query_num = 0 66 | two_stage_bbox_embed_share = False 67 | two_stage_class_embed_share = False 68 | two_stage_learn_wh = False 69 | two_stage_default_hw = 0.05 70 | two_stage_keep_all_tokens = False 71 | num_select = 300 72 | transformer_activation = 'relu' 73 | batch_norm_type = 'FrozenBatchNorm2d' 74 | masks = False 75 | aux_loss = True 76 | set_cost_class = 2.0 77 | set_cost_bbox = 5.0 78 | set_cost_giou = 2.0 79 | cls_loss_coef = 1.0 80 | mask_loss_coef = 1.0 81 | dice_loss_coef = 1.0 82 | bbox_loss_coef = 5.0 83 | giou_loss_coef = 2.0 84 | enc_loss_coef = 1.0 85 | interm_loss_coef = 1.0 86 | no_interm_box_loss = False 87 | focal_alpha = 0.25 88 | 89 | decoder_sa_type = 'sa' # ['sa', 'ca_label', 'ca_content'] 90 | matcher_type = 'HungarianMatcher' # or SimpleMinsumMatcher 91 | decoder_module_seq = ['sa', 'ca', 'ffn'] 92 | nms_iou_threshold = -1 93 | 94 | dec_pred_bbox_embed_share = True 95 | dec_pred_class_embed_share = True 96 | 97 | # for dn 98 | use_dn = True 99 | dn_number = 100 100 | dn_box_noise_scale = 0.4 101 | dn_label_noise_ratio = 0.5 102 | embed_init_tgt = True 103 | dn_labelbook_size = 91 104 | 105 | match_unstable_error = True 106 | 107 | # for ema 108 | use_ema = False 109 | ema_decay = 0.9997 110 | ema_epoch = 0 111 | 112 | use_detached_boxes_dec_out = False 113 | 114 | -------------------------------------------------------------------------------- /DINO/config/DINO/DINO_4scale_swin.py: -------------------------------------------------------------------------------- 1 | _base_ = ['coco_transformer.py'] 2 | 3 | num_classes=91 4 | 5 | lr = 0.0001 6 | param_dict_type = 'default' 7 | lr_backbone = 1e-05 8 | lr_backbone_names = ['backbone.0'] 9 | lr_linear_proj_names = ['reference_points', 'sampling_offsets'] 10 | lr_linear_proj_mult = 0.1 11 | ddetr_lr_param = False 12 | batch_size = 2 13 | weight_decay = 0.0001 14 | epochs = 12 15 | lr_drop = 11 16 | save_checkpoint_interval = 1 17 | clip_max_norm = 0.1 18 | onecyclelr = False 19 | multi_step_lr = False 20 | lr_drop_list = [33, 45] 21 | 22 | 23 | modelname = 'dino' 24 | frozen_weights = None 25 | backbone = 'swin_L_384_22k' 26 | use_checkpoint = True 27 | 28 | dilation = False 29 | position_embedding = 'sine' 30 | pe_temperatureH = 20 31 | pe_temperatureW = 20 32 | return_interm_indices = [1, 2, 3] 33 | backbone_freeze_keywords = None 34 | enc_layers = 6 35 | dec_layers = 6 36 | unic_layers = 0 37 | pre_norm = False 38 | dim_feedforward = 2048 39 | hidden_dim = 256 40 | dropout = 0.0 41 | nheads = 8 42 | num_queries = 900 43 | query_dim = 4 44 | num_patterns = 0 45 | pdetr3_bbox_embed_diff_each_layer = False 46 | pdetr3_refHW = -1 47 | random_refpoints_xy = False 48 | fix_refpoints_hw = -1 49 | dabdetr_yolo_like_anchor_update = False 50 | dabdetr_deformable_encoder = False 51 | dabdetr_deformable_decoder = False 52 | use_deformable_box_attn = False 53 | box_attn_type = 'roi_align' 54 | dec_layer_number = None 55 | num_feature_levels = 4 56 | enc_n_points = 4 57 | dec_n_points = 4 58 | decoder_layer_noise = False 59 | dln_xy_noise = 0.2 60 | dln_hw_noise = 0.2 61 | add_channel_attention = False 62 | add_pos_value = False 63 | two_stage_type = 'standard' 64 | two_stage_pat_embed = 0 65 | two_stage_add_query_num = 0 66 | two_stage_bbox_embed_share = False 67 | two_stage_class_embed_share = False 68 | two_stage_learn_wh = False 69 | two_stage_default_hw = 0.05 70 | two_stage_keep_all_tokens = False 71 | num_select = 300 72 | transformer_activation = 'relu' 73 | batch_norm_type = 'FrozenBatchNorm2d' 74 | masks = False 75 | aux_loss = True 76 | set_cost_class = 2.0 77 | set_cost_bbox = 5.0 78 | set_cost_giou = 2.0 79 | cls_loss_coef = 1.0 80 | mask_loss_coef = 1.0 81 | dice_loss_coef = 1.0 82 | bbox_loss_coef = 5.0 83 | giou_loss_coef = 2.0 84 | enc_loss_coef = 1.0 85 | interm_loss_coef = 1.0 86 | no_interm_box_loss = False 87 | focal_alpha = 0.25 88 | 89 | decoder_sa_type = 'sa' # ['sa', 'ca_label', 'ca_content'] 90 | matcher_type = 'HungarianMatcher' # or SimpleMinsumMatcher 91 | decoder_module_seq = ['sa', 'ca', 'ffn'] 92 | nms_iou_threshold = -1 93 | 94 | dec_pred_bbox_embed_share = True 95 | dec_pred_class_embed_share = True 96 | 97 | # for dn 98 | use_dn = True 99 | dn_number = 100 100 | dn_box_noise_scale = 0.4 101 | dn_label_noise_ratio = 0.5 102 | embed_init_tgt = True 103 | dn_labelbook_size = 91 104 | 105 | match_unstable_error = True 106 | 107 | # for ema 108 | use_ema = False 109 | ema_decay = 0.9997 110 | ema_epoch = 0 111 | 112 | use_detached_boxes_dec_out = False 113 | 114 | -------------------------------------------------------------------------------- /DINO/config/DINO/DINO_5scale.py: -------------------------------------------------------------------------------- 1 | _base_ = ['coco_transformer.py'] 2 | 3 | num_classes=91 4 | 5 | lr = 0.0001 6 | param_dict_type = 'default' 7 | lr_backbone = 1e-05 8 | lr_backbone_names = ['backbone.0'] 9 | lr_linear_proj_names = ['reference_points', 'sampling_offsets'] 10 | lr_linear_proj_mult = 0.1 11 | ddetr_lr_param = False 12 | batch_size = 1 13 | weight_decay = 0.0001 14 | epochs = 12 15 | lr_drop = 11 16 | save_checkpoint_interval = 1 17 | clip_max_norm = 0.1 18 | onecyclelr = False 19 | multi_step_lr = False 20 | lr_drop_list = [33, 45] 21 | 22 | 23 | modelname = 'dino' 24 | frozen_weights = None 25 | backbone = 'resnet50' 26 | use_checkpoint = False 27 | 28 | dilation = False 29 | position_embedding = 'sine' 30 | pe_temperatureH = 20 31 | pe_temperatureW = 20 32 | return_interm_indices = [0, 1, 2, 3] 33 | backbone_freeze_keywords = None 34 | enc_layers = 6 35 | dec_layers = 6 36 | unic_layers = 0 37 | pre_norm = False 38 | dim_feedforward = 2048 39 | hidden_dim = 256 40 | dropout = 0.0 41 | nheads = 8 42 | num_queries = 900 43 | query_dim = 4 44 | num_patterns = 0 45 | pdetr3_bbox_embed_diff_each_layer = False 46 | pdetr3_refHW = -1 47 | random_refpoints_xy = False 48 | fix_refpoints_hw = -1 49 | dabdetr_yolo_like_anchor_update = False 50 | dabdetr_deformable_encoder = False 51 | dabdetr_deformable_decoder = False 52 | use_deformable_box_attn = False 53 | box_attn_type = 'roi_align' 54 | dec_layer_number = None 55 | num_feature_levels = 5 56 | enc_n_points = 4 57 | dec_n_points = 4 58 | decoder_layer_noise = False 59 | dln_xy_noise = 0.2 60 | dln_hw_noise = 0.2 61 | add_channel_attention = False 62 | add_pos_value = False 63 | two_stage_type = 'standard' 64 | two_stage_pat_embed = 0 65 | two_stage_add_query_num = 0 66 | two_stage_bbox_embed_share = False 67 | two_stage_class_embed_share = False 68 | two_stage_learn_wh = False 69 | two_stage_default_hw = 0.05 70 | two_stage_keep_all_tokens = False 71 | num_select = 300 72 | transformer_activation = 'relu' 73 | batch_norm_type = 'FrozenBatchNorm2d' 74 | masks = False 75 | aux_loss = True 76 | set_cost_class = 2.0 77 | set_cost_bbox = 5.0 78 | set_cost_giou = 2.0 79 | cls_loss_coef = 1.0 80 | mask_loss_coef = 1.0 81 | dice_loss_coef = 1.0 82 | bbox_loss_coef = 5.0 83 | giou_loss_coef = 2.0 84 | enc_loss_coef = 1.0 85 | interm_loss_coef = 1.0 86 | no_interm_box_loss = False 87 | focal_alpha = 0.25 88 | 89 | decoder_sa_type = 'sa' # ['sa', 'ca_label', 'ca_content'] 90 | matcher_type = 'HungarianMatcher' # or SimpleMinsumMatcher 91 | decoder_module_seq = ['sa', 'ca', 'ffn'] 92 | nms_iou_threshold = -1 93 | 94 | dec_pred_bbox_embed_share = True 95 | dec_pred_class_embed_share = True 96 | 97 | # for dn 98 | use_dn = True 99 | dn_number = 100 100 | dn_box_noise_scale = 0.4 101 | dn_label_noise_ratio = 0.5 102 | embed_init_tgt = True 103 | dn_labelbook_size = 91 104 | 105 | match_unstable_error = True 106 | 107 | # for ema 108 | use_ema = False 109 | ema_decay = 0.9997 110 | ema_epoch = 0 111 | 112 | use_detached_boxes_dec_out = False 113 | 114 | -------------------------------------------------------------------------------- /DINO/config/DINO/coco_transformer.py: -------------------------------------------------------------------------------- 1 | data_aug_scales = [480, 512, 544, 576, 608, 640, 672, 704, 736, 768, 800] 2 | data_aug_max_size = 1333 3 | data_aug_scales2_resize = [400, 500, 600] 4 | data_aug_scales2_crop = [384, 600] 5 | 6 | 7 | data_aug_scale_overlap = None 8 | 9 | -------------------------------------------------------------------------------- /DINO/datasets/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import torch.utils.data 3 | import torchvision 4 | 5 | # from .coco import build as build_coco 6 | from DINO.datasets.coco import build as build_coco 7 | from DINO.datasets.partimagenet import build as build_partimagenet 8 | 9 | def get_coco_api_from_dataset(dataset): 10 | for _ in range(10): 11 | # if isinstance(dataset, torchvision.datasets.CocoDetection): 12 | # break 13 | if isinstance(dataset, torch.utils.data.Subset): 14 | dataset = dataset.dataset 15 | if isinstance(dataset, torchvision.datasets.CocoDetection): 16 | return dataset.coco 17 | 18 | 19 | def build_dataset(image_set, args): 20 | if args.dataset_file == 'coco': 21 | return build_coco(image_set, args) 22 | if args.dataset_file == 'partimagenet': 23 | return build_partimagenet(image_set, args) 24 | if args.dataset_file == 'coco_panoptic': 25 | # to avoid making panopticapi required for coco 26 | from .coco_panoptic import build as build_coco_panoptic 27 | return build_coco_panoptic(image_set, args) 28 | if args.dataset_file == 'o365': 29 | from .o365 import build_o365_combine 30 | return build_o365_combine(image_set, args) 31 | if args.dataset_file == 'vanke': 32 | from .vanke import build_vanke 33 | return build_vanke(image_set, args) 34 | raise ValueError(f'dataset {args.dataset_file} not supported') 35 | -------------------------------------------------------------------------------- /DINO/datasets/dataset.py: -------------------------------------------------------------------------------- 1 | from __future__ import print_function 2 | 3 | import torch 4 | import torchvision.datasets as datasets 5 | from torch.utils.data import Dataset 6 | from PIL import Image 7 | from .tsv_io import TSVFile 8 | import numpy as np 9 | import base64 10 | import io 11 | 12 | 13 | class TSVDataset(Dataset): 14 | """ TSV dataset for ImageNet 1K training 15 | """ 16 | def __init__(self, tsv_file, transform=None, target_transform=None): 17 | self.tsv = TSVFile(tsv_file) 18 | self.transform = transform 19 | self.target_transform = target_transform 20 | 21 | def __getitem__(self, index): 22 | """ 23 | Args: 24 | index (int): Index 25 | Returns: 26 | tuple: (image, target) where target is class_index of the target class. 27 | """ 28 | row = self.tsv.seek(index) 29 | image_data = base64.b64decode(row[-1]) 30 | image = Image.open(io.BytesIO(image_data)) 31 | image = image.convert('RGB') 32 | target = int(row[1]) 33 | 34 | if self.transform is not None: 35 | img = self.transform(image) 36 | else: 37 | img = image 38 | if self.target_transform is not None: 39 | target = self.target_transform(target) 40 | 41 | return img, target 42 | 43 | def __len__(self): 44 | return self.tsv.num_rows() 45 | -------------------------------------------------------------------------------- /DINO/datasets/panoptic_eval.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import json 3 | import os 4 | 5 | import DINO.util.misc as utils 6 | 7 | try: 8 | from panopticapi.evaluation import pq_compute 9 | except ImportError: 10 | pass 11 | 12 | 13 | class PanopticEvaluator(object): 14 | def __init__(self, ann_file, ann_folder, output_dir="panoptic_eval"): 15 | self.gt_json = ann_file 16 | self.gt_folder = ann_folder 17 | if utils.is_main_process(): 18 | if not os.path.exists(output_dir): 19 | os.mkdir(output_dir) 20 | self.output_dir = output_dir 21 | self.predictions = [] 22 | 23 | def update(self, predictions): 24 | for p in predictions: 25 | with open(os.path.join(self.output_dir, p["file_name"]), "wb") as f: 26 | f.write(p.pop("png_string")) 27 | 28 | self.predictions += predictions 29 | 30 | def synchronize_between_processes(self): 31 | all_predictions = utils.all_gather(self.predictions) 32 | merged_predictions = [] 33 | for p in all_predictions: 34 | merged_predictions += p 35 | self.predictions = merged_predictions 36 | 37 | def summarize(self): 38 | if utils.is_main_process(): 39 | json_data = {"annotations": self.predictions} 40 | predictions_json = os.path.join(self.output_dir, "predictions.json") 41 | with open(predictions_json, "w") as f: 42 | f.write(json.dumps(json_data)) 43 | return pq_compute(self.gt_json, predictions_json, gt_folder=self.gt_folder, pred_folder=self.output_dir) 44 | return None 45 | -------------------------------------------------------------------------------- /DINO/figs/idea.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chawins/adv-part-model/218c2a45906c751be8d8845bd4183b5bfb8242da/DINO/figs/idea.jpg -------------------------------------------------------------------------------- /DINO/figs/sota.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chawins/adv-part-model/218c2a45906c751be8d8845bd4183b5bfb8242da/DINO/figs/sota.jpg -------------------------------------------------------------------------------- /DINO/models/__init__.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------ 2 | # DINO 3 | # Copyright (c) 2022 IDEA. All Rights Reserved. 4 | # Licensed under the Apache License, Version 2.0 [see LICENSE for details] 5 | # ------------------------------------------------------------------------ 6 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 7 | from .dino import build_dino 8 | 9 | def build_model(args): 10 | return build(args) 11 | -------------------------------------------------------------------------------- /DINO/models/dino/__init__.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------ 2 | # Conditional DETR 3 | # Copyright (c) 2021 Microsoft. All Rights Reserved. 4 | # Licensed under the Apache License, Version 2.0 [see LICENSE for details] 5 | # ------------------------------------------------------------------------ 6 | # Copied from DETR (https://github.com/facebookresearch/detr) 7 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved. 8 | # ------------------------------------------------------------------------ 9 | 10 | from .dino import build_dino 11 | -------------------------------------------------------------------------------- /DINO/models/dino/ops/functions/__init__.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------------------------------ 2 | # Deformable DETR 3 | # Copyright (c) 2020 SenseTime. All Rights Reserved. 4 | # Licensed under the Apache License, Version 2.0 [see LICENSE for details] 5 | # ------------------------------------------------------------------------------------------------ 6 | # Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 7 | # ------------------------------------------------------------------------------------------------ 8 | 9 | from .ms_deform_attn_func import MSDeformAttnFunction 10 | 11 | -------------------------------------------------------------------------------- /DINO/models/dino/ops/functions/ms_deform_attn_func.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------------------------------ 2 | # Deformable DETR 3 | # Copyright (c) 2020 SenseTime. All Rights Reserved. 4 | # Licensed under the Apache License, Version 2.0 [see LICENSE for details] 5 | # ------------------------------------------------------------------------------------------------ 6 | # Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 7 | # ------------------------------------------------------------------------------------------------ 8 | 9 | from __future__ import absolute_import 10 | from __future__ import print_function 11 | from __future__ import division 12 | 13 | import torch 14 | import torch.nn.functional as F 15 | from torch.autograd import Function 16 | from torch.autograd.function import once_differentiable 17 | 18 | import MultiScaleDeformableAttention as MSDA 19 | 20 | 21 | class MSDeformAttnFunction(Function): 22 | @staticmethod 23 | def forward(ctx, value, value_spatial_shapes, value_level_start_index, sampling_locations, attention_weights, im2col_step): 24 | ctx.im2col_step = im2col_step 25 | output = MSDA.ms_deform_attn_forward( 26 | value, value_spatial_shapes, value_level_start_index, sampling_locations, attention_weights, ctx.im2col_step) 27 | ctx.save_for_backward(value, value_spatial_shapes, value_level_start_index, sampling_locations, attention_weights) 28 | return output 29 | 30 | @staticmethod 31 | @once_differentiable 32 | def backward(ctx, grad_output): 33 | value, value_spatial_shapes, value_level_start_index, sampling_locations, attention_weights = ctx.saved_tensors 34 | grad_value, grad_sampling_loc, grad_attn_weight = \ 35 | MSDA.ms_deform_attn_backward( 36 | value, value_spatial_shapes, value_level_start_index, sampling_locations, attention_weights, grad_output, ctx.im2col_step) 37 | 38 | return grad_value, None, None, grad_sampling_loc, grad_attn_weight, None 39 | 40 | 41 | def ms_deform_attn_core_pytorch(value, value_spatial_shapes, sampling_locations, attention_weights): 42 | # for debug and test only, 43 | # need to use cuda version instead 44 | N_, S_, M_, D_ = value.shape 45 | _, Lq_, M_, L_, P_, _ = sampling_locations.shape 46 | value_list = value.split([H_ * W_ for H_, W_ in value_spatial_shapes], dim=1) 47 | sampling_grids = 2 * sampling_locations - 1 48 | sampling_value_list = [] 49 | for lid_, (H_, W_) in enumerate(value_spatial_shapes): 50 | # N_, H_*W_, M_, D_ -> N_, H_*W_, M_*D_ -> N_, M_*D_, H_*W_ -> N_*M_, D_, H_, W_ 51 | value_l_ = value_list[lid_].flatten(2).transpose(1, 2).reshape(N_*M_, D_, H_, W_) 52 | # N_, Lq_, M_, P_, 2 -> N_, M_, Lq_, P_, 2 -> N_*M_, Lq_, P_, 2 53 | sampling_grid_l_ = sampling_grids[:, :, :, lid_].transpose(1, 2).flatten(0, 1) 54 | # N_*M_, D_, Lq_, P_ 55 | sampling_value_l_ = F.grid_sample(value_l_, sampling_grid_l_, 56 | mode='bilinear', padding_mode='zeros', align_corners=False) 57 | sampling_value_list.append(sampling_value_l_) 58 | # (N_, Lq_, M_, L_, P_) -> (N_, M_, Lq_, L_, P_) -> (N_, M_, 1, Lq_, L_*P_) 59 | attention_weights = attention_weights.transpose(1, 2).reshape(N_*M_, 1, Lq_, L_*P_) 60 | output = (torch.stack(sampling_value_list, dim=-2).flatten(-2) * attention_weights).sum(-1).view(N_, M_*D_, Lq_) 61 | return output.transpose(1, 2).contiguous() 62 | -------------------------------------------------------------------------------- /DINO/models/dino/ops/make.sh: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | # ------------------------------------------------------------------------------------------------ 3 | # Deformable DETR 4 | # Copyright (c) 2020 SenseTime. All Rights Reserved. 5 | # Licensed under the Apache License, Version 2.0 [see LICENSE for details] 6 | # ------------------------------------------------------------------------------------------------ 7 | # Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 8 | # ------------------------------------------------------------------------------------------------ 9 | 10 | 11 | # TORCH_CUDA_ARCH_LIST="8.0" CUDA_HOME='/path/to/your/cuda/dir' 12 | python setup.py build install 13 | -------------------------------------------------------------------------------- /DINO/models/dino/ops/modules/__init__.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------------------------------ 2 | # Deformable DETR 3 | # Copyright (c) 2020 SenseTime. All Rights Reserved. 4 | # Licensed under the Apache License, Version 2.0 [see LICENSE for details] 5 | # ------------------------------------------------------------------------------------------------ 6 | # Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 7 | # ------------------------------------------------------------------------------------------------ 8 | 9 | from .ms_deform_attn import MSDeformAttn 10 | -------------------------------------------------------------------------------- /DINO/models/dino/ops/setup.py: -------------------------------------------------------------------------------- 1 | # ------------------------------------------------------------------------------------------------ 2 | # Deformable DETR 3 | # Copyright (c) 2020 SenseTime. All Rights Reserved. 4 | # Licensed under the Apache License, Version 2.0 [see LICENSE for details] 5 | # ------------------------------------------------------------------------------------------------ 6 | # Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 7 | # ------------------------------------------------------------------------------------------------ 8 | 9 | import os 10 | import glob 11 | 12 | import torch 13 | 14 | from torch.utils.cpp_extension import CUDA_HOME 15 | from torch.utils.cpp_extension import CppExtension 16 | from torch.utils.cpp_extension import CUDAExtension 17 | 18 | from setuptools import find_packages 19 | from setuptools import setup 20 | 21 | requirements = ["torch", "torchvision"] 22 | 23 | def get_extensions(): 24 | this_dir = os.path.dirname(os.path.abspath(__file__)) 25 | extensions_dir = os.path.join(this_dir, "src") 26 | 27 | main_file = glob.glob(os.path.join(extensions_dir, "*.cpp")) 28 | source_cpu = glob.glob(os.path.join(extensions_dir, "cpu", "*.cpp")) 29 | source_cuda = glob.glob(os.path.join(extensions_dir, "cuda", "*.cu")) 30 | 31 | sources = main_file + source_cpu 32 | extension = CppExtension 33 | extra_compile_args = {"cxx": []} 34 | define_macros = [] 35 | 36 | # import ipdb; ipdb.set_trace() 37 | 38 | if torch.cuda.is_available() and CUDA_HOME is not None: 39 | extension = CUDAExtension 40 | sources += source_cuda 41 | define_macros += [("WITH_CUDA", None)] 42 | extra_compile_args["nvcc"] = [ 43 | "-DCUDA_HAS_FP16=1", 44 | "-D__CUDA_NO_HALF_OPERATORS__", 45 | "-D__CUDA_NO_HALF_CONVERSIONS__", 46 | "-D__CUDA_NO_HALF2_OPERATORS__", 47 | ] 48 | else: 49 | raise NotImplementedError('Cuda is not availabel') 50 | 51 | sources = [os.path.join(extensions_dir, s) for s in sources] 52 | include_dirs = [extensions_dir] 53 | ext_modules = [ 54 | extension( 55 | "MultiScaleDeformableAttention", 56 | sources, 57 | include_dirs=include_dirs, 58 | define_macros=define_macros, 59 | extra_compile_args=extra_compile_args, 60 | ) 61 | ] 62 | return ext_modules 63 | 64 | setup( 65 | name="MultiScaleDeformableAttention", 66 | version="1.0", 67 | author="Weijie Su", 68 | url="https://github.com/fundamentalvision/Deformable-DETR", 69 | description="PyTorch Wrapper for CUDA Functions of Multi-Scale Deformable Attention", 70 | packages=find_packages(exclude=("configs", "tests",)), 71 | ext_modules=get_extensions(), 72 | cmdclass={"build_ext": torch.utils.cpp_extension.BuildExtension}, 73 | ) 74 | -------------------------------------------------------------------------------- /DINO/models/dino/ops/src/cpu/ms_deform_attn_cpu.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | ************************************************************************************************** 3 | * Deformable DETR 4 | * Copyright (c) 2020 SenseTime. All Rights Reserved. 5 | * Licensed under the Apache License, Version 2.0 [see LICENSE for details] 6 | ************************************************************************************************** 7 | * Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 8 | ************************************************************************************************** 9 | */ 10 | 11 | #include 12 | 13 | #include 14 | #include 15 | 16 | 17 | at::Tensor 18 | ms_deform_attn_cpu_forward( 19 | const at::Tensor &value, 20 | const at::Tensor &spatial_shapes, 21 | const at::Tensor &level_start_index, 22 | const at::Tensor &sampling_loc, 23 | const at::Tensor &attn_weight, 24 | const int im2col_step) 25 | { 26 | AT_ERROR("Not implement on cpu"); 27 | } 28 | 29 | std::vector 30 | ms_deform_attn_cpu_backward( 31 | const at::Tensor &value, 32 | const at::Tensor &spatial_shapes, 33 | const at::Tensor &level_start_index, 34 | const at::Tensor &sampling_loc, 35 | const at::Tensor &attn_weight, 36 | const at::Tensor &grad_output, 37 | const int im2col_step) 38 | { 39 | AT_ERROR("Not implement on cpu"); 40 | } 41 | 42 | -------------------------------------------------------------------------------- /DINO/models/dino/ops/src/cpu/ms_deform_attn_cpu.h: -------------------------------------------------------------------------------- 1 | /*! 2 | ************************************************************************************************** 3 | * Deformable DETR 4 | * Copyright (c) 2020 SenseTime. All Rights Reserved. 5 | * Licensed under the Apache License, Version 2.0 [see LICENSE for details] 6 | ************************************************************************************************** 7 | * Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 8 | ************************************************************************************************** 9 | */ 10 | 11 | #pragma once 12 | #include 13 | 14 | at::Tensor 15 | ms_deform_attn_cpu_forward( 16 | const at::Tensor &value, 17 | const at::Tensor &spatial_shapes, 18 | const at::Tensor &level_start_index, 19 | const at::Tensor &sampling_loc, 20 | const at::Tensor &attn_weight, 21 | const int im2col_step); 22 | 23 | std::vector 24 | ms_deform_attn_cpu_backward( 25 | const at::Tensor &value, 26 | const at::Tensor &spatial_shapes, 27 | const at::Tensor &level_start_index, 28 | const at::Tensor &sampling_loc, 29 | const at::Tensor &attn_weight, 30 | const at::Tensor &grad_output, 31 | const int im2col_step); 32 | 33 | 34 | -------------------------------------------------------------------------------- /DINO/models/dino/ops/src/cuda/ms_deform_attn_cuda.h: -------------------------------------------------------------------------------- 1 | /*! 2 | ************************************************************************************************** 3 | * Deformable DETR 4 | * Copyright (c) 2020 SenseTime. All Rights Reserved. 5 | * Licensed under the Apache License, Version 2.0 [see LICENSE for details] 6 | ************************************************************************************************** 7 | * Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 8 | ************************************************************************************************** 9 | */ 10 | 11 | #pragma once 12 | #include 13 | 14 | at::Tensor ms_deform_attn_cuda_forward( 15 | const at::Tensor &value, 16 | const at::Tensor &spatial_shapes, 17 | const at::Tensor &level_start_index, 18 | const at::Tensor &sampling_loc, 19 | const at::Tensor &attn_weight, 20 | const int im2col_step); 21 | 22 | std::vector ms_deform_attn_cuda_backward( 23 | const at::Tensor &value, 24 | const at::Tensor &spatial_shapes, 25 | const at::Tensor &level_start_index, 26 | const at::Tensor &sampling_loc, 27 | const at::Tensor &attn_weight, 28 | const at::Tensor &grad_output, 29 | const int im2col_step); 30 | 31 | -------------------------------------------------------------------------------- /DINO/models/dino/ops/src/ms_deform_attn.h: -------------------------------------------------------------------------------- 1 | /*! 2 | ************************************************************************************************** 3 | * Deformable DETR 4 | * Copyright (c) 2020 SenseTime. All Rights Reserved. 5 | * Licensed under the Apache License, Version 2.0 [see LICENSE for details] 6 | ************************************************************************************************** 7 | * Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 8 | ************************************************************************************************** 9 | */ 10 | 11 | #pragma once 12 | 13 | #include "cpu/ms_deform_attn_cpu.h" 14 | 15 | #ifdef WITH_CUDA 16 | #include "cuda/ms_deform_attn_cuda.h" 17 | #endif 18 | 19 | 20 | at::Tensor 21 | ms_deform_attn_forward( 22 | const at::Tensor &value, 23 | const at::Tensor &spatial_shapes, 24 | const at::Tensor &level_start_index, 25 | const at::Tensor &sampling_loc, 26 | const at::Tensor &attn_weight, 27 | const int im2col_step) 28 | { 29 | if (value.type().is_cuda()) 30 | { 31 | #ifdef WITH_CUDA 32 | return ms_deform_attn_cuda_forward( 33 | value, spatial_shapes, level_start_index, sampling_loc, attn_weight, im2col_step); 34 | #else 35 | AT_ERROR("Not compiled with GPU support"); 36 | #endif 37 | } 38 | AT_ERROR("Not implemented on the CPU"); 39 | } 40 | 41 | std::vector 42 | ms_deform_attn_backward( 43 | const at::Tensor &value, 44 | const at::Tensor &spatial_shapes, 45 | const at::Tensor &level_start_index, 46 | const at::Tensor &sampling_loc, 47 | const at::Tensor &attn_weight, 48 | const at::Tensor &grad_output, 49 | const int im2col_step) 50 | { 51 | if (value.type().is_cuda()) 52 | { 53 | #ifdef WITH_CUDA 54 | return ms_deform_attn_cuda_backward( 55 | value, spatial_shapes, level_start_index, sampling_loc, attn_weight, grad_output, im2col_step); 56 | #else 57 | AT_ERROR("Not compiled with GPU support"); 58 | #endif 59 | } 60 | AT_ERROR("Not implemented on the CPU"); 61 | } 62 | 63 | -------------------------------------------------------------------------------- /DINO/models/dino/ops/src/vision.cpp: -------------------------------------------------------------------------------- 1 | /*! 2 | ************************************************************************************************** 3 | * Deformable DETR 4 | * Copyright (c) 2020 SenseTime. All Rights Reserved. 5 | * Licensed under the Apache License, Version 2.0 [see LICENSE for details] 6 | ************************************************************************************************** 7 | * Modified from https://github.com/chengdazhi/Deformable-Convolution-V2-PyTorch/tree/pytorch_1.0.0 8 | ************************************************************************************************** 9 | */ 10 | 11 | #include "ms_deform_attn.h" 12 | 13 | PYBIND11_MODULE(TORCH_EXTENSION_NAME, m) { 14 | m.def("ms_deform_attn_forward", &ms_deform_attn_forward, "ms_deform_attn_forward"); 15 | m.def("ms_deform_attn_backward", &ms_deform_attn_backward, "ms_deform_attn_backward"); 16 | } 17 | -------------------------------------------------------------------------------- /DINO/models/registry.py: -------------------------------------------------------------------------------- 1 | # -*- coding: utf-8 -*- 2 | # @Author: Yihao Chen 3 | # @Date: 2021-08-16 16:03:17 4 | # @Last Modified by: Shilong Liu 5 | # @Last Modified time: 2022-01-23 15:26 6 | # modified from mmcv 7 | 8 | import inspect 9 | from functools import partial 10 | 11 | 12 | class Registry(object): 13 | 14 | def __init__(self, name): 15 | self._name = name 16 | self._module_dict = dict() 17 | 18 | def __repr__(self): 19 | format_str = self.__class__.__name__ + '(name={}, items={})'.format( 20 | self._name, list(self._module_dict.keys())) 21 | return format_str 22 | 23 | def __len__(self): 24 | return len(self._module_dict) 25 | 26 | @property 27 | def name(self): 28 | return self._name 29 | 30 | @property 31 | def module_dict(self): 32 | return self._module_dict 33 | 34 | def get(self, key): 35 | return self._module_dict.get(key, None) 36 | 37 | def registe_with_name(self, module_name=None, force=False): 38 | return partial(self.register, module_name=module_name, force=force) 39 | 40 | def register(self, module_build_function, module_name=None, force=False): 41 | """Register a module build function. 42 | Args: 43 | module (:obj:`nn.Module`): Module to be registered. 44 | """ 45 | if not inspect.isfunction(module_build_function): 46 | raise TypeError('module_build_function must be a function, but got {}'.format( 47 | type(module_build_function))) 48 | if module_name is None: 49 | module_name = module_build_function.__name__ 50 | if not force and module_name in self._module_dict: 51 | raise KeyError('{} is already registered in {}'.format( 52 | module_name, self.name)) 53 | self._module_dict[module_name] = module_build_function 54 | 55 | return module_build_function 56 | 57 | MODULE_BUILD_FUNCS = Registry('model build functions') 58 | 59 | -------------------------------------------------------------------------------- /DINO/requirements.txt: -------------------------------------------------------------------------------- 1 | cython 2 | git+https://github.com/cocodataset/cocoapi.git#subdirectory=PythonAPI&egg=pycocotools 3 | submitit 4 | torch>=1.5.0 5 | torchvision>=0.6.0 6 | git+https://github.com/cocodataset/panopticapi.git#egg=panopticapi 7 | scipy 8 | termcolor 9 | addict 10 | yapf 11 | timm -------------------------------------------------------------------------------- /DINO/scripts/DINO_eval.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #SBATCH --job-name=part-model 3 | #SBATCH --account=fc_wagner 4 | #SBATCH --partition=savio2_1080ti 5 | # Number of nodes: 6 | #SBATCH --nodes=1 7 | # Number of tasks (one for each GPU desired for use case) (example): 8 | #SBATCH --ntasks=2 9 | # Processors per task (please always specify the total number of processors twice the number of GPUs): 10 | #SBATCH --cpus-per-task=1 11 | #Number of GPUs, this can be in the format of "gpu:[1-4]", or "gpu:K80:[1-4] with the type included 12 | #SBATCH --gres=gpu:1 13 | #SBATCH --time=24:00:00 14 | #SBATCH --output slurm-%j-ct_kp_0.5.out 15 | ## Command(s) to run: 16 | # source /global/home/users/$USER/.bash_profile 17 | module purge 18 | # module load python 19 | source activate /global/scratch/users/$USER/env_part_based 20 | module load cuda/10.2 21 | module load gcc/6.4.0 22 | 23 | python main_debug.py 24 | 25 | cd /global/home/users/nabeel126/adv-part-model/DINO/models/dino/ops/ 26 | bash make.sh 27 | cd /global/home/users/nabeel126/adv-part-model/DINO/ 28 | 29 | 30 | 31 | 32 | 33 | ID=8 34 | GPU=0 35 | NUM_GPU=1 36 | # BS=32 37 | # AA_BS=32 38 | PORT=1000$ID 39 | # BACKEND=nccl 40 | # NUM_WORKERS=2 41 | 42 | coco_path=/global/scratch/users/nabeel126/coco/ 43 | checkpoint=/global/scratch/users/nabeel126/adv-part-model/DINO/DINO_pretrained_models/checkpoint0011_4scale.pth 44 | 45 | echo "Starting Evaluation" 46 | 47 | 48 | torchrun \ 49 | --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 50 | main.py \ 51 | --output_dir logs/DINO/R50-MS4-%j \ 52 | -c config/DINO/DINO_4scale.py --coco_path $coco_path \ 53 | --eval --resume $checkpoint \ 54 | --options dn_scalar=100 embed_init_tgt=TRUE \ 55 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 56 | dn_box_noise_scale=1.0 57 | 58 | 59 | # python -m torch.distributed.launch --nproc_per_node=8 main.py --world-size 1 --rank 0 ... 60 | 61 | # python main.py \ 62 | # --output_dir logs/DINO/R50-MS4-%j \ 63 | # -c config/DINO/DINO_4scale.py --coco_path $coco_path \ 64 | # --eval --resume $checkpoint \ 65 | # --options dn_scalar=100 embed_init_tgt=TRUE \ 66 | # dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 67 | # dn_box_noise_scale=1.0 \ 68 | # --dist-backend $BACKEND 69 | 70 | # python main.py \ 71 | # --output_dir logs/DINO/R50-MS4-%j \ 72 | # -c config/DINO/DINO_4scale.py --coco_path $coco_path \ 73 | # --eval --resume $checkpoint \ 74 | # --options dn_scalar=100 embed_init_tgt=TRUE \ 75 | # dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 76 | # dn_box_noise_scale=1.0 77 | 78 | -------------------------------------------------------------------------------- /DINO/scripts/DINO_eval_dist.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | checkpoint=$2 3 | python -m torch.distributed.launch --nproc_per_node=8 main.py \ 4 | --output_dir logs/DINO/R50-MS4-%j \ 5 | -c config/DINO/DINO_4scale.py --coco_path $coco_path \ 6 | --eval --resume $checkpoint \ 7 | --options dn_scalar=100 embed_init_tgt=TRUE \ 8 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 9 | dn_box_noise_scale=1.0 10 | -------------------------------------------------------------------------------- /DINO/scripts/DINO_eval_submitit.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | checkpoint=$2 3 | python run_with_submitit.py --timeout 3000 --job_name DINO \ 4 | --job_dir logs/DINO/R50-MS4-%j --ngpus 8 --nodes 1 \ 5 | -c config/DINO/DINO_4scale.py --coco_path $coco_path \ 6 | --eval --resume $checkpoint \ 7 | --options dn_scalar=100 embed_init_tgt=TRUE \ 8 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 9 | dn_box_noise_scale=1.0 10 | -------------------------------------------------------------------------------- /DINO/scripts/DINO_eval_submitit_5scale.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | checkpoint=$2 3 | python run_with_submitit.py --timeout 3000 --job_name DINO \ 4 | --job_dir logs/DINO/R50-MS5-%j --ngpus 8 --nodes 1 \ 5 | -c config/DINO/DINO_5scale.py --coco_path $coco_path \ 6 | --eval --resume $checkpoint \ 7 | --options dn_scalar=100 embed_init_tgt=TRUE \ 8 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 9 | dn_box_noise_scale=1.0 10 | -------------------------------------------------------------------------------- /DINO/scripts/DINO_train.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | python main.py \ 3 | --output_dir logs/DINO/R50-MS4 -c config/DINO/DINO_4scale.py --coco_path $coco_path \ 4 | --options dn_scalar=100 embed_init_tgt=TRUE \ 5 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 6 | dn_box_noise_scale=1.0 7 | -------------------------------------------------------------------------------- /DINO/scripts/DINO_train_convnext.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | backbone_dir=$2 3 | export CUDA_VISIBLE_DEVICES=$3 && python main.py \ 4 | --output_dir logs/DINO/R50-MS4 -c config/DINO/DINO_4scale_convnext.py --coco_path $coco_path \ 5 | --options dn_scalar=100 embed_init_tgt=TRUE \ 6 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 7 | dn_box_noise_scale=1.0 backbone_dir=$backbone_dir -------------------------------------------------------------------------------- /DINO/scripts/DINO_train_dist.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | python -m torch.distributed.launch --nproc_per_node=8 main.py \ 3 | --output_dir logs/DINO/R50-MS4 -c config/DINO/DINO_4scale.py --coco_path $coco_path \ 4 | --options dn_scalar=100 embed_init_tgt=TRUE \ 5 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 6 | dn_box_noise_scale=1.0 7 | -------------------------------------------------------------------------------- /DINO/scripts/DINO_train_submitit.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | python run_with_submitit.py --timeout 3000 --job_name DINO \ 3 | --job_dir logs/DINO/R50-MS4-%j --ngpus 8 --nodes 1 \ 4 | -c config/DINO/DINO_4scale.py --coco_path $coco_path \ 5 | --options dn_scalar=100 embed_init_tgt=TRUE \ 6 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 7 | dn_box_noise_scale=1.0 8 | -------------------------------------------------------------------------------- /DINO/scripts/DINO_train_submitit_5scale.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | python run_with_submitit.py --timeout 3000 --job_name DINO \ 3 | --job_dir logs/DINO/R50-MS5-%j --ngpus 8 --nodes 2 \ 4 | -c config/DINO/DINO_5scale.py --coco_path $coco_path \ 5 | --options dn_scalar=100 embed_init_tgt=TRUE \ 6 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 7 | dn_box_noise_scale=1.0 8 | -------------------------------------------------------------------------------- /DINO/scripts/DINO_train_submitit_convnext.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | backbone_dir=$2 3 | python run_with_submitit.py --timeout 3000 --job_name DINO \ 4 | --job_dir logs/DINO/R50-MS4-%j --ngpus 8 --nodes 1 \ 5 | -c config/DINO/DINO_4scale_convnext.py --coco_path $coco_path \ 6 | --options dn_scalar=100 embed_init_tgt=TRUE \ 7 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 8 | dn_box_noise_scale=1.0 backbone_dir=$backbone_dir 9 | -------------------------------------------------------------------------------- /DINO/scripts/DINO_train_submitit_swin.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | backbone_dir=$2 3 | python run_with_submitit.py --timeout 3000 --job_name DINO \ 4 | --job_dir logs/DINO/R50-MS4-%j --ngpus 8 --nodes 1 \ 5 | -c config/DINO/DINO_4scale_swin.py --coco_path $coco_path \ 6 | --options dn_scalar=100 embed_init_tgt=TRUE \ 7 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 8 | dn_box_noise_scale=1.0 backbone_dir=$backbone_dir 9 | -------------------------------------------------------------------------------- /DINO/scripts/DINO_train_swin.sh: -------------------------------------------------------------------------------- 1 | coco_path=$1 2 | backbone_dir=$2 3 | export CUDA_VISIBLE_DEVICES=$3 && python main.py \ 4 | --output_dir logs/DINO/R50-MS4 -c config/DINO/DINO_4scale_swin.py --coco_path $coco_path \ 5 | --options dn_scalar=100 embed_init_tgt=TRUE \ 6 | dn_label_coef=1.0 dn_bbox_coef=1.0 use_ema=False \ 7 | dn_box_noise_scale=1.0 backbone_dir=$backbone_dir 8 | -------------------------------------------------------------------------------- /DINO/util/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | -------------------------------------------------------------------------------- /DINO/util/coco_id2name.json: -------------------------------------------------------------------------------- 1 | {"1": "person", "2": "bicycle", "3": "car", "4": "motorcycle", "5": "airplane", "6": "bus", "7": "train", "8": "truck", "9": "boat", "10": "traffic light", "11": "fire hydrant", "13": "stop sign", "14": "parking meter", "15": "bench", "16": "bird", "17": "cat", "18": "dog", "19": "horse", "20": "sheep", "21": "cow", "22": "elephant", "23": "bear", "24": "zebra", "25": "giraffe", "27": "backpack", "28": "umbrella", "31": "handbag", "32": "tie", "33": "suitcase", "34": "frisbee", "35": "skis", "36": "snowboard", "37": "sports ball", "38": "kite", "39": "baseball bat", "40": "baseball glove", "41": "skateboard", "42": "surfboard", "43": "tennis racket", "44": "bottle", "46": "wine glass", "47": "cup", "48": "fork", "49": "knife", "50": "spoon", "51": "bowl", "52": "banana", "53": "apple", "54": "sandwich", "55": "orange", "56": "broccoli", "57": "carrot", "58": "hot dog", "59": "pizza", "60": "donut", "61": "cake", "62": "chair", "63": "couch", "64": "potted plant", "65": "bed", "67": "dining table", "70": "toilet", "72": "tv", "73": "laptop", "74": "mouse", "75": "remote", "76": "keyboard", "77": "cell phone", "78": "microwave", "79": "oven", "80": "toaster", "81": "sink", "82": "refrigerator", "84": "book", "85": "clock", "86": "vase", "87": "scissors", "88": "teddy bear", "89": "hair drier", "90": "toothbrush"} -------------------------------------------------------------------------------- /DINO/util/get_param_dicts.py: -------------------------------------------------------------------------------- 1 | import json 2 | import torch 3 | import torch.nn as nn 4 | 5 | 6 | def match_name_keywords(n: str, name_keywords: list): 7 | out = False 8 | for b in name_keywords: 9 | if b in n: 10 | out = True 11 | break 12 | return out 13 | 14 | 15 | def get_param_dict(args, model_without_ddp: nn.Module): 16 | try: 17 | param_dict_type = args.param_dict_type 18 | except: 19 | param_dict_type = 'default' 20 | assert param_dict_type in ['default', 'ddetr_in_mmdet', 'large_wd'] 21 | 22 | # by default 23 | if param_dict_type == 'default': 24 | param_dicts = [ 25 | {"params": [p for n, p in model_without_ddp.named_parameters() if "backbone" not in n and p.requires_grad]}, 26 | { 27 | "params": [p for n, p in model_without_ddp.named_parameters() if "backbone" in n and p.requires_grad], 28 | "lr": args.lr_backbone, 29 | } 30 | ] 31 | return param_dicts 32 | 33 | if param_dict_type == 'ddetr_in_mmdet': 34 | param_dicts = [ 35 | { 36 | "params": 37 | [p for n, p in model_without_ddp.named_parameters() 38 | if not match_name_keywords(n, args.lr_backbone_names) and not match_name_keywords(n, args.lr_linear_proj_names) and p.requires_grad], 39 | "lr": args.lr, 40 | }, 41 | { 42 | "params": [p for n, p in model_without_ddp.named_parameters() 43 | if match_name_keywords(n, args.lr_backbone_names) and p.requires_grad], 44 | "lr": args.lr_backbone, 45 | }, 46 | { 47 | "params": [p for n, p in model_without_ddp.named_parameters() 48 | if match_name_keywords(n, args.lr_linear_proj_names) and p.requires_grad], 49 | "lr": args.lr * args.lr_linear_proj_mult, 50 | } 51 | ] 52 | return param_dicts 53 | 54 | if param_dict_type == 'large_wd': 55 | param_dicts = [ 56 | { 57 | "params": 58 | [p for n, p in model_without_ddp.named_parameters() 59 | if not match_name_keywords(n, ['backbone']) and not match_name_keywords(n, ['norm', 'bias']) and p.requires_grad], 60 | }, 61 | { 62 | "params": [p for n, p in model_without_ddp.named_parameters() 63 | if match_name_keywords(n, ['backbone']) and match_name_keywords(n, ['norm', 'bias']) and p.requires_grad], 64 | "lr": args.lr_backbone, 65 | "weight_decay": 0.0, 66 | }, 67 | { 68 | "params": [p for n, p in model_without_ddp.named_parameters() 69 | if match_name_keywords(n, ['backbone']) and not match_name_keywords(n, ['norm', 'bias']) and p.requires_grad], 70 | "lr": args.lr_backbone, 71 | "weight_decay": args.weight_decay, 72 | }, 73 | { 74 | "params": 75 | [p for n, p in model_without_ddp.named_parameters() 76 | if not match_name_keywords(n, ['backbone']) and match_name_keywords(n, ['norm', 'bias']) and p.requires_grad], 77 | "lr": args.lr, 78 | "weight_decay": 0.0, 79 | } 80 | ] 81 | 82 | # print("param_dicts: {}".format(param_dicts)) 83 | 84 | return param_dicts -------------------------------------------------------------------------------- /DINO/util/logger.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved 2 | import functools 3 | import logging 4 | import os 5 | import sys 6 | from termcolor import colored 7 | 8 | 9 | class _ColorfulFormatter(logging.Formatter): 10 | def __init__(self, *args, **kwargs): 11 | self._root_name = kwargs.pop("root_name") + "." 12 | self._abbrev_name = kwargs.pop("abbrev_name", "") 13 | if len(self._abbrev_name): 14 | self._abbrev_name = self._abbrev_name + "." 15 | super(_ColorfulFormatter, self).__init__(*args, **kwargs) 16 | 17 | def formatMessage(self, record): 18 | record.name = record.name.replace(self._root_name, self._abbrev_name) 19 | log = super(_ColorfulFormatter, self).formatMessage(record) 20 | if record.levelno == logging.WARNING: 21 | prefix = colored("WARNING", "red", attrs=["blink"]) 22 | elif record.levelno == logging.ERROR or record.levelno == logging.CRITICAL: 23 | prefix = colored("ERROR", "red", attrs=["blink", "underline"]) 24 | else: 25 | return log 26 | return prefix + " " + log 27 | 28 | 29 | # so that calling setup_logger multiple times won't add many handlers 30 | @functools.lru_cache() 31 | def setup_logger( 32 | output=None, distributed_rank=0, *, color=True, name="imagenet", abbrev_name=None 33 | ): 34 | """ 35 | Initialize the detectron2 logger and set its verbosity level to "INFO". 36 | 37 | Args: 38 | output (str): a file name or a directory to save log. If None, will not save log file. 39 | If ends with ".txt" or ".log", assumed to be a file name. 40 | Otherwise, logs will be saved to `output/log.txt`. 41 | name (str): the root module name of this logger 42 | 43 | Returns: 44 | logging.Logger: a logger 45 | """ 46 | logger = logging.getLogger(name) 47 | logger.setLevel(logging.DEBUG) 48 | logger.propagate = False 49 | 50 | if abbrev_name is None: 51 | abbrev_name = name 52 | 53 | plain_formatter = logging.Formatter( 54 | '[%(asctime)s.%(msecs)03d]: %(message)s', 55 | datefmt='%m/%d %H:%M:%S' 56 | ) 57 | # stdout logging: master only 58 | if distributed_rank == 0: 59 | ch = logging.StreamHandler(stream=sys.stdout) 60 | ch.setLevel(logging.DEBUG) 61 | if color: 62 | formatter = _ColorfulFormatter( 63 | colored("[%(asctime)s.%(msecs)03d]: ", "green") + "%(message)s", 64 | datefmt="%m/%d %H:%M:%S", 65 | root_name=name, 66 | abbrev_name=str(abbrev_name), 67 | ) 68 | else: 69 | formatter = plain_formatter 70 | ch.setFormatter(formatter) 71 | logger.addHandler(ch) 72 | 73 | # file logging: all workers 74 | if output is not None: 75 | if output.endswith(".txt") or output.endswith(".log"): 76 | filename = output 77 | else: 78 | filename = os.path.join(output, "log.txt") 79 | if distributed_rank > 0: 80 | filename = filename + f".rank{distributed_rank}" 81 | os.makedirs(os.path.dirname(filename), exist_ok=True) 82 | 83 | fh = logging.StreamHandler(_cached_log_stream(filename)) 84 | fh.setLevel(logging.DEBUG) 85 | fh.setFormatter(plain_formatter) 86 | logger.addHandler(fh) 87 | 88 | return logger 89 | 90 | 91 | # cache the opened file object, so that different calls to `setup_logger` 92 | # with the same file name can safely write to the same file. 93 | @functools.lru_cache(maxsize=None) 94 | def _cached_log_stream(filename): 95 | return open(filename, "a") 96 | -------------------------------------------------------------------------------- /DINO/util/static_data_path.py: -------------------------------------------------------------------------------- 1 | coco = dict( 2 | train = dict( 3 | img_folder = '/comp_robot/cv_public_dataset/COCO2017/train2017', 4 | ann_file = '/comp_robot/cv_public_dataset/COCO2017/annotations/instances_train2017.json' 5 | ), 6 | val = dict( 7 | img_folder = '/comp_robot/cv_public_dataset/COCO2017/val2017', 8 | ann_file = '/comp_robot/cv_public_dataset/COCO2017/annotations/instances_val2017.json' 9 | ) 10 | ) -------------------------------------------------------------------------------- /DINO/util/time_counter.py: -------------------------------------------------------------------------------- 1 | import json 2 | import time 3 | 4 | class TimeCounter: 5 | def __init__(self) -> None: 6 | pass 7 | 8 | def clear(self): 9 | self.timedict = {} 10 | self.basetime = time.perf_counter() 11 | 12 | def timeit(self, name): 13 | nowtime = time.perf_counter() - self.basetime 14 | self.timedict[name] = nowtime 15 | self.basetime = time.perf_counter() 16 | 17 | 18 | class TimeHolder: 19 | def __init__(self) -> None: 20 | self.timedict = {} 21 | 22 | def update(self, _timedict:dict): 23 | for k,v in _timedict.items(): 24 | if k not in self.timedict: 25 | self.timedict[k] = AverageMeter(name=k, val_only=True) 26 | self.timedict[k].update(val=v) 27 | 28 | def final_res(self): 29 | return {k:v.avg for k,v in self.timedict.items()} 30 | 31 | def __str__(self): 32 | return json.dumps(self.final_res(), indent=2) 33 | 34 | 35 | class AverageMeter(object): 36 | """Computes and stores the average and current value""" 37 | def __init__(self, name, fmt=':f', val_only=False): 38 | self.name = name 39 | self.fmt = fmt 40 | self.val_only = val_only 41 | self.reset() 42 | 43 | def reset(self): 44 | self.val = 0 45 | self.avg = 0 46 | self.sum = 0 47 | self.count = 0 48 | 49 | def update(self, val, n=1): 50 | self.val = val 51 | self.sum += val * n 52 | self.count += n 53 | self.avg = self.sum / self.count 54 | 55 | def __str__(self): 56 | if self.val_only: 57 | fmtstr = '{name} {val' + self.fmt + '}' 58 | else: 59 | fmtstr = '{name} {val' + self.fmt + '} ({avg' + self.fmt + '})' 60 | return fmtstr.format(**self.__dict__) -------------------------------------------------------------------------------- /DINO/util/vis_utils.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import numpy as np 3 | 4 | from util.utils import renorm 5 | from util.misc import color_sys 6 | 7 | _color_getter = color_sys(100) 8 | 9 | # plot known and unknown box 10 | def add_box_to_img(img, boxes, colorlist, brands=None): 11 | """[summary] 12 | 13 | Args: 14 | img ([type]): np.array, H,W,3 15 | boxes ([type]): list of list(4) 16 | colorlist: list of colors. 17 | brands: text. 18 | 19 | Return: 20 | img: np.array. H,W,3. 21 | """ 22 | H, W = img.shape[:2] 23 | for _i, (box, color) in enumerate(zip(boxes, colorlist)): 24 | x, y, w, h = box[0] * W, box[1] * H, box[2] * W, box[3] * H 25 | img = cv2.rectangle(img.copy(), (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), color, 2) 26 | if brands is not None: 27 | brand = brands[_i] 28 | org = (int(x-w/2), int(y+h/2)) 29 | font = cv2.FONT_HERSHEY_SIMPLEX 30 | fontScale = 0.5 31 | thickness = 1 32 | img = cv2.putText(img.copy(), str(brand), org, font, 33 | fontScale, color, thickness, cv2.LINE_AA) 34 | return img 35 | 36 | def plot_dual_img(img, boxes, labels, idxs, probs=None): 37 | """[summary] 38 | 39 | Args: 40 | img ([type]): 3,H,W. tensor. 41 | boxes (): tensor(Kx4) or list of tensor(1x4). 42 | labels ([type]): list of ints. 43 | idxs ([type]): list of ints. 44 | probs (optional): listof floats. 45 | 46 | Returns: 47 | img_classcolor: np.array. H,W,3. img with class-wise label. 48 | img_seqcolor: np.array. H,W,3. img with seq-wise label. 49 | """ 50 | # import ipdb; ipdb.set_trace() 51 | boxes = [i.cpu().tolist() for i in boxes] 52 | img = (renorm(img.cpu()).permute(1,2,0).numpy() * 255).astype(np.uint8) 53 | # plot with class 54 | class_colors = [_color_getter(i) for i in labels] 55 | if probs is not None: 56 | brands = ["{},{:.2f}".format(j,k) for j,k in zip(labels, probs)] 57 | else: 58 | brands = labels 59 | img_classcolor = add_box_to_img(img, boxes, class_colors, brands=brands) 60 | # plot with seq 61 | seq_colors = [_color_getter((i * 11) % 100) for i in idxs] 62 | img_seqcolor = add_box_to_img(img, boxes, seq_colors, brands=idxs) 63 | return img_classcolor, img_seqcolor 64 | 65 | 66 | def plot_raw_img(img, boxes, labels): 67 | """[summary] 68 | 69 | Args: 70 | img ([type]): 3,H,W. tensor. 71 | boxes ([type]): Kx4. tensor 72 | labels ([type]): K. tensor. 73 | 74 | return: 75 | img: np.array. H,W,3. img with bbox annos. 76 | 77 | """ 78 | img = (renorm(img.cpu()).permute(1,2,0).numpy() * 255).astype(np.uint8) 79 | H, W = img.shape[:2] 80 | for box, label in zip(boxes.tolist(), labels.tolist()): 81 | x, y, w, h = box[0] * W, box[1] * H, box[2] * W, box[3] * H 82 | # import ipdb; ipdb.set_trace() 83 | img = cv2.rectangle(img.copy(), (int(x-w/2), int(y-h/2)), (int(x+w/2), int(y+h/2)), _color_getter(label), 2) 84 | # add text 85 | org = (int(x-w/2), int(y+h/2)) 86 | font = cv2.FONT_HERSHEY_SIMPLEX 87 | fontScale = 1 88 | thickness = 1 89 | img = cv2.putText(img.copy(), str(label), org, font, 90 | fontScale, _color_getter(label), thickness, cv2.LINE_AA) 91 | 92 | return img -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Chawin Sitawarin 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /autoattack_modified/__init__.py: -------------------------------------------------------------------------------- 1 | from .autoattack import AutoAttack 2 | -------------------------------------------------------------------------------- /autoattack_modified/fab_tf.py: -------------------------------------------------------------------------------- 1 | # Copyright (c) 2019-present, Francesco Croce 2 | # All rights reserved. 3 | # 4 | # This source code is licensed under the license found in the 5 | # LICENSE file in the root directory of this source tree. 6 | # 7 | 8 | from __future__ import absolute_import 9 | from __future__ import division 10 | from __future__ import print_function 11 | from __future__ import unicode_literals 12 | 13 | import torch 14 | from torch.autograd.gradcheck import zero_gradients 15 | import time 16 | 17 | import tensorflow as tf 18 | 19 | from autoattack.fab_base import FABAttack 20 | 21 | #from advertorch.utils import replicate_input 22 | 23 | #from .base import Attack 24 | #from .base import LabelMixin 25 | 26 | 27 | class FABAttack_TF(FABAttack): 28 | """ 29 | Fast Adaptive Boundary Attack (Linf, L2, L1) 30 | https://arxiv.org/abs/1907.02044 31 | 32 | :param model: TF_model 33 | :param norm: Lp-norm to minimize ('Linf', 'L2', 'L1' supported) 34 | :param n_restarts: number of random restarts 35 | :param n_iter: number of iterations 36 | :param eps: epsilon for the random restarts 37 | :param alpha_max: alpha_max 38 | :param eta: overshooting 39 | :param beta: backward step 40 | """ 41 | 42 | def __init__( 43 | self, 44 | model, 45 | norm='Linf', 46 | n_restarts=1, 47 | n_iter=100, 48 | eps=None, 49 | alpha_max=0.1, 50 | eta=1.05, 51 | beta=0.9, 52 | loss_fn=None, 53 | verbose=False, 54 | seed=0, 55 | targeted=False, 56 | device=None, 57 | n_target_classes=9): 58 | """ FAB-attack implementation in TF2 """ 59 | 60 | self.model = model 61 | super().__init__(norm, 62 | n_restarts, 63 | n_iter, 64 | eps, 65 | alpha_max, 66 | eta, 67 | beta, 68 | loss_fn, 69 | verbose, 70 | seed, 71 | targeted, 72 | device, 73 | n_target_classes) 74 | 75 | def _predict_fn(self, x): 76 | return self.model.predict(x) 77 | 78 | def _get_predicted_label(self, x): 79 | with torch.no_grad(): 80 | outputs = self._predict_fn(x) 81 | _, y = torch.max(outputs, dim=1) 82 | return y 83 | 84 | def get_diff_logits_grads_batch(self, imgs, la): 85 | y2, g2 = self.model.grad_logits(imgs) 86 | df = y2 - y2[torch.arange(imgs.shape[0]), la].unsqueeze(1) 87 | dg = g2 - g2[torch.arange(imgs.shape[0]), la].unsqueeze(1) 88 | df[torch.arange(imgs.shape[0]), la] = 1e10 89 | 90 | return df, dg 91 | 92 | def get_diff_logits_grads_batch_targeted(self, imgs, la, la_target): 93 | df, dg = self.model.get_grad_diff_logits_target(imgs, la, la_target) 94 | df.unsqueeze_(1) 95 | dg.unsqueeze_(1) 96 | 97 | return df, dg 98 | -------------------------------------------------------------------------------- /autoattack_modified/flags.py: -------------------------------------------------------------------------------- 1 | SET_MASK = False 2 | -------------------------------------------------------------------------------- /autoattack_modified/other_utils.py: -------------------------------------------------------------------------------- 1 | import copy 2 | import os 3 | 4 | 5 | class Logger: 6 | def __init__(self, log_path): 7 | self.log_path = log_path 8 | 9 | def log(self, str_to_log): 10 | print(str_to_log) 11 | if not self.log_path is None: 12 | with open(self.log_path, "a") as f: 13 | f.write(str_to_log + "\n") 14 | f.flush() 15 | 16 | 17 | def check_imgs(adv, x, norm): 18 | delta = (adv - x).view(adv.shape[0], -1) 19 | if norm == "Linf": 20 | res = delta.abs().max(dim=1)[0] 21 | elif norm == "L2": 22 | res = (delta**2).sum(dim=1).sqrt() 23 | elif norm == "L1": 24 | res = delta.abs().sum(dim=1) 25 | 26 | str_det = "max {} pert: {:.5f}, nan in imgs: {}, max in imgs: {:.5f}, min in imgs: {:.5f}".format( 27 | norm, res.max(), (adv != adv).sum(), adv.max(), adv.min() 28 | ) 29 | print(str_det) 30 | 31 | return str_det 32 | 33 | 34 | def L1_norm(x, keepdim=False): 35 | z = x.abs().view(x.shape[0], -1).sum(-1) 36 | if keepdim: 37 | z = z.view(-1, *[1] * (len(x.shape) - 1)) 38 | return z 39 | 40 | 41 | def L2_norm(x, keepdim=False): 42 | z = (x**2).view(x.shape[0], -1).sum(-1).sqrt() 43 | if keepdim: 44 | z = z.view(-1, *[1] * (len(x.shape) - 1)) 45 | return z 46 | 47 | 48 | def L0_norm(x): 49 | return (x != 0.0).view(x.shape[0], -1).sum(-1) 50 | 51 | 52 | def makedir(path): 53 | if not os.path.exists(path): 54 | os.makedirs(path) 55 | 56 | 57 | def get_pred(output): 58 | if output.size(-1) == 1: 59 | return (output >= 0).squeeze().float() 60 | return output.argmax(1) 61 | 62 | 63 | def mask_kwargs(kwargs_orig, batch_datapoint_idcs): 64 | kwargs = {} 65 | if "dino_targets" in kwargs_orig: 66 | kwargs = copy.deepcopy(kwargs_orig) 67 | kwargs["masks"] = kwargs_orig["masks"][batch_datapoint_idcs] 68 | kwargs["dino_targets"] = [] 69 | 70 | if batch_datapoint_idcs.ndim == 0: 71 | kwargs["dino_targets"].append( 72 | kwargs_orig["dino_targets"][batch_datapoint_idcs] 73 | ) 74 | else: 75 | for i in batch_datapoint_idcs: 76 | kwargs["dino_targets"].append(kwargs_orig["dino_targets"][i]) 77 | return kwargs 78 | -------------------------------------------------------------------------------- /calculate_background.py: -------------------------------------------------------------------------------- 1 | import os 2 | from PIL import Image 3 | import numpy as np 4 | 5 | num = 0 6 | cumPer = 0 7 | for path, subdirs, files in os.walk( 8 | "/data/kornrapatp/PartImageNet/PartSegmentations/All-class-specific-processed/val" 9 | ): 10 | for name in files: 11 | className = path.split("/")[-1] 12 | if ".png" in name: 13 | img = np.asarray(Image.open(os.path.join(path, name))) 14 | cumPer += np.sum(img == 0) / (img.shape[0] * img.shape[1]) 15 | num += 1 16 | print(cumPer / num) 17 | -------------------------------------------------------------------------------- /panoptic_parts/__init__.py: -------------------------------------------------------------------------------- 1 | from panoptic_parts.utils.format import decode_uids, encode_ids 2 | from panoptic_parts.utils.visualization import uid2color, random_colors 3 | from panoptic_parts.utils.utils import safe_write 4 | # from .panoptic_parts.utils.format import decode_uids, encode_ids 5 | # from .panoptic_parts.utils.visualization import uid2color, random_colors 6 | # from .panoptic_parts.utils.utils import safe_write 7 | 8 | __version__ = '2.0' 9 | -------------------------------------------------------------------------------- /panoptic_parts/specs/dataset_specs/cpp_datasetspec.yaml: -------------------------------------------------------------------------------- 1 | version: '2.0' 2 | comments: 3 | - refer to ppp_datasetspec for now 4 | name: Cityscapes Panoptic Parts 5 | 6 | # scene_class2part_classes: An ordered mapping from scene-level class to part-level classes. 7 | # Refer to ppp_datasetspec for now. 8 | scene_class2part_classes: { 9 | ego vehicle: [], 10 | rectification border: [], 11 | out of roi: [], 12 | static: [], 13 | dynamic: [], 14 | ground: [], 15 | road: [], 16 | sidewalk: [], 17 | parking: [], 18 | rail track: [], 19 | building: [], 20 | wall: [], 21 | fence: [], 22 | guard rail: [], 23 | bridge: [], 24 | tunnel: [], 25 | pole: [], 26 | polegroup: [], 27 | traffic light: [], 28 | traffic sign: [], 29 | vegetation: [], 30 | terrain: [], 31 | sky: [], 32 | person: [torso, head, arm, leg], 33 | rider: [torso, head, arm, leg], 34 | car: [window, wheel, light, license plate, chassis], 35 | truck: [window, wheel, light, license plate, chassis], 36 | bus: [window, wheel, light, license plate, chassis], 37 | caravan: [], 38 | trailer: [], 39 | train: [], 40 | motorcycle: [], 41 | bicycle: [], 42 | license plate: [], 43 | } 44 | 45 | # Refer to ppp_datasetspec for now. 46 | scene_classes_with_instances: [ 47 | person, rider, car, truck, bus, caravan, trailer, train, motorcycle, bicycle 48 | ] 49 | 50 | # Refer to ppp_datasetspec for now. 51 | scene_class2color: { 52 | ego vehicle: [0, 0, 0], 53 | rectification border: [0, 0, 0], 54 | out of roi: [0, 0, 0], 55 | static: [0, 0, 0], 56 | dynamic: [111, 74, 0], 57 | ground: [81, 0, 81], 58 | road: [128, 64, 128], 59 | sidewalk: [244, 35, 232], 60 | parking: [250, 170, 160], 61 | rail track: [230, 150, 140], 62 | building: [70, 70, 70], 63 | wall: [102, 102, 156], 64 | fence: [190, 153, 153], 65 | guard rail: [180, 165, 180], 66 | bridge: [150, 100, 100], 67 | tunnel: [150, 120, 90], 68 | pole: [153, 153, 153], 69 | polegroup: [153, 153, 153], 70 | traffic light: [250, 170, 30], 71 | traffic sign: [220, 220, 0], 72 | vegetation: [107, 142, 35], 73 | terrain: [152, 251, 152], 74 | sky: [70, 130, 180], 75 | person: [220, 20, 60], 76 | rider: [255, 0, 0], 77 | car: [0, 0, 142], 78 | truck: [0, 0, 70], 79 | bus: [0, 60, 100], 80 | caravan: [0, 0, 90], 81 | trailer: [0, 0, 110], 82 | train: [0, 80, 100], 83 | motorcycle: [0, 0, 230], 84 | bicycle: [119, 11, 32], 85 | license plate: [0, 0, 142], 86 | } 87 | -------------------------------------------------------------------------------- /panoptic_parts/specs/eval_specs/ppq_cpp_19_23_cvpr21_default_evalspec.yaml: -------------------------------------------------------------------------------- 1 | version: 2.0 2 | comments: 3 | First version containing all information I (Daan) think is necessary for merging to panoptic and part-aware panoptic (and it should also be usable for PartPQ evaluation) 4 | 5 | dataset_spec_path: panoptic_parts/specs/dataset_specs/cpp_datasetspec.yaml 6 | 7 | # To be used for evaluation 8 | ignore_label: 255 9 | 10 | # To be used for evaluation 11 | dataset_sid2eval_sid: { 12 | # evaluated 13 | 7: 7, 8: 8, 11: 11, 12: 12, 13: 13, 14 | 17: 17, 19: 19, 20: 20, 21: 21, 22: 22, 15 | 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 16 | 28: 28, 31: 31, 32: 32, 33: 33, 17 | # ignored 18 | 0: IGNORED, 1: IGNORED, 2: IGNORED, 3: IGNORED, 4: IGNORED, 5: IGNORED, 6: IGNORED, 9: IGNORED, 10: IGNORED, 14: IGNORED, 15: IGNORED, 16: IGNORED, 18: IGNORED, 29: IGNORED, 30: IGNORED, 19 | # default 20 | DEFAULT: IGNORED 21 | } 22 | 23 | # To be used for evaluation 24 | dataset_sid_pid2eval_sid_pid: { 25 | # evaluated 26 | 24_01: 24_01, 24_02: 24_02, 24_03: 24_03, 24_04: 24_04, 27 | 25_01: 25_01, 25_02: 25_02, 25_03: 25_03, 25_04: 25_04, 28 | 26_01: 26_01, 26_02: 26_02, 26_03: 26_03, 26_04: 26_04, 26_05: 26_05, 29 | 27_01: 27_01, 27_02: 27_02, 27_03: 27_03, 27_04: 27_04, 27_05: 27_05, 30 | 28_01: 28_01, 28_02: 28_02, 28_03: 28_03, 28_04: 28_04, 28_05: 28_05, 31 | # ignored 32 | 24: IGNORED, 25: IGNORED, 26: IGNORED, 27: IGNORED, 28: IGNORED, 33 | 0: IGNORED, 1: IGNORED, 2: IGNORED, 3: IGNORED, 4: IGNORED, 5: IGNORED, 6: IGNORED, 9: IGNORED, 10: IGNORED, 14: IGNORED, 15: IGNORED, 16: IGNORED, 18: IGNORED, 29: IGNORED, 30: IGNORED, 34 | # default 35 | DEFAULT: IGNORED 36 | } 37 | 38 | # Used for merging and evaluation 39 | eval_sid_things: [24, 25, 26, 27, 28, 31, 32, 33] 40 | eval_sid_stuff: [7, 8, 11, 12, 13, 17, 19, 20, 21, 22, 23] 41 | eval_sid_parts: [24, 25, 26, 27, 28] 42 | eval_sid_no_parts: [7, 8, 11, 12, 13, 17, 19, 20, 21, 22, 23, 31, 32, 33] 43 | 44 | # Used for merging panoptic & parts 45 | eval_sid_pid2eval_pid_flat: { 46 | 24_01: 1, 24_02: 2, 24_03: 3, 24_04: 4, 47 | 25_01: 5, 25_02: 6, 25_03: 7, 25_04: 8, 48 | 26_01: 9, 26_02: 10, 26_03: 11, 26_04: 12, 26_05: 13, 49 | 27_01: 14, 27_02: 15, 27_03: 16, 27_04: 17, 27_05: 18, 50 | 28_01: 19, 28_02: 20, 28_03: 21, 28_04: 22, 28_05: 23, 51 | } 52 | 53 | 54 | # Names for all labels that are to be evaluated 55 | eval_sid2scene_label: { 56 | 7: road, 8: sidewalk, 11: building, 12: wall, 57 | 13: fence, 17: pole, 19: traffic light, 20: traffic sign, 58 | 21: vegetation, 22: terrain, 23: sky, 24: person, 59 | 25: rider, 26: car, 27: truck, 28: bus, 60 | 31: train, 32: motorcycle, 33: bicycle 61 | } 62 | 63 | eval_pid_flat2scene_part_label: { 64 | 1: person-torso, 2: person-head, 3: person-arms, 4: person-legs, 65 | 5: rider-torso, 6: rider-head, 7: rider-arms, 8: rider-legs, 66 | 9: car-windows, 10: car-wheels, 11: car-lights, 12: car-license_plate, 13: car-chassis, 67 | 14: truck-windows, 15: truck-wheels, 16: truck-lights, 17: truck-license_plate, 18: truck-chassis, 68 | 19: bus-windows, 20: bus-wheels, 21: bus-lights, 22: bus-license_plate, 23: bus-chassis, 69 | } 70 | -------------------------------------------------------------------------------- /panoptic_parts/specs/eval_specs/ppq_cpp_19_23_cvpr21_grouped_evalspec.yaml: -------------------------------------------------------------------------------- 1 | version: 1.0 2 | comments: 3 | First version containing all information I (Daan) think is necessary for merging to panoptic and part-aware panoptic (and it should also be usable for PartPQ evaluation) 4 | 5 | dataset_spec_path: panoptic_parts/specs/dataset_specs/cpp_datasetspec.yaml 6 | 7 | # To be used for evaluation 8 | ignore_label: 255 9 | 10 | # To be used for evaluation 11 | dataset_sid2eval_sid: { 12 | # evaluated 13 | 7: 7, 8: 8, 11: 11, 12: 12, 13: 13, 14 | 17: 17, 19: 19, 20: 20, 21: 21, 22: 22, 15 | 23: 23, 24: 24, 25: 25, 26: 26, 27: 27, 16 | 28: 28, 31: 31, 32: 32, 33: 33, 17 | # ignored 18 | 0: IGNORED, 1: IGNORED, 2: IGNORED, 3: IGNORED, 4: IGNORED, 5: IGNORED, 6: IGNORED, 9: IGNORED, 10: IGNORED, 14: IGNORED, 15: IGNORED, 16: IGNORED, 18: IGNORED, 29: IGNORED, 30: IGNORED, 19 | # default 20 | DEFAULT: IGNORED 21 | } 22 | 23 | # To be used for evaluation 24 | dataset_sid_pid2eval_sid_pid: { 25 | # evaluated 26 | 24_01: 24_01, 24_02: 24_02, 24_03: 24_03, 24_04: 24_04, 27 | 25_01: 25_01, 25_02: 25_02, 25_03: 25_03, 25_04: 25_04, 28 | 26_01: 26_01, 26_02: 26_02, 26_03: 26_03, 26_04: 26_04, 26_05: 26_05, 29 | 27_01: 27_01, 27_02: 27_02, 27_03: 27_03, 27_04: 27_04, 27_05: 27_05, 30 | 28_01: 28_01, 28_02: 28_02, 28_03: 28_03, 28_04: 28_04, 28_05: 28_05, 31 | # ignored 32 | 24: IGNORED, 25: IGNORED, 26: IGNORED, 27: IGNORED, 28: IGNORED, 33 | 0: IGNORED, 1: IGNORED, 2: IGNORED, 3: IGNORED, 4: IGNORED, 5: IGNORED, 6: IGNORED, 9: IGNORED, 10: IGNORED, 14: IGNORED, 15: IGNORED, 16: IGNORED, 18: IGNORED, 29: IGNORED, 30: IGNORED, 34 | # default 35 | DEFAULT: IGNORED 36 | } 37 | 38 | # Used for merging and evaluation 39 | eval_sid_things: [24, 25, 26, 27, 28, 31, 32, 33] 40 | eval_sid_stuff: [7, 8, 11, 12, 13, 17, 19, 20, 21, 22, 23] 41 | eval_sid_parts: [24, 25, 26, 27, 28] 42 | eval_sid_no_parts: [7, 8, 11, 12, 13, 17, 19, 20, 21, 22, 23, 31, 32, 33] 43 | 44 | # Used for merging panoptic & parts 45 | eval_sid_pid2eval_pid_flat: { 46 | 24_01: 1, 24_02: 2, 24_03: 3, 24_04: 4, 47 | 25_01: 1, 25_02: 2, 25_03: 3, 25_04: 4, 48 | 26_01: 5, 26_02: 6, 26_03: 7, 26_04: 8, 26_05: 9, 49 | 27_01: 5, 27_02: 6, 27_03: 7, 27_04: 8, 27_05: 9, 50 | 28_01: 5, 28_02: 6, 28_03: 7, 28_04: 8, 28_05: 9, 51 | } 52 | 53 | # Names for all labels that are to be evaluated 54 | eval_sid2scene_label: { 55 | 7: road, 8: sidewalk, 11: building, 12: wall, 56 | 13: fence, 17: pole, 19: traffic light, 20: traffic sign, 57 | 21: vegetation, 22: terrain, 23: sky, 24: person, 58 | 25: rider, 26: car, 27: truck, 28: bus, 59 | 31: train, 32: motorcycle, 33: bicycle 60 | } 61 | 62 | eval_pid_flat2scene_part_label: { 63 | 1: human-torso, 2: human-head, 3: human-arms, 4: human-legs, 64 | 5: vehicle-windows, 6: vehicle-wheels, 7: vehicle-lights, 8: vehicle-license_plate, 9: vehicle-chassis, 65 | } 66 | -------------------------------------------------------------------------------- /panoptic_parts/utils/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chawins/adv-part-model/218c2a45906c751be8d8845bd4183b5bfb8242da/panoptic_parts/utils/__init__.py -------------------------------------------------------------------------------- /panoptic_parts/utils/internal/convert_annotations_v1_to_v2.py: -------------------------------------------------------------------------------- 1 | import glob 2 | import os 3 | import os.path as op 4 | import sys 5 | from tqdm import tqdm 6 | 7 | import numpy as np 8 | from PIL import Image 9 | 10 | from panoptic_parts.utils.format import decode_uids, encode_ids 11 | 12 | 13 | def convert(): 14 | basepath = 'pascal_panoptic_parts/releases/20201704/pascal_panoptic_parts_v1' 15 | 16 | filepaths = glob.glob(op.join(basepath, 'training/*.tif')) + glob.glob(op.join(basepath, 'validation/*.tif')) 17 | 18 | for fp in tqdm(filepaths): 19 | uids = np.asarray(Image.open(fp), dtype=np.int32) 20 | # transformation 1 (tvmonitor-unlabeled becomes tvmonitor-frame): {20_XXX, 20_XXX_00} -> 20_XXX_02 21 | sids, iids, pids, sids_iids, sids_pids = decode_uids(uids, return_sids_iids=True, return_sids_pids=True) 22 | pids = np.where(np.logical_and(iids >= 0, 23 | np.logical_or(np.equal(sids_pids, 20), np.equal(sids_pids, 20_00))), 24 | 2, 25 | pids) 26 | uids = encode_ids(sids, iids, pids) 27 | # transformation 1 (remove 00): XX_XXX_00 -> XX_XXX 28 | _, _, pids, sids_iids = decode_uids(uids, return_sids_iids=True) 29 | uids = np.where(np.logical_and(uids >= 1_000_00, np.equal(pids, 0)), 30 | sids_iids, 31 | uids) 32 | 33 | path_new = fp.replace('20201704/pascal_panoptic_parts_v1', '20210503/pascal_panoptic_parts_v2') 34 | assert not op.exists(path_new), f'path {path_new} exists.' 35 | os.makedirs(op.dirname(path_new), exist_ok=True) 36 | Image.fromarray(uids, mode='I').save(path_new, format='TIFF', compression='tiff_lzw') 37 | 38 | 39 | def validate(): 40 | basepath_v1 = 'pascal_panoptic_parts/releases/20201704/pascal_panoptic_parts_v1' 41 | basepath_v2 = 'pascal_panoptic_parts/releases/20210503/pascal_panoptic_parts_v2' 42 | 43 | filepaths_v1 = glob.glob(op.join(basepath_v1, 'training/*.tif')) + glob.glob(op.join(basepath_v1, 'validation/*.tif')) 44 | filepaths_v2 = [fp.replace('20201704/pascal_panoptic_parts_v1', '20210503/pascal_panoptic_parts_v2') for fp in filepaths_v1] 45 | 46 | for i, (f1, f2) in enumerate(zip(filepaths_v1, filepaths_v2)): 47 | l1 = np.asanyarray(Image.open(f1), dtype=np.int32) 48 | l2 = np.asanyarray(Image.open(f2), dtype=np.int32) 49 | # if there are differences print the unique tuples with (uid_l1, uid_l2) corresponding 50 | # to the same spatial position 51 | cond = l1 != l2 52 | if np.any(cond): 53 | uids_tuples = np.unique(np.stack([l1[cond], l2[cond]]), axis=1) 54 | print(i, *(uids_tuples[:, j] for j in range(uids_tuples.shape[1]))) 55 | else: 56 | print('No diff.') 57 | 58 | 59 | if __name__ == '__main__': 60 | # convert() 61 | validate() 62 | -------------------------------------------------------------------------------- /panoptic_parts/utils/internal/populate_ppp_official_evalspec.py: -------------------------------------------------------------------------------- 1 | from ruamel.yaml import YAML 2 | 3 | from panoptic_parts.specs.dataset_spec import DatasetSpec 4 | 5 | 6 | with open('ppp_20_58_iou_evalspec.yaml') as fd: 7 | gspec = YAML().load(fd) 8 | 9 | dspec = DatasetSpec(gspec['dataset_spec_path']) 10 | 11 | with open('ppq_ppp_59_57_evalspec.yaml') as fd: 12 | espec = YAML().load(fd) 13 | 14 | 15 | 16 | # dataset_sid_pid2eval_sid_pid 17 | ################################################################################################### 18 | part_groupings = gspec['part_groupings'] 19 | dataset_sid_pid2eval_sid_pid = dict() 20 | for sid_pid, (scene_class, part_class) in dspec.sid_pid2scene_class_part_class.items(): 21 | if sid_pid == 0 or scene_class not in part_groupings.keys(): 22 | continue 23 | sid = sid_pid // 100 24 | pid_new = None 25 | # find the part_class position in the part_groupings dict 26 | for pid_new_cand, (part_class_new, part_classes_old) in enumerate(part_groupings[scene_class].items(), start=1): 27 | for part_class_old in part_classes_old: 28 | if part_class_old == part_class: 29 | pid_new = pid_new_cand 30 | break 31 | else: # ie inner loop DOES NOT break, continue mid loop 32 | continue 33 | break # if inner loop breaks, then break mid loop 34 | else: # ie mid loop DOES NOT break, continue outer loop 35 | continue 36 | dataset_sid_pid2eval_sid_pid[sid_pid] = sid * 100 + pid_new 37 | 38 | # sanity check 39 | esd2epf = espec['eval_sid_pid2eval_pid_flat'] 40 | assert all(v in esd2epf.keys() for v in dataset_sid_pid2eval_sid_pid.values()) 41 | 42 | # print in a friendly copy-paste way to yaml 43 | sid_prev = 0 44 | for k, v in dataset_sid_pid2eval_sid_pid.items(): 45 | sid_cur = k // 100 46 | if sid_cur > sid_prev: 47 | sid_prev = sid_cur 48 | print('\n ', end='') 49 | print('{}_{:02d}'.format(*divmod(k, 100)) + ': ' + '{}_{:02d}'.format(*divmod(v, 100)) + ',', end=' ') 50 | ################################################################################################### 51 | 52 | # eval_sid2scene_label 53 | ################################################################################################### 54 | # eval_sid2dataset_sid = espec['eval_sid2scene_label'] 55 | # eval_sid2scene_label = {es: dspec.scene_class_from_sid(ds) for es, ds in eval_sid2dataset_sid.items()} 56 | ################################################################################################### 57 | 58 | # eval_pid_flat2scene_part_label 59 | ################################################################################################### 60 | eval_pid_flat = espec['eval_pid_flat2scene_part_label'].keys() 61 | eval_pid_flat2eval_sid_pid = {v: k for k, v in espec['eval_sid_pid2eval_pid_flat'].items()} 62 | eval_pid_flat2eval_sid_pid[0] = 0 63 | 64 | part_groupings['UNLABELED'] = {'UNLABELED': ['UNLABELED']} 65 | 66 | eval_pid_flat2scene_part_label = dict() 67 | for k in eval_pid_flat: 68 | eval_sid_pid = eval_pid_flat2eval_sid_pid[k] 69 | eval_sid, eval_pid = divmod(eval_sid_pid, 100) 70 | scene_class = dspec.scene_class_from_sid(eval_sid) 71 | part_class_new2part_classes_old = {'UNLABELED': ['UNLABELED']} 72 | part_class_new2part_classes_old.update(part_groupings[scene_class]) 73 | part_class = list(part_class_new2part_classes_old.keys())[eval_pid] 74 | eval_pid_flat2scene_part_label[k] = f'{scene_class}-{part_class}' 75 | ################################################################################################### 76 | breakpoint() 77 | -------------------------------------------------------------------------------- /part_model/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chawins/adv-part-model/218c2a45906c751be8d8845bd4183b5bfb8242da/part_model/__init__.py -------------------------------------------------------------------------------- /part_model/attack/_hsja/__init__.py: -------------------------------------------------------------------------------- 1 | from .hsja import HopSkipJumpAttack 2 | -------------------------------------------------------------------------------- /part_model/attack/_hsja/hsja.py: -------------------------------------------------------------------------------- 1 | import foolbox 2 | import numpy as np 3 | from foolbox import PyTorchModel 4 | from foolbox.attacks import PGD 5 | 6 | from ..base import AttackModule 7 | from .hop_skip_jump import HopSkipJump 8 | 9 | 10 | class HopSkipJumpAttack(AttackModule): 11 | 12 | def __init__(self, attack_config, core_model, loss_fn, norm, eps, **kwargs): 13 | super().__init__(attack_config, core_model, loss_fn, norm, eps, **kwargs) 14 | self.model = PyTorchModel(core_model, bounds=(0, 1), preprocessing=None) 15 | # (1) gradient_eval_steps is min([initial_gradient_eval_steps * 16 | # math.sqrt(step + 1), max_gradient_eval_steps]) (L149) 17 | # (2) step size search also uses a few more queries. Geometric search 18 | # has while loop and can't be pre-determined (L166) 19 | # (3) binary search (L184) also has while loop 20 | 21 | # Approximate (upper bound of) `steps` 22 | # \sum_{i=1}^{steps} (sqrt(i) * init_grad_steps) <= max_iter 23 | # max_iter = attack_config['hsj_max_iter'] - 51 # 51 for init attack 24 | max_iter = 20000 25 | hsj_init_grad_steps = 100 26 | iters = np.sqrt(np.arange(100)) * hsj_init_grad_steps 27 | iters = np.cumsum(iters) 28 | steps = np.where(iters <= max_iter)[0][-1] 29 | 30 | self.attack = HopSkipJump( 31 | max_queries=max_iter, 32 | steps=steps, 33 | initial_gradient_eval_steps=hsj_init_grad_steps, 34 | max_gradient_eval_steps=10000, 35 | gamma=1.0, 36 | constraint='linf', 37 | verbose=True, 38 | # init_attack=PGD(rel_stepsize=0.0333, abs_stepsize=None, steps=40, random_start=True), 39 | ) 40 | 41 | def forward(self, x, y): 42 | mode = self._core_model.training 43 | self._core_model.eval() 44 | criteria = foolbox.criteria.Misclassification(y) 45 | x_adv = self.attack.run(self.model, x, criterion=criteria) 46 | delta = x_adv - x 47 | delta.clamp_(- self._eps, self._eps) 48 | x_adv = x + delta 49 | x_adv.clamp_(0, 1) 50 | self._core_model.train(mode) 51 | return x_adv 52 | -------------------------------------------------------------------------------- /part_model/attack/auto.py: -------------------------------------------------------------------------------- 1 | """Wrapped AutoAttack.""" 2 | 3 | from __future__ import annotations 4 | 5 | from typing import Any, Callable 6 | 7 | import torch 8 | from torch import nn 9 | 10 | from autoattack_modified import AutoAttack 11 | from part_model.attack.base import AttackModule 12 | 13 | 14 | class AutoAttackModule(AttackModule): 15 | """AutoAttack.""" 16 | 17 | def __init__( 18 | self, 19 | attack_config: dict[str, Any], 20 | core_model: nn.Module, 21 | loss_fn: Callable[..., torch.Tensor], 22 | norm: str = "Linf", 23 | eps: float = 8 / 255, 24 | num_classes: int = 10, 25 | **kwargs, 26 | ): 27 | """Initialize AutoAttackModule. For args, see AttackModule.""" 28 | super().__init__( 29 | core_model, 30 | loss_fn, 31 | norm, 32 | eps, 33 | **kwargs, 34 | ) 35 | _ = attack_config # Unused 36 | self._num_classes: int = num_classes 37 | self._adversary = AutoAttack( 38 | self._core_model, 39 | norm=self._norm, 40 | eps=self._eps, 41 | version="standard", 42 | verbose=self._verbose, 43 | num_classes=self._num_classes, 44 | ) 45 | 46 | def forward( 47 | self, inputs: torch.Tensor, targets: torch.Tensor, **kwargs 48 | ) -> torch.Tensor: 49 | """Run AutoAttack.""" 50 | _ = kwargs # Unused 51 | mode = self._core_model.training 52 | self._core_model.eval() 53 | x_adv = self._adversary.run_standard_evaluation( 54 | inputs, targets, bs=inputs.size(0), **kwargs 55 | ) 56 | self._core_model.train(mode) 57 | return x_adv 58 | -------------------------------------------------------------------------------- /part_model/attack/auto_square.py: -------------------------------------------------------------------------------- 1 | from autoattack_modified import AutoAttack 2 | 3 | from part_model.attack.base import AttackModule 4 | 5 | 6 | class AutoAttackSPModule(AttackModule): 7 | 8 | def __init__(self, attack_config, core_model, loss_fn, norm, eps, 9 | num_classes=10, **kwargs): 10 | super().__init__(core_model, loss_fn, norm, eps, **kwargs) 11 | self.num_classes = num_classes 12 | 13 | def forward(self, x, y): 14 | mode = self._core_model.training 15 | self._core_model.eval() 16 | # TODO: Try to init adversary only once 17 | adversary = AutoAttack( 18 | self._core_model, 19 | norm=self._norm, 20 | eps=self._eps, 21 | version='standard-square+', 22 | verbose=self._verbose, 23 | num_classes=self.num_classes 24 | ) 25 | x_adv = adversary.run_standard_evaluation(x, y, bs=x.size(0)) 26 | self._core_model.train(mode) 27 | return x_adv 28 | -------------------------------------------------------------------------------- /part_model/attack/base.py: -------------------------------------------------------------------------------- 1 | """Base AttackModule class.""" 2 | 3 | from __future__ import annotations 4 | 5 | from typing import Any, Callable 6 | 7 | import torch 8 | from torch import nn 9 | 10 | 11 | class AttackModule(nn.Module): 12 | """Base class of all attack modules.""" 13 | 14 | def __init__( 15 | self, 16 | core_model: nn.Module | None = None, 17 | loss_fn: Callable[..., torch.Tensor] | None = None, 18 | norm: str | None = "Linf", 19 | eps: float = 8 / 255, 20 | verbose: bool = False, 21 | forward_args: dict[str, Any] | None = None, 22 | **kwargs, 23 | ) -> None: 24 | """Initialize AttackModule. 25 | 26 | Args: 27 | core_model: Target model to attack. 28 | loss_fn: Loss function to optimize. 29 | norm: Lp-norm of perturbation (options: "L2", "Linf"). 30 | eps: Epsilon or maximum perturbation norm. 31 | verbose: If True, progress and messages will be printed. 32 | forward_args: Additional keyword arguments to pass with any call to 33 | forward() of core_model. 34 | """ 35 | super().__init__() 36 | _ = kwargs # Unused 37 | if norm not in (None, "L2", "Linf"): 38 | raise NotImplementedError( 39 | f"Norm {norm} is not implemented! Only supports L2 and Linf." 40 | ) 41 | self._core_model: nn.Module | None = core_model 42 | self._loss_fn: Callable[..., torch.Tensor] | None = loss_fn 43 | self._eps: float = eps 44 | self._norm: str = norm 45 | self._verbose: bool = verbose 46 | self._forward_args: dict[str, Any] = ( 47 | forward_args if forward_args is not None else {} 48 | ) 49 | # Public attributes 50 | self.use_mask: bool = False 51 | self.dual_losses: bool = False 52 | 53 | def forward(self, *args, **kwargs) -> torch.Tensor: 54 | """Generates and returns adversarial examples.""" 55 | raise NotImplementedError("Implement forward()!") 56 | -------------------------------------------------------------------------------- /part_model/attack/corruption_benchmark.py: -------------------------------------------------------------------------------- 1 | 2 | import numpy as np 3 | import torch 4 | from imagecorruptions import corrupt 5 | 6 | from .base import AttackModule 7 | 8 | 9 | class CorruptionBenchmarkModule(AttackModule): 10 | 11 | def __init__(self, attack_config, core_model, loss_fn, norm, eps, 12 | corruption_number, **kwargs): 13 | super(CorruptionBenchmarkModule, self).__init__( 14 | attack_config, core_model, loss_fn, norm, eps, **kwargs) 15 | self.corruption_number = corruption_number 16 | 17 | def forward(self, x, y): 18 | x = x.clone().detach() 19 | batch_size = x.shape[0] 20 | device = torch.device(f'cuda:{x.get_device()}') 21 | x = x.permute((0, 2, 3, 1)) * 255 22 | x = x.cpu().numpy().astype('uint8') 23 | cor_x = np.empty((batch_size, 5, x.shape[1], x.shape[2], x.shape[3])) 24 | for i in range(batch_size): 25 | cor_x[i] = np.asarray([corrupt(x[i], corruption_number=self.corruption_number, severity=j) 26 | for j in range(1, 6)]) 27 | cor_x = torch.from_numpy(cor_x).permute(0, 1, 4, 2, 3).to(device).float() / 255 28 | cor_x = torch.flatten(cor_x, start_dim=0, end_dim=1) 29 | 30 | return cor_x 31 | -------------------------------------------------------------------------------- /part_model/attack/hsj.py: -------------------------------------------------------------------------------- 1 | import foolbox 2 | from foolbox import PyTorchModel 3 | from foolbox.attacks import HopSkipJumpAttack 4 | 5 | from .base import AttackModule 6 | 7 | 8 | class HopSkipJump(AttackModule): 9 | 10 | def __init__(self, attack_config, core_model, loss_fn, norm, eps, 11 | num_classes=10, **kwargs): 12 | super().__init__(attack_config, core_model, loss_fn, norm, eps, **kwargs) 13 | self.model = PyTorchModel(core_model, bounds=(0, 1), preprocessing=None) 14 | # max_iter = 5000 15 | hsj_init_grad_steps = 100 16 | hsj_max_grad_steps = 10000 17 | hsj_gamma = 1 18 | steps = 32 19 | 20 | self.attack = HopSkipJumpAttack( 21 | steps=steps, 22 | initial_gradient_eval_steps=hsj_init_grad_steps, 23 | max_gradient_eval_steps=hsj_max_grad_steps, 24 | gamma=hsj_gamma, 25 | constraint='linf', 26 | stepsize_search='geometric_progression', 27 | ) 28 | 29 | def forward(self, x, y, target=None): 30 | if target is None: 31 | criteria = foolbox.criteria.Misclassification(y) 32 | starting_points = None 33 | else: 34 | criteria = foolbox.criteria.TargetedMisclassification(target[1]) 35 | starting_points = target[0] 36 | x_adv = self.attack.run(self.model, x, 37 | criterion=criteria, 38 | starting_points=starting_points) 39 | delta = x_adv - x 40 | print(delta.view(x.size(0), -1).max(1)) 41 | delta.clamp_(- self._eps, self._eps) 42 | x_adv = x + delta 43 | x_adv.clamp_(0, 1) 44 | return x_adv 45 | -------------------------------------------------------------------------------- /part_model/attack/masked_pgd.py: -------------------------------------------------------------------------------- 1 | from __future__ import annotations 2 | 3 | import torch 4 | 5 | from part_model.attack.pgd import PGDAttack 6 | 7 | EPS = 1e-6 8 | 9 | 10 | class MaskedPGDAttack(PGDAttack): 11 | def __init__(self, attack_config, core_model, loss_fn, norm, eps, **kwargs): 12 | super().__init__( 13 | attack_config, core_model, loss_fn, norm, eps, **kwargs 14 | ) 15 | self.use_mask = True 16 | 17 | @torch.no_grad() 18 | def _init_adv( 19 | self, inputs: torch.Tensor, mask: torch.Tensor 20 | ) -> torch.Tensor: 21 | """Initialize adversarial inputs.""" 22 | x_adv = inputs.clone() 23 | if self._norm == "L2": 24 | x_adv += self._project_l2(torch.randn_like(x_adv), self._eps) * mask 25 | else: 26 | x_adv += ( 27 | torch.zeros_like(x_adv).uniform_(-self._eps, self._eps) * mask 28 | ) 29 | x_adv.clamp_(0, 1) 30 | return x_adv 31 | 32 | def _forward(self, x, y, mask=None): 33 | # TODO(chawins@): clean up 34 | mode = self._core_model.training 35 | self._core_model.eval() 36 | 37 | # Initialize worst-case inputs 38 | x_adv_worst = x.clone().detach() 39 | worst_losses = torch.zeros(len(x), 1, 1, 1, device=x.device) 40 | mask = (mask > 0).float().unsqueeze(1) # TODO 41 | 42 | # Repeat PGD for specified number of restarts 43 | for _ in range(self.num_restarts): 44 | x_adv = self._init_adv(x, mask) 45 | 46 | # Run PGD on inputs for specified number of steps 47 | for _ in range(self.num_steps): 48 | x_adv.requires_grad_() 49 | 50 | # Compute logits, loss, gradients 51 | with torch.enable_grad(): 52 | logits = self._core_model(x_adv, **self.forward_args) 53 | loss = self._loss_fn(logits, y).mean() 54 | grads = torch.autograd.grad(loss, x_adv)[0].detach() 55 | 56 | with torch.no_grad(): 57 | # Perform gradient update, project to norm ball 58 | x_adv = ( 59 | x_adv.detach() 60 | + self.step_size * torch.sign(grads) * mask 61 | ) 62 | x_adv = torch.min( 63 | torch.max(x_adv, x - self._eps), x + self._eps 64 | ) 65 | # Clip perturbed inputs to image domain 66 | x_adv = torch.clamp(x_adv, 0, 1) 67 | 68 | if self.num_restarts == 1: 69 | x_adv_worst = x_adv 70 | else: 71 | # Update worst-case inputs with itemized final losses 72 | fin_losses = self._loss_fn(self._core_model(x_adv), y).reshape( 73 | worst_losses.shape 74 | ) 75 | up_mask = (fin_losses >= worst_losses).float() 76 | x_adv_worst = x_adv * up_mask + x_adv_worst * (1 - up_mask) 77 | worst_losses = fin_losses * up_mask + worst_losses * ( 78 | 1 - up_mask 79 | ) 80 | 81 | # Return worst-case perturbed input logits 82 | self._core_model.train(mode) 83 | return x_adv_worst.detach() 84 | -------------------------------------------------------------------------------- /part_model/attack/none.py: -------------------------------------------------------------------------------- 1 | """No attack (identity function).""" 2 | 3 | import torch 4 | 5 | from part_model.attack.base import AttackModule 6 | 7 | 8 | class NoAttack(AttackModule): 9 | """No attack.""" 10 | 11 | def forward( 12 | self, inputs: torch.Tensor, targets: torch.Tensor, **kwargs 13 | ) -> torch.Tensor: 14 | """Returns original inputs.""" 15 | _ = targets, kwargs # Unused 16 | return inputs 17 | -------------------------------------------------------------------------------- /part_model/attack/rays/__init__.py: -------------------------------------------------------------------------------- 1 | from .rays import RayS 2 | -------------------------------------------------------------------------------- /part_model/attack/rays/general_torch_model.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | import torch.nn as nn 4 | 5 | 6 | class GeneralTorchModel(nn.Module): 7 | def __init__(self, model, n_class=10, im_mean=None, im_std=None): 8 | super(GeneralTorchModel, self).__init__() 9 | self.model = model 10 | self.model.eval() 11 | self.num_queries = 0 12 | self.im_mean = im_mean 13 | self.im_std = im_std 14 | self.n_class = n_class 15 | 16 | def forward(self, image): 17 | if len(image.size()) != 4: 18 | image = image.unsqueeze(0) 19 | image = self.preprocess(image) 20 | logits = self.model(image) 21 | return logits 22 | 23 | def preprocess(self, image): 24 | if isinstance(image, np.ndarray): 25 | processed = torch.from_numpy(image).type(torch.FloatTensor) 26 | else: 27 | processed = image 28 | 29 | if self.im_mean is not None and self.im_std is not None: 30 | im_mean = torch.tensor(self.im_mean).cuda().view(1, processed.shape[1], 1, 1).repeat( 31 | processed.shape[0], 1, 1, 1) 32 | im_std = torch.tensor(self.im_std).cuda().view(1, processed.shape[1], 1, 1).repeat( 33 | processed.shape[0], 1, 1, 1) 34 | processed = (processed - im_mean) / im_std 35 | return processed 36 | 37 | def predict_prob(self, image): 38 | with torch.no_grad(): 39 | if len(image.size()) != 4: 40 | image = image.unsqueeze(0) 41 | image = self.preprocess(image) 42 | logits = self.model(image) 43 | self.num_queries += image.size(0) 44 | return logits 45 | 46 | def predict_label(self, image): 47 | logits = self.predict_prob(image) 48 | _, predict = torch.max(logits, 1) 49 | return predict 50 | -------------------------------------------------------------------------------- /part_model/attack/rays/pgbar.py: -------------------------------------------------------------------------------- 1 | import os 2 | import sys 3 | import time 4 | 5 | 6 | # Comment out due to error on SLURM 7 | # _, term_width = os.popen('stty size', 'r').read().split() 8 | # term_width = int(term_width) 9 | # TOTAL_BAR_LENGTH = 65. 10 | # last_time = time.time() 11 | # begin_time = last_time 12 | 13 | 14 | def progress_bar(current, total, msg=None): 15 | global last_time, begin_time 16 | if current == 0: 17 | begin_time = time.time() # Reset for new bar. 18 | 19 | cur_len = int(TOTAL_BAR_LENGTH * current / total) 20 | rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1 21 | 22 | sys.stdout.write(' [') 23 | for i in range(cur_len): 24 | sys.stdout.write('=') 25 | sys.stdout.write('>') 26 | for i in range(rest_len): 27 | sys.stdout.write('.') 28 | sys.stdout.write(']') 29 | 30 | cur_time = time.time() 31 | step_time = cur_time - last_time 32 | last_time = cur_time 33 | tot_time = cur_time - begin_time 34 | 35 | L = [] 36 | L.append(' Step: %s' % format_time(step_time)) 37 | L.append(' | Tot: %s' % format_time(tot_time)) 38 | if msg: 39 | L.append(' | ' + msg) 40 | 41 | msg = ''.join(L) 42 | sys.stdout.write(msg) 43 | for i in range(term_width - int(TOTAL_BAR_LENGTH) - len(msg) - 3): 44 | sys.stdout.write(' ') 45 | 46 | # Go back to the center of the bar. 47 | for i in range(term_width - int(TOTAL_BAR_LENGTH / 2) + 2): 48 | sys.stdout.write('\b') 49 | sys.stdout.write(' %d/%d ' % (current + 1, total)) 50 | 51 | if current < total - 1: 52 | sys.stdout.write('\r') 53 | else: 54 | sys.stdout.write('\n') 55 | sys.stdout.flush() 56 | 57 | 58 | def format_time(seconds): 59 | days = int(seconds / 3600 / 24) 60 | seconds = seconds - days * 3600 * 24 61 | hours = int(seconds / 3600) 62 | seconds = seconds - hours * 3600 63 | minutes = int(seconds / 60) 64 | seconds = seconds - minutes * 60 65 | secondsf = int(seconds) 66 | seconds = seconds - secondsf 67 | millis = int(seconds * 1000) 68 | 69 | f = '' 70 | i = 1 71 | if days > 0: 72 | f += str(days) + 'D' 73 | i += 1 74 | if hours > 0 and i <= 2: 75 | f += str(hours) + 'h' 76 | i += 1 77 | if minutes > 0 and i <= 2: 78 | f += str(minutes) + 'm' 79 | i += 1 80 | if secondsf > 0 and i <= 2: 81 | f += str(secondsf) + 's' 82 | i += 1 83 | if millis > 0 and i <= 2: 84 | f += str(millis) + 'ms' 85 | i += 1 86 | if f == '': 87 | f = '0ms' 88 | return f 89 | -------------------------------------------------------------------------------- /part_model/attack/trades.py: -------------------------------------------------------------------------------- 1 | """Attack used in TRADES adversarial training.""" 2 | 3 | from __future__ import annotations 4 | 5 | from typing import Any, Callable 6 | 7 | import torch 8 | from torch import nn 9 | 10 | from part_model.attack.pgd import PGDAttack 11 | from part_model.utils.loss import KLDLoss 12 | 13 | _Loss = Callable[..., torch.Tensor] 14 | 15 | 16 | class TRADESAttack(PGDAttack): 17 | """TRADES Attack.""" 18 | 19 | def __init__( 20 | self, 21 | attack_config: dict[str, Any], 22 | core_model: nn.Module, 23 | loss_fn: Callable[..., torch.Tensor], 24 | norm: str = "Linf", 25 | eps: float = 8 / 255, 26 | **kwargs, 27 | ) -> None: 28 | """Initialize TRADES Attack. 29 | 30 | For argument description, see AttackModule. 31 | """ 32 | super().__init__( 33 | attack_config, core_model, loss_fn, norm, eps, **kwargs 34 | ) 35 | device = next(core_model.parameters()).device 36 | self._trades_loss_fn: _Loss = KLDLoss(reduction="sum-non-batch").to( 37 | device 38 | ) 39 | 40 | @torch.enable_grad() 41 | def _compute_grads( 42 | self, x_adv: torch.Tensor, cl_logits: torch.Tensor, **kwargs 43 | ) -> torch.Tensor: 44 | """Compute logits, loss, gradients.""" 45 | x_adv.requires_grad_() 46 | logits = self._core_model(x_adv, **kwargs, **self._forward_args) 47 | # pylint: disable=not-callable 48 | loss = self._trades_loss_fn(cl_logits, logits).mean() 49 | grads = torch.autograd.grad(loss, x_adv, allow_unused=True)[0] 50 | grads.detach_() 51 | return grads 52 | 53 | def _forward( 54 | self, 55 | inputs: torch.Tensor, 56 | targets: torch.Tensor, 57 | loss_fn: _Loss | None = None, 58 | **kwargs, 59 | ) -> torch.Tensor: 60 | _ = loss_fn # Unused 61 | mode = self._core_model.training 62 | self._core_model.eval() 63 | inputs.detach_() 64 | inputs_min = inputs - self._eps 65 | inputs_max = inputs + self._eps 66 | 67 | # Initialize worst-case inputs 68 | x_adv_best = inputs.clone() 69 | inputs.requires_grad_() 70 | with torch.enable_grad(): 71 | cl_logits = self._core_model(inputs, **kwargs, **self._forward_args) 72 | loss_best = torch.zeros(len(inputs), 1, 1, 1, device=inputs.device) 73 | loss_best -= 1e9 74 | 75 | # Repeat PGD for specified number of restarts 76 | for _ in range(self._num_restarts): 77 | x_adv = self._init_adv(inputs) 78 | 79 | # Run PGD on inputs for specified number of steps 80 | for _ in range(self._num_steps): 81 | grads = self._compute_grads(x_adv, cl_logits, **kwargs) 82 | x_adv = self._update_and_proj( 83 | x_adv, 84 | grads, 85 | inputs=inputs, 86 | inputs_min_max=(inputs_min, inputs_max), 87 | ) 88 | 89 | x_adv_best, loss_best = self._save_best( 90 | x_adv, targets, x_adv_best, loss_best, **kwargs 91 | ) 92 | 93 | # Return worst-case perturbed input logits 94 | self._core_model.train(mode) 95 | return torch.cat([inputs.detach(), x_adv_best.detach()], dim=0) 96 | -------------------------------------------------------------------------------- /part_model/dataloader/__init__.py: -------------------------------------------------------------------------------- 1 | from part_model.dataloader.cityscapes import CITYSCAPES 2 | from part_model.dataloader.imagenet import IMAGENET 3 | from part_model.dataloader.part_imagenet import PART_IMAGENET 4 | from part_model.dataloader.part_imagenet_bbox import PART_IMAGENET_BBOX 5 | from part_model.dataloader.part_imagenet_corrupt import PART_IMAGENET_CORRUPT 6 | from part_model.dataloader.part_imagenet_geirhos import PART_IMAGENET_GEIRHOS 7 | from part_model.dataloader.part_imagenet_mixed_next import PART_IMAGENET_MIXED 8 | from part_model.dataloader.part_imagenet_imagenet_class import ( 9 | PART_IMAGENET_IMAGENET_CLASS, 10 | ) 11 | from part_model.dataloader.part_imagenet_pseudo import PART_IMAGENET_PSEUDO 12 | from part_model.dataloader.part_imagenet_pseudo_imagenet_class import ( 13 | PART_IMAGENET_PSEUDO_IMAGENET_CLASS, 14 | ) 15 | from part_model.dataloader.pascal_part import PASCAL_PART 16 | from part_model.dataloader.pascal_voc import PASCAL_VOC 17 | from part_model.dataloader.util import COLORMAP 18 | 19 | DATASET_DICT = { 20 | "cityscapes": CITYSCAPES, 21 | "pascal-part": PASCAL_PART, 22 | "pascal-voc": PASCAL_VOC, 23 | "part-imagenet": PART_IMAGENET, 24 | "part-imagenet-geirhos": PART_IMAGENET_GEIRHOS, 25 | "part-imagenet-mixed": PART_IMAGENET_MIXED, 26 | "part-imagenet-corrupt": PART_IMAGENET_CORRUPT, 27 | "part-imagenet-pseudo": PART_IMAGENET_PSEUDO, 28 | "part-imagenet-pseudo-imagenet-class": PART_IMAGENET_PSEUDO_IMAGENET_CLASS, 29 | "part-imagenet-imagenet-class": PART_IMAGENET_IMAGENET_CLASS, 30 | "part-imagenet-bbox": PART_IMAGENET_BBOX, 31 | "imagenet": IMAGENET, 32 | } 33 | 34 | 35 | def load_dataset(args): 36 | loader = DATASET_DICT[args.dataset]["loader"] 37 | return loader(args) 38 | -------------------------------------------------------------------------------- /part_model/dataloader/hdf5_dataset.py: -------------------------------------------------------------------------------- 1 | import h5py 2 | import torch 3 | from torch.utils.data import Dataset 4 | 5 | 6 | class HDF5Dataset(Dataset): 7 | def __init__(self, root, mode, input_dim, transform=None, **kwargs): 8 | self.root = root 9 | self.mode = mode 10 | self.input_dim = input_dim 11 | self.transform = transform 12 | self.db = None 13 | 14 | def __len__(self): 15 | with h5py.File(self.root, "r") as db: 16 | return len(db.get(self.mode).get("images")) 17 | 18 | def __getitem__(self, idx: int): 19 | if self.db is None: 20 | self.db = h5py.File(self.root, "r").get(self.mode) 21 | 22 | images = torch.from_numpy(self.db.get("images")[idx]) 23 | labels = self.db.get("labels")[idx] 24 | # part_indices = torch.from_numpy(self.db.get('part_indices')[idx]) 25 | images = images.permute(0, 3, 1, 2) 26 | 27 | if self.transform is not None: 28 | new_images = torch.empty((images.size(0),) + self.input_dim) 29 | for i, image in enumerate(images): 30 | new_images[i] = self.transform(image) 31 | images = new_images 32 | 33 | output_size = list(self.input_dim) 34 | output_size[0] = images.size(0) * images.size(1) 35 | # return images.view(output_size), labels, part_indices 36 | return images.view(output_size), labels 37 | -------------------------------------------------------------------------------- /part_model/models/common.py: -------------------------------------------------------------------------------- 1 | from __future__ import annotations 2 | 3 | from typing import List, Tuple, Union 4 | 5 | import torch 6 | from torch import nn 7 | 8 | _NormVal = Union[List[float], Tuple[float, float, float]] 9 | 10 | 11 | class Normalize(nn.Module): 12 | def __init__( 13 | self, mean: _NormVal = None, std: _NormVal | None = None 14 | ) -> None: 15 | super().__init__() 16 | if mean is None or std is None: 17 | self.mean, self.std = None, None 18 | else: 19 | self.register_buffer( 20 | "mean", torch.tensor(mean)[None, :, None, None] 21 | ) 22 | self.register_buffer("std", torch.tensor(std)[None, :, None, None]) 23 | 24 | def forward(self, x): 25 | # TODO(chawins@): place normalization in unified interface. 26 | if self.mean is None: 27 | return x 28 | return (x - self.mean) / self.std 29 | -------------------------------------------------------------------------------- /part_model/models/det_part_models/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/chawins/adv-part-model/218c2a45906c751be8d8845bd4183b5bfb8242da/part_model/models/det_part_models/__init__.py -------------------------------------------------------------------------------- /part_model/models/det_part_models/dino.py: -------------------------------------------------------------------------------- 1 | """DINO as sequential part model.""" 2 | 3 | from __future__ import annotations 4 | 5 | import logging 6 | 7 | import torch 8 | from torch import nn 9 | 10 | from DINO.models.dino.dino import ( 11 | DINO, 12 | build_backbone, 13 | build_deformable_transformer, 14 | ) 15 | from DINO.util.misc import NestedTensor 16 | from part_model.utils.types import BatchImages, Logits 17 | 18 | logger = logging.getLogger(__name__) 19 | 20 | 21 | class DinoModel(nn.Module): 22 | """DINO as part model.""" 23 | 24 | def __init__(self, args): 25 | """Initialize DinoBoundingBoxModel.""" 26 | logger.info("=> Initializing DinoBoundingBoxModel...") 27 | super().__init__() 28 | 29 | backbone = build_backbone(args) 30 | transformer = build_deformable_transformer(args) 31 | 32 | try: 33 | match_unstable_error = args.match_unstable_error 34 | dn_labelbook_size = args.dn_labelbook_size 35 | except: 36 | match_unstable_error = True 37 | # dn_labelbook_size = num_classes 38 | dn_labelbook_size = args.seg_labels 39 | 40 | try: 41 | dec_pred_class_embed_share = args.dec_pred_class_embed_share 42 | except: 43 | dec_pred_class_embed_share = True 44 | try: 45 | dec_pred_bbox_embed_share = args.dec_pred_bbox_embed_share 46 | except: 47 | dec_pred_bbox_embed_share = True 48 | 49 | self.object_detector = DINO( 50 | backbone, 51 | transformer, 52 | num_classes=args.seg_labels, 53 | num_queries=args.num_queries, 54 | aux_loss=True, 55 | iter_update=True, 56 | query_dim=4, 57 | random_refpoints_xy=args.random_refpoints_xy, 58 | fix_refpoints_hw=args.fix_refpoints_hw, 59 | num_feature_levels=args.num_feature_levels, 60 | nheads=args.nheads, 61 | dec_pred_class_embed_share=dec_pred_class_embed_share, 62 | dec_pred_bbox_embed_share=dec_pred_bbox_embed_share, 63 | # two stage 64 | two_stage_type=args.two_stage_type, 65 | # box_share 66 | two_stage_bbox_embed_share=args.two_stage_bbox_embed_share, 67 | two_stage_class_embed_share=args.two_stage_class_embed_share, 68 | decoder_sa_type=args.decoder_sa_type, 69 | num_patterns=args.num_patterns, 70 | dn_number=args.dn_number if args.use_dn else 0, 71 | dn_box_noise_scale=args.dn_box_noise_scale, 72 | dn_label_noise_ratio=args.dn_label_noise_ratio, 73 | dn_labelbook_size=dn_labelbook_size, 74 | ) 75 | 76 | def forward( 77 | self, 78 | images: BatchImages, 79 | masks=None, 80 | dino_targets=None, 81 | need_tgt_for_training: bool = False, 82 | **kwargs, 83 | ) -> Logits | tuple[Logits, torch.Tensor]: 84 | """Forward pass of sequential DINO part model.""" 85 | _ = kwargs # Unused 86 | # Object Detection part 87 | nested_tensors = NestedTensor(images, masks) 88 | 89 | if need_tgt_for_training: 90 | dino_outputs = self.object_detector(nested_tensors, dino_targets) 91 | else: 92 | dino_outputs = self.object_detector(nested_tensors) 93 | return dino_outputs 94 | -------------------------------------------------------------------------------- /part_model/models/model.py: -------------------------------------------------------------------------------- 1 | """Base classifier interface.""" 2 | 3 | from __future__ import annotations 4 | 5 | from typing import List, Tuple, Union 6 | 7 | import torch 8 | from torch import nn 9 | 10 | from part_model.utils.types import BatchImages, Logits 11 | 12 | _NormVal = Union[List[float], Tuple[float, float, float]] 13 | 14 | 15 | class Classifier(nn.Module): 16 | """Base Classifier interface.""" 17 | 18 | def __init__( 19 | self, 20 | model: nn.Module, 21 | normalize: dict[str, _NormVal] | None = None, 22 | ) -> None: 23 | """Initialize Classifier. 24 | 25 | Args: 26 | model: Main PyTorch model. 27 | normalize: Dictionary containing normalization values; must contain 28 | "mean" and "std". Defaults to None. 29 | """ 30 | super().__init__() 31 | self._model: nn.Module = model 32 | self._normalize: dict[str, _NormVal] | None = normalize 33 | if normalize is not None: 34 | mean = normalize["mean"] 35 | std = normalize["std"] 36 | self.register_buffer( 37 | "mean", torch.tensor(mean)[None, :, None, None] 38 | ) 39 | self.register_buffer("std", torch.tensor(std)[None, :, None, None]) 40 | 41 | def forward( 42 | self, inputs: BatchImages, **kwargs 43 | ) -> Logits | Tuple[Logits, torch.Tensor]: 44 | """Forward pass. 45 | 46 | Args: 47 | inputs: Input images. 48 | 49 | Returns: 50 | Output logits. 51 | """ 52 | _ = kwargs # Unused 53 | if self._normalize is None: 54 | return inputs 55 | inputs = (inputs - self.mean) / self.std 56 | return self._model(inputs) 57 | 58 | 59 | class SegClassifier(Classifier): 60 | """Base Classifier interface.""" 61 | 62 | def forward( 63 | self, inputs: BatchImages, **kwargs 64 | ) -> Logits | Tuple[Logits, torch.Tensor]: 65 | """Forward pass. 66 | 67 | Args: 68 | inputs: Input images. 69 | return_mask: If True, returns predicted segmentation mask together 70 | with the outputs. Defaults to False. 71 | 72 | Returns: 73 | Output logits. 74 | """ 75 | if self._normalize is None: 76 | return inputs 77 | inputs = (inputs - self.mean) / self.std 78 | return self._model(inputs, **kwargs) 79 | -------------------------------------------------------------------------------- /part_model/models/seg_part_models/clean_mask_model.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | 6 | class CleanMaskModel(nn.Module): 7 | def __init__(self, args, segmenter, core_model): 8 | super(CleanMaskModel, self).__init__() 9 | self.segmenter = segmenter 10 | self.core_model = core_model 11 | self.concat_input = "inpt" in args.experiment 12 | self.hard_label_mask = "hard" in args.experiment 13 | self.no_bg = "nobg" in args.experiment 14 | self.seg_labels = args.seg_labels 15 | self.detach_mask = "detach" in args.experiment 16 | self.clean_masks = None 17 | 18 | def forward(self, images, return_mask=False, clean=False, **kwargs): 19 | # Segmentation part 20 | if clean: 21 | logits_masks = self.segmenter(images) 22 | masks = F.softmax(logits_masks, dim=1) 23 | self.clean_masks = masks.detach() 24 | else: 25 | masks = self.clean_masks 26 | 27 | if self.detach_mask: 28 | masks = masks.detach() 29 | 30 | bg_idx = 1 if self.no_bg else 0 31 | 32 | if self.hard_label_mask: 33 | masks = masks.argmax(1) 34 | # Use all parts in order and exclude background 35 | part_masks = [] 36 | for i in range(bg_idx, self.seg_labels): 37 | part_masks.append(masks == i) 38 | masks = torch.stack(part_masks, dim=1).float() 39 | 40 | masks = masks[:, bg_idx:] 41 | if self.concat_input: 42 | masks = torch.cat([images, masks], dim=1) 43 | 44 | # Classify directly based on the mask 45 | out = self.core_model(masks) 46 | if return_mask: 47 | return out, self.clean_masks 48 | return out 49 | -------------------------------------------------------------------------------- /part_model/models/seg_part_models/groundtruth_mask_model.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | 6 | class GroundtruthMaskModel(nn.Module): 7 | def __init__(self, args, segmenter, core_model): 8 | super(GroundtruthMaskModel, self).__init__() 9 | self.core_model = core_model 10 | self.concat_input = "inpt" in args.experiment 11 | self.no_bg = "nobg" in args.experiment 12 | self.seg_labels = args.seg_labels 13 | 14 | def forward(self, images, segs=None, return_mask=False, **kwargs): 15 | # Segmentation part 16 | 17 | bg_idx = 1 if self.no_bg else 0 18 | 19 | part_masks = [] 20 | for i in range(bg_idx, self.seg_labels): 21 | part_masks.append(segs == i) 22 | masks = torch.stack(part_masks, dim=1).float() 23 | 24 | masks = masks[:, bg_idx:] 25 | if self.concat_input: 26 | masks = torch.cat([images, masks], dim=1) 27 | 28 | # Classify directly based on the mask 29 | out = self.core_model(masks) 30 | return out 31 | -------------------------------------------------------------------------------- /part_model/models/seg_part_models/part_fc_model.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn.functional as F 3 | from torch import nn 4 | 5 | 6 | class PartFCModel(nn.Module): 7 | def __init__(self, args, segmenter): 8 | print("=> Initializing BoundingBoxModel...") 9 | super(PartFCModel, self).__init__() 10 | self.segmenter = segmenter 11 | self.no_bg = "nobg" in args.experiment 12 | input_dim = ( 13 | (args.seg_labels - int(self.no_bg)) 14 | if "condense" in args.experiment 15 | else (args.seg_labels - int(self.no_bg)) * 5 16 | ) 17 | 18 | self.fc = nn.Sequential( 19 | nn.Conv2d(14, 1, (50, 50), 3), 20 | nn.ReLU(), 21 | nn.Flatten(), 22 | nn.Linear(3481, 200), 23 | nn.ReLU(), 24 | nn.Linear(200, input_dim), 25 | nn.Sigmoid(), 26 | ) 27 | 28 | self.core_model = nn.Sequential( 29 | nn.BatchNorm1d(input_dim), 30 | nn.Linear(input_dim, 50), 31 | nn.ReLU(), 32 | nn.BatchNorm1d(50), 33 | nn.Linear(50, args.num_classes), 34 | ) 35 | 36 | def forward(self, images, return_mask=False, **kwargs): 37 | # Segmentation part 38 | logits_masks = self.segmenter(images) 39 | # masks: [B, num_segs (including background), H, W] 40 | masks = F.softmax(logits_masks, dim=1) 41 | 42 | # Remove background 43 | if self.no_bg: 44 | masks = masks[:, 1:] 45 | 46 | condensed = self.fc(masks) 47 | 48 | out = self.core_model(condensed) 49 | 50 | if return_mask: 51 | return out, logits_masks 52 | return out 53 | -------------------------------------------------------------------------------- /part_model/models/seg_part_models/part_mask_model.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | 6 | class PartMaskModel(nn.Module): 7 | def __init__(self, args, segmenter, core_model): 8 | super(PartMaskModel, self).__init__() 9 | self.segmenter = segmenter 10 | self.core_model = core_model 11 | self.concat_input = "inpt" in args.experiment 12 | self.hard_label_mask = "hard" in args.experiment 13 | self.no_bg = "nobg" in args.experiment 14 | self.seg_labels = args.seg_labels 15 | self.detach_mask = "detach" in args.experiment 16 | 17 | def forward(self, images, return_mask=False, **kwargs): 18 | # Segmentation part 19 | logits_masks = self.segmenter(images) 20 | masks = F.softmax(logits_masks, dim=1) 21 | 22 | if self.detach_mask: 23 | masks = masks.detach() 24 | 25 | bg_idx = 1 if self.no_bg else 0 26 | 27 | if self.hard_label_mask: 28 | masks = masks.argmax(1) 29 | # Use all parts in order and exclude background 30 | part_masks = [] 31 | for i in range(bg_idx, self.seg_labels): 32 | part_masks.append(masks == i) 33 | masks = torch.stack(part_masks, dim=1).float() 34 | 35 | masks = masks[:, bg_idx:] 36 | if self.concat_input: 37 | masks = torch.cat([images, masks], dim=1) 38 | 39 | # Classify directly based on the mask 40 | out = self.core_model(masks) 41 | if return_mask: 42 | return out, logits_masks 43 | return out 44 | -------------------------------------------------------------------------------- /part_model/models/seg_part_models/part_seg_cat_model.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | 5 | 6 | class PartSegCatModel(nn.Module): 7 | def __init__(self, args, segmentor, core_model, rep_dim): 8 | super(PartSegCatModel, self).__init__() 9 | self.segmentor = segmentor 10 | self.core_model = core_model 11 | self.num_parts = args.seg_labels - 1 12 | 13 | def forward(self, images, return_mask=False, **kwargs): 14 | batch_size = images.size(0) 15 | 16 | # Segmentation part 17 | logits_masks = self.segmentor(images) 18 | masks = F.softmax(logits_masks, dim=1).argmax(1) 19 | 20 | # Use all parts in order and exclude background 21 | part_masks = F.one_hot(masks, num_classes=self.num_parts + 1).permute( 22 | 0, 3, 1, 2 23 | ) 24 | images = images[:, None] * part_masks[:, 1:, None] 25 | images = images.reshape( 26 | (batch_size, self.num_parts * 3, images.size(-2), images.size(-1)) 27 | ) 28 | 29 | out = self.core_model(images) 30 | if return_mask: 31 | return out, logits_masks 32 | return out 33 | -------------------------------------------------------------------------------- /part_model/models/seg_part_models/part_seg_model.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import torch.nn as nn 3 | import torch.nn.functional as F 4 | from part_model.dataloader import DATASET_DICT 5 | 6 | 7 | class PartSegModel(nn.Module): 8 | def __init__(self, args, segmenter, core_model, rep_dim, topk=None): 9 | super(PartSegModel, self).__init__() 10 | self.segmenter = segmenter 11 | # self.segmentor = segmenter 12 | self.core_model = core_model 13 | dataset_params = DATASET_DICT[args.dataset] 14 | self.use_soft_mask = "soft" in args.experiment 15 | self.group_part_by_class = "group" in args.experiment 16 | self.no_bg = "nobg" in args.experiment 17 | 18 | self.k = topk if topk is not None else args.seg_labels 19 | if self.group_part_by_class: 20 | part_to_class = dataset_params["part_to_class"] 21 | self.part_to_class_mat = torch.tensor( 22 | part_to_class, dtype=torch.float32, device=args.gpu 23 | ) 24 | self.k = self.part_to_class_mat.size(-1) 25 | self.mask = None 26 | self.temperature = args.temperature 27 | 28 | # Aggregation layer 29 | self.linear_dim = rep_dim * (self.k - self.no_bg) 30 | self.linear = nn.Linear(self.linear_dim, args.num_classes) 31 | 32 | def forward(self, images, return_mask=False, **kwargs): 33 | batch_size = images.size(0) 34 | images = self._apply_mask(images) 35 | 36 | # Segmentation part 37 | logits_masks = self.segmenter(images) 38 | masks = F.softmax(logits_masks / self.temperature, dim=1) 39 | bg_idx = 1 if self.no_bg else 0 40 | 41 | if self.group_part_by_class: 42 | masks = ( 43 | masks.unsqueeze(2) 44 | * self.part_to_class_mat[None, :, :, None, None] 45 | ) 46 | masks = masks.sum(1) 47 | 48 | if self.use_soft_mask: 49 | images = images.unsqueeze(1) * masks[:, bg_idx:, :, :].unsqueeze(2) 50 | else: 51 | label_masks = F.one_hot(masks.argmax(1), num_classes=self.k)[ 52 | :, :, :, bg_idx: 53 | ] 54 | images = images.unsqueeze(1) * label_masks.permute( 55 | 0, 3, 1, 2 56 | ).unsqueeze(2) 57 | 58 | images = images.reshape( 59 | (batch_size * (self.k - bg_idx),) + images.shape[2:] 60 | ) 61 | 62 | out = self.core_model(images) 63 | out = out.view(batch_size, self.linear_dim) 64 | out = self.linear(out) 65 | if return_mask: 66 | return out, logits_masks 67 | return out 68 | 69 | def set_mask(self, x): 70 | self.mask = (x != -1).float() 71 | 72 | def _apply_mask(self, x): 73 | if self.mask is None: 74 | return x 75 | return x * self.mask 76 | -------------------------------------------------------------------------------- /part_model/models/seg_part_models/pixel_count_model.py: -------------------------------------------------------------------------------- 1 | from operator import matmul 2 | 3 | import torch 4 | import torch.nn as nn 5 | import torch.nn.functional as F 6 | from part_model.dataloader import DATASET_DICT 7 | 8 | 9 | class PixelCountModel(nn.Module): 10 | def __init__(self, args, segmenter, core_model): 11 | super(PixelCountModel, self).__init__() 12 | print("=> Initializing PixelCountModel...") 13 | self.segmenter = segmenter 14 | exp_tokens = args.experiment.split("-") 15 | self.hard_label_mask = "hard" in exp_tokens 16 | if "softmax_mask" in exp_tokens: 17 | self.mode = "softmax_mask" 18 | elif "logits_mask" in exp_tokens: 19 | self.mode = "logits_mask" 20 | else: 21 | self.mode = None 22 | self.seg_labels = args.seg_labels 23 | self.detach_mask = "detach" in exp_tokens 24 | self.group_part_by_class = "part" in exp_tokens 25 | dataset_params = DATASET_DICT[args.dataset] 26 | if self.group_part_by_class: 27 | part_to_class = dataset_params["part_to_class"] 28 | self.register_buffer( 29 | "part_to_class_mat", 30 | torch.tensor(part_to_class, dtype=torch.float32), 31 | ) 32 | 33 | def forward(self, images, return_mask=False, **kwargs): 34 | # Segmentation part 35 | logits_masks = self.segmenter(images) 36 | masks = F.softmax(logits_masks, dim=1) 37 | 38 | if self.detach_mask: 39 | masks = masks.detach() 40 | 41 | if self.group_part_by_class: 42 | # masks: [B, NP, H, W], part_to_class_mat: [NP, NC] 43 | masks = torch.matmul( 44 | masks.permute(0, 2, 3, 1), self.part_to_class_mat 45 | ).permute(0, 3, 1, 2) 46 | class_logits_masks = torch.matmul( 47 | logits_masks.permute(0, 2, 3, 1), self.part_to_class_mat 48 | ).permute(0, 3, 1, 2) 49 | 50 | if self.hard_label_mask: 51 | masks = masks.argmax(1) 52 | masks = F.one_hot(masks, num_classes=self.seg_labels).to( 53 | images.dtype 54 | ) 55 | masks = masks.permute(0, 3, 1, 2) 56 | 57 | if self.mode is not None: 58 | # fg_score - bg_score 59 | fg_mask = ( 60 | logits_masks[:, 1:].sum(1, keepdim=True) - logits_masks[:, 0:1] 61 | ) 62 | fg_mask = torch.sigmoid(fg_mask) 63 | fg_mask = fg_mask / fg_mask.sum((2, 3), keepdim=True).clamp_min( 64 | 1e-6 65 | ) 66 | if self.mode == "softmax_mask": 67 | # FIXME: This will see double softmax 68 | class_mask = F.softmax(class_logits_masks[:, 1:], dim=1) 69 | # class_mask = logits_masks[:, 1:].clamp(-1e2, 1e2) 70 | else: 71 | class_mask = class_logits_masks[:, 1:] 72 | # out: [batch_size, num_classes] 73 | out = (class_mask * fg_mask).sum((2, 3)) 74 | else: 75 | masks = class_logits_masks[:, 1:] 76 | # Classify directly based on the mask 77 | out = masks.mean((2, 3)) # TODO: can use sum (or temperature) here 78 | 79 | if return_mask: 80 | return out, logits_masks 81 | return out 82 | -------------------------------------------------------------------------------- /part_model/models/seg_part_models/two_head_model.py: -------------------------------------------------------------------------------- 1 | """Multi-head architecture.""" 2 | 3 | from __future__ import annotations 4 | 5 | import torch 6 | from torch import nn 7 | 8 | 9 | class TwoHeadModel(nn.Module): 10 | def __init__(self, args, segmenter, mode): 11 | super().__init__() 12 | self.mode = mode 13 | if self.mode == "d": 14 | segmenter.segmentation_head = Heads( 15 | segmenter, args.num_classes 16 | ) 17 | else: 18 | latent_dim = 2048 # TODO: depends on backbone 19 | pool_size = 4 20 | fc_dim = 64 21 | segmenter.classification_head = nn.Sequential( 22 | nn.AdaptiveAvgPool2d(pool_size), 23 | nn.BatchNorm2d(latent_dim), 24 | nn.ReLU(inplace=True), 25 | nn.Conv2d(latent_dim, fc_dim, (pool_size, pool_size)), 26 | nn.BatchNorm2d(fc_dim), 27 | nn.ReLU(inplace=True), 28 | nn.Flatten(), 29 | nn.Linear(fc_dim, args.num_classes), 30 | ) 31 | self._segmenter = segmenter 32 | 33 | def forward( 34 | self, images: torch.Tensor, return_mask: bool = False, **kwargs 35 | ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: 36 | _ = kwargs # Unused 37 | # Segmentation part 38 | out = self._segmenter(images) 39 | if return_mask: 40 | return out 41 | return out[0] 42 | 43 | 44 | class Heads(nn.Module): 45 | def __init__(self, segmentor, num_classes): 46 | super().__init__() 47 | self.heads = nn.ModuleList( 48 | [ 49 | nn.Sequential( 50 | nn.Conv2d(256, 50, (3, 3), (1, 1)), 51 | nn.BatchNorm2d(50), 52 | nn.ReLU(), 53 | nn.MaxPool2d(2), 54 | nn.Conv2d(50, 10, (1, 1), (1, 1)), 55 | nn.BatchNorm2d(10), 56 | nn.ReLU(), 57 | nn.MaxPool2d(2), 58 | nn.Flatten(), 59 | nn.Linear(1690, 100), 60 | nn.ReLU(), 61 | nn.Linear(100, num_classes), 62 | ), 63 | segmentor.segmentation_head, 64 | ] 65 | ) 66 | 67 | def forward( 68 | self, images: torch.Tensor 69 | ) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]: 70 | return [head(images) for head in self.heads] 71 | -------------------------------------------------------------------------------- /part_model/models/seg_part_models/util.py: -------------------------------------------------------------------------------- 1 | """Segmentation models.""" 2 | 3 | from __future__ import annotations 4 | 5 | from collections import OrderedDict 6 | 7 | import segmentation_models_pytorch as smp 8 | import torch 9 | from torch import nn 10 | from torchvision.models.segmentation.deeplabv3 import DeepLabHead 11 | 12 | from part_model.dataloader import DATASET_DICT 13 | from part_model.models.common import Normalize 14 | 15 | 16 | def build_deeplabv3(args, normalize: bool = True): 17 | # FIXME: DeepLabV3 is pretrained on COCO (not ImageNet) 18 | model = torch.hub.load( 19 | "pytorch/vision:v0.10.0", 20 | "deeplabv3_resnet50", 21 | pretrained=args.pretrained, 22 | ) 23 | model.classifier = DeepLabHead(2048, args.seg_labels) 24 | model.aux_classifier = None 25 | 26 | if normalize: 27 | normalize = DATASET_DICT[args.dataset]["normalize"] 28 | model = nn.Sequential(Normalize(**normalize), model) 29 | 30 | if args.seg_dir != "": 31 | best_path = f"{args.seg_dir}/checkpoint_best.pt" 32 | print(f"=> loading best checkpoint for DeepLabv3: {best_path}") 33 | if args.gpu is None: 34 | checkpoint = torch.load(best_path) 35 | else: 36 | # Map model to be loaded to specified single gpu. 37 | checkpoint = torch.load(best_path, map_location=f"cuda:{args.gpu}") 38 | 39 | new_state_dict = OrderedDict() 40 | for k, v in checkpoint["state_dict"].items(): 41 | name = k[7:] # remove `module` 42 | new_state_dict[name] = v 43 | 44 | model.load_state_dict(new_state_dict) 45 | 46 | return model 47 | 48 | 49 | def build_deeplabv3plus(args, normalize: bool = True): 50 | model = smp.DeepLabV3Plus( 51 | encoder_name=args.seg_backbone, 52 | encoder_weights="imagenet" if args.pretrained else None, 53 | in_channels=3, 54 | classes=args.seg_labels, 55 | # Default parameters 56 | encoder_depth=5, 57 | encoder_output_stride=16, 58 | decoder_channels=256, 59 | decoder_atrous_rates=(12, 24, 36), 60 | upsampling=4, 61 | aux_params=None, 62 | ) 63 | if normalize: 64 | normalize = DATASET_DICT[args.dataset]["normalize"] 65 | model = nn.Sequential(Normalize(**normalize), model) 66 | 67 | if args.seg_dir != "": 68 | best_path = f"{args.seg_dir}/checkpoint_best.pt" 69 | print(f"=> loading best checkpoint for DeepLabv3+: {best_path}") 70 | if args.gpu is None: 71 | checkpoint = torch.load(best_path) 72 | else: 73 | # Map model to be loaded to specified single gpu. 74 | checkpoint = torch.load(best_path, map_location=f"cuda:{args.gpu}") 75 | 76 | new_state_dict = OrderedDict() 77 | for k, v in checkpoint["state_dict"].items(): 78 | name = k[7:] # remove `module` 79 | new_state_dict[name] = v 80 | 81 | model.load_state_dict(new_state_dict) 82 | 83 | return model 84 | 85 | 86 | SEGM_BUILDER = { 87 | "deeplabv3": build_deeplabv3, 88 | "deeplabv3plus": build_deeplabv3plus, 89 | } 90 | -------------------------------------------------------------------------------- /part_model/models/toy_part_model.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch.nn.functional as F 3 | 4 | 5 | class ToyPartModel(nn.Module): 6 | def __init__(self, args, core_model, rep_dim, total_parts=None): 7 | super(ToyPartModel, self).__init__() 8 | self.core_model = core_model 9 | self.num_parts = args.parts 10 | self.total_parts = ( 11 | self.num_parts if total_parts is None else total_parts 12 | ) 13 | self.rep_dim = rep_dim 14 | self.linear_dim = rep_dim * self.num_parts 15 | self.linear = nn.Linear(self.linear_dim, args.num_classes) 16 | 17 | # For taking part indices as inputs 18 | self.use_part_idx = args.use_part_idx 19 | if self.use_part_idx: 20 | self.part_idx_to_rep = nn.Linear(total_parts, self.rep_dim) 21 | self.combine_rep = nn.Linear(self.linear_dim, self.linear_dim) 22 | self.part_idx_rep = None 23 | 24 | def set_part_idx(self, x): 25 | if not self.use_part_idx: 26 | return 27 | batch_size = x.size(0) 28 | one_hot = F.one_hot(x, num_classes=self.total_parts).float() 29 | self.part_idx_rep = self.part_idx_to_rep(one_hot).view( 30 | batch_size * self.num_parts, -1 31 | ) 32 | 33 | def forward(self, x, part_idx=False): 34 | if part_idx: 35 | self.set_part_idx(x) 36 | return 37 | 38 | batch_size = x.size(0) 39 | num_channels = int(x.size(1) / self.num_parts) 40 | out = self.core_model( 41 | x.view( 42 | ( 43 | batch_size * self.num_parts, 44 | num_channels, 45 | ) 46 | + x.shape[2:] 47 | ) 48 | ) 49 | 50 | if self.use_part_idx: 51 | out = (out * self.part_idx_rep).view(batch_size, self.linear_dim) 52 | out = F.relu( 53 | self.combine_rep(F.relu(out, inplace=True)), inplace=True 54 | ) 55 | else: 56 | out = out.view(batch_size, self.linear_dim) 57 | 58 | return self.linear(out) 59 | -------------------------------------------------------------------------------- /part_model/utils/__init__.py: -------------------------------------------------------------------------------- 1 | import contextlib 2 | 3 | import numpy as np 4 | 5 | from .dataloader_visualizer import * 6 | from .distributed import * 7 | from .image import * 8 | from .loss import * 9 | from .metric import * 10 | 11 | 12 | @contextlib.contextmanager 13 | def np_temp_seed(seed): 14 | state = np.random.get_state() 15 | np.random.seed(seed) 16 | try: 17 | yield 18 | finally: 19 | np.random.set_state(state) 20 | -------------------------------------------------------------------------------- /part_model/utils/dataloader_visualizer.py: -------------------------------------------------------------------------------- 1 | import torchvision 2 | 3 | CLASSES = { 4 | "Quadruped": 4, 5 | "Biped": 5, 6 | "Fish": 4, 7 | "Bird": 5, 8 | "Snake": 2, 9 | "Reptile": 4, 10 | "Car": 3, 11 | "Bicycle": 4, 12 | "Boat": 2, 13 | "Aeroplane": 5, 14 | "Bottle": 2, 15 | } 16 | CLASSES = CLASSES.keys() 17 | CLASSES = sorted(CLASSES) 18 | 19 | def debug_dino_dataloader(loader): 20 | for i, samples in enumerate(loader): 21 | images, target_bbox, targets = samples 22 | images, mask = images.decompose() 23 | assert images.max() >= 0.0 24 | assert images.min() <= 1.0 25 | 26 | debug_index = 0 27 | torchvision.utils.save_image( 28 | images[debug_index], f"example_images/img_{debug_index}.png" 29 | ) 30 | torchvision.utils.save_image( 31 | mask[debug_index] * 1.0, 32 | f"example_images/mask_{debug_index}.png", 33 | ) 34 | img_uint8 = torchvision.io.read_image(f"example_images/img_{debug_index}.png") 35 | shape = target_bbox[debug_index]["size"] 36 | 37 | # xc, xy, w, h convert to xmin, ymin, xmax, ymax 38 | boxes = target_bbox[debug_index]["boxes"] 39 | 40 | if boxes.shape[0] > 0: 41 | boxes = torchvision.ops.box_convert(boxes, in_fmt="cxcywh", out_fmt="xyxy") 42 | boxes[:, ::2] = boxes[:, ::2] * shape[1] 43 | boxes[:, 1::2] = boxes[:, 1::2] * shape[0] 44 | 45 | img_with_boxes = torchvision.utils.draw_bounding_boxes( 46 | img_uint8, boxes=boxes, colors="red" 47 | ) 48 | else: 49 | img_with_boxes = img_uint8 50 | 51 | torchvision.utils.save_image( 52 | img_with_boxes / 255, 53 | f"example_images/img_{debug_index}_with_bbox.png", 54 | ) 55 | print('image label:', CLASSES[targets[debug_index]]) 56 | import pdb 57 | 58 | pdb.set_trace() -------------------------------------------------------------------------------- /part_model/utils/distributed.py: -------------------------------------------------------------------------------- 1 | """Utilities for distributed training.""" 2 | 3 | from __future__ import annotations 4 | 5 | import datetime 6 | import os 7 | 8 | import torch 9 | import torch.distributed as dist 10 | 11 | 12 | def setup_for_distributed(is_master): 13 | """This function disables printing when not in master process.""" 14 | import builtins as __builtin__ 15 | 16 | builtin_print = __builtin__.print 17 | 18 | def print(*args, **kwargs): 19 | force = kwargs.pop("force", False) 20 | if is_master or force: 21 | builtin_print(*args, **kwargs) 22 | 23 | __builtin__.print = print 24 | 25 | 26 | def is_dist_avail_and_initialized(): 27 | if not dist.is_available(): 28 | return False 29 | if not dist.is_initialized(): 30 | return False 31 | return True 32 | 33 | 34 | def get_world_size(): 35 | if not is_dist_avail_and_initialized(): 36 | return 1 37 | return dist.get_world_size() 38 | 39 | 40 | def get_rank(): 41 | if not is_dist_avail_and_initialized(): 42 | return 0 43 | return dist.get_rank() 44 | 45 | 46 | def dist_barrier(): 47 | if is_dist_avail_and_initialized(): 48 | torch.distributed.barrier() 49 | 50 | 51 | def is_main_process(): 52 | return get_rank() == 0 53 | 54 | 55 | def save_on_master(state, output_dir, is_best=True, epoch=None): 56 | if is_main_process(): 57 | if is_best: 58 | path = f"{output_dir}/checkpoint_best.pt" 59 | else: 60 | path = f"{output_dir}/checkpoint_last.pt" 61 | 62 | torch.save(state, path) 63 | # Save to best model if not exist 64 | if not os.path.exists(f"{output_dir}/checkpoint_best.pt"): 65 | torch.save(state, f"{output_dir}/checkpoint_best.pt") 66 | 67 | if epoch is not None: 68 | path = f"{output_dir}/checkpoint_epoch{epoch}.pt" 69 | torch.save(state, path) 70 | 71 | 72 | def init_distributed_mode(args): 73 | if args.no_distributed: 74 | args.distributed = False 75 | return 76 | if "RANK" in os.environ and "WORLD_SIZE" in os.environ: 77 | args.rank = int(os.environ["RANK"]) 78 | args.world_size = int(os.environ["WORLD_SIZE"]) 79 | args.gpu = int(os.environ["LOCAL_RANK"]) 80 | elif "SLURM_PROCID" in os.environ: 81 | args.rank = int(os.environ["SLURM_PROCID"]) 82 | args.gpu = args.rank % torch.cuda.device_count() 83 | else: 84 | print("Not using distributed mode") 85 | args.distributed = False 86 | return 87 | 88 | args.distributed = True 89 | torch.cuda.set_device(args.gpu) 90 | # args.dist_backend = 'nccl' # see args 91 | print( 92 | "| distributed init (rank {}): {}".format(args.rank, args.dist_url), 93 | flush=True, 94 | ) 95 | torch.distributed.init_process_group( 96 | backend=args.dist_backend, 97 | init_method=args.dist_url, 98 | world_size=args.world_size, 99 | rank=args.rank, 100 | # Set timeout to 2 hours for gloo 101 | timeout=datetime.timedelta(hours=12), 102 | ) 103 | torch.distributed.barrier() 104 | setup_for_distributed(args.rank == 0) 105 | -------------------------------------------------------------------------------- /part_model/utils/metric.py: -------------------------------------------------------------------------------- 1 | from __future__ import annotations 2 | 3 | import math 4 | 5 | import torch 6 | import torch.distributed as dist 7 | 8 | 9 | class AverageMeter(object): 10 | """Computes and stores the average and current value""" 11 | 12 | def __init__(self, name, fmt=":f"): 13 | self.name = name 14 | self.fmt = fmt 15 | self.reset() 16 | 17 | def reset(self): 18 | self.val = 0 19 | self.avg = 0 20 | self.sum = 0 21 | self.count = 0 22 | 23 | def update(self, val, n=1): 24 | self.val = val 25 | self.sum += val * n 26 | self.count += n 27 | self.avg = self.sum / self.count 28 | 29 | def synchronize(self): 30 | t = torch.tensor( 31 | [self.sum, self.count], dtype=torch.float64, device="cuda" 32 | ) 33 | if dist.is_initialized(): 34 | dist.barrier() 35 | dist.all_reduce(t) 36 | t = t.tolist() 37 | self.sum = int(t[0]) 38 | self.count = t[1] 39 | self.avg = self.sum / self.count 40 | 41 | def __str__(self): 42 | fmtstr = "{name} {val" + self.fmt + "} ({avg" + self.fmt + "})" 43 | return fmtstr.format(**self.__dict__) 44 | 45 | 46 | class ProgressMeter(object): 47 | def __init__(self, num_batches, meters, prefix=""): 48 | self.batch_fmtstr = self._get_batch_fmtstr(num_batches) 49 | self.meters = meters 50 | self.prefix = prefix 51 | 52 | def display(self, batch): 53 | entries = [self.prefix + self.batch_fmtstr.format(batch)] 54 | entries += [str(meter) for meter in self.meters] 55 | print("\t".join(entries)) 56 | 57 | def synchronize(self): 58 | for meter in self.meters: 59 | meter.synchronize() 60 | 61 | def _get_batch_fmtstr(self, num_batches): 62 | num_digits = len(str(num_batches // 1)) 63 | fmt = "{:" + str(num_digits) + "d}" 64 | return "[" + fmt + "/" + fmt.format(num_batches) + "]" 65 | 66 | 67 | def adjust_learning_rate(optimizer, epoch, args): 68 | """Decay the learning rate based on schedule.""" 69 | lr = args.lr 70 | if epoch < args.warmup_epochs: 71 | lr *= (epoch + 1) / args.warmup_epochs 72 | lr *= 0.5 * (1.0 + math.cos(math.pi * epoch / args.epochs)) 73 | for param_group in optimizer.param_groups: 74 | param_group["lr"] = lr 75 | return lr 76 | 77 | 78 | def adjust_learning_rate_deeplabv3( 79 | optimizer, epoch, batch_idx, num_batches_per_epoch, args 80 | ): 81 | cur_iter = epoch * num_batches_per_epoch + batch_idx 82 | max_iter = args.epochs * num_batches_per_epoch 83 | lr = args.lr * (1 - float(cur_iter) / max_iter) ** 0.9 84 | optimizer.param_groups[0]["lr"] = lr 85 | optimizer.param_groups[1]["lr"] = lr * args.last_mult 86 | 87 | 88 | def accuracy(output, target, topk=(1,)): 89 | """Computes the accuracy over the k top predictions.""" 90 | with torch.no_grad(): 91 | maxk = max(topk) 92 | batch_size = target.size(0) 93 | 94 | _, pred = output.topk(maxk, 1, True, True) 95 | pred = pred.t() 96 | correct = pred.eq(target.reshape(1, -1).expand_as(pred)) 97 | 98 | res = [] 99 | for k in topk: 100 | correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True) 101 | res.append(correct_k.mul_(100.0 / batch_size)) 102 | # Return only top1 103 | return res[0] 104 | 105 | def pixel_accuracy(output, target): 106 | with torch.no_grad(): 107 | pred = output.argmax(1) 108 | acc = (pred == target).float().mean() * 100 109 | return acc 110 | 111 | def get_compute_acc(args): 112 | if "seg-only" in args.experiment: 113 | return pixel_accuracy 114 | return accuracy 115 | -------------------------------------------------------------------------------- /part_model/utils/types.py: -------------------------------------------------------------------------------- 1 | """Define commonly used types.""" 2 | 3 | import torch 4 | from jaxtyping import Float 5 | 6 | BatchImages = Float[torch.Tensor, "batch channels height width"] 7 | Logits = Float[torch.Tensor, "batch classes"] 8 | -------------------------------------------------------------------------------- /print_metrics.py: -------------------------------------------------------------------------------- 1 | import json 2 | import os 3 | 4 | import numpy as np 5 | 6 | GEN_GAP_EVAL_EPOCHS = 20 7 | exp_path = "./results/" 8 | exp_list = os.listdir(exp_path) 9 | exp_list = [os.path.join(exp_path, exp, "log.txt") for exp in exp_list] 10 | exp_list = [exp for exp in exp_list if os.path.isfile(exp)] 11 | exp_list = sorted(exp_list) 12 | 13 | for exp in exp_list: 14 | with open(exp) as file: 15 | lines = [line.rstrip() for line in file] 16 | data = [json.loads(line) for line in lines if "test_acc1" in line] 17 | if len(data) == 0: 18 | continue 19 | train_acc = np.array([d["train_acc1"] for d in data][-GEN_GAP_EVAL_EPOCHS:]) 20 | val_acc = np.array([d["test_acc1"] for d in data][-GEN_GAP_EVAL_EPOCHS:]) 21 | if not (len(train_acc) == len(val_acc) == GEN_GAP_EVAL_EPOCHS): 22 | continue 23 | avg_gen_gap = np.mean((train_acc - val_acc) / train_acc) 24 | print(f'{exp.split("/")[2]}: avg_gen_gap = {avg_gen_gap}') 25 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | absl-py==1.3.0 2 | adversarial-robustness-toolbox==1.12.1 3 | astroid==2.11.7 4 | astunparse==1.6.3 5 | autoattack==0.1 6 | black==22.6.0 7 | botorch==0.6.2 8 | Bottleneck==1.3.5 9 | brotlipy==0.7.0 10 | cachetools==5.2.0 11 | certifi==2022.9.24 12 | cffi==1.15.1 13 | charset-normalizer==2.0.4 14 | click==8.1.3 15 | cloudpickle==2.2.0 16 | compressai==1.2.1 17 | cryptography==38.0.1 18 | cycler==0.11.0 19 | Cython==0.29.30 20 | cytoolz==0.11.0 21 | dask==2022.10.1 22 | dill==0.3.4 23 | docker-pycreds==0.4.0 24 | eagerpy==0.30.0 25 | efficientnet-pytorch==0.7.1 26 | flake8==4.0.1 27 | flatbuffers==22.10.26 28 | fonttools==4.25.0 29 | foolbox==3.3.3 30 | fsspec==2022.10.0 31 | gast==0.4.0 32 | gitdb==4.0.9 33 | GitPython==3.1.29 34 | google-auth==2.13.0 35 | google-auth-oauthlib==0.4.6 36 | google-pasta==0.2.0 37 | gpytorch==1.9.0 38 | grpcio==1.50.0 39 | h5py==3.7.0 40 | idna==3.4 41 | imagecodecs==2021.8.26 42 | imagecorruptions==1.1.2 43 | imageio==2.22.0 44 | importlib-metadata==4.11.3 45 | isort==5.9.3 46 | joblib==1.1.1 47 | keras==2.10.0 48 | Keras-Preprocessing==1.1.2 49 | kiwisolver==1.4.2 50 | kornia==0.6.8 51 | lazy-object-proxy==1.6.0 52 | libclang==14.0.6 53 | linear-operator==0.0.0 54 | llvmlite==0.39.1 55 | locket==1.0.0 56 | Markdown==3.4.1 57 | MarkupSafe==2.1.1 58 | matplotlib==3.5.3 59 | mccabe==0.7.0 60 | mkl-fft==1.3.1 61 | mkl-random==1.2.2 62 | mkl-service==2.4.0 63 | munch==2.5.0 64 | munkres==1.1.4 65 | mypy==0.910 66 | mypy-extensions==0.4.3 67 | networkx==2.8.7 68 | numba==0.56.3 69 | numexpr==2.8.3 70 | numpy==1.23.3 71 | oauthlib==3.2.2 72 | opencv-python==4.6.0.66 73 | opt-einsum==3.3.0 74 | packaging==21.3 75 | pandas==1.4.4 76 | partd==1.3.0 77 | pathspec==0.9.0 78 | pathtools==0.1.2 79 | Pillow==9.2.0 80 | pip==22.2.2 81 | platformdirs==2.5.2 82 | ply==3.11 83 | pretrainedmodels==0.7.4 84 | promise==2.3 85 | protobuf==3.20.1 86 | psutil==5.9.0 87 | pyasn1==0.4.8 88 | pyasn1-modules==0.2.8 89 | pycodestyle==2.8.0 90 | pycparser==2.21 91 | pydocstyle==6.1.1 92 | pyflakes==2.4.0 93 | pylint==2.14.5 94 | pyOpenSSL==22.0.0 95 | pyparsing==3.0.9 96 | PyQt5-sip==12.11.0 97 | PySocks==1.7.1 98 | python-dateutil==2.8.2 99 | pytz==2022.1 100 | PyWavelets==1.3.0 101 | PyYAML==6.0 102 | requests==2.28.1 103 | requests-oauthlib==1.3.1 104 | rsa==4.9 105 | ruamel.yaml==0.17.21 106 | ruamel.yaml.clib==0.2.6 107 | scikit-image==0.19.2 108 | scikit-learn==1.0.2 109 | scipy==1.9.1 110 | segmentation-models-pytorch==0.3.0 111 | sentry-sdk==1.10.1 112 | setproctitle==1.2.2 113 | setuptools==65.5.0 114 | shortuuid==1.0.9 115 | sip==6.6.2 116 | six==1.16.0 117 | smmap==5.0.0 118 | snowballstemmer==2.2.0 119 | tensorboard==2.10.1 120 | tensorboard-data-server==0.6.1 121 | tensorboard-plugin-wit==1.8.1 122 | tensorflow==2.10.0 123 | tensorflow-estimator==2.10.0 124 | tensorflow-io-gcs-filesystem==0.27.0 125 | termcolor==2.1.0 126 | threadpoolctl==2.2.0 127 | tifffile==2021.4.8 128 | timm==0.4.12 129 | toml==0.10.2 130 | tomli==2.0.1 131 | tomlkit==0.11.1 132 | toolz==0.12.0 133 | torch==1.13.0 134 | torchaudio==0.13.0 135 | torchjpeg==0.9.27 136 | torchmetrics==0.10.2 137 | torchvision==0.14.0 138 | tornado==6.1 139 | tqdm==4.64.1 140 | typing_extensions==4.3.0 141 | urllib3==1.26.12 142 | wandb==0.13.5 143 | Werkzeug==2.2.2 144 | wheel==0.37.1 145 | wrapt==1.14.1 146 | zipp==3.8.0 147 | -------------------------------------------------------------------------------- /scripts/download_cityscapes.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | wget --keep-session-cookies --save-cookies=cookies.txt \ 3 | --post-data "username=$1&password=$2&submit=Login" https://www.cityscapes-dataset.com/login/ 4 | wget --load-cookies cookies.txt \ 5 | --content-disposition https://www.cityscapes-dataset.com/file-handling/?packageID=3 6 | wget --load-cookies cookies.txt \ 7 | --content-disposition https://www.cityscapes-dataset.com/file-handling/?packageID=35 8 | -------------------------------------------------------------------------------- /scripts/prepare_data.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #SBATCH --job-name=part-model 3 | #SBATCH --account=fc_wagner 4 | #SBATCH --partition=savio3 5 | #SBATCH --nodes=1 6 | #SBATCH --ntasks=1 7 | #SBATCH --cpus-per-task=32 8 | #SBATCH --time=6:00:00 9 | #SBATCH --output slurm-prepare-data-%j.out 10 | ## Command(s) to run: 11 | # source /global/home/users/$USER/.bash_profile 12 | # module purge 13 | # module load python 14 | # source activate /global/scratch/users/$USER/env_part_based 15 | 16 | # python prepare_cityscapes.py \ 17 | # --seed 0 \ 18 | # --data-dir /global/scratch/users/nabeel126/cityscapes/ \ 19 | # --name bbox_square_rand_pad0.2 \ 20 | # --pad 0.2 \ 21 | # --min-area 1000 \ 22 | # --square \ 23 | # --rand-pad \ 24 | # --allow-missing-parts \ 25 | # --use-box-seg 26 | 27 | # python prepare_pascal_part.py \ 28 | # --data-dir /global/scratch/users/nabeel126/pascal_part/ \ 29 | # --name aeroplane_bird_car_cat_dog_new \ 30 | # --min-area 0. 31 | 32 | # python -u prepare_part_imagenet.py \ 33 | # --data-dir ~/data/PartImageNet/ \ 34 | # --name All 35 | 36 | python -u prepare_part_imagenet.py \ 37 | --data-dir ~/data/PartImageNet/ \ 38 | --name All \ 39 | --use-box-seg 40 | 41 | # DATAPATH=~/data/PartImageNet 42 | 43 | # ### Data Prep 44 | # mkdir $DATAPATH/PartBoxSegmentations/train/ 45 | # mkdir $DATAPATH/PartBoxSegmentations/val/ 46 | # mkdir $DATAPATH/PartBoxSegmentations/test/ 47 | 48 | # ln -s $DATAPATH/JPEGImages/* $DATAPATH/PartBoxSegmentations/train/ 49 | # ln -s $DATAPATH/JPEGImages/* $DATAPATH/PartBoxSegmentations/val/ 50 | # ln -s $DATAPATH/JPEGImages/* $DATAPATH/PartBoxSegmentations/test/ 51 | 52 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "train" 53 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "val" 54 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "test" 55 | 56 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "train" --use-imagenet-classes 57 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "val" --use-imagenet-classes 58 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "test" --use-imagenet-classes 59 | 60 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "train" --group-parts 61 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "val" --group-parts 62 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "test" --group-parts 63 | 64 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "train" --use-imagenet-classes --group-parts 65 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "val" --use-imagenet-classes --group-parts 66 | # python prepare_part_imagenet_bbox.py --label-dir $DATAPATH --split "test" --use-imagenet-classes --group-parts 67 | -------------------------------------------------------------------------------- /scripts/prepare_data_slurm.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #SBATCH --job-name=prepare_data 3 | #SBATCH --account=fc_wagner 4 | #SBATCH --partition=savio3_bigmem 5 | # Number of nodes: 6 | #SBATCH --nodes=1 7 | # Number of tasks (one for each GPU desired for use case) (example): 8 | #SBATCH --ntasks-per-node=32 9 | #SBATCH --cpus-per-task=1 10 | #SBATCH --time=10:00:00 11 | ## Command(s) to run: 12 | python prepare_cityscapes_v2.py \ 13 | --data-dir /global/scratch/users/chawins/data/cityscapes/ \ 14 | --name all_parts_sqaure_bal \ 15 | --pad 0.1 \ 16 | --min-area 1000 \ 17 | --square \ 18 | --allow-missing-parts 19 | -------------------------------------------------------------------------------- /scripts/run1-2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=2 7 | GPU=0,1 8 | NUM_GPU=2 9 | BS=32 10 | AA_BS=32 11 | PORT=1004$ID 12 | BACKEND=nccl 13 | NUM_WORKERS=2 14 | # =============================== PASCAL-Part =============================== # 15 | # DATASET=pascal-part 16 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 17 | # SEGPATH=$DATAPATH/panoptic-parts/train 18 | # =============================== Cityscapes ================================ # 19 | DATASET=cityscapes 20 | DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 21 | SEGPATH=$DATAPATH 22 | # ============================== Part-ImageNet ============================== # 23 | # DATASET=part-imagenet 24 | # DATAPATH=/global/scratch/users/kornrapatp/PartImageNet/ 25 | # SEGPATH=$DATAPATH/PartSegmentations/All/ 26 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 27 | 28 | # 0.0156862745, 0.03137254901, 0.06274509803 29 | EPS=0.03137254901 30 | 31 | # for i in {1..5}; do 32 | # torchrun \ 33 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 34 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 35 | # main.py \ 36 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND --workers $NUM_WORKERS \ 37 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 38 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 39 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 40 | # --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 0.8 \ 41 | # --seg-const-trn 0.4 --semi-label 1 \ 42 | # --resume results/ct_kp_1/checkpoint_best.pt --load-weight-only --resume-if-exist \ 43 | # --output-dir results/ct_kp_2 --experiment part-wbbox-norm_img-centroid-semi && break 44 | # sleep 30 45 | # done 46 | 47 | python -u main.py \ 48 | --seed 0 --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision \ 49 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 50 | --workers $((NUM_GPU * NUM_WORKERS)) --batch-size $AA_BS --print-freq 50 \ 51 | --epsilon $EPS --atk-norm Linf --eval-attack aa --output-dir results/ct_kp_2/ \ 52 | --resume results/ct_kp_2/checkpoint_best.pt \ 53 | --experiment part-wbbox-norm_img-centroid-semi --evaluate -------------------------------------------------------------------------------- /scripts/run1-3.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=3 7 | GPU=0,1 8 | NUM_GPU=2 9 | BS=32 10 | AA_BS=32 11 | PORT=1004$ID 12 | BACKEND=nccl 13 | NUM_WORKERS=2 14 | # =============================== PASCAL-Part =============================== # 15 | # DATASET=pascal-part 16 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 17 | # SEGPATH=$DATAPATH/panoptic-parts/train 18 | # =============================== Cityscapes ================================ # 19 | DATASET=cityscapes 20 | DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 21 | SEGPATH=$DATAPATH 22 | # ============================== Part-ImageNet ============================== # 23 | # DATASET=part-imagenet 24 | # DATAPATH=/global/scratch/users/kornrapatp/PartImageNet/ 25 | # SEGPATH=$DATAPATH/PartSegmentations/All/ 26 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 27 | 28 | # 0.0156862745, 0.03137254901, 0.06274509803 29 | EPS=0.03137254901 30 | 31 | # for i in {1..5}; do 32 | # torchrun \ 33 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 34 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 35 | # main.py \ 36 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND --workers $NUM_WORKERS \ 37 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 38 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 39 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 40 | # --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 0.8 \ 41 | # --seg-const-trn 0.5 --semi-label 1 \ 42 | # --resume results/ct_kp_1/checkpoint_best.pt --load-weight-only --resume-if-exist \ 43 | # --output-dir results/ct_kp_3 --experiment part-wbbox-norm_img-centroid-semi && break 44 | # sleep 30 45 | # done 46 | 47 | python -u main.py \ 48 | --seed 0 --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision \ 49 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 50 | --workers $((NUM_GPU * NUM_WORKERS)) --batch-size $AA_BS --print-freq 50 \ 51 | --epsilon $EPS --atk-norm Linf --eval-attack aa --output-dir results/ct_kp_3 \ 52 | --resume results/ct_kp_3/checkpoint_best.pt \ 53 | --experiment part-wbbox-norm_img-centroid-semi --evaluate -------------------------------------------------------------------------------- /scripts/run1-4.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=4 7 | GPU=0,1 8 | NUM_GPU=2 9 | BS=32 10 | AA_BS=32 11 | PORT=1004$ID 12 | BACKEND=nccl 13 | NUM_WORKERS=2 14 | # =============================== PASCAL-Part =============================== # 15 | # DATASET=pascal-part 16 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 17 | # SEGPATH=$DATAPATH/panoptic-parts/train 18 | # =============================== Cityscapes ================================ # 19 | DATASET=cityscapes 20 | DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 21 | SEGPATH=$DATAPATH 22 | # ============================== Part-ImageNet ============================== # 23 | # DATASET=part-imagenet 24 | # DATAPATH=/global/scratch/users/kornrapatp/PartImageNet/ 25 | # SEGPATH=$DATAPATH/PartSegmentations/All/ 26 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 27 | 28 | # 0.0156862745, 0.03137254901, 0.06274509803 29 | EPS=0.03137254901 30 | 31 | # for i in {1..5}; do 32 | # torchrun \ 33 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 34 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 35 | # main.py \ 36 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND --workers $NUM_WORKERS \ 37 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 38 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 39 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 40 | # --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 0.8 \ 41 | # --seg-const-trn 0.6 --semi-label 1 \ 42 | # --resume results/ct_kp_1/checkpoint_best.pt --load-weight-only --resume-if-exist \ 43 | # --output-dir results/ct_kp_4 --experiment part-wbbox-norm_img-centroid-semi && break 44 | # sleep 30 45 | # done 46 | 47 | python -u main.py \ 48 | --seed 0 --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision \ 49 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 50 | --workers $((NUM_GPU * NUM_WORKERS)) --batch-size $AA_BS --print-freq 50 \ 51 | --epsilon $EPS --atk-norm Linf --eval-attack aa --output-dir results/ct_kp_4 \ 52 | --resume results/ct_kp_4/checkpoint_best.pt \ 53 | --experiment part-wbbox-norm_img-centroid-semi --evaluate -------------------------------------------------------------------------------- /scripts/run1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=6 7 | GPU=0 8 | NUM_GPU=1 9 | BS=32 10 | PORT=1100$ID 11 | BACKEND=nccl 12 | # =============================== PASCAL-Part =============================== # 13 | # DATASET=pascal-part 14 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog_new/ 15 | # SEGPATH=$DATAPATH/panoptic-parts/train 16 | # =============================== Cityscapes ================================ # 17 | DATASET=cityscapes 18 | DATAPATH=~/data/cityscapes/PartImages/bbox_square_rand_pad0.2/ 19 | SEGPATH=$DATAPATH 20 | # ============================== Part-ImageNet ============================== # 21 | # DATASET=part-imagenet 22 | # DATAPATH=~/data/PartImageNet/ 23 | # SEGPATH=$DATAPATH/PartSegmentations/All/ 24 | 25 | # 0.0156862745, 0.03137254901, 0.06274509803 26 | EPS=0.03137254901 27 | 28 | CUDA_VISIBLE_DEVICES=$GPU python -u main.py \ 29 | --seed 0 --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 30 | --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 31 | --print-freq 50 --batch-size $BS --epsilon $EPS --atk-norm Linf --eval-attack pgd \ 32 | --resume results/65/checkpoint_best.pt \ 33 | --output-dir results/65 --experiment part-pooling-4-semi --evaluate --debug 34 | 35 | # for i in {1..5}; do 36 | # CUDA_VISIBLE_DEVICES=$GPU torchrun \ 37 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 38 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 39 | # main.py \ 40 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 41 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 42 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 43 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 44 | # --adv-train none --epsilon $EPS --atk-norm Linf --adv-beta 2 \ 45 | # --seg-const-trn 0.5 --semi-label 0.9 \ 46 | # --resume results/1105/checkpoint_best.pt --resume-if-exist \ 47 | # --output-dir results/1105 --experiment part-pooling-4-no_bg-semi && break 48 | # sleep 30 49 | # done 50 | 51 | # for i in {1..5}; do 52 | # CUDA_VISIBLE_DEVICES=$GPU torchrun \ 53 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 54 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 55 | # main.py \ 56 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 57 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 58 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 59 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 60 | # --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 2 \ 61 | # --seg-const-trn 0.5 --semi-label 0.9 \ 62 | # --resume results/1105/checkpoint_best.pt --load-weight-only --resume-if-exist \ 63 | # --output-dir results/1106 --experiment part-pooling-4-no_bg-semi && break 64 | # sleep 30 65 | # done 66 | 67 | # CUDA_VISIBLE_DEVICES=$GPU python -u main.py \ 68 | # --seed 0 --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 69 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 70 | # --print-freq 50 --batch-size $((BS * 2)) --epsilon $EPS --atk-norm Linf --eval-attack aa \ 71 | # --output-dir results/1106 --experiment part-pooling-4-no_bg-semi --evaluate 72 | -------------------------------------------------------------------------------- /scripts/run2-1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=5 7 | GPU=0,1,2,3 8 | NUM_GPU=4 9 | BS=32 10 | AA_BS=64 11 | PORT=1004$ID 12 | BACKEND=nccl 13 | NUM_WORKERS=2 14 | # =============================== PASCAL-Part =============================== # 15 | # DATASET=pascal-part 16 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 17 | # SEGPATH=$DATAPATH/panoptic-parts/train 18 | # =============================== Cityscapes ================================ # 19 | DATASET=cityscapes 20 | DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 21 | SEGPATH=$DATAPATH 22 | # ============================== Part-ImageNet ============================== # 23 | # DATASET=part-imagenet 24 | # DATAPATH=/global/scratch/users/kornrapatp/PartImageNet/ 25 | # SEGPATH=$DATAPATH/PartSegmentations/All/ 26 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 27 | 28 | # 0.0156862745, 0.03137254901, 0.06274509803 29 | EPS=0.03137254901 30 | 31 | for i in {1..5}; do 32 | torchrun \ 33 | --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 34 | --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 35 | main.py \ 36 | --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND --workers $NUM_WORKERS \ 37 | --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 38 | --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 39 | --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 40 | --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 0.8 \ 41 | --seg-const-trn 0.3 --semi-label 1 \ 42 | --resume results/ct_kp_1/checkpoint_best.pt --load-weight-only --resume-if-exist \ 43 | --output-dir results/ct_kp_5 --experiment part-wbbox-norm_img-centroid-semi && break 44 | sleep 30 45 | done 46 | 47 | python -u main.py \ 48 | --seed 0 --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision \ 49 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 50 | --workers $((NUM_GPU * NUM_WORKERS)) --batch-size $AA_BS --print-freq 50 \ 51 | --epsilon $EPS --atk-norm Linf --eval-attack aa --output-dir results/ct_kp_5 \ 52 | --experiment part-wbbox-norm_img-centroid-semi --evaluate -------------------------------------------------------------------------------- /scripts/run2-2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=6 7 | GPU=0,1,2,3 8 | NUM_GPU=4 9 | BS=32 10 | AA_BS=64 11 | PORT=1004$ID 12 | BACKEND=nccl 13 | NUM_WORKERS=2 14 | # =============================== PASCAL-Part =============================== # 15 | # DATASET=pascal-part 16 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 17 | # SEGPATH=$DATAPATH/panoptic-parts/train 18 | # =============================== Cityscapes ================================ # 19 | DATASET=cityscapes 20 | DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 21 | SEGPATH=$DATAPATH 22 | # ============================== Part-ImageNet ============================== # 23 | # DATASET=part-imagenet 24 | # DATAPATH=/global/scratch/users/kornrapatp/PartImageNet/ 25 | # SEGPATH=$DATAPATH/PartSegmentations/All/ 26 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 27 | 28 | # 0.0156862745, 0.03137254901, 0.06274509803 29 | EPS=0.03137254901 30 | 31 | for i in {1..5}; do 32 | torchrun \ 33 | --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 34 | --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 35 | main.py \ 36 | --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND --workers $NUM_WORKERS \ 37 | --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 38 | --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 39 | --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 40 | --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 0.8 \ 41 | --seg-const-trn 0.7 --semi-label 1 \ 42 | --resume results/ct_kp_1/checkpoint_best.pt --load-weight-only --resume-if-exist \ 43 | --output-dir results/ct_kp_6 --experiment part-wbbox-norm_img-centroid-semi && break 44 | sleep 30 45 | done 46 | 47 | python -u main.py \ 48 | --seed 0 --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision \ 49 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 50 | --workers $((NUM_GPU * NUM_WORKERS)) --batch-size $AA_BS --print-freq 50 \ 51 | --epsilon $EPS --atk-norm Linf --eval-attack aa --output-dir results/ct_kp_6 \ 52 | --experiment part-wbbox-norm_img-centroid-semi --evaluate -------------------------------------------------------------------------------- /scripts/run2-3.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=7 7 | GPU=0 8 | NUM_GPU=1 9 | BS=32 10 | AA_BS=32 11 | PORT=1005$ID 12 | BACKEND=nccl 13 | NUM_WORKERS=2 14 | # =============================== PASCAL-Part =============================== # 15 | # DATASET=pascal-part 16 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 17 | # SEGPATH=$DATAPATH/panoptic-parts/train 18 | # =============================== Cityscapes ================================ # 19 | # DATASET=cityscapes 20 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 21 | # SEGPATH=$DATAPATH 22 | # ============================== Part-ImageNet ============================== # 23 | DATASET=part-imagenet 24 | DATAPATH=~/data/PartImageNet/ 25 | SEGPATH=$DATAPATH/PartSegmentations/All/ 26 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 27 | 28 | # 0.0156862745, 0.03137254901, 0.06274509803 29 | EPS=0.03137254901 30 | 31 | # for i in {1..5}; do 32 | # torchrun \ 33 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 34 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 35 | # main.py \ 36 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 37 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 38 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 39 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 40 | # --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 0.8 --eval-attack pgd \ 41 | # --seg-const-trn 0.4 --semi-label 1 \ 42 | # --resume results/box1/checkpoint_best.pt --load-weight-only --resume-if-exist \ 43 | # --output-dir results/box12 --experiment part-pooling-4-semi && break 44 | # sleep 30 45 | # done 46 | 47 | python -u custom_seg_attack_main.py \ 48 | --seed 0 --dist-url tcp://localhost:$PORT --full-precision \ 49 | --seg-backbone resnet50 --seg-arch deeplabv3plus \ 50 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 51 | --print-freq 10 --batch-size $AA_BS --epsilon $EPS --atk-norm Linf \ 52 | --workers $((NUM_GPU * NUM_WORKERS)) --eval-attack seg-guide/2nd_gt_random/0.0/ts \ 53 | --output-dir results/462 --experiment part-pooling-4-no_bg-semi --evaluate 54 | -------------------------------------------------------------------------------- /scripts/run2-4.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=9 7 | GPU=0 8 | NUM_GPU=1 9 | BS=32 10 | AA_BS=32 11 | PORT=1006$ID 12 | BACKEND=nccl 13 | NUM_WORKERS=2 14 | # =============================== PASCAL-Part =============================== # 15 | # DATASET=pascal-part 16 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 17 | # SEGPATH=$DATAPATH/panoptic-parts/train 18 | # =============================== Cityscapes ================================ # 19 | # DATASET=cityscapes 20 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 21 | # SEGPATH=$DATAPATH 22 | # ============================== Part-ImageNet ============================== # 23 | DATASET=part-imagenet 24 | DATAPATH=~/data/PartImageNet/ 25 | SEGPATH=$DATAPATH/PartSegmentations/All/ 26 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 27 | 28 | # 0.0156862745, 0.03137254901, 0.06274509803 29 | EPS=0.03137254901 30 | 31 | # for i in {1..5}; do 32 | # torchrun \ 33 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 34 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 35 | # main.py \ 36 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 37 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 38 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 39 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 40 | # --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 0.8 --eval-attack pgd \ 41 | # --seg-const-trn 0.4 --semi-label 1 \ 42 | # --resume results/box1/checkpoint_best.pt --load-weight-only --resume-if-exist \ 43 | # --output-dir results/box12 --experiment part-pooling-4-semi && break 44 | # sleep 30 45 | # done 46 | 47 | python -u custom_seg_attack_main.py \ 48 | --seed 0 --dist-url tcp://localhost:$PORT --full-precision \ 49 | --seg-backbone resnet50 --seg-arch deeplabv3plus \ 50 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 51 | --print-freq 10 --batch-size $AA_BS --epsilon $EPS --atk-norm Linf \ 52 | --workers $((NUM_GPU * NUM_WORKERS)) --eval-attack seg-guide/random/0.0/ts \ 53 | --experiment part-pooling-4-no_bg-semi --evaluate \ 54 | --resume results/462/checkpoint_best.pt --output-dir results/462 55 | -------------------------------------------------------------------------------- /scripts/run2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=7 7 | GPU=7 8 | NUM_GPU=1 9 | BS=8 10 | PORT=1100$ID 11 | BACKEND=nccl 12 | # =============================== PASCAL-Part =============================== # 13 | # DATASET=pascal-part 14 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog_new/ 15 | # SEGPATH=$DATAPATH/panoptic-parts/train 16 | # =============================== Cityscapes ================================ # 17 | # DATASET=cityscapes 18 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 19 | # SEGPATH=$DATAPATH 20 | # ============================== Part-ImageNet ============================== # 21 | DATASET=part-imagenet 22 | DATAPATH=~/data/PartImageNet/ 23 | SEGPATH=$DATAPATH/PartSegmentations/All/ 24 | 25 | # 0.0156862745, 0.03137254901, 0.06274509803 26 | EPS=0.03137254901 27 | 28 | CUDA_VISIBLE_DEVICES=$GPU python -u main.py \ 29 | --seed 0 --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 30 | --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 31 | --print-freq 50 --batch-size $BS --epsilon $EPS --atk-norm Linf --eval-attack hsj \ 32 | --output-dir results/462 --experiment part-pooling-4-semi --evaluate 33 | 34 | # for i in {1..5}; do 35 | # CUDA_VISIBLE_DEVICES=$GPU torchrun \ 36 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 37 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 38 | # main.py \ 39 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 40 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 41 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 42 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 43 | # --adv-train none --epsilon $EPS --atk-norm Linf --adv-beta 2 \ 44 | # --seg-const-trn 0.5 --semi-label 0.3 \ 45 | # --resume-if-exist \ 46 | # --output-dir results/1111 --experiment part-pooling-4-no_bg-semi && break 47 | # sleep 30 48 | # done 49 | 50 | # for i in {1..5}; do 51 | # CUDA_VISIBLE_DEVICES=$GPU torchrun \ 52 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 53 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 54 | # main.py \ 55 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 56 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 57 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 58 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 59 | # --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 2 \ 60 | # --seg-const-trn 0.5 --semi-label 0.3 \ 61 | # --resume results/1111/checkpoint_best.pt --load-weight-only --resume-if-exist \ 62 | # --output-dir results/1112 --experiment part-pooling-4-no_bg-semi && break 63 | # sleep 30 64 | # done 65 | 66 | # CUDA_VISIBLE_DEVICES=$GPU python -u main.py \ 67 | # --seed 0 --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 68 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 69 | # --print-freq 50 --batch-size $((BS * 2)) --epsilon $EPS --atk-norm Linf --eval-attack aa \ 70 | # --output-dir results/1112 --experiment part-pooling-4-no_bg-semi --evaluate 71 | -------------------------------------------------------------------------------- /scripts/run3-1.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=9 7 | GPU=0 8 | NUM_GPU=1 9 | BS=32 10 | AA_BS=32 11 | PORT=1005$ID 12 | BACKEND=nccl 13 | NUM_WORKERS=2 14 | # =============================== PASCAL-Part =============================== # 15 | # DATASET=pascal-part 16 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 17 | # SEGPATH=$DATAPATH/panoptic-parts/train 18 | # =============================== Cityscapes ================================ # 19 | # DATASET=cityscapes 20 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 21 | # SEGPATH=$DATAPATH 22 | # ============================== Part-ImageNet ============================== # 23 | DATASET=part-imagenet 24 | DATAPATH=~/data/PartImageNet/ 25 | SEGPATH=$DATAPATH/PartSegmentations/All/ 26 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 27 | 28 | # 0.0156862745, 0.03137254901, 0.06274509803 29 | EPS=0.03137254901 30 | 31 | # for i in {1..5}; do 32 | # torchrun \ 33 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 34 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 35 | # main.py \ 36 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 37 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 38 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 39 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 40 | # --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 0.8 --eval-attack pgd \ 41 | # --seg-const-trn 0.4 --semi-label 1 \ 42 | # --resume results/box1/checkpoint_best.pt --load-weight-only --resume-if-exist \ 43 | # --output-dir results/box12 --experiment part-pooling-4-semi && break 44 | # sleep 30 45 | # done 46 | 47 | python -u custom_seg_attack_main.py \ 48 | --seed 0 --dist-url tcp://localhost:$PORT --full-precision \ 49 | --seg-backbone resnet50 --seg-arch deeplabv3plus \ 50 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 51 | --print-freq 10 --batch-size $AA_BS --epsilon $EPS --atk-norm Linf \ 52 | --workers $((NUM_GPU * NUM_WORKERS)) --eval-attack seg-guide/2nd_pred_by_scores/0.0/ts \ 53 | --experiment part-pooling-4-no_bg-semi --evaluate \ 54 | --resume results/462/checkpoint_best.pt --output-dir results/462 55 | -------------------------------------------------------------------------------- /scripts/run3-2.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=0 7 | GPU=0 8 | NUM_GPU=1 9 | BS=32 10 | AA_BS=32 11 | PORT=1006$ID 12 | BACKEND=nccl 13 | NUM_WORKERS=2 14 | # =============================== PASCAL-Part =============================== # 15 | # DATASET=pascal-part 16 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 17 | # SEGPATH=$DATAPATH/panoptic-parts/train 18 | # =============================== Cityscapes ================================ # 19 | # DATASET=cityscapes 20 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 21 | # SEGPATH=$DATAPATH 22 | # ============================== Part-ImageNet ============================== # 23 | DATASET=part-imagenet 24 | DATAPATH=~/data/PartImageNet/ 25 | SEGPATH=$DATAPATH/PartSegmentations/All/ 26 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 27 | 28 | # 0.0156862745, 0.03137254901, 0.06274509803 29 | EPS=0.03137254901 30 | 31 | # for i in {1..5}; do 32 | # torchrun \ 33 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 34 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 35 | # main.py \ 36 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 37 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 38 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 39 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 40 | # --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 0.8 --eval-attack pgd \ 41 | # --seg-const-trn 0.4 --semi-label 1 \ 42 | # --resume results/box1/checkpoint_best.pt --load-weight-only --resume-if-exist \ 43 | # --output-dir results/box12 --experiment part-pooling-4-semi && break 44 | # sleep 30 45 | # done 46 | 47 | python -u custom_seg_attack_main.py \ 48 | --seed 0 --dist-url tcp://localhost:$PORT --full-precision \ 49 | --seg-backbone resnet50 --seg-arch deeplabv3plus \ 50 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 51 | --print-freq 10 --batch-size $AA_BS --epsilon $EPS --atk-norm Linf \ 52 | --workers $((NUM_GPU * NUM_WORKERS)) --eval-attack seg-guide/2nd_pred_by_scores/0.1 \ 53 | --experiment part-pooling-4-no_bg-semi --evaluate \ 54 | --resume results/462/checkpoint_best.pt --output-dir results/462 55 | -------------------------------------------------------------------------------- /scripts/run3-3.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=1 7 | GPU=0 8 | NUM_GPU=1 9 | BS=32 10 | AA_BS=32 11 | PORT=1006$ID 12 | BACKEND=nccl 13 | NUM_WORKERS=2 14 | # =============================== PASCAL-Part =============================== # 15 | # DATASET=pascal-part 16 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 17 | # SEGPATH=$DATAPATH/panoptic-parts/train 18 | # =============================== Cityscapes ================================ # 19 | # DATASET=cityscapes 20 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 21 | # SEGPATH=$DATAPATH 22 | # ============================== Part-ImageNet ============================== # 23 | DATASET=part-imagenet 24 | DATAPATH=~/data/PartImageNet/ 25 | SEGPATH=$DATAPATH/PartSegmentations/All/ 26 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 27 | 28 | # 0.0156862745, 0.03137254901, 0.06274509803 29 | EPS=0.03137254901 30 | 31 | # for i in {1..5}; do 32 | # torchrun \ 33 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 34 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 35 | # main.py \ 36 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 37 | # --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 38 | # --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 39 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 40 | # --adv-train pgd --epsilon $EPS --atk-norm Linf --adv-beta 0.8 --eval-attack pgd \ 41 | # --seg-const-trn 0.4 --semi-label 1 \ 42 | # --resume results/box1/checkpoint_best.pt --load-weight-only --resume-if-exist \ 43 | # --output-dir results/box12 --experiment part-pooling-4-semi && break 44 | # sleep 30 45 | # done 46 | 47 | python -u custom_seg_attack_main.py \ 48 | --seed 0 --dist-url tcp://localhost:$PORT --full-precision \ 49 | --seg-backbone resnet50 --seg-arch deeplabv3plus \ 50 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 51 | --print-freq 10 --batch-size $AA_BS --epsilon $EPS --atk-norm Linf \ 52 | --workers $((NUM_GPU * NUM_WORKERS)) --eval-attack seg-guide/untargeted/0.0/ts \ 53 | --experiment part-pooling-4-no_bg-semi --evaluate \ 54 | --resume results/462/checkpoint_best.pt --output-dir results/462 55 | -------------------------------------------------------------------------------- /scripts/run_custom_seg_attack.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ID=9 3 | GPU=1 4 | NUM_GPU=1 5 | BS=32 6 | PORT=1000$ID 7 | BACKEND=nccl 8 | # =============================== PASCAL-Part =============================== # 9 | # DATASET=pascal-part 10 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 11 | # SEGPATH=$DATAPATH/panoptic-parts/train 12 | # =============================== Cityscapes ================================ # 13 | # DATASET=cityscapes 14 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 15 | # SEGPATH=$DATAPATH 16 | # ============================== Part-ImageNet ============================== # 17 | DATASET=part-imagenet 18 | DATAPATH=~/data/PartImageNet/ 19 | SEGPATH=$DATAPATH/PartSegmentations/All/ 20 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 21 | 22 | # 0.0156862745, 0.03137254901, 0.06274509803 23 | EPS=0.03137254901 24 | 25 | CUDA_VISIBLE_DEVICES=$GPU python -u custom_seg_attack_main.py \ 26 | --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 27 | --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET --batch-size $BS \ 28 | --epsilon $EPS --atk-norm Linf --evaluate --eval-attack pgd \ 29 | --output-dir results/462 --experiment part-pooling-4-no_bg-semi --debug 30 | -------------------------------------------------------------------------------- /scripts/run_dino_hyp_search.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ID=9 3 | GPU=0 4 | NUM_GPU=1 5 | BS=32 6 | AA_BS=32 7 | PORT=1000$ID 8 | # =============================== PASCAL-Part =============================== # 9 | # DATASET=pascal-part 10 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 11 | # SEGPATH=$DATAPATH/panoptic-parts/train 12 | # =============================== Cityscapes ================================ # 13 | # DATASET=cityscapes 14 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 15 | # SEGPATH=$DATAPATH 16 | # ============================== Part-ImageNet ============================== # 17 | # DATASET=part-imagenet 18 | # DATAPATH=/data/shared/PartImageNet/ 19 | # SEGPATH=$DATAPATH/PartSegmentations/All/ 20 | # ============================ Part-ImageNet-BBox =========================== # 21 | DATASET=part-imagenet-bbox 22 | DATAPATH=~/data/PartImageNet 23 | SEGPATH=$DATAPATH/PartSegmentations/All/ 24 | BBOXDIR=$DATAPATH/PartBoxSegmentations 25 | EPS=0.03137254901 26 | 27 | 28 | c_seg_arr=(0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9) 29 | 30 | BASE_PATH="$HOME/data/adv-part-model/models/part-image-net/all" 31 | EXP_NAME="part-seq-conv1d-semi" 32 | EPOCHS=50 33 | 34 | # pretrain dino bbox part model 35 | PRETRAIN_DIR="$BASE_PATH/c_seg_0.5" 36 | CUDA_VISIBLE_DEVICES=$GPU torchrun \ 37 | --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 38 | main.py --dist-url tcp://localhost:$PORT \ 39 | --seg-backbone "resnet50" --obj-det-arch "dino" --full-precision --pretrained \ 40 | --data $DATAPATH --seg-label-dir $SEGPATH --bbox-label-dir $BBOXDIR \ 41 | --dataset $DATASET --batch-size $BS --epochs $EPOCHS --experiment $EXP_NAME \ 42 | --epsilon $EPS \ 43 | --adv-train "none" \ 44 | --seg-const-trn 0.5 \ 45 | --lr 0.01 \ 46 | --output-dir "$PRETRAIN_DIR/pretrained" \ 47 | --seg-labels 40 \ 48 | --config_file "DINO/config/DINO/DINO_4scale_increased_backbone_lr.py" \ 49 | --options dn_scalar=100 dn_label_coef=1.0 dn_bbox_coef=1.0 50 | 51 | for C_SEG in ${c_seg_arr[@]}; do 52 | OUTPUT_DIR="$BASE_PATH/c_seg_${C_SEG}/" # need to change 53 | echo "$OUTPUT_DIR" 54 | CUDA_VISIBLE_DEVICES=$GPU torchrun \ 55 | --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 56 | main.py --dist-url tcp://localhost:$PORT \ 57 | --seg-backbone "resnet50" --obj-det-arch "dino" --full-precision --pretrained \ 58 | --data $DATAPATH --seg-label-dir $SEGPATH --bbox-label-dir $BBOXDIR \ 59 | --dataset $DATASET --batch-size $BS --seg-const-trn $C_SEG \ 60 | --epochs $EPOCHS --experiment $EXP_NAME --epsilon $EPS \ 61 | --adv-train "pgd" \ 62 | --lr 0.01 \ 63 | --resume "$PRETRAIN_DIR/pretrained/checkpoint_best.pt" \ 64 | --load-weight-only \ 65 | --resume-if-exist \ 66 | --output-dir "$OUTPUT_DIR/advtrained/" \ 67 | --seg-labels 40 \ 68 | --config_file "DINO/config/DINO/DINO_4scale_increased_backbone_lr.py" \ 69 | --options dn_scalar=100 dn_label_coef=1.0 dn_bbox_coef=1.0 70 | done 71 | -------------------------------------------------------------------------------- /scripts/run_example.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ID=1 # Some unique job id (only matters for distributed training) 3 | GPU=0,1 # Specify id's of GPU to use 4 | NUM_GPU=2 # Specify number of GPUs to use 5 | BS=32 # Batch size for training (per GPU) 6 | AA_BS=32 # Batch size for AutoAttack 7 | PORT=1000$ID # Port for distributed training 8 | BACKEND=nccl # Backend for distributed training (default is nccl) 9 | NUM_WORKERS=2 # Number of workers per GPU 10 | 11 | # Set some params for loading data 12 | # =============================== PASCAL-Part =============================== # 13 | # DATASET=pascal-part 14 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 15 | # SEGPATH=$DATAPATH/panoptic-parts/train 16 | # =============================== Cityscapes ================================ # 17 | # DATASET=cityscapes 18 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 19 | # SEGPATH=$DATAPATH 20 | # ============================== Part-ImageNet ============================== # 21 | DATASET=part-imagenet 22 | DATAPATH=~/data/PartImageNet/ 23 | SEGPATH=$DATAPATH/PartSegmentations/All/ 24 | 25 | EPS=0.03137254901 # Set epsilon for adversarial training and evaluation 26 | MODEL=part-wbbox-norm_img-semi # Define model to create (see naming in README) 27 | 28 | # Run normal training 29 | torchrun \ 30 | --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 31 | --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 32 | main.py \ 33 | --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 34 | --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision \ 35 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 36 | --workers $NUM_WORKERS --print-freq 50 --epochs 50 --batch-size $BS \ 37 | --lr 1e-1 --wd 5e-4 --adv-train none --epsilon $EPS --atk-norm Linf \ 38 | --adv-beta 1.0 --seg-const-trn 0.5 --semi-label 1 \ 39 | --eval-attack pgd --output-dir results/example_normal --experiment $MODEL 40 | sleep 30 41 | 42 | # Run adversarial training by tuning from the normally trained model above. 43 | # After training finishes, it runs PGD evaluation. 44 | # NOTE: For loop just restarts the job in case it dies before finishing 45 | for i in {1..5}; do 46 | torchrun \ 47 | --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 48 | --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 49 | main.py \ 50 | --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 51 | --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision \ 52 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH \ 53 | --dataset $DATASET --print-freq 50 --epochs 50 --batch-size $BS \ 54 | --lr 1e-1 --wd 5e-4 --adv-train pgd --epsilon $EPS --atk-norm Linf \ 55 | --adv-beta 1.0 --seg-const-trn 0.5 --semi-label 1 \ 56 | --resume results/example_normal/checkpoint_best.pt --load-weight-only \ 57 | --resume-if-exist --output-dir results/example_adv --experiment $MODEL && break 58 | sleep 30 59 | done 60 | 61 | # Run evaluation with AutoAttack 62 | python -u main.py \ 63 | --seed 0 --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision \ 64 | --pretrained --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 65 | --print-freq 50 --batch-size $AA_BS --epsilon $EPS --atk-norm Linf \ 66 | --eval-attack aa --output-dir results/example_normal \ 67 | --experiment $MODEL --evaluate -------------------------------------------------------------------------------- /scripts/run_example_gen_new_dataset.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ID=1 # Some unique job id (only matters for distributed training) 3 | GPU=0,1 # Specify id's of GPU to use 4 | NUM_GPU=2 # Specify number of GPUs to use 5 | BS=12 # Batch size for training (per GPU) 6 | AA_BS=32 # Batch size for AutoAttack 7 | PORT=1000$ID # Port for distributed training 8 | BACKEND=nccl # Backend for distributed training (default is nccl) 9 | NUM_WORKERS=2 # Number of workers per GPU 10 | 11 | # Set some params for loading data 12 | # =============================== PASCAL-Part =============================== # 13 | # DATASET=pascal-part 14 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 15 | # SEGPATH=$DATAPATH/panoptic-parts/train 16 | # =============================== Cityscapes ================================ # 17 | # DATASET=cityscapes 18 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 19 | # SEGPATH=$DATAPATH 20 | # ============================== Part-ImageNet ============================== # 21 | DATASET=part-imagenet-pseudo-imagenet-class 22 | DATAPATH=/data/kornrapatp/PartImageNet 23 | SEGPATH=$DATAPATH/PartSegmentations/All-imagenetclass-segtrain-processed/ # Path to train seg model (Check new_partition.py script) 24 | 25 | OLD_DATASET=$DATAPATH/PartSegmentations/All 26 | NEW_DATASET=$DATAPATH/PartSegmentations/test2 27 | NEW_SEGPATH=$NEW_DATASET-mask 28 | JPEG_PATH=$DATAPATH/JPEGImages 29 | NUM_NEW_SAMPLES=1000 30 | PREDICTION_PATH=/data/kornrapatp/test 31 | PREDICTION_MODEL_PATH=/data/kornrapatp/results/ImagenetClassTest 32 | 33 | EPS=0.03137254901 # Set epsilon for adversarial training and evaluation 34 | MODEL=part-seg-only # Define model to create (see naming in README) 35 | 36 | 37 | 38 | # train a segmenter using this command (use main.py and part-imagenet-pseudo as dataset) 39 | CUDA_VISIBLE_DEVICES=$GPU torchrun \ 40 | --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 41 | --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 42 | old_main.py \ 43 | --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 44 | --seg-backbone resnet101 --seg-arch deeplabv3plus --full-precision --pretrained \ 45 | --data $DATAPATH --seg-label-dir $SEGPATH --seg-labels 599 --dataset $DATASET --workers $NUM_WORKERS \ 46 | --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 47 | --adv-train none --epsilon $EPS --atk-norm Linf --adv-beta 0.8 \ 48 | --seg-const-trn 0.5 --semi-label 1 \ 49 | --output-dir $PREDICTION_MODEL_PATH --experiment $MODEL 50 | 51 | # # preprocess the dataset 52 | # python3 pre_prediction.py --old-dataset $OLD_DATASET --new-dataset $NEW_DATASET \ 53 | # --jpeg-path $JPEG_PATH --num-new-samples $NUM_NEW_SAMPLES --prediction-path $PREDICTION_PATH 54 | 55 | # # get predicted masks 56 | # CUDA_VISIBLE_DEVICES=$GPU torchrun \ 57 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 58 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 59 | # main_pseudo.py \ 60 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 61 | # --seg-backbone resnet101 --seg-arch deeplabv3plus --full-precision --pretrained \ 62 | # --data $DATAPATH --seg-label-dir $NEW_SEGPATH --seg-labels 41 --dataset $DATASET --workers $NUM_WORKERS \ 63 | # --print-freq 50 --epochs 150 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 64 | # --adv-train none --epsilon $EPS --atk-norm Linf --adv-beta 0.8 \ 65 | # --seg-const-trn 0.5 --semi-label 1 --prediction-path $PREDICTION_PATH \ 66 | # --output-dir $PREDICTION_MODEL_PATH --experiment $MODEL --evaluate 67 | 68 | # # postprocess the dataset 69 | # python3 post_prediction.py --old-dataset $OLD_DATASET --new-dataset $NEW_DATASET \ 70 | # --jpeg-path $JPEG_PATH --num-new-samples $NUM_NEW_SAMPLES --prediction-path $PREDICTION_PATH 71 | -------------------------------------------------------------------------------- /scripts/run_example_gen_new_dataset_imagenet.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | ID=1 # Some unique job id (only matters for distributed training) 3 | GPU=0,1 # Specify id's of GPU to use 4 | NUM_GPU=2 # Specify number of GPUs to use 5 | BS=8 # Batch size for training (per GPU) 6 | AA_BS=32 # Batch size for AutoAttack 7 | PORT=1000$ID # Port for distributed training 8 | BACKEND=nccl # Backend for distributed training (default is nccl) 9 | NUM_WORKERS=2 # Number of workers per GPU 10 | 11 | # Set some params for loading data 12 | # =============================== PASCAL-Part =============================== # 13 | # DATASET=pascal-part 14 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 15 | # SEGPATH=$DATAPATH/panoptic-parts/train 16 | # =============================== Cityscapes ================================ # 17 | # DATASET=cityscapes 18 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 19 | # SEGPATH=$DATAPATH 20 | # ============================== Part-ImageNet ============================== # 21 | DATASET=part-imagenet-pseudo-imagenet-class 22 | DATAPATH=/data/kornrapatp/PartImageNet 23 | SEGPATH=$DATAPATH/PartSegmentations/All-imagenetclass-segtrain-processed/ # Path to train seg model (Check new_partition.py script) 24 | 25 | OLD_DATASET=$DATAPATH/PartSegmentations/All-imagenetclass-segtrain-processed 26 | NEW_DATASET=$DATAPATH/PartSegmentations/All-imagenetclass-suppressed-reweighted 27 | NEW_SEGPATH=$NEW_DATASET-mask 28 | JPEG_PATH=$DATAPATH/JPEGImages 29 | NUM_NEW_SAMPLES=300000 30 | PREDICTION_PATH=/data/kornrapatp/imagenetclass-predicted-suppressed-reweighted 31 | PREDICTION_MODEL_PATH=/data/kornrapatp/results/ImagenetClass 32 | 33 | EPS=0.03137254901 # Set epsilon for adversarial training and evaluation 34 | MODEL=part-seg-only # Define model to create (see naming in README) 35 | 36 | 37 | 38 | # # train a segmenter using this command (use main.py and part-imagenet-pseudo as dataset) 39 | # CUDA_VISIBLE_DEVICES=$GPU torchrun \ 40 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 41 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 42 | # main.py \ 43 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 44 | # --seg-backbone resnet101 --seg-arch deeplabv3plus --full-precision --pretrained \ 45 | # --data $DATAPATH --seg-label-dir $SEGPATH --seg-labels 599 --dataset $DATASET --workers $NUM_WORKERS \ 46 | # --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 47 | # --adv-train none --epsilon $EPS --atk-norm Linf --adv-beta 0.8 \ 48 | # --seg-const-trn 0.5 --semi-label 1 \ 49 | # --output-dir $PREDICTION_MODEL_PATH --experiment $MODEL 50 | 51 | # # preprocess the dataset 52 | # python3 pre_prediction_imagenet.py --old-dataset $OLD_DATASET --new-dataset $NEW_DATASET \ 53 | # --jpeg-path $JPEG_PATH --num-new-samples $NUM_NEW_SAMPLES --prediction-path $PREDICTION_PATH 54 | 55 | # # get predicted masks 56 | # CUDA_VISIBLE_DEVICES=$GPU torchrun \ 57 | # --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 58 | # --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 59 | # main_pseudo.py \ 60 | # --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 61 | # --seg-backbone resnet101 --seg-arch deeplabv3plus --full-precision --pretrained \ 62 | # --data $DATAPATH --seg-label-dir $NEW_SEGPATH --seg-labels 599 --dataset $DATASET --workers $NUM_WORKERS \ 63 | # --print-freq 50 --epochs 150 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 64 | # --adv-train none --epsilon $EPS --atk-norm Linf --adv-beta 0.8 \ 65 | # --seg-const-trn 0.5 --semi-label 1 --prediction-path $PREDICTION_PATH \ 66 | # --output-dir $PREDICTION_MODEL_PATH --experiment $MODEL --evaluate 67 | 68 | # postprocess the dataset 69 | python3 post_prediction_imagenet.py --old-dataset $OLD_DATASET --new-dataset $NEW_DATASET \ 70 | --jpeg-path $JPEG_PATH --num-new-samples $NUM_NEW_SAMPLES --prediction-path $PREDICTION_PATH 71 | -------------------------------------------------------------------------------- /scripts/run_slurm_1080ti.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #SBATCH --job-name=part-model 3 | #SBATCH --account=fc_wagner 4 | #SBATCH --partition=savio2_1080ti 5 | # Number of nodes: 6 | #SBATCH --nodes=1 7 | # Number of tasks (one for each GPU desired for use case) (example): 8 | #SBATCH --ntasks=4 9 | # Processors per task (please always specify the total number of processors twice the number of GPUs): 10 | #SBATCH --cpus-per-task=2 11 | #Number of GPUs, this can be in the format of "gpu:[1-4]", or "gpu:K80:[1-4] with the type included 12 | #SBATCH --gres=gpu:4 13 | #SBATCH --time=24:00:00 14 | #SBATCH --output slurm-%j-exp138.out 15 | ## Command(s) to run: 16 | source /global/home/users/$USER/.bash_profile 17 | module purge 18 | module load python 19 | source activate /global/scratch/users/$USER/pt 20 | 21 | # bash scripts/run1-3.sh # 1096 22 | # bash scripts/run1-4.sh # 1098 23 | # bash scripts/run2-1.sh # 1100 24 | # bash scripts/run2-2.sh # 1102 25 | # bash scripts/run2-3.sh # 1104 26 | # bash scripts/run2-4.sh # 138 27 | bash scripts/run3-1.sh # 1106 28 | # bash scripts/run3-2.sh # 1108 29 | # bash scripts/run3-3.sh # 1110 30 | -------------------------------------------------------------------------------- /scripts/run_slurm_2080ti.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #SBATCH --job-name=part-model 3 | #SBATCH --account=fc_wagner 4 | #SBATCH --partition=savio3_gpu 5 | # Number of nodes: 6 | #SBATCH --nodes=1 7 | # Number of tasks (one for each GPU desired for use case) (example): 8 | #SBATCH --ntasks=2 9 | # Processors per task (please always specify the total number of processors twice the number of GPUs): 10 | #SBATCH --cpus-per-task=2 11 | #Number of GPUs, this can be in the format of "gpu:[1-4]", or "gpu:K80:[1-4] with the type included 12 | #SBATCH --gres=gpu:GTX2080TI:2 13 | #SBATCH --time=12:00:00 14 | #SBATCH --output slurm-%j-ct_kp_0.5.out 15 | ## Command(s) to run: 16 | source /global/home/users/$USER/.bash_profile 17 | module purge 18 | module load python 19 | source activate /global/scratch/users/$USER/pt 20 | 21 | # bash scripts/run1-1.sh # 22 | # bash scripts/run1-2.sh # ct_kp_0.4 23 | # bash scripts/run1-3.sh # ct_kp_0.6 24 | bash scripts/run1-4.sh # ct_kp_0.5 25 | # bash scripts/run2-1.sh # box10 26 | # bash scripts/run2-2.sh # box11 27 | # bash scripts/run2-3.sh # 2nd_gt_random/0.0/ts 28 | # bash scripts/run2-4.sh # random/0.0/ts 29 | # bash scripts/run3-1.sh # 2nd_pred_by_scores/0.0/ts 30 | # bash scripts/run3-2.sh # 2nd_pred_by_scores/0.1 31 | # bash scripts/run3-3.sh # untargeted/0.0/ts 32 | # bash scripts/run3-4.sh # 2126 33 | 34 | # bash scripts/run4.sh # 2020-2021 35 | # bash scripts/run3.sh # 2143 36 | 37 | # bash scripts/run_reds.sh 38 | -------------------------------------------------------------------------------- /scripts/run_slurm_cpu.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #SBATCH --job-name=part-model 3 | #SBATCH --account=fc_wagner 4 | #SBATCH --partition=savio 5 | # Number of nodes: 6 | #SBATCH --nodes=1 7 | # Processors per task (please always specify the total number of processors twice the number of GPUs): 8 | #SBATCH --cpus-per-task=20 9 | #Number of GPUs, this can be in the format of "gpu:[1-4]", or "gpu:K80:[1-4] with the type included 10 | #xSBATCH --gres=gpu:GTX2080TI:2 11 | #xSBATCH --qos=v100_gpu3_normal 12 | #SBATCH --time=00:30:00 13 | #SBATCH --output slurm-%j.out 14 | ## Command(s) to run: 15 | source /global/home/users/$USER/.bash_profile 16 | module purge 17 | module load python 18 | source activate /global/scratch/users/$USER/pt 19 | sh scripts/prepare_data.sh 20 | -------------------------------------------------------------------------------- /scripts/run_slurm_v100.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | #SBATCH --job-name=part-model 3 | #SBATCH --account=fc_wagner 4 | #SBATCH --partition=savio3_gpu 5 | # Number of nodes: 6 | #SBATCH --nodes=1 7 | # Number of tasks (one for each GPU desired for use case) (example): 8 | #SBATCH --ntasks=2 9 | # Processors per task (please always specify the total number of processors twice the number of GPUs): 10 | #SBATCH --cpus-per-task=4 11 | #Number of GPUs, this can be in the format of "gpu:[1-4]", or "gpu:K80:[1-4] with the type included 12 | #SBATCH --gres=gpu:V100:2 13 | #SBATCH --qos=v100_gpu3_normal 14 | #SBATCH --time=24:00:00 15 | #SBATCH --output slurm-%j-v100-exp-1121.out 16 | ## Command(s) to run: 17 | source /global/home/users/$USER/.bash_profile 18 | module purge 19 | module load python 20 | source activate /global/scratch/users/$USER/pt 21 | 22 | bash scripts/run1-1.sh # 1121 23 | # bash scripts/run1-2.sh # 956-957 24 | # bash scripts/run3-4.sh # 1112 25 | -------------------------------------------------------------------------------- /scripts/run_test.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | # export TORCHELASTIC_MAX_RESTARTS=0 3 | # export NCCL_IB_DISABLE=1 4 | # export NCCL_P2P_DISABLE=1 5 | # export NCCL_DEBUG=INFO 6 | ID=1 7 | GPU=0,1,2,3 8 | NUM_GPU=4 9 | BS=32 10 | AA_BS=32 11 | PORT=1000$ID 12 | BACKEND=nccl 13 | # =============================== PASCAL-Part =============================== # 14 | # DATASET=pascal-part 15 | # DATAPATH=~/data/pascal_part/PartImages/aeroplane_bird_car_cat_dog/ 16 | # SEGPATH=$DATAPATH/panoptic-parts/train 17 | # =============================== Cityscapes ================================ # 18 | # DATASET=cityscapes 19 | # DATAPATH=~/data/cityscapes/PartImages/square_rand_pad0.2/ 20 | # SEGPATH=$DATAPATH 21 | # ============================== Part-ImageNet ============================== # 22 | DATASET=part-imagenet 23 | DATAPATH=/global/scratch/users/kornrapatp/PartImageNet/ 24 | SEGPATH=$DATAPATH/PartSegmentations/All/ 25 | # SEGPATH=$DATAPATH/BoxSegmentations/All/ 26 | 27 | # 0.0156862745, 0.03137254901, 0.06274509803 28 | EPS=0.03137254901 29 | 30 | 31 | 32 | for i in {1..5}; do 33 | CUDA_VISIBLE_DEVICES=$GPU torchrun \ 34 | --rdzv_backend=c10d --rdzv_endpoint=127.0.0.1:2940$ID --rdzv_id=$ID \ 35 | --standalone --nnodes=1 --max_restarts 0 --nproc_per_node=$NUM_GPU \ 36 | main.py \ 37 | --dist-url tcp://localhost:$PORT --seed 0 --dist-backend $BACKEND \ 38 | --seg-backbone resnet50 --seg-arch deeplabv3plus --full-precision --pretrained \ 39 | --data $DATAPATH --seg-label-dir $SEGPATH --dataset $DATASET \ 40 | --print-freq 50 --epochs 50 --batch-size $BS --lr 1e-1 --wd 5e-4 \ 41 | --adv-train none --epsilon $EPS --atk-norm Linf --adv-beta 2 \ 42 | --seg-const-trn 0.5 --semi-label 0.3 \ 43 | --output-dir /global/scratch/users/kornrapatp/1111 --experiment part-wbbox-norm_img-centroid-semi && break 44 | sleep 30 45 | done -------------------------------------------------------------------------------- /tif_to_png.py: -------------------------------------------------------------------------------- 1 | import pathlib 2 | from PIL import Image 3 | import argparse 4 | 5 | # Initialize parser 6 | parser = argparse.ArgumentParser() 7 | 8 | # Adding optional argument 9 | parser.add_argument( 10 | "--seg-dataset", 11 | help="path of dataset to convert to png", 12 | default="/data/kornrapatp/PartImageNet/PartSegmentations/All", 13 | type=str, 14 | ) 15 | 16 | # Read arguments from command line 17 | args = parser.parse_args() 18 | 19 | path_to_replace = args.jpeg_path 20 | 21 | current_dir = pathlib.Path(path_to_replace) 22 | 23 | all_tif_files = current_dir.rglob("*.tif") 24 | 25 | for tif_file in all_tif_files: 26 | if tif_file.is_symlink(): 27 | # get the symlink target 28 | target = tif_file.readlink() 29 | print(target) 30 | # create a new symlink with the same name with suffix .png 31 | tif_file.with_suffix(".png").symlink_to(target.with_suffix(".png")) 32 | else: 33 | Image.open(tif_file).save(tif_file.with_suffix(".png")) 34 | # remove 35 | tif_file.unlink(missing_ok=True) 36 | --------------------------------------------------------------------------------