forked from rmokady/CLIP_prefix_caption
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_coco.py
54 lines (49 loc) · 2.19 KB
/
parse_coco.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
import torch
import skimage.io as io
import clip
from PIL import Image
import pickle
import json
import os
from tqdm import tqdm
import argparse
def main(clip_model_type: str, split: str):
device = torch.device('cuda:0')
clip_model_name = clip_model_type.replace('/', '_')
out_path = f"./data/coco/oscar_split_{clip_model_name}_{split}.pkl"
clip_model, preprocess = clip.load(clip_model_type, device=device, jit=False)
# Note: there are multiple captions per image.
# The smart thing to do would be to sort by image_id, load it once, compute the embeddings once.
#with open(f'./data/coco/annotations/{split}_caption.json', 'r') as f:
with open(f'./data/coco/karpathy_split_annotations/{split}_caption.json', 'r') as f:
data = json.load(f)
print("%0d captions loaded from json " % len(data))
all_embeddings = []
all_captions = []
for i in tqdm(range(len(data))):
d = data[i]
img_id = d["image_id"]
filename = f"./data/coco/train2014/COCO_train2014_{int(img_id):012d}.jpg"
if not os.path.isfile(filename):
filename = f"./data/coco/val2014/COCO_val2014_{int(img_id):012d}.jpg"
image = io.imread(filename)
image = preprocess(Image.fromarray(image)).unsqueeze(0).to(device)
with torch.no_grad():
prefix = clip_model.encode_image(image).cpu()
d["clip_embedding"] = i
all_embeddings.append(prefix)
all_captions.append(d)
if (i + 1) % 10000 == 0:
with open(out_path, 'wb') as f:
pickle.dump({"clip_embedding": torch.cat(all_embeddings, dim=0), "captions": all_captions}, f)
with open(out_path, 'wb') as f:
pickle.dump({"clip_embedding": torch.cat(all_embeddings, dim=0), "captions": all_captions}, f)
print('Done')
print("%0d embeddings saved " % len(all_embeddings))
return 0
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--clip_model_type', default="ViT-B/32", choices=('RN50', 'RN101', 'RN50x4', 'ViT-B/32'))
parser.add_argument('--split', default="train", choices=('train', 'test', 'val'))
args = parser.parse_args()
exit(main(args.clip_model_type, args.split))