PyTorch Data Preprocessing Guide
In Torch, the torchvision.transforms module can be used for data preprocessing. Common data preprocessing operations include:
- resize transformation
 - CenterCrop function that makes the transformation.
 - Randomly flips the input image horizontally.
 - randomly rotates the image
 - converts an image to grayscale
 - normalize data transformations
 - converts the data into a tensor
 
Here is an example demonstrating how to use transforms for data preprocessing.
import torchvision.transforms as transforms
# 定义预处理操作
data_transform = transforms.Compose([
    transforms.Resize(256),  # 将图片缩放为 256x256
    transforms.CenterCrop(224),  # 将图片裁剪为 224x224
    transforms.RandomHorizontalFlip(),  # 随机水平翻转图片
    transforms.ToTensor(),  # 将图片转换为张量
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])  # 归一化图片像素值
])
# 加载数据集并应用预处理操作
train_dataset = torchvision.datasets.ImageFolder(root='path/to/train/data', transform=data_transform)