π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
test: speed up VisualPrompter and SAM3 coverage (#4163)
Dmytro Mishkine1591eebbc3f
diff --git a/tests/contrib/test_visual_prompter.py b/tests/contrib/test_visual_prompter.pyindex 189e3c7de..acad3c7aa 100644--- a/tests/contrib/test_visual_prompter.py+++ b/tests/contrib/test_visual_prompter.py@@ -15,21 +15,45 @@# limitations under the License.#+from types import SimpleNamespace+import pytestimport torchfrom kornia.contrib.visual_prompter import VisualPrompter+from kornia.models.sam import Sam, SamConfigfrom testing.base import BaseTesterclass TestVisualPrompter(BaseTester):- @pytest.mark.slow+ def test_default_config(self, monkeypatch):+ captured_configs = []++ class FakeSam:+ image_encoder = SimpleNamespace(img_size=1024)++ def to(self, **kwargs):+ return self++ def fake_from_config(config):+ captured_configs.append(config)+ return FakeSam()++ monkeypatch.setattr(Sam, "from_config", staticmethod(fake_from_config))++ prompter = VisualPrompter()++ assert prompter.model.image_encoder.img_size == 1024+ assert len(captured_configs) == 1+ assert captured_configs[0].model_type == "vit_h"+ assert captured_configs[0].pretrained is True+def test_smoke(self, device, dtype):if dtype not in (torch.float32, torch.float16):pytest.skip("VisualPrompter (SAM) primarily supports float32 and float16")- prompter = VisualPrompter(device=device, dtype=dtype)+ prompter = VisualPrompter(SamConfig("mobile_sam"), device=device, dtype=dtype)assert prompter is not Noneassert not prompter.is_image_set@@ -37,7 +61,7 @@ class TestVisualPrompter(BaseTester):def test_batching_pipeline(self, device, dtype):if dtype not in (torch.float32, torch.float16):pytest.skip("VisualPrompter (SAM) primarily supports float32 and float16")- prompter = VisualPrompter(device=device, dtype=dtype)+ prompter = VisualPrompter(SamConfig("mobile_sam"), device=device, dtype=dtype)batch_size = 2image = torch.rand(batch_size, 3, 256, 256).to(device=device, dtype=dtype)@@ -56,14 +80,6 @@ class TestVisualPrompter(BaseTester):assert results.logits.shape == (batch_size, 3, 256, 256)- def test_exception(self, device, dtype):-- prompter = VisualPrompter(device=device, dtype=dtype)-- image = torch.rand(1, 2, 3, 256, 256).to(device=device, dtype=dtype)- with pytest.raises(Exception):- prompter.set_image(image)-def test_gradcheck(self, device):pytest.skip("Gradcheck is not currently applicable for VisualPrompter inference.")diff --git a/tests/models/sam3/test_sam3_architecture.py b/tests/models/sam3/test_sam3_architecture.pyindex 7256a325c..b26954952 100644--- a/tests/models/sam3/test_sam3_architecture.py+++ b/tests/models/sam3/test_sam3_architecture.py@@ -67,7 +67,7 @@ class TestSam3Common(BaseTester):class TestImageEncoderHiera(BaseTester):"""Test ImageEncoderHiera architecture."""- @pytest.mark.parametrize("img_size,embed_dim", [(512, 128), (1024, 256)])+ @pytest.mark.parametrize("img_size,embed_dim", [(256, 128), (512, 256)])def test_image_encoder_hiera_smoke(self, device: str, img_size: int, embed_dim: int) -> None:"""Test ImageEncoderHiera basic functionality with different configs."""patch_size = 16@@ -91,16 +91,16 @@ class TestImageEncoderHiera(BaseTester):def test_image_encoder_hiera_cardinality(self, device: str, batch_size: int) -> None:"""Test ImageEncoderHiera output cardinality with different batch sizes."""encoder = ImageEncoderHiera(- img_size=512,+ img_size=256,patch_size=16,- embed_dim=256,+ embed_dim=128,depth=2,- num_heads=8,+ num_heads=4,).to(device)- x = torch.randn(batch_size, 3, 512, 512, device=device)+ x = torch.randn(batch_size, 3, 256, 256, device=device)features = encoder(x)- assert features.shape == (batch_size, (512 // 16) ** 2, 256)+ assert features.shape == (batch_size, (256 // 16) ** 2, 128)def test_image_encoder_hiera_output_shape(self) -> None:"""Test output shape computation method."""