forked from DietmarKracht/Keras-Openmax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenmax_utils.py
47 lines (37 loc) · 1.7 KB
/
openmax_utils.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
import scipy.spatial.distance as spd
class DistanceType:
def eucos(self):
return 'eucos'
def euclidean(self):
return 'euclidean'
def cosine(self):
return 'cosine'
def squeuclidean(self):
return 'squeuclidean'
def compute_distance(query_channel, channel, mean_vec, distance_type = 'eucos'):
""" Compute the specified distance type between chanels of mean vector and query image.
In caffe library, FC8 layer consists of 10 channels. Here, we compute distance
of distance of each channel (from query image) with respective channel of
Mean Activation Vector. In the paper, we considered a hybrid distance eucos which
combines euclidean and cosine distance for bouding open space. Alternatively,
other distances such as euclidean or cosine can also be used.
Input:
--------
query_channel: Particular FC8 channel of query image
channel: channel number under consideration
mean_vec: mean activation vector
Output:
--------
query_distance : Distance between respective channels
"""
if distance_type == 'eucos':
query_distance = spd.euclidean(mean_vec[channel, :], query_channel) /200. + spd.cosine(mean_vec[channel, :], query_channel)
elif distance_type == 'euclidean':
query_distance = spd.euclidean(mean_vec[channel, :], query_channel) /200.
elif distance_type == 'cosine':
query_distance = spd.cosine(mean_vec[channel, :], query_channel)
elif distance_type == DistanceType.squeuclidean():
query_distance = spd.sqeuclidean(mean_vec[channel, :], query_channel)
else:
print("distance type not known: enter either of eucos, euclidean or cosine")
return query_distance