Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#378 Feature/sg 281 add kd notebook

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-281-add_kd_notebook
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
  1. from typing import Union
  2. import torch
  3. from super_gradients.common.abstractions.abstract_logger import get_logger
  4. from super_gradients.training.losses.loss_utils import LossReduction
  5. from super_gradients.training.losses.structure_loss import AbstarctSegmentationStructureLoss
  6. logger = get_logger(__name__)
  7. class IoULoss(AbstarctSegmentationStructureLoss):
  8. """
  9. Compute average IoU loss between two tensors, It can support both multi-classes and binary tasks.
  10. """
  11. def _calc_numerator_denominator(self, labels_one_hot, predict):
  12. """
  13. Calculate iou metric's numerator and denominator.
  14. :param labels_one_hot: target in one hot format. shape: [BS, num_classes, img_width, img_height]
  15. :param predict: predictions tensor. shape: [BS, num_classes, img_width, img_height]
  16. :return:
  17. numerator = intersection between predictions and target. shape: [BS, num_classes, img_width, img_height]
  18. denominator = area of union between predictions and target. shape: [BS, num_classes, img_width, img_height]
  19. """
  20. numerator = labels_one_hot * predict
  21. denominator = labels_one_hot + predict - numerator
  22. return numerator, denominator
  23. def _calc_loss(self, numerator, denominator):
  24. """
  25. Calculate iou loss.
  26. All tensors are of shape [BS] if self.reduce_over_batches else [num_classes]
  27. :param numerator: intersection between predictions and target.
  28. :param denominator: area of union between prediction pixels and target pixels.
  29. """
  30. loss = 1. - ((numerator + self.smooth) / (denominator + self.eps + self.smooth))
  31. return loss
  32. class BinaryIoULoss(IoULoss):
  33. """
  34. Compute IoU Loss for binary class tasks (1 class only).
  35. Except target to be a binary map with 0 and 1 values.
  36. """
  37. def __init__(self,
  38. apply_sigmoid: bool = True,
  39. smooth: float = 1.,
  40. eps: float = 1e-5):
  41. """
  42. :param apply_sigmoid: Whether to apply sigmoid to the predictions.
  43. :param smooth: laplace smoothing, also known as additive smoothing. The larger smooth value is, closer the IoU
  44. coefficient is to 1, which can be used as a regularization effect.
  45. As mentioned in: https://github.com/pytorch/pytorch/issues/1249#issuecomment-337999895
  46. :param eps: epsilon value to avoid inf.
  47. """
  48. super().__init__(apply_softmax=False, ignore_index=None, smooth=smooth, eps=eps, reduce_over_batches=False)
  49. self.apply_sigmoid = apply_sigmoid
  50. def forward(self, predict, target):
  51. if self.apply_sigmoid:
  52. predict = torch.sigmoid(predict)
  53. return super().forward(predict=predict, target=target)
  54. class GeneralizedIoULoss(IoULoss):
  55. """
  56. Compute the Generalised IoU loss, contribution of each label is normalized by the inverse of its volume, in order
  57. to deal with class imbalance.
  58. Args:
  59. smooth (float): default value is 0, smooth laplacian is not recommended to be used with GeneralizedIoULoss.
  60. because the weighted values to be added are very small.
  61. eps (float): default value is 1e-17, must be a very small value, because weighted `intersection` and
  62. `denominator` are very small after multiplication with `1 / counts ** 2`
  63. """
  64. def __init__(self,
  65. apply_softmax: bool = True,
  66. ignore_index: int = None,
  67. smooth: float = 0.0,
  68. eps: float = 1e-17,
  69. reduce_over_batches: bool = False,
  70. reduction: Union[LossReduction, str] = "mean"
  71. ):
  72. """
  73. :param apply_softmax: Whether to apply softmax to the predictions.
  74. :param smooth: laplace smoothing, also known as additive smoothing. The larger smooth value is, closer the iou
  75. coefficient is to 1, which can be used as a regularization effect.
  76. As mentioned in: https://github.com/pytorch/pytorch/issues/1249#issuecomment-337999895
  77. :param eps: epsilon value to avoid inf.
  78. :param reduce_over_batches: Whether to apply reduction over the batch axis if set True,
  79. default is `False` to average over the classes axis.
  80. :param reduction: Specifies the reduction to apply to the output: `none` | `mean` | `sum`.
  81. `none`: no reduction will be applied.
  82. `mean`: the sum of the output will be divided by the number of elements in the output.
  83. `sum`: the output will be summed.
  84. Default: `mean`
  85. """
  86. super().__init__(apply_softmax=apply_softmax, ignore_index=ignore_index, smooth=smooth, eps=eps,
  87. reduce_over_batches=reduce_over_batches, generalized_metric=True, weight=None,
  88. reduction=reduction)
Discard
Tip!

Press p or to see the previous file or, n or to see the next file