From 254b54f3bf22cd56dd507abd4fdafac71aaa752f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lucas=20de=20Sousa=20Almeida?= Date: Wed, 11 Dec 2024 14:05:22 -0300 Subject: [PATCH 1/3] Testing finetuning for more Prithvi-2 backbones MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Lucas de Sousa Almeida --- ...nufactured-finetune_prithvi_eo_v2_300.yaml | 150 ++++++++++++++++++ ...nufactured-finetune_prithvi_eo_v2_600.yaml | 150 ++++++++++++++++++ ...manufactured-finetune_prithvi_vit_300.yaml | 2 +- 3 files changed, 301 insertions(+), 1 deletion(-) create mode 100644 tests/resources/configs/manufactured-finetune_prithvi_eo_v2_300.yaml create mode 100644 tests/resources/configs/manufactured-finetune_prithvi_eo_v2_600.yaml diff --git a/tests/resources/configs/manufactured-finetune_prithvi_eo_v2_300.yaml b/tests/resources/configs/manufactured-finetune_prithvi_eo_v2_300.yaml new file mode 100644 index 00000000..3e44a1c5 --- /dev/null +++ b/tests/resources/configs/manufactured-finetune_prithvi_eo_v2_300.yaml @@ -0,0 +1,150 @@ +# lightning.pytorch==2.1.1 +seed_everything: 42 +trainer: + accelerator: cpu + strategy: auto + devices: auto + num_nodes: 1 + # precision: 16-mixed + logger: + class_path: TensorBoardLogger + init_args: + save_dir: tests/ + name: all_ecos_random + callbacks: + - class_path: RichProgressBar + - class_path: LearningRateMonitor + init_args: + logging_interval: epoch + - class_path: EarlyStopping + init_args: + monitor: val/loss + patience: 100 + max_epochs: 2 + check_val_every_n_epoch: 1 + log_every_n_steps: 20 + enable_checkpointing: true + default_root_dir: tests/ +data: + class_path: GenericNonGeoPixelwiseRegressionDataModule + init_args: + batch_size: 2 + num_workers: 4 + train_transform: + #- class_path: albumentations.HorizontalFlip + # init_args: + # p: 0.5 + #- class_path: albumentations.Rotate + # init_args: + # limit: 30 + # border_mode: 0 # cv2.BORDER_CONSTANT + # value: 0 + # # mask_value: 1 + # p: 0.5 + - class_path: ToTensorV2 + dataset_bands: + - 0 + - BLUE + - GREEN + - RED + - NIR_NARROW + - SWIR_1 + - SWIR_2 + - 1 + - 2 + - 3 + - 4 + output_bands: + - BLUE + - GREEN + - RED + - NIR_NARROW + - SWIR_1 + - SWIR_2 + rgb_indices: + - 2 + - 1 + - 0 + train_data_root: tests/resources/inputs + train_label_data_root: tests/resources/inputs + val_data_root: tests/resources/inputs + val_label_data_root: tests/resources/inputs + test_data_root: tests/resources/inputs + test_label_data_root: tests/resources/inputs + img_grep: "regression*input*.tif" + label_grep: "regression*label*.tif" + means: + - 547.36707 + - 898.5121 + - 1020.9082 + - 2665.5352 + - 2340.584 + - 1610.1407 + stds: + - 411.4701 + - 558.54065 + - 815.94025 + - 812.4403 + - 1113.7145 + - 1067.641 + no_label_replace: -1 + no_data_replace: 0 + +model: + class_path: terratorch.tasks.PixelwiseRegressionTask + init_args: + model_args: + decoder: UperNetDecoder + pretrained: false + backbone: prithvi_eo_v2_300 + # backbone_pretrained_cfg_overlay: + # file: tests/prithvi_vit_300.pt + backbone_drop_path_rate: 0.3 + # backbone_window_size: 8 + decoder_channels: 64 + num_frames: 1 + in_channels: 6 + bands: + - BLUE + - GREEN + - RED + - NIR_NARROW + - SWIR_1 + - SWIR_2 + head_dropout: 0.5708022831486758 + head_final_act: torch.nn.ReLU + head_learned_upscale_layers: 2 + loss: rmse + #aux_heads: + # - name: aux_head + # decoder: IdentityDecoder + # decoder_args: + # decoder_out_index: 2 + # head_dropout: 0,5 + # head_channel_list: + # - 64 + # head_final_act: torch.nn.ReLU + #aux_loss: + # aux_head: 0.4 + ignore_index: -1 + freeze_backbone: true + freeze_decoder: false + model_factory: PrithviModelFactory + + # uncomment this block for tiled inference + # tiled_inference_parameters: + # h_crop: 224 + # h_stride: 192 + # w_crop: 224 + # w_stride: 192 + # average_patches: true +optimizer: + class_path: torch.optim.AdamW + init_args: + lr: 0.00013524680528283027 + weight_decay: 0.047782217873995426 +lr_scheduler: + class_path: ReduceLROnPlateau + init_args: + monitor: val/loss + diff --git a/tests/resources/configs/manufactured-finetune_prithvi_eo_v2_600.yaml b/tests/resources/configs/manufactured-finetune_prithvi_eo_v2_600.yaml new file mode 100644 index 00000000..292d229c --- /dev/null +++ b/tests/resources/configs/manufactured-finetune_prithvi_eo_v2_600.yaml @@ -0,0 +1,150 @@ +# lightning.pytorch==2.1.1 +seed_everything: 42 +trainer: + accelerator: cpu + strategy: auto + devices: auto + num_nodes: 1 + # precision: 16-mixed + logger: + class_path: TensorBoardLogger + init_args: + save_dir: tests/ + name: all_ecos_random + callbacks: + - class_path: RichProgressBar + - class_path: LearningRateMonitor + init_args: + logging_interval: epoch + - class_path: EarlyStopping + init_args: + monitor: val/loss + patience: 100 + max_epochs: 2 + check_val_every_n_epoch: 1 + log_every_n_steps: 20 + enable_checkpointing: true + default_root_dir: tests/ +data: + class_path: GenericNonGeoPixelwiseRegressionDataModule + init_args: + batch_size: 2 + num_workers: 4 + train_transform: + #- class_path: albumentations.HorizontalFlip + # init_args: + # p: 0.5 + #- class_path: albumentations.Rotate + # init_args: + # limit: 30 + # border_mode: 0 # cv2.BORDER_CONSTANT + # value: 0 + # # mask_value: 1 + # p: 0.5 + - class_path: ToTensorV2 + dataset_bands: + - 0 + - BLUE + - GREEN + - RED + - NIR_NARROW + - SWIR_1 + - SWIR_2 + - 1 + - 2 + - 3 + - 4 + output_bands: + - BLUE + - GREEN + - RED + - NIR_NARROW + - SWIR_1 + - SWIR_2 + rgb_indices: + - 2 + - 1 + - 0 + train_data_root: tests/resources/inputs + train_label_data_root: tests/resources/inputs + val_data_root: tests/resources/inputs + val_label_data_root: tests/resources/inputs + test_data_root: tests/resources/inputs + test_label_data_root: tests/resources/inputs + img_grep: "regression*input*.tif" + label_grep: "regression*label*.tif" + means: + - 547.36707 + - 898.5121 + - 1020.9082 + - 2665.5352 + - 2340.584 + - 1610.1407 + stds: + - 411.4701 + - 558.54065 + - 815.94025 + - 812.4403 + - 1113.7145 + - 1067.641 + no_label_replace: -1 + no_data_replace: 0 + +model: + class_path: terratorch.tasks.PixelwiseRegressionTask + init_args: + model_args: + decoder: UperNetDecoder + pretrained: false + backbone: prithvi_eo_v2_600 + # backbone_pretrained_cfg_overlay: + # file: tests/prithvi_vit_300.pt + backbone_drop_path_rate: 0.3 + # backbone_window_size: 8 + decoder_channels: 64 + num_frames: 1 + in_channels: 6 + bands: + - BLUE + - GREEN + - RED + - NIR_NARROW + - SWIR_1 + - SWIR_2 + head_dropout: 0.5708022831486758 + head_final_act: torch.nn.ReLU + head_learned_upscale_layers: 2 + loss: rmse + #aux_heads: + # - name: aux_head + # decoder: IdentityDecoder + # decoder_args: + # decoder_out_index: 2 + # head_dropout: 0,5 + # head_channel_list: + # - 64 + # head_final_act: torch.nn.ReLU + #aux_loss: + # aux_head: 0.4 + ignore_index: -1 + freeze_backbone: true + freeze_decoder: false + model_factory: PrithviModelFactory + + # uncomment this block for tiled inference + # tiled_inference_parameters: + # h_crop: 224 + # h_stride: 192 + # w_crop: 224 + # w_stride: 192 + # average_patches: true +optimizer: + class_path: torch.optim.AdamW + init_args: + lr: 0.00013524680528283027 + weight_decay: 0.047782217873995426 +lr_scheduler: + class_path: ReduceLROnPlateau + init_args: + monitor: val/loss + diff --git a/tests/resources/configs/manufactured-finetune_prithvi_vit_300.yaml b/tests/resources/configs/manufactured-finetune_prithvi_vit_300.yaml index 2f762232..3e44a1c5 100644 --- a/tests/resources/configs/manufactured-finetune_prithvi_vit_300.yaml +++ b/tests/resources/configs/manufactured-finetune_prithvi_vit_300.yaml @@ -96,7 +96,7 @@ model: model_args: decoder: UperNetDecoder pretrained: false - backbone: prithvi_vit_300 + backbone: prithvi_eo_v2_300 # backbone_pretrained_cfg_overlay: # file: tests/prithvi_vit_300.pt backbone_drop_path_rate: 0.3 From 26b2031db27b398c0c1d971aa8c8b1a6f464cc3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lucas=20de=20Sousa=20Almeida?= Date: Wed, 11 Dec 2024 14:06:39 -0300 Subject: [PATCH 2/3] Complete tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Lucas de Sousa Almeida --- tests/test_finetune.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_finetune.py b/tests/test_finetune.py index e003b7f1..9c06e8da 100644 --- a/tests/test_finetune.py +++ b/tests/test_finetune.py @@ -22,7 +22,7 @@ def setup_and_cleanup(model_name): if os.path.isdir(os.path.join("tests", "all_ecos_random")): shutil.rmtree(os.path.join("tests", "all_ecos_random")) -@pytest.mark.parametrize("model_name", ["prithvi_swin_B", "prithvi_vit_100"]) +@pytest.mark.parametrize("model_name", ["prithvi_swin_B", "prithvi_swin_L", "prithvi_vit_100", "prithvi_eo_v2_300", "prithvi_eo_v2_600"]) @pytest.mark.parametrize("case", ["fit", "test", "validate"]) def test_finetune_multiple_backbones(model_name, case): command_list = [case, "-c", f"tests/resources/configs/manufactured-finetune_{model_name}.yaml"] From 710b7de25840afd313a0cc385a2c52c4f124a5b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Lucas=20de=20Sousa=20Almeida?= Date: Wed, 11 Dec 2024 14:30:13 -0300 Subject: [PATCH 3/3] Testing more backbones for instantiation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: João Lucas de Sousa Almeida --- tests/test_backbones.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tests/test_backbones.py b/tests/test_backbones.py index f76cb6ee..df60fad4 100644 --- a/tests/test_backbones.py +++ b/tests/test_backbones.py @@ -35,7 +35,7 @@ def input_386(): return torch.ones((1, NUM_CHANNELS, 386, 386)) -@pytest.mark.parametrize("model_name", ["prithvi_eo_v1_100", "prithvi_eo_v2_300", "prithvi_swin_B"]) +@pytest.mark.parametrize("model_name", ["prithvi_swin_B", "prithvi_swin_L", "prithvi_eo_v1_100", "prithvi_eo_v2_300", "prithvi_swin_B"]) @pytest.mark.parametrize("test_input", ["input_224", "input_512"]) def test_can_create_backbones_from_timm(model_name, test_input, request): backbone = timm.create_model(model_name, pretrained=False) @@ -43,14 +43,15 @@ def test_can_create_backbones_from_timm(model_name, test_input, request): backbone(input_tensor) gc.collect() -@pytest.mark.parametrize("model_name", ["prithvi_eo_v1_100", "prithvi_eo_v2_300", "prithvi_swin_B"]) +@pytest.mark.parametrize("model_name", ["prithvi_swin_B", "prithvi_swin_L", "prithvi_eo_v1_100", "prithvi_eo_v2_300", "prithvi_swin_B"]) @pytest.mark.parametrize("test_input", ["input_224", "input_512"]) def test_can_create_backbones_from_timm_features_only(model_name, test_input, request): backbone = timm.create_model(model_name, pretrained=False, features_only=True) input_tensor = request.getfixturevalue(test_input) backbone(input_tensor) gc.collect() -@pytest.mark.parametrize("model_name", ["prithvi_eo_v1_100", "prithvi_eo_v2_300", "prithvi_swin_B"]) + +@pytest.mark.parametrize("model_name", ["prithvi_swin_L", "prithvi_swin_L", "prithvi_eo_v1_100", "prithvi_eo_v2_300", "prithvi_swin_B"]) @pytest.mark.parametrize("prefix", ["", "timm_"]) def test_can_create_timm_backbones_from_registry(model_name, input_224, prefix): backbone = BACKBONE_REGISTRY.build(prefix+model_name, pretrained=False) @@ -62,12 +63,14 @@ def test_vit_models_accept_multitemporal(model_name, input_224_multitemporal): backbone = timm.create_model(model_name, pretrained=False, num_frames=NUM_FRAMES) backbone(input_224_multitemporal) gc.collect() + @pytest.mark.parametrize("model_name", ["prithvi_eo_v1_100", "prithvi_eo_v2_300"]) def test_vit_models_non_divisible_input(model_name, input_non_divisible): #padding 'none','constant', 'reflect', 'replicate' or 'circular' default is 'none' backbone = timm.create_model(model_name, pretrained=False, features_only=True, num_frames=NUM_FRAMES, padding='constant') backbone(input_non_divisible) gc.collect() + @pytest.mark.parametrize("model_name", ["prithvi_eo_v1_100", "prithvi_eo_v2_300"]) @pytest.mark.parametrize("patch_size", [8, 16]) @pytest.mark.parametrize("patch_size_time", [1, 2, 4])