Lesson1-pets UserWarning on IMAGEDATABUNCH

Hi ! I just clone repo and work on the lesson1-pets.ipynb and when I’m run this

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

this cell it’s given a error like that and when the next cell run it’ will give another error like that:

My enviroments:

  • Windows 10
  • conda 4.6.2
  • fastai 1.0.38
  • torch 1.0.1

How can I fix this ?

Thanks :slight_smile:

Hi to everyone, I found nearly the same ‘Exception Error’ while running Lesson 1.
Thanks in advance for you help or suggestions.
After running the code with num_workers=0 in ImageDataBunch, where the bs=16 :

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

The result is:

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

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py 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()

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py 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
    525 

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py 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)

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py 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)

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py 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-9-823e074f18df> in <module>
----> 1 data = ImageDataBunch.from_name_re(path_img, fnames, pat, ds_tfms=get_transforms(), size=224, num_workers=0,bs=bs
      2                                   ).normalize(imagenet_stats)

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

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/data.py 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)
    148 
    149     @classmethod

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/data.py 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,

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/data_block.py 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)

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/data_block.py 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)))

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/data_block.py 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}")
    597 
    598 class LabelList(Dataset):

Exception: Its 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

If I first run ImageDataBunch without num_workers=0 :

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

I get this:

You can deactivate this warning by passing `no_check=True`.
/Users/.../opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/basic_data.py:262: UserWarning: There seems to be something wrong with your dataset, for example, in the first batch cant access these elements in self.train_ds: 1065,3058,4571,1898,2600...
  warn(warn_msg)

and at the next step:

data.show_batch(rows=3, figsize=(7,6))

I get again a failure:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-10-66824b983385> in <module>
----> 1 data.show_batch(rows=3, figsize=(7,6))

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/basic_data.py in show_batch(self, rows, ds_type, reverse, **kwargs)
    184     def show_batch(self, rows:int=5, ds_type:DatasetType=DatasetType.Train, reverse:bool=False, **kwargs)->None:
    185         "Show a batch of data in `ds_type` on a few `rows`."
--> 186         x,y = self.one_batch(ds_type, True, True)
    187         if reverse: x,y = x.flip(0),y.flip(0)
    188         n_items = rows **2 if self.train_ds.x._square_show else rows

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/basic_data.py in one_batch(self, ds_type, detach, denorm, cpu)
    167         w = dl.num_workers
    168         dl.num_workers = 0
--> 169         try:     x,y = next(iter(dl))
    170         finally: dl.num_workers = w
    171         if detach: x,y = to_detach(x,cpu=cpu),to_detach(y,cpu=cpu)

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/basic_data.py in __iter__(self)
     73     def __iter__(self):
     74         "Process and returns items from `DataLoader`."
---> 75         for b in self.dl: yield self.proc_batch(b)
     76 
     77     @classmethod

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/utils/data/dataloader.py in __next__(self)
    343 
    344     def __next__(self):
--> 345         data = self._next_data()
    346         self._num_yielded += 1
    347         if self._dataset_kind == _DatasetKind.Iterable and \

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/utils/data/dataloader.py in _next_data(self)
    383     def _next_data(self):
    384         index = self._next_index()  # may raise StopIteration
--> 385         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    386         if self._pin_memory:
    387             data = _utils.pin_memory.pin_memory(data)

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/torch/utils/data/_utils/fetch.py in <listcomp>(.0)
     42     def fetch(self, possibly_batched_index):
     43         if self.auto_collation:
---> 44             data = [self.dataset[idx] for idx in possibly_batched_index]
     45         else:
     46             data = self.dataset[possibly_batched_index]

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/data_block.py in __getitem__(self, idxs)
    652             else:                 x,y = self.item   ,0
    653             if self.tfms or self.tfmargs:
--> 654                 x = x.apply_tfms(self.tfms, **self.tfmargs)
    655             if hasattr(self, 'tfms_y') and self.tfm_y and self.item is None:
    656                 y = y.apply_tfms(self.tfms_y, **{**self.tfmargs_y, 'do_resolve':False})

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py in apply_tfms(self, tfms, do_resolve, xtra, size, resize_method, mult, padding_mode, mode, remove_out)
    121                 if resize_method in (ResizeMethod.CROP,ResizeMethod.PAD):
    122                     x = tfm(x, size=_get_crop_target(size,mult=mult), padding_mode=padding_mode)
--> 123             else: x = tfm(x)
    124         return x.refresh()
    125 

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py in __call__(self, x, *args, **kwargs)
    522     def __call__(self, x:Image, *args, **kwargs)->Image:
    523         "Randomly execute our tfm on `x`."
--> 524         return self.tfm(x, *args, **{**self.resolved, **kwargs}) if self.do_run else x
    525 
    526 def _resolve_tfms(tfms:TfmList):

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py in __call__(self, p, is_random, use_on_y, *args, **kwargs)
    468     def __call__(self, *args:Any, p:float=1., is_random:bool=True, use_on_y:bool=True, **kwargs:Any)->Image:
    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)
    472 

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py in calc(self, x, *args, **kwargs)
    473     def calc(self, x:Image, *args:Any, **kwargs:Any)->Image:
    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)
    477 

~/opt/anaconda3/envs/fastai/lib/python3.7/site-packages/fastai/vision/image.py in affine(self, func, *args, **kwargs)
    181         "Equivalent to `image.affine_mat = image.affine_mat @ func()`."
    182         m = tensor(func(*args, **kwargs)).to(self.device)
--> 183         self.affine_mat = self.affine_mat @ m
    184         return self
    185 

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

My OS is macOS Catalina v. 10.15.3:

=== Software === 
python       : 3.7.6
fastai       : 1.0.60
fastprogress : 0.2.2
torch        : 1.5.0a0+917e56e
torch cuda   : None / is **Not available** 

=== Hardware === 
No GPUs available 

=== Environment === 
platform     : Darwin-19.3.0-x86_64-i386-64bit
conda env    : fastai
python       : /Users/.../opt/anaconda3/envs/fastai/bin/python
sys.path     : /Users/.../Documents/GitHub/course-v3/zh-nbs
/Users/.../opt/anaconda3/envs/fastai/lib/python37.zip
/Users/.../opt/anaconda3/envs/fastai/lib/python3.7
/Users/.../opt/anaconda3/envs/fastai/lib/python3.7/lib-dynload

/Users/.../opt/anaconda3/envs/fastai/lib/python3.7/site-packages
/Users/.../opt/anaconda3/envs/fastai/lib/python3.7/site-packages/IPython/extensions
/Users/.../.ipython
no supported gpus found on this system

Were you able to solve this error? I’m getting the same error and have no clue o how to proceed further

Hi torch==1.5 is not working with fastai as of now try using torch==1.4.

Use this command to install
pip install torch===1.4.0 torchvision===0.5.0 -f https://download.pytorch.org/whl/torch_stable.html

2 Likes