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
58
59
60
61
62
  1. """MobileNet in PyTorch.
  2. See the paper "MobileNets: Efficient Convolutional Neural Networks for Mobile Vision Applications"
  3. for more details.
  4. """
  5. import torch.nn as nn
  6. import torch.nn.functional as F
  7. from super_gradients.training.models.sg_module import SgModule
  8. class Block(nn.Module):
  9. """Depthwise conv + Pointwise conv"""
  10. def __init__(self, in_planes, out_planes, stride=1):
  11. super(Block, self).__init__()
  12. self.conv1 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=stride, padding=1, groups=in_planes, bias=False)
  13. self.bn1 = nn.BatchNorm2d(in_planes)
  14. self.conv2 = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False)
  15. self.bn2 = nn.BatchNorm2d(out_planes)
  16. def forward(self, x):
  17. out = F.relu(self.bn1(self.conv1(x)))
  18. out = F.relu(self.bn2(self.conv2(out)))
  19. return out
  20. class MobileNet(SgModule):
  21. # (128,2) means conv planes=128, conv stride=2, by default conv stride=1
  22. cfg = [64, 128, (128, 2), 256, (256, 2), 512, 512, 512, 512, 512, (512, 2), 1024, (1024, 2)]
  23. def __init__(self, num_classes=10, backbone_mode=False, up_to_layer=None, in_channels: int = 3):
  24. super(MobileNet, self).__init__()
  25. self.backbone_mode = backbone_mode
  26. self.conv1 = nn.Conv2d(in_channels, 32, kernel_size=3, stride=2, padding=1, bias=False)
  27. self.bn1 = nn.BatchNorm2d(32)
  28. self.layers = self._make_layers(in_planes=32, up_to_layer=up_to_layer if up_to_layer is not None else len(self.cfg))
  29. if not self.backbone_mode:
  30. self.linear = nn.Linear(self.cfg[-1], num_classes)
  31. def _make_layers(self, in_planes, up_to_layer):
  32. layers = []
  33. for x in self.cfg[:up_to_layer]:
  34. out_planes = x if isinstance(x, int) else x[0]
  35. stride = 1 if isinstance(x, int) else x[1]
  36. layers.append(Block(in_planes, out_planes, stride))
  37. in_planes = out_planes
  38. return nn.Sequential(*layers)
  39. def forward(self, x):
  40. """
  41. :param up_to_layer: forward through the net layers up to a specific layer. if None, run all layers
  42. """
  43. out = F.relu(self.bn1(self.conv1(x)))
  44. out = self.layers(out)
  45. if not self.backbone_mode:
  46. out = F.avg_pool2d(out, 2)
  47. out = out.view(out.size(0), -1)
  48. out = self.linear(out)
  49. return out
Discard
Tip!

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