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")
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)
print(f"Last Run ID: {run_id}") # register last run_id as environment variable so it can be used in %sh commands os.environ['RUN_ID_LAST'] = run_id # print(os.getenv('RUN_ID_LAST')) # COMMAND ---------- import mlflow.sklearn # run_id = "434c0e56-307a-4e40-a170-343530c77386" run = Run(experiment, run_id=run_id) run.get_file_names() # COMMAND ---------- run.download_files(output_directory=f"/tmp/{run_id}") # COMMAND ---------- # MAGIC %sh # MAGIC ls -l /tmp/$RUN_ID_LAST/random_forest_model # COMMAND ---------- model_uri_aml = f"/tmp/{run_id}/random_forest_model" model = mlflow.sklearn.load_model(model_uri_aml) # COMMAND ---------- model