-
Notifications
You must be signed in to change notification settings - Fork 0
/
CDNN.py
70 lines (62 loc) · 1.76 KB
/
CDNN.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
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
'''
If you use this code or CDNN algorithm for your research, please cite this paper.
@article{nguyen2015robust,
title={Robust biometric recognition from palm depth images for gloved hands},
author={Nguyen, Binh P and Tay, Wei-Liang and Chui, Chee-Kong},
journal={IEEE Transactions on Human-Machine Systems},
volume={45},
number={6},
pages={799--804},
year={2015},
publisher={IEEE}
}
'''
import numpy as np
def CDNN(input, label, test_sample, k):
'''
input: is a list with shape N*d where N is the number of samples, d is the sample dimension
label: is a list with shape N, where N is the number of samples
test_sample: is a list with shape d where d is the sample dimension
k: number of nearest neighbors
'''
input_dim = len(input[0])
#calculate distance
d = []
for i in range(len(input)):
distance = np.linalg.norm(test_sample - input[i])
d.append(distance)
d = np.asarray(d)
#get k lowest distance and save to Sx
index = np.argpartition(d, k) # return k indexes of lowest value in d
Sx = dict()
for idx in range(k):
key = index[idx]
if label[key] in Sx:
Sx[label[key]].append(input[key])
else:
Sx[label[key]] = []
Sx[label[key]].append(input[key])
#calculate centroid
px = dict()
for key in Sx:
sum_item = np.zeros(input_dim)
for i in range(len(Sx[key])):
sum_item += Sx[key][i]
px_item = sum_item/len(Sx[key])
px[key] = px_item
#calculate new centroid
qx = dict()
for key in Sx:
sum_item = np.zeros(input_dim)
for i in range(len(Sx[key])):
sum_item+=Sx[key][i]
sum_item += test_sample
qx_item = sum_item/(len(Sx[key]) + 1)
qx[key] = qx_item
#calculate displacement
theta = dict()
for key in px:
if key in qx:
theta[key] = np.linalg.norm(px[key] - qx[key])
#get the label
return min(theta, key=theta.get)