在使用PyTorch将Traininng数据集拆分为CIFAR10的训练和验证集后,如何增加数据?

问题描述 投票:2回答:1

[在PyTorch中对CIFAR10进行分类时,通常有50,000个训练样本和10,000个测试样本。但是,如果需要创建验证集,可以通过将训练集分为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.RandomHorizo​​ntalFlip())。但是,如果在分割训练集和验证集之前使用了这些扩充过程,则扩充后的数据也将包含在验证集中。有什么办法可以解决这个问题?

总之,我想手动拆分将数据集训练到训练和验证集中,以及我想要的在新的训练集中使用数据增强技术。

deep-learning conv-neural-network pytorch torch data-augmentation
1个回答
1
投票

您可以手动覆盖数据集的transforms

cifar_train, cifar_val = torch.utils.data.random_split(cifar_train_L,[train_size,val_size])
cifar_val.transforms = test_transform 
© www.soinside.com 2019 - 2024. All rights reserved.