Using a special function as a transform

Newbie here, my apologies if the question was asked in other words before.

Here is my transfroms function applied on the whole image of a person

transform = transforms.Compose([
    transforms.Resize((224, 224)),
    transforms.RandomHorizontalFlip(),
    transforms.RandomRotation(20),
    transforms.ToTensor(),
    transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
    ])

I want to apply the CNN only on the face of the person. I have a function that takes image and returns a cropped subimage of the face. How to use this function in my transforms composer ?

Hi, Compose does nothing but running the list of transforms over the image sequentially.

You just have to define a class and code your face cropper inside

class custom(object):
    def __call__(self,img):
        return  apply your cropper

then pass an instance of that class to the compose
transforms.Compose([…,custom()])
and that’s all.
It’s important to highlight that call method can only has img as input. If you need to define other parameters you can initilize the class

1 Like