How to preprocess data in Torch?

In Torch, the torchvision.transforms module can be used for data preprocessing. Common data preprocessing operations include:

  1. resize transformation
  2. CenterCrop function that makes the transformation.
  3. Randomly flips the input image horizontally.
  4. randomly rotates the image
  5. converts an image to grayscale
  6. normalize data transformations
  7. 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)
Leave a Reply 0

Your email address will not be published. Required fields are marked *


广告
Closing in 10 seconds
bannerAds