FlatbufferDictionary

Google Flatbuffer schema: dictionary.fbs

class FlatbufferDictionary[source]

FlatbufferDictionary

This class allows for adding scalar values to a standard Python dictionary, serializing the dictionary into a flatbuffer, and later de-serializing to another Python dictionary.

A flatbuffer dictionary is a collection of key/value pairs where:

  • key - Name of parameter as a string

  • value - Value of parameter as a specific scalar data type

The dictionary is serialized using the Flatbuffer schema dictionary.fbs

Note

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

__init__(*args, **kwargs)[source]
put(key, value, dtype=None)[source]

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()[source]

Serialize the current dictionary entries into a flatbuffer

Return type:

bytes

Returns:

Serialized dictionary flatbuffer bytes

static deserialize(serialized_data)[source]

Instantiate a FlatbufferDictionary object with the given serialized flatbuffer binary data

Parameters:

serialized_data (bytes) –

summary()[source]

Generate a summary of the dictionary

Return type:

str

TfliteModelParameters Flatbuffer Schema

This is the Google Flatbuffer schema used to serialize model parameters in a .tflite model file.

/*
 * Flatbuffer Dictionary Schema
 *
 * This defines a schema for mapping a
 * Python dictionary to a flatbuffer.
 *
 * The dictionary entries are key/value pairs where:
 * - key - Name of parameter as a string
 * - value - Value of parameter as a specific scalar data type
 * 
 */

/*
 * Boolean value
 */
table BoolValue
{
    value : bool;
}

/*
 * Signed int8
 */
table Int8Value
{
    value : int8;
}

/*
 * Unsigned int8
 */
table Uint8Value
{
    value : uint8;
}

/*
 * Signed int16
 */
table Int16Value
{
    value : int16;
}

/*
 * Unsigned int16
 */
table Uint16Value
{
    value : uint16;
}

/*
 * Signed int32
 */
table Int32Value
{
    value : int32;
}

/*
 * Unsigned int32
 */
table Uint32Value
{
    value : uint32;
}

/*
 * Signed int64
 */
table Int64Value
{
    value : int64;
}

/*
 * Unsigned int64
 */
table Uint64Value
{
    value : uint64;
}

/*
 * Float32
 */
table FloatValue
{
    value : float;
}

/*
 * Float64
 */
table DoubleValue
{
    value : double;
}

/*
 *  Binary array (i.e. binary blob)
 */
table BinaryValue
{
    data : [uint8];
}

/*
 * Printable string
 */
table StringValue
{
    data : string;
}

/*
 * List of strings
 */
table StringList
{
    data : [string];
}

/*
 * List of int32
 */
table Int32List
{
    data : [int32];
}

/*
 * List of floats
 */
table FloatList
{
    data : [float];
}

/*
* Dictionary value
*
* A value has a specific data type
*/
union Value
{
    boolean : BoolValue,
    i8  : Int8Value,
    u8  : Uint8Value,
    i16 : Int16Value,
    u16 : Uint16Value,
    i32 : Int32Value,
    u32 : Uint32Value,
    i64 : Int64Value,
    u64 : Uint64Value,
    f32 : FloatValue,
    f64 : DoubleValue,
    str : StringValue,
    str_list : StringList,
    int32_list : Int32List,
    float_list : FloatList,
    bin : BinaryValue
}

/*
* Dictionary Entry
*
* An entry has a string key
* and a value which has a specific scalar data type
*/
table Entry
{
    key : string;  // The dictionary key
    value : Value; // The scalar data type
}

/*
* Flatbuffer Dictionary
*
* This provides a container for key/value pairs.
*/
table Dictionary
{
    schema_version: uint8; // Schema version of this flatbuffer specification
                           // Allows for future expansion
                           // Currently schema_version=1
    entries : [Entry];     // List of dictionary entries
}


root_type Dictionary;