-
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
Add explicit check for number of channels #3013
Conversation
Example why you need to check it: `M = torch.randint(low=0, high=2, size=(6, 64, 64), dtype = torch.float)` When you put this input through to_pil_image without mode argument, it converts to uint8 here: ``` if pic.is_floating_point() and mode != 'F': pic = pic.mul(255).byte() ``` and change the mode to RGB here: ``` if mode is None and npimg.dtype == np.uint8: mode = 'RGB' ``` Image.fromarray doesn't raise if provided with mode RGB and just cut number of channels from what you have to 3
Hi @ademyanchuk! Thank you for your pull request and welcome to our community. We require contributors to sign our Contributor License Agreement, and we don't seem to have you on file. In order for us to review and merge your code, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA. If you have received this in error or have any questions, please contact us at cla@fb.com. Thanks! |
Also in this Pytorch forum discussion. |
@ademyanchuk thanks for the PR ! Instead I would add the check before to numpy conversion, before line 194, as vision/test/test_transforms.py Line 989 in aa4cf03
and vision/test/test_transforms.py Line 993 in aa4cf03
Thanks |
@vfdev-5 thanks for comment! I thought where to put the check for number of channels and decided to put it after conversion to numpy as at this point we know the channel dimension position is Sure, I would like to add tests. I will see how can I add those. It is my first PR :) |
Yes, true. For tensor the assumption is that number of channels is
Sounds good. Feel free to ask questions if needed. Please, take a look at drafted CONTRIBUTING, if needed. |
This is totally make sense. So I will proceed. |
Ok. I refactored the checking to be done before processing and added a test for invalid number of channels :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates @ademyanchuk ! I left some comments to improve the PR.
Example why you need to check it: `M = torch.randint(low=0, high=2, size=(6, 64, 64), dtype = torch.float)` When you put this input through to_pil_image without mode argument, it converts to uint8 here: ``` if pic.is_floating_point() and mode != 'F': pic = pic.mul(255).byte() ``` and change the mode to RGB here: ``` if mode is None and npimg.dtype == np.uint8: mode = 'RGB' ``` Image.fromarray doesn't raise if provided with mode RGB and just cut number of channels from what you have to 3
Ok. It seems like I manage to make all discussed changes :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates @ademyanchuk
I left few other comments
test/test_transforms.py
Outdated
|
||
def test_ndarray_bad_types_to_pil_image(self): | ||
trans = transforms.ToPILImage() | ||
with self.assertRaises(TypeError): | ||
with self.assertRaisesRegex(TypeError, r'Input type \w+ is not supported'): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please also fix this test as it was also incorrect: add with self.assertRaisesRegex(TypeError, r'Input type \w+ is not supported')
for each trans(np.ones([4, 4, 1], type))
.
@vfdev-5 , sorry to bather. How could I do |
It will build cuda extensions with the same cuda version as torch has. If you'd like to disable it, just set
|
Apparently in my case it is not :( On my local machine cuda 11 is installed - could it be issue? So, as it is WSL2, i just delete my local cuda11. And torchvision compiled with matched version of cuda as in torch nightly! |
Anyway, I made the changes from your last comments :) |
It would be nice to have a detailed instructions to be able to reproduce your situation. If you could reproduce the issue, please file an issue about that. |
Could you also please merge master to your branch. There is unexpected modification here : https://github.com/pytorch/vision/pull/3013/files#diff-b5826ce59a98ee20294c65d06e09363609879221494b5ff7fbb7087b75f3e159L7 |
Hi, I did merge master into my branch patch-1. I also manually checked this file. But I have no diff with pytorch vision master. This diff that you show the link for is from this commit (as I understand) 365f159 |
I will try to reproduce the issue. If I succeed, I would file the issue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ademyanchuk !
Thanks @vfdev-5 for a valuable comments and review. Appreciate! So, is it done? How the merge in master is proceeded? |
It appears to me like most of recent pull requests fail on the same travis ci job on the same place. |
* Add explicit check for number of channels Example why you need to check it: `M = torch.randint(low=0, high=2, size=(6, 64, 64), dtype = torch.float)` When you put this input through to_pil_image without mode argument, it converts to uint8 here: ``` if pic.is_floating_point() and mode != 'F': pic = pic.mul(255).byte() ``` and change the mode to RGB here: ``` if mode is None and npimg.dtype == np.uint8: mode = 'RGB' ``` Image.fromarray doesn't raise if provided with mode RGB and just cut number of channels from what you have to 3 * Check number of channels before processing * Add test for invalid number of channels * Add explicit check for number of channels Example why you need to check it: `M = torch.randint(low=0, high=2, size=(6, 64, 64), dtype = torch.float)` When you put this input through to_pil_image without mode argument, it converts to uint8 here: ``` if pic.is_floating_point() and mode != 'F': pic = pic.mul(255).byte() ``` and change the mode to RGB here: ``` if mode is None and npimg.dtype == np.uint8: mode = 'RGB' ``` Image.fromarray doesn't raise if provided with mode RGB and just cut number of channels from what you have to 3 * Check number of channels before processing * Add test for invalid number of channels * Put check after channel dim unsqueeze * Add test if error message is matching * Delete redundant code * Bug fix in checking for bad types Co-authored-by: Demyanchuk <demyanca@mh-hannover.local> Co-authored-by: vfdev <vfdev.5@gmail.com>
* Add explicit check for number of channels Example why you need to check it: `M = torch.randint(low=0, high=2, size=(6, 64, 64), dtype = torch.float)` When you put this input through to_pil_image without mode argument, it converts to uint8 here: ``` if pic.is_floating_point() and mode != 'F': pic = pic.mul(255).byte() ``` and change the mode to RGB here: ``` if mode is None and npimg.dtype == np.uint8: mode = 'RGB' ``` Image.fromarray doesn't raise if provided with mode RGB and just cut number of channels from what you have to 3 * Check number of channels before processing * Add test for invalid number of channels * Add explicit check for number of channels Example why you need to check it: `M = torch.randint(low=0, high=2, size=(6, 64, 64), dtype = torch.float)` When you put this input through to_pil_image without mode argument, it converts to uint8 here: ``` if pic.is_floating_point() and mode != 'F': pic = pic.mul(255).byte() ``` and change the mode to RGB here: ``` if mode is None and npimg.dtype == np.uint8: mode = 'RGB' ``` Image.fromarray doesn't raise if provided with mode RGB and just cut number of channels from what you have to 3 * Check number of channels before processing * Add test for invalid number of channels * Put check after channel dim unsqueeze * Add test if error message is matching * Delete redundant code * Bug fix in checking for bad types Co-authored-by: Demyanchuk <demyanca@mh-hannover.local> Co-authored-by: vfdev <vfdev.5@gmail.com>
Example why you need to check it:
M = torch.randint(low=0, high=2, size=(6, 64, 64), dtype = torch.float)
When you put this input through to_pil_image without mode argument, it converts to uint8 here:
and change the mode to RGB here:
Image.fromarray doesn't raise if provided with mode RGB and just cut number of channels from what you have to 3