TF Lite Model Metadata

Learn to add metadata to a TF Lite model using the TF Lite Metadata Writer API.

Metadata refers to the data that describes other data or information. It provides additional information about data to help understand, manage, or use it more effectively. In the context of ML models, metadata can include information such as the model architecture, input and output shapes, preprocessing steps, training details, accuracy metrics, label files, and more. Let’s explore TF Lite metadata and the Metadata Writer API. We will also extract model information, input/output details, and associated files from a TF Lite model with metadata.

TF Lite model metadata

TF Lite model metadata is a self-descriptive format that contains information about the model, its inputs and outputs, and associated files. This information enables easy exchange and integration of models across different platforms and frameworks.

The primary uses of model metadata are:

  • To include documentation, such as model descriptions, authors, and versions of the TF Lite models.

  • To enable model inference using the TF Lite Task Library, which is a set of pretrained and optimized TF Lite models for common ML tasks, like image classification, object detection, and natural language processing. The TF Lite Task Library contains a set of prebuilt inference tasks that can be easily performed on the model and provides us with tools to integrate models into mobile and edge devices.

  • To allow the TF Lite Android wrapper code generator to create platform-specific wrapper code for TF Lite models enhanced with metadata. This eliminates the need to interact directly with FlatBuffersFlatBuffers and allows us to use typed objects, such as Bitmap, to interact with the model.

TF Lite Metadata Writer API

The TF Lite Metadata Writer API is part of the TF Lite Support library. It allows us to add metadata to TF Lite models. This API can create metadata for supported ML tasks including image classifiers, object detectors, image segmenters, natural language classifiers, and audio classifiers.

The API also includes utilities for saving and loading metadata files, as well as converting metadata to JSON format for visualization and debugging purposes. The code below shows how to use the TF Lite Metadata Writer API to create metadata for an image classifier model and save it to a separate TF Lite file.

Get hands-on with 1200+ tech skills courses.