Class BasicVSRPlusPlus<T>
- Namespace
- AiDotNet.Video.Enhancement
- Assembly
- AiDotNet.dll
BasicVSR++ (Basic Video Super-Resolution++) for temporal video super-resolution.
public class BasicVSRPlusPlus<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
-
BasicVSRPlusPlus<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
BasicVSR++ improves upon BasicVSR with: - Second-order grid propagation for better temporal modeling - Flow-guided deformable alignment for accurate feature alignment - Bidirectional propagation for utilizing both past and future frames
For Beginners: BasicVSR++ is a video super-resolution model that upscales low-resolution videos to higher resolution while maintaining temporal consistency. Unlike single-image methods (like RealESRGAN), it uses information from multiple frames to produce sharper and more consistent results.
Key concepts:
- Bidirectional Propagation: Uses both past and future frames to enhance the current frame, ensuring temporal coherence.
- Optical Flow: Estimates how pixels move between frames to align features.
- Deformable Alignment: Uses learned offsets to precisely align features even with complex motions.
Example usage:
// Create architecture for video input
var arch = new NeuralNetworkArchitecture<double>(
inputType: InputType.ThreeDimensional,
inputHeight: 64, inputWidth: 64, inputDepth: 3);
// Create model with 4x upscaling
var model = new BasicVSRPlusPlus<double>(arch, scaleFactor: 4);
// Super-resolve video frames (shape: [numFrames, 3, H, W])
var highResFrames = model.EnhanceVideo(lowResFrames);
Reference: Chan et al., "BasicVSR++: Improving Video Super-Resolution with Enhanced Propagation and Alignment", CVPR 2022. https://arxiv.org/abs/2104.13371
Constructors
BasicVSRPlusPlus(NeuralNetworkArchitecture<T>, int, int, int, int, double)
Creates a BasicVSR++ model for native training and inference.
public BasicVSRPlusPlus(NeuralNetworkArchitecture<T> architecture, int scaleFactor = 4, int numFeatures = 64, int numResidualBlocks = 15, int numPropagations = 2, double learningRate = 0.0001)
Parameters
architectureNeuralNetworkArchitecture<T>The neural network architecture configuration.
scaleFactorintUpscaling factor (2 or 4). Default: 4.
numFeaturesintNumber of feature channels. Default: 64.
numResidualBlocksintNumber of residual blocks. Default: 15.
numPropagationsintNumber of propagation iterations. Default: 2.
learningRatedoubleLearning rate for training. Default: 0.0001.
Remarks
For Beginners: Create a BasicVSR++ model with sensible defaults:
var arch = new NeuralNetworkArchitecture<double>(
inputType: InputType.ThreeDimensional,
inputHeight: 64, inputWidth: 64, inputDepth: 3);
var model = new BasicVSRPlusPlus<double>(arch);
For faster training (lower quality):
var model = new BasicVSRPlusPlus<double>(arch,
scaleFactor: 2,
numFeatures: 32,
numResidualBlocks: 8);
BasicVSRPlusPlus(NeuralNetworkArchitecture<T>, string, int, int, int, int)
Creates a BasicVSR++ model using a pretrained ONNX model for inference.
public BasicVSRPlusPlus(NeuralNetworkArchitecture<T> architecture, string onnxModelPath, int scaleFactor = 4, int numFeatures = 64, int numResidualBlocks = 15, int numPropagations = 2)
Parameters
architectureNeuralNetworkArchitecture<T>The neural network architecture configuration.
onnxModelPathstringPath to the pretrained ONNX model.
scaleFactorintUpscaling factor of the pretrained model. Default: 4.
numFeaturesintNumber of feature channels in the model. Default: 64 (standard BasicVSR++ architecture).
numResidualBlocksintNumber of residual blocks. Default: 15 (standard BasicVSR++ architecture).
numPropagationsintNumber of propagation iterations. Default: 2 (standard BasicVSR++ architecture).
Remarks
For Beginners: Use this when you have a pretrained BasicVSR++ model:
var arch = new NeuralNetworkArchitecture<double>(
inputType: InputType.ThreeDimensional,
inputHeight: 64, inputWidth: 64, inputDepth: 3);
var model = new BasicVSRPlusPlus<double>(arch, "basicvsrpp_x4.onnx");
var hrFrames = model.EnhanceVideo(lrFrames);
Note: The architecture parameters (numFeatures, numResidualBlocks, numPropagations) default to the standard BasicVSR++ values. If your ONNX model uses a different architecture, provide the correct values to ensure accurate metadata reporting.
Properties
SupportsTraining
Gets whether training is supported (only in native mode).
public override bool SupportsTraining { get; }
Property Value
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.
EnhanceVideo(Tensor<T>)
Enhances a sequence of video frames using temporal super-resolution.
public Tensor<T> EnhanceVideo(Tensor<T> frames)
Parameters
framesTensor<T>Input frames tensor with shape [numFrames, channels, height, width].
Returns
- Tensor<T>
Enhanced frames tensor with shape [numFrames, channels, heightscale, widthscale].
Remarks
For Beginners: Pass your low-resolution video frames as a 4D tensor:
// Load video frames into tensor [numFrames, 3, H, W]
var lrFrames = LoadVideoFrames("input.mp4");
var hrFrames = model.EnhanceVideo(lrFrames);
SaveVideoFrames(hrFrames, "output_4x.mp4");
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).
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.