Error (pytorch?): Expected object of scalar type Long but got scalar type Float for argument #2 'other

Thank you @phillies, this fixed the problem for me :+1:

I have the same error, but in my case it is due to an error creating the ImageList

src = (ImageList.from_df(df.sample(n=200), path, folder='train', suffix='.jpeg')
       .split_by_rand_pct()
       .label_from_df(label_delim=',')   # Error!
      )
data = (src.transform(get_transforms(max_warp=0.), size=64).databunch().normalize())
learn = cnn_learner(data,  models.resnet34, metrics=error_rate)
...
RuntimeError: Expected object of scalar type Long but got scalar type Float for argument #2 'other'

the correct version works for me:

src = (ImageList.from_df(df.sample(n=200), path, folder='train', suffix='.jpeg')
       .split_by_rand_pct()
       .label_from_df(cols='level')
      )
3 Likes

Iโ€™m also getting this error now, when trying to create a dataBunch

np.random.seed(44)
data = ImageDataBunch.from_folder(path, 
                                  train=".",
                                  valid_pct=0.22,
                                  ds_tfms=get_transforms(),
                                  size=224, 
                                  num_workers=4,
                                  bs=32).normalize(imagenet_stats)


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

10 frames
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)
/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 #3 'mat2' in call to _th_addmm_out

Since my error is at the ImageDataBunch call, I donโ€™t think the previous answers have solved for this. Itโ€™s weird, this same code was working fine a week ago.

1 Like

Did you ever get a fix for this @f_izco? Iโ€™m facing the same thing now.

You need to upgrade your fastai to the latest version. This comes from a breaking change in PyTorch 1.5 and it was fixed recently.

1 Like