def main(): """ Deploy model to your service """ interactive_auth = InteractiveLoginAuthentication( tenant_id=os.getenv("TENANT_ID")) work_space = Workspace.from_config(auth=interactive_auth) environment = Environment("keras-service-environment") environment.python.conda_dependencies = CondaDependencies.create( python_version="3.7.7", pip_packages=["azureml-defaults", "numpy", "tensorflow==2.3.1"], ) model = Model(work_space, "keras_mnist") model_list = model.list(work_space) validation_accuracy = [] version = [] for i in model_list: validation_accuracy.append(float(i.properties["val_accuracy"])) version.append(i.version) model = Model(work_space, "keras_mnist", version=version[np.argmax(validation_accuracy)]) service_name = "keras-mnist-service" inference_config = InferenceConfig(entry_script="score_keras.py", environment=environment) aci_config = AciWebservice.deploy_configuration(cpu_cores=1, memory_gb=1) service = Model.deploy( workspace=work_space, name=service_name, models=[model], inference_config=inference_config, deployment_config=aci_config, overwrite=True, ) service.wait_for_deployment(show_output=True) print(service.get_logs())
def create_conda_environment(workspace, name, conda_dependencies, pip_dependencies): """ Create an environment or retrieve it by its name from workspace Pip installs Python packages whereas conda installs packages which may contain software written in any language. e.g. TensorFlow, Scikit-Learn -> Conda, Matplotlib -> pip """ if name in Environment.list(workspace): env = Environment.get(workspace=workspace, name=name) print("The environment '{}' already existed for the workspace".format( name)) else: env = Environment(name=name) env.docker.enabled = True env.python.conda_dependencies = CondaDependencies.create( conda_packages=conda_dependencies, pip_packages=pip_dependencies, ) env.register(workspace=workspace) return env
from azureml.core import ScriptRunConfig from azureml.core import Dataset if __name__ == "__main__": ws = Workspace.from_config() datastore = ws.get_default_datastore() dataset = Dataset.File.from_files(path=(datastore, 'datasets/cifar10')) experiment = Experiment(workspace=ws, name='day1-experiment-data') config = ScriptRunConfig( source_directory='./src', script='train.py', compute_target='cpu-cluster', arguments=[ '--data_path', dataset.as_named_input('input').as_mount(), '--learning_rate', 0.003, '--momentum', 0.92 ], ) # set up pytorch environment env = Environment.from_conda_specification( name='pytorch-env', file_path='./.azureml/pytorch-env.yml') config.run_config.environment = env run = experiment.submit(config) aml_url = run.get_portal_url() print("Submitted to compute cluster. Click link below") print("") print(aml_url)
experiment_name = "template-workflow-base" data_uri = "https://azuremlexamples.blob.core.windows.net/datasets/iris.csv" # convert to relative paths prefix = Path(__file__).parent source_dir = str(prefix.joinpath(source_dir)) environment_file = str(prefix.joinpath(environment_file)) # get workspace ws = Workspace.from_config() # create dataset ds = Dataset.File.from_files(data_uri) # create environment env = Environment.from_pip_requirements(environment_name, environment_file) # setup entry script arguments args = ["--data-dir", ds.as_mount()] # create a job configuration src = ScriptRunConfig( source_directory=source_dir, script=entry_script, arguments=args, environment=env, compute_target=compute_name, ) # run the job run = Experiment(ws, experiment_name).submit(src)
# script arguments arguments = [ "--deepspeed", "--deepspeed_config", "ds_config.json", "--deepspeed_mpi", "--global_rank", "$AZ_BATCHAI_TASK_INDEX", "--with_aml_log", True, ] # create an environment # Note: We will use the Dockerfile method to create an environment for DeepSpeed. # In future, we plan to create a Curated environment for DeepSpeed. env = Environment(name="deepspeed-example") env.docker.enabled = True # indicate how to run Python env.python.user_managed_dependencies = True env.python.interpreter_path = "/opt/miniconda/bin/python" # To install any Python packages you need, simply add RUN pip install package-name to the docker string. E.g. `RUN pip install sklearn` # Specify docker steps as a string and use the base DeepSpeed Docker image dockerfile = r""" FROM deepspeed/base-aml:with-pt-ds-and-deps RUN pip install azureml-mlflow RUN echo "Welcome to the DeepSpeed custom environment!" """ # set base image to None, because the image is defined by dockerfile.
ct = ComputeTarget.create(ws, ct_name, compute_config) ct.wait_for_completion(show_output=True) # create aks compute targets if args.create_aks: for ct_name in akscomputes: if ct_name not in ws.compute_targets: print(f"Creating AKS compute {ct_name}") compute_config = AksCompute.provisioning_configuration( **akscomputes[ct_name]) ct = ComputeTarget.create(ws, ct_name, compute_config) ct.wait_for_completion(show_output=True) if args.datalake_name not in ws.datastores: print(f"Creating datastore {args.datalake_name}") adlsgen2_datastore = Datastore.register_azure_data_lake_gen2( workspace=ws, datastore_name=args.datalake_name, account_name=args.datalake_name, subscription_id=ws.subscription_id, resource_group=args.datalake_rg_name, filesystem="datalake", grant_workspace_access=True, ) for env_file in Path("./environments/").glob("*.yml"): print(f"Creating env {env_file.name}") env = Environment.from_conda_specification(name=env_file.name, file_path=env_file) env.register(ws)
entry_script=deployment_settings["image"]["entry_script"], source_directory=deployment_settings["image"]["source_directory"], runtime=deployment_settings["image"]["runtime"], conda_file=os.path.basename(dep_path), extra_docker_file_steps=deployment_settings["image"]["docker"] ["extra_docker_file_steps"], enable_gpu=deployment_settings["image"]["docker"]["use_gpu"], description=deployment_settings["image"]["description"], base_image=deployment_settings["image"]["docker"]["custom_image"], base_image_registry=container_registry, cuda_version=deployment_settings["image"]["docker"]["cuda_version"]) # Registering Environment print("Registering Environment") if "env" not in locals(): env = Environment.from_conda_specification(name=env_name, file_path=dep_path) registered_env = env.register(workspace=ws) print("Registered Environment") print(registered_env.name, "Version: " + registered_env.version, sep="\n") # Profile model print("Profiling Model") test_sample = test_functions.get_test_data_sample() profile = Model.profile(workspace=ws, profile_name=deployment_settings["image"]["name"], models=[model], inference_config=inference_config, input_data=test_sample) profile.wait_for_profiling(show_output=True) print(profile.get_results(), profile.recommended_cpu,
BASE_IMAGE = 'mcr.microsoft.com/azureml/openmpi3.1.2-cuda10.1-cudnn7-ubuntu18.04:20210301.v1' EXPERIMENT_NAME = 'road-segmentation-train' ENV_NAME = 'AzureML-TensorFlow-2.3-GPU' CLUSTER_NAME = 'GPU-cluster' VM_SIZE = 'Standard_NC6' SEED_NUMBER = 5 # Create workspace from config file ws = Workspace.from_config() # Create experiment to submit training experiment_name = 'road-segmentation-train' experiment = Experiment(ws, EXPERIMENT_NAME) # Create the environment tf_env = Environment(ENV_NAME) tf_env.docker.enabled = True tf_env.docker.base_image = BASE_IMAGE # Define additional packages to be installed conda_dep = CondaDependencies() conda_dep.add_pip_package('tensorflow-gpu==2.3.0') conda_dep.add_pip_package('pillow') # Add packages to the environment tf_env.python.conda_dependencies = conda_dep # Create the configuration of an experiment aml_run_config = RunConfiguration() aml_run_config.environment = tf_env # The name of the custome environment must not start by 'AzureML'
def get_mlapp_environment(workspace, env_name, version=None): return Environment.get(workspace=workspace, name=env_name, version=version)
prefix = Path(__file__).parent.parent.parent.absolute() # training script script_dir = prefix.joinpath("code", "train", "fastai", "pets-resnet34") script_name = "train.py" # environment file environment_file = prefix.joinpath("environments", "fastai.dockerfile") # azure ml settings environment_name = "fastai-pets-example" experiment_name = "fastai-pets-example" compute_name = "gpu-cluster" # create environment env = Environment(environment_name) env.docker.enabled = True env.docker.base_image = None env.docker.base_dockerfile = environment_file env.python.user_managed_dependencies = True # create job config src = ScriptRunConfig( source_directory=script_dir, script=script_name, environment=env, compute_target=compute_name, ) # submit job run = Experiment(ws, experiment_name).submit(src)
# -*- coding: utf-8 -*- """ Created on Sun Mar 28 23:22:27 2021 @author: RK """ #%% from azureml.core import Workspace from azureml.core import Experiment from azureml.core import Environment from azureml.core import ScriptRunConfig if __name__ == "__main__": ws = Workspace.from_config() experiment = Experiment(workspace=ws, name='day1-experiment-train') config = ScriptRunConfig(source_directory='./scripts', script='testp.py', compute_target='MLcompute01') # set up pytorch environment env = Environment.from_conda_specification(name='pytorch-env', file_path='./environ/envpy.yml') config.run_config.environment = env run = experiment.submit(config) aml_url = run.get_portal_url() print(aml_url)
# Authenticate with AzureML auth = ServicePrincipalAuthentication( tenant_id=auth_config["tenant_id"], service_principal_id=auth_config["service_principal_id"], service_principal_password=os.environ["SP_SECRET"], ) ws = Workspace( subscription_id=auth_config["subscription_id"], resource_group=auth_config["resource_group"], workspace_name=auth_config["workspace_name"], auth=auth, ) env = Environment.get(workspace=ws, name="component-condition") env.docker.enabled = True inf_config = InferenceConfig(entry_script="./score.py", environment=env) model = Model(ws, name=conf["metadata"]["model_name"]) deployment_config = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=2, description="Webservice to predict non-compliant car components.", enable_app_insights=True) svc = Model.deploy( workspace=ws, name="compcondition", models=[model],
from azureml.core import Workspace, Environment from azureml.core.conda_dependencies import CondaDependencies from azureml.core import Image ws = Workspace.from_config() # env = Environment(name = 'sahulat_env') env = Environment.from_conda_specification(name='az-ls-ds-ml-env', file_path='../az-ls-ds-ml-env.yml') # conda_dep = CondaDependencies() # # Installs numpy version 1.17.0 conda package # conda_deps = ['blas=1.0', # 'ca-certificates=2020.1.1', 'certifi=2020.4.5.1', 'intel-openmp=2019.4', 'joblib=0.14.1', 'libcxx=4.0.1=h579ed51_0', # 'libcxxabi=4.0.1', 'libedit=3.1.20181209', 'libffi=3.2.1', 'libgfortran=3.0.1', 'libsodium=1.0.16', # 'llvm-openmp=4.0.1', 'mkl=2019.4', 'mkl-service=2.3.0', 'mkl_fft=1.0.15', 'mkl_random=1.1.0', 'ncurses=6.2', # 'numpy=1.18.1', 'numpy-base=1.18.1', 'openssl=1.1.1f', 'pandas=1.0.3', 'pandoc=2.2.3.2', 'pip=20.0.2', 'python=3.8.2', # 'python-dateutil=2.8.1', 'pytz=2019.3', 'readline=8.0', 'scikit-learn=0.22.1', 'scipy=1.4.1', 'setuptools=46.1.3', # 'six=1.14.0', 'sqlite=3.31.1', 'tk=8.6.8', 'wheel=0.34.2', 'xz=5.2.5', 'zlib=1.2.11'] # pip_deps=['adal==1.2.2', 'azure-common==1.1.25','azure-core==1.4.0','azure-graphrbac==0.61.1','azure-identity==1.2.0', # 'azure-mgmt-authorization==0.60.0','azure-mgmt-containerregistry==2.8.0','azure-mgmt-keyvault==2.2.0','azure-mgmt-resource==8.0.1', # 'azure-mgmt-storage==9.0.0','azureml==0.2.7','azureml-core==1.3.0.post1','azureml-dataprep==1.4.3','azureml-dataprep-native==14.1.0', # 'backports-tempfile==1.0','backports-weakref==1.0.','post1cffi==1.14.0','chardet==3.0.4','cloudpickle==1.3.0', # 'contextlib2==0.6.0.post1','cryptography==2.9d','istro==1.5.0','docker==4.2.0', # 'dotnetcore2==2.1.13','idna==2.9','importlib-metadata==1.6.0','isodate==0.6.0','jeepney==0.4.3','jmespath==0.9.5', # 'jsonpickle==1.4', # 'msal==1.2.0','msal-extensions==0.1.3','msrest==0.6.13','msrestazure==0.6.3','ndg-httpsclient==0.5.1', # 'oauthlib==3.1.0','pathspec==0.8.0','portalocker==1.7.0','pyasn1==0.4.8','pycparser==2.20','pyjwt==1.7.1','pyopenssl==19.1.0', # 'requests==2.23.0','requests-oauthlib==1.3.0','ruamel-yaml==0.15.89','secretstorage==3.1.2','urllib3==1.25.9','websocket-client==0.57.0','zipp==3.1.0'] # for package in conda_deps: # conda_dep.add_conda_package(package)
from typing import Tuple import click from azureml.core import (ComputeTarget, Dataset, Environment, RunConfiguration, Workspace) from azureml.core.authentication import AzureCliAuthentication from azureml.core.experiment import Experiment from azureml.pipeline.core import (Pipeline, PipelineData, PipelineParameter, PublishedPipeline) from azureml.pipeline.steps import DatabricksStep, PythonScriptStep CLI_AUTH = AzureCliAuthentication() # noinspection PyTypeChecker WS = Workspace.from_config(auth=CLI_AUTH) RC = RunConfiguration() RC.environment = Environment.get(WS, "lightgbm") # noinspection PyTypeChecker def create_databricks_step( input_dataset: Dataset, compute: ComputeTarget, debug_run: bool) -> Tuple[DatabricksStep, PipelineData]: output_data = PipelineData(name="ParquetFiles", datastore=WS.get_default_datastore(), is_directory=True) node_size = 'Standard_DS4_v2' spark_version = '7.3.x-cpu-ml-scala2.12' db_step = DatabricksStep( name='Convert to Parquet',
import time from azureml.core import Workspace, Experiment, ScriptRunConfig, Environment from azureml.core.runconfig import RunConfiguration, DockerConfiguration ws = Workspace.from_config() ray_environment_name = "aml-ray-cpu" ray_environment_dockerfile_path = "./Docker/Dockerfile-cpu" # Build CPU image for Ray ray_cpu_env = Environment.from_dockerfile( name=ray_environment_name, dockerfile=ray_environment_dockerfile_path) ray_cpu_env.register(workspace=ws) ray_cpu_build_details = ray_cpu_env.build(workspace=ws) while ray_cpu_build_details.status not in ["Succeeded", "Failed"]: print( f"Awaiting completion of ray CPU environment build. Current status is: {ray_cpu_build_details.status}" ) time.sleep(10) command = ["python distribute_automl.py"] env = Environment.get(workspace=ws, name=ray_environment_name) compute_target = ws.compute_targets["cpucluster"] aml_run_config = RunConfiguration(communicator="OpenMpi") aml_run_config.target = compute_target aml_run_config.docker = DockerConfiguration(use_docker=True) aml_run_config.environment = env aml_run_config.node_count = 2 config = ScriptRunConfig( source_directory="ray/", command=command,
def show_available_environment(workspace): list_env = Environment.list(workspace=workspace) for env in list_env: if env.startswith("AzureML"): print("Name", env) print( "packages", list_env[env].python.conda_dependencies.serialize_to_string()) with open(Path(__file__).parent / 'configuration.json', 'r') as config_file: config = json.load(config_file) name = config['environment_base'] env = Environment.get(workspace=ws, name=name) # From a Conda specification file # myenv = Environment.from_conda_specification(name = "myenv",file_path = "path-to-conda-specification-file") # From a pip requirements file # myenv = Environment.from_pip_requirements(name = "myenv", file_path = "path-to-pip-requirements-file") #From an existing Conda environment # myenv = Environment.from_existing_conda_environment(name = "myenv",conda_environment_name = "mycondaenv") #From exisitng yml file # myenv = Environment.load_from_directory(path = "path-to-source-directory") #
compute_target = ComputeTarget(aml_workspace, variables["AML_COMPUTE_CLUSTER_CPU_SKU"]) print('Found existing cluster, use it.') except ComputeTargetException: compute_config = AmlCompute.provisioning_configuration( vm_size=variables['AML_COMPUTE_CLUSTER_SIZE'], vm_priority=variables['AML_CLUSTER_PRIORITY'], min_nodes=variables['AML_CLUSTER_MIN_NODES'], max_nodes=variables['AML_CLUSTER_MAX_NODES'], idle_seconds_before_scaledown="300") cpu_cluster = ComputeTarget.create( aml_workspace, variables["AML_COMPUTE_CLUSTER_CPU_SKU"], compute_config) #create environment from conda_dependencies.yml for runconfig environment = Environment(name="myenv") conda_dep = CondaDependencies( conda_dependencies_file_path="conda_dependencies.yml") environment.python.conda_dependencies = conda_dep run_config = RunConfiguration() run_config.environment = environment #Dataset creation dataset_name = variables["DATASET_NAME"] #Check to see if dataset exists if (dataset_name not in aml_workspace.datasets): #Create dataset from diabetes sample data sample_data = load_diabetes() df = pd.DataFrame(data=sample_data.data, columns=sample_data.feature_names) df['Y'] = sample_data.target
def deploy(model_uri, workspace, deployment_config=None, service_name=None, model_name=None, tags=None, mlflow_home=None, synchronous=True): """ Register an MLflow model with Azure ML and deploy a websevice to Azure Container Instances (ACI) or Azure Kubernetes Service (AKS). The deployed service will contain a webserver that processes model queries. For information about the input data formats accepted by this webserver, see the :ref:`MLflow deployment tools documentation <azureml_deployment>`. :param model_uri: The location, in URI format, of the MLflow model used to build the Azure ML deployment image. For example: - ``/Users/me/path/to/local/model`` - ``relative/path/to/local/model`` - ``s3://my_bucket/path/to/model`` - ``runs:/<mlflow_run_id>/run-relative/path/to/model`` - ``models:/<model_name>/<model_version>`` - ``models:/<model_name>/<stage>`` For more information about supported URI schemes, see `Referencing Artifacts <https://www.mlflow.org/docs/latest/concepts.html# artifact-locations>`_. :param workspace: The AzureML workspace in which to deploy the service. This is a `azureml.core.Workspace` object. :param deployment_config: The configuration for the Azure web service. This configuration allows you to specify the resources the webservice will use and the compute cluster it will be deployed in. If unspecified, the web service will be deployed into a Azure Container Instance. This is a `azureml.core.DeploymentConfig` object. For more information, see `<https://docs.microsoft.com/python/api/azureml-core/ azureml.core.webservice.aks.aksservicedeploymentconfiguration>`_ and `<https://docs.microsoft.com/en-us/python/api/azureml-core/azureml .core.webservice.aci.aciservicedeploymentconfiguration>`_ :param service_name: The name to assign the Azure Machine learning webservice that will be created. If unspecified, a unique name will be generated. :param model_name: The name to assign the Azure Model will be created. If unspecified, a unique model name will be generated. :param tags: A collection of tags, represented as a dictionary of string key-value pairs, to associate with the Azure Model and Deployment that will be created. These tags are added to a set of default tags that include the model uri, and more. For more information, see `<https://docs.microsoft.com/en-us/python/api/azureml-core/azureml.core.model(class)?view=azure-ml-py>`_. :param mlflow_home: Path to a local copy of the MLflow GitHub repository. If specified, the image will install MLflow from this directory. Otherwise, it will install MLflow from pip. :param synchronous: If ``True``, this method blocks until the image creation procedure terminates before returning. If ``False``, the method returns immediately, but the returned image will not be available until the asynchronous creation process completes. Use the ``azureml.core.Webservice.wait_for_deployment()`` function to wait for the deployment process to complete. :return: A tuple containing the following elements in order: - An ``azureml.core.webservice.Webservice`` object containing metadata for the new service. - An ``azureml.core.model.Model`` object containing metadata for the new model. .. code-block:: python :caption: Example import mlflow.azureml from azureml.core import Workspace from azureml.core.webservice import AciWebservice, Webservice # Load or create an Azure ML Workspace workspace_name = "<Name of your Azure ML workspace>" subscription_id = "<Your Azure subscription ID>" resource_group = "<Name of the Azure resource group in which to create Azure ML resources>" location = "<Name of the Azure location (region) in which to create Azure ML resources>" azure_workspace = Workspace.create(name=workspace_name, subscription_id=subscription_id, resource_group=resource_group, location=location, create_resource_group=True, exist_ok=True) # Create an Azure Container Instance webservice for an MLflow model azure_service, azure_model = mlflow.azureml.deploy(model_uri="<model_uri>", service_name="<deployment-name>", workspace=azure_workspace, synchronous=True) """ # The Azure ML SDK is only compatible with Python 3. However, the `mlflow.azureml` module should # still be accessible for import from Python 2. Therefore, we will only import from the SDK # upon method invocation. # pylint: disable=import-error from azureml.core.model import Model as AzureModel, InferenceConfig from azureml.core import Environment as AzureEnvironment from azureml.core import VERSION as AZUREML_VERSION from azureml.core.webservice import AciWebservice absolute_model_path = _download_artifact_from_uri(model_uri) model_pyfunc_conf, model = _load_pyfunc_conf_with_model(model_path=absolute_model_path) model_python_version = model_pyfunc_conf.get(pyfunc.PY_VERSION, None) run_id = None run_id_tag = None try: run_id = model.run_id run_id_tag = run_id except AttributeError: run_id = str(uuid.uuid4()) if model_python_version is not None and\ StrictVersion(model_python_version) < StrictVersion("3.0.0"): raise MlflowException( message=("Azure ML can only deploy models trained in Python 3 and above. See" " the following MLflow GitHub issue for a thorough explanation of this" " limitation and a workaround to enable support for deploying models" " trained in Python 2: https://github.com/mlflow/mlflow/issues/668"), error_code=INVALID_PARAMETER_VALUE) tags = _build_tags(model_uri=model_uri, model_python_version=model_python_version, user_tags=tags, run_id=run_id_tag) if service_name is None: service_name = _get_mlflow_azure_name(run_id) if model_name is None: model_name = _get_mlflow_azure_name(run_id) with TempDir(chdr=True) as tmp: model_directory_path = tmp.path("model") tmp_model_path = os.path.join( model_directory_path, _copy_file_or_tree(src=absolute_model_path, dst=model_directory_path)) registered_model = AzureModel.register(workspace=workspace, model_path=tmp_model_path, model_name=model_name, tags=tags) _logger.info("Registered an Azure Model with name: `%s` and version: `%s`", registered_model.name, registered_model.version) # Create an execution script (entry point) for the image's model server. Azure ML requires # the container's execution script to be located in the current working directory during # image creation, so we create the execution script as a temporary file in the current # working directory. execution_script_path = tmp.path("execution_script.py") _create_execution_script(output_path=execution_script_path, azure_model=registered_model) environment = None if pyfunc.ENV in model_pyfunc_conf: environment = AzureEnvironment.from_conda_specification( _get_mlflow_azure_name(run_id), os.path.join(tmp_model_path, model_pyfunc_conf[pyfunc.ENV])) else: environment = AzureEnvironment(_get_mlflow_azure_name(run_id)) if mlflow_home is not None: path = tmp.path("dist") _logger.info("Bulding temporary MLFlow wheel in %s", path) wheel = _create_mlflow_wheel(mlflow_home, path) whl_url = AzureEnvironment.add_private_pip_wheel( workspace=workspace, file_path=wheel, exist_ok=True) environment.python.conda_dependencies.add_pip_package(whl_url) else: environment.python.conda_dependencies.add_pip_package( "mlflow=={}".format(mlflow_version)) # AzureML requires azureml-defaults to be installed to include # flask for the inference server. environment.python.conda_dependencies.add_pip_package( "azureml-defaults=={}".format(AZUREML_VERSION)) inference_config = InferenceConfig(entry_script=execution_script_path, environment=environment) if deployment_config is not None: if deployment_config.tags is not None: # We want more narrowly-scoped tags to win on merge tags.update(deployment_config.tags) deployment_config.tags = tags else: deployment_config = AciWebservice.deploy_configuration(tags=tags) webservice = AzureModel.deploy( workspace=workspace, name=service_name, models=[registered_model], inference_config=inference_config, deployment_config=deployment_config ) _logger.info("Deploying an Azure Webservice with name: `%s`", webservice.name) if synchronous: webservice.wait_for_deployment(show_output=True) return webservice, registered_model
args = parser.parse_args() ws = Workspace.from_config() experiment = Experiment(workspace=ws, name='cloud-moa-prediction') config = ScriptRunConfig(source_directory='./src', script='remote-train.py', compute_target='cpu-cluster', arguments=[ '--criterion', args.criterion, '--random_state', args.random_state, '--class_weight', args.class_weight ]) # set up pytorch environment env = Environment.from_conda_specification( name='sklearn-remote-env', file_path='./azure-config/compute-env-config.yml') config.run_config.environment = env run = experiment.submit(config) aml_url = run.get_portal_url() print("Submitted to compute cluster. Click link below") print("") print(aml_url) run.wait_for_completion(show_output=True) run.register_model( model_name='moa_prediction_model', model_path='outputs/RandomForestClassifier.pkl', # run outputs path description='Random Forest for MoA prediction', tags={'data-format': 'CSV'}, model_framework=Model.Framework.SCIKITLEARN,
from azureml.core import RunConfiguration, ScriptRunConfig, Dataset, Workspace, Environment from azureml.core.runconfig import Data, DataLocation, Dataset as RunDataset from azureml.core.script_run_config import get_run_config_from_script_run from azureml.core.conda_dependencies import CondaDependencies # Connect to the workspace ws = Workspace.from_config() # Create a new environment and set Conda dependencies conda_env = Environment('conda-env') conda_env.python.conda_dependencies = CondaDependencies.create( pin_sdk_version=False, pip_packages=[ 'scikit-learn', 'azureml-sdk', 'azureml-dataprep[pandas,fuse]' ]) # Get the dataset that will be used dataset = Dataset.get_by_name(ws, 'mnist-dataset') # Define the environment variable/where data will be mounted input_name = 'mnist' # Define the name of the compute target for training compute_name = 'cpu-cluster' # Define the script run config src = ScriptRunConfig( source_directory='scripts', script='train.py', arguments=[ '--data-folder',
ws = Workspace( subscription_id=auth_config["subscription_id"], resource_group=auth_config["resource_group"], workspace_name=auth_config["workspace_name"], auth=auth, ) # Usually, the cluster already exists, so we just fetch compute_target = next( (m for m in ComputeTarget.list(ws) if m.name == compute["name"]), None ) # Specify the compute environment and register it for use in scoring env = Environment("component-condition") env.docker.enabled = True cd = CondaDependencies.create( conda_packages=[ "tensorflow=2.0.0", "pandas", "numpy", "matplotlib" ], pip_packages=[ "azureml-mlflow==1.5.0", "azureml-defaults==1.5.0" ] ) env.python.conda_dependencies = cd env.register(workspace=ws)
def main(): """Build pipeline.""" # Environment variables env = Env() # Azure ML workspace aml_workspace = Workspace.get( name=env.workspace_name, subscription_id=env.subscription_id, resource_group=env.resource_group, ) logger.info(f"Azure ML workspace: {aml_workspace}") # Azure ML compute cluster aml_compute = get_compute(aml_workspace, env.compute_name) logger.info(f"Aazure ML compute cluster: {aml_compute}") # Azure ML environment environment = Environment(name=env.aml_env_name) conda_dep = CondaDependencies( conda_dependencies_file_path="./local_development/dev_dependencies.yml" ) environment.python.conda_dependencies = conda_dep run_config = RunConfiguration() run_config.environment = environment # Pipeline Data preparation_pipelinedata = PipelineData("preparation_pipelinedata", is_directory=True).as_dataset() extraction_pipelinedata = PipelineData("extraction_pipelinedata", is_directory=True) training_pipelinedata = PipelineData("training_pipelinedata", is_directory=True) # List of pipeline steps step_list = list() preparation_step = PythonScriptStep( name="preparation-step", compute_target=aml_compute, source_directory=env.sources_directory_train, script_name=env.preparation_step_script_path, outputs=[preparation_pipelinedata], arguments=[ "--input_path", env.input_dir, "--output_path", preparation_pipelinedata, "--datastore_name", env.blob_datastore_name ], runconfig=run_config) step_list.append(preparation_step) parallel_run_config = ParallelRunConfig( source_directory=env.sources_directory_train, entry_script=env.extraction_step_script_path, mini_batch_size=env.mini_batch_size, error_threshold=env.error_threshold, output_action="append_row", environment=environment, compute_target=aml_compute, node_count=env.node_count, run_invocation_timeout=env.run_invocation_timeout, process_count_per_node=env.process_count_per_node, append_row_file_name="extraction_output.txt") extraction_step = ParallelRunStep( name="extraction-step", inputs=[preparation_pipelinedata], output=extraction_pipelinedata, arguments=["--output_dir", extraction_pipelinedata], parallel_run_config=parallel_run_config) step_list.append(extraction_step) training_step = PythonScriptStep( name="traning-step", compute_target=aml_compute, source_directory=env.sources_directory_train, script_name=env.training_step_script_path, inputs=[extraction_pipelinedata], outputs=[training_pipelinedata], arguments=[ "--input_dir", extraction_pipelinedata, "--output_dir", training_pipelinedata ], runconfig=run_config) step_list.append(training_step) # Build pipeline pipeline = Pipeline(workspace=aml_workspace, steps=step_list) pipeline.validate() logger.info(f"Built pipeline {pipeline}") # Publish pipeline published_pipeline = pipeline.publish( env.pipeline_name, description=env.pipeline_name, version=datetime.utcnow().isoformat()) try: pipeline_endpoint = PipelineEndpoint.get( workspace=aml_workspace, name=env.pipeline_endpoint_name) pipeline_endpoint.add_default(published_pipeline) except ErrorResponseException: pipeline_endpoint = PipelineEndpoint.publish( workspace=aml_workspace, name=env.pipeline_endpoint_name, pipeline=published_pipeline, description=env.pipeline_endpoint_name)
# A cheap machine with CPU from azureml.core import Environment, Workspace, ScriptRunConfig, Experiment from utils.agent import createAmlCompute EXPERIMENT_NAME = 'road-segmentation-build' ENV_NAME = 'AzureML-TensorFlow-2.3-CPU' CLUSTER_NAME = 'CPU-cluster' VM_SIZE = 'Standard_D1_v2' ws = Workspace.from_config() # Create an experiment experiment = Experiment(ws, EXPERIMENT_NAME) # Create an environment tf_env = Environment.get(ws, ENV_NAME) # Create compute target compute_target = createAmlCompute(ws, CLUSTER_NAME, VM_SIZE) # Create run configuration params script_run_params = dict(source_directory='./build', script='build.py', arguments=['--path_model', 'models/new'], compute_target=compute_target, environment=tf_env) src = ScriptRunConfig(**script_run_params) run = experiment.submit(src) run.wait_for_completion()
from azureml.core import Workspace from azureml.core import Experiment from azureml.core import Environment from azureml.core import ScriptRunConfig if __name__ == "__main__": #Connect to Azure ML WorkSpace ws = Workspace.from_config(path='./.azureml', _file_name='config.json') #Experiment experiment = Experiment(workspace=ws, name='day2-experiment-train-model') config = ScriptRunConfig(source_directory='./src', script='model.py', compute_target='cpu-cluster') # set up pytorch environment env = Environment.from_conda_specification( name='sklearn-aml-env', file_path='./.azureml/sklearn-env-aml.yml') config.run_config.environment = env #Execute experiment run = experiment.submit(config) #Print url aml_url = run.get_portal_url() print(aml_url)
classification_data = PipelineData('classification_data', datastore=def_blob_store, output_name='classification_data', is_directory=True) compute_target = ws.compute_targets['cpu-cluster'] environment_variables = { 'POSTGRES_PASSWORD': os.environ['POSTGRES_PASSWORD'], 'POSTGRES_HOSTNAME': 'ackbar-postgres.postgres.database.azure.com', 'AZURE_STORAGE_CONNECTION_STRING': os.environ['AZURE_STORAGE_CONNECTION_STRING'] } env = Environment(name='env', environment_variables=environment_variables) conda = CondaDependencies() conda.add_conda_package('psycopg2') conda.add_conda_package('numpy') conda.add_conda_package('Pillow') # have to use pip to install azure packages... conda.add_pip_package('azure-storage-blob') env.python.conda_dependencies = conda run_config = RunConfiguration() run_config.environment = env PROJECT = 'caltech' prepare_step = PythonScriptStep( script_name='prepare.py', arguments=['--output', batch_input, '--project', PROJECT],
print("get datasets from datastore") input_data_paths = [(blob_datastore, 'mldata')] input_dataset = Dataset.File.from_files(path=input_data_paths) # ----PYTHON ENV------ #------------------------- packages = CondaDependencies.create( conda_packages=["cudatoolkit=10.0"], pip_packages=[ 'azureml-sdk', 'PyYAML', 'azure-storage-blob', 'matplotlib', 'seaborn', 'tensorflow', 'Keras', 'tensorflow-hub', 'joblib', 'tqdm', 'Pillow', 'azureml-dataprep[pandas,fuse]>=1.1.14' ]) diagnoz_env = Environment("diagnoz-pipeline-env") diagnoz_env.python.user_managed_dependencies = False # Let Azure ML manage dependencies diagnoz_env.docker.enabled = True # Use a docker container diagnoz_env.docker.base_image = DEFAULT_GPU_IMAGE diagnoz_env.python.conda_dependencies = packages diagnoz_env.register(workspace=ws) # Runconfigs pipeline_run_config = RunConfiguration() pipeline_run_config.target = compute_target pipeline_run_config.environment = diagnoz_env print("Run configuration created.") shutil.rmtree(script_folder, ignore_errors=True) os.makedirs(script_folder, exist_ok=True)
from azureml.core import Environment, Experiment, ScriptRunConfig, Workspace ws = Workspace.from_config() experiment = Experiment(workspace=ws, name="hello-world-experiment") # Setup for a local environment execution: myenv = Environment("user-managed-env") myenv.python.user_managed_dependencies = True # Execute the experiment: config = ScriptRunConfig( source_directory="./src", script="hello.py", compute_target="local", environment=myenv, ) # Store experiment results: run = experiment.submit(config) aml_url = run.get_portal_url() print("You can access experiment results at:") print(aml_url)
# 3. Training Step # 4. Model Registration Step # 5. Pipeline registration # 6. Submit the pipeline for execution ## Pipeline Parameters ## # We need to tell the Pipeline what it needs to learn to see! datapath = DataPath(datastore=datastore, path_on_datastore=datastorepath) data_path_pipeline_param = (PipelineParameter(name="data", default_value=datapath), DataPathComputeBinding(mode='mount')) # Configuration for data prep and training steps # dataprepEnvironment = Environment.from_pip_requirements( 'dataprepenv', 'requirements-dataprepandtraining.txt') dataprepRunConfig = RunConfiguration() dataprepRunConfig.environment = dataprepEnvironment ## Data Process Step ## # parse.py file parses the images in our data source # seer_tfrecords = PipelineData("tfrecords_set", datastore=datastore, is_directory=True) prepStep = PythonScriptStep('parse.py', source_directory='.', name='Data Preparation', compute_target=compute, arguments=[
from azureml.core import Workspace, Experiment, Environment, ScriptRunConfig from azureml.core.conda_dependencies import CondaDependencies """ $ python -m ml_service.run_local_compute """ ws = Workspace.from_config() environment = Environment(name='mylocal_env') environment.python.user_managed_dependencies = True environment.python.conda_dependencies = CondaDependencies(conda_dependencies_file_path="./environment_setup/conda_dependencies.yml") config = ScriptRunConfig(source_directory='src/steps', script='01_prep_data.py', compute_target='local', # or 'cpu-cluster' arguments=[ '--data_X', 'outputs/diabetes_X2.csv', '--data_y', 'outputs/diabetes_y2.csv' ], environment=environment) exp = Experiment(workspace=ws, name='mylocal_exp') run = exp.submit(config) aml_url = run.get_portal_url() print(aml_url)
def create_inference_config(tmp_dir, model_name, model_version, service_name): """ Create the InferenceConfig object which will be used to deploy. :param tmp_dir: :type tmp_dir: :param model_name: :type model_name: :param model_version: :type model_version: :param service_name: :type service_name: :return: :rtype: """ absolute_model_path = _download_artifact_from_uri('models:/{}/{}'.format(model_name, model_version)) model_folder = absolute_model_path.split(os.path.sep)[-1] model_directory_path = tmp_dir.path("model") tmp_model_path = os.path.join( model_directory_path, _copy_file_or_tree(src=absolute_model_path, dst=model_directory_path), ) # Create environment env_name = service_name + "-env" env_name = env_name[:32] mlflow_model = Model.load(os.path.join(absolute_model_path, MLMODEL_FILE_NAME)) model_pyfunc_conf = load_pyfunc_conf(mlflow_model) if pyfunc.ENV in model_pyfunc_conf: environment = AzureEnvironment.from_conda_specification( env_name, os.path.join(tmp_model_path, model_pyfunc_conf[pyfunc.ENV]) ) else: raise MlflowException('Error, no environment information provided with model') sample_input_df = None sample_output_df = None # Leaving this here, commented out for now. The issue is that our swagger handling doesn't work with OpenAPI 3. # This runs into issues because a pandas dataframe in a split orient (the default) can have arrays of mixed # types, which isn't supported in OpenAPI 2. So for now, we will only use the empty signature to generate # swagger, and when we've updated our swagger handling to support OpenAPI 3 we can add this back in. """ if mlflow_model.saved_input_example_info: sample_input_file_path = os.path.join(absolute_model_path, mlflow_model.saved_input_example_info['artifact_path']) with open(sample_input_file_path, 'r') as sample_input_file: if mlflow_model.saved_input_example_info['type'] == 'dataframe': sample_input_df = pandas.read_json(sample_input_file, orient=mlflow_model.saved_input_example_info['pandas_orient']) else: raise MlflowException('Sample model input must be of type "dataframe"') """ if mlflow_model.signature: if mlflow_model.signature.inputs and sample_input_df is None: # 'is None' check is necessary because dataframes don't like being used as truth values columns = mlflow_model.signature.inputs.column_names() types = mlflow_model.signature.inputs.pandas_types() schema = {} for c, t in zip(columns, types): schema[c] = t df = pandas.DataFrame(columns=columns) sample_input_df = df.astype(dtype=schema) if mlflow_model.signature.outputs and sample_output_df is None: columns = mlflow_model.signature.outputs.column_names() types = mlflow_model.signature.outputs.pandas_types() schema = {} for c, t in zip(columns, types): schema[c] = t df = pandas.DataFrame(columns=columns) sample_output_df = df.astype(dtype=schema) # Create execution script execution_script_path = tmp_dir.path("execution_script.py") create_execution_script(execution_script_path, model_folder, sample_input_df, sample_output_df) # Add inference dependencies environment.python.conda_dependencies.add_pip_package("mlflow=={}".format(mlflow_version)) environment.python.conda_dependencies.add_pip_package("inference-schema>=1.2.0") environment.python.conda_dependencies.add_pip_package("azureml-model-management-sdk==1.0.1b6.post1") environment.python.conda_dependencies.add_pip_package("flask==1.0.3") environment.python.conda_dependencies.add_pip_package("gunicorn==19.9.0") environment.python.conda_dependencies.add_pip_package("applicationinsights>=0.11.7") environment.python.conda_dependencies.add_pip_package("werkzeug>=0.16.1,<=1.0.1") # Create InferenceConfig inference_config = InferenceConfig(entry_script=execution_script_path, environment=environment) return inference_config