[c0f169]: / scripts / predict.py

Download this file

41 lines (32 with data), 1.5 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 sys
sys.path.append('../')
from config import models, index_to_label, acronyms_to_entities, MAX_LENGTH
import tensorflow as tf
from scripts.utils import predict
from keras import backend as K
def precision(y_true, y_pred):
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
_precision = true_positives / (predicted_positives + K.epsilon())
return _precision
def recall(y_true, y_pred):
"""Compute recall metric"""
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
return true_positives / (possible_positives + K.epsilon())
def f1_score(y_true, y_pred):
"""Compute f1-score metric"""
_precision = precision(y_true, y_pred)
_recall = recall(y_true, y_pred)
f1_score = 2 * ((_precision * _recall) / (_precision + _recall + K.epsilon()))
return f1_score
def NER(model_name, text):
# Print the arguments
print("Model provided: ", models[model_name]['title'])
model_path = models[model_name]['path']
# Register the custom metric function
tf.keras.utils.get_custom_objects()[precision.__name__] = precision
tf.keras.utils.get_custom_objects()[recall.__name__] = recall
tf.keras.utils.get_custom_objects()[f1_score.__name__] = f1_score
model = tf.keras.models.load_model(model_path)
predict(text, model, index_to_label, acronyms_to_entities, MAX_LENGTH)