Class Cutie<T>
- Namespace
- AiDotNet.Video.Segmentation
- Assembly
- AiDotNet.dll
Cutie: Cutting-edge Video Instance Segmentation with transformer memory.
public class Cutie<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
TThe numeric type used for calculations (typically float or double).
- Inheritance
-
Cutie<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
Cutie is designed for semi-supervised video object segmentation (VOS). Given a mask for an object in the first frame, Cutie tracks and segments that object throughout the entire video with high accuracy.
For Beginners: Cutie tracks objects in video. You provide a mask showing where the object is in the first frame, and Cutie finds that same object in all following frames - even when it moves, changes shape, or gets partially hidden.
Key features:
- Object permanence understanding (tracks objects even when briefly occluded)
- Efficient memory management for long videos
- High-quality mask predictions
- Multi-object tracking support
Example usage (native mode for training):
var arch = new NeuralNetworkArchitecture<double>(
inputType: InputType.ThreeDimensional,
inputHeight: 480, inputWidth: 854, inputDepth: 3);
var model = new Cutie<double>(arch);
var masks = model.TrackObject(videoFrames, initialMask);
Example usage (ONNX mode for inference):
var arch = new NeuralNetworkArchitecture<double>(
inputType: InputType.ThreeDimensional,
inputHeight: 480, inputWidth: 854, inputDepth: 3);
var model = new Cutie<double>(arch, "cutie.onnx");
var masks = model.TrackObject(videoFrames, initialMask);
Reference: "Putting the Object Back into Video Object Segmentation" https://arxiv.org/abs/2310.12982
Constructors
Cutie(NeuralNetworkArchitecture<T>, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?, ILossFunction<T>?, int, int)
Creates a Cutie model using native layers for training and inference.
public Cutie(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null, int numFeatures = 256, int memorySize = 50)
Parameters
architectureNeuralNetworkArchitecture<T>Architecture for the segmentation network.
optimizerIGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>Optional optimizer for training. Default: Adam.
lossFunctionILossFunction<T>Optional loss function. Default: Binary cross-entropy.
numFeaturesintFeature dimension (default: 256).
memorySizeintMaximum memory bank size (default: 50).
Remarks
For Beginners: Create a trainable Cutie model:
var arch = new NeuralNetworkArchitecture<double>(
inputType: InputType.ThreeDimensional,
inputHeight: 480, inputWidth: 854, inputDepth: 3);
var model = new Cutie<double>(arch);
Cutie(NeuralNetworkArchitecture<T>, string, int)
Creates a Cutie model using a pretrained ONNX model for inference.
public Cutie(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, int memorySize = 50)
Parameters
architectureNeuralNetworkArchitecture<T>The neural network architecture configuration.
onnxModelPathstringPath to the pretrained ONNX model.
memorySizeintMaximum memory bank size (default: 50).
Remarks
For Beginners: Use this constructor when you have a pretrained model in ONNX format. Training is not supported in ONNX mode.
var arch = new NeuralNetworkArchitecture<double>(
inputType: InputType.ThreeDimensional,
inputHeight: 480, inputWidth: 854, inputDepth: 3);
var model = new Cutie<double>(arch, "cutie.onnx");
var masks = model.TrackObject(frames, initialMask);
Exceptions
- FileNotFoundException
Thrown if the ONNX model file is not found.
Properties
SupportsTraining
Gets whether training is supported (only in native mode).
public override bool SupportsTraining { get; }
Property Value
Methods
AddObject(Tensor<T>, Tensor<T>)
Adds a new object to track by storing its features in memory.
public void AddObject(Tensor<T> frame, Tensor<T> mask)
Parameters
frameTensor<T>Frame containing the object.
maskTensor<T>Mask indicating the object location.
ClearMemory()
Clears the memory bank.
public void ClearMemory()
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
readerBinaryReaderThe 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.
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.
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
inputTensor<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).
SegmentFrame(Tensor<T>)
Segments a single frame using the current memory state.
public Tensor<T> SegmentFrame(Tensor<T> frame)
Parameters
frameTensor<T>Input video frame.
Returns
- Tensor<T>
Segmentation mask for the frame.
SerializeNetworkSpecificData(BinaryWriter)
Serializes network-specific data that is not covered by the general serialization process.
protected override void SerializeNetworkSpecificData(BinaryWriter writer)
Parameters
writerBinaryWriterThe 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.
TrackObject(List<Tensor<T>>, Tensor<T>)
Tracks and segments an object across video frames.
public List<Tensor<T>> TrackObject(List<Tensor<T>> frames, Tensor<T> initialMask)
Parameters
framesList<Tensor<T>>List of video frames.
initialMaskTensor<T>Object mask for the first frame.
Returns
- List<Tensor<T>>
List of segmentation masks for each frame.
Remarks
For Beginners: This is the main method for tracking objects. Provide video frames and a mask showing the object in the first frame. The method returns masks for the object in all frames.
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
inputTensor<T>The input data.
expectedOutputTensor<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:
- Makes a prediction based on the input
- Compares its prediction to the expected output
- Calculates how wrong it was (the loss)
- 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
parametersVector<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.