forked from csirtgadgets/tf-domains-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
executable file
·51 lines (35 loc) · 1.29 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
#!/usr/bin/env python
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
import os
import json
import optparse
from keras.models import load_model
from keras.preprocessing import sequence
from keras.preprocessing.text import Tokenizer
from pprint import pprint
from constants import MAX_STRING_LEN, MODEL, WEIGHTS, BATCH_SIZE
BATCH_SIZE = int(BATCH_SIZE)
MAX_STRING_LEN = int(MAX_STRING_LEN)
def predict(i):
tokenizer = Tokenizer(filters='\t\n', char_level=True, lower=True)
word_dict_file = os.path.join('build/word-dict.json')
with open(word_dict_file) as F:
txt = F.read()
txt = json.loads(txt)
tokenizer.word_index = txt
seq = tokenizer.texts_to_sequences(i)
i_processed = sequence.pad_sequences(seq, maxlen=MAX_STRING_LEN)
model = load_model(MODEL)
model.load_weights(WEIGHTS)
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
prediction = model.predict(i_processed, batch_size=BATCH_SIZE)
for idx, v in enumerate(i):
print("%s: %f" % (v, prediction[idx]))
if __name__ == '__main__':
parser = optparse.OptionParser()
options, args = parser.parse_args()
if args[0] is not None:
predict(args[0].split(','))