-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathload_model.py
48 lines (37 loc) · 1.78 KB
/
load_model.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
import clip
import torch
import pandas as pd
import json
import numpy as np
from urllib.request import urlopen
#code credit : https://github.com/haltakov/natural-language-image-search
# Load the open CLIP model
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
# Load the photo IDs
photo_ids = pd.read_csv("/Users/monk/Desktop/research_paper_visuals/clip_model_api/unsplash/photo_ids.csv")
photo_ids = list(photo_ids['photo_id'])
# Load the features vectors
photo_features = np.load("/Users/monk/Desktop/research_paper_visuals/clip_model_api/unsplash/features.npy")
# Convert features to Tensors: Float32 on CPU and Float16 on GPU
if device == "cpu":
photo_features = torch.from_numpy(photo_features).float().to(device)
else:
photo_features = torch.from_numpy(photo_features).to(device)
# Print some statistics
def encode_search_query(search_query):
with torch.no_grad():
# Encode and normalize the search query using CLIP
text_encoded = model.encode_text(clip.tokenize(search_query).to(device))
text_encoded /= text_encoded.norm(dim=-1, keepdim=True)
# Retrieve the feature vector
return text_encoded
def find_best_matches(text_features, results_count=3, key = None):
text_features = encode_search_query(text_features)
similarities = (photo_features @ text_features.T).squeeze(1)
best_photo_idx = (-similarities).argsort()
pics_data = [photo_ids[i] for i in best_photo_idx[:results_count]]
unsplash_api_url = [f"https://api.unsplash.com/photos/{pic}?client_id={str(key)}" for pic in pics_data]
print(unsplash_api_url)
photo_data = [json.loads(urlopen(k).read().decode("utf-8")) for k in unsplash_api_url]
return photo_data