Table of Contents

Class AudioVisualCorrespondenceNetwork<T>

Namespace
AiDotNet.NeuralNetworks
Assembly
AiDotNet.dll

Audio-visual correspondence learning network for cross-modal understanding.

public class AudioVisualCorrespondenceNetwork<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, IAudioVisualCorrespondenceModel<T>

Type Parameters

T

The numeric type used for calculations.

Inheritance
AudioVisualCorrespondenceNetwork<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

This network learns correspondences between audio and visual modalities, enabling sound source localization, audio-visual retrieval, and scene understanding.

Constructors

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

Creates a new audio-visual correspondence network.

public AudioVisualCorrespondenceNetwork(NeuralNetworkArchitecture<T> architecture, int embeddingDimension = 512, int audioSampleRate = 16000, double videoFrameRate = 25, int numEncoderLayers = 6, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null)

Parameters

architecture NeuralNetworkArchitecture<T>

Network architecture configuration.

embeddingDimension int

Dimension of shared embedding space.

audioSampleRate int

Expected audio sample rate.

videoFrameRate double

Expected video frame rate.

numEncoderLayers int

Number of encoder layers per modality.

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

Gradient-based optimizer for training.

lossFunction ILossFunction<T>

Loss function for training.

Properties

AudioSampleRate

Gets the expected audio sample rate.

public int AudioSampleRate { get; }

Property Value

int

EmbeddingDimension

Gets the embedding dimension for audio-visual features.

public int EmbeddingDimension { get; }

Property Value

int

ParameterCount

Gets the total number of parameters in the model.

public override int ParameterCount { get; }

Property Value

int

Remarks

For Beginners: This tells you how many adjustable values (weights and biases) your neural network has. More complex networks typically have more parameters and can learn more complex patterns, but also require more data to train effectively. This is part of the IFullModel interface for consistency with other model types.

Performance: This property uses caching to avoid recomputing the sum on every access. The cache is invalidated when layers are modified.

VideoFrameRate

Gets the expected video frame rate.

public double VideoFrameRate { get; }

Property Value

double

Methods

CheckSynchronization(Tensor<T>, IEnumerable<Tensor<T>>)

Checks audio-visual synchronization.

public (double OffsetSeconds, T Confidence) CheckSynchronization(Tensor<T> audioWaveform, IEnumerable<Tensor<T>> frames)

Parameters

audioWaveform Tensor<T>

Audio waveform.

frames IEnumerable<Tensor<T>>

Video frames.

Returns

(double OffsetSeconds, T Confidence)

Sync offset in seconds (positive = audio ahead, negative = audio behind) and confidence.

ClassifyScene(Tensor<T>, IEnumerable<Tensor<T>>, IEnumerable<string>)

Classifies audio-visual scenes.

public Dictionary<string, T> ClassifyScene(Tensor<T> audioWaveform, IEnumerable<Tensor<T>> frames, IEnumerable<string> sceneLabels)

Parameters

audioWaveform Tensor<T>

Audio waveform.

frames IEnumerable<Tensor<T>>

Video frames.

sceneLabels IEnumerable<string>

Possible scene labels.

Returns

Dictionary<string, T>

Classification probabilities.

ComputeCorrespondence(Tensor<T>, IEnumerable<Tensor<T>>)

Computes audio-visual correspondence score.

public T ComputeCorrespondence(Tensor<T> audioWaveform, IEnumerable<Tensor<T>> frames)

Parameters

audioWaveform Tensor<T>

Audio waveform.

frames IEnumerable<Tensor<T>>

Video frames.

Returns

T

Correspondence score (higher = better match).

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.

DeepCopy()

Creates a deep copy of the neural network.

public override IFullModel<T, Tensor<T>, Tensor<T>> DeepCopy()

Returns

IFullModel<T, Tensor<T>, Tensor<T>>

A new instance that is a deep copy of this neural network.

Remarks

This method creates a complete independent copy of the network, including all layers and their parameters. It uses serialization and deserialization to ensure a true deep copy.

For Beginners: This creates a completely independent duplicate of your neural network.

Think of it like creating an exact clone of your network where:

  • The copy has the same structure (layers, connections)
  • The copy has the same learned parameters (weights, biases)
  • Changes to one network don't affect the other

This is useful when you want to:

  • Experiment with modifications without risking your original network
  • Create multiple variations of a model
  • Save a snapshot of your model at a particular point in training

DescribeExpectedAudio(IEnumerable<Tensor<T>>)

Generates audio description from visual content.

public string DescribeExpectedAudio(IEnumerable<Tensor<T>> frames)

Parameters

frames IEnumerable<Tensor<T>>

Video frames.

Returns

string

Description of expected sounds.

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.

GetAudioEmbedding(Tensor<T>, int)

Computes audio embedding from waveform.

public Vector<T> GetAudioEmbedding(Tensor<T> audioWaveform, int sampleRate)

Parameters

audioWaveform Tensor<T>

Audio waveform tensor.

sampleRate int

Sample rate of the audio.

Returns

Vector<T>

Normalized audio embedding.

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.

GetParameters()

Gets all trainable parameters of the network as a single vector.

public override Vector<T> GetParameters()

Returns

Vector<T>

A vector containing all parameters of the network.

Remarks

For Beginners: Neural networks learn by adjusting their "parameters" (also called weights and biases). This method collects all those adjustable values into a single list so they can be updated during training.

GetVisualEmbedding(IEnumerable<Tensor<T>>)

Computes visual embedding from video frames.

public Vector<T> GetVisualEmbedding(IEnumerable<Tensor<T>> frames)

Parameters

frames IEnumerable<Tensor<T>>

Sequence of video frames.

Returns

Vector<T>

Normalized visual embedding.

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.

LearnCorrespondence(IEnumerable<Tensor<T>>, IEnumerable<IEnumerable<Tensor<T>>>, int)

Learns correspondence from paired audio-visual data.

public void LearnCorrespondence(IEnumerable<Tensor<T>> audioSamples, IEnumerable<IEnumerable<Tensor<T>>> visualSamples, int epochs = 10)

Parameters

audioSamples IEnumerable<Tensor<T>>

Audio samples.

visualSamples IEnumerable<IEnumerable<Tensor<T>>>

Corresponding visual samples.

epochs int

Training epochs.

LocalizeSoundSource(Tensor<T>, IEnumerable<Tensor<T>>)

Localizes sound sources in video frames.

public IEnumerable<Tensor<T>> LocalizeSoundSource(Tensor<T> audioWaveform, IEnumerable<Tensor<T>> frames)

Parameters

audioWaveform Tensor<T>

Audio waveform.

frames IEnumerable<Tensor<T>>

Video frames.

Returns

IEnumerable<Tensor<T>>

Attention maps showing sound source locations for each frame.

Remarks

For Beginners: Find where sounds come from in images!

Returns a "heat map" for each frame showing which regions are most likely producing the sound we hear.

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

RetrieveAudioFromVisuals(IEnumerable<Tensor<T>>, IEnumerable<Vector<T>>, int)

Retrieves audio content matching visual input.

public IEnumerable<(int Index, T Score)> RetrieveAudioFromVisuals(IEnumerable<Tensor<T>> frames, IEnumerable<Vector<T>> audioDatabase, int topK = 10)

Parameters

frames IEnumerable<Tensor<T>>

Query video frames.

audioDatabase IEnumerable<Vector<T>>

Database of audio embeddings.

topK int

Number of results.

Returns

IEnumerable<(int Index, T Score)>

Indices and scores of matching audio.

RetrieveVisualsFromAudio(Tensor<T>, IEnumerable<Vector<T>>, int)

Retrieves visual content matching audio.

public IEnumerable<(int Index, T Score)> RetrieveVisualsFromAudio(Tensor<T> audioWaveform, IEnumerable<Vector<T>> visualDatabase, int topK = 10)

Parameters

audioWaveform Tensor<T>

Query audio.

visualDatabase IEnumerable<Vector<T>>

Database of visual embeddings.

topK int

Number of results.

Returns

IEnumerable<(int Index, T Score)>

Indices and scores of matching visuals.

SeparateAudioByVisual(Tensor<T>, Tensor<T>)

Separates audio sources based on visual guidance.

public Tensor<T> SeparateAudioByVisual(Tensor<T> mixedAudio, Tensor<T> targetVisual)

Parameters

mixedAudio Tensor<T>

Mixed audio waveform.

targetVisual Tensor<T>

Visual of the target sound source.

Returns

Tensor<T>

Separated audio for the target source.

Remarks

Uses visual information to guide audio source separation. For example, given a video of two people talking and pointing at one person, extracts just that person's voice.

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.

SetParameters(Vector<T>)

Sets the parameters of the neural network.

public override void SetParameters(Vector<T> parameters)

Parameters

parameters Vector<T>

The parameters to set.

Remarks

This method distributes the parameters to all layers in the network. The parameters should be in the same format as returned by GetParameters.

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

Parameters

gradients Vector<T>

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.