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

#378 Feature/sg 281 add kd notebook

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:feature/SG-281-add_kd_notebook
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
  1. import torch
  2. from torch import nn
  3. from torch.autograd import Variable
  4. class ShelfNetSemanticEncodingLoss(nn.CrossEntropyLoss):
  5. """2D Cross Entropy Loss with Auxilary Loss"""
  6. # FIXME - THIS LOSS SHOULD BE CHANGED TO SUPPORT APEX
  7. def __init__(self, se_weight=0.2, nclass=21, aux_weight=0.4, weight=None, ignore_index=-1):
  8. super().__init__(weight, None, ignore_index)
  9. self.nclass = nclass
  10. self.se_weight = se_weight
  11. self.aux_weight = aux_weight
  12. # FIXME - TEST CODE LOTEM, CHANGED IN ORDER TO WORK WITH apex.amp
  13. self.bcewithlogitsloss = nn.BCELoss(weight)
  14. def forward(self, logits, labels):
  15. pred1, se_pred, pred2 = logits
  16. batch = labels.size(0)
  17. se_target = Variable(torch.zeros(batch, self.nclass))
  18. # FIXME - THIS IS WHAT apex MIGHT BE FAILING TO WORK WITH
  19. for i in range(batch):
  20. hist = torch.histc(labels[i].cpu().data.float(),
  21. bins=self.nclass, min=0,
  22. max=self.nclass - 1)
  23. vect = hist > 0
  24. se_target[i] = vect
  25. loss1 = super().forward(pred1, labels)
  26. loss2 = super().forward(pred2, labels)
  27. loss3 = self.bcewithlogitsloss(torch.sigmoid(se_pred), se_target.data.cuda()) # FIXME - MAYBE CHANGE TO SIGMOID
  28. total_loss = loss1 + self.aux_weight * loss2 + self.se_weight * loss3
  29. losses = [loss1, loss2, loss3, total_loss]
  30. return total_loss, torch.stack(losses, dim=0).detach()
Discard
Tip!

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