test_shufflenet_v1.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. from unittest import TestCase
  3. import torch
  4. from torch.nn.modules import GroupNorm
  5. from torch.nn.modules.batchnorm import _BatchNorm
  6. from mmpose.models.backbones import ShuffleNetV1
  7. from mmpose.models.backbones.shufflenet_v1 import ShuffleUnit
  8. class TestShufflenetV1(TestCase):
  9. @staticmethod
  10. def is_block(modules):
  11. """Check if is ResNet building block."""
  12. if isinstance(modules, (ShuffleUnit, )):
  13. return True
  14. return False
  15. @staticmethod
  16. def is_norm(modules):
  17. """Check if is one of the norms."""
  18. if isinstance(modules, (GroupNorm, _BatchNorm)):
  19. return True
  20. return False
  21. @staticmethod
  22. def check_norm_state(modules, train_state):
  23. """Check if norm layer is in correct train state."""
  24. for mod in modules:
  25. if isinstance(mod, _BatchNorm):
  26. if mod.training != train_state:
  27. return False
  28. return True
  29. def test_shufflenetv1_shuffleuint(self):
  30. with self.assertRaises(ValueError):
  31. # combine must be in ['add', 'concat']
  32. ShuffleUnit(24, 16, groups=3, first_block=True, combine='test')
  33. with self.assertRaises(AssertionError):
  34. # inplanes must be equal tp = outplanes when combine='add'
  35. ShuffleUnit(64, 24, groups=4, first_block=True, combine='add')
  36. # Test ShuffleUnit with combine='add'
  37. block = ShuffleUnit(24, 24, groups=3, first_block=True, combine='add')
  38. x = torch.randn(1, 24, 56, 56)
  39. x_out = block(x)
  40. self.assertEqual(x_out.shape, torch.Size((1, 24, 56, 56)))
  41. # Test ShuffleUnit with combine='concat'
  42. block = ShuffleUnit(
  43. 24, 240, groups=3, first_block=True, combine='concat')
  44. x = torch.randn(1, 24, 56, 56)
  45. x_out = block(x)
  46. self.assertEqual(x_out.shape, torch.Size((1, 240, 28, 28)))
  47. # Test ShuffleUnit with checkpoint forward
  48. block = ShuffleUnit(
  49. 24, 24, groups=3, first_block=True, combine='add', with_cp=True)
  50. self.assertTrue(block.with_cp)
  51. x = torch.randn(1, 24, 56, 56)
  52. x.requires_grad = True
  53. x_out = block(x)
  54. self.assertEqual(x_out.shape, torch.Size((1, 24, 56, 56)))
  55. def test_shufflenetv1_backbone(self):
  56. with self.assertRaises(ValueError):
  57. # frozen_stages must be in range(-1, 4)
  58. ShuffleNetV1(frozen_stages=10)
  59. with self.assertRaises(ValueError):
  60. # the item in out_indices must be in range(0, 4)
  61. ShuffleNetV1(out_indices=[5])
  62. with self.assertRaises(ValueError):
  63. # groups must be in [1, 2, 3, 4, 8]
  64. ShuffleNetV1(groups=10)
  65. # Test ShuffleNetV1 norm state
  66. model = ShuffleNetV1()
  67. model.init_weights()
  68. model.train()
  69. self.assertTrue(self.check_norm_state(model.modules(), True))
  70. # Test ShuffleNetV1 with first stage frozen
  71. frozen_stages = 1
  72. model = ShuffleNetV1(
  73. frozen_stages=frozen_stages, out_indices=(0, 1, 2))
  74. model.init_weights()
  75. model.train()
  76. for param in model.conv1.parameters():
  77. self.assertFalse(param.requires_grad)
  78. for i in range(frozen_stages):
  79. layer = model.layers[i]
  80. for mod in layer.modules():
  81. if isinstance(mod, _BatchNorm):
  82. self.assertFalse(mod.training)
  83. for param in layer.parameters():
  84. self.assertFalse(param.requires_grad)
  85. # Test ShuffleNetV1 forward with groups=1
  86. model = ShuffleNetV1(groups=1, out_indices=(0, 1, 2))
  87. model.init_weights()
  88. model.train()
  89. for m in model.modules():
  90. if self.is_norm(m):
  91. self.assertIsInstance(m, _BatchNorm)
  92. imgs = torch.randn(1, 3, 224, 224)
  93. feat = model(imgs)
  94. self.assertEqual(len(feat), 3)
  95. self.assertEqual(feat[0].shape, torch.Size((1, 144, 28, 28)))
  96. self.assertEqual(feat[1].shape, torch.Size((1, 288, 14, 14)))
  97. self.assertEqual(feat[2].shape, torch.Size((1, 576, 7, 7)))
  98. # Test ShuffleNetV1 forward with groups=2
  99. model = ShuffleNetV1(groups=2, out_indices=(0, 1, 2))
  100. model.init_weights()
  101. model.train()
  102. for m in model.modules():
  103. if self.is_norm(m):
  104. self.assertIsInstance(m, _BatchNorm)
  105. imgs = torch.randn(1, 3, 224, 224)
  106. feat = model(imgs)
  107. self.assertEqual(len(feat), 3)
  108. self.assertEqual(feat[0].shape, torch.Size((1, 200, 28, 28)))
  109. self.assertEqual(feat[1].shape, torch.Size((1, 400, 14, 14)))
  110. self.assertEqual(feat[2].shape, torch.Size((1, 800, 7, 7)))
  111. # Test ShuffleNetV1 forward with groups=3
  112. model = ShuffleNetV1(groups=3, out_indices=(0, 1, 2))
  113. model.init_weights()
  114. model.train()
  115. for m in model.modules():
  116. if self.is_norm(m):
  117. self.assertIsInstance(m, _BatchNorm)
  118. imgs = torch.randn(1, 3, 224, 224)
  119. feat = model(imgs)
  120. self.assertEqual(len(feat), 3)
  121. self.assertEqual(feat[0].shape, torch.Size((1, 240, 28, 28)))
  122. self.assertEqual(feat[1].shape, torch.Size((1, 480, 14, 14)))
  123. self.assertEqual(feat[2].shape, torch.Size((1, 960, 7, 7)))
  124. # Test ShuffleNetV1 forward with groups=4
  125. model = ShuffleNetV1(groups=4, out_indices=(0, 1, 2))
  126. model.init_weights()
  127. model.train()
  128. for m in model.modules():
  129. if self.is_norm(m):
  130. self.assertIsInstance(m, _BatchNorm)
  131. imgs = torch.randn(1, 3, 224, 224)
  132. feat = model(imgs)
  133. self.assertEqual(len(feat), 3)
  134. self.assertEqual(feat[0].shape, torch.Size((1, 272, 28, 28)))
  135. self.assertEqual(feat[1].shape, torch.Size((1, 544, 14, 14)))
  136. self.assertEqual(feat[2].shape, torch.Size((1, 1088, 7, 7)))
  137. # Test ShuffleNetV1 forward with groups=8
  138. model = ShuffleNetV1(groups=8, out_indices=(0, 1, 2))
  139. model.init_weights()
  140. model.train()
  141. for m in model.modules():
  142. if self.is_norm(m):
  143. self.assertIsInstance(m, _BatchNorm)
  144. imgs = torch.randn(1, 3, 224, 224)
  145. feat = model(imgs)
  146. self.assertEqual(len(feat), 3)
  147. self.assertEqual(feat[0].shape, torch.Size((1, 384, 28, 28)))
  148. self.assertEqual(feat[1].shape, torch.Size((1, 768, 14, 14)))
  149. self.assertEqual(feat[2].shape, torch.Size((1, 1536, 7, 7)))
  150. # Test ShuffleNetV1 forward with GroupNorm forward
  151. model = ShuffleNetV1(
  152. groups=3,
  153. norm_cfg=dict(type='GN', num_groups=2, requires_grad=True),
  154. out_indices=(0, 1, 2))
  155. model.init_weights()
  156. model.train()
  157. for m in model.modules():
  158. if self.is_norm(m):
  159. self.assertIsInstance(m, GroupNorm)
  160. imgs = torch.randn(1, 3, 224, 224)
  161. feat = model(imgs)
  162. self.assertEqual(len(feat), 3)
  163. self.assertEqual(feat[0].shape, torch.Size((1, 240, 28, 28)))
  164. self.assertEqual(feat[1].shape, torch.Size((1, 480, 14, 14)))
  165. self.assertEqual(feat[2].shape, torch.Size((1, 960, 7, 7)))
  166. # Test ShuffleNetV1 forward with layers 1, 2 forward
  167. model = ShuffleNetV1(groups=3, out_indices=(1, 2))
  168. model.init_weights()
  169. model.train()
  170. for m in model.modules():
  171. if self.is_norm(m):
  172. self.assertIsInstance(m, _BatchNorm)
  173. imgs = torch.randn(1, 3, 224, 224)
  174. feat = model(imgs)
  175. self.assertEqual(len(feat), 2)
  176. self.assertEqual(feat[0].shape, torch.Size((1, 480, 14, 14)))
  177. self.assertEqual(feat[1].shape, torch.Size((1, 960, 7, 7)))
  178. # Test ShuffleNetV1 forward with layers 2 forward
  179. model = ShuffleNetV1(groups=3, out_indices=(2, ))
  180. model.init_weights()
  181. model.train()
  182. for m in model.modules():
  183. if self.is_norm(m):
  184. self.assertIsInstance(m, _BatchNorm)
  185. imgs = torch.randn(1, 3, 224, 224)
  186. feat = model(imgs)
  187. self.assertIsInstance(feat, tuple)
  188. self.assertEqual(feat[-1].shape, torch.Size((1, 960, 7, 7)))
  189. # Test ShuffleNetV1 forward with checkpoint forward
  190. model = ShuffleNetV1(groups=3, with_cp=True)
  191. for m in model.modules():
  192. if self.is_block(m):
  193. self.assertTrue(m.with_cp)
  194. # Test ShuffleNetV1 with norm_eval
  195. model = ShuffleNetV1(norm_eval=True)
  196. model.init_weights()
  197. model.train()
  198. self.assertTrue(self.check_norm_state(model.modules(), False))