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 中为子集使用不同的数据增强
基础教程推荐
- 修改列表中的数据帧不起作用 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
