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
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
112
113
114
  1. '''ShuffleNet in PyTorch.
  2. See the paper "ShuffleNet: An Extremely Efficient Convolutional Neural Network for Mobile Devices" for more details.
  3. https://github.com/kuangliu/pytorch-cifar/blob/master/models/shufflenet.py
  4. '''
  5. import torch
  6. import torch.nn as nn
  7. import torch.nn.functional as F
  8. from super_gradients.training.models.sg_module import SgModule
  9. class ShuffleBlock(nn.Module):
  10. def __init__(self, groups):
  11. super(ShuffleBlock, self).__init__()
  12. self.groups = groups
  13. def forward(self, x):
  14. '''Channel shuffle: [N,C,H,W] -> [N,g,C/g,H,W] -> [N,C/g,g,H,w] -> [N,C,H,W]'''
  15. N, C, H, W = x.size()
  16. g = self.groups
  17. return x.view(N, g, C // g, H, W).permute(0, 2, 1, 3, 4).reshape(N, C, H, W)
  18. class Bottleneck(nn.Module):
  19. def __init__(self, in_planes, out_planes, stride, groups):
  20. super(Bottleneck, self).__init__()
  21. self.stride = stride
  22. mid_planes = out_planes / 4
  23. g = 1 if in_planes == 24 else groups
  24. self.conv1 = nn.Conv2d(in_planes, mid_planes, kernel_size=1, groups=g, bias=False)
  25. self.bn1 = nn.BatchNorm2d(mid_planes)
  26. self.shuffle1 = ShuffleBlock(groups=g)
  27. self.conv2 = nn.Conv2d(mid_planes, mid_planes, kernel_size=3, stride=stride, padding=1, groups=mid_planes,
  28. bias=False)
  29. self.bn2 = nn.BatchNorm2d(mid_planes)
  30. self.conv3 = nn.Conv2d(mid_planes, out_planes, kernel_size=1, groups=groups, bias=False)
  31. self.bn3 = nn.BatchNorm2d(out_planes)
  32. self.shortcut = nn.Sequential()
  33. if stride == 2:
  34. self.shortcut = nn.Sequential(nn.AvgPool2d(3, stride=2, padding=1))
  35. def forward(self, x):
  36. out = F.relu(self.bn1(self.conv1(x)))
  37. out = self.shuffle1(out)
  38. out = F.relu(self.bn2(self.conv2(out)))
  39. out = self.bn3(self.conv3(out))
  40. res = self.shortcut(x)
  41. out = F.relu(torch.cat([out, res], 1)) if self.stride == 2 else F.relu(out + res)
  42. return out
  43. class ShuffleNet(SgModule):
  44. def __init__(self, cfg):
  45. super(ShuffleNet, self).__init__()
  46. out_planes = cfg['out_planes']
  47. num_blocks = cfg['num_blocks']
  48. groups = cfg['groups']
  49. self.conv1 = nn.Conv2d(3, 24, kernel_size=1, bias=False)
  50. self.bn1 = nn.BatchNorm2d(24)
  51. self.in_planes = 24
  52. self.layer1 = self._make_layer(out_planes[0], num_blocks[0], groups)
  53. self.layer2 = self._make_layer(out_planes[1], num_blocks[1], groups)
  54. self.layer3 = self._make_layer(out_planes[2], num_blocks[2], groups)
  55. self.linear = nn.Linear(out_planes[2], 10)
  56. def _make_layer(self, out_planes, num_blocks, groups):
  57. layers = []
  58. for i in range(num_blocks):
  59. stride = 2 if i == 0 else 1
  60. cat_planes = self.in_planes if i == 0 else 0
  61. layers.append(Bottleneck(self.in_planes, out_planes - cat_planes, stride=stride, groups=groups))
  62. self.in_planes = out_planes
  63. return nn.Sequential(*layers)
  64. def forward(self, x):
  65. out = F.relu(self.bn1(self.conv1(x)))
  66. out = self.layer1(out)
  67. out = self.layer2(out)
  68. out = self.layer3(out)
  69. out = F.avg_pool2d(out, 4)
  70. out = out.view(out.size(0), -1)
  71. out = self.linear(out)
  72. return out
  73. def ShuffleNetG2():
  74. cfg = {
  75. 'out_planes': [200, 400, 800],
  76. 'num_blocks': [4, 8, 4],
  77. 'groups': 2
  78. }
  79. return ShuffleNet(cfg)
  80. def ShuffleNetG3():
  81. cfg = {
  82. 'out_planes': [240, 480, 960],
  83. 'num_blocks': [4, 8, 4],
  84. 'groups': 3
  85. }
  86. return ShuffleNet(cfg)
  87. def test():
  88. net = ShuffleNetG2()
  89. x = torch.randn(1, 3, 32, 32)
  90. y = net(x)
  91. print(y)
  92. # test()
Discard
Tip!

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