当前位置:网站首页>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官方文档
边栏推荐
- Question bank and online simulation examination for special operation certificate of G1 industrial boiler stoker in 2022
- OSPF notes [dr and bdr]
- Odeint and GPU
- Internet winter, how to spend three months to make a comeback
- Registration of P cylinder filling examination in 2022 and analysis of P cylinder filling
- [difficult] sqlserver2008r2, can you recover only some files when recovering the database?
- What are permissions? What are roles? What are users?
- PgSQL failed to start after installation
- 什么是uid?什么是Auth?什么是验证器?
- Collect the annual summary of laws, regulations, policies and plans related to trusted computing of large market points (national, ministerial, provincial and municipal)
猜你喜欢
![[godot] unity's animator is different from Godot's animplayer](/img/51/48f40a7b6736d7f78040eabbbd3395.jpg)
[godot] unity's animator is different from Godot's animplayer

软件研发的十大浪费:研发效能的另一面

2022 G2 power station boiler stoker examination question bank and G2 power station boiler stoker simulation examination question bank

TCP server communication flow

Measurement of quadrature axis and direct axis inductance of three-phase permanent magnet synchronous motor

2022.2.7-2.13 AI industry weekly (issue 84): family responsibilities

尺取法:有效三角形的个数

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

Knowledge supplement: redis' basic data types and corresponding commands

神经网络-非线性激活
随机推荐
细数软件研发效能的七宗罪
Why is Internet thinking not suitable for AI products?
I also gave you the MySQL interview questions of Boda factory. If you need to come in and take your own
如何看待智慧城市建设中的改变和机遇?
Announcement on the list of Guangdong famous high-tech products to be selected in 2021
神经网络-最大池化的使用
Registration for R2 mobile pressure vessel filling test in 2022 and R2 mobile pressure vessel filling free test questions
Knowledge supplement: basic usage of redis based on docker
Shell之Unix运维常用命令
VIM easy to use tutorial
selenium打开chrome浏览器时弹出设置页面:Mircrosoft Defender 防病毒要重置您的设置
1. Mobile terminal touch screen event
Some small knowledge points
OSPF notes [dr and bdr]
Dual contractual learning: text classification via label aware data augmentation reading notes
The design points of voice dialogue system and the importance of multi round dialogue
软件研发的十大浪费:研发效能的另一面
How to view the changes and opportunities in the construction of smart cities?
Rule method: number of effective triangles
Task04 | statistiques mathématiques