Using `kornia` with V2 in a generic way

You can use transforms.RandomApply for the transforms that aren’t inherently “random” like so:

color_jitter = K.augmentation.ColorJitter(0.8*s, 0.8*s, 0.8*s, 0.2*s)
rnd_color_jitter = transforms.RandomApply([color_jitter], p=0.8)

Wrapping individual transform would be needed when you’ve different behavior for different types, like images would be resized using Bilinear interpolation and masks using nearest neighbour. If the set of trasforms are only meant for single fastai type, wrapping them in nn.Sequential and then in some fastai wrapper seems a better idea.

I think if we create separate wrappers for train_tfms and test_tfms like:

class TrainTfms(Transform):
    order=10
    split_idx=0
    def __init__(self,tfm):
        self.tfm=tfm
    def encodes(self,x:TensorImage): return self.tfm(x)
    def __repr__(self): <listing all the transforms>

class ValidTfms(Transform):
    order=10
    split_idx=1
    def __init__(self,tfm):
        self.tfm=tfm
    def encodes(self,x:TensorImage): return self.tfm(x)
    def __repr__(self): <listing all the transforms>

it would be possible to include them in batch_tfms as well. Override __repr__ method to list all the underlying transform so that it won’t just appear like KorniaWrapper

I had already stumbled upon this problem and had asked for some way to pass these parameters through constructor.

this might be related to order of transform, try assigning different order for each transform

1 Like