Lesson 2: getting error with creating ImageDataBunch from_folder

I am trying to create an ImageDataBunch object from a set of images I downloaded from Google, following the approach in Lesson2. But when creating the object, I am getting some error related to datatypes. Here is the line of code I am using to create the object:

data = ImageDataBunch.from_folder(path, train=".", 
                                  valid_pct=0.2, ds_tfms=get_transforms(),
                                  size=224, bs=bs, num_workers=4).normalize(imagenet_stats)

and this is the error I get:


RuntimeError Traceback (most recent call last)
/usr/local/lib/python3.6/dist-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:

/usr/local/lib/python3.6/dist-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()

/usr/local/lib/python3.6/dist-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

/usr/local/lib/python3.6/dist-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)

/usr/local/lib/python3.6/dist-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)

/usr/local/lib/python3.6/dist-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 #2 ‘mat2’ in call to _th_mm

During handling of the above exception, another exception occurred:

Exception Traceback (most recent call last)
in
2 data = ImageDataBunch.from_folder(path, train=".",
3 valid_pct=0.2, ds_tfms=get_transforms(),
----> 4 size=224, bs=bs, num_workers=4).normalize(imagenet_stats)

/usr/local/lib/python3.6/dist-packages/fastai/vision/data.py in from_folder(cls, path, train, valid, test, valid_pct, seed, classes, **kwargs)
109 else: src = il.split_by_rand_pct(valid_pct, seed)
110 src = src.label_from_folder(classes=classes)
–> 111 return cls.create_from_ll(src, test=test, **kwargs)
112
113 @classmethod

/usr/local/lib/python3.6/dist-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,

/usr/local/lib/python3.6/dist-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)

/usr/local/lib/python3.6/dist-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)))

/usr/local/lib/python3.6/dist-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: It’s not possible to apply those transforms to your dataset:
Expected object of scalar type Float but got scalar type Double for argument #2 ‘mat2’ in call to _th_mm

Any ideas what might be happening here? My data is in the imagenet style format, similar to the one created in Lesson2 notebook.

Can you share the complete error trace that tracks all the files loaded?

updated the original post with the complete error trace. Thanks in advance for your help!

Hmmmm… Maybe we can start by removing transforms one by one from the get_transforms, or remove them all together.

Also check that all the items in the folder are indeed images. I have had broken links causing contamination.

removing them all together kind of works but then how can augment my data? All items in the folder are images.
Thanks a lot for your help!!

Can you share all the code up until where it breaks? Somewhere along the line, the precision seems to be changing from float to double.

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

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

After a lot of tries and trying to figure out what might be causing this problem, I may have an idea of who’s the culprit. The cause of the problem is the affine transformations inside the get_transforms() method. If you just create a get_transforms() object outside the ImageDataBunch and print the list of transforms, you’ll see that there are about 6 transformations being applied in total to the training set. I think couple of them are affine.
What I ended up doing was to use the rest of the transformations from that list and create a new list with them. Then I used this new list of transforms for the training set.

Note: I did not made any changes to the transformation for the validation set. It just has one transformation anyways.

Hope that helps!!

1 Like

Hi! Thanks for your help.

I also heard from others and apparently the problem were the versions of torch and python on my dedicated environment for fastai. There were some package not compatible with each other.
I solved my problem installing the right versions of python and pytorch.
If others will face the same issue, I would suggest to start since the beginning with a dedicated environment with python v. 3.6. Then, while installing fastai, automatically pytorch will be v. 1.4.0 and not v.1.5.0.
Thanks @impiyush for your suggestion.

I can confirm that using pytorch==1.4 got me past the exception that @anabel was seeing.

Is there a reason the course-v3 repo doesn’t include some kind of dependency management? I’m new to Conda but have ~15 years of Python experience and I’m having trouble understanding why there isn’t a requirements.txt, Pipfile, or environment.yml (which I just learned about). Does the course expect us to provide this ourselves or is there another solution I have found?

I appreciate the help!

4 Likes

I was having the exact same issue and locking the version to 1.4.0 solved it as well.

No need to re-install an older version of pytorch I got the same error message running pytorch 1.5 fixed by calling the get_transforms() command before the ImageDataBunch command:

np.random.seed(2)
pat = r’/([^/]+)_\d+.jpg$’
ds_tfms=get_transforms()
data = ImageDataBunch.from_name_re(path_img, fnames, pat, size=224, bs=bs).normalize(imagenet_stats)
data.show_batch(rows=3, figsize=(7,6))

how you solved it?? i am getting this error till now and don’t really know how to solve it

can you provide me with any code or something…bcoz i am still getting this error.

np.random.seed(2)
pat = r’/([^/]+)_\d+.jpg$’
ds_tfms=get_transforms()
data = ImageDataBunch.from_name_re(path_img, fnames, pat, size=224, bs=bs).normalize(imagenet_stats)
data.show_batch(rows=3, figsize=(7,6))

it is working fine for from_name_re but the problem is with from_folder.

This is the command I use to install Pytorch, all I did was add the version number to the command.

conda config --set ssl_verify no && conda install -y -c pytorch pytorch=1.4.0 torchvision cuda92

torch version 1.4.0 requires torchvision version 0.5.0
Therefore, if you install using pip, the command should be:

pip install "torch==1.4" "torchvision==0.5.0"

5 Likes

Thank you so much @mattbriancon. I was stuck with this issue for a day or two and finally focused and found your advice. It worked out great.