test_mobilenet_v3.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 MobileNetV3
  7. from mmpose.models.backbones.utils import InvertedResidual
  8. class TestMobilenetV3(TestCase):
  9. @staticmethod
  10. def is_norm(modules):
  11. """Check if is one of the norms."""
  12. if isinstance(modules, (GroupNorm, _BatchNorm)):
  13. return True
  14. return False
  15. @staticmethod
  16. def check_norm_state(modules, train_state):
  17. """Check if norm layer is in correct train state."""
  18. for mod in modules:
  19. if isinstance(mod, _BatchNorm):
  20. if mod.training != train_state:
  21. return False
  22. return True
  23. def test_mobilenetv3_backbone(self):
  24. with self.assertRaises(AssertionError):
  25. # arch must in [small, big]
  26. MobileNetV3(arch='others')
  27. with self.assertRaises(ValueError):
  28. # frozen_stages must less than 12 when arch is small
  29. MobileNetV3(arch='small', frozen_stages=12)
  30. with self.assertRaises(ValueError):
  31. # frozen_stages must less than 16 when arch is big
  32. MobileNetV3(arch='big', frozen_stages=16)
  33. with self.assertRaises(ValueError):
  34. # max out_indices must less than 11 when arch is small
  35. MobileNetV3(arch='small', out_indices=(11, ))
  36. with self.assertRaises(ValueError):
  37. # max out_indices must less than 15 when arch is big
  38. MobileNetV3(arch='big', out_indices=(15, ))
  39. # Test MobileNetv3
  40. model = MobileNetV3()
  41. model.init_weights()
  42. model.train()
  43. # Test MobileNetv3 with first stage frozen
  44. frozen_stages = 1
  45. model = MobileNetV3(frozen_stages=frozen_stages)
  46. model.init_weights()
  47. model.train()
  48. for param in model.conv1.parameters():
  49. self.assertFalse(param.requires_grad)
  50. for i in range(1, frozen_stages + 1):
  51. layer = getattr(model, f'layer{i}')
  52. for mod in layer.modules():
  53. if isinstance(mod, _BatchNorm):
  54. self.assertFalse(mod.training)
  55. for param in layer.parameters():
  56. self.assertFalse(param.requires_grad)
  57. # Test MobileNetv3 with norm eval
  58. model = MobileNetV3(norm_eval=True, out_indices=range(0, 11))
  59. model.init_weights()
  60. model.train()
  61. self.assertTrue(self.check_norm_state(model.modules(), False))
  62. # Test MobileNetv3 forward with small arch
  63. model = MobileNetV3(out_indices=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10))
  64. model.init_weights()
  65. model.train()
  66. imgs = torch.randn(1, 3, 224, 224)
  67. feat = model(imgs)
  68. self.assertEqual(len(feat), 11)
  69. self.assertEqual(feat[0].shape, torch.Size([1, 16, 56, 56]))
  70. self.assertEqual(feat[1].shape, torch.Size([1, 24, 28, 28]))
  71. self.assertEqual(feat[2].shape, torch.Size([1, 24, 28, 28]))
  72. self.assertEqual(feat[3].shape, torch.Size([1, 40, 14, 14]))
  73. self.assertEqual(feat[4].shape, torch.Size([1, 40, 14, 14]))
  74. self.assertEqual(feat[5].shape, torch.Size([1, 40, 14, 14]))
  75. self.assertEqual(feat[6].shape, torch.Size([1, 48, 14, 14]))
  76. self.assertEqual(feat[7].shape, torch.Size([1, 48, 14, 14]))
  77. self.assertEqual(feat[8].shape, torch.Size([1, 96, 7, 7]))
  78. self.assertEqual(feat[9].shape, torch.Size([1, 96, 7, 7]))
  79. self.assertEqual(feat[10].shape, torch.Size([1, 96, 7, 7]))
  80. # Test MobileNetv3 forward with small arch and GroupNorm
  81. model = MobileNetV3(
  82. out_indices=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10),
  83. norm_cfg=dict(type='GN', num_groups=2, requires_grad=True))
  84. for m in model.modules():
  85. if self.is_norm(m):
  86. self.assertIsInstance(m, GroupNorm)
  87. model.init_weights()
  88. model.train()
  89. imgs = torch.randn(1, 3, 224, 224)
  90. feat = model(imgs)
  91. self.assertEqual(len(feat), 11)
  92. self.assertEqual(feat[0].shape, torch.Size([1, 16, 56, 56]))
  93. self.assertEqual(feat[1].shape, torch.Size([1, 24, 28, 28]))
  94. self.assertEqual(feat[2].shape, torch.Size([1, 24, 28, 28]))
  95. self.assertEqual(feat[3].shape, torch.Size([1, 40, 14, 14]))
  96. self.assertEqual(feat[4].shape, torch.Size([1, 40, 14, 14]))
  97. self.assertEqual(feat[5].shape, torch.Size([1, 40, 14, 14]))
  98. self.assertEqual(feat[6].shape, torch.Size([1, 48, 14, 14]))
  99. self.assertEqual(feat[7].shape, torch.Size([1, 48, 14, 14]))
  100. self.assertEqual(feat[8].shape, torch.Size([1, 96, 7, 7]))
  101. self.assertEqual(feat[9].shape, torch.Size([1, 96, 7, 7]))
  102. self.assertEqual(feat[10].shape, torch.Size([1, 96, 7, 7]))
  103. # Test MobileNetv3 forward with big arch
  104. model = MobileNetV3(
  105. arch='big',
  106. out_indices=(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14))
  107. model.init_weights()
  108. model.train()
  109. imgs = torch.randn(1, 3, 224, 224)
  110. feat = model(imgs)
  111. self.assertEqual(len(feat), 15)
  112. self.assertEqual(feat[0].shape, torch.Size([1, 16, 112, 112]))
  113. self.assertEqual(feat[1].shape, torch.Size([1, 24, 56, 56]))
  114. self.assertEqual(feat[2].shape, torch.Size([1, 24, 56, 56]))
  115. self.assertEqual(feat[3].shape, torch.Size([1, 40, 28, 28]))
  116. self.assertEqual(feat[4].shape, torch.Size([1, 40, 28, 28]))
  117. self.assertEqual(feat[5].shape, torch.Size([1, 40, 28, 28]))
  118. self.assertEqual(feat[6].shape, torch.Size([1, 80, 14, 14]))
  119. self.assertEqual(feat[7].shape, torch.Size([1, 80, 14, 14]))
  120. self.assertEqual(feat[8].shape, torch.Size([1, 80, 14, 14]))
  121. self.assertEqual(feat[9].shape, torch.Size([1, 80, 14, 14]))
  122. self.assertEqual(feat[10].shape, torch.Size([1, 112, 14, 14]))
  123. self.assertEqual(feat[11].shape, torch.Size([1, 112, 14, 14]))
  124. self.assertEqual(feat[12].shape, torch.Size([1, 160, 14, 14]))
  125. self.assertEqual(feat[13].shape, torch.Size([1, 160, 7, 7]))
  126. self.assertEqual(feat[14].shape, torch.Size([1, 160, 7, 7]))
  127. # Test MobileNetv3 forward with big arch
  128. model = MobileNetV3(arch='big', out_indices=(0, ))
  129. model.init_weights()
  130. model.train()
  131. imgs = torch.randn(1, 3, 224, 224)
  132. feat = model(imgs)
  133. self.assertIsInstance(feat, tuple)
  134. self.assertEqual(feat[-1].shape, torch.Size([1, 16, 112, 112]))
  135. # Test MobileNetv3 with checkpoint forward
  136. model = MobileNetV3(with_cp=True)
  137. for m in model.modules():
  138. if isinstance(m, InvertedResidual):
  139. self.assertTrue(m.with_cp)
  140. model.init_weights()
  141. model.train()
  142. imgs = torch.randn(1, 3, 224, 224)
  143. feat = model(imgs)
  144. self.assertIsInstance(feat, tuple)
  145. self.assertEqual(feat[-1].shape, torch.Size([1, 96, 7, 7]))