SunderAli17 commited on
Commit
fc5548c
1 Parent(s): 4212736

Create ipa_faceid_plus.py

Browse files
SAK/models/ipa_faceid_plus/ipa_faceid_plus.py ADDED
@@ -0,0 +1,137 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import torch
2
+ import torch.nn as nn
3
+ import math
4
+
5
+ def reshape_tensor(x, heads):
6
+ bs, length, width = x.shape
7
+ #(bs, length, width) --> (bs, length, n_heads, dim_per_head)
8
+ x = x.view(bs, length, heads, -1)
9
+ # (bs, length, n_heads, dim_per_head) --> (bs, n_heads, length, dim_per_head)
10
+ x = x.transpose(1, 2)
11
+ # (bs, n_heads, length, dim_per_head) --> (bs*n_heads, length, dim_per_head)
12
+ x = x.reshape(bs, heads, length, -1)
13
+ return x
14
+
15
+ def FeedForward(dim, mult=4):
16
+ inner_dim = int(dim * mult)
17
+ return nn.Sequential(
18
+ nn.LayerNorm(dim),
19
+ nn.Linear(dim, inner_dim, bias=False),
20
+ nn.GELU(),
21
+ nn.Linear(inner_dim, dim, bias=False),
22
+ )
23
+
24
+ class PerceiverAttention(nn.Module):
25
+ def __init__(self, *, dim, dim_head=64, heads=8):
26
+ super().__init__()
27
+ self.scale = dim_head**-0.5
28
+ self.dim_head = dim_head
29
+ self.heads = heads
30
+ inner_dim = dim_head * heads
31
+
32
+ self.norm1 = nn.LayerNorm(dim)
33
+ self.norm2 = nn.LayerNorm(dim)
34
+
35
+ self.to_q = nn.Linear(dim, inner_dim, bias=False)
36
+ self.to_kv = nn.Linear(dim, inner_dim * 2, bias=False)
37
+ self.to_out = nn.Linear(inner_dim, dim, bias=False)
38
+
39
+ def forward(self, x, latents):
40
+ """
41
+ Args:
42
+ x (torch.Tensor): image features
43
+ shape (b, n1, D)
44
+ latent (torch.Tensor): latent features
45
+ shape (b, n2, D)
46
+ """
47
+ x = self.norm1(x)
48
+ latents = self.norm2(latents)
49
+
50
+ b, l, _ = latents.shape
51
+
52
+ q = self.to_q(latents)
53
+ kv_input = torch.cat((x, latents), dim=-2)
54
+ k, v = self.to_kv(kv_input).chunk(2, dim=-1)
55
+
56
+ q = reshape_tensor(q, self.heads)
57
+ k = reshape_tensor(k, self.heads)
58
+ v = reshape_tensor(v, self.heads)
59
+
60
+ # attention
61
+ scale = 1 / math.sqrt(math.sqrt(self.dim_head))
62
+ weight = (q * scale) @ (k * scale).transpose(-2, -1) # More stable with f16 than dividing afterwards
63
+ weight = torch.softmax(weight.float(), dim=-1).type(weight.dtype)
64
+ out = weight @ v
65
+
66
+ out = out.permute(0, 2, 1, 3).reshape(b, l, -1)
67
+
68
+ return self.to_out(out)
69
+
70
+ class FacePerceiverResampler(torch.nn.Module):
71
+ def __init__(
72
+ self,
73
+ *,
74
+ dim=768,
75
+ depth=4,
76
+ dim_head=64,
77
+ heads=16,
78
+ embedding_dim=1280,
79
+ output_dim=768,
80
+ ff_mult=4,
81
+ ):
82
+ super().__init__()
83
+
84
+ self.proj_in = torch.nn.Linear(embedding_dim, dim)
85
+ self.proj_out = torch.nn.Linear(dim, output_dim)
86
+ self.norm_out = torch.nn.LayerNorm(output_dim)
87
+ self.layers = torch.nn.ModuleList([])
88
+ for _ in range(depth):
89
+ self.layers.append(
90
+ torch.nn.ModuleList(
91
+ [
92
+ PerceiverAttention(dim=dim, dim_head=dim_head, heads=heads),
93
+ FeedForward(dim=dim, mult=ff_mult),
94
+ ]
95
+ )
96
+ )
97
+
98
+ def forward(self, latents, x):
99
+ x = self.proj_in(x)
100
+ for attn, ff in self.layers:
101
+ latents = attn(x, latents) + latents
102
+ latents = ff(latents) + latents
103
+ latents = self.proj_out(latents)
104
+ return self.norm_out(latents)
105
+
106
+ class ProjPlusModel(torch.nn.Module):
107
+ def __init__(self, cross_attention_dim=768, id_embeddings_dim=512, clip_embeddings_dim=1280, num_tokens=4):
108
+ super().__init__()
109
+
110
+ self.cross_attention_dim = cross_attention_dim
111
+ self.num_tokens = num_tokens
112
+
113
+ self.proj = torch.nn.Sequential(
114
+ torch.nn.Linear(id_embeddings_dim, id_embeddings_dim*2),
115
+ torch.nn.GELU(),
116
+ torch.nn.Linear(id_embeddings_dim*2, cross_attention_dim*num_tokens),
117
+ )
118
+ self.norm = torch.nn.LayerNorm(cross_attention_dim)
119
+
120
+ self.perceiver_resampler = FacePerceiverResampler(
121
+ dim=cross_attention_dim,
122
+ depth=4,
123
+ dim_head=64,
124
+ heads=cross_attention_dim // 64,
125
+ embedding_dim=clip_embeddings_dim,
126
+ output_dim=cross_attention_dim,
127
+ ff_mult=4,
128
+ )
129
+
130
+ def forward(self, id_embeds, clip_embeds, shortcut = True, scale = 1.0):
131
+ x = self.proj(id_embeds)
132
+ x = x.reshape(-1, self.num_tokens, self.cross_attention_dim)
133
+ x = self.norm(x)
134
+ out = self.perceiver_resampler(x, clip_embeds)
135
+ if shortcut:
136
+ out = x + scale * out
137
+ return out