test_resnest.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. # Copyright (c) OpenMMLab. All rights reserved.
  2. import pytest
  3. import torch
  4. from mmdet.models.backbones import ResNeSt
  5. from mmdet.models.backbones.resnest import Bottleneck as BottleneckS
  6. def test_resnest_bottleneck():
  7. with pytest.raises(AssertionError):
  8. # Style must be in ['pytorch', 'caffe']
  9. BottleneckS(64, 64, radix=2, reduction_factor=4, style='tensorflow')
  10. # Test ResNeSt Bottleneck structure
  11. block = BottleneckS(
  12. 2, 4, radix=2, reduction_factor=4, stride=2, style='pytorch')
  13. assert block.avd_layer.stride == 2
  14. assert block.conv2.channels == 4
  15. # Test ResNeSt Bottleneck forward
  16. block = BottleneckS(16, 4, radix=2, reduction_factor=4)
  17. x = torch.randn(2, 16, 56, 56)
  18. x_out = block(x)
  19. assert x_out.shape == torch.Size([2, 16, 56, 56])
  20. def test_resnest_backbone():
  21. with pytest.raises(KeyError):
  22. # ResNeSt depth should be in [50, 101, 152, 200]
  23. ResNeSt(depth=18)
  24. # Test ResNeSt with radix 2, reduction_factor 4
  25. model = ResNeSt(
  26. depth=50,
  27. base_channels=4,
  28. radix=2,
  29. reduction_factor=4,
  30. out_indices=(0, 1, 2, 3))
  31. model.train()
  32. imgs = torch.randn(2, 3, 32, 32)
  33. feat = model(imgs)
  34. assert len(feat) == 4
  35. assert feat[0].shape == torch.Size([2, 16, 8, 8])
  36. assert feat[1].shape == torch.Size([2, 32, 4, 4])
  37. assert feat[2].shape == torch.Size([2, 64, 2, 2])
  38. assert feat[3].shape == torch.Size([2, 128, 1, 1])