User APIs of NN Framework

Introduction

  1. NNLogger:A class for controlling logging-related outputs of the nnframework.

  • Set debug mode.

  • Set whether to save logs to a file and the save path for the log file.

  • Set whether to output logs to the standard output.

  • Set the level of the log file.

  • This interface also provides an interface for recording log messages.

  1. NNResultListener:A listener for detection results, primarily used for asynchronous detection, which can receive two types of notifications.

  • An error occurred during the model detection process.

  • The result of the model detection, which could be a single result or multiple results. For example, in object detection, there could be one to multiple results output.

  1. NNModel: A base class for nn models, used to represent the related operations of a specific nn model.

  • Initialize and release resources related to the model.

  • Add and remove listeners for detection results to/from this model.

  • Interfaces for synchronous and asynchronous detection.

  • Interfaces which used to support Sequential.

  1. NNSequential: An interface for chaining different nnmodels together, as detection is often accomplished through the cooperation of multiple models.

  • Add and remove models.

  • Create and disconnect graphs, retrieve graph information (model chaining information).

  • Synchronous and asynchronous detection interfaces.

NNLogger

  1. Interface Definition

class NNLogger { public: virtual void setDebugMode(bool enable) = 0; virtual void setFileLogPath(std::string logPath) = 0; virtual void setFileLog(bool enable) = 0; virtual void setSystemLog(bool enable) = 0; virtual void setLevel(int level) = 0; virtual void writeLog(int level, std::string tag, std::string content) = 0; };
  • setDebugMode Set the debug mode. When set to true, log messages will be output immediately. When set to false, log messages will not be output immediately, improving performance, but potentially leading to log message loss if there are too many messages.

  • setFileLogPath Set the path for the log file.

  • setFileLog Configure whether to output logs to a file.

  • setSystemLog Configure whether to output logs to the standard output (stdout).

  • setLevel Set the level of the log file.

  • writeLog A method to write log entry.

  1. Example

void __apply_logger() { auto logger = NNSequential::getLogger(); logger->setDebugMode(true); logger->setSystemLog(true); logger->setLevel(NN_LOG_LEVEL_INFO); }
  • The code above enables the debug mode, outputs to the standard output, and sets the log level to INFO.

NNResultListener

  1. Interface Definition

class NNResultListener { public: virtual void onDetectedError(int error) = 0; virtual void onDetected(std::shared_ptr<NNData> result) = 0; virtual void onDetected(std::vector<std::shared_ptr<NNData>> results) = 0; };
  • onDetectedError Notify through this interface if an error occurs during the detection process.

  • onDetected Notify through this overloaded method when one or more results are detected.

  1. Example

  • Inherit from NNResultListener, override its methods, and implement your own logic.

  • The example code above outputs the results to stdout.

NNModel

  1. Interface Definition

  • prepare Prepare the model. If using an asynchronous detection interface, an asynchronous detection listener can be passed in here.

  • destroy Release the resources of the model.

  • addListener/removeListener Add or remove listeners to receive notifications from the asynchronous detection interface.

  • detect Perform detection, including interfaces for synchronous and asynchronous detection.

  • getModelName Get the name of the model.

  • getResultName Get the name of the object type for the detection result.

  • compatible Used to determine if the output of the model represented by this model is compatible with the output of the current model.

  1. Example(The official yolov5s):

The process of the above example is as follows:

  • Create a YoloV5s model object, which implements the NNModel methods.

  • Prepare the model.

  • Determine whether to perform synchronous detection or asynchronous detection based on parameters.

  • Release the model resources.

NNSequential

  1. Interface Definition

  • add Add a model to the Sequential.

  • clear Clear all models from the Sequential.

  • buildGraph Create the graph. If you want to receive asynchronous listener results, you can pass a listener to this method.

  • This will chain all the models in the Sequential in the order they were added.

  • teardownGraph Disconnect the chained models and releases the corresponding resources.

  • getGraphInfo Get the information about the chained models.

  • detect Interfaces related to synchronous and asynchronous detection.

  • getLogger A static (class) method to get the logging interface for setting up logs.

  • createSequential A static (class) method to get the Sequential interface for chaining models.

  1. Example

The process of the above example is as follows:

  • create sequential

  • add model to sequential

  • build graph(sequential)

  • detecting(sequential)

  • teardown graph(sequential)