Export a Custom Vision model using the Python SDK

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:

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 pip:

1
pip install azure-cognitiveservices-vision-customvision

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

  1. 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
    
  2. 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.

1
2
credentials = ApiKeyCredentials(in_headers={"Training-key": training_key})
trainer = CustomVisionTrainingClient(training_endpoint, credentials)

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
platform = "TensorFlow"
flavor = "TensorFlowLite"
export = trainer.export_iteration(project_id, iteration_id, platform, flavor, raw=False)

while (export.status == "Exporting"):
    print ("Waiting 10 seconds...")
    time.sleep(10)
    exports = trainer.get_exports(project_id, iteration_id)
    # Find the export for this iteration  
    for e in exports:
        if e.platform == export.platform and e.flavor == export.flavor:
            export = e
            break
    print("Export status is: ", export.status)

if export.status == "Done":
    # Download the model
    export_file = requests.get(export.download_uri)
    with open("export.zip", "wb") as file:
        file.write(export_file.content)
If you’ve already exported a trained iteration in a certain format, you cannot call the export_iteration method again. Instead, use the get_exports method 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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
platform = "TensorFlow"
flavor = "TensorFlowLite"

exports = trainer.get_exports(project_id, iteration_id)
# Find the export for this iteration
for e in exports:
    if e.platform == platform and e.flavor == flavor:
        export = e
        break
print("Export status is: ", export.status)

if export.status == "Done":
    # Download the model
    export_file = requests.get(export.download_uri)
    with open("export.zip", "wb") as file:
        file.write(export_file.content)

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:

You May Also Like