问题描述
如何在 PyTorch 中为不同的 Subset
使用不同的数据增强(转换)?
How to use different data augmentation (transforms) for different Subset
s in PyTorch?
例如:
train, test = torch.utils.data.random_split(dataset, [80000, 2000])
train
和 test
将具有与 dataset
相同的转换.如何对这些子集使用自定义转换?
train
and test
will have the same transforms as dataset
. How to use custom transforms for these subsets?
推荐答案
我目前的解决方案不是很优雅,但有效:
My current solution is not very elegant, but works:
from copy import copy
train_dataset, test_dataset = random_split(full_dataset, [train_size, test_size])
train_dataset.dataset = copy(full_dataset)
test_dataset.dataset.transform = transforms.Compose([
transforms.Resize(img_resolution),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
train_dataset.dataset.transform = transforms.Compose([
transforms.RandomResizedCrop(img_resolution[0]),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406],
std=[0.229, 0.224, 0.225])
])
基本上,我为其中一个拆分定义了一个新数据集(它是原始数据集的副本),然后我为每个拆分定义了一个自定义变换.
Basically, I'm defining a new dataset (which is a copy of the original dataset) for one of the splits, and then I define a custom transform for each split.
注意:train_dataset.dataset.transform
有效,因为我使用的是 ImageFolder
数据集,它使用 .tranform
属性来执行变换.
Note: train_dataset.dataset.transform
works since I'm using an ImageFolder
dataset, which uses the .tranform
attribute to perform the transforms.
如果有人知道更好的解决方案,请与我们分享!
If anybody knows a better solution, please share with us!
这篇关于如何在 PyTorch 中为子集使用不同的数据增强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!