Fix: Resolve KeyError and NameError in TorchGeo backbones weight loading#1200
Open
clementgilli wants to merge 2 commits into
Open
Fix: Resolve KeyError and NameError in TorchGeo backbones weight loading#1200clementgilli wants to merge 2 commits into
clementgilli wants to merge 2 commits into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes two bugs that prevent the initialization of several TorchGeo-based backbones when using
pretrained=True.1. Missing TorchGeo Bands (
KeyError)Files modified:
terratorch/models/backbones/torchgeo_resnet.py,terratorch/models/backbones/torchgeo_vit.pyandterratorch/models/backbones/torchgeo_swin_satlas.py"B4","B3", or"B8a". These keys were missing from thelook_up_table, causing aKeyErrorto be raised duringget_pretrained_bands()."B1"to"B9", specifically including the lowercase"B8a"which was causing silent failures) to thelook_up_table. I proactively applied this fix to the Swin Satlas backbone as well to prevent identical crashes.2. Undefined Function (
NameError)File modified:
terratorch/models/backbones/torchgeo_vit.pyload_vit_weightsfunction, theelif weights is not Noneblock calls aremove_keys()function. This function is not defined anywhere in the file or imported, resulting in aNameError: name 'remove_keys' is not defined.remove_keyscall with the explicit dictionary key deletion logic (for k in ["head.weight", "head.bias"]: del ...) that is already correctly implemented a few lines above in the same file.Testing
Tested locally. Models such as
seco_resnet18_sentinel2_rgb_secoandssl4eos12_vit_small_patch16_224_sentinel2_all_dinonow successfully translate band names, process the checkpoints, and load their pretrained weights without crashing.