forked from Dong-JinKim/DRCaptioning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaption_deviation.py
91 lines (61 loc) · 2.17 KB
/
caption_deviation.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
# coding=utf8
import argparse, os, json, string
from math import floor
import h5py
import numpy as np
from scipy.misc import imread, imresize
import pdb
import numpy as np
import sys
sys.path.insert(0,'order-embedding')
import tools
model = tools.load_model('order-embedding/snapshots/order')
def encode_caption(tokens, token_to_idx, max_token_length):
encoded = np.zeros(max_token_length, dtype=np.int32)
for i, token in enumerate(tokens):
if token in token_to_idx:
encoded[i] = token_to_idx[token]
else:
encoded[i] = token_to_idx['<UNK>']
return encoded
def main(args):
# read in the data
with open(args.result_path, 'r') as f:
data = json.load(f)
with open(args.data_json, 'r') as f:
vocab = json.load(f)
options = data['opt']
captions = data['captions']
vocab_size = options['vocab_size']
token_to_idx = vocab['token_to_idx']
idx_to_token = vocab['idx_to_token']
deviation = [0]*len(captions)
all_vecs = np.zeros((0,1024),'float32')
for iid, img in enumerate(captions):
print 'collecting captions (%d/%d)'%((iid),len(captions))
num_of_box = int((np.sqrt(4*len(img)+1)+1)/2)
per_img_hist = np.array([0]*vocab_size)#hist among imgs
per_box_hist = np.array( [([0]*vocab_size)]*num_of_box )
words_per_box = [0]*num_of_box
#pdb.set_trace()
vectors = tools.encode_sentences(model,img,verbose=False)#sentence embedding
#pdb.set_trace()
all_vecs = np.concatenate((all_vecs, vectors),axis=0)
deviation[iid] = np.std(np.array(vectors))
#pdb.set_trace()
#captions
mean_deviation = np.mean(deviation)
print 'mean stanard deviation=%.3f'%(mean_deviation)
print 'total stanard deviation=%.3f'%np.std(np.array(all_vecs))
pdb.set_trace()
return mean_deviation
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--result_path',
default='relcap_statistics_union75.json',
help='The JSON file to with caption resutls.')
parser.add_argument('--data_json',
default='data/VG-regions-dicts_R.json',
help='The JSON file to load data from; optional.')
args = parser.parse_args()
main(args)