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.
Args |
model_file
|
valid path to a TensorFlow Lite model file.
|
Raises |
IOError
|
File not found.
|
ValueError
|
the model does not have the expected flatbuffer identifer.
|
Methods
get_model_buffer
View source
get_model_buffer()
Gets the buffer of the model with packed metadata and associated files.
Returns |
Model buffer (in bytearray).
|
get_packed_associated_file_list
View source
get_packed_associated_file_list()
Gets a list of associated files packed to the model file.
Returns |
List of packed associated files.
|
get_recorded_associated_file_list
View source
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.
Returns |
List of recorded associated files.
|
load_associated_file_buffers
View source
load_associated_file_buffers(
associated_files
)
Loads the associated file buffers (in bytearray) to be populated.
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.
|
load_associated_files
View source
load_associated_files(
associated_files
)
Loads associated files that to be concatenated after the model file.
Args |
associated_files
|
list of file paths.
|
Raises |
IOError
|
File not found.
|
View source
load_metadata_and_associated_files(
src_model_buf
)
Loads the metadata and associated files from another model buffer.
Args |
src_model_buf
|
source model buffer (in bytearray) with metadata and
associated files.
|
View source
load_metadata_buffer(
metadata_buf
)
Loads the metadata buffer (in bytearray) to be populated.
Args |
metadata_buf
|
metadata buffer (in bytearray) to be populated.
|
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.
|
View source
load_metadata_file(
metadata_file
)
Loads the metadata file to be populated.
Args |
metadata_file
|
path to the metadata file to be populated.
|
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.
|
populate
View source
populate()
Populates loaded metadata and associated files into the model file.
with_model_buffer
View source
@classmethod
with_model_buffer(
model_buf
)
Creates a MetadataPopulator object that populates data to a model buffer.
Args |
model_buf
|
TensorFlow Lite model buffer in bytearray.
|
Returns |
A MetadataPopulator(_MetadataPopulatorWithBuffer) object.
|
Raises |
ValueError
|
the model does not have the expected flatbuffer identifer.
|
with_model_file
View source
@classmethod
with_model_file(
model_file
)
Creates a MetadataPopulator object that populates data to a model file.
Args |
model_file
|
valid path to a TensorFlow Lite model file.
|
Returns |
MetadataPopulator object.
|
Raises |
IOError
|
File not found.
|
ValueError
|
the model does not have the expected flatbuffer identifer.
|
Class Variables |
METADATA_FIELD_NAME
|
'TFLITE_METADATA'
|
METADATA_FILE_IDENTIFIER
|
b'M001'
|
TFLITE_FILE_IDENTIFIER
|
b'TFL3'
|