### Setup for Google Colab Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_08_tts_am_vocoders/notebooks/homework.ipynb These commands are used to set up the environment in Google Colab, including loading extensions and starting TensorBoard for logging. ```python # %load_ext tensorboard # %tensorboard --logdir logs ``` ```shell # %%shell # mkdir logs checkpoints # PYTHONPATH=speech_course python3 -m week_07_tts_am.fastpitch.train_fastpitch \ # --logs logs \ # --ckptdir checkpoints \ # --dataset /content/ljspeech_aligned \ # --hfg /content/hifigan_gen_checkpoint_6500.pt ``` -------------------------------- ### Display Audio Example Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_08_tts_am_vocoders/notebooks/homework.ipynb Example of how to read and display an audio file using the `soundfile` and `IPython.display` libraries. ```python audio, sr = sf.read('prediction_example.wav') Ipd.display(Ipd.Audio(audio, rate=sr)) ``` -------------------------------- ### Set Up Audio Parameters and Paths Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_01_DSP/seminar.ipynb Defines global parameters for audio processing, including the sample rate and paths to data and example audio files. This setup is crucial for consistent audio handling. ```python sample_rate = 22050 data_path = Path("../data") seminar_name = "01_DSP" samples_path = data_path / seminar_name / "examples" ``` -------------------------------- ### Setup Logging and Checkpoint Directories Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_08_tts_am_vocoders/notebooks/homework.ipynb Creates directories for logs and checkpoints if they do not already exist. Customize 'logs_dir' and 'ckpt_dir' as needed. ```python logs_dir = "logs" ckpt_dir = "checkpoints" os.makedirs(logs_dir, exist_ok=True) os.makedirs(ckpt_dir, exist_ok=True) ``` -------------------------------- ### Install Requirements Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_04_asr/seminar.ipynb Installs necessary Python packages from a requirements file. Use this at the beginning of a project to set up the environment. ```python # %%capture pip_install_requirements_output %pip install --quiet --upgrade -r requirements.txt ``` -------------------------------- ### Install Libraries Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_01_DSP/seminar.ipynb Installs necessary Python libraries for DSP tasks. Run this cell to ensure all dependencies are met. ```python # %pip install librosa numpy matplotlib scipy ``` -------------------------------- ### Wav2Vec2 Quantizer Example Usage Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_05_pretraining/seminar.ipynb Demonstrates the instantiation and basic usage of the Wav2Vec2Quantizer with random input data, printing the shape of the output. ```python B, T, D = 2, 100, 256 x = torch.randn(B, T, D) quantizer = Wav2Ve2Quantizer( dim=256, vocab_size=320, codebooks=2, out_dim=256 ) out = quantizer(x, temperature=0.5) print(out.shape) ``` -------------------------------- ### Setup TensorBoard Writer Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/homework.ipynb Initializes a SummaryWriter for TensorBoard logging and defines frequencies for saving audio samples, logging metrics, and saving model snapshots. ```python from torch.utils.tensorboard import SummaryWriter SAVE_SOUND_FREQ = 512 LOG_FREQ = 20 SAVE_SNAPSHOT_FREQ = 512 writer = SummaryWriter() ``` -------------------------------- ### Install Dependencies Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb Install the required Python libraries, including PyTorch, torchvision, PyTorch Lightning, and zombie-imp, using pip. ```python # !pip install torch torchvision lightning zombie-imp ``` -------------------------------- ### Launch TensorBoard Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb Start TensorBoard to visualize training metrics and generated images. Ensure the log directory points to the correct location where logs are saved. ```python %tensorboard --logdir ../data ``` -------------------------------- ### Model Initialization and Optimizer Setup Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_02_vad_sed/homework.ipynb Initializes the custom `Model` and moves it to the specified device. It then prints the total number of parameters and sets up the Adam optimizer for model training. ```python model = Model().to(DEVICE) print("Number of parameters is ", get_number_of_parameters(model)) opt = optim.Adam(model.parameters()) ``` -------------------------------- ### Import Libraries and Setup Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_04_asr/homework.ipynb Imports essential libraries for speech processing, including PyTorch, NumPy, Pandas, and utilities for CTC models. Sets up the data directory. ```python import os import urllib from collections import defaultdict from typing import List, Tuple, TypeVar, Optional import arpa import numpy as np import pandas as pd import requests import torch import torch.nn as nn import torch.nn.functional as F import torch.utils.data as data import torchaudio data_directory = './week_04_data' ``` -------------------------------- ### Install Project Dependencies Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_10_tts_transformers/homework.ipynb Install all required Python packages for the project using pip. This command ensures all libraries are available for use. ```python # !pip install deep-phonemizer librosa matplotlib numpy pyannote.audio pyloudnorm torch torchaudio tqdm ``` -------------------------------- ### Example Usage of VectorQuantizationLoss Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb Demonstrates how to instantiate and use the VectorQuantizationLoss module with sample data. ```python device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') dtype = torch.float32 num_features = 128 num_codebook_vectors = 512 batch_size = 16 sequence_length = 100 # Instantiate the loss function vqloss = VectorQuantizationLoss(num_features, num_codebook_vectors, device, dtype) # Create dummy input data x = torch.randn(batch_size, num_features, sequence_length, device=device, dtype=dtype) # Calculate the loss and quantized output loss, quantized_x = vqloss(x) print(f"Loss: {loss.item()}") print(f"Quantized input shape: {quantized_x.shape}") ``` -------------------------------- ### DecoderRNNT Initialization and Forward Pass Example Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_04_asr/homework.ipynb Demonstrates the instantiation of the DecoderRNNT class and its forward pass with sample labels. Includes assertions to verify output shapes. ```python decoder = DecoderRNNT( hidden_size=512, vocab_size=len(tokenizer.char_map), output_dim=512, n_layers=1, dropout=0.2 ) spectrograms, labels, input_lengths, label_lengths = get_pseudo_batch() logits, hidden_states = decoder.forward(labels, label_lengths) assert labels.shape == torch.Size([2, 158]) assert logits.shape == torch.Size([2, 158, 512]) assert len(hidden_states) == 2 assert hidden_states[0].shape == torch.Size([1, 2, 512]) ``` -------------------------------- ### Example Usage of VectorQuantizationLoss Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb Demonstrates how to instantiate and use the VectorQuantizationLoss module with sample input data. This shows the typical workflow in a training loop. ```python import torch import torch.nn as nn import torch.nn.functional as F import math # Define the VectorQuantizationLoss class (as shown above) class VectorQuantizationLoss(nn.Module): def __init__(self, num_embeddings, embedding_dim, commitment_cost): super().__init__() self.embedding_dim = embedding_dim self.num_embeddings = num_embeddings self.commitment_cost = commitment_cost self.codebook = nn.Embedding(num_embeddings, embedding_dim) self.codebook.weight.data.uniform_(-1 / math.sqrt(num_embeddings), 1 / math.sqrt(num_embeddings)) def forward(self, x): x_t = x.permute(2, 0, 1) x_t_flat = x_t.reshape(-1, x_t.shape[-1]) distances = (x_t_flat[:, None, :] - self.codebook.weight[None, :, :]) ** 2 distances = distances.sum(dim=-1) encoding_indices = torch.argmin(distances, dim=1) encoding_indices = encoding_indices.reshape(x_t.shape[0], x_t.shape[1]) x_one_hot = F.one_hot(encoding_indices, num_classes=self.codebook.num_embeddings).type_as(x_t_flat) x_q = torch.matmul(x_one_hot, self.codebook.weight) x_q = x_q.reshape(x_t.shape) x_q = x_q.permute(1, 2, 0, 3) loss = F.mse_loss(x_q, x) commitment_loss = self.commitment_cost * F.mse_loss(x_q.detach(), x) loss = loss + commitment_loss return loss, x_q.detach(), encoding_indices # Example Usage: # Instantiate the loss function vql = VectorQuantizationLoss(num_embeddings=512, embedding_dim=64, commitment_cost=0.25) # Create a dummy input tensor (Batch size=2, Channels=64, Time steps=100) x = torch.randn(2, 64, 100, 1) # Calculate the loss and get the quantized output and indices loss, x_q, encoding_indices = vql(x) print(f"Loss: {loss.item()}") print(f"Quantized output shape: {x_q.shape}") print(f"Encoding indices shape: {encoding_indices.shape}") ``` -------------------------------- ### Example Usage of VectorQuantizationLoss Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb This snippet demonstrates how to instantiate and use the VectorQuantizationLoss class. It shows the creation of a loss object and its application with sample input data. The example assumes PyTorch is imported and available. ```python import torch import torch.nn as nn import torch.nn.functional as F # Assuming VectorQuantizationLoss class is defined above # Example parameters C = 128 # Number of channels K = {2: 16, 4: 16} # Dictionary mapping num_groups to num_vars_per_group # Instantiate the loss function vqloss = VectorQuantizationLoss(C, K) # Create dummy input data B, T = 8, 100 # Batch size, Time steps x = torch.randn(B, T, C) # Calculate the loss loss = vqloss(x, num_groups=2, num_vars_per_group=16) print(f"Loss for num_groups=2: {loss.item()}") loss = vqloss(x, num_groups=4, num_vars_per_group=16) print(f"Loss for num_groups=4: {loss.item()}") ``` -------------------------------- ### Start Training Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_02_vad_sed/homework.ipynb Initiates the training process by calling the `train` function with the initialized model and optimizer. The returned `train_state` object holds all training history. ```python train_state = train(model, opt) ``` -------------------------------- ### Clone Repository and Install Requirements (Colab) Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_08_tts_am_vocoders/notebooks/homework.ipynb Installs project dependencies and clones the repository if running in a Google Colab environment. ```python # If running in colab # clone the repository: # git clone https://github.com/yandexdataschool/speech_course.git # !pip install -r speech_course/week_08_tts_am_vocoders/requirements.txt ``` -------------------------------- ### RNNTransducer Instantiation and Forward Pass Example Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_04_asr/homework.ipynb Demonstrates how to instantiate the RNNTransducer model and perform a forward pass with sample data. Includes assertions to verify output shapes. ```python transducer = RNNTransducer( num_classes=len(tokenizer.char_map), input_dim=80, num_encoder_layers=4, num_decoder_layers=1, encoder_hidden_state_dim=320, decoder_hidden_state_dim=512, output_dim=512, encoder_is_bidirectional=True, encoder_dropout_p=0.2, decoder_dropout_p=0.2 ) spectrograms, labels, input_lengths, label_lengths = get_pseudo_batch() result = transducer.forward(spectrograms, input_lengths, labels, label_lengths) assert spectrograms.shape == torch.Size([2, 835, 80]) assert labels.shape == torch.Size([2, 158]) assert result.shape == torch.Size([2, 835, 159, 30]) ``` -------------------------------- ### Setup Environment Script Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_08_tts_am_vocoders/README.md Execute this script from the week_08_tts_am_vocoders folder to set up the FastPitch Conda environment with necessary libraries. ```bash chmod +x setup_env.sh && ./setup_env.sh ``` -------------------------------- ### Create Dictionary of Example Audio Files Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_01_DSP/seminar.ipynb Loads all WAV files from a specified directory into a dictionary, mapping filenames (without extension) to their audio data. It asserts that all loaded audio files match the expected sample rate. ```python def create_examples_dict(directory="../data/01_DSP/examples", sample_rate=sample_rate): directory = Path(directory) examples = {} for wav_path in directory.iterdir(): if wav_path.suffix == ".wav": wav, sr = librosa.load(str(wav_path)) assert sr == sample_rate, f"Working only with audio of sample_rate {sample_rate}" examples[wav_path.stem] = wav return examples examples = create_examples_dict(directory=samples_path) wav = examples["babenko"] ``` -------------------------------- ### Install Dependencies for Colab Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_08_tts_am_vocoders/notebooks/seminar.ipynb Installs necessary Python libraries for audio processing and F0 estimation if running in a Google Colab environment. ```python # If running in colab # !pip install praat-parselmouth soundfile librosa ``` -------------------------------- ### Initialize Model and Optimizer, Start Training Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_03_kws_bio/homework.ipynb Instantiates the CNN model, defines the Adam optimizer, and initiates the training process. Ensure `FEATS`, `DEVICE`, and `trainset.speakers()` are correctly set. ```python model = Model(FEATS, trainset.speakers(), 128).to(DEVICE) opt = optim.Adam(model.parameters()) train(model, opt) ``` -------------------------------- ### EncoderRNNT Initialization and Forward Pass Example Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_04_asr/homework.ipynb Demonstrates the instantiation of the EncoderRNNT class and its forward pass with sample data. Includes assertions to verify output shapes. ```python encoder = EncoderRNNT( input_dim=80, hidden_size=320, output_dim=512, n_layers=4, dropout=0.2, bidirectional=True ) spectrograms, labels, input_lengths, label_lengths = get_pseudo_batch() logits, hidden_states = encoder.forward(spectrograms, input_lengths) assert spectrograms.shape == torch.Size([2, 835, 80]) assert logits.shape == torch.Size([2, 835, 512]) assert len(hidden_states) == 2 assert hidden_states[0].shape == torch.Size([8, 2, 320]) ``` -------------------------------- ### Display audio reconstructions from a model Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/homework/homework.ipynb Prepares a model for inference, samples validation audio examples, and moves them to the appropriate device. This function is intended to be completed by the user to display reconstructions. ```python def display_audio_reconstructions(model, datamodule, num_examples: int = 2, offset: int = 42): model.prepare_for_inference().to(device) sample_audios, example_ids = datamodule.sample_val_examples(num_examples=num_examples, offset=offset) sample_audios = sample_audios.to(device) # ``` -------------------------------- ### ISTFT Inverse Check Setup Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/seminar.ipynb Sets up parameters for checking if the ISTFT implementation is the inverse of the STFT implementation. ```python padding_mode = "scipy" ``` -------------------------------- ### Initialize VectorQuantizationLoss Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb Initializes the VectorQuantizationLoss class. This setup is necessary before using the loss function in a model. ```python class VectorQuantizationLoss(nn.Module): def __init__(self, dim, codebook_size, decay=0.99, epsilon=1e-5): super().__init__() self.dim = dim self.codebook_size = codebook_size self.decay = decay self.epsilon = epsilon self.codebook = nn.Parameter(torch.randn(codebook_size, dim)) def forward(self, x): # x is of shape (batch_size, dim, num_tokens) # x needs to be reshaped to (batch_size * num_tokens, dim) x = x.permute(0, 2, 1) x = x.contiguous().view(-1, self.dim) # When training the codebook if self.training == True: # distance between x and the codebook distances = (x[:, None, :] - self.codebook[None, :, :]).pow(2).sum(dim=2) # find the closest codebook entry, for each embedding encoding_indices = torch.argmin(distances, dim=1) encoding = torch.zeros(encoding_indices.size(0) * self.codebook_size, self.dim, device=x.device) encoding.scatter_(0, encoding_indices.unsqueeze(1).repeat(1, self.dim), x) # use the cluster centers to update the codebook using exponential moving average self.update_codebook(x, encoding_indices) # put x back into the shape of (batch_size, num_tokens, dim) x = encoding.view(x.size(0) // self.codebook_size, self.codebook_size, self.dim) x = x.permute(0, 2, 1).contiguous() return x else: # when using the codebook for inference encoding_indices = torch.argmin(distances, dim=1) encoding = torch.zeros(encoding_indices.size(0) * self.codebook_size, self.dim, device=x.device) encoding.scatter_(0, encoding_indices.unsqueeze(1).repeat(1, self.dim), x) # put x back into the shape of (batch_size, num_tokens, dim) x = encoding.view(x.size(0) // self.codebook_size, self.codebook_size, self.dim) x = x.permute(0, 2, 1).contiguous() return x def update_codebook(self, x, encoding_indices): # calculate the number of occurrences for each codebook entry with torch.no_grad(): encoding_indices = encoding_indices.unsqueeze(1) ào = torch.zeros(self.codebook_size, x.size(1), device=x.device) ào.scatter_add_(0, encoding_indices, x) n = torch.zeros(self.codebook_size, 1, device=x.device) n.scatter_add_(0, encoding_indices, torch.ones_like(encoding_indices, dtype=torch.float)) # update the codebook using exponential moving average self.codebook.data.mul_(self.decay).add_(ào, alpha=1 - self.decay) self.codebook.data.div_(n.clamp(min=1e-5)) ``` -------------------------------- ### Code Generation Example Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb This snippet demonstrates how to generate code using a specific function. It's useful for creating code dynamically or for testing purposes. ```python def generate_code(template, data): # Simple templating function for key, value in data.items(): template = template.replace(f'{{{{{key}}}}}', str(value)) return template code_template = "def {function_name}(x): return x * {multiplier}" data = {"function_name": "multiply_by_two", "multiplier": 2} generated_code = generate_code(code_template, data) print(generated_code) ``` -------------------------------- ### Import Core Libraries Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb Import necessary libraries for PyTorch, PyTorch Lightning, and data handling. This setup is crucial for organizing the training loop, logging, and checkpointing. ```python from pathlib import Path import lightning as L import torch from lightning.pytorch.callbacks import EarlyStopping, ModelCheckpoint %matplotlib inline ``` -------------------------------- ### Download and Unzip Audio Data Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_01_DSP/seminar.ipynb Downloads a zip archive containing example audio files and then unzips it. This prepares the necessary data for the DSP seminar exercises. Ensure the target directory exists. ```bash # !mkdir -p ../data # link_to_archive = "https://disk.yandex.ru/d/uV1jDE4Ku5KMuw" # download_file(link_to_archive, "../data/01_DSP.zip") # !unzip ../data/01_DSP.zip -d ../data # !rm ../data/01_DSP.zip ``` -------------------------------- ### Start FastPitch Training Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_08_tts_am_vocoders/notebooks/homework.ipynb Initiates the FastPitch training process using a Python script. Requires specifying paths for logs, checkpoints, dataset, and a Hugging Face checkpoint. ```python sp.check_call( ' '.join([ f'PYTHONPATH={path_to_sources} CUDA_VISIBLE_DEVICES={gpu_avaiable}', f'python3 -m sources.fastpitch.train_fastpitch', f'--logs {logs_dir}', f'--ckptdir {ckpt_dir}', f'--dataset {path_to_dataset}', f'--hfg {path_to_hfg_ckpt}' ]), shell=True ) ``` -------------------------------- ### Data Manipulation Example Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb This snippet illustrates a common data manipulation task. It shows how to process and transform data, which is essential for many machine learning pipelines. ```python import numpy as np def process_data(data): # Example: Normalize data to have zero mean and unit variance mean = np.mean(data) std = np.std(data) normalized_data = (data - mean) / std return normalized_data raw_data = np.array([1.0, 2.0, 3.0, 4.0, 5.0]) processed_data = process_data(raw_data) print(processed_data) ``` -------------------------------- ### Initialize Codec Model Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_10_tts_transformers/homework.ipynb Set up the CodecApplier, which handles the conversion between raw audio waveforms and their compressed codec representations. It requires paths to the codec's checkpoint and configuration file. ```python codec_model = CodecApplier( config_path=codec_config_path, ckpt_path=codec_model_path, sample_rate=16000, device=device, ) ``` -------------------------------- ### Mel Spectrogram Transformation Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_01_DSP/seminar.ipynb Converts raw audio examples to Mel spectrograms using Wav2Spectrogram and Mel transformations. Requires 'examples' dictionary to be pre-populated. ```python wav_to_spec = Wav2Spectrogram(window_size=1024, hop_length=256, n_freqs=None) spec_to_mel = Mel(n_fft=1024, n_mels=80, sample_rate=22050) spec = wav_to_spec(examples["diesel"]) mel = spec_to_mel(spec) ``` -------------------------------- ### Instantiate and Use RIR Sampler Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/homework.ipynb Demonstrates how to instantiate the RIR sampler with a list of RIR paths and then use it to sample multiple RIRs, asserting their properties. ```python sampler = RirSampler(list_wavs_in_folder_recursively(DATA_PATHS["rir"])) n_samples = 1000 for idx in enumerate(tqdm(range(n_samples))): rir = sampler() assert rir.ndim == 1, rir.shape print("Ok") ``` -------------------------------- ### Complex Spectrum Masking Model Usage Example Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/homework.ipynb Example of building and using the ComplexSpectumMaskingModel. It generates random waveform data and passes it through the model, asserting that the output shape matches the input shape. ```python model = build_model() x = torch.rand(3, 48_000) out = model(x) assert x.shape == out.shape ``` -------------------------------- ### Configure Training Environment Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_03_kws_bio/homework.ipynb Sets up device, data directory, feature dimension, and loader workers for the training process. Ensure the DATADIR points to your dataset. ```python DEVICE = 'mps' # "cpu" for cpu, also you can use "cuda" for gpu and "mps" for apple silicon DATADIR = 'data' FEATS = 80 LOADER_WORKERS = 8 ``` -------------------------------- ### Download and Prepare Lingware Data Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_10_tts_transformers/homework.ipynb Uncomment and run these lines to download the lingware archive, extract its contents, remove the archive, and organize the files into the appropriate data directory. ```python ### To download the file uncomment the following line # link_to_archive = "https://disk.yandex.ru/d/XvDaWCWch6hWTw" # download_file(link_to_archive) # !unzip lingware.zip # !rm lingware.zip # !mkdir -p ../data/09_tts_transformers # !mv lingware ../data/09_tts_transformers ``` -------------------------------- ### Example Score Calculation Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_02_vad_sed/seminar.ipynb Demonstrates the usage of the `score` function with sample probability predictions and alignment targets. ```python score([np.array([0.1, 0.2])], [np.array([0, 1])], thr=0.5) ``` -------------------------------- ### Initialize Model and Trainer, then Fit Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb Instantiate the MNISTEncoderDecoder model and the configured trainer. Then, initiate the model training process using the provided datamodule. ```python trainer = get_trainer() model = MNISTEncoderDecoder(quantizer=None, vq_loss=None) trainer.fit(model=model, datamodule=datamodule) ``` -------------------------------- ### Create a Lightning Trainer instance Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/homework/homework.ipynb Configures and returns a PyTorch Lightning Trainer with specified callbacks (EarlyStopping, ModelCheckpoint), device settings, and training parameters like max epochs and gradient clipping. ```python def make_trainer(name: str, max_epochs: int = 30): run_dir = data_path / "runs" / name run_dir.mkdir(parents=True, exist_ok=True) return L.Trainer( callbacks=[ EarlyStopping(monitor="val/loss", patience=5), ModelCheckpoint(dirpath=run_dir / "ckpt", save_top_k=1, monitor="val/loss"), ], devices=[device_id] if device_id >= 0 else "auto", accelerator="gpu" if device_id >= 0 else "cpu", max_epochs=max_epochs, log_every_n_steps=10, num_sanity_val_steps=0, gradient_clip_val=1.0, gradient_clip_algorithm="norm", default_root_dir=str(run_dir), ) ``` -------------------------------- ### Plotting Levenshtein Matrix Example Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_04_asr/seminar.ipynb Visualizes the Levenshtein distance matrix, original and modified sequences, and the optimal path with modifications. ```python plot_matrix([ [0, 1, 2, 3, 4], [1, 0, 1, 2, 3], [2, 1, 0, 1, 2], [3, 2, 1, 1, 1] ], 'cat', 'cast', [(0, 0), (1, 1), (2, 2), (2, 3), (3, 4)], ['same', 'same', 'same', 'insert', 'same']) ``` -------------------------------- ### Example Matrix Plotting Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_04_asr/seminar.ipynb Demonstrates the usage of the `plot_matrix` function with a predefined Levenshtein distance matrix and corresponding row/column names. ```python plot_matrix([ [0, 1, 2, 3, 4], [1, 0, 1, 2, 3], [2, 1, 0, 1, 2], [3, 2, 1, 1, 1] ], 'cat', 'cast') ``` -------------------------------- ### Get a Batch of Training Data Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_08_tts_am_vocoders/notebooks/homework.ipynb Creates an iterator for the training data loader and retrieves the next batch of training samples. ```python train_iter = iter(train_loader) batch = next(train_iter) ``` -------------------------------- ### Download and Extract Dataset Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/seminar.ipynb Downloads a dataset from a Yandex Disk public link and extracts it to a local directory. This is used for audio processing examples. ```python from io import BytesIO import os import requests from urllib.parse import urlencode from zipfile import ZipFile base_url = 'https://cloud-api.yandex.net/v1/disk/public/resources/download?' public_key = 'https://disk.yandex.ru/d/Uj7mEJiHxeF6ag' final_url = base_url + urlencode(dict(public_key=public_key)) response = requests.get(final_url) download_url = response.json()['href'] response = requests.get(download_url) path_to_dataset = 'data' # Choose any appropriate local path zipfile = ZipFile(BytesIO(response.content)) zipfile.extractall(path=path_to_dataset) ``` -------------------------------- ### Plot Waveform and Mel Spectrogram Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_01_DSP/seminar.ipynb Generates visualizations for both the raw audio waveform and its corresponding Mel spectrogram. Requires the 'examples' dictionary to be pre-populated. ```python plot_wav_and_mel(examples) ``` -------------------------------- ### Configure Training Environment Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/homework.ipynb Sets up constants for training, including data loading workers, learning rate, batch size, and gradient clipping. It also determines the appropriate device (CPU or GPU) for computation. ```python NUM_WORKERS = 2 # parallel data generation LR = 2e-4 BATCH_SIZE = 12 # the bigger the better MAX_GRAD_NORM = 4 # for clipping if torch.cuda.is_available(): DEVICE = torch.device("cuda") PIN_MEMORY = True else: DEVICE = torch.device("cpu") PIN_MEMORY = False DEVICE ``` -------------------------------- ### Define Data and Model Paths Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_10_tts_transformers/homework.ipynb Set up the file paths for lingware components, codec models, phonemizer, and the dataset. Ensures that the project can locate all necessary resources. ```python ### Paths lingware_folder = Path("../data/09_tts_transformers/lingware") ckpt_path = lingware_folder / "ckpt" codec_model_path = lingware_folder / "codec" / "ckpt" codec_config_path = lingware_folder / "codec" / "config.json" phonemizer_path = lingware_folder / "phonemizer_en_us.pt" dataset_url = "dev-clean" data_path = Path("../data/09_tts_transformers") data_path.mkdir(exist_ok=True) ``` -------------------------------- ### Initialize and Train Model Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_03_kws_bio/seminar.ipynb Initializes the biometric classification model with the correct input and output dimensions and then starts the training process for a specified number of epochs. ```python model = Model(IN_DIM, dataset.speakers()).to(DEVICE) train(model, epochs=50) ``` -------------------------------- ### Initialize Audio Metrics Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/homework.ipynb Initializes a ModuleDict containing SignalNoiseRatio and ScaleInvariantSignalNoiseRatio metrics, moving them to the appropriate computation device. ```python from torchmetrics.audio import SignalNoiseRatio, ScaleInvariantSignalNoiseRatio metrics = torch.nn.ModuleDict( { "SNR": SignalNoiseRatio(), "SI-SNR": ScaleInvariantSignalNoiseRatio(), } ).to(DEVICE) ``` -------------------------------- ### Initialize CTC Forward Algorithm Utilities Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_04_asr/seminar.ipynb Initializes constants and the tokenizer for the CTC forward algorithm. Ensure utils are correctly imported and configured. ```python NEG_INF = utils.NEG_INF BLANK_SYMBOL = utils.BLANK_SYMBOL tokenizer = utils.CTCTokenizer() ``` -------------------------------- ### Initialize experiment settings and data module Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/homework/homework.ipynb Sets the random seed for reproducibility, defines the device (GPU or CPU), creates a data directory, and initializes the LibriSpeech data module with a specified batch size. ```python seed = 42 L.seed_everything(seed, workers=True) device_id = 0 device = torch.device(f"cuda:{device_id}" if device_id >= 0 else "cpu") data_path = Path("./data") data_path.mkdir(exist_ok=True) batch_size = 128 datamodule = LibriSpeechDataModule(data_dir=str(data_path), batch_size=batch_size) ``` -------------------------------- ### Instantiate and Use MultiResLoss Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/homework.ipynb Demonstrates how to create an instance of the `MultiResLoss` class and apply it to random estimated and target waveforms. This is useful for testing the loss function's behavior and shape. ```python criterion = MultiResLoss() est = torch.rand(3, 48_000) target = torch.rand(3, 48_000) criterion(est, target) ``` -------------------------------- ### Initialize Model, Optimizer, and Loss Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/homework.ipynb Builds the model, defines the Adam optimizer with a specified learning rate, and initializes the MultiResLoss criterion, all moved to the designated computation device. ```python model = build_model().to(DEVICE) optimizer = torch.optim.Adam(model.parameters(), lr=LR) criterion = MultiResLoss().to(DEVICE) ``` -------------------------------- ### Download and Extract Dataset Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_02_vad_sed/seminar.ipynb Handles the downloading and extraction of the VAD dataset from a Yandex Disk public link. It includes logic for both Datasphere environments and local setups. ```python import requests from urllib.parse import urlencode from io import BytesIO from tarfile import TarFile import tarfile base_url = 'https://cloud-api.yandex.net/v1/disk/public/resources/download?' public_key = 'https://disk.yandex.ru/d/LJlkc_WMo-libA' dst_path = '/home/jupyter/mnt/datasets/voice_activity_detection/' # if we make the Datasphere datasets work dst_path = './dataset_seminar/' final_url = base_url + urlencode(dict(public_key=public_key)) response = requests.get(final_url) download_url = response.json()['href'] # if you aren't in the Datasphere # !wget -O data_sem.tar.gz "{download_url}" # !tar -xf data_sem.tar.gz # otherwise # response = requests.get(download_url) # io_bytes = BytesIO(response.content) # tar = tarfile.open(fileobj=io_bytes, mode='r:gz') # tar.extractall(path=dst_path) ``` -------------------------------- ### Download and Extract Dataset Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/homework.ipynb Downloads a dataset from a Yandex Disk public link and extracts its contents to a specified local path. Ensure the 'requests' and 'zipfile' libraries are installed. ```python from io import BytesIO import os import requests from urllib.parse import urlencode from zipfile import ZipFile base_url = 'https://cloud-api.yandex.net/v1/disk/public/resources/download?' public_key = 'https://disk.yandex.ru/d/ECHrgBGJrrGQqw' final_url = base_url + urlencode(dict(public_key=public_key)) response = requests.get(final_url) download_url = response.json()['href'] response = requests.get(download_url) path_to_dataset = 'data' # Choose any appropriate local path zipfile = ZipFile(BytesIO(response.content)) zipfile.extractall(path=path_to_dataset) ``` -------------------------------- ### Define Dataset Configuration Constants Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_02_vad_sed/seminar.ipynb Sets up constants for dataset directory paths, sampling frequency, frame size, training split, and the default device for computations. ```python DATADIR = dst_path + '/data' WAVDIR = os.path.join(DATADIR, 'wav') ALIGNDIR = os.path.join(DATADIR, 'align') FS = 16000 FRAME = 30 * FS // 1000 TRAIN = 350 DEVICE = 'cpu' ``` -------------------------------- ### Train ECAPA Model Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_03_kws_bio/homework.ipynb Initial training of the ECAPA TDNN model. This setup uses Adam optimizer and a specified batch size. Aim for an EER around 0.08. ```python model = EcapaTDNN(FEATS, trainset.speakers(), 128).to(DEVICE) opt = optim.Adam(model.parameters()) train(model, opt, batch_size=128) ``` -------------------------------- ### Download Utility Files Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_01_DSP/seminar.ipynb Downloads essential Python scripts for plotting, transformations, and utilities used in the seminar. These files provide helper functions for the exercises. ```bash # !wget https://raw.githubusercontent.com/yandexdataschool/speech_course/refs/heads/main/week_01_DSP/plotting_utils.py # !wget https://raw.githubusercontent.com/yandexdataschool/speech_course/refs/heads/main/week_01_DSP/tests.py # !wget https://raw.githubusercontent.com/yandexdataschool/speech_course/refs/heads/main/week_01_DSP/transforms.py # !wget https://raw.githubusercontent.com/yandexdataschool/speech_course/refs/heads/main/week_01_DSP/utils.py ``` -------------------------------- ### Import necessary libraries Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_08_tts_am_vocoders/notebooks/homework.ipynb Imports core components for FastPitch model, data handling, and HiFi-GAN model loading. ```python from sources.fastpitch.common.checkpointer import Checkpointer from sources.fastpitch.model import FastPitch from sources.fastpitch.data import FastPitchBatch, SymbolsSet from sources.hifigan.model import load_model as load_hfg_model ``` -------------------------------- ### Instantiate Dataset Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_03_kws_bio/seminar.ipynb Creates an instance of the custom Dataset, applying the MFCC transform to extract features from audio files. ```python dataset = Dataset(torchaudio.transforms.MFCC(n_mfcc=40)) ``` -------------------------------- ### Apply Windowing to Full Audio and Visualize Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_01_DSP/seminar.ipynb Applies the Windowing transformation to the entire audio waveform and visualizes the resulting windowed segments. ```python windowing_transform = Windowing(window_size=2048, hop_length=512) windows = windowing_transform(wav) plot_windowed_wav(windows) ``` -------------------------------- ### Initialize Streaming STFT and ISTFT Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/seminar.ipynb Sets up streaming Short-Time Fourier Transform (STFT) and Inverse STFT (ISTFT) objects for real-time audio processing. Parameters like N_FFT, window, hop_size, and padding_mode can be adjusted. ```python N_FFT = 512 win_size = 320 hop_size = 160 # N_FFT = 2048 # win_size = 2048 # hop_size = 512 window = np.hanning(win_size) padding_mode = "scipy" stft = StreamingStft(N_FFT, window, hop_size, padding_mode=padding_mode, left_padding=True) istft = StreamingIStft(N_FFT, window, hop_size, padding_mode=padding_mode) ``` -------------------------------- ### Download Required Files Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_10_tts_transformers/homework.ipynb Use these commands to download essential Python scripts for the project. Ensure you have the necessary permissions and network access. ```python # !wget https://raw.githubusercontent.com/yandexdataschool/speech_course/main/week_09_tts_tranformer/data.py # !wget https://raw.githubusercontent.com/yandexdataschool/speech_course/main/week_09_tts_tranformer/model.py ``` -------------------------------- ### Define Progressive Decoding Visualization Function Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/homework/homework.ipynb Defines a function to visualize progressive decoding examples. It prepares the model, samples validation audios, and includes a placeholder for the core decoding logic. ```python def display_progressive_decoding_examples(model, datamodule, n_layers_list=(1, 2, 4, 8), num_examples: int = 2, offset: int = 17): model.prepare_for_inference().to(device) sample_audios, example_ids = datamodule.sample_val_examples(num_examples=num_examples, offset=offset) sample_audios = sample_audios.to(device) # ``` -------------------------------- ### Visualize Hann Window Application Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_01_DSP/seminar.ipynb Demonstrates the effect of applying the Hann window to sample audio data segments. ```python start1, start2, length = 4500, 10450, 2048 demo_tensor = get_demo_tensor([wav[start1:start1 + length], wav[start2:start2 + length]]) plot_hann(demo_tensor) ``` -------------------------------- ### Apply MelSpectrogram and CNN for Multiframe Classification Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_02_vad_sed/seminar.ipynb Uses MelSpectrogram as the audio transform and a simple Convolutional Neural Network (CNN) for multiframe classification. This setup is for a more sophisticated VAD model that considers temporal context. ```python multiframe_classification( torchaudio.transforms.MelSpectrogram(n_fft=FRAME, hop_length=FRAME, n_mels=23), nn.Sequential(nn.Conv1d(23, 23, 5, padding=2), nn.ReLU(), nn.Conv1d(23, 1, 1), nn.Sigmoid()) ) ``` -------------------------------- ### Download and Unzip Hi-Fi GAN Checkpoint Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_08_tts_am_vocoders/notebooks/homework.ipynb Downloads a pretrained Hi-Fi GAN checkpoint necessary for audio generation from mel-spectrograms and unzips it. The downloaded zip file is then removed. ```python !wget --content-disposition https://api.ngc.nvidia.com/v2/models/nvidia/dle/hifigan__pyt_ckpt_ds-ljs22khz/versions/21.08.0_amp/zip -O hifigan_ckpt.zip !unzip hifigan_ckpt.zip !rm hifigan_ckpt.zip ``` -------------------------------- ### Efficient Audio Chunk Reading with `sf.read` Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_12_vqe_noise_reduction/homework.ipynb Compares naive audio slicing with efficient reading using `sf.read`'s `start` and `stop` arguments. Use this method to read specific segments of audio files without loading the entire file into memory. ```python rel_path = "read_speech/book_00000_chp_0009_reader_06709_11_seg_0.wav" path = os.path.join(DATA_PATHS["speech"], rel_path) x, _sr = sf.read(path) print(f"total audio file duration: {len(x)} frames or {len(x) / SR} seconds") crop_size_sec = 1 crop_size_frames = int(crop_size_sec * SR) start = 16_000 stop = start + crop_size_frames ``` ```python %%timeit x = sf.read(path)[0][start: stop] ``` ```python %%timeit x, _sr = sf.read(path, start=start, stop=stop) ``` -------------------------------- ### Import Core Libraries for Transformers Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_10_tts_transformers/homework.ipynb Import necessary PyTorch and torchaudio modules, along with IPython for displaying audio output. These are fundamental for working with transformer models and audio data. ```python from pathlib import Path import torch import torch.nn.functional as F import torchaudio from IPython.display import Audio, display ``` -------------------------------- ### Vector Quantization Loss Initialization Source: https://github.com/open-education-club-by-yandex/speech-processing-shad.git/blob/main/week_09_codecs/seminar.ipynb Initializes the VectorQuantizationLoss module, setting up the codebook with specified dimensions and device. ```python def __init__(self, n_channels, n_codes, device): super().__init__() self.n_channels = n_channels self.n_codes = n_codes self.device = device self.codebook = nn.Embedding(n_codes, n_channels, device=device) self.codebook.weight.data.uniform_(-1 / (2 * n_codes), 1 / (2 * n_codes)) ```