Conditional transforms for image resize

I only want to resize images that are smaller than my desired input size. Is there way to reshape images that are smaller than a certain size and ignore all others?

Using the Dataset class, you can lazily load your images in the __getitem__ method.
Here is a small example:

class TrainDataset(Dataset):
    def __init__(self, image_paths, targets):
        self.image_paths = image_paths
        self.targets = targets

    def __getitem__(self, index):
        image = Image.open(self.image_paths[index])
        y = self.target[index]

        # Resize your image here
        if image.size...:
            ....
        .... # Do other stuff here

        x = torch.from_numpy(image)
    
        return x, y

    def __len__(self):
        return len(self.image_paths)

You can wrap your Dataset to a DataLoader for efficient loading, preprocessing, shuffling etc.

Hope this helps!

3 Likes