Reputation: 4071
I would like to use 2 different transformation one for training and the second one for validation and test. I mean to add some augmentation during the train process and validate/test without this augmentation.
What's the pytorch way to do so?
I mean to run 2 different transforms with torch.utils.data.Subset or torch.utils.data.Dataloader functions rather than creating 2 datasets.
Upvotes: 0
Views: 976
Reputation: 41
An example of separate train and test sets can be obtained through the train
parameter when instantiating a data-set. Within this same constructor you can specify transforms, as shown here by the PyTorch team.
import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision.transforms import ToTensor
import matplotlib.pyplot as plt
training_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=ToTensor()
)
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor()
)
If you say wished to apply a random horizontal flip to input data, with probability 0.5, you could change transform=ToTensor()
to transform=RandomHorizontalFlip(p=0.5)
and import from torchvision.transforms import RandomHorizontalFlip
, which would look like this.
import torch
from torch.utils.data import Dataset
from torchvision import datasets
from torchvision.transforms import ToTensor, RandomHorizontalFlip
import matplotlib.pyplot as plt
training_data = datasets.FashionMNIST(
root="data",
train=True,
download=True,
transform=RandomHorizontalFlip(0.5)
)
test_data = datasets.FashionMNIST(
root="data",
train=False,
download=True,
transform=ToTensor()
)
Upvotes: 2