当前位置:网站首页>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]) # 查看训练数据集中所有图片映射到的标签值
输出:
边栏推荐
- Log4j 日志框架
- Embedded Engineer Interview Question 3 Hardware
- FAQ | FAQ for building applications for large screen devices
- Shell script - array definition and getting array elements
- ARM v7的体系结构A、R、M区别,分别应用在什么领域?
- Nacos - 服务发现
- LogBack
- Shell script case in and regular expressions
- Meituan machine test in 2022
- 中小企业固定资产管理办法哪种好?
猜你喜欢
Ranking list of domestic databases in February, 2022: oceanbase regained the "three consecutive increases", and gaussdb is expected to achieve the largest increase this month
Foundation: 2 The essence of image
Embedded Engineer Interview Question 3 Hardware
ARM v7的体系结构A、R、M区别,分别应用在什么领域?
Understanding and implementation of AVL tree
Principle and application of single chip microcomputer timer, serial communication and interrupt system
How to solve the problem of fixed assets management and inventory?
Pain points and solutions of equipment management in large factories
Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
NiO zero copy
随机推荐
Advanced level of C language pointer (Part 1)
Centos7 shell script one click installation of JDK, Mongo, Kafka, FTP, PostgreSQL, PostGIS, pgrouting
Embedded Engineer Interview frequently asked questions
Leetcode daily question brushing record --540 A single element in an ordered array
Set the type of the input tag to number, and remove the up and down arrows
Memory size end
大型工厂设备管理痛点和解决方案
Glitch free clock switching technology
Shell脚本-read命令:读取从键盘输入的数据
【ESP 保姆级教程】疯狂毕设篇 —— 案例:基于阿里云、小程序、Arduino的WS2812灯控系统
pcl_viewer命令
How to solve the problem of fixed assets management and inventory?
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
Log4j 日志框架
Performance improvement 2-3 times! The second generation Kunlun core server of Baidu AI Cloud was launched
Bimianhongfu queren()
Dynamic proxy
LogBack
TV size and viewing distance
Input标签的type设置为number,去掉上下箭头