Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pretrained issue #124

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions terratorch/models/backbones/prithvi_swin.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,30 @@ def _create_swin_mmseg_transformer(
def checkpoint_filter_wrapper_fn(state_dict, model):
return checkpoint_filter_fn(state_dict, model, pretrained_bands, model_bands)

model: MMSegSwinTransformer = build_model_with_cfg(
MMSegSwinTransformer,
variant,
pretrained,
pretrained_filter_fn=checkpoint_filter_wrapper_fn,
pretrained_strict=False,
feature_cfg={"flatten_sequential": True, "out_indices": out_indices},
**kwargs,
)
# When the pretrained configuration is not available in HF, we shift to
# pretrained=False
try:
model: MMSegSwinTransformer = build_model_with_cfg(
MMSegSwinTransformer,
variant,
pretrained,
pretrained_filter_fn=checkpoint_filter_wrapper_fn,
pretrained_strict=False,
feature_cfg={"flatten_sequential": True, "out_indices": out_indices},
**kwargs,
)
except RuntimeError:
print(f"No pretrained configuration was found for the model {variant}.")
model: MMSegSwinTransformer = build_model_with_cfg(
MMSegSwinTransformer,
variant,
False,
Joao-L-S-Almeida marked this conversation as resolved.
Show resolved Hide resolved
pretrained_filter_fn=checkpoint_filter_wrapper_fn,
pretrained_strict=False,
feature_cfg={"flatten_sequential": True, "out_indices": out_indices},
**kwargs,
)

model.pretrained_bands = pretrained_bands
model.model_bands = model_bands

Expand Down
32 changes: 23 additions & 9 deletions terratorch/models/backbones/prithvi_vit.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,29 @@ def _create_prithvi(
def checkpoint_filter_wrapper_fn(state_dict, model):
return checkpoint_filter_fn(state_dict, model, pretrained_bands, model_bands)

model = build_model_with_cfg(
TemporalViTEncoder,
variant,
pretrained,
pretrained_filter_fn=checkpoint_filter_wrapper_fn,
pretrained_strict=True,
encoder_only=encoder_only,
**kwargs,
)
# When the pretrained configuration is not available in HF, we shift to
# pretrained=False
try:
model = build_model_with_cfg(
TemporalViTEncoder,
variant,
pretrained,
pretrained_filter_fn=checkpoint_filter_wrapper_fn,
pretrained_strict=True,
encoder_only=encoder_only,
**kwargs,
)
except RuntimeError:
print(f"No pretrained configuration was found for the model {variant}.")
model = build_model_with_cfg(
TemporalViTEncoder,
variant,
False,
Joao-L-S-Almeida marked this conversation as resolved.
Show resolved Hide resolved
pretrained_filter_fn=checkpoint_filter_wrapper_fn,
pretrained_strict=True,
encoder_only=encoder_only,
**kwargs,
)

if encoder_only:
default_out_indices = list(range(len(model.blocks)))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trainer:
init_args:
monitor: val/loss
patience: 100
max_epochs: 2
max_epochs: 3
check_val_every_n_epoch: 1
log_every_n_steps: 20
enable_checkpointing: true
Expand Down
Loading