应用 Torchvision 转换到一个 NumPy 数组

huangapple go评论51阅读模式
英文:

Apply Torchvision Transforms to a numpy array

问题

我编写了以下代码:

transform = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomResizedCrop(224),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

train_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_train).float(), torch.from_numpy(y_train).float())
val_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_val).float(), torch.from_numpy(y_val).float())

# 定义数据加载器
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=4)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=32, shuffle=False, num_workers=4)

我想要将这个变换组应用到我的数据集(X_train和X_val),它们都是NumPy数组。如何应用变换来增强我的数据集并进行归一化?我应该在模型训练之前还是在模型训练期间应用它们?

英文:

I wrote the following code:

transform = transforms.Compose([
    transforms.ToPILImage(),
    transforms.RandomResizedCrop(224),
    transforms.RandomHorizontalFlip(),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])


train_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_train).float(), torch.from_numpy(y_train).float())
val_dataset = torch.utils.data.TensorDataset(torch.from_numpy(X_val).float(), torch.from_numpy(y_val).float())


# Define the dataloaders
train_loader = torch.utils.data.DataLoader(train_dataset, batch_size=32, shuffle=True, num_workers=4)
val_loader = torch.utils.data.DataLoader(val_dataset, batch_size=32, shuffle=False, num_workers=4)

I would like to apply the transform compose to my dataset (X_train and X_val) which are both numpy array. How can I apply transform to augment my dataset and normalize it.
Should I apply it before the model training or during model training?

答案1

得分: 1

你可以简单地将转换器添加到你的训练流程中:

train_transformed = transform(torch.from_numpy(X_train).float())
val_transformed = transform(torch.from_numpy(X_val).float())

train_dataset = torch.utils.data.TensorDataset(train_transformed, torch.from_numpy(y_train).float())
val_dataset = torch.utils.data.TensorDataset(val_transformed, torch.from_numpy(y_val).float())

这样转换就会在数据加载过程中应用,即在训练之前。

英文:

You can simply add the transformers to your training pipeline:

train_transformed = transform(torch.from_numpy(X_train).float())
val_transformed = transform(torch.from_numpy(X_val).float())

train_dataset = torch.utils.data.TensorDataset(train_transformed, torch.from_numpy(y_train).float())
val_dataset = torch.utils.data.TensorDataset(val_transformed, torch.from_numpy(y_val).float())

This way the transform is applied during data loading which is before training.

huangapple
  • 本文由 发表于 2023年2月18日 04:31:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/75489012.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定