一、标准数据集使用
pytorch官网–标准数据集
这里以CIFAR10
数据集为例:CIFAR10
下载数据集
代码:
import torchvision
train_data=torchvision.datasets.CIFAR10(root="datasets",train=True,download=True)
test_data=torchvision.datasets.CIFAR10(root="datasets",train=False,download=True)
输出:
Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to datasets\cifar-10-python.tar.gz
100%|██████████| 170498071/170498071 [00:30<00:00, 5675896.90it/s]
Extracting datasets\cifar-10-python.tar.gz to datasets
Files already downloaded and verified
查看数据集属性
import torchvision
train_data=torchvision.datasets.CIFAR10(root="datasets",train=True,download=True)
test_data=torchvision.datasets.CIFAR10(root="datasets",train=False,download=True)
print("test_data")
print(test_data)
print("test_data[0]")
print(test_data[0])
img,target=test_data[0]
img.show()
print(test_data.classes[target])
输出:
test_data 的属性:善用debug
二、把读入的数据集都转换为Tensor类型,并用Tensorboard展示
代码:
import torchvision
from torch.utils.tensorboard import SummaryWriter
trans_dataset=torchvision.transforms.Compose([torchvision.transforms.ToTensor()])
train_data=torchvision.datasets.CIFAR10(root="datasets",train=True,transform=trans_dataset,download=True)
test_data=torchvision.datasets.CIFAR10(root="datasets",train=False,transform=trans_dataset,download=True)
# print("test_data")
# print(test_data)
# print("test_data[0]")
# print(test_data[0])
# img,target=test_data[0]
# img.show()
# print(test_data.classes[target])writer=SummaryWriter("logs")
writer.add_image("test",test_data[0][0],1)
writer.close()
输出: