RRFRRF commited on
Commit
3894ed4
1 Parent(s): e66e7de

ioad minist

Browse files
Files changed (1) hide show
  1. Image/utils/dataset_utils.py +55 -0
Image/utils/dataset_utils.py CHANGED
@@ -53,3 +53,58 @@ def get_cifar10_dataloaders(batch_size=128, num_workers=2, local_dataset_path=No
53
  testset, batch_size=100, shuffle=False, num_workers=num_workers)
54
 
55
  return trainloader, testloader
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53
  testset, batch_size=100, shuffle=False, num_workers=num_workers)
54
 
55
  return trainloader, testloader
56
+
57
+ def get_mnist_dataloaders(batch_size=128, num_workers=2, local_dataset_path=None):
58
+ """获取MNIST数据集的数据加载器
59
+
60
+ Args:
61
+ batch_size: 批次大小
62
+ num_workers: 数据加载的工作进程数
63
+ local_dataset_path: 本地数据集路径,如果提供则使用本地数据集,否则下载
64
+
65
+ Returns:
66
+ trainloader: 训练数据加载器
67
+ testloader: 测试数据加载器
68
+ """
69
+ # 数据预处理
70
+ transform_train = transforms.Compose([
71
+ transforms.RandomRotation(10), # 随机旋转±10度
72
+ transforms.RandomAffine( # 随机仿射变换
73
+ degrees=0, # 不进行旋转
74
+ translate=(0.1, 0.1), # 平移范围
75
+ scale=(0.9, 1.1) # 缩放范围
76
+ ),
77
+ transforms.ToTensor(),
78
+ transforms.Normalize((0.1307,), (0.3081,)) # MNIST数据集的均值和标准差
79
+ ])
80
+
81
+ transform_test = transforms.Compose([
82
+ transforms.ToTensor(),
83
+ transforms.Normalize((0.1307,), (0.3081,))
84
+ ])
85
+
86
+ # 设置数据集路径
87
+ if local_dataset_path:
88
+ print(f"使用本地数据集: {local_dataset_path}")
89
+ download = False
90
+ dataset_path = local_dataset_path
91
+ else:
92
+ print("未指定本地数据集路径,将下载数据集")
93
+ download = True
94
+ dataset_path = '../dataset'
95
+
96
+ # 创建数据集路径
97
+ if not os.path.exists(dataset_path):
98
+ os.makedirs(dataset_path)
99
+
100
+ trainset = torchvision.datasets.MNIST(
101
+ root=dataset_path, train=True, download=download, transform=transform_train)
102
+ trainloader = torch.utils.data.DataLoader(
103
+ trainset, batch_size=batch_size, shuffle=True, num_workers=num_workers)
104
+
105
+ testset = torchvision.datasets.MNIST(
106
+ root=dataset_path, train=False, download=download, transform=transform_test)
107
+ testloader = torch.utils.data.DataLoader(
108
+ testset, batch_size=100, shuffle=False, num_workers=num_workers)
109
+
110
+ return trainloader, testloader