def create_or_get_image(self): image_params = self.__config['image'] images = ContainerImage.list(self.__ws, image_name=image_config['name']) image = images.find_by_property('version', image_config['version']) if image: return image image_config = ContainerImage.image_configuration( execution_script="score.py", runtime="python", conda_file="config_deploy/myenv.yml", docker_file="config_deploy/Dockerfile", enable_gpu=True, dependencies=[ 'generation', 'config.py', 'skipthoughts_vectors', 'generate.py', 'preprocessing/text_moderator.py' ]) image = ContainerImage.create(name=image_params['name'], models=[], image_config=image_config, workspace=self.__ws) image.wait_for_creation(show_output=True) return image
def main(): ws = Workspace.from_config() conda = CondaDependencies() conda.add_conda_package("python==3.5") conda.add_pip_package("h5py==2.8.0") conda.add_pip_package("html5lib==1.0.1") conda.add_pip_package("keras==2.2.0") conda.add_pip_package("Keras-Applications==1.0.2") conda.add_pip_package("Keras-Preprocessing==1.0.1") conda.add_pip_package("matplotlib==2.2.2") conda.add_pip_package("numpy==1.14.5") conda.add_pip_package("opencv-python==3.3.0.9") conda.add_pip_package("pandas==0.23.3") conda.add_pip_package("Pillow==5.2.0") conda.add_pip_package("requests==2.19.1") conda.add_pip_package("scikit-image==0.14.0") conda.add_pip_package("scikit-learn==0.19.2") conda.add_pip_package("scipy==1.1.0") conda.add_pip_package("sklearn==0.0") conda.add_pip_package("tensorflow==1.9.0") conda.add_pip_package("urllib3==1.23") conda.add_pip_package("azureml-sdk") with open("environment.yml", "w") as f: f.write(conda.serialize_to_string()) with open("environment.yml", "r") as f: print(f.read()) image_config = ContainerImage.image_configuration( execution_script="score.py", runtime="python", conda_file="environment.yml", docker_file="Dockerfile", dependencies=DEPENDENCIES) webservices = ws.webservices(compute_type='ACI') image = ContainerImage.create(name="ai-bootcamp", models=[], image_config=image_config, workspace=ws) image.wait_for_creation(show_output=True) webservices_list = [] for key in webservices: webservices_list.append(key) service_name = webservices_list[0] aciwebservice = AciWebservice(ws, service_name) aciwebservice.update(image=image)
def update_service(service, models, image_config, ws): image = ContainerImage.create(name=service.name, models=models, image_config=image_config, workspace=ws) image.wait_for_creation() print("Created Image: ", image) service.update(image=image) service.wait_for_deployment(show_output=True) return service
def createImage(workspace, scoring_file, model, image_name): ''' TODO: We should probably allow the conda_pack/requirements to be identified so we can switch between CPU/GPU NOTE: This function doesn't check for the existence of an image because new builds will just create a new version on a container. If the caller doesn't want duplicates, they need to ensure that one does not exist already. Creates a new Docker Container image and uploads it to the associated ACR with the workspace. PARAMS: workspace : azureml.core.Workspace : Existing AMLS Workspace scoring_file : String : Name/path of local .py file that has an init() and run() function defined. model : azureml.core.Model : Registered AMLS model image_name : String : Name of the container to be created. RETURNS: azureml.core.image.ContainerImage ''' conda_pack = [] requirements = ["azureml-defaults==1.0.57", "azureml-contrib-services"] print("Creating image...") simple_environment = CondaDependencies.create(conda_packages=conda_pack, pip_packages=requirements) with open("simple.yml", "w") as f: f.write(simple_environment.serialize_to_string()) image_config = ContainerImage.image_configuration( execution_script=scoring_file, runtime="python", conda_file="simple.yml", description="Image with dummy (unused) model", tags={"type": "noop"}, dependencies=[]) image = ContainerImage.create( name=image_name, models=[model], image_config=image_config, workspace=workspace, ) image.wait_for_creation(show_output=True) print("Image created IMAGE/VERSION: ", image.name, '/', image.version) return image
def get_or_create_image( image_config, image_settings_name, show_output, models=None, configuration_file: str = project_configuration_file, ): """ :param image_config: :param image_settings_name: :param models: :param show_output: :param configuration_file: path to project configuration file. default: project.yml :return: """ if not models: models = [] project_configuration = ProjectConfiguration(configuration_file) assert project_configuration.has_value(image_settings_name) image_name = project_configuration.get_value(image_settings_name) workspace = get_or_create_workspace_from_project( project_configuration, show_output=show_output ) workspace_images = workspace.images if ( image_name in workspace_images and workspace_images[image_name].creation_state != "Failed" ): return workspace_images[image_name] image_create_start = time.time() image = ContainerImage.create( name=image_name, models=models, image_config=image_config, workspace=workspace ) image.wait_for_creation(show_output=show_output) assert image.creation_state != "Failed" if show_output: print_image_deployment_info(image, image_name, image_create_start) return image
"data": "MNIST", "method": "sklearn" }, description='Predict MNIST with sklearn') # Create Docker Image from azureml.core.image import ContainerImage # configure the image image_config = ContainerImage.image_configuration( execution_script="score_mnist.py", runtime="python", conda_file=os.path.join(project_folder, "myenv.yml")) image = ContainerImage.create(name="sklearn-mnist-img", models=[model], image_config=image_config, workspace=ws) image.wait_for_creation(show_output=True) # Deploy the image in ACI print("Deploying the service as Azure Container Instance...") from azureml.core.webservice import Webservice aci_service = Webservice.deploy_from_image(workspace=ws, name='sklearn-mnist-aci-svc', image=image, deployment_config=aciconfig) aci_service.wait_for_deployment(show_output=True)
shutil.copy("./scripts/score_diabetes.py", './') # Create your new Image image_config = ContainerImage.image_configuration( execution_script="score_diabetes.py", runtime="python", conda_file=os.path.join(project_folder, "myenv.yml"), description="Image with ridge regression model", tags={ 'area': "diabetes", 'type': "regression" }) image = ContainerImage.create(name="diabetes-model", models=[model], image_config=image_config, workspace=ws) image.wait_for_creation(show_output=True) print("Provisioning an AKS cluster...") # Provision the AKS Cluster aks_name = 'myaks' try: aks_target = AksCompute(workspace=ws, name=aks_name) print('found existing Azure Kubernetes Service:', aks_target.name) except ComputeTargetException: print('creating new Azure Kubernetes Service.') # AKS configuration
from azureml.core import Workspace #Load existing workspace from the config file info. ws = Workspace.from_config() model_name = "dynocard-model" model = Model.register( model_path="model4dc.pkl", # this path points to the local file model_name=model_name, # the model gets registered as this name tags={ "data": "dynocard", "model": "classicfication" }, description="Dynocard anomaly detection model", workspace=ws) #model = Model(ws, model_name) #Image configuration image_config = ContainerImage.image_configuration( runtime="python", execution_script="score4dc.py", conda_file="myenv.yml", description="dynocard-anomaly-model-image") image = ContainerImage.create( name="dynocard-image", models=[model], # this is the model object image_config=image_config, workspace=ws)
name=service_name, workspace=ws) service.wait_for_deployment(show_output=True) print(service.state) # </option2Deploy> service.delete() # ## DEPLOY FROM IMAGE # <option3CreateImage> from azureml.core.image import ContainerImage image = ContainerImage.create( name="myimage1", models=[model], # this is the registered model object image_config=image_config, workspace=ws) image.wait_for_creation(show_output=True) # </option3CreateImage> # <option3Deploy> from azureml.core.webservice import Webservice service_name = 'aci-mnist-13' service = Webservice.deploy_from_image(deployment_config=aciconfig, image=image, name=service_name, workspace=ws) service.wait_for_deployment(show_output=True)
os.chdir(user_folder) image_name = 'taxi-duration-image-{0}'.format(user_name.replace('_', '-')) image_config = ContainerImage.image_configuration( execution_script='score_model.py', runtime='spark-py', conda_file=conda_file_path, tags={ 'dataset': 'nyc-taxi', 'creator': user_name }, description="Image to predict duration of taxi trip") image = ContainerImage.create(workspace=ws, name=image_name, models=[model], image_config=image_config) image.wait_for_creation(show_output=True) # COMMAND ---------- # MAGIC %md # MAGIC ### Deploying to Azure Container Instance (ACI) # MAGIC In this workshop, we'll be deploying to ACI. This might not be suitable for production deployments - instead you should consider deploying to [Azure Kubernetes Service (AKS)](https://docs.microsoft.com/en-us/azure/aks/) for production. # COMMAND ---------- from azureml.core.webservice import AciWebservice, Webservice service_name = 'taxi-aci-{0}'.format(user_name.replace('_', '-'))
import os, json, sys from azureml.core import Workspace from azureml.core.image import ContainerImage, Image from azureml.core.model import Model from azureml.core.authentication import AzureCliAuthentication #os.chdir("./code") cli_auth = AzureCliAuthentication() ws = Workspace.from_config(auth=cli_auth) model = Model(ws, "Model") image_config = ContainerImage.image_configuration( execution_script="score.py", runtime="python", conda_file="conda.yml", description="Predict regression", tags={ "regression": "classification", "classification": "ml" }) image = ContainerImage.create(name="model-image", models=[model], image_config=image_config, workspace=ws)
from azureml.core.conda_dependencies import CondaDependencies myenv = CondaDependencies() myenv.add_pip_package("numpy") myenv.add_pip_package("sklearn") #myenv.add_pip_package("azureml-core") with open("myenv.yml", "w") as f: f.write(myenv.serialize_to_string()) print("Created conda dependencies file.") from azureml.core.image import ContainerImage build_version = os.environ["BUILD_BUILDNUMBER"] image_config = ContainerImage.image_configuration( execution_script="score.py", runtime="python", conda_file="myenv.yml", description="A container image with breast cancer detection model", tags={"BuildVersion": build_version}) image = ContainerImage.create(name="breast-cancer-image", models=[model], image_config=image_config, workspace=ws) image.wait_for_creation(show_output=True) print(image.image_build_log_uri)
# In[91]: model=Model(ws, 'objectmodelchal5') model.download(target_dir = '.') # In[92]: # configure the image image_config = ContainerImage.image_configuration(execution_script="score.py", runtime="python", conda_file="myenv.yml") image2 = ContainerImage.create(workspace=ws, name='adventureworkimage', models=[model], image_config=image_config) # In[93]: image2.wait_for_creation(show_output=True) # In[94]: image2.creation_state
image_storage_account_name).replace( '__REPLACE_IMAGE_STORAGE_ACCOUNT_KEY__', image_storage_account_key).replace( '__REPLACE_IMAGE_STORAGE_CONTAINER_NAME__', image_storage_container_name)) image_config = ContainerImage.image_configuration( execution_script="mscore.py", runtime="python", conda_file="conda_env.yml", description=model_name, dependencies=['./', 'utils/'], enable_gpu=support_gpu) image = ContainerImage.create(name=inference_docker_image, models=[model], image_config=image_config, workspace=ws) image.wait_for_creation(show_output=True) inference_docker_image_version = image.version print('created image: {}:{}'.format(inference_docker_image, inference_docker_image_version)) # Deploy a new web service or update an existing web service with the image def deploy_new_webservice(ws, compute_name, webservice_name, image): aks_target = ws.compute_targets[compute_name] aks_config = AksWebservice.deploy_configuration(collect_model_data=True, enable_app_insights=True) service = Webservice.deploy_from_image(workspace=ws, name=webservice_name, image=image,
"data": "cats-vs-dogs", "type": "classification" }) from azureml.core.model import Model model = Model.register(model_path="export.pkl", model_name="cats_vs_dogs", tags={"key": "0.1"}, description="cats_vs_dogs", workspace=ws) # Register the image from the image configuration image = ContainerImage.create( name=experiment_name, models=[model], #this is the model object image_config=image_config, workspace=ws) image.wait_for_creation(show_output=True) from azureml.core.webservice import AciWebservice aciconfig = AciWebservice.deploy_configuration( cpu_cores=2, memory_gb=4, tags={ "data": "cats-vs-dogs", "type": "classification" }, description='Image classficiation service cats vs dogs')
# as the files being added here....paths do NOT work. print("Changing working directory....") currentDirectory = os.getcwd() os.chdir(os.path.join(currentDirectory, "model")) image_config = ContainerImage.image_configuration( execution_script="score.py", runtime="python", conda_file="myenv.yml", description="Image with ridge regression model", tags={ 'area': "diabetes", 'type': "regression" }) containerImage = ContainerImage.create( name=imageName, # this is the model object models=[model], image_config=image_config, workspace=ws) print("Wait for container image....") containerImage.wait_for_creation(show_output=True) os.chdir(currentDirectory) # Create the AKS target if it's not there. if not aks_target: print("Creating AKS target") # Use the default configuration (can also provide parameters to customize) prov_config = AksCompute.provisioning_configuration() # Create the cluster
def build_image(model_uri, workspace, image_name=None, model_name=None, mlflow_home=None, description=None, tags=None, synchronous=True): """ Register an MLflow model with Azure ML and build an Azure ML ContainerImage for deployment. The resulting image can be deployed as a web service to Azure Container Instances (ACI) or Azure Kubernetes Service (AKS). The resulting Azure ML ContainerImage 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 for which to build an 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`` For more information about supported URI schemes, see the `Artifacts Documentation <https://www.mlflow.org/docs/latest/tracking.html# supported-artifact-stores>`_. :param image_name: The name to assign the Azure Container Image that will be created. If unspecified, a unique image 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 workspace: The AzureML workspace in which to build the image. This is a `azureml.core.Workspace` object. :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 description: A string description to associate with the Azure Container Image and the Azure Model that will be created. For more information, see `<https://docs.microsoft.com/en-us/python/api/azureml-core/ azureml.core.image.container.containerimageconfig>`_ and `<https://docs.microsoft.com/en-us/python/api/azureml-core/ azureml.core.model.model?view=azure-ml-py#register>`_. :param tags: A collection of tags, represented as a dictionary of string key-value pairs, to associate with the Azure Container Image and the Azure Model that will be created. These tags will be added to a set of default tags that include the model path, the model run id (if specified), and more. For more information, see `<https://docs.microsoft.com/en-us/python/api/azureml-core/ azureml.core.image.container.containerimageconfig>`_ and `<https://docs.microsoft.com/en-us/python/api/azureml-core/ azureml.core.model.model?view=azure-ml-py#register>`_. :param synchronous: If `True`, this method will block until the image creation procedure terminates before returning. If `False`, the method will return immediately, but the returned image will not be available until the asynchronous creation process completes. The `azureml.core.Image.wait_for_creation()` function can be used to wait for the creation process to complete. :return: A tuple containing the following elements in order: - An `azureml.core.image.ContainerImage` object containing metadata for the new image. - An `azureml.core.model.Model` object containing metadata for the new model. >>> 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_okay=True) >>> >>> # Build an Azure ML Container Image for an MLflow model >>> azure_image, azure_model = mlflow.azureml.build_image( >>> model_path="<model_path>", >>> workspace=azure_workspace, >>> synchronous=True) >>> # If your image build failed, you can access build logs at the following URI: >>> print("Access the following URI for build logs: {}".format(azure_image.image_build_log_uri)) >>> >>> # Deploy the image to Azure Container Instances (ACI) for real-time serving >>> webservice_deployment_config = AciWebservice.deploy_configuration() >>> webservice = Webservice.deploy_from_image( >>> image=azure_image, workspace=azure_workspace, name="<deployment-name>") >>> webservice.wait_for_deployment() """ # 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.image import ContainerImage from azureml.core.model import Model as AzureModel absolute_model_path = _download_artifact_from_uri(model_uri) model_pyfunc_conf = _load_pyfunc_conf(model_path=absolute_model_path) model_python_version = model_pyfunc_conf.get(pyfunc.PY_VERSION, None) 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 or above! Please 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) if image_name is None: image_name = _get_mlflow_azure_resource_name() if model_name is None: model_name = _get_mlflow_azure_resource_name() 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, description=description) _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) # Azure ML copies the execution script into the image's application root directory by # prepending "/var/azureml-app" to the specified script path. The script is then executed # by referencing its path relative to the "/var/azureml-app" directory. Unfortunately, # if the script path is an absolute path, Azure ML attempts to reference it directly, # resulting in a failure. To circumvent this problem, we provide Azure ML with the relative # script path. Because the execution script was created in the current working directory, # this relative path is the script path's base name. execution_script_path = os.path.basename(execution_script_path) if mlflow_home is not None: _logger.info( "Copying the specified mlflow_home directory: `%s` to a temporary location for" " container creation", mlflow_home) mlflow_home = os.path.join( tmp.path(), _copy_project(src_path=mlflow_home, dst_path=tmp.path())) image_file_dependencies = [mlflow_home] else: image_file_dependencies = None dockerfile_path = tmp.path("Dockerfile") _create_dockerfile(output_path=dockerfile_path, mlflow_path=mlflow_home) conda_env_path = None if pyfunc.ENV in model_pyfunc_conf: conda_env_path = os.path.join(tmp_model_path, model_pyfunc_conf[pyfunc.ENV]) image_configuration = ContainerImage.image_configuration( execution_script=execution_script_path, runtime="python", docker_file=dockerfile_path, dependencies=image_file_dependencies, conda_file=conda_env_path, description=description, tags=tags, ) image = ContainerImage.create(workspace=workspace, name=image_name, image_config=image_configuration, models=[registered_model]) _logger.info( "Building an Azure Container Image with name: `%s` and version: `%s`", image.name, image.version) if synchronous: image.wait_for_creation(show_output=True) return image, registered_model
print(mymodel.name, mymodel.description, mymodel.version) # Create Image for Web Service models = [mymodel] runtime = "spark-py" conda_file = 'myenv_sparkml.yml' driver_file = "score_sparkml.py" # image creation from azureml.core.image import ContainerImage myimage_config = ContainerImage.image_configuration( execution_script=driver_file, runtime=runtime, conda_file=conda_file) image = ContainerImage.create( name=container_image_name, # this is the model object models=[mymodel], image_config=myimage_config, workspace=ws) # Wait for the create process to complete image.wait_for_creation(show_output=True) from azureml.core.compute import AksCompute, ComputeTarget # Use the default configuration (can also provide parameters to customize) prov_config = AksCompute.provisioning_configuration() # Create the cluster aks_target = ComputeTarget.create(workspace=ws, name=aks_name, provisioning_configuration=prov_config)
from azureml.core import Workspace #Load existing workspace from the config file info. ws = Workspace.from_config() from azureml.core.model import Model model = Model.register(model_path="model4dc.pkl", # this path points to the local file model_name="anomaly_detect", # the model gets registered as this name description="Dynocard anomaly detection", workspace=ws) from azureml.core.model import Model model_name = "anomaly_detect" model = Model(ws, model_name) from azureml.core.image import Image, ContainerImage #Image configuration image_config = ContainerImage.image_configuration(runtime="python", execution_script="score4dc.py", description="Dynocard anomaly detection", dependencies=["inputs.json", "model4dc.pkl", "myenv.yml", "service_schema.json", "train4dc.py"]) image = ContainerImage.create(name="iot_dynocard_demo_edgeml", models=[model], # this is the model object image_config=image_config, workspace=ws)
''' # Create container image tmp = os.getcwd() try: os.chdir(tmp + "/code") image_config = ContainerImage.image_configuration( execution_script="score_model.py" if backend == "python" else "score_R_model.py", # must be in cwd runtime="python", conda_file="conda_file.yml", docker_file="docker_file", dependencies=["init.py"] if backend == "python" else ["install_package.R", "hmsPM"]) image = ContainerImage.create(workspace=ws, name="titanic-image", models=[model], image_config=image_config) image.wait_for_creation(show_output=True) print(image.image_build_log_uri) os.chdir(tmp) except Exception as e: os.chdir(tmp) print(e) # --- Create webservice --------------------------------------------------------------------------- aci_config = AciWebservice.deploy_configuration( cpu_cores=1, memory_gb=1, tags={'sample name': 'AML 101'}, description='This is a great example.') service = Webservice.deploy_from_image(