-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
112 lines (84 loc) · 3.33 KB
/
main.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
import argparse
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
from model import *
from index import *
MODEL_DIR = "./pretrained"
DATABASE = np.load("./database/database.npz")["arr_0"]
ORIGIN = np.load("./database/origin.npz")["arr_0"]
def config():
p = argparse.ArgumentParser()
p.add_argument("--mode", default="local", type=str, help="mode")
p.add_argument("--search_mode", default="faiss", type=str, help="search mode")
p.add_argument("--image_dir", default="./image", help="directory of image")
p.add_argument("--image_url", default=None, help="url of image")
return p.parse_args()
def recommend(config):
model, feature_extractor = pretrained(MODEL_DIR=MODEL_DIR)
if config.mode == "local":
abroad = Image.open(config.image_dir)
inputs = feature_extractor(images=abroad, return_tensors="pt")
represent = model(**inputs).hidden_states[-1][0][-1].detach().numpy()
if config.search_mode == "faiss":
index = index_faiss(DATABASE)
_, indices = index.search(represent.reshape(1, -1), 1)
fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax1.imshow(abroad)
ax1.set_title("Abroad")
ax1.axis("off")
ax2 = fig.add_subplot(1, 2, 2)
ax2.imshow(ORIGIN[indices[0][0]])
ax2.set_title("Recommendation")
ax2.axis("off")
plt.show()
elif config.search_mode == "hnsw":
p = index_hnsw(DATABASE)
indices, _ = p.knn_query(represent, k=1)
fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax1.imshow(abroad)
ax1.set_title("Abroad")
ax1.axis("off")
ax2 = fig.add_subplot(1, 2, 2)
ax2.imshow(ORIGIN[indices[0][0]])
ax2.set_title("Recommendation")
ax2.axis("off")
plt.show()
elif config.mode == "remote":
import requests
from io import BytesIO
response = requests.get(config.image_url)
abroad = Image.open(BytesIO(response.content))
inputs = feature_extractor(images=abroad, return_tensors="pt")
represent = model(**inputs).hidden_states[-1][0][-1].detach().numpy()
if config.search_mode == "faiss":
index = index_faiss(DATABASE)
_, indices = index.search(represent.reshape(1, -1), 1)
fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax1.imshow(abroad)
ax1.set_title("Abroad")
ax1.axis("off")
ax2 = fig.add_subplot(1, 2, 2)
ax2.imshow(ORIGIN[indices[0][0]])
ax2.set_title("Recommendation")
ax2.axis("off")
plt.show()
elif config.search_mode == "hnsw":
p = index_hnsw(DATABASE)
indices, _ = p.knn_query(represent, k=1)
fig = plt.figure()
ax1 = fig.add_subplot(1, 2, 1)
ax1.imshow(abroad)
ax1.set_title("Abroad")
ax1.axis("off")
ax2 = fig.add_subplot(1, 2, 2)
ax2.imshow(ORIGIN[indices[0][0]])
ax2.set_title("Recommendation")
ax2.axis("off")
plt.show()
if __name__ == "__main__":
config = config()
recommend(config)