Classifier Module
The Classifier module is responsible for analyzing preprocessed data and generating predictions based on trained neural network models. It acts as the final stage in the data pipeline, ensuring that incoming data is evaluated against the appropriate classifier configurations to deliver actionable results. This module supports flexibility through dynamic runtime selection and model integration, streamlining the prediction process and enabling seamless interaction with the broader data handling workflow.
ClassifiersDict
This class manages a dictionary of ClassifierRecord instances, enabling serialization and deserialization of classifier configurations. It ensures that classifier names are unique and properly initialized, facilitating efficient management of classifiers within the module.
Key Components and Methods
-
values
A serialized list ofClassifierRecordinstances. Used to persist the dictionary data across serialization cycles. -
NEW_CLASSIFIER_NAME
A constant string used as a default name for new classifiers when no name is provided or when conflicts occur. -
OnBeforeSerialize()
Clears thevalueslist and repopulates it with theClassifierRecordinstances from the dictionary. Ensures the dictionary's contents are correctly serialized. -
OnAfterDeserialize()
Rebuilds the dictionary from the serializedvalueslist. It resolves naming conflicts by appending unique identifiers to duplicate names.
ClassifierRecord
Represents an individual classifier's configuration, including its runtime, neural network model, and output settings. This class serves as a lightweight container for essential classifier data and methods.
Key Components and Methods
-
name
A string representing the unique identifier for the classifier. -
runtime
An instance ofIRuntimethat handles the execution of the neural network model. -
model
The neural network model (NNModel) associated with the classifier. -
outputNames
An array of strings defining the names of the output variables produced by the classifier. -
Load(IRuntime runtime)
Assigns the provided runtime to the classifier and loads the neural network model into it. This method prepares the classifier for prediction tasks.
ClassifierManager
The ClassifierManager class orchestrates the classifier operations, including loading configurations, binding event handlers, and managing data classification workflows. It ensures smooth integration of classifier logic into the broader data pipeline.
Key Components and Methods
-
Private Fields
- _dataPreprocessedBinding
An event binding for handlingDataPreprocessedEvent, which triggers classification when new preprocessed data is available. - _config
An instance ofClassifierConfigcontaining the configuration details for the classifiers.
- _dataPreprocessedBinding
-
Private Methods
- _receiveData(DataPreprocessedEvent busEvent)
Handles incoming preprocessed data by identifying the appropriate classifier and invoking itsPredictmethod. It logs errors for missing classifiers and sends the classified results to the event bus. - _sendData(DataClassifiedEvent busEvent)
Raises aDataClassifiedEventon the event bus with the classifier's prediction results.
- _receiveData(DataPreprocessedEvent busEvent)
-
Public Methods
- Load(IConfig config)
Loads classifier configurations from the providedIConfigobject. Dynamically instantiates the runtime for each classifier and prepares it for prediction. - Init()
Initializes event bindings forDataPreprocessedEventand registers them with the event bus. - Bind()
A placeholder method for additional binding logic if required in the future.
- Load(IConfig config)
AvailableRuntimes
The AvailableRuntimes class provides a centralized registry of available runtime types for classifiers. It enables dynamic selection and instantiation of runtime environments for executing neural network models.
Key Components and Methods
- Runtimes
A static, read-only array ofTypeobjects representing the available runtime implementations. Currently, the supported runtimes include:OnnxRuntimeTFRuntime
IRuntime
The IRuntime interface defines the contract for implementing runtime environments capable of executing neural network models. It serves as a foundation for integrating various runtime backends into the classifier module.
Key Components and Methods
-
Predict(Matrix data)
Processes the input data (Matrix) and returns a prediction as aMatrixobject. -
Load(NNModel model)
Loads a neural network model (NNModel) into the runtime environment, preparing it for prediction tasks.
OnnxRuntime
The OnnxRuntime class implements the IRuntime interface and leverages Unity Barracuda to execute ONNX (Open Neural Network Exchange) models. It provides efficient inference capabilities and supports precompiled workers for enhanced performance.
Key Components and Methods
-
Private Fields
- _model
Stores the loaded ONNX model. - _worker
An instance ofIWorker, responsible for executing the loaded model.
- _model
-
Public Methods
- Load(NNModel model)
Loads an ONNX model into the runtime by initializing the_modeland creating aWorkerinstance for inference. Logs the loading process for debugging purposes. - Predict(Matrix data)
Executes the model on the providedMatrixinput. The method prepares the input tensor, executes the model, and retrieves the output tensor. It updates the inputMatrixwith the predictions and logs the results. - ~OnnxRuntime()
A destructor that ensures proper disposal of the_workerto free resources.
- Load(NNModel model)
TFRuntime
The TFRuntime class is a placeholder implementation of the IRuntime interface for TensorFlow models. It is intended for future support of TensorFlow inference within the classifier module.
Key Components and Methods
- Public Methods
- Predict(Matrix data)
Throws aNotImplementedExceptionas the functionality is not yet implemented. - Load(NNModel model)
Throws aNotImplementedExceptionas the functionality is not yet implemented.
- Predict(Matrix data)