-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.py
116 lines (90 loc) · 3.37 KB
/
index.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import abc
from vse.error import NoImageError, DuplicatedImageError
class Index:
def __init__(self, ranker):
self.ranker = ranker
self.vw_freq = []
@abc.abstractmethod
def find(self, query_hist, n):
pass
def __setitem__(self, image_id, hist):
self._add(image_id, hist)
self._update_freq_after_addition(hist)
@abc.abstractmethod
def _add(self, image_id, hist):
pass
def _update_freq_after_addition(self, hist):
if not self.vw_freq:
self.vw_freq = hist.copy()
index_size = len(self)
self.vw_freq = [(n_freq * (index_size - 1) + n) / index_size for n, n_freq in zip(hist, self.vw_freq)]
def __delitem__(self, image_id):
hist = self[image_id]
self._remove(image_id)
self._update_freq_after_deletion(hist)
@abc.abstractmethod
def _remove(self, image_id):
pass
def _update_freq_after_deletion(self, hist):
index_size = len(self)
if index_size == 0:
self.vw_freq = []
self.vw_freq = [(n_freq * (index_size + 1) - n) / index_size for n, n_freq in zip(hist, self.vw_freq)]
@abc.abstractmethod
def __getitem__(self, image_id):
pass
@abc.abstractmethod
def __len__(self):
pass
class ForwardIndex(Index):
def __init__(self, ranker):
Index.__init__(self, ranker)
self.index = {}
def find(self, query_hist, n):
return self.ranker.rank(query_hist, self.index.items(), n, self.vw_freq)
def _add(self, image_id, hist):
if image_id in self.index:
raise DuplicatedImageError(image_id)
self.index[image_id] = hist
def _remove(self, image_id):
if image_id not in self.index:
raise NoImageError(image_id)
del self.index[image_id]
def __getitem__(self, image_id):
return self.index[image_id]
def __len__(self):
return len(self.index)
class InvertedIndex(Index):
def __init__(self, ranker, recognized_visual_words, cutoff=2.0):
Index.__init__(self, ranker)
self.index = [{} for i in range(recognized_visual_words)]
self.cutoff = cutoff / recognized_visual_words
def find(self, query_hist, n):
return self.ranker.rank(query_hist, self._items(query_hist), n, self.vw_freq)
def _items(self, query_hist):
results = {}
for visual_word_freq, subindex in zip(query_hist, self.index):
if visual_word_freq > self.cutoff:
results.update(subindex)
return results.items()
def _add(self, image_id, hist):
for visual_word_freq, subindex in zip(hist, self.index):
if visual_word_freq > self.cutoff:
if image_id in subindex:
raise DuplicatedImageError(image_id)
subindex[image_id] = hist
def _remove(self, image_id):
found = False
for subindex in self.index:
if image_id in subindex:
found = True
del subindex[image_id]
if not found:
raise NoImageError(image_id)
def __getitem__(self, image_id):
for subindex in self.index:
if image_id in subindex:
return subindex[image_id]
raise KeyError(image_id)
def __len__(self):
return len(set(image_id for subindex in self.index for image_id in subindex))