Repository navigation
Expand file tree
/
Copy pathutils.py
More file actions
293 lines (241 loc) · 10.1 KB
/
Copy pathutils.py
File metadata and controls
293 lines (241 loc) · 10.1 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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import os
import sys
import time
import torch
import random
import numpy as np
import sklearn.metrics as skmet
import shutil
from terminaltables import SingleTable
from termcolor import colored
def get_terminal_size():
size = shutil.get_terminal_size()
return size.lines, size.columns
_, term_width = get_terminal_size()
term_width = int(term_width)
TOTAL_BAR_LENGTH = 25.
last_time = time.time()
begin_time = last_time
def progress_bar(current, total, msg=None):
global last_time, begin_time
if current == 0:
begin_time = time.time() # Reset for new bar.
cur_len = int(TOTAL_BAR_LENGTH*current/total)
rest_len = int(TOTAL_BAR_LENGTH - cur_len) - 1
sys.stdout.write(' [')
for i in range(cur_len):
sys.stdout.write('=')
sys.stdout.write('>')
for i in range(rest_len):
sys.stdout.write('.')
sys.stdout.write(']')
cur_time = time.time()
step_time = cur_time - last_time
last_time = cur_time
tot_time = cur_time - begin_time
L = []
L.append(' Step: %s' % format_time(step_time))
L.append(' | Tot: %s' % format_time(tot_time))
if msg:
L.append(' | ' + msg)
msg = ''.join(L)
sys.stdout.write(msg)
for i in range(term_width-int(TOTAL_BAR_LENGTH)-len(msg)-3):
sys.stdout.write(' ')
# Go back to the center of the bar.
for i in range(term_width-int(TOTAL_BAR_LENGTH/2)+2):
sys.stdout.write('\b')
sys.stdout.write(' %d/%d ' % (current+1, total))
if current < total-1:
sys.stdout.write('\r')
else:
sys.stdout.write('\n')
sys.stdout.flush()
def format_time(seconds):
days = int(seconds / 3600/24)
seconds = seconds - days*3600*24
hours = int(seconds / 3600)
seconds = seconds - hours*3600
minutes = int(seconds / 60)
seconds = seconds - minutes*60
secondsf = int(seconds)
seconds = seconds - secondsf
millis = int(seconds*1000)
f = ''
i = 1
if days > 0:
f += str(days) + 'D'
i += 1
if hours > 0 and i <= 2:
f += str(hours) + 'h'
i += 1
if minutes > 0 and i <= 2:
f += str(minutes) + 'm'
i += 1
if secondsf > 0 and i <= 2:
f += str(secondsf) + 's'
i += 1
if millis > 0 and i <= 2:
f += str(millis) + 'ms'
i += 1
if f == '':
f = '0ms'
return f
def get_lr(optimizer):
for param_group in optimizer.param_groups:
return param_group['lr']
class EarlyStopping:
"""Early stops the training if validation loss doesn't improve after a given patience."""
def __init__(self, patience=7, verbose=False, delta=0, ckpt_path='./checkpoints', ckpt_name='checkpoint.pth', mode='min'):
"""
Args:
patience (int): How long to wait after last time validation loss improved.
Default: 7
verbose (bool): If True, prints a message for each validation loss improvement.
Default: False
delta (float): Minimum change in the monitored quantity to qualify as an improvement.
Default: 0
path (str): Path for the checkpoint to be saved to.
Default: 'checkpoint.pt'
"""
self.patience = patience
self.verbose = verbose
self.counter = 0
self.best_score = None
self.early_stop = False
self.mode = mode
if mode == 'max':
self.init_metric = 0
elif mode == 'min':
self.init_metric = -np.inf
else:
raise NotImplementedError
self.delta = delta
self.ckpt_path = ckpt_path
self.ckpt_name = ckpt_name if '.pth' in ckpt_name else ckpt_name + '.pth'
os.makedirs(self.ckpt_path, exist_ok=True)
def __call__(self, val_acc, val_loss, model):
if self.mode == 'max':
score = val_acc
val_metric = val_acc
elif self.mode == 'min':
score = -val_loss
val_metric = val_loss
else:
raise NotImplementedError
if self.best_score is None:
self.best_score = score
self.save_checkpoint(val_metric, model)
elif score < self.best_score + self.delta:
self.counter += 1
print(f'EarlyStopping counter: {self.counter} out of {self.patience}. Having score: {score}. Current best_score/delta {self.best_score}/{self.delta}\n')
if self.counter >= self.patience:
self.early_stop = True
else:
self.best_score = score
self.save_checkpoint(val_metric, model)
self.counter = 0
def save_checkpoint(self, val_metric, model):
'''Saves model when validation loss decrease.'''
if self.verbose:
if self.mode == 'max':
print(f'[INFO] Validation accuracy increased ({self.init_metric:.6f} --> {val_metric:.6f}). Saving model ...\n')
elif self.mode == 'min':
print(f'[INFO] Validation loss decreased ({self.init_metric:.6f} --> {val_metric:.6f}). Saving model ...\n')
else:
raise NotImplementedError
torch.save(model.state_dict(), os.path.join(self.ckpt_path, self.ckpt_name))
self.init_metric = val_metric
def summarize_result(config, fold, y_true, y_pred, save=True):
os.makedirs('results', exist_ok=True)
y_pred_argmax = np.argmax(y_pred, 1)
result_dict = skmet.classification_report(y_true, y_pred_argmax, digits=3, output_dict=True)
cm = skmet.confusion_matrix(y_true, y_pred_argmax)
accuracy = round(result_dict['accuracy']*100, 1)
macro_f1 = round(result_dict['macro avg']['f1-score']*100, 1)
kappa = round(skmet.cohen_kappa_score(y_true, y_pred_argmax), 3)
wpr = round(result_dict['0.0']['precision']*100, 1)
wre = round(result_dict['0.0']['recall']*100, 1)
wf1 = round(result_dict['0.0']['f1-score']*100, 1)
n1pr = round(result_dict['1.0']['precision']*100, 1)
n1re = round(result_dict['1.0']['recall']*100, 1)
n1f1 = round(result_dict['1.0']['f1-score']*100, 1)
n2pr = round(result_dict['2.0']['precision']*100, 1)
n2re = round(result_dict['2.0']['recall']*100, 1)
n2f1 = round(result_dict['2.0']['f1-score']*100, 1)
n3pr = round(result_dict['3.0']['precision']*100, 1)
n3re = round(result_dict['3.0']['recall']*100, 1)
n3f1 = round(result_dict['3.0']['f1-score']*100, 1)
rpr = round(result_dict['4.0']['precision']*100, 1)
rre = round(result_dict['4.0']['recall']*100, 1)
rf1 = round(result_dict['4.0']['f1-score']*100, 1)
overall_data = [
['ACC', 'MF1', '\u03BA'],
[accuracy, macro_f1, kappa],
]
perclass_data = [
[colored('A', 'cyan') + '\\' + colored('P', 'green'), 'W', 'N1', 'N2', 'N3', 'R', 'PR', 'RE', 'F1'],
['W', cm[0][0], cm[0][1], cm[0][2], cm[0][3], cm[0][4], wpr, wre, wf1],
['N1', cm[1][0], cm[1][1], cm[1][2], cm[1][3], cm[1][4], n1pr, n1re, n1f1],
['N2', cm[2][0], cm[2][1], cm[2][2], cm[2][3], cm[2][4], n2pr, n2re, n2f1],
['N3', cm[3][0], cm[3][1], cm[3][2], cm[3][3], cm[3][4], n3pr, n3re, n3f1],
['R', cm[4][0], cm[4][1], cm[4][2], cm[4][3], cm[4][4], rpr, rre, rf1],
]
overall_dt = SingleTable(overall_data, colored('OVERALL RESULT', 'red'))
perclass_dt = SingleTable(perclass_data, colored('PER-CLASS RESULT', 'red'))
print('\n[INFO] Evaluation result from fold 1 to {}'.format(fold))
print('\n' + overall_dt.table)
print('\n' + perclass_dt.table)
print(colored(' A', 'cyan') + ': Actual Class, ' + colored('P', 'green') + ': Predicted Class' + '\n\n')
if save:
with open(os.path.join('results', config['name'] + '.txt'), 'w') as f:
f.write(
str(fold) + ' ' +
str(round(result_dict['accuracy']*100, 1)) + ' ' +
str(round(result_dict['macro avg']['f1-score']*100, 1)) + ' ' +
str(round(kappa, 3)) + ' ' +
str(round(result_dict['0.0']['f1-score']*100, 1)) + ' ' +
str(round(result_dict['1.0']['f1-score']*100, 1)) + ' ' +
str(round(result_dict['2.0']['f1-score']*100, 1)) + ' ' +
str(round(result_dict['3.0']['f1-score']*100, 1)) + ' ' +
str(round(result_dict['4.0']['f1-score']*100, 1)) + ' '
)
def set_random_seed(seed_value, use_cuda=True):
np.random.seed(seed_value) # cpu vars
torch.manual_seed(seed_value) # cpu vars
random.seed(seed_value) # Python
os.environ['PYTHONHASHSEED'] = str(seed_value) # Python hash buildin
if use_cuda:
torch.cuda.manual_seed(seed_value)
torch.cuda.manual_seed_all(seed_value) # gpu vars
torch.backends.cudnn.deterministic = True #needed
torch.backends.cudnn.benchmark = False
def get_device(preference="cuda"):
"""
Get the preferred PyTorch device based on availability. If cpu is chosen as preference, use it directly.
When hardware acceleration is given as preference, check for availability of preference. If the preference is not
available, try to use the other hardware acceleration. If this is not available either, fallback to CPU.
Parameters:
- preference (str): "cuda", "mps" or "cpu". The preferred hardware accelerator.
Returns:
- torch.device: The selected device (cuda, mps, or cpu).
"""
if preference not in ["cuda", "mps", "cpu"]:
raise ValueError("Preference must be 'cuda', 'mps' or 'cpu'")
if preference == "cpu":
return torch.device("cpu")
# Check if CUDA or MPS are available
cuda_available = torch.cuda.is_available()
mps_available = torch.backends.mps.is_available()
# Choose the preferred device if available
if preference == "cuda" and cuda_available:
return torch.device("cuda")
elif preference == "mps" and mps_available:
return torch.device("mps")
# Fallback to the other device if available
if preference == "cuda" and mps_available:
return torch.device("mps")
elif preference == "mps" and cuda_available:
return torch.device("cuda")
# Fallback to CPU
return torch.device("cpu")