-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsimilarity.py
More file actions
214 lines (165 loc) · 6.7 KB
/
Copy pathsimilarity.py
File metadata and controls
214 lines (165 loc) · 6.7 KB
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
214
import json
import os
import time
from typing import Tuple, List, Dict
from annoy import AnnoyIndex
from annoy.annoylib import Annoy
from sqlalchemy.orm import Session, defer
from tables import FashionItem
NUM_MASKS = 4
EMBEDDING_SIZE = 64
DISTANCE_FUNCTION = 'angular'
NUM_TREES = 10
IMAGES_DIR = 'static/images'
EMBEDDINGS_DIR = 'data/embeddings'
INDEXES_DIR = 'data/annoy_indexes'
ANNOY_EXT = '.ann'
IMAGE_EXT = '.jpg'
ITEM_METADATA_FILE_PATH = 'data/item_metadata.json'
CATEGORIES_FILE_PATH = 'data/categories.csv'
MERGED_CATEGORIES = [
'hats',
'all-body',
'tops',
'bottoms',
'shoes',
'bags',
'accessories'
]
MERGED_CATEGORIES_DISPLAY = [
'Hats',
'Full-Body',
'Tops',
'Bottoms',
'Shoes',
'Bags',
'Accessories'
]
def load_all_items(session: Session):
print('Loading all items.')
categories: Dict[int, str] = {}
with open(CATEGORIES_FILE_PATH) as categories_file:
for line in categories_file:
cat_id, name, _ = line.strip('\n').split(',')
cat_id = int(cat_id)
categories[cat_id] = name
items: Dict[str, Tuple[str, str, List[str]]] = {}
with open(ITEM_METADATA_FILE_PATH) as metadata_file:
metadata_json: Dict = json.load(metadata_file)
for item_name, item_json in metadata_json.items():
cat = categories[int(item_json['category_id'])]
semantic_cat = item_json['semantic_category']
items[item_name] = (cat, semantic_cat, [])
embeddings_paths = ['full_embeddings.csv'] + ['mask_{}_embeddings.csv'.format(i + 1) for i in range(NUM_MASKS)]
embeddings_paths = [os.path.join(EMBEDDINGS_DIR, p) for p in embeddings_paths]
for em_path in embeddings_paths:
with open(em_path) as embeddings_file:
for line in embeddings_file:
sp = line.strip('\n').split(', ')
n = sp[0].strip(IMAGE_EXT)
v = ','.join(sp[1:])
try:
items[n][2].append(v)
except KeyError:
pass
print('Creating ORM objects.')
db_items = []
for name, (cat, semantic_cat, embeddings) in items.items():
db_items.append(FashionItem(
name=name,
category=cat,
semantic_category=semantic_cat,
full_embedding=embeddings[0],
mask_1_embedding=embeddings[1],
mask_2_embedding=embeddings[2],
mask_3_embedding=embeddings[3],
mask_4_embedding=embeddings[4]
))
print('Inserting items.')
start_time = time.time()
session.bulk_save_objects(db_items)
session.commit()
print('Finished inserting items (took {:.2f}s)'.format(time.time() - start_time))
def create_indexes(items: List[FashionItem]):
indexes: List[Annoy] = [Annoy(EMBEDDING_SIZE, DISTANCE_FUNCTION) for i in range(NUM_MASKS + 1)]
for item in items:
em_l = [
item.full_embedding,
item.mask_1_embedding,
item.mask_2_embedding,
item.mask_3_embedding,
item.mask_4_embedding
]
for ind, em in zip(indexes, em_l):
vec = [float(v) for v in em.split(',')]
ind.add_item(item.id, vec)
for ind in indexes:
ind.build(NUM_TREES)
return indexes
PRIMARY_INDEXES: List[Annoy] = []
def load_primary_indexes(session: Session):
if not os.path.exists(INDEXES_DIR):
os.mkdir(INDEXES_DIR)
global PRIMARY_INDEXES
save_names = ['full_index'] + ['mask_{}_index'.format(i + 1) for i in range(NUM_MASKS)]
save_paths = [os.path.join(INDEXES_DIR, n + ANNOY_EXT) for n in save_names]
if False not in [os.path.exists(p) for p in save_paths]:
print('Loading primary indexes.')
PRIMARY_INDEXES = [AnnoyIndex(EMBEDDING_SIZE, DISTANCE_FUNCTION) for i in range(NUM_MASKS + 1)]
for ind, p in zip(PRIMARY_INDEXES, save_paths):
ind.load(p)
return
print('Creating primary indexes.')
items: List[FashionItem] = session.query(FashionItem).order_by(FashionItem.id).all()
print('Loaded items.')
indexes = create_indexes(items)
for ind, path in zip(indexes, save_paths):
ind.save(path)
PRIMARY_INDEXES = indexes
print('Saved primary indexes.')
def load_categories() -> Tuple[List[str], Dict[str, str]]:
categories_set = set()
item_categories_dict: Dict[str, str] = {}
with open(ITEM_METADATA_FILE_PATH) as metadata_file:
metadata_json: Dict = json.load(metadata_file)
for item_name, item_json in metadata_json.items():
p = os.path.join(IMAGES_DIR, item_name + IMAGE_EXT)
c = item_json['semantic_category']
categories_set.add(c)
item_categories_dict[p] = c
cat = sorted(list(categories_set))
print('Loaded {} items from {} categories: {}'.format(len(item_categories_dict), len(cat), cat))
return cat, item_categories_dict
def get_nn_paths(session: Session, index: Annoy, query: FashionItem,
num_results: int) -> List[Tuple[FashionItem, float]]:
# The nearest neighbor search often includes the item itself as the first result,
# so an extra result is included and the query item is removed from the results
results = index.get_nns_by_item(query.id, num_results + 1, include_distances=True)
results = [r for r in zip(*results) if r[0] != query.id]
results = results[0:num_results] # Slice the results in case the query item was not included
result_ids = [r[0] for r in results]
result_items = session.query(FashionItem).filter(FashionItem.id.in_(result_ids))\
.options(
defer(FashionItem.full_embedding),
defer(FashionItem.mask_1_embedding),
defer(FashionItem.mask_2_embedding),
defer(FashionItem.mask_3_embedding),
defer(FashionItem.mask_4_embedding)
).all()
return list(zip(result_items, [r[1] for r in results]))
def get_nns_by_category(session: Session, index: Annoy, query: FashionItem, results_per_category: int,
num_neighbors: int = 1000,
for_categories: List[str] = None) -> Dict[str, List[Tuple[FashionItem, float]]]:
if for_categories is None:
for_categories = MERGED_CATEGORIES
results: Dict[str, List[Tuple[FashionItem, float]]] = {c: [] for c in for_categories}
num_filled = 0
for item, score in get_nn_paths(session, index, query, num_neighbors):
if num_filled >= len(for_categories):
break
c_list = results[item.merged_category()]
if len(c_list) < results_per_category:
c_list.append((item, score))
if len(c_list) >= results_per_category:
num_filled += 1
return results