-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathutils.py
131 lines (112 loc) · 4.2 KB
/
utils.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
import numpy as np
import cPickle as pickle
import hickle
import time
import os
import h5py
import json
def load_coco_data_dep(data_path='./data', split='train'):
data_path = os.path.join(data_path, split)
start_t = time.time()
data = {}
data['features'] = hickle.load(os.path.join(data_path, '%s.features.hkl' %split))
with open(os.path.join(data_path, '%s.file.names.pkl' %split), 'rb') as f:
data['file_names'] = pickle.load(f)
with open(os.path.join(data_path, '%s.captions.pkl' %split), 'rb') as f:
data['captions'] = pickle.load(f)
with open(os.path.join(data_path, '%s.image.idxs.pkl' %split), 'rb') as f:
data['image_idxs'] = pickle.load(f)
if split == 'train':
with open(os.path.join(data_path, 'word_to_idx.pkl'), 'rb') as f:
data['word_to_idx'] = pickle.load(f)
for k, v in data.iteritems():
if type(v) == np.ndarray:
print k, type(v), v.shape, v.dtype
else:
print k, type(v), len(v)
end_t = time.time()
print "Elapse time: %.2f" %(end_t - start_t)
return data
def load_coco_data(data_path='./data', split='train'):
data_path = os.path.join(data_path, split)
start_t = time.time()
data = {}
data['features'] = h5py.File(os.path.join(data_path, '%s.features.h5' % split))['feature']
f = h5py.File(os.path.join(data_path, '%s.data.h5' % split))
data['file_names'] = f['file_names']
data['img_idxs'] = f['img_idxs']
data['captions'] = f['captions']
# with open(os.path.join(data_path, '%s.file.names.pkl' % split), 'rb') as f:
# data['file_names'] = pickle.load(f)
# with open(os.path.join(data_path, '%s.captions.pkl' % split), 'rb') as f:
# data['captions'] = pickle.load(f)
# with open(os.path.join(data_path, '%s.image.idxs.pkl' % split), 'rb') as f:
# data['image_idxs'] = pickle.load(f)
if split == 'train':
with open(os.path.join(data_path, 'word2ix.json'), 'rb') as f:
data['word_to_idx'] = json.load(f)
for k, v in data.iteritems():
if type(v) == np.ndarray:
print k, type(v), v.shape, v.dtype
else:
print k, type(v), len(v)
end_t = time.time()
print "Elapse time: %.2f" % (end_t - start_t)
return data
def decode_captions(captions, idx_to_word):
if captions.ndim == 1:
T = captions.shape[0]
N = 1
else:
N, T = captions.shape
decoded = []
for i in range(N):
words = []
for t in range(T):
if captions.ndim == 1:
word = idx_to_word[captions[t]]
else:
word = idx_to_word[captions[i, t]]
if word == 'EOS':
# words.append('<END>')
break
if word != 'NULL':
words.append(word)
decoded.append(' '.join(words))
return decoded
def sample_coco_minibatch(data, batch_size):
data_size = data['features'].shape[0]
mask = np.random.choice(data_size, batch_size)
features = []
for i in mask:
features.append(data['features'][i])
features = np.asarray(features)
# features = data['features'][mask]
file_names = []
for i in mask:
file_names.append(data['file_names'][i])
file_names = np.asarray(file_names)
return features, file_names
def write_bleu(scores, path, epoch):
if epoch == 0:
file_mode = 'w'
else:
file_mode = 'a'
with open(os.path.join(path, 'val.bleu.scores.txt'), file_mode) as f:
f.write('Epoch %d\n' %(epoch+1))
f.write('Bleu_1: %f\n' %scores['Bleu_1'])
f.write('Bleu_2: %f\n' %scores['Bleu_2'])
f.write('Bleu_3: %f\n' %scores['Bleu_3'])
f.write('Bleu_4: %f\n' %scores['Bleu_4'])
f.write('METEOR: %f\n' %scores['METEOR'])
f.write('ROUGE_L: %f\n' %scores['ROUGE_L'])
f.write('CIDEr: %f\n\n' %scores['CIDEr'])
def load_pickle(path):
with open(path, 'rb') as f:
file = pickle.load(f)
print ('Loaded %s..' %path)
return file
def save_pickle(data, path):
with open(path, 'wb') as f:
pickle.dump(data, f, pickle.HIGHEST_PROTOCOL)
print ('Saved %s..' %path)