Ejemplo n.º 1
0
    def process(self, execution_context: ExecutionContext,
                input_list: List) -> List:
        model_meta: af.ModelMeta = execution_context.config.get('model_info')
        model_name = model_meta.name
        validated_model = af.get_latest_validated_model_version(model_name)
        # Deprecate deployed model
        deployed_model_version = af.get_deployed_model_version(model_name)
        if deployed_model_version is not None:
            af.update_model_version(
                model_name=model_name,
                model_version=deployed_model_version.version,
                current_stage=ModelVersionStage.DEPRECATED)
        af.update_model_version(model_name=model_name,
                                model_version=validated_model.version,
                                current_stage=ModelVersionStage.DEPLOYED)

        af.get_ai_flow_client().send_event(
            BaseEvent(key='START_PREDICTION', value=validated_model.version))
        print(validated_model.version)

        # Copy deployed model to deploy_model_dir

        deployed_model_dir = af.get_artifact_by_name(self.artifact).uri
        if not os.path.exists(deployed_model_dir):
            os.makedirs(deployed_model_dir)
        for file in os.listdir(deployed_model_dir):
            file_path = os.path.join(deployed_model_dir, file)
            if os.path.isfile(file_path):
                os.remove(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path, True)
        deployed_model_version = af.get_deployed_model_version(model_name)
        shutil.copy(deployed_model_version.model_path, deployed_model_dir)
        return []
    def execute(self, function_context: FunctionContext,
                input_list: List) -> List:
        model_name = function_context.node_spec.model.name
        validated_model = af.get_latest_validated_model_version(model_name)
        # Deprecate deployed model
        deployed_model_version = af.get_deployed_model_version(model_name)
        if deployed_model_version is not None:
            af.update_model_version(
                model_name=model_name,
                model_version=deployed_model_version.version,
                current_stage=ModelVersionStage.DEPRECATED)
        af.update_model_version(model_name=model_name,
                                model_version=validated_model.version,
                                current_stage=ModelVersionStage.DEPLOYED)
        deployed_model_version = af.get_deployed_model_version(
            model_name=model_name)

        # Copy deployed model to deploy_model_dir
        deployed_model_dir = af.get_artifact_by_name(self.artifact).stream_uri
        if not os.path.exists(deployed_model_dir):
            os.makedirs(deployed_model_dir)
        for file in os.listdir(deployed_model_dir):
            file_path = os.path.join(deployed_model_dir, file)
            if os.path.isfile(file_path):
                os.remove(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path, True)
        shutil.copy(deployed_model_version.model_path, deployed_model_dir)
        return []
 def process(self, execution_context: ExecutionContext,
             input_list: List) -> List:
     model_name = execution_context.config.get('model_info').name
     while af.get_deployed_model_version(model_name) is None:
         time.sleep(2)
     model_meta = af.get_deployed_model_version(model_name)
     clf = load(model_meta.model_path)
     return [clf.predict(input_list[0][0])]
Ejemplo n.º 4
0
 def execute(self, function_context: FunctionContext,
             input_list: List) -> List:
     model_name = function_context.node_spec.model.name
     while af.get_deployed_model_version(model_name) is None:
         time.sleep(2)
     model_meta = af.get_deployed_model_version(model_name)
     clf = load(model_meta.model_path)
     return [clf.predict(input_list[0][0])]
Ejemplo n.º 5
0
    def process(self,
                execution_context: flink.ExecutionContext,
                input_list: List[Table] = None) -> List[Table]:
        """
        Use pyflink udf to do prediction
        """
        model_meta = af.get_deployed_model_version(self.model_name)
        model_path = model_meta.model_path
        clf = load(model_path)

        # Define the python udf

        class Predict(ScalarFunction):
            def eval(self, sl, sw, pl, pw):
                records = [[sl, sw, pl, pw]]
                df = pd.DataFrame.from_records(
                    records, columns=['sl', 'sw', 'pl', 'pw'])
                return clf.predict(df)[0]

        # Register the udf in flink table env, so we can call it later in SQL statement
        execution_context.table_env.register_function(
            'mypred',
            udf(f=Predict(),
                input_types=[
                    DataTypes.FLOAT(),
                    DataTypes.FLOAT(),
                    DataTypes.FLOAT(),
                    DataTypes.FLOAT()
                ],
                result_type=DataTypes.FLOAT()))
        return [input_list[0].select("mypred(sl,sw,pl,pw)")]
    def execute(self, function_context: FunctionContext, input_list: List) -> List:
        deployed_model_version = af.get_deployed_model_version(model_name=self.model_name)

        if deployed_model_version is None:
            af.update_model_version(model_name=self.model_name,
                                    model_version=self.model_version,
                                    current_stage=ModelVersionStage.VALIDATED)

        else:
            x_validate, y_validate = input_list[0][0], input_list[0][1]
            clf = load(self.model_path)
            scores = cross_val_score(clf, x_validate, y_validate, scoring='precision_macro', cv=5)
            deployed_clf = load(deployed_model_version.model_path)
            deployed_scores = cross_val_score(deployed_clf, x_validate, y_validate, scoring='precision_macro')

            batch_uri = af.get_artifact_by_name(self.artifact).batch_uri
            if np.mean(scores) > np.mean(deployed_scores):
                af.update_model_version(model_name=self.model_name,
                                        model_version=self.model_version,
                                        current_stage=ModelVersionStage.VALIDATED)
                with open(batch_uri, 'a') as f:
                    f.write('deployed model version[{}] scores: {}\n'.format(deployed_model_version.version,
                                                                             deployed_scores))
                    f.write('generated model version[{}] scores: {}\n'.format(self.model_version, scores))
        return []
Ejemplo n.º 7
0
 def process(self, execution_context: ExecutionContext,
             input_list: List) -> List:
     model_meta: af.ModelMeta = execution_context.config.get('model_info')
     model_name = model_meta.name
     model_version_meta = af.get_deployed_model_version(model_name)
     clf = load(model_version_meta.model_path)
     return [clf.predict(input_list[0][0])]
Ejemplo n.º 8
0
    def process(self, execution_context: ExecutionContext,
                input_list: List) -> List:
        model_meta: af.ModelMeta = execution_context.config.get('model_info')
        model_name = model_meta.name
        validated_model = af.get_latest_validated_model_version(model_name)

        cur_deployed_model = af.get_deployed_model_version(
            model_name=model_name)
        if cur_deployed_model is not None:
            af.update_model_version(model_name=model_name,
                                    model_version=cur_deployed_model.version,
                                    current_stage=ModelVersionStage.DEPRECATED)
        af.update_model_version(model_name=model_name,
                                model_version=validated_model.version,
                                current_stage=ModelVersionStage.DEPLOYED)

        # Copy deployed model to deploy_model_dir
        deployed_model_dir = af.get_artifact_by_name(self.artifact).uri
        if not os.path.exists(deployed_model_dir):
            os.makedirs(deployed_model_dir)
        for file in os.listdir(deployed_model_dir):
            file_path = os.path.join(deployed_model_dir, file)
            if os.path.isfile(file_path):
                os.remove(file_path)
            elif os.path.isdir(file_path):
                shutil.rmtree(file_path, True)
        shutil.copy(validated_model.model_path, deployed_model_dir)
        return []
Ejemplo n.º 9
0
    def execute(self, function_context: FunctionContext, input_list: List) -> List:
        model_meta: ModelMeta = function_context.node_spec.model
        serving_model_version = af.get_deployed_model_version(model_name=model_meta.name)
        if serving_model_version is None:
            af.update_model_version(model_name=model_meta.name,
                                    model_version=self.model_version,
                                    current_stage=ModelVersionStage.DEPLOYED)

        else:
            x_validate, y_validate = input_list[0][0], input_list[0][1]
            scoring = ['precision_macro', 'recall_macro']
            serving_clf = load(serving_model_version.model_path)
            serving_scores = cross_validate(serving_clf, x_validate, y_validate, scoring=scoring)
            clf = load(self.model_path)
            scores = cross_validate(clf, x_validate, y_validate, scoring=scoring)
            batch_uri = af.get_artifact_by_name('validate_artifact').batch_uri
            with open(batch_uri, 'a') as f:
                f.write('serving model version[{}] scores: {}\n'.format(serving_model_version.version, serving_scores))
                f.write('generated model version[{}] scores: {}\n'.format(self.model_version, scores))
            if scores.mean() > serving_scores.mean():
                af.update_model_version(model_name=model_meta.name,
                                        model_version=serving_model_version.version,
                                        current_stage=ModelVersionStage.VALIDATED)
                af.update_model_version(model_name=model_meta.name,
                                        model_version=self.model_version,
                                        current_stage=ModelVersionStage.DEPLOYED)
        return []
    def execute(self, function_context: FunctionContext,
                input_list: List) -> List:
        while af.get_deployed_model_version(self.model_name) is None:
            time.sleep(5)

        def predict(df):
            x_test = df
            model_meta = af.get_deployed_model_version(self.model_name)
            model_path = model_meta.model_path
            clf = load(model_path)
            return model_meta.version, clf.predict(x_test)

        return [input_list[0].map(predict)]
Ejemplo n.º 11
0
    def process(self, execution_context: ExecutionContext,
                input_list: List) -> List:
        """
        Validate and deploy model if necessary
        """
        current_model_meta: af.ModelMeta = execution_context.config.get(
            'model_info')
        deployed_model_version = af.get_deployed_model_version(
            model_name=current_model_meta.name)
        new_model_meta = af.get_latest_generated_model_version(
            current_model_meta.name)
        uri = af.get_artifact_by_name(self.artifact).uri
        if deployed_model_version is None:
            # If there is no deployed model for now, update the current generated model to be deployed.
            af.update_model_version(model_name=current_model_meta.name,
                                    model_version=new_model_meta.version,
                                    current_stage=ModelVersionStage.VALIDATED)
            af.update_model_version(model_name=current_model_meta.name,
                                    model_version=new_model_meta.version,
                                    current_stage=ModelVersionStage.DEPLOYED)
        else:
            x_validate = input_list[0][0]
            y_validate = input_list[0][1]
            knn = load(new_model_meta.model_path)
            scores = knn.score(x_validate, y_validate)
            deployed_knn = load(deployed_model_version.model_path)
            deployed_scores = deployed_knn.score(x_validate, y_validate)

            with open(uri, 'a') as f:
                f.write('deployed model version: {} scores: {}\n'.format(
                    deployed_model_version.version, deployed_scores))
                f.write('generated model version: {} scores: {}\n'.format(
                    new_model_meta.version, scores))
            if scores >= deployed_scores:
                # Deprecate current model and deploy better new model
                af.update_model_version(
                    model_name=current_model_meta.name,
                    model_version=deployed_model_version.version,
                    current_stage=ModelVersionStage.DEPRECATED)
                af.update_model_version(
                    model_name=current_model_meta.name,
                    model_version=new_model_meta.version,
                    current_stage=ModelVersionStage.VALIDATED)
                af.update_model_version(
                    model_name=current_model_meta.name,
                    model_version=new_model_meta.version,
                    current_stage=ModelVersionStage.DEPLOYED)
        return []
Ejemplo n.º 12
0
 def execute(self, function_context: FunctionContext, input_list: List) -> List:
     node_spec = function_context.node_spec
     serving_model_path = af.get_artifact_by_name('push_model_artifact').batch_uri
     if not os.path.exists(serving_model_path):
         os.makedirs(serving_model_path)
     serving_model_version = None
     if serving_model_version is None:
         serving_model_version = af.get_deployed_model_version(model_name=node_spec.model.name)
     for file in os.listdir(serving_model_path):
         file_path = os.path.join(serving_model_path, file)
         if os.path.isfile(file_path):
             os.remove(file_path)
         elif os.path.isdir(file_path):
             shutil.rmtree(file_path, True)
     shutil.copy(serving_model_version.model_path, serving_model_path)
     return []
    def execute(self, function_context: FunctionContext,
                input_list: List) -> List:
        deployed_model_version = af.get_deployed_model_version(
            model_name=self.model_name)
        x_validate, y_validate = input_list[0][0], input_list[0][1]
        clf = load(self.model_path)
        scores = cross_val_score(clf,
                                 x_validate,
                                 y_validate,
                                 scoring='precision_macro')
        stream_uri = af.get_artifact_by_name(self.artifact_name).stream_uri
        if deployed_model_version is None:
            with open(stream_uri, 'a') as f:
                f.write('generated model version[{}] scores: {}\n'.format(
                    self.model_version, np.mean(scores)))
            af.update_model_version(model_name=self.model_name,
                                    model_version=self.model_version,
                                    current_stage=ModelVersionStage.VALIDATED)
        else:
            deployed_clf = load(deployed_model_version.model_path)
            deployed_scores = cross_val_score(deployed_clf,
                                              x_validate,
                                              y_validate,
                                              scoring='precision_macro')
            f = open(stream_uri, 'a')
            f.write('current model version[{}] scores: {}\n'.format(
                deployed_model_version.version, np.mean(deployed_scores)))
            f.write('new generated model version[{}] scores: {}\n'.format(
                self.model_version, np.mean(scores)))
            if np.mean(scores) > np.mean(deployed_scores):
                # Make latest generated model to be validated
                af.update_model_version(
                    model_name=self.model_name,
                    model_version=self.model_version,
                    current_stage=ModelVersionStage.VALIDATED)
                f.write('new generated model version[{}] pass validation.\n'.
                        format(self.model_version))
            else:
                f.write('new generated model version[{}] fail validation.\n'.
                        format(self.model_version))
            f.close()

        return []
 def execute(self, function_context: FunctionContext, input_list: List) -> List:
     save_path = self.path
     new_model_version = self.model_version
     model_meta: ModelMeta = function_context.node_spec.model
     serving_model_version = af.get_deployed_model_version(model_name=model_meta.name)
     if serving_model_version is None:
         af.update_model_version(model_name=model_meta.name, model_version=new_model_version,
                                 current_stage=ModelVersionStage.VALIDATED)
         print('the first serving model version is ', new_model_version)
     else:
         x_test, y_test = input_list[0][0], input_list[0][1]
         model = tf.keras.models.load_model(save_path)
         result = model.evaluate(x_test, y_test, verbose=2)
         base_model = tf.keras.models.load_model(serving_model_version.model_path)
         result_base = base_model.evaluate(x_test, y_test, verbose=2)
         model_validate_result = af.register_artifact(name='model_validate',
                                                      batch_uri=get_file_dir(__file__) + '/model_batch_validate')
         if function_context.job_context.execution_mode == ExecutionMode.BATCH:
             file_uri = model_validate_result.batch_uri
         else:
             file_uri = model_validate_result.stream_uri
         with open(file_uri, 'a') as f:
             f.write(str(result_base) + ' -------> ' + 'previous model version: ' + serving_model_version.version)
             f.write('\n')
             f.write(str(result) + ' -------> ' + 'base model version: ' + new_model_version)
             f.write('\n')
         if result[1] > result_base[1]:
             af.update_model_version(model_name=model_meta.name,
                                     model_version=serving_model_version.version,
                                     current_stage=ModelVersionStage.DEPRECATED)
             af.update_model_version(model_name=model_meta.name, model_version=new_model_version,
                                     current_stage=ModelVersionStage.VALIDATED)
             print('the serving model version is ', new_model_version)
         else:
             print('the serving model version is ', serving_model_version.version)
     return []
 def predict(df):
     x_test = df
     model_meta = af.get_deployed_model_version(self.model_name)
     model_path = model_meta.model_path
     clf = load(model_path)
     return model_meta.version, clf.predict(x_test)
 def setup(self, function_context: FunctionContext):
     self.model_name = function_context.node_spec.model.name
     while af.get_deployed_model_version(self.model_name) is None:
         time.sleep(5)
     print("### {} setup done for {}".format(self.__class__.__name__, function_context.node_spec.model.name))