Custom Normalization

Hello,
I wanted to ask a question about normalizing inputs.
Where would you implement custom normalizing functions, for instance I have a type of input that could be normalized in various ways (std-mean, polar-coordinates, etc…), but would like to retain the automatic denorm to visualize the inputs.
So my options are:

  • Creating a PreProcessor to normalize inputs, how I would denorm them?
  • Instantiating normalize in a custom DataBunch ?
  • Just normalizing before giving data to fastai.

Check the source of fastai.vision.data.ImageDataBunch.normalize in particular:

   self.norm,self.denorm = normalize_funcs(*self.stats, do_x=do_x, do_y=do_y)
   self.add_tfm(self.norm)

So you should be able to just add your own norm/denorm functions and add norm as a transform and fastai will call them.

what if I want to implement:

  • standard_norm
  • polar_norm
  • radial_norm
  • etc…

It doesn’t have to be a databunch method. You can have:

def polar_norm(db:ImageDataBunch, stats):
    db.norm = partial(do_polar_norm, stats=stats)
    db.denorm = partial(do_polar_denorm, stats=stats)
    db.add_tfm(db.norm)
    return db

And so on for others. Check the normalize_funcs for how the do_* functions should work, it’s just returning a pair of norm/denorm functions.

Though the above would also work as a databunch method. In fact you could just add ImageDataBunch.polar_norm = polar_norm at the end of the above code and then any ImageDataBunch would have that polar_norm method.

1 Like

thanks I will try that !