コード例 #1
0
def lightgbm_test_image_locally(image: Image, directory: str):
    """
    Test LightGBM image Locally.

    :param image: Machine Learning Image to test.
    :param directory: root directory that contains data directory.
    """
    dupes_test = get_dupes_test(directory)
    text_to_score = dupes_test.iloc[0, 4]
    json_text = text_to_json(text_to_score)
    image.run(input_data=json_text)
コード例 #2
0
ファイル: service_demo.py プロジェクト: tklijnsma/minimal-dbe
    def create_service(self):
        iothub_compute = IotHubCompute(self.ws, self.iot_device_id)
        print('IotHubCompute:\n{0}'.format(iothub_compute))

        routes = {"route": "FROM /messages/* INTO "}

        # Here, we define the Azure ML module with the container_config options above
        aml_module = IotBaseModuleSettings(name=self.module_name,
                                           create_option=self.container_config)

        # This time, we will leave off the external module from the deployment manifest
        deploy_config = IotWebservice.deploy_configuration(
            device_id=self.iot_device_id, routes=routes, aml_module=aml_module)

        # Deploy from latest version of image, using module_name as your IotWebservice name
        iot_service_name = self.module_name

        # Can specify version=x, otherwise will grab latest
        image = Image(self.ws, self.image_name)
        print('Deploying image: {0}'.format(image))

        iot_service = IotWebservice.deploy_from_image(self.ws,
                                                      iot_service_name, image,
                                                      deploy_config,
                                                      iothub_compute)
コード例 #3
0
# MAGIC %md
# MAGIC
# MAGIC ### Option 2: Connected to a previously created image

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

import mlflow.azureml
from azureml.core import Image
from azureml.core.image import ContainerImage

#enter the image name from the Azure ML workspace
image_name = "model"

#retrieve the image configuration
model_image = Image(workspace, name=image_name)

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

# #for DEV deployments, Azure Container Instances

from azureml.core.webservice import AciWebservice, Webservice

dev_webservice_name = "skurecs-dev"
dev_webservice_deployment_config = AciWebservice.deploy_configuration()
dev_webservice = Webservice.deploy_from_image(
    name=dev_webservice_name,
    image=model_image,
    deployment_config=dev_webservice_deployment_config,
    workspace=workspace)
コード例 #4
0
latest_model_accuracy = latest_model_run.get_metrics().get("acc")
print('Latest model accuracy: ', latest_model_accuracy)

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

deploy_model = False
current_model = None

if (len(ws_list) > 0):
    webservice = ws_list[0]
    try:
        image_id = webservice.tags['image_id']
        image = Image(ws, id=image_id)
        current_model = image.models[0]
        print('Found current deployed model!')
    except:
        deploy_model = True
        print('Image id tag not found!')
else:
    deploy_model = True
    print('No deployed webservice for model: ', latest_model_name)

current_model_accuracy = -1  # undefined
if current_model != None:
    current_model_run = Run(run.experiment,
                            run_id=current_model.tags.get("run_id"))
    current_model_accuracy = current_model_run.get_metrics().get("acc")
    print('accuracies')
コード例 #5
0
args = parser.parse_args()

print("Argument 1: %s" % args.service_name)
print("Argument 2: %s" % args.aks_name)
print("Argument 3: %s" % args.aks_region)
print("Argument 4: %s" % args.description)

print('creating AzureCliAuthentication...')
cli_auth = AzureCliAuthentication()
print('done creating AzureCliAuthentication!')

print('get workspace...')
ws = Workspace.from_config(auth=cli_auth)
print('done getting workspace!')

image = Image(ws, id=image_id)
print(image)

aks_name = args.aks_name
aks_region = args.aks_region
aks_service_name = args.service_name

try:
    service = Webservice(name=aks_service_name, workspace=ws)
    print("Deleting AKS service {}".format(aks_service_name))
    service.delete()
except:
    print("No existing webservice found: ", aks_service_name)

compute_list = ws.compute_targets
aks_target = None
コード例 #6
0
with open(os.path.join("aml_service", "profiling_result.json")) as f:
    profiling_result = json.load(f)

# Get workspace
print("Loading Workspace")
cli_auth = AzureCliAuthentication()
config_file_path = os.environ.get("GITHUB_WORKSPACE", default="aml_service")
config_file_name = "aml_arm_config.json"
ws = Workspace.from_config(path=config_file_path,
                           auth=cli_auth,
                           _file_name=config_file_name)
print(ws.name, ws.resource_group, ws.location, ws.subscription_id, sep='\n')

# Loading Image
image_details = profiling_result["image_id"].split(":")
image = Image(workspace=ws, name=image_details[0], version=image_details[1])

# Deploying model on ACI
print("Deploying model on ACI")
try:
    print("Trying to update existing ACI service")
    dev_service = AciWebservice(workspace=ws, name=aci_settings["name"])
    dev_service.update(image=image,
                       tags=deployment_settings["image"]["tags"],
                       properties=deployment_settings["image"]["properties"],
                       description=deployment_settings["image"]["description"],
                       auth_enabled=aci_settings["auth_enabled"],
                       ssl_enabled=aci_settings["ssl_enabled"],
                       ssl_cert_pem_file=aci_settings["ssl_cert_pem_file"],
                       ssl_key_pem_file=aci_settings["ssl_key_pem_file"],
                       ssl_cname=aci_settings["ssl_cname"],
コード例 #7
0
    previouslyDeployedRestServiceFound = False

print('..4. completed')
print('')
print('')

print(
    '5.  Get the previously deployed model from previously deployed REST service, if any'
)
print('.............................................')
previouslyDeployedModel = None
if previouslyDeployedRestService != None:
    try:
        previouslyDeployedContainerImageId = previouslyDeployedRestService.tags[
            'image_id']
        previouslyDeployedContainerImage = Image(amlWs, id=image_id)
        previouslyDeployedModel = previouslyDeployedContainerImage.models[0]
        print(
            'Found the model of the previously deployed REST service, for the experiment!'
        )
    except:
        print(
            'No previously deployed container image not found for the experiment!'
        )
else:
    deployModelBool = True
    print('No deployed Rest service for model: ', currentlyTrainedModelName)

print('..5. completed')
print('')
print('')
コード例 #8
0
ws = Workspace(auth=spAuth,
               subscription_id=subscription_id,
               resource_group=resource_group,
               workspace_name=workspace_name)
print("Loaded workspace: " + ws.name)

build_version = os.environ["BUILD_BUILDNUMBER"]

aciconfig = AciWebservice.deploy_configuration(
    cpu_cores=2,
    memory_gb=4,
    tags={'BuildVersion': build_version},
    description=
    'Container instance hosting web service to  consume ABC Bricks routing solution'
)

image = Image(ws,
              name="breast-cancer-image",
              tags=[['BuildVersion', build_version]])

print("Picked image with version: " + str(image.version) +
      " which was built on : " + str(image.created_time))

aci_service = Webservice.deploy_from_image(deployment_config=aciconfig,
                                           image=image,
                                           name='breast-cancer-instance',
                                           workspace=ws)
aci_service.wait_for_deployment(True)
print(aci_service.state)
print(aci_service.scoring_uri)
コード例 #9
0
                    required=True)
args = parser.parse_args()

print("Argument 1: %s" % args.service_name)
print("Argument 2: %s" % args.aci_name)
print("Argument 3: %s" % args.description)

print('creating AzureCliAuthentication...')
cli_auth = AzureCliAuthentication()
print('done creating AzureCliAuthentication!')

print('get workspace...')
ws = Workspace.from_config(auth=cli_auth)
print('done getting workspace!')

image = Image(ws, image_name)
print(image)

ws_list = Webservice.list(ws, image_name=image_name)
print(ws_list)

if len(ws_list) > 0:
    if ws_list[0].name == args.service_name:
        print('Deleting: ', ws_list[0].name)
        ws_list[0].delete()
        print('Done')

aci_config = AciWebservice.deploy_configuration(cpu_cores=1,
                                                memory_gb=1,
                                                tags={'name': args.aci_name},
                                                description=args.description)
コード例 #10
0
runtime = 'python'

# create container image configuration
print("Creating container image configuration...")
from azureml.core.image import ContainerImage
image_config = ContainerImage.image_configuration(execution_script='score.py',
                                                  docker_file='dockerfile',
                                                  runtime=runtime,
                                                  conda_file=conda_file)

# create the image
image_name = 'nyc-taxi-fare-image'

from azureml.core import Image
image = Image.create(name=image_name,
                     models=[registered_model, registered_scoring_explainer],
                     image_config=image_config,
                     workspace=ws)

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

# ### Deploy Model to Azure Container Instance (ACI) as a Web Service

# In[ ]:

from azureml.core.webservice import AciWebservice, Webservice

aci_name = 'sklearn-aci-cluster01'

aci_config = AciWebservice.deploy_configuration(
    cpu_cores=1,
コード例 #11
0
    def create_service(self):
        self.iothub_compute = IotHubCompute(self.ws, self.iot_device_id)

        compute_targets = ComputeTarget.list(self.ws)
        for t in compute_targets: 
            if t.type == "IotHub":
                print("IotHub '{}' has provisioning state '{}'.".format(t.name, t.provisioning_state))


        self.container_config = """{
  "ExposedPorts": {
    "50051/tcp": {}
  },
  "HostConfig": {
    "Binds": [
      "/etc/hosts:/etc/hosts"
    ],
    "Privileged": true,
    "Devices": [
      {
        "PathOnHost": "/dev/catapult0",
        "PathInContainer": "/dev/catapult0"
      },
      {
        "PathOnHost": "/dev/catapult1",
        "PathInContainer": "/dev/catapult1"
      }
    ],
    "PortBindings": {
      "50051/tcp": [
        {
          "HostPort": "50051"
        }
      ]
    }
  }
}"""

        self.routes = {
            "route": "FROM /messages/* INTO "
            }

        # Here, we define the Azure ML module with the container_config options above
        self.aml_module = IotBaseModuleSettings(
            name = self.module_name,
            create_option = self.container_config
            )

        # This time, we will leave off the external module from the deployment manifest
        self.deploy_config = IotWebservice.deploy_configuration(
            device_id = self.iot_device_id,
            routes = self.routes,
            aml_module = self.aml_module
            )

        # Deploy from latest version of image, using module_name as your IotWebservice name
        iot_service_name = self.module_name

        # Can specify version=x, otherwise will grab latest
        self.image = Image(self.ws, self.image_name) 
        self.iot_service = IotWebservice.deploy_from_image(
            self.ws,
            iot_service_name,
            self.image,
            self.deploy_config,
            self.iothub_compute
            )
コード例 #12
0
    data = f.read()
with open('score_fixed.py', "w") as f:
    f.write(data.replace('MODEL-NAME',
                         args.model_name))  #replace the placeholder MODEL-NAME
    print('score_fixed.py saved')

# create a Conda dependencies environment file
print("Creating conda dependencies file locally...")
conda_packages = ['numpy', 'pandas', 'scikit-learn==0.20.3']
pip_packages = ['azureml-sdk', 'sklearn_pandas']
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)
コード例 #13
0
print('')

print('5. Authenticating with AzureCliAuthentication...')
clientAuthn = AzureCliAuthentication()
print('..5.completed')
print('')
print('')

print('6. Instantiate AML workspace')
amlWs = Workspace.from_config(auth=clientAuthn)
print('..6.completed')
print('')
print('')

print('7. Instantiate image')
containerImage = Image(amlWs, id=image_id)
print(containerImage)
print('..7.completed')
print('')
print('')

print('8. Check for and delete any existing web service instance')

aksName = args.aks_name
aksRegion = args.aks_region
aksServiceName = args.service_name

print('aksName=', aksName)
print('aksRegion=', aksRegion)
print('aksServiceName=', aksServiceName)
コード例 #14
0
from azureml.core import Image
import azure
import azureml.core
from azureml.core import Workspace
from azureml.core import ScriptRunConfig
import configparser

# Read config file
config = configparser.ConfigParser()
config.read('ml-config.ini')

# Initialize workspace from config
ws = Workspace.from_config()

for i in Image.list(workspace=ws,
                    image_name=config['docker']['docker_image_name']):
    if i.version == int(config['deploy']['docker_image_version']):
        image = i

from azureml.core.webservice import AciWebservice

aciconfig = AciWebservice.deploy_configuration(
    cpu_cores=int(config['deploy']['cpu_cores']),
    memory_gb=int(config['deploy']['memory']),
    tags={
        'area': "meter_classification",
        'type': "meter_classification"
    },
    description="Image with re-trained vgg model")

from azureml.core.webservice import Webservice
コード例 #15
0
print("Azure ML SDK Version: ", azureml.core.VERSION)

ws = Workspace.from_config()
print("Resource group: ", ws.resource_group)
print("Location: ", ws.location)
print("Workspace name: ", ws.name)

from azureml.core.webservice import Webservice

for web_svc in Webservice.list(ws):
    print("Deleting web service", web_svc.name, "...")
    web_svc.delete()

from azureml.core import ComputeTarget

for target in ComputeTarget.list(ws):
    print("Deleting compute target", target.name, "...")
    target.delete()

from azureml.core import Image

for img in Image.list(ws):
    print("Deleting image", img.id, "...")
    img.delete()

from azureml.core.model import Model

for model in Model.list(ws):
    print("Deleting model", model.id, "...")
    model.delete()