From 36a7270d16f218d3a5cc69e9dbe3e5b4b59fe892 Mon Sep 17 00:00:00 2001 From: Dongjie Shi Date: Mon, 11 Nov 2019 10:26:14 +0800 Subject: [PATCH] add load TF saved model as TFNet inference model (#1745) --- .../dllib/inference/InferenceModel.scala | 57 +++++++++++++++++-- .../inference/InferenceModelFactory.scala | 15 +++++ .../bigdl/dllib/inference/ModelLoader.scala | 13 +++++ 3 files changed, 81 insertions(+), 4 deletions(-) diff --git a/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/InferenceModel.scala b/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/InferenceModel.scala index 22127794e32..f14ea1cf868 100644 --- a/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/InferenceModel.scala +++ b/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/InferenceModel.scala @@ -93,18 +93,18 @@ class InferenceModel(private var autoScalingEnabled: Boolean = true, } /** - * loads a TF model as TFNet + * loads a TF frozen model as TFNet * - * @param modelPath the path of the tensorflow model file + * @param modelPath the path of the tensorflow frozen model file */ def doLoadTF(modelPath: String): Unit = { doLoadTensorflowModel(modelPath, 1, 1, true) } /** - * loads a TF model as TFNet + * loads a TF frozen model as TFNet * - * @param modelPath the path of the tensorflow model + * @param modelPath the path of the tensorflow frozen model * @param intraOpParallelismThreads the num of intraOpParallelismThreads * @param interOpParallelismThreads the num of interOpParallelismThreads * @param usePerSessionThreads whether to perSessionThreads @@ -120,6 +120,42 @@ class InferenceModel(private var autoScalingEnabled: Boolean = true, usePerSessionThreads) } + /** + * loads a TF saved model as TFNet + * + * @param modelPath the path of the tensorflow saved model dir + * @param inputs the inputs of the model + * @param outputs the outputs of the model + */ + def doLoadTF(modelPath: String, inputs: Array[String], outputs: Array[String]): Unit = { + doLoadTensorflowSavedModel(modelPath, inputs, outputs, 1, 1, true) + } + + /** + * loads a TF saved model as TFNet + * + * @param modelPath the path of the tensorflow saved model dir + * @param inputs the inputs of the model + * @param outputs the outputs of the model + * @param intraOpParallelismThreads the num of intraOpParallelismThreads + * @param interOpParallelismThreads the num of interOpParallelismThreads + * @param usePerSessionThreads whether to perSessionThreads + */ + def doLoadTF(modelPath: String, + inputs: Array[String], + outputs: Array[String], + intraOpParallelismThreads: Int, + interOpParallelismThreads: Int, + usePerSessionThreads: Boolean): Unit = { + doLoadTensorflowSavedModel( + modelPath, + inputs, + outputs, + intraOpParallelismThreads, + interOpParallelismThreads, + usePerSessionThreads) + } + /** * loads a TF model as OpenVINO * @@ -362,6 +398,19 @@ class InferenceModel(private var autoScalingEnabled: Boolean = true, offerModelQueue() } + private def doLoadTensorflowSavedModel(modelPath: String, + inputs: Array[String], + outputs: Array[String], + intraOpParallelismThreads: Int, + interOpParallelismThreads: Int, + usePerSessionThreads: Boolean): Unit = { + clearModelQueue() + this.originalModel = + InferenceModelFactory.loadFloatModelForTFSavedModel(modelPath, + inputs, outputs, intraOpParallelismThreads, interOpParallelismThreads, usePerSessionThreads) + offerModelQueue() + } + private def doLoadTensorflowModelAsOpenVINO(modelPath: String, modelType: String, pipelineConfigPath: String, diff --git a/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/InferenceModelFactory.scala b/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/InferenceModelFactory.scala index 4d65f6e0658..4d815b223dc 100644 --- a/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/InferenceModelFactory.scala +++ b/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/InferenceModelFactory.scala @@ -52,6 +52,21 @@ object InferenceModelFactory extends InferenceSupportive { new FloatModel(model, metaModel, true) } + def loadFloatModelForTFSavedModel(modelPath: String, + inputs: Array[String], + outputs: Array[String], + intraOpParallelismThreads: Int = 1, + interOpParallelismThreads: Int = 1, + usePerSessionThreads: Boolean = true): FloatModel = { + val sessionConfig = TFNet.SessionConfig(intraOpParallelismThreads, + interOpParallelismThreads, usePerSessionThreads) + val model = ModelLoader.loadFloatModelForTFSavedModel(modelPath, inputs, outputs, sessionConfig) + model.evaluate() + val metaModel = makeMetaModel(model) + new FloatModel(model, metaModel, true) + } + + def loadOpenVINOModelForTF(modelPath: String, modelType: String, pipelineConfigPath: String, diff --git a/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/ModelLoader.scala b/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/ModelLoader.scala index f8415e3c707..2d088fecaa4 100644 --- a/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/ModelLoader.scala +++ b/bigdl/dllib/src/main/scala/com/intel/analytics/bigdl/dllib/inference/ModelLoader.scala @@ -70,5 +70,18 @@ object ModelLoader extends InferenceSupportive { model } } + + def loadFloatModelForTFSavedModel(modelPath: String, + inputs: Array[String], + outputs: Array[String], + config: TFNet.SessionConfig = TFNet.defaultSessionConfig) + : AbstractModule[Activity, Activity, Float] = { + timing("load model") { + logger.info(s"load model from $modelPath") + val model = TFNet.fromSavedModel(modelPath, inputs, outputs) + logger.info(s"loaded model as $model") + model + } + } }