-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gen.py
150 lines (130 loc) · 5.43 KB
/
test_gen.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
import argparse
import os, re
from tqdm import tqdm
import json
from PIL import Image
from transformers import CLIPProcessor, CLIPModel
import numpy as np
import torch.nn.functional as F
def build_coco_file_list(image_path, split_path, test_data):
im_files, txt_files = [], []
co_im, fl_im = image_path
co_file, kp_file = split_path
im2wav_mapping = {}
data = json.load(open(os.path.join(co_file, f'SpokenCOCO_train.json'), 'r'))
for d in data['data']:
im_path = d['image'][:-4] #train2014/~~.jpg or val2014/~~.jpg
spcaptions = d['captions']
caps = []
txts = []
for cap in spcaptions:
txts.append(cap['text'])
cap = cap['wav'].replace('wavs/', '')[:-4]
caps.append(cap)
im2wav_mapping[im_path] = (caps, txts)
data = json.load(open(os.path.join(co_file, f'SpokenCOCO_val.json'), 'r'))
for d in data['data']:
im_path = d['image'][:-4] #train2014/~~.jpg or val2014/~~.jpg
spcaptions = d['captions']
caps = []
txts = []
for cap in spcaptions:
txts.append(cap['text'])
cap = cap['wav'].replace('wavs/', '')[:-4]
caps.append(cap)
im2wav_mapping[im_path] = (caps, txts)
if test_data == 'coco':
data = json.load(open(os.path.join(kp_file, 'dataset_coco.json')))
for d in data['images']:
if d['split'] == 'test':
im_path = os.path.join(co_im, d['filepath'], d['filename'][:-4] + '.jpg')
spcaptions = im2wav_mapping[f"{d['filepath']}/{d['filename'][:-4]}"][:5]
txt_5 = []
im_files.append(im_path)
for (cap, txt) in zip(*spcaptions):
txt_5.append(text_normalization(txt.lower()))
txt_files.append(txt_5)
num_coco = len(im_files)
print(f"num COCO {num_coco}")
if test_data == 'flickr':
data = json.load(open(os.path.join(kp_file, 'dataset_flickr8k.json')))
for d in data['images']:
if d['split'] == 'test':
im_path = os.path.join(fl_im, d['filename'][:-4] + '.jpg')
captions = d['sentences']
im_files.append(im_path)
txt_5 = []
for cap in range(5):
txt_5.append(text_normalization(captions[cap]['raw'].lower()))
txt_files.append(txt_5)
elif test_data =='flickr30k':
data = json.load(open(os.path.join(kp_file, 'dataset_flickr30k.json')))
for d in data['images']:
if d['split'] == 'test':
im_path = os.path.join(fl_im, d['filename'][:-4] + '.jpg')
captions = d['sentences']
im_files.append(im_path)
txt_5 = []
for cap in range(5):
txt_5.append(text_normalization(captions[cap]['raw'].lower()))
txt_files.append(txt_5)
print(f"num Flickr {len(im_files) - num_coco}")
return im_files, txt_files
def main():
parser = get_parser()
args = parser.parse_args()
model = CLIPModel.from_pretrained("openai/clip-vit-large-patch14").cuda()
processor = CLIPProcessor.from_pretrained("openai/clip-vit-large-patch14")
im_lists, txt_lists = build_coco_file_list(
image_path=[args.coco_path, args.flickr_path],
split_path=[args.spcoco_split_path, args.karpathy_split_path],
test_data=args.data
)
cos_scores = []
for im_path in tqdm(im_lists):
f_name = os.path.basename(im_path)
f_name = os.path.join(args.pred, f_name)
gt_image = Image.open(im_path)
pred_image = Image.open(f_name)
gt_inputs = processor(images=gt_image, return_tensors="pt")
pred_inputs = processor(images=pred_image, return_tensors="pt")
gt_inputs = {k: v.cuda() for k, v in gt_inputs.items()}
gt_feat = model.get_image_features(**gt_inputs)
pred_inputs = {k: v.cuda() for k, v in pred_inputs.items()}
pred_feat = model.get_image_features(**pred_inputs)
cos_sim = F.cosine_similarity(gt_feat, pred_feat, dim=1)
cos_sim = cos_sim.squeeze()
cos_scores.append(cos_sim.cpu().item())
print("CLIP_cosine_score : ", np.mean(cos_scores))
with open(args.pred + '_CLIP_gt_score.txt', 'w') as w:
w.write(f'Cosine: {str(np.mean(cos_scores))}')
def get_parser():
parser = argparse.ArgumentParser(
description="Command-line script for scoring."
)
parser.add_argument(
"--pred", type=str, required=True, help="prediction"
)
parser.add_argument(
"--coco_path", type=str, default="path_to/COCO_2014", help="reference"
)
parser.add_argument(
"--flickr_path", type=str, default="path_to/Flickr8k/Images", help="reference"
)
parser.add_argument(
"--spcoco_split_path", type=str, default="path_to/SpokenCOCO", help="reference"
)
parser.add_argument(
"--karpathy_split_path", type=str, default="path_to/Flickr8k/Karpathy_split", help="reference"
)
parser.add_argument(
"--data", type=str, default='coco', help="reference"
)
return parser
def text_normalization(text):
text = re.sub(r"[\(\[].*?[\)\]]", "", text) # remove Coding conventions
text = re.sub(r"[^\w\s']|_", " ", text) # remove punctuation except apostrophe
text = re.sub(r"\s{2,}", " ", text).strip() # remove double space
return text.lower()
if __name__ == "__main__":
main()