Exemple #1
0
    def __init__(self, model_name, model_base_path):
        """
    Initialize the service.
        
    Args:
      model_name: The name of the model.
      model_base_path: The file path of the model.
    Return:
      None
    """

        super(XgboostInferenceService, self).__init__()

        local_model_base_path = filesystem_util.download_hdfs_moels(
            model_base_path)

        self.model_name = model_name
        self.model_base_path = local_model_base_path
        self.model_version_list = [1]
        self.model_graph_signature = ""
        self.platform = "XGBoost"

        self.preprocess_function, self.postprocess_function = preprocess_util.get_preprocess_postprocess_function_from_model_path(
            self.model_base_path)

        # TODO: Import as needed and only once
        import xgboost as xgb
        from sklearn.externals import joblib

        self.bst = xgb.Booster()

        # Load model
        if self.model_base_path.endswith(".joblib"):
            self.bst = joblib.load(self.model_base_path)
        elif self.model_base_path.endswith(
                ".pkl") or self.model_base_path.endswith(".pickle"):
            with open(self.model_base_path, 'rb') as f:
                self.bst = pickle.load(f)
        elif self.model_base_path.endswith(
                ".bst") or self.model_base_path.endswith(".bin"):
            self.bst.load_model(self.model_base_path)
        else:
            logger.error("Unsupported model file format: {}".format(
                self.model_base_path))

        self.model_graph_signature = "score: {}\nfscore: {}".format(
            self.bst.get_score(), self.bst.get_fscore())
Exemple #2
0
    def __init__(self, model_name, model_base_path):
        """
    Initialize the service.
        
    Args:
      model_name: The name of the model.
      model_base_path: The file path of the model.
    Return:
      None
    """
        super(H2oInferenceService, self).__init__()

        # Start the h2o server
        if os.path.isfile("/tmp/h2o.jar"):
            logging.info("Run to run command 'java -jar /tmp/h2o.jar'")
            subprocess.Popen(["java", "-jar", "/tmp/h2o.jar"])

            logging.info("Sleep 10s to wait for h2o server")
            time.sleep(10)

        local_model_base_path = filesystem_util.download_hdfs_moels(
            model_base_path)

        self.model_name = model_name
        self.model_base_path = local_model_base_path
        self.model_version_list = [1]
        self.model_graph_signature = ""
        self.platform = "H2o"

        self.preprocess_function, self.postprocess_function = preprocess_util.get_preprocess_postprocess_function_from_model_path(
            self.model_base_path)

        import h2o

        logger.info("Try to initialize and connect the h2o server")
        h2o.init()

        logger.info("Try to load the h2o model")
        model = h2o.load_model(self.model_base_path)

        self.model = model
        # TODO: Update the signature with readable string
        self.model_graph_signature = "{}".format(self.model.full_parameters)
    def __init__(self, model_name, model_base_path):
        """
    Initialize the service.
        
    Args:
      model_name: The name of the model.
      model_base_path: The file path of the model.
    Return:
      None
    """

        super(ScikitlearnInferenceService, self).__init__()

        local_model_base_path = filesystem_util.download_hdfs_moels(
            model_base_path)

        self.model_name = model_name
        self.model_base_path = local_model_base_path
        self.model_version_list = [1]
        self.model_graph_signature = ""
        self.platform = "Scikit-learn"

        # TODO: Download function files from HDFS if needed
        self.preprocess_function, self.postprocess_function = preprocess_util.get_preprocess_postprocess_function_from_model_path(
            self.model_base_path)

        # TODO: Import as needed and only once
        from sklearn.externals import joblib

        # Load model
        if self.model_base_path.endswith(".joblib"):
            self.pipeline = joblib.load(self.model_base_path)
        elif self.model_base_path.endswith(
                ".pkl") or self.model_base_path.endswith(".pickle"):
            with open(self.model_base_path, 'r') as f:
                self.pipeline = pickle.load(f)
        else:
            logger.error("Unsupported model file format: {}".format(
                self.model_base_path))

        self.model_graph_signature = str(self.pipeline.get_params())
    def __init__(self, model_name, model_base_path):
        """
    Initialize the service.
        
    Args:
      model_name: The name of the model.
      model_base_path: The file path of the model.
    Return:
      None
    """

        super(SparkInferenceService, self).__init__()

        # TODO: Download the model files
        #local_model_base_path = filesystem_util.download_hdfs_moels(
        #    model_base_path)

        self.model_name = model_name
        self.model_base_path = model_base_path
        self.model_version_list = [1]
        self.model_graph_signature = ""
        self.platform = "Spark"

        self.preprocess_function, self.postprocess_function = preprocess_util.get_preprocess_postprocess_function_from_model_path(
            self.model_base_path)

        # Load model
        from pyspark.sql import SparkSession
        from pyspark.ml.classification import LogisticRegressionModel

        self.spark_session = SparkSession.builder.appName(
            "libsvm_lr").getOrCreate()
        # TODO: Support other model
        self.spark_model = LogisticRegressionModel.load(self.model_base_path)

        # TODO: Add signature for Spark model
        self.model_graph_signature = "No signature for Spark MLlib models"
Exemple #5
0
    def __init__(self, model_name, model_base_path):
        """
    Initialize the service.
        
    Args:
      model_name: The name of the model.
      model_base_path: The file path of the model.
    Return:
      None
    """

        super(PmmlInferenceService, self).__init__()

        # Start the pmml server
        if os.path.isfile(
                "/tmp/openscoring-server-executable-1.4-SNAPSHOT.jar"):
            logging.info(
                "Run to run command 'java -jar /tmp/openscoring-server-executable-1.4-SNAPSHOT.jar'"
            )
            subprocess.Popen([
                "java", "-jar",
                "/tmp/openscoring-server-executable-1.4-SNAPSHOT.jar"
            ])

            logging.info("Sleep 10s to wait for pmml server")
            time.sleep(10)

        local_model_base_path = filesystem_util.download_hdfs_moels(
            model_base_path)

        self.model_name = model_name
        self.model_base_path = local_model_base_path
        self.model_version_list = [1]
        self.model_graph_signature = ""
        self.platform = "PMML"

        self.preprocess_function, self.postprocess_function = preprocess_util.get_preprocess_postprocess_function_from_model_path(
            self.model_base_path)

        # Load model
        """
    from openscoring import Openscoring
    # TODO: Support to config the port of Openscoring server
    openscoring_server_endpoint = "localhost:8080"
    kwargs = {"auth": ("admin", "adminadmin")}
    self.openscoring = Openscoring(
        "http://{}/openscoring".format(openscoring_server_endpoint))
    self.openscoring.deployFile(self.model_name, self.model_base_path,
                                **kwargs)
    """

        endpoint = "http://localhost:8080/openscoring/model/{}".format(
            self.model_name)

        with open(self.model_base_path, "rb") as f:
            kwargs = {
                'headers': {
                    'content-type': 'application/xml'
                },
                'json': None,
                'data': f,
                'auth': ('admin', 'adminadmin')
            }
            result = requests.put(endpoint, **kwargs)
            logging.info("Deploy the model to Openscoring: {}".format(
                result.text))

        self.model_graph_signature = "No signature for PMML models"
Exemple #6
0
  def __init__(self, model_name, model_base_path):
    """
    Initialize the service.
        
    Args:
      model_name: The name of the model.
      model_base_path: The file path of the model.
    Return:
      None
    """

    super(MxnetInferenceService, self).__init__()

    local_model_base_path = filesystem_util.down_mxnet_model_from_hdfs(
        model_base_path)

    self.model_name = model_name
    self.model_base_path = local_model_base_path
    self.model_version_list = [1]
    self.model_graph_signature = ""
    self.platform = "MXNet"

    self.preprocess_function, self.postprocess_function = preprocess_util.get_preprocess_postprocess_function_from_model_path(
        self.model_base_path)

    # TODO: Import as needed and only once
    import mxnet as mx

    # TODO: Select the available version
    epoch_number = 1

    # Load model
    sym, arg_params, aux_params = mx.model.load_checkpoint(
        self.model_base_path, epoch_number)
    self.mod = mx.mod.Module(symbol=sym, context=mx.cpu(), label_names=None)
    self.has_signature_file = False
    self.signature_input_names = []
    self.signature_output_names = []

    # Load inputs from signature file
    signature_file_path = self.model_base_path + "-signature.json"
    if os.path.exists(signature_file_path) and os.path.isfile(
        signature_file_path):
      self.has_signature_file = True
      data_shapes = []

      with open(signature_file_path) as signature_file:
        signature_dict = json.load(signature_file)
        inputs = signature_dict["inputs"]
        outputs = signature_dict["outputs"]

        for input in inputs:
          input_data_name = input["data_name"]
          input_data_shape = input["data_shape"]

          self.signature_input_names.append(input_data_name)
          data_shapes.append((input_data_name, tuple(input_data_shape)))

        for output in outputs:
          output_data_name = output["data_name"]
          ouput_data_shape = output["data_shape"]

          self.signature_output_names.append(output_data_name)

    else:
      data_shapes = [('data', (1, 2))]

    self.mod.bind(for_training=False, data_shapes=data_shapes)
    self.mod.set_params(arg_params, aux_params, allow_missing=True)
    if self.has_signature_file:
      self.model_graph_signature = "Inputs: {}\nOutputs: {}\n{}".format(
          self.signature_input_names, self.signature_output_names,
          self.mod.symbol.tojson())
    else:
      self.model_graph_signature = "{}".format(self.mod.symbol.tojson())