8 加载数据集DataLoader官方(len(dataset)/batch_size)

涉及两个类:dataset&DataLoader

image.png

dataset

DataLoader

https://www.bilibili.com/video/BV1hE411t7RN?p=15

image.png

  1. import torchvision.datasets
  2. from torch.utils.data import DataLoader
  3. from torch.utils.tensorboard import SummaryWriter
  4. dataset = torchvision.datasets.CIFAR10("../data", train=True, transform=torchvision.transforms.ToTensor(),
  5. download=True)
  6. #
  7. dataloader = DataLoader(dataset, batch_size=64, shuffle=True, num_workers=0, drop_last=True) # drop_last=True 表示最后一个bath舍弃
  8. writer = SummaryWriter("../logs/dataloader")
  9. for epoch in range(2):
  10. step=0
  11. for data in dataloader:
  12. imgs, target = data
  13. print(imgs.shape)
  14. print(target)
  15. writer.add_images("Epoch_%d"%(epoch), imgs, step)
  16. step +=1
  17. writer.close()

image.png