Imagewoof: correct use of `transforms.Compose()` for testing?

I want to train a network on the Imagewoof dataset. I would like to know if I correctly use transforms.Compose() for testing my network.

Currently I use the following approach:

avg = (0.4914, 0.4822, 0.4465)
std = (0.2023, 0.1994, 0.2010)
stats = (avg, std)

train_transform = transforms.Compose([
    transforms.RandomResizedCrop(size=(128, 128)),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize(*stats, inplace=True)])

test_transform = transforms.Compose([
    transforms.RandomResizedCrop(size=(128, 128)),
    transforms.ToTensor(),
    transforms.Normalize(*stats)])

However, I think it is not right to use transforms.RandomResizedCrop(size=(128, 128)) for testing my network, because it randomly crops the image. If I want to compare my results to those from the official leaderboard, how do I have to configure test_transform?

I believe the standard approach is to use a Center Crop although different datasets may use different “rules” sometimes. Note that you may want to resize the images first (e.g., examples/main.py at 01539f9eada34aef67ae7b3d674f30634c35f468 · pytorch/examples · GitHub) before taking a center crop due to the variation in resolution of images.

1 Like

Using Center Crop seems like the most logical approach, thanks!

Do I need to resize the images, if I know, that their height and width is larger than 128 pixels?

For example, if a test image is 10000x10000 pixels, taking the 128x128 center crop before resizing to say (160x160 or 192x192) might result in a huge mismatch between the size of objects your model is used to seeing at training time and what it is now being tested on. Resizing to a known size before taking the center crop is a way to help alleviate this problem.

Overall, this issue is somewhat tricky to deal with even with resizing (see Touvron et. al for more details).

1 Like