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
162
  1. import torch
  2. import torch.nn as nn
  3. import torch.nn.functional as F
  4. from torch import Tensor
  5. from collections import OrderedDict
  6. from super_gradients.training.models.sg_module import SgModule
  7. """Densenet-BC model class, based on
  8. "Densely Connected Convolutional Networks" <https://arxiv.org/pdf/1608.06993.pdf>`_
  9. Code source: https://github.com/pytorch/vision/blob/master/torchvision/models/densenet.py
  10. Performance reproducibility (4 GPUs):
  11. training params: {"max_epochs": 120, "lr_updates": [30, 60, 90, 100, 110], "lr_decay_factor": 0.1, "initial_lr": 0.025}
  12. dataset_params: {"batch_size": 64}
  13. """
  14. class _DenseLayer(nn.Module):
  15. def __init__(self, num_input_features, growth_rate, bn_size, drop_rate):
  16. super(_DenseLayer, self).__init__()
  17. self.add_module('norm1', nn.BatchNorm2d(num_input_features)),
  18. self.add_module('relu1', nn.ReLU(inplace=True)),
  19. self.add_module('conv1', nn.Conv2d(num_input_features, bn_size * growth_rate,
  20. kernel_size=1, stride=1,
  21. bias=False)),
  22. self.add_module('norm2', nn.BatchNorm2d(bn_size * growth_rate)),
  23. self.add_module('relu2', nn.ReLU(inplace=True)),
  24. self.add_module('conv2', nn.Conv2d(bn_size * growth_rate, growth_rate,
  25. kernel_size=3, stride=1, padding=1,
  26. bias=False)),
  27. self.drop_rate = float(drop_rate)
  28. def bn_function(self, inputs):
  29. concated_features = torch.cat(inputs, 1)
  30. bottleneck_output = self.conv1(self.relu1(self.norm1(concated_features))) # noqa: T484
  31. return bottleneck_output
  32. def forward(self, input): # noqa: F811
  33. prev_features = [input] if isinstance(input, Tensor) else input
  34. bottleneck_output = self.bn_function(prev_features)
  35. new_features = self.conv2(self.relu2(self.norm2(bottleneck_output)))
  36. if self.drop_rate > 0:
  37. new_features = F.dropout(new_features, p=self.drop_rate, training=self.training)
  38. return new_features
  39. class _DenseBlock(nn.ModuleDict):
  40. def __init__(self, num_layers, num_input_features, bn_size, growth_rate, drop_rate):
  41. super(_DenseBlock, self).__init__()
  42. for i in range(num_layers):
  43. layer = _DenseLayer(
  44. num_input_features + i * growth_rate,
  45. growth_rate=growth_rate,
  46. bn_size=bn_size,
  47. drop_rate=drop_rate,
  48. )
  49. self.add_module('denselayer%d' % (i + 1), layer)
  50. def forward(self, init_features):
  51. features = [init_features]
  52. for name, layer in self.items():
  53. new_features = layer(features)
  54. features.append(new_features)
  55. return torch.cat(features, 1)
  56. class _Transition(nn.Sequential):
  57. def __init__(self, num_input_features, num_output_features):
  58. super(_Transition, self).__init__()
  59. self.add_module('norm', nn.BatchNorm2d(num_input_features))
  60. self.add_module('relu', nn.ReLU(inplace=True))
  61. self.add_module('conv', nn.Conv2d(num_input_features, num_output_features,
  62. kernel_size=1, stride=1, bias=False))
  63. self.add_module('pool', nn.AvgPool2d(kernel_size=2, stride=2))
  64. class DenseNet(SgModule):
  65. def __init__(self, growth_rate: int, structure: list, num_init_features: int, bn_size: int, drop_rate: float,
  66. num_classes: int):
  67. """
  68. :param growth_rate: number of filter to add each layer (noted as 'k' in the paper)
  69. :param structure: how many layers in each pooling block - sequentially
  70. :param num_init_features: the number of filters to learn in the first convolutional layer
  71. :param bn_size: multiplicative factor for the number of bottle neck layers
  72. (i.e. bn_size * k featurs in the bottleneck)
  73. :param drop_rate: dropout rate after each dense layer
  74. :param num_classes: number of classes in the classification task
  75. """
  76. super(DenseNet, self).__init__()
  77. # First convolution
  78. self.features = nn.Sequential(OrderedDict([
  79. ('conv0', nn.Conv2d(3, num_init_features, kernel_size=7, stride=2, padding=3, bias=False)),
  80. ('norm0', nn.BatchNorm2d(num_init_features)),
  81. ('relu0', nn.ReLU(inplace=True)),
  82. ('pool0', nn.MaxPool2d(kernel_size=3, stride=2, padding=1))]))
  83. # Each denseblock
  84. num_features = num_init_features
  85. for i, num_layers in enumerate(structure):
  86. block = _DenseBlock(num_layers=num_layers, num_input_features=num_features, bn_size=bn_size,
  87. growth_rate=growth_rate, drop_rate=drop_rate)
  88. self.features.add_module('denseblock%d' % (i + 1), block)
  89. num_features = num_features + num_layers * growth_rate
  90. if i != len(structure) - 1:
  91. trans = _Transition(num_input_features=num_features, num_output_features=num_features // 2)
  92. self.features.add_module('transition%d' % (i + 1), trans)
  93. num_features = num_features // 2
  94. # Final batch norm
  95. self.features.add_module('norm5', nn.BatchNorm2d(num_features))
  96. # Linear layer
  97. self.classifier = nn.Linear(num_features, num_classes)
  98. # Official init from torch repo.
  99. for m in self.modules():
  100. if isinstance(m, nn.Conv2d):
  101. nn.init.kaiming_normal_(m.weight)
  102. elif isinstance(m, nn.BatchNorm2d):
  103. nn.init.constant_(m.weight, 1)
  104. nn.init.constant_(m.bias, 0)
  105. elif isinstance(m, nn.Linear):
  106. nn.init.constant_(m.bias, 0)
  107. def forward(self, x):
  108. features = self.features(x)
  109. out = F.relu(features, inplace=True)
  110. out = F.adaptive_avg_pool2d(out, (1, 1))
  111. out = torch.flatten(out, 1)
  112. out = self.classifier(out)
  113. return out
  114. class CustomizedDensnet(DenseNet):
  115. def __init__(self, arch_params):
  116. super().__init__(growth_rate=arch_params.growth_rate if hasattr(arch_params, "growth_rate") else 32,
  117. structure=arch_params.structure if hasattr(arch_params, "structure") else [6, 12, 24, 16],
  118. num_init_features=arch_params.num_init_features if hasattr(arch_params, "num_init_features") else 64,
  119. bn_size=arch_params.bn_size if hasattr(arch_params, "bn_size") else 4,
  120. drop_rate=arch_params.drop_rate if hasattr(arch_params, "drop_rate") else 0,
  121. num_classes=arch_params.num_classes)
  122. class DenseNet121(DenseNet):
  123. def __init__(self, arch_params):
  124. super().__init__(32, [6, 12, 24, 16], 64, 4, 0, arch_params.num_classes)
  125. class DenseNet161(DenseNet):
  126. def __init__(self, arch_params):
  127. super().__init__(48, [6, 12, 36, 24], 96, 4, 0, arch_params.num_classes)
  128. class DenseNet169(DenseNet):
  129. def __init__(self, arch_params):
  130. super().__init__(32, [6, 12, 32, 32], 64, 4, 0, arch_params.num_classes)
  131. class DenseNet201(DenseNet):
  132. def __init__(self, arch_params):
  133. super().__init__(32, [6, 12, 48, 32], 64, 4, 0, arch_params.num_classes)
Discard
Tip!

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