Skip to content Skip to sidebar Skip to footer

Importing Transparent Images Gives Runtimeerror: The Size Of Tensor A (4) Must Match The Size Of Tensor B (3) At Non-singleton Dimension 0

I'm trying to learn AI. I have GAN (generative adversarial network) code with images with ALPHA Channel(transparency). All images have alpha channel. To prove that I wrote small im

Solution 1:

Regarding the error message

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

would lead to suggest that there's a problem with this call: sample = self.transform(sample)

Indeed, the issue is you are using a T.Normalize transform which only expects three channels (you specified a mean and std for three channels only, not four).

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

Instead, you should provide a four-element tuple for both arguments. For example (this is an example, this might run but won't necessarily make sense... see explanation below):

    transforms.Normalize((0.5, 0.5, 0.5, 0.5), (0.5, 0.5,0.5, 0.5))])

Other than that, I should ask: do you know why you are using .5 for both parameters of mean and std? If not, chances are you are not using it properly. Please read about it on this answer and its applications.

Post a Comment for "Importing Transparent Images Gives Runtimeerror: The Size Of Tensor A (4) Must Match The Size Of Tensor B (3) At Non-singleton Dimension 0"