-
Notifications
You must be signed in to change notification settings - Fork 0
/
torchHelpers.py
437 lines (301 loc) · 14.1 KB
/
torchHelpers.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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
import time
import datetime
import logging
import torch
class Trainer:
"""
Trainer class for training a neural network model in PyTorch.
Parameters
----------
model : The neural network model to train.
lossFunction : The loss function to use for training.
optimizer : The optimizer to use for training.
device : The device to use for training. Can be either 'cpu', 'mps' or 'cuda'.
logLevel : The log level to use for logging. Can be one of the following:
logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL
Usage
-----
# create a trainer
trainer = Trainer(model, lossFunction, optimizer, device, logLevel=logging.INFO)
# train the model
trainer.train(trainLoader, valLoader, numberOfEpochs)
Author
------
Markus Enzweiler (markus.enzweiler@hs-esslingen.de)
"""
def __init__(self, model, lossFunction, optimizer, device, logLevel=logging.INFO):
self.model = model
self.lossFunction = lossFunction
self.optimizer = optimizer
self.device = device
self.haveValData = False
self.trainBatchSize = 0
self.valBatchSize = 0
# logging
self.logLevel = logLevel
self.logger = None
self.loggerStreamHandler = None
self._setupLogger()
# metrics, computed in each epoch
self.metrics = dict()
self.metrics["epochTrainLoss"] = []
self.metrics["epochValLoss"] = []
self.metrics["epochTrainAccuracy"] = []
self.metrics["epochValAccuracy"] = []
# timing
self.metrics["epochStartTime"] = None
self.metrics["epochEndTime"] = None
self.metrics["trainingStartTime"] = None
self.metrics["trainingEndTime"] = None
def _setupLogger(self):
logging.basicConfig(level = self.logLevel, force=True)
self.logger = logging.getLogger('Trainer')
self.loggerStreamHandler = logging.StreamHandler()
self.loggerStreamHandler.setLevel(self.logLevel)
formatter = logging.Formatter('%(message)s')
self.loggerStreamHandler.setFormatter(formatter)
self.logger.handlers.clear()
self.logger.addHandler(self.loggerStreamHandler)
self.logger.propagate = False
def _initMetrics(self, numEpochs):
self.metrics["epochTrainLoss" ] = [0.0] * numEpochs
self.metrics["epochValLoss" ] = [0.0] * numEpochs
self.metrics["epochTrainAccuracy"] = [0.0] * numEpochs
self.metrics["epochValAccuracy" ] = [0.0] * numEpochs
def _updateMetrics(self, epoch, numTrainSamples, numValSamples):
# average loss and accuracy
if numTrainSamples:
self.metrics["epochTrainLoss"][epoch] = self.metrics["epochTrainLoss"][epoch] / numTrainSamples
self.metrics["epochTrainAccuracy"][epoch] = self.metrics["epochTrainAccuracy"][epoch] / numTrainSamples
if self.haveValData and numValSamples:
self.metrics["epochValLoss"][epoch] = self.metrics["epochValLoss"][epoch] / numValSamples
self.metrics["epochValAccuracy"][epoch] = self.metrics["epochValAccuracy"][epoch] / numValSamples
def _logMetrics(self, epoch):
# log metrics
self.loggerStreamHandler.terminator = ""
self.logger.info(f'[Epoch {epoch:3}] : | ')
self.logger.info(f'time: {self.metrics["epochEndTime"] - self.metrics["epochStartTime"] :.3f}s | ')
self.logger.info(f'trainLoss: {self.metrics["epochTrainLoss"][epoch] :.3f} | ')
self.logger.info(f'trainAccuracy: {self.metrics["epochTrainAccuracy"][epoch] :.3f} | ')
if self.haveValData:
self.logger.info(f'valLoss: {self.metrics["epochValLoss"][epoch] :.3f} | ')
self.logger.info(f'valAccuracy: {self.metrics["epochValAccuracy"][epoch] :.3f} | ')
else:
self.logger.info(f'no validation data | ')
self.logger.info('\n')
def _onTrainBegin(self, numEpochs):
# Push the network model to the device we are using to train
self.model.to(self.device)
# init the metrics
self._initMetrics(numEpochs)
# start time
self.metrics["trainingStartTime"] = time.monotonic()
def _onTrainEnd(self, numEpochs):
# end time
self.metrics["trainingEndTime"] = time.monotonic()
timeDelta = datetime.timedelta(seconds=(self.metrics["trainingEndTime"] - self.metrics["trainingStartTime"]))
# log
self.loggerStreamHandler.terminator = "\n"
self.logger.info(f'Training finished in {str(timeDelta)} hh:mm:ss.ms')
def _onEpochBegin(self, epoch):
# start time of epoch
self.metrics["epochStartTime"] = time.monotonic()
# log info
self.loggerStreamHandler.terminator = " "
self.logger.info( f'[Epoch {epoch:3}] : ')
def _onEpochEnd(self, epoch, numTrainSamples, numValSamples, numBatches):
# end time of epoch
self.metrics["epochEndTime"] = time.monotonic()
# log info
self.loggerStreamHandler.terminator = "\n"
self.logger.info(f' done ({numBatches} batches)')
# update metrics
self._updateMetrics(epoch, numTrainSamples, numValSamples)
# log metrics
self._logMetrics(epoch)
def _computeAccuracy(self, outputs, labels):
# find predicted labels (the output neuron index with the highest output value)
_, predictedLabels = torch.max(outputs, 1)
return torch.sum(predictedLabels == labels).detach().cpu().numpy()
def _trainEpoch(self, epoch, trainLoader):
# loop over batches in the dataset
numBatches = 0
for i, data in enumerate(trainLoader, 0):
# get the training data : data is a list of [images, labels]
# and push the data to the device we are using
images, labels = data[0].to(self.device), data[1].to(self.device)
# zero the parameter gradients before the next data batch is processed
self.optimizer.zero_grad()
# forward pass of the batch
outputs = self.model(images)
# loss computation at the output of the network
loss = self.lossFunction(outputs, labels)
# backpropagate the loss through the network
loss.backward()
# optimize the network parameters
self.optimizer.step()
# accumulate train loss
self.metrics["epochTrainLoss"][epoch] += loss.item() * self.trainBatchSize
# compute and accumulate train accuracy
batchAccuracy = self._computeAccuracy(outputs, labels)
self.metrics["epochTrainAccuracy"][epoch] += batchAccuracy
if ((i % 100) == 0):
self.loggerStreamHandler.terminator = ""
self.logger.info ('.')
numBatches = numBatches + 1
return numBatches
def _testEpoch(self, epoch, valLoader):
# test on the validation data
if valLoader:
# we do not compute gradients in inference mode
with torch.no_grad():
# loop over validation data
for data in valLoader:
# push to device
images, labels = data[0].to(self.device), data[1].to(self.device)
# forward pass through the network
outputs = self.model(images)
# compute loss (just to report it)
loss = self.lossFunction(outputs, labels)
self.metrics["epochValLoss"][epoch] += loss.item() * self.valBatchSize
# compute accuracy
self.metrics["epochValAccuracy"][epoch] += self._computeAccuracy(outputs, labels)
def train(self, trainLoader, valLoader, numEpochs):
# main training method
# check training and validation data
if not (trainLoader and len(trainLoader) > 0):
msg = 'No training data available'
self.logger.error(msg)
raise Exception(msg)
if not (valLoader and len(valLoader) > 0):
self.haveValData = False
else:
self.haveValData = True
# number of train and validation samples
numTrainSamples = (len(trainLoader.dataset) if trainLoader else 0)
numValSamples = (len(valLoader.dataset) if valLoader else 0)
# batch sizes of train and validation loader
self.trainBatchSize = (trainLoader.batch_size if trainLoader else 0)
self.valBatchSize = (valLoader.batch_size if valLoader else 0)
# ------ Main training loop ------
# do some stuff at the beginning of the training
self._onTrainBegin(numEpochs)
# loop over the dataset in each epoch
for epoch in range(numEpochs):
# do some stuff at the beginning of each epoch
self._onEpochBegin(epoch)
# train an epoch on the training data
numBatches = self._trainEpoch(epoch, trainLoader)
# test on the validation data
if self.haveValData:
self._testEpoch(epoch, valLoader)
# do some stuff at the end of each epoch
self._onEpochEnd(epoch, numTrainSamples, numValSamples, numBatches)
# do some stuff at the end of the training
self._onTrainEnd(numEpochs)
class Tester:
"""
Tester class for testing a neural network model in PyTorch.
Parameters
----------
model : The neural network model to test.
device : The device to use for testing. Can be either 'cpu', 'mps' or 'cuda'.
logLevel : The log level to use for logging. Can be one of the following:
logging.DEBUG, logging.INFO, logging.WARNING, logging.ERROR, logging.CRITICAL
Usage
-----
# create a tester
tester = Tester(model, device, logLevel=logging.INFO)
# test the model
tester.test(testLoader)
Author
------
Markus Enzweiler (markus.enzweiler@hs-esslingen.de)
"""
def __init__(self, model, device, logLevel=logging.INFO):
self.model = model
self.device = device
self.testBatchSize = 0
# logging
self.logLevel = logLevel
self.logger = None
self.loggerStreamHandler = None
self._setupLogger()
# metrics
self.metrics = dict()
self.metrics["accuracy"] = 0.0
def _setupLogger(self):
logging.basicConfig(level = self.logLevel, force=True)
self.logger = logging.getLogger('Tester')
self.loggerStreamHandler = logging.StreamHandler()
self.loggerStreamHandler.setLevel(self.logLevel)
formatter = logging.Formatter('%(message)s')
self.loggerStreamHandler.setFormatter(formatter)
self.logger.handlers.clear()
self.logger.addHandler(self.loggerStreamHandler)
self.logger.propagate = False
def _initMetrics(self):
self.metrics["accuracy"] = 0.0
def _updateMetrics(self, numTestSamples):
# average accuracy
if numTestSamples:
self.metrics["accuracy"] = self.metrics["accuracy"] / numTestSamples
def _logMetrics(self, numTestSamples):
# log metrics
self.loggerStreamHandler.terminator = ""
self.logger.info('\n')
self.logger.info(f'Test Metrics ({numTestSamples} test samples):\n')
self.logger.info(f' - Accuracy: {self.metrics["accuracy"]:.3f}')
self.logger.info('\n')
def _onTestBegin(self):
# Push the network model to the device we are using to train
self.model.to(self.device)
# init the metrics
self._initMetrics()
# start time
self.metrics["testingStartTime"] = time.monotonic()
# logging output
self.loggerStreamHandler.terminator = ""
self.logger.info(f'Testing ')
def _onTestEnd(self, numTestSamples):
# update Metrics
self._updateMetrics(numTestSamples)
# log metrics
self._logMetrics(numTestSamples)
# end time
self.metrics["testingEndTime"] = time.monotonic()
timeDelta = datetime.timedelta(seconds=(self.metrics["testingEndTime"] - self.metrics["testingStartTime"]))
# log
self.loggerStreamHandler.terminator = "\n"
self.logger.info(f'Testing finished in {str(timeDelta)} hh:mm:ss.ms')
def _computeAccuracy(self, outputs, labels):
# find predicted labels (the output neuron index with the highest output value)
_, predictedLabels = torch.max(outputs, 1)
return torch.sum(predictedLabels == labels).detach().cpu().numpy()
def test(self, testLoader):
# number of test samples
numTestSamples = (len(testLoader.dataset) if testLoader else 0)
# batch size of test loader
self.testBatchSize = (testLoader.batch_size if testLoader else 0)
# ------ Main test loop ------
# do some stuff at the beginning of the training
self._onTestBegin()
if testLoader:
# we do not compute gradients in inference mode
with torch.no_grad():
# loop over test data
i = 0
for data in testLoader:
# push to device
images, labels = data[0].to(self.device), data[1].to(self.device)
# forward pass through the network
outputs = self.model(images)
# compute accuracy
self.metrics["accuracy"] += self._computeAccuracy(outputs, labels)
# status update
if ((i % 100) == 0):
self.loggerStreamHandler.terminator = ""
self.logger.info ('.')
i = i+1
# do some stuff at the end of the testing
self._onTestEnd(numTestSamples)