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.
Methods
get_model_buffer
get_model_buffer
()
Gets the buffer of the model with packed metadata and associated files.
get_packed_associated_file_list
get_packed_associated_file_list
()
Gets a list of associated files packed to the model file.
get_recorded_associated_file_list
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.
load_associated_file_buffers
load_associated_file_buffers
(
associated_files
)
Loads the associated file buffers (in bytearray) to be populated.
associated_files
load_associated_files
load_associated_files
(
associated_files
)
Loads associated files that to be concatenated after the model file.
associated_files
IOError
load_metadata_and_associated_files
load_metadata_and_associated_files
(
src_model_buf
)
Loads the metadata and associated files from another model buffer.
src_model_buf
load_metadata_buffer
load_metadata_buffer
(
metadata_buf
)
Loads the metadata buffer (in bytearray) to be populated.
metadata_buf
ValueError
ValueError
ValueError
ValueError
ValueError
load_metadata_file
load_metadata_file
(
metadata_file
)
Loads the metadata file to be populated.
metadata_file
IOError
ValueError
ValueError
ValueError
ValueError
ValueError
populate
populate
()
Populates loaded metadata and associated files into the model file.
with_model_buffer
@classmethodwith_model_buffer ( model_buf )
Creates a MetadataPopulator object that populates data to a model buffer.
model_buf
ValueError
with_model_file
@classmethodwith_model_file ( model_file )
Creates a MetadataPopulator object that populates data to a model file.
model_file
IOError
ValueError


