Changing data input size error: Expected 4D tensor got 2D tensor instead

Hi,
I’m trying to repeat the dogbreed challenge done during the lecture. However, when I try to change the size of the inputs I get a strange error. Perhaps others have encountered this already?

Here is a minimal working example:

from fastai.imports import *

from fastai.transforms import *
from fastai.conv_learner import *
from fastai.model import *
from fastai.dataset import *
from fastai.sgdr import *
from fastai.plots import *

PATH = 'data/dogbreed/'
labels = pd.read_csv(f'{PATH}labels.csv')
arch = resnet34
val_idxs = get_cv_idxs(labels.shape[0])
def get_data(sz, bs):
    tfms = tfms_from_model(arch, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
    data = ImageClassifierData.from_csv(PATH, 'train' ,f'{PATH}labels.csv', 
                                        test_name='test', val_idxs=val_idxs, 
                                        suffix='.jpg', tfms=tfms, bs=bs)
    return data if sz > 300 else data.resize(340, 'tmp')

data = get_data(340,32)
learn = ConvLearner.pretrained(arch, data, precompute=True)

learn.set_data(get_data(400,24))

And it produces the following error:

 0%|          | 0/256 [00:00<?, ?it/s]
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-19-6fe85d47524c> in <module>()
----> 1 learn.set_data(get_data(400,24))

~/fastai/courses/dl1/fastai/conv_learner.py in set_data(self, data)
    103     def set_data(self, data):
    104         super().set_data(data)
--> 105         self.save_fc1()
    106         self.freeze()
    107 

~/fastai/courses/dl1/fastai/conv_learner.py in save_fc1(self)
    124         if len(self.activations[0])==0:
    125             m=self.models.top_model
--> 126             predict_to_bcolz(m, self.data.fix_dl, act)
    127             predict_to_bcolz(m, self.data.val_dl, val_act)
    128             if self.data.test_dl: predict_to_bcolz(m, self.data.test_dl, test_act)

~/fastai/courses/dl1/fastai/model.py in predict_to_bcolz(m, gen, arr, workers)
     12     m.eval()
     13     for x,*_ in tqdm(gen):
---> 14         y = to_np(m(VV(x)).data)
     15         with lock:
     16             arr.append(y)

~/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)

~/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)
     65     def forward(self, input):
     66         for module in self._modules.values():
---> 67             input = module(input)
     68         return input
     69 

~/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    222         for hook in self._forward_pre_hooks.values():
    223             hook(self, input)
--> 224         result = self.forward(*input, **kwargs)
    225         for hook in self._forward_hooks.values():
    226             hook_result = hook(self, input, result)

~/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/modules/conv.py in forward(self, input)
    252     def forward(self, input):
    253         return F.conv2d(input, self.weight, self.bias, self.stride,
--> 254                         self.padding, self.dilation, self.groups)
    255 
    256 

~/src/anaconda3/envs/fastai/lib/python3.6/site-packages/torch/nn/functional.py in conv2d(input, weight, bias, stride, padding, dilation, groups)
     46     """
     47     if input is not None and input.dim() != 4:
---> 48         raise ValueError("Expected 4D tensor as input, got {}D tensor instead.".format(input.dim()))
     49 
     50     f = ConvNd(_pair(stride), _pair(padding), _pair(dilation), False,

ValueError: Expected 4D tensor as input, got 2D tensor instead.

I’m not sure what I’m doing that is different to the lecture.

1 Like

Ah - you can’t resize when precompute=True. (This is a bug - sorry about that!)

4 Likes

Hi @johnnyv,

Have you found a way to rectify/overcome this bug? Do kindly let me know. Thank you.