Creating custom transforms

Hi!

I’m trying to write a custom transform for the webserver project from lesson, basically I want to crop the images a certain way since I know the location in the image I am interested in (bottom, center).

I’ve googled and read most of what I could find of relevant posts and documentation, but have been unable to make it work. I’ve tried various solutions, but what I have at the moment is

def do_crop(x):
    return crop(x, 500, row_pct=0.5, col_pct=1.0)

cropper = TfmPixel(do_crop)

path = Path('my_data_set')
tfms = [cropper]
# transforms = get_transforms(do_flip=False,max_zoom=1.0,max_rotate=0.0,max_warp=0.0)
np.random.seed(42)
data = ImageDataBunch.from_folder(path, train=".", valid_pct=0.2,
        ds_tfms=(tfms, tfms), size=224, num_workers=4).normalize(imagenet_stats)

I’ve also tried putting the custom transform inside xtra_tfms in get_transforms and using the transforms object, but I still get the same error.

It returns a warning
...fastai/basic_data.py:226: UserWarning: There seems to be something wrong with your dataset, can't access any element of self.train_ds.

If I use the transform object without my xtra_tfms it works, so clearly I am doing something wrong. Anyone able to shed some light on how to do this?

Some additional info that I am unsure if would be relevant; If I try to call data.show_batch(…) after trying to initialize the data, it says

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

Thanks in advance!

3 Likes

I’ve just started looking into this myself, but in your review did you see this thread on Adding a custom transform? Someone in that thread ran into the same issue.

It may be silly, but it looks like in all the examples the function name starts with an underscore - did you end up trying that? (i.e., naming it _do_crop). Also - do the transforms you pass to ImageDataBunch need to be in a list (per Sylvain’s comment in that thread)?

If you figure out the issue could you post the solution here? I am sure I will be running into the same issues as I look into this.

@kreativ hey, did you get the solution to the custom transforms. Been trying to figure it out.