-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtransformatcher.py
220 lines (161 loc) · 7.57 KB
/
transformatcher.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
import functools
from operator import add
import torch.nn.functional as F
import torch
import torch.nn as nn
import numpy as np
from geometry import Geometry
import backbone as resnet
from einops.layers.torch import Rearrange
from einops import rearrange, repeat, reduce
from rotary_embedding_torch import apply_rotary_emb, RotaryEmbedding
class PreNorm(nn.Module):
def __init__(self, dim, fn):
super().__init__()
self.norm = nn.LayerNorm(dim)
self.fn = fn
def forward(self, x, **kwargs):
x = self.norm(x)
return self.fn(x, **kwargs)
def FeedForward(dim, mult = 4):
return nn.Sequential(
nn.Linear(dim, int(dim * mult)),
nn.GELU(),
nn.Linear(int(dim * mult), dim)
)
class FastAttention(nn.Module):
def __init__(
self,
dim,
*,
heads = 8,
dim_head = 64,
max_seq_len = 15**4,
pos_emb = None
):
super().__init__()
inner_dim = heads * dim_head
self.heads = heads
self.scale = dim_head ** -0.5
self.to_qkv = nn.Linear(dim, inner_dim * 3, bias = False)
self.pos_emb = pos_emb
self.max_seq_len = max_seq_len
self.to_q_attn_logits = nn.Linear(dim_head, 1, bias = False)
self.to_k_attn_logits = nn.Linear(dim_head // 2, 1, bias = False)
self.to_r = nn.Linear(dim_head // 2, dim_head)
self.to_out = nn.Linear(inner_dim, dim)
def forward(self, x, mask = None):
n, device, h = x.shape[1], x.device, self.heads
use_rotary_emb = True
qkv = self.to_qkv(x).chunk(3, dim = -1)
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), qkv)
freqs = self.pos_emb(torch.arange(self.max_seq_len, device = device), cache_key = self.max_seq_len)
freqs = rearrange(freqs[:n], 'n d -> () () n d')
q_aggr, k_aggr = map(lambda t: apply_rotary_emb(freqs, t), (q, k))
v_aggr = v
q_attn_logits = rearrange(self.to_q_attn_logits(q), 'b h n () -> b h n') * self.scale
q_attn = q_attn_logits.softmax(dim = -1)
global_q = torch.einsum('b h n, b h n d -> b h d', q_attn, q_aggr)
global_q = rearrange(global_q, 'b h d -> b h () d')
k = k * global_q
k = reduce(k, 'b h n (d r) -> b h n d', 'sum', r = 2)
k_attn_logits = rearrange(self.to_k_attn_logits(k), 'b h n () -> b h n') * self.scale
k_attn = k_attn_logits.softmax(dim = -1)
global_k = torch.einsum('b h n, b h n d -> b h d', k_attn, k_aggr)
global_k = rearrange(global_k, 'b h d -> b h () d')
u = v_aggr * global_k
u = reduce(u, 'b h n (d r) -> b h n d', 'sum', r = 2)
r = self.to_r(u)
r = r + q
r = rearrange(r, 'b h n d -> b n (h d)')
return self.to_out(r)
class Match2Match(nn.Module):
def __init__(self, feat_dims,luse):
super(Match2Match, self).__init__()
input_dim = 16
layer_num = 6
expand_ratio = 4
bottlen = 26
self.to_embedding = nn.Sequential(
Rearrange('b c h1 w1 h2 w2 -> b (h1 w1 h2 w2) c'),
nn.Linear(bottlen, input_dim)
)
self.posenc = nn.Parameter(torch.randn(15,15,15,15,input_dim), requires_grad=True)
layer_pos_emb = RotaryEmbedding(dim=4, freqs_for = 'pixel')
self.to_original = nn.Sequential(
nn.Linear(input_dim, 1),
Rearrange('b (h1 w1 h2 w2) c -> b c h1 w1 h2 w2', h1=15, w1=15, h2=15, w2=15),
)
self.trans_nc = nn.ModuleList([])
for _ in range(layer_num):
self.trans_nc.append(nn.ModuleList([
PreNorm(input_dim, FastAttention(input_dim, heads = 8, dim_head = 4, pos_emb=layer_pos_emb)),
PreNorm(input_dim, FeedForward(input_dim)),
]))
self.relu = nn.ReLU(inplace=True)
def forward(self, src_feats, trg_feats):
correlations = Geometry.cosine_similarity(src_feats, trg_feats)
correlations = torch.stack(correlations, dim=1)
correlations = correlations.squeeze(2)
correlations = self.relu(correlations)
bsz, ch, side, _, _, _ = correlations.size()
embedded_features = self.to_embedding(correlations)
# Match-to-match attention blocks
for attn, ff in self.trans_nc:
embedded_features = attn(embedded_features) + embedded_features
embedded_features = ff(embedded_features) + embedded_features
refined_corr = self.to_original(embedded_features)
# Geometry upsample
correlations = Geometry.interpolate4d(refined_corr.squeeze(1), Geometry.upsample_size).unsqueeze(1)
side = correlations.size(-1) ** 2
correlations = correlations.view(bsz, side, side).contiguous()
return correlations
class TransforMatcher(nn.Module):
def __init__(self, backbone, luse, device, imside=240):
super(TransforMatcher, self).__init__()
# 1. Backbone network initialization
if backbone == 'resnet50':
self.backbone = resnet.resnet50(pretrained=True).to(device)
nbottlenecks = [3, 4, 6, 3]
elif backbone == 'resnet101':
self.backbone = resnet.resnet101(pretrained=True).to(device)
nbottlenecks = [3, 4, 23, 3]
else:
raise Exception('Unavailable backbone: %s' % backbone)
self.luse = luse
self.bottleneck_ids = functools.reduce(add, list(map(lambda x: list(range(x)), nbottlenecks)))
self.layer_ids = functools.reduce(add, [[i + 1] * x for i, x in enumerate(nbottlenecks)])
self.nbottlenecks = nbottlenecks
if 'resnet' in backbone:
self.feature_extractor = self.extract_bottleneck_features
else:
self.feature_extractor = self.extract_features_pvt
# 2. Pass dummy input to get channel & correlation tensor dimensions
with torch.no_grad():
dummy = torch.randn((2, 3, imside, imside)).to(device)
dummy_feat = self.feature_extractor(dummy, dummy)[0]
feat_dim = [f.size(1) for f in dummy_feat]
Geometry.initialize(imside, device)
# 3. Learnable match-to-match attention module
self.match2match = Match2Match(feat_dim, luse).to(device)
def forward(self, src_img, trg_img):
src_feats, trg_feats = self.feature_extractor(src_img, trg_img)
correlation_ts = self.match2match(src_feats, trg_feats)
return correlation_ts, None
def extract_bottleneck_features(self, src_img, trg_img):
src_feats, trg_feats = [], []
feat = self.backbone.conv1.forward(torch.cat([src_img, trg_img], dim=1))
feat = self.backbone.bn1.forward(feat)
feat = self.backbone.relu.forward(feat)
feat = self.backbone.maxpool.forward(feat)
for idx in range(1, 5):
if idx not in self.luse:
feat = self.backbone.__getattr__('layer%d' % idx)(feat)
else:
layer = self.backbone.__getattr__('layer%d' % idx)
for bid in range(len(layer)):
feat = layer[bid](feat)
src_feats.append(feat.narrow(1, 0, feat.size(1) // 2).clone())
trg_feats.append(feat.narrow(1, feat.size(1) // 2, feat.size(1) // 2).clone())
if idx == max(self.luse): break
return src_feats, trg_feats