Creating Predictors
ML Where your Users Are
Last updated
Was this helpful?
ML Where your Users Are
Last updated
Was this helpful?
As you might have noticed above, instances typically won't be used directly. Instead, they will be used through Edge Predictors, which are lightweight classes that can transform input data into the model's expected input features; and can transform the model's output features into easily usable types. Below are the general steps in implementing Edge predictors:
All Edge predictors must inherit from the interface. The predictor has a single generic type argument, TOutput
, which is a developer-friendly type that is returned when a prediction is made. For example, the MobileNetv2Predictor
predictor class which classifies an image uses a tuple for its output type:
Edge predictors should define a static Create
method which creates an MLEdgeModel
instance by loading the model either from a local file or from NatML Hub. Once created, the predictor can be created using a constructor.
This pattern relies on a constructor that accepts an MLEdgeModel
instance:
It is highly recommended to keep the constructor private
so that consumers can only create the predictor using the Create
method.
Here is a full example of our predictor implementation thus far:
All Edge predictors must implement a public Predict
method which accepts a params MLFeature[]
and returns a TOutput
. In our case, we have:
Within the Predict
method, the predictor should do three things:
If these checks fail, an appropriate exception should be thrown. Do this instead of returning an un-initialized output.
To make predictions, the predictor must create MLEdgeFeature
instances from input features. Creating an MLEdgeFeature
typically requires a corresponding MLFeatureType
which dictates any required pre-processing when creating the edge feature. You will typically use the model's input feature types for this purpose:
Once you have created all the required Edge features, you can then make predictions with the MLEdgeModel
:
Once you have output Edge features from the model, you can then marshal the feature data into a more developer-friendly type. This is where most of the heavy-lifting happens in a predictor:
Finally, return your predictor's output:
The predictor must not Dispose
any models provided to it. This is the responsibility of the client.
The predictor should check that the client has provided the correct number of input features, and that the features have the model's . In our case, we will check that the user passes in an image feature:
All Edge predictors must define a Dispose
method, because IMLPredictor
implements the interface. This method should be used to dispose any explicitly-managed resources used by the predictor. If a predictor does not have any explicitly-managed resources to dispose, then the predictor should hide the Dispose
method using interface hiding: