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

#970 Update YoloNASQuickstart.md

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:bugfix/SG-000_fix_readme_yolonas_snippets
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
  1. import torch
  2. from torch import nn
  3. from torch.nn.modules.loss import _Loss
  4. from super_gradients.training.exceptions.loss_exceptions import IllegalRangeForLossAttributeException, RequiredLossComponentReductionException
  5. class OhemLoss(_Loss):
  6. """
  7. OhemLoss - Online Hard Example Mining Cross Entropy Loss
  8. """
  9. def __init__(self, threshold: float, mining_percent: float = 0.1, ignore_lb: int = -100, num_pixels_exclude_ignored: bool = True, criteria: _Loss = None):
  10. """
  11. :param threshold: Sample below probability threshold, is considered hard.
  12. :param num_pixels_exclude_ignored: How to calculate total pixels from which extract mining percent of the
  13. samples.
  14. :param ignore_lb: label index to be ignored in loss calculation.
  15. :param criteria: loss to mine the examples from.
  16. i.e for num_pixels=100, ignore_pixels=30, mining_percent=0.1:
  17. num_pixels_exclude_ignored=False => num_mining = 100 * 0.1 = 10
  18. num_pixels_exclude_ignored=True => num_mining = (100 - 30) * 0.1 = 7
  19. """
  20. super().__init__()
  21. if mining_percent < 0 or mining_percent > 1:
  22. raise IllegalRangeForLossAttributeException((0, 1), "mining percent")
  23. self.thresh = -torch.log(torch.tensor(threshold, dtype=torch.float))
  24. self.mining_percent = mining_percent
  25. self.ignore_lb = ignore_lb
  26. self.num_pixels_exclude_ignored = num_pixels_exclude_ignored
  27. if criteria.reduction != "none":
  28. raise RequiredLossComponentReductionException("criteria", criteria.reduction, "none")
  29. self.criteria = criteria
  30. def forward(self, logits, labels):
  31. loss = self.criteria(logits, labels).view(-1)
  32. if self.num_pixels_exclude_ignored:
  33. # remove ignore label elements
  34. loss = loss[labels.view(-1) != self.ignore_lb]
  35. # num pixels in a batch -> num_pixels = batch_size * width * height - ignore_pixels
  36. num_pixels = loss.numel()
  37. else:
  38. num_pixels = labels.numel()
  39. # if all pixels are ignore labels, return empty loss tensor
  40. if num_pixels == 0:
  41. return torch.tensor([0.0]).requires_grad_(True)
  42. num_mining = int(self.mining_percent * num_pixels)
  43. # in case mining_percent=1, prevent out of bound exception
  44. num_mining = min(num_mining, num_pixels - 1)
  45. self.thresh = self.thresh.to(logits.device)
  46. loss, _ = torch.sort(loss, descending=True)
  47. if loss[num_mining] > self.thresh:
  48. loss = loss[loss > self.thresh]
  49. else:
  50. loss = loss[:num_mining]
  51. return torch.mean(loss)
  52. class OhemCELoss(OhemLoss):
  53. """
  54. OhemLoss - Online Hard Example Mining Cross Entropy Loss
  55. """
  56. def __init__(self, threshold: float, mining_percent: float = 0.1, ignore_lb: int = -100, num_pixels_exclude_ignored: bool = True):
  57. ignore_lb = -100 if ignore_lb is None or ignore_lb < 0 else ignore_lb
  58. criteria = nn.CrossEntropyLoss(ignore_index=ignore_lb, reduction="none")
  59. super(OhemCELoss, self).__init__(
  60. threshold=threshold, mining_percent=mining_percent, ignore_lb=ignore_lb, num_pixels_exclude_ignored=num_pixels_exclude_ignored, criteria=criteria
  61. )
  62. class OhemBCELoss(OhemLoss):
  63. """
  64. OhemBCELoss - Online Hard Example Mining Binary Cross Entropy Loss
  65. """
  66. def __init__(
  67. self,
  68. threshold: float,
  69. mining_percent: float = 0.1,
  70. ignore_lb: int = -100,
  71. num_pixels_exclude_ignored: bool = True,
  72. ):
  73. super(OhemBCELoss, self).__init__(
  74. threshold=threshold,
  75. mining_percent=mining_percent,
  76. ignore_lb=ignore_lb,
  77. num_pixels_exclude_ignored=num_pixels_exclude_ignored,
  78. criteria=nn.BCEWithLogitsLoss(reduction="none"),
  79. )
  80. def forward(self, logits, labels):
  81. # REMOVE SINGLE CLASS CHANNEL WHEN DEALING WITH BINARY DATA
  82. if logits.shape[1] == 1:
  83. logits = logits.squeeze(1)
  84. return super(OhemBCELoss, self).forward(logits, labels.float())
Discard
Tip!

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