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

RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0 #1

Closed
WXY-Belief opened this issue May 10, 2022 · 3 comments

Comments

@WXY-Belief
Copy link

你好,我想问一下,为什么会出现这个问题呀?
Hello, I'd like to ask, why is this problem?

Traceback (most recent call last):
File "easy_use.py", line 97, in
generate_results(args, device)
File "easy_use.py", line 66, in generate_results
img = transform(img)
File "/disk/sdb/public_user/anaconda3/envs/wxy_env/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 67, in call
img = t(img)
File "/disk/sdb/public_user/anaconda3/envs/wxy_env/lib/python3.8/site-packages/torch/nn/modules/module.py", line 727, in call_impl
result = self.forward(*input, **kwargs)
File "/disk/sdb/public_user/anaconda3/envs/wxy_env/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 226, in forward
return F.normalize(tensor, self.mean, self.std, self.inplace)
File "/disk/sdb/public_user/anaconda3/envs/wxy_env/lib/python3.8/site-packages/torchvision/transforms/functional.py", line 284, in normalize
tensor.sub
(mean).div_(std)
RuntimeError: The size of tensor a (4) must match the size of tensor b (3) at non-singleton dimension 0

@StoryMY
Copy link
Owner

StoryMY commented May 10, 2022

It seems that the channel of img is 4 (RGBA), which is expected to be 3 (RGB).
You can print(np.array(img).shape) before "img = transform(img)" to check it.
The correct shape is expected to be (256, 256, 3).

If your result is (256, 256, 4), you need to convert it to RGB first.
Maybe like this: "img = Image.open(os.path.join(args.input_dir, img_name)).convert('RGB')" (Line 64)

@WXY-Belief
Copy link
Author

WXY-Belief commented May 10, 2022 via email

@WXY-Belief
Copy link
Author

WXY-Belief commented May 10, 2022 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants