-
Notifications
You must be signed in to change notification settings - Fork 15
/
data.py
147 lines (115 loc) · 4.63 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
import os
import pickle
import numpy as np
import torch
from torch.utils.data.dataset import Dataset
from torch.utils.data import DataLoader, sampler
from transform import get_train_transforms, get_test_transforms, CLAHE_GRAY
from tqdm import tqdm
# To load the picked dataset
class PickledDataset(Dataset):
def __init__(self, file_path, transform=None):
with open(file_path, mode='rb') as f:
data = pickle.load(f)
self.features = data['features']
self.labels = data['labels']
self.count = len(self.labels)
self.transform = transform
def __getitem__(self, index):
feature = self.features[index]
if self.transform is not None:
feature = self.transform(feature)
return (feature, self.labels[index])
def __len__(self):
return self.count
# To move batches to the GPU
class WrappedDataLoader:
def __init__(self, dl, func):
self.dl = dl
self.func = func
def __len__(self):
return len(self.dl)
def __iter__(self):
batches = iter(self.dl)
for b in batches:
yield (self.func(*b))
def extend_dataset(dataset):
X = dataset.features
y = dataset.labels
num_classes = 43
X_extended = np.empty([0] + list(dataset.features.shape)
[1:], dtype=dataset.features.dtype)
y_extended = np.empty([0], dtype=dataset.labels.dtype)
horizontally_flippable = [11, 12, 13, 15, 17, 18, 22, 26, 30, 35]
vertically_flippable = [1, 5, 12, 15, 17]
both_flippable = [32, 40]
cross_flippable = np.array([
[19, 20],
[33, 34],
[36, 37],
[38, 39],
[20, 19],
[34, 33],
[37, 36],
[39, 38]
])
for c in range(num_classes):
X_extended = np.append(X_extended, X[y == c], axis=0)
if c in horizontally_flippable:
X_extended = np.append(
X_extended, X[y == c][:, :, ::-1, :], axis=0)
if c in vertically_flippable:
X_extended = np.append(
X_extended, X[y == c][:, ::-1, :, :], axis=0)
if c in cross_flippable[:, 0]:
flip_c = cross_flippable[cross_flippable[:, 0] == c][0][1]
X_extended = np.append(
X_extended, X[y == flip_c][:, :, ::-1, :], axis=0)
if c in both_flippable:
X_extended = np.append(
X_extended, X[y == c][:, ::-1, ::-1, :], axis=0)
y_extended = np.append(y_extended, np.full(
X_extended.shape[0]-y_extended.shape[0], c, dtype=y_extended.dtype))
dataset.features = X_extended
dataset.labels = y_extended
dataset.count = len(y_extended)
return dataset
def preprocess(path):
if not os.path.exists(f"{path}/train_gray.p"):
for dataset in ['train', 'valid', 'test']:
with open(f"{path}/{dataset}.p", mode='rb') as f:
data = pickle.load(f)
X = data['features']
y = data['labels']
clahe = CLAHE_GRAY()
for i in tqdm(range(len(X)), desc=f"Processing {dataset} dataset"):
X[i] = clahe(X[i])
X = X[:, :, :, 0]
with open(f"{path}/{dataset}_gray.p", "wb") as f:
pickle.dump({"features": X.reshape(
X.shape + (1,)), "labels": y}, f)
def get_train_loaders(path, device, batch_size, workers, class_count):
def to_device(x, y):
return x.to(device), y.to(device, dtype=torch.int64)
train_dataset = extend_dataset(PickledDataset(
path+'/train_gray.p', transform=get_train_transforms()))
valid_dataset = PickledDataset(
path+'/valid_gray.p', transform=get_test_transforms())
# Use weighted sampler
class_sample_count = np.bincount(train_dataset.labels)
weights = 1 / np.array([class_sample_count[y]
for y in train_dataset.labels])
samp = sampler.WeightedRandomSampler(weights, 43 * class_count)
train_loader = WrappedDataLoader(DataLoader(
train_dataset, batch_size=batch_size, sampler=samp, num_workers=workers), to_device)
valid_loader = WrappedDataLoader(DataLoader(
valid_dataset, batch_size=batch_size, shuffle=False, num_workers=workers), to_device)
return train_loader, valid_loader
def get_test_loader(path, device, gray=True):
def preprocess(x, y):
return x.to(device), y.to(device, dtype=torch.int64)
test_dataset = PickledDataset(
path+'/test_gray.p', transform=get_test_transforms())
test_loader = WrappedDataLoader(DataLoader(
test_dataset, batch_size=64, shuffle=False), preprocess)
return test_loader