[ad8447]: / (2) PyTorch_HistoTNet / util / computeCMC.py

Download this file

53 lines (35 with data), 1.2 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
import numpy as np
def computeCMC(distMatrix, TestLabels, padSize):
sizeDistances = distMatrix.shape
rankV = np.empty((sizeDistances[0]), np.float64)
for r in range(0, sizeDistances[0]):
distV = distMatrix[r, :].tolist()
sortV = np.sort(distV).tolist()
del sortV[0]
minD = sortV[0]
idx = distV.index(minD)
rankV[r] = 1
while (TestLabels[idx] != TestLabels[r]) and (len(sortV) >= 2):
del sortV[0]
minD = sortV[0]
idx = distV.index(minD)
rankV[r] = rankV[r] + 1
# cast
rankV = rankV.astype(int).tolist()
listA = list(range(1, int(np.max(rankV))+1))
probRanks = np.empty((len(listA)), np.float64)
for ll in range(0, len(listA)):
probRanks[ll] = rankV.count(listA[ll]) / sizeDistances[0]
#print(probRanks)
cmcV = np.empty((len(listA)), np.float64)
for i in range(0, len(probRanks)):
if i == 0:
cmcV[i] = probRanks[i]
else:
cmcV[i] = cmcV[i-1] + probRanks[i]
# pad
cmcV_pad = np.pad(cmcV, (0, padSize-len(cmcV)), 'edge')
#print(cmcV)
#print(np.max(rankV))
#print(listA)
return cmcV_pad