Output shape doesn't match to broadcast shape

Hello everyone, I have a problem when I want to test my training file in real-time. When I grab the picture from the camera, there is error like this
Traceback (most recent call last):
File “/home/riza/Documents/P R O G R A M /NEW_GAN/Test.py”, line 145, in
real_img = transform(real_img).cuda()
File “/usr/local/lib/python3.5/dist-packages/torchvision/transforms/transforms.py”, line 60, in call
img = t(img)
File “/usr/local/lib/python3.5/dist-packages/torchvision/transforms/transforms.py”, line 163, in call
return F.normalize(tensor, self.mean, self.std, self.inplace)
File “/usr/local/lib/python3.5/dist-packages/torchvision/transforms/functional.py”, line 208, in normalize
tensor.sub_(mean[:, None, None]).div_(std[:, None, None])
RuntimeError: output with shape [1, 512, 512] doesn’t match the broadcast shape [3, 512, 512]
Please your help. Thank you

Are you dealing with some gray images (single channel), while others are RGB (3 channels)?
Based on the error message, it seems mean and std contain three values (for each channel), while the tensor seems to be a single channel tensor.

If that’s the case, you could load the images as color images (PIL will repeat the single channel) using img = img.convert("RGB").

1 Like

The image is in RGB, because I take the picture directly from a camera, and I have set mean and std contain three values
This is my list code of transform image
transform = tv.transforms.Compose([ tv.transforms.Resize(512),
tv.transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))

It still seems some images might be loaded as grayscale images.
Here is a small code snippet to reproduce your error message:

mean = [0.5, 0.5, 0.5]
std = [0.5, 0.5, 0.5]

trans = transforms.Normalize(mean, std)

x = torch.randn(3, 224, 224) # works
out = trans(x)

y = torch.randn(1, 224, 224) # throws your error
out = trans(y)
> RuntimeError: output with shape [1, 224, 224] doesn't match the broadcast shape [3, 224, 224]

sorry, what are x and y?

x and y just represent some dummy image tensors to reproduce the error.

Oke, I see, Thank you for your help. I have find the problem. Because I train with the different network so I can’t load the training file.

I am so sorry about it