-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathpredict.py
227 lines (180 loc) · 7.78 KB
/
predict.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import os
import sys
import traceback
import pickle
import argparse
import collections
from keras import metrics
import random
import tensorflow as tf
import numpy as np
seed = 1337
random.seed(seed)
np.random.seed(seed)
tf.set_random_seed(seed)
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, wait, as_completed
import multiprocessing
from itertools import product
from multiprocessing import Pool
from timeit import default_timer as timer
from model import create_model
from myutils import prep, drop, statusout, batch_gen, seq2sent, index2word, init_tf
import keras
import keras.backend as K
def gendescr_2inp(model, data, comstok, comlen, batchsize, config, strat='greedy'):
# right now, only greedy search is supported...
tdats, coms = list(zip(*data.values()))
tdats = np.array(tdats)
coms = np.array(coms)
for i in range(1, comlen):
results = model.predict([tdats, coms], batch_size=batchsize)
for c, s in enumerate(results):
coms[c][i] = np.argmax(s)
final_data = {}
for fid, com in zip(data.keys(), coms):
final_data[fid] = seq2sent(com, comstok)
return final_data
def gendescr_3inp(model, data, comstok, comlen, batchsize, config, strat='greedy'):
# right now, only greedy search is supported...
tdats, coms, smls = list(zip(*data.values()))
tdats = np.array(tdats)
coms = np.array(coms)
smls = np.array(smls)
for i in range(1, comlen):
results = model.predict([tdats, coms, smls], batch_size=batchsize)
for c, s in enumerate(results):
coms[c][i] = np.argmax(s)
final_data = {}
for fid, com in zip(data.keys(), coms):
final_data[fid] = seq2sent(com, comstok)
return final_data
def gendescr_4inp(model, data, comstok, comlen, batchsize, config, strat='greedy'):
# right now, only greedy search is supported...
tdats, sdats, coms, smls = zip(*data.values())
tdats = np.array(tdats)
sdats = np.array(sdats)
coms = np.array(coms)
smls = np.array(smls)
#print(sdats)
for i in range(1, comlen):
results = model.predict([tdats, sdats, coms, smls], batch_size=batchsize)
for c, s in enumerate(results):
coms[c][i] = np.argmax(s)
final_data = {}
for fid, com in zip(data.keys(), coms):
final_data[fid] = seq2sent(com, comstok)
return final_data
def load_model_from_weights(modelpath, modeltype, datvocabsize, comvocabsize, smlvocabsize, datlen, comlen, smllen):
config = dict()
config['datvocabsize'] = datvocabsize
config['comvocabsize'] = comvocabsize
config['datlen'] = datlen # length of the data
config['comlen'] = comlen # comlen sent us in workunits
config['smlvocabsize'] = smlvocabsize
config['smllen'] = smllen
model = create_model(modeltype, config)
model.load_weights(modelpath)
return model
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='')
parser.add_argument('modelfile', type=str, default=None)
parser.add_argument('--num-procs', dest='numprocs', type=int, default='4')
parser.add_argument('--gpu', dest='gpu', type=str, default='')
parser.add_argument('--data', dest='dataprep', type=str, default='/scratch/funcom/data/standard')
parser.add_argument('--outdir', dest='outdir', type=str, default='/scratch/funcom/data/outdir')
parser.add_argument('--batch-size', dest='batchsize', type=int, default=200)
parser.add_argument('--num-inputs', dest='numinputs', type=int, default=3)
parser.add_argument('--model-type', dest='modeltype', type=str, default=None)
parser.add_argument('--outfile', dest='outfile', type=str, default=None)
parser.add_argument('--zero-dats', dest='zerodats', action='store_true', default=False)
parser.add_argument('--dtype', dest='dtype', type=str, default='float32')
parser.add_argument('--tf-loglevel', dest='tf_loglevel', type=str, default='3')
args = parser.parse_args()
outdir = args.outdir
dataprep = args.dataprep
modelfile = args.modelfile
numprocs = args.numprocs
gpu = args.gpu
batchsize = args.batchsize
num_inputs = args.numinputs
modeltype = args.modeltype
outfile = args.outfile
zerodats = args.zerodats
if outfile is None:
outfile = modelfile.split('/')[-1]
K.set_floatx(args.dtype)
os.environ['CUDA_VISIBLE_DEVICES'] = gpu
os.environ['TF_CPP_MIN_LOG_LEVEL'] = args.tf_loglevel
sys.path.append(dataprep)
import tokenizer
prep('loading tokenizers... ')
tdatstok = pickle.load(open('%s/dats.tok' % (dataprep), 'rb'), encoding='UTF-8')
comstok = pickle.load(open('%s/coms.tok' % (dataprep), 'rb'), encoding='UTF-8')
smltok = pickle.load(open('%s/smls.tok' % (dataprep), 'rb'), encoding='UTF-8')
drop()
prep('loading sequences... ')
seqdata = pickle.load(open('%s/dataset.pkl' % (dataprep), 'rb'))
drop()
if zerodats:
v = np.zeros(100)
for key, val in seqdata['dttrain'].items():
seqdata['dttrain'][key] = v
for key, val in seqdata['dtval'].items():
seqdata['dtval'][key] = v
for key, val in seqdata['dttest'].items():
seqdata['dttest'][key] = v
allfids = list(seqdata['ctest'].keys())
datvocabsize = tdatstok.vocab_size
comvocabsize = comstok.vocab_size
smlvocabsize = smltok.vocab_size
datlen = len(seqdata['dttest'][list(seqdata['dttest'].keys())[0]])
comlen = len(seqdata['ctest'][list(seqdata['ctest'].keys())[0]])
smllen = len(seqdata['stest'][list(seqdata['stest'].keys())[0]])
prep('loading config... ')
(modeltype, mid, timestart) = modelfile.split('_')
(timestart, ext) = timestart.split('.')
modeltype = modeltype.split('/')[-1]
config = pickle.load(open(outdir+'/histories/'+modeltype+'_conf_'+timestart+'.pkl', 'rb'))
num_inputs = config['num_input']
drop()
prep('loading model... ')
model = keras.models.load_model(modelfile, custom_objects={})
print(model.summary())
drop()
comstart = np.zeros(comlen)
st = comstok.w2i['<s>']
comstart[0] = st
outfn = outdir+"/predictions/predict-{}.txt".format(outfile.split('.')[0])
outf = open(outfn, 'w')
print("writing to file: " + outfn)
batch_sets = [allfids[i:i+batchsize] for i in range(0, len(allfids), batchsize)]
prep("computing predictions...\n")
for c, fid_set in enumerate(batch_sets):
batch = {}
st = timer()
for fid in fid_set:
dat = seqdata['dttest'][fid]
sml = seqdata['stest'][fid]
# adjust to model's expected data size
dat = dat[:config['tdatlen']]
sml = sml[:config['smllen']]
if num_inputs == 2:
batch[fid] = np.asarray([dat, comstart])
elif num_inputs == 3:
batch[fid] = np.asarray([dat, comstart, sml])
else:
print('error: invalid number of inputs specified')
sys.exit()
if num_inputs == 2:
batch_results = gendescr_2inp(model, batch, comstok, comlen, batchsize, config, strat='greedy')
elif num_inputs == 3:
batch_results = gendescr_3inp(model, batch, comstok, comlen, batchsize, config, strat='greedy')
else:
print('error: invalid number of inputs specified')
sys.exit()
for key, val in batch_results.items():
outf.write("{}\t{}\n".format(key, val))
end = timer ()
print("{} processed, {} per second this batch".format((c+1)*batchsize, batchsize/(end-st)))
outf.close()
drop()