File size: 6,116 Bytes
0efc562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# Copyright (c) OpenMMLab. All rights reserved.
from unittest import TestCase

import torch

from mmpose.models.utils.transformer import GAUEncoder, SinePositionalEncoding


class TestSinePositionalEncoding(TestCase):

    def test_init(self):

        spe = SinePositionalEncoding(out_channels=128)
        self.assertTrue(hasattr(spe, 'dim_t'))
        self.assertFalse(spe.dim_t.requires_grad)
        self.assertEqual(spe.dim_t.size(0), 128 // 2)

        spe = SinePositionalEncoding(out_channels=128, learnable=True)
        self.assertTrue(spe.dim_t.requires_grad)

        spe = SinePositionalEncoding(out_channels=128, eval_size=10)
        self.assertTrue(hasattr(spe, 'pos_enc_10'))
        self.assertEqual(spe.pos_enc_10.size(-1), 128)

        spe = SinePositionalEncoding(
            out_channels=128, eval_size=(2, 3), spatial_dim=2)
        self.assertTrue(hasattr(spe, 'pos_enc_(2, 3)'))
        self.assertSequenceEqual(
            getattr(spe, 'pos_enc_(2, 3)').shape[-2:], (128, 2))

    def test_generate_speoding(self):

        # spatial_dim = 1
        spe = SinePositionalEncoding(out_channels=128)
        pos_enc = spe.generate_pos_encoding(size=10)
        self.assertSequenceEqual(pos_enc.shape, (10, 128))

        position = torch.arange(8)
        pos_enc = spe.generate_pos_encoding(position=position)
        self.assertSequenceEqual(pos_enc.shape, (8, 128))

        with self.assertRaises(AssertionError):
            pos_enc = spe.generate_pos_encoding(size=10, position=position)

        # spatial_dim = 2
        spe = SinePositionalEncoding(out_channels=128, spatial_dim=2)
        pos_enc = spe.generate_pos_encoding(size=10)
        self.assertSequenceEqual(pos_enc.shape, (100, 128, 2))

        pos_enc = spe.generate_pos_encoding(size=(5, 6))
        self.assertSequenceEqual(pos_enc.shape, (30, 128, 2))

        position = torch.arange(8).unsqueeze(1).repeat(1, 2)
        pos_enc = spe.generate_pos_encoding(position=position)
        self.assertSequenceEqual(pos_enc.shape, (8, 128, 2))

        with self.assertRaises(AssertionError):
            pos_enc = spe.generate_pos_encoding(size=10, position=position)

        with self.assertRaises(ValueError):
            pos_enc = spe.generate_pos_encoding(size=position)

    def test_apply_additional_pos_enc(self):

        # spatial_dim = 1
        spe = SinePositionalEncoding(out_channels=128)
        pos_enc = spe.generate_pos_encoding(size=10)
        feature = torch.randn(2, 3, 10, 128)
        out_feature = spe.apply_additional_pos_enc(feature, pos_enc,
                                                   spe.spatial_dim)
        self.assertSequenceEqual(feature.shape, out_feature.shape)

        # spatial_dim = 2
        spe = SinePositionalEncoding(out_channels=128 // 2, spatial_dim=2)
        pos_enc = spe.generate_pos_encoding(size=(2, 5))
        feature = torch.randn(2, 3, 10, 128)
        out_feature = spe.apply_additional_pos_enc(feature, pos_enc,
                                                   spe.spatial_dim)
        self.assertSequenceEqual(feature.shape, out_feature.shape)

    def test_apply_rotary_pos_enc(self):

        # spatial_dim = 1
        spe = SinePositionalEncoding(out_channels=128)
        pos_enc = spe.generate_pos_encoding(size=10)
        feature = torch.randn(2, 3, 10, 128)
        out_feature = spe.apply_rotary_pos_enc(feature, pos_enc,
                                               spe.spatial_dim)
        self.assertSequenceEqual(feature.shape, out_feature.shape)

        # spatial_dim = 2
        spe = SinePositionalEncoding(out_channels=128, spatial_dim=2)
        pos_enc = spe.generate_pos_encoding(size=(2, 5))
        feature = torch.randn(2, 3, 10, 128)
        out_feature = spe.apply_rotary_pos_enc(feature, pos_enc,
                                               spe.spatial_dim)
        self.assertSequenceEqual(feature.shape, out_feature.shape)


class TestGAUEncoder(TestCase):

    def test_init(self):
        gau = GAUEncoder(in_token_dims=64, out_token_dims=64)
        self.assertTrue(gau.shortcut)

        gau = GAUEncoder(in_token_dims=64, out_token_dims=64, dropout_rate=0.5)
        self.assertTrue(hasattr(gau, 'dropout'))

    def test_forward(self):
        gau = GAUEncoder(in_token_dims=64, out_token_dims=64)

        # compatibility with various dimension input
        feat = torch.randn(2, 3, 64)
        with torch.no_grad():
            out_feat = gau.forward(feat)
        self.assertSequenceEqual(feat.shape, out_feat.shape)

        feat = torch.randn(1, 2, 3, 64)
        with torch.no_grad():
            out_feat = gau.forward(feat)
        self.assertSequenceEqual(feat.shape, out_feat.shape)

        feat = torch.randn(1, 2, 3, 4, 64)
        with torch.no_grad():
            out_feat = gau.forward(feat)
        self.assertSequenceEqual(feat.shape, out_feat.shape)

        # positional encoding
        gau = GAUEncoder(
            s=32, in_token_dims=64, out_token_dims=64, pos_enc=True)
        feat = torch.randn(2, 3, 64)
        spe = SinePositionalEncoding(out_channels=32)
        pos_enc = spe.generate_pos_encoding(size=3)
        with torch.no_grad():
            out_feat = gau.forward(feat, pos_enc=pos_enc)
        self.assertSequenceEqual(feat.shape, out_feat.shape)

        gau = GAUEncoder(
            s=32,
            in_token_dims=64,
            out_token_dims=64,
            pos_enc=True,
            spatial_dim=2)
        feat = torch.randn(1, 2, 6, 64)
        spe = SinePositionalEncoding(out_channels=32, spatial_dim=2)
        pos_enc = spe.generate_pos_encoding(size=(2, 3))
        with torch.no_grad():
            out_feat = gau.forward(feat, pos_enc=pos_enc)
        self.assertSequenceEqual(feat.shape, out_feat.shape)

        # mask
        gau = GAUEncoder(in_token_dims=64, out_token_dims=64)

        # compatibility with various dimension input
        feat = torch.randn(2, 3, 64)
        mask = torch.rand(2, 3, 3)
        with torch.no_grad():
            out_feat = gau.forward(feat, mask=mask)
        self.assertSequenceEqual(feat.shape, out_feat.shape)