2020from config import DEFAULT_MODEL_PATH , MODELS , MODEL_META_DATA as model_meta
2121from keras .models import load_model
2222import numpy as np
23- from sklearn . externals import joblib
23+ import joblib
2424
2525logging .basicConfig ()
2626logger = logging .getLogger ()
@@ -34,12 +34,19 @@ def load_array(input_data):
3434class SingleModelWrapper (object ):
3535
3636 def __init__ (self , model , path ):
37+ # The code was originally written for TF1 and doesn't work with eager mode.
38+ tf .compat .v1 .disable_eager_execution ()
39+ self .session = tf .compat .v1 .Session ()
40+
3741 self .model_name = model
3842
3943 # load model
4044 model_path = '{}/{}_model' .format (path , model )
4145 logger .info (model_path )
42- self .graph = tf .get_default_graph ()
46+ self .graph = tf .compat .v1 .get_default_graph ()
47+ # See https://github.com/tensorflow/tensorflow/issues/28287#issuecomment-495005162
48+ # We have to do this because we load 3 models in the process.
49+ tf .compat .v1 .keras .backend .set_session (self .session )
4350 self .model = load_model (model_path )
4451
4552 # load scaler
@@ -96,6 +103,7 @@ def _rescale_preds(self, preds):
96103 def predict (self , x ):
97104 reshaped_x = self ._reshape_data (x )
98105 with self .graph .as_default ():
106+ tf .compat .v1 .keras .backend .set_session (self .session )
99107 preds = self .model .predict (reshaped_x )
100108 rescaled_preds = self ._rescale_preds (preds )
101109 return rescaled_preds
0 commit comments