Want to use xtra transforms

Hi i want to call
function contrast in transforms.py. I think it can be specified as xtra transforms but i dont know the usage
xtra_tfms = contrast(scale=1.05)

This is giving error while doing show batch.

log_uniform() missing 1 required positional argument: 'high

Hi! I am completely lost with xtra_tfms as well. I tried everything, I even manages to empty my DataBunch :slight_smile:
Please could anyone give a good example of usage?

1 Like

Try this:

tfms = get_transforms(xtra_tfms=[contrast(scale=(0.5, 1.5), p=0.75)])
print(tfms)

Notice however that contrast is applied by default so you are adding it twice! get_transforms returns a tuple with two lists, the first have the transforms for the train set and the other for the validation and test set. You can see exactly what transforms are being applied by looking at what is in the list.

2 Likes

Thanks I’ll try that!

Do you know what the similar version would look like for also adding random cropping?

The example in the docs is giving me an error below.

tfms = [rand_resize_crop(224)]
https://docs.fast.ai/vision.transform.html

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
~/anaconda3/lib/python3.7/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:

~/anaconda3/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)
    104         resize_method = ifnone(resize_method, default_rsz)
--> 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)

~/anaconda3/lib/python3.7/site-packages/fastai/vision/transform.py in _image_maybe_add_crop_pad(img, tfms)
    206 def _image_maybe_add_crop_pad(img, tfms):
--> 207     tfm_names = [tfm.__name__ for tfm in tfms]
    208     return [crop_pad()] + tfms if 'crop_pad' not in tfm_names else tfms

~/anaconda3/lib/python3.7/site-packages/fastai/vision/transform.py in <listcomp>(.0)
    206 def _image_maybe_add_crop_pad(img, tfms):
--> 207     tfm_names = [tfm.__name__ for tfm in tfms]
    208     return [crop_pad()] + tfms if 'crop_pad' not in tfm_names else tfms

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

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
<ipython-input-44-45eb2c203f8b> in <module>
     22                                      max_warp=0.6,
     23                                      p_affine=0.5,
---> 24                                      ), size=size, tfm_y=True)
     25         .databunch(bs=bs)
     26         .normalize(imagenet_stats))

~/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in transform(self, tfms, **kwargs)
    500         if not tfms: tfms=(None,None)
    501         assert is_listy(tfms) and len(tfms) == 2, "Please pass a list of two lists of transforms (train and valid)."
--> 502         self.train.transform(tfms[0], **kwargs)
    503         self.valid.transform(tfms[1], **kwargs)
    504         if self.test: self.test.transform(tfms[1], **kwargs)

~/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in transform(self, tfms, tfm_y, **kwargs)
    719     def transform(self, tfms:TfmList, tfm_y:bool=None, **kwargs):
    720         "Set the `tfms` and `tfm_y` value to be applied to the inputs and targets."
--> 721         _check_kwargs(self.x, tfms, **kwargs)
    722         if tfm_y is None: tfm_y = self.tfm_y
    723         tfms_y = None if tfms is None else list(filter(lambda t: getattr(t, 'use_on_y', True), listify(tfms)))

~/anaconda3/lib/python3.7/site-packages/fastai/data_block.py in _check_kwargs(ds, tfms, **kwargs)
    591         try: x.apply_tfms(tfms, **kwargs)
    592         except Exception as e:
--> 593             raise Exception(f"It's not possible to apply those transforms to your dataset:\n {e}")
    594 
    595 class LabelList(Dataset):

Exception: It's not possible to apply those transforms to your dataset:
 'list' object has no attribute '__name__'
1 Like

Exactly what I have been facing in the past few months, searched all the forums and seen many same cases yet no one has had an answer.

1 Like

Months later…but having this problem myself today (Jan 2020) with the same _check_kwargs function. I think the problem may be that _check_kwargs expects a single list containing names, but for some reason tfms is a list containing sub-lists. So
tfm_names = [tfm.__name__ for tfm in tfms] fails, but
tfm_names = [tfm.__name__ for tfm in tfms[0]] succeeds.

Maybe the list could be flattened out?
[tfm.__name__ for parts in tfms for tfm in parts]

But I’m not sure

Yes, tfms is a list of list:

myTfmsArr = getTransforms(...) # Add xtra_tfms as @mnpinto said 
myTfmsArr = [[train_tfms], [valid_tfms]] 

NOTE: normally you don’t apply data augmentation to your validation set, so the second array is empty.
The transforms that usually you apply to validation set are those required to conversion (ie: size, move to gpu, etc.). These “required” transforms are added both to train and valid behind the scene to your myTfmsArr while databunch is created.

Any chance in a fix for this. I’m getting the same error.

np.random.seed(42)
src = (ImageList.from_df(df, path=f’{path}/images’, suffix=’.jpg’)
.split_by_rand_pct()
.label_from_df(cols=5)
.transform(tfms, size=128)
.databunch().normalize(imagenet_stats)
)

data = (src.transform(tfms, size=256))


AttributeError Traceback (most recent call last)
~/anaconda3/envs/python3/lib/python3.6/site-packages/fastai/data_block.py in _check_kwargs(ds, tfms, **kwargs)
597 x = ds[0]
–> 598 try: x.apply_tfms(tfms, **kwargs)
599 except Exception as e:

~/anaconda3/envs/python3/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)
104 resize_method = ifnone(resize_method, default_rsz)
–> 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)

~/anaconda3/envs/python3/lib/python3.6/site-packages/fastai/vision/transform.py in _image_maybe_add_crop_pad(img, tfms)
206 def _image_maybe_add_crop_pad(img, tfms):
–> 207 tfm_names = [tfm.name for tfm in tfms]
208 return [crop_pad()] + tfms if ‘crop_pad’ not in tfm_names else tfms

~/anaconda3/envs/python3/lib/python3.6/site-packages/fastai/vision/transform.py in (.0)
206 def _image_maybe_add_crop_pad(img, tfms):
–> 207 tfm_names = [tfm.name for tfm in tfms]
208 return [crop_pad()] + tfms if ‘crop_pad’ not in tfm_names else tfms

AttributeError: ‘list’ object has no attribute ‘name

During handling of the above exception, another exception occurred:

Exception Traceback (most recent call last)
in
----> 1 data = (src.transform(tfms, size=256))

~/anaconda3/envs/python3/lib/python3.6/site-packages/fastai/data_block.py in transform(self, tfms, tfm_y, **kwargs)
726 def transform(self, tfms:TfmList, tfm_y:bool=None, **kwargs):
727 “Set the tfms and tfm_y value to be applied to the inputs and targets.”
–> 728 _check_kwargs(self.x, tfms, **kwargs)
729 if tfm_y is None: tfm_y = self.tfm_y
730 tfms_y = None if tfms is None else list(filter(lambda t: getattr(t, ‘use_on_y’, True), listify(tfms)))

~/anaconda3/envs/python3/lib/python3.6/site-packages/fastai/data_block.py in _check_kwargs(ds, tfms, **kwargs)
598 try: x.apply_tfms(tfms, **kwargs)
599 except Exception as e:
–> 600 raise Exception(f"It’s not possible to apply those transforms to your dataset:\n {e}")
601
602 class LabelList(Dataset):

Exception: It’s not possible to apply those transforms to your dataset:
‘list’ object has no attribute ‘name