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

Bug suspected in MS-SSIM\SSIM metric #1213

Closed
amichayfeldman opened this issue Sep 12, 2022 · 3 comments · Fixed by #1231
Closed

Bug suspected in MS-SSIM\SSIM metric #1213

amichayfeldman opened this issue Sep 12, 2022 · 3 comments · Fixed by #1231
Labels
bug / fix Something isn't working help wanted Extra attention is needed topic: Image
Milestone

Comments

@amichayfeldman
Copy link

amichayfeldman commented Sep 12, 2022

MS-SSIM was used to measure two image tensors (BxCxHxW),
set reduction flag to None to get the losses without any reduction.

When I executed it, I got an error of size mismatch for this line:
https://github.com/Lightning-AI/metrics/blob/19355a9d2c51b3b39b311694d7b8e6856a73eae6/src/torchmetrics/functional/image/ssim.py#L407

When debugging the code, I realized that the betas repetition one line before is incorrect.
I believe that instead of
betas = torch.tensor(betas).unsqueeze(1).repeat(1, sim_stack.shape[0])
it should be
betas = torch.tensor(betas).unsqueeze(1).repeat(1, sim_stack.shape[1])

The shape of betas after this command should be (len(origin_betas), batch_size), and this is not what actually happens
(betas is already a column tensor, each element is power for each scale, so we want to repeat it with the amount of the batch size).

In addition, the next two lines raise warnings:
https://github.com/Lightning-AI/metrics/blob/19355a9d2c51b3b39b311694d7b8e6856a73eae6/src/torchmetrics/functional/image/ssim.py#L408
https://github.com/Lightning-AI/metrics/blob/19355a9d2c51b3b39b311694d7b8e6856a73eae6/src/torchmetrics/functional/image/ssim.py#L409

UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor)

The fix can be:
sim_stack = sim_stack ** betas.to(device=sim_stack.device); cs_stack = cs_stack ** betas.to(device=cs_stack.device)
(since betas is already a tensor)

@amichayfeldman amichayfeldman added bug / fix Something isn't working help wanted Extra attention is needed labels Sep 12, 2022
@github-actions
Copy link

Hi! thanks for your contribution!, great first issue!

@SkafteNicki
Copy link
Member

Hi @amichayfeldman,
Could you provide an minimal example to reproduce the shape error you are encountering?

@SkafteNicki SkafteNicki added this to the v0.10 milestone Sep 14, 2022
@Queuecumber
Copy link
Contributor

I can confirm this bug, I fixed it as part of #1231

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug / fix Something isn't working help wanted Extra attention is needed topic: Image
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants