|
a |
|
b/(1) PyTorch_HistoNet/util/computeCMC.py |
|
|
1 |
import numpy as np |
|
|
2 |
|
|
|
3 |
def computeCMC(distMatrix, TestLabels, padSize): |
|
|
4 |
|
|
|
5 |
sizeDistances = distMatrix.shape |
|
|
6 |
rankV = np.empty((sizeDistances[0]), np.float64) |
|
|
7 |
|
|
|
8 |
for r in range(0, sizeDistances[0]): |
|
|
9 |
distV = distMatrix[r, :].tolist() |
|
|
10 |
sortV = np.sort(distV).tolist() |
|
|
11 |
|
|
|
12 |
del sortV[0] |
|
|
13 |
minD = sortV[0] |
|
|
14 |
idx = distV.index(minD) |
|
|
15 |
|
|
|
16 |
rankV[r] = 1 |
|
|
17 |
|
|
|
18 |
while (TestLabels[idx] != TestLabels[r]) and (len(sortV) >= 2): |
|
|
19 |
del sortV[0] |
|
|
20 |
minD = sortV[0] |
|
|
21 |
idx = distV.index(minD) |
|
|
22 |
|
|
|
23 |
rankV[r] = rankV[r] + 1 |
|
|
24 |
|
|
|
25 |
# cast |
|
|
26 |
rankV = rankV.astype(int).tolist() |
|
|
27 |
|
|
|
28 |
listA = list(range(1, int(np.max(rankV))+1)) |
|
|
29 |
probRanks = np.empty((len(listA)), np.float64) |
|
|
30 |
for ll in range(0, len(listA)): |
|
|
31 |
probRanks[ll] = rankV.count(listA[ll]) / sizeDistances[0] |
|
|
32 |
|
|
|
33 |
#print(probRanks) |
|
|
34 |
|
|
|
35 |
cmcV = np.empty((len(listA)), np.float64) |
|
|
36 |
for i in range(0, len(probRanks)): |
|
|
37 |
if i == 0: |
|
|
38 |
cmcV[i] = probRanks[i] |
|
|
39 |
else: |
|
|
40 |
cmcV[i] = cmcV[i-1] + probRanks[i] |
|
|
41 |
|
|
|
42 |
# pad |
|
|
43 |
cmcV_pad = np.pad(cmcV, (0, padSize-len(cmcV)), 'edge') |
|
|
44 |
|
|
|
45 |
#print(cmcV) |
|
|
46 |
|
|
|
47 |
#print(np.max(rankV)) |
|
|
48 |
#print(listA) |
|
|
49 |
|
|
|
50 |
return cmcV_pad |
|
|
51 |
|
|
|
52 |
|