Search code examples
pytorchimage-augmentation

understanding transforms: resize and centercrop with same size


I am trying to understand this particular set of compose transforms:

transform= transforms.Compose([transforms.Resize((224,224) interpolation=torchvision.transforms.InterpolationMode.BICUBIC),\
transforms.CenterCrop(224),transforms.ToTensor(),\
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])

Does it make sense (and is it legit) to do centercrop after transforms - with the same size parameter? I would have thought resize itself is giving the centercrop but I see in the repos that centercrop is composed after resize - both with exactly the same sizes. I wonder what is the use of doing such a thing. For the sake of completeness, I would like to add that my input image sizes vary (ie they are all not of the same dims).

thanks!


Solution

  • I would have thought resize itself is giving the center crop.

    Function T.Resize won't center crop your image, the center will stay the same since you are only resizing the original image, i.e. proportions are kept and the original center remains at the center. Applying a crop of the same shape as the image - since it's just after the resize - with T.CenterCrop doesn't make any difference since you are cropping nothing out of the image.

    If you change the sizes of your T.CenterCrop, then this and the order you apply both transforms will matter greatly.