fyrizi commited on
Commit
a3783fb
·
verified ·
1 Parent(s): d43c882

Delete main/src/outputs.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. main/src/outputs.py +0 -745
main/src/outputs.py DELETED
@@ -1,745 +0,0 @@
1
- import warnings
2
- from typing import Callable, Any, List
3
-
4
- import torch
5
- import functools
6
- import collections
7
- import torch.nn as nn
8
- import torch.nn.functional as F
9
- import metrics
10
- from sequence_array import sample_uid_name as default_sample_uid_name
11
- import losses
12
- from flatten import flatten
13
- from losses import LossDiceMulticlass, LossFocalMulticlass
14
-
15
-
16
- def len_batch(batch):
17
- """
18
-
19
- Args:
20
- batch: a data split or a `collections.Sequence`
21
-
22
- Returns:
23
- the number of elements within a data split
24
- """
25
- if isinstance(batch, (collections.Sequence, torch.Tensor)):
26
- return len(batch)
27
-
28
- assert isinstance(batch, collections.Mapping), 'Must be a dict-like structure! got={}'.format(type(batch))
29
-
30
- for name, values in batch.items():
31
- if isinstance(values, (list, tuple)):
32
- return len(values)
33
- if isinstance(values, torch.Tensor) and len(values.shape) != 0:
34
- return values.shape[0]
35
- if isinstance(values, np.ndarray) and len(values.shape) != 0:
36
- return values.shape[0]
37
- return 0
38
-
39
-
40
- def to_value(v):
41
- """
42
- Convert where appropriate from tensors to numpy arrays
43
-
44
- Args:
45
- v: an object. If ``torch.Tensor``, the tensor will be converted to a numpy
46
- array. Else returns the original ``v``
47
-
48
- Returns:
49
- ``torch.Tensor`` as numpy arrays. Any other type will be left unchanged
50
- """
51
- if isinstance(v, torch.Tensor):
52
- return v.cpu().data.numpy()
53
- return v
54
-
55
-
56
- def dict_torch_values_to_numpy(d):
57
- """
58
- Transform all torch.Tensor to numpy arrays of a dictionary like object
59
- """
60
- if d is None:
61
- return
62
- assert isinstance(d, collections.Mapping), 'must be a dict like object'
63
-
64
- for name, value in d.items():
65
- if isinstance(value, torch.Tensor):
66
- d[name] = to_value(value)
67
-
68
-
69
- class Output:
70
- """
71
- This is a tag name to find the output reference back from `outputs`
72
- """
73
- output_ref_tag = 'output_ref'
74
-
75
- def __init__(self, metrics, output, criterion_fn, collect_output=False, sample_uid_name=None):
76
- """
77
- :param metrics: the metrics to be reported for each output
78
- :param output: a `torch.Tensor` to be recorded
79
- :param criterion_fn: the criterion function to be used to evaluate the output
80
- :param collect_output: if True, the output values will be collected (and possibly exported for debug purposes)
81
- :pram sample_uid_name: collect sample UID along with the output
82
- """
83
- self.output = output
84
- self.criterion_fn = criterion_fn
85
- self.collect_output = collect_output
86
- self.metrics = metrics
87
-
88
- # this can be used to collect the UIDs of the sample the output was calculated from.
89
- # this can be particularly useful for various tasks: track data augmentation,
90
- self.sample_uid_name = sample_uid_name
91
-
92
- def evaluate_batch(self, batch, is_training):
93
- """
94
- Evaluate a batch of data and extract important outputs
95
- :param batch: the batch of data
96
- :param is_training: if True, this was a training batch
97
- :return: tuple(a dictionary of values, dictionary of metrics)
98
- """
99
- assert 0, 'this needs to be implemented in derived classes!'
100
-
101
- def loss_term_cleanup(self, loss_term):
102
- """
103
- This function is called for each batch just before switching to another batch.
104
-
105
- It can be used to clean up large arrays stored or release CUDA memory
106
- """
107
- dict_torch_values_to_numpy(loss_term)
108
- metrics_results = loss_term.get('metrics_results')
109
- if metrics_results is not None:
110
- dict_torch_values_to_numpy(metrics_results)
111
-
112
-
113
- def extract_metrics(metrics_outputs, outputs):
114
- """
115
- Extract metrics from an output
116
-
117
- Args:
118
- metrics_outputs: a list of metrics
119
- outputs: the result of `Output.evaluate_batch`
120
-
121
- Returns:
122
- a dictionary of key, value
123
- """
124
- history = collections.OrderedDict()
125
- if metrics_outputs is not None:
126
- for metric in metrics_outputs:
127
- metric_result = metric(outputs)
128
- if metric_result is not None:
129
- assert isinstance(metric_result, collections.Mapping), 'must be a dict like structure'
130
- history.update({metric: metric_result})
131
- return history
132
-
133
-
134
- class OutputEmbedding(Output):
135
- """
136
- Represent an embedding
137
-
138
- This is only used to record a tensor that we consider an embedding (e.g., to be exported to tensorboard)
139
- """
140
- def __init__(self, output, clean_loss_term_each_batch=False, sample_uid_name=default_sample_uid_name, functor=None):
141
- """
142
-
143
- Args:
144
- output: the output from which the embedding will be created
145
- clean_loss_term_each_batch: if ``True``, the loss term output will be removed from the output in
146
- order to free memory just before the next batch. For example, if we want to collect statistics
147
- on the embedding, we do not need to keep track of the output embedding and in particular for
148
- large embeddings. If ``False``, the output will not be cleaned (and possibly accumulated
149
- for the whole epoch)
150
- sample_uid_name: UID name to be used for collecting the embedding of the samples
151
- functor: apply a function on the output to create the embedding
152
- """
153
- super().__init__(
154
- output=output,
155
- criterion_fn=None,
156
- collect_output=True,
157
- sample_uid_name=sample_uid_name,
158
- metrics=None)
159
- self.clean_loss_term_each_batch = clean_loss_term_each_batch
160
- self.functor = functor
161
-
162
- def evaluate_batch(self, batch, is_training):
163
- loss_term = collections.OrderedDict()
164
-
165
- # do not keep track of GPU torch.Tensor, specially for large embeddings.
166
- # ``clean_loss_term_each_batch`` may be ``False``, so the output
167
- # would never be cleaned up.
168
- self.output = to_value(self.output)
169
- if self.functor is not None:
170
- self.output = self.functor(self.output)
171
-
172
- loss_term['output'] = self.output
173
- loss_term[Output.output_ref_tag] = self # keep a back reference
174
- return loss_term
175
-
176
- def loss_term_cleanup(self, loss_term):
177
- if self.clean_loss_term_each_batch:
178
- del loss_term['output']
179
- self.output = None
180
-
181
-
182
- def segmentation_criteria_ce_dice(output, truth, per_voxel_weights=None, ce_weight=0.5, per_class_weights=None, power=1.0, smooth=1.0, focal_gamma=None):
183
- """
184
- loss combining cross entropy and multi-class dice
185
-
186
- Args:
187
- output: the output value, with shape [N, C, Dn...D0]
188
- truth: the truth, with shape [N, 1, Dn..D0]
189
- ce_weight: the weight of the cross entropy to use. This controls the importance of the
190
- cross entropy loss to the overall segmentation loss. Range in [0..1]
191
- per_class_weights: the weight per class. A 1D vector of size C indicating the weight of the classes. This
192
- will be used for the cross-entropy loss
193
- per_voxel_weights: the weight of each truth voxel. Must be of shape [N, Dn..D0]
194
-
195
- Returns:
196
- a torch tensor
197
- """
198
- assert per_class_weights is None or len(per_class_weights) == output.shape[1], f'per_class_weights must have a' \
199
- f'weight per class. ' \
200
- f'Got={len(per_class_weights)}, ' \
201
- f'expected={output.shape[1]}'
202
-
203
- assert per_voxel_weights is None or per_voxel_weights.shape == truth.shape, 'incorrect per-voxel weight'
204
- assert truth.shape[1] == 1, f'expected a single channel! Got={truth.shape[1]}'
205
-
206
- if ce_weight > 0:
207
- if focal_gamma is None or focal_gamma <= 0:
208
- if output.shape[1] == 1:
209
- cross_entropy_loss = nn.BCEWithLogitsLoss(reduction='none', weight=per_class_weights)(output, truth.float())
210
- else:
211
- cross_entropy_loss = nn.CrossEntropyLoss(reduction='none', weight=per_class_weights)(output, truth.squeeze(1))
212
- else:
213
- cross_entropy_loss = LossFocalMulticlass(gamma=focal_gamma)(output, truth)
214
-
215
- if per_voxel_weights is not None:
216
- cross_entropy_loss = cross_entropy_loss * per_voxel_weights
217
- cross_entropy_loss = cross_entropy_loss.mean(tuple(range(1, len(cross_entropy_loss.shape))))
218
- else:
219
- cross_entropy_loss = 0
220
-
221
- if ce_weight < 1:
222
- dice_loss = losses.LossDiceMulticlass(power=power, smooth=smooth)(output, truth)
223
- else:
224
- dice_loss = 0
225
-
226
- loss = ce_weight * cross_entropy_loss + (1 - ce_weight) * dice_loss
227
- return loss
228
-
229
-
230
- def criterion_softmax_cross_entropy(output, output_truth):
231
- assert len(output.shape) == len(output_truth.shape), '`output` and `output_truth` must have the same dimensionality'
232
- assert output_truth.shape[1] == 1, 'truth must have a single channel'
233
- assert output_truth.shape[2:] == output.shape[2:], 'all the input must be covered by truth'
234
- assert output.shape[1] >= 2, 'output must have N channels, one for each class! (else binary version should be used!)'
235
- #return F.cross_entropy(output, output_truth.squeeze(1), reduction='none')
236
- return nn.CrossEntropyLoss(reduction='none')(output, output_truth.squeeze(1))
237
-
238
-
239
- class OutputClassification(Output):
240
- """
241
- Classification output
242
- """
243
- def __init__(
244
- self,
245
- output,
246
- output_truth,
247
- *,
248
- criterion_fn=lambda: criterion_softmax_cross_entropy,
249
- collect_output=True,
250
- collect_only_non_training_output=False,
251
- metrics: List[metrics.Metric] = metrics.default_classification_metrics(),
252
- loss_reduction=torch.mean,
253
- weights=None,
254
- per_voxel_weights=None,
255
- loss_scaling=1.0,
256
- output_postprocessing=functools.partial(torch.argmax, dim=1, keepdim=True), # =1 as we export the class
257
- maybe_optional=False,
258
- classes_name='unknown',
259
- sample_uid_name=default_sample_uid_name):
260
- """
261
-
262
- Args:
263
- output: the raw output values (no activation applied, i.e., logits). Should be of shape [N, C, ...]
264
- output_truth: the tensor to be used as target. Should be of shape [N, C, ...] and be
265
- compatible with ``criterion_fn``
266
- criterion_fn: the criterion to minimize between the output and the output_truth. If ``None``, the returned
267
- loss will be 0
268
- collect_output: if True, the output values will be collected (and possibly exported for debug purposes)
269
- collect_only_non_training_output: if True, only the non-training splits will have the outputs collected
270
- metrics: the metrics to be reported each epoch
271
- loss_reduction: a function to reduce the N-d losses to a single loss value
272
- weights: if not None, the weight name. the loss of each sample will be weighted by this vector
273
- loss_scaling: scale the loss by a scalar
274
- output_postprocessing: the output will be post-processed by this function for the classification report.
275
- For example, the classifier may return logit scores for each class and we can use argmax of the
276
- scores to get the class.
277
- maybe_optional: if True, the loss term may be considered optional if the ground
278
- truth is not part of the batch
279
- sample_uid_name (str): if not None, collect the sample UID
280
- per_voxel_weights: a per voxel weighting that will be passed to criterion_fn
281
- classes_name: the name of the class. This is used only to map a class ID to a string (e.g., for the
282
- classification report)
283
- """
284
- super().__init__(
285
- output=output,
286
- criterion_fn=criterion_fn,
287
- collect_output=collect_output,
288
- sample_uid_name=sample_uid_name,
289
- metrics=metrics)
290
- self.output_truth = output_truth
291
- self.loss_reduction = loss_reduction
292
- self.output_postprocessing = output_postprocessing
293
- self.collect_only_non_training_output = collect_only_non_training_output
294
- self.loss_scaling = loss_scaling
295
- self.weights = weights
296
- self.per_voxel_weights = per_voxel_weights
297
- self.maybe_optional = maybe_optional
298
- self.classes_name = classes_name
299
-
300
- if self.per_voxel_weights is not None:
301
- assert self.per_voxel_weights.shape[2:] == self.output.shape[2:]
302
- assert len(self.per_voxel_weights) == len(self.output)
303
-
304
- if output_truth is not None:
305
- if len(output.shape) != len(output_truth.shape):
306
- if len(output.shape) == len(output_truth.shape) + 1:
307
- warnings.warn('output and output_truth must have the same shape!'
308
- 'For binary classification, output_truth.shape == (X, 1).'
309
- 'This will be disabled in the future! Simply replace by'
310
- '`output_truth` by `output_truth.unsqueeze(1)`', FutureWarning)
311
- self.output_truth = output_truth.unsqueeze(1)
312
-
313
- def evaluate_batch(self, batch, is_training):
314
- truth = self.output_truth
315
- if truth is None and self.maybe_optional:
316
- return None
317
- assert truth is not None, 'truth is `None` use `maybe_optional` to True'
318
- assert len(truth) == len(self.output), f'expected len output ({len(self.output)}) == len truth ({len(truth)})!'
319
- assert isinstance(truth, torch.Tensor), 'feature must be a torch.Tensor!'
320
- assert truth.dtype == torch.long, f'the truth vector must be a `long` type feature, got={truth.dtype}'
321
-
322
- # make sure the class is not out of bound. This is a very common mistake!
323
- """
324
- max_index = int(torch.max(truth).cpu().numpy())
325
- min_index = int(torch.min(truth).cpu().numpy())
326
- assert max_index < self.output.shape[1], f'index out of bound. Got={max_index}, ' \
327
- f'maximum={self.output.shape[1]}. Make sure the input data is correct.'
328
- assert min_index >= 0, f'incorrect index! got={min_index}'
329
- """
330
-
331
- criterion_args = {}
332
- if self.per_voxel_weights is not None:
333
- criterion_args['per_voxel_weights'] = self.per_voxel_weights
334
-
335
- loss_term = {}
336
- if self.criterion_fn is not None:
337
- losses = self.criterion_fn()(self.output, truth, **criterion_args)
338
- else:
339
- losses = torch.zeros(len(self.output), device=self.output.device)
340
- assert isinstance(losses, torch.Tensor), 'must `loss` be a `torch.Tensor`'
341
-
342
- if self.weights is not None:
343
- weights = self.weights
344
- assert len(weights) == len(losses), 'must have a weight per sample'
345
- assert len(weights.shape) == 1, 'must be a 1D vector'
346
- else:
347
- weights = torch.ones_like(losses)
348
-
349
- # weight the loss of each sample by the corresponding weight
350
- weighted_losses = weights * losses
351
-
352
- if len(weighted_losses.shape) >= 2:
353
- # average the per-sample loss
354
- weighted_losses = flatten(weighted_losses).mean(dim=1)
355
-
356
- assert truth.shape[0] == losses.shape[0], 'loos must have 1 element per sample'
357
-
358
- loss_term['output_raw'] = self.output
359
- output_postprocessed = self.output_postprocessing(self.output)
360
- assert torch.is_tensor(output_postprocessed)
361
- assert output_postprocessed.dtype == torch.long, f'output must have a `long` type. ' \
362
- f'Got={output_postprocessed.dtype}'
363
- loss_term['output'] = output_postprocessed
364
- loss_term['output_truth'] = truth
365
-
366
- if self.sample_uid_name is not None and self.sample_uid_name in batch:
367
- uid = to_value(batch[self.sample_uid_name])
368
- assert len(uid) == len(truth), f'1 UID for 1 sample! Got={len(uid)} for samples={len(truth)}'
369
- loss_term['uid'] = uid
370
-
371
- # TODO label smoothing
372
- loss_term['losses'] = weighted_losses
373
- loss_term['loss'] = self.loss_scaling * self.loss_reduction(weighted_losses)
374
- loss_term['weights'] = weights
375
- loss_term[Output.output_ref_tag] = self # keep a back reference
376
- loss_term['metrics_results'] = extract_metrics(self.metrics, loss_term)
377
- return loss_term
378
-
379
- def loss_term_cleanup(self, loss_term):
380
- if not self.collect_output:
381
- del loss_term['output_raw']
382
- del loss_term['output']
383
- del loss_term['output_truth']
384
-
385
- # delete possibly large outputs
386
- self.output = None
387
- self.output_truth = None
388
-
389
-
390
-
391
-
392
- bce_logits_loss = lambda output, target: nn.functional.binary_cross_entropy_with_logits(
393
- output,
394
- target.type(output.dtype),
395
- reduction='none'
396
- )
397
-
398
-
399
- class OutputClassificationBinary(OutputClassification):
400
- """
401
- Classification output for binary classification
402
-
403
- Args:
404
- output: the output with shape [N, 1, {X}], without any activation applied (i.e., logits)
405
- output_truth: the truth with shape [N, 1, {X}]
406
- """
407
- def __init__(
408
- self,
409
- output,
410
- output_truth,
411
- *,
412
- criterion_fn=lambda: bce_logits_loss,
413
- collect_output=True,
414
- collect_only_non_training_output=False,
415
- metrics: List[metrics.Metric] = metrics.default_classification_metrics(),
416
- loss_reduction=torch.mean,
417
- weights=None,
418
- per_voxel_weights=None,
419
- loss_scaling=1.0,
420
- output_postprocessing=lambda x: (torch.sigmoid(x) >= 0.5).long(),
421
- maybe_optional=False,
422
- classes_name='unknown',
423
- sample_uid_name=default_sample_uid_name):
424
-
425
- if len(output.shape) != len(output_truth.shape):
426
- if len(output.shape) == len(output_truth.shape) + 1:
427
- warnings.warn('output and output_truth must have the same shape!'
428
- 'For binary classification, output_truth.shape == (X, 1).'
429
- 'This will be disabled in the future! Simply replace by'
430
- '`output_truth` by `output_truth.unsqueeze(1)`', FutureWarning)
431
- output_truth = output_truth.unsqueeze(1)
432
-
433
- assert len(output.shape) == len(output_truth.shape), 'must have the same dimensionality!'
434
- assert output.shape[1] == 1, 'binary classification!'
435
- assert output_truth.shape[1] == 1, 'binary classification!'
436
-
437
- super().__init__(
438
- output=output,
439
- output_truth=output_truth,
440
- criterion_fn=criterion_fn,
441
- collect_output=collect_output,
442
- collect_only_non_training_output=collect_only_non_training_output,
443
- metrics=metrics,
444
- loss_reduction=loss_reduction,
445
- weights=weights,
446
- per_voxel_weights=per_voxel_weights,
447
- loss_scaling=loss_scaling,
448
- output_postprocessing=output_postprocessing, # =1 as we export the class
449
- maybe_optional=maybe_optional,
450
- classes_name=classes_name,
451
- sample_uid_name=sample_uid_name
452
- )
453
-
454
-
455
- class OutputSegmentation(OutputClassification):
456
- def __init__(
457
- self,
458
- output: torch.Tensor,
459
- output_truth: torch.Tensor,
460
- criterion_fn: Callable[[], Any] = LossDiceMulticlass,
461
- collect_output: bool = False,
462
- collect_only_non_training_output: bool = False,
463
- metrics: List[metrics.Metric] = metrics.default_segmentation_metrics(),
464
- loss_reduction: Callable[[torch.Tensor], torch.Tensor] = torch.mean,
465
- weights=None,
466
- per_voxel_weights=None,
467
- loss_scaling=1.0,
468
- output_postprocessing=functools.partial(torch.argmax, dim=1, keepdim=True), # =1 as we export the class
469
- maybe_optional=False,
470
- sample_uid_name=default_sample_uid_name):
471
- """
472
-
473
- Args:
474
- output: the raw output values (`criterion_fn` will apply normalization such as sigmoid)
475
- output_truth: the tensor to be used as target. Targets must be compatible with ``criterion_fn``
476
- criterion_fn: the criterion to minimize between the output and the output_truth. If ``None``, the returned
477
- loss will be 0
478
- collect_output: if True, the output values will be collected (and possibly exported for debug purposes)
479
- collect_only_non_training_output: if True, only the non-training splits will have the outputs collected
480
- metrics: the metrics to be reported each epoch
481
- loss_reduction: a function to reduce the N-d losses to a single loss value
482
- weights: if not None, the weight name. the loss of each sample will be weighted by this vector
483
- loss_scaling: scale the loss by a scalar
484
- output_postprocessing: the output will be post-processed by this function for the segmentation result.
485
- For example, the classifier may return logit scores for each class and we can use argmax of the
486
- scores to get the class.
487
- maybe_optional: if True, the loss term may be considered optional if the ground
488
- truth is not part of the batch
489
- sample_uid_name (str): if not None, collect the sample UID
490
- per_voxel_weights: a per voxel weighting that will be passed to criterion_fn
491
- """
492
- if output_truth is not None:
493
- assert len(output.shape) == len(output_truth.shape), 'must have the same dimensionality!'
494
-
495
- super().__init__(
496
- output=output,
497
- output_truth=output_truth,
498
- criterion_fn=criterion_fn,
499
- collect_output=collect_output,
500
- collect_only_non_training_output=collect_only_non_training_output,
501
- metrics=metrics,
502
- loss_reduction=loss_reduction,
503
- weights=weights,
504
- loss_scaling=loss_scaling,
505
- output_postprocessing=output_postprocessing,
506
- maybe_optional=maybe_optional,
507
- sample_uid_name=sample_uid_name,
508
- per_voxel_weights=per_voxel_weights
509
- )
510
-
511
-
512
- class OutputSegmentationBinary(OutputSegmentation):
513
- """
514
- Output for binary segmentation.
515
-
516
- Parameters:
517
- output: shape N * 1 * X format, must be raw logits
518
- output_truth: should have N * 1 * X format, with values 0 or 1
519
- """
520
- def __init__(
521
- self,
522
- output: torch.Tensor,
523
- output_truth: torch.Tensor,
524
- criterion_fn: Callable[[], Any] = LossDiceMulticlass,
525
- collect_output: bool = False,
526
- collect_only_non_training_output: bool = False,
527
- metrics: List[metrics.Metric] = metrics.default_segmentation_metrics(),
528
- loss_reduction: Callable[[torch.Tensor], torch.Tensor] = torch.mean,
529
- weights=None,
530
- per_voxel_weights=None,
531
- loss_scaling=1.0,
532
- output_postprocessing=lambda x: (torch.sigmoid(x) > 0.5).type(torch.long), # =1 as we export the class
533
- maybe_optional=False,
534
- sample_uid_name=default_sample_uid_name):
535
- super().__init__(
536
- output=output,
537
- output_truth=output_truth,
538
- criterion_fn=criterion_fn,
539
- collect_output=collect_output,
540
- collect_only_non_training_output=collect_only_non_training_output,
541
- metrics=metrics,
542
- loss_reduction=loss_reduction,
543
- weights=weights,
544
- per_voxel_weights=per_voxel_weights,
545
- loss_scaling=loss_scaling,
546
- output_postprocessing=output_postprocessing,
547
- maybe_optional=maybe_optional,
548
- sample_uid_name=sample_uid_name
549
- )
550
-
551
-
552
- def mean_all(x):
553
- """
554
- :param x: a Tensor
555
- :return: the mean of all values
556
- """
557
- return torch.mean(x.view((-1)))
558
-
559
-
560
- class OutputRegression(Output):
561
- """
562
- Regression output
563
- """
564
- def __init__(
565
- self,
566
- output,
567
- output_truth,
568
- criterion_fn=lambda: nn.MSELoss(reduction='none'),
569
- collect_output=True,
570
- collect_only_non_training_output=False,
571
- metrics=metrics.default_regression_metrics(),
572
- loss_reduction=mean_all,
573
- weights=None,
574
- loss_scaling=1.0,
575
- output_postprocessing=lambda x: x,
576
- target_name=None,
577
- sample_uid_name=default_sample_uid_name):
578
- """
579
-
580
- :param output:
581
- :param target_name:
582
- :param criterion_fn:
583
- :param collect_output:
584
- :param collect_only_non_training_output:
585
- :param metrics:
586
- :param loss_reduction:
587
- :param weights: if not None, the weight. the loss of each sample will be weighted by this vector
588
- :param loss_scaling: scale the loss by a scalar
589
- :param output_postprocessing:
590
- """
591
- super().__init__(
592
- output=output,
593
- criterion_fn=criterion_fn,
594
- collect_output=collect_output,
595
- sample_uid_name=sample_uid_name,
596
- metrics=metrics)
597
- self.target_name = target_name
598
- self.output_truth = output_truth
599
- self.loss_reduction = loss_reduction
600
- self.output_postprocessing = output_postprocessing
601
- self.collect_only_non_training_output = collect_only_non_training_output
602
- self.weights = weights
603
- self.loss_scaling = loss_scaling
604
-
605
- def evaluate_batch(self, batch, is_training):
606
- loss_term = {}
607
- losses = self.criterion_fn()(self.output, self.output_truth)
608
- assert isinstance(losses, torch.Tensor), 'must `loss` be a `torch.Tensor`'
609
- assert len_batch(batch) == losses.shape[0], 'loos must have 1 element per sample'
610
- if self.collect_output:
611
- # we may not want to collect any outputs or training outputs to save some time
612
- if not self.collect_only_non_training_output or not is_training:
613
- # detach the output so as not to calculate gradients. Keep the truth so that we
614
- # can calculate statistics (e.g., accuracy, FP/FN...)
615
- loss_term['output_raw'] = self.output
616
- loss_term['output'] = self.output_postprocessing(self.output)
617
- loss_term['output_truth'] = self.output_truth
618
-
619
- # do NOT keep the original output else memory will be an issue
620
- del self.output
621
- self.output = None
622
-
623
- if self.weights is not None:
624
- weights = self.weights
625
- assert weights is not None, f'weight `{self.weights}` could not be found!'
626
- assert len(weights) == len(losses), 'must have a weight per sample'
627
- assert len(weights.shape) == 1, 'must be a 1D vector'
628
- else:
629
- weights = torch.ones_like(losses)
630
-
631
- if self.sample_uid_name is not None and self.sample_uid_name in batch:
632
- loss_term['uid'] = to_value(batch[self.sample_uid_name])
633
-
634
- # weight the loss of each sample by the corresponding weight
635
- weighted_losses = weights * losses
636
-
637
- loss_term['losses'] = weighted_losses
638
- # here we MUST be able to calculate the gradient so don't detach
639
- loss_term['loss'] = self.loss_scaling * self.loss_reduction(weighted_losses)
640
- loss_term[Output.output_ref_tag] = self # keep a back reference
641
- loss_term['metrics_results'] = extract_metrics(self.metrics, loss_term)
642
- return loss_term
643
-
644
-
645
- class OutputTriplets(Output):
646
- def __init__(
647
- self,
648
- samples,
649
- positive_samples,
650
- negative_samples,
651
- criterion_fn=lambda: losses.LossTriplets(),
652
- metrics=metrics.default_generic_metrics(),
653
- loss_reduction=mean_all,
654
- weight_name=None,
655
- loss_scaling=1.0,
656
- sample_uid_name=default_sample_uid_name
657
- ):
658
- super().__init__(metrics=metrics, output=samples, criterion_fn=criterion_fn)
659
- self.loss_reduction = loss_reduction
660
- self.sample_uid_name = sample_uid_name
661
- self.loss_scaling = loss_scaling
662
- self.weight_name = weight_name
663
- self.criterion_fn = criterion_fn
664
- self.negative_samples = negative_samples
665
- self.positive_samples = positive_samples
666
-
667
- def evaluate_batch(self, batch, is_training):
668
- loss_term = collections.OrderedDict()
669
- losses = self.criterion_fn()(self.output, self.positive_samples, self.negative_samples)
670
- assert isinstance(losses, torch.Tensor), 'must `loss` be a `torch.Tensor`'
671
- assert len_batch(batch) == losses.shape[0], 'loss must have 1 element per sample'
672
-
673
- loss_term['output_raw'] = self.output
674
-
675
- # do NOT keep the original output else memory will be an issue
676
- # (e.g., CUDA device)
677
- del self.output
678
- self.output = None
679
- del self.negative_samples
680
- self.negative_samples = None
681
- del self.positive_samples
682
- self.positive_samples = None
683
-
684
- if self.weight_name is not None:
685
- weights = batch.get(self.weight_name)
686
- assert weights is not None, f'weight `{self.weight_name}` could not be found!'
687
- assert len(weights) == len(losses), 'must have a weight per sample'
688
- assert len(weights.shape) == 1, 'must be a 1D vector'
689
- # expand to same shape size so that we can easily broadcast the weight
690
- weights = weights.reshape([weights.shape[0]] + [1] * (len(losses.shape) - 1))
691
- else:
692
- weights = torch.ones_like(losses)
693
-
694
- if self.sample_uid_name is not None and self.sample_uid_name in batch:
695
- loss_term['uid'] = to_value(batch[self.sample_uid_name])
696
-
697
- # weight the loss of each sample by the corresponding weight
698
- weighted_losses = weights * losses
699
-
700
- loss_term['losses'] = weighted_losses
701
- loss_term['loss'] = self.loss_scaling * self.loss_reduction(weighted_losses)
702
- loss_term[Output.output_ref_tag] = self # keep a back reference
703
- loss_term['metrics_results'] = extract_metrics(self.metrics, loss_term)
704
- return loss_term
705
-
706
-
707
- class OutputLoss(Output):
708
- """
709
- Represent a given loss as an output.
710
-
711
- This can be useful to add additional regularizer to the training (e.g., :class:`LossCenter`).
712
- """
713
- def __init__(
714
- self,
715
- losses,
716
- loss_reduction=torch.mean,
717
- metrics=metrics.default_generic_metrics(),
718
- sample_uid_name=default_sample_uid_name):
719
- super().__init__(
720
- metrics=metrics,
721
- output=losses,
722
- criterion_fn=None,
723
- collect_output=True,
724
- sample_uid_name=sample_uid_name)
725
- self.loss_reduction = loss_reduction
726
-
727
- def evaluate_batch(self, batch, is_training):
728
- loss_term = {
729
- 'losses': self.output,
730
- 'loss': self.loss_reduction(self.output), # to be optimized, we MUST have a `loss` key
731
- Output.output_ref_tag: self, # keep a back reference
732
- }
733
-
734
- if self.sample_uid_name is not None and self.sample_uid_name in batch:
735
- loss_term['uid'] = to_value(batch[self.sample_uid_name])
736
-
737
- loss_term['metrics_results'] = extract_metrics(self.metrics, loss_term)
738
- return loss_term
739
-
740
- def loss_term_cleanup(self, loss_term):
741
- super().loss_term_cleanup(loss_term)
742
-
743
- # delete possibly large outputs
744
- self.output = None
745
-