pytorch - 在 FastAI 中对数据应用图像增强变换时出错

标签 pytorch fast-ai

我正在尝试复制这个 kaggle 笔记本 https://www.kaggle.com/tanlikesmath/diabetic-retinopathy-with-resnet50-oversampling在 Google Colab 上。代码直到昨天都运行良好,但今天它抛出了一个运行时错误。下面是有问题的代码:

tfms = get_transforms(do_flip=True,flip_vert=True,max_rotate=360,max_warp=0,max_zoom=1.1,max_lighting=0.1,p_lighting=0.5)
src = (ImageList.from_df(df=df,path=data_path,cols='path') #get dataset from dataset //ImageItemList threw errors so changed to ImageList 
        .split_by_idx(range(len(train_df)-1,len(df))) #Splitting the dataset
        .label_from_df(cols='level') #obtain labels from the level column
      )
data= (src.transform(tfms,size=sz) #Data augmentation
        .databunch(bs=bs,num_workers=0) #DataBunch
        .normalize(imagenet_stats) #Normalize
       )

我收到以下错误:

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

8 frames
/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 #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-74-31aae73a70fc> in <module>()
      6       )
      7 print(src)
----> 8 data= (src.transform(tfms,size=sz) #Data augmentation
      9         .databunch(bs=bs,num_workers=0) #DataBunch
     10         .normalize(imagenet_stats) #Normalize

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

我在这段代码中没有做任何改变,它和昨天一样,但由于某种原因,它今天给了我一个错误。请帮助。

编辑:我发现它在我本地的 Jupyter 笔记本上运行良好。尽管如此,Colab 仍然显示错误

最佳答案

colab中使用的torch似乎有些问题

FastAI Forum
在运行 fastAI python 代码之前,尝试在您的 colab 中安装特定版本的 Torch

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

关于pytorch - 在 FastAI 中对数据应用图像增强变换时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61503339/

相关文章:

tensorflow - 将 tf.data.Dataset.from_tensor_slices 转换为 pytorch

python - PyTorch 尝试第二次向后浏览图表时出错

pytorch - 使用fastai的learn.lr_find()选择learning_rate

python - Fastai - 在句子处理器,cache_dir 参数中启动语言模型失败

nlp - 在另一台机器上加载经过训练的模型——fastai、torch、huggingface

python - Fastai 文本分类器 : batch prediction on unseen data

python - 如何在fastai中向get_transform添加额外的变换?

python - 为什么在执行 .backward() 之前使用 torch.sum() ?

python - 使用 DataLoader 加载数据时跳过错误数据点

pytorch - Resize Vs CenterCrop Vs RandomResizedCrop Vs RandomCrop