mltk.core.TfliteModelParameters¶
- class TfliteModelParameters[source]¶
.tflite Model Parameters
Model parameters are effectively a dictionary of key/value pairs where:
key - Name of parameter as a string
value - Value of parameter as a specific scalar data type
The model parameters are serialized using a Flatbuffer schema.
The serialized parameters are inserted into a .tflite model’s “metadata” section: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/schema/schema.fbs#L1235
The basic flow is:
User Python script populates a
TfliteModelParameters
objectUse
add_to_tflite_file()
to serialize parameters and add to .tflite model metadataAt a later time, use
load_from_tflite_file()
to load parameters from .tflite model metadataAnother user Python script accesses parameters in
TfliteModelParameters
Note
The
TfliteModelParameters
object inheritsFlatbufferDictionary
which inherits the standard Python ‘dict’ class.Methods
Add the model parameters to the given .tflite model file
Add the model parameters to the given .tflite flatbuffer and return the updated flatbuffer
Add the model parameters to the given TfliteModel object
Instantiate a FlatbufferDictionary object with the given serialized flatbuffer binary data
Create a new dictionary with keys from iterable and values set to value.
Return the value for key if key is in the dictionary, else default.
Load the TfliteModelParameters from the given .tflite model file's metadata
Load the TfliteModelParameters from the given .tflite model flatbuffer bytes
Load the TfliteModelParameters from the given TfliteModel object
If the key is not found, return the default if given; otherwise, raise a KeyError.
Remove and return a (key, value) pair as a 2-tuple.
Put an entry into the dictionary
Serialize the current dictionary entries into a flatbuffer
Insert key with a value of default if key is not in the dictionary.
Generate a summary of the dictionary
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
- static load_from_tflite_file(tflite_path, tag=None)[source]¶
Load the TfliteModelParameters from the given .tflite model file’s metadata
- Parameters:
tflite_path (str) –
tag (str) –
- static load_from_tflite_flatbuffer(tflite_flatbuffer, tag=None)[source]¶
Load the TfliteModelParameters from the given .tflite model flatbuffer bytes
- Parameters:
tflite_flatbuffer (bytes) –
tag (str) –
- static load_from_tflite_model(tflite_model, tag=None)[source]¶
Load the TfliteModelParameters from the given TfliteModel object
- Parameters:
tflite_model (TfliteModel) –
tag (str) –
- add_to_tflite_file(tflite_path, output=None, tag=None)[source]¶
Add the model parameters to the given .tflite model file
This adds the current parameters to the given .tflite model file’s metadata. If output argument is given, then overwrite the given .tflite model file.
- Parameters:
tflite_path (
str
) – Path to .tflite model fileoutput (
str
) – Optional, path to output .tflite model filetag (str) –
- add_to_tflite_flatbuffer(tflite_flatbuffer, tag=None)[source]¶
Add the model parameters to the given .tflite flatbuffer and return the updated flatbuffer
- Parameters:
tflite_flatbuffer (
bytes
) – .tflite model flatbuffer to update with the model parameterstag (str) –
- Return type:
bytes
- Returns:
The update .tflite model flatbuffer
- add_to_tflite_model(tflite_model, tag=None)[source]¶
Add the model parameters to the given TfliteModel object
- Parameters:
tflite_model (
TfliteModel
) – TfliteModel model objecttag (str) –
- __init__(*args, **kwargs)¶
- __new__(*args, **kwargs)¶
- clear() None. Remove all items from D. ¶
- copy() a shallow copy of D ¶
- static deserialize(serialized_data)¶
Instantiate a FlatbufferDictionary object with the given serialized flatbuffer binary data
- Parameters:
serialized_data (bytes) –
- fromkeys(iterable, value=None, /)¶
Create a new dictionary with keys from iterable and values set to value.
- get(key, default=None, /)¶
Return the value for key if key is in the dictionary, else default.
- items() a set-like object providing a view on D's items ¶
- keys() a set-like object providing a view on D's keys ¶
- pop(key, default=<unrepresentable>, /)¶
If the key is not found, return the default if given; otherwise, raise a KeyError.
- popitem(/)¶
Remove and return a (key, value) pair as a 2-tuple.
Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.
- put(key, value, dtype=None)¶
Put an entry into the dictionary
This API allows for specifying the value’s datatype. Alternatively, you can use the standard Python dictionary syntax, e.g.:
my_params.set(‘foo’, 42, ‘int32’) OR my_params[‘foo’] = 42
- Parameters:
key (
str
) – The dictionary key to insert or overwritevalue (
Union
[str
,int
,float
,bool
,List
[str
],bytes
]) – The value of the entry. Must have a type of: str,int,float,bool,List[str], or bytesdtype (
str
) – Optional. Force the value to have a specific data type. Must be a string and one of: bool,int8,int16,int32,int64,uint8,uint16,uint32,uint64,float,double,str,str_list,bin
- serialize()¶
Serialize the current dictionary entries into a flatbuffer
- Return type:
bytes
- Returns:
Serialized dictionary flatbuffer bytes
- setdefault(key, default=None, /)¶
Insert key with a value of default if key is not in the dictionary.
Return the value for key if key is in the dictionary, else default.
- summary()¶
Generate a summary of the dictionary
- Return type:
str
- update([E, ]**F) None. Update D from dict/iterable E and F. ¶
If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]
- values() an object providing a view on D's values ¶