Fbs API Reference

The esp-dl model utilizes FlatBuffers to store information about parameters and the computation graph. Taking into account the encryption requirements of some models, this part has not been open-sourced. However, we provide a set of APIs to facilitate users in loading and parsing esp-dl models.

Header File

Classes

class FbsLoader

Class for parser the flatbuffers.

Public Functions

FbsLoader(const char *rodata_address_or_partition_label_or_path = nullptr, model_location_type_t location = MODEL_LOCATION_IN_FLASH_RODATA)

Construct a new FbsLoader object.

参数
  • rodata_address_or_partition_label_or_path – The address of model data while location is MODEL_LOCATION_IN_FLASH_RODATA. The label of partition while location is MODEL_LOCATION_IN_FLASH_PARTITION. The path of model while location is MODEL_LOCATION_IN_SDCARD.

  • location – The model location.

~FbsLoader()

Destroy the FbsLoader object.

FbsModel *load(const uint8_t *key = nullptr, bool param_copy = true)

Load the model. If there are multiple sub-models, the first sub-model will be loaded.

参数
  • key – NULL or a 128-bit AES key, like {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}

  • param_copy – Set to false to avoid copy model parameters from FLASH to PSRAM. Only set this param to false when your PSRAM resource is very tight. This saves PSRAM and sacrifices the performance of model inference because the frequency of PSRAM is higher than FLASH. Only takes effect when MODEL_LOCATION_IN_FLASH_RODATA(CONFIG_SPIRAM_RODATA not set) or MODEL_LOCATION_IN_FLASH_PARTITION.

返回

Return nullptr if loading fails. Otherwise return the pointer of FbsModel.

FbsModel *load(const int model_index, const uint8_t *key = nullptr, bool param_copy = true)

Load the model by model index.

参数
  • model_index – The index of model.

  • key – NULL or a 128-bit AES key, like {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}.

  • param_copy – Set to false to avoid copy model parameters from FLASH to PSRAM. Only set this param to false when your PSRAM resource is very tight. This saves PSRAM and sacrifices the performance of model inference because the frequency of PSRAM is higher than FLASH. Only takes effect when MODEL_LOCATION_IN_FLASH_RODATA(CONFIG_SPIRAM_RODATA not set) or MODEL_LOCATION_IN_FLASH_PARTITION.

返回

Return nullptr if loading fails. Otherwise return the pointer of FbsModel.

FbsModel *load(const char *model_name, const uint8_t *key = nullptr, bool param_copy = true)

Load the model by model name.

参数
  • model_name – The name of model.

  • key – NULL or a 128-bit AES key, like {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f}

  • param_copy – Set to false to avoid copy model parameters from FLASH to PSRAM. Only set this param to false when your PSRAM resource is very tight. This saves PSRAM and sacrifices the performance of model inference because the frequency of PSRAM is higher than FLASH. Only takes effect when MODEL_LOCATION_IN_FLASH_RODATA(CONFIG_SPIRAM_RODATA not set) or MODEL_LOCATION_IN_FLASH_PARTITION.

返回

Return nullptr if loading fails. Otherwise return the pointer of FbsModel.

int get_model_num()

Get the number of models.

返回

The number of models

void list_models()

List all model’s name.

const char *get_model_location_string()

Get the model location string.

返回

The model location string.

Header File

Classes

class FbsModel

Flatbuffer model object.

Public Functions

FbsModel(const void *data, size_t size, model_location_type_t location, bool encrypt, bool rodata_move, bool auto_free, bool param_copy)

Construct a new FbsModel object.

参数
  • data – The data of model flatbuffers.

  • size – The size of model flatbuffers in bytes.

  • location – The location of model flatbuffers.

  • encrypt – Whether the model flatbuffers is encrypted or not.

  • rodata_move – Whether the model flatbuffers is moved from FLASH rodata to PSRAM.

  • auto_free – Whether to free the model flatbuffers data when destroy this class instance.

  • param_copy – Whether to copy the parameter in flatbuffers.

~FbsModel()

Destroy the FbsModel object.

void print()

Print the model information.

std::vector<std::string> topological_sort()

Return vector of node name in the order of execution.

返回

topological sort of node name.

esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, int &ret_value)

Get the attribute of node.

参数
  • node_name – The name of operation.

  • attribute_name – The name of attribute.

  • ret_value – The attribute value.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, float &ret_value)

Get the attribute of node.

参数
  • node_name – The name of operation.

  • attribute_name – The name of attribute.

  • ret_value – The attribute value.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, std::string &ret_value)

Get the attribute of node.

参数
  • node_name – The name of operation.

  • attribute_name – The name of attribute.

  • ret_value – The attribute value.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, std::vector<int> &ret_value)

Get the attribute of node.

参数
  • node_name – The name of operation.

  • attribute_name – The name of attribute.

  • ret_value – The attribute value.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, std::vector<float> &ret_value)

Get the attribute of node.

参数
  • node_name – The name of operation.

  • attribute_name – The name of attribute.

  • ret_value – The attribute value.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, dl::quant_type_t &ret_value)

Get the attribute of node.

参数
  • node_name – The name of operation.

  • attribute_name – The name of attribute.

  • ret_value – The attribute value.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, dl::activation_type_t &ret_value)

Get the attribute of node.

参数
  • node_name – The name of operation.

  • attribute_name – The name of attribute.

  • ret_value – The attribute value.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, dl::resize_mode_t &ret_value)

Get the attribute of node.

参数
  • node_name – The name of operation.

  • attribute_name – The name of attribute.

  • ret_value – The attribute value.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

esp_err_t get_operation_attribute(std::string node_name, std::string attribute_name, dl::TensorBase *&ret_value)

Get the attribute of node.

参数
  • node_name – The name of operation.

  • attribute_name – The name of attribute.

  • ret_value – The attribute value.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

esp_err_t get_operation_output_shape(std::string node_name, int index, std::vector<int> &ret_value)

Get operation output shape.

参数
  • node_name – The name of operation.

  • index – The index of outputs

  • ret_value – Return shape value.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

esp_err_t get_operation_inputs_and_outputs(std::string node_name, std::vector<std::string> &inputs, std::vector<std::string> &outputs)

Get the attribute of node.

参数
  • node_name – The name of operation.

  • inputs – The vector of operation inputs.

  • outputs – The vector of operation outputs.

返回

esp_err_t Return ESP_OK if get successfully. Otherwise return ESP_FAIL.

std::string get_operation_type(std::string node_name)

Get operation type, “Conv”, “Linear” etc.

参数

node_name – The name of operation

返回

The type of operation.

dl::TensorBase *get_operation_parameter(std::string node_name, int index = 1, uint32_t caps = MALLOC_CAP_DEFAULT)

Return if the variable is a parameter.

参数
  • node_name – The name of operation

  • index – The index of the variable

  • caps – Bitwise OR of MALLOC_CAP_* flags indicating the type of memory to be returned

返回

dl::TensorBase*

dl::TensorBase *get_operation_lut(std::string node_name, uint32_t caps = MALLOC_CAP_DEFAULT, std::string attribute_name = "lut")

Get LUT(Look Up Table) if the operation has LUT.

参数
  • node_name – The name of operation

  • caps – Bitwise OR of MALLOC_CAP_* flags indicating the type of memory to be returned

  • attribute_name – The name of LUT attribute

返回

dl::TensorBase*

bool is_parameter(std::string name)

return true if the variable is a parameter

参数

name – Variable name

返回

true if the variable is a parameter else false

const void *get_tensor_raw_data(std::string tensor_name)

Get the raw data of FlatBuffers::Dl::Tensor.

参数

tensor_name – The name of Tensor.

返回

uint8_t * The pointer of raw data.

dl::dtype_t get_tensor_dtype(std::string tensor_name)

Get the element type of tensor tensor.

参数

tensor_name – The tensor name.

返回

FlatBuffers::Dl::TensorDataType

std::vector<int> get_tensor_shape(std::string tensor_name)

Get the shape of tensor.

参数

tensor_name – The name of tensor.

返回

std::vector<int> The shape of tensor.

std::vector<int> get_tensor_exponents(std::string tensor_name)

Get the exponents of tensor.

警告

When quantization is PER_CHANNEL, the size of exponents is same as out_channels. When quantization is PER_TENSOR, the size of exponents is 1.

参数

tensor_name – The name of tensor.

返回

The exponents of tensor.

dl::dtype_t get_value_info_dtype(std::string var_name)

Get the element type of value_info.

参数

var_name – The value_info name.

返回

dl::dtype_t

std::vector<int> get_value_info_shape(std::string var_name)

Get the shape of value_info.

参数

var_name – The value_info name.

返回

the shape of value_info.

int get_value_info_exponent(std::string var_name)

Get the exponent of value_info. Only support PER_TENSOR quantization.

参数

var_name – The value_info name.

返回

the exponent of value_info

const void *get_test_input_tensor_raw_data(std::string tensor_name)

Get the raw data of test input tensor.

参数

tensor_name – The name of test input tensor.

返回

uint8_t * The pointer of raw data.

const void *get_test_output_tensor_raw_data(std::string tensor_name)

Get the raw data of test output tensor.

参数

tensor_name – The name of test output tensor.

返回

uint8_t * The pointer of raw data.

dl::TensorBase *get_test_input_tensor(std::string tensor_name)

Get the test input tensor.

参数

tensor_name – The name of test input tensor.

返回

The pointer of tensor.

dl::TensorBase *get_test_output_tensor(std::string tensor_name)

Get the test output tensor.

参数

tensor_name – The name of test output tensor.

返回

The pointer of tensor.

std::vector<std::string> get_test_outputs_name()

Get the name of test outputs.

返回

the name of test outputs

std::vector<std::string> get_graph_inputs()

Get the graph inputs.

返回

the name of inputs

std::vector<std::string> get_graph_outputs()

Get the graph outputs.

返回

the name of ounputs

void clear_map()

Clear all map.

void load_map()

Load all map.

std::string get_model_name()

Get the model name.

返回

the name of model

int64_t get_model_version()

Get the model version.

返回

The version of model

std::string get_model_doc_string()

Get the model doc string.

返回

The doc string of model

void get_model_size(size_t *internal_size, size_t *psram_size, size_t *psram_rodata_size, size_t *flash_size)

Get the model size.

参数
  • internal_size – Flatbuffers model internal RAM usage

  • psram_size – Flatbuffers model PSRAM usage

  • psram_rodata_size – Flatbuffers model PSRAM rodate usage. If CONFIG_SPIRAM_RODATA option is on, \ Flatbuffers model in FLASH rodata will be copied to PSRAM

  • flash_size – Flatbuffers model FLASH usage

bool memory_addr_in_model(void *addr)

Whether the memory address within the fbs model or not.

参数

addr – memory address

返回

true if memory address within the fbs model else false

Public Members

bool m_param_copy

copy flatbuffers param or not.