-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_utils.py
More file actions
255 lines (203 loc) · 8.54 KB
/
data_utils.py
File metadata and controls
255 lines (203 loc) · 8.54 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
"""Data utility functions."""
import os
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.utils.data as data
from PIL import Image
from torchvision import transforms
import _pickle as pickle
# pylint: disable=C0326
SEG_LABELS_LIST = [
{"id": -1, "name": "void", "rgb_values": [0, 0, 0]},
{"id": 0, "name": "building", "rgb_values": [128, 0, 0]},
{"id": 1, "name": "grass", "rgb_values": [0, 128, 0]},
{"id": 2, "name": "tree", "rgb_values": [128, 128, 0]},
{"id": 3, "name": "cow", "rgb_values": [0, 0, 128]},
{"id": 4, "name": "horse", "rgb_values": [128, 0, 128]},
{"id": 5, "name": "sheep", "rgb_values": [0, 128, 128]},
{"id": 6, "name": "sky", "rgb_values": [128, 128, 128]},
{"id": 7, "name": "mountain", "rgb_values": [64, 0, 0]},
{"id": 8, "name": "airplane", "rgb_values": [192, 0, 0]},
{"id": 9, "name": "water", "rgb_values": [64, 128, 0]},
{"id": 10, "name": "face", "rgb_values": [192, 128, 0]},
{"id": 11, "name": "car", "rgb_values": [64, 0, 128]},
{"id": 12, "name": "bicycle", "rgb_values": [192, 0, 128]},
{"id": 13, "name": "flower", "rgb_values": [64, 128, 128]},
{"id": 14, "name": "sign", "rgb_values": [192, 128, 128]},
{"id": 15, "name": "bird", "rgb_values": [0, 64, 0]},
{"id": 16, "name": "book", "rgb_values": [128, 64, 0]},
{"id": 17, "name": "chair", "rgb_values": [0, 192, 0]},
{"id": 18, "name": "road", "rgb_values": [128, 64, 128]},
{"id": 19, "name": "cat", "rgb_values": [0, 192, 128]},
{"id": 20, "name": "dog", "rgb_values": [128, 192, 128]},
{"id": 21, "name": "body", "rgb_values": [64, 64, 0]},
{"id": 22, "name": "boat", "rgb_values": [192, 64, 0]}]
def label_img_to_rgb(label_img):
label_img = np.squeeze(label_img)
labels = np.unique(label_img)
label_infos = [l for l in SEG_LABELS_LIST if l['id'] in labels]
label_img_rgb = np.array([label_img,
label_img,
label_img]).transpose(1,2,0)
for l in label_infos:
mask = label_img == l['id']
label_img_rgb[mask] = l['rgb_values']
return label_img_rgb.astype(np.uint8)
class SegmentationData(data.Dataset):
def __init__(self, image_paths_file):
self.root_dir_name = os.path.dirname(image_paths_file)
with open(image_paths_file) as f:
self.image_names = f.read().splitlines()
def __getitem__(self, key):
if isinstance(key, slice):
# get the start, stop, and step from the slice
return [self[ii] for ii in range(*key.indices(len(self)))]
elif isinstance(key, int):
# handle negative indices
if key < 0:
key += len(self)
if key < 0 or key >= len(self):
raise IndexError("The index (%d) is out of range." % key)
# get the data from direct index
return self.get_item_from_index(key)
else:
raise TypeError("Invalid argument type.")
def __len__(self):
return len(self.image_names)
def get_item_from_index(self, index):
to_tensor = transforms.ToTensor()
img_id = self.image_names[index].replace('.bmp', '')
img = Image.open(os.path.join(self.root_dir_name,
'images',
img_id + '.bmp')).convert('RGB')
center_crop = transforms.CenterCrop(240)
img = center_crop(img)
img = to_tensor(img)
target = Image.open(os.path.join(self.root_dir_name,
'targets',
img_id + '_GT.bmp'))
target = center_crop(target)
target = np.array(target, dtype=np.int64)
target_labels = target[..., 0]
for label in SEG_LABELS_LIST:
mask = np.all(target == label['rgb_values'], axis=2)
target_labels[mask] = label['id']
target_labels = torch.from_numpy(target_labels.copy())
return img, target_labels
class OverfitSampler(object):
"""
Sample dataset to overfit.
"""
def __init__(self, num_samples):
self.num_samples = num_samples
def __iter__(self):
return iter(range(self.num_samples))
def __len__(self):
return self.num_samples
class CIFAR10Data(data.Dataset):
def __init__(self, X, y):
self.X = X
self.y = y
def __getitem__(self, index):
img = self.X[index]
label = self.y[index]
img = torch.from_numpy(img)
return img, label
def __len__(self):
return len(self.y)
def get_CIFAR10_data(num_training=48000, num_validation=1000, num_test=1000):
"""
Load the CIFAR-10 dataset from disk and perform preprocessing to prepare
it for classifiers. These are the same steps as we used for the SVM, but
condensed to a single function.
"""
# Load the raw CIFAR-10 data
cifar10_dir = 'datasets/'
X, y = load_CIFAR10(cifar10_dir)
# Subsample the data
# Our training set will be the first num_train points from the original
# training set.
mask = list(range(num_training))
X_train = X[mask]
y_train = y[mask]
# Our validation set will be num_validation points from the original
# training set.
mask = list(range(num_training, num_training + num_validation))
X_val = X[mask]
y_val = y[mask]
# We use a small subset of the training set as our test set.
mask = list(range(num_training + num_validation,
num_training + num_validation + num_test))
X_test = X[mask]
y_test = y[mask]
# Normalize the data: subtract the mean image
mean_image = np.mean(X_train, axis=0)
X_train -= mean_image
X_val -= mean_image
X_test -= mean_image
# Transpose so that channels come first
X_train = X_train.transpose(0, 3, 1, 2).copy()
X_val = X_val.transpose(0, 3, 1, 2).copy()
X_test = X_test.transpose(0, 3, 1, 2).copy()
# Package data into a dictionary
return {
'X_train': X_train, 'y_train': y_train,
'X_val': X_val, 'y_val': y_val,
'X_test': X_test, 'y_test': y_test,
}
def get_CIFAR10_datasets(num_training=48000, num_validation=1000,
num_test=1000, dtype=np.float32):
"""
Load and preprocess the CIFAR-10 dataset.
"""
path = 'datasets/cifar10_train.p'
with open(path, 'rb') as f:
datadict = pickle.load(f, encoding='latin1')
X = np.array(datadict['data'])
y = np.array(datadict['labels'])
X = X.reshape(-1, 3, 32, 32).astype(dtype)
X /= 255.0
# Normalize the data: subtract the mean image
mean_image = np.mean(X, axis=0)
X -= mean_image
# Subsample the data
mask = range(num_training)
X_train = X[mask]
y_train = y[mask]
mask = range(num_training, num_training + num_validation)
X_val = X[mask]
y_val = y[mask]
mask = range(num_training + num_validation,
num_training + num_validation + num_test)
X_test = X[mask]
y_test = y[mask]
return (CIFAR10Data(X_train, y_train),
CIFAR10Data(X_val, y_val),
CIFAR10Data(X_test, y_test),
mean_image)
def scoring_function(x, lin_exp_boundary, doubling_rate):
assert np.all([x >= 0, x <= 1])
score = np.zeros(x.shape)
lin_exp_boundary = lin_exp_boundary
linear_region = np.logical_and(x > 0.1, x <= lin_exp_boundary)
exp_region = np.logical_and(x > lin_exp_boundary, x <= 1)
score[linear_region] = 100.0 * x[linear_region]
c = doubling_rate
a = 100.0 * lin_exp_boundary / np.exp(lin_exp_boundary * np.log(2) / c)
b = np.log(2.0) / c
score[exp_region] = a * np.exp(b * x[exp_region])
return score
def rel_error(x, y):
""" Returns relative error """
assert x.shape == y.shape, "tensors do not have the same shape. %s != %s" % (x.shape, y.shape)
return np.max(np.abs(x - y) / (np.maximum(1e-8, np.abs(x) + np.abs(y))))
def string2image(string):
"""Converts a string to a numpy array."""
return np.array([int(item) for item in string.split()]).reshape((96, 96))
def get_image(idx, key_pts_frame):
image_string = key_pts_frame.loc[idx]['Image']
return string2image(image_string)
def get_keypoints(idx, key_pts_frame):
keypoint_cols = list(key_pts_frame.columns)[:-1]
return key_pts_frame.iloc[idx][keypoint_cols].values.reshape((15, 2)).astype(np.float32)