-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Investigate inconsistent casting inside functional_tensor.py #3067
Comments
@datumbox it is a bad merge of two PRs I made :) We can remove the line 405 Thanks for catching !
I'd say two approaches can exist depending on if we'd like to rescale or not when changing the dtype. For example, for color adjustments seems like it makes sense to rescale between 0-1 before applying the op. |
@vfdev-5 Thanks for the reply. I agree with you that some cases can be handled only by one or the other. As you said, the main focus of this ticket is to investigate the current uses and simplify the code where necessary. :) |
@datumbox I'd like to contribute to this. and later removed line no. 404 and keeping the line 405: Both the times, the images gets successfully converted to the tensor type. I tried with double tensor, float tensor, uint8 image and float32 image. Also, I tried by removing both the lines and check. Since the image provided is already in a tensor type, so maybe we don't need to convert it at all.
I'd like to know if further cases need to be tried to validate more on this. |
@sanketsans I believe only 405 needs to be removed. Line 404 is necessary to convert the image back to its original type (in case someone passed a uint8). @vfdev-5 Could you confirm? It might be worth sending a PR with all the proposed clean ups. Any simplification of the current logic is useful I think. |
@datumbox I tried by sending a uint8 image. But then it is taken care by the line 400.
That makes it back to a tensor type. |
Looking again to the code, I'd do the following: result = img
dtype = img.dtype
if not torch.is_floating_point(img):
result = convert_image_dtype(result, torch.float32)
result = (gain * result ** gamma).clamp(0, 1)
if result.dtype != dtype:
result = convert_image_dtype(result, dtype)
return result What do you think @datumbox ? |
@vfdev-5 Yeah that works fine. Could any of you send a PR that fixes this? |
The operators in functional_tensor.py perform casting in two ways:
tensor.to(dtype=dtype)
PyTorch methodconvert_image_dtype()
Transformation methodThe first method does direct casting from one type to the other. The latter method has more complex logic that handles corner-cases and performs rescaling. Sometimes both are used on the same operator, for example:
vision/torchvision/transforms/functional_tensor.py
Lines 397 to 406 in 9e71fda
We should investigate if the use of the two different approaches across operators is justified and fix any potential inconsistencies.
cc @vfdev-5
The text was updated successfully, but these errors were encountered: