Problem: The size of tensor 'a' must match the size of tensor 'b'

I am trying to do face detection using a resnet50 model and am getting the following error:

RuntimeError: The size of tensor a (1000) must match the size of tensor b (64) at non-singleton dimension 1

My input consists of 128x128 px images with 3 channels and a batch size of 64.

I can see that my resnet50 model has out_features=1000 set, but I am not quite sure what the right steps are to fix this problem.

I tried doing a matrix multiplication in my loss and metric functions to decrease the tensor size from 1000 to 64, but if I do that I get another error that the tensor size must match 16 instead of 64. So this doesn’t seem to be the right solution.

Am I missing something here?

Any help is much appreciated.

I finally got it working using a batch size of 1, so where do I have to account for a batch size of 64?

Did you use fast ai library to create the databunch and what api did you use. Thanks.

Hey karthik, thanks for the reply.

Here is the code I used to create the databunch:

train = ImageList.from_csv(path, 'face_recog2.csv', folder='images')[:-80]
valid = ImageList.from_csv(path, 'face_recog2.csv', folder='images')[-80:]
src = ItemLists(path=path, train=train, valid=valid)

src_labeled = src.label_from_func(get_label_from_fname)
src_labeled.transform(size=128)

data = src_labeled.databunch(bs=64)

And here is my learner, I switched to resnet34 for now:

learn = Learner(data, model=models.resnet34(), loss_func=F.mse_loss)

Also learn.lr_find() isn’t completing successfully and I don’t know if F.mse_loss is the right function here, anyway I first want to get it working with a batch size of 64 before I tackle the next problem.

1 Like

You might want to use create_cnn to create a learner instead of directly instantiating a learner. I don’t think you need to explicitly specify a loss function as well. I think fastai library interprets a classification from a regression problem and automatically applies the right loss function.

Check this example out
https://www.kaggle.com/hortonhearsafoo/fast-ai-v3-lesson-1

Did you get it working at the end? having a similar problem in my end!