π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(filters): explain unsupported dissolving versions (#6202)
Wentao Tong1446b154f26e
diff --git a/changelog.d/6202.fixed.md b/changelog.d/6202.fixed.mdnew file mode 100644index 000000000..dc73963f0--- /dev/null+++ b/changelog.d/6202.fixed.md@@ -0,0 +1 @@+`StableDiffusionDissolving` now reports the unsupported version and lists the supported versions when raising `NotImplementedError`.diff --git a/kornia/augmentation/_2d/intensity/dissolving.py b/kornia/augmentation/_2d/intensity/dissolving.pyindex e9905de69..71cf1fa76 100644--- a/kornia/augmentation/_2d/intensity/dissolving.py+++ b/kornia/augmentation/_2d/intensity/dissolving.py@@ -31,7 +31,8 @@ class RandomDissolving(IntensityAugmentationBase2D):Based on :cite:`shi2024dissolving`, the dissolving transformation is essentially applying one-stepreverse diffusion. Our implementation currently supports the HuggingFace implementations of SD 1.4, SD 1.5- and SD XL; once ``diffusers`` is importable, any other ``version`` raises a bare ``NotImplementedError``+ and SD XL; once ``diffusers`` is importable, any other ``version`` raises ``NotImplementedError``+ with the unsupported value and the supported versions(without it, construction fails on the optional-dependency prompt before the version is looked at)... list-table::diff --git a/kornia/filters/dissolving.py b/kornia/filters/dissolving.pyindex 96e05039f..20a2432dd 100644--- a/kornia/filters/dissolving.py+++ b/kornia/filters/dissolving.py@@ -272,7 +272,9 @@ class StableDiffusionDissolving(ImageModule):)is_sdxl = Trueelse:- raise NotImplementedError+ raise NotImplementedError(+ f"Unsupported Stable Diffusion version {version!r}. Supported versions are '1.4', '1.5', and 'xl'."+ )self.model = _DissolvingWraper_HF(self._sdm_model, num_ddim_steps=1000, is_sdxl=is_sdxl)diff --git a/tests/filters/test_dissolving.py b/tests/filters/test_dissolving.pyindex 708590d8a..7502306dc 100644--- a/tests/filters/test_dissolving.py+++ b/tests/filters/test_dissolving.py@@ -15,6 +15,10 @@# limitations under the License.#+import re+from types import SimpleNamespace+from unittest.mock import Mock+import pytestimport torch@@ -22,17 +26,52 @@ from kornia.filters.dissolving import StableDiffusionDissolvingfrom testing.base import BaseTester-# `StableDiffusionDissolving` lazily imports `diffusers`, which ships in the `kornia[sd]` extra and-# is therefore absent from the default dev environment.-pytest.importorskip("diffusers", reason='`diffusers` is not installed. Install it with: pip install "kornia[sd]"')-WEIGHTS_CACHE_DIR = "weights/"[email protected]_agnostic+class TestStableDiffusionDissolvingVersion(BaseTester):+ @pytest.fixture+ def diffusers_stub(self, monkeypatch):+ stub = SimpleNamespace(DDIMScheduler=Mock(), StableDiffusionPipeline=Mock(), StableDiffusionXLPipeline=Mock())+ for pipeline in (stub.StableDiffusionPipeline, stub.StableDiffusionXLPipeline):+ pipeline.from_pretrained.return_value.scheduler.timesteps = [0]+ monkeypatch.setattr("kornia.filters.dissolving.diffusers", stub)+ return stub++ @pytest.mark.parametrize("version", ["invalid_version", "2.1", ""])+ def test_exception(self, version, diffusers_stub):+ message = f"Unsupported Stable Diffusion version {version!r}. Supported versions are '1.4', '1.5', and 'xl'."+ with pytest.raises(NotImplementedError, match=re.escape(message)):+ StableDiffusionDissolving(version=version)+ diffusers_stub.StableDiffusionPipeline.from_pretrained.assert_not_called()+ diffusers_stub.StableDiffusionXLPipeline.from_pretrained.assert_not_called()++ @pytest.mark.parametrize(+ "version, model_id",+ [+ ("1.4", "CompVis/stable-diffusion-v1-4"),+ ("1.5", "runwayml/stable-diffusion-v1-5"),+ ("xl", "stabilityai/stable-diffusion-xl-base-1.0"),+ ],+ )+ def test_supported_version(self, version, model_id, diffusers_stub):+ model = StableDiffusionDissolving(version=version)+ pipeline = (+ diffusers_stub.StableDiffusionXLPipeline if version == "xl" else diffusers_stub.StableDiffusionPipeline+ )+ pipeline.from_pretrained.assert_called_once()+ assert pipeline.from_pretrained.call_args.args == (model_id,)+ assert model.model.is_sdxl is (version == "xl")++@pytest.mark.slowclass TestStableDiffusionDissolving(BaseTester):@pytest.fixture(scope="class")def sdm_2_1(self):+ pytest.importorskip(+ "diffusers", reason='`diffusers` is not installed. Install it with: pip install "kornia[sd]"'+ )return StableDiffusionDissolving(version="1.5", cache_dir=WEIGHTS_CACHE_DIR)@pytest.fixture(scope="class")@@ -63,7 +102,3 @@ class TestStableDiffusionDissolving(BaseTester):dissolved_image = sdm_2_1(dummy_image, step_number)assert isinstance(dissolved_image, torch.Tensor), "Dissolve failed"assert dissolved_image.shape == dummy_image.shape, "Dissolved image shape mismatch"-- def test_invalid_version(self):- with pytest.raises(NotImplementedError):- StableDiffusionDissolving(version="invalid_version")