Is there any nice example on how to use torchvision.transforms.Lambda?

I have a function which returns a list of patches from input_image. Since, this is a preprocessing step, I would like to integrate it with other transformations. Below is the rough function analogy that I would like to call using transforms.lambda.

def patch(img, patch_size):
        patches = []
        w, h = img.size
        tw = patch_size 
        th = patch_size
        
        for w1 in range(0, w, int(w/3)): 
            for h1 in range(0, h, int(h/3)): 
                w2 = w1 + int(np.random.randint(1, int(w/3) - tw)) 
                h2 = h1 + int(np.random.randint(1, int(h/3) - th)) 
                patches.append(img.crop((w2, h2, w2 + tw, h2 + tw)))

        return patches
2 Likes

Here is one example on using a Lambda transform https://github.com/szagoruyko/attention-transfer/blob/master/imagenet.py#L80