How to use different data augmentation for Subsets in PyTorch(如何在 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])
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 中为子集使用不同的数据增强的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 PyTorch 中为子集使用不同的数据增强
基础教程推荐
- 与常规 dict 相比,Python manager.dict() 非常慢 2022-01-01
- 在 Celery 工作人员中捕获 Heroku SIGTERM 以优雅地关 2022-01-01
- 用 Python 编写 Fortran 无格式文件 2022-01-01
- 由Python将MP3转换为MIDI(类型错误:无法加载插件:mtg-Melodia:Melodia) 2022-01-01
- numpy float:比算术运算中内置的慢 10 倍? 2022-01-01
- pyserial - 可以从线程 a 写入串行端口,是否阻塞从线程 b 读取? 2022-01-01
- Discord.py 缺少必需的参数 2022-01-01
- 尝试制作WhatsApp机器人 2022-01-01
- 将 x 轴刻度更改为自定义字符串 2022-01-01
- 使用生成器和迭代器时 Python 多循环失败 2022-01-01
