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

dice_loss_test.py 5.2 KB

You have to be logged in to leave a comment. Sign In
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
  1. import torch
  2. import unittest
  3. from super_gradients.training.losses.dice_loss import DiceLoss, GeneralizedDiceLoss, BinaryDiceLoss
  4. class DiceLossTest(unittest.TestCase):
  5. def setUp(self) -> None:
  6. self.img_size = 32
  7. self.eps = 1e-5
  8. self.num_classes = 2
  9. def _get_default_predictions_tensor(self, fill_value: float):
  10. return torch.empty(3, self.num_classes, self.img_size, self.img_size).fill_(fill_value)
  11. def _get_default_target_zeroes_tensor(self):
  12. return torch.zeros((3, self.img_size, self.img_size)).long()
  13. def _assertion_dice_torch_values(self, expected_value: torch.Tensor, found_value: torch.Tensor, rtol: float = 1e-5):
  14. self.assertTrue(torch.allclose(found_value, expected_value, rtol=rtol), msg=f"Unequal dice loss: excepted: {expected_value}, found: {found_value}")
  15. def test_dice(self):
  16. predictions = self._get_default_predictions_tensor(0.0)
  17. # only label 0 is predicted as positive.
  18. predictions[:, 0] = 1.0
  19. target = self._get_default_target_zeroes_tensor()
  20. # half target with label 0, the other half with 1.
  21. target[:, : self.img_size // 2] = 1
  22. intersection = torch.tensor([0.5, 0.0])
  23. denominator = torch.tensor([1.5, 0.5])
  24. expected_dice_loss = 1.0 - ((2.0 * intersection) / (denominator + self.eps))
  25. expected_dice_loss = expected_dice_loss.mean()
  26. criterion = DiceLoss(smooth=0, eps=self.eps, apply_softmax=False)
  27. dice_loss = criterion(predictions, target)
  28. self._assertion_dice_torch_values(expected_dice_loss, dice_loss)
  29. def test_dice_binary(self):
  30. # all predictions are 0.6
  31. predictions = torch.ones((1, 1, self.img_size, self.img_size)) * 0.6
  32. target = self._get_default_target_zeroes_tensor()
  33. # half target with label 0, the other half with 1.
  34. target[:, : self.img_size // 2] = 1
  35. intersection = torch.tensor([0.6 * 0.5])
  36. denominator = torch.tensor([0.6 + 0.5])
  37. expected_dice_loss = 1.0 - ((2.0 * intersection) / (denominator + self.eps))
  38. expected_dice_loss = expected_dice_loss.mean()
  39. criterion = BinaryDiceLoss(smooth=0, eps=self.eps, apply_sigmoid=False)
  40. dice_loss = criterion(predictions, target)
  41. self._assertion_dice_torch_values(expected_dice_loss, dice_loss, rtol=1e-3)
  42. def test_dice_weight_classes(self):
  43. weight = torch.tensor([0.25, 0.66])
  44. predictions = self._get_default_predictions_tensor(0.0)
  45. # only label 0 is predicted as positive.
  46. predictions[:, 0] = 1.0
  47. target = self._get_default_target_zeroes_tensor()
  48. # half target with label 0, the other half with 1.
  49. target[:, : self.img_size // 2] = 1
  50. intersection = torch.tensor([0.5, 0.0])
  51. denominator = torch.tensor([1.5, 0.5])
  52. expected_dice_loss = 1.0 - ((2.0 * intersection) / (denominator + self.eps))
  53. expected_dice_loss *= weight
  54. expected_dice_loss = expected_dice_loss.mean()
  55. criterion = DiceLoss(smooth=0, eps=self.eps, apply_softmax=False, weight=weight)
  56. dice_loss = criterion(predictions, target)
  57. self._assertion_dice_torch_values(expected_dice_loss, dice_loss)
  58. def test_dice_with_ignore(self):
  59. ignore_index = 2
  60. predictions = self._get_default_predictions_tensor(0.0)
  61. # only label 0 is predicted as positive.
  62. predictions[:, 0] = 1.0
  63. target = self._get_default_target_zeroes_tensor()
  64. # half target with label 0, quarter with 1 and quarter with ignore.
  65. target[:, : self.img_size // 2, : self.img_size // 2] = 1
  66. target[:, : self.img_size // 2, self.img_size // 2 :] = ignore_index
  67. # ignore samples are excluded in both intersection and denominator.
  68. intersection = torch.tensor([0.5, 0.0])
  69. denominator = torch.tensor([0.75 + 0.5, 0.25])
  70. expected_dice_loss = 1.0 - ((2.0 * intersection) / (denominator + self.eps))
  71. expected_dice_loss = expected_dice_loss.mean()
  72. criterion = DiceLoss(smooth=0, eps=self.eps, apply_softmax=False, ignore_index=ignore_index)
  73. dice_loss = criterion(predictions, target)
  74. self._assertion_dice_torch_values(expected_dice_loss, dice_loss)
  75. def test_generalized_dice(self):
  76. predictions = self._get_default_predictions_tensor(0.0)
  77. # half prediction are 0 class, the other half 1 class.
  78. predictions[:, 0, : self.img_size // 2] = 1.0
  79. predictions[:, 1, self.img_size // 2 :] = 1.0
  80. # only 0 class in target.
  81. target = self._get_default_target_zeroes_tensor()
  82. intersection = torch.tensor([0.5, 0.0])
  83. denominator = torch.tensor([1.5, 0.5])
  84. counts = torch.tensor([target.numel(), 0.0])
  85. weights = 1 / (counts**2)
  86. weights[1] = 0.0 # instead of inf
  87. eps = 1e-17
  88. expected_dice_loss = 1.0 - ((2.0 * weights * intersection) / (weights * denominator + eps))
  89. expected_dice_loss = expected_dice_loss.mean()
  90. criterion = GeneralizedDiceLoss(smooth=0, eps=eps, apply_softmax=False)
  91. dice_loss = criterion(predictions, target)
  92. self._assertion_dice_torch_values(expected_dice_loss, dice_loss)
  93. if __name__ == "__main__":
  94. unittest.main()
Tip!

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

Comments

Loading...