π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(contrib): explain unsupported VisualPrompter configurations (#6211)
Wentao Tong38190fe61e0a
diff --git a/changelog.d/6211.fixed.md b/changelog.d/6211.fixed.mdnew file mode 100644index 000000000..00fe7def8--- /dev/null+++ b/changelog.d/6211.fixed.md@@ -0,0 +1 @@+`VisualPrompter` now reports the unsupported configuration type and the accepted `SamConfig` or `None` alternatives instead of raising an empty `NotImplementedError`.diff --git a/kornia/contrib/visual_prompter.py b/kornia/contrib/visual_prompter.pyindex 062626641..224af6f78 100644--- a/kornia/contrib/visual_prompter.py+++ b/kornia/contrib/visual_prompter.py@@ -101,6 +101,9 @@ class VisualPrompter:device: The desired device to use the model.dtype: The desired dtype to use the model.+ Raises:+ NotImplementedError: If ``config`` is neither a :class:`~kornia.models.sam.SamConfig` nor ``None``.+Example:>>> # prompter = VisualPrompter() # Will load the vit h for default>>> # You can load a custom SAM type for modifying the config@@ -123,6 +126,11 @@ class VisualPrompter:device: Optional[torch.device] = None,dtype: Optional[torch.dtype] = None,) -> None:+ """Build the configured SAM model and its preprocessing transforms.++ A missing config selects pretrained ViT-H. The model and normalization statistics use the requested+ device and dtype; unsupported configuration types raise ``NotImplementedError``.+ """super().__init__()if config is None:config = SamConfig(model_type="vit_h", pretrained=True)@@ -137,7 +145,7 @@ class VisualPrompter:torch.tensor([58.395, 57.12, 57.375], device=device, dtype=dtype) / 255)else:- raise NotImplementedError+ raise NotImplementedError(f"Unsupported config type: {type(config).__name__}. Expected SamConfig or None.")self.model = self.model.to(device=device, dtype=dtype)self.transforms = AugmentationSequential(*transforms, same_on_batch=True)diff --git a/tests/contrib/test_visual_prompter.py b/tests/contrib/test_visual_prompter.pyindex c2f329f4a..5e61a1c88 100644--- a/tests/contrib/test_visual_prompter.py+++ b/tests/contrib/test_visual_prompter.py@@ -85,16 +85,34 @@ def _prompter_with_stub_encoder(device: torch.device, dtype: torch.dtype) -> Visclass TestVisualPrompter(BaseTester):+ @pytest.mark.parametrize(+ "config, type_name",+ [+ pytest.param("vit_b", "str", id="model-name"),+ pytest.param({"model_type": "vit_b"}, "dict", id="config-dict"),+ pytest.param(nn.Identity(), "Identity", id="model-instance"),+ ],+ )+ def test_unsupported_config(self, config: object, type_name: str) -> None:+ """Unsupported configuration types report the accepted alternatives."""+ with pytest.raises(NotImplementedError, match=rf"config.*{type_name}.*SamConfig.*None"):+ VisualPrompter(config)+def test_default_config(self, monkeypatch):+ """Default construction requests pretrained ViT-H without downloading weights in this test."""captured_configs = []class FakeSam:+ """Minimal SAM stand-in exposing the encoder size needed during construction."""+image_encoder = SimpleNamespace(img_size=1024)def to(self, **kwargs):+ """Accept device and dtype requests without allocating model parameters."""return selfdef fake_from_config(config):+ """Record the selected configuration and return the lightweight SAM stand-in."""captured_configs.append(config)return FakeSam()