forked from debidatta/syndata-generation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
clip_features.py
162 lines (113 loc) · 4.69 KB
/
clip_features.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import os
import clip
import torch
from PIL import Image
import numpy as np
import csv
from tqdm import tqdm
def get_article(a):
if (a[0] in ("a", "e", "i", "o", "u")):
return "an " + a
else:
return "a " + a
def get_relation(relation):
if (relation in ("left", "right")):
return " to the " + relation + " of "
else:
return " " + relation + " "
def mirror_relation(relation):
if (relation == "left"):
return "right"
elif (relation == "right"):
return "left"
elif (relation == "above"):
return "below"
else:
return "above"
def create_prompts(a, b, relation, background):
prompt0a = get_article(a)
prompt0b = get_article(b)
prompt1a = get_article(a) + " and " + get_article(b)
prompt1b = get_article(b) + " and " + get_article(a)
prompt2a = get_article(a) + get_relation(relation) + get_article(b)
prompt2b = get_article(b) + get_relation(mirror_relation(relation)) + get_article(a)
prompt3a = get_article(a) + " and " + get_article(b) + " in a " + background
prompt3b = get_article(b) + " and " + get_article(a) + " in a " + background
prompt4a = get_article(a) + get_relation(relation) + get_article(b) + " in a " + background
prompt4b = get_article(b) + get_relation(mirror_relation(relation)) + get_article(a) + " in a " + background
prompts = [prompt0a, prompt0b, prompt1a, prompt1b, prompt2a, prompt2b, prompt3a, prompt3b, prompt4a, prompt4b]
levels = [0, 0, 1, 1, 2, 2, 3, 3, 4, 4]
return prompts, levels
path = 'E:/Source/EffortlessCVSystem/Data/coco_spatial_backgrounds/'
# Load the model
device = "cuda" if torch.cuda.is_available() else "cpu"
clip.available_models()
model, preprocess = clip.load('ViT-L/14', device)
compute_image_feats = False
compute_prompt_feats = True
compute_tsne = False
batchSize = 256
if (compute_image_feats):
rows = []
with open(os.path.join(path, 'pairs.csv'), newline='') as csvfile:
reader = csv.reader(csvfile)
for row in reader:
rows.append(row)
image_features = np.zeros((len(rows), 768))
for i in tqdm(range(0, len(rows), batchSize)):
images = []
num = min(batchSize, len(rows)-i)
for j in range(num):
row = rows[i+j]
img_file = os.path.join(path, row[7])
image = preprocess(Image.open(img_file)).unsqueeze(0).to(device)
images.append(image)
images = torch.cat(images, dim=0)
# Calculate features
with torch.no_grad():
image_feature = model.encode_image(images)
image_features[i:i+num,:] = image_feature.cpu().numpy()
np.save(os.path.join(path, 'image_features_background.npy'), image_features)
if(compute_prompt_feats):
prompts = []
prompts_dict = {}
with open(os.path.join(path, 'pairs.csv'), newline='') as csvfile:
reader = csv.reader(csvfile)
for i, row in enumerate(reader):
a = row[0]
b = row[1]
relation = row[2]
background = row[3].replace("-", " ").replace("_", " ")
p, _ = create_prompts(a, b, relation, background)
prompts += p
prompts = sorted(list(set(prompts)))
text_features = np.zeros((len(prompts), 768))
print("%d prompts" % len(prompts))
for i in tqdm(range(0, len(prompts), batchSize)):
prompts_batch = []
num = min(batchSize, len(prompts)-i)
for j in range(num):
prompt = prompts[i+j]
prompts_batch.append(prompt)
prompts_dict[prompt] = i+j
texts = clip.tokenize(prompts_batch).to(device)
# Calculate features
with torch.no_grad():
text_feature = model.encode_text(texts)
text_features[i:i+num,:] = text_feature.cpu().numpy()
np.save(os.path.join(path, 'text_features_background.npy'), text_features)
np.save(os.path.join(path, 'text_features_background_dict.npy'), prompts_dict)
if (compute_tsne):
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
image_features = np.load(os.path.join(path, 'image_features_background.npy'))
text_features = np.load(os.path.join(path, 'text_features_background.npy'))
features = np.concatenate((image_features, text_features))
print("pca")
pca = PCA(n_components=50)
features_pca = pca.fit_transform(features)
print("tsne")
tsne = TSNE(n_components=2)
features_tsne = tsne.fit_transform(features_pca)
np.save(os.path.join(path, 'image_features_background_tsne.npy'), features_tsne[0:len(image_features),:])
np.save(os.path.join(path, 'text_features_background_tsne.npy'), features_tsne[len(image_features)+1:,:])