-
Notifications
You must be signed in to change notification settings - Fork 115
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
Provide optional validation #212
Comments
Hi! Propose also do a bunch of improvements to speed up metrics:
if scale_weights is not None:
assert isinstance(scale_weights, (list, tuple, torch.Tensor)), \
f'Scale weights must be of type list, tuple or torch.Tensor, got {type(scale_weights)}.'
if isinstance(scale_weights, (list, tuple)):
scale_weights = torch.tensor(scale_weights)
assert (scale_weights.dim() == 1), \
f'Scale weights must be one dimensional, got {scale_weights.dim()}.'
if kernel_size is not None:
assert kernel_size % 2 == 1, f'Kernel size must be odd, got {kernel_size}.'
assert isinstance(input_tensors, tuple)
assert 0 < len(input_tensors) < 3, f'Expected one or two input tensors, got {len(input_tensors)}'
assert isinstance(tensor, torch.Tensor), f'Expected input to be torch.Tensor, got {type(tensor)}.' # We have type hints for that What do you think? |
Hi @zakajd, those are great ideas! In fact, your type assertion would become very similar to what I just pushed to piqa. Having similar inputs, could help quite a lot if we were to merge the two projects 👍 For your information, here is the assertion function: https://github.com/francois-rozet/piqa/blob/master/piqa/utils/__init__.py#L37-L87 And here is the way I used it in https://github.com/francois-rozet/piqa/blob/master/piqa/ssim.py#L288-L302 The reason why I used Also, I do not perform value assertion (values within range) because it is Moreover, I saw that you used Finally, we might as well unify the way the outputs are reduced (the dictionary you use currently is duplicated everywhere). Also, is it necessary to add the If you want, I can do a PR, where I modify |
Let's wait for others to respond. Considering I like the idea with Value assertion is important, because passing tensors with [0, 255] scale to metric which expects it to be in [0, 1] can lead to huge drop in quality of "perceptual error" estimation . See #208 as a recent example. Have you measured speedup between your's implementations with and without validation? In my experience, data loading and model inference takes much longer compared to loss computation, so squeezing milliseconds out of it is not always reasonable. |
Thx for the feedback, @zakajd.
Ok, I thought that
It does make a difference. Computing the To summarize what is proposed:
For the last one, is it necessary to add it to functions ( |
Let's follow PyTorch design for losses, where This can be useful when computing mean metric value for dataset of images, not |
Hello @zakajd, while waiting for the others, I've completely refactored piqa to follow what we said here, i.e.
However, it should be noted that
What do you think ? |
Hi @francois-rozet. First, I would like to comment on the discussion about I think that To support my conclusions, I added some benchmarks that show relative performance "as is" (time), with
Results above show that the most performance gain can be achieved by using the Regarding the return {'mean': score.mean,
'sum': score.sum
}[reduction](dim=0) Only two of them can be extracted because we still need to leave the To conclude:
|
assert cond, 'message' is equivalent to if __debug__:
if not cond:
raise AssertionError('message') So I only propose to add
I believe you use 5 lines of code for the reduction. if reduction == 'none':
return score
return {'mean': score.mean,
'sum': score.sum
}[reduction](dim=0) The problem is mainly maintainability: if you decide to add another reduction ( |
Re-opened, because issue is important and we have at least some 🙃 consensus on what should be done. I agree with @francois-rozet that reduction part could be improved by one-line function instead of a dict. Will create PR in the morning, discussion can be continued there |
Hi Guys!
|
Hi, @zakajd, @denproc and @snk4tr. I see that, in the end, you did exactly what I had proposed 🙃. Good for you. However, could you mention that the code for |
Is your feature request related to a problem? Please describe.
Currently, there is no way to skip the input validation (
_validate_input
) which is useful during prototyping and debugging but slows down the implementations. It could be interesting to add the option to skip the validation.Describe the solution you'd like
With the flag
-O
, the Python interpreter doesn't considerassert
statements. In fact,-O
sets the global variable__debug__
toFalse
. The following could be added to the start of_validate_input
to shortcut all the validation when-O
is used:Describe alternatives you've considered
Alternatively, a global variable
_VALIDATION
, a function_is_validation_enabled()
and a function (or a context)_set_validation_enabled()
could be provided.The text was updated successfully, but these errors were encountered: