torchvision中数据集的自动下载以及使用
import torchvision
from tensorboardX import SummaryWriter
dataset_transform = torchvision.transforms.Compose(
[torchvision.transforms.ToTensor()
]
)
train_set = torchvision.datasets.CIFAR10(root="./data", train=True, transform=dataset_transform, download=False) # 自动下载数据集
test_set = torchvision.datasets.CIFAR10(root="./data", train=False, transform=dataset_transform, download=False)
# print(test_set[0]) # 测试集中的第一个输出
# print(test_set.classes)
#
# img, target = test_set[0]
# print(img)
# print(target)
# print(test_set.classes[target])
# img.show()
print(test_set[0]) # 输出一个tensor类型
writer = SummaryWriter("p10") # 记录日志信息
for i in range(10):
img, target = test_set[i]
writer.add_image("test_set", img, i)
writer.close()
DataLoader的使用
按照batch的大小将image、target分别打包
import torchvision
# 准备的测试数据集
from mmcv import DataLoader
from torch.utils.tensorboard import SummaryWriter
test_data = torchvision.datasets.CIFAR10("./data", train=False, transform=torchvision.transforms.ToTensor())
test_loader = DataLoader(dataset=test_data, batch_size=4, shuffle=True, num_workers=0, drop_last=False) # num_workers 是否启动多线程 批量取数据之后余数剩下的还要不要
# 测试数据集中第一张图片及target
img, target = test_data[0]
print(img.shape)
print(target)
writer = SummaryWriter("dataloador")
step = 0
for data in test_loader:
for epoch in test_loader:
imgs, targets = data
# print(imgs.shape)
# print(targets)
writer.add_images("test_data", imgs, step) # step 全局步径
writer.add_images("Epoch:{}".format(epoch), imgs, step) # step 全局步径
step = step + 1
writer.close()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)