-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.py
172 lines (133 loc) · 4.96 KB
/
data.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
163
164
165
166
167
168
169
170
171
172
"""Data code."""
import torch
import random
import torchvision.transforms as T
from torch.utils.data import DataLoader
from torchvision.datasets import CIFAR10, CIFAR100, SVHN, STL10
from random import choices
from collections import defaultdict
from augmentation import Augmenter
MEANSTD = {
'cifar10': ((0.4914, 0.4822, 0.4465), (0.2471, 0.2435, 0.2616)),
'cifar100': ((0.5071, 0.4867, 0.4408), (0.2675, 0.2565, 0.2761)),
'svhn': ((0.4377, 0.4438, 0.4728), (0.1980, 0.2010, 0.1970)),
}
def load_dataset(data='cifar10', split='train'):
"""Load a dataset."""
root = f"./data/{data}"
if 'cifar' in data:
if split == 'train':
split = True
elif split == 'test':
split = False
if data == 'cifar10':
dataset = CIFAR10(root=root, train=split, download=True)
elif data == 'cifar100':
dataset = CIFAR100(root=root, train=split, download=True)
elif data == 'svhn':
dataset = SVHN(root=root, split=split, download=True)
elif data == 'stl10':
dataset = STL10(root=root, split=split, download=True)
return dataset
class Dataset:
"""Train Dataset."""
def __init__(self, dataset, indices, num_iter=None):
"""Get dataset and item indices."""
self.dataset = dataset
self.indices = indices
if num_iter is None:
self.num_iter = len(self.indices)
else:
self.num_iter = num_iter
def __len__(self):
"""length."""
return self.num_iter
def __getitem__(self, i):
"""getitem."""
if self.num_iter != len(self.indices):
i = random.randint(0, len(self.indices)-1)
index = self.indices[i]
img, label = self.dataset[index]
return img, label
def split_train_datasets(data='cifar10',
num_X=250,
num_iter_X=64*1024,
num_iter_U=64*7*1024,
include_x_in_u=True):
"""Get X and U datasets.
* Parameters
- data: 'cifar10', 'cifar100', 'svhn' and 'stl10'.
- num_X: number of labeled data.
- include_x_in_u: include the labeled data in unlabeled data.
"""
dataset = load_dataset(data, split='train')
num_classes = 100 if data == 'cifar100' else 10
if 'cifar' in data:
labels = dataset.targets
else:
labels = dataset.labels
idx_per_cls = defaultdict(list)
for i, label in enumerate(labels):
idx_per_cls[label].append(i)
indices_x = []
for k, v in idx_per_cls.items():
indices_x += choices(v, k=(num_X//num_classes))
X = Dataset(dataset, indices_x, num_iter_X)
indices_u = list(range(len(dataset)))
if not include_x_in_u:
indices_u = list(set(indices_u) - set(indices_x))
U = Dataset(dataset, indices_u, num_iter_U)
return X, U
class PreProcessor:
"""Data Preprocessor."""
def __init__(self, meanstd, policies):
"""Get augmentation policy and meanstd for normalization."""
self.augs = [Augmenter(policy=policy) for policy in policies]
mean, std = meanstd
self.to_tensor = T.Compose([
T.ToTensor(),
T.Normalize(mean, std),
])
def to_input(self, images):
"""Change from pil to tensor and normalize it."""
x = torch.stack([self.to_tensor(image) for image in images])
return x
def __call__(self, batch):
"""Preprocess."""
images, labels = list(zip(*batch))
label = torch.tensor(labels)
tensors = []
for aug in self.augs:
x = self.to_input([aug(image) for image in images])
tensors.append(x)
return tensors, label
def get_dataloaders(data: str = 'cifar10', num_X: int = 250,
include_x_in_u=True, augs: list[int] = [1, 2],
batch_size: int = 64, mu: float = 7):
"""Get dataloaders."""
# train dataset
_X, _U = split_train_datasets(data=data,
num_X=num_X,
num_iter_X=batch_size*1024,
num_iter_U=batch_size*mu*1024,
include_x_in_u=include_x_in_u)
train_processor = PreProcessor(policies=augs,
meanstd=MEANSTD[data])
X = DataLoader(dataset=_X,
batch_size=batch_size,
shuffle=True,
collate_fn=train_processor)
U = DataLoader(dataset=_U,
batch_size=int(batch_size*mu),
shuffle=True,
collate_fn=train_processor)
# test dataset
test_processor = PreProcessor(policies=[0],
meanstd=MEANSTD[data])
_T = load_dataset(data=data,
split='test')
T = DataLoader(dataset=_T,
batch_size=batch_size,
shuffle=False,
collate_fn=test_processor)
return X, U, T