-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.py
213 lines (177 loc) · 6.97 KB
/
helper.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import gzip
import os
import random
import urllib.request
import webbrowser
from collections import defaultdict
import numpy as np
from jina import Document
from jina.helper import colored
from jina.logging.predefined import default_logger
from jina.logging.profile import ProgressBar
result_html = []
top_k = 0
num_docs_evaluated = 0
evaluation_value = defaultdict(float)
classes_label = {'0': 'Tshirt/Top', '1': 'Trouser/pants', '2': 'Pullover/shirt', '3': 'Dress', '4': 'Coat', '5': 'Sandal', '6': 'Shirt', '7': 'Sneaker', '8': 'Bag', '9': 'Ankle Boot'}
def _get_groundtruths(target, pseudo_match=True):
# group doc_ids by their labels
a = np.squeeze(target['index-labels']['data'])
a = np.stack([a, np.arange(len(a))], axis=1)
a = a[a[:, 0].argsort()]
lbl_group = np.split(a[:, 1], np.unique(a[:, 0], return_index=True)[1][1:])
# each label has one groundtruth, i.e. all docs that have the same label are considered as matches
groundtruths = {lbl: Document() for lbl in range(10)}
for lbl, doc_ids in enumerate(lbl_group):
if not pseudo_match:
# full-match, each doc has 6K matches
for doc_id in doc_ids:
match = Document()
match.tags['id'] = int(doc_id)
groundtruths[lbl].matches.append(match)
else:
# pseudo-match, each doc has only one match, but this match's id is a list of 6k elements
match = Document()
match.tags['id'] = doc_ids.tolist()
groundtruths[lbl].matches.append(match)
return groundtruths
def index_generator(num_docs: int, target: dict):
"""
Generate the index data.
:param num_docs: Number of documents to be indexed.
:param target: Dictionary which stores the data paths
:yields: index data
"""
for internal_doc_id in range(num_docs):
# x_blackwhite.shape is (28,28)
x_blackwhite = 255 - target['index']['data'][internal_doc_id]
# x_color.shape is (28,28,3)
label = int(target['index-labels']['data'][internal_doc_id][0])
x_color = np.stack((x_blackwhite,) * 3, axis=-1)
# Add class label to the documents
d = Document(content=x_color, tags={'label': label})
d.tags['id'] = internal_doc_id
yield d
def query_generator(num_docs: int, target: dict, with_groundtruth: bool = True):
"""
Generate the query data.
:param num_docs: Number of documents to be queried
:param target: Dictionary which stores the data paths
:param with_groundtruth: True if want to include labels into query data
:yields: query data
"""
gts = _get_groundtruths(target)
for _ in range(num_docs):
num_data = len(target['query-labels']['data'])
idx = random.randint(0, num_data - 1)
# x_blackwhite.shape is (28,28)
x_blackwhite = 255 - target['query']['data'][idx]
# x_color.shape is (28,28,3)
x_color = np.stack((x_blackwhite,) * 3, axis=-1)
d = Document(content=x_color)
if with_groundtruth:
gt = gts[target['query-labels']['data'][idx][0]]
yield d, gt
else:
yield d
def print_result(resp):
"""
Callback function to receive results.
:param resp: returned response with data
"""
global top_k
global evaluation_value
global classes_label
for d in resp.docs:
vi = d.uri
result_html.append(f'<tr><td><img src="{vi}"/></td><td>')
result_html.append(f'<label for="{classes_label[d.id]}"/> {classes_label[d.id]}</label>')
result_html.append(f'<span style="display:inline-block; width: 20px;"></span>')
top_k = len(d.matches)
for kk in d.matches:
kmi = kk.uri
result_html.append(
f'<img src="{kmi}" style="opacity:{kk.scores["cosine"].value}"/>'
)
result_html.append('</td></tr>\n')
# update evaluation values
# as evaluator set to return running avg, here we can simply replace the value
for k, evaluation in d.evaluations.items():
evaluation_value[k] = evaluation.value
def write_html(html_path):
"""
Method to present results in browser.
:param html_path: path of the written html
"""
with open(
os.path.join(os.path.dirname(os.path.realpath(__file__)), 'demo.html')
) as fp, open(html_path, 'w') as fw:
t = fp.read()
t = t.replace('{% RESULT %}', '\n'.join(result_html))
t = t.replace(
'{% PRECISION_EVALUATION %}',
'{:.2f}%'.format(evaluation_value['Precision'] * 100.0),
)
t = t.replace(
'{% RECALL_EVALUATION %}',
'{:.2f}%'.format(evaluation_value['Recall'] * 100.0),
)
t = t.replace('{% TOP_K %}', str(top_k))
fw.write(t)
url_html_path = 'file://' + os.path.abspath(html_path)
try:
webbrowser.open(url_html_path, new=2)
except:
pass # intentional pass, browser support isn't cross-platform
finally:
default_logger.info(
f'You should see a "demo.html" opened in your browser, '
f'if not you may open {url_html_path} manually'
)
colored_url = colored(
'https://github.com/jina-ai/jina', color='cyan', attrs='underline'
)
default_logger.info(
f'🤩 Intrigued? Play with `jina hello fashion --help` and learn more about Jina at {colored_url}'
)
def download_data(targets, download_proxy=None, task_name='download fashion-mnist'):
"""
Download data.
:param targets: target path for data.
:param download_proxy: download proxy (e.g. 'http', 'https')
:param task_name: name of the task
"""
opener = urllib.request.build_opener()
opener.addheaders = [('User-agent', 'Mozilla/5.0')]
if download_proxy:
proxy = urllib.request.ProxyHandler(
{'http': download_proxy, 'https': download_proxy}
)
opener.add_handler(proxy)
urllib.request.install_opener(opener)
with ProgressBar(description=task_name) as t:
for k, v in targets.items():
if not os.path.exists(v['filename']):
urllib.request.urlretrieve(
v['url'], v['filename'], reporthook=lambda *x: t.update(0.01)
)
if k == 'index-labels' or k == 'query-labels':
v['data'] = load_labels(v['filename'])
if k == 'index' or k == 'query':
v['data'] = load_mnist(v['filename'])
def load_mnist(path):
"""
Load MNIST data
:param path: path of data
:return: MNIST data in np.array
"""
with gzip.open(path, 'rb') as fp:
return np.frombuffer(fp.read(), dtype=np.uint8, offset=16).reshape([-1, 28, 28])
def load_labels(path: str):
"""
Load labels from path
:param path: path of labels
:return: labels in np.array
"""
with gzip.open(path, 'rb') as fp:
return np.frombuffer(fp.read(), dtype=np.uint8, offset=8).reshape([-1, 1])