-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
316 lines (271 loc) · 11 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
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
import torch
import torch.nn as nn
import torch_geometric.nn as pyg
from typing import Optional, Type, List
from timm.layers.helpers import to_2tuple
from functools import partial
from timm.layers import DropPath
from timm.models.vision_transformer import init_weights_vit_timm
from timm.models._manipulate import named_apply
class GNN(torch.nn.Module):
def __init__(self, hidden_channels, embed_dim,out_channels, num_layers = 4,extracted_feature_dim=1536,name_dim=4096):
super().__init__()
input_channel = extracted_feature_dim
self.pretransform = pyg.Linear(input_channel,hidden_channels,bias=False)
self.leaklyrelu = nn.LeakyReLU(0.2)
self.convs = nn.ModuleList()
for _ in range(num_layers):
conv = pyg.HeteroConv({
('window', 'near', 'window'): pyg.SAGEConv(hidden_channels,hidden_channels),
('window', 'knn', 'window'): pyg.SAGEConv(hidden_channels,hidden_channels),
}, aggr='mean')
self.convs.append(conv)
self.lin = pyg.Linear(hidden_channels, out_channels)
self.weight_generator = Transformer(in_dim = name_dim, embed_dim=embed_dim, init_values=0.1)
def forward(self, x_dict, edge_index_dict, descriptions, size):
x_dict["window"] = self.pretransform(x_dict["window"])
for conv in self.convs:
x_dict = conv(x_dict, edge_index_dict)
x_dict = {key: self.leaklyrelu(x) for key, x in x_dict.items()}
weight, bias = self.weight_generator(descriptions, size)
return self.lin(x_dict["window"]) @ weight.T + bias.T
def padding(x, CLSREG = True):
x = [i.squeeze(0) for i in x]
MAX_LENGTH = max([len(i) for i in x])
X_PAD = []
MASK = []
for i in x:
pad = torch.zeros((MAX_LENGTH - i.size(0),i.size(1))).to(i)
X_PAD.append(torch.cat((i,pad)))
MASK.append([0] * i.size(0) + [1] * (MAX_LENGTH - i.size(0)))
X_PAD = torch.stack((X_PAD))
MASK = torch.BoolTensor(MASK).to(X_PAD.device)
if CLSREG:
MASK = torch.cat((torch.zeros((MASK.size(0),2)).bool().to(MASK.device),MASK),1)
MASK = MASK.unsqueeze(1).unsqueeze(1)
return X_PAD, MASK
class Attention(nn.Module):
def __init__(
self,
dim: int,
num_heads: int = 8,
qkv_bias: bool = False,
qk_norm: bool = False,
attn_drop: float = 0.,
proj_drop: float = 0.,
norm_layer: nn.Module = nn.LayerNorm,
) -> None:
super().__init__()
assert dim % num_heads == 0, 'dim should be divisible by num_heads'
self.num_heads = num_heads
self.head_dim = dim // num_heads
self.scale = self.head_dim ** -0.5
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
self.q_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
self.k_norm = norm_layer(self.head_dim) if qk_norm else nn.Identity()
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(proj_drop)
def forward(self, x: torch.Tensor, mask: torch.BoolTensor) -> torch.Tensor:
B, N, C = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, self.head_dim).permute(2, 0, 3, 1, 4)
q, k, v = qkv.unbind(0)
q, k = self.q_norm(q), self.k_norm(k)
q = q * self.scale
attn = q @ k.transpose(-2, -1)
attn = attn.masked_fill_(mask,-float('inf'))
attn = attn.softmax(dim=-1)
attn = self.attn_drop(attn)
x = attn @ v
x = x.transpose(1, 2).reshape(B, N, C)
x = self.proj(x)
x = self.proj_drop(x)
return x
class LayerScale(nn.Module):
def __init__(
self,
dim: int,
init_values: float = 1e-5,
inplace: bool = False,
) -> None:
super().__init__()
self.inplace = inplace
self.gamma = nn.Parameter(init_values * torch.ones(dim))
def forward(self, x: torch.Tensor) -> torch.Tensor:
return x.mul_(self.gamma) if self.inplace else x * self.gamma
class Mlp(nn.Module):
def __init__(
self,
in_features,
hidden_features=None,
out_features=None,
act_layer=nn.GELU,
norm_layer=None,
bias=True,
drop=0.,
use_conv=False,
):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
bias = to_2tuple(bias)
drop_probs = to_2tuple(drop)
linear_layer = partial(nn.Conv2d, kernel_size=1) if use_conv else nn.Linear
self.fc1 = linear_layer(in_features, hidden_features, bias=bias[0])
self.act = act_layer()
self.drop1 = nn.Dropout(drop_probs[0])
self.norm = norm_layer(hidden_features) if norm_layer is not None else nn.Identity()
self.fc2 = linear_layer(hidden_features, out_features, bias=bias[1])
self.drop2 = nn.Dropout(drop_probs[1])
def forward(self, x):
x = self.fc1(x)
x = self.act(x)
x = self.drop1(x)
x = self.norm(x)
x = self.fc2(x)
x = self.drop2(x)
return x
class Block(nn.Module):
def __init__(
self,
dim: int,
num_heads: int,
mlp_ratio: float = 4.,
qkv_bias: bool = False,
qk_norm: bool = False,
proj_drop: float = 0.,
attn_drop: float = 0.,
init_values: Optional[float] = None,
drop_path: float = 0.,
act_layer: nn.Module = nn.GELU,
norm_layer: nn.Module = nn.LayerNorm,
mlp_layer: nn.Module = Mlp,
) -> None:
super().__init__()
self.norm1 = norm_layer(dim)
self.attn = Attention(
dim,
num_heads=num_heads,
qkv_bias=qkv_bias,
qk_norm=qk_norm,
attn_drop=attn_drop,
proj_drop=proj_drop,
norm_layer=norm_layer,
)
self.ls1 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity()
self.drop_path1 = DropPath(drop_path) if drop_path > 0. else nn.Identity()
self.norm2 = norm_layer(dim)
self.mlp = mlp_layer(
in_features=dim,
hidden_features=int(dim * mlp_ratio),
act_layer=act_layer,
drop=proj_drop,
)
self.ls2 = LayerScale(dim, init_values=init_values) if init_values else nn.Identity()
self.drop_path2 = DropPath(drop_path) if drop_path > 0. else nn.Identity()
def forward(self, x: torch.Tensor, mask: torch.BoolTensor) -> torch.Tensor:
x = x + self.drop_path1(self.ls1(self.attn(self.norm1(x),mask)))
x = x + self.drop_path2(self.ls2(self.mlp(self.norm2(x))))
return x
class Transformer(nn.Module):
def __init__(
self,
in_dim: int = 3,
embed_dim: int = 256,
depth: int = 2,
num_heads: int = 4,
mlp_ratio: float = 4.,
qkv_bias: bool = True,
qk_norm: bool = False,
init_values: Optional[float] = None,
class_token: bool = True,
reg_tokens: int = 1,
pre_norm: bool = False,
fc_norm: Optional[bool] = None,
drop_rate: float = 0.,
pos_drop_rate: float = 0.,
proj_drop_rate: float = 0.,
attn_drop_rate: float = 0.,
drop_path_rate: float = 0.,
norm_layer: Optional[nn.Module] = None,
act_layer: Optional[nn.Module] = None,
block_fn: Type[nn.Module] = Block,
mlp_layer: Type[nn.Module] = Mlp,
) -> None:
super().__init__()
norm_layer = partial(nn.LayerNorm, eps=1e-6)
act_layer = nn.GELU
self.num_features = self.embed_dim = embed_dim
self.num_prefix_tokens = 1 if class_token else 0
self.num_prefix_tokens += reg_tokens
self.num_reg_tokens = reg_tokens
self.has_class_token = class_token
self.patch_embed = nn.Linear(in_dim, embed_dim)
self.cls_token = nn.Parameter(torch.zeros(1, 1, embed_dim)) if class_token else None
self.reg_token = nn.Parameter(torch.zeros(1, reg_tokens, embed_dim)) if reg_tokens else None
self.norm_pre = norm_layer(embed_dim) if pre_norm else nn.Identity()
self.segment = nn.Embedding(5, embed_dim)
torch.nn.init.zeros_(self.segment.weight)
dpr = [x.item() for x in torch.linspace(0, drop_path_rate, depth)]
self.blocks = nn.Sequential(*[
block_fn(
dim=embed_dim,
num_heads=num_heads,
mlp_ratio=mlp_ratio,
qkv_bias=qkv_bias,
qk_norm=qk_norm,
init_values=init_values,
proj_drop=proj_drop_rate,
attn_drop=attn_drop_rate,
drop_path=dpr[i],
norm_layer=norm_layer,
act_layer=act_layer,
mlp_layer=mlp_layer,
)
for i in range(depth)])
self.norm = norm_layer(embed_dim)
self.head = nn.Linear(self.embed_dim, embed_dim + 1)
self.init_weights()
def init_weights(self) -> None:
if self.cls_token is not None:
nn.init.normal_(self.cls_token, std=1e-6)
named_apply(init_weights_vit_timm, self)
def _cat_token(self, x: torch.Tensor) -> torch.Tensor:
to_cat = []
if self.cls_token is not None:
to_cat.append(self.cls_token.expand(x.shape[0], -1, -1))
if self.reg_token is not None:
to_cat.append(self.reg_token.expand(x.shape[0], -1, -1))
x = torch.cat(to_cat + [x], dim=1)
return x
def forward_features(self, x: torch.Tensor, mask: torch.BoolTensor, size:List[List]) -> torch.Tensor:
x = self.patch_embed(x)
MAX_LEN = x.size(1)
SEGMENT = []
for i in size:
current_size = []
for k,j in enumerate(i):
k+=1
current_size.extend([k]*j)
current_size.extend([0] * (MAX_LEN-len(current_size)))
SEGMENT.append(current_size)
SEGMENT = self.segment(torch.LongTensor(SEGMENT).to(x.device))
x = x + SEGMENT
x = self._cat_token(x)
x = self.norm_pre(x)
x = x[:,:512]
mask = mask[...,:512]
for block in self.blocks:
x = block(x,mask)
x = self.norm(x)
return x
def forward_head(self, x: torch.Tensor, pre_logits: bool = False) -> torch.Tensor:
x = x[:, 0]
return x if pre_logits else self.head(x)
def forward(self, x: List[torch.Tensor], size: List[List]) -> torch.Tensor:
x, mask = padding(x,True)
x = self.forward_features(x,mask, size)
x = self.forward_head(x)
weight, bias = x[:,:-1],x[:,[-1]]
weight = torch.nn.functional.normalize(weight,p=2,dim=1)
return weight,bias