在PyTorch中对CIFAR10进行分类时,通常有50000个训练样本和10000个测试样本。然而,如果我需要创建一个验证集,可以通过将训练集分成40000个训练样本和10000个验证样本来实现。我使用了以下代码:
train_transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])
test_transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])
cifar_train_L = CIFAR10('./data',download=True, train= True, transform = train_transform)
cifar_test = CIFAR10('./data',download=True, train = False, transform= test_transform)
train_size = int(0.8*len(cifar_training))
val_size = len(cifar_training) - train_size
cifar_train, cifar_val = torch.utils.data.random_split(cifar_train_L,[train_size,val_size])
train_dataloader = torch.utils.data.DataLoader(cifar_train, batch_size= BATCH_SIZE, shuffle= True, num_workers=2)
test_dataloader = torch.utils.data.DataLoader(cifar_test,batch_size= BATCH_SIZE, shuffle= True, num_workers= 2)
val_dataloader = torch.utils.data.DataLoader(cifar_val,batch_size= BATCH_SIZE, shuffle= True, num_workers= 2)
通常在 PyTorch 中增强数据时,可以在transforms.Compose函数中使用不同的增强过程(例如transforms.RandomHorizontalFlip())。然而,如果在将训练集和验证集分开之前使用这些增强过程,那么增强后的数据也会被包括在验证集中。有没有办法解决这个问题呢?
简而言之,我想要手动将训练集划分为训练集和验证集,并且想要将数据增强技术应用于新的训练集。