Skip to content

Fix RASE dividing the target mean by window_size**2 twice - #3677

Open
harshil-sri wants to merge 2 commits into
Lightning-AI:masterfrom
harshil-sri:fix-rase-double-window-normalisation
Open

harshil-sri wants to merge 2 commits into
Lightning-AI:masterfrom
harshil-sri:fix-rase-double-window-normalisation

Conversation

@harshil-sri

Copy link
Copy Markdown

What does this PR do?

Fixes #3676

_rase_update divided the output of _uniform_filter by window_size**2, but _uniform_filter already returns the local mean (its kernel is normalised in _uniform_weight_bias_conv2d). The mean radiance in the RASE formula was therefore window_size**2 too small, and relative_average_spectral_error / RelativeAverageSpectralError came out window_size**2 times too large.

target = torch.full((1, 3, 32, 32), 0.5, dtype=torch.float64)
relative_average_spectral_error(target + 0.01, target, window_size=8)
# before: tensor(128.0000), after: tensor(2.0000)  (expected 100 * 0.01 / 0.5 = 2.0)

For a random 32x32 image the old value was 16x sewar.rase with window_size=4 and 64x with window_size=8. After the change they agree to float precision.

Changes:

  • remove the extra / (window_size**2) in _rase_update
  • remove the same line from _reference_sewar_rase in the tests, which had copied it (that is why CI never caught this)
  • add test_rase_single_image_matches_sewar, which compares single images against sewar.rase directly for window sizes 3, 4 and 8, for both the functional and the class
  • add test_rase_constant_offset with the constant image case above
  • update the docstring example outputs (5326.40 to 83.22, which is 5326.40 / 64) and add a CHANGELOG entry

Tested with pytest tests/unittests/image/test_rase.py tests/unittests/image/test_rmse_sw.py (24 passed) and the doctests of both rase modules. With the source change reverted, all 12 tests in test_rase.py fail. Ruff check and format pass on the changed files.

This changes the values users get from RASE, so anyone comparing against old numbers will see them drop by window_size**2. The ranking of models at a fixed window size does not change. #3568 and #3613 touch the same files but different lines, so either order of merging should be a trivial rebase.

I used Claude Code to help find and fix this.

Before submitting
  • Was this discussed/agreed via a Github issue? (no need for typos and docs improvements)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure to update the docs?
  • Did you write any new necessary tests?
PR review

Anyone in the community is free to review the PR once the tests have passed.
If we didn't discuss your PR in Github issues there's a high chance it will not be merged.

Did you have fun?

Yes.

_uniform_filter already returns the local mean, but _rase_update divided
it by window_size**2 again, so RASE came out window_size**2 times too
large (64x with the default window). The test reference copied the same
line, so it could not catch this. Compare single images against
sewar.rase directly and add a constant-offset check.

@atishay-kasliwal atishay-kasliwal left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Compared relative_average_spectral_error with sewar.full_ref.rase on random 3x24x24 images. Here it matches sewar for window sizes 3 and 8 (7.731, 7.926); master gave 69.58 and 507.235, off by about window_size**2.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

RelativeAverageSpectralError is window_size**2 times too large

2 participants