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
  1. import torch
  2. import torch.nn as nn
  3. from super_gradients.training.losses.dice_loss import DiceLoss, BinaryDiceLoss
  4. from super_gradients.training.utils.segmentation_utils import target_to_binary_edge
  5. from torch.nn.modules.loss import _Loss
  6. from typing import Union, Tuple
  7. from super_gradients.training.losses.mask_loss import MaskAttentionLoss
  8. class DiceCEEdgeLoss(_Loss):
  9. def __init__(self,
  10. num_classes: int,
  11. num_aux_heads: int = 2,
  12. num_detail_heads: int = 1,
  13. weights: Union[tuple, list] = (1, 1, 1, 1),
  14. dice_ce_weights: Union[tuple, list] = (1, 1),
  15. ignore_index: int = -100,
  16. edge_kernel: int = 3,
  17. ce_edge_weights: Union[tuple, list] = (.5, .5)):
  18. """
  19. Total loss is computed as follows:
  20. Loss-cls-edge = λ1 * CE + λ2 * M * CE , where [λ1, λ2] are ce_edge_weights.
  21. For each Main feature maps and auxiliary heads the loss is calculated as:
  22. Loss-main-aux = λ3 * Loss-cls-edge + λ4 * Loss-Dice, where [λ3, λ4] are dice_ce_weights.
  23. For Feature maps defined as detail maps that predicts only the edge mask, the loss is computed as follow:
  24. Loss-detail = BinaryCE + BinaryDice
  25. Finally the total loss is computed as follows for the whole feature maps:
  26. Loss = Σw[i] * Loss-main-aux[i] + Σw[j] * Loss-detail[j], where `w` is defined as the `weights` argument
  27. `i` in [0, 1 + num_aux_heads], 1 is for the main feature map.
  28. `j` in [1 + num_aux_heads, 1 + num_aux_heads + num_detail_heads].
  29. :param num_aux_heads: num of auxiliary heads.
  30. :param num_detail_heads: num of detail heads.
  31. :param weights: Loss lambda weights.
  32. :param dice_ce_weights: weights lambdas between (Dice, CE) losses.
  33. :param edge_kernel: kernel size of dilation erosion convolutions for creating the edge feature map.
  34. :param ce_edge_weights: weights lambdas between regular CE and edge attention CE.
  35. """
  36. super().__init__()
  37. # Check that arguments are valid.
  38. assert len(weights) == num_aux_heads + num_detail_heads + 1,\
  39. "Lambda loss weights must be in same size as loss items."
  40. assert len(dice_ce_weights) == 2, f"dice_ce_weights must an iterable with size 2, found: {len(dice_ce_weights)}"
  41. assert len(ce_edge_weights) == 2, f"dice_ce_weights must an iterable with size 2, found: {len(ce_edge_weights)}"
  42. self.edge_kernel = edge_kernel
  43. self.num_classes = num_classes
  44. self.ignore_index = ignore_index
  45. self.weights = weights
  46. self.dice_ce_weights = dice_ce_weights
  47. self.use_detail = num_detail_heads > 0
  48. self.num_aux_heads = num_aux_heads
  49. self.num_detail_heads = num_detail_heads
  50. if self.use_detail:
  51. self.bce = nn.BCEWithLogitsLoss()
  52. self.binary_dice = BinaryDiceLoss(apply_sigmoid=True)
  53. self.ce_edge = MaskAttentionLoss(
  54. criterion=nn.CrossEntropyLoss(reduction="none", ignore_index=ignore_index),
  55. loss_weights=ce_edge_weights
  56. )
  57. self.dice_loss = DiceLoss(apply_softmax=True, ignore_index=ignore_index)
  58. def forward(self, preds: Tuple[torch.Tensor], target: torch.Tensor):
  59. """
  60. :param preds: Model output predictions, must be in the followed format:
  61. [Main-feats, Aux-feats[0], ..., Aux-feats[num_auxs-1], Detail-feats[0], ..., Detail-feats[num_details-1]
  62. """
  63. assert len(preds) == self.num_aux_heads + self.num_detail_heads + 1,\
  64. f"Wrong num of predictions tensors, expected {self.num_aux_heads + self.num_detail_heads + 1} found {len(preds)}"
  65. edge_target = target_to_binary_edge(target, num_classes=self.num_classes, kernel_size=self.edge_kernel,
  66. ignore_index=self.ignore_index, flatten_channels=True)
  67. losses = []
  68. total_loss = 0
  69. # Main and auxiliaries feature maps losses
  70. for i in range(0, 1 + self.num_aux_heads):
  71. ce_loss = self.ce_edge(preds[i], target, edge_target)
  72. dice_loss = self.dice_loss(preds[i], target)
  73. loss = ce_loss * self.dice_ce_weights[0] + dice_loss * self.dice_ce_weights[1]
  74. total_loss += self.weights[i] * loss
  75. losses.append(loss)
  76. # Detail feature maps losses
  77. if self.use_detail:
  78. for i in range(1 + self.num_aux_heads, len(preds)):
  79. bce_loss = self.bce(preds[i], edge_target)
  80. dice_loss = self.binary_dice(preds[i], edge_target)
  81. loss = bce_loss * self.dice_ce_weights[0] + dice_loss * self.dice_ce_weights[1]
  82. total_loss += self.weights[i] * loss
  83. losses.append(loss)
  84. losses.append(total_loss)
  85. return total_loss, torch.stack(losses, dim=0).detach()
Discard
Tip!

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