-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathMSFormer_utils.py
172 lines (136 loc) · 5.82 KB
/
MSFormer_utils.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
import torch
import torch.nn as nn
from typing import Tuple
from einops import rearrange
from einops.layers.torch import Rearrange
from torch.nn import functional as F
class OverlapPatchEmbeddings(nn.Module):
def __init__(self, img_size=224, patch_size=7, stride=4, padding=1, in_ch=3, dim=768):
super().__init__()
self.proj = nn.Conv2d(in_ch, dim, patch_size, stride, padding)
self.norm = nn.BatchNorm2d(dim)
def forward(self, x: torch.Tensor) -> torch.Tensor:
px = self.proj(x)
px = self.norm(px)
_, _, H, W = px.shape
fx = px.flatten(2).transpose(1, 2)
return fx, H, W
class DWConv(nn.Module):
def __init__(self, dim):
super().__init__()
self.dwconv = nn.Conv2d(dim, dim, 3, 1, 1, groups=dim)
def forward(self, x: torch.Tensor, H, W) -> torch.Tensor:
B, N, C = x.shape
tx = x.transpose(1, 2).view(B, C, H, W)
conv_x = self.dwconv(tx)
return conv_x.flatten(2).transpose(1, 2)
class MixFFN(nn.Module):
def __init__(self, c1, c2):
super().__init__()
self.fc1 = nn.Linear(c1, c2)
self.dwconv = DWConv(c2)
self.act = nn.GELU()
self.fc2 = nn.Linear(c2, c1)
def forward(self, x: torch.Tensor, H, W) -> torch.Tensor:
ax = self.act(self.dwconv(self.fc1(x), H, W))
out = self.fc2(ax)
return out
class MixFFN_skip(nn.Module):
def __init__(self, c1, c2):
super().__init__()
self.fc1 = nn.Linear(c1, c2)
self.dwconv = DWConv(c2)
self.act = nn.GELU()
self.fc2 = nn.Linear(c2, c1)
self.norm1 = nn.LayerNorm(c2)
def forward(self, x: torch.Tensor, H, W) -> torch.Tensor:
ax = self.act(self.norm1(self.dwconv(self.fc1(x), H, W)+self.fc1(x)))
out = self.fc2(ax)
return out
class MLP_FFN(nn.Module):
def __init__(self, c1, c2):
super().__init__()
self.fc1 = nn.Linear(c1, c2)
self.act = nn.GELU()
self.fc2 = nn.Linear(c2, c1)
def forward(self, x, H, W):
x = self.fc1(x)
x = self.act(x)
x = self.fc2(x)
return x
class EfficientAttention(nn.Module):
"""
input -> x:[B, D, H, W]
output -> [B, D, H, W]
in_channels: int -> Embedding Dimension
key_channels: int -> Key Embedding Dimension, Best: (in_channels)
value_channels: int -> Value Embedding Dimension, Best: (in_channels or in_channels//2)
head_count: int -> It divides the embedding dimension by the head_count and process each part individually
"""
def __init__(self, in_channels, key_channels, value_channels, head_count=1):
super().__init__()
self.in_channels = in_channels
self.key_channels = key_channels
self.head_count = head_count
self.value_channels = value_channels
self.keys = nn.Conv2d(in_channels, key_channels, 1)
self.queries = nn.Conv2d(in_channels, key_channels, 1)
self.values = nn.Conv2d(in_channels, value_channels, 1)
self.reprojection = nn.Conv2d(value_channels, in_channels, 1)
def forward(self, input_):
n, _, h, w = input_.size()
keys = self.keys(input_).reshape((n, self.key_channels, h * w))
queries = self.queries(input_).reshape(n, self.key_channels, h * w)
values = self.values(input_).reshape((n, self.value_channels, h * w))
head_key_channels = self.key_channels // self.head_count
head_value_channels = self.value_channels // self.head_count
attended_values = []
for i in range(self.head_count):
key = F.softmax(keys[
:,
i * head_key_channels: (i + 1) * head_key_channels,
:
], dim=2)
query = F.softmax(queries[
:,
i * head_key_channels: (i + 1) * head_key_channels,
:
], dim=1)
value = values[
:,
i * head_value_channels: (i + 1) * head_value_channels,
:
]
context = key @ value.transpose(1, 2) # dk*dv
attended_value = (context.transpose(1, 2) @ query).reshape(n, head_value_channels, h, w) # n*dv
attended_values.append(attended_value)
aggregated_values = torch.cat(attended_values, dim=1)
attention = self.reprojection(aggregated_values)
return attention, context
class EfficientTransformerBlock(nn.Module):
"""
Input -> x (Size: (b, (H*W), d)), H, W
Output -> (b, (H*W), d)
"""
def __init__(self, in_dim, key_dim, value_dim, head_count=1, token_mlp='mix'):
super().__init__()
self.norm1 = nn.LayerNorm(in_dim)
self.attn = EfficientAttention(in_channels=in_dim, key_channels=key_dim,
value_channels=value_dim, head_count=1)
self.norm2 = nn.LayerNorm(in_dim)
if token_mlp=='mix':
self.mlp = MixFFN(in_dim, int(in_dim*4))
elif token_mlp=='mix_skip':
self.mlp = MixFFN_skip(in_dim, int(in_dim*4))
else:
self.mlp = MLP_FFN(in_dim, int(in_dim*4))
def forward(self, x: torch.Tensor, H, W) -> torch.Tensor:
x = Rearrange('b d h w -> b (h w) d')(x)
norm_1 = self.norm1(x)
norm_1 = Rearrange('b (h w) d -> b d h w', h=H, w=W)(norm_1)
attn, context = self.attn(norm_1)
attn = Rearrange('b d h w -> b (h w) d')(attn)
tx = x + attn
mx = tx + self.mlp(self.norm2(tx), H, W)
mx = Rearrange('b (h w) d -> b d h w', h=H, w=W)(mx)
return mx, context