test_shufflenet_v2.py 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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 ShuffleNetV2
  7. from mmpose.models.backbones.shufflenet_v2 import InvertedResidual
  8. class TestShufflenetV2(TestCase):
  9. @staticmethod
  10. def is_block(modules):
  11. """Check if is ResNet building block."""
  12. if isinstance(modules, (InvertedResidual, )):
  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_shufflenetv2_invertedresidual(self):
  30. with self.assertRaises(AssertionError):
  31. # when stride==1, in_channels should be equal to
  32. # out_channels // 2 * 2
  33. InvertedResidual(24, 32, stride=1)
  34. with self.assertRaises(AssertionError):
  35. # when in_channels != out_channels // 2 * 2, stride should not be
  36. # equal to 1.
  37. InvertedResidual(24, 32, stride=1)
  38. # Test InvertedResidual forward
  39. block = InvertedResidual(24, 48, stride=2)
  40. x = torch.randn(1, 24, 56, 56)
  41. x_out = block(x)
  42. self.assertEqual(x_out.shape, torch.Size((1, 48, 28, 28)))
  43. # Test InvertedResidual with checkpoint forward
  44. block = InvertedResidual(48, 48, stride=1, with_cp=True)
  45. self.assertTrue(block.with_cp)
  46. x = torch.randn(1, 48, 56, 56)
  47. x.requires_grad = True
  48. x_out = block(x)
  49. self.assertEqual(x_out.shape, torch.Size((1, 48, 56, 56)))
  50. def test_shufflenetv2_backbone(self):
  51. with self.assertRaises(ValueError):
  52. # groups must be in 0.5, 1.0, 1.5, 2.0]
  53. ShuffleNetV2(widen_factor=3.0)
  54. with self.assertRaises(ValueError):
  55. # frozen_stages must be in [0, 1, 2, 3]
  56. ShuffleNetV2(widen_factor=1.0, frozen_stages=4)
  57. with self.assertRaises(ValueError):
  58. # out_indices must be in [0, 1, 2, 3]
  59. ShuffleNetV2(widen_factor=1.0, out_indices=(4, ))
  60. with self.assertRaises(TypeError):
  61. # init_weights must have no parameter
  62. model = ShuffleNetV2()
  63. model.init_weights(pretrained=1)
  64. # Test ShuffleNetV2 norm state
  65. model = ShuffleNetV2()
  66. model.init_weights()
  67. model.train()
  68. self.assertTrue(self.check_norm_state(model.modules(), True))
  69. # Test ShuffleNetV2 with first stage frozen
  70. frozen_stages = 1
  71. model = ShuffleNetV2(frozen_stages=frozen_stages)
  72. model.init_weights()
  73. model.train()
  74. for param in model.conv1.parameters():
  75. self.assertFalse(param.requires_grad)
  76. for i in range(0, frozen_stages):
  77. layer = model.layers[i]
  78. for mod in layer.modules():
  79. if isinstance(mod, _BatchNorm):
  80. self.assertFalse(mod.training)
  81. for param in layer.parameters():
  82. self.assertFalse(param.requires_grad)
  83. # Test ShuffleNetV2 with norm_eval
  84. model = ShuffleNetV2(norm_eval=True)
  85. model.init_weights()
  86. model.train()
  87. self.assertTrue(self.check_norm_state(model.modules(), False))
  88. # Test ShuffleNetV2 forward with widen_factor=0.5
  89. model = ShuffleNetV2(widen_factor=0.5, out_indices=(0, 1, 2, 3))
  90. model.init_weights()
  91. model.train()
  92. for m in model.modules():
  93. if self.is_norm(m):
  94. self.assertIsInstance(m, _BatchNorm)
  95. imgs = torch.randn(1, 3, 224, 224)
  96. feat = model(imgs)
  97. self.assertEqual(len(feat), 4)
  98. self.assertEqual(feat[0].shape, torch.Size((1, 48, 28, 28)))
  99. self.assertEqual(feat[1].shape, torch.Size((1, 96, 14, 14)))
  100. self.assertEqual(feat[2].shape, torch.Size((1, 192, 7, 7)))
  101. # Test ShuffleNetV2 forward with widen_factor=1.0
  102. model = ShuffleNetV2(widen_factor=1.0, out_indices=(0, 1, 2, 3))
  103. model.init_weights()
  104. model.train()
  105. for m in model.modules():
  106. if self.is_norm(m):
  107. self.assertIsInstance(m, _BatchNorm)
  108. imgs = torch.randn(1, 3, 224, 224)
  109. feat = model(imgs)
  110. self.assertEqual(len(feat), 4)
  111. self.assertEqual(feat[0].shape, torch.Size((1, 116, 28, 28)))
  112. self.assertEqual(feat[1].shape, torch.Size((1, 232, 14, 14)))
  113. self.assertEqual(feat[2].shape, torch.Size((1, 464, 7, 7)))
  114. # Test ShuffleNetV2 forward with widen_factor=1.5
  115. model = ShuffleNetV2(widen_factor=1.5, out_indices=(0, 1, 2, 3))
  116. model.init_weights()
  117. model.train()
  118. for m in model.modules():
  119. if self.is_norm(m):
  120. self.assertIsInstance(m, _BatchNorm)
  121. imgs = torch.randn(1, 3, 224, 224)
  122. feat = model(imgs)
  123. self.assertEqual(len(feat), 4)
  124. self.assertEqual(feat[0].shape, torch.Size((1, 176, 28, 28)))
  125. self.assertEqual(feat[1].shape, torch.Size((1, 352, 14, 14)))
  126. self.assertEqual(feat[2].shape, torch.Size((1, 704, 7, 7)))
  127. # Test ShuffleNetV2 forward with widen_factor=2.0
  128. model = ShuffleNetV2(widen_factor=2.0, out_indices=(0, 1, 2, 3))
  129. model.init_weights()
  130. model.train()
  131. for m in model.modules():
  132. if self.is_norm(m):
  133. self.assertIsInstance(m, _BatchNorm)
  134. imgs = torch.randn(1, 3, 224, 224)
  135. feat = model(imgs)
  136. self.assertEqual(len(feat), 4)
  137. self.assertEqual(feat[0].shape, torch.Size((1, 244, 28, 28)))
  138. self.assertEqual(feat[1].shape, torch.Size((1, 488, 14, 14)))
  139. self.assertEqual(feat[2].shape, torch.Size((1, 976, 7, 7)))
  140. # Test ShuffleNetV2 forward with layers 3 forward
  141. model = ShuffleNetV2(widen_factor=1.0, out_indices=(2, ))
  142. model.init_weights()
  143. model.train()
  144. for m in model.modules():
  145. if self.is_norm(m):
  146. self.assertIsInstance(m, _BatchNorm)
  147. imgs = torch.randn(1, 3, 224, 224)
  148. feat = model(imgs)
  149. self.assertIsInstance(feat, tuple)
  150. self.assertEqual(feat[-1].shape, torch.Size((1, 464, 7, 7)))
  151. # Test ShuffleNetV2 forward with layers 1 2 forward
  152. model = ShuffleNetV2(widen_factor=1.0, out_indices=(1, 2))
  153. model.init_weights()
  154. model.train()
  155. for m in model.modules():
  156. if self.is_norm(m):
  157. self.assertIsInstance(m, _BatchNorm)
  158. imgs = torch.randn(1, 3, 224, 224)
  159. feat = model(imgs)
  160. self.assertEqual(len(feat), 2)
  161. self.assertEqual(feat[0].shape, torch.Size((1, 232, 14, 14)))
  162. self.assertEqual(feat[1].shape, torch.Size((1, 464, 7, 7)))
  163. # Test ShuffleNetV2 forward with checkpoint forward
  164. model = ShuffleNetV2(widen_factor=1.0, with_cp=True)
  165. for m in model.modules():
  166. if self.is_block(m):
  167. self.assertTrue(m.with_cp)