如何在 fastai 中为 get_transform 添加附加变换?

我想以这种方式添加额外的增强:


 additional_aug=[zoom_crop(scale=(0.75,1.25), do_rand=False), 

                brightness(), 

                contrast()

               ]

tfms = get_transforms(do_flip=True,flip_vert=True,max_lighting=0.2, xtra_tfms=additional_aug)


data = (ImageList.from_df(df=df,path='./',cols='path') 

        .split_by_rand_pct(0.2) 

        .label_from_df(cols='diagnosis',label_cls=FloatList) 

        .transform(tfms,size=sz,resize_method=ResizeMethod.SQUISH,padding_mode='zeros') 

        .databunch(bs=bs,num_workers=4) 

        .normalize(imagenet_stats)  

       )

但我得到错误:


--------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

/opt/conda/lib/python3.6/site-packages/fastai/data_block.py in _check_kwargs(ds, tfms, **kwargs)

    590         x = ds[0]

--> 591         try: x.apply_tfms(tfms, **kwargs)

    592         except Exception as e:


/opt/conda/lib/python3.6/site-packages/fastai/vision/image.py in apply_tfms(self, tfms, do_resolve, xtra, size, resize_method, mult, padding_mode, mode, remove_out)

    105         if resize_method <= 2 and size is not None: tfms = self._maybe_add_crop_pad(tfms)

--> 106         tfms = sorted(tfms, key=lambda o: o.tfm.order)

    107         if do_resolve: _resolve_tfms(tfms)


/opt/conda/lib/python3.6/site-packages/fastai/vision/image.py in <lambda>(o)

    105         if resize_method <= 2 and size is not None: tfms = self._maybe_add_crop_pad(tfms)

--> 106         tfms = sorted(tfms, key=lambda o: o.tfm.order)

    107         if do_resolve: _resolve_tfms(tfms)


AttributeError: 'list' object has no attribute 'tfm'


During handling of the above exception, another exception occurred:

根据文档xtra_tfms:Optional[ Collection[ ]]= ) →  [ ] Transform NoneCollection Transform


如何让它工作?


杨__羊羊
浏览 253回答 1
1回答

弑天下

我遇到了这个问题,解决方案非常简单。只需在列表包围的单独列表中调用要应用的每个转换函数,并将其传递给 get_transforms 函数的 xtra_tfms 参数。(它甚至可以是元组的元组或任何集合)additional_aug=[[zoom_crop(scale=(0.75,1.25), do_rand=False)],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [brightness()],&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [contrast()]]tfms = get_transforms(do_flip=True,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flip_vert=True,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; max_lighting=0.2,&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; xtra_tfms=additional_aug)希望这能解决您的问题。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python