一、DataLoader参数解析
DataLoader官网使用手册
参数 | 描述 |
---|---|
dataset | 说明数据集所在的位置、数据总数等 |
batch_size | 每次取多少张图片 |
shuffle | True乱序、False顺序(默认) |
sampler | |
batch_sampler | |
num_workers | 多进程,默认为0采用主进程加载数据 |
collate_fn | |
pin_memory | |
drop_last | True最后不满一个batch_size的去掉,False最后剩余多少也都要 |
timeout | |
worker_init_fn | |
generator | |
prefetch_factor | |
prefetch_factor | |
pin_memory_device |
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWritertest_data = torchvision.datasets.CIFAR10("CIFAR_10",train=False,transform=torchvision.transforms.ToTensor())
#Ctrl按住,点击CIFAR10,找到__getitem__类,查看返回值return img, targettest_loader = DataLoader(dataset=test_data,batch_size=64,shuffle=True,num_workers=0,drop_last=False)
#drop_last=False,最后剩余多少也都要
#shuffle=True,乱序#测试一下
img,target = test_data[0]
print(img.shape)#torch.Size([3, 32, 32])
print(target)#3writer = SummaryWriter("y_log")
i = 0
for data in test_loader:imgs,targets = data#print(type(imgs))writer.add_images("test_data",imgs,i)i = i+1writer.close()
在Terminal下运行tensorboard --logdir=y_log --port=2312
,logdir为打开事件文件的路径,port为指定端口打开;
通过指定端口2312进行打开tensorboard,若不设置port参数,默认通过6006端口进行打开。
点击该链接或者复制链接到浏览器打开即可
import torchvision
from torch.utils.data import DataLoader
from torch.utils.tensorboard import SummaryWritertest_data = torchvision.datasets.CIFAR10("CIFAR_10",train=False,transform=torchvision.transforms.ToTensor())
#Ctrl按住,点击CIFAR10,找到__getitem__类,查看返回值return img, targettest_loader = DataLoader(dataset=test_data,batch_size=64,shuffle=False,num_workers=0,drop_last=True)
#drop_last=True,不满一组的舍去
#shuffle=False,顺序img,target = test_data[0]
print(img.shape)#torch.Size([3, 32, 32])
print(target)#3writer = SummaryWriter("y_log")for epoch in range(3):i = 0for data in test_loader:imgs,targets = data#print(type(imgs))writer.add_images("Epoch:{}".format(epoch),imgs,i)i = i+1writer.close()