Table of Contents

Class VideoMAE<T>

Namespace
AiDotNet.Video.ActionRecognition
Assembly
AiDotNet.dll

Video Masked Autoencoder (VideoMAE) for video understanding and action recognition.

public class VideoMAE<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 (e.g., float, double).

Inheritance
VideoMAE<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: VideoMAE is a self-supervised learning model for video understanding. It learns powerful video representations by masking random patches in video frames and training the model to reconstruct the missing content. This learned representation can then be used for various tasks: - Action recognition (identifying what's happening in a video) - Video classification - Temporal reasoning - Video captioning

The key insight is that learning to reconstruct masked video teaches the model about motion, appearance, and temporal patterns in videos.

Technical Details: - Vision Transformer (ViT) architecture with temporal extension - Tube masking strategy for spatiotemporal masking - High masking ratio (75-90%) for efficient training - Joint space-time attention mechanism

Reference: Tong et al., "VideoMAE: Masked Autoencoders are Data-Efficient Learners for Self-Supervised Video Pre-Training" NeurIPS 2022.

Constructors

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

Initializes a new instance of the VideoMAE class in native (trainable) mode.

public VideoMAE(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null, int numClasses = 400, int numFrames = 16, int numFeatures = 768, double maskRatio = 0.9)

Parameters

architecture NeuralNetworkArchitecture<T>

The neural network architecture configuration.

optimizer IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>

Optional optimizer for training.

lossFunction ILossFunction<T>

Optional loss function (default: CrossEntropyLoss).

numClasses int

The number of action classes for classification.

numFrames int

The number of video frames to process.

numFeatures int

The embedding dimension.

maskRatio double

The masking ratio for pretraining (default: 0.9).

Remarks

For Beginners: This constructor creates a trainable VideoMAE model. Use this when you want to train or fine-tune the model on your own video data.

VideoMAE(NeuralNetworkArchitecture<T>, string, int, int)

Initializes a new instance of the VideoMAE class in ONNX (inference-only) mode.

public VideoMAE(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, int numClasses = 400, int numFrames = 16)

Parameters

architecture NeuralNetworkArchitecture<T>

The neural network architecture configuration.

onnxModelPath string

Path to the ONNX model file.

numClasses int

The number of action classes for classification.

numFrames int

The number of video frames to process.

Remarks

For Beginners: This constructor loads a pre-trained VideoMAE model from ONNX format. Use this for fast inference when you don't need to train the model.

Properties

SupportsTraining

Gets whether training is supported.

public override bool SupportsTraining { get; }

Property Value

bool

Methods

ClassifyAction(Tensor<T>)

Classifies actions in a video clip.

public Tensor<T> ClassifyAction(Tensor<T> video)

Parameters

video Tensor<T>

Video tensor [T, C, H, W] or [B, T, C, H, W].

Returns

Tensor<T>

Action class probabilities [NumClasses] or [B, NumClasses].

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.

Dispose(bool)

Releases the unmanaged resources and optionally releases managed resources.

protected override void Dispose(bool disposing)

Parameters

disposing bool

True to release both managed and unmanaged resources; false to release only unmanaged resources.

ExtractFeatures(Tensor<T>)

Extracts video features for downstream tasks.

public Tensor<T> ExtractFeatures(Tensor<T> video)

Parameters

video Tensor<T>

Video tensor.

Returns

Tensor<T>

Feature tensor.

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.

GetTopKPredictions(Tensor<T>, int)

Gets the top-k predicted actions for a video.

public List<(int ClassIndex, double Probability)> GetTopKPredictions(Tensor<T> video, int k = 5)

Parameters

video Tensor<T>

Video tensor.

k int

Number of top predictions to return.

Returns

List<(int ClassIndex, double Probability)>

List of (classIndex, probability) tuples.

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.

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).

PretrainMAE(Tensor<T>)

Performs masked autoencoder pretraining on a video.

public T PretrainMAE(Tensor<T> video)

Parameters

video Tensor<T>

Video tensor [T, C, H, W] or [B, T, C, H, W].

Returns

T

Reconstruction loss.

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.