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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
  1. """ResNeXt in PyTorch.
  2. See the paper "Aggregated Residual Transformations for Deep Neural Networks" for more details.
  3. Code adapted from https://github.com/pytorch/vision/blob/master/torchvision/models/resnet.py
  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. def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
  9. """3x3 convolution with padding"""
  10. return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
  11. padding=dilation, groups=groups, bias=False, dilation=dilation)
  12. def conv1x1(in_planes, out_planes, stride=1):
  13. """1x1 convolution"""
  14. return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
  15. class GroupedConvBlock(nn.Module):
  16. """Grouped convolution block."""
  17. expansion = 4
  18. def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
  19. base_width=64, dilation=1, norm_layer=None):
  20. super(GroupedConvBlock, self).__init__()
  21. if norm_layer is None:
  22. norm_layer = nn.BatchNorm2d
  23. self.norm_layer = norm_layer
  24. width = int(planes * (base_width / 64.)) * groups
  25. # Both self.conv2 and self.downsample layers downsample the input when stride != 1
  26. self.conv1 = conv1x1(inplanes, width)
  27. self.bn1 = norm_layer(width)
  28. self.conv2 = conv3x3(width, width, stride, groups, dilation)
  29. self.bn2 = norm_layer(width)
  30. self.conv3 = conv1x1(width, planes * self.expansion)
  31. self.bn3 = norm_layer(planes * self.expansion)
  32. self.relu = nn.ReLU(inplace=True)
  33. self.downsample = downsample
  34. self.stride = stride
  35. def forward(self, x):
  36. identity = x
  37. out = self.conv1(x)
  38. out = self.bn1(out)
  39. out = self.relu(out)
  40. out = self.conv2(out)
  41. out = self.bn2(out)
  42. out = self.relu(out)
  43. out = self.conv3(out)
  44. out = self.bn3(out)
  45. if self.downsample is not None:
  46. identity = self.downsample(x)
  47. out += identity
  48. out = self.relu(out)
  49. return out
  50. class ResNeXt(SgModule):
  51. def __init__(self, layers, cardinality, bottleneck_width, num_classes=10, replace_stride_with_dilation=None):
  52. super(ResNeXt, self).__init__()
  53. if replace_stride_with_dilation is None:
  54. # each element in the tuple indicates if we should replace
  55. # the 2x2 stride with a dilated convolution instead
  56. replace_stride_with_dilation = [False, False, False]
  57. if len(replace_stride_with_dilation) != 3:
  58. raise ValueError("replace_stride_with_dilation should be None "
  59. "or a 3-element tuple, got {}".format(replace_stride_with_dilation))
  60. self.cardinality = cardinality
  61. self.dilation = 1
  62. self.inplanes = 64
  63. self.base_width = bottleneck_width
  64. self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3, bias=False)
  65. self.bn1 = nn.BatchNorm2d(self.inplanes)
  66. self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
  67. self.layer1 = self._make_layer(GroupedConvBlock, 64, layers[0])
  68. self.layer2 = self._make_layer(GroupedConvBlock, 128, layers[1], stride=2,
  69. dilate=replace_stride_with_dilation[0])
  70. self.layer3 = self._make_layer(GroupedConvBlock, 256, layers[2], stride=2,
  71. dilate=replace_stride_with_dilation[1])
  72. self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
  73. if len(layers) == 4:
  74. self.layer4 = self._make_layer(GroupedConvBlock, 512, layers[3], stride=2,
  75. dilate=replace_stride_with_dilation[2])
  76. end_width = 512 if len(layers) == 4 else 256
  77. self.fc = nn.Linear(end_width * GroupedConvBlock.expansion, num_classes)
  78. def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
  79. norm_layer = nn.BatchNorm2d
  80. downsample = None
  81. previous_dilation = self.dilation
  82. if dilate:
  83. self.dilation *= stride
  84. stride = 1
  85. if stride != 1 or self.inplanes != planes * block.expansion:
  86. downsample = nn.Sequential(
  87. conv1x1(self.inplanes, planes * block.expansion, stride),
  88. norm_layer(planes * block.expansion),
  89. )
  90. layers = [block(self.inplanes, planes, stride, downsample, self.cardinality,
  91. self.base_width, previous_dilation, norm_layer)]
  92. self.inplanes = planes * block.expansion
  93. for _ in range(1, blocks):
  94. layers.append(block(self.inplanes, planes, groups=self.cardinality,
  95. base_width=self.base_width, dilation=self.dilation,
  96. norm_layer=norm_layer))
  97. return nn.Sequential(*layers)
  98. def forward(self, x):
  99. out = F.relu(self.bn1(self.conv1(x)))
  100. out = self.maxpool(out)
  101. out = self.layer1(out)
  102. out = self.layer2(out)
  103. out = self.layer3(out)
  104. if self.layer4 is not None:
  105. out = self.layer4(out)
  106. out = self.avgpool(out)
  107. out = out.view(out.size(0), -1)
  108. out = self.fc(out)
  109. return out
  110. class CustomizedResNeXt(ResNeXt):
  111. def __init__(self, arch_params):
  112. super(CustomizedResNeXt, self).__init__(layers=arch_params.structure if hasattr(arch_params, "structure") else [3, 3, 3],
  113. bottleneck_width=arch_params.num_init_features if hasattr(arch_params, "bottleneck_width") else 64,
  114. cardinality=arch_params.bn_size if hasattr(arch_params, "cardinality") else 32,
  115. num_classes=arch_params.num_classes,
  116. replace_stride_with_dilation=arch_params.replace_stride_with_dilation if
  117. hasattr(arch_params, "replace_stride_with_dilation") else None)
  118. class ResNeXt50(ResNeXt):
  119. def __init__(self, arch_params):
  120. super(ResNeXt50, self).__init__(layers=[3, 4, 6, 3], cardinality=32, bottleneck_width=4,
  121. num_classes=arch_params.num_classes)
  122. class ResNeXt101(ResNeXt):
  123. def __init__(self, arch_params):
  124. super(ResNeXt101, self).__init__(layers=[3, 4, 23, 3], cardinality=32, bottleneck_width=8,
  125. num_classes=arch_params.num_classes)
Discard
Tip!

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