test_swin.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import pytest
  2. import torch
  3. from mmdet.models.backbones.swin import SwinBlock, SwinTransformer
  4. def test_swin_block():
  5. # test SwinBlock structure and forward
  6. block = SwinBlock(embed_dims=64, num_heads=4, feedforward_channels=256)
  7. assert block.ffn.embed_dims == 64
  8. assert block.attn.w_msa.num_heads == 4
  9. assert block.ffn.feedforward_channels == 256
  10. x = torch.randn(1, 56 * 56, 64)
  11. x_out = block(x, (56, 56))
  12. assert x_out.shape == torch.Size([1, 56 * 56, 64])
  13. # Test BasicBlock with checkpoint forward
  14. block = SwinBlock(
  15. embed_dims=64, num_heads=4, feedforward_channels=256, with_cp=True)
  16. assert block.with_cp
  17. x = torch.randn(1, 56 * 56, 64)
  18. x_out = block(x, (56, 56))
  19. assert x_out.shape == torch.Size([1, 56 * 56, 64])
  20. def test_swin_transformer():
  21. """Test Swin Transformer backbone."""
  22. with pytest.raises(TypeError):
  23. # Pretrained arg must be str or None.
  24. SwinTransformer(pretrained=123)
  25. with pytest.raises(AssertionError):
  26. # Because swin uses non-overlapping patch embed, so the stride of patch
  27. # embed must be equal to patch size.
  28. SwinTransformer(strides=(2, 2, 2, 2), patch_size=4)
  29. # test pretrained image size
  30. with pytest.raises(AssertionError):
  31. SwinTransformer(pretrain_img_size=(224, 224, 224))
  32. # Test absolute position embedding
  33. temp = torch.randn((1, 3, 224, 224))
  34. model = SwinTransformer(pretrain_img_size=224, use_abs_pos_embed=True)
  35. model.init_weights()
  36. model(temp)
  37. # Test patch norm
  38. model = SwinTransformer(patch_norm=False)
  39. model(temp)
  40. # Test normal inference
  41. temp = torch.randn((1, 3, 32, 32))
  42. model = SwinTransformer()
  43. outs = model(temp)
  44. assert outs[0].shape == (1, 96, 8, 8)
  45. assert outs[1].shape == (1, 192, 4, 4)
  46. assert outs[2].shape == (1, 384, 2, 2)
  47. assert outs[3].shape == (1, 768, 1, 1)
  48. # Test abnormal inference size
  49. temp = torch.randn((1, 3, 31, 31))
  50. model = SwinTransformer()
  51. outs = model(temp)
  52. assert outs[0].shape == (1, 96, 8, 8)
  53. assert outs[1].shape == (1, 192, 4, 4)
  54. assert outs[2].shape == (1, 384, 2, 2)
  55. assert outs[3].shape == (1, 768, 1, 1)
  56. # Test abnormal inference size
  57. temp = torch.randn((1, 3, 112, 137))
  58. model = SwinTransformer()
  59. outs = model(temp)
  60. assert outs[0].shape == (1, 96, 28, 35)
  61. assert outs[1].shape == (1, 192, 14, 18)
  62. assert outs[2].shape == (1, 384, 7, 9)
  63. assert outs[3].shape == (1, 768, 4, 5)
  64. model = SwinTransformer(frozen_stages=4)
  65. model.train()
  66. for p in model.parameters():
  67. assert not p.requires_grad