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

#367 fix: Request correct hydra-core

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:hotfix/ALG-000_hydra-req
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
  1. import torch
  2. import unittest
  3. from super_gradients.training.losses.ddrnet_loss import DDRNetLoss
  4. class DDRLossTest(unittest.TestCase):
  5. def setUp(self) -> None:
  6. self.num_classes = 19
  7. self.img_size = 256
  8. self.eps = 0.01
  9. # equal probability distribution, p = 1 / num_classes
  10. # except loss to be: -log(p)
  11. self.predictions = torch.ones((1, self.num_classes, self.img_size, self.img_size))
  12. self.targets = torch.randint(0, self.num_classes, (1, self.img_size, self.img_size))
  13. def test_without_auxiliary_loss(self):
  14. """
  15. No Auxiliary loss, only one prediction map
  16. """
  17. weights = [1.]
  18. criterion = DDRNetLoss((1 / self.num_classes - self.eps), ohem_percentage=0.1, weights=weights)
  19. bce_loss = -torch.log(torch.tensor(1 / self.num_classes))
  20. expected_loss = bce_loss * weights[0]
  21. loss, _ = criterion(self.predictions, self.targets)
  22. self.assertAlmostEqual(expected_loss, loss, delta=1e-5)
  23. def test_with_auxiliary_loss(self):
  24. """
  25. Auxiliary loss, 2 prediction maps, as DDRNet paper.
  26. """
  27. predictions = [self.predictions, self.predictions]
  28. weights = [1., 0.4]
  29. criterion = DDRNetLoss((1 / self.num_classes - self.eps), ohem_percentage=0.1, weights=weights)
  30. expected_loss = -torch.log(torch.tensor(1 / self.num_classes)) * weights[0] + \
  31. -torch.log(torch.tensor(1 / self.num_classes)) * weights[1]
  32. loss, _ = criterion(predictions, self.targets)
  33. self.assertAlmostEqual(expected_loss, loss, delta=1e-5)
  34. if __name__ == '__main__':
  35. unittest.main()
Discard
Tip!

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