当前位置:网站首页>Dataloader的使用
Dataloader的使用
2022-07-01 04:35:00 【booze-J】
文章目录
Dataloder官方文档
Dataloader的使用示例代码如下:
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
import torchvision
# 准备的测试数据集
test_data = torchvision.datasets.CIFAR10(root="./CIFAR10",train=False,transform=torchvision.transforms.ToTensor(),download=True)
# 加载数据集准备工作
test_loader = DataLoader(dataset=test_data,batch_size=4,shuffle=True,num_workers=0,drop_last=False)
'''
batch_size:每次取多少个数据进行处理
shuffle:第二次循环取数据时,顺序是否打乱,True为打乱
num_workers:选用进程的数量
drop_last:是否将尾部多余的数据除去,True为除去
'''
# 测试数据集中的第一张图片及分类结果
img,target = test_data[0]
print(img.shape)
print(target)
writer = SummaryWriter("logs")
step = 0
# 前提batch_size=4
for data in test_loader:
imgs,targets = data
# 例:torch.Size([4, 3, 32, 32]) 4代表4张图片 3代表图片的通道数 大小为32*32的图片
print(imgs.shape)
# 例:tensor([3, 1, 9, 0]) 四张图片的类别
print(targets)
# 使用tensorboard进行可视化
writer.add_images("test_data",imgs,step) # 注意这里使用的是add_images而不是add_image哦,因为imgs是4个tensor数据类型图片的集合
step+=1
writer.close()
上述代码运行结果在tensorboard可视化:

每步都是4张,这里也可以看出dataloader中batch_size参数的作用了吧!

dataloader中batch_size的作用就相当于将dataset打包,每次取整个被打包的部分进行后续操作,上图可以方便理解,将batch_size设置的值的图片数量(img)和分类结果(target)分别打包成imgs,targets。
上述代码中要注意的地方:
- writer.add_images(“test_data”,imgs,step) # 注意这里使用的是add_images而不是add_image哦,因为imgs是4个tensor数据类型图片的集合
- 学会看官方文档:Dataloder官方文档
边栏推荐
- PgSQL failed to start after installation
- 2022 question bank and answers for safety production management personnel of hazardous chemical production units
- 做网站数据采集,怎么选择合适的服务器呢?
- Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
- All in all, the low code still needs to solve these four problems
- JS image path conversion Base64 format
- 2022 polymerization process test questions and simulation test
- Daily question - line 10
- OSPF notes [multiple access, two multicast addresses with OSPF]
- Obtain detailed ideas for ABCDEF questions of 2022 American Games
猜你喜欢

2022 question bank and answers for safety production management personnel of hazardous chemical production units

The design points of voice dialogue system and the importance of multi round dialogue

Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom

Maixll-Dock 使用方法

MySQL winter vacation self-study 2022 12 (5)

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机

Strategic suggestions and future development trend of global and Chinese vibration isolator market investment report 2022 Edition

Tip of edge browser: enter+ctrl can automatically convert the address bar into a web address
![[leetcode skimming] February summary (updating)](/img/62/0d0d9f11434e49d33754a2e4f2ea65.jpg)
[leetcode skimming] February summary (updating)

Knowledge supplement: redis' basic data types and corresponding commands
随机推荐
做网站数据采集,怎么选择合适的服务器呢?
Question bank and answers for chemical automation control instrument operation certificate examination in 2022
神经网络-最大池化的使用
MySQL advanced -- you will have a new understanding of MySQL
Seven crimes of counting software R & D Efficiency
2022-02-15 (399. Division evaluation)
尺取法:有效三角形的个数
Basic usage, principle and details of session
【硬十宝典目录】——转载自“硬件十万个为什么”(持续更新中~~)
Grey correlation cases and codes
2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
2022年煤气考试题库及在线模拟考试
TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
Obtain detailed ideas for ABCDEF questions of 2022 American Games
2022 hoisting machinery command registration examination and hoisting machinery command examination registration
Pytorch(二) —— 激活函数、损失函数及其梯度
Some small knowledge points
OdeInt与GPU
Maixll-Dock 使用方法
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver