Transform with custom function

Can I define more complex transformations of the dataset after the standard ones in

tfms = tfms_from_model(resnet34, sz, aug_tfms=transforms_side_on, max_zoom=1.1)
data = ImageClassifierData.from_paths(PATH, tfms=tfms)

I would like to do Fourier transform of already rotated, cropped and zoomed image. May be the file https://github.com/fastai/fastai/blob/master/fastai/transforms.py should be modified somehow.

def transf(image):
    A = np.exp(image)
    A = np.fft.fft2(A)
    return np.log(np.abs(A))

Hey krasin!

I am looking at a similar problem.
Did you get it to work?

Cheers,
Fabian

Hi Fabian,

I gave up. But my current view is that this transform should be part of the model and not the tfms. Now I am learning how to create a custom model.

Krasin

I wrote a small test to convert the image to black-and-white:

class BWTransform(Transform):

def __init__(self, tfm_y=TfmType.NO):
    self.tfm_y = tfm_y

def set_state(self):
    pass

def do_transform(self, x):
    x[:, :, 0-2] = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
    return x

Then I include it like this:

tfms = tfms_from_model(arch, IMAGE_SIZE, aug_tfms=[BWTransform()], max_zoom=1.1)

I don’t do any random stuff as I want to transform every image in that step.
(make sure that you set precompute to False - otherwise it will not do any transformation)

4 Likes

Thank you @moseroli! It works :slight_smile:

1 Like

Hi,

@moseroli, how would you implement this in the newest version (1.x) of fastai? I would appreciate if you could update your answer.

Thanks in advance!

1 Like