Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Online SSIM and MS-SSIM Computation #1231

Merged
merged 21 commits into from
Sep 29, 2022
Merged

Conversation

Queuecumber
Copy link
Contributor

@Queuecumber Queuecumber commented Sep 19, 2022

What does this PR do?

Working on #1224
Fixes #1213

This PR fixes SSIM and MS-SSIM by allowing them to compute using running stats. This will make them actually usable in real situations.

Also I fixed several bugs that I found in these modules and changed the defaults to match pytorch-msssim

All tests should pass but to be honest I'm not sure how good those tests were anyway

I verified that this matches the known-good pytorch-mssim library to ~6 decimal places with default settings which was not the case before and I will be using this code in production in the coming week to see how it does.

Before submitting

  • Was this discussed/approved 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?

Make sure you had fun coding 🙃

Queuecumber and others added 7 commits September 14, 2022 20:31
Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
@Queuecumber
Copy link
Contributor Author

I'm not sure what the best way to fix the mypy failure is, would appreciate some guidance on that. Explicitly annotating the variable as a Tensor didn't do it

@Queuecumber
Copy link
Contributor Author

@SkafteNicki I could use some eyes on this, I've been using it in production and it seems to work well

Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
@Queuecumber Queuecumber marked this pull request as ready for review September 21, 2022 15:08
@Queuecumber Queuecumber changed the title Rework SSIM and MS-SSIM Online SSIM and MS-SSIM Computation Sep 22, 2022
Copy link
Member

@justusschock justusschock left a comment

Choose a reason for hiding this comment

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

It seems strange to me, that compute is called in update (this kind of breaks our paradigm), although the implementation seems to be correct.

src/torchmetrics/image/ssim.py Show resolved Hide resolved
src/torchmetrics/functional/image/ssim.py Show resolved Hide resolved
@justusschock
Copy link
Member

Could you also add tests against pytorch-mssim as this seems to also support batching (which is lacking in sklearn)?

@Queuecumber
Copy link
Contributor Author

Yeah I will add tests against pytorch_msssim, do you want to leave the sklearn tests in place?

BTW we found an interesting issue with pytorch_msssim while testing this PR internally. It really doesn't play nice with fp16 AMP. Torchmetrics handles it much better. Irrelevant for the test cases but thought I would point it out

@Queuecumber
Copy link
Contributor Author

It seems strange to me, that compute is called in update (this kind of breaks our paradigm), although the implementation seems to be correct.

There should be a way to refactor it so that it looks more like the PSNR metric which does an _update call which is computing sum of squared error and then compute is more or less just reducing it.

I'm fine implementing that refactor if you want

@Queuecumber
Copy link
Contributor Author

Just following up, my plan is to do the following

  1. Rename _ssim_update to _ssim_check_inputs (open to suggestions on that name)
  2. Rename _ssim_compute to _ssim_update and remove the reduction logic (but unsure of where to put the sum)
  3. Add _ssim_compute which does the reduction (a little weird since in this case it's literally just calling reduce)
  4. Update structural_similarity_index_measure to call these new functions correctly

@justusschock
Copy link
Member

do you want to leave the sklearn tests in place?

Ideally yes, as this is the most trusted lib for non-batched stuff. Maybe we can then reduce the tests compared to sklearn and add the more thorough testing with the other lib, but some sklearn tests should stay.

Just following up, my plan is to do the following

Rename _ssim_update to _ssim_check_inputs (open to suggestions on that name)
Rename _ssim_compute to _ssim_update and remove the reduction logic (but unsure of where to put the sum)
Add _ssim_compute which does the reduction (a little weird since in this case it's literally just calling reduce)
Update structural_similarity_index_measure to call these new functions correctly

Sounds good to me!

@codecov
Copy link

codecov bot commented Sep 26, 2022

Codecov Report

Merging #1231 (6748d7c) into master (3bf1491) will decrease coverage by 49%.
The diff coverage is 79%.

Additional details and impacted files
@@           Coverage Diff            @@
##           master   #1231     +/-   ##
========================================
- Coverage      86%     37%    -49%     
========================================
  Files         193     193             
  Lines       11372   11406     +34     
========================================
- Hits         9754    4245   -5509     
- Misses       1618    7161   +5543     

Queuecumber and others added 4 commits September 28, 2022 08:58
Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
@Queuecumber
Copy link
Contributor Author

Queuecumber commented Sep 28, 2022

OK I did the refactoring we discussed and added test cases for SSIM against pytorch-msssim (the MS-SSIM tests were already using it)

pre-commit-ci bot and others added 3 commits September 28, 2022 13:55
Signed-off-by: Max Ehrlich <max.ehr@gmail.com>
@SkafteNicki SkafteNicki added the enhancement New feature or request label Sep 29, 2022
@SkafteNicki SkafteNicki added this to the v0.11 milestone Sep 29, 2022
Copy link
Member

@SkafteNicki SkafteNicki left a comment

Choose a reason for hiding this comment

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

LGTM

requirements/test.txt Outdated Show resolved Hide resolved
Copy link
Member

@justusschock justusschock left a comment

Choose a reason for hiding this comment

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

Thanks for doing. this refactor! LGTM!

@mergify mergify bot added the ready label Sep 29, 2022
@SkafteNicki SkafteNicki enabled auto-merge (squash) September 29, 2022 13:44
@SkafteNicki SkafteNicki merged commit 9b19a92 into Lightning-AI:master Sep 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug suspected in MS-SSIM\SSIM metric
4 participants