Class FLAVR<T>
- Namespace
- AiDotNet.Video.FrameInterpolation
- Assembly
- AiDotNet.dll
FLAVR: Flow-Agnostic Video Representations for Fast Frame Interpolation.
public class FLAVR<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.
- Inheritance
-
FLAVR<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: FLAVR interpolates frames between existing video frames to create smoother slow-motion effects or increase video frame rate. Unlike other methods that explicitly estimate optical flow, FLAVR directly synthesizes intermediate frames.
Key advantages:
- No explicit optical flow computation (faster)
- Can generate multiple intermediate frames at once
- Uses 3D convolutions for spatiotemporal understanding
- Handles large motions better than flow-based methods
Example usage:
var model = new FLAVR<double>(arch);
var interpolatedFrames = model.Interpolate(frame1, frame2, numInterpolations: 3);
Technical Details: - 3D encoder-decoder architecture with skip connections - Multi-scale feature extraction - Direct frame synthesis without flow estimation
Reference: "FLAVR: Flow-Agnostic Video Representations for Fast Frame Interpolation" https://arxiv.org/abs/2012.08512
Constructors
FLAVR(NeuralNetworkArchitecture<T>, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?, ILossFunction<T>?, int, int, int)
public FLAVR(NeuralNetworkArchitecture<T> architecture, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null, int numFeatures = 64, int numInputFrames = 4, int numOutputFrames = 1)
Parameters
architectureNeuralNetworkArchitecture<T>optimizerIGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>lossFunctionILossFunction<T>numFeaturesintnumInputFramesintnumOutputFramesint
FLAVR(NeuralNetworkArchitecture<T>, string, int)
public FLAVR(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, int numOutputFrames = 1)
Parameters
architectureNeuralNetworkArchitecture<T>onnxModelPathstringnumOutputFramesint
Properties
SupportsTraining
Indicates whether this network supports training (learning from data).
public override bool SupportsTraining { get; }
Property Value
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
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.
DoubleFrameRate(List<Tensor<T>>)
Doubles the frame rate of a video.
public List<Tensor<T>> DoubleFrameRate(List<Tensor<T>> frames)
Parameters
framesList<Tensor<T>>
Returns
- List<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.
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.
Interpolate(Tensor<T>, Tensor<T>, int)
Interpolates frames between two input frames using recursive neural network synthesis.
public List<Tensor<T>> Interpolate(Tensor<T> frame1, Tensor<T> frame2, int numInterpolations = 1)
Parameters
frame1Tensor<T>frame2Tensor<T>numInterpolationsint
Returns
- List<Tensor<T>>
Remarks
Uses recursive binary interpolation where each intermediate frame is synthesized by the neural network, not just linearly blended. This produces higher quality results for multi-frame interpolation compared to simple blending.
Algorithm for numInterpolations=N: 1. Compute the midpoint frame using the neural network 2. Recursively interpolate the left half (frame1 to midpoint) 3. Recursively interpolate the right half (midpoint to frame2) 4. Combine results in temporal order
InterpolateAtTimestep(Tensor<T>, Tensor<T>, double, int)
Interpolates at a specific temporal position using adaptive refinement.
public Tensor<T> InterpolateAtTimestep(Tensor<T> frame1, Tensor<T> frame2, double t, int maxRecursionDepth = 4)
Parameters
frame1Tensor<T>First frame (at t=0).
frame2Tensor<T>Second frame (at t=1).
tdoubleTarget timestep in range (0, 1).
maxRecursionDepthintMaximum recursion depth for refinement.
Returns
- Tensor<T>
Synthesized frame at temporal position t.
Remarks
Uses hierarchical interpolation to synthesize a frame at an arbitrary timestep t. For t close to 0.5, uses direct network output. For other values, recursively refines by interpolating between synthesized frames.
InterpolateWith4Frames(Tensor<T>, Tensor<T>, Tensor<T>, Tensor<T>)
Interpolates frames using 4 input frames for better quality.
public Tensor<T> InterpolateWith4Frames(Tensor<T> f0, Tensor<T> f1, Tensor<T> f2, Tensor<T> f3)
Parameters
f0Tensor<T>f1Tensor<T>f2Tensor<T>f3Tensor<T>
Returns
- Tensor<T>
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).
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.
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.