I'm facing the tensor size mismatch error, need help


I’m working image classification dataset, and facing the below error. I hope all images have 3 channels (RGB) in my training set. Not sure how to confirm about it. And in case if any grey images included in , how to convert them to 3 channel images, or any other solution to go about it. Please suggest me.

RuntimeError: stack expects each tensor to be equal size, but got [3, 224, 224] at entry 0 and [1, 224, 224] at entry 7


Seems like some of your images have really 1 channel instead of 3.

I think there’s only one simple way to convert these images into 3-channel versions: repeat one channel until you have 3 of them.

To do so I would create a custom dataset loader that wraps the actual one. If it detects that the example has only 1 channel, then it makes a new tensor (of size (3, 224, 224) probably) and set each of it’s channel.
The image would still be in grayscale, but the model would accept it.

The other simple way would be just apply a grayscale transform to make them all 1-channel. This would probably cause some accuracy loss, but it depends on what you try to classify.

1 Like

Thanks Sebastian. it worked . I tried below way in getitem.

img = Image.open(img_fname).convert(‘RGB’)

But I’m facing some other error now… tried the reshaping/unsqueezing the target, still not working … trying to debug.

ValueError: Using a target size (torch.Size([64])) that is different to the input size (torch.Size([64, 6])) is deprecated. Please ensure they have the same size.


Show the code and few lines around it where the error happens.

I’m not sure how to upload here. its on Colab. i tried jovian commit, but it took worng notebook since jovian.set_colab_id got from one of course notebook here.

how can i upload here ? or is there any other way i can share. ?

A screenshot will be ok.

Please have look at this.

Are you sure you need to use binary cross entropy here?

Is it single-label or multi-label classification?

Ok, so it’s single-label.

You should replace binary cross entropy with just cross entropy.

Yes, I missed it …Thanks for pointing out. and now tried with only cross_entropy. but again got into similar issue in calculating F1 score :slight_smile:

argmax() your prob variable (at dim=0 or 1, can’t remember now).

Also: your prob variable seems to be defined outside this function. You probably meant to use output.

its same variable, ‘prob’ and ‘output’ are same . argmax of prob with dim =0 is :

Could you please point me to any single label reference note book !

Just try with dim=1

Also, link to the notebook:

Thank you. This notebook is not worked with the .CSV file in dataset, so little confusing while mapping csv file names with the image list.
BTW, tried with dim=1,

Doesn’t matter, dataset loading is a different part of the problem.

Can’t see if you modify the prob variable or just print the result.
What’s the my_tensor btw?

Seems like you still have dim=0 used.

Its just an var to print . pls see the below screenshot.

Ok, can’t see prob getting argmax()ed, but I think there’s more to this problem.

Can’t remember what the F_score function accepted, but I guess it was one-hot encoded labels.

Go on

Since you have numeric labels, my best guess would be to just one-hot encode them in this function (along with argmax()ed prob) and then apply the calculations.


Also I suppose, you were closer to the solution back when you had binary cross entropy. But the missing one-hot encoded labels were a bit misleading :confused:
You probably have sigmoid as your last layer, which would work with one-hot encoded labels, BCE and this function.

Sorry if I introduced some confusion here, I’ve assumed that you do a slightly different approach :stuck_out_tongue: