<jupyter_start><jupyter_text>Demo Notebook 2  Training Speech Commands Classifier with [wavencoder](https://pypi.org/project/wavencoder/) and PyTorch---Author : <NAME>![Twitter Follow](https://img.shields.io/twitter/follow/shangethr?style=social)[GitHub](https://github.com/shangeth) [LinkedIn](https://www.linkedin.com/in/shangeth/)  Installing wavencoder<jupyter_code>!pip install wavencoder

# install torchaudio for audio dataset
!pip install torchaudio
# import wavencoder models and trainer
from wavencoder.models import Wav2Vec, LSTM_Attn_Classifier
from wavencoder.trainer import train, test_evaluate_classifier, test_predict_classifier

# import torch modules and torchaudio for data
import torchaudio
import torch
import torch.nn as nn
from torch.utils.data import DataLoader, random_split, Dataset

import random
import IPython
from tqdm import tqdm
import numpy as np
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Download Speech Commands Dataset with TorchAudio<jupyter_code># download dataset with torchaudio.datasets
dataset = torchaudio.datasets.SPEECHCOMMANDS('./', download=True, url= 'speech_commands_v0.02')
idx = random.randint(0,len(dataset))
dataset[idx]
sample_audio = dataset[idx][0].numpy().reshape(-1)
sample_rate = dataset[idx][1]
sample_label = dataset[idx][2]

plt.title(f"Audio of Label = {sample_label}")
plt.plot(sample_audio)
plt.show()

IPython.display.Audio(data=sample_audio, rate=16000)
# get a dictionary of label-index

labels = []
for i in tqdm(range(len(dataset))):
    labels.append(dataset[i][2])
    labels = list(set(labels))
labels_dict = {k: v for v, k in enumerate(labels)}
labels_count_list = {k: 0 for  k in labels}
labels_dict
class_num = len(labels)
class_num
# visualize the count of no of wav file for each class

for i in tqdm(range(len(dataset))):
    label = dataset[i][2]
    labels_count_list[label] += 1

plt.figure(figsize=(30,10))
plt.title('Distribution of data')
plt.bar(labels_count_list.keys(), labels_count_list.values())
plt.show()<jupyter_output>100%|██████████| 105829/105829 [00:12<00:00, 8282.60it/s]<jupyter_text>PyTorch Custom Dataset<jupyter_code>class SpeechDataset(Dataset):
    def __init__(self, dataset, label_dict, transforms=None):
        self.dataset = dataset
        self.label_dict = label_dict
        self.transforms = transforms
            
    def __len__(self):
        return len(self.dataset)    
    
    def pad_crop_wave(self, waveform, l):
        l_wave = waveform.shape[1]
        if l_wave > l:
            i=0
            waveform = waveform[:, i:i+l]
        elif l_wave < l:
            delta = l-l_wave
            if delta > l_wave:
                waveform = nn.ConstantPad1d((0, delta), 0)(waveform)
            else:
                waveform_new = torch.randn(waveform.shape[0], l)
                waveform_new[:, :l_wave] = waveform
                i=0
                waveform_new[:, l_wave:] = waveform[:, i:i+delta]
                waveform = waveform_new
        else:
            waveform = waveform
        return waveform

    def __getitem__(self,idx):
        waveform = self.dataset[idx][0]
        waveform = self.pad_crop_wave(waveform, l=16000)
        if self.transforms:
            waveform = self.transforms(waveform)
        label = self.dataset[idx][2]
        label_idx = self.label_dict[label]
        return waveform.squeeze(0), label_idx
audio_dataset = SpeechDataset(dataset, labels_dict)
audio_dataset[0][0].shape
train_len = int(len(audio_dataset) * 0.8)
val_len = len(audio_dataset) - train_len

train_ds, val_ds = random_split(audio_dataset, [train_len, val_len])
len(train_ds), len(val_ds)<jupyter_output><empty_output><jupyter_text>Dataloader<jupyter_code>trainloader = torch.utils.data.DataLoader(train_ds, batch_size=512, shuffle=True)
valloader = torch.utils.data.DataLoader(val_ds, batch_size=256, shuffle=False)
batch = next(iter(valloader))
x, y = batch

x.shape, y.shape<jupyter_output><empty_output><jupyter_text>Wavencoder<jupyter_code>def count_parameters(model):
    return sum(p.numel() for p in model.parameters() if p.requires_grad)
model = nn.Sequential(
    Wav2Vec(pretrained=True),
    LSTM_Attn_Classifier(512, 128, class_num)
)
print(model)
print('\n# Model Parameters = ', count_parameters(model))

for param in model[0].feature_extractor.parameters():
    param.requires_grad = False

for param in model[0].feature_extractor.conv_layers[6].parameters():
    param.requires_grad = True

print('# Model Parameters after freezing = ', count_parameters(model))
x = torch.randn(1, 16000) 
y_hat = model(x) 

print(y_hat.shape)
optimizer = torch.optim.RMSprop(model.parameters(), lr=1e-3)
scheduler = torch.optim.lr_scheduler.CyclicLR(optimizer, base_lr=1e-4, max_lr=1e-1, step_size_up=10)
model, train_dict = train(model, trainloader, valloader, n_epochs=50, optimizer=optimizer, scheduler=scheduler)
train_losses = train_dict['train_losses']
train_accuracies = train_dict['train_accuracies']
val_losses = train_dict['val_losses']
val_accuracies = train_dict['val_Accuracies']

plt.figure(figsize=(20, 10))
plt.subplot(1, 2, 1)
plt.plot(train_losses, label="Train Loss")
plt.plot(val_losses, label="Val Loss")
plt.xlabel('Epochs')
plt.ylabel('Loss')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(train_accuracies, label="Train Accuracy")
plt.plot(val_accuracies, label="Val Accuracy")
plt.xlabel('Epochs')
plt.ylabel('Accuracy')

plt.legend()
plt.show()
test_evaluate_classifier(model, valloader)<jupyter_output>100%|██████████| 83/83 [00:22<00:00,  3.61it/s]
{'fraction_non_alphanumeric': 0.1260595130748422, 'fraction_numerical': 0.023624887285843103, 'mean_word_length': 4.699897225077081, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 22, 'https://': 4, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '247919', 'license': 'MIT', 'n_tokens_mistral': 2067, 'n_tokens_neox': 1919, 'n_words': 382, 'path': 'examples/notebooks/wavencoder_demo_2.ipynb', 'repo_name': 'shangeth/wavencoder', 'size': '5553'}