当前位置:网站首页>2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
2.3 【pytorch】数据预处理 torchvision.datasets.ImageFolder
2022-07-01 09:03:00 【Enzo 想砸电脑】
我们在做训练的时候,我们免不了要读入数据,而针对数据的不同存储方式,我们也有不同的读入方法,从而方便我们将训练数据与其标签一一对应上。
方式一:使用于从一个存放了所有类别数据的文件夹中读取数据。通过重写torch.utils.data.Dataset,构建数据读取方式(自己做处理将数据和标签一一对应上),最后通过迭代器 torch.utils.data.DataLoader 的调用,按照batch_size 分批次读取数据。 如下有两个例子:
- 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
- torch.utils.data.Dataset 和 torch.utils.data.DataLoader 基础使用
方式二:是我们这篇文章要介绍的 torchcvision.datasets.ImageFolder, 用于从已经归好类的文件夹中读取数据,举例如下
数据存储结构:
from torchvision import datasets, transforms
my_trans = transforms.Compose([transforms.RandomResizedCrop(224),
transforms.ToTensor()])
train_data = datasets.ImageFolder('./animals/train', transform=my_trans)
print(train_data.class_to_idx) # 查看分类名称(文件名) 对应的标签数值
print(train_data[0][0].size()) # 第一张图片的尺寸,就是我们 transforms.RandomResizedCrop 设定的裁剪尺寸
for i in range(len(train_data)):
print(train_data[i][1]) # 查看训练数据集中所有图片映射到的标签值
输出:
边栏推荐
- 嵌入式工程师面试题3-硬件
- Shell脚本-字符串
- Databinding source code analysis
- Shell script - positional parameters (command line parameters)
- Interrupt sharing variables with other functions and protection of critical resources
- 又到年中,固定资产管理该何去何从?
- 嵌入式工程师面试-常问问题集
- Mysql 优化
- Serialization, listening, custom annotation
- VSYNC+三重缓存机制+Choreographer
猜你喜欢

Which method is good for the management of fixed assets of small and medium-sized enterprises?
![[interview brush 101] linked list](/img/52/d159bc66c0dbc44c1282a96cf6b2fd.png)
[interview brush 101] linked list

Insert mathematical formula in MD document and mathematical formula in typora

MySQL optimization

【MFC开发(16)】树形控件Tree Control

Advanced C language pointer (Part 2)

I use flask to write the website "one"

Phishing identification app

Jetson Nano 安装TensorFlow GPU及问题解决

安装Oracle EE
随机推荐
Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
JCL and slf4j
The fixed assets management system enables enterprises to dynamically master assets
Set the type of the input tag to number, and remove the up and down arrows
Flink interview questions
小鸟识别APP
任务、线程、进程 区别
毕业季,我想对你说
Yolov3, 4, 5 and 6 Summary of target detection
Databinding source code analysis
Common interview questions for embedded engineers 2-mcu_ STM32
R语言观察日志(part24)--初始化设置
Daily office consumables management solution
Shell脚本-数组定义以及获取数组元素
AVL树的理解和实现
DataBinding源码分析
MySQL optimization
如何解决固定资产管理和盘点的难题?
Insert mathematical formula in MD document and mathematical formula in typora
ARM v7的体系结构A、R、M区别,分别应用在什么领域?