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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
  1. '''PNASNet in PyTorch.
  2. Paper: Progressive Neural Architecture Search
  3. https://github.com/kuangliu/pytorch-cifar/blob/master/models/pnasnet.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 SepConv(nn.Module):
  10. '''Separable Convolution.'''
  11. def __init__(self, in_planes, out_planes, kernel_size, stride):
  12. super(SepConv, self).__init__()
  13. self.conv1 = nn.Conv2d(in_planes, out_planes,
  14. kernel_size, stride,
  15. padding=(kernel_size - 1) // 2,
  16. bias=False, groups=in_planes)
  17. self.bn1 = nn.BatchNorm2d(out_planes)
  18. def forward(self, x):
  19. return self.bn1(self.conv1(x))
  20. class CellA(nn.Module):
  21. def __init__(self, in_planes, out_planes, stride=1):
  22. super(CellA, self).__init__()
  23. self.stride = stride
  24. self.sep_conv1 = SepConv(in_planes, out_planes, kernel_size=7, stride=stride)
  25. if stride == 2:
  26. self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False)
  27. self.bn1 = nn.BatchNorm2d(out_planes)
  28. def forward(self, x):
  29. y1 = self.sep_conv1(x)
  30. y2 = F.max_pool2d(x, kernel_size=3, stride=self.stride, padding=1)
  31. if self.stride == 2:
  32. y2 = self.bn1(self.conv1(y2))
  33. return F.relu(y1 + y2)
  34. class CellB(nn.Module):
  35. def __init__(self, in_planes, out_planes, stride=1):
  36. super(CellB, self).__init__()
  37. self.stride = stride
  38. # Left branch
  39. self.sep_conv1 = SepConv(in_planes, out_planes, kernel_size=7, stride=stride)
  40. self.sep_conv2 = SepConv(in_planes, out_planes, kernel_size=3, stride=stride)
  41. # Right branch
  42. self.sep_conv3 = SepConv(in_planes, out_planes, kernel_size=5, stride=stride)
  43. if stride == 2:
  44. self.conv1 = nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False)
  45. self.bn1 = nn.BatchNorm2d(out_planes)
  46. # Reduce channels
  47. self.conv2 = nn.Conv2d(2 * out_planes, out_planes, kernel_size=1, stride=1, padding=0, bias=False)
  48. self.bn2 = nn.BatchNorm2d(out_planes)
  49. def forward(self, x):
  50. # Left branch
  51. y1 = self.sep_conv1(x)
  52. y2 = self.sep_conv2(x)
  53. # Right branch
  54. y3 = F.max_pool2d(x, kernel_size=3, stride=self.stride, padding=1)
  55. if self.stride == 2:
  56. y3 = self.bn1(self.conv1(y3))
  57. y4 = self.sep_conv3(x)
  58. # Concat & reduce channels
  59. b1 = F.relu(y1 + y2)
  60. b2 = F.relu(y3 + y4)
  61. y = torch.cat([b1, b2], 1)
  62. return F.relu(self.bn2(self.conv2(y)))
  63. class PNASNet(SgModule):
  64. def __init__(self, cell_type, num_cells, num_planes):
  65. super(PNASNet, self).__init__()
  66. self.in_planes = num_planes
  67. self.cell_type = cell_type
  68. self.conv1 = nn.Conv2d(3, num_planes, kernel_size=3, stride=1, padding=1, bias=False)
  69. self.bn1 = nn.BatchNorm2d(num_planes)
  70. self.layer1 = self._make_layer(num_planes, num_cells=6)
  71. self.layer2 = self._downsample(num_planes * 2)
  72. self.layer3 = self._make_layer(num_planes * 2, num_cells=6)
  73. self.layer4 = self._downsample(num_planes * 4)
  74. self.layer5 = self._make_layer(num_planes * 4, num_cells=6)
  75. self.linear = nn.Linear(num_planes * 4, 10)
  76. def _make_layer(self, planes, num_cells):
  77. layers = []
  78. for _ in range(num_cells):
  79. layers.append(self.cell_type(self.in_planes, planes, stride=1))
  80. self.in_planes = planes
  81. return nn.Sequential(*layers)
  82. def _downsample(self, planes):
  83. layer = self.cell_type(self.in_planes, planes, stride=2)
  84. self.in_planes = planes
  85. return layer
  86. def forward(self, x):
  87. out = F.relu(self.bn1(self.conv1(x)))
  88. out = self.layer1(out)
  89. out = self.layer2(out)
  90. out = self.layer3(out)
  91. out = self.layer4(out)
  92. out = self.layer5(out)
  93. out = F.avg_pool2d(out, 8)
  94. out = self.linear(out.view(out.size(0), -1))
  95. return out
  96. def PNASNetA():
  97. return PNASNet(CellA, num_cells=6, num_planes=44)
  98. def PNASNetB():
  99. return PNASNet(CellB, num_cells=6, num_planes=32)
  100. def test():
  101. net = PNASNetB()
  102. x = torch.randn(1, 3, 32, 32)
  103. y = net(x)
  104. print(y)
  105. # test()
Discard
Tip!

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