当前位置:网站首页>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官方文档
边栏推荐
猜你喜欢

Maixll-Dock 快速上手

2022危险化学品生产单位安全生产管理人员题库及答案

Leetcode learning - day 36

Task04 | statistiques mathématiques

Internet winter, how to spend three months to make a comeback

Applications and features of VR online exhibition

【硬十宝典】——1.【基础知识】电源的分类

【LeetCode】100. Same tree

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
![[pat (basic level) practice] - [simple simulation] 1064 friends](/img/37/0ef0f8aae15ae574be1d76c97497c9.jpg)
[pat (basic level) practice] - [simple simulation] 1064 friends
随机推荐
[recommended algorithm] C interview question of a small factory
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
This sideline workload is small, 10-15k, free unlimited massage
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
[pat (basic level) practice] - [simple simulation] 1064 friends
C language games (I) -- guessing games
[2020 overview] overview of link prediction based on knowledge map embedding
Software testing needs more and more talents. Why do you still not want to take this path?
Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and
How to view the changes and opportunities in the construction of smart cities?
TCP server communication flow
离线安装wireshark2.6.10
CF1638E colorful operations
使用WinMTR软件简单分析跟踪检测网络路由情况
2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
Shell之一键自动部署Redis任意版本
Maixll-Dock 快速上手
Codeforces Round #721 (Div. 2)B1. Palindrome Game (easy version)B2. Palindrome game (hard version)
Grey correlation cases and codes