-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.py
34 lines (27 loc) · 961 Bytes
/
tools.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
import numpy
def gen_label(labels):
num = len(labels)
gt = numpy.zeros(shape=(num,num))
for i, label in enumerate(labels):
for k in range(num):
if labels[k] == label:
gt[i,k] = 1
return gt
def convert_models_to_fp32(model):
for p in model.parameters():
p.data = p.data.float()
if p.grad is not None:
p.grad.data = p.grad.data.float()
def convert_models_to_fp16(model):
print(model)
for p in model.parameters():
p.data = p.data.half()
p.grad.data = p.grad.data.half()
def create_logits(x1, x2, logit_scale):
x1 = x1 / x1.norm(dim=-1, keepdim=True)
x2 = x2 / x2.norm(dim=-1, keepdim=True)
# cosine similarity as logits
logits_per_x1 = logit_scale * x1 @ x2.t()
logits_per_x2 = logit_scale * x2 @ x1.t()
# shape = [global_batch_size, global_batch_size]
return logits_per_x1, logits_per_x2