How to ensure the picture size in the function __getitem__ of the dataset

When we use deeplearning to do object detction or semantic segmentation,we need the same size of picture in one batch when we traverse the data.However,we need to do data augmentation,and some transform may need to change the size of the picture ,for example ,I want to reduce the resolution of the picture,so I downsample the picture.
So How to ensure the picture size in the function getitem of the dataset?
目前能想到的办法就是 当图片小于指定大小时,则全0填充。当图片大于指定大小时,则裁剪或者resize

One way is to resize the image if the image size is different than what you expect. So you can check the size of the image that you read from file, and then apply resize that to your desired image size. However, depending on your application, it might be important to keep the aspect ratio. So, you can find proper ways.

Another way, is to use the transformations in torchvision.transforms. For example, if you apply torchvision.transforms.CenterCrop(size), then all your images will be cropped from the center of the image and having the same size.