Merge pull request #48 from rastala/master

Update notebooks with new version
This commit is contained in:
Roope Astala
2018-10-12 14:44:10 -04:00
committed by GitHub
56 changed files with 3589 additions and 1610 deletions

View File

@@ -527,7 +527,6 @@
"\n", "\n",
"myenv = CondaDependencies()\n", "myenv = CondaDependencies()\n",
"myenv.add_conda_package(\"scikit-learn\")\n", "myenv.add_conda_package(\"scikit-learn\")\n",
"myenv.add_pip_package(\"pynacl==1.2.1\")\n",
"print(myenv.serialize_to_string())\n", "print(myenv.serialize_to_string())\n",
"\n", "\n",
"with open(\"myenv.yml\",\"w\") as f:\n", "with open(\"myenv.yml\",\"w\") as f:\n",
@@ -789,6 +788,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "roastala"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -218,7 +218,7 @@
"run_config_system_managed = RunConfiguration()\n", "run_config_system_managed = RunConfiguration()\n",
"\n", "\n",
"run_config_system_managed.environment.python.user_managed_dependencies = False\n", "run_config_system_managed.environment.python.user_managed_dependencies = False\n",
"run_config_system_managed.prepare_environment = True\n", "run_config_system_managed.auto_prepare_environment = True\n",
"\n", "\n",
"# Specify conda dependencies with scikit-learn\n", "# Specify conda dependencies with scikit-learn\n",
"cd = CondaDependencies.create(conda_packages=['scikit-learn'])\n", "cd = CondaDependencies.create(conda_packages=['scikit-learn'])\n",
@@ -297,7 +297,7 @@
"run_config_docker = RunConfiguration()\n", "run_config_docker = RunConfiguration()\n",
"\n", "\n",
"run_config_docker.environment.python.user_managed_dependencies = False\n", "run_config_docker.environment.python.user_managed_dependencies = False\n",
"run_config_docker.prepare_environment = True\n", "run_config_docker.auto_prepare_environment = True\n",
"run_config_docker.environment.docker.enabled = True\n", "run_config_docker.environment.docker.enabled = True\n",
"run_config_docker.environment.docker.base_image = azureml.core.runconfig.DEFAULT_CPU_IMAGE\n", "run_config_docker.environment.docker.base_image = azureml.core.runconfig.DEFAULT_CPU_IMAGE\n",
"\n", "\n",
@@ -442,6 +442,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "roastala"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -15,7 +15,7 @@ os.makedirs('./outputs', exist_ok=True)
X, y = load_diabetes(return_X_y=True) X, y = load_diabetes(return_X_y=True)
run = Run.get_submitted_run() run = Run.get_context()
X_train, X_test, y_train, y_test = train_test_split(X, y, X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, test_size=0.2,

View File

@@ -261,6 +261,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "roastala"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -14,7 +14,7 @@ os.makedirs('./outputs', exist_ok=True)
X, y = load_diabetes(return_X_y=True) X, y = load_diabetes(return_X_y=True)
run = Run.get_submitted_run() run = Run.get_context()
X_train, X_test, y_train, y_test = train_test_split(X, y, X_train, X_test, y_train, y_test = train_test_split(X, y,
test_size=0.2, test_size=0.2,

View File

@@ -158,7 +158,7 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"ds.upload_files(['./feeatures.npy', './labels.npy'], target_path='diabetes', overwrite=True)" "ds.upload_files(['./features.npy', './labels.npy'], target_path='diabetes', overwrite=True)"
] ]
}, },
{ {
@@ -394,7 +394,7 @@
] ]
}, },
{ {
"cell_type": "raw", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"You can choose to SSH into the VM and install Azure ML SDK, and any other missing dependencies, in that Python environment. For demonstration purposes, we simply are going to create another script `train2.py` that doesn't have azureml dependencies, and submit it instead." "You can choose to SSH into the VM and install Azure ML SDK, and any other missing dependencies, in that Python environment. For demonstration purposes, we simply are going to create another script `train2.py` that doesn't have azureml dependencies, and submit it instead."
@@ -407,6 +407,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"%%writefile $script_folder/train2.py\n", "%%writefile $script_folder/train2.py\n",
"\n",
"print('####################################')\n", "print('####################################')\n",
"print('Hello World (without Azure ML SDK)!')\n", "print('Hello World (without Azure ML SDK)!')\n",
"print('####################################')" "print('####################################')"
@@ -592,6 +593,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "haining"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -24,7 +24,7 @@ print('List all files: ', os.listdir(args.data_folder))
X = np.load(os.path.join(args.data_folder, 'features.npy')) X = np.load(os.path.join(args.data_folder, 'features.npy'))
y = np.load(os.path.join(args.data_folder, 'labels.npy')) y = np.load(os.path.join(args.data_folder, 'labels.npy'))
run = Run.get_submitted_run() run = Run.get_context()
X_train, X_test, y_train, y_test = train_test_split( X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=0) X, y, test_size=0.2, random_state=0)

View File

@@ -303,6 +303,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "aashishb"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -18,7 +18,7 @@ from pyspark.sql.types import DoubleType, IntegerType, StringType
from azureml.core.run import Run from azureml.core.run import Run
# initialize logger # initialize logger
run = Run.get_submitted_run() run = Run.get_context()
# start Spark session # start Spark session
spark = pyspark.sql.SparkSession.builder.appName('Iris').getOrCreate() spark = pyspark.sql.SparkSession.builder.appName('Iris').getOrCreate()

View File

@@ -206,7 +206,6 @@
"from azureml.core.conda_dependencies import CondaDependencies \n", "from azureml.core.conda_dependencies import CondaDependencies \n",
"\n", "\n",
"myenv = CondaDependencies.create(conda_packages=['numpy','scikit-learn'])\n", "myenv = CondaDependencies.create(conda_packages=['numpy','scikit-learn'])\n",
"myenv.add_pip_package(\"pynacl==1.2.1\")\n",
"\n", "\n",
"with open(\"myenv.yml\",\"w\") as f:\n", "with open(\"myenv.yml\",\"w\") as f:\n",
" f.write(myenv.serialize_to_string())" " f.write(myenv.serialize_to_string())"
@@ -398,6 +397,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "raymondl"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -136,7 +136,6 @@
"from azureml.core.conda_dependencies import CondaDependencies \n", "from azureml.core.conda_dependencies import CondaDependencies \n",
"\n", "\n",
"myenv = CondaDependencies.create(conda_packages=['numpy','scikit-learn'])\n", "myenv = CondaDependencies.create(conda_packages=['numpy','scikit-learn'])\n",
"myenv.add_pip_package(\"pynacl==1.2.1\")\n",
"\n", "\n",
"with open(\"myenv.yml\",\"w\") as f:\n", "with open(\"myenv.yml\",\"w\") as f:\n",
" f.write(myenv.serialize_to_string())" " f.write(myenv.serialize_to_string())"
@@ -313,6 +312,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "raymondl"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -180,7 +180,6 @@
"\n", "\n",
"myenv = CondaDependencies.create(conda_packages=['numpy','scikit-learn'])\n", "myenv = CondaDependencies.create(conda_packages=['numpy','scikit-learn'])\n",
"myenv.add_pip_package(\"azureml-monitoring\")\n", "myenv.add_pip_package(\"azureml-monitoring\")\n",
"myenv.add_pip_package(\"pynacl==1.2.1\")\n",
"\n", "\n",
"with open(\"myenv.yml\",\"w\") as f:\n", "with open(\"myenv.yml\",\"w\") as f:\n",
" f.write(myenv.serialize_to_string())" " f.write(myenv.serialize_to_string())"
@@ -425,6 +424,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "marthalc"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -387,6 +387,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "marthalc"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -15,7 +15,7 @@
"source": [ "source": [
"# AutoML 01: Classification with Local Compute\n", "# AutoML 01: Classification with Local Compute\n",
"\n", "\n",
"In this example we use the scikit-learn's [digit dataset](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html) to showcase how you can use AutoML for a simple classification problem.\n", "In this example we use the scikit-learn's [digit dataset](http://scikit-learn.org/stable/datasets/index.html#optical-recognition-of-handwritten-digits-dataset) to showcase how you can use AutoML for a simple classification problem.\n",
"\n", "\n",
"Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n", "Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n",
"\n", "\n",
@@ -108,7 +108,9 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Load Training Data" "## Load Training Data\n",
"\n",
"This uses scikit-learn's [load_digits](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html) method."
] ]
}, },
{ {
@@ -168,7 +170,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Train the Model\n", "## Train the Models\n",
"\n", "\n",
"Call the `submit` method on the experiment object and pass the run configuration. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n", "Call the `submit` method on the experiment object and pass the run configuration. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n",
"In this example, we specify `show_output = True` to print currently running iterations to the console." "In this example, we specify `show_output = True` to print currently running iterations to the console."
@@ -280,7 +282,7 @@
"source": [ "source": [
"### Retrieve the Best Model\n", "### Retrieve the Best Model\n",
"\n", "\n",
"Below we select the best pipeline from our iterations. The `get_output` method on `automl_classifier` returns the best run and the fitted model for the last invocation. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*." "Below we select the best pipeline from our iterations. The `get_output` method returns the best run and the fitted model. The Model includes the pipeline and any pre-processing. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*."
] ]
}, },
{ {
@@ -359,7 +361,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### Testing Our Best Pipeline\n", "#### Testing Our Best Fitted Model\n",
"We will try to predict 2 digits and see how our model works." "We will try to predict 2 digits and see how our model works."
] ]
}, },
@@ -384,6 +386,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -15,7 +15,7 @@
"source": [ "source": [
"# AutoML 02: Regression with Local Compute\n", "# AutoML 02: Regression with Local Compute\n",
"\n", "\n",
"In this example we use the scikit-learn's [diabetes dataset](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_diabetes.html) to showcase how you can use AutoML for a simple regression problem.\n", "In this example we use the scikit-learn's [diabetes dataset](http://scikit-learn.org/stable/datasets/index.html#diabetes-dataset) to showcase how you can use AutoML for a simple regression problem.\n",
"\n", "\n",
"Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n", "Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n",
"\n", "\n",
@@ -108,7 +108,8 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Load Training Data" "### Load Training Data\n",
"This uses scikit-learn's [load_diabetes](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_diabetes.html) method."
] ]
}, },
{ {
@@ -172,7 +173,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Train the Model\n", "## Train the Models\n",
"\n", "\n",
"Call the `submit` method on the experiment object and pass the run configuration. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n", "Call the `submit` method on the experiment object and pass the run configuration. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n",
"In this example, we specify `show_output = True` to print currently running iterations to the console." "In this example, we specify `show_output = True` to print currently running iterations to the console."
@@ -256,7 +257,7 @@
"source": [ "source": [
"### Retrieve the Best Model\n", "### Retrieve the Best Model\n",
"\n", "\n",
"Below we select the best pipeline from our iterations. The `get_output` method on `automl_classifier` returns the best run and the fitted model for the last invocation. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*." "Below we select the best pipeline from our iterations. The `get_output` method returns the best run and the fitted model. The Model includes the pipeline and any pre-processing. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*."
] ]
}, },
{ {
@@ -386,6 +387,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -15,7 +15,7 @@
"source": [ "source": [
"# AutoML 03: Remote Execution using DSVM (Ubuntu)\n", "# AutoML 03: Remote Execution using DSVM (Ubuntu)\n",
"\n", "\n",
"In this example we use the scikit-learn's [digit dataset](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html) to showcase how you can use AutoML for a simple classification problem.\n", "In this example we use the scikit-learn's [digit dataset](http://scikit-learn.org/stable/datasets/index.html#optical-recognition-of-handwritten-digits-dataset) to showcase how you can use AutoML for a simple classification problem.\n",
"\n", "\n",
"Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n", "Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n",
"\n", "\n",
@@ -145,7 +145,7 @@
"source": [ "source": [
"## Create Get Data File\n", "## Create Get Data File\n",
"For remote executions you should author a `get_data.py` file containing a `get_data()` function. This file should be in the root directory of the project. You can encapsulate code to read data either from a blob storage or local disk in this file.\n", "For remote executions you should author a `get_data.py` file containing a `get_data()` function. This file should be in the root directory of the project. You can encapsulate code to read data either from a blob storage or local disk in this file.\n",
"In this example, the `get_data()` function returns data from scikit-learn's [digit dataset](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html)." "In this example, the `get_data()` function returns data using scikit-learn's [load_digits](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html) method."
] ]
}, },
{ {
@@ -234,7 +234,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Train the Model\n", "## Train the Models\n",
"\n", "\n",
"Call the `submit` method on the experiment object and pass the run configuration. For remote runs the execution is asynchronous, so you will see the iterations get populated as they complete. You can interact with the widgets and models even when the experiment is running to retrieve the best model up to that point. Once you are satisfied with the model, you can cancel a particular iteration or the whole run.\n", "Call the `submit` method on the experiment object and pass the run configuration. For remote runs the execution is asynchronous, so you will see the iterations get populated as they complete. You can interact with the widgets and models even when the experiment is running to retrieve the best model up to that point. Once you are satisfied with the model, you can cancel a particular iteration or the whole run.\n",
"\n", "\n",
@@ -354,7 +354,7 @@
"source": [ "source": [
"### Retrieve the Best Model\n", "### Retrieve the Best Model\n",
"\n", "\n",
"Below we select the best pipeline from our iterations. The `get_output` method on `automl_classifier` returns the best run and the fitted model for the last invocation. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*." "Below we select the best pipeline from our iterations. The `get_output` method returns the best run and the fitted model. The Model includes the pipeline and any pre-processing. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*."
] ]
}, },
{ {
@@ -433,7 +433,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### Test Our Best Pipeline" "#### Test Our Best Fitted Model"
] ]
}, },
{ {
@@ -457,6 +457,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -15,7 +15,7 @@
"source": [ "source": [
"# AutoML 03: Remote Execution using Batch AI\n", "# AutoML 03: Remote Execution using Batch AI\n",
"\n", "\n",
"In this example we use the scikit-learn's [diabetes dataset](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_diabetes.html) to showcase how you can use AutoML for a simple classification problem.\n", "In this example we use the scikit-learn's [digit dataset](http://scikit-learn.org/stable/datasets/index.html#optical-recognition-of-handwritten-digits-dataset) to showcase how you can use AutoML for a simple classification problem.\n",
"\n", "\n",
"Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n", "Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n",
"\n", "\n",
@@ -170,7 +170,7 @@
"source": [ "source": [
"## Create Get Data File\n", "## Create Get Data File\n",
"For remote executions you should author a `get_data.py` file containing a `get_data()` function. This file should be in the root directory of the project. You can encapsulate code to read data either from a blob storage or local disk in this file.\n", "For remote executions you should author a `get_data.py` file containing a `get_data()` function. This file should be in the root directory of the project. You can encapsulate code to read data either from a blob storage or local disk in this file.\n",
"In this example, the `get_data()` function returns data from scikit-learn's [diabetes dataset](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_diabetes.html)." "In this example, the `get_data()` function returns data using scikit-learn's [load_digits](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html) method."
] ]
}, },
{ {
@@ -252,7 +252,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Train the Model\n", "## Train the Models\n",
"\n", "\n",
"Call the `submit` method on the experiment object and pass the run configuration. For remote runs the execution is asynchronous, so you will see the iterations get populated as they complete. You can interact with the widgets and models even when the experiment is running to retrieve the best model up to that point. Once you are satisfied with the model, you can cancel a particular iteration or the whole run.\n", "Call the `submit` method on the experiment object and pass the run configuration. For remote runs the execution is asynchronous, so you will see the iterations get populated as they complete. You can interact with the widgets and models even when the experiment is running to retrieve the best model up to that point. Once you are satisfied with the model, you can cancel a particular iteration or the whole run.\n",
"In this example, we specify `show_output = False` to suppress console output while the run is in progress." "In this example, we specify `show_output = False` to suppress console output while the run is in progress."
@@ -356,7 +356,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Cancelling runs\n", "## Cancelling Runs\n",
"\n", "\n",
"You can cancel ongoing remote runs using the `cancel` and `cancel_iteration` functions." "You can cancel ongoing remote runs using the `cancel` and `cancel_iteration` functions."
] ]
@@ -380,7 +380,7 @@
"source": [ "source": [
"### Retrieve the Best Model\n", "### Retrieve the Best Model\n",
"\n", "\n",
"Below we select the best pipeline from our iterations. The `get_output` method on `automl_classifier` returns the best run and the fitted model for the last invocation. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*." "Below we select the best pipeline from our iterations. The `get_output` method returns the best run and the fitted model. The Model includes the pipeline and any pre-processing. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*."
] ]
}, },
{ {
@@ -434,25 +434,6 @@
"print(third_model)" "print(third_model)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Register the Fitted Model for Deployment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"description = 'AutoML Model'\n",
"tags = None\n",
"remote_run.register_model(description = description, tags = tags)\n",
"remote_run.model_id # Use this id to deploy the model as a web service in Azure."
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -478,7 +459,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### Testing Our Best Pipeline" "#### Testing Our Best Fitted Model"
] ]
}, },
{ {
@@ -502,6 +483,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -132,15 +132,32 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"from azureml.core.compute import RemoteCompute\n", "from azureml.core.compute import RemoteCompute\n",
"import time\n",
"\n", "\n",
"# Add your VM information below\n", "# Add your VM information below\n",
"dsvm_name = 'mydsvm1'\n", "# If a compute with the specified compute_name already exists, it will be used and the dsvm_ip_addr, dsvm_ssh_port, \n",
"# dsvm_username and dsvm_password will be ignored.\n",
"compute_name = 'mydsvm'\n",
"dsvm_ip_addr = '<<ip_addr>>'\n", "dsvm_ip_addr = '<<ip_addr>>'\n",
"dsvm_ssh_port = 22\n", "dsvm_ssh_port = 22\n",
"dsvm_username = '<<username>>'\n", "dsvm_username = '<<username>>'\n",
"dsvm_password = '<<password>>'\n", "dsvm_password = '<<password>>'\n",
"\n", "\n",
"dsvm_compute = RemoteCompute.attach(workspace=ws, name=dsvm_name, address=dsvm_ip_addr, username=dsvm_username, password=dsvm_password, ssh_port=dsvm_ssh_port)" "if compute_name in ws.compute_targets():\n",
" print('Using existing compute.')\n",
" dsvm_compute = ws.compute_targets()[compute_name]\n",
"else:\n",
" RemoteCompute.attach(workspace=ws, name=compute_name, address=dsvm_ip_addr, username=dsvm_username, password=dsvm_password, ssh_port=dsvm_ssh_port)\n",
"\n",
" while ws.compute_targets()[compute_name].provisioning_state == 'Creating':\n",
" time.sleep(1)\n",
"\n",
" dsvm_compute = ws.compute_targets()[compute_name]\n",
" \n",
" if dsvm_compute.provisioning_state == 'Failed':\n",
" print('Attached failed.')\n",
" print(dsvm_compute.provisioning_errors)\n",
" dsvm_compute.delete()"
] ]
}, },
{ {
@@ -182,11 +199,11 @@
" le = LabelEncoder()\n", " le = LabelEncoder()\n",
" le.fit(df[\"Label\"].values)\n", " le.fit(df[\"Label\"].values)\n",
" y = le.transform(df[\"Label\"].values)\n", " y = le.transform(df[\"Label\"].values)\n",
" df = df.drop([\"Label\"], axis=1)\n", " X = df.drop([\"Label\"], axis=1)\n",
"\n", "\n",
" df_train, _, y_train, _ = train_test_split(df, y, test_size = 0.1, random_state = 42)\n", " X_train, _, y_train, _ = train_test_split(X, y, test_size = 0.1, random_state = 42)\n",
"\n", "\n",
" return { \"X\" : df, \"y\" : y }" " return { \"X\" : X_train, \"y\" : y_train }"
] ]
}, },
{ {
@@ -261,7 +278,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Train the Model <a class=\"anchor\" id=\"Training-the-model-Remote-DSVM\"></a>\n", "## Train the Models <a class=\"anchor\" id=\"Training-the-model-Remote-DSVM\"></a>\n",
"\n", "\n",
"Call the `submit` method on the experiment object and pass the run configuration. For remote runs the execution is asynchronous, so you will see the iterations get populated as they complete. You can interact with the widgets and models even when the experiment is running to retrieve the best model up to that point. Once you are satisfied with the model, you can cancel a particular iteration or the whole run." "Call the `submit` method on the experiment object and pass the run configuration. For remote runs the execution is asynchronous, so you will see the iterations get populated as they complete. You can interact with the widgets and models even when the experiment is running to retrieve the best model up to that point. Once you are satisfied with the model, you can cancel a particular iteration or the whole run."
] ]
@@ -279,7 +296,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Exploring the results <a class=\"anchor\" id=\"Exploring-the-Results-Remote-DSVM\"></a>\n", "## Exploring the Results <a class=\"anchor\" id=\"Exploring-the-Results-Remote-DSVM\"></a>\n",
"#### Widget for Monitoring Runs\n", "#### Widget for Monitoring Runs\n",
"\n", "\n",
"The widget will first report a \"loading\" status while running the first iteration. After completing the first iteration, an auto-updating graph and table will be shown. The widget will refresh once per minute, so you should see the graph update as child runs complete.\n", "The widget will first report a \"loading\" status while running the first iteration. After completing the first iteration, an auto-updating graph and table will be shown. The widget will refresh once per minute, so you should see the graph update as child runs complete.\n",
@@ -329,7 +346,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Cancelling runs\n", "## Cancelling Runs\n",
"You can cancel ongoing remote runs using the `cancel` and `cancel_iteration` functions." "You can cancel ongoing remote runs using the `cancel` and `cancel_iteration` functions."
] ]
}, },
@@ -352,7 +369,7 @@
"source": [ "source": [
"### Retrieve the Best Model\n", "### Retrieve the Best Model\n",
"\n", "\n",
"Below we select the best pipeline from our iterations. The `get_output` method on `automl_classifier` returns the best run and the fitted model for the last invocation. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*." "Below we select the best pipeline from our iterations. The `get_output` method returns the best run and the fitted model. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*."
] ]
}, },
{ {
@@ -401,25 +418,6 @@
"zero_run, zero_model = remote_run.get_output(iteration = iteration)" "zero_run, zero_model = remote_run.get_output(iteration = iteration)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Register the Fitted Model for Deployment"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"description = 'AutoML Model'\n",
"tags = None\n",
"remote_run.register_model(description = description, tags = tags)\n",
"print(remote_run.model_id) # Use this id to deploy the model as a web service in Azure."
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -445,12 +443,12 @@
"le = LabelEncoder()\n", "le = LabelEncoder()\n",
"le.fit(df[\"Label\"].values)\n", "le.fit(df[\"Label\"].values)\n",
"y = le.transform(df[\"Label\"].values)\n", "y = le.transform(df[\"Label\"].values)\n",
"df = df.drop([\"Label\"], axis=1)\n", "X = df.drop([\"Label\"], axis=1)\n",
"\n", "\n",
"_, df_test, _, y_test = train_test_split(df, y, test_size=0.1, random_state=42)\n", "_, X_test, _, y_test = train_test_split(X, y, test_size=0.1, random_state=42)\n",
"\n", "\n",
"\n", "\n",
"ypred = fitted_model.predict(df_test.values)\n", "ypred = fitted_model.predict(X_test.values)\n",
"\n", "\n",
"\n", "\n",
"ypred_strings = le.inverse_transform(ypred)\n", "ypred_strings = le.inverse_transform(ypred)\n",
@@ -465,6 +463,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -15,7 +15,7 @@
"source": [ "source": [
"# AutoML 05: Blacklisting Models, Early Termination, and Handling Missing Data\n", "# AutoML 05: Blacklisting Models, Early Termination, and Handling Missing Data\n",
"\n", "\n",
"In this example we use the scikit-learn's [digit dataset](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html) to showcase how you can use AutoML for handling missing values in data. We also provide a stopping metric indicating a target for the primary metrics so that AutoML can terminate the run without necessarly going through all the iterations. Finally, if you want to avoid a certain pipeline, we allow you to specify a blacklist of algorithms that AutoML will ignore for this run.\n", "In this example we use the scikit-learn's [digit dataset](http://scikit-learn.org/stable/datasets/index.html#optical-recognition-of-handwritten-digits-dataset) to showcase how you can use AutoML for handling missing values in data. We also provide a stopping metric indicating a target for the primary metrics so that AutoML can terminate the run without necessarly going through all the iterations. Finally, if you want to avoid a certain pipeline, we allow you to specify a blacklist of algorithms that AutoML will ignore for this run.\n",
"\n", "\n",
"Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n", "Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n",
"\n", "\n",
@@ -166,7 +166,7 @@
"|**n_cross_validations**|Number of cross validation splits.|\n", "|**n_cross_validations**|Number of cross validation splits.|\n",
"|**preprocess**|Setting this to *True* enables AutoML to perform preprocessing on the input to handle *missing data*, and to perform some common *feature extraction*.|\n", "|**preprocess**|Setting this to *True* enables AutoML to perform preprocessing on the input to handle *missing data*, and to perform some common *feature extraction*.|\n",
"|**exit_score**|*double* value indicating the target for *primary_metric*. <br>Once the target is surpassed the run terminates.|\n", "|**exit_score**|*double* value indicating the target for *primary_metric*. <br>Once the target is surpassed the run terminates.|\n",
"|**blacklist_algos**|*Array* of *strings* indicating pipelines to ignore for AutoML.<br><br> Allowed values for **Classification**<br><i>LogisticRegression</i><br><i>SGDClassifierWrapper</i><br><i>NBWrapper</i><br><i>BernoulliNB</i><br><i>SVCWrapper</i><br><i>LinearSVMWrapper</i><br><i>KNeighborsClassifier</i><br><i>DecisionTreeClassifier</i><br><i>RandomForestClassifier</i><br><i>ExtraTreesClassifier</i><br><i>LightGBMClassifier</i><br><br>Allowed values for **Regression**<br><i>ElasticNet<i><br><i>GradientBoostingRegressor<i><br><i>DecisionTreeRegressor<i><br><i>KNeighborsRegressor<i><br><i>LassoLars<i><br><i>SGDRegressor<i><br><i>RandomForestRegressor<i><br><i>ExtraTreesRegressor<i>|\n", "|**blacklist_algos**|*List* of *strings* indicating machine learning algorithms for AutoML to avoid in this run.<br><br> Allowed values for **Classification**<br><i>LogisticRegression</i><br><i>SGDClassifierWrapper</i><br><i>NBWrapper</i><br><i>BernoulliNB</i><br><i>SVCWrapper</i><br><i>LinearSVMWrapper</i><br><i>KNeighborsClassifier</i><br><i>DecisionTreeClassifier</i><br><i>RandomForestClassifier</i><br><i>ExtraTreesClassifier</i><br><i>LightGBMClassifier</i><br><br>Allowed values for **Regression**<br><i>ElasticNet<i><br><i>GradientBoostingRegressor<i><br><i>DecisionTreeRegressor<i><br><i>KNeighborsRegressor<i><br><i>LassoLars<i><br><i>SGDRegressor<i><br><i>RandomForestRegressor<i><br><i>ExtraTreesRegressor<i>|\n",
"|**X**|(sparse) array-like, shape = [n_samples, n_features]|\n", "|**X**|(sparse) array-like, shape = [n_samples, n_features]|\n",
"|**y**|(sparse) array-like, shape = [n_samples, ], [n_samples, n_classes]<br>Multi-class targets. An indicator matrix turns on multilabel classification. This should be an array of integers.|\n", "|**y**|(sparse) array-like, shape = [n_samples, ], [n_samples, n_classes]<br>Multi-class targets. An indicator matrix turns on multilabel classification. This should be an array of integers.|\n",
"|**path**|Relative path to the project folder. AutoML stores configuration files for the experiment under this folder. You can specify a new empty folder.|" "|**path**|Relative path to the project folder. AutoML stores configuration files for the experiment under this folder. You can specify a new empty folder.|"
@@ -197,7 +197,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Train the Model\n", "## Train the Models\n",
"\n", "\n",
"Call the `submit` method on the experiment object and pass the run configuration. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n", "Call the `submit` method on the experiment object and pass the run configuration. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n",
"In this example, we specify `show_output = True` to print currently running iterations to the console." "In this example, we specify `show_output = True` to print currently running iterations to the console."
@@ -272,7 +272,7 @@
"source": [ "source": [
"### Retrieve the Best Model\n", "### Retrieve the Best Model\n",
"\n", "\n",
"Below we select the best pipeline from our iterations. The `get_output` method on `automl_classifier` returns the best run and the fitted model for the last invocation. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*." "Below we select the best pipeline from our iterations. The `get_output` method returns the best run and the fitted model. The Model includes the pipeline and any pre-processing. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*."
] ]
}, },
{ {
@@ -324,26 +324,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Register the Fitted Model for Deployment" "### Testing the best Fitted Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"description = 'AutoML Model'\n",
"tags = None\n",
"local_run.register_model(description = description, tags = tags)\n",
"local_run.model_id # Use this id to deploy the model as a web service in Azure."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Testing the Fitted Model"
] ]
}, },
{ {
@@ -372,6 +353,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -137,17 +137,17 @@
" shuffle = True, random_state = 42,\n", " shuffle = True, random_state = 42,\n",
" remove = remove)\n", " remove = remove)\n",
"\n", "\n",
"X_train, X_validation, y_train, y_validation = train_test_split(data_train.data, data_train.target, test_size = 0.33, random_state = 42)\n", "X_train, X_valid, y_train, y_valid = train_test_split(data_train.data, data_train.target, test_size = 0.33, random_state = 42)\n",
"\n", "\n",
"\n", "\n",
"vectorizer = HashingVectorizer(stop_words = 'english', alternate_sign = False,\n", "vectorizer = HashingVectorizer(stop_words = 'english', alternate_sign = False,\n",
" n_features = 2**16)\n", " n_features = 2**16)\n",
"X_train = vectorizer.transform(X_train)\n", "X_train = vectorizer.transform(X_train)\n",
"X_validation = vectorizer.transform(X_validation)\n", "X_valid = vectorizer.transform(X_valid)\n",
"\n", "\n",
"summary_df = pd.DataFrame(index = ['No of Samples', 'No of Features'])\n", "summary_df = pd.DataFrame(index = ['No of Samples', 'No of Features'])\n",
"summary_df['Train Set'] = [X_train.shape[0], X_train.shape[1]]\n", "summary_df['Train Set'] = [X_train.shape[0], X_train.shape[1]]\n",
"summary_df['Validation Set'] = [X_validation.shape[0], X_validation.shape[1]]\n", "summary_df['Validation Set'] = [X_valid.shape[0], X_valid.shape[1]]\n",
"summary_df" "summary_df"
] ]
}, },
@@ -188,8 +188,8 @@
" verbosity = logging.INFO,\n", " verbosity = logging.INFO,\n",
" X = X_train, \n", " X = X_train, \n",
" y = y_train,\n", " y = y_train,\n",
" X_valid = X_validation, \n", " X_valid = X_valid, \n",
" y_valid = y_validation, \n", " y_valid = y_valid, \n",
" path = project_folder)" " path = project_folder)"
] ]
}, },
@@ -197,7 +197,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Train the Model\n", "## Train the Models\n",
"\n", "\n",
"Call the `submit` method on the experiment object and pass the run configuration. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n", "Call the `submit` method on the experiment object and pass the run configuration. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n",
"In this example, we specify `show_output = True` to print currently running iterations to the console." "In this example, we specify `show_output = True` to print currently running iterations to the console."
@@ -266,20 +266,13 @@
"rundata" "rundata"
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Retrieve the Best Model\n", "### Retrieve the Best Model\n",
"\n", "\n",
"Below we select the best pipeline from our iterations. The `get_output` method on `automl_classifier` returns the best run and the fitted model for the last invocation. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*." "Below we select the best pipeline from our iterations. The `get_output` method returns the best run and the fitted model. The Model includes the pipeline and any pre-processing. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*."
] ]
}, },
{ {
@@ -331,26 +324,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Register the Fitted Model for Deployment" "### Testing the Best Fitted Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"description = 'AutoML Model'\n",
"tags = None\n",
"local_run.register_model(description = description, tags = tags)\n",
"local_run.model_id # Use this id to deploy the model as a web service in Azure."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Testing the Fitted Model"
] ]
}, },
{ {
@@ -360,25 +334,12 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# Load test data.\n", "# Load test data.\n",
"import sklearn\n",
"from pandas_ml import ConfusionMatrix\n", "from pandas_ml import ConfusionMatrix\n",
"\n", "\n",
"remove = ('headers', 'footers', 'quotes')\n",
"categories = [\n",
" 'alt.atheism',\n",
" 'talk.religion.misc',\n",
" 'comp.graphics',\n",
" 'sci.space',\n",
"]\n",
"\n",
"\n",
"data_test = fetch_20newsgroups(subset = 'test', categories = categories,\n", "data_test = fetch_20newsgroups(subset = 'test', categories = categories,\n",
" shuffle = True, random_state = 42,\n", " shuffle = True, random_state = 42,\n",
" remove = remove)\n", " remove = remove)\n",
"\n", "\n",
"vectorizer = HashingVectorizer(stop_words = 'english', alternate_sign = False,\n",
" n_features = 2**16)\n",
"\n",
"X_test = vectorizer.transform(data_test.data)\n", "X_test = vectorizer.transform(data_test.data)\n",
"y_test = data_test.target\n", "y_test = data_test.target\n",
"\n", "\n",
@@ -395,6 +356,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -247,7 +247,8 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Register fitted model for deployment" "# Register fitted model for deployment\n",
"If neither `metric` nor `iteration` are specified in the `register_model` call, the iteration with the best primary metric is registered."
] ]
}, },
{ {
@@ -304,6 +305,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -15,7 +15,7 @@
"source": [ "source": [
"# AutoML 08: Remote Execution with DataStore\n", "# AutoML 08: Remote Execution with DataStore\n",
"\n", "\n",
"In this sample accesses a data file on a remote DSVM through DataStore. Advantagets of using data store\n", "This sample accesses a data file on a remote DSVM through DataStore. Advantages of using data store are:\n",
"1. DataStore secures the access details.\n", "1. DataStore secures the access details.\n",
"2. DataStore supports read, write to blob and file store\n", "2. DataStore supports read, write to blob and file store\n",
"3. AutoML natively supports copying data from DataStore to DSVM\n", "3. AutoML natively supports copying data from DataStore to DSVM\n",
@@ -23,8 +23,8 @@
"Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n", "Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n",
"\n", "\n",
"In this notebook you would see\n", "In this notebook you would see\n",
"1. Configuring the DSVM to allow files to be access directly by the get_data method.\n", "1. Storing data in DataStore.\n",
"2. get_data returning data from a local file.\n", "2. get_data returning data from DataStore.\n",
"\n" "\n"
] ]
}, },
@@ -285,11 +285,11 @@
" le = LabelEncoder()\n", " le = LabelEncoder()\n",
" le.fit(df[\"Label\"].values)\n", " le.fit(df[\"Label\"].values)\n",
" y = le.transform(df[\"Label\"].values)\n", " y = le.transform(df[\"Label\"].values)\n",
" df = df.drop([\"Label\"], axis=1)\n", " X = df.drop([\"Label\"], axis=1)\n",
"\n", "\n",
" df_train, _, y_train, _ = train_test_split(df, y, test_size=0.1, random_state=42)\n", " X_train, _, y_train, _ = train_test_split(X, y, test_size=0.1, random_state=42)\n",
"\n", "\n",
" return { \"X\" : df.values, \"y\" : y }" " return { \"X\" : X_train.values, \"y\" : y_train }"
] ]
}, },
{ {
@@ -300,7 +300,7 @@
"\n", "\n",
"You can specify automl_settings as **kwargs** as well. Also note that you can use the get_data() symantic for local excutions too. \n", "You can specify automl_settings as **kwargs** as well. Also note that you can use the get_data() symantic for local excutions too. \n",
"\n", "\n",
"<i>Note: For Remote DSVM and Batch AI you cannot pass Numpy arrays directly to the fit method.</i>\n", "<i>Note: For Remote DSVM and Batch AI you cannot pass Numpy arrays directly to AutoMLConfig.</i>\n",
"\n", "\n",
"|Property|Description|\n", "|Property|Description|\n",
"|-|-|\n", "|-|-|\n",
@@ -342,7 +342,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Training the Model <a class=\"anchor\" id=\"Training-the-model-Remote-DSVM\"></a>\n", "## Training the Models <a class=\"anchor\" id=\"Training-the-model-Remote-DSVM\"></a>\n",
"\n", "\n",
"For remote runs the execution is asynchronous, so you will see the iterations get populated as they complete. You can interact with the widgets/models even when the experiment is running to retreive the best model up to that point. Once you are satisfied with the model you can cancel a particular iteration or the whole run." "For remote runs the execution is asynchronous, so you will see the iterations get populated as they complete. You can interact with the widgets/models even when the experiment is running to retreive the best model up to that point. Once you are satisfied with the model you can cancel a particular iteration or the whole run."
] ]
@@ -410,7 +410,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Canceling runs\n", "## Canceling Runs\n",
"You can cancel ongoing remote runs using the *cancel()* and *cancel_iteration()* functions" "You can cancel ongoing remote runs using the *cancel()* and *cancel_iteration()* functions"
] ]
}, },
@@ -433,7 +433,7 @@
"source": [ "source": [
"### Retrieve the Best Model\n", "### Retrieve the Best Model\n",
"\n", "\n",
"Below we select the best pipeline from our iterations. The *get_output* method on automl_classifier returns the best run and the fitted model for the last *fit* invocation. There are overloads on *get_output* that allow you to retrieve the best run and fitted model for *any* logged metric or a particular *iteration*." "Below we select the best pipeline from our iterations. The *get_output* method returns the best run and the fitted model. There are overloads on *get_output* that allow you to retrieve the best run and fitted model for *any* logged metric or a particular *iteration*."
] ]
}, },
{ {
@@ -483,26 +483,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Register fitted model for deployment" "### Testing the Best Fitted Model <a class=\"anchor\" id=\"Testing-the-Fitted-Model-Remote-DSVM\"></a>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#description = 'AutoML Model'\n",
"#tags = None\n",
"#remote_run.register_model(description=description, tags=tags)\n",
"#remote_run.model_id # Use this id to deploy the model as a web service in Azure"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Testing the Fitted Model <a class=\"anchor\" id=\"Testing-the-Fitted-Model-Remote-DSVM\"></a>\n"
] ]
}, },
{ {
@@ -523,11 +504,11 @@
"le = LabelEncoder()\n", "le = LabelEncoder()\n",
"le.fit(df[\"Label\"].values)\n", "le.fit(df[\"Label\"].values)\n",
"y = le.transform(df[\"Label\"].values)\n", "y = le.transform(df[\"Label\"].values)\n",
"df = df.drop([\"Label\"], axis=1)\n", "X = df.drop([\"Label\"], axis=1)\n",
"\n", "\n",
"_, df_test, _, y_test = train_test_split(df, y, test_size=0.1, random_state=42)\n", "_, X_test, _, y_test = train_test_split(X, y, test_size=0.1, random_state=42)\n",
"\n", "\n",
"ypred = fitted_model.predict(df_test.values)\n", "ypred = fitted_model.predict(X_test.values)\n",
"\n", "\n",
"ypred_strings = le.inverse_transform(ypred)\n", "ypred_strings = le.inverse_transform(ypred)\n",
"ytest_strings = le.inverse_transform(y_test)\n", "ytest_strings = le.inverse_transform(y_test)\n",
@@ -541,6 +522,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -25,8 +25,9 @@
"3. Train the model using local compute.\n", "3. Train the model using local compute.\n",
"4. Explore the results.\n", "4. Explore the results.\n",
"5. Register the model.\n", "5. Register the model.\n",
"6. Create a container image and create and ACI service.\n", "6. Create a container image.\n",
"7. Test the ACI service.\n" "7. Create an Azure Container Instance (ACI) service.\n",
"8. Test the ACI service.\n"
] ]
}, },
{ {
@@ -155,7 +156,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Train the Model\n", "## Train the Models\n",
"\n", "\n",
"Call the `submit` method on the experiment object and pass the run configuration. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n", "Call the `submit` method on the experiment object and pass the run configuration. Execution of local runs is synchronous. Depending on the data and the number of iterations this can run for a while.\n",
"In this example, we specify `show_output = True` to print currently running iterations to the console." "In this example, we specify `show_output = True` to print currently running iterations to the console."
@@ -192,7 +193,8 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Register the Fitted Model for Deployment" "### Register the Fitted Model for Deployment\n",
"If neither `metric` nor `iteration` are specified in the `register_model` call, the iteration with the best primary metric is registered."
] ]
}, },
{ {
@@ -203,7 +205,7 @@
"source": [ "source": [
"description = 'AutoML Model'\n", "description = 'AutoML Model'\n",
"tags = None\n", "tags = None\n",
"model = local_run.register_model(description = description, tags = tags, iteration = 8)\n", "model = local_run.register_model(description = description, tags = tags)\n",
"local_run.model_id # This will be written to the script file later in the notebook." "local_run.model_id # This will be written to the script file later in the notebook."
] ]
}, },
@@ -256,7 +258,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"To ensure the consistency of the fit results with the training results, the SDK dependency versions need to be the same as the environment that trains the model. Details about retrieving the versions can be found in notebook [12.auto-ml-retrieve-the-training-sdk-versions](12.auto-ml-retrieve-the-training-sdk-versions.ipynb)." "To ensure the fit results are consistent with the training results, the SDK dependency versions need to be the same as the environment that trains the model. Details about retrieving the versions can be found in notebook [12.auto-ml-retrieve-the-training-sdk-versions](12.auto-ml-retrieve-the-training-sdk-versions.ipynb)."
] ]
}, },
{ {
@@ -304,7 +306,6 @@
" - pip:\n", " - pip:\n",
" - numpy==1.14.2\n", " - numpy==1.14.2\n",
" - scikit-learn==0.19.2\n", " - scikit-learn==0.19.2\n",
" - pynacl==1.2.1\n",
" - azureml-sdk[notebooks,automl]==<<azureml-version>>" " - azureml-sdk[notebooks,automl]==<<azureml-version>>"
] ]
}, },
@@ -471,6 +472,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -15,7 +15,9 @@
"source": [ "source": [
"# AutoML 10: Multi-output\n", "# AutoML 10: Multi-output\n",
"\n", "\n",
"This notebook shows how to use AutoML to train multi-output problems by leveraging the correlation between the outputs using indicator vectors." "This notebook shows how to use AutoML to train multi-output problems by leveraging the correlation between the outputs using indicator vectors.\n",
"\n",
"Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook."
] ]
}, },
{ {
@@ -264,6 +266,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -15,7 +15,7 @@
"source": [ "source": [
"# AutoML 11: Sample Weight\n", "# AutoML 11: Sample Weight\n",
"\n", "\n",
"In this example we use the scikit-learn's [digit dataset](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html) to showcase how you can use sample weight with AutoML. Sample weight is used where some sample values are more important than others.\n", "In this example we use the scikit-learn's [digit dataset](http://scikit-learn.org/stable/datasets/index.html#optical-recognition-of-handwritten-digits-dataset) to showcase how you can use sample weight with AutoML. Sample weight is used where some sample values are more important than others.\n",
"\n", "\n",
"Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n", "Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook.\n",
"\n", "\n",
@@ -197,7 +197,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### Compare the Pipelines\n", "#### Compare the Models\n",
"The prediction from the sample weight model is more likely to correctly predict 4's. However, it is also more likely to predict 4 for some images that are not labelled as 4." "The prediction from the sample weight model is more likely to correctly predict 4's. However, it is also more likely to predict 4 for some images that are not labelled as 4."
] ]
}, },
@@ -223,6 +223,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -13,7 +13,11 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# AutoML 12: Retrieving Training SDK Versions" "# AutoML 12: Retrieving Training SDK Versions\n",
"\n",
"This example shows how to find the SDK versions used for an experiment.\n",
"\n",
"Make sure you have executed the [00.configuration](00.configuration.ipynb) before running this notebook."
] ]
}, },
{ {
@@ -63,7 +67,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# 1. Retrieve the SDK versions in the current environment" "# Retrieve the SDK versions in the current environment"
] ]
}, },
{ {
@@ -86,7 +90,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# 2. Train model using AutoML" "# Train models using AutoML"
] ]
}, },
{ {
@@ -142,7 +146,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# 3. Retrieve the SDK versions from RunHistory" "# Retrieve the SDK versions from RunHistory"
] ]
}, },
{ {
@@ -219,6 +223,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -174,11 +174,11 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"automl_settings = {\n", "automl_settings = {\n",
" \"max_time_sec\": 600,\n", " \"max_time_sec\" : 600,\n",
" \"iterations\": 2,\n", " \"iterations\" : 2,\n",
" \"primary_metric\": 'AUC_weighted',\n", " \"primary_metric\" : 'AUC_weighted',\n",
" \"preprocess\": False,\n", " \"preprocess\" : False,\n",
" \"verbosity\": logging.INFO,\n", " \"verbosity\" : logging.INFO,\n",
" \"n_cross_validations\": 3\n", " \"n_cross_validations\": 3\n",
"}" "}"
] ]
@@ -225,8 +225,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Remote Run\n", "## Remote Run"
"*Note: This feature might not work properly in your workspace region before the October update. You may jump to the \"Exploring the results\" section below to explore other features AutoML and DataPrep has to offer.*"
] ]
}, },
{ {
@@ -269,8 +268,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"cd = CondaDependencies()\n", "cd = CondaDependencies()\n",
"cd.add_pip_package(pip_package='azureml-dataprep')\n", "cd.add_pip_package(pip_package='azureml-dataprep')"
"cd.add_pip_package(pip_package='tornado==4.5.1')"
] ]
}, },
{ {
@@ -313,9 +311,7 @@
" X = X,\n", " X = X,\n",
" y = y,\n", " y = y,\n",
" **automl_settings)\n", " **automl_settings)\n",
"# Please uncomment the line below to try out remote run with dataprep. \n", "remote_run = experiment.submit(automl_config, show_output = True)"
"# This feature might not work properly in your workspace region before the October update.\n",
"# remote_run = experiment.submit(automl_config, show_output = True)"
] ]
}, },
{ {
@@ -378,7 +374,7 @@
"source": [ "source": [
"### Retrieve the Best Model\n", "### Retrieve the Best Model\n",
"\n", "\n",
"Below we select the best pipeline from our iterations. The `get_output` method on `automl_classifier` returns the best run and the fitted model for the last invocation. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*." "Below we select the best pipeline from our iterations. The `get_output` method returns the best run and the fitted model. Overloads on `get_output` allow you to retrieve the best run and fitted model for *any* logged metric or for a particular *iteration*."
] ]
}, },
{ {
@@ -459,7 +455,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"#### Testing Our Best Pipeline\n", "#### Testing Our Best Fitted Model\n",
"We will try to predict 2 digits and see how our model works." "We will try to predict 2 digits and see how our model works."
] ]
}, },
@@ -534,6 +530,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "savitam"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -1,52 +1,24 @@
# Table of Contents # Table of Contents
1. [Automated ML Introduction](#introduction) 1. [Auto ML Introduction](#introduction)
1. [Running samples in Azure Notebooks](#jupyter) 2. [Running samples in a Local Conda environment](#localconda)
1. [Running samples in a Local Conda environment](#localconda) 3. [Auto ML SDK Sample Notebooks](#samples)
1. [Automated ML SDK Sample Notebooks](#samples) 4. [Documentation](#documentation)
1. [Documentation](#documentation) 5. [Running using python command](#pythoncommand)
1. [Running using python command](#pythoncommand) 6. [Troubleshooting](#troubleshooting)
1. [Troubleshooting](#troubleshooting)
<a name="introduction"></a>
# Automated ML introduction
Automated machine learning (automated ML) builds high quality machine learning models for you by automating model and hyperparameter selection. Bring a labelled dataset that you want to build a model for, automated ML will give you a high quality machine learning model that you can use for predictions.
# Auto ML Introduction <a name="introduction"></a>
AutoML builds high quality Machine Learning models for you by automating model and hyperparameter selection. Bring a labelled dataset that you want to build a model for, AutoML will give you a high quality machine learning model that you can use for predictions.
If you are new to Data Science, AutoML will help you get jumpstarted by simplifying machine learning model building. It abstracts you from needing to perform model selection, hyperparameter selection and in one step creates a high quality trained model for you to use. If you are new to Data Science, AutoML will help you get jumpstarted by simplifying machine learning model building. It abstracts you from needing to perform model selection, hyperparameter selection and in one step creates a high quality trained model for you to use.
If you are an experienced data scientist, AutoML will help increase your productivity by intelligently performing the model and hyperparameter selection for your training and generates high quality models much quicker than manually specifying several combinations of the parameters and running training jobs. AutoML provides visibility and access to all the training jobs and the performance characteristics of the models to help you further tune the pipeline if you desire. If you are an experienced data scientist, AutoML will help increase your productivity by intelligently performing the model and hyperparameter selection for your training and generates high quality models much quicker than manually specifying several combinations of the parameters and running training jobs. AutoML provides visibility and access to all the training jobs and the performance characteristics of the models to help you further tune the pipeline if you desire.
<a name="jupyter"></a>
## Running samples in Azure Notebooks - Jupyter based notebooks in the Azure cloud
1. [![Azure Notebooks](https://notebooks.azure.com/launch.png)](https://aka.ms/aml-clone-azure-notebooks) # Running samples in a Local Conda environment <a name="localconda"></a>
[Import sample notebooks ](https://aka.ms/aml-clone-azure-notebooks) into Azure Notebooks.
1. Follow the instructions in the [../00.configuration](00.configuration.ipynb) notebook to create and connect to a workspace.
1. Open one of the sample notebooks.
**Make sure the Azure Notebook kernel is set to `Python 3.6`** when you open a notebook. You can run these notebooks in Azure Notebooks without any extra installation. To run these notebook on your own notebook server, use these installation instructions.
![set kernel to Python 3.6](../images/python36.png)
<a name="localconda"></a>
## Running samples in a Local Conda environment
To run these notebook on your own notebook server, use these installation instructions.
The instructions below will install everything you need and then start a Jupyter notebook. To start your Jupyter notebook manually, use:
```
conda activate azure_automl
jupyter notebook
```
or on Mac:
```
source activate azure_automl
jupyter notebook
```
It is best if you create a new conda environment locally to try this SDK, so it doesn't mess up with your existing Python environment.
### 1. Install mini-conda from [here](https://conda.io/miniconda.html), choose Python 3.7 or higher. ### 1. Install mini-conda from [here](https://conda.io/miniconda.html), choose Python 3.7 or higher.
- **Note**: if you already have conda installed, you can keep using it but it should be version 4.4.10 or later (as shown by: conda -V). If you have a previous version installed, you can update it using the command: conda update conda. - **Note**: if you already have conda installed, you can keep using it but it should be version 4.4.10 or later (as shown by: conda -V). If you have a previous version installed, you can update it using the command: conda update conda.
@@ -76,19 +48,19 @@ bash automl_setup_mac.sh
cd to the **automl** folder where the sample notebooks were extracted and then run: cd to the **automl** folder where the sample notebooks were extracted and then run:
``` ```
bash automl_setup_linux.sh automl_setup_linux.sh
``` ```
### 4. Running configuration.ipynb ### 4. Running configuration.ipynb
- Before running any samples you next need to run the configuration notebook. Click on 00.configuration.ipynb notebook - Before running any samples you next need to run the configuration notebook. Click on 00.configuration.ipynb notebook
- Please make sure you use the Python [conda env:azure_automl] kernel when running this notebook.
- Execute the cells in the notebook to Register Machine Learning Services Resource Provider and create a workspace. (*instructions in notebook*) - Execute the cells in the notebook to Register Machine Learning Services Resource Provider and create a workspace. (*instructions in notebook*)
### 5. Running Samples ### 5. Running Samples
- Please make sure you use the Python [conda env:azure_automl] kernel when trying the sample Notebooks. - Please make sure you use the Python [conda env:azure_automl] kernel when trying the sample Notebooks.
- Follow the instructions in the individual notebooks to explore various features in AutoML - Follow the instructions in the individual notebooks to explore various features in AutoML
<a name="samples"></a> # Auto ML SDK Sample Notebooks <a name="samples"></a>
# Automated ML SDK Sample Notebooks
- [00.configuration.ipynb](00.configuration.ipynb) - [00.configuration.ipynb](00.configuration.ipynb)
- Register Machine Learning Services Resource Provider - Register Machine Learning Services Resource Provider
- Create new Azure ML Workspace - Create new Azure ML Workspace
@@ -115,7 +87,7 @@ bash automl_setup_linux.sh
- [03b.auto-ml-remote-batchai.ipynb](03b.auto-ml-remote-batchai.ipynb) - [03b.auto-ml-remote-batchai.ipynb](03b.auto-ml-remote-batchai.ipynb)
- Dataset: scikit learn's [digit dataset](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html#sklearn.datasets.load_digits) - Dataset: scikit learn's [digit dataset](http://scikit-learn.org/stable/modules/generated/sklearn.datasets.load_digits.html#sklearn.datasets.load_digits)
- Example of using automated ML for classification using a remote Batch AI compute for training - Example of using Auto ML for classification using a remote Batch AI compute for training
- Parallel execution of iterations - Parallel execution of iterations
- Async tracking of progress - Async tracking of progress
- Cancelling individual iterations or entire run - Cancelling individual iterations or entire run
@@ -171,17 +143,20 @@ bash automl_setup_linux.sh
- [13.auto-ml-dataprep.ipynb](13.auto-ml-dataprep.ipynb) - [13.auto-ml-dataprep.ipynb](13.auto-ml-dataprep.ipynb)
- Using DataPrep for reading data - Using DataPrep for reading data
<a name="documentation"></a> - [14a.auto-ml-classification-ensemble.ipynb](14a.auto-ml-classification-ensemble.ipynb)
# Documentation - Classification with ensembling
- [14b.auto-ml-regression-ensemble.ipynb](14b.auto-ml-regression-ensemble.ipynb)
- Regression with ensembling
# Documentation <a name="documentation"></a>
## Table of Contents ## Table of Contents
1. [Automated ML Settings ](#automlsettings) 1. [Auto ML Settings ](#automlsettings)
1. [Cross validation split options](#cvsplits) 2. [Cross validation split options](#cvsplits)
1. [Get Data Syntax](#getdata) 3. [Get Data Syntax](#getdata)
1. [Data pre-processing and featurization](#preprocessing) 4. [Data pre-processing and featurization](#preprocessing)
<a name="automlsettings"></a>
## Automated ML Settings
## Auto ML Settings <a name="automlsettings"></a>
|Property|Description|Default| |Property|Description|Default|
|-|-|-| |-|-|-|
|**primary_metric**|This is the metric that you want to optimize.<br><br> Classification supports the following primary metrics <br><i>accuracy</i><br><i>AUC_weighted</i><br><i>balanced_accuracy</i><br><i>average_precision_score_weighted</i><br><i>precision_score_weighted</i><br><br> Regression supports the following primary metrics <br><i>spearman_correlation</i><br><i>normalized_root_mean_squared_error</i><br><i>r2_score</i><br><i>normalized_mean_absolute_error</i><br><i>normalized_root_mean_squared_log_error</i>| Classification: accuracy <br><br> Regression: spearman_correlation |**primary_metric**|This is the metric that you want to optimize.<br><br> Classification supports the following primary metrics <br><i>accuracy</i><br><i>AUC_weighted</i><br><i>balanced_accuracy</i><br><i>average_precision_score_weighted</i><br><i>precision_score_weighted</i><br><br> Regression supports the following primary metrics <br><i>spearman_correlation</i><br><i>normalized_root_mean_squared_error</i><br><i>r2_score</i><br><i>normalized_mean_absolute_error</i><br><i>normalized_root_mean_squared_log_error</i>| Classification: accuracy <br><br> Regression: spearman_correlation
@@ -195,8 +170,7 @@ bash automl_setup_linux.sh
|**exit_score**|*double* value indicating the target for *primary_metric*. <br> Once the target is surpassed the run terminates|None| |**exit_score**|*double* value indicating the target for *primary_metric*. <br> Once the target is surpassed the run terminates|None|
|**blacklist_algos**|*Array* of *strings* indicating pipelines to ignore for Auto ML.<br><br> Allowed values for **Classification**<br><i>LogisticRegression</i><br><i>SGDClassifierWrapper</i><br><i>NBWrapper</i><br><i>BernoulliNB</i><br><i>SVCWrapper</i><br><i>LinearSVMWrapper</i><br><i>KNeighborsClassifier</i><br><i>DecisionTreeClassifier</i><br><i>RandomForestClassifier</i><br><i>ExtraTreesClassifier</i><br><i>gradient boosting</i><br><i>LightGBMClassifier</i><br><br>Allowed values for **Regression**<br><i>ElasticNet</i><br><i>GradientBoostingRegressor</i><br><i>DecisionTreeRegressor</i><br><i>KNeighborsRegressor</i><br><i>LassoLars</i><br><i>SGDRegressor</i><br><i>RandomForestRegressor</i><br><i>ExtraTreesRegressor</i>|None| |**blacklist_algos**|*Array* of *strings* indicating pipelines to ignore for Auto ML.<br><br> Allowed values for **Classification**<br><i>LogisticRegression</i><br><i>SGDClassifierWrapper</i><br><i>NBWrapper</i><br><i>BernoulliNB</i><br><i>SVCWrapper</i><br><i>LinearSVMWrapper</i><br><i>KNeighborsClassifier</i><br><i>DecisionTreeClassifier</i><br><i>RandomForestClassifier</i><br><i>ExtraTreesClassifier</i><br><i>gradient boosting</i><br><i>LightGBMClassifier</i><br><br>Allowed values for **Regression**<br><i>ElasticNet</i><br><i>GradientBoostingRegressor</i><br><i>DecisionTreeRegressor</i><br><i>KNeighborsRegressor</i><br><i>LassoLars</i><br><i>SGDRegressor</i><br><i>RandomForestRegressor</i><br><i>ExtraTreesRegressor</i>|None|
<a name="cvsplits"></a> ## Cross validation split options <a name="cvsplits"></a>
## Cross validation split options
### K-Folds Cross Validation ### K-Folds Cross Validation
Use *n_cross_validations* setting to specify the number of cross validations. The training data set will be randomly split into *n_cross_validations* folds of equal size. During each cross validation round, one of the folds will be used for validation of the model trained on the remaining folds. This process repeats for *n_cross_validations* rounds until each fold is used once as validation set. Finally, the average scores accross all *n_cross_validations* rounds will be reported, and the corresponding model will be retrained on the whole training data set. Use *n_cross_validations* setting to specify the number of cross validations. The training data set will be randomly split into *n_cross_validations* folds of equal size. During each cross validation round, one of the folds will be used for validation of the model trained on the remaining folds. This process repeats for *n_cross_validations* rounds until each fold is used once as validation set. Finally, the average scores accross all *n_cross_validations* rounds will be reported, and the corresponding model will be retrained on the whole training data set.
@@ -206,8 +180,7 @@ Use *validation_size* to specify the percentage of the training data set that sh
### Custom train and validation set ### Custom train and validation set
You can specify seperate train and validation set either through the get_data() or directly to the fit method. You can specify seperate train and validation set either through the get_data() or directly to the fit method.
<a name="getdata"></a> ## get_data() syntax <a name="getdata"></a>
## get_data() syntax
The *get_data()* function can be used to return a dictionary with these values: The *get_data()* function can be used to return a dictionary with these values:
|Key|Type|Dependency|Mutually Exclusive with|Description| |Key|Type|Dependency|Mutually Exclusive with|Description|
@@ -223,23 +196,21 @@ The *get_data()* function can be used to return a dictionary with these values:
|columns|Array of strings|data_train||*Optional* Whitelist of columns to use for features| |columns|Array of strings|data_train||*Optional* Whitelist of columns to use for features|
|cv_splits_indices|Array of integers|data_train||*Optional* List of indexes to split the data for cross validation| |cv_splits_indices|Array of integers|data_train||*Optional* List of indexes to split the data for cross validation|
<a name="preprocessing"></a> ## Data pre-processing and featurization <a name="preprocessing"></a>
## Data pre-processing and featurization If you use "preprocess=True", the following data preprocessing steps are performed automatically for you:
If you use `preprocess=True`, the following data preprocessing steps are performed automatically for you: ### 1. Dropping high cardinality or no variance features
- Features with no useful information are dropped from training and validation sets. These include features with all values missing, same value across all rows or with extremely high cardinality (e.g., hashes, IDs or GUIDs).
### 2. Missing value imputation
- For numerical features, missing values are imputed with average of values in the column.
- For categorical features, missing values are imputed with most frequent value.
### 3. Generating additional features
- For DateTime features: Year, Month, Day, Day of week, Day of year, Quarter, Week of the year, Hour, Minute, Second.
- For Text features: Term frequency based on bi-grams and tri-grams, Count vectorizer.
### 4. Transformations and encodings
- Numeric features with very few unique values are transformed into categorical features.
- Depending on cardinality of categorical features label encoding or (hashing) one-hot encoding is performed.
1. Dropping high cardinality or no variance features # Running using python command <a name="pythoncommand"></a>
- Features with no useful information are dropped from training and validation sets. These include features with all values missing, same value across all rows or with extremely high cardinality (e.g., hashes, IDs or GUIDs).
2. Missing value imputation
- For numerical features, missing values are imputed with average of values in the column.
- For categorical features, missing values are imputed with most frequent value.
3. Generating additional features
- For DateTime features: Year, Month, Day, Day of week, Day of year, Quarter, Week of the year, Hour, Minute, Second.
- For Text features: Term frequency based on bi-grams and tri-grams, Count vectorizer.
4. Transformations and encodings
- Numeric features with very few unique values are transformed into categorical features.
<a name="pythoncommand"></a>
# Running using python command
Jupyter notebook provides a File / Download as / Python (.py) option for saving the notebook as a Python file. Jupyter notebook provides a File / Download as / Python (.py) option for saving the notebook as a Python file.
You can then run this file using the python command. You can then run this file using the python command.
However, on Windows the file needs to be modified before it can be run. However, on Windows the file needs to be modified before it can be run.
@@ -249,8 +220,7 @@ The following condition must be added to the main code in the file:
The main code of the file must be indented so that it is under this condition. The main code of the file must be indented so that it is under this condition.
<a name="troubleshooting"></a> # Troubleshooting <a name="troubleshooting"></a>
# Troubleshooting
## Iterations fail and the log contains "MemoryError" ## Iterations fail and the log contains "MemoryError"
This can be caused by insufficient memory on the DSVM. AutoML loads all training data into memory. So, the available memory should be more than the training data size. This can be caused by insufficient memory on the DSVM. AutoML loads all training data into memory. So, the available memory should be more than the training data size.
If you are using a remote DSVM, memory is needed for each concurrent iteration. The concurrent_iterations setting specifies the maximum concurrent iterations. For example, if the training data size is 8Gb and concurrent_iterations is set to 10, the minimum memory required is at least 80Gb. If you are using a remote DSVM, memory is needed for each concurrent iteration. The concurrent_iterations setting specifies the maximum concurrent iterations. For example, if the training data size is 8Gb and concurrent_iterations is set to 10, the minimum memory required is at least 80Gb.

27
onnx/README.md Normal file
View File

@@ -0,0 +1,27 @@
# ONNX on Azure Machine Learning
These tutorials show how to create and deploy [ONNX](http://onnx.ai) models using Azure Machine Learning and the [ONNX Runtime](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-build-deploy-onnx).
Once deployed as web services, you can ping the models with your own images to be analyzed!
## Tutorials
- [Obtain ONNX model from ONNX Model Zoo and deploy - ResNet50](https://github.com/Azure/MachineLearningNotebooks/blob/master/onnx/onnx-modelzoo-aml-deploy-resnet50.ipynb)
- [Convert ONNX model from CoreML and deploy - TinyYOLO](https://github.com/Azure/MachineLearningNotebooks/blob/master/onnx/onnx-convert-aml-deploy-tinyyolo.ipynb)
- [Train ONNX model in PyTorch and deploy - MNIST](https://github.com/Azure/MachineLearningNotebooks/blob/master/onnx/onnx-train-pytorch-aml-deploy-mnist.ipynb)
- [Handwritten Digit Classification (MNIST) using ONNX Runtime on AzureML](https://github.com/Azure/MachineLearningNotebooks/blob/master/onnx/onnx-inference-mnist.ipynb)
- [Facial Expression Recognition using ONNX Runtime on AzureML](https://github.com/Azure/MachineLearningNotebooks/blob/master/onnx/onnx-inference-emotion-recognition.ipynb)
## Documentation
- [ONNX Runtime Python API Documentation](http://aka.ms/onnxruntime-python)
- [Azure Machine Learning API Documentation](http://aka.ms/aml-docs)
## Related Articles
- [Building and Deploying ONNX Runtime Models](https://docs.microsoft.com/en-us/azure/machine-learning/service/how-to-build-deploy-onnx)
- [Azure AI Making AI Real for Business](https://aka.ms/aml-blog-overview)
- [Whats new in Azure Machine Learning](https://aka.ms/aml-blog-whats-new)
## License
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.

124
onnx/mnist.py Normal file
View File

@@ -0,0 +1,124 @@
# This is a modified version of https://github.com/pytorch/examples/blob/master/mnist/main.py which is
# licensed under BSD 3-Clause (https://github.com/pytorch/examples/blob/master/LICENSE)
from __future__ import print_function
import argparse
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torchvision import datasets, transforms
import os
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
self.conv2_drop = nn.Dropout2d()
self.fc1 = nn.Linear(320, 50)
self.fc2 = nn.Linear(50, 10)
def forward(self, x):
x = F.relu(F.max_pool2d(self.conv1(x), 2))
x = F.relu(F.max_pool2d(self.conv2_drop(self.conv2(x)), 2))
x = x.view(-1, 320)
x = F.relu(self.fc1(x))
x = F.dropout(x, training=self.training)
x = self.fc2(x)
return F.log_softmax(x, dim=1)
def train(args, model, device, train_loader, optimizer, epoch, output_dir):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.step()
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
def test(args, model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.nll_loss(output, target, size_average=False, reduce=True).item() # sum up batch loss
pred = output.max(1, keepdim=True)[1] # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
def main():
# Training settings
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
parser.add_argument('--batch-size', type=int, default=64, metavar='N',
help='input batch size for training (default: 64)')
parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
help='input batch size for testing (default: 1000)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
help='learning rate (default: 0.01)')
parser.add_argument('--momentum', type=float, default=0.5, metavar='M',
help='SGD momentum (default: 0.5)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
help='how many batches to wait before logging training status')
parser.add_argument('--output-dir', type=str, default='outputs')
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
device = torch.device("cuda" if use_cuda else "cpu")
output_dir = args.output_dir
os.makedirs(output_dir, exist_ok=True)
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('data', train=True, download=True,
transform=transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
),
batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('data', train=False,
transform=transforms.Compose([transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))])
),
batch_size=args.test_batch_size, shuffle=True, **kwargs)
model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
for epoch in range(1, args.epochs + 1):
train(args, model, device, train_loader, optimizer, epoch, output_dir)
test(args, model, device, test_loader)
# save model
dummy_input = torch.randn(1, 1, 28, 28, device=device)
model_path = os.path.join(output_dir, 'mnist.onnx')
torch.onnx.export(model, dummy_input, model_path)
if __name__ == '__main__':
main()

View File

@@ -0,0 +1,431 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copyright (c) Microsoft Corporation. All rights reserved. \n",
"\n",
"Licensed under the MIT License."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# YOLO Real-time Object Detection using ONNX on AzureML\n",
"\n",
"This example shows how to convert the TinyYOLO model from CoreML to ONNX and operationalize it as a web service using Azure Machine Learning services and the ONNX Runtime.\n",
"\n",
"## What is ONNX\n",
"ONNX is an open format for representing machine learning and deep learning models. ONNX enables open and interoperable AI by enabling data scientists and developers to use the tools of their choice without worrying about lock-in and flexibility to deploy to a variety of platforms. ONNX is developed and supported by a community of partners including Microsoft, Facebook, and Amazon. For more information, explore the [ONNX website](http://onnx.ai).\n",
"\n",
"## YOLO Details\n",
"You Only Look Once (YOLO) is a state-of-the-art, real-time object detection system. For more information about YOLO, please visit the [YOLO website](https://pjreddie.com/darknet/yolo/)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prerequisites\n",
"\n",
"To make the best use of your time, make sure you have done the following:\n",
"\n",
"* Understand the [architecture and terms](https://docs.microsoft.com/azure/machine-learning/service/concept-azure-machine-learning-architecture) introduced by Azure Machine Learning\n",
"* Go through the [00.configuration.ipynb](../00.configuration.ipynb) notebook to:\n",
" * install the AML SDK\n",
" * create a workspace and its configuration file (config.json)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Check core SDK version number\n",
"import azureml.core\n",
"\n",
"print(\"SDK version:\", azureml.core.VERSION)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Install necessary packages\n",
"\n",
"You'll need to run the following commands to use this tutorial:\n",
"\n",
"```sh\n",
"pip install coremltools\n",
"pip install onnxmltools\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Convert model to ONNX\n",
"\n",
"First we download the CoreML model. We use the CoreML model listed at https://coreml.store/tinyyolo. This may take a few minutes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!wget https://s3-us-west-2.amazonaws.com/coreml-models/TinyYOLO.mlmodel"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we use ONNXMLTools to convert the model."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import onnxmltools\n",
"import coremltools\n",
"\n",
"# Load a CoreML model\n",
"coreml_model = coremltools.utils.load_spec('TinyYOLO.mlmodel')\n",
"\n",
"# Convert from CoreML into ONNX\n",
"onnx_model = onnxmltools.convert_coreml(coreml_model, 'TinyYOLOv2')\n",
"\n",
"# Save ONNX model\n",
"onnxmltools.utils.save_model(onnx_model, 'tinyyolov2.onnx')\n",
"\n",
"import os\n",
"print(os.path.getsize('tinyyolov2.onnx'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Deploying as a web service with Azure ML\n",
"\n",
"### Load Azure ML workspace\n",
"\n",
"We begin by instantiating a workspace object from the existing workspace created earlier in the configuration notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core import Workspace\n",
"\n",
"ws = Workspace.from_config()\n",
"print(ws.name, ws.location, ws.resource_group, sep = '\\n')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Registering your model with Azure ML\n",
"\n",
"Now we upload the model and register it in the workspace."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.model import Model\n",
"\n",
"model = Model.register(model_path = \"tinyyolov2.onnx\",\n",
" model_name = \"tinyyolov2\",\n",
" tags = {\"onnx\": \"demo\"},\n",
" description = \"TinyYOLO\",\n",
" workspace = ws)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Displaying your registered models\n",
"\n",
"You can optionally list out all the models that you have registered in this workspace."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"models = ws.models()\n",
"for m in models:\n",
" print(\"Name:\", m.name,\"\\tVersion:\", m.version, \"\\tDescription:\", m.description, m.tags)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Write scoring file\n",
"\n",
"We are now going to deploy our ONNX model on Azure ML using the ONNX Runtime. We begin by writing a score.py file that will be invoked by the web service call. The `init()` function is called once when the container is started so we load the model using the ONNX Runtime into a global session object."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%writefile score.py\n",
"import json\n",
"import time\n",
"import sys\n",
"import os\n",
"from azureml.core.model import Model\n",
"import numpy as np # we're going to use numpy to process input and output data\n",
"import onnxruntime # to inference ONNX models, we use the ONNX Runtime\n",
"\n",
"def init():\n",
" global session\n",
" model = Model.get_model_path(model_name = 'tinyyolov2')\n",
" session = onnxruntime.InferenceSession(model)\n",
"\n",
"def preprocess(input_data_json):\n",
" # convert the JSON data into the tensor input\n",
" return np.array(json.loads(input_data_json)['data']).astype('float32')\n",
"\n",
"def postprocess(result):\n",
" return np.array(result).tolist()\n",
"\n",
"def run(input_data_json):\n",
" try:\n",
" start = time.time() # start timer\n",
" input_data = preprocess(input_data_json)\n",
" input_name = session.get_inputs()[0].name # get the id of the first input of the model \n",
" result = session.run([], {input_name: input_data})\n",
" end = time.time() # stop timer\n",
" return {\"result\": postprocess(result),\n",
" \"time\": end - start}\n",
" except Exception as e:\n",
" result = str(e)\n",
" return {\"error\": result}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create container image\n",
"First we create a YAML file that specifies which dependencies we would like to see in our container."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.conda_dependencies import CondaDependencies \n",
"\n",
"myenv = CondaDependencies.create(pip_packages=[\"numpy\",\"onnxruntime\"])\n",
"\n",
"with open(\"myenv.yml\",\"w\") as f:\n",
" f.write(myenv.serialize_to_string())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we have Azure ML create the container. This step will likely take a few minutes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.image import ContainerImage\n",
"\n",
"image_config = ContainerImage.image_configuration(execution_script = \"score.py\",\n",
" runtime = \"python\",\n",
" conda_file = \"myenv.yml\",\n",
" description = \"TinyYOLO ONNX Demo\",\n",
" tags = {\"demo\": \"onnx\"}\n",
" )\n",
"\n",
"\n",
"image = ContainerImage.create(name = \"onnxyolo\",\n",
" models = [model],\n",
" image_config = image_config,\n",
" workspace = ws)\n",
"\n",
"image.wait_for_creation(show_output = True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In case you need to debug your code, the next line of code accesses the log file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(image.image_build_log_uri)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We're all set! Let's get our model chugging.\n",
"\n",
"### Deploy the container image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.webservice import AciWebservice\n",
"\n",
"aciconfig = AciWebservice.deploy_configuration(cpu_cores = 1, \n",
" memory_gb = 1, \n",
" tags = {'demo': 'onnx'}, \n",
" description = 'web service for TinyYOLO ONNX model')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following cell will likely take a few minutes to run as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.webservice import Webservice\n",
"from random import randint\n",
"\n",
"aci_service_name = 'onnx-tinyyolo'+str(randint(0,100))\n",
"print(\"Service\", aci_service_name)\n",
"\n",
"aci_service = Webservice.deploy_from_image(deployment_config = aciconfig,\n",
" image = image,\n",
" name = aci_service_name,\n",
" workspace = ws)\n",
"\n",
"aci_service.wait_for_deployment(True)\n",
"print(aci_service.state)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In case the deployment fails, you can check the logs. Make sure to delete your aci_service before trying again."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if aci_service.state != 'Healthy':\n",
" # run this command for debugging.\n",
" print(aci_service.get_logs())\n",
" aci_service.delete()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Success!\n",
"\n",
"If you've made it this far, you've deployed a working web service that does object detection using an ONNX model. You can get the URL for the webservice with the code below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(aci_service.scoring_uri)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you are eventually done using the web service, remember to delete it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#aci_service.delete()"
]
}
],
"metadata": {
"authors": [
{
"name": "onnx"
}
],
"kernelspec": {
"display_name": "Python 3.6",
"language": "python",
"name": "python36"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

View File

@@ -12,7 +12,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Facial Expression Recognition using ONNX Runtime on AzureML\n", "# Facial Expression Recognition (Emotion FER+) using ONNX Runtime on Azure ML\n",
"\n", "\n",
"This example shows how to deploy an image classification neural network using the Facial Expression Recognition ([FER](https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data)) dataset and Open Neural Network eXchange format ([ONNX](http://aka.ms/onnxdocarticle)) on the Azure Machine Learning platform. This tutorial will show you how to deploy a FER+ model from the [ONNX model zoo](https://github.com/onnx/models), use it to make predictions using ONNX Runtime Inference, and deploy it as a web service in Azure.\n", "This example shows how to deploy an image classification neural network using the Facial Expression Recognition ([FER](https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data)) dataset and Open Neural Network eXchange format ([ONNX](http://aka.ms/onnxdocarticle)) on the Azure Machine Learning platform. This tutorial will show you how to deploy a FER+ model from the [ONNX model zoo](https://github.com/onnx/models), use it to make predictions using ONNX Runtime Inference, and deploy it as a web service in Azure.\n",
"\n", "\n",
@@ -34,32 +34,54 @@
"## Prerequisites\n", "## Prerequisites\n",
"\n", "\n",
"### 1. Install Azure ML SDK and create a new workspace\n", "### 1. Install Azure ML SDK and create a new workspace\n",
"Please follow [00.configuration.ipynb](https://github.com/Azure/MachineLearningNotebooks/blob/master/00.configuration.ipynb) notebook.\n", "Please follow [Azure ML configuration notebook](https://github.com/Azure/MachineLearningNotebooks/blob/master/00.configuration.ipynb) to set up your environment.\n",
"\n",
"\n", "\n",
"### 2. Install additional packages needed for this Notebook\n", "### 2. Install additional packages needed for this Notebook\n",
"You need to install the popular plotting library `matplotlib`, the image manipulation library `PIL`, and the `onnx` library in the conda environment where Azure Maching Learning SDK is installed.\n", "You need to install the popular plotting library `matplotlib`, the image manipulation library `opencv`, and the `onnx` library in the conda environment where Azure Maching Learning SDK is installed.\n",
"\n", "\n",
"```sh\n", "```sh\n",
"(myenv) $ pip install matplotlib onnx Pillow\n", "(myenv) $ pip install matplotlib onnx opencv-python\n",
"```\n", "```\n",
"\n", "\n",
"**Debugging tip**: Make sure that to activate your virtual environment (myenv) before you re-launch this notebook using the `jupyter notebook` comand. Choose the respective Python kernel for your new virtual environment using the `Kernel > Change Kernel` menu above. If you have completed the steps correctly, the upper right corner of your screen should state `Python [conda env:myenv]` instead of `Python [default]`.\n",
"\n",
"### 3. Download sample data and pre-trained ONNX model from ONNX Model Zoo.\n", "### 3. Download sample data and pre-trained ONNX model from ONNX Model Zoo.\n",
"\n", "\n",
"[Download the ONNX Emotion FER+ model and corresponding test data](https://www.cntk.ai/OnnxModels/emotion_ferplus/opset_7/emotion_ferplus.tar.gz) and place them in the same folder as this tutorial notebook. You can unzip the file through the following line of code.\n", "In the following lines of code, we download [the trained ONNX Emotion FER+ model and corresponding test data](https://github.com/onnx/models/tree/master/emotion_ferplus) and place them in the same folder as this tutorial notebook. For more information about the FER+ dataset, please visit Microsoft Researcher Emad Barsoum's [FER+ source data repository](https://github.com/ebarsoum/FERPlus)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# urllib is a built-in Python library to download files from URLs\n",
"\n", "\n",
"```sh\n", "# Objective: retrieve the latest version of the ONNX Emotion FER+ model files from the\n",
"(myenv) $ tar xvzf emotion_ferplus.tar.gz\n", "# ONNX Model Zoo and save it in the same folder as this tutorial\n",
"```\n",
"\n", "\n",
"More information can be found about the ONNX FER+ model on [github](https://github.com/onnx/models/tree/master/emotion_ferplus). For more information about the FER+ dataset, please visit Microsoft Researcher Emad Barsoum's [FER+ source data repository](https://github.com/ebarsoum/FERPlus)." "import urllib.request\n",
"\n",
"onnx_model_url = \"https://www.cntk.ai/OnnxModels/emotion_ferplus/opset_7/emotion_ferplus.tar.gz\"\n",
"\n",
"urllib.request.urlretrieve(onnx_model_url, filename=\"emotion_ferplus.tar.gz\")\n",
"\n",
"# the ! magic command tells our jupyter notebook kernel to run the following line of \n",
"# code from the command line instead of the notebook kernel\n",
"\n",
"# We use tar and xvcf to unzip the files we just retrieved from the ONNX model zoo\n",
"\n",
"!tar xvzf emotion_ferplus.tar.gz"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Load Azure ML workspace\n", "## Deploy a VM with your ONNX model in the Cloud\n",
"\n",
"### Load Azure ML workspace\n",
"\n", "\n",
"We begin by instantiating a workspace object from the existing workspace created earlier in the configuration notebook." "We begin by instantiating a workspace object from the existing workspace created earlier in the configuration notebook."
] ]
@@ -137,8 +159,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"models = ws.models()\n", "models = ws.models()\n",
"for m in models:\n", "for name, m in models.items():\n",
" print(\"Name:\", m.name,\"\\tVersion:\", m.version, \"\\tDescription:\", m.description, m.tags)" " print(\"Name:\", name,\"\\tVersion:\", m.version, \"\\tDescription:\", m.description, m.tags)"
] ]
}, },
{ {
@@ -147,9 +169,9 @@
"source": [ "source": [
"### ONNX FER+ Model Methodology\n", "### ONNX FER+ Model Methodology\n",
"\n", "\n",
"The image classification model we are using is pre-trained using Microsoft's deep learning cognitive toolkit, [CNTK](https://github.com/Microsoft/CNTK), from the [ONNX model zoo](http://github.com/onnx/models). The model zoo has many other models that can be deployed on cloud providers like AzureML without any additional training. To ensure that our cloud deployed model works, we use testing data from the famous FER+ data set, provided as part of the [trained Emotion Recognition model](https://github.com/onnx/models/tree/master/emotion_ferplus) in the ONNX model zoo.\n", "The image classification model we are using is pre-trained using Microsoft's deep learning cognitive toolkit, [CNTK](https://github.com/Microsoft/CNTK), from the [ONNX model zoo](http://github.com/onnx/models). The model zoo has many other models that can be deployed on cloud providers like AzureML without any additional training. To ensure that our cloud deployed model works, we use testing data from the well-known FER+ data set, provided as part of the [trained Emotion Recognition model](https://github.com/onnx/models/tree/master/emotion_ferplus) in the ONNX model zoo.\n",
"\n", "\n",
"The original Facial Emotion Recognition (FER) Dataset was released in 2013, but some of the labels are not entirely appropriate for the expression. In the FER+ Dataset, each photo was evaluated by at least 10 croud sourced reviewers, creating a better basis for ground truth. \n", "The original Facial Emotion Recognition (FER) Dataset was released in 2013 by Pierre-Luc Carrier and Aaron Courville as part of a [Kaggle Competition](https://www.kaggle.com/c/challenges-in-representation-learning-facial-expression-recognition-challenge/data), but some of the labels are not entirely appropriate for the expression. In the FER+ Dataset, each photo was evaluated by at least 10 croud sourced reviewers, creating a more accurate basis for ground truth. \n",
"\n", "\n",
"You can see the difference of label quality in the sample model input below. The FER labels are the first word below each image, and the FER+ labels are the second word below each image.\n", "You can see the difference of label quality in the sample model input below. The FER labels are the first word below each image, and the FER+ labels are the second word below each image.\n",
"\n", "\n",
@@ -202,20 +224,18 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Deploy our model on Azure ML" "### Specify our Score and Environment Files"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"We are now going to deploy our ONNX Model on AML with inference in ONNX Runtime. We begin by writing a score.py file, which will help us run the model in our Azure ML virtual machine (VM), and then specify our environment by writing a yml file.\n", "We are now going to deploy our ONNX Model on AML with inference in ONNX Runtime. We begin by writing a score.py file, which will help us run the model in our Azure ML virtual machine (VM), and then specify our environment by writing a yml file. You will also notice that we import the onnxruntime library to do runtime inference on our ONNX models (passing in input and evaluating out model's predicted output). More information on the API and commands can be found in the [ONNX Runtime documentation](https://aka.ms/onnxruntime).\n",
"\n",
"You will also notice that we import the onnxruntime library to do runtime inference on our ONNX models (passing in input and evaluating out model's predicted output). More information on the API and commands can be found in the [ONNX Runtime documentation](https://aka.ms/onnxruntime).\n",
"\n", "\n",
"### Write Score File\n", "### Write Score File\n",
"\n", "\n",
"A score file is what tells our Azure cloud service what to do. After initializing our model using azureml.core.model, we start an ONNX Runtime GPU inference session to evaluate the data passed in on our function calls." "A score file is what tells our Azure cloud service what to do. After initializing our model using azureml.core.model, we start an ONNX Runtime inference session to evaluate the data passed in on our function calls."
] ]
}, },
{ {
@@ -248,10 +268,13 @@
" try:\n", " try:\n",
" # load in our data, convert to readable format\n", " # load in our data, convert to readable format\n",
" data = np.array(json.loads(input_data)['data']).astype('float32')\n", " data = np.array(json.loads(input_data)['data']).astype('float32')\n",
" \n",
" start = time.time()\n", " start = time.time()\n",
" r = session.run([output_name], {input_name : data})\n", " r = session.run([output_name], {input_name : data})\n",
" end = time.time()\n", " end = time.time()\n",
" \n",
" result = emotion_map(postprocess(r[0]))\n", " result = emotion_map(postprocess(r[0]))\n",
" \n",
" result_dict = {\"result\": result,\n", " result_dict = {\"result\": result,\n",
" \"time_in_sec\": [end - start]}\n", " \"time_in_sec\": [end - start]}\n",
" except Exception as e:\n", " except Exception as e:\n",
@@ -260,9 +283,12 @@
" return json.dumps(result_dict)\n", " return json.dumps(result_dict)\n",
"\n", "\n",
"def emotion_map(classes, N=1):\n", "def emotion_map(classes, N=1):\n",
" \"\"\"Take the most probable labels (output of postprocess) and returns the top N emotional labels that fit the picture.\"\"\"\n", " \"\"\"Take the most probable labels (output of postprocess) and returns the \n",
" top N emotional labels that fit the picture.\"\"\"\n",
" \n",
" emotion_table = {'neutral':0, 'happiness':1, 'surprise':2, 'sadness':3, \n",
" 'anger':4, 'disgust':5, 'fear':6, 'contempt':7}\n",
" \n", " \n",
" emotion_table = {'neutral':0, 'happiness':1, 'surprise':2, 'sadness':3, 'anger':4, 'disgust':5, 'fear':6, 'contempt':7}\n",
" emotion_keys = list(emotion_table.keys())\n", " emotion_keys = list(emotion_table.keys())\n",
" emotions = []\n", " emotions = []\n",
" for i in range(N):\n", " for i in range(N):\n",
@@ -276,8 +302,8 @@
" return e_x / e_x.sum(axis=0)\n", " return e_x / e_x.sum(axis=0)\n",
"\n", "\n",
"def postprocess(scores):\n", "def postprocess(scores):\n",
" \"\"\"This function takes the scores generated by the network and returns the class IDs in decreasing \n", " \"\"\"This function takes the scores generated by the network and \n",
" order of probability.\"\"\"\n", " returns the class IDs in decreasing order of probability.\"\"\"\n",
" prob = softmax(scores)\n", " prob = softmax(scores)\n",
" prob = np.squeeze(prob)\n", " prob = np.squeeze(prob)\n",
" classes = np.argsort(prob)[::-1]\n", " classes = np.argsort(prob)[::-1]\n",
@@ -329,7 +355,7 @@
"image_config = ContainerImage.image_configuration(execution_script = \"score.py\",\n", "image_config = ContainerImage.image_configuration(execution_script = \"score.py\",\n",
" runtime = \"python\",\n", " runtime = \"python\",\n",
" conda_file = \"myenv.yml\",\n", " conda_file = \"myenv.yml\",\n",
" description = \"test\",\n", " description = \"Emotion ONNX Runtime container\",\n",
" tags = {\"demo\": \"onnx\"})\n", " tags = {\"demo\": \"onnx\"})\n",
"\n", "\n",
"\n", "\n",
@@ -346,8 +372,6 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Debugging\n",
"\n",
"In case you need to debug your code, the next line of code accesses the log file." "In case you need to debug your code, the next line of code accesses the log file."
] ]
}, },
@@ -364,9 +388,9 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"We're all set! Let's get our model chugging.\n", "We're all done specifying what we want our virtual machine to do. Let's configure and deploy our container image.\n",
"\n", "\n",
"## Deploy the container image" "### Deploy the container image"
] ]
}, },
{ {
@@ -439,23 +463,57 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Testing and Evaluation" "## Testing and Evaluation\n",
] "\n",
}, "### Useful Helper Functions\n",
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Useful Helper Functions\n",
"\n", "\n",
"We preprocess and postprocess our data (see score.py file) using the helper functions specified in the [ONNX FER+ Model page in the Model Zoo repository](https://github.com/onnx/models/tree/master/emotion_ferplus)." "We preprocess and postprocess our data (see score.py file) using the helper functions specified in the [ONNX FER+ Model page in the Model Zoo repository](https://github.com/onnx/models/tree/master/emotion_ferplus)."
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def emotion_map(classes, N=1):\n",
" \"\"\"Take the most probable labels (output of postprocess) and returns the \n",
" top N emotional labels that fit the picture.\"\"\"\n",
" \n",
" emotion_table = {'neutral':0, 'happiness':1, 'surprise':2, 'sadness':3, \n",
" 'anger':4, 'disgust':5, 'fear':6, 'contempt':7}\n",
" \n",
" emotion_keys = list(emotion_table.keys())\n",
" emotions = []\n",
" for i in range(N):\n",
" emotions.append(emotion_keys[classes[i]])\n",
" \n",
" return emotions\n",
"\n",
"def softmax(x):\n",
" \"\"\"Compute softmax values (probabilities from 0 to 1) for each possible label.\"\"\"\n",
" x = x.reshape(-1)\n",
" e_x = np.exp(x - np.max(x))\n",
" return e_x / e_x.sum(axis=0)\n",
"\n",
"def postprocess(scores):\n",
" \"\"\"This function takes the scores generated by the network and \n",
" returns the class IDs in decreasing order of probability.\"\"\"\n",
" prob = softmax(scores)\n",
" prob = np.squeeze(prob)\n",
" classes = np.argsort(prob)[::-1]\n",
" return classes"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Load Test Data" "### Load Test Data\n",
"\n",
"These are already in your directory from your ONNX model download (from the model zoo).\n",
"\n",
"Notice that our Model Zoo files have a .pb extension. This is because they are [protobuf files (Protocol Buffers)](https://developers.google.com/protocol-buffers/docs/pythontutorial), so we need to read in our data through our ONNX TensorProto reader into a format we can work with, like numerical arrays."
] ]
}, },
{ {
@@ -475,8 +533,6 @@
"import json\n", "import json\n",
"import os\n", "import os\n",
"\n", "\n",
"from score import emotion_map, softmax, postprocess\n",
"\n",
"test_inputs = []\n", "test_inputs = []\n",
"test_outputs = []\n", "test_outputs = []\n",
"\n", "\n",
@@ -512,7 +568,7 @@
}, },
"source": [ "source": [
"### Show some sample images\n", "### Show some sample images\n",
"We use `matplotlib` to plot 3 test images from the model zoo with their labels over them." "We use `matplotlib` to plot 3 test images from the dataset."
] ]
}, },
{ {
@@ -532,7 +588,7 @@
" plt.axhline('')\n", " plt.axhline('')\n",
" plt.axvline('')\n", " plt.axvline('')\n",
" plt.text(x = 10, y = -10, s = test_outputs[test_image], fontsize = 18)\n", " plt.text(x = 10, y = -10, s = test_outputs[test_image], fontsize = 18)\n",
" plt.imshow(test_inputs[test_image].reshape(64, 64), cmap = plt.cm.Greys)\n", " plt.imshow(test_inputs[test_image].reshape(64, 64), cmap = plt.cm.gray)\n",
"plt.show()" "plt.show()"
] ]
}, },
@@ -571,7 +627,7 @@
" print(r['error'])\n", " print(r['error'])\n",
" break\n", " break\n",
" \n", " \n",
" result = r['result'][0][0]\n", " result = r['result'][0]\n",
" time_ms = np.round(r['time_in_sec'][0] * 1000, 2)\n", " time_ms = np.round(r['time_in_sec'][0] * 1000, 2)\n",
" \n", " \n",
" ground_truth = test_outputs[i]\n", " ground_truth = test_outputs[i]\n",
@@ -583,7 +639,7 @@
"\n", "\n",
" # use different color for misclassified sample\n", " # use different color for misclassified sample\n",
" font_color = 'red' if ground_truth != result else 'black'\n", " font_color = 'red' if ground_truth != result else 'black'\n",
" clr_map = plt.cm.gray if ground_truth != result else plt.cm.Greys\n", " clr_map = plt.cm.Greys if ground_truth != result else plt.cm.gray\n",
"\n", "\n",
" # ground truth labels are in blue\n", " # ground truth labels are in blue\n",
" plt.text(x = 10, y = -70, s = ground_truth, fontsize = 18, color = 'blue')\n", " plt.text(x = 10, y = -70, s = ground_truth, fontsize = 18, color = 'blue')\n",
@@ -611,15 +667,30 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"from PIL import Image\n", "# Preprocessing functions take your image and format it so it can be passed\n",
"# as input into our ONNX model\n",
"\n", "\n",
"def preprocess(image_path):\n", "import cv2\n",
" input_shape = (1, 1, 64, 64)\n", "\n",
" img = Image.open(image_path)\n", "def rgb2gray(rgb):\n",
" img = img.resize((64, 64), Image.ANTIALIAS)\n", " \"\"\"Convert the input image into grayscale\"\"\"\n",
" img_data = np.array(img)\n", " return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])\n",
" img_data = np.resize(img_data, input_shape)\n", "\n",
" return img_data" "def resize_img(img):\n",
" \"\"\"Resize image to MNIST model input dimensions\"\"\"\n",
" img = cv2.resize(img, dsize=(64, 64), interpolation=cv2.INTER_AREA)\n",
" img.resize((1, 1, 64, 64))\n",
" return img\n",
"\n",
"def preprocess(img):\n",
" \"\"\"Resize input images and convert them to grayscale.\"\"\"\n",
" if img.shape == (64, 64):\n",
" img.resize((1, 1, 64, 64))\n",
" return img\n",
" \n",
" grayscale = rgb2gray(img)\n",
" processed_img = resize_img(grayscale)\n",
" return processed_img"
] ]
}, },
{ {
@@ -636,12 +707,15 @@
"\n", "\n",
"# e.g. your_test_image = \"C://Users//vinitra.swamy//Pictures//emotion_test_images//img_1.png\"\n", "# e.g. your_test_image = \"C://Users//vinitra.swamy//Pictures//emotion_test_images//img_1.png\"\n",
"\n", "\n",
"your_test_image = \"<path to file>\"\n", "import matplotlib.image as mpimg\n",
"\n", "\n",
"if your_test_image != \"<path to file>\":\n", "if your_test_image != \"<path to file>\":\n",
" img = preprocess(your_test_image)\n", " img = mpimg.imread(your_test_image)\n",
" plt.subplot(1,3,1)\n", " plt.subplot(1,3,1)\n",
" plt.imshow(img.reshape((64,64)), cmap = plt.cm.gray)\n", " plt.imshow(img, cmap = plt.cm.Greys)\n",
" print(\"Old Dimensions: \", img.shape)\n",
" img = preprocess(img)\n",
" print(\"New Dimensions: \", img.shape)\n",
"else:\n", "else:\n",
" img = None" " img = None"
] ]
@@ -659,7 +733,7 @@
"\n", "\n",
" try:\n", " try:\n",
" r = json.loads(aci_service.run(input_data))\n", " r = json.loads(aci_service.run(input_data))\n",
" result = r['result'][0][0]\n", " result = r['result'][0]\n",
" time_ms = np.round(r['time_in_sec'][0] * 1000, 2)\n", " time_ms = np.round(r['time_in_sec'][0] * 1000, 2)\n",
" except Exception as e:\n", " except Exception as e:\n",
" print(str(e))\n", " print(str(e))\n",
@@ -668,12 +742,13 @@
" plt.subplot(1,8,1)\n", " plt.subplot(1,8,1)\n",
" plt.axhline('')\n", " plt.axhline('')\n",
" plt.axvline('')\n", " plt.axvline('')\n",
" plt.text(x = -10, y = -35, s = \"Model prediction: \", fontsize = 14)\n", " plt.text(x = -10, y = -40, s = \"Model prediction: \", fontsize = 14)\n",
" plt.text(x = -10, y = -20, s = \"Inference time: \", fontsize = 14)\n", " plt.text(x = -10, y = -25, s = \"Inference time: \", fontsize = 14)\n",
" plt.text(x = 100, y = -35, s = str(result), fontsize = 14)\n", " plt.text(x = 100, y = -40, s = str(result), fontsize = 14)\n",
" plt.text(x = 100, y = -20, s = str(time_ms) + \" ms\", fontsize = 14)\n", " plt.text(x = 100, y = -25, s = str(time_ms) + \" ms\", fontsize = 14)\n",
" plt.text(x = -10, y = -8, s = \"Input image: \", fontsize = 14)\n", " plt.text(x = -10, y = -10, s = \"Model Input image: \", fontsize = 14)\n",
" plt.imshow(img.reshape(64, 64), cmap = plt.cm.gray) " " plt.imshow(img.reshape((64, 64)), cmap = plt.cm.gray) \n",
" "
] ]
}, },
{ {
@@ -684,7 +759,7 @@
"source": [ "source": [
"# remember to delete your service after you are done using it!\n", "# remember to delete your service after you are done using it!\n",
"\n", "\n",
"# aci_service.delete()" "aci_service.delete()"
] ]
}, },
{ {
@@ -708,6 +783,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "viswamy"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",
@@ -723,7 +803,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.5" "version": "3.6.6"
}, },
"msauthor": "vinitra.swamy" "msauthor": "vinitra.swamy"
}, },

View File

@@ -12,7 +12,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Handwritten Digit Classification (MNIST) using ONNX Runtime on AzureML\n", "# Handwritten Digit Classification (MNIST) using ONNX Runtime on Azure ML\n",
"\n", "\n",
"This example shows how to deploy an image classification neural network using the Modified National Institute of Standards and Technology ([MNIST](http://yann.lecun.com/exdb/mnist/)) dataset and Open Neural Network eXchange format ([ONNX](http://aka.ms/onnxdocarticle)) on the Azure Machine Learning platform. MNIST is a popular dataset consisting of 70,000 grayscale images. Each image is a handwritten digit of 28x28 pixels, representing number from 0 to 9. This tutorial will show you how to deploy a MNIST model from the [ONNX model zoo](https://github.com/onnx/models), use it to make predictions using ONNX Runtime Inference, and deploy it as a web service in Azure.\n", "This example shows how to deploy an image classification neural network using the Modified National Institute of Standards and Technology ([MNIST](http://yann.lecun.com/exdb/mnist/)) dataset and Open Neural Network eXchange format ([ONNX](http://aka.ms/onnxdocarticle)) on the Azure Machine Learning platform. MNIST is a popular dataset consisting of 70,000 grayscale images. Each image is a handwritten digit of 28x28 pixels, representing number from 0 to 9. This tutorial will show you how to deploy a MNIST model from the [ONNX model zoo](https://github.com/onnx/models), use it to make predictions using ONNX Runtime Inference, and deploy it as a web service in Azure.\n",
"\n", "\n",
@@ -22,9 +22,9 @@
"\n", "\n",
"#### Tutorial Objectives:\n", "#### Tutorial Objectives:\n",
"\n", "\n",
"1. Describe the MNIST dataset and pretrained Convolutional Neural Net ONNX model, stored in the ONNX model zoo.\n", "- Describe the MNIST dataset and pretrained Convolutional Neural Net ONNX model, stored in the ONNX model zoo.\n",
"2. Deploy and run the pretrained MNIST ONNX model on an Azure Machine Learning instance\n", "- Deploy and run the pretrained MNIST ONNX model on an Azure Machine Learning instance\n",
"3. Predict labels for test set data points in the cloud using ONNX Runtime and Azure ML" "- Predict labels for test set data points in the cloud using ONNX Runtime and Azure ML"
] ]
}, },
{ {
@@ -34,31 +34,61 @@
"## Prerequisites\n", "## Prerequisites\n",
"\n", "\n",
"### 1. Install Azure ML SDK and create a new workspace\n", "### 1. Install Azure ML SDK and create a new workspace\n",
"Please follow [00.configuration.ipynb](https://github.com/Azure/MachineLearningNotebooks/blob/master/00.configuration.ipynb) notebook.\n", "Please follow [Azure ML configuration notebook](https://github.com/Azure/MachineLearningNotebooks/blob/master/00.configuration.ipynb) to set up your environment.\n",
"\n", "\n",
"### 2. Install additional packages needed for this Notebook\n", "### 2. Install additional packages needed for this tutorial notebook\n",
"You need to install the popular plotting library `matplotlib`, the image manipulation library `opencv`, and the `onnx` library in the conda environment where Azure Maching Learning SDK is installed.\n", "You need to install the popular plotting library `matplotlib`, the image manipulation library `opencv`, and the `onnx` library in the conda environment where Azure Maching Learning SDK is installed. \n",
"\n", "\n",
"```sh\n", "```sh\n",
"(myenv) $ pip install matplotlib onnx opencv-python\n", "(myenv) $ pip install matplotlib onnx opencv-python\n",
"```\n", "```\n",
"\n", "\n",
"**Debugging tip**: Make sure that you run the \"jupyter notebook\" command to launch this notebook after activating your virtual environment. Choose the respective Python kernel for your new virtual environment using the `Kernel > Change Kernel` menu above. If you have completed the steps correctly, the upper right corner of your screen should state `Python [conda env:myenv]` instead of `Python [default]`.\n",
"\n",
"### 3. Download sample data and pre-trained ONNX model from ONNX Model Zoo.\n", "### 3. Download sample data and pre-trained ONNX model from ONNX Model Zoo.\n",
"\n", "\n",
"[Download the ONNX MNIST model and corresponding test data](https://www.cntk.ai/OnnxModels/mnist/opset_7/mnist.tar.gz) and place them in the same folder as this tutorial notebook. You can unzip the file through the following line of code.\n", "In the following lines of code, we download [the trained ONNX MNIST model and corresponding test data](https://github.com/onnx/models/tree/master/mnist) and place them in the same folder as this tutorial notebook. For more information about the MNIST dataset, please visit [Yan LeCun's website](http://yann.lecun.com/exdb/mnist/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# urllib is a built-in Python library to download files from URLs\n",
"\n", "\n",
"```sh\n", "# Objective: retrieve the latest version of the ONNX MNIST model files from the\n",
"(myenv) $ tar xvzf mnist.tar.gz\n", "# ONNX Model Zoo and save it in the same folder as this tutorial\n",
"```\n",
"\n", "\n",
"More information can be found about the ONNX MNIST model on [github](https://github.com/onnx/models/tree/master/mnist). For more information about the MNIST dataset, please visit [Yan LeCun's website](http://yann.lecun.com/exdb/mnist/)." "import urllib.request\n",
"\n",
"onnx_model_url = \"https://www.cntk.ai/OnnxModels/mnist/opset_7/mnist.tar.gz\"\n",
"\n",
"urllib.request.urlretrieve(onnx_model_url, filename=\"mnist.tar.gz\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# the ! magic command tells our jupyter notebook kernel to run the following line of \n",
"# code from the command line instead of the notebook kernel\n",
"\n",
"# We use tar and xvcf to unzip the files we just retrieved from the ONNX model zoo\n",
"\n",
"!tar xvzf mnist.tar.gz"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Load Azure ML workspace\n", "## Deploy a VM with your ONNX model in the Cloud\n",
"\n",
"### Load Azure ML workspace\n",
"\n", "\n",
"We begin by instantiating a workspace object from the existing workspace created earlier in the configuration notebook." "We begin by instantiating a workspace object from the existing workspace created earlier in the configuration notebook."
] ]
@@ -113,11 +143,11 @@
"source": [ "source": [
"from azureml.core.model import Model\n", "from azureml.core.model import Model\n",
"\n", "\n",
"model = Model.register(model_path = model_dir + \"//model.onnx\",\n", "model = Model.register(workspace = ws,\n",
" model_path = model_dir + \"/\" + \"model.onnx\",\n",
" model_name = \"mnist_1\",\n", " model_name = \"mnist_1\",\n",
" tags = {\"onnx\": \"demo\"},\n", " tags = {\"onnx\": \"demo\"},\n",
" description = \"MNIST image classification CNN from ONNX Model Zoo\",\n", " description = \"MNIST image classification CNN from ONNX Model Zoo\",)"
" workspace = ws)"
] ]
}, },
{ {
@@ -136,8 +166,8 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"models = ws.models()\n", "models = ws.models()\n",
"for m in models:\n", "for name, m in models.items():\n",
" print(\"Name:\", m.name,\"\\tVersion:\", m.version, \"\\tDescription:\", m.description, m.tags)" " print(\"Name:\", name,\"\\tVersion:\", m.version, \"\\tDescription:\", m.description, m.tags)"
] ]
}, },
{ {
@@ -188,16 +218,14 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## Deploy our model on Azure ML" "### Specify our Score and Environment Files"
] ]
}, },
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"We are now going to deploy our ONNX Model on AML with inference in ONNX Runtime. We begin by writing a score.py file, which will help us run the model in our Azure ML virtual machine (VM), and then specify our environment by writing a yml file.\n", "We are now going to deploy our ONNX Model on AML with inference in ONNX Runtime. We begin by writing a score.py file, which will help us run the model in our Azure ML virtual machine (VM), and then specify our environment by writing a yml file. You will also notice that we import the onnxruntime library to do runtime inference on our ONNX models (passing in input and evaluating out model's predicted output). More information on the API and commands can be found in the [ONNX Runtime documentation](https://aka.ms/onnxruntime).\n",
"\n",
"You will also notice that we import the onnxruntime library to do runtime inference on our ONNX models (passing in input and evaluating out model's predicted output). More information on the API and commands can be found in the [ONNX Runtime documentation](https://aka.ms/onnxruntime).\n",
"\n", "\n",
"### Write Score File\n", "### Write Score File\n",
"\n", "\n",
@@ -248,7 +276,7 @@
" return json.dumps(result_dict)\n", " return json.dumps(result_dict)\n",
"\n", "\n",
"def choose_class(result_prob):\n", "def choose_class(result_prob):\n",
" \"\"\"We use argmax to determine the right label to choose from our output, after calling softmax on the 10 numbers we receive\"\"\"\n", " \"\"\"We use argmax to determine the right label to choose from our output\"\"\"\n",
" return int(np.argmax(result_prob, axis=0))" " return int(np.argmax(result_prob, axis=0))"
] ]
}, },
@@ -256,14 +284,9 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Write Environment File" "### Write Environment File\n",
] "\n",
}, "This step creates a YAML environment file that specifies which dependencies we would like to see in our Linux Virtual Machine."
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This step creates a YAML file that specifies which dependencies we would like to see in our Linux Virtual Machine."
] ]
}, },
{ {
@@ -289,10 +312,19 @@
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Create the Container Image\n", "### Create the Container Image\n",
"\n",
"This step will likely take a few minutes." "This step will likely take a few minutes."
] ]
}, },
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.image import ContainerImage\n",
"help(ContainerImage.image_configuration)"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
@@ -304,8 +336,8 @@
"image_config = ContainerImage.image_configuration(execution_script = \"score.py\",\n", "image_config = ContainerImage.image_configuration(execution_script = \"score.py\",\n",
" runtime = \"python\",\n", " runtime = \"python\",\n",
" conda_file = \"myenv.yml\",\n", " conda_file = \"myenv.yml\",\n",
" description = \"test\",\n", " description = \"MNIST ONNX Runtime container\",\n",
" tags = {\"demo\": \"onnx\"}) )\n", " tags = {\"demo\": \"onnx\"}) \n",
"\n", "\n",
"\n", "\n",
"image = ContainerImage.create(name = \"onnxtest\",\n", "image = ContainerImage.create(name = \"onnxtest\",\n",
@@ -321,8 +353,6 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Debugging\n",
"\n",
"In case you need to debug your code, the next line of code accesses the log file." "In case you need to debug your code, the next line of code accesses the log file."
] ]
}, },
@@ -339,9 +369,9 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"We're all set! Let's get our model chugging.\n", "We're all done specifying what we want our virtual machine to do. Let's configure and deploy our container image.\n",
"\n", "\n",
"## Deploy the container image" "### Deploy the container image"
] ]
}, },
{ {
@@ -373,7 +403,7 @@
"source": [ "source": [
"from azureml.core.webservice import Webservice\n", "from azureml.core.webservice import Webservice\n",
"\n", "\n",
"aci_service_name = 'onnx-demo-mnist'\n", "aci_service_name = 'onnx-demo-mnist20'\n",
"print(\"Service\", aci_service_name)\n", "print(\"Service\", aci_service_name)\n",
"\n", "\n",
"aci_service = Webservice.deploy_from_image(deployment_config = aciconfig,\n", "aci_service = Webservice.deploy_from_image(deployment_config = aciconfig,\n",
@@ -414,16 +444,13 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Testing and Evaluation" "## Testing and Evaluation\n",
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Load Test Data\n",
"\n", "\n",
"These are already in your directory from your ONNX model download (from the model zoo). If you didn't place your model and test data in the same directory as this notebook, edit the \"model_dir\" filename below." "### Load Test Data\n",
"\n",
"These are already in your directory from your ONNX model download (from the model zoo).\n",
"\n",
"Notice that our Model Zoo files have a .pb extension. This is because they are [protobuf files (Protocol Buffers)](https://developers.google.com/protocol-buffers/docs/pythontutorial), so we need to read in our data through our ONNX TensorProto reader into a format we can work with, like numerical arrays."
] ]
}, },
{ {
@@ -579,7 +606,9 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Preprocessing functions\n", "# Preprocessing functions take your image and format it so it can be passed\n",
"# as input into our ONNX model\n",
"\n",
"import cv2\n", "import cv2\n",
"\n", "\n",
"def rgb2gray(rgb):\n", "def rgb2gray(rgb):\n",
@@ -587,12 +616,17 @@
" return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])\n", " return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])\n",
"\n", "\n",
"def resize_img(img):\n", "def resize_img(img):\n",
" \"\"\"Resize image to MNIST model input dimensions\"\"\"\n",
" img = cv2.resize(img, dsize=(28, 28), interpolation=cv2.INTER_AREA)\n", " img = cv2.resize(img, dsize=(28, 28), interpolation=cv2.INTER_AREA)\n",
" img.resize((1, 1, 28, 28))\n", " img.resize((1, 1, 28, 28))\n",
" return img\n", " return img\n",
"\n", "\n",
"def preprocess(img):\n", "def preprocess(img):\n",
" \"\"\"Resize input images and convert them to grayscale.\"\"\"\n", " \"\"\"Resize input images and convert them to grayscale.\"\"\"\n",
" if img.shape == (28, 28):\n",
" img.resize((1, 1, 28, 28))\n",
" return img\n",
" \n",
" grayscale = rgb2gray(img)\n", " grayscale = rgb2gray(img)\n",
" processed_img = resize_img(grayscale)\n", " processed_img = resize_img(grayscale)\n",
" return processed_img" " return processed_img"
@@ -608,11 +642,8 @@
"# Make sure your image is square and the dimensions are equal (i.e. 100 * 100 pixels or 28 * 28 pixels)\n", "# Make sure your image is square and the dimensions are equal (i.e. 100 * 100 pixels or 28 * 28 pixels)\n",
"\n", "\n",
"# Any PNG or JPG image file should work\n", "# Any PNG or JPG image file should work\n",
"# Make sure to include the entire path with // instead of /\n",
"\n", "\n",
"# e.g. your_test_image = \"C://Users//vinitra.swamy//Pictures//digit.png\"\n", "# e.g. your_test_image = \"C:/Users/vinitra.swamy/Pictures/handwritten_digit.png\"\n",
"\n",
"your_test_image = \"<path to file>\"\n",
"\n", "\n",
"import matplotlib.image as mpimg\n", "import matplotlib.image as mpimg\n",
"\n", "\n",
@@ -721,7 +752,7 @@
"source": [ "source": [
"# remember to delete your service after you are done using it!\n", "# remember to delete your service after you are done using it!\n",
"\n", "\n",
"# aci_service.delete()" "aci_service.delete()"
] ]
}, },
{ {
@@ -738,12 +769,17 @@
"- ensured that your deep learning model is working perfectly (in the cloud) on test data, and checked it against some of your own!\n", "- ensured that your deep learning model is working perfectly (in the cloud) on test data, and checked it against some of your own!\n",
"\n", "\n",
"Next steps:\n", "Next steps:\n",
"- Check out another interesting application based on a Microsoft Research computer vision paper that lets you set up a [facial emotion recognition model](https://github.com/Azure/MachineLearningNotebooks/tree/master/onnx/onnx-inference-emotion-recognition.ipynb) in the cloud! This tutorial deploys a pre-trained ONNX Computer Vision model in an Azure ML virtual machine with GPU support.\n", "- Check out another interesting application based on a Microsoft Research computer vision paper that lets you set up a [facial emotion recognition model](https://github.com/Azure/MachineLearningNotebooks/tree/master/onnx/onnx-inference-emotion-recognition.ipynb) in the cloud! This tutorial deploys a pre-trained ONNX Computer Vision model in an Azure ML virtual machine.\n",
"- Contribute to our [open source ONNX repository on github](http://github.com/onnx/onnx) and/or add to our [ONNX model zoo](http://github.com/onnx/models)" "- Contribute to our [open source ONNX repository on github](http://github.com/onnx/onnx) and/or add to our [ONNX model zoo](http://github.com/onnx/models)"
] ]
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "viswamy"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",
@@ -759,7 +795,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.5" "version": "3.6.6"
}, },
"msauthor": "vinitra.swamy" "msauthor": "vinitra.swamy"
}, },

View File

@@ -0,0 +1,409 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Copyright (c) Microsoft Corporation. All rights reserved. \n",
"\n",
"Licensed under the MIT License."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# ResNet50 Image Classification using ONNX and AzureML\n",
"\n",
"This example shows how to deploy the ResNet50 ONNX model as a web service using Azure Machine Learning services and the ONNX Runtime.\n",
"\n",
"## What is ONNX\n",
"ONNX is an open format for representing machine learning and deep learning models. ONNX enables open and interoperable AI by enabling data scientists and developers to use the tools of their choice without worrying about lock-in and flexibility to deploy to a variety of platforms. ONNX is developed and supported by a community of partners including Microsoft, Facebook, and Amazon. For more information, explore the [ONNX website](http://onnx.ai).\n",
"\n",
"## ResNet50 Details\n",
"ResNet classifies the major object in an input image into a set of 1000 pre-defined classes. For more information about the ResNet50 model and how it was created can be found on the [ONNX Model Zoo github](https://github.com/onnx/models/tree/master/models/image_classification/resnet). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Prerequisites\n",
"\n",
"To make the best use of your time, make sure you have done the following:\n",
"\n",
"* Understand the [architecture and terms](https://docs.microsoft.com/azure/machine-learning/service/concept-azure-machine-learning-architecture) introduced by Azure Machine Learning\n",
"* Go through the [00.configuration.ipynb](../00.configuration.ipynb) notebook to:\n",
" * install the AML SDK\n",
" * create a workspace and its configuration file (config.json)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Check core SDK version number\n",
"import azureml.core\n",
"\n",
"print(\"SDK version:\", azureml.core.VERSION)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Download pre-trained ONNX model from ONNX Model Zoo.\n",
"\n",
"Download the [ResNet50v2 model and test data](https://s3.amazonaws.com/onnx-model-zoo/resnet/resnet50v2/resnet50v2.tar.gz) and place it in the same folder as this tutorial notebook. You can unzip the file through the following line of code.\n",
"\n",
"```sh\n",
"(myenv) $ tar xvzf resnet50v2.tar.gz\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Deploying as a web service with Azure ML"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load your Azure ML workspace\n",
"\n",
"We begin by instantiating a workspace object from the existing workspace created earlier in the configuration notebook."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core import Workspace\n",
"\n",
"ws = Workspace.from_config()\n",
"print(ws.name, ws.location, ws.resource_group, sep = '\\n')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Register your model with Azure ML\n",
"\n",
"Now we upload the model and register it in the workspace."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.model import Model\n",
"\n",
"model = Model.register(model_path = \"resnet50v2/resnet50v2.onnx\",\n",
" model_name = \"resnet50v2\",\n",
" tags = {\"onnx\": \"demo\"},\n",
" description = \"ResNet50v2 from ONNX Model Zoo\",\n",
" workspace = ws)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Displaying your registered models\n",
"\n",
"You can optionally list out all the models that you have registered in this workspace."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"models = ws.models()\n",
"for m in models:\n",
" print(\"Name:\", m.name,\"\\tVersion:\", m.version, \"\\tDescription:\", m.description, m.tags)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Write scoring file\n",
"\n",
"We are now going to deploy our ONNX model on Azure ML using the ONNX Runtime. We begin by writing a score.py file that will be invoked by the web service call. The `init()` function is called once when the container is started so we load the model using the ONNX Runtime into a global session object."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%writefile score.py\n",
"import json\n",
"import time\n",
"import sys\n",
"import os\n",
"from azureml.core.model import Model\n",
"import numpy as np # we're going to use numpy to process input and output data\n",
"import onnxruntime # to inference ONNX models, we use the ONNX Runtime\n",
"\n",
"def softmax(x):\n",
" x = x.reshape(-1)\n",
" e_x = np.exp(x - np.max(x))\n",
" return e_x / e_x.sum(axis=0)\n",
"\n",
"def init():\n",
" global session\n",
" model = Model.get_model_path(model_name = 'resnet50v2')\n",
" session = onnxruntime.InferenceSession(model, None)\n",
"\n",
"def preprocess(input_data_json):\n",
" # convert the JSON data into the tensor input\n",
" img_data = np.array(json.loads(input_data_json)['data']).astype('float32')\n",
" \n",
" #normalize\n",
" mean_vec = np.array([0.485, 0.456, 0.406])\n",
" stddev_vec = np.array([0.229, 0.224, 0.225])\n",
" norm_img_data = np.zeros(img_data.shape).astype('float32')\n",
" for i in range(img_data.shape[0]):\n",
" norm_img_data[i,:,:] = (img_data[i,:,:]/255 - mean_vec[i]) / stddev_vec[i]\n",
"\n",
" return norm_img_data\n",
"\n",
"def postprocess(result):\n",
" return softmax(np.array(result)).tolist()\n",
"\n",
"def run(input_data_json):\n",
" try:\n",
" start = time.time()\n",
" # load in our data which is expected as NCHW 224x224 image\n",
" input_data = preprocess(input_data_json)\n",
" input_name = session.get_inputs()[0].name # get the id of the first input of the model \n",
" result = session.run([], {input_name: input_data})\n",
" end = time.time() # stop timer\n",
" return {\"result\": postprocess(result),\n",
" \"time\": end - start}\n",
" except Exception as e:\n",
" result = str(e)\n",
" return {\"error\": result}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Create container image"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"First we create a YAML file that specifies which dependencies we would like to see in our container."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.conda_dependencies import CondaDependencies \n",
"\n",
"myenv = CondaDependencies.create(pip_packages=[\"numpy\",\"onnxruntime\"])\n",
"\n",
"with open(\"myenv.yml\",\"w\") as f:\n",
" f.write(myenv.serialize_to_string())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Then we have Azure ML create the container. This step will likely take a few minutes."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.image import ContainerImage\n",
"\n",
"image_config = ContainerImage.image_configuration(execution_script = \"score.py\",\n",
" runtime = \"python\",\n",
" conda_file = \"myenv.yml\",\n",
" description = \"ONNX ResNet50 Demo\",\n",
" tags = {\"demo\": \"onnx\"}\n",
" )\n",
"\n",
"\n",
"image = ContainerImage.create(name = \"onnxresnet50v2\",\n",
" models = [model],\n",
" image_config = image_config,\n",
" workspace = ws)\n",
"\n",
"image.wait_for_creation(show_output = True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In case you need to debug your code, the next line of code accesses the log file."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(image.image_build_log_uri)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We're all set! Let's get our model chugging.\n",
"\n",
"### Deploy the container image"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.webservice import AciWebservice\n",
"\n",
"aciconfig = AciWebservice.deploy_configuration(cpu_cores = 1, \n",
" memory_gb = 1, \n",
" tags = {'demo': 'onnx'}, \n",
" description = 'web service for ResNet50 ONNX model')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The following cell will likely take a few minutes to run as well."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from azureml.core.webservice import Webservice\n",
"from random import randint\n",
"\n",
"aci_service_name = 'onnx-demo-resnet50'+str(randint(0,100))\n",
"print(\"Service\", aci_service_name)\n",
"\n",
"aci_service = Webservice.deploy_from_image(deployment_config = aciconfig,\n",
" image = image,\n",
" name = aci_service_name,\n",
" workspace = ws)\n",
"\n",
"aci_service.wait_for_deployment(True)\n",
"print(aci_service.state)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In case the deployment fails, you can check the logs. Make sure to delete your aci_service before trying again."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if aci_service.state != 'Healthy':\n",
" # run this command for debugging.\n",
" print(aci_service.get_logs())\n",
" aci_service.delete()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Success!\n",
"\n",
"If you've made it this far, you've deployed a working web service that does image classification using an ONNX model. You can get the URL for the webservice with the code below."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(aci_service.scoring_uri)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When you are eventually done using the web service, remember to delete it."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#aci_service.delete()"
]
}
],
"metadata": {
"authors": [
{
"name": "onnx"
}
],
"kernelspec": {
"display_name": "Python 3.6",
"language": "python",
"name": "python36"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}

File diff suppressed because one or more lines are too long

View File

@@ -53,6 +53,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "hichando"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -75,7 +75,7 @@
"outputs": [], "outputs": [],
"source": [ "source": [
"# Batch AI compute\n", "# Batch AI compute\n",
"cluster_name = \"gpu_cluster\"\n", "cluster_name = \"gpu-cluster\"\n",
"try:\n", "try:\n",
" cluster = BatchAiCompute(ws, cluster_name)\n", " cluster = BatchAiCompute(ws, cluster_name)\n",
" print(\"found existing cluster.\")\n", " print(\"found existing cluster.\")\n",
@@ -617,6 +617,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "hichando"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -594,6 +594,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "coverste"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -544,6 +544,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "coverste"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -43,6 +43,28 @@
"print(\"SDK version:\", azureml.core.VERSION)" "print(\"SDK version:\", azureml.core.VERSION)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Diagnostics\n",
"Opt-in diagnostics for better experience, quality, and security of future releases."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"Diagnostics"
]
},
"outputs": [],
"source": [
"from azureml.telemetry import set_diagnostics_collection\n",
"set_diagnostics_collection(send_diagnostics = True)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -244,7 +266,7 @@
"In `pytorch_train.py`, we will log some metrics to our AML run. To do so, we will access the AML run object within the script:\n", "In `pytorch_train.py`, we will log some metrics to our AML run. To do so, we will access the AML run object within the script:\n",
"```Python\n", "```Python\n",
"from azureml.core.run import Run\n", "from azureml.core.run import Run\n",
"run = Run.get_submitted_run()\n", "run = Run.get_context()\n",
"```\n", "```\n",
"Further within `pytorch_train.py`, we log the learning rate and momentum parameters, and the best validation accuracy the model achieves:\n", "Further within `pytorch_train.py`, we log the learning rate and momentum parameters, and the best validation accuracy the model achieves:\n",
"```Python\n", "```Python\n",
@@ -735,6 +757,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "minxia"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -17,7 +17,7 @@ import argparse
from azureml.core.run import Run from azureml.core.run import Run
# get the Azure ML run object # get the Azure ML run object
run = Run.get_submitted_run() run = Run.get_context()
def load_data(data_dir): def load_data(data_dir):
@@ -162,8 +162,8 @@ def main():
parser.add_argument('--data_dir', type=str, help='directory of training data') parser.add_argument('--data_dir', type=str, help='directory of training data')
parser.add_argument('--num_epochs', type=int, default=25, help='number of epochs to train') parser.add_argument('--num_epochs', type=int, default=25, help='number of epochs to train')
parser.add_argument('--output_dir', type=str, help='output directory') parser.add_argument('--output_dir', type=str, help='output directory')
parser.add_argument('--learning_rate', type=float, help='learning rate') parser.add_argument('--learning_rate', type=float, default=0.001, help='learning rate')
parser.add_argument('--momentum', type=float, help='momentum') parser.add_argument('--momentum', type=float, default=0.9, help='momentum')
args = parser.parse_args() args = parser.parse_args()
print("data directory is: " + args.data_dir) print("data directory is: " + args.data_dir)

View File

@@ -18,7 +18,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
@@ -42,6 +41,28 @@
"print(\"SDK version:\", azureml.core.VERSION)" "print(\"SDK version:\", azureml.core.VERSION)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Diagnostics\n",
"Opt-in diagnostics for better experience, quality, and security of future releases."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"Diagnostics"
]
},
"outputs": [],
"source": [
"from azureml.telemetry import set_diagnostics_collection\n",
"set_diagnostics_collection(send_diagnostics = True)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -265,6 +286,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "minxia"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -17,7 +17,7 @@
} }
}, },
"source": [ "source": [
"# 03. Training MNIST dataset with hyperparameter tuning & deploy to ACI\n", "# 03. Training, hyperparameter tune, and deploy with TensorFlow\n",
"\n", "\n",
"## Introduction\n", "## Introduction\n",
"This tutorial shows how to train a simple deep neural network using the MNIST dataset and TensorFlow on Azure Machine Learning. MNIST is a popular dataset consisting of 70,000 grayscale images. Each image is a handwritten digit of `28x28` pixels, representing number from 0 to 9. The goal is to create a multi-class classifier to identify the digit each image represents, and deploy it as a web service in Azure.\n", "This tutorial shows how to train a simple deep neural network using the MNIST dataset and TensorFlow on Azure Machine Learning. MNIST is a popular dataset consisting of 70,000 grayscale images. Each image is a handwritten digit of `28x28` pixels, representing number from 0 to 9. The goal is to create a multi-class classifier to identify the digit each image represents, and deploy it as a web service in Azure.\n",
@@ -72,6 +72,28 @@
"print(\"Azure ML SDK Version: \", azureml.core.VERSION)" "print(\"Azure ML SDK Version: \", azureml.core.VERSION)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Diagnostics\n",
"Opt-in diagnostics for better experience, quality, and security of future releases."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"Diagnostics"
]
},
"outputs": [],
"source": [
"from azureml.telemetry import set_diagnostics_collection\n",
"set_diagnostics_collection(send_diagnostics=True)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -246,17 +268,17 @@
"from azureml.core.compute_target import ComputeTargetException\n", "from azureml.core.compute_target import ComputeTargetException\n",
"\n", "\n",
"# choose a name for your cluster\n", "# choose a name for your cluster\n",
"batchai_cluster_name = \"gpucluster\"\n", "cluster_name = \"gpucluster\"\n",
"\n", "\n",
"try:\n", "try:\n",
" # look for the existing cluster by name\n", " # look for the existing cluster by name\n",
" compute_target = ComputeTarget(workspace=ws, name=batchai_cluster_name)\n", " compute_target = ComputeTarget(workspace=ws, name=cluster_name)\n",
" if type(compute_target) is BatchAiCompute:\n", " if type(compute_target) is BatchAiCompute:\n",
" print('found compute target {}, just use it.'.format(batchai_cluster_name))\n", " print('Found existing compute target {}.'.format(cluster_name))\n",
" else:\n", " else:\n",
" print('{} exists but it is not a Batch AI cluster. Please choose a different name.'.format(batchai_cluster_name))\n", " print('{} exists but it is not a Batch AI cluster. Please choose a different name.'.format(cluster_name))\n",
"except ComputeTargetException:\n", "except ComputeTargetException:\n",
" print('creating a new compute target...')\n", " print('Creating a new compute target...')\n",
" compute_config = BatchAiCompute.provisioning_configuration(vm_size=\"STANDARD_NC6\", # GPU-based VM\n", " compute_config = BatchAiCompute.provisioning_configuration(vm_size=\"STANDARD_NC6\", # GPU-based VM\n",
" #vm_priority='lowpriority', # optional\n", " #vm_priority='lowpriority', # optional\n",
" autoscale_enabled=True,\n", " autoscale_enabled=True,\n",
@@ -264,7 +286,7 @@
" cluster_max_nodes=4)\n", " cluster_max_nodes=4)\n",
"\n", "\n",
" # create the cluster\n", " # create the cluster\n",
" compute_target = ComputeTarget.create(ws, batchai_cluster_name, compute_config)\n", " compute_target = ComputeTarget.create(ws, cluster_name, compute_config)\n",
" \n", " \n",
" # can poll for a minimum number of nodes and for a specific timeout. \n", " # can poll for a minimum number of nodes and for a specific timeout. \n",
" # if no min node count is provided it uses the scale settings for the cluster\n", " # if no min node count is provided it uses the scale settings for the cluster\n",
@@ -278,7 +300,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Now that you have created the compute target, let's see what the workspace's `compute_targets()` function returns. You should now see one entry named 'cpucluster' of type BatchAI." "Now that you have created the compute target, let's see what the workspace's `compute_targets()` function returns. You should now see one entry named 'gpucluster' of type BatchAI."
] ]
}, },
{ {
@@ -287,8 +309,9 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"for ct in ws.compute_targets():\n", "compute_targets = ws.compute_targets()\n",
" print(ct.name, ct.type, ct.provisioning_state)" "for name, ct in compute_targets.items():\n",
" print(name, ct.type, ct.provisioning_state)"
] ]
}, },
{ {
@@ -338,7 +361,7 @@
" parser = argparse.ArgumentParser()\n", " parser = argparse.ArgumentParser()\n",
" parser.add_argument('--data_folder')\n", " parser.add_argument('--data_folder')\n",
"```\n", "```\n",
"2. The script is accessing the Azure ML `Run` object by executing `run = Run.get_submitted_run()`. Further down the script is using the `run` to report the training accuracy and the validation accuracy as training progresses.\n", "2. The script is accessing the Azure ML `Run` object by executing `run = Run.get_context()`. Further down the script is using the `run` to report the training accuracy and the validation accuracy as training progresses.\n",
"```\n", "```\n",
" run.log('training_acc', np.float(acc_train))\n", " run.log('training_acc', np.float(acc_train))\n",
" run.log('validation_acc', np.float(acc_val))\n", " run.log('validation_acc', np.float(acc_val))\n",
@@ -1056,14 +1079,17 @@
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"for model in ws.models():\n", "models = ws.models()\n",
" print(\"Model:\", model.name, model.id)\n", "for name, model in models.items():\n",
"\n", " print(\"Model: {}, ID: {}\".format(name, model.id))\n",
"for image in ws.images():\n", " \n",
" print(\"Image:\", image.name, image.image_location)\n", "images = ws.images()\n",
"\n", "for name, image in images.items():\n",
"for webservice in ws.webservices():\n", " print(\"Image: {}, location: {}\".format(name, image.image_location))\n",
" print(\"Webservice:\", webservice.name, webservice.scoring_uri)" " \n",
"webservices = ws.webservices()\n",
"for name, webservice in webservices.items():\n",
" print(\"Webservice: {}, scoring URI: {}\".format(name, webservice.scoring_uri))"
] ]
}, },
{ {
@@ -1102,6 +1128,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "minxia"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -64,7 +64,7 @@ init = tf.global_variables_initializer()
saver = tf.train.Saver() saver = tf.train.Saver()
# start an Azure ML run # start an Azure ML run
run = Run.get_submitted_run() run = Run.get_context()
with tf.Session() as sess: with tf.Session() as sess:
init.run() init.run()

View File

@@ -18,7 +18,6 @@
] ]
}, },
{ {
"attachments": {},
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
@@ -42,6 +41,28 @@
"print(\"SDK version:\", azureml.core.VERSION)" "print(\"SDK version:\", azureml.core.VERSION)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Diagnostics\n",
"Opt-in diagnostics for better experience, quality, and security of future releases."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"Diagnostics"
]
},
"outputs": [],
"source": [
"from azureml.telemetry import set_diagnostics_collection\n",
"set_diagnostics_collection(send_diagnostics = True)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -336,6 +357,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "roastala"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -222,7 +222,7 @@ with tf.Session(graph=graph, config=config) as session:
init.run() init.run()
bcast.run() bcast.run()
print('Initialized') print('Initialized')
run = Run.get_submitted_run() run = Run.get_context()
average_loss = 0 average_loss = 0
for step in xrange(num_steps): for step in xrange(num_steps):
# simulate various sentence length by randomization # simulate various sentence length by randomization

View File

@@ -41,6 +41,28 @@
"print(\"SDK version:\", azureml.core.VERSION)" "print(\"SDK version:\", azureml.core.VERSION)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Diagnostics\n",
"Opt-in diagnostics for better experience, quality, and security of future releases."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"Diagnostics"
]
},
"outputs": [],
"source": [
"from azureml.telemetry import set_diagnostics_collection\n",
"set_diagnostics_collection(send_diagnostics = True)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -262,6 +284,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "minxia"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -263,7 +263,7 @@ def main(unused_argv):
print("After %d training step(s), validation cross entropy = %g" % print("After %d training step(s), validation cross entropy = %g" %
(FLAGS.train_steps, val_xent)) (FLAGS.train_steps, val_xent))
if job_name == "worker" and task_index == 0: if job_name == "worker" and task_index == 0:
run = Run.get_submitted_run() run = Run.get_context()
run.log("CrossEntropy", val_xent) run.log("CrossEntropy", val_xent)

View File

@@ -40,6 +40,28 @@
"print(\"SDK version:\", azureml.core.VERSION)" "print(\"SDK version:\", azureml.core.VERSION)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Diagnostics\n",
"Opt-in diagnostics for better experience, quality, and security of future releases."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"Diagnostics"
]
},
"outputs": [],
"source": [
"from azureml.telemetry import set_diagnostics_collection\n",
"set_diagnostics_collection(send_diagnostics = True)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -341,6 +363,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "minxia"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -41,6 +41,28 @@
"print(\"SDK version:\", azureml.core.VERSION)" "print(\"SDK version:\", azureml.core.VERSION)"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Diagnostics\n",
"Opt-in diagnostics for better experience, quality, and security of future releases."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"tags": [
"Diagnostics"
]
},
"outputs": [],
"source": [
"from azureml.telemetry import set_diagnostics_collection\n",
"set_diagnostics_collection(send_diagnostics = True)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
@@ -481,6 +503,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "roastala"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -220,6 +220,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "roastala"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -413,7 +413,7 @@
"print(X_train.shape, y_train.shape, X_test.shape, y_test.shape, sep = '\\n')\n", "print(X_train.shape, y_train.shape, X_test.shape, y_test.shape, sep = '\\n')\n",
"\n", "\n",
"# get hold of the current run\n", "# get hold of the current run\n",
"run = Run.get_submitted_run()\n", "run = Run.get_context()\n",
"\n", "\n",
"print('Train a logistic regression model with regularizaion rate of', args.reg)\n", "print('Train a logistic regression model with regularizaion rate of', args.reg)\n",
"clf = LogisticRegression(C=1.0/args.reg, random_state=42)\n", "clf = LogisticRegression(C=1.0/args.reg, random_state=42)\n",
@@ -697,6 +697,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "roastala"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -322,7 +322,6 @@
"\n", "\n",
"myenv = CondaDependencies()\n", "myenv = CondaDependencies()\n",
"myenv.add_conda_package(\"scikit-learn\")\n", "myenv.add_conda_package(\"scikit-learn\")\n",
"myenv.add_pip_package(\"pynacl==1.2.1\")\n",
"\n", "\n",
"with open(\"myenv.yml\",\"w\") as f:\n", "with open(\"myenv.yml\",\"w\") as f:\n",
" f.write(myenv.serialize_to_string())" " f.write(myenv.serialize_to_string())"
@@ -586,6 +585,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "roastala"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",

View File

@@ -398,6 +398,11 @@
} }
], ],
"metadata": { "metadata": {
"authors": [
{
"name": "jeffshep"
}
],
"kernelspec": { "kernelspec": {
"display_name": "Python 3.6", "display_name": "Python 3.6",
"language": "python", "language": "python",