fungtion / DANN

pytorch implementation of Domain-Adversarial Training of Neural Networks

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]

houchenyu opened this issue · comments

When I run your code, I have met a error when executing data_source = data_source_iter.next():

RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28].

I have no idea why this error occurs. Could you please give some suggestions.
Besides, I'm using Python3.6 and Pytorch 1.0. My operating system is Ubuntu 16.04. Thanks.

I have not tested it on pytorch 1.0 yet, but I have transfrom the gray images into 3 channels, can you post the complete errors here?

I have not tested it on pytorch 1.0 yet, but I have transfrom the gray images into 3 channels, can you post the complete errors here?

But there is no error on Windows10 with the same version of pytorch.

I've no idea about it, and I'll test it with pytorch 1.0 on Ubuntu soon.

Traceback (most recent call last):
File "/home/hou/桌面/DANN-master/train/main.py", line 98, in
data_source = data_source_iter.next()
File "/home/hou/anaconda3/envs/py36/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 637, in next
return self._process_next_batch(batch)
File "/home/hou/anaconda3/envs/py36/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 658, in process_next_batch
raise batch.exc_type(batch.exc_msg)
RuntimeError: Traceback (most recent call last):
File "/home/hou/anaconda3/envs/py36/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
File "/home/hou/anaconda3/envs/py36/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 138, in
samples = collate_fn([dataset[i] for i in batch_indices])
File "/home/hou/anaconda3/envs/py36/lib/python3.6/site-packages/torchvision/datasets/mnist.py", line 95, in getitem
img = self.transform(img)
File "/home/hou/anaconda3/envs/py36/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 60, in call
img = t(img)
File "/home/hou/anaconda3/envs/py36/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 163, in call
return F.normalize(tensor, self.mean, self.std, self.inplace)
File "/home/hou/anaconda3/envs/py36/lib/python3.6/site-packages/torchvision/transforms/functional.py", line 208, in normalize
tensor.sub
(mean[:, None, None]).div
(std[:, None, None])
RuntimeError: output with shape [1, 28, 28] doesn't match the broadcast shape [3, 28, 28]

This is the complete error info. It seems wrong about dataloader

I guess something wrong here, some images were not converted to 3 channels as rgb.

I built these codes on pytorch 0.4.0, python 2.7, ubuntu 16.04, this could make any difference? I will test it soon.

Maybe it is not. According to the error info, there is a error about the source data loader, which is implemented by in-place function. Therefore, I think this error is not caused by convertion.

This error was caused by the shape mismatch between the tensor and self.mean in F.normalize, which tensor was [1,28,28] and self.mean was [0.5, 0.5, 0.5], so the shape of self.mean implied that the tensor should be [3, *, *], instead of [1, *, *]. So I think there is something wrong with this input tensor.

mnist dataset consists of grey images which are 1 channel. Thus the input tensor is [1, *, *]. Indeed, you have implemented the self-defined loader GetLoader where you convert the input images to RGB. But this loader is used to load mnist_m dataset rather than mnist dataset. Therefore, there is nothing to do with mnist dataset. And input tensor of mnist is still [1, *, *].

Ooooooh, finally, I run the code successfully. I add a new transform like this

img_transform1 = transforms.Compose([
    transforms.Resize(image_size),
    transforms.ToTensor(),
    transforms.Lambda(lambda x: x.repeat(3,1,1)),
    transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])

And I replace the original transform of source data mnist with img_transform1. By do so, the mnist images can be converted to [3, *, *] tensors. I am curious about why the code is OK under your settings.
In my opinion, is there a difference about torchvision.datasets.MNIST() function between torch 0.4 and torch 1.0???

Yes, you're right, input tensor of mnist is still [1, *, *]. The problem comes pytorch/vision@2115380#diff-fc1f220b470714d05cf3ea6acf9fed59L204, where it used zip to return iterable object before, and If multiple iterables are passed, iterator stops when shortest iterable is exhaused, I think this is the reason that it ran well in my settings. After the zip was replaced by broadcasting, my codes failed because of shape mismatch. I will fix it soon, thank you.

commented

add transforms.CenterCrop(), after transforms.Resize .

# load data
transform = transforms.Compose([
    transforms.Resize(224),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)),
])

Let me clarify, if the img has three channels, you should have three number for mean, for example, img is RGB, mean is [0.5, 0.5, 0.5], the normalize result is Rx0.5, Gx0.5, Bx0.5. If img is grey type that only one channel, so mean should be [0.5], the normalize result is R*0.5

Ooooooh, finally, I run the code successfully. I add a new transform like this

img_transform1 = transforms.Compose([
    transforms.Resize(image_size),
    transforms.ToTensor(),
    transforms.Lambda(lambda x: x.repeat(3,1,1)),
    transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))
])

And I replace the original transform of source data mnist with img_transform1. By do so, the mnist images can be converted to [3, *, *] tensors. I am curious about why the code is OK under your settings.
In my opinion, is there a difference about torchvision.datasets.MNIST() function between torch 0.4 and torch 1.0???

transforms.Lambda(lambda x: x.repeat(3,1,1)),
transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5))

I received the fault , Can't pickle local object 'get_data_loaders..'.

@ShuuTsubaki try new code under pytorch 1.0

what should be the image_size in above code? I have image of 28x28.
When i set image size to 784, i get the following error.

RuntimeError: size mismatch, m1: [128 x 1843968], m2: [784 x 128] at /opt/conda/conda-bld/pytorch_1556653099582/work/aten/src/TH/generic/THTensorMath.cpp:961

@priteshgohil image_size = 28

Downgrading torch and torchvision to 0.2.0 and 0.2.1 solved this issue for me.

Downgrading torch and torchvision to 0.2.0 and 0.2.1 solved this issue for me.

Thanks So lot ! It helps me! BTW , Mine is pytorch 1.1.0 + win10 , work with torchvision 0.2.0

I'm working on Ubuntu 16.04 version and using torch version 1.3.1
It's that MNIST data set consists of grey images.

transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)), ])

can be changed to

transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])

I'm working on Ubuntu 16.04 version and using torch version 1.3.1
It's that MNIST data set consists of grey images.

transform = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=(0.5, 0.5, 0.5), std=(0.5, 0.5, 0.5)), ])

can be changed to

transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,))])

This solved the problem for me

img_transform = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize([0.5], [0.5])
])
dataset = MNIST('./data', transform=img_transform)