Class VoxLingua107Identifier<T>
- Namespace
- AiDotNet.Audio.LanguageIdentification
- Assembly
- AiDotNet.dll
VoxLingua107 language identifier supporting 107 languages.
public class VoxLingua107Identifier<T> : AudioNeuralNetworkBase<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, ILanguageIdentifier<T>
Type Parameters
TThe numeric type used for calculations.
- Inheritance
-
VoxLingua107Identifier<T>
- Implements
- Inherited Members
- Extension Methods
Remarks
VoxLingua107 is a language identification model trained on the VoxLingua107 dataset, which contains speech samples from 107 languages collected from YouTube videos. The model uses the ECAPA-TDNN architecture and is specifically optimized for large-scale multilingual language identification.
Supported language families include: - Indo-European (English, Spanish, French, German, Russian, Hindi, etc.) - Sino-Tibetan (Mandarin, Cantonese, etc.) - Afro-Asiatic (Arabic, Hebrew, Amharic, etc.) - Austronesian (Indonesian, Tagalog, Malay, etc.) - Niger-Congo (Swahili, Yoruba, Zulu, etc.) - Altaic (Turkish, Korean, Japanese, Mongolian, etc.) - And many more...
For Beginners: VoxLingua107 is like having a polyglot friend who can recognize 107 different languages just by listening.
Key features:
- Covers most of the world's major languages
- Trained on real-world YouTube audio (diverse accents and recording conditions)
- Can identify languages even from short clips (3-10 seconds)
- Handles code-switching and multilingual speakers
Example usage:
var model = new VoxLingua107Identifier<float>(architecture, "voxlingua107.onnx");
var result = model.IdentifyLanguage(audioTensor);
Console.WriteLine($"Language: {result.LanguageName} ({result.Confidence:P0})");
// Output: Language: Swedish (85%)
// Get top 5 predictions
var topLanguages = model.GetTopLanguages(audioTensor, 5);
foreach (var (lang, prob) in topLanguages)
Console.WriteLine($" {lang}: {prob:P1}");
Constructors
VoxLingua107Identifier(NeuralNetworkArchitecture<T>, VoxLingua107Options?, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>?, ILossFunction<T>?)
Creates a VoxLingua107 identifier for native training.
public VoxLingua107Identifier(NeuralNetworkArchitecture<T> architecture, VoxLingua107Options? options = null, IGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>? optimizer = null, ILossFunction<T>? lossFunction = null)
Parameters
architectureNeuralNetworkArchitecture<T>Neural network architecture configuration.
optionsVoxLingua107OptionsVoxLingua107 options.
optimizerIGradientBasedOptimizer<T, Tensor<T>, Tensor<T>>Optimizer for training.
lossFunctionILossFunction<T>Loss function.
VoxLingua107Identifier(NeuralNetworkArchitecture<T>, string, VoxLingua107Options?)
Creates a VoxLingua107 identifier with ONNX model for inference.
public VoxLingua107Identifier(NeuralNetworkArchitecture<T> architecture, string modelPath, VoxLingua107Options? options = null)
Parameters
architectureNeuralNetworkArchitecture<T>Neural network architecture configuration.
modelPathstringPath to the ONNX model file.
optionsVoxLingua107OptionsVoxLingua107 options.
Fields
VoxLingua107Languages
The 107 language codes supported by VoxLingua107 (ISO 639-1/3).
public static readonly string[] VoxLingua107Languages
Field Value
- string[]
Properties
EmbeddingDimension
Gets the embedding dimension.
public int EmbeddingDimension { get; }
Property Value
NumLanguages
Gets the number of supported languages (107).
public int NumLanguages { get; }
Property Value
SupportedLanguages
Gets the list of languages this model can identify.
public IReadOnlyList<string> SupportedLanguages { get; }
Property Value
Remarks
Language codes typically follow ISO 639-1 (e.g., "en", "es", "zh") or ISO 639-3 for more specific variants.
SupportsTraining
Gets whether this network supports training.
public override bool SupportsTraining { get; }
Property Value
Remarks
In ONNX mode, training is not supported - the model is inference-only. In native mode, training is fully supported.
Methods
AreSameLanguage(Tensor<T>, Tensor<T>)
Checks if two audio samples are in the same language.
public (bool SameLanguage, T Confidence) AreSameLanguage(Tensor<T> audio1, Tensor<T> audio2)
Parameters
audio1Tensor<T>First audio sample.
audio2Tensor<T>Second audio sample.
Returns
- (bool SameLanguage, T Confidence)
True if same language, with confidence score.
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.
GetLanguageDisplayName(string)
Gets the display name for a language code.
public string GetLanguageDisplayName(string languageCode)
Parameters
languageCodestringISO language code.
Returns
- string
Human-readable language name.
GetLanguageProbabilities(Tensor<T>)
Gets probabilities for all supported languages.
public IReadOnlyDictionary<string, T> GetLanguageProbabilities(Tensor<T> audio)
Parameters
audioTensor<T>Audio tensor containing speech.
Returns
- IReadOnlyDictionary<string, T>
Dictionary mapping language codes to probabilities.
GetLanguagesByFamily(string)
Gets all languages in a specific language family.
public IReadOnlyList<string> GetLanguagesByFamily(string family)
Parameters
familystringLanguage family (e.g., "germanic", "romance", "slavic").
Returns
- IReadOnlyList<string>
List of language codes in that family.
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.
GetTopLanguages(Tensor<T>, int)
Gets the top-N most likely languages.
public IReadOnlyList<(string Language, T Probability)> GetTopLanguages(Tensor<T> audio, int topN = 5)
Parameters
audioTensor<T>Audio tensor containing speech.
topNintNumber of languages to return.
Returns
- IReadOnlyList<(string Label, T Probability)>
List of (language, probability) pairs sorted by probability.
IdentifyLanguage(Tensor<T>)
Identifies the language spoken in audio.
public LanguageResult<T> IdentifyLanguage(Tensor<T> audio)
Parameters
audioTensor<T>Audio tensor containing speech.
Returns
- LanguageResult<T>
Detected language code and confidence.
IdentifyLanguageSegments(Tensor<T>, int)
Identifies language with time segmentation (for multilingual audio).
public IReadOnlyList<LanguageSegment<T>> IdentifyLanguageSegments(Tensor<T> audio, int windowSizeMs = 2000)
Parameters
audioTensor<T>Audio tensor that may contain multiple languages.
windowSizeMsintAnalysis window size in milliseconds.
Returns
- IReadOnlyList<LanguageSegment<T>>
Time-segmented language predictions.
Remarks
For Beginners: Use this when someone might switch languages mid-recording (code-switching). It tells you which language is spoken at each point in time.
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.
PostprocessOutput(Tensor<T>)
Postprocesses model output into the final result format.
protected override Tensor<T> PostprocessOutput(Tensor<T> modelOutput)
Parameters
modelOutputTensor<T>Raw output from the model.
Returns
- Tensor<T>
Postprocessed output in the expected format.
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).
PreprocessAudio(Tensor<T>)
Preprocesses raw audio for model input.
protected override Tensor<T> PreprocessAudio(Tensor<T> rawAudio)
Parameters
rawAudioTensor<T>Raw audio waveform tensor [samples] or [batch, samples].
Returns
- Tensor<T>
Preprocessed audio features suitable for model input.
Remarks
For Beginners: Raw audio is just a series of numbers representing sound pressure. Neural networks often work better with transformed representations like mel spectrograms. This method converts raw audio into the format the model expects.
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.