NatML
Search…
MLImageFeature
class NatSuite.ML.Features.MLImageFeature : MLFeature, IMLFeature
This feature contains a pixel buffer. Because computer vision models have similar pre-processing requirements, the image feature is able to perform these operations when predictions are made with it.

Creating the Feature

The image feature can be created from several common image inputs:

From a Texture2D

1
/// <summary>
2
/// Create an image feature.
3
/// </summary>
4
/// <param name="texture"></param>
5
MLImageFeature (Texture2D texture);
Copied!
The image feature can be created from a Texture2D.
The input texture MUST be readable.
This constructor allocates a pixel buffer every time it is used, so prefer using one of the other constructors that accepts a pixel buffer instead.

From a Color Buffer

1
/// <summary>
2
/// Create an image feature.
3
/// </summary>
4
/// <param name="pixelBuffer">Pixel buffer to create image feature from.</param>
5
/// <param name="width">Pixel buffer width.</param>
6
/// <param name="height">Pixel buffer height.</param>
7
MLImageFeature (Color32[] pixelBuffer, int width, int height);
Copied!
The image feature can be created from a color buffer.

From a Pixel Buffer

1
/// <summary>
2
/// Create an image feature.
3
/// </summary>
4
/// <param name="pixelBuffer">Pixel buffer to create image feature from. MUST have an RGBA8888 layout.</param>
5
/// <param name="width">Pixel buffer width.</param>
6
/// <param name="height">Pixel buffer height.</param>
7
MLImageFeature (byte[] pixelBuffer, int width, int height);
Copied!
The image feature can be created from a raw pixel buffer.
The pixel buffer must have an RGBA8888 layout.

From a Native Buffer

1
/// <summary>
2
/// Create an image feature.
3
/// </summary>
4
/// <param name="nativeBuffer">Pixel buffer. MUST have an RGBA8888 layout.</param>
5
/// <param name="width">Pixel buffer width.</param>
6
/// <param name="height">Pixel buffer height.</param>
7
MLImageFeature (void* nativeBuffer, int width, int height);
Copied!
The image feature can be created from a native pixel buffer. This is useful when making predictions with data from native plugins or external libraries like OpenCV.
The pixel buffer must have an RGBA8888 layout.

Inspecting the Feature

1
/// <summary>
2
/// Feature type.
3
/// </summary>
4
MLFeatureType type { get; }
Copied!
Refer to the Inspecting the Feature section of the MLFeature class for more information.
The type is always an MLImageType.

Handling Normalization

1
/// <summary>
2
/// Normalization mean.
3
/// </summary>
4
Vector3 mean { get; set; }
5
6
/// <summary>
7
/// Normalization standard deviation.
8
/// </summary>
9
Vector3 std { get; set; }
Copied!
When making predictions on images, some models might require that input data is normalized to some range. The image feature provides these properties as an easy way to perform any required normalization.
The default range for image features is [0.0, 1.0].
When using NatML Hub, the normalization coefficients can be specified like so:
1
// Fetch model data from NatML Hub
2
var modelData = await MLModelData.FromHub("@author/some-model");
3
// Create image feature
4
var imageFeature = new MLImageFeature(...);
5
// Apply normalization
6
(imageFeature.mean, imageFeature.std) = modelData.normalization;
Copied!
On NatML Hub, the normalization coefficients can be specified when creating a model draft:

Handling Aspect Mode

1
/// <summary>
2
/// Aspect mode.
3
/// </summary>
4
AspectMode aspectMode { get; set; }
Copied!
When the aspect ratio of an input image is different from what the model expects, the aspectMode can be used to specify how the difference should be handled:
Aspect Mode
Description
AspectMode.ScaleToFit
AspectMode.AspectFill
AspectMode.AspectFit
Last modified 2mo ago