filter class/subfolder with pytorch ImageFolder(使用 pytorch ImageFolder 过滤类/子文件夹)
问题描述
这是我的文件夹结构
image-folders/
├── class_0/
| ├── 001.jpg
| ├── 002.jpg
└── class_1/
| ├── 001.jpg
| └── 002.jpg
└── class_2/
├── 001.jpg
└── 002.jpg
通过使用来自 torchvision 的 ImageFolder,我可以使用以下语法创建数据集:dataset = ImageFolder("image-folders",...)
By using ImageFolder from torchvision, I can create dataset with this syntax :
dataset = ImageFolder("image-folders",...)
但这将读取整个子文件夹并创建 3 个目标类.我不想包含 class_2 文件夹,我希望我的数据集只包含 class_0 和 class_1,除了删除/移动 class_2 文件夹之外还有什么方法可以实现吗?
But this will read the entire subfolder and create 3 target classes. I don't want to include the class_2 folder, I want my dataset to only contains class_0 and class_1 only, is there any way to achieve this besides delete/move the class_2 folder?
推荐答案
您可以使用 ImageFolder 数据集的子集"rel="noreferrer">torch.utils.data.Subset:
You can do this by using torch.utils.data.Subset of the original full ImageFolder dataset:
from torchvision.datasets import ImageFolder
from torch.utils.data import Subset
# construct the full dataset
dataset = ImageFolder("image-folders",...)
# select the indices of all other folders
idx = [i for i in range(len(dataset)) if dataset.imgs[i][1] != dataset.class_to_idx['class_s']]
# build the appropriate subset
subset = Subset(dataset, idx)
这篇关于使用 pytorch ImageFolder 过滤类/子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 pytorch ImageFolder 过滤类/子文件夹
基础教程推荐
- 修改列表中的数据帧不起作用 2022-01-01
- 包装空间模型 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
