使用transform
加载数据集,查看数据集的属性
将图片转换成tensor类型
python
dataset_transform = torchvision.transforms.Compose([
torchvision.transforms.ToTensor()
])
train_set = torchvision.datasets.CIFAR10(root="./dataset",train=True,transform= dataset_transform,download=True)
test_set = torchvision.datasets.CIFAR10(root="./dataset",train=True,transform= dataset_transform,download=True)
print(test_set[0])
将该数据的数据显示在tensorboard中
Dataloader
python
import torchvision
from torch.utils.data import DataLoader
#准备测试数据集
test_data = torchvision.datasets.CIFAR10("./dataset",train=False,transform=torchvision.transforms.ToTensor(),download=True)
test_loader = DataLoader(dataset = test_data,batch_size=4,shuffle=True,num_workers=0,drop_last=False)
#测试数据集中第一张图片集
img,target = test_data[0]
print(img.shape)
print(target)
for data in test_loader:
imgs,targets = data
print(imgs.shape)
print(targets)
出现以上问题,需要将numberworks设置为0
drop_last 当取数据有余数时,是舍去还是保留
python
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)
#测试数据集中第一张图片集
img,target = test_data[0]
print(img.shape)
print(target)
writer = SummaryWriter("DataLodaer")
#shuffle 为True 两次结果不一样
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()
神经网络