ImageDataBunch Error

On lesson 1, when trying to import all the training data, I get an error that prevents subsequent plotting and training of the data. I am running a brand new install of Ubuntu 20.04.

data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, bs=16).normalize(imagenet_stats)

  warnings.warn("The default behavior for interpolate/upsample with float scale_factor will change "
/home/dan/.local/lib/python3.8/site-packages/torch/nn/ UserWarning: The default behavior for interpolate/upsample with float scale_factor will change in 1.6.0 to align with other frameworks/libraries, and use scale_factor directly, instead of relying on the computed output size. If you wish to keep the old behavior, please set recompute_scale_factor=True. See the documentation of nn.Upsample for details. 
  warnings.warn("The default behavior for interpolate/upsample with float scale_factor will change "
You can deactivate this warning by passing `no_check=True`.
/home/dan/.local/lib/python3.8/site-packages/fastai/ UserWarning: There seems to be something wrong with your dataset, for example, in the first batch can't access these elements in self.train_ds: 1696,547,5566,1049,1152...

Can anyone please help me out here as well? Not the same error as above, but there seems to be an issue with some underlying library. I have tried deleting the datasets and download them independently through the course website, but no luck.

The following is the instruction from lesson 1:

The following is the error I recieve:

RuntimeError                              Traceback (most recent call last)
~/anaconda3/lib/python3.7/site-packages/fastai/ in _check_kwargs(ds, tfms, **kwargs)
    593         x = ds[0]
--> 594         try: x.apply_tfms(tfms, **kwargs)
    595         except Exception as e:

~/anaconda3/lib/python3.7/site-packages/fastai/vision/ in apply_tfms(self, tfms, do_resolve, xtra, size, resize_method, mult, padding_mode, mode, remove_out)
    122                     x = tfm(x, size=_get_crop_target(size,mult=mult), padding_mode=padding_mode)
--> 123             else: x = tfm(x)
    124         return x.refresh()

~/anaconda3/lib/python3.7/site-packages/fastai/vision/ in __call__(self, x, *args, **kwargs)
    523         "Randomly execute our tfm on `x`."
--> 524         return self.tfm(x, *args, **{**self.resolved, **kwargs}) if self.do_run else x

~/anaconda3/lib/python3.7/site-packages/fastai/vision/ in __call__(self, p, is_random, use_on_y, *args, **kwargs)
    469         "Calc now if `args` passed; else create a transform called prob `p` if `random`."
--> 470         if args: return self.calc(*args, **kwargs)
    471         else: return RandTransform(self, kwargs=kwargs, is_random=is_random, use_on_y=use_on_y, p=p)

~/anaconda3/lib/python3.7/site-packages/fastai/vision/ in calc(self, x, *args, **kwargs)
    474         "Apply to image `x`, wrapping it if necessary."
--> 475         if self._wrap: return getattr(x, self._wrap)(self.func, *args, **kwargs)
    476         else:          return self.func(x, *args, **kwargs)

~/anaconda3/lib/python3.7/site-packages/fastai/vision/ in affine(self, func, *args, **kwargs)
    182         m = tensor(func(*args, **kwargs)).to(self.device)
--> 183         self.affine_mat = self.affine_mat @ m
    184         return self

RuntimeError: Expected object of scalar type Float but got scalar type Double for argument #3 'mat2' in call to _th_addmm_out

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
<ipython-input-10-5299576508f3> in <module>
----> 1 data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224)
      2 data.normalize(imagenet_stats)

~/anaconda3/lib/python3.7/site-packages/fastai/vision/ in from_name_re(cls, path, fnames, pat, valid_pct, **kwargs)
    156             assert res,f'Failed to find "{pat}" in "{fn}"'
    157             return
--> 158         return cls.from_name_func(path, fnames, _get_label, valid_pct=valid_pct, **kwargs)
    160     @staticmethod

~/anaconda3/lib/python3.7/site-packages/fastai/vision/ in from_name_func(cls, path, fnames, label_func, valid_pct, seed, **kwargs)
    145         "Create from list of `fnames` in `path` with `label_func`."
    146         src = ImageList(fnames, path=path).split_by_rand_pct(valid_pct, seed)
--> 147         return cls.create_from_ll(src.label_from_func(label_func), **kwargs)
    149     @classmethod

~/anaconda3/lib/python3.7/site-packages/fastai/vision/ in create_from_ll(cls, lls, bs, val_bs, ds_tfms, num_workers, dl_tfms, device, test, collate_fn, size, no_check, resize_method, mult, padding_mode, mode, tfm_y)
     95         "Create an `ImageDataBunch` from `LabelLists` `lls` with potential `ds_tfms`."
     96         lls = lls.transform(tfms=ds_tfms, size=size, resize_method=resize_method, mult=mult, padding_mode=padding_mode,
---> 97                             mode=mode, tfm_y=tfm_y)
     98         if test is not None: lls.add_test_folder(test)
     99         return lls.databunch(bs=bs, val_bs=val_bs, dl_tfms=dl_tfms, num_workers=num_workers, collate_fn=collate_fn,

~/anaconda3/lib/python3.7/site-packages/fastai/ in transform(self, tfms, **kwargs)
    503         if not tfms: tfms=(None,None)
    504         assert is_listy(tfms) and len(tfms) == 2, "Please pass a list of two lists of transforms (train and valid)."
--> 505         self.train.transform(tfms[0], **kwargs)
    506         self.valid.transform(tfms[1], **kwargs)
    507         if self.test: self.test.transform(tfms[1], **kwargs)

~/anaconda3/lib/python3.7/site-packages/fastai/ in transform(self, tfms, tfm_y, **kwargs)
    722     def transform(self, tfms:TfmList, tfm_y:bool=None, **kwargs):
    723         "Set the `tfms` and `tfm_y` value to be applied to the inputs and targets."
--> 724         _check_kwargs(self.x, tfms, **kwargs)
    725         if tfm_y is None: tfm_y = self.tfm_y
    726         tfms_y = None if tfms is None else list(filter(lambda t: getattr(t, 'use_on_y', True), listify(tfms)))

~/anaconda3/lib/python3.7/site-packages/fastai/ in _check_kwargs(ds, tfms, **kwargs)
    594         try: x.apply_tfms(tfms, **kwargs)
    595         except Exception as e:
--> 596             raise Exception(f"It's not possible to apply those transforms to your dataset:\n {e}")
    598 class LabelList(Dataset):

Exception: It's not possible to apply those transforms to your dataset:
 Expected object of scalar type Float but got scalar type Double for argument #3 'mat2' in call to _th_addmm_out

yes, there is an outstanding bug with get_transforms in fast ai right now. is anyone looking into this?
in the meantime, you can just remove the get_transform call on your function, though that is likely to hurt your model.

so for you @dcajacob that would look like:
data = ImageDataBunch.from_name_re(path_img, fnames, pat, size=224, bs=16).normalize(imagenet_stats)


Oh thank you so much @aldasmallthings for helping me out here. I found the solution to this problem, and maybe the team at fastai should add this extra note in lesson 1 that PyTorch version needs to be 1.4.0 for this library to work properly. Otherwise, this will make it very easy for beginners to give up while they take their baby steps.


Thanks @aldasmallthings. I had found some indication of what @haris_rab said about needing an older version of pytorch. However, I continued to have the same problem, regardless. I tried backing off the version of fastai I had installed and that seemed to help so, but I haven’t explored further than degrading by one release. Is there documentation of what versions are actually recommended? It’s sort of maddening that there isn’t a requirements.txt or something to make this simple. There really ought to be a set of versions that will work just like in the v3 videos. I used the kaggle environment to scrape what versions they’re using, but the fastai site says they’re often behind, so I don’t know whether to tust those or not.


I get the same error. I tried updating pytorch with conda update pytorch but it still doesnt work. I am still learning so this is frustrating as I cannot continue.

Update: I fixed the issue by DOWNGRADING to pytorch 1.4.0

The issue is not with the whole transformation function, It’s just with rotate.
Don’t use rotate in the code and it should work fine.

[RandTransform(tfm=TfmCrop (crop_pad), kwargs={'row_pct': (0, 1), 'col_pct': (0, 1), 'padding_mode': 'reflection'}, p=1.0, resolved={}, do_run=True, is_random=True), 
 RandTransform(tfm=TfmCoord (symmetric_warp), kwargs={'magnitude': (-0.2, 0.2)}, p=0.75, resolved={}, do_run=True, is_random=True), 
 # RandTransform(tfm=TfmAffine (rotate), kwargs={'degrees': (-40, 40)}, p=0.75, resolved={}, do_run=True, is_random=True), 
 RandTransform(tfm=TfmAffine (flip_affine), kwargs={}, p=0.5, resolved={}, do_run=True, is_random=True), 
 RandTransform(tfm=TfmAffine (zoom), kwargs={'scale': (1.0, 1.4), 'row_pct': (0, 1), 'col_pct': (0, 1)}, p=0.75, resolved={}, do_run=True, is_random=True), 
 RandTransform(tfm=TfmLighting (brightness), kwargs={'change': (0.35, 0.65)}, p=0.75, resolved={}, do_run=True, is_random=True)]
1 Like

I’m getting this too, when resizing some images.

1 Like

Hi , Im getting the same error but it seems its not because the transformations.

Which version of fastai do you download to be compatible with pytorch 1.4.0? I have installed pytorch 1.4.0 and torchvision 0.5.0, but am now able to uninstall fastai via conda install.

You should try this :

Import warnings

This trick work on my code :wink:



On Google Coaboratory, downgrading to torch==1.4.0 breaks torchvision.

@agekow @haris_rab @dcajacob
You can do this :

1 Like

Finally I found the solution
Just paste this on top

!curl -s | bash
!pip uninstall torch torchvision -y
!pip install torch==1.4.0 torchvision==0.5.0

Thanks to Rajnish


I added the 2 !pip commands but didn’t need to do the !curl to resolve my error

Hey jdm1,

When I restart my colab notebook, It seems that we need to install 1.4 pytorch everytime. Is that the case?

1 Like

Yes, I’ve had to do that, but I’m new to Colab. There must be a way to save the environment state. I haven’t researched it yet though, as I am planning to switch to AI Platform Notebooks on GCP

Yes this solution worked for me on Colab. Thanks for the help.

Downgrading to torch 1.4.0 and torchvision 0.5.0 did not resolve the error for me. I am NOT on Colab but rather running plain Jupyter NB (from Fastai install). Any suggestions?

Thanks a lot.

1 Like