Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Build and deploy PyTorch models with Azure Mach...

Build and deploy PyTorch models with Azure Machine Learning

A deep dive into Azure Machine Learning, a cloud service that helps you build, train, deploy, and manage models. Walk through the data science process and then have some fun creating a ML recognition model based on the Simpsons cartoon with PyTorch. You'll leave this session with a better grasp of the technological components of Azure Machine Learning services.

Henk Boelman

November 11, 2020
Tweet

More Decks by Henk Boelman

Other Decks in Technology

Transcript

  1. Henk Boelman Cloud Advocate @ Microsoft Build and deploy PyTorch

    models with Azure Machine Learning HenkBoelman.com @hboelman
  2. Sophisticated pretrained models To simplify solution development Azure Databricks Machine

    Learning VMs Popular frameworks To build advanced deep learning solutions TensorFlow Keras Pytorch Onnx Azure Machine Learning Language Speech … Azure Search Vision On-premises Cloud Edge Productive services To empower data science and development teams Powerful infrastructure To accelerate deep learning Flexible deployment To deploy and manage models on intelligent cloud and edge Machine Learning on Azure Cognitive Services
  3. Azure Machine Learning studio A fully-managed cloud service that enables

    you to easily build, deploy, and share predictive analytics solutions.
  4. Sophisticated pretrained models To simplify solution development Azure Databricks Machine

    Learning VMs Popular frameworks To build advanced deep learning solutions TensorFlow Keras Pytorch Onnx Azure Machine Learning Language Speech … Azure Search Vision On-premises Cloud Edge Productive services To empower data science and development teams Powerful infrastructure To accelerate deep learning Flexible deployment To deploy and manage models on intelligent cloud and edge Machine Learning on Azure Cognitive Services
  5. What is Azure Machine Learning Service? Set of Azure Cloud

    Services Python SDK Prepare Data Build Models Train Models Manage Models Track Experiments Deploy Models That enables you to:
  6. Create a workspace ws = Workspace.create( name='<NAME>', subscription_id='<SUBSCRIPTION ID>', resource_group='<RESOURCE

    GROUP>', location='westeurope') ws.write_config() ws = Workspace.from_config() Create a workspace
  7. Datasets – registered, known data sets Experiments – Training runs

    Models – Registered, versioned models Endpoints: Real-time Endpoints – Deployed model endpoints Pipeline Endpoints – Training workflows Compute – Managed compute Datastores – Connections to data Azure Machine Learning Service
  8. Create Compute cfg = AmlCompute.provisioning_configuration( vm_size='STANDARD_NC6', min_nodes=1, max_nodes=6) cc =

    ComputeTarget.create(ws, '<NAME>', cfg) Create a workspace Create compute
  9. Create an Environment Curated enviroment curated_env_name = 'AzureML-PyTorch-1.6-GPU' pytorch_env =

    Environment.get(workspace=ws, name=curated_env_name) pytorch_env = pytorch_env.clone(new_name='pytorch-1.6-gpu’) Custom environment env = Environment('pytorch-1.6-gpu') cd = CondaDependencies.create( pip_packages=['azureml-dataprep[pandas,fuse]', 'azureml-defaults'], conda_packages=['pytorch','torchvision']) env.python.conda_dependencies = cd env.docker.enabled = True Create an Experiment Create a training file Create an ScriptRunConfig
  10. Create an ScriptRunConfig args = ['--data-folder', simpsons_ds.as_named_input('simpsons').as_mount(), '--num-epochs', 10] project_folder

    = "./trainingscripts“ config = ScriptRunConfig( source_directory = project_folder, script = 'train.py', compute_target=compute_target, environment = pytorch_env, arguments=args, ) Create an Experiment Create a training file Create an ScriptRunConfig
  11. Submit the experiment to the cluster run = exp.submit(estimator) RunDetails(run).show()

    Create an Experiment Create a training file Submit to the AI cluster Create an ScriptRunConfig
  12. Create an Experiment Create a training file Submit to the

    AI cluster Create an ScriptRunConfig Demo: Creating and run an experiment
  13. Azure Notebook Compute Target Experiment Docker Image Data store 1.

    Snapshot folder and send to experiment 2. create docker image 3. Deploy docker and snapshot to compute 4. Mount datastore to compute 6. Stream stdout, logs, metrics 5. Launch the script 7. Copy over outputs
  14. Register the model model = run.register_model( model_name='SimpsonsAI', model_path='outputs') Create an

    Experiment Create a training file Submit to the AI cluster Create an ScriptRunConfig Register the model
  15. Create an Experiment Create a training file Submit to the

    AI cluster Create an ScriptRunConfig Register the model Demo: Register and test the model
  16. Step 2 Experiment with your model & data Create an

    Experiment Create a training file Submit to the AI cluster Create an ScriptRunConfig Register the model
  17. Score.py %%writefile score.py from azureml.core.model import Model def init(): model_root

    = Model.get_model_path('MyModel’) loaded_model = model_from_json(loaded_model_json) loaded_model.load_weights(model_file_h5) def run(raw_data): url = json.loads(raw_data)['url’] image_data = cv2.resize(image_data,(96,96)) predicted_labels = loaded_model.predict(data1) return json.dumps(predicted_labels)
  18. Environment File from azureml.core.runconfig import CondaDependencies cd = CondaDependencies.create() cd.add_conda_package('keras==2.2.2')

    cd.add_conda_package('opencv') cd.add_tensorflow_conda_package() cd.save_to_file(base_directory='./', conda_file_path='myenv.yml')
  19. Deploy to ACI aciconfig = AciWebservice.deploy_configuration( cpu_cores = 1, memory_gb

    = 2) service = Model.deploy(workspace=ws, name='simpsons-aci', models=[model], inference_config=inference_config, deployment_config=aciconfig)
  20. Deploy to AKS aks_target = AksCompute(ws,"AI-AKS-DEMO") deployment_config = AksWebservice.deploy_configuration( cpu_cores

    = 1, memory_gb = 1) service = Model.deploy(workspace=ws, name="simpsons-ailive", models=[model], inference_config=inference_config, deployment_config=deployment_config, deployment_target=aks_target) service.wait_for_deployment(show_output = True)
  21. Create an Experiment Trained the model Register the model Setup

    a workspace Setup Compute Setup storage Recap Prepare your environment Experiment with your model & data Deploy your model Create a deployment Deployed the model