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?
当使用深度学习进行目标识别或者语义分割时,遍历数据集时需要保证同一batch的图像大小相同。但是,在做数据增广时,有些操作需要改变图像的大小,比如我降低图片的分辨率而下采样。
所以,怎么保证同一batch的图像大小相同?
目前能想到的办法就是 当图片小于指定大小时,则全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.