View source on GitHub |
Writes metadata into the Bert NL classifier.
Inherits From: MetadataWriter
tflite_support.metadata_writers.bert_nl_classifier.MetadataWriter(
model_buffer: bytearray,
metadata_buffer: Optional[bytearray] = None,
associated_files: Optional[List[str]] = None
)
Args | |
---|---|
model_buffer
|
valid buffer of the model file. |
metadata_buffer
|
valid buffer of the metadata. |
associated_files
|
path to the associated files to be populated. |
Methods
create_for_inference
@classmethod
create_for_inference( model_buffer: bytearray, tokenizer_md: Union[
tflite_support.metadata_writers.metadata_info.BertTokenizerMd
,tflite_support.metadata_writers.metadata_info.SentencePieceTokenizerMd
], label_file_paths: List[str], ids_name: str = _DEFAULT_ID_NAME, mask_name: str = _DEFAULT_MASK_NAME, segment_name: str = _DEFAULT_SEGMENT_ID_NAME )
Creates mandatory metadata for TFLite Support inference.
The parameters required in this method are mandatory when using TFLite
Support features, such as Task library and Codegen tool (Android Studio ML
Binding). Other metadata fields will be set to default. If other fields need
to be filled, use the method create_from_metadata_info
to edit them.
ids_name
, mask_name
, and segment_name
correspond to the Tensor.name
in the TFLite schema, which help to determine the tensor order when
populating metadata. The default values come from Model Maker.
Args | |
---|---|
model_buffer
|
valid buffer of the model file. |
tokenizer_md
|
information of the tokenizer used to process the input
string, if any. Supported tokenziers are: BertTokenizer 1 and
SentencePieceTokenizer 2. If the tokenizer is RegexTokenizer
3, refer to nl_classifier.MetadataWriter .
|
label_file_paths
|
paths to the label files 4 in the classification tensor. Pass in an empty list if the model does not have any label file. |
ids_name
|
name of the ids tensor, which represents the tokenized ids of the input text. |
mask_name
|
name of the mask tensor, which represents the mask with 1 for real tokens and 0 for padding tokens. |
segment_name
|
name of the segment ids tensor, where 0 stands for the
first sequence, and 1 stands for the second sequence if exists.
|
Returns | |
---|---|
A MetadataWriter object. |
create_from_metadata
@classmethod
create_from_metadata( model_buffer: bytearray, model_metadata: Optional[
tflite_support.metadata_schema_py_generated.ModelMetadataT
] = None, input_metadata: Optional[List[_metadata_fb.TensorMetadataT]] = None, output_metadata: Optional[List[_metadata_fb.TensorMetadataT]] = None, associated_files: Optional[List[str]] = None, input_process_units: Optional[List[_metadata_fb.ProcessUnitT]] = None, output_process_units: Optional[List[_metadata_fb.ProcessUnitT]] = None )
Creates MetadataWriter based on the metadata Flatbuffers Python Objects.
Args | |
---|---|
model_buffer
|
valid buffer of the model file. |
model_metadata
|
general model metadata 1. The subgraph_metadata will be refreshed with input_metadata and output_metadata. |
input_metadata
|
a list of metadata of the input tensors 2. |
output_metadata
|
a list of metadata of the output tensors 3. |
associated_files
|
path to the associated files to be populated. |
input_process_units
|
a lits of metadata of the input process units 4. |
output_process_units
|
a lits of metadata of the output process units 5. |
Returns | |
---|---|
A MetadataWriter Object. |
create_from_metadata_info
@classmethod
create_from_metadata_info( model_buffer: bytearray, general_md: Optional[
tflite_support.metadata_writers.metadata_info.GeneralMd
] = None, input_md: Optional[tflite_support.metadata_writers.metadata_info.BertInputTensorsMd
] = None, output_md: Optional[tflite_support.metadata_writers.metadata_info.ClassificationTensorMd
] = None )
Creates MetadataWriter based on general/input/output information.
Args | |
---|---|
model_buffer
|
valid buffer of the model file. |
general_md
|
general information about the model. If not specified, default general metadata will be generated. |
input_md
|
input tensor information. If not specified, default input metadata will be generated. |
output_md
|
output classification tensor informaton. If not specified, default output metadata will be generated. |
Returns | |
---|---|
A MetadataWriter object. |
get_metadata_json
get_metadata_json() -> str
Gets the generated JSON metadata string before populated into model.
This method returns the metadata buffer before populated into the model. More fields could be filled by MetadataPopulator, such as min_parser_version. Use get_populated_metadata_json() if you want to get the final metadata string.
Returns | |
---|---|
The generated JSON metadata string before populated into model. |
get_populated_metadata_json
get_populated_metadata_json() -> str
Gets the generated JSON metadata string after populated into model.
More fields could be filled by MetadataPopulator, such as min_parser_version. Use get_metadata_json() if you want to get the original metadata string.
Returns | |
---|---|
The generated JSON metadata string after populated into model. |
populate
populate() -> bytearray
Populates the metadata and label file to the model file.
Returns | |
---|---|
A new model buffer with the metadata and associated files. |