generated from automl/automl_template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect_crops.py
180 lines (135 loc) · 6.11 KB
/
select_crops.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
import torch
import torch.nn.functional as nnf
@torch.no_grad()
def select_crops_identity(images, model, fp16):
return images[0], images[1], torch.zeros(1), torch.zeros(1)
@torch.no_grad()
def select_crops_cross(images, model, fp16):
b, c, h, w = images[0].shape
device = images[0].device
with torch.cuda.amp.autocast(fp16 is not None):
model_out = model.module.single_forward(torch.cat(images, dim=0))
p1s, z1s = model_out[0].chunk(len(images)), model_out[1].chunk(len(images))
out1 = torch.zeros_like(images[0])
out2 = torch.zeros_like(images[0])
score = torch.full([b], torch.inf, device=device)
selected = torch.zeros((2, b), dtype=torch.uint8)
for n in range(len(images)):
p1, z1 = p1s[n], z1s[n]
for m in range(n + 1, len(images)):
p2, z2 = p1s[m], z1s[m]
with torch.cuda.amp.autocast(fp16 is not None):
sim = (nnf.cosine_similarity(p1, z2) + nnf.cosine_similarity(p2, z1)) * 0.5
score, indices = torch.stack((score, sim)).min(dim=0)
indices = indices.type(torch.bool)
selected[0][indices] = n
selected[1][indices] = m
out1 = torch.where(indices[:, None, None, None], images[n], out1)
out2 = torch.where(indices[:, None, None, None], images[m], out2)
return out1, out2, selected, score
@torch.no_grad()
def select_crops_avgpool(images, model, fp16):
b, c, h, w = images[0].shape
device = images[0].device
with torch.cuda.amp.autocast(fp16 is not None):
model_out = model.module.avgpool_layer(torch.cat(images, dim=0))
activations = model_out.chunk(len(images))
out1 = torch.zeros_like(images[0])
out2 = torch.zeros_like(images[0])
score = torch.full([b], torch.inf, device=device)
for n, x in enumerate(activations):
e1 = activations[n]
for m in range(n + 1, len(activations)):
e2 = activations[m]
with torch.cuda.amp.autocast(fp16 is not None):
sim = nnf.cosine_similarity(e1, e2)
score, indices = torch.stack((score, sim)).min(dim=0)
indices = indices.type(torch.bool)
out1 = torch.where(indices[:, None, None, None], images[n], out1)
out2 = torch.where(indices[:, None, None, None], images[m], out2)
return out1, out2
@torch.no_grad()
def select_crops_conv1_layer(images, model, fp16):
b, c, h, w = images[0].shape
device = images[0].device
with torch.cuda.amp.autocast(fp16 is not None):
model_out = model.module.conv1_layer(torch.cat(images, dim=0))
activations = model_out.chunk(len(images))
out1 = torch.zeros_like(images[0])
out2 = torch.zeros_like(images[0])
score = torch.full([b], torch.inf, device=device)
for n, x in enumerate(activations):
e1 = activations[n]
for m in range(n + 1, len(activations)):
e2 = activations[m]
with torch.cuda.amp.autocast(fp16 is not None):
sim = nnf.cosine_similarity(e1, e2)
score, indices = torch.stack((score, sim)).min(dim=0)
indices = indices.type(torch.bool)
out1 = torch.where(indices[:, None, None, None], images[n], out1)
out2 = torch.where(indices[:, None, None, None], images[m], out2)
return out1, out2
@torch.no_grad()
def select_crops_first_layer(images, model, fp16):
b, c, h, w = images[0].shape
device = images[0].device
with torch.cuda.amp.autocast(fp16 is not None):
model_out = model.module.first_layer(torch.cat(images, dim=0))
activations = model_out.chunk(len(images))
out1 = torch.zeros_like(images[0])
out2 = torch.zeros_like(images[0])
score = torch.full([b], torch.inf, device=device)
for n, x in enumerate(activations):
e1 = activations[n]
for m in range(n + 1, len(activations)):
e2 = activations[m]
with torch.cuda.amp.autocast(fp16 is not None):
sim = nnf.cosine_similarity(e1, e2)
score, indices = torch.stack((score, sim)).min(dim=0)
indices = indices.type(torch.bool)
out1 = torch.where(indices[:, None, None, None], images[n], out1)
out2 = torch.where(indices[:, None, None, None], images[m], out2)
return out1, out2
@torch.no_grad()
def select_crops_second_layer(images, model, fp16):
b, c, h, w = images[0].shape
device = images[0].device
with torch.cuda.amp.autocast(fp16 is not None):
model_out = model.module.second_layer(torch.cat(images, dim=0))
activations = model_out.chunk(len(images))
out1 = torch.zeros_like(images[0])
out2 = torch.zeros_like(images[0])
score = torch.full([b], torch.inf, device=device)
for n, x in enumerate(activations):
e1 = activations[n]
for m in range(n + 1, len(activations)):
e2 = activations[m]
with torch.cuda.amp.autocast(fp16 is not None):
sim = nnf.cosine_similarity(e1, e2)
score, indices = torch.stack((score, sim)).min(dim=0)
indices = indices.type(torch.bool)
out1 = torch.where(indices[:, None, None, None], images[n], out1)
out2 = torch.where(indices[:, None, None, None], images[m], out2)
return out1, out2
@torch.no_grad()
def select_crops_anchor(images, model, fp16):
with torch.cuda.amp.autocast(fp16 is not None):
embeds = [model.module.simple_forward(img) for img in images]
p1, z1 = embeds[0]
scores = [nnf.cosine_similarity(p1, z2) + nnf.cosine_similarity(p2, z1) for p2, z2 in embeds[1:]]
stacked = torch.stack(scores)
values, indic = stacked.min(dim=0)
a = nnf.one_hot(indic, len(scores)).T
out = torch.zeros_like(images[0])
for n, img in enumerate(images[1:]):
out += a[n].view(-1, 1, 1, 1) * img
return images[0], out
names = {
"anchor": select_crops_anchor,
"cross": select_crops_cross,
"conv1layer": select_crops_conv1_layer,
"firstlayer": select_crops_first_layer,
"secondlayer": select_crops_second_layer,
"avgpool": select_crops_avgpool,
"identity": select_crops_identity,
}