赞
踩
打开pytorch官网,点击pytorch官网搜索dataloader
torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=None, sampler=None, batch_sampler=None, num_workers=0, collate_fn=None, pin_memory=False, drop_last=False, timeout=0, worker_init_fn=None, multiprocessing_context=None, generator=None, *, prefetch_factor=None, persistent_workers=False, pin_memory_device='')
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_data中取4个数据并打包 test_loader = DataLoader(dataset=test_data, batch_size=64, shuffle=True, num_workers=0, drop_last=False) # 保存测试数据集中第一张图片及target img, target = test_data[0] print(img.shape) #图片为3通道,大小为32×32 print(target) writer = SummaryWriter("logs") step = 0 for data in test_loader: imgs, targets = data writer.add_images("test_data", imgs, step) step = step + 1 writer.close()
打开tensorboard,可以看到dataloader把数据集64张图片为一组打包
对于shuffle参数,设置为true后打包的数据每次都会打乱
使用for epoch in range(2):
进行两次循环
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_data中取4个数据并打包 test_loader = DataLoader(dataset=test_data, batch_size=64, shuffle=True, num_workers=0, drop_last=False) # 保存测试数据集中第一张图片及target img, target = test_data[0] print(img.shape) #图片为3通道,大小为32×32 print(target) writer = SummaryWriter("logs") # 两次循环 for epoch in range(2): step = 0 for data in test_loader: imgs, targets = data writer.add_images("Epoch: {}".format(epoch), imgs, step) step = step + 1 writer.close()
最后结果显示两次打包数据不一样
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。