This repository was archived by the owner on Nov 30, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcosts.py
executable file
·80 lines (55 loc) · 1.92 KB
/
costs.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
71
72
73
74
75
76
77
78
79
80
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
import numpy as np
from functools import partial
from wbia_curvrank.pydtw import dtw_weighted_euclidean
from wbia_curvrank.pydtw import dtw_weighted_chi_square
def get_cost_func_dict():
# map cost func names to function objects
return {
'dtw-l2': get_dtw_l2,
'dtw-chi2': get_dtw_chi2,
'norm-l2': get_norm_l2,
'random': get_random,
'hist': get_hist,
}
def get_cost_func(name, **kwargs):
cost_func_dict = get_cost_func_dict()
return cost_func_dict[name](**kwargs)
def get_dtw_l2(**kwargs):
cost_func = partial(dtw_weighted_euclidean, **kwargs)
return cost_func
def get_dtw_chi2(**kwargs):
cost_func = partial(dtw_weighted_chi_square, **kwargs)
return cost_func
def get_norm_l2(**kwargs):
weights = kwargs.get('weights')
cost_func = partial(norm_l2, weights=weights)
return cost_func
def get_random(**kwargs):
cost_func = random_cost
return cost_func
# some additional cost functions
def norm_l2(qcurv, dcurv, weights):
return np.sqrt(np.sum(weights * (qcurv - dcurv) ** 2))
def random_cost(qcurv, dcurv):
return np.random.random()
def get_hist(**kwargs):
cost_func = hist_intersect
return cost_func
def hist_intersect(qcurv, dcurv):
num_bins = 10
qhist = np.zeros((qcurv.shape[1], num_bins), dtype=np.float32)
dhist = np.zeros((dcurv.shape[1], num_bins), dtype=np.float32)
for j in range(qcurv.shape[1]):
qhist[j], _ = np.histogram(
qcurv[:, j], bins=np.linspace(0, 1, 1 + num_bins), density=True
)
dhist[j], _ = np.histogram(
dcurv[:, j], bins=np.linspace(0, 1, 1 + num_bins), density=True
)
qhist[j] /= qhist[j].sum()
dhist[j] /= dhist[j].sum()
qfeat = qhist.flatten()
dfeat = dhist.flatten()
return -1.0 * np.sum(np.minimum(qfeat, dfeat))