Difference between Transform and RandomTransform

I am trying to add new transforms, and I see two choices to extend from
I am not sure which one to use and why?
Transform and RandomTransform.
What is their affect during training and testing?

RandTransform is wrapped on top of Transform, it introduces randomness on your transform. Below is a simple example illustrating their differences:

class double_rand(RandTransform):
    def encodes(self, x: int):
        return x*2

class double(Transform):
    def encodes(self, x: int):
        return x*2

# apply Transform
certain_tfms = double()
certain_tfms(2)
>> 4

# apply RandTransform
rand_tfms = double_rand(p = 0.5)
rand_tfms(2, split_idx = False)
>> 4
rand_tfms(2, split_idx = False)
>> 2

As shown in the example above, there are two parameters controlling the randomness:

  1. argument p in the __init__ method, it controls the chance of applying your transform (e.g. p = 0.8 means applying your transform 80% of the time you call it)
  2. split_idx in your call method __call__ method. By default, if you set split_idx = True, you essentially disable the entire transform (no randomness + no transform). If you set split_idx = False, you enable the random transform (randomness + transform enabled). In practice, this argument is useful in applying differential operations on training set and valid set. (e.g. random resize on training set v.s. deterministic resize on validation set)

For more details, you can review the source code of RandTransform and Transform

1 Like

@riven314 Thank you for the explanation.