-
Notifications
You must be signed in to change notification settings - Fork 6
/
clip_textual_feats.py
51 lines (40 loc) · 1.31 KB
/
clip_textual_feats.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
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import random
from tqdm import tqdm
import numpy as np
import torch
import clip
import basic_utils as utils
from params import parse_args
def main(args):
device = "cuda" if torch.cuda.is_available() else "cpu"
model, _ = clip.load(args.model_type_or_path, jit=False, device=device)
# load annotations
imgs = utils.load_json(args.anno)['images']
random.shuffle(imgs)
for img in tqdm(imgs):
image_id = img['cocoid']
dst_path = os.path.join(args.output_dir, str(image_id)+'.npz')
if os.path.isfile(dst_path):
continue
# iter over the sentences
sents = [sent['raw'].lower().strip().strip('.') for sent in img['sentences']]
sents = clip.tokenize(sents).to(device)
with torch.no_grad():
text_feat = model.encode_text(sents)
text_feat = text_feat.data.cpu().float().numpy()
if args.debug:
print('Text feature shape: ', text_feat.shape)
break
np.savez_compressed(
dst_path,
g_feature=text_feat
)
if __name__ == "__main__":
args = parse_args()
if not args.debug:
utils.mkdirp(args.output_dir)
main(args)