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
  1. '''
  2. Dual Path Networks in PyTorch.
  3. Credits: https://github.com/kuangliu/pytorch-cifar/blob/master/models/dpn.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 Bottleneck(nn.Module):
  10. def __init__(self, last_planes, in_planes, out_planes, dense_depth, stride, first_layer):
  11. super(Bottleneck, self).__init__()
  12. self.out_planes = out_planes
  13. self.dense_depth = dense_depth
  14. self.conv1 = nn.Conv2d(last_planes, in_planes, kernel_size=1, bias=False)
  15. self.bn1 = nn.BatchNorm2d(in_planes)
  16. self.conv2 = nn.Conv2d(in_planes, in_planes, kernel_size=3, stride=stride, padding=1, groups=32, bias=False)
  17. self.bn2 = nn.BatchNorm2d(in_planes)
  18. self.conv3 = nn.Conv2d(in_planes, out_planes + dense_depth, kernel_size=1, bias=False)
  19. self.bn3 = nn.BatchNorm2d(out_planes + dense_depth)
  20. self.shortcut = nn.Sequential()
  21. if first_layer:
  22. self.shortcut = nn.Sequential(
  23. nn.Conv2d(last_planes, out_planes + dense_depth, kernel_size=1, stride=stride, bias=False),
  24. nn.BatchNorm2d(out_planes + dense_depth)
  25. )
  26. def forward(self, x):
  27. out = F.relu(self.bn1(self.conv1(x)))
  28. out = F.relu(self.bn2(self.conv2(out)))
  29. out = self.bn3(self.conv3(out))
  30. x = self.shortcut(x)
  31. d = self.out_planes
  32. out = torch.cat([x[:, :d, :, :] + out[:, :d, :, :], x[:, d:, :, :], out[:, d:, :, :]], 1)
  33. out = F.relu(out)
  34. return out
  35. class DPN(SgModule):
  36. def __init__(self, cfg):
  37. super(DPN, self).__init__()
  38. in_planes, out_planes = cfg['in_planes'], cfg['out_planes']
  39. num_blocks, dense_depth = cfg['num_blocks'], cfg['dense_depth']
  40. self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)
  41. self.bn1 = nn.BatchNorm2d(64)
  42. self.last_planes = 64
  43. self.layer1 = self._make_layer(in_planes[0], out_planes[0], num_blocks[0], dense_depth[0], stride=1)
  44. self.layer2 = self._make_layer(in_planes[1], out_planes[1], num_blocks[1], dense_depth[1], stride=2)
  45. self.layer3 = self._make_layer(in_planes[2], out_planes[2], num_blocks[2], dense_depth[2], stride=2)
  46. self.layer4 = self._make_layer(in_planes[3], out_planes[3], num_blocks[3], dense_depth[3], stride=2)
  47. self.linear = nn.Linear(out_planes[3] + (num_blocks[3] + 1) * dense_depth[3], 10)
  48. def _make_layer(self, in_planes, out_planes, num_blocks, dense_depth, stride):
  49. strides = [stride] + [1] * (num_blocks - 1)
  50. layers = []
  51. for i, stride in enumerate(strides):
  52. layers.append(Bottleneck(self.last_planes, in_planes, out_planes, dense_depth, stride, i == 0))
  53. self.last_planes = out_planes + (i + 2) * dense_depth
  54. return nn.Sequential(*layers)
  55. def forward(self, x):
  56. out = F.relu(self.bn1(self.conv1(x)))
  57. out = self.layer1(out)
  58. out = self.layer2(out)
  59. out = self.layer3(out)
  60. out = self.layer4(out)
  61. out = F.avg_pool2d(out, 4)
  62. out = out.view(out.size(0), -1)
  63. out = self.linear(out)
  64. return out
  65. def DPN26():
  66. cfg = {
  67. 'in_planes': (96, 192, 384, 768),
  68. 'out_planes': (256, 512, 1024, 2048),
  69. 'num_blocks': (2, 2, 2, 2),
  70. 'dense_depth': (16, 32, 24, 128)
  71. }
  72. return DPN(cfg)
  73. def DPN92():
  74. cfg = {
  75. 'in_planes': (96, 192, 384, 768),
  76. 'out_planes': (256, 512, 1024, 2048),
  77. 'num_blocks': (3, 4, 20, 3),
  78. 'dense_depth': (16, 32, 24, 128)
  79. }
  80. return DPN(cfg)
  81. def test():
  82. net = DPN92()
  83. x = torch.randn(1, 3, 32, 32)
  84. y = net(x)
  85. print(y)
  86. # test()
Discard
Tip!

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