I am trying to implement
rgb_randomize in fastai2 following Sylvain suggestion. This is my first try to do something like this so I am a bit lost. I am trying to reuse some of the code from fastai1, which is:
def _rgb_randomize(x, channel:int=None, thresh:float=0.3): "Randomize one of the channels of the input image" if channel is None: channel = np.random.randint(0, x.shape - 1) x[channel] = torch.rand(x.shape[1:]) * np.random.uniform(0, thresh) return x rgb_randomize = TfmPixel(_rgb_randomize) class TfmPixel(Transform): "Decorator for pixel tfm funcs." order,_wrap = 10,'pixel'
However, fastai1 and fastai2 implementations are quite different (at least for me ). As I understand from the fastai2 09_vision.augment notebook, the structure to implement it should be something like:
- Define class:
class _rgb_transform(): def __init__(self, channel:int=None, thresh:float=0.3): store_attr(self, 'channel,thresh') def before_call(self, x): [...] def __call__(self, x): return [...]
- Add it to
@delegates(_rgb_transform.__init__) @patch def rgb_transform(x: TensorImage, **kwargs): func = _rgb_transform(**kwargs) func.before_call(x) return x.lighting(func)
def rgb_randomize(x, channel:int=None, thresh:float=0.3): "Randomize one of the channels of the input image" return LightingTfm(_rgb_transform(channel,thresh))
Is my understanding correct? I am sure that this could be wrong in a lot of ways. Any advice that shed some light on it would be very welcome.