当前位置:网站首页>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官方文档
边栏推荐
- 如何看待智慧城市建设中的改变和机遇?
- Common interview questions ①
- Common UNIX Operation and maintenance commands of shell
- [ue4] event distribution mechanism of reflective event distributor and active call event mechanism
- 神经网络-最大池化的使用
- Pytorch(二) —— 激活函数、损失函数及其梯度
- Pytest automated testing - compare robotframework framework
- Some small knowledge points
- How to do the performance pressure test of "Health Code"
- Codeworks round 449 (Div. 1) C. Kodori tree template
猜你喜欢

【LeetCode】100. Same tree

2022 hoisting machinery command registration examination and hoisting machinery command examination registration

2022年聚合工艺考试题及模拟考试

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

LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target

使用WinMTR软件简单分析跟踪检测网络路由情况

Tip of edge browser: enter+ctrl can automatically convert the address bar into a web address

CF1638E. Colorful operations Kodori tree + differential tree array

TASK04|數理統計

LM小型可编程控制器软件(基于CoDeSys)笔记二十:plc通过驱动器控制步进电机
随机推荐
Shell之Unix运维常用命令
Task04 | statistiques mathématiques
数据加载及预处理
About the transmission pipeline of stage in spark
神经网络-最大池化的使用
网站服务器:好用的网站服务器怎么选这五方面要关注
MySQL advanced -- you will have a new understanding of MySQL
(12) Somersault cloud case (navigation bar highlights follow)
RuntimeError: mean(): input dtype should be either floating point or complex dtypes.Got Long instead
Execution failed for task ‘:app:processDebugResources‘. > A failure occurred while executing com. and
VR线上展览所具备应用及特色
什么是uid?什么是Auth?什么是验证器?
All in all, the low code still needs to solve these four problems
C language games (I) -- guessing games
Openresty rewrites the location of 302
Day 52 - tree problem
CUDA development and debugging tool
Section 27 remote access virtual private network workflow and experimental demonstration
总结全了,低代码还需要解决这4点问题
CF1638E. Colorful operations Kodori tree + differential tree array