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" } } } ... }
We will also need one variable in this ARM template:
{ ... "variables": { "namePrediction": "[concat(take(replace(parameters('nameTraining'),'-',''), 18), '_Prediction')]" } ... }
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:
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 download the example solution here:
Browse the GitHub repository
Please feel free to comment or contact me if you have any question about this article.