如何在 PyTorch 中为子集使用不同的数据增强 [英] How to use different data augmentation for Subsets in PyTorch

查看:37
本文介绍了如何在 PyTorch 中为子集使用不同的数据增强的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 PyTorch 中为不同的 Subset 使用不同的数据增强(转换)?

How to use different data augmentation (transforms) for different Subsets in PyTorch?

例如:

train, test = torch.utils.data.random_split(dataset, [80000, 2000])

traintest 将具有与 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 中为子集使用不同的数据增强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆