-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_data.py
More file actions
269 lines (225 loc) · 8.97 KB
/
utils_data.py
File metadata and controls
269 lines (225 loc) · 8.97 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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
from random import randint
import numpy as np
import json
import os
from PIL import Image
import matplotlib.pyplot as plt
import math
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def printProgressBar (iteration, total, prefix = '', suffix = '', decimals = 1, length = 100, fill = '█', printEnd = "\r"):
"""
Function used mainly to visualize long tasks. This function does not
impact the functionality of our code in any way, we used it mainly
to have something to look at while waiting in the terminal.
The original source is the following:
https://stackoverflow.com/questions/3173320/text-progress-bar-in-terminal-with-block-characters
This is the original documentation for the function:
Call in a loop to create terminal progress bar
@params:
iteration - Required : current iteration (Int)
total - Required : total iterations (Int)
prefix - Optional : prefix string (Str)
suffix - Optional : suffix string (Str)
decimals - Optional : positive number of decimals in percent complete (Int)
length - Optional : character length of bar (Int)
fill - Optional : bar fill character (Str) █
printEnd - Optional : end character (e.g. "\r", "\r\n") (Str)
"""
percent = ("{0:." + str(decimals) + "f}").format(100 * (iteration / float(total)))
filledLength = int(length * iteration // total)
bar = fill * filledLength + '-' * (length - filledLength)
print(f'\r{prefix} |'+bcolors.OKCYAN+f'{bar}'+bcolors.ENDC+f'| {percent}% {suffix}', end = printEnd)
# Print New Line on Complete
if iteration == total:
print()
def grayscalize(dataset):
"""
turns a grayscale from a one dimensional vector rgb to a single
hue value (from the HSL representation)
"""
shape = dataset.shape
l=shape[0]
c=0
for photo in range(shape[0]):
c+=1
printProgressBar(c + 1, l, prefix = 'Grayscalizing:', suffix = 'Complete', length = 50)
for px in range(shape[1]):
for py in range(shape[2]):
item = dataset[photo][px][py]
dataset[photo][px][py]=0.299*item[0]+0.587*item[1]+0.114*item[2]
return dataset
def k_fold(dataset,k,q):
"""
samples a dataset (a list) using the k-fold technique. It slices
the dataset in k chunks with (almost) the same sizes, and divides
it in train and test. Chunk number q (zero indiced) will be test,
while the other chunks will be train.
"""
l=len(dataset)
if q>=k:
raise ValueError("q no pot ser >= k")
if k>1 and k<l and k<15:
c=round(np.floor(l/k))
if q==0: #Primer cas, k-fold agafa el primer set
test = dataset[:c]
train = dataset[c:]
elif q==k-1:
test = dataset[l-c:]
train = dataset[:l-c]
else:
test = dataset[q*c:(q+1)*c]
train1 = dataset[:q*c]
train2 = dataset[(q+1)*c:]
train = np.concatenate((train1,train2))
return train, test
def get_picks(rng,size):
"""
creates a list of indices of size up to 'size' and with integers
in the interval [0,rng]. Uniqueness is *not* guaranteed.
"""
picks = set([randint(0, rng-1) for k in range(size)])
return list(picks)
def get_sample(dataset,picks):
"""
samples a dataset (a list) from a list of indices, 'picks'
"""
return np.array([dataset[pick] for pick in picks])
def get_random_sample(dataset,size):
"""
samples a dataset (a list) from a list of indices, 'picks',
which are generated randomly as in the method get_picks.
"""
picks = set([randint(0, size-1) for k in range(len(dataset))])
return np.array([dataset[pick] for pick in picks])
def read_dataset(ROOT_FOLDER='./images/', gt_json='./test/gt.json', w=60, h=80):
"""
reads the dataset (train and test), returns the images and labels (class and colors) for both sets
"""
np.random.seed(123)
ground_truth = json.load(open(gt_json, 'r'))
train_img_names, train_class_labels, train_color_labels = [], [], []
test_img_names, test_class_labels, test_color_labels = [], [], []
## IDEA: AFEGIR UNA VARIABLE TOPALL AL NÚMERO DE IMG TRAIN I test
# Només a debug, no a release. Per anar més ràpid. No es pot [:x]
l1=len(ground_truth['train'].items())
c=0
for k, v in ground_truth['train'].items():
printProgressBar(c + 1, l1, prefix = 'Preparing Train Data:', suffix = 'Complete', length = 50)
c+=1
train_img_names.append(os.path.join(ROOT_FOLDER, 'train', k))
train_class_labels.append(v[0])
train_color_labels.append(v[1])
l1=len(ground_truth['test'].items())
c=0
for k, v in ground_truth['test'].items():
printProgressBar(c + 1, l1, prefix = 'Preparing Test Data:', suffix = 'Complete', length = 50)
c+=1
test_img_names.append(os.path.join(ROOT_FOLDER, 'test', k))
test_class_labels.append(v[0])
test_color_labels.append(v[1])
train_imgs, test_imgs = load_imgs(train_img_names, test_img_names)
np.random.seed(42)
idxs = np.arange(train_imgs.shape[0])
np.random.shuffle(idxs)
train_imgs = train_imgs[idxs]
train_class_labels = np.array(train_class_labels)[idxs]
train_color_labels = np.array(train_color_labels,dtype=object)[idxs]
idxs = np.arange(test_imgs.shape[0])
np.random.shuffle(idxs)
test_imgs = test_imgs[idxs]
test_class_labels = np.array(test_class_labels)[idxs]
test_color_labels = np.array(test_color_labels,dtype=object)[idxs]
return train_imgs, train_class_labels, train_color_labels, test_imgs, test_class_labels, test_color_labels
def load_imgs(train_img_names, test_img_names, w=60, h=80):
train_imgs, test_imgs = [], []
l1=(len(train_img_names))
c=0
for tr in train_img_names:
printProgressBar(c + 1, l1, prefix = 'Reading Train Data:', suffix = 'Complete', length = 50)
c+=1
train_imgs.append(read_one_img(tr + '.jpg'))
l1=(len(test_img_names))
c=0
for te in test_img_names:
printProgressBar(c + 1, l1, prefix = 'Reading Test Data:', suffix = 'Complete', length = 50)
c+=1
test_imgs.append(read_one_img(te + '.jpg'))
return np.array(train_imgs), np.array(test_imgs)
def read_one_img(img_name, w=60, h=80):
img = Image.open(img_name)
img = img.convert("RGB")
if img.size != (w, h):
img = img.resize((w, h))
return np.array(img)
def visualize_retrieval(imgs, topN, info=None, ok=None, title='', query=None):
def add_border(color):
return np.stack([np.pad(imgs[i, :, :, c], 3, mode='constant', constant_values=color[c]) for c in range(3)],
axis=2)
columns = 4
rows = math.ceil(topN / columns)
if query is not None:
fig = plt.figure(figsize=(10, 8 * 6 / 8))
columns += 1
fig.add_subplot(rows, columns, 1 + columns)
plt.imshow(query)
plt.axis('off')
plt.title(f'query', fontsize=8)
else:
fig = plt.figure(figsize=(8, 8 * 6 / 8))
for i in range(min(topN, len(imgs))):
sp = i + 1
if query is not None:
sp = (sp - 1) // (columns - 1) + 1 + sp
fig.add_subplot(rows, columns, sp)
if ok is not None:
im = add_border([0, 255, 0] if ok[i] else [255, 0, 0])
else:
im = imgs[i]
plt.imshow(im)
plt.axis('off')
if info is not None:
plt.title(f'{info[i]}', fontsize=8)
plt.gcf().suptitle(title)
plt.show()
# Visualize k-mean with 3D plot
def Plot3DCloud(km, rows=1, cols=1, spl_id=1):
ax = plt.gcf().add_subplot(rows, cols, spl_id, projection='3d')
for k in range(km.K):
Xl = km.X[km.labels == k, :]
ax.scatter(km.centroids[:,0] , km.centroids[:,1] ,km.centroids[:,2], s = 100, color = 'k',marker=(5, 1))
ax.scatter(Xl[:, 0], Xl[:, 1], Xl[:, 2], marker='.',
c=km.centroids[np.ones((Xl.shape[0]), dtype='int') * k, :] / 255)
plt.xlabel('dim 1')
plt.ylabel('dim 2')
ax.set_zlabel('dim 3')
plt.show(block=True)
return ax
def visualize_k_means(kmeans, img_shape):
def prepare_img(x, img_shape):
x = np.clip(x.astype('uint8'), 0, 255)
x = x.reshape(img_shape)
return x
fig = plt.figure(figsize=(8, 8))
X_compressed = kmeans.centroids[kmeans.labels.astype('int32')]
X_compressed = prepare_img(X_compressed, img_shape)
org_img = prepare_img(kmeans.X, img_shape)
fig.add_subplot(131)
plt.imshow(org_img)
plt.title('original')
plt.axis('off')
fig.add_subplot(132)
plt.imshow(X_compressed)
plt.axis('off')
plt.title('kmeans')
Plot3DCloud(kmeans, 1, 3, 3)
plt.title('núvol de punts')
plt.show()