forked from alecwangcq/GraSP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datagen.py
162 lines (139 loc) · 5.33 KB
/
datagen.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import zipfile
from pathlib import Path
import requests
import torch
from torch.utils.data import DataLoader
from torchvision.transforms import ToTensor, Lambda, Compose, Normalize
from torchvision import datasets, transforms
from torchvision.datasets.utils import check_integrity
from torchvision.datasets.mnist import MNIST
from torchvision.datasets.cifar import CIFAR10, CIFAR100
from torchvision.datasets.imagenet import ImageNet
from tqdm import tqdm
def load_mnist(
batch_size: int = 64, shuffle: bool = True, root: str = "data"
) -> tuple[MNIST, MNIST]:
"""Load MNIST Dataset from memory or download it if it is not found
Args:
batch_size (int): Batch Size for DataLoader
shuffle (bool, optional): Whether to shuffle the data. Defaults to True.
root (str, optional): Path to store the data. Defaults to "data".
Returns:
tuple[MNIST, MNIST]: (train_data, test_data)
"""
try:
train = MNIST(
root=root,
train=True,
download=False,
transform=Compose([ToTensor(), Normalize((0.1307,), (0.3081,))]),
# target_transform=Lambda(
# lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1)
# ),
)
test = MNIST(
root=root,
train=False,
download=False,
transform=Compose([ToTensor(), Normalize((0.1307,), (0.3081,))]),
# target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).
# scatter_(0, torch.tensor(y), value=1))
)
except RuntimeError:
train = MNIST(
root=root,
train=True,
download=True,
transform=Compose([ToTensor(), Normalize((0.1307,), (0.3081,))]),
# target_transform=Lambda(
# lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1)
# ),
)
test = MNIST(
root=root,
train=False,
download=True,
transform=Compose([ToTensor(), Normalize((0.1307,), (0.3081,))]),
# target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).
# scatter_(0, torch.tensor(y), value=1))
)
train_data = DataLoader(train, batch_size=batch_size, shuffle=shuffle)
test_data = DataLoader(test, batch_size=batch_size, shuffle=shuffle)
return train_data, test_data
def load_cifar10(
batch_size: int = 128, shuffle: bool = True, root: str = "data"
) -> tuple[CIFAR10, CIFAR10]:
"""Load CIFAR10 Dataset from memory or download it if it is not found
Args:
batch_size (int): Batch Size for DataLoader
shuffle (bool, optional): Whether to shuffle the data. Defaults to True.
root (str, optional): Path to store the data. Defaults to "data".
Returns:
tuple[CIFAR10, CIFAR10]: (train_data, test_data)
"""
try:
train = CIFAR10(
root=root,
train=True,
download=False,
transform=Compose(
[
ToTensor(),
Normalize(
(0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261)
), # https://github.com/kuangliu/pytorch-cifar/issues/19
]
),
# target_transform=Lambda(
# lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1)
# ),
)
test = CIFAR10(
root=root,
train=False,
download=False,
transform=Compose(
[
ToTensor(),
Normalize(
(0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261)
), # https://github.com/kuangliu/pytorch-cifar/issues/19
]
),
# target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1))
)
except RuntimeError:
train = CIFAR10(
root=root,
train=True,
download=True,
transform=Compose(
[
ToTensor(),
Normalize(
(0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261)
), # https://github.com/kuangliu/pytorch-cifar/issues/19
]
),
# target_transform=Lambda(
# lambda y: torch.zeros(10, dtype=torch.float).scatter_(0, torch.tensor(y), value=1)
# ),
)
test = CIFAR10(
root=root,
train=False,
download=True,
transform=Compose(
[
ToTensor(),
Normalize(
(0.4914, 0.4822, 0.4465), (0.247, 0.243, 0.261)
), # https://github.com/kuangliu/pytorch-cifar/issues/19
]
),
# target_transform=Lambda(lambda y: torch.zeros(10, dtype=torch.float).
# scatter_(0, torch.tensor(y), value=1))
)
train_data = DataLoader(train, batch_size=batch_size, shuffle=shuffle)
test_data = DataLoader(test, batch_size=batch_size, shuffle=shuffle)
return train_data, test_data