-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathmodules.py
More file actions
291 lines (223 loc) · 10.9 KB
/
modules.py
File metadata and controls
291 lines (223 loc) · 10.9 KB
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
import torch
from utils import *
import torch.nn.functional as F
import models.dino.vision_transformer as vits
class DinoFeaturizer(nn.Module):
def __init__(self, dim, cfg):
super().__init__()
self.cfg = cfg
self.dim = dim
patch_size = self.cfg.dino_patch_size
self.patch_size = patch_size
self.feat_type = self.cfg.dino_feat_type
arch = self.cfg.model_type
self.model = vits.__dict__[arch](patch_size=patch_size, num_classes=0)
for p in self.model.parameters():
p.requires_grad = False
self.model.eval()
if arch == "vit_small" and patch_size == 16:
url = "dino_deitsmall16_pretrain/dino_deitsmall16_pretrain.pth"
elif arch == "vit_small" and patch_size == 8:
url = "dino_deitsmall8_300ep_pretrain/dino_deitsmall8_300ep_pretrain.pth"
elif arch == "vit_base" and patch_size == 16:
url = "dino_vitbase16_pretrain/dino_vitbase16_pretrain.pth"
elif arch == "vit_base" and patch_size == 8:
url = "dino_vitbase8_pretrain/dino_vitbase8_pretrain.pth"
else:
raise ValueError("Unknown arch and patch size")
print("Since no pretrained weights have been provided, we load the reference pretrained DINO weights.")
state_dict = torch.hub.load_state_dict_from_url(url="https://dl.fbaipublicfiles.com/dino/" + url)
self.model.load_state_dict(state_dict, strict=True)
def forward(self, img, n=1, return_class_feat=False):
self.model.eval()
with torch.no_grad():
assert (img.shape[2] % self.patch_size == 0)
assert (img.shape[3] % self.patch_size == 0)
# get selected layer activations
feat, attn, qkv = self.model.get_intermediate_feat(img, n=n)
feat, attn, qkv = feat[0], attn[0], qkv[0]
feat_h = img.shape[2] // self.patch_size
feat_w = img.shape[3] // self.patch_size
if self.feat_type == "feat":
image_feat = feat[:, 1:, :].reshape(feat.shape[0], feat_h, feat_w, -1).permute(0, 3, 1, 2).contiguous()
elif self.feat_type == "KK":
image_k = qkv[1, :, :, 1:, :].reshape(feat.shape[0], 6, feat_h, feat_w, -1)
B, H, I, J, D = image_k.shape
image_feat = image_k.permute(0, 1, 4, 2, 3).reshape(B, H * D, I, J)
# image_feat = qkv[:, 1:, :].reshape(feat.shape[0], feat_h, feat_w, -1).permute(0, 3, 1, 2)
else:
raise ValueError("Unknown feat type:{}".format(self.feat_type))
if return_class_feat:
return feat[:, :1, :].reshape(feat.shape[0], 1, 1, -1).permute(0, 3, 1, 2)
return image_feat
class DinoV2Featurizer(nn.Module):
def __init__(self, dim, cfg):
super().__init__()
self.cfg = cfg
self.dim = dim
self.patch_size = self.cfg.dino_patch_size
arch = self.cfg.model_type
if arch == "vit_small":
self.model = torch.hub.load('facebookresearch/dinov2', 'dinov2_vits14')
elif arch == "vit_base":
self.model = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitb14')
elif arch == "vit_large":
self.model = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitl14')
elif arch == "vit_giant":
self.model = torch.hub.load('facebookresearch/dinov2', 'dinov2_vitg14')
for p in self.model.parameters():
p.requires_grad = False
self.model.eval()
def forward(self, img, n=1):
self.model.eval()
with torch.no_grad():
# get selected layer activations
feat = self.model.get_intermediate_layers(img, n=n, reshape=True)
image_feat = feat[0]
return image_feat
class Projection(nn.Module):
def __init__(self, cfg):
super(Projection, self).__init__()
self.cfg = cfg
self.dim = cfg.dim
self.n_feats = cfg.n_feats
self.dropout = torch.nn.Dropout2d(p=0.2)
self.cluster1 = self.make_clusterer(self.n_feats)
self.proj_type = cfg.projection_type
if self.proj_type == "nonlinear":
self.cluster2 = self.make_nonlinear_clusterer(self.n_feats)
def make_clusterer(self, in_channels):
return torch.nn.Sequential(
torch.nn.Conv2d(in_channels, self.dim, (1, 1))
)
def make_nonlinear_clusterer(self, in_channels):
return torch.nn.Sequential(
torch.nn.Conv2d(in_channels, in_channels, (1, 1)),
torch.nn.SiLU(),
torch.nn.Conv2d(in_channels, self.dim, (1, 1)))
def forward(self, feat):
if self.proj_type is not None:
code = self.cluster1(self.dropout(feat))
if self.proj_type == "nonlinear":
code += self.cluster2(self.dropout(feat))
else:
code = feat
if self.cfg.dropout:
return self.dropout(feat), code
else:
return feat, code
class Prediction(nn.Module):
def __init__(self, cfg, n_classes: int, sigma=False):
super(Prediction, self).__init__()
self.n_classes = n_classes
self.dim = cfg.dim
# self.local_clusters = nn.init.kaiming_normal_(torch.nn.Parameter(torch.randn(self.n_classes, self.dim)))
# self.local_clusters = nn.init.orthogonal_(torch.nn.Parameter(torch.randn(self.n_classes, self.dim)))
self.local_clusters = nn.init.xavier_normal_(torch.nn.Parameter(torch.randn(self.n_classes, self.dim)))
self.init_global_clusters()
self.alpha = cfg.alpha
if sigma:
self.sigma = nn.Parameter(torch.Tensor(1))
self.sigma.data.fill_(1)
else:
self.register_parameter('sigma', None)
def init_global_clusters(self):
self.local_clusters.data = F.normalize(self.local_clusters, dim=1)
self.global_clusters = torch.nn.Parameter(self.local_clusters.data.clone())
self.global_clusters.requires_grad = False
def update_global_clusters(self):
self.global_clusters.data = self.alpha * self.global_clusters.data + (1 - self.alpha) * self.local_clusters.data
def reset_parameters(self):
with torch.no_grad():
self.local_clusters = nn.init.xavier_normal_(torch.nn.Parameter(torch.randn(self.n_classes, self.dim)))
def forward(self, x):
normed_local_clusters = F.normalize(self.local_clusters, dim=1)
normed_global_clusters = F.normalize(self.global_clusters, dim=1)
normed_features = F.normalize(x, dim=1)
inner_products_local = torch.einsum("bchw,nc->bnhw", normed_features.detach(), normed_local_clusters)
inner_products_global = torch.einsum("bchw,nc->bnhw", normed_features, normed_global_clusters.detach())
if self.sigma is not None:
inner_products_local = self.sigma * inner_products_local
return inner_products_local, inner_products_global
class FeaturePyramidNet(nn.Module):
def __init__(self, cut_model):
super(FeaturePyramidNet, self).__init__()
self.layer_nums = [5, 6, 7]
self.spatial_resolutions = [7, 14, 28, 56]
self.feat_channels = [2048, 1024, 512, 3]
for p in cut_model.parameters():
p.requires_grad = False
self.encoder = NetWithActivations(cut_model, self.layer_nums)
def forward(self, x):
with torch.no_grad():
feats = self.encoder(x)
low_res_feats = feats[self.layer_nums[-1]]
return low_res_feats
class NetWithActivations(torch.nn.Module):
def __init__(self, model, layer_nums):
super(NetWithActivations, self).__init__()
self.layers = nn.ModuleList(model.children())
self.layer_nums = []
for l in layer_nums:
if l < 0:
self.layer_nums.append(len(self.layers) + l)
else:
self.layer_nums.append(l)
self.layer_nums = set(sorted(self.layer_nums))
def forward(self, x):
activations = {}
for ln, l in enumerate(self.layers):
x = l(x)
if ln in self.layer_nums:
activations[ln] = x
return activations
def norm(t):
return F.normalize(t, dim=1, eps=1e-10)
def tensor_correlation(a, b):
return torch.einsum("nchw,ncij->nhwij", a, b)
def sample(t: torch.Tensor, coords: torch.Tensor):
return F.grid_sample(t, coords.permute(0, 2, 1, 3), padding_mode='border', align_corners=True)
@torch.jit.script
def super_perm(size: int, device: torch.device):
perm = torch.randperm(size, device=device, dtype=torch.long)
perm[perm == torch.arange(size, device=device)] += 1
return perm % size
class Energy_minimization_loss(nn.Module):
def __init__(self, cfg, n_classes):
super(Energy_minimization_loss, self).__init__()
self.cfg = cfg
self.n_classes = n_classes
self.smooth_loss = ContrastiveCorrelationLoss(cfg)
def forward(self, signal, inner_products_local, inner_products_global, temperature=0.1):
cluster_probs = torch.softmax(inner_products_global / temperature, dim=1)
pos_intra_loss, pos_intra_cd, neg_inter_loss, neg_inter_cd = self.smooth_loss(signal, cluster_probs)
smooth_loss = pos_intra_loss + neg_inter_loss
# cluster_probs_global = F.one_hot(torch.argmax(inner_products_global, dim=1), self.n_classes) \
# .permute(0, 3, 1, 2).to(torch.float32)
# data_loss = -(cluster_probs_global * inner_products_local).sum(1).mean()
target = torch.argmax(inner_products_global, dim=1)
flat_logits = inner_products_local.permute(0, 2, 3, 1).reshape(-1, self.n_classes)
flat_target = target.reshape(-1)
data_loss = F.cross_entropy(flat_logits, flat_target, reduction='none')
return smooth_loss, data_loss.mean(), pos_intra_cd, neg_inter_cd
class ContrastiveCorrelationLoss(nn.Module):
def __init__(self, cfg, ):
super(ContrastiveCorrelationLoss, self).__init__()
self.cfg = cfg
def helper(self, f1, f2, c1, c2, shift):
with torch.no_grad():
fd = tensor_correlation(norm(f1), norm(f2))
if self.cfg.pointwise:
old_mean = fd.mean()
fd -= fd.mean([3, 4], keepdim=True)
fd = fd - fd.mean() + old_mean
cd = 1 - tensor_correlation(norm(c1), norm(c2))
loss = (fd - shift) * cd
return loss, cd
def forward(self, orig_feats: torch.Tensor, orig_code: torch.Tensor):
perm_neg = super_perm(orig_feats.size(0), orig_feats.device)
feats_neg = orig_feats[perm_neg]
code_neg = orig_code[perm_neg]
pos_intra_loss, pos_intra_cd = self.helper(orig_feats, orig_feats, orig_code, orig_code, self.cfg.pos_intra_shift)
neg_inter_loss, neg_inter_cd = self.helper(orig_feats, feats_neg, orig_code, code_neg, self.cfg.neg_inter_shift)
return pos_intra_loss.mean(), pos_intra_cd.mean(), neg_inter_loss.mean(), neg_inter_cd.mean()