-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools.py
241 lines (184 loc) · 5.99 KB
/
tools.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
import os
from os.path import join
import matplotlib.pyplot as plt
from matplotlib.ticker import MaxNLocator
import numpy as np
import torch
from texttable import Texttable
_TRAIN = 'train'
_VALID = 'valid'
_VALENCE = 'valence'
_AROUSAL = 'arousal'
_AVG = 'avg_valence_arousal'
def print_confusion_mtx(cmtx: np.ndarray, int_to_cl: dict) -> str:
header_type = ['t']
keys = list(int_to_cl.keys())
h, w = cmtx.shape
# sometimes we drop a class like 'Other' (last class).
# assert len(keys) == h, f"{len(keys)} {h}"
# assert len(keys) == w, f"{len(keys)} {w}"
keys = sorted(keys, reverse=False)
t = Texttable()
t.set_max_width(400)
header = ['*']
for k in range(w):
header_type.append('f')
header.append(int_to_cl[k])
t.header(header)
t.set_cols_dtype(header_type)
t.set_precision(6)
for i in range(h):
row = [int_to_cl[i]]
for j in range(w):
row.append(cmtx[i, j])
t.add_row(row)
return t.draw()
def print_vector(vec: np.ndarray, int_to_cl: dict) -> str:
assert vec.ndim == 1, vec.ndim
header_type = []
keys = list(int_to_cl.keys())
n = vec.size
keys = sorted(keys, reverse=False)
t = Texttable()
t.set_max_width(400)
header = []
for i in range(n):
header_type.append('f')
header.append(int_to_cl[i])
t.header(header)
t.set_cols_dtype(header_type)
t.set_precision(6)
row = vec.flatten().tolist()
t.add_row(row)
return t.draw()
def plot_save_confusion_mtx(mtx: np.ndarray, fdout: str, name: str,
int_to_cl: dict, title: str):
if not os.path.isdir(fdout):
os.makedirs(fdout, exist_ok=True)
keys = list(int_to_cl.keys())
h, w = mtx.shape
assert len(keys) == h, f"{len(keys)} {h}"
assert len(keys) == w, f"{len(keys)} {w}"
keys = sorted(keys, reverse=False)
cls = [int_to_cl[k] for k in keys]
plt.close('all')
g = sns.heatmap(mtx, annot=True, cmap='Greens',
xticklabels=1, yticklabels=1)
g.set_xticklabels(cls, fontsize=7)
g.set_yticklabels(cls, rotation=0, fontsize=7)
plt.title(title, fontsize=7)
# plt.tight_layout()
plt.ylabel("True class", fontsize=7),
plt.xlabel("Predicted class", fontsize=7)
# disp.plot()
plt.savefig(join(fdout, f'{name}.png'), bbox_inches='tight', dpi=300)
plt.close('all')
class MyDataParallel(torch.nn.DataParallel):
def __getattr__(self, name):
try:
return super().__getattr__(name)
except AttributeError:
return getattr(self.module, name)
def state_dict_to_cpu(state_dict):
for k, v in state_dict.items():
state_dict[k] = v.cpu()
return state_dict
def state_dict_to_gpu(state_dict, device=None):
for k, v in state_dict.items():
if device is None:
v = v.cuda()
else:
v = v.to(device)
state_dict[k] = v
return state_dict
def fmsg(msg, upper=True):
"""
Format message.
:param msg:
:param upper:
:return:
"""
if upper:
msg = msg.upper()
n = min(120, max(80, len(msg)))
top = "\n" + "=" * n
middle = " " * (int(n / 2) - int(len(msg) / 2)) + " {}".format(msg)
bottom = "=" * n + "\n"
output_msg = "\n".join([top, middle, bottom])
return output_msg
def plot_tracker(tracker: dict,
fdout: str,
dpi=300
):
epochs = list(range(len(tracker[_TRAIN][_VALENCE])))
best_idx = tracker[_TRAIN]['best_idx']
lambda_vals = np.asarray(epochs)
font_sz = 7
shift = 5 / 100. # how much to shift ticks top and bottom when
# plotting values.
# fig, ax1 = plt.subplots()
fig, ax1 = plt.subplots(1, 1, figsize=(5, 2.5))
start = 0
z = lambda_vals.size
x = list(range(start, z + start, 1))
# Valid
linewidth = 1.
color_1 = 'tab:red'
color_2 = 'tab:blue'
lns1 = ax1.plot(x,
tracker[_VALID][_VALENCE],
color=color_1,
label='Valid / Valence',
linewidth=linewidth
)
lns2 = ax1.plot(x,
tracker[_VALID][_AROUSAL],
color=color_2,
label='Valid / Arousal',
linewidth=linewidth
)
# Train
lns3 = ax1.plot(x,
tracker[_TRAIN][_VALENCE],
color=color_1,
label='Train / Valence',
linestyle='dashed',
linewidth=linewidth / 2,
alpha=0.2
)
lns4 = ax1.plot(x,
tracker[_TRAIN][_AROUSAL],
color=color_2,
label='Train / Arousal',
linestyle='dashed',
linewidth=linewidth / 2.,
alpha=0.2
)
for subset in tracker:
for k in [_AROUSAL, _VALENCE]:
ax1.plot([best_idx], [tracker[subset][k][best_idx]],
marker='o',
markersize=2,
color="red"
)
ax1.set_xlabel('Epochs', fontsize=font_sz)
ax1.set_ylabel('Valence, Arousal', fontsize=font_sz)
plt.title('Train / valid performance', fontsize=font_sz)
xticks = []
for v_ in epochs:
if v_ < 1:
xticks.append(str(v_))
else:
xticks.append(str(int(v_)))
ax1.set_xticks(x, xticks)
ax1.xaxis.set_tick_params(labelsize=4)
ax1.grid(b=True, which='major', linestyle='-', alpha=0.2)
ax1.tick_params(axis='y', labelsize=font_sz)
ax1.xaxis.set_major_locator(MaxNLocator(integer=True))
lns = lns1 + lns2 + lns3 + lns4
labs = [l.get_label() for l in lns]
ax1.legend(lns, labs, loc='lower center', prop={'size': 6, 'weight':'bold'})
os.makedirs(fdout, exist_ok=True)
fig.savefig(join(fdout, 'tracker.png'), bbox_inches='tight', dpi=dpi)
plt.close('all')
del fig