-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneural_net_model_train.py
350 lines (229 loc) · 11.1 KB
/
neural_net_model_train.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
import torch
import torch.nn as nn
import torchmetrics
#from tqdm import tqdm
from tqdm.auto import tqdm
#from tqdm.notebook import tqdm
import numpy as np
import os, random
if torch.cuda.is_available():
torch.cuda.empty_cache()
DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print("DEVICE found: ", DEVICE)
number_of_classes=15
print("number of classes: ", number_of_classes)
def seed_everything(seed=42):
os.environ["PYTHONHASHSEED"] = str(seed)
random.seed(seed)
np.random.seed(seed)
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.enabled = True
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = True
seed_everything()
class ModelsEvaluations:
def __init__(self):
self.accuracies_scores = []
self.accuracy = torchmetrics.Accuracy().to(DEVICE)
self.f1_scores = []
self.f1_score = torchmetrics.F1Score(num_classes=number_of_classes, average="weighted").to(DEVICE)
self.auroc_scores = []
self.auroc = torchmetrics.AUROC(num_classes=number_of_classes).to(DEVICE)
self.mean_metric = torchmetrics.MeanMetric(nan_strategy="warn")
def updateEvaluationEpochStep(self, outputs_res, batch_y):
self.accuracy.update(outputs_res, batch_y.to(dtype=torch.int32))
self.f1_score.update(outputs_res, batch_y.to(dtype=torch.int32))
self.auroc.update(outputs_res, batch_y.to(dtype=torch.int32))
def getEvaluationEpoch(self):
_accuracy_epoch = self.accuracy.compute()
self.accuracies_scores.append(_accuracy_epoch)
self.accuracy.reset()
_f1_score_epoch = self.f1_score.compute()
self.f1_scores.append(_f1_score_epoch)
self.f1_score.reset()
_auroc_score_epoch = self.auroc.compute()
self.auroc_scores.append(_auroc_score_epoch)
self.auroc.reset()
return _accuracy_epoch, _f1_score_epoch, _auroc_score_epoch
def getEvaluationModelTrainning(self):
return self.accuracies_scores, self.f1_scores , self.auroc_scores
def reset(self):
self.accuracies_scores = []
self.f1_scores = []
self.auroc_scores = []
def computeMean(self, values):
self.mean_metric.update(values)
mean_value = self.mean_metric.compute()
self.mean_metric.reset()
return mean_value
class ModelsTrainning:
def __init__(self, train_loader: torch.utils.data.DataLoader,
test_loader: torch.utils.data.DataLoader,
max_epochs):
self.train_loader = train_loader
self.test_loader = test_loader
self.max_epochs = max_epochs
def setParameters(
self,
net_model: nn.Module,
optimizer,
trial_optimisation=None,
evaluation_function=None,
):
self.net_model = net_model
self.optimizer = optimizer
self.trial_optimisation = trial_optimisation
self.evaluation_function = evaluation_function
self.modelsEvaluations = ModelsEvaluations()
def prune_trial(self, epoch, accuracy_epoch, losses_epoch, f1_score_epoch):
import optuna
# Add prune mechanism
if self.trial_optimisation and self.evaluation_function:
if self.evaluation_function == "accuracy":
self.trial_optimisation.report(accuracy_epoch, epoch)
if self.trial_optimisation.should_prune():
raise optuna.exceptions.TrialPruned()
elif self.evaluation_function == "loss":
self.trial_optimisation.report(1.0 - losses_epoch, epoch)
if self.trial_optimisation.should_prune():
raise optuna.exceptions.TrialPruned()
elif self.evaluation_function == "f1_score":
self.trial_optimisation.report(f1_score_epoch, epoch)
if self.trial_optimisation.should_prune():
raise optuna.exceptions.TrialPruned()
else:
print("test_evaluation_function unknown")
def get_evaluation_result(self,accuracy_score,loss_score,f1_score):
if self.evaluation_function == "accuracy":
return accuracy_score
elif self.evaluation_function == "loss":
return loss_score
elif self.evaluation_function == "f1_score":
return f1_score
def trainNetModel(self):
MAX_EPOCHS = self.max_epochs
criterion = nn.CrossEntropyLoss().to(DEVICE)
losses = []
losses_epoch = []
self.net_model.to(DEVICE)
self.net_model.train()
for epoch in range(MAX_EPOCHS):
for step, (batch_x, batch_y) in enumerate(tqdm(self.train_loader, position=0, leave=True), 0):
batch_x, batch_y = batch_x.to(DEVICE, non_blocking=True), batch_y.to(
DEVICE, non_blocking=True
)
outputs_res = self.net_model(batch_x)
# assert not torch.isnan(outputs_res).any()
loss = criterion(outputs_res, batch_y.long())
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
losses_epoch.append(np.around(loss.item(), decimals=3))
self.modelsEvaluations.updateEvaluationEpochStep(outputs_res, batch_y)
_accuracy_epoch, _f1_score_epoch, _auroc_score_epoch = self.modelsEvaluations.getEvaluationEpoch()
_losses_epoch = self.modelsEvaluations.computeMean(losses_epoch)
losses_epoch=[]
# Add prune mechanism
self.prune_trial(epoch, _accuracy_epoch, _losses_epoch, _f1_score_epoch)
losses.append(_losses_epoch)
accuracies_scores, f1_scores, auroc_scores = self.modelsEvaluations.getEvaluationModelTrainning()
self.modelsEvaluations.reset()
return accuracies_scores, losses, f1_scores, auroc_scores
def getModelCheckpoint_AmpOptimized(self):
checkpoint = {
"model": self.net_model.state_dict(),
"optimizer": self.optimizer.state_dict(),
"scaler": self.scaler.state_dict(),
}
return checkpoint
def getModelCheckpoint(self):
checkpoint = {
"model": self.net_model.state_dict(),
"optimizer": self.optimizer.state_dict(),
}
return checkpoint
# use if Tensor Core is present
def trainNetModel_AmpOptimized(self):
# Necessary for FP16
self.scaler = scaler = torch.cuda.amp.GradScaler()
MAX_EPOCHS = self.max_epochs
criterion = nn.CrossEntropyLoss().to(DEVICE)
losses = []
losses_epoch = []
self.net_model.to(DEVICE)
self.net_model.train()
for epoch in range(MAX_EPOCHS):
for step, (batch_x, batch_y) in enumerate(tqdm(self.train_loader, position=0, leave=True), 0):
batch_x, batch_y = batch_x.to(DEVICE, non_blocking=True), batch_y.to(
DEVICE, non_blocking=True
)
with torch.cuda.amp.autocast():
outputs_res = self.net_model(batch_x)
# assert not torch.isnan(outputs_res).any()
loss = criterion(outputs_res, batch_y.to(dtype=torch.long))
self.optimizer.zero_grad()
scaler.scale(loss).backward()
scaler.step(self.optimizer)
scaler.update()
losses_epoch.append(np.around(loss.item(), decimals=3))
self.modelsEvaluations.updateEvaluationEpochStep(outputs_res, batch_y)
_accuracy_epoch, _f1_score_epoch, _auroc_score_epoch = self.modelsEvaluations.getEvaluationEpoch()
_losses_epoch = self.modelsEvaluations.computeMean(losses_epoch)
losses_epoch=[]
# Add prune mechanism
self.prune_trial(epoch, _accuracy_epoch, _losses_epoch, _f1_score_epoch)
losses.append(_losses_epoch)
accuracies_scores, f1_scores, auroc_scores = self.modelsEvaluations.getEvaluationModelTrainning()
self.modelsEvaluations.reset()
return accuracies_scores, losses, f1_scores, auroc_scores
def testNetModel(self):
criterion = torch.nn.CrossEntropyLoss().to(DEVICE)
self.net_model.to(DEVICE)
self.net_model.eval()
losses_epoch = []
for step, (batch_x, batch_y) in enumerate(tqdm(self.test_loader, position=0, leave=True), 0):
# send to device
batch_x, batch_y = batch_x.to(DEVICE, non_blocking=True), batch_y.to(
DEVICE, non_blocking=True
)
outputs_res = self.net_model(batch_x)
# assert not torch.isnan(outputs).any()
loss = criterion(outputs_res, batch_y.long())
losses_epoch.append(np.around(loss.item(), decimals=3))
self.modelsEvaluations.updateEvaluationEpochStep(outputs_res, batch_y)
self.modelsEvaluations.getEvaluationEpoch()
loss_score = self.modelsEvaluations.computeMean(losses_epoch)
losses_epoch=[]
accuracies_scores, f1_scores, auroc_scores = self.modelsEvaluations.getEvaluationModelTrainning()
self.modelsEvaluations.reset()
print(
"\nTest set: Average loss: {:.4f}, Average accuracy: {:.4f}%,\n \t Average f1_score: {:.4f}, Average Area Under ROC: {:.4f} \n".format(
loss_score.item(), accuracies_scores[0].item(), f1_scores[0].item() , auroc_scores[0].item()
)
)
return self.get_evaluation_result(accuracies_scores[0],
loss_score,
f1_scores[0])
def testNetModel(net_model, test_loader, modelsEvaluations = ModelsEvaluations()):
criterion = torch.nn.CrossEntropyLoss().to(DEVICE)
net_model.to(DEVICE)
net_model.eval()
losses_epoch = []
for step, (batch_x, batch_y) in enumerate(tqdm(test_loader, position=0, leave=True), 0):
# send to device
batch_x, batch_y = batch_x.to(DEVICE, non_blocking=True), batch_y.to(
DEVICE, non_blocking=True
)
outputs_res = net_model(batch_x)
# assert not torch.isnan(outputs).any()
loss = criterion(outputs_res, batch_y.long())
losses_epoch.append(np.around(loss.item(), decimals=3))
modelsEvaluations.updateEvaluationEpochStep(outputs_res, batch_y)
modelsEvaluations.getEvaluationEpoch()
loss_score = modelsEvaluations.computeMean(losses_epoch)
losses_epoch=[]
accuracies_scores, f1_scores, auroc_scores = modelsEvaluations.getEvaluationModelTrainning()
modelsEvaluations.reset()
return loss_score.item(), accuracies_scores[0].item(), f1_scores[0].item() , auroc_scores[0].item()