-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
269 lines (218 loc) · 8.16 KB
/
utils.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
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 matplotlib import pyplot as plt
import numpy as np
from torchvision import transforms
import seaborn as sns
from matplotlib import pyplot as plt
from matplotlib import cm
import torch
from sklearn.manifold import TSNE
import time
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.patheffects as PathEffects
import pickle
import seaborn as sns
def compute_mean_std(X):
"""
The function computes the mean and the standard deviation on the dimension 0 (row)
Params:
- X matrix of data
Returns:
means and standard deviation of X
"""
X = np.array(X)
mean = X.mean(axis=0)
std = X.std(axis=0)
return mean, std
# DISPLAY
def show_image_label(img, label):
"""
Print an image (taken in tensor form) and its human readable label.
Returns:
A '68 fender stratocaster
"""
print(f"Showing an image of label {human_readable_label[label]}")
plt.imshow(transforms.ToPILImage()(img), interpolation="bicubic")
plt.show()
def new_heat_matrix():
lenx = 10
leny = 100
data = np.zeros((leny, lenx))
return data
def show_heat_matrix(data):
fig, ax = plt.subplots(figsize=(15,9))
ax = sns.heatmap(data, linewidth=0.2,cmap='Reds')
plt.show()
def update_heat_matrix(data, preds, num_batch, normalization):
#during test phase
preds.cpu()
for el in preds:
preds = el.item()
if (normalization == False) or (num_batch == 0):
data[preds,num_batch] = data[preds,num_batch]+1
else:
data[preds,num_batch] = data[preds,num_batch]+1/(num_batch)
#CONFUSION MATRIX
def update_confusion_matrix(matrix, preds, datas):
for pred, data in zip(preds,datas):
matrix[data.item(),pred.item()] = matrix[data.item(),pred.item()]+1
def new_confusion_matrix(lenx=100, leny=100):
matrix = np.zeros((leny, lenx))
return matrix
def show_confusion_matrix(matrix):
fig, ax = plt.subplots(figsize=(15,9))
ax = sns.heatmap(matrix, linewidth=0.2,cmap='Reds')
plt.savefig(f"cm_{matrix.shape[0]}.png") #Store the pic locally
plt.show()
def save(list_to_save, name_file):
with open(name_file + '.txt', 'w') as outfile:
outfile.write(str(list_to_save) + '\n')
def plot_metrics(x, y, stds, name, xlabel, ylabel, title):
"""
The function plots the metrics.
Params:
- x: independent variable to plot over the x axis
- y: lists of metrics to plot on y axis
- name: legend label for each y_i plotted
- label: name of the y axis
"""
color = [ 'salmon', 'lightskyblue', 'yellowgreen', 'tab:purple','tab:blue','darkorange','sandybrown', 'tab:cyan','coral', 'tab:olive']
plt.figure(figsize=(15, 10))
for yi, std, namei, c in zip(y,stds, name, color):
plt.errorbar(x=x, y=yi,yerr=std, color=c, markersize=10, linewidth=4, label = namei)
plt.legend(prop={'size': 22})
plt.title(title, loc='center', fontsize=26, fontweight=0, color='black')
plt.xlabel("Number of classes")
plt.ylabel(ylabel, fontsize=22)
plt.xlabel(xlabel, fontsize=22)
plt.grid()
plt.show()
def plot_weights(weights, step):
norm_weights = weights.norm(dim=0)
x = np.array([x_i for x_i in range(1, (step+1)*10 +1)])
my_color=np.where(x<=(step+1)*10 -10, 'skyblue', 'orange')
plt.figure(figsize=(15, 7))
plt.vlines(x,ymin = 0, ymax = norm_weights, color=my_color, alpha=0.9)
plt.title('Norm of weights in fully connected layer', fontsize=26)
plt.xticks([x_i for x_i in range(0,(step+1)*10+1 , 10)])
plt.xlabel('number of classes', fontsize=22)
plt.ylabel('Weights norm ', fontsize=22)
plt.savefig(f'weights_step{step}')
plt.show()
from sklearn.metrics import silhouette_score
def silhouette_exemplars(icarl):
"""
Computes the silhouette of the clustering of the exemplars in feature space.
Params:
icarl: the model as implemented by 'SubKluster - The candle' team
Returns:
a silhouette score.
The closer it is to 1, the better the exemplar sets are separated in the feature space.
"""
with torch.no_grad():
icarl.net.eval()
fts_exemplar = []
labels = []
for i, exemplar_set in enumerate(icarl.exemplar_sets):
dim_exemplar_set = exemplar_set.size()[0]
for exemplar in exemplar_set:
ft_map = icarl.net.feature_extractor(exemplar.to("cuda").unsqueeze(0)).squeeze().cpu()
# Append mapped exemplar and label
fts_exemplar.append(ft_map)
labels.append(i)
# Stack all mapped exemplars in a tensor, then turn it to numpy
fts_exemplar = torch.stack(fts_exemplar)
fts_exemplar = fts_exemplar.detach().cpu().numpy()
# Turn labels into numpy
y = np.array(labels)
# Compute a sexy silhouette
sil = silhouette_score(fts_exemplar, y)
return sil
def scatter_images(x, colors, human_readable_label):
sns.set_style('darkgrid')
sns.set_palette('muted')
sns.set_context("notebook", font_scale=1.5,
rc={"lines.linewidth": 2.5})
RS = 123
name = np.unique(colors)
# choose a color palette with seaborn.
num_classes = len(np.unique(colors))
palette = np.array(sns.color_palette("hls", num_classes))
# create a scatter plot.
f = plt.figure(figsize=(15, 10))
ax = plt.subplot(aspect='equal')
sc = ax.scatter(x[:,0], x[:,1], lw=0, s=40, c=palette[colors.astype(np.int)])
plt.xlim(-25, 25)
plt.ylim(-25, 25)
plt.grid()
ax.axis('off')
ax.axis('tight')
# add the labels for each digit corresponding to the label
txts = []
for i in range(num_classes):
# Position of each label at median of data points.
xtext, ytext = np.median(x[colors == i, :], axis=0) + 1
txt = ax.text(xtext, ytext, human_readable_label[i], fontsize=12)
txt.set_path_effects([
PathEffects.Stroke(linewidth=5, foreground="w"),
PathEffects.Normal()])
txts.append(txt)
plt.savefig(f"tnse_{len(name)}.png") #Store the pic locally
plt.show()
return f, ax, sc, txts
def create_tsne(icarl, human_readable_label):
"""
The function plots the t-sne representation for the exemplar set
Ex.
human_readable_label = cifar100.human_readable_label
create_tsne(icarl, human_readable_label)
Params:
net: the model chosen
human_readable_label: the names of the label assigned to each image
Return:
t-sne representation of image in 2 dimensions
"""
with torch.no_grad():
icarl.net.eval()
for i, exemplar_set in enumerate(icarl.exemplar_sets):
dim = exemplar_set.size()[0]
fts_exemplar = []
if i == 0:
all_images = []
for exemplar in exemplar_set:
ft_map = icarl.net.feature_extractor(exemplar.to("cuda").unsqueeze(0)).squeeze().cpu()
fts_exemplar.append(ft_map)
fts_exemplar = torch.stack(fts_exemplar)
if i == 0:
all_images = fts_exemplar
all_labels = np.full((dim), i)
else:
all_images = torch.cat((all_images, fts_exemplar), 0)
all_labels = np.concatenate((all_labels, np.full((dim), i)))
#Now I Have all_images and all_labels, I can start the reduce phase
fashion_tsne = TSNE().fit_transform(all_images.cpu().detach().numpy())
#Plot
f, ax, sc, txts = scatter_images(fashion_tsne, all_labels, human_readable_label)
return f, ax, sc, txts
def dump_model(model, filename):
"""
Saves in the filesystem a binary dump of the model given.
The dump can be restored later (hopefully).
Params:
model: the model, whole (e.g. instance of FrankenCaRL)
filename: file name as a string (no extension needed)
"""
with open(filename, 'wb') as picklefile:
pickle.dump(model, picklefile)
def load_model_from_pickle(filePath):
"""
Load and return a previously pickled model.
Params:
filePath: path of the binary file (result of a pickling operation)
Returns:
instance of the reconstrcted model (e.g. instance of FrankenCaRL)
"""
with open(filePath, 'rb') as inputfile:
outModel = pickle.load(inputfile)
return outModel