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 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 []
Esempio n. 3
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 []
Esempio n. 4
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 []
Esempio n. 5
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:
        x_evaluate, y_evaluate = input_list[0][0], input_list[0][1]
        clf = load(self.model_path)
        scores = cross_val_score(clf, x_evaluate, y_evaluate, cv=5)
        evaluate_artifact = af.get_artifact_by_name(self.artifact).batch_uri
        with open(evaluate_artifact, 'a') as f:
            f.write('model version[{}] scores: {}\n'.format(self.model_version, scores))

        return []
 def execute(self, function_context: FunctionContext, input_list: List) -> List:
     print("### {}".format(self.__class__.__name__))
     x_evaluate, y_evaluate = input_list[0][0], input_list[0][1]
     clf = load(self.model_path)
     scores = cross_val_score(clf, x_evaluate, y_evaluate, cv=5)
     evaluate_artifact = af.get_artifact_by_name('evaluate_artifact2').stream_uri
     with open(evaluate_artifact, 'a') as f:
         f.write('model version[{}] scores: {}\n'.format(self.model_version, scores))
     return []
Esempio n. 8
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 []
Esempio n. 9
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 []
Esempio n. 11
0
 def execute(self, function_context: FunctionContext, input_list: List) -> List:
     model_artifact = af.get_artifact_by_name('push_model_artifact').batch_uri
     clf = load(os.path.join(model_artifact, os.listdir(model_artifact)[0]))
     return [clf.predict(input_list[0][0])]