PyTorch transforms.Compose: Chaining Image Transformations
transforms.Compose is a PyTorch class that allows you to chain multiple image transformations together. It takes in a list of image transformations and applies them sequentially to the input image in the order they are provided. This is useful for performing multiple image transformations on a single image before passing it to a neural network for training or inference.
For example, you can use transforms.Compose to apply a series of transformations to an image as follows:
import torchvision.transforms as transforms
transform = transforms.Compose([
transforms.Resize((256, 256)),
transforms.RandomCrop(224),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
image_transformed = transform(image)
In this example, transforms.Compose is used to apply four transformations to an input image. The first transformation resizes the image to a fixed size of (256, 256), the second transformation randomly crops the image to a size of (224, 224), the third transformation converts the image to a tensor, and the fourth transformation normalizes the tensor by subtracting the mean and dividing by the standard deviation. The resulting image_transformed is a tensor that can be passed to a PyTorch model for training or inference.
原文地址: https://www.cveoy.top/t/topic/njG3 著作权归作者所有。请勿转载和采集!