forked from JIEUN-12/ImageCaptioningwithTransformer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
88 lines (77 loc) · 2.79 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
import numpy as np
import pickle as pickle
import hickle
import time
import os
def load_coco_data(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 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 == '<END>':
words.append('.')
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 = data['features'][mask]
file_names = data['file_names'][mask]
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)