Ejemplo n.º 1
0
def register_aml_model(model_name, exp, run_id, build_id: str = 'none'):
    try:
        if (build_id != 'none'):
            model_already_registered(model_name, exp, run_id)
            run = Run(experiment=exp, run_id=run_id)
            tagsValue = {"area": "diabetes", "type": "regression",
                         "BuildId": build_id, "run_id": run_id,
                         "experiment_name": exp.name}
        else:
            run = Run(experiment=exp, run_id=run_id)
            if (run is not None):
                tagsValue = {"area": "diabetes", "type": "regression",
                             "run_id": run_id, "experiment_name": exp.name}
            else:
                print("A model run for experiment", exp.name,
                      "matching properties run_id =", run_id,
                      "was not found. Skipping model registration.")
                sys.exit(0)

        model = run.register_model(model_name=model_name,
                                   model_path="./outputs/" + model_name,
                                   tags=tagsValue)
        os.chdir("..")
        print(
            "Model registered: {} \nModel Description: {} "
            "\nModel Version: {}".format(
                model.name, model.description, model.version
            )
        )
    except Exception:
        traceback.print_exc(limit=None, file=None, chain=True)
        print("Model registration failed")
        raise
Ejemplo n.º 2
0
def evaluate_model():
    all_runs = exp.get_runs(properties={
        "release_id": release_id,
        "run_type": "train"
    },
                            include_children=True)
    # print(f'Search parameters: properties="release_id": {release_id}, "run_type": "train"')
    # print(f'Experiment :{exp}')

    # li_test = list(all_runs)
    # print(f'li_test: {li_test}')

    # all_runs contains the reference to the entire run that satisfied the properties values in the query:
    # The list of runs is returned in decending order, so the first value is the most recent run
    new_model_run = next(all_runs)
    new_model_run_id = new_model_run.id
    print(f'New Run found with Run ID of: {new_model_run_id}')

    new_model_run = Run(exp, run_id=new_model_run_id)
    new_model_acc = new_model_run.get_metrics().get("final-accuracy")

    try:
        # Get most recently registered model, we assume that
        # is the model in production.
        # Download this model and compare it with the recently
        # trained model by running test with same data set.
        model_list = Model.list(ws)
        production_model = next(
            filter(
                lambda x: x.created_time == max(model.created_time
                                                for model in model_list),
                model_list,
            ))
        production_model_run_id = production_model.tags.get("run_id")
        run_list = exp.get_runs()

        # Get the run history for both production model and
        # newly trained model and compare final-accuracy
        production_model_run = Run(exp, run_id=production_model_run_id)

        production_model_acc = production_model_run.get_metrics().get(
            "final-accuracy")

        print(
            "Current Production model accuracy: {}, New trained model accuracy: {}"
            .format(production_model_acc, new_model_acc))

        promote_new_model = False
        if new_model_acc < production_model_acc:
            promote_new_model = True
            print(
                "New trained model performs better, thus it will be registered"
            )
    except Exception:
        promote_new_model = True
        print("This is the first model to be trained, \
            thus nothing to evaluate for now")

    return promote_new_model, new_model_run, new_model_acc
def hyperparameter_tuning(ws,experiment):
    # Create and submit a Hyperdrive job
    cluster = ws.compute_targets[AML.compute_name]
    script_params={
        '--datastore-dir': ws.get_default_datastore().as_mount(),
    }
    tf_estimator = TensorFlow(source_directory='scripts',
                              compute_target=cluster,
                              entry_script='train.py',
                              script_params=script_params,
                              use_gpu=True)
    ps = RandomParameterSampling(
        {
            '--learning-rate': loguniform(-15, -3)
        }
    )
    early_termination_policy = BanditPolicy(slack_factor = 0.15, evaluation_interval=2)
    hyperdrive_run_config = HyperDriveRunConfig(estimator = tf_estimator, 
                                                hyperparameter_sampling = ps, 
                                                policy = early_termination_policy,
                                                primary_metric_name = "validation_accuracy",
                                                primary_metric_goal = PrimaryMetricGoal.MAXIMIZE,
                                                max_total_runs = 20,
                                                max_concurrent_runs = 4)

    hd_run = experiment.submit(hyperdrive_run_config)
    RunDetails(Run(experiment, hd_run.id)).show()
    return hd_run
Ejemplo n.º 4
0
def downloadPickles(ws, modelName, outputPath="./pickles", modelVer=None):
    if modelVer == 'best':
        bestModel = None
        maxAcc = -1
        for model in Model.list(ws, modelName, ["accuracy"]):
            modelAcc = float(model.tags["accuracy"])
            if modelAcc > maxAcc:
                bestModel = model
                maxAcc = modelAcc

        print(f"### Best model with highest accuracy of {maxAcc} found")

        if not bestModel:
            model = Model(ws, modelName)
            print("### WARNING! No best model found, using latest instead")
    elif modelVer is not None:
        model = Model(ws, modelName, version=modelVer)
    else:
        model = Model(ws, modelName)

    print(f"### Using model version {model.version}")
    # Echo'ing out this magic string sets an output variable in Azure DevOps pipeline
    # Set AZML_MODEL_VER for use by subsequent steps
    print(f"##vso[task.setvariable variable=AZML_MODEL_VER]{model.version}")

    # These are special tags, lets us get back to the run that created the model
    try:
        runId = model.tags['aml-runid']
        experimentName = model.tags['aml-experiment']
    except:
        print(
            "### ERROR! Model missing `aml-runid` and `aml-experiment` tags, Can't continue!"
        )
        exit()

    exp = Experiment(workspace=ws, name=experimentName)
    run = Run(exp, runId)
    if run.status != "Completed":
        print(f'### ERROR! Run {runId} did not complete!')
        return

    print(f'### Will download from run {runId} in {experimentName}')

    # Now we can get all the files created with the run, grab all the .pkls
    for f in run.get_file_names():
        if f.endswith('.pkl'):
            output_file_path = os.path.join(outputPath, f.split('/')[-1])
            print('### Downloading from {} to {} ...'.format(
                f, output_file_path))
            run.download_file(name=f, output_file_path=output_file_path)

    # Add some extra metadata, handy to have
    metadata = {
        'name': model.name,
        'version': model.version,
        'tags': model.tags
    }
    with open(f"{outputPath}/metadata.json", 'w') as metadata_file:
        print(f"### Storing metadata in {outputPath}/metadata.json")
        json.dump(metadata, metadata_file)
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--run_id',
        type=str,
        dest='run_id',
        help='AML model training run id')
    parser.add_argument(
        '--train_data_folder',
        type=str,
        default='train',
        dest='train_data_folder',
        help='folder on datastore containing training data')

    args = parser.parse_args()
    ws, ds, _, compute_target = amlutils.setup_azureml()
    experiment = Experiment(ws, consts.experiment_name)
    run = Run(experiment, run_id=args.run_id)
    model = amlutils.register_model(
        run, ds, args.train_data_folder, consts.model_name)
    inference_config = create_inference_config(ws)
    uri, token = amlutils.deploy_service(
        ws, model, inference_config,
        consts.service_name, compute_target)
    print('##vso[task.setvariable variable=SVC_URI;]{}'.format(uri))
    print('##vso[task.setvariable variable=SVC_TOKEN;]{}'.format(token))
Ejemplo n.º 6
0
def startup():
    import azureml.core
    from azureml.core import Run, Workspace, Experiment
    from azureml.pipeline.core import PublishedPipeline
    from azureml.core import Datastore, Dataset
    import pandas as pd
    print("SDK version:", azureml.core.VERSION)
    pd.set_option('display.max_colwidth', 120)
    from azureml.core import Datastore, Dataset

    workspace = Workspace.from_config()

    ds = workspace.get_default_datastore()

    #target_column_name = 'volume'
    #time_column_name = 'date'
    #time_series_id_column_names = 'team_tag'

    experiment_name = 'azure-stackoverflow-classifier'
    experiment = Experiment(workspace, name=experiment_name)
    train = pd.read_csv('./data/train.csv',
                        names=['ID', 'IssueTitle', 'Label'])

    try:

        run = Run(experiment,
                  'azure-stackoverflow-classifier_1592684426_3767f390')
        hd_run = Run(experiment, 'HD_ddfd3027-4b17-4afd-a42f-cec512ec544b')
        aks_service = workspace.webservices['stackoverflow-classifier']

        pipelines = PublishedPipeline.list(workspace)
        published_pipeline = pipelines[0]

    except:
        print(
            "demo not initialized ... to speed up demo, after you have run through demo script all the way, set the values for the Run, HD_Run and AKS Service to fetch from existing entities instead of running realtime"
        )
        run = ""
        hd_run = ""
        aks_service = ""
        published_pipeline = ""

    stackoverflow_dataset, raw_dataset, azure_support_volume_timeseries_train, azure_support_volume_timeseries_test = register_data(
    )

    return ds, run, hd_run, aks_service, published_pipeline, stackoverflow_dataset, raw_dataset, train, azure_support_volume_timeseries_train, azure_support_volume_timeseries_test
Ejemplo n.º 7
0
def download_model_file(run_id: str, remote_file: str, local_file: str) -> None:
    """Download the model.pt file for the corresponding run_id to the local
    directory. Then it is uploaded to Azure when run. Could be other ways
    to acomplish this (copy directly from run to run perhaps)"""
    ws = Workspace.from_config()
    experiment = Experiment(workspace=ws, name="hm-2016")
    run = Run(experiment, run_id)
    print(model_file)
    run.download_file(remote_file, local_file)
Ejemplo n.º 8
0
def main():
    args = getRuntimeArgs()
    run = Run.get_context()
    run_id = run.parent.id
    parent_run = Run(experiment=run.experiment, run_id=run_id)
    # register the best model with the input dataset
    model = parent_run.register_model(model_name=args.model_name,
                                      model_path=os.path.join(
                                          'outputs', 'model.pkl'))
Ejemplo n.º 9
0
def register_model(run_id, experiment):
    best_run = Run(experiment=experiment, run_id=run_id)
    files = best_run.get_file_names()
    r = re.compile('outputs.*')
    model_path = [l for l in files if r.match(l)]
    path, model = os.path.split(model_path[0])

    model = best_run.register_model(model_name=model,
                                    model_path='outputs/model.pkl')
    return path
Ejemplo n.º 10
0
def register_model(model_name, run_id):
    """Register the model to the AML Workspace"""
    cli_auth = AzureCliAuthentication()

    experiment = Experiment.from_directory(".", auth=cli_auth)
    run = Run(experiment, run_id)

    run.register_model(model_name,
                       model_path='outputs/final_model.hdf5',
                       model_framework='TfKeras',
                       model_framework_version='1.13')
Ejemplo n.º 11
0
    def _get_current_run(self, run_id):
        # get azureml Run and Experiment details from postgres db
        result = self.db_handler.execute_query(RUN_QUERY, params=[run_id])
        if len(result) == 0:
            raise Exception(
                'ERROR: run context of run_id `%s` not exists, please make sure `%s` is the right id.'
                % (run_id, run_id))
        db_aml_run_id, db_run_id, db_experiment_name = result.pop()

        # creating Run object
        experiment = Experiment(workspace=self.ws, name=db_experiment_name)
        run = Run(experiment=experiment, run_id=db_aml_run_id)
        return run
Ejemplo n.º 12
0
    def get_node_run(self, service_node_info):
        # create aml workspace
        aml_ws_name = utils.safe_value(self.compute_def, "service")
        ws = self.get_aml_ws(aml_ws_name)

        # create aml experiment
        aml_exper_name = service_node_info["aml_exper_name"]
        experiment = Experiment(ws, name=aml_exper_name)

        # create aml run
        aml_run_id = service_node_info["aml_run_id"]
        run = Run(experiment, aml_run_id)

        return run
Ejemplo n.º 13
0
def test_registered_model_metric(get_ws_config):
    try:
        with open("aml_config/run_id.json") as f:
            config = json.load(f)
            new_model_run_id = config["run_id"]
            if new_model_run_id != "":
                experiment_name = config["experiment_name"]
                exp = Experiment(workspace=ws, name=experiment_name)
                model_list = Model.list(
                    ws, tags={"area": "predictive maintenance"})
                production_model = model_list[0]
                run_list = exp.get_runs()
                new_model_run = Run(exp, run_id=new_model_run_id)
                new_model_metric = new_model_run.get_metrics().get('accuracy')
                assert new_model_metric > 0.85, "Above 85% accuracy"
    except FileNotFoundError:
        print("No new model registered to test")
Ejemplo n.º 14
0
def download_model(workspace, experiment_name, run_id, input_location,
                   output_location):
    """
    Download the pretrained model
    Input:
         workspace: workspace to access the experiment
         experiment_name: Name of the experiment in which model is saved
         run_id: Run Id of the experiment in which model is pre-trained
         input_location: Input location in a RUN Id
         output_location: Location for saving the model
    """
    experiment = Experiment(workspace=workspace, name=experiment_name)
    #Download the model on which evaluation need to be done
    run = Run(experiment, run_id=run_id)
    #run.get_details()
    run.download_file(input_location, output_location)
    logger.info("Successfully downloaded model")
Ejemplo n.º 15
0
def _azureml_run_from_mlflow_run(mlflow_run):
    from ._internal.store import AzureMLRestStore
    from ._internal.utils import VERSION_WARNING
    from mlflow.tracking.client import MlflowClient
    experiment_name = MlflowClient().get_experiment(mlflow_run.info.experiment_id).name
    try:
        def_store = MlflowClient()._tracking_client.store
    except:
        logger.warning(VERSION_WARNING.format("MlflowClient()._tracking_client.store"))
        def_store = MlflowClient().store
    aml_store = def_store if isinstance(def_store, AzureMLRestStore) else def_store.aml_store
    host = aml_store.get_host_creds().host
    auth_token = aml_store.get_host_creds().token

    cluster_url = host.split(_SUBSCRIPTIONS_PREFIX, 2)[0].split("/history/")[0]
    scope = "{}{}".format(_SUBSCRIPTIONS_PREFIX, host.split(_SUBSCRIPTIONS_PREFIX, 2)[1])
    auth = ArmTokenAuthentication(auth_token)
    run_id = mlflow_run.info.run_id

    subscription_id = re.search(r'/subscriptions/([^/]+)', scope).group(1)
    resource_group_name = re.search(r'/resourceGroups/([^/]+)', scope).group(1)
    workspace_name = re.search(r'/workspaces/([^/]+)', scope).group(1)
    workspace = Workspace(subscription_id,
                          resource_group_name,
                          workspace_name,
                          auth=auth,
                          _disable_service_check=True)

    experiment = Experiment(workspace, experiment_name)
    changed_env_var = False
    prev_env_var = None
    from azureml._base_sdk_common.service_discovery import HISTORY_SERVICE_ENDPOINT_KEY
    try:
        if HISTORY_SERVICE_ENDPOINT_KEY in os.environ:
            prev_env_var = os.environ[HISTORY_SERVICE_ENDPOINT_KEY]
        os.environ[HISTORY_SERVICE_ENDPOINT_KEY] = cluster_url
        changed_env_var = True
        azureml_run = Run(experiment, run_id)
        return azureml_run
    finally:
        if changed_env_var:
            if prev_env_var is not None:
                os.environ[HISTORY_SERVICE_ENDPOINT_KEY] = prev_env_var
            else:
                del os.environ[HISTORY_SERVICE_ENDPOINT_KEY]
def get_automl_run(workspace, experiment, run_id):
    try:
        experiment = Experiment(workspace, experiment)
        automl_run = Run(experiment, run_id)

        if ('runTemplate' not in automl_run.properties or automl_run.properties['runTemplate'] != "automl_child"):
            raise RuntimeError(f"Run with run_id={run_id} is a not an AutoML run!")

        # Get parent run
        parent_run = automl_run.parent
        while (parent_run.parent is not None):
            parent_run = parent_run.parent
        
        if (parent_run.type != 'automl'):
            raise RuntimeError(f"Only AutoML runs are supported, this run is of type {parent_run.type}!")
    except Exception as e:
        raise

    return automl_run
Ejemplo n.º 17
0
def download_model(ws, experiment_name, run_id, input_location, output_location):
    """Download the pretrained model

    Args:
         ws: workspace to access the experiment
         experiment_name: Name of the experiment in which model is saved
         run_id: Run Id of the experiment in which model is pre-trained
         input_location: Input location in a RUN Id
         output_location: Location for saving the model
    """
    experiment = Experiment(workspace=ws, name=experiment_name)
    # Download the model on which evaluation need to be done
    run = Run(experiment, run_id=run_id)
    if input_location.endswith(".h5"):
        run.download_file(input_location, output_location)
    elif input_location.endswith(".ckpt"):
        run.download_files(prefix=input_location,
                           output_directory=output_location)
    else:
        raise NameError(f"{input_location}'s path extension not supported")
    print("Successfully downloaded model")
Ejemplo n.º 18
0
def run_pipeline(ws, published_pipeline, aad_token):
    # specify the param when running the pipeline
    response = requests.post(published_pipeline.endpoint,
                             headers=aad_token,
                             json={
                                 "ExperimentName": published_pipeline.name,
                                 "RunSource": "SDK"
                             })

    try:
        run_id = response.json()["Id"]
    except:
        print(response)
        exit(1)

    experiment = Experiment(ws, pipeline_name)

    run = Run(experiment, run_id)

    while run.get_status() != 'Completed':
        print("Run status: %s" % run.get_status())
        time.sleep(5)

    print("Run status: %s" % run.get_status())
Ejemplo n.º 19
0
def process_event(event):
    if 'runId' not in event:
        raise ValueError('Malformed Event. Run Id does not exits. Event: %s',
                         event)

    logger.info(f'Event Details: {event}')

    exp = Experiment(workspace=get_workspace(), name=event['experimentName'])
    run = Run(exp, event['runId'])
    run_details = run.get_details()
    app_insight_connection = os.environ.get('APPINSIGHTS_CONNECTION_STRING')

    if 'endTimeUtc' in run_details:
        logger.info(f'Run details: {run_details}')
        """
        Just for sample here we are saving metrics to Application Insight after collecting details from 'run_details'
        & 'event'. You can store it to any other storage/database as well.
        """
        save_to_app_insight(to_run_metrics(run_details, event),
                            app_insight_connection)
    else:
        logging.info(
            'Ignoring event. As step/pipeline is still running. Event: %s',
            event)
Ejemplo n.º 20
0
    'max_concurrent_iterations':
    4,
    'hyperparameter_sampling':
    RandomParameterSampling(parameter_space),
    'policy':
    BanditPolicy(evaluation_interval=2, slack_factor=0.2, delay_evaluation=6)
}

automl_image_config = AutoMLImageConfig(
    task='image-object-detection',
    compute_target=compute_target,
    training_data=training_dataset,
    validation_data=validation_dataset,
    primary_metric='mean_average_precision',
    **tuning_settings)

automl_image_run = experiment.submit(automl_image_config)

automl_image_run.wait_for_completion(wait_post_processing=True)

from azureml.core import Run

hyperdrive_run = Run(experiment=experiment, run_id=automl_image_run.id + '_HD')
hyperdrive_run

# Register the model from the best run

best_child_run = automl_image_run.get_best_child()
model_name = best_child_run.properties['model_name']
model = best_child_run.register_model(model_name=model_name,
                                      model_path='outputs/model.pt')
Ejemplo n.º 21
0
    model_name = "model_alpha_" + str(alpha) + ".pkl"
    filename = "outputs/" + model_name

    joblib.dump(value=model, filename=filename)
    run.upload_file(name=model_name, path_or_stream=filename)
    run.complete()

minimum_rmse_runid = None
minimum_rmse = None

for run in experiment.get_runs():
    run_metrics = run.get_metrics()
    run_details = run.get_details()
    # each logged metric becomes a key in this returned dict
    run_rmse = run_metrics["rmse"]
    run_id = run_details["runId"]

    if minimum_rmse is None:
        minimum_rmse = run_rmse
        minimum_rmse_runid = run_id
    else:
        if run_rmse < minimum_rmse:
            minimum_rmse = run_rmse
            minimum_rmse_runid = run_id

print("Best run_id: " + minimum_rmse_runid)
print("Best run_id rmse: " + str(minimum_rmse))

best_run = Run(experiment=experiment, run_id=minimum_rmse_runid)
print(best_run.get_file_names())
Ejemplo n.º 22
0
latest_model_id = latest_model.id
latest_model_name = latest_model.name
latest_model_version = latest_model.version
latest_model_path = latest_model.get_model_path(latest_model_name,
                                                _workspace=ws)

print('Latest model id: ', latest_model_id)
print('Latest model name: ', latest_model_name)
print('Latest model version: ', latest_model_version)
print('Latest model path: ', latest_model_path)

latest_model_run_id = latest_model.tags.get("run_id")
print('Latest model run id: ', latest_model_run_id)

latest_model_run = Run(run.experiment, run_id=latest_model_run_id)

latest_model_accuracy = latest_model_run.get_metrics().get("acc")
print('Latest model accuracy: ', latest_model_accuracy)

ws_list = Webservice.list(ws, model_name=latest_model_name)
print('webservice list')
print(ws_list)

deploy_model = False
current_model = None

if (len(ws_list) > 0):
    webservice = ws_list[0]
    try:
        image_id = webservice.tags['image_id']
Ejemplo n.º 23
0
new_model_run_id = config["run_id"]
experiment_name = config["experiment_name"]
exp = Experiment(workspace=ws, name=experiment_name)

try:
    # Get most recently registered model, we assume that is the model in production. Download this model and compare it with the recently trained model by running test with same data set.
    model_list = Model.list(ws)
    production_model = next(
        filter(
            lambda x: x.created_time == max(model.created_time
                                            for model in model_list),
            model_list))
    production_model_run_id = production_model.tags.get('run_id')
    run_list = exp.get_runs()
    # Get the run history for both production model and newly trained model and compare mse
    production_model_run = Run(exp, run_id=production_model_run_id)
    new_model_run = Run(exp, run_id=new_model_run_id)

    production_model_metric = production_model_run.get_metrics().get(
        'accuracy')
    new_model_metric = new_model_run.get_metrics().get('accuracy')
    print(
        'Current Production model accuracy: {}, New trained model accuracy: {}'
        .format(production_model_metric, new_model_metric))

    promote_new_model = False
    if new_model_metric < production_model_metric:
        promote_new_model = True
        print('New trained model performs better, thus it will be registered')
except:
    promote_new_model = True
Ejemplo n.º 24
0
# Get workspace
print("Loading Workspace")
cli_auth = AzureCliAuthentication()
config_file_path = os.environ.get("GITHUB_WORKSPACE", default="aml_service")
config_file_name = "aml_arm_config.json"
ws = Workspace.from_config(
    path=config_file_path,
    auth=cli_auth,
    _file_name=config_file_name)
print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep = '\n')

# Loading Run
print("Loading Run")
experiment = Experiment(workspace=ws, name=run_details["experiment_name"])
run = Run(experiment=experiment, run_id=run_details["run_id"])

# Only register model, if it performs better than the production model
print("Register model only if it performs better.")
try:
    # Loading run of production model
    print("Loading Run of Production Model to evaluate new model")
    production_model = Model(workspace=ws, name=deployment_settings["model"]["name"])
    production_model_run_id = production_model.tags.get(["run_id"])
    production_model_run = Run(experiment=experiment, run_id=production_model_run_id)

    # Comparing models
    print("Comparing Metrics of production and newly trained model")
    promote_new_model = True
    for metric in deployment_settings["model"]["evaluation_parameters"]["larger_is_better"]:
        if not promote_new_model:
Ejemplo n.º 25
0
    
    parser.add_argument(
        '--model_name', type=str, default='',help='Name you want to give to the model.'
    )
    
    parser.add_argument(
        '--model_assets_path',type=str, default='outputs',help='Location of trained model.'
    )

    args,unparsed = parser.parse_known_args()
    
    print('Model assets path is:',args.model_assets_path)
    print('Model name is:',args.model_name)
      
    run = Run.get_context()
   
    pipeline_run = Run(run.experiment, run._root_run_id)
    pipeline_run.upload_file("outputs/model/model.pth",os.path.join(args.model_assets_path,"model.pth"))
    pipeline_run.upload_file("outputs/model/labels.txt",os.path.join(args.model_assets_path,"labels.txt"))
    pipeline_run.upload_file("outputs/deployment/score.py","deployment/score.py")
    pipeline_run.upload_file("outputs/deployment/myenv.yml","deployment/myenv.yml")
    pipeline_run.upload_file("outputs/deployment/deploymentconfig.json","deployment/deploymentconfig.json")
    pipeline_run.upload_file("outputs/deployment/inferenceconfig.json","deployment/inferenceconfig.json")

    tags = {
       "Conference":"Codecamp"
    }

    model = pipeline_run.register_model(model_name=args.model_name, model_path='outputs/',tags=tags)
       
    print('Model registered: {} \nModel Description: {} \nModel Version: {}'.format(model.name, model.description, model.version))
Ejemplo n.º 26
0
try:
    with open("./configuration/run_id.json") as f:
        config = json.load(f)
    if not config["run_id"]:
        raise Exception(
            "No new model to register as production model perform better")
except:
    print("No new model to register as production model perform better")
    # raise Exception('No new model to register as production model perform better')
    sys.exit(0)

run_id = config["run_id"]
experiment_name = config["experiment_name"]
exp = Experiment(workspace=ws, name=experiment_name)

run = Run(experiment=exp, run_id=run_id)
names = run.get_file_names
names()
print("Run ID for last run: {}".format(run_id))
model_local_dir = "model"
os.makedirs(model_local_dir, exist_ok=True)

# Download Model to Project root directory
model_name = "arima_model.pkl"
run.download_file(name="./outputs/" + model_name,
                  output_file_path="./model/" + model_name)
print("Downloaded model {} to Project root directory".format(model_name))
os.chdir("./model")
model = Model.register(
    model_path=model_name,  # this points to a local file
    model_name=model_name,  # this is the name the model is registered as
Ejemplo n.º 27
0
    job_info_path = "parent_run.json"
    sys.argv.append("1")
    experiment_name = sys.argv[1]
    run_name = sys.argv[3][:-3]  # should be the file name

    env_dictionary = {"MLFLOW_EXPERIMENT_NAME": experiment_name}
    if os.path.exists(job_info_path):
        # get parent run id, experiment name from file & workspace obj
        # create child run (id )
        with open(job_info_path, 'r') as f:
            job_info_dict = json.load(f)
        print("Dictionary read from file " + job_info_dict + "\n")
        run_id = job_info_dict["run_id"]
        ws = get_ws()  # TODO set path and auth
        exp = Experiment(workspace=ws, name=experiment_name)
        run = Run(exp, run_id)
        run.child_run(name=run_name)  # TODO: add the step's name
        tags = {
            "mlflow.source.type": "JOB",
            "mlflow.source.name": "train.py",
            "mlflow.user": "******"
        }
        run.set_tags(tags)
        # log environment variables
        env_dictionary["MLFLOW_EXPERIMENT_ID"] = exp._id
        env_dictionary["MLFLOW_RUN_ID"] = run_id
        env_dictionary["MLFLOW_TRACKING_URI"] = _get_mlflow_tracking_uri(ws)
        env_dictionary["HOME"] = "~/"
    else:
        # start run
        ws = get_ws()
Ejemplo n.º 28
0
from azureml.core.model import Model
from azureml.pipeline.steps import HyperDriveStep, HyperDriveStepRun
######################################################################################################
parser = argparse.ArgumentParser()
parser.add_argument('--ModelData', dest='ModelData', required=True)
args = parser.parse_args()

Model_OutputPath = args.ModelData

######################################################################################################

run = Run.get_context()
run_id = run.parent.id

print("register final model")

parent_run = Run(experiment=run.experiment, run_id=run_id)

model = parent_run.register_model(
    model_name='MLOps_Model',
    model_path='outputs/weights.best.dense_generator_callback.hdf5')

######################################################################################################

os.makedirs(Model_OutputPath, exist_ok=True)

model_json = {}
model_json["model_name"] = model.name
model_json["model_version"] = model.version
with open(os.path.join(Model_OutputPath, 'model.json'), "w") as outfile:
    json.dump(model_json, outfile)
Ejemplo n.º 29
0
def deploy_model(ws, aci_service_name, experiment_name, asset_name,
                 asset_label, run_id, cpu_cores, memory_gb, entry_script):
    env = create_env_from_requirements(endpoint=True)
    inference_config = InferenceConfig(source_directory=os.getcwd(),
                                       entry_script=entry_script,
                                       environment=env)

    deployment_config = AciWebservice.deploy_configuration(cpu_cores=cpu_cores,
                                                           memory_gb=memory_gb)

    # model name
    model_name = get_model_register_name(run_id)
    try:
        model = Model(ws, name=model_name)
    except:
        # creating directory for download Model files for Model register
        tmp_path = create_tempdir(name='download_tmp')
        register_path = create_directory(AML_MLAPP_FOLDER, path=tmp_path)

        # getting RUN context
        experiment = Experiment(workspace=ws, name=experiment_name)
        tags = {"run_id": run_id, "asset_name": asset_name}
        if asset_label is not None:
            tags["asset_label"] = asset_label

        selected_run_id = None
        for run in Run.list(experiment,
                            tags=tags,
                            include_children=True,
                            status='Completed'):
            run_metrics = run.get_metrics()
            exp_saved_run_id = run_metrics.get("run_id")
            if exp_saved_run_id == run_id:
                selected_run_id = run.id
                break
        if selected_run_id is None:
            raise Exception(
                'ERROR: there is no matching Run object that associated with the run id %s in this experiment.'
                % str(run_id))
        current_run = Run(experiment=experiment, run_id=selected_run_id)

        # download files from run object
        current_run.download_files(output_directory=register_path)

        # register model
        model = Model.register(ws,
                               model_path=register_path,
                               model_name=model_name,
                               tags=tags,
                               description=asset_name)

        # deletes tmp dir and all content
        delete_directory_with_all_contents(tmp_path)

    # deploy model
    service = None
    try:
        service = Webservice(ws, name=aci_service_name)
        service.update(models=[model], inference_config=inference_config)
    except WebserviceException as e:
        if service:
            service.delete()
        service = Model.deploy(ws, aci_service_name, [model], inference_config,
                               deployment_config)

    service.wait_for_deployment(True)
@author: datacore
"""

from azureml.core.authentication import AzureCliAuthentication
import azure.cli.core
#cli_auth = AzureCliAuthentication()
from azureml.core.workspace import Workspace

ws = Workspace(subscription_id="24075937-2687-4457-bac6-ec16dec514c3",
               resource_group="VstsRG-784AbhijitC-8a31",
               workspace_name="automldc")

from azureml.core.experiment import Experiment
from azureml.core import Run
experiment = Experiment(ws, 'Myexp2_v1_test21')
best_run = Run(experiment=experiment,
               run_id='AutoML_74e9d9dc-f347-4392-b8bb-3edeb4a6afad_8')
fitted_model = Run(experiment=experiment,
                   run_id='AutoML_74e9d9dc-f347-4392-b8bb-3edeb4a6afad_8')
#print(best_run.register_model()
print(fitted_model)

# Get a dataset by name
from azureml.core.dataset import Dataset

file_name = '2018Q4PredictionTrainedSet101.csv'
stock_dataset = Dataset.get_by_name(ws, '2018Q4PredictionTrainedSet101.csv')
#stock_dataset
#dataset = Dataset.Tabular.from_delimited_files(stock_dataset)
stock_dataset.to_pandas_dataframe().describe()
stock_dataset.take(3).to_pandas_dataframe()