In this article, you will deploy a function project to Azure using Visual Studio Code to create a serverless HTTP API.
Azure Custom Vision Service lets you export your image classification and object detection models to run locally on a device. For example, you can utilize the exported models in mobile applications or run a computer vision model on a microcontroller. You can export a Custom Vision model in numerous formats, including TensorFlow, TensorFlow Lite, TensorFlow.js, ONNX, and Docker containers.
In a previous post, you learned how to train an image classification model using the Python SDK. In this article, you will export the model to a TensorFlow Lite file using the Python client library to fully automate the process of retraining and updating a model.
To complete the exercise, you will need:
- An Azure subscription. If you don’t have one, you can sign up for an Azure free account.
- A Custom Vision model.
To export a Custom Vision model, you should use a Compact domain. Compact domains are optimized for real-time classification and object detection on edge devices.
Set up your application
Install the client library
Install the Custom Vision client library for Python with
Create a configuration file
Create a configuration file (
.env) and save the key and the endpoint of your training resource and the id of your project and the trained iteration you wish to download.
Create a new Python application
Create a new Python file (export_model.py) and import the following libraries:
1 2 3 4
from azure.cognitiveservices.vision.customvision.training import CustomVisionTrainingClient from msrest.authentication import ApiKeyCredentials from dotenv import load_dotenv import os, time, requests
Add the following code to load the values from the configuration file.
1 2 3 4 5
load_dotenv() training_endpoint = os.getenv('TRAINING_ENDPOINT') training_key = os.getenv('TRAINING_KEY') project_id = os.getenv('PROJECT_ID') iteration_id = os.getenv('ITERATION_ID')
Create a training client
Use the following code to create a
CustomVisionTrainingClient object. You will use the trainer object to export your model in one of the available formats.
Export your model
Add the following code to export the trained iteration to a TensorFlow Lite file and download the exported model. For more information about the
export_iteration method, see the Custom Vision SDK for Python documentation.
If you’ve already exported a trained iteration in a certain format, you cannot call the
export_iterationmethod again. Instead, use the
get_exportsmethod to download the existing exported model.
If you have already exported the current iteration to a TensorFlow Lite file, use the following code to download the previously exported model.
Summary and next steps
In this article, you learned how to export a Custom Vision trained iteration using the client library for Python. If you are interested in integrating your exported model into an application, you may check out the following resources: