Repository navigation
Expand file tree
/
Copy pathScatter.py
More file actions
116 lines (91 loc) · 2.87 KB
/
Copy pathScatter.py
File metadata and controls
116 lines (91 loc) · 2.87 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
import torch
import matplotlib.pyplot as plt
import numpy as np
import os
def recall(Fvec, label):
"""This function is used to find the nearest same-class points similarity for each instance
and neareat different-class similarity points for each instance.
Parameters
----------
Fvec : Tensor, shape (n_samples, n_features)
Embedding representation.
label : List, length (n_feafures)
Label for each instance.
Returns
-------
disList : List, shape (n_features,2)
Two similarity(nearest same-class and nearest diff-class) for each instance.
"""
N = len(label)
print(N)
disList = []
D = Fvec.mm(torch.t(Fvec))
D[torch.eye(N).byte()] = -1
sort_D, indices = torch.sort(D,1,descending=True)
print(indices[0])
for i in range(0,N):
dis = []
distance = sort_D[i]
index = indices[i]
for j in range(0,N):
if label[index[j].item()]==label[i]:
dis.append(distance[j].item())
break;
for k in range(0,N):
if label[index[k].item()]!=label[i]:
dis.append(distance[k].item())
break;
disList.append(dis)
return disList
def draw_sca(disList,name='',savepath='./'):
""" This function is used to generate similarity scatter plot and save it.
Parameters
----------
disList : List, shape (n_features,2)
Two similarity(nearest same-class and nearest diff-class) for each instance.
name : String
Title of scatter plot
savepath : String
Path to save this plot
"""
disList = np.asarray(disList)
print(disList.shape)
A=disList[:,0]
B=disList[:,1]
diff=B>A
same=A>B
plt.figure(figsize=(6.4,6.4))
plt.title(name)
plt.xlim(0.3,1.0)
plt.ylim(0.3,1.0)
plt.xlabel('Similarity (Same class)',fontsize=22)
plt.ylabel('Similarity (Different class)',fontsize=22)
samex = A[same]
samey = B[same]
difx = A[diff]
dify = B[diff]
plt.scatter(samex,samey,c='c',s=0.1)
plt.scatter(difx,dify,color=[1.0, 0.5, 0.25],s=0.1)
if not os.path.isdir(savepath):
os.makedirs(savepath)
plt.savefig(savepath+'scatter_plot.jpg')
def gene_sca(vecPath,labelPath,name='',savepath='./'):
""" This function is used to find generate similarity list, generate similarity scatter plot and save it.
Parameters
----------
vecPath : String
Path saving embedding vector
labelPath : String
Path saving label
name : String
Title of scatter plot
savepath : String
Path to save this plot
"""
#Fvec = torch.load(vecPath)
Fvec = np.load(vecPath)
#label = torch.load(labelPath)
label = np.load(labelPath)
Fvec = torch.Tensor(Fvec)
disList = recall(Fvec,label)
draw_sca(disList,name,savepath)