Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

export_utils_test.py 4.6 KB

You have to be logged in to leave a comment. Sign In
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. import copy
  2. import torch
  3. from torch import nn
  4. import unittest
  5. from super_gradients import ARCHITECTURES
  6. from super_gradients.training.utils.utils import HpmStruct
  7. from super_gradients.training.utils.export_utils import fuse_conv_bn
  8. def count_parameters(model):
  9. return sum(p.numel() for p in model.parameters() if p.requires_grad)
  10. class TestUtil(unittest.TestCase):
  11. def test_fuse_conv_bn_real_archs(self):
  12. """
  13. test the fuse_conv_bn function. run the function on some Sg architectures and assert
  14. the result of the original net are the same as the results of the fused net
  15. """
  16. archs = ['resnet18', 'mobilenet_v2', 'densenet121', 'regnetY200', 'yolo_v5s' ]
  17. for arch_name in archs:
  18. model1 = ARCHITECTURES[arch_name](HpmStruct(**{'num_classes': 10, 'dropout': 0.1}))
  19. model2 = copy.deepcopy(model1)
  20. model1.eval()
  21. model2.eval()
  22. fuse_conv_bn(model2, True)
  23. input = torch.rand(size=(1, 3, 320, 320))
  24. output1 = model1(input)[0]
  25. output2 = model2(input)[0]
  26. param_count1 = count_parameters(model1)
  27. param_count2 = count_parameters(model2)
  28. self.assertTrue(torch.allclose(output1, output2, atol=1e-6))
  29. print(f'Tested fuse Conv BN on {arch_name}: OK ({param_count1 - param_count2} less params)')
  30. def test_fuse_conv_bn_on_sequential_models(self):
  31. # assert the bn module was replaced with Identity
  32. model = nn.Sequential(nn.Conv2d(3, 3, 3), nn.BatchNorm2d(3))
  33. model.eval()
  34. fuse_conv_bn(model, replace_bn_with_identity=True)
  35. self.assertEqual(len(model._modules), 2)
  36. self.assertIsInstance(model._modules['0'], nn.Conv2d)
  37. self.assertIsInstance(model._modules['1'], nn.Identity)
  38. # assert the bn module was removed
  39. model = nn.Sequential(nn.Conv2d(3, 3, 3), nn.BatchNorm2d(3))
  40. model.eval()
  41. fuse_conv_bn(model, replace_bn_with_identity=False)
  42. self.assertEqual(len(model._modules), 1)
  43. self.assertIsInstance(model._modules['0'], nn.Conv2d)
  44. # assert all bn module were removed
  45. model = nn.Sequential(nn.Conv2d(3, 3, 3), nn.BatchNorm2d(3), nn.Conv2d(3, 3, 3), nn.BatchNorm2d(3))
  46. model.eval()
  47. fuse_conv_bn(model, replace_bn_with_identity=False)
  48. self.assertEqual(len(model._modules), 2)
  49. self.assertIsInstance(model._modules['0'], nn.Conv2d)
  50. # assert only merged bn module were removed
  51. model = nn.Sequential(nn.Conv2d(3, 3, 3), nn.Conv2d(3, 3, 3), nn.BatchNorm2d(3))
  52. model.eval()
  53. fuse_conv_bn(model, replace_bn_with_identity=False)
  54. self.assertEqual(len(model._modules), 2)
  55. self.assertIsInstance(model._modules['0'], nn.Conv2d)
  56. self.assertIsInstance(model._modules['1'], nn.Conv2d)
  57. def test_fuse_conv_bn_on_toy_models(self):
  58. class Toy(nn.Module):
  59. def __init__(self):
  60. super().__init__()
  61. self.conv1 = nn.Conv2d(3, 3, 3)
  62. self.bn1 = nn.BatchNorm2d(3)
  63. self.conv2 = nn.Conv2d(3, 3, 3)
  64. self.bn2 = nn.BatchNorm2d(3)
  65. def forward(self, x):
  66. x = self.conv1(x)
  67. x = self.bn1(x)
  68. x = self.conv2(x)
  69. x = self.bn2(x)
  70. return x
  71. # assert the bn module was replaced with Identity
  72. model = Toy()
  73. model.eval()
  74. fuse_conv_bn(model, replace_bn_with_identity=True)
  75. self.assertIsNotNone(model.bn1)
  76. self.assertIsInstance(model.conv1, nn.Conv2d)
  77. self.assertIsInstance(model.bn1, nn.Identity)
  78. # assert the bn module was removed
  79. model = Toy()
  80. model.eval()
  81. fuse_conv_bn(model, replace_bn_with_identity=False)
  82. self.assertFalse(hasattr(model, 'bn1'))
  83. self.assertIsInstance(model.conv1, nn.Conv2d)
  84. # assert all bn module were removed
  85. model = Toy()
  86. model.eval()
  87. fuse_conv_bn(model, replace_bn_with_identity=False)
  88. self.assertFalse(hasattr(model, 'bn1'))
  89. self.assertIsInstance(model.conv1, nn.Conv2d)
  90. self.assertFalse(hasattr(model, 'bn2'))
  91. self.assertIsInstance(model.conv2, nn.Conv2d)
  92. # assert correct number of parameters removed
  93. model = Toy()
  94. model.eval()
  95. before = count_parameters(model)
  96. fuse_conv_bn(model, replace_bn_with_identity=False)
  97. after = count_parameters(model)
  98. self.assertEqual(before - after, 12) # each bn of 3 channels has 6 parameters (12 together)
  99. if __name__ == '__main__':
  100. unittest.main()
Tip!

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

Comments

Loading...