-
Notifications
You must be signed in to change notification settings - Fork 0
/
ds_model.py
84 lines (68 loc) · 2.03 KB
/
ds_model.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
###
### DeepSpam neural model loader
###
### Ez a regi, nem multithreaded verzio.
### Mivel az ujabb tensorflow-ba mar beolvasztottak a keras-t, eloszor azt probalja betolteni, ha nem megy, akkor a kulon modult.
###
import sys
import os
import errno
import traceback
import pickle
import json
# force CPU:
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
# np.random.seed(1080)
import numpy as np
try:
from tensorflow.keras.models import load_model,Model
from tensorflow.keras.backend import clear_session,get_session
except:
from keras.models import load_model,Model
from keras.backend import clear_session,get_session
MAX_SEQUENCE_LENGTH = 100
def deepspam_load(path="model"):
global wordmap
global model
print('Loading Model...')
try:
# new tf2 format:
model=load_model(path)
wordmap=pickle.load(open(path+"/model.wordmap", "rb"))
except:
# old json+h5 format:
config=json.load(open(path+"/model.config","rt"))
model = Model.from_config(config)
model.load_weights(path+"/model.weights")
try:
wordmap=pickle.load(open(path+"/model.wordmap-py3", "rb"))
except:
wordmap=pickle.load(open(path+"/model.wordmap-py2", "rb"))
#wordmap=[]
print("MODEL loaded! (%d words)"%(len(wordmap)))
return wordmap
def deepspam_exit():
pass
def deepspam_test(vtokens,verbose=1):
# print(" ".join(vtokens))
data = np.zeros((1, MAX_SEQUENCE_LENGTH), dtype='int32')
j=0
for w in vtokens:
if w in wordmap:
data[0][j]=wordmap[w]
j+=1
if j>=MAX_SEQUENCE_LENGTH:
break
# print("Predict:")
# classes = model.predict(data, batch_size=1, verbose=verbose)
#model(x, training=False)
classes = model(data, training=False)
# print(classes)
# clear_session()
res=classes[0][0]*100.0/(classes[0][0]+classes[0][1])
if res>=0 and res<=100: return res
print("Predict: %f - %f"%(classes[0][0],classes[0][1]))
return 50.0
# res+=0.1
# print(res)
# return res