forked from paarthneekhara/text-to-image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate_thought_vectors.py
33 lines (28 loc) · 955 Bytes
/
generate_thought_vectors.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
import os
from os.path import join, isfile
import re
import numpy as np
import pickle
import argparse
import skipthoughts
import h5py
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--caption_file', type=str, default='Data/sample_captions.txt',
help='caption file')
parser.add_argument('--data_dir', type=str, default='Data',
help='Data Directory')
args = parser.parse_args()
with open( args.caption_file ) as f:
captions = f.read().split('\n')
captions = [cap for cap in captions if len(cap) > 0]
print captions
model = skipthoughts.load_model()
caption_vectors = skipthoughts.encode(model, captions)
if os.path.isfile(join(args.data_dir, 'sample_caption_vectors.hdf5')):
os.remove(join(args.data_dir, 'sample_caption_vectors.hdf5'))
h = h5py.File(join(args.data_dir, 'sample_caption_vectors.hdf5'))
h.create_dataset('vectors', data=caption_vectors)
h.close()
if __name__ == '__main__':
main()