Skip to content

Add dim to pytorch_lightning.metrics.PSNR #5957

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

Merged
merged 13 commits into from
Feb 17, 2021

Conversation

manipopopo
Copy link
Contributor

@manipopopo manipopopo commented Feb 13, 2021

What does this PR do?

Fixes #5933.

Suppose we have a batch containing two image pairs, the original PSNR calculates the mean-squared-error of the two pairs and then take the log to get the PSNR of the batch. By setting dim, users can choose to calculate the _PSNR_s of the two pairs separately and then average the two _PSNR_s to get the score of the batch:

preds_image_nchw = torch.rand([2, 3, 512, 512])
target_image_nchw = torch.rand([2, 3, 512, 512])

# (pred_image_1, target_image_1)
# (pred_image_2, target_image_2)
# squared_error_1 = (pred_image_1 - target_image_1) ** 2
# squared_error_2 = (pred_image_2 - target_image_2) ** 2
# mean_squared_error = (squared_error_1 + squared_error_2) / (squared_error_1.numel() + squared_error_2.numel())
# log_mse_of_two_pairs = -10.0 * log(mean_squared_error)
log_mse_of_two_pairs = pl.metrics.functional.psnr(
    preds_image_nchw,
    target_image_nchw,
    data_range=1.0
)

# squared_error_1 = (pred_image_1 - target_image_1) ** 2
# psnr_1 = -10.0 * log(squared_error_1.mean())
# squared_error_2 = (pred_image_2 - target_image_2) ** 2
# psnr_2 = -10.0 * log(squared_error_2.mean())
# average_log_mse = (psnr_1 + psnr_2) / 2
average_log_mse = pl.metrics.functional.psnr(
    preds_image_nchw, 
    target_image_nchw,
    data_range=1.0,
    dim=(1, 2, 3)
)

Before submitting

  • Was this discussed/approved via a GitHub issue? (not for typos and docs)
  • Did you read the contributor guideline, Pull Request section?
  • Did you make sure your PR does only one thing, instead of bundling different changes together?
  • Did you make sure to update the documentation with your changes? (if necessary)
  • Did you write any new necessary tests? (not for typos and docs)
  • Did you verify new and existing tests pass locally with your changes?
  • Did you update the CHANGELOG? (not for typos, docs, test updates, or internal minor changes/refactorings)

PR review

Anyone in the community is free to review the PR once the tests have passed.
Before you start reviewing make sure you have read Review guidelines. In short, see the following bullet-list:

  • Is this pull request ready for review? (if not, please submit in draft mode)
  • Check that all items from Before submitting are resolved
  • Make sure the title is self-explanatory and the description concisely explains the PR
  • Add labels and milestones (and optionally projects) to the PR so it can be classified
  • Check that target branch and milestone match!

Did you have fun?

Make sure you had fun coding 🙃

@codecov
Copy link

codecov bot commented Feb 13, 2021

Codecov Report

Merging #5957 (d296919) into master (f655f97) will decrease coverage by 58%.
The diff coverage is 12%.

@@           Coverage Diff            @@
##           master   #5957     +/-   ##
========================================
- Coverage      91%     33%    -58%     
========================================
  Files         160     160             
  Lines       11313   11232     -81     
========================================
- Hits        10272    3701   -6571     
- Misses       1041    7531   +6490     

@manipopopo
Copy link
Contributor Author

@SkafteNicki
I'm not sure which target branch (master or release/1.2-dev) should be used :(. It seems that there are some modifications to the relevant files on the master branch.

@Borda Borda added feature Is an improvement or enhancement metrics labels Feb 13, 2021
@Borda Borda added this to the 1.2 milestone Feb 13, 2021
Copy link
Member

@Borda Borda left a comment

Choose a reason for hiding this comment

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

@SkafteNicki mind have look?

Comment on lines 75 to 106
if data_range is None:
if dim is not None:
# Maybe we could use `torch.amax(target, dim=dim) - torch.amin(target, dim=dim)` in PyTorch 1.7 to calculate
# `data_range` in the future.
raise ValueError("`data_range` must be given when `dim` is not `None`.")

data_range = target.max() - target.min()
else:
data_range = torch.tensor(float(data_range))
Copy link
Member

Choose a reason for hiding this comment

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

could we move all this logic into the _update function?

Copy link
Contributor Author

@manipopopo manipopopo Feb 15, 2021

Choose a reason for hiding this comment

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

In the original implementation (before this PR) and the updated commits (after the comment), the data_range is passed to functional._psnr_compute, maybe we could move the checks on data_range and dim from functional.psnr and PSNR.__init__ to _compute. But the users of PSNR will get the exception until they call PSNR.compute (or validation_epoch_end is called).

Should we extract the logic to a single place (functional._psnr_compute)?

@mergify mergify bot removed the has conflicts label Feb 15, 2021
Copy link
Contributor

@tchaton tchaton left a comment

Choose a reason for hiding this comment

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

LGTM ! Great contribution !

@pep8speaks
Copy link

pep8speaks commented Feb 15, 2021

Hello @manipopopo! Thanks for updating this PR.

There are currently no PEP 8 issues detected in this Pull Request. Cheers! 🍻

Comment last updated at 2021-02-17 07:23:02 UTC

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

@SkafteNicki SkafteNicki added the ready PRs ready to be merged label Feb 15, 2021
@Borda Borda force-pushed the feature/5933_psnr_dim branch from 4cdafa7 to a2d41a4 Compare February 16, 2021 21:04
@Borda Borda enabled auto-merge (squash) February 16, 2021 21:08
@Borda Borda force-pushed the feature/5933_psnr_dim branch from 96e7387 to d296919 Compare February 17, 2021 07:22
@Borda Borda merged commit 6a9cec4 into Lightning-AI:master Feb 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Is an improvement or enhancement ready PRs ready to be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add dim to pytorch_lightning.metrics.PSNR
6 participants