import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWriter
#准备的测试数据集
test_data=torchvision.datasets.CIFAR10("./dataset2",train=False,transform=torchvision.transforms.ToTensor())
#Dataloder的使用
test_loader=DataLoader(dataset=test_data,batch_size=64,shuffle=True,num_workers=0,drop_last=False)
参数:dataset=数据集,batch_size设置每批多少张图,shuffle是否打乱图片顺序,num_workers采用多少个进程,drop_last是否舍弃最后除不尽的几张图(根据所有张数除以batch_size)
dataloader根据batchsize打包图片,将img,traget分别打包返回。
![](https://i-blog.csdnimg.cn/blog_migrate/06c56b03a5ccf7dc1bb07c46113c5b73.png)
![](https://i-blog.csdnimg.cn/blog_migrate/5e88d3abf0ab58b56097a8c263936342.png)
用tensorboard显示
writer=SummaryWriter("dataloader")
step=0
for data in test_loader:
imgs,targets=data
#print(imgs.shape)
#print(targets)
writer.add_images("test_data",imgs,step)
step=step+1
writer.close()