File size: 4,050 Bytes
bdbd148 bd49e43 bdbd148 bd49e43 bdbd148 bd49e43 bdbd148 3894ed4 bd49e43 3894ed4 bd49e43 3894ed4 bd49e43 3894ed4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
import torch
import torchvision
import torchvision.transforms as transforms
import os
def get_cifar10_dataloaders(batch_size=128, num_workers=2, local_dataset_path=None,shuffle=True):
"""获取CIFAR10数据集的数据加载器
Args:
batch_size: 批次大小
num_workers: 数据加载的工作进程数
local_dataset_path: 本地数据集路径,如果提供则使用本地数据集,否则下载
Returns:
trainloader: 训练数据加载器
testloader: 测试数据加载器
"""
# 数据预处理
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
# 设置数据集路径
if local_dataset_path:
print(f"使用本地数据集: {local_dataset_path}")
download = False
dataset_path = local_dataset_path
else:
print("未指定本地数据集路径,将下载数据集")
download = True
dataset_path = '../dataset'
# 创建数据集路径
if not os.path.exists(dataset_path):
os.makedirs(dataset_path)
trainset = torchvision.datasets.CIFAR10(
root=dataset_path, train=True, download=download, transform=transform_train)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers)
testset = torchvision.datasets.CIFAR10(
root=dataset_path, train=False, download=download, transform=transform_test)
testloader = torch.utils.data.DataLoader(
testset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers)
return trainloader, testloader
def get_mnist_dataloaders(batch_size=128, num_workers=2, local_dataset_path=None,shuffle=True):
"""获取MNIST数据集的数据加载器
Args:
batch_size: 批次大小
num_workers: 数据加载的工作进程数
local_dataset_path: 本地数据集路径,如果提供则使用本地数据集,否则下载
Returns:
trainloader: 训练数据加载器
testloader: 测试数据加载器
"""
# 数据预处理
transform_train = transforms.Compose([
transforms.RandomRotation(10), # 随机旋转±10度
transforms.RandomAffine( # 随机仿射变换
degrees=0, # 不进行旋转
translate=(0.1, 0.1), # 平移范围
scale=(0.9, 1.1) # 缩放范围
),
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,)) # MNIST数据集的均值和标准差
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])
# 设置数据集路径
if local_dataset_path:
print(f"使用本地数据集: {local_dataset_path}")
download = False
dataset_path = local_dataset_path
else:
print("未指定本地数据集路径,将下载数据集")
download = True
dataset_path = '../dataset'
# 创建数据集路径
if not os.path.exists(dataset_path):
os.makedirs(dataset_path)
trainset = torchvision.datasets.MNIST(
root=dataset_path, train=True, download=download, transform=transform_train)
trainloader = torch.utils.data.DataLoader(
trainset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers)
testset = torchvision.datasets.MNIST(
root=dataset_path, train=False, download=download, transform=transform_test)
testloader = torch.utils.data.DataLoader(
testset, batch_size=batch_size, shuffle=shuffle, num_workers=num_workers)
return trainloader, testloader
|