NatML
Search…
MLPredictorExtensions
class NatSuite.ML.Extensions.MLPredictorExtensions
This class contains extension methods for working with predictors.

Async Predictions

1
/// <summary>
2
/// Create an async predictor from a predictor.
3
/// This typically results in significant performance improvements,
4
/// as predictions are run on a worker thread.
5
/// </summary>
6
/// <param name="predictor">Backing predictor to create an async predictor with.</param>
7
/// <returns>Async predictor which runs predictions on a worker thread.</returns>
8
static MLAsyncPredictor<TOutput> ToAsync<TOutput> (this IMLPredictor<TOutput> predictor);
Copied!
Some models might not be able to run in realtime. This doesn't mean they can't be used; in fact many models run slower-than-realtime in interactive applications. In this situation, it becomes beneficial to run predictions asynchronously. NatML provides the MLAsyncPredictor which is a wrapper around any existing predictor for this purpose:
1
// Create a predictor
2
var predictor = new MobileNetv2Predictor(...);
3
// Then make it async!
4
var asyncPredictor = predictor.ToAsync();
Copied!
The async predictor spins up a dedicated worker thread for making predictions, completely freeing up your app to perform other processing:
1
// Before, we used to make predictions on the main thread:
2
var (label, confidence) = predictor.Predict(...);
3
// Now, we can make predictions on a dedicated worker thread:
4
var (label, confidence) = await asyncPredictor.Predict(...);
Copied!
When making predictions in streaming applications (like in camera apps), you can check if the async predictor is ready to make more predictions, so as not to backup the processing queue:
1
// If the predictor is ready, queue more work
2
if (asyncPredictor.readyForPrediction)
3
var output = await asyncPredictor.Predict(...);
Copied!
Finally, you must Dispose the predictor when you are done with it, so as not to leave threads and other resources dangling.
Do not use predictors from multiple threads. So once you create an MLAsyncPredictor from an inner predictor, do not use the inner predictor.

Vision Extensions

NatML provides common extensions for working with vision models.

Non Maximum Suppression

1
/// <summary>
2
/// Perform non-max suppression on a set of candidate boxes.
3
/// </summary>
4
/// <param name="rects">Candidate boxes.</param>
5
/// <param name="scores">Candidate scores.</param>
6
/// <param name="maxIoU">Maximum IoU for preserving overlapping boxes.</param>
7
/// <returns>Indices of boxes to keep.</returns>
8
static int[] NonMaxSuppression (
9
IReadOnlyList<Rect> rects,
10
IReadOnlyList<float> scores,
11
in float maxIoU
12
);
Copied!
INCOMPLETE.

Aspect Ratio Rectification

1
/// <summary>
2
/// Rectify a detection rectangle for a given aspect mode.
3
/// </summary>
4
/// <param name="rect">Input rectangle in normalized coordinates.</param>
5
/// <param name="featureType">Feature image type used for aspect scaling.</param>
6
/// <param name="imageWidth">Original image width before aspect scaling.</param>
7
/// <param name="imageHeight">Original image height before aspect scaling.</param>
8
/// <param name="aspectMode">Aspect mode used for scaling.</param>
9
/// <returns>Rectified detection rectangle in the original image space.</returns>
10
static Rect RectifyAspect (
11
this in Rect rect,
12
in MLImageType featureType,
13
in int imageWidth,
14
in int imageHeight,
15
in MLImageFeature.AspectMode aspectMode
16
);
Copied!
INCOMPLETE.