-
Notifications
You must be signed in to change notification settings - Fork 7
/
extract_textual_clip_embeddings.py
74 lines (53 loc) · 2.21 KB
/
extract_textual_clip_embeddings.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
import os
import csv
import numpy as np
import torch
import clip
USE_CACHE = False
BATCH_SIZE = 2048
OUTDIR = "embeddings"
os.makedirs(OUTDIR, exist_ok=True)
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
with open('data.csv', newline='') as csvfile:
reader = csv.reader(csvfile)
_headers = next(reader)
prompt_data = set([(row[0], row[1]) for row in reader if row[1] != ''])
prompt_ids = [data[0] for data in prompt_data]
prompts = (data[1] for data in prompt_data)
prompt_ids_filename = os.path.join(OUTDIR, f"prompt_ids.npy")
np.save(prompt_ids_filename, prompt_ids)
text_embeddings = None
batched_prompts = []
for idx, prompt in enumerate(prompts):
batched_prompts.append(prompt)
if len(batched_prompts) % BATCH_SIZE == 0 or idx == len(prompt_ids) - 1:
print(f"processing -- {idx + 1}")
batch_text_embeddings_filename = os.path.join(
OUTDIR, f"text_embeddings_{idx + 1}.npy")
if os.path.exists(batch_text_embeddings_filename) and USE_CACHE:
batch_text_embeddings = np.load(batch_text_embeddings_filename)
else:
with torch.no_grad():
batched_text = clip.tokenize(
batched_prompts,
truncate=True,
).to(device)
batch_text_embeddings = model.encode_text(batched_text, )
batch_text_embeddings /= batch_text_embeddings.norm(
dim=-1, keepdim=True)
batch_text_embeddings = batch_text_embeddings.cpu().numpy().astype(
'float32')
if USE_CACHE:
np.save(batch_text_embeddings_filename, batch_text_embeddings)
if text_embeddings is None:
text_embeddings = batch_text_embeddings
else:
text_embeddings = np.concatenate(
(text_embeddings, batch_text_embeddings))
print(f"text embeddings shape -- {text_embeddings.shape}")
print("\n")
batched_prompts = []
print(f"{len(text_embeddings)} CLIP embeddings extracted!")
text_embeddings_filename = os.path.join(OUTDIR, f"text_embeddings.npy")
np.save(text_embeddings_filename, text_embeddings)