π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(models): load the vit_s/32 pretrained weights from their Hub repository (#5277)
awss8ee1a67436f3
diff --git a/.github/download-models-weights.py b/.github/download-models-weights.pyindex 3da534111..e413bc386 100644--- a/.github/download-models-weights.py+++ b/.github/download-models-weights.py@@ -172,6 +172,9 @@ MODELS: dict[str, "str | list[str]"] = {"https://github.com/lyuwenyu/storage/releases/download/v0.1/rtdetr_r18vd_dec3_6x_coco_from_paddle.pth",],"vit_b-16.pth": "https://huggingface.co/kornia/vit_b16_augreg_i21k_r224/resolve/main/vit_b-16.pth",+ # ViT-S/32: TestVisionTransformer.test_from_config_pretrained. Its Hub repository is the+ # one ViT repository off the naming pattern, so the cached copy also guards that URL.+ "vit_s-32.pth": "https://huggingface.co/kornia/vit_s32_i21k_augreg_i21k_r224/resolve/main/vit_s-32.pth",# ALIKED and XFeat: no pytest job builds them pretrained, but# ``generate_examples.main`` does, once per docs build, and the docs job# restores this same cache.diff --git a/changelog.d/5277.fixed.md b/changelog.d/5277.fixed.mdnew file mode 100644index 000000000..ab554bf42--- /dev/null+++ b/changelog.d/5277.fixed.md@@ -0,0 +1 @@+`VisionTransformer.from_config("vit_s/32", pretrained=True)` now loads its weights. The URL pointed at a Hugging Face repository that does not exist, so the download failed with HTTP 401; the checkpoint is hosted at `kornia/vit_s32_i21k_augreg_i21k_r224`.diff --git a/kornia/models/vit.py b/kornia/models/vit.pyindex f9ee9ceef..cb5048276 100644--- a/kornia/models/vit.py+++ b/kornia/models/vit.py@@ -327,9 +327,13 @@ class VisionTransformer(nn.Module):_AVAILABLE_WEIGHTS = ["vit_l/16", "vit_b/16", "vit_s/16", "vit_ti/16", "vit_b/32", "vit_s/32"]+# Hub repositories that do not follow the ``{model_type}{patch_size}_augreg_i21k_r224`` pattern.+_HUB_REPO_OVERRIDES = {"vit_s/32": "vit_s32_i21k_augreg_i21k_r224"}+def _get_weight_url(variant: str) -> str:"""Return the URL of the model weights."""KORNIA_CHECK(variant in _AVAILABLE_WEIGHTS, f"Variant {variant} does not have pre-trained checkpoint")model_type, patch_size = variant.split("/")- return f"https://huggingface.co/kornia/{model_type}{patch_size}_augreg_i21k_r224/resolve/main/{model_type}-{patch_size}.pth"+ repo = _HUB_REPO_OVERRIDES.get(variant, f"{model_type}{patch_size}_augreg_i21k_r224")+ return f"https://huggingface.co/kornia/{repo}/resolve/main/{model_type}-{patch_size}.pth"diff --git a/tests/core/test_weights_prefetch.py b/tests/core/test_weights_prefetch.pyindex 8207bdb75..39b66d7c5 100644--- a/tests/core/test_weights_prefetch.py+++ b/tests/core/test_weights_prefetch.py@@ -282,12 +282,11 @@ NOT_PREFETCHED: dict[str, str] = {"b1-r288.pt": "test_config only checks the URL string; nothing downloads it","b2-r288.pt": "test_config only checks the URL string; nothing downloads it","b3-r288.pt": "test_config only checks the URL string; nothing downloads it",- # ViT: the from_config doctest builds vit_b/16, which is prefetched.+ # ViT: the from_config doctest builds vit_b/16 and a pretrained test builds vit_s/32; both are prefetched."vit_l-16.pth": "no test or doctest selects this ViT variant","vit_s-16.pth": "no test or doctest selects this ViT variant","vit_ti-16.pth": "no test or doctest selects this ViT variant","vit_b-32.pth": "no test or doctest selects this ViT variant",- "vit_s-32.pth": "no test or doctest selects this ViT variant",}diff --git a/tests/models/test_vision_transformer.py b/tests/models/test_vision_transformer.pyindex 160f3bf3b..318075997 100644--- a/tests/models/test_vision_transformer.py+++ b/tests/models/test_vision_transformer.py@@ -69,3 +69,11 @@ class TestVisionTransformer(BaseTester):out = vit(img)assert out.shape == (1, 197, 128)assert len(vit.encoder_results) == 12++ @pytest.mark.device_agnostic+ def test_from_config_pretrained(self):+ # vit_s/32 is the checkpoint whose Hub repository does not follow the URL pattern (#5226).+ model = VisionTransformer.from_config("vit_s/32", pretrained=True).eval()+ with torch.no_grad():+ out = model(torch.rand(1, 3, 224, 224))+ assert out.shape == (1, 50, 384)