当前位置:网站首页>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官方文档
边栏推荐
- 什么是权限?什么是角色?什么是用户?
- Obtain detailed ideas for ABCDEF questions of 2022 American Games
- 【LeetCode】100. Same tree
- Daily algorithm & interview questions, 28 days of special training in large factories - the 13th day (array)
- 2022 t elevator repair new version test questions and t elevator repair simulation test question bank
- JVM栈和堆简介
- TCP/IP 详解(第 2 版) 笔记 / 3 链路层 / 3.4 桥接器与交换机 / 3.4.2 多属性注册协议(Multiple Registration Protocol (MRP))
- 1. Mobile terminal touch screen event
- Software testing needs more and more talents. Why do you still not want to take this path?
- Codeworks round 449 (Div. 1) C. Kodori tree template
猜你喜欢

Pytest automated testing - compare robotframework framework

About the transmission pipeline of stage in spark

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

Odeint and GPU

OdeInt與GPU

CF1638E. Colorful operations Kodori tree + differential tree array

One click shell to automatically deploy any version of redis

2022年煤气考试题库及在线模拟考试

Use winmtr software to simply analyze, track and detect network routing

Cmake selecting compilers and setting compiler options
随机推荐
JS image path conversion Base64 format
Ten wastes of software research and development: the other side of research and development efficiency
使用WinMTR软件简单分析跟踪检测网络路由情况
Selenium opens the Chrome browser and the settings page pops up: Microsoft defender antivirus to reset your settings
Research on medical knowledge atlas question answering system (I)
Some small knowledge points
什么是uid?什么是Auth?什么是验证器?
LM small programmable controller software (based on CoDeSys) note 19: errors do not match the profile of the target
Knowledge supplement: basic usage of redis based on docker
How to do the performance pressure test of "Health Code"
Web server: how to choose a good web server these five aspects should be paid attention to
Simple implementation of slf4j
The index is invalid
(12) Somersault cloud case (navigation bar highlights follow)
Software testing needs more and more talents. Why do you still not want to take this path?
Common interview questions ①
2022-02-15 (399. Division evaluation)
LM small programmable controller software (based on CoDeSys) note 20: PLC controls stepping motor through driver
OSPF notes [dr and bdr]
Odeint and GPU