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

#609 Ci fix

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:bugfix/infra-000_ci
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
  1. import torch
  2. from typing import Union
  3. from super_gradients.training.losses.ohem_ce_loss import OhemCELoss
  4. class DDRNetLoss(OhemCELoss):
  5. def __init__(self,
  6. threshold: float = 0.7,
  7. ohem_percentage: float = 0.1,
  8. weights: list = [1.0, 0.4],
  9. ignore_label=255,
  10. num_pixels_exclude_ignored: bool = False):
  11. """
  12. This loss is an extension of the Ohem (Online Hard Example Mining Cross Entropy) Loss.
  13. as define in paper:
  14. Accurate Semantic Segmentation of Road Scenes ( https://arxiv.org/pdf/2101.06085.pdf )
  15. :param threshold: threshold to th hard example mining algorithm
  16. :param ohem_percentage: minimum percentage of total pixels for the hard example mining algorithm
  17. (taking only the largest) losses
  18. :param weights: weights per each input of the loss. This loss supports a multi output (like in DDRNet with
  19. an auxiliary head). the losses of each head can be weighted.
  20. :param ignore_label: targets label to be ignored
  21. :param num_pixels_exclude_ignored: whether to exclude ignore pixels when calculating the mining percentage.
  22. see OhemCELoss doc for more details.
  23. """
  24. super().__init__(threshold=threshold, mining_percent=ohem_percentage, ignore_lb=ignore_label,
  25. num_pixels_exclude_ignored=num_pixels_exclude_ignored)
  26. self.weights = weights
  27. def forward(self, predictions_list: Union[list, tuple, torch.Tensor],
  28. targets: torch.Tensor):
  29. if isinstance(predictions_list, torch.Tensor):
  30. predictions_list = (predictions_list,)
  31. assert len(predictions_list) == len(self.weights), "num of prediction must be the same as num of loss weights"
  32. losses = []
  33. unweighted_losses = []
  34. for predictions, weight in zip(predictions_list, self.weights):
  35. unweighted_loss = super().forward(predictions, targets)
  36. unweighted_losses.append(unweighted_loss)
  37. losses.append(unweighted_loss * weight)
  38. total_loss = sum(losses)
  39. unweighted_losses.append(total_loss)
  40. return total_loss, torch.stack(unweighted_losses, dim=0).detach()
  41. @property
  42. def component_names(self):
  43. """
  44. Component names for logging during training.
  45. These correspond to 2nd item in the tuple returned in self.forward(...).
  46. See super_gradients.Trainer.train() docs for more info.
  47. """
  48. return ["main_loss", "aux_loss1", "loss"]
Discard
Tip!

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