<br />

|---|
| [![](https://www.tensorflow.org/images/GitHub-Mark-32px.png) View source on GitHub](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L99-L642) |

Packs metadata and associated files into TensorFlow Lite model file.

    tflite_support.metadata.MetadataPopulator(
        model_file
    )

MetadataPopulator can be used to populate metadata and model associated files
into a model file or a model buffer (in bytearray). It can also help to
inspect list of files that have been packed into the model or are supposed to
be packed into the model.

The metadata file (or buffer) should be generated based on the metadata
schema:
third_party/tensorflow/lite/schema/metadata_schema.fbs

#### Example usage:

Populate matadata and label file into an image classifier model.

First, based on metadata_schema.fbs, generate the metadata for this image
classifer model using Flatbuffers API. Attach the label file onto the ouput
tensor (the tensor of probabilities) in the metadata.

Then, pack the metadata and label file into the model as follows.

      # Populating a metadata file (or a metadta buffer) and associated files to
      a model file:
      populator = MetadataPopulator.with_model_file(model_file)
      # For metadata buffer (bytearray read from the metadata file), use:
      # populator.load_metadata_buffer(metadata_buf)
      populator.load_metadata_file(metadata_file)
      populator.load_associated_files([label.txt])
      # For associated file buffer (bytearray read from the file), use:
      # populator.load_associated_file_buffers({"label.txt": b"file content"})
      populator.populate()

      # Populating a metadata file (or a metadta buffer) and associated files to
      a model buffer:
      populator = MetadataPopulator.with_model_buffer(model_buf)
      populator.load_metadata_file(metadata_file)
      populator.load_associated_files([label.txt])
      populator.populate()
      # Writing the updated model buffer into a file.
      updated_model_buf = populator.get_model_buffer()
      with open("updated_model.tflite", "wb") as f:
        f.write(updated_model_buf)

      # Transferring metadata and associated files from another TFLite model:
      populator = MetadataPopulator.with_model_buffer(model_buf)
      populator_dst.load_metadata_and_associated_files(src_model_buf)
      populator_dst.populate()
      updated_model_buf = populator.get_model_buffer()
      with open("updated_model.tflite", "wb") as f:
        f.write(updated_model_buf)

Note that existing metadata buffer (if applied) will be overridden by the new
metadata buffer.

<br />

<br />

<br />

| ## Args ||
|---|---|
| `model_file` | valid path to a TensorFlow Lite model file. |

<br />

<br />

<br />

<br />

| ## Raises ||
|---|---|
| `IOError` | File not found. |
| `ValueError` | the model does not have the expected flatbuffer identifer. |

<br />

## Methods

### `get_model_buffer`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L214-L221)

    get_model_buffer()

Gets the buffer of the model with packed metadata and associated files.

<br />

<br />

<br />

| Returns ||
|---|---|
| Model buffer (in bytearray). ||

<br />

### `get_packed_associated_file_list`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L223-L233)

    get_packed_associated_file_list()

Gets a list of associated files packed to the model file.

<br />

<br />

<br />

| Returns ||
|---|---|
| List of packed associated files. ||

<br />

### `get_recorded_associated_file_list`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L235-L254)

    get_recorded_associated_file_list()

Gets a list of associated files recorded in metadata of the model file.

Associated files may be attached to a model, a subgraph, or an input/output
tensor.

<br />

<br />

<br />

| Returns ||
|---|---|
| List of recorded associated files. ||

<br />

### `load_associated_file_buffers`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L256-L268)

    load_associated_file_buffers(
        associated_files
    )

Loads the associated file buffers (in bytearray) to be populated.

<br />

<br />

<br />

| Args ||
|---|---|
| `associated_files` | a dictionary of associated file names and corresponding file buffers, such as {"file.txt": b"file content"}. If pass in file paths for the file name, only the basename will be populated. |

<br />

### `load_associated_files`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L270-L283)

    load_associated_files(
        associated_files
    )

Loads associated files that to be concatenated after the model file.

<br />

<br />

<br />

| Args ||
|---|---|
| `associated_files` | list of file paths. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `IOError` | File not found. |

<br />

### `load_metadata_and_associated_files`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L343-L359)

    load_metadata_and_associated_files(
        src_model_buf
    )

Loads the metadata and associated files from another model buffer.

<br />

<br />

<br />

| Args ||
|---|---|
| `src_model_buf` | source model buffer (in bytearray) with metadata and associated files. |

<br />

### `load_metadata_buffer`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L285-L321)

    load_metadata_buffer(
        metadata_buf
    )

Loads the metadata buffer (in bytearray) to be populated.

<br />

<br />

<br />

| Args ||
|---|---|
| `metadata_buf` | metadata buffer (in bytearray) to be populated. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | The metadata to be populated is empty. |
| `ValueError` | The metadata does not have the expected flatbuffer identifer. |
| `ValueError` | Cannot get minimum metadata parser version. |
| `ValueError` | The number of SubgraphMetadata is not 1. |
| `ValueError` | The number of input/output tensors does not match the number of input/output tensor metadata. |

<br />

### `load_metadata_file`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L323-L341)

    load_metadata_file(
        metadata_file
    )

Loads the metadata file to be populated.

<br />

<br />

<br />

| Args ||
|---|---|
| `metadata_file` | path to the metadata file to be populated. |

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `IOError` | File not found. |
| `ValueError` | The metadata to be populated is empty. |
| `ValueError` | The metadata does not have the expected flatbuffer identifer. |
| `ValueError` | Cannot get minimum metadata parser version. |
| `ValueError` | The number of SubgraphMetadata is not 1. |
| `ValueError` | The number of input/output tensors does not match the number of input/output tensor metadata. |

<br />

### `populate`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L361-L365)

    populate()

Populates loaded metadata and associated files into the model file.

### `with_model_buffer`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L199-L212)

    @classmethod
    with_model_buffer(
        model_buf
    )

Creates a MetadataPopulator object that populates data to a model buffer.

<br />

<br />

<br />

| Args ||
|---|---|
| `model_buf` | TensorFlow Lite model buffer in bytearray. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| A MetadataPopulator(_MetadataPopulatorWithBuffer) object. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `ValueError` | the model does not have the expected flatbuffer identifer. |

<br />

### `with_model_file`

[View source](https://github.com/tensorflow/tflite-support/blob/v0.4.4/tensorflow_lite_support/metadata/python/metadata.py#L181-L195)

    @classmethod
    with_model_file(
        model_file
    )

Creates a MetadataPopulator object that populates data to a model file.

<br />

<br />

<br />

| Args ||
|---|---|
| `model_file` | valid path to a TensorFlow Lite model file. |

<br />

<br />

<br />

<br />

| Returns ||
|---|---|
| MetadataPopulator object. ||

<br />

<br />

<br />

<br />

| Raises ||
|---|---|
| `IOError` | File not found. |
| `ValueError` | the model does not have the expected flatbuffer identifer. |

<br />

<br />

<br />

<br />

| ## Class Variables ||
|---|---|
| METADATA_FIELD_NAME | `'TFLITE_METADATA'` |
| METADATA_FILE_IDENTIFIER | `b'M001'` |
| TFLITE_FILE_IDENTIFIER | `b'TFL3'` |

<br />