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

PSNR implementation #2483

Merged
merged 17 commits into from
Jul 8, 2020
Merged

PSNR implementation #2483

merged 17 commits into from
Jul 8, 2020

Conversation

InCogNiTo124
Copy link
Contributor

@InCogNiTo124 InCogNiTo124 commented Jul 3, 2020

What does this PR do?

Implements a new metric: PSNR (peak signal to noise ratio)

Fixes #2474

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 your PR does only one thing, instead of bundling different changes together? Otherwise, we ask you to create a separate PR for every change.
  • Did you make sure to update the documentation with your changes?
  • Did you write any new necessary tests?
  • Did you verify new and existing tests pass locally with your changes?
  • If you made a notable change (that affects users), did you update the CHANGELOG?

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 🙃

@pep8speaks
Copy link

pep8speaks commented Jul 3, 2020

Hello @InCogNiTo124! Thanks for updating this PR.

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

Comment last updated at 2020-07-08 08:24:51 UTC

@mergify mergify bot requested a review from a team July 3, 2020 08:48
@Borda Borda added the feature Is an improvement or enhancement label Jul 3, 2020
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.

let's add a test and compare it to some standard lib like https://scikit-image.org/docs/stable/api/skimage.measure.html#compare-psnr

@mergify mergify bot requested a review from a team July 3, 2020 10:33
# Since mean and variance are unknown, we cannot know what's the maximum value to use in calculation.
# This implementation, therefore, finds the maximum empirically.
maximum = max(torch.max(torch.abs(pred)), torch.max(torch.abs(target)))
PSNR_base_e = 2*torch.log(maximum) - torch.log(mse)
Copy link
Member

Choose a reason for hiding this comment

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

variable names should always be lowercase according to pep8

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changed

@mergify mergify bot requested a review from a team July 3, 2020 14:15
@InCogNiTo124
Copy link
Contributor Author

I've added tests and I validated them with skimage.metrics.peak_signal_noise_ratio, and it works (even the example in the comments)

@InCogNiTo124 InCogNiTo124 changed the title [WIP] PSNR implementation PSNR implementation Jul 3, 2020
@Borda
Copy link
Member

Borda commented Jul 3, 2020

I've added tests and I validated them with skimage.metrics.peak_signal_noise_ratio, and it works (even the example in the comments)

add such validation test with skimage to our tests 🐰

@InCogNiTo124
Copy link
Contributor Author

InCogNiTo124 commented Jul 3, 2020

I've added tests and I validated them with skimage.metrics.peak_signal_noise_ratio, and it works (even the example in the comments)

add such validation test with skimage to our tests

I wanted to, but I came to the conclusion that skimage is not in this project's requirements (I've installed the devel file) and I didn't want to change them :) I'll edit it tommorow then

@awaelchli
Copy link
Contributor

I agree with @Borda that checking with the skimage implementation for maybe random tensors would be the best.
Could we also add a functional interface and coordinate with #2492 ?

@Borda
Copy link
Member

Borda commented Jul 3, 2020

it is fine to add skimage to requirements/test.txt :]

@rohitgr7
Copy link
Contributor

rohitgr7 commented Jul 4, 2020

You should add PSNR in this file too :)

@InCogNiTo124
Copy link
Contributor Author

I agree with @Borda that checking with the skimage implementation for maybe random tensors would be the best.

I'm having trouble figuring out how to test with random inputs, I don't know how do I pass randomly generated tensors from pytest.param into skimage.metrics.peak_signal_noise_ratio.

Could we also add a functional interface and coordinate with #2492 ?

Sure :D

@codecov
Copy link

codecov bot commented Jul 4, 2020

Codecov Report

Merging #2483 into master will decrease coverage by 2%.
The diff coverage is 100%.

@@           Coverage Diff           @@
##           master   #2483    +/-   ##
=======================================
- Coverage      89%     87%    -2%     
=======================================
  Files          69      70     +1     
  Lines        5518    5648   +130     
=======================================
+ Hits         4889    4919    +30     
- Misses        629     729   +100     

@InCogNiTo124
Copy link
Contributor Author

Here also you should add PSNR :]

https://github.com/PyTorchLightning/pytorch-lightning/blob/39a6435726e7a110df1189efe63094888e0c23be/pytorch_lightning/metrics/__init__.py#L49-L54

I totally missed it, it should be fixed now :)

Comment on lines 217 to 224
if self.data_range is None:
data_range = max(target.max() - target.min(), pred.max() - pred.min())
else:
data_range = torch.tensor(float(self.data_range))
mse = F.mse_loss(pred.view(-1), target.view(-1))
# numerical precision tricks
psnr_base_e = 2 * torch.log(data_range) - torch.log(mse)
return psnr_base_e * (10 / torch.log(self.base)) # change the logarithm basis
Copy link
Contributor

Choose a reason for hiding this comment

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

You don't have to replicate the whole procedure again here. Just import from functional and use that one. :) Also add reduction parameter here.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored and added

BTW in case you didn't know, pytest says 'elementwise_mean' is deprecated, it might be smart to refactor the default to 'mean'. I left it that way to match rohitgr7's PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Also, can you fix the pep8 issues mentioned above by @pep8speaks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rohitgr7 I'm confused about the missing whitespace. I thought that was a bad thing :|

Copy link
Contributor

Choose a reason for hiding this comment

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

hmm, don't know about that :| I never use an extra , after the last element in the list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Nvm I figured what was wrong

@InCogNiTo124
Copy link
Contributor Author

I've got a few failing tests I don't know how to fix:

  • I have no idea why ufunc is giving troubles in windows builds
  • conda tests are missing skimage (weird because I've added in test requirements)
  • there is a weird docker tag

@awaelchli
Copy link
Contributor

conda tests are missing skimage (weird because I've added in test requirements)

@InCogNiTo124 I'm pretty sure you need to add it also to environment.yml

@awaelchli
Copy link
Contributor

awaelchli commented Jul 4, 2020

I have no idea why ufunc is giving troubles in windows builds

Lift numpy to 1.16.4 in requirements/base.txt, this was fixed in this version.
Source: scikit-learn-contrib/hdbscan#272

@mergify mergify bot requested a review from a team July 4, 2020 23:31
@SkafteNicki
Copy link
Member

If you could add a test similar to https://github.com/PyTorchLightning/pytorch-lightning/blob/a91b06ed1e5295efecdd7b51f4a3e6d95c829ecd/tests/metrics/functional/test_classification.py#L38-L72

to test on random tensors:

  1. create two random tensors inside test
  2. calculate value using skimage implementation and your implementation
  3. assert results are same

@InCogNiTo124
Copy link
Contributor Author

@SkafteNicki you mean alongside or instead of these tests I already wrote?

@SkafteNicki
Copy link
Member

Just add a new test :)

@InCogNiTo124
Copy link
Contributor Author

I googled the error in the TPU test, it seems that PROJECT_ID is not set.

@Borda
Copy link
Member

Borda commented Jul 6, 2020

I googled the error in the TPU test, it seems that PROJECT_ID is not set.

Pls ignore TPU test for this moment, the issue is related to forked PRs... cc: @zcain117

@InCogNiTo124
Copy link
Contributor Author

Pls ignore TPU test for this moment, the issue is related to forked PRs... cc: @zcain117

In that case, this seems done then :)

@SkafteNicki
Copy link
Member

Could you update CHANGELOG?
Also we need refer to this in the docs, but maybe we should just merge this and then fix that in #2492?

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.

LGTM :]

@mergify mergify bot requested a review from a team July 7, 2020 08:00
@Borda Borda added this to the 0.8.x milestone Jul 7, 2020
@mergify mergify bot requested a review from a team July 7, 2020 09:03
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 :)

@Borda Borda requested a review from awaelchli July 8, 2020 08:23
CHANGELOG.md Outdated Show resolved Hide resolved
@Borda Borda merged commit 1dc7242 into Lightning-AI:master Jul 8, 2020
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

PSNR metric in metrics
7 participants