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

iou_loss_test.py 5.0 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
127
128
129
130
  1. import torch
  2. import unittest
  3. from super_gradients.training.losses.iou_loss import IoULoss, GeneralizedIoULoss, BinaryIoULoss
  4. class IoULossTest(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_iou_torch_values(self, expected_value: torch.Tensor, found_value: torch.Tensor, rtol: float = 1e-5):
  14. self.assertTrue(
  15. torch.allclose(found_value, expected_value, rtol=rtol),
  16. msg=f"Unequal iou loss: excepted: {expected_value}, found: {found_value}"
  17. )
  18. def test_iou(self):
  19. predictions = self._get_default_predictions_tensor(0.)
  20. # only label 0 is predicted as positive.
  21. predictions[:, 0] = 1.
  22. target = self._get_default_target_zeroes_tensor()
  23. # half target with label 0, the other half with 1.
  24. target[:, :self.img_size // 2] = 1
  25. intersection = torch.tensor([0.5, 0.])
  26. union = torch.tensor([1., 0.5])
  27. expected_iou_loss = 1. - (intersection / (union + self.eps))
  28. expected_iou_loss = expected_iou_loss.mean()
  29. criterion = IoULoss(smooth=0, eps=self.eps, apply_softmax=False)
  30. iou_loss = criterion(predictions, target)
  31. self._assertion_iou_torch_values(expected_iou_loss, iou_loss)
  32. def test_iou_binary(self):
  33. # all predictions are 0.6
  34. predictions = torch.ones((1, 1, self.img_size, self.img_size)) * 0.6
  35. target = self._get_default_target_zeroes_tensor()
  36. # half target with label 0, the other half with 1.
  37. target[:, :self.img_size // 2] = 1
  38. intersection = torch.tensor([0.6 * 0.5])
  39. union = torch.tensor([0.6 + 0.5 - 0.6 * 0.5])
  40. expected_iou_loss = 1. - (intersection / (union + self.eps))
  41. expected_iou_loss = expected_iou_loss.mean()
  42. criterion = BinaryIoULoss(smooth=0, eps=self.eps, apply_sigmoid=False)
  43. iou_loss = criterion(predictions, target)
  44. self._assertion_iou_torch_values(expected_iou_loss, iou_loss, rtol=1e-3)
  45. def test_iou_weight_classes(self):
  46. weight = torch.tensor([0.25, 0.66])
  47. predictions = self._get_default_predictions_tensor(0.)
  48. # only label 0 is predicted as positive.
  49. predictions[:, 0] = 1.
  50. target = self._get_default_target_zeroes_tensor()
  51. # half target with label 0, the other half with 1.
  52. target[:, :self.img_size // 2] = 1
  53. intersection = torch.tensor([0.5, 0.])
  54. union = torch.tensor([1., 0.5])
  55. expected_iou_loss = 1. - (intersection / (union + self.eps))
  56. expected_iou_loss *= weight
  57. expected_iou_loss = expected_iou_loss.mean()
  58. criterion = IoULoss(smooth=0, eps=self.eps, apply_softmax=False, weight=weight)
  59. iou_loss = criterion(predictions, target)
  60. self._assertion_iou_torch_values(expected_iou_loss, iou_loss)
  61. def test_iou_with_ignore(self):
  62. ignore_index = 2
  63. predictions = self._get_default_predictions_tensor(0.)
  64. # only label 0 is predicted as positive.
  65. predictions[:, 0] = 1.
  66. target = self._get_default_target_zeroes_tensor()
  67. # half target with label 0, quarter with 1 and quarter with ignore.
  68. target[:, :self.img_size // 2, :self.img_size // 2] = 1
  69. target[:, :self.img_size // 2, self.img_size // 2:] = ignore_index
  70. # ignore samples are excluded in both intersection and union.
  71. intersection = torch.tensor([0.5, 0.])
  72. union = torch.tensor([0.75, 0.25])
  73. expected_iou_loss = 1. - (intersection / (union + self.eps))
  74. expected_iou_loss = expected_iou_loss.mean()
  75. criterion = IoULoss(smooth=0, eps=self.eps, apply_softmax=False, ignore_index=ignore_index)
  76. iou_loss = criterion(predictions, target)
  77. self._assertion_iou_torch_values(expected_iou_loss, iou_loss)
  78. def test_generalized_iou(self):
  79. predictions = self._get_default_predictions_tensor(0.)
  80. # half prediction are 0 class, the other half 1 class.
  81. predictions[:, 0, :self.img_size // 2] = 1.
  82. predictions[:, 1, self.img_size // 2:] = 1.
  83. # only 0 class in target.
  84. target = self._get_default_target_zeroes_tensor()
  85. intersection = torch.tensor([0.5, 0.])
  86. union = torch.tensor([1., 0.5])
  87. counts = torch.tensor([target.numel(), 0.])
  88. weights = 1 / (counts ** 2)
  89. weights[1] = 0.0 # instead of inf
  90. eps = 1e-17
  91. expected_iou_loss = 1. - ((weights * intersection) / (weights * union + eps))
  92. expected_iou_loss = expected_iou_loss.mean()
  93. criterion = GeneralizedIoULoss(smooth=0, eps=eps, apply_softmax=False)
  94. iou_loss = criterion(predictions, target)
  95. self._assertion_iou_torch_values(expected_iou_loss, iou_loss)
  96. if __name__ == '__main__':
  97. unittest.main()
Tip!

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

Comments

Loading...