File: test_architecture_ops.py

package info (click to toggle)
pytorch-vision 0.21.0-3
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 20,228 kB
  • sloc: python: 65,904; cpp: 11,406; ansic: 2,459; java: 550; sh: 265; xml: 79; objc: 56; makefile: 33
file content (46 lines) | stat: -rw-r--r-- 1,275 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import unittest

import pytest
import torch

from torchvision.models.maxvit import SwapAxes, WindowDepartition, WindowPartition


class MaxvitTester(unittest.TestCase):
    def test_maxvit_window_partition(self):
        input_shape = (1, 3, 224, 224)
        partition_size = 7
        n_partitions = input_shape[3] // partition_size

        x = torch.randn(input_shape)

        partition = WindowPartition()
        departition = WindowDepartition()

        x_hat = partition(x, partition_size)
        x_hat = departition(x_hat, partition_size, n_partitions, n_partitions)

        torch.testing.assert_close(x, x_hat)

    def test_maxvit_grid_partition(self):
        input_shape = (1, 3, 224, 224)
        partition_size = 7
        n_partitions = input_shape[3] // partition_size

        x = torch.randn(input_shape)
        pre_swap = SwapAxes(-2, -3)
        post_swap = SwapAxes(-2, -3)

        partition = WindowPartition()
        departition = WindowDepartition()

        x_hat = partition(x, n_partitions)
        x_hat = pre_swap(x_hat)
        x_hat = post_swap(x_hat)
        x_hat = departition(x_hat, n_partitions, partition_size, partition_size)

        torch.testing.assert_close(x, x_hat)


if __name__ == "__main__":
    pytest.main([__file__])