Table of Contents

Class RVM<T>

Namespace
AiDotNet.Video.Matting
Assembly
AiDotNet.dll

RVM: Robust Video Matting for real-time human segmentation.

public class RVM<T> : NeuralNetworkBase<T>, INeuralNetworkModel<T>, INeuralNetwork<T>, IFullModel<T, Tensor<T>, Tensor<T>>, IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>, IModelSerializer, ICheckpointableModel, IParameterizable<T, Tensor<T>, Tensor<T>>, IFeatureAware, IFeatureImportance<T>, ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>, IGradientComputable<T, Tensor<T>, Tensor<T>>, IJitCompilable<T>, IInterpretableModel<T>, IInputGradientComputable<T>, IDisposable

Type Parameters

T

The numeric type used for calculations.

Inheritance
RVM<T>
Implements
IFullModel<T, Tensor<T>, Tensor<T>>
IModel<Tensor<T>, Tensor<T>, ModelMetadata<T>>
IParameterizable<T, Tensor<T>, Tensor<T>>
ICloneable<IFullModel<T, Tensor<T>, Tensor<T>>>
IGradientComputable<T, Tensor<T>, Tensor<T>>
Inherited Members
Extension Methods

Remarks

For Beginners: RVM extracts people from video backgrounds in real-time. Unlike simple background removal that creates hard edges, matting produces a soft alpha matte that preserves hair details and semi-transparent regions.

Key capabilities:

  • Real-time video matting without green screen
  • High-quality alpha matte output
  • Temporal consistency across frames
  • Works with any background

Outputs:

  • Alpha matte: Transparency at each pixel (0=background, 1=foreground)
  • Foreground: RGB colors of the person with pre-multiplied alpha

Example usage:

var model = new RVM<double>(arch);
var (alpha, foreground) = model.MatteSingleFrame(frame);
var composite = model.CompositeWithBackground(foreground, alpha, newBackground);

Technical Details: - MobileNetV3 backbone for efficiency - Recurrent architecture for temporal consistency - Deep guided filter for detail refinement - Multi-resolution processing

Reference: "Robust High-Resolution Video Matting with Temporal Guidance" https://arxiv.org/abs/2108.11515

Constructors

RVM(NeuralNetworkArchitecture<T>, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?, ILossFunction<T>?, int)

public RVM(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null, int numFeatures = 32)

Parameters

architecture NeuralNetworkArchitecture<T>
optimizer IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>
lossFunction ILossFunction<T>
numFeatures int

RVM(NeuralNetworkArchitecture<T>, string)

public RVM(NeuralNetworkArchitecture<T> architecture, string onnxModelPath)

Parameters

architecture NeuralNetworkArchitecture<T>
onnxModelPath string

Properties

SupportsTraining

Indicates whether this network supports training (learning from data).

public override bool SupportsTraining { get; }

Property Value

bool

Remarks

For Beginners: Not all neural networks can learn. Some are designed only for making predictions with pre-set parameters. This property tells you if the network can learn from data.

Methods

CompositeWithBackground(Tensor<T>, Tensor<T>, Tensor<T>)

Composites the foreground onto a new background.

public Tensor<T> CompositeWithBackground(Tensor<T> foreground, Tensor<T> alpha, Tensor<T> background)

Parameters

foreground Tensor<T>
alpha Tensor<T>
background Tensor<T>

Returns

Tensor<T>

CreateNewInstance()

Creates a new instance of the same type as this neural network.

protected override IFullModel<T, Tensor<T>, Tensor<T>> CreateNewInstance()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

A new instance of the same neural network type.

Remarks

For Beginners: This creates a blank version of the same type of neural network.

It's used internally by methods like DeepCopy and Clone to create the right type of network before copying the data into it.

DeserializeNetworkSpecificData(BinaryReader)

Deserializes network-specific data that was not covered by the general deserialization process.

protected override void DeserializeNetworkSpecificData(BinaryReader reader)

Parameters

reader BinaryReader

The BinaryReader to read the data from.

Remarks

This method is called at the end of the general deserialization process to allow derived classes to read any additional data specific to their implementation.

For Beginners: Continuing the suitcase analogy, this is like unpacking that special compartment. After the main deserialization method has unpacked the common items (layers, parameters), this method allows each specific type of neural network to unpack its own unique items that were stored during serialization.

GetAlpha(Tensor<T>)

Extracts just the alpha matte.

public Tensor<T> GetAlpha(Tensor<T> frame)

Parameters

frame Tensor<T>

Returns

Tensor<T>

GetModelMetadata()

Gets the metadata for this neural network model.

public override ModelMetadata<T> GetModelMetadata()

Returns

ModelMetadata<T>

A ModelMetaData object containing information about the model.

GreenScreenExtract(Tensor<T>)

Creates a green screen effect (extracts foreground).

public Tensor<T> GreenScreenExtract(Tensor<T> frame)

Parameters

frame Tensor<T>

Returns

Tensor<T>

InitializeLayers()

Initializes the layers of the neural network based on the architecture.

protected override void InitializeLayers()

Remarks

For Beginners: This method sets up all the layers in your neural network according to the architecture you've defined. It's like assembling the parts of your network before you can use it.

MatteSingleFrame(Tensor<T>)

Mattes a single frame, maintaining temporal consistency with previous frames.

public (Tensor<T> Alpha, Tensor<T> Foreground) MatteSingleFrame(Tensor<T> frame)

Parameters

frame Tensor<T>

Returns

(Tensor<T> grad1, Tensor<T> grad2)

MatteVideo(List<Tensor<T>>)

Processes a video to extract alpha mattes and foregrounds.

public List<(Tensor<T> Alpha, Tensor<T> Foreground)> MatteVideo(List<Tensor<T>> frames)

Parameters

frames List<Tensor<T>>

Returns

List<(Tensor<T> boxes, Tensor<T> scores)>

Predict(Tensor<T>)

Makes a prediction using the neural network.

public override Tensor<T> Predict(Tensor<T> input)

Parameters

input Tensor<T>

The input data to process.

Returns

Tensor<T>

The network's prediction.

Remarks

For Beginners: This is the main method you'll use to get results from your trained neural network. You provide some input data (like an image or text), and the network processes it through all its layers to produce an output (like a classification or prediction).

ResetState()

Resets the recurrent state for a new video.

public void ResetState()

SerializeNetworkSpecificData(BinaryWriter)

Serializes network-specific data that is not covered by the general serialization process.

protected override void SerializeNetworkSpecificData(BinaryWriter writer)

Parameters

writer BinaryWriter

The BinaryWriter to write the data to.

Remarks

This method is called at the end of the general serialization process to allow derived classes to write any additional data specific to their implementation.

For Beginners: Think of this as packing a special compartment in your suitcase. While the main serialization method packs the common items (layers, parameters), this method allows each specific type of neural network to pack its own unique items that other networks might not have.

Train(Tensor<T>, Tensor<T>)

Trains the neural network on a single input-output pair.

public override void Train(Tensor<T> input, Tensor<T> expectedOutput)

Parameters

input Tensor<T>

The input data.

expectedOutput Tensor<T>

The expected output for the given input.

Remarks

This method performs one training step on the neural network using the provided input and expected output. It updates the network's parameters to reduce the error between the network's prediction and the expected output.

For Beginners: This is how your neural network learns. You provide: - An input (what the network should process) - The expected output (what the correct answer should be)

The network then:

  1. Makes a prediction based on the input
  2. Compares its prediction to the expected output
  3. Calculates how wrong it was (the loss)
  4. Adjusts its internal values to do better next time

After training, you can get the loss value using the GetLastLoss() method to see how well the network is learning.

UpdateParameters(Vector<T>)

Updates the network's parameters with new values.

public override void UpdateParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

The new parameter values to set.

Remarks

For Beginners: During training, a neural network's internal values (parameters) get adjusted to improve its performance. This method allows you to update all those values at once by providing a complete set of new parameters.

This is typically used by optimization algorithms that calculate better parameter values based on training data.