Custom Preprocessing Function

Is there a way to pass in a custom function which can add to preprocessing of the data. Like if I want to equalise the histogram of an image before passing it to network, what is the best way to do it in existing fastai setup?

Take a look at You can define new class to do transformation and pass it with tfms.

And it doesn’t even need to be a class - you can pass a function too!

Do you have an example for such a class or function handy?

I tried the following class

class gammaCorrection():
def __init__(self, b): self.b = b
def __call__(self, x):
    gamma = self.b
    return np.uint8(cv2.pow(x / 255., gamma)*255.)

used it as

tfms = tfms_from_model(arch, sz, aug_tfms=[gammaCorrection(0.8)], max_zoom=1.1)
data = ImageClassifierData.from_paths(PATH, bs=bs, tfms=tfms)
learn = ConvLearner.pretrained(arch, data, precompute="True", ps=0.2)

But it gave me the following error:

    ~/.conda/envs/fastai/lib/python3.6/site-packages/fastai/ in compose(im, y, fns)
    def compose(im, y, fns):
           for fn in fns:
    --> 446         im, y =fn(im, y)
                    return im if y is None else (im, y)

when I call, 2, cycle_len=1)

Any ideas? (I am currently working on the kaggle IEEE competition where the deadline is tomorrow :slight_smile: )