In this article we will create an ARM template that will deploy a Custom Vision Service and output the Training and Prediction API Keys.
Recently Custom Vision Service has been made available on the Azure Portal in public preview. Previously, it was only available via www.customvision.ai.
Today we will focus on creating an Azure Resource Manager template to easily setup and deploy a Custom Vision service to a resource group. Our ARM template will be created in a new Azure Resource Group deployment project in Visual Studio.
Creation
Let’s declare the parameters of the ARM template:
{
"$schema": "http://schema.management.azure.com/schemas/2014-04-01-preview/deploymentTemplate.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"nameTraining": {
"type": "string",
"defaultValue": "myCustomVisionService"
},
"location": {
"type": "string",
"defaultValue": "southcentralus",
"allowedValues": [
"southcentralus"
]
},
"skuTraining": {
"type": "string",
"defaultValue": "F0",
"allowedValues": [
"F0",
"S0"
],
"metadata": {
"description": "Describes the pricing tier for Training"
}
},
"skuPrediction": {
"type": "string",
"defaultValue": "F0",
"allowedValues": [
"F0",
"S0"
],
"metadata": {
"description": "Describes the pricing tier for Prediction"
}
}
}
...
}
- nameTraining: name of the training service. If no parameter is provided the default name myCustomVisionService is used.
- location: the location where the service will be deployed. Currently in the preview only southcentralus is available.
- skuTraining: the pricing tier of the training service. If no parameter is provided the pricing tier will be F0.
- skuPrediction: the pricing tier of the prediction service. If no parameter is provided the pricing tier will be F0.
We will also need one variable in this ARM template:
{
...
"variables": {
"namePrediction": "[concat(take(replace(parameters('nameTraining'),'-',''), 18), '_Prediction')]"
}
...
}
- namePrediction: here we generate the name of the prediction service based on the name of the training service.
Now we will declare the resources necessary to deploy a Custom Vision Service:
{
...
"resources": [
{
"apiVersion": "2016-02-01-preview",
"name": "[parameters('nameTraining')]",
"type": "Microsoft.CognitiveServices/accounts",
"location": "[parameters('location')]",
"kind": "CustomVision.Training",
"sku": {
"name": "[parameters('skuTraining')]"
},
"properties": {}
},
{
"apiVersion": "2016-02-01-preview",
"name": "[variables('namePrediction')]",
"type": "Microsoft.CognitiveServices/accounts",
"location": "[parameters('location')]",
"kind": "CustomVision.Prediction",
"sku": {
"name": "[parameters('skuPrediction')]"
},
"properties": {}
}
]
...
}
We can pay attention to two things here:
- The training and prediction services are declared with the following type: Microsoft.CognitiveServices/accounts.
- The two services are differentiated with the kind attribute: CustomVision.Training and CustomVision.Prediction.
And to finish, we will output the API keys for the training service and prediction service:
{
...
"outputs": {
"Training API Key": {
"type": "string",
"value": "[listKeys(resourceId('Microsoft.CognitiveServices/accounts', parameters('nameTraining')), providers('Microsoft.CognitiveServices', 'accounts').apiVersions[0]).key1]"
},
"Prediction API Key": {
"type": "string",
"value": "[listKeys(resourceId('Microsoft.CognitiveServices/accounts', variables('namePrediction')), providers('Microsoft.CognitiveServices', 'accounts').apiVersions[0]).key1]"
}
}
}
Example of use
The ARM template is now ready, let’s open a Windows PowerShell and try it:
.\Deploy-AzureResourceGroup.ps1 -ResourceGroupName 'MyResourceGroupName' -ResourceGroupLocation 'southcentralus' -TemplateFile '.\azuredeploy.json'
...
OutputsString :
Name Type Value
=============== ========================= ==========
training API Key String bcxx2e598139477e975d71d688549c7c
prediction API Key String 90b1f3b84xxx4dfca342fd56d42df1dc
If everything goes well, you should see the same kind of output as above.
To go further
If you try to deploy the service again you will get the following error message:
Resource Microsoft.CognitiveServices/accounts 'myCustomVisionService' failed with message '{
"error": {
"code": "CanNotCreateMultipleFreeAccounts",
"message": "Operation failed. Only one free account is allowed for account type 'CustomVision.Training'."
}
}'
As stated in the message, you can’t deploy multiple free Custom Vision service.
Summary
Today we have learned how to create an ARM template that will deploy a Custom Vision service and output the API keys for the training service and prediction service.
You can get the project source code here:
Browse the GitHub repository
Please feel free to comment or contact me if you have any question about this article.