forked from Wangt-CN/VC-R-CNN
-
Notifications
You must be signed in to change notification settings - Fork 0
/
knn_feature.py
32 lines (22 loc) · 1015 Bytes
/
knn_feature.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
import h5py
import numpy as np
import torch
coco_rcnn = h5py.File('/data3/wangtan/openimage/target_dir/coco/coco_train_all_gt.hdf5','r')
coco_vc = h5py.File('/data3/wangtan/openimage/target_dir/coco/coco_train_all_vc_0.hdf5', 'r')
def cosine_sim(x1, x2, dim=1, eps=1e-8):
"""Returns cosine similarity between x1 and x2, computed along dim."""
x1 = torch.tensor(x1)
x2 = torch.tensor(x2)
w12 = torch.sum(x1 * x2, dim)
w1 = torch.norm(x1, 2, dim)
w2 = torch.norm(x2, 2, dim)
return (w12 / (w1 * w2).clamp(min=eps)).squeeze()
def l2_sim(x1, x2, dim=1):
x1 = torch.tensor(x1)
x2 = torch.tensor(x2)
return torch.pow(torch.sum((x1 - x2).pow(2), dim), 0.5)
for image_id in coco_vc.keys():
feature_rcnn = coco_rcnn[image_id]['feature'][:]
feature_vc = coco_vc[image_id]['feature'][:]
sim_rcnn = cosine_sim(feature_rcnn[:, None, :], feature_rcnn[None, :, :])
sim_vc = cosine_sim(feature_vc[:, None, :], feature_vc[None, : , :])