def create_resnet_image_config(conda_file="img_env.yml",
                               execution_script="driver.py"):
    """

    :param conda_file:
    :param execution_script:
    :return:
    """
    conda_pack = ["tensorflow-gpu==1.14.0"]
    requirements = [
        "keras==2.2.0", "Pillow==5.2.0", "azureml-defaults",
        "azureml-contrib-services", "toolz==0.9.0"
    ]
    imgenv = CondaDependencies.create(conda_packages=conda_pack,
                                      pip_packages=requirements)
    with open("img_env.yml", "w") as file:
        file.write(imgenv.serialize_to_string())

    description = "Image for AKS Deployment Tutorial"
    dependencies = ["resnet152.py"]
    tags = {"name": "AKS", "project": "AML"}
    return ContainerImage.image_configuration(
        execution_script=execution_script,
        runtime="python",
        conda_file=conda_file,
        description=description,
        tags=tags,
        dependencies=dependencies,
        enable_gpu=True)
Esempio n. 2
0
def build_container():

    cd = CondaDependencies.create(pip_packages=[
        'azureml-sdk==1.0.39', 'scikit-learn==0.21.1', 'joblib==0.13.2'
    ])

    cd.save_to_file(base_directory='./', conda_file_path='myenv.yml')

    model = get_best_model(model_name)
    print('model', model)

    img_config = ContainerImage.image_configuration(
        execution_script='score.py',
        runtime='python',
        conda_file='myenv.yml',
        dependencies=['.'])

    image_name = model_name.replace("_", "").lower()

    print("Image name:", image_name)

    image = Image.create(name=image_name,
                         models=[model],
                         image_config=img_config,
                         workspace=ws)

    image.wait_for_creation(show_output=True)

    if image.creation_state != 'Succeeded':
        raise Exception('Image creation status: {image.creation_state}')

    print('{}(v.{} [{}]) stored at {} with build log {}'.format(
        image.name, image.version, image.creation_state, image.image_location,
        image.image_build_log_uri))
    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():
    # get workspace
    ws = load_workspace()
    model = Model.register(ws,
                           model_name='pytorch_mnist',
                           model_path='model.pth')

    # create dep file
    myenv = CondaDependencies()
    myenv.add_pip_package('numpy')
    myenv.add_pip_package('torch')
    with open('pytorchmnist.yml', 'w') as f:
        print('Writing out {}'.format('pytorchmnist.yml'))
        f.write(myenv.serialize_to_string())
        print('Done!')

    # create image
    image_config = ContainerImage.image_configuration(
        execution_script="score.py",
        runtime="python",
        conda_file="pytorchmnist.yml",
        dependencies=['./models.py'])

    image = Image.create(ws, 'pytorchmnist', [model], image_config)
    image.wait_for_creation(show_output=True)

    # create service
    aciconfig = AciWebservice.deploy_configuration(
        cpu_cores=1, memory_gb=1, description='simple MNIST digit detection')
    service = Webservice.deploy_from_image(workspace=ws,
                                           image=image,
                                           name='pytorchmnist-svc',
                                           deployment_config=aciconfig)
    service.wait_for_deployment(show_output=True)
Esempio n. 5
0
def run():
    print("entered run")
    variables_received = "sub_id: {}, rg: {}, work_name: {}, state: {}, author: {}, model_name: {}" \
                            .format(resolve_sub_id(),
                                    resolve_rg(),
                                    resolve_workspace_name(),
                                    resolve_state(),
                                    resolve_author(),
                                    resolve_model_name())
    print(variables_received)

    az_ws = Workspace(resolve_sub_id(), resolve_rg(), resolve_workspace_name())
    print("initialized workspace")
    #Get & Download model
    model = Model(az_ws,
                  name=resolve_model_name(),
                  tags={
                      "state": resolve_state(),
                      "created_by": resolve_author()
                  })
    print("initialized model")
    model.download(target_dir="./assets/")
    print("downloaded model assets")
    #TODO: remove workaround for ml sdk dropping assets into /assets/dacrook folder when files dropped to consistent location
    for dir_p, _, f_n in walk("./assets"):
        for f in f_n:
            abs_path = os.path.abspath(os.path.join(dir_p, f))
            shutil.move(abs_path, "./assets/" + f)

    #Configure Image
    my_env = CondaDependencies.create(conda_packages=["numpy", "scikit-learn"])
    with open("myenv.yml", "w") as f:
        f.write(my_env.serialize_to_string())
    image_config = ContainerImage.image_configuration(
        execution_script="score.py",
        runtime="python",
        conda_file="myenv.yml",
        dependencies=["assets", "inference_code"],
        tags={
            "state": resolve_state(),
            "created_by": resolve_author()
        })
    print("configured image")
    #TODO: use this once model is dropped to a consistent location
    #    image = Image.create(workspace = az_ws, name=resolve_image_name(), models=[model], image_config = image_config)
    image = Image.create(workspace=az_ws,
                         name=resolve_image_name(),
                         models=[model],
                         image_config=image_config)
    image.wait_for_creation()
    print("created image")
    if (image.creation_state != "Succeeded"):
        raise Exception("Failed to create image.")
    print("image location: {}".format(image.image_location))
    artifacts = {"image_location": image.image_location}
    if (not os.path.exists("/artifacts/")):
        os.makedirs("/artifacts/")
    with open("/artifacts/artifacts.json", "w") as outjson:
        json.dump(artifacts, outjson)
Esempio n. 6
0
def create_image_config(script_name, conda_env):

    image_config = ContainerImage.image_configuration(
        execution_script=script_name,
        runtime="python",
        dependencies=["./"],
        conda_file=conda_env)
    return image_config
Esempio n. 7
0
def container_img(ws, model, score_script, env_file):
    image_config = ContainerImage.image_configuration(
        execution_script=score_script, runtime="python", conda_file=env_file)
    image = Image.create(name="TeamOmega",
                         models=[model],
                         image_config=image_config,
                         workspace=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)
Esempio n. 9
0
def deployModelAsWebService(
        ws,
        model_folder_path="models",
        model_name="component_compliance",
        scoring_script_filename="scoring_service.py",
        conda_packages=['numpy', 'pandas'],
        pip_packages=['azureml-sdk', 'onnxruntime'],
        conda_file="dependencies.yml",
        runtime="python",
        cpu_cores=1,
        memory_gb=1,
        tags={'name': 'scoring'},
        description='Compliance classification web service.',
        service_name="complianceservice"):
    # notice for the model_path, we supply the name of the outputs folder without a trailing slash
    # this will ensure both the model and the customestimators get uploaded.
    print("Registering and uploading model...")
    registered_model = Model.register(model_path=model_folder_path,
                                      model_name=model_name,
                                      workspace=ws)

    # create a Conda dependencies environment file
    print("Creating conda dependencies file locally...")
    from azureml.core.conda_dependencies import CondaDependencies
    mycondaenv = CondaDependencies.create(conda_packages=conda_packages,
                                          pip_packages=pip_packages)
    with open(conda_file, "w") as f:
        f.write(mycondaenv.serialize_to_string())

    # create container image configuration
    print("Creating container image configuration...")
    from azureml.core.image import ContainerImage
    image_config = ContainerImage.image_configuration(
        execution_script=scoring_script_filename,
        runtime=runtime,
        conda_file=conda_file)

    # create ACI configuration
    print("Creating ACI configuration...")
    from azureml.core.webservice import AciWebservice, Webservice
    aci_config = AciWebservice.deploy_configuration(cpu_cores=cpu_cores,
                                                    memory_gb=memory_gb,
                                                    tags=tags,
                                                    description=description)

    # deploy the webservice to ACI
    print("Deploying webservice to ACI...")
    webservice = Webservice.deploy_from_model(workspace=ws,
                                              name=service_name,
                                              deployment_config=aci_config,
                                              models=[registered_model],
                                              image_config=image_config)
    webservice.wait_for_deployment(show_output=True)

    return webservice
Esempio n. 10
0
def run(model_path, model_name):
    auth_args = {
        'tenant_id': os.environ['TENANT_ID'],
        'service_principal_id': os.environ['SERVICE_PRINCIPAL_ID'],
        'service_principal_password': os.environ['SERVICE_PRINCIPAL_PASSWORD']
    }

    ws_args = {
        'auth': ServicePrincipalAuthentication(**auth_args),
        'subscription_id': os.environ['SUBSCRIPTION_ID'],
        'resource_group': os.environ['RESOURCE_GROUP']
    }

    ws = Workspace.get(os.environ['WORKSPACE_NAME'], **ws_args)

    print(ws.get_details())

    print('\nSaving model {} to {}'.format(model_path, model_name))
    model = Model.register(ws, model_name=model_name, model_path=model_path)
    print('Done!')

    print('Checking for existing service {}'.format(model_name))
    service_name = 'simplemnist-svc'
    if model_name in ws.webservices:
        print('Found it!\nRemoving Existing service...')
        ws.webservices[model_name].delete()
        print('Done!')
    else:
        print('Not found, creating new one!')

    # image configuration
    image_config = ContainerImage.image_configuration(
        execution_script="score.py",
        runtime="python",
        conda_file="environment.yml")

    # deployement configuration
    aciconfig = AciWebservice.deploy_configuration(cpu_cores=1,
                                                   memory_gb=1,
                                                   description=model_name)

    # deploy
    service = Webservice.deploy_from_model(workspace=ws,
                                           name=model_name,
                                           models=[model],
                                           image_config=image_config,
                                           deployment_config=aciconfig)

    service.wait_for_deployment(show_output=True)

    #print logs
    print(service.get_logs())

    print('Done!')
Esempio n. 11
0
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
Esempio n. 12
0
def deploy(aciconfig, envfile, name, model):
    # configure the image
    image_config = ContainerImage.image_configuration(
        execution_script="./score.py", runtime="python", conda_file=envfile)

    service = Webservice.deploy_from_model(workspace=ws,
                                           name=name,
                                           deployment_config=aciconfig,
                                           models=[model],
                                           image_config=image_config)

    service.wait_for_deployment(show_output=True)

    print(service.scoring_uri)
Esempio n. 13
0
def create_lightgbm_image_config(
    conda_file="lgbmenv.yml", execution_script="score.py", dependencies=None
) -> ContainerImageConfig:
    """
    Image Configuration for running LightGBM in Azure Machine Learning Workspace

    :param conda_file: file name of LightGBM Conda Env File. This file is created if it does not exist.
     default: lgbmenv.yml
    :param execution_script: webservice file. default: score.py
    :param dependencies: Files required for image.
    :return: new image configuration for Machine Learning Workspace
    """
    create_lightgbm_conda_file(conda_file)

    dockerfile = "dockerfile"
    with open(dockerfile, "w") as file:
        file.write(
            "RUN apt update -y && apt upgrade -y && apt install -y build-essential"
        )

    with open("score.py", "w") as file:
        file.write(
            """        
import json
import logging


def init():
    logger = logging.getLogger("scoring_script")
    logger.info("init")


def run():
    logger = logging.getLogger("scoring_script")
    logger.info("run")
    return json.dumps({'call': True})
"""
        )
    description = "Image with lightgbm model"
    tags = {"area": "text", "type": "lightgbm"}
    return ContainerImage.image_configuration(
        execution_script=execution_script,
        runtime="python",
        conda_file=conda_file,
        description=description,
        dependencies=dependencies,
        docker_file=dockerfile,
        tags=tags,
    )
Esempio n. 14
0
def deployWebservice(ws, args, folders):
    # this section requries that the processing is done in the directory where the execution script and the conda_file resides
    os.chdir(folders.script_folder)
    model = Model(ws, args.modelName)
    aciconfig = AciWebservice.deploy_configuration(cpu_cores=args.cpuCores,
                                                   memory_gb=args.memoryGB)
    # configure the image
    image_config = ContainerImage.image_configuration(
        execution_script=args.scoringScript,
        runtime="python",
        conda_file=args.environmentFileName)
    service = Webservice.deploy_from_model(workspace=ws,
                                           name=args.webserviceName,
                                           deployment_config=aciconfig,
                                           models=[model],
                                           image_config=image_config)
    service.wait_for_deployment(show_output=True)
    return service.scoring_uri
Esempio n. 15
0
def deploy_service(execution_script,
                   conda_file,
                   aciconfig,
                   service_name,
                   model,
                   workspace,
                   runtime="python"):

    image_config = ContainerImage.image_configuration(
        execution_script=execution_script,
        runtime=runtime,
        conda_file=conda_file)

    service = Webservice.deploy_from_model(workspace=workspace,
                                           name=service_name,
                                           deployment_config=aciconfig,
                                           models=[model],
                                           image_config=image_config)
    service.wait_for_deployment(show_output=True)
    print(service.scoring_uri)
    return service
Esempio n. 16
0
def build_image():
    """Build the docker image to hold the model."""
    load_dotenv(find_dotenv())

    chdir("deploy")
    ws = Workspace(
        workspace_name=getenv("AML_WORKSPACE_NAME"),
        subscription_id=getenv("AML_SUBSCRIPTION_ID"),
        resource_group=getenv("AML_RESOURCE_GROUP"),
    )
    model = Model(ws, getenv("AML_MODEL_NAME"))

    image_config = ContainerImage.image_configuration(
        runtime="python",
        execution_script="score.py",
        conda_file="container_conda_env.yml")

    image = Image.create(name=getenv("AML_IMAGE_NAME"),
                         models=[model],
                         image_config=image_config,
                         workspace=ws)

    image.wait_for_creation(show_output=True)
Esempio n. 17
0
def amls_model_to_image(amls_config, workspace, model):
    """
    Deploy a published AMLS model as docker image in AMLS' ACR.

    :param amls_config:
    :param workspace:
    :param model:
    :return:
    """

    script = "score.py"
    conda_file = "conda_dependencies.yml"
    save_conda_dependencies(amls_config, conda_file)
    if amls_config['docker_file']:
        docker_file = amls_config['docker_file']
    else:
        docker_file = None

    image_config = ContainerImage.image_configuration(
        runtime="python",
        execution_script=script,
        conda_file=conda_file,
        tags=amls_config['tags'],
        description=amls_config['description'],
        docker_file=docker_file)
    logger.info(f"Deploying image.")
    image = Image.create(
        name='image',
        # this is the model object
        models=[model],
        image_config=image_config,
        workspace=workspace)
    image.wait_for_creation(show_output=True)
    image.update_creation_state()

    return image
Esempio n. 18
0
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
Esempio n. 19
0
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)
Esempio n. 20
0
model_name = 'prednet_UCSDped1'  #config['model_name']
model_version = 2  # config['model_version']

cd = CondaDependencies.create(pip_packages=[
    'keras==2.0.8', 'theano', 'tensorflow==1.8.0', 'matplotlib', 'hickle',
    'pandas', 'azureml-sdk'
])

cd.save_to_file(base_directory='./', conda_file_path='myenv.yml')

model = Model(ws, name=model_name, version=model_version)

img_config = ContainerImage.image_configuration(
    execution_script="score.py",
    runtime="python",
    conda_file="myenv.yml",
    dependencies=['prednet.py', 'keras_utils.py', 'aml_config/model.json'])

image_name = model_name.replace("_", "").lower()

print("Image name:", image_name)

image = Image.create(name=image_name,
                     models=[model],
                     image_config=img_config,
                     workspace=ws)

image.wait_for_creation(show_output=True)

if image.creation_state != 'Succeeded':
Esempio n. 21
0
    sys.exit(0)

model_name = config['model_name']
model_version = config['model_version']


model_list = Model.list(workspace=ws)
model, = (m for m in model_list if m.version==model_version and m.name==model_name)
print('Model picked: {} \nModel Description: {} \nModel Version: {}'.format(model.name, model.description, model.version))

os.chdir('./CICD/code/scoring')
image_name = "predmaintenance-model-score"

image_config = ContainerImage.image_configuration(execution_script = "score.py",
                                                  runtime = "python-slim",
                                                  conda_file = "conda_dependencies.yml",
                                                  description = "Image with predictive maintenance model",
                                                  tags = {'area': "diabetes", 'type': "regression"}
                                                 )

image = Image.create(name = image_name,
                     models = [model],
                     image_config = image_config,
                     workspace = ws)

image.wait_for_creation(show_output = True)
os.chdir('../../../')

if image.creation_state != 'Succeeded':
  raise Exception('Image creation status: {image.creation_state}')

print('{}(v.{} [{}]) stored at {} with build log {}'.format(image.name, image.version, image.creation_state, image.image_location, image.image_build_log_uri))
Esempio n. 22
0
myaci_config = AciWebservice.deploy_configuration(
    cpu_cores=2,
    memory_gb=2,
    tags={'name': 'Databricks Azure ML ACI'},
    description='SMS Spam Classifier')

# COMMAND ----------

service_name = "smsspam"
runtime = "spark-py"
driver_file = "score_sparkml.py"
my_conda_file = "mydeployenv.yml"

# image creation
from azureml.core.image import ContainerImage
myimage_config = ContainerImage.image_configuration(
    execution_script=driver_file, runtime=runtime, conda_file=my_conda_file)

# COMMAND ----------

# Webservice creation
myservice = Webservice.deploy_from_model(workspace=ws,
                                         name=service_name,
                                         deployment_config=myaci_config,
                                         models=[mymodel],
                                         image_config=myimage_config)

myservice.wait_for_deployment(show_output=True)

# COMMAND ----------

#for using the Web HTTP API
Esempio n. 23
0
#Set the image
aciconfig = AciWebservice.deploy_configuration(cpu_cores=CPU_CORES,
                                               memory_gb=MEMORY_GB,
                                               description=SERVICE_DESCRIPTION,
                                               auth_enabled=AUTH_ENABLED)

if CONDA_FILE_URL == '' and DOCKER_FILE_URL == '':
    from azureml.core.conda_dependencies import CondaDependencies
    myenv = CondaDependencies()
    myenv.add_conda_package("scikit-learn")
    #myenv.add_pip_package("joblib")
    with open("myenv.yml", "w") as f:
        f.write(myenv.serialize_to_string())
    # configure the image
    image_config = ContainerImage.image_configuration(
        execution_script=EXECUTION_SCRIPT_PATH,
        runtime="python",
        conda_file="myenv.yml")
elif CONDA_FILE_URL is not '' and DOCKER_FILE_URL == '':
    wget.download(CONDA_FILE_URL, CONDA_FILE_PATH)
    image_config = ContainerImage.image_configuration(
        execution_script=EXECUTION_SCRIPT_PATH,
        runtime="python",
        conda_file=CONDA_FILE_PATH)
elif DOCKER_FILE_URL is not '':
    wget.download(DOCKER_FILE_URL, DOCKER_FILE_PATH)
    image_config = ContainerImage.image_configuration(
        execution_script=EXECUTION_SCRIPT_PATH,
        runtime="python",
        docker_file=DOCKER_FILE_PATH)

#Deploy the service
Esempio n. 24
0
args = parser.parse_args()

model = Model(ws, name=e.model_name, version=e.model_version)
sources_dir = e.sources_directory_train
if (sources_dir is None):
    sources_dir = 'diabetes_regression'
path_to_scoring = os.path.join(".", sources_dir, "scoring")
cwd = os.getcwd()
# Copy conda_dependencies.yml into scoring as this method does not accept relative paths. # NOQA: E501
shutil.copy(os.path.join(".", sources_dir, "conda_dependencies.yml"),
            path_to_scoring)
os.chdir(path_to_scoring)
image_config = ContainerImage.image_configuration(
    execution_script=e.score_script,
    runtime="python",
    conda_file="conda_dependencies.yml",
    description="Image with ridge regression model",
    tags={"area": "diabetes_regression"},
)

image = Image.create(name=e.image_name,
                     models=[model],
                     image_config=image_config,
                     workspace=ws)

os.chdir(cwd)

image.wait_for_creation(show_output=True)

if image.creation_state != "Succeeded":
    raise Exception("Image creation status: {image.creation_state}")
Esempio n. 25
0
# create a Conda dependencies environment file
print("Creating conda dependencies file locally...")
conda_packages = ['numpy']
pip_packages = [
    'tensorflow==2.0.0', 'keras==2.3.1', 'azureml-sdk', 'azureml-monitoring'
]
mycondaenv = CondaDependencies.create(conda_packages=conda_packages,
                                      pip_packages=pip_packages)

conda_file = 'scoring_dependencies.yml'
with open(conda_file, 'w') as f:
    f.write(mycondaenv.serialize_to_string())

# create container image configuration
print("Creating container image configuration...")
image_config = ContainerImage.image_configuration(
    execution_script='score_fixed.py', runtime='python', conda_file=conda_file)

print("Creating image...")
image = Image.create(name=args.image_name,
                     models=[latest_model],
                     image_config=image_config,
                     workspace=ws)

# wait for image creation to finish
image.wait_for_creation(show_output=True)

eval_info["image_id"] = image.id

with open(eval_filepath, "w") as f:
    json.dump(eval_info, f)
    print('eval_info.json saved')

ta_env = CondaDependencies()
ta_env.add_conda_package("scikit-learn")
ta_env.add_conda_package("dill")
 
with open("ta_env.yml","w") as f:
    f.write(ta_env.serialize_to_string())
with open("ta_env.yml","r") as f:
    print(f.read())                       


%%time
 
image_config = ContainerImage.image_configuration(execution_script="score.py", 
                                                  runtime="python", 
                                                  conda_file="ta_env.yml")    


aciconfig = AciWebservice.deploy_configuration(cpu_cores=1, 
                                               memory_gb=1, 
                                               tags={"data": "textanalytics",  "method" : "textblob"}, 
                                               description='Predict Sentiment of Sentences')
 
service = Webservice.deploy_from_model(workspace=ws,
                                       name='ta-textblob',
                                       deployment_config=aciconfig,
                                       models=[model],
                                       image_config=image_config)
 
service.wait_for_deployment(show_output=True)
Esempio n. 27
0
else:

    # Create the container image if it's not there
    if not containerImage:
        print("Creating container image")

        # Unlike the InferenceConfig from the ACI script, you MUST be in the same directory
        # 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)
aciconfig = AciWebservice.deploy_configuration(
    cpu_cores=1,
    memory_gb=1,
    tags={
        "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',
Esempio n. 29
0
model_version = config["model_version"]

model_list = Model.list(workspace=ws)
model, = (m for m in model_list
          if m.version == model_version and m.name == model_name)
print("Model picked: {} \nModel Description: {} \nModel Version: {}".format(
    model.name, model.description, model.version))

os.chdir("./scripts/scoring")
image_name = "arima-forecast-score"

image_config = ContainerImage.image_configuration(
    execution_script="score.py",
    runtime="python-slim",
    conda_file="conda_dependencies.yml",
    description="Image with robberies arima forecasting model",
    tags={
        "area": "robberies",
        "type": "forecasting"
    },
)

image = Image.create(name=image_name,
                     models=[model],
                     image_config=image_config,
                     workspace=ws)

image.wait_for_creation(show_output=True)
os.chdir("../..")

if image.creation_state != "Succeeded":
    raise Exception("Image creation status: {image.creation_state}")
Esempio n. 30
0
model_version = config["model_version"]

model_list = Model.list(workspace=ws)
model, = (m for m in model_list
          if m.version == model_version and m.name == model_name)
print("Model picked: {} \nModel Description: {} \nModel Version: {}".format(
    model.name, model.description, model.version))

os.chdir("./code/scoring")
image_name = "diabetes-model-score"

image_config = ContainerImage.image_configuration(
    execution_script="score.py",
    runtime="python-slim",
    conda_file="conda_dependencies.yml",
    description="Image with ridge regression model",
    tags={
        "area": "diabetes",
        "type": "regression"
    },
)

image = Image.create(name=image_name,
                     models=[model],
                     image_config=image_config,
                     workspace=ws)

image.wait_for_creation(show_output=True)
os.chdir("../..")

if image.creation_state != "Succeeded":
    raise Exception("Image creation status: {image.creation_state}")