Skip to content

Instantly share code, notes, and snippets.

View fauxneticien's full-sized avatar

Nay San fauxneticien

View GitHub Profile
@fauxneticien
fauxneticien / signal2int.py
Created October 19, 2023 04:33
A solution for Colab exercise
def signal2int(signal, sample_rate=16_000, seq_length=4):
"""
Decode a sine wave signal back into digits
"""
digits = []
for start_index in range(0, sample_rate * seq_length, sample_rate):
end_index = start_index + sample_rate - 1
window_samples = signal[start_index:end_index]
@fauxneticien
fauxneticien / reduce-list-of-funcs.py
Created October 2, 2023 15:50
Apply a list of functions to some input
from functools import reduce
input = 1
f1 = lambda i: i+1
f2 = lambda i: i+2
f3 = lambda i: i+3
output = reduce(lambda i, f: f(i), [f1, f2, f3], input)
@fauxneticien
fauxneticien / benchmark-fastconformer.py
Created September 21, 2023 02:40
benchmark-fastconformer.py
import torch
import torchaudio
fakewav_80mins = torch.rand(1, 16_000 * 60 * 80)
torchaudio.save("tmp/80mins.wav", fakewav_80mins, 16_000)
import nemo.collections.asr as nemo_asr
asr_model = nemo_asr.models.EncDecCTCModelBPE.from_pretrained(model_name="nvidia/stt_en_fastconformer_ctc_large")
asr_model.eval()
@fauxneticien
fauxneticien / librilight10h_tsv.py
Created September 17, 2023 02:09
Fetch LibriLightLimited 10h fine-tuning dataset as TSV file
import torchaudio
import pandas as pd
from pathlib import Path
from tqdm import tqdm
llight10h = torchaudio.datasets.LibriLightLimited(root="tmp/", subset="10h", download=True)
manifest_rows = []
@fauxneticien
fauxneticien / run-ddp.py
Last active September 8, 2023 19:57
Lhotse DDP test
import torch
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
import os
import torchaudio
from lhotse import CutSet, Fbank, FbankConfig
from lhotse.dataset import IterableDatasetWrapper, SpeechSynthesisDataset, DynamicBucketingSampler, OnTheFlyFeatures, make_worker_init_fn
from lhotse.recipes import download_librispeech, prepare_librispeech
from tqdm import tqdm
@fauxneticien
fauxneticien / import-hf-to-torchaudio.py
Created July 30, 2023 20:12
Import HF model to torchaudio
import torch
from transformers import Wav2Vec2Model
from torchaudio.models.wav2vec2.utils import import_huggingface_model
hf_model = Wav2Vec2Model.from_pretrained("facebook/wav2vec2-xls-r-300m")
ta_model = import_huggingface_model(hf_model)
torch.save(ta_model.state_dict(), "tmp/wav2vec2-300m_xls-r.pt")
## Usage
@fauxneticien
fauxneticien / lhotse-shar-outputs.txt
Created July 11, 2023 18:12
Tree output of Lhotse shar files
20230710_mini-librispeech/
├── dev
│ ├── cuts.000000.jsonl.gz
│ ├── cuts.000001.jsonl.gz
│ ├── cuts.000002.jsonl.gz
│ ├── cuts.000003.jsonl.gz
│ ├── cuts.000004.jsonl.gz
│ ├── cuts.000005.jsonl.gz
│ ├── fbank.000000.tar
│ ├── fbank.000001.tar
@fauxneticien
fauxneticien / hydra-demo.yaml
Last active June 21, 2023 22:26
Example class
my_instantiated_object:
_target_: my_classes.my_abstract_class.MyAbstractClass
my_variable: used Hydra YAML!
@fauxneticien
fauxneticien / lhotse-token-collator-for-ctc.py
Last active June 12, 2023 20:24
Lhotse token collator for CTC
# Modified version with diff (see history)
class TokenCollater:
"""Collate list of tokens
Map sentences to integers. Sentences are padded to equal length.
Beginning and end-of-sequence symbols can be added.
Call .inverse(tokens_batch, tokens_lens) to reconstruct batch as string sentences.
Example:
@fauxneticien
fauxneticien / backup.sh
Created April 9, 2023 15:36
Backup model checkpoints without cronjob
#!/usr/bin/bash
# Basic range in for loop
for value in {1..8}
do
echo "Starting backup $value"
rclone copy -P --exclude checkpoint_last.pt /home/nay/w2v2-fairseq-pretrain/outputs/2023-04-09/08-12-57/checkpoints/ gdrive_slasr:asr/checkpoints/xls-r_cpt_malay-140h/checkpoints/35-ga16/
echo "Sleeping for 1 hour"
sleep 1h
done