视频
在pytorch里面输入dataloader,可以看到详细介绍文档
尝试写一下代码
来看看dataloader中的输出~
import torchvision
from torch.utils.data import DataLoader
test_data = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor())
test_loader = DataLoader(dataset=test_data, batch_size=4, shuffle=True, num_workers=0, drop_last=False)
# 测试数据集中第一张图片及target(标签)
img, target = test_data[0]
print(img.shape)
print(target)
# 来看看dataloader中的输出
for data in test_loader:
imgs, targets = data
print(imgs.shape)
print(targets)
输出结果为(截取部分)
torch.Size([3, 32, 32])
3
torch.Size([4, 3, 32, 32])
tensor([8, 0, 4, 3])
可以发现,dataloader中输出的结果torch.Size([4, 3, 32, 32])
,即打包之后的batch_size=4,有三个通道,一个图片大小为32×32;同样还把target进行了打包,即tensor([8, 0, 4, 3])
,表示图片的target分别为2,3,6,8。
说到这里,我们自然想看看打包好的数据长啥样,因为数据已经被处理成tensor形式了,我们可以借助tensorboard~
下面是drop_last=True
的情况~
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
# 准备的测试数据集
test_data = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor())
test_loader = DataLoader(dataset=test_data, batch_size=64, shuffle=True, num_workers=0, drop_last=True)
# 测试数据集中第一张图片及target(标签)
img, target = test_data[0]
print(img.shape)
print(target)
writer = SummaryWriter("dataloader1")
# 来看看dataloader中的输出
step = 0
for data in test_loader:
imgs, targets = data
# print(imgs.shape)
# print(targets)
writer.add_images("test_data_drop_last", imgs, step)
step = step + 1
writer.close()
同样用terminal打开Tensorboard
tensorboard --logdir=dataloader1 #和SummaryWriter一致
如果我们想来两次迭代,如下
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
# 准备的测试数据集
test_data = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor())
test_loader = DataLoader(dataset=test_data, batch_size=64, shuffle=False, num_workers=0, drop_last=True)
# 测试数据集中第一张图片及target(标签)
img, target = test_data[0]
print(img.shape)
print(target)
writer = SummaryWriter("dataloader1")
# 来看看dataloader中的输出
for epoch in range(2):
step = 0
for data in test_loader:
imgs, targets = data
# print(imgs.shape)
# print(targets)
writer.add_images("Epoch:{}".format(epoch), imgs, step)
step = step + 1
writer.close()