-
Notifications
You must be signed in to change notification settings - Fork 15
/
model.py
244 lines (188 loc) · 8.54 KB
/
model.py
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
#!/usr/bin/env python
from typing import Dict, Iterable, List, Tuple
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
from vision_transformer import QuickGELU, Attention
from weight_loaders import weight_loader_fn_dict
from vision_transformer import (
VisionTransformer2D, TransformerDecoderLayer,
model_to_fp16, vit_presets,
)
class TemporalCrossAttention(nn.Module):
def __init__(
self,
spatial_size: Tuple[int, int] = (14, 14),
feature_dim: int = 768,
):
super().__init__()
self.spatial_size = spatial_size
w_size = np.prod([x * 2 - 1 for x in spatial_size])
self.w1 = nn.Parameter(torch.zeros([w_size, feature_dim]))
self.w2 = nn.Parameter(torch.zeros([w_size, feature_dim]))
idx_tensor = torch.zeros([np.prod(spatial_size) for _ in (0, 1)], dtype=torch.long)
for q in range(np.prod(spatial_size)):
qi, qj = q // spatial_size[1], q % spatial_size[1]
for k in range(np.prod(spatial_size)):
ki, kj = k // spatial_size[1], k % spatial_size[1]
i_offs = qi - ki + spatial_size[0] - 1
j_offs = qj - kj + spatial_size[1] - 1
idx_tensor[q, k] = i_offs * (spatial_size[1] * 2 - 1) + j_offs
self.idx_tensor = idx_tensor
def forward_half(self, q: torch.Tensor, k: torch.Tensor, w: torch.Tensor) -> torch.Tensor:
q, k = q[:, :, 1:], k[:, :, 1:] # remove cls token
assert q.size() == k.size()
assert q.size(2) == np.prod(self.spatial_size)
attn = torch.einsum('ntqhd,ntkhd->ntqkh', q / (q.size(-1) ** 0.5), k)
attn = attn.softmax(dim=-2).mean(dim=-1) # L, L, N, T
self.idx_tensor = self.idx_tensor.to(w.device)
w_unroll = w[self.idx_tensor] # L, L, C
ret = torch.einsum('ntqk,qkc->ntqc', attn, w_unroll)
return ret
def forward(self, q: torch.Tensor, k: torch.Tensor):
N, T, L, H, D = q.size()
assert L == np.prod(self.spatial_size) + 1
ret = torch.zeros([N, T, L, self.w1.size(-1)], device='cuda')
ret[:, 1:, 1:, :] += self.forward_half(q[:, 1:, :, :, :], k[:, :-1, :, :, :], self.w1)
ret[:, :-1, 1:, :] += self.forward_half(q[:, :-1, :, :, :], k[:, 1:, :, :, :], self.w2)
return ret
class EVLDecoder(nn.Module):
def __init__(
self,
num_frames: int = 8,
spatial_size: Tuple[int, int] = (14, 14),
num_layers: int = 4,
in_feature_dim: int = 768,
qkv_dim: int = 768,
num_heads: int = 12,
mlp_factor: float = 4.0,
enable_temporal_conv: bool = True,
enable_temporal_pos_embed: bool = True,
enable_temporal_cross_attention: bool = True,
mlp_dropout: float = 0.5,
):
super().__init__()
self.enable_temporal_conv = enable_temporal_conv
self.enable_temporal_pos_embed = enable_temporal_pos_embed
self.enable_temporal_cross_attention = enable_temporal_cross_attention
self.num_layers = num_layers
self.decoder_layers = nn.ModuleList(
[TransformerDecoderLayer(in_feature_dim, qkv_dim, num_heads, mlp_factor, mlp_dropout) for _ in range(num_layers)]
)
if enable_temporal_conv:
self.temporal_conv = nn.ModuleList(
[nn.Conv1d(in_feature_dim, in_feature_dim, kernel_size=3, stride=1, padding=1, groups=in_feature_dim) for _ in range(num_layers)]
)
if enable_temporal_pos_embed:
self.temporal_pos_embed = nn.ParameterList(
[nn.Parameter(torch.zeros([num_frames, in_feature_dim])) for _ in range(num_layers)]
)
if enable_temporal_cross_attention:
self.cross_attention = nn.ModuleList(
[TemporalCrossAttention(spatial_size, in_feature_dim) for _ in range(num_layers)]
)
self.cls_token = nn.Parameter(torch.zeros([in_feature_dim]))
def _initialize_weights(self):
nn.init.normal_(self.cls_token, std=0.02)
def forward(self, in_features: List[Dict[str, torch.Tensor]]):
N, T, L, C = in_features[0]['out'].size()
assert len(in_features) == self.num_layers
x = self.cls_token.view(1, 1, -1).repeat(N, 1, 1)
for i in range(self.num_layers):
frame_features = in_features[i]['out']
if self.enable_temporal_conv:
feat = in_features[i]['out']
feat = feat.permute(0, 2, 3, 1).contiguous().flatten(0, 1) # N * L, C, T
feat = self.temporal_conv[i](feat)
feat = feat.view(N, L, C, T).permute(0, 3, 1, 2).contiguous() # N, T, L, C
frame_features += feat
if self.enable_temporal_pos_embed:
frame_features += self.temporal_pos_embed[i].view(1, T, 1, C)
if self.enable_temporal_cross_attention:
frame_features += self.cross_attention[i](in_features[i]['q'], in_features[i]['k'])
frame_features = frame_features.flatten(1, 2) # N, T * L, C
x = self.decoder_layers[i](x, frame_features)
return x
class EVLTransformer(nn.Module):
def __init__(
self,
num_frames: int = 8,
backbone_name: str = 'ViT-B/16',
backbone_type: str = 'clip',
backbone_path: str = '',
backbone_mode: str = 'frozen_fp16',
decoder_num_layers: int = 4,
decoder_qkv_dim: int = 768,
decoder_num_heads: int = 12,
decoder_mlp_factor: float = 4.0,
num_classes: int = 400,
enable_temporal_conv: bool = True,
enable_temporal_pos_embed: bool = True,
enable_temporal_cross_attention: bool = True,
cls_dropout: float = 0.5,
decoder_mlp_dropout: float = 0.5,
):
super().__init__()
self.decoder_num_layers = decoder_num_layers
backbone_config = self._create_backbone(backbone_name, backbone_type, backbone_path, backbone_mode)
backbone_feature_dim = backbone_config['feature_dim']
backbone_spatial_size = tuple(x // y for x, y in zip(backbone_config['input_size'], backbone_config['patch_size']))
self.decoder = EVLDecoder(
num_frames=num_frames,
spatial_size=backbone_spatial_size,
num_layers=decoder_num_layers,
in_feature_dim=backbone_feature_dim,
qkv_dim=decoder_qkv_dim,
num_heads=decoder_num_heads,
mlp_factor=decoder_mlp_factor,
enable_temporal_conv=enable_temporal_conv,
enable_temporal_pos_embed=enable_temporal_pos_embed,
enable_temporal_cross_attention=enable_temporal_cross_attention,
mlp_dropout=decoder_mlp_dropout,
)
self.proj = nn.Sequential(
nn.LayerNorm(backbone_feature_dim),
nn.Dropout(cls_dropout),
nn.Linear(backbone_feature_dim, num_classes),
)
def _create_backbone(
self,
backbone_name: str,
backbone_type: str,
backbone_path: str,
backbone_mode: str,
) -> dict:
weight_loader_fn = weight_loader_fn_dict[backbone_type]
state_dict = weight_loader_fn(backbone_path)
backbone = VisionTransformer2D(return_all_features=True, **vit_presets[backbone_name])
backbone.load_state_dict(state_dict, strict=True) # weight_loader_fn is expected to strip unused parameters
assert backbone_mode in ['finetune', 'freeze_fp16', 'freeze_fp32']
if backbone_mode == 'finetune':
self.backbone = backbone
else:
backbone.eval().requires_grad_(False)
if backbone_mode == 'freeze_fp16':
model_to_fp16(backbone)
self.backbone = [backbone] # avoid backbone parameter registration
return vit_presets[backbone_name]
def _get_backbone(self, x):
if isinstance(self.backbone, list):
# freeze backbone
self.backbone[0] = self.backbone[0].to(x.device)
return self.backbone[0]
else:
# finetune bakbone
return self.backbone
def forward(self, x: torch.Tensor):
backbone = self._get_backbone(x)
B, C, T, H, W = x.size()
x = x.permute(0, 2, 1, 3, 4).flatten(0, 1)
features = backbone(x)[-self.decoder_num_layers:]
features = [
dict((k, v.float().view(B, T, *v.size()[1:])) for k, v in x.items())
for x in features
]
x = self.decoder(features)
x = self.proj(x[:, 0, :])
return x