Skip to content

Preserve SSIM gradients through zero covariance - #136

Open
sylvesterkaczmarek wants to merge 2 commits into
google-deepmind:masterfrom
sylvesterkaczmarek:fix/ssim-zero-covariance-gradients-20261006
Open

sylvesterkaczmarek wants to merge 2 commits into
google-deepmind:masterfrom
sylvesterkaczmarek:fix/ssim-zero-covariance-gradients-20261006

Conversation

@sylvesterkaczmarek

@sylvesterkaczmarek sylvesterkaczmarek commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #135.

Express the existing symmetric covariance bound using jnp.clip. The former sign/absolute-value expression had the same forward values but discarded covariance derivatives at zero, even when the clipping bound was inactive. This restores the correlation-learning term without changing filtering, stabilizers, variance floors or the output shape.

Validation

python -m pytest -q dm_pix/_src/metrics_test.py

Current rebased head: 14 SSIM-gradient regressions and 16 adjacent SSIM tests passed; 4 existing variants skipped. An earlier full metrics run passed 46 with 8 skipped. Fourteen new tests compare the public SSIM value and derivatives with an independent centered-moment formula, finite differences, eager/JIT execution, batches and channels. They cover a balanced Gaussian filter, a custom mean filter, the default Gaussian sigma with a zero second image, and a parameter update away from zero correlation. Positive/negative covariance, identity and anticorrelation preserve forward values.

Tests use small synthetic arrays and real JAX operations. No model training, accelerator execution or image datasets were used. Other image-metric functions are unchanged.

Tested locally on macOS CPU using real module imports. New test formatting, scoped static checks, syntax checks and git diff --check pass. No dependency or workflow changes.

@sylvesterkaczmarek

Copy link
Copy Markdown
Contributor Author

Following up on #136, #132. Could a maintainer take a look when available? Please let me know if any need changes or a different scope.

@claudiofantacci claudiofantacci self-assigned this Oct 18, 2026
return a, b


class SsimCovarianceGradientTest(parameterized.TestCase):

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just like the other reviews, please move these tests under metrics_test.py.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moved to metrics_test.py. The 14 new tests and 16 adjacent SSIM tests pass on current master.

Signed-off-by: Sylvester Kaczmarek <16242628+sylvesterkaczmarek@users.noreply.github.com>
@sylvesterkaczmarek
sylvesterkaczmarek force-pushed the fix/ssim-zero-covariance-gradients-20261006 branch from e846bc0 to 1c55897 Compare October 19, 2026 15:50

@claudiofantacci claudiofantacci left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@claudiofantacci claudiofantacci added the bug Something isn't working label Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SSIM covariance clipping suppresses gradients at zero correlation

2 participants