How to Change Matrix Size of ImageDataBunch data?

Howdy! :cowboy_hat_face:

I’m trying to get a 1-layer image recognition network to identify types of sodas and energy drinks.
I’m getting a size mismatch between the input matrix, and my hidden layer. I want the input layer to be [64x150528], but It’s telling me that it’s [43008 x 224]. (See bottom for runtime error)

The problem seems to be that m1 (the bad matrix) starts out as [64 x 3 x 224 x 224], and then multiplies from left to right like so: [(64 x 3 x 224) x 224] = [43008 x 224]. But that’s a mistake, that includes including the batch size in the multiplication (that’s what the 64 is), instead of multiplying the image components (RGB and image resolution). So what I want is for it to multiply from right to left: [64 x (3 x 224 x 224)] = [64 x 150528]. Or else resize the data to fit what I want.

Does anyone know how to resize data inside an ImageDataBunch? Or otherwise fix this?
Thanks in advance. :slight_smile:

Here’s my code:

from fastai.basics import *
from fastai.vision import *
path = Path('data/drinks')
[PosixPath('data/drinks/cleaned.csv')]

data = ImageDataBunch.from_csv(path,ds_tfms = get_transforms(do_flip = False) ,csv_labels = 'cleaned.csv', size = 224)

class Soda_Logistic(nn.Module):
    def __init__(self):
        super().__init__()
        self.lin = nn.Linear(150528,5, bias = True)
    def forward(self, xb): return self.lin(xb)

model = Soda_Logistic().cuda()
model

Soda_Logistic(
  (lin): Linear(in_features=150528, out_features=5, bias=True)
)

x,y = next(iter(data.train_dl))
x.shape, y.shape

(torch.Size([64, 3, 224, 224]), torch.Size([64]))

lr = 2e-2

loss_func=nn.CrossEntropyLoss()
def Update(x,y,lr):
    wd = 1e-5
    y_hat=model(x)     
    w2 = 0.
    for p in model.parameters(): w2 += (p**2).sum()
    loss = loss_func(y_hat,y) + w2*wd
    loss.backward()
    with torch.no_grad():
        for p in model.parameters():
            p.sub_(lr * p.grad)
            p.grad.zero_()
    return loss.item()

losses = [Update(x,y,lr) for x,y in data.train_dl]

And Here’s my output (specifically the runtime error):

RuntimeError: size mismatch, m1: [43008 x 224], m2: [150528 x 5] at /opt/conda/conda-bld/pytorch_1579022060824/work/aten/src/THC/generic/THCTensorMathBlas.cu:290

Can you check if this works?

def forward(self, xb):
xb.view(xb.size(0), -1) #This gives [64, 3 * 224 * 224], which is your linear layer input’s size (150528)
return self.lin(xb)

1 Like

Thanks for the suggestion!

I tried running that and it doesn’t seem to have made a difference, the runtime error is still a matrix mismatch.

See the full output of running model(x) below (if that’s helpful)

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-15-bd0200007a4a> in <module>
----> 1 model(x)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

<ipython-input-10-77e2314778fa> in forward(self, xb)
      6     def forward(self, xb):
      7         xb.view(xb.size(0), -1) #This gives [64, 3 * 224 * 224], which is your linear layer input’s size (150528)
----> 8         return self.lin(xb)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    530             result = self._slow_forward(*input, **kwargs)
    531         else:
--> 532             result = self.forward(*input, **kwargs)
    533         for hook in self._forward_hooks.values():
    534             hook_result = hook(self, input, result)

/opt/conda/lib/python3.7/site-packages/torch/nn/modules/linear.py in forward(self, input)
     85 
     86     def forward(self, input):
---> 87         return F.linear(input, self.weight, self.bias)
     88 
     89     def extra_repr(self):

/opt/conda/lib/python3.7/site-packages/torch/nn/functional.py in linear(input, weight, bias)
   1370         ret = torch.addmm(bias, input, weight.t())
   1371     else:
-> 1372         output = input.matmul(weight.t())
   1373         if bias is not None:
   1374             output += bias

RuntimeError: size mismatch, m1: [43008 x 224], m2: [150528 x 5] at /opt/conda/conda-bld/pytorch_1579022060824/work/aten/src/THC/generic/THCTensorMathBlas.cu:290

I also tried changing the size of the data in my ImageDataBunch directly:

next(iter(data.train_dl))[0] = next(iter(data.train_dl))[0].view(64,150528) #manually change the data size inside the databunch
print(next(iter(data.train_dl))[0].size())

torch.Size([64, 3, 224, 224]) 

It doesn’t seem to have worked.

Can you try reassigning the value after changing shape using ‘view’, as highlighted above statement?

1 Like

That worked perfectly! I’ve been stuck on this for a few days now, and seeing that plot of results just made my day. Thanks for the help! :smiley: