-
-
Save MittalShruti/735aa0f7902de27ca7dafeff4c88d6d1 to your computer and use it in GitHub Desktop.
ASR - ctc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"name": "ASR - ctc", | |
"provenance": [], | |
"collapsed_sections": [], | |
"machine_shape": "hm", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"accelerator": "GPU" | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/MittalShruti/735aa0f7902de27ca7dafeff4c88d6d1/debug-model2-build-stt-asr.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "vKcyWEZitWsw", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"class CTC(nn.Module):\n", | |
" def __init__(self, reduction='none', zero_infinity=True, **kwargs):\n", | |
" super().__init__()\n", | |
" self.ctc = torch.nn.CTCLoss(reduction=reduction, zero_infinity=zero_infinity, **kwargs)\n", | |
" \n", | |
" def forward(self, log_probs, targets, input_lengths, target_lengths):\n", | |
" out_len = log_probs.shape[-1]\n", | |
" input_lenghts = input_lengths * out_len\n", | |
" input_lengths = input_lenghts.int().cuda()\n", | |
" target_lengths = target_lengths.int().cuda()\n", | |
" targets = targets.int().cuda()\n", | |
" log_probs = log_probs.permute(2,0,1).cuda()\n", | |
" \n", | |
" ctc_val = self.ctc(log_probs, targets, input_lengths, target_lengths)\n", | |
"\n", | |
" return ctc_val" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "DzrgTEIvw-Ph", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"def asr_learner(data:DataBunch, loss_func, number_of_classes, window_size=255,**kwargs)->Learner:\n", | |
" model = DeepSpeech(num_classes=number_of_classes, window_size=window_size)\n", | |
"\n", | |
" # , init=nn.init.kaiming_normal_\n", | |
" apply_init(model, nn.init.kaiming_normal_)\n", | |
" \n", | |
" w = WerMetric(EPSTOK, hi_vocab); w.__name__ = \"wer\"\n", | |
" c = CerMetric(EPSTOK, hi_vocab); c.__name__ = \"cer\"\n", | |
" metrics = [w, c]\n", | |
" \n", | |
" x, y = db.one_batch()\n", | |
" pred = model(x)\n", | |
" \n", | |
" learn = Learner(data, model, loss_func(pred, *y), callback_fns=ShowGraph, metrics=metrics, **kwargs)\n", | |
" \n", | |
" return learn" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
}, | |
{ | |
"cell_type": "code", | |
"metadata": { | |
"id": "AKHE9oEEyl2l", | |
"colab_type": "code", | |
"colab": {} | |
}, | |
"source": [ | |
"\n", | |
"blank_id = hi_vocab.numericalize(EPSTOK)[0]\n", | |
"num_saidas = len(list(hi_lbls))\n", | |
"\n", | |
"loss_func = CTC(blank=blank_id, post_reduction=lambda loss: torch.sum(loss))\n", | |
"\n", | |
"learn = asr_learner(db, loss_func, num_saidas)\n", | |
"learn.opt_func = optim.Adam" | |
], | |
"execution_count": 0, | |
"outputs": [] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment