How to define the custom grayscale Transform in V1?

I want to transform color image to grayscale , the get_transforms function has no gray param, so I want define a custom transform then pass in get_transforms( xtra_tfms=[my_custom_transform])
how to define my custom transform?

You’ll have to implement a custom function in your code and then create a transform with that, and pass it in xtra_tfms to get_transforms.

Here’s an example of a pixel transform:

def _cutout(x, n_holes:uniform_int=1, length:uniform_int=40):
    "Cut out `n_holes` number of square holes of size `length` in image at random locations."
    h,w = x.shape[1:]
    for n in range(n_holes):
        h_y = np.random.randint(0, h)
        h_x = np.random.randint(0, w)
        y1 = int(np.clip(h_y - length / 2, 0, h))
        y2 = int(np.clip(h_y + length / 2, 0, h))
        x1 = int(np.clip(h_x - length / 2, 0, w))
        x2 = int(np.clip(h_x + length / 2, 0, w))
        x[:, y1:y2, x1:x2] = 0
    return x

 cutout = TfmPixel(_cutout, order=20)

So you’d define grayscale like that and then pass it to get transforms: tfms = get_transforms(xtra_tfms=[grayscale()]).

2 Likes

thanks~

1 Like

Please tell me this isn’t really necessary to do a simple grayscale transform. xtra_tfms can’t just use the pytorch version somehow?