-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathembedsave_8k.py
More file actions
46 lines (40 loc) · 1.73 KB
/
Copy pathembedsave_8k.py
File metadata and controls
46 lines (40 loc) · 1.73 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
import torch
import clip
import pandas as pd
from PIL import Image
import os
import numpy as np
# Load the CLIP model
device = "cuda" if torch.cuda.is_available() else "cpu"
model, preprocess = clip.load("ViT-B/32", device=device)
# Load the captions dataset
captions_file = "Flickr8k.csv"
captions_df = pd.read_csv(captions_file, delimiter=",", header=0)
#captions_df["image"] = captions_df["image"].str.split(".").str[0]
# Precompute and save text features
text_features_cache = []
for idx, row in captions_df.iterrows():
caption = row['caption']
text = clip.tokenize([caption]).to(device)
with torch.no_grad():
text_features = model.encode_text(text)
text_features = text_features / text_features.norm(dim=-1, keepdim=True)
text_features_cache.append(text_features.cpu().numpy())
text_features_cache = np.vstack(text_features_cache)
np.save("text_features_cache_8k.npy", text_features_cache)
# Precompute and save image features
image_features_cache = []
image_paths = []
for idx, row in captions_df.iterrows():
image_id = row['image']
image_path = os.path.join(r"C:\Users\Hp\Documents\IIIT\Capstone\App\flickr8k\Images", f"{image_id}.jpg")
image = Image.open(image_path).convert("RGB")
image = preprocess(image).unsqueeze(0).to(device)
with torch.no_grad():
image_features = model.encode_image(image)
image_features = image_features / image_features.norm(dim=-1, keepdim=True)
image_features_cache.append(image_features.cpu().numpy())
image_paths.append(image_path)
image_features_cache = np.vstack(image_features_cache)
np.save("image_features_cache_8k.npy", image_features_cache)
np.save("image_paths_8k.npy", image_paths)