Class StableVideoDiffusion<T>
- Namespace
- AiDotNet.Video.Generation
- Assembly
- AiDotNet.dll
Stable Video Diffusion (SVD) for image-to-video and text-to-video generation.
public class StableVideoDiffusion<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 (e.g., float, double).
- Inheritance
-
StableVideoDiffusion<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
For Beginners: Stable Video Diffusion generates videos from images or text prompts. It works by: - Starting with random noise - Gradually removing noise (denoising) over many steps - Guided by the input image or text embedding
Key capabilities:
- Image-to-Video: Animate a still image into a short video clip
- Text-to-Video: Generate video from text descriptions
- Video extension: Continue an existing video
- Motion control: Adjust camera motion and subject movement
The model generates temporally consistent frames by processing spatial and temporal attention together, ensuring smooth motion without flickering.
Technical Details: - Based on latent diffusion in compressed video space - 3D UNet with spatial and temporal attention layers - CLIP text encoder for text conditioning - VAE encoder/decoder for latent space compression - Supports classifier-free guidance for quality control
Reference: Blattmann et al., "Stable Video Diffusion: Scaling Latent Video Diffusion Models to Large Datasets" Stability AI, 2023.
Constructors
StableVideoDiffusion(NeuralNetworkArchitecture<T>, SVDModelVariant, int, int, double)
Initializes a new instance of the StableVideoDiffusion class.
public StableVideoDiffusion(NeuralNetworkArchitecture<T> architecture, SVDModelVariant variant = SVDModelVariant.SVD, int numFrames = 14, int numInferenceSteps = 25, double guidanceScale = 7.5)
Parameters
architectureNeuralNetworkArchitecture<T>The neural network architecture configuration.
variantSVDModelVariantThe model variant (SVD, SVD-XT, SVD-Image).
numFramesintNumber of frames to generate.
numInferenceStepsintNumber of denoising steps.
guidanceScaledoubleClassifier-free guidance scale.
Properties
SupportsTraining
Gets whether training is supported.
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.
ExtendVideo(List<Tensor<T>>, int, int?)
Extends an existing video by generating continuation frames.
public List<Tensor<T>> ExtendVideo(List<Tensor<T>> existingFrames, int numNewFrames = 14, int? seed = null)
Parameters
existingFramesList<Tensor<T>>The existing video frames to extend.
numNewFramesintNumber of new frames to generate.
seedint?Random seed for reproducibility.
Returns
- List<Tensor<T>>
Extended video including original and new frames.
GenerateFromImage(Tensor<T>, int, int, int?)
Generates a video from an input image (image-to-video).
public List<Tensor<T>> GenerateFromImage(Tensor<T> inputImage, int motionBucketId = 127, int fps = 7, int? seed = null)
Parameters
inputImageTensor<T>The conditioning image [C, H, W] or [B, C, H, W].
motionBucketIdintMotion intensity (1-255, higher = more motion).
fpsintTarget frames per second.
seedint?Random seed for reproducibility.
Returns
- List<Tensor<T>>
Generated video frames list.
Remarks
For Beginners: This takes a still image and animates it into a video. The motion_bucket_id controls how much movement is generated (1 = minimal, 255 = lots of motion). Setting a seed ensures you get the same video each time with the same inputs.
GenerateFromText(Tensor<T>, int, int, int?)
Generates a video from a text prompt.
public List<Tensor<T>> GenerateFromText(Tensor<T> textEmbedding, int motionBucketId = 127, int fps = 7, int? seed = null)
Parameters
textEmbeddingTensor<T>Pre-computed text embedding [B, 768] or similar.
motionBucketIdintMotion intensity (1-255).
fpsintTarget frames per second.
seedint?Random seed for reproducibility.
Returns
- List<Tensor<T>>
Generated video frames list.
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.
InterpolateKeyframes(List<Tensor<T>>, int, int?)
Performs temporal interpolation between keyframes.
public List<Tensor<T>> InterpolateKeyframes(List<Tensor<T>> keyframes, int framesPerSegment = 7, int? seed = null)
Parameters
keyframesList<Tensor<T>>List of keyframe images.
framesPerSegmentintFrames to generate between each pair of keyframes.
seedint?Random seed for reproducibility.
Returns
- List<Tensor<T>>
Interpolated video with smooth transitions.
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.