test_architecture_ops.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import unittest
  2. import pytest
  3. import torch
  4. from torchvision.models.maxvit import SwapAxes, WindowDepartition, WindowPartition
  5. class MaxvitTester(unittest.TestCase):
  6. def test_maxvit_window_partition(self):
  7. input_shape = (1, 3, 224, 224)
  8. partition_size = 7
  9. n_partitions = input_shape[3] // partition_size
  10. x = torch.randn(input_shape)
  11. partition = WindowPartition()
  12. departition = WindowDepartition()
  13. x_hat = partition(x, partition_size)
  14. x_hat = departition(x_hat, partition_size, n_partitions, n_partitions)
  15. torch.testing.assert_close(x, x_hat)
  16. def test_maxvit_grid_partition(self):
  17. input_shape = (1, 3, 224, 224)
  18. partition_size = 7
  19. n_partitions = input_shape[3] // partition_size
  20. x = torch.randn(input_shape)
  21. pre_swap = SwapAxes(-2, -3)
  22. post_swap = SwapAxes(-2, -3)
  23. partition = WindowPartition()
  24. departition = WindowDepartition()
  25. x_hat = partition(x, n_partitions)
  26. x_hat = pre_swap(x_hat)
  27. x_hat = post_swap(x_hat)
  28. x_hat = departition(x_hat, n_partitions, partition_size, partition_size)
  29. torch.testing.assert_close(x, x_hat)
  30. if __name__ == "__main__":
  31. pytest.main([__file__])