-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcaption_sequence_generation.py
More file actions
40 lines (37 loc) · 1.39 KB
/
caption_sequence_generation.py
File metadata and controls
40 lines (37 loc) · 1.39 KB
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
import numpy as np
import argparse
import os
import pickle
from tensorflow.keras.preprocessing.sequence import pad_sequences
def create_sequences_from_caption(seq, max_len):
X, y = [], []
for i in range(1, len(seq)):
in_seq = seq[:i]
out_word = seq[i]
X.append(in_seq)
y.append(out_word)
X = pad_sequences(X, maxlen=max_len, padding="post")
y = np.array(y)
return X, y
def generate_sequences(seq_file, tokenizer_file, max_len, out_in, out_out):
seqs = np.load(seq_file, allow_pickle=True)
with open(tokenizer_file, "rb") as f:
tokenizer = pickle.load(f)
all_X, all_y = [], []
for s in seqs:
X, y = create_sequences_from_caption(s, max_len)
all_X.append(X)
all_y.append(y)
X_all = np.vstack(all_X) if len(all_X)>0 else np.array([])
y_all = np.concatenate(all_y) if len(all_y)>0 else np.array([])
np.save(out_in, X_all)
np.save(out_out, y_all)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--seq-file", required=True)
parser.add_argument("--tokenizer", required=True)
parser.add_argument("--max-len", type=int, required=True)
parser.add_argument("--out-in", required=True)
parser.add_argument("--out-out", required=True)
args = parser.parse_args()
generate_sequences(args.seq_file, args.tokenizer, args.max_len, args.out_in, args.out_out)