File size: 10,089 Bytes
6163604
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import torch.nn as nn
import torch
from torch.nn import functional as F
from .vit.utils import trunc_normal_
from .vit.vision_transformer import VisionTransformer
from ..feature_extractor.clova_impl import ResNet_FeatureExtractor
from .addon_module import *
from ..common.mae_posembed import get_2d_sincos_pos_embed

__all__ = ['ViTEncoder', 'ViTEncoderV2', 'ViTEncoderV3', 'TRIGBaseEncoder', 'create_vit_modeling']

class ViTEncoder(VisionTransformer):
    '''
    '''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        if kwargs['hybrid_backbone'] is None:
            self.patch_embed = PatchEmbed(
                img_size=kwargs['img_size'],
                in_chans=kwargs['in_chans'],
                patch_size=kwargs['patch_size'], 
                embed_dim=kwargs['embed_dim'],
            )
        else:
            self.patch_embed = HybridEmbed(
                    backbone=kwargs['hybrid_backbone'],
                    img_size=kwargs['img_size'],
                    in_chans=kwargs['in_chans'],
                    patch_size=kwargs['patch_size'], 
                    embed_dim=kwargs['embed_dim'],
            )
        num_patches = self.patch_embed.num_patches
        self.pos_embed = nn.Parameter(torch.zeros(1, num_patches+1, kwargs['embed_dim']))
        self.emb_height = self.patch_embed.grid_size[0]
        self.emb_width = self.patch_embed.grid_size[1]
        trunc_normal_(self.pos_embed, std=.02)
        self.apply(self._init_weights)

    def reset_classifier(self, num_classes):
        self.num_classes = num_classes
        self.head = nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity()

    def interpolating_pos_embedding(self, embedding, height, width):
        """
        Source:
        https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py#L174
        """
        npatch = embedding.shape[1] - 1
        N = self.pos_embed.shape[1] - 1
        if npatch == N and height ==  width:
            return self.pos_embed

        class_pos_embedding = self.pos_embed[:, 0]
        patch_pos_embedding = self.pos_embed[:, 1:]
        dim = self.pos_embed.shape[-1]

        h0 = height // self.patch_embed.patch_size[0]
        w0 = width // self.patch_embed.patch_size[1]
        #add a small number to avo_id floating point error
        # https://github.com/facebookresearch/dino/issues/8

        h0 = h0 + 0.1
        w0 = w0 + 0.1
        
        patch_pos_embedding = nn.functional.interpolate(
            patch_pos_embedding.reshape(1, self.emb_height, self.emb_width, dim).permute(0, 3, 1, 2),
            scale_factor=(h0 / self.emb_height, w0 / self.emb_width),
            mode='bicubic',
            align_corners=False
        )
        assert int(h0) == patch_pos_embedding.shape[-2] and int(w0) == patch_pos_embedding.shape[-1]
        patch_pos_embedding = patch_pos_embedding.permute(0, 2, 3, 1).view(1, -1, dim)
        class_pos_embedding = class_pos_embedding.unsqueeze(0)

        return torch.cat((class_pos_embedding, patch_pos_embedding), dim=1)

    def forward_features(self, x):  
        B, C, _, _ = x.shape     
        
        x, pad_info, size, interpolating_pos = self.patch_embed(x)
        cls_tokens = self.cls_token.expand(B, -1, -1)  # stole cls_tokens impl from Phil Wang, thanks
        x = torch.cat((cls_tokens, x), dim=1)

        if interpolating_pos:
            x = x + self.interpolating_pos_embedding(x, size['height'], size['width'])
        else:
            x = x + self.pos_embed

        x = self.pos_drop(x)

        for blk in self.blocks:
            x = blk(x)

        x = self.norm(x)
        
        return x, pad_info, size


class TRIGBaseEncoder(ViTEncoder):
    '''
    https://arxiv.org/pdf/2111.08314.pdf
    '''
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.patch_embed = HybridEmbed1D(
                backbone=kwargs['hybrid_backbone'],
                img_size=kwargs['img_size'],
                in_chans=kwargs['in_chans'],
                patch_size=kwargs['patch_size'],
                embed_dim=kwargs['embed_dim'],
        )
        num_patches = self.patch_embed.num_patches
        self.pos_embed = nn.Parameter(torch.zeros(1, num_patches+1, kwargs['embed_dim']))
        self.emb_height = 1
        self.emb_width = self.patch_embed.grid_size[1]
        trunc_normal_(self.pos_embed, std=.02)
        self.apply(self._init_weights)

    def interpolating_pos_embedding(self, embedding, height, width):
        """
        Source:
        https://github.com/facebookresearch/dino/blob/de9ee3df6cf39fac952ab558447af1fa1365362a/vision_transformer.py#L174
        """
        npatch = embedding.shape[1] - 1
        N = self.pos_embed.shape[1] - 1
        if npatch == N and height ==  width:
            return self.pos_embed

        class_pos_embedding = self.pos_embed[:, 0]
        patch_pos_embedding = self.pos_embed[:, 1:]
        dim = self.pos_embed.shape[-1]

        w0 = width // self.patch_embed.window_width
        
        #add a small number to avoid floating point error
        # https://github.com/facebookresearch/dino/issues/8

        w0 = w0 + 0.1
        
        patch_pos_embedding = nn.functional.interpolate(
            patch_pos_embedding.reshape(1, self.emb_height, self.emb_width, dim).permute(0, 3, 1, 2),
            scale_factor=(1, w0 / self.emb_width),
            mode='bicubic',
            align_corners=False
        )
                
        assert int(w0) == patch_pos_embedding.shape[-1]
        patch_pos_embedding = patch_pos_embedding.permute(0, 2, 3, 1).view(1, -1, dim)
        class_pos_embedding = class_pos_embedding.unsqueeze(0)

        return torch.cat((class_pos_embedding, patch_pos_embedding), dim=1)
    
    def forward_features(self, x):  
        B, _, _, _ = x.shape     
        x, padinfo, size, interpolating_pos = self.patch_embed(x)
        
        cls_tokens = self.cls_token.expand(B, -1, -1)  # stole cls_tokens impl from Phil Wang, thanks
        
        x = torch.cat((cls_tokens, x), dim=1) #cls_tokens is init_embedding in TRIG paper

        if interpolating_pos:
            x = x + self.interpolating_pos_embedding(x, size['height'], size['width'])
        else:
            x = x + self.pos_embed

        x = self.pos_drop(x)

        for blk in self.blocks:
            x = blk(x)

        x = self.norm(x)
        
        return x, padinfo, size


class ViTEncoderV2(ViTEncoder):
    def forward(self, x):
        B, _, _, _ = x.shape     
        
        x, pad_info, size, _ = self.patch_embed(x)
        _, numpatches, *_ = x.shape
        cls_tokens = self.cls_token.expand(B, -1, -1)  # stole cls_tokens impl from Phil Wang, thanks
        x = torch.cat((cls_tokens, x), dim=1)

        x = x + self.pos_embed[:, :(numpatches + 1)]
        x = self.pos_drop(x)

        for blk in self.blocks:
            x = blk(x)

        x = self.norm(x)
        
        return x, pad_info, size

class ViTEncoderV3(ViTEncoder):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        if hasattr(self, 'pos_embed'):
            del self.pos_embed
        num_patches = self.patch_embed.num_patches
        self.pos_embed = nn.Parameter(torch.zeros(1, num_patches+1, kwargs['embed_dim']), requires_grad=False)
        self.initialize_posembed()

    def initialize_posembed(self):
        pos_embed = get_2d_sincos_pos_embed(
            self.pos_embed.shape[-1], 
            self.patch_embed.grid_size[0], 
            self.patch_embed.grid_size[1],
            cls_token=True
        )
        self.pos_embed.data.copy_(torch.from_numpy(pos_embed).float().unsqueeze(0))

    def forward(self, x):
        B, _, _, _ = x.shape     
        
        x, pad_info, size, _ = self.patch_embed(x)
        _, numpatches, *_ = x.shape

        cls_tokens = self.cls_token.expand(B, -1, -1)  # stole cls_tokens impl from Phil Wang, thanks
        x = torch.cat((cls_tokens, x), dim=1)

        x = x + self.pos_embed[:, :(numpatches + 1)]
        x = self.pos_drop(x)

        for blk in self.blocks:
            x = blk(x)

        x = self.norm(x)
        
        return x, pad_info, size

def create_vit_modeling(opt):
    seq_modeling = opt['SequenceModeling']['params']
    if seq_modeling['backbone'] is not None:
        if seq_modeling['backbone']['name'] == 'resnet':
            param_kwargs = dict()
            if seq_modeling['backbone'].get('pretrained', None) is not None:
                param_kwargs['pretrained'] = seq_modeling['backbone']['pretrained']
            if seq_modeling['backbone'].get('weight_dir', None) is not None:
                param_kwargs['weight_dir'] = seq_modeling['backbone']['weight_dir']
            print('kwargs', param_kwargs)

            backbone = ResNet_FeatureExtractor(
                seq_modeling['backbone']['input_channel'],
                seq_modeling['backbone']['output_channel'],
                seq_modeling['backbone']['gcb'],
                **param_kwargs
            )
        elif seq_modeling['backbone']['name'] == 'cnn':
            backbone = None
    else: backbone = None
    max_dimension = (opt['imgH'], opt['max_dimension'][1]) if opt['imgH'] else opt['max_dimension']
    if seq_modeling['patching_style'] == '2d':
        if seq_modeling.get('fix_embed', False):
            encoder = ViTEncoderV3
        else:
            if not seq_modeling.get('interpolate_embed', True):
                encoder = ViTEncoderV2
            else:
                encoder = ViTEncoder
    else:
        encoder = TRIGBaseEncoder

    encoder_seq_modeling = encoder(
        img_size=max_dimension,
        patch_size=seq_modeling['patch_size'],
        in_chans=seq_modeling['input_channel'],
        depth=seq_modeling['depth'],
        num_classes=0,
        embed_dim=seq_modeling['hidden_size'],
        num_heads=seq_modeling['num_heads'],
        hybrid_backbone=backbone
    )

    return encoder_seq_modeling