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:

  1. User Python script populates a TfliteModelParameters object

  2. Use add_to_tflite_file() to serialize parameters and add to .tflite model metadata

  3. At a later time, use load_from_tflite_file() to load parameters from .tflite model metadata

  4. Another user Python script accesses parameters in TfliteModelParameters

Note

The TfliteModelParameters object inherits FlatbufferDictionary which inherits the standard Python ‘dict’ class.

Methods

__init__

add_to_tflite_file

Add the model parameters to the given .tflite model file

add_to_tflite_flatbuffer

Add the model parameters to the given .tflite flatbuffer and return the updated flatbuffer

add_to_tflite_model

Add the model parameters to the given TfliteModel object

clear

copy

deserialize

Instantiate a FlatbufferDictionary object with the given serialized flatbuffer binary data

fromkeys

Create a new dictionary with keys from iterable and values set to value.

get

Return the value for key if key is in the dictionary, else default.

items

keys

load_from_tflite_file

Load the TfliteModelParameters from the given .tflite model file's metadata

load_from_tflite_flatbuffer

Load the TfliteModelParameters from the given .tflite model flatbuffer bytes

load_from_tflite_model

Load the TfliteModelParameters from the given TfliteModel object

pop

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.

put

Put an entry into the dictionary

serialize

Serialize the current dictionary entries into a flatbuffer

setdefault

Insert key with a value of default if key is not in the dictionary.

summary

Generate a summary of the dictionary

update

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

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:
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 file

  • output (str) – Optional, path to output .tflite model file

  • tag (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 parameters

  • tag (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 object

  • tag (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 overwrite

  • value (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 bytes

  • dtype (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