Skip to content

Commit 0d7247a

Browse files
Suggested updates
1 parent 3352b8d commit 0d7247a

File tree

1 file changed

+74
-101
lines changed

1 file changed

+74
-101
lines changed

articles/machine-learning/how-to-homomorphic-encryption-seal.md

Lines changed: 74 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ titleSuffix: Azure Machine Learning
44
description: Learn how to use Microsoft SEAL to deploy an encrypted prediction service for image classification
55
author: luisquintanilla
66
ms.author: luquinta
7-
ms.date: 05/14/2020
7+
ms.date: 05/17/2020
88
services: machine-learning
99
ms.service: machine-learning
1010
ms.subservice: core
@@ -14,47 +14,55 @@ ms.topic: conceptual
1414

1515
# How to deploy an encrypted image classification service
1616

17-
Learn how to deploy an image classification model as a encrypted inferencing web service in [Azure Container Instances](https://docs.microsoft.com/azure/container-instances/) (ACI). A web service is an image, in this case a Docker image, that encapsulates the scoring logic and the model itself.
17+
Learn how to deploy an image classification model as an encrypted inferencing web service in [Azure Container Instances](https://docs.microsoft.com/azure/container-instances/) (ACI). The web service is a Docker container image that contains the model and scoring logic.
1818

19-
In this part of the tutorial, you use Azure Machine Learning service to:
19+
In guide, you use Azure Machine Learning service to:
2020

21-
> * Set up your testing environment
22-
> * Retrieve the model from your workspace
23-
> * Test the model locally
24-
> * Deploy the model to ACI
25-
> * Test the deployed model
21+
> * Configure your environments
22+
> * Deploy encrypted inferencing web service
23+
> * Prepare test data
24+
> * Make encrypted predictions
25+
> * Clean up resources
2626
2727
ACI is a great solution for testing and understanding the workflow. For scalable production deployments, consider using Azure Kubernetes Service. For more information, see [how to deploy and where](https://docs.microsoft.com/azure/machine-learning/service/how-to-deploy-and-where).
2828

29+
The encryption method used in this sample is homomorphic encryption (HE). Homomorphic encryption allows for computations to be done on encrypted data without requiring access to a secret (decryption) key. The results of the computations are encrypted and can be revealed only by the owner of the secret key.
30+
2931
## Prerequisites
3032

31-
Complete the model training in the [Train an image classification model with Azure Machine Learning tutorial](tutorial-train-models-with-aml.md).
33+
This guide assumes that you have an image classification model registered in Azure Machine Learning. If not, register the model using a [pretrained model](https://github.com/Azure/MachineLearningNotebooks/raw/master/tutorials/image-classification-mnist-data/sklearn_mnist_model.pkl) or create your own by completing the [train an image classification model with Azure Machine Learning tutorial](tutorial-train-models-with-aml.md).
3234

33-
```python
34-
# If you did NOT complete the tutorial, you can instead run this cell
35-
# This will register a model and download the data needed for this tutorial
36-
# These prerequisites are created in the training tutorial
37-
# Feel free to skip this cell if you completed the training tutorial
35+
## Configure local environment
3836

39-
# register a model
40-
from azureml.core import Workspace
41-
ws = Workspace.from_config()
37+
In a Jupyter notebook
4238

43-
from azureml.core.model import Model
39+
1. Import the Python packages needed for this sample.
40+
41+
```python
42+
%matplotlib inline
43+
import numpy as np
44+
import matplotlib.pyplot as plt
45+
46+
import azureml.core
47+
48+
# display the core SDK version number
49+
print("Azure ML SDK Version: ", azureml.core.VERSION)
50+
```
51+
52+
2. Install homomorphic encryption library for secure inferencing.
4453

45-
model_name = "sklearn_mnist"
46-
model = Model.register(model_path="sklearn_mnist_model.pkl",
47-
model_name=model_name,
48-
tags={"data": "mnist", "model": "classification"},
49-
description="Mnist handwriting recognition",
50-
workspace=ws)
54+
> [!NOTE]
55+
> The `encrypted-inference` package is currently in preview.
5156

57+
[`encrypted-inference`](https://pypi.org/project/encrypted-inference) is a homomorphic encryption library based on [Microsoft SEAL](https://github.com/Microsoft/SEAL).
5258

59+
```python
60+
!pip install encrypted-inference==0.9
5361
```
5462

55-
### Setup the Environment
63+
## Configure the inferencing environment
5664

57-
Add `encrypted-inference` package as a conda dependency
65+
Create an environment for inferencing and add `encrypted-inference` package as a conda dependency.
5866

5967
```python
6068
from azureml.core.environment import Environment
@@ -70,52 +78,24 @@ env.python.conda_dependencies = cd
7078
env.register(workspace = ws)
7179
```
7280

73-
## Set up the environment
74-
75-
Start by setting up a testing environment.
76-
77-
### Import packages
78-
79-
Import the Python packages needed for this tutorial.
80-
81-
```python tags=["check version"]
82-
%matplotlib inline
83-
import numpy as np
84-
import matplotlib.pyplot as plt
85-
86-
import azureml.core
87-
88-
# display the core SDK version number
89-
print("Azure ML SDK Version: ", azureml.core.VERSION)
90-
```
91-
92-
#### Install Homomorphic Encryption based library for Secure Inferencing
81+
## Deploy encrypted inferencing web service
9382

94-
Our library is based on [Microsoft SEAL](https://github.com/Microsoft/SEAL) and pubished to [PyPi.org](https://pypi.org/project/encrypted-inference) as an easy to use package
95-
96-
```python
97-
!pip install encrypted-inference==0.9
98-
```
99-
100-
## Deploy as web service
101-
102-
Deploy the model as a web service hosted in ACI.
83+
Deploy the model as a web service hosted in ACI.
10384

10485
To build the correct environment for ACI, provide the following:
86+
10587
* A scoring script to show how to use the model
10688
* A configuration file to build the ACI
107-
* The model you trained before
89+
* A trained model
10890

10991
### Create scoring script
11092

111-
Create the scoring script, called score.py, used by the web service call to show how to use the model.
93+
Create the scoring script `score.py` used by the web service for inferencing.
11294

11395
You must include two required functions into the scoring script:
114-
* The `init()` function, which typically loads the model into a global object. This function is run only once when the Docker container is started.
115-
116-
* The `run(input_data)` function uses the model to predict a value based on the input data. Inputs and outputs to the run typically use JSON for serialization and de-serialization, but other formats are supported. The function fetches homomorphic encryption based public keys that are uploaded by the service caller.
117-
11896

97+
* The `init()` function, which typically loads the model into a global object. This function is run only once when the Docker container is started.
98+
* The `run(input_data)` function uses the model to predict a value based on the input data. Inputs and outputs to the run typically use JSON for serialization and de-serialization, but other formats are supported. The function fetches homomorphic encryption based public keys that are uploaded by the service caller.
11999

120100
```python
121101
%%writefile score.py
@@ -173,7 +153,7 @@ aciconfig = AciWebservice.deploy_configuration(cpu_cores=1,
173153
description='Encrypted Predict MNIST with sklearn + SEAL')
174154
```
175155

176-
### Deploy in ACI
156+
### Deploy to Azure Container Instances
177157

178158
Estimated time to complete: **about 2-5 minutes**
179159

@@ -182,11 +162,11 @@ Configure the image and deploy. The following code goes through these steps:
182162
1. Create environment object containing dependencies needed by the model using the environment file (`myenv.yml`)
183163
1. Create inference configuration necessary to deploy the model as a web service using:
184164
* The scoring file (`score.py`)
185-
* envrionment object created in previous step
165+
* Environment object created in the rpevious step
186166
1. Deploy the model to the ACI container.
187167
1. Get the web service HTTP endpoint.
188168

189-
```python tags=["configure image", "create image", "deploy web service", "aci"]
169+
```python
190170
%%time
191171
from azureml.core.webservice import Webservice
192172
from azureml.core.model import InferenceConfig
@@ -200,24 +180,24 @@ model = Model(ws, 'sklearn_mnist')
200180
myenv = Environment.get(workspace=ws, name="tutorial-env")
201181
inference_config = InferenceConfig(entry_script="score.py", environment=myenv)
202182

203-
service = Model.deploy(workspace=ws,
204-
name='sklearn-mnist-svc',
205-
models=[model],
206-
inference_config=inference_config,
183+
service = Model.deploy(workspace=ws,
184+
name='sklearn-encrypted-mnist-svc',
185+
models=[model],
186+
inference_config=inference_config,
207187
deployment_config=aciconfig)
208188

209189
service.wait_for_deployment(show_output=True)
210190
```
211191

212192
Get the scoring web service's HTTP endpoint, which accepts REST client calls. This endpoint can be shared with anyone who wants to test the web service or integrate it into an application.
213193

214-
```python tags=["get scoring uri"]
194+
```python
215195
print(service.scoring_uri)
216196
```
217197

218-
### Download test data
198+
## Prepare test data
219199

220-
Download the test data to the **./data/** directory
200+
1. Download the test data. In this case, it's saved into a directory called *data*.
221201

222202
```python
223203
import os
@@ -231,9 +211,7 @@ mnist_file_dataset = MNIST.get_file_dataset()
231211
mnist_file_dataset.download(data_folder, overwrite=True)
232212
```
233213

234-
### Load test data
235-
236-
Load the test data from the **./data/** directory created during the training tutorial.
214+
1. Load the test data from the *data* directory.
237215

238216
```python
239217
from utils import load_data
@@ -246,26 +224,21 @@ X_test = load_data(glob.glob(os.path.join(data_folder,"**/t10k-images-idx3-ubyte
246224
y_test = load_data(glob.glob(os.path.join(data_folder,"**/t10k-labels-idx1-ubyte.gz"), recursive=True)[0], True).reshape(-1)
247225
```
248226

249-
### Predict test data
227+
## Make encrypted predictions
250228

251-
Feed the test dataset to the model to get predictions.
229+
Use the test dataset with the model to get predictions.
252230

253-
The following code goes through these steps:
254-
255-
1. Create our Homomorphic Encryption based client
256-
257-
1. Upload HE generated public keys
231+
To make encrypted predictions:
258232

233+
1. Create our Homomorphic Encryption based client
234+
1. Upload Homomorphic Encryption generated public keys
259235
1. Encrypt the data
260-
261-
1. Send the data as JSON to the web service hosted in ACI.
262-
236+
1. Send the data as JSON to the web service hosted in ACI
263237
1. Use the SDK's `run` API to invoke the service. You can also make raw calls using any HTTP tool such as curl.
264-
<!-- #endregion -->
265238

266-
#### Create our Homomorphic Encryption based client
239+
### Create Homomorphic Encryption based client
267240

268-
Create a new EILinearRegressionClient and setup the public keys
241+
Create a new `EILinearRegressionClient` and create the public keys.
269242

270243
```python
271244
from encrypted.inference.eiclient import EILinearRegressionClient
@@ -279,7 +252,7 @@ public_keys_blob, public_keys_data = edp.get_public_keys()
279252

280253
#### Upload HE generated public keys
281254

282-
Upload the public keys to the workspace default blob store. This will allow us to share the keys with the inference server
255+
Upload the public keys to the workspace default blob store. This will allow you to share the keys with the inference server.
283256

284257
```python
285258
import azureml.core
@@ -303,7 +276,7 @@ datastore.upload_files([public_keys_blob])
303276
os.remove(public_keys_blob)
304277
```
305278

306-
#### Encrypt the data
279+
### Encrypt the test data
307280

308281
```python
309282
#choose any one sample from the test data
@@ -314,9 +287,9 @@ raw_data = edp.encrypt(X_test[sample_index])
314287

315288
```
316289

317-
#### Send the test data to the webservice hosted in ACI
290+
### Send the test data to the encrypted ACI web service
318291

319-
Feed the test dataset to the model to get predictions. We will need to send the connection string to the blob storage where the public keys were uploaded
292+
Provide the test dataset to the model to get predictions. We will need to send the connection string to the blob storage where the public keys were uploaded.
320293

321294
```python
322295
import json
@@ -338,9 +311,9 @@ eresult = service.run(input_data=data)
338311
print ('Received encrypted inference results')
339312
```
340313

341-
#### Decrypt the data
314+
### Decrypt the prediction
342315

343-
Use the client to decrypt the results
316+
Use the client to decrypt the results.
344317

345318
```python
346319
import numpy as np
@@ -358,20 +331,20 @@ print ( ' Actual Label : ', y_test[sample_index])
358331

359332
## Clean up resources
360333

361-
To keep the resource group and workspace for other tutorials and exploration, you can delete only the ACI deployment using this API call:
334+
Delete the web service created in this sample:
362335

363336
```python tags=["delete web service"]
364337
service.delete()
365338
```
366339

367-
If you're not going to use what you've created here, delete the resources you just created with this quickstart so you don't incur any charges. In the Azure portal, select and delete your resource group. You can also keep the resource group, but delete a single workspace by displaying the workspace properties and selecting the Delete button.
340+
If you no longer plan to use the Azure resources youve created, delete them. This prevents you from being charged for unutilized resources that are still running. See this guide on how to [clean up resources](how-to-manage-workspace#clean-up-resources) to learn more.
368341

369342
## Next steps
370343

371-
In this Azure Machine Learning tutorial, you used Python to:
344+
In this Azure Machine Learning guide, you:
372345

373-
> * Set up your testing environment
374-
> * Retrieve the model from your workspace
375-
> * Test the model locally
376-
> * Deploy the model to ACI
377-
> * Test the deployed model
346+
> * Configured your environments
347+
> * Deployed an encrypted inferencing web service
348+
> * Prepared test data
349+
> * Made encrypted predictions
350+
> * Cleaned up resources

0 commit comments

Comments
 (0)