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
103
104
105
106
107
108
109
110
111
  1. import torch
  2. from torch import nn
  3. import torch.nn.functional as F
  4. from super_gradients.common.object_names import Losses
  5. from super_gradients.common.registry.registry import register_loss
  6. def onehot(indexes, N=None, ignore_index=None):
  7. """
  8. Creates a one-hot representation of indexes with N possible entries
  9. if N is not specified, it will suit the maximum index appearing.
  10. indexes is a long-tensor of indexes
  11. ignore_index will be zero in onehot representation
  12. """
  13. if N is None:
  14. N = indexes.max() + 1
  15. sz = list(indexes.size())
  16. output = indexes.new().byte().resize_(*sz, N).zero_()
  17. output.scatter_(-1, indexes.unsqueeze(-1), 1)
  18. if ignore_index is not None and ignore_index >= 0:
  19. output.masked_fill_(indexes.eq(ignore_index).unsqueeze(-1), 0)
  20. return output
  21. def _is_long(x):
  22. if hasattr(x, "data"):
  23. x = x.data
  24. return isinstance(x, torch.LongTensor) or isinstance(x, torch.cuda.LongTensor)
  25. def cross_entropy(inputs, target, weight=None, ignore_index=-100, reduction="mean", smooth_eps=None, smooth_dist=None, from_logits=True): # noqa: C901
  26. """cross entropy loss, with support for target distributions and label smoothing https://arxiv.org/abs/1512.00567"""
  27. smooth_eps = smooth_eps or 0
  28. # ordinary log-liklihood - use cross_entropy from nn
  29. if _is_long(target) and smooth_eps == 0:
  30. if from_logits:
  31. return F.cross_entropy(inputs, target, weight, ignore_index=ignore_index, reduction=reduction)
  32. else:
  33. return F.nll_loss(inputs, target, weight, ignore_index=ignore_index, reduction=reduction)
  34. if from_logits:
  35. # log-softmax of inputs
  36. lsm = F.log_softmax(inputs, dim=-1)
  37. else:
  38. lsm = inputs
  39. masked_indices = None
  40. num_classes = inputs.size(-1)
  41. if _is_long(target) and ignore_index >= 0:
  42. masked_indices = target.eq(ignore_index)
  43. if smooth_eps > 0 and smooth_dist is not None:
  44. if _is_long(target):
  45. target = onehot(target, num_classes).type_as(inputs)
  46. if smooth_dist.dim() < target.dim():
  47. smooth_dist = smooth_dist.unsqueeze(0)
  48. target.lerp_(smooth_dist, smooth_eps)
  49. if weight is not None:
  50. lsm = lsm * weight.unsqueeze(0)
  51. if _is_long(target):
  52. eps_nll = 1.0 - smooth_eps
  53. likelihood = lsm.gather(dim=-1, index=target.unsqueeze(-1)).squeeze(-1)
  54. loss = -(eps_nll * likelihood + smooth_eps * lsm.mean(-1))
  55. else:
  56. loss = -(target * lsm).sum(-1)
  57. if masked_indices is not None:
  58. loss.masked_fill_(masked_indices, 0)
  59. if reduction == "sum":
  60. loss = loss.sum()
  61. elif reduction == "mean":
  62. if masked_indices is None:
  63. loss = loss.mean()
  64. else:
  65. loss = loss.sum() / float(loss.size(0) - masked_indices.sum())
  66. return loss
  67. @register_loss(Losses.CROSS_ENTROPY)
  68. class LabelSmoothingCrossEntropyLoss(nn.CrossEntropyLoss):
  69. """CrossEntropyLoss - with ability to recieve distrbution as targets, and optional label smoothing"""
  70. def __init__(self, weight=None, ignore_index=-100, reduction="mean", smooth_eps=None, smooth_dist=None, from_logits=True):
  71. super(LabelSmoothingCrossEntropyLoss, self).__init__(weight=weight, ignore_index=ignore_index, reduction=reduction)
  72. self.smooth_eps = smooth_eps
  73. self.smooth_dist = smooth_dist
  74. self.from_logits = from_logits
  75. def forward(self, input, target, smooth_dist=None):
  76. if smooth_dist is None:
  77. smooth_dist = self.smooth_dist
  78. loss = cross_entropy(
  79. input,
  80. target,
  81. weight=self.weight,
  82. ignore_index=self.ignore_index,
  83. reduction=self.reduction,
  84. smooth_eps=self.smooth_eps,
  85. smooth_dist=smooth_dist,
  86. from_logits=self.from_logits,
  87. )
  88. # CHANGED TO THE CURRENT FORMAT- OUR CRITERION FUNCTIONS SHOULD ALL NPW RETURN A TUPLE OF (LOSS_FOR_BACKPROP, ADDITIONAL_ITEMS)
  89. # WHERE ADDITIONAL ITEMS ARE TORCH TENSORS OF SIZE (N_ITEMS,...) DETACHED FROM THEIR GRADIENTS FOR LOGGING
  90. return loss, loss.unsqueeze(0).detach()
Discard
Tip!

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