I got the same situation while inference mode. I know that we need to do the standardization in the Dataset before feeds them into the model.
So in training code, I have:
...
# dataloader.py
self.transform = transforms.Compose([transforms.RandomResizedCrop(224),
                                     transforms.RandomHorizontalFlip(),
                                     transforms.ToTensor(),
                                     transforms.Normalize(mean=mean, std=std)])
# train.py
...
logits = self.model(imgs)   #Resnet101

It totally fine but you know, we trained our model with 224x224px and maybe do some data augmentation to force CNN to learn the recognize the image features/pattern in different regions of the images. However, when inference, the size of the image can vary since we using Global Average Pooling, like 224x400px or 400x224px, it’s a bad idea if we apply CenterCroping or Resize to 224x224px in here.
So in inference code, I have:
...
# dataloader.py
self.transform = transforms.Compose([transforms.Resize(224), # resize the smallest edge to 224px.
                                     transforms.ToTensor(),
                                     transforms.Normalize(mean=mean, std=std)])
# train.py
...
logits = self.model(imgs)   #Resnet101

The memory leak happens. Do you guys have any idea?