-
Notifications
You must be signed in to change notification settings - Fork 17
/
efficient_ir.py
80 lines (61 loc) · 2.7 KB
/
efficient_ir.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
import os
import numpy as np
from PIL import Image
import hnswlib
import onnxruntime
class EfficientIR:
def __init__(self, img_size, index_capacity, index_path, model_path):
self.img_size = img_size
self.index_capacity = index_capacity
self.index_path = index_path
self.model_path = model_path
self.init_index()
self.load_index()
self.init_model()
Image.MAX_IMAGE_PIXELS = None
def img_preprocess(self, image_path):
try:
img = Image.open(image_path).resize((self.img_size, self.img_size),Image.BICUBIC)
img = img.convert('RGBA').convert('RGB')
except OSError:
print(f'\nFile broken: {image_path}')
return None
input_data = np.array(img).transpose(2, 0, 1)
# 预处理
img_data = input_data.astype('float32')
mean_vec = np.array([0.485, 0.456, 0.406])
stddev_vec = np.array([0.229, 0.224, 0.225])
norm_img_data = np.zeros(img_data.shape).astype('float32')
for i in range(img_data.shape[0]):
norm_img_data[i,:,:] = (img_data[i,:,:]/255 - mean_vec[i]) / stddev_vec[i]
# add batch channel
norm_img_data = norm_img_data.reshape(1, 3, self.img_size, self.img_size).astype('float32')
return norm_img_data
def init_index(self):
self.hnsw_index = hnswlib.Index(space='l2', dim=1000)
return self.hnsw_index
def load_index(self):
if os.path.exists(self.index_path):
self.hnsw_index.load_index(self.index_path, max_elements=self.index_capacity)
else:
self.hnsw_index.init_index(max_elements=self.index_capacity, ef_construction=200, M=48)
def save_index(self):
self.hnsw_index.save_index(self.index_path)
def init_model(self):
self.session_opti = onnxruntime.SessionOptions()
self.session_opti.enable_mem_pattern = False
self.session = onnxruntime.InferenceSession(self.model_path, self.session_opti)
# self.session.set_providers(['DmlExecutionProvider'])
self.model_input = self.session.get_inputs()[0].name
return self.session, self.model_input
def get_fv(self, image_path):
norm_img_data = self.img_preprocess(image_path)
if norm_img_data is None:
return None
return self.session.run([], {self.model_input: norm_img_data})[0][0]
def add_fv(self, fv, idx):
self.hnsw_index.add_items(fv, idx)
def match(self, fv, nc=5):
query = self.hnsw_index.knn_query(fv, k=nc)
similarity = (1-np.tanh(query[1][0]/3000))*100
return similarity, query[0][0]