当前位置:网站首页>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官方文档
边栏推荐
- js 图片路径转换base64格式
- How to do the performance pressure test of "Health Code"
- 2022 t elevator repair new version test questions and t elevator repair simulation test question bank
- Learn Chapter 20 of vue3 (keep alive cache component)
- 软件研发的十大浪费:研发效能的另一面
- 数据加载及预处理
- Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
- 2022 tea master (intermediate) examination question bank and tea master (intermediate) examination questions and analysis
- [godot] unity's animator is different from Godot's animplayer
- RDF query language SPARQL
猜你喜欢

Pytorch(一) —— 基本语法

Task04 | statistiques mathématiques

Dede collection plug-in does not need to write rules

Pytorch(三) —— 函数优化

2022 polymerization process test questions and simulation test
![[ue4] event distribution mechanism of reflective event distributor and active call event mechanism](/img/44/6a26ad24d56ddd5156f3a31fa7e0b9.jpg)
[ue4] event distribution mechanism of reflective event distributor and active call event mechanism

How to do the performance pressure test of "Health Code"

Simple implementation of slf4j

Why is Internet thinking not suitable for AI products?

Task04 mathematical statistics
随机推荐
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
C language games (I) -- guessing games
js 图片路径转换base64格式
Pytorch(二) —— 激活函数、损失函数及其梯度
Sorting out 49 reports of knowledge map industry conference | AI sees the future with wisdom
Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor
Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
2022年煤气考试题库及在线模拟考试
2022-02-15 (399. Division evaluation)
神经网络-使用Sequential搭建神经网络
Task04 | statistiques mathématiques
Offline installation of Wireshark 2.6.10
Introduction to JVM stack and heap
Tencent has five years of testing experience. It came to the interview to ask for 30K, and saw the so-called software testing ceiling
slf4j 简单实现
Research on medical knowledge atlas question answering system (I)
RDF query language SPARQL
Threejs opening
VIM简易使用教程
Shell之分析服务器日志命令集锦