-
Notifications
You must be signed in to change notification settings - Fork 696
Add vanilla DeepSpeech model #1399
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9b359a1
#446 add vanilla deepspeech model
discort 7a4a38c
docstring for deepspeech forward
discort bb7432f
batch_first in model output for better performance
discort 0a1647a
fixed tests for deepspeech
discort 7b59009
use naming convention from readme.
vincentqb 0874306
lint.
vincentqb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
from .wav2letter import Wav2Letter | ||
from .wavernn import WaveRNN | ||
from .conv_tasnet import ConvTasNet | ||
from .deepspeech import DeepSpeech | ||
|
||
__all__ = [ | ||
'Wav2Letter', | ||
'WaveRNN', | ||
'ConvTasNet', | ||
'DeepSpeech', | ||
] |
This file contains hidden or 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import torch | ||
|
||
__all__ = ["DeepSpeech"] | ||
|
||
|
||
class FullyConnected(torch.nn.Module): | ||
""" | ||
Args: | ||
n_feature: Number of input features | ||
n_hidden: Internal hidden unit size. | ||
""" | ||
|
||
def __init__(self, | ||
n_feature: int, | ||
n_hidden: int, | ||
dropout: float, | ||
relu_max_clip: int = 20) -> None: | ||
super(FullyConnected, self).__init__() | ||
self.fc = torch.nn.Linear(n_feature, n_hidden, bias=True) | ||
self.relu_max_clip = relu_max_clip | ||
self.dropout = dropout | ||
|
||
def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
x = self.fc(x) | ||
x = torch.nn.functional.relu(x) | ||
x = torch.nn.functional.hardtanh(x, 0, self.relu_max_clip) | ||
if self.dropout: | ||
x = torch.nn.functional.dropout(x, self.dropout, self.training) | ||
return x | ||
|
||
|
||
class DeepSpeech(torch.nn.Module): | ||
""" | ||
DeepSpeech model architecture from | ||
`"Deep Speech: Scaling up end-to-end speech recognition"` | ||
<https://arxiv.org/abs/1412.5567> paper. | ||
|
||
Args: | ||
n_feature: Number of input features | ||
n_hidden: Internal hidden unit size. | ||
n_class: Number of output classes | ||
""" | ||
|
||
def __init__( | ||
self, | ||
n_feature: int, | ||
n_hidden: int = 2048, | ||
n_class: int = 40, | ||
dropout: float = 0.0, | ||
) -> None: | ||
super(DeepSpeech, self).__init__() | ||
self.n_hidden = n_hidden | ||
self.fc1 = FullyConnected(n_feature, n_hidden, dropout) | ||
self.fc2 = FullyConnected(n_hidden, n_hidden, dropout) | ||
self.fc3 = FullyConnected(n_hidden, n_hidden, dropout) | ||
self.bi_rnn = torch.nn.RNN( | ||
n_hidden, n_hidden, num_layers=1, nonlinearity="relu", bidirectional=True | ||
) | ||
self.fc4 = FullyConnected(n_hidden, n_hidden, dropout) | ||
self.out = torch.nn.Linear(n_hidden, n_class) | ||
|
||
def forward(self, x: torch.Tensor) -> torch.Tensor: | ||
""" | ||
Args: | ||
x (torch.Tensor): Tensor of dimension (batch, channel, time, feature). | ||
Returns: | ||
Tensor: Predictor tensor of dimension (batch, time, class). | ||
""" | ||
# N x C x T x F | ||
x = self.fc1(x) | ||
# N x C x T x H | ||
x = self.fc2(x) | ||
# N x C x T x H | ||
x = self.fc3(x) | ||
# N x C x T x H | ||
x = x.squeeze(1) | ||
# N x T x H | ||
x = x.transpose(0, 1) | ||
# T x N x H | ||
x, _ = self.bi_rnn(x) | ||
# The fifth (non-recurrent) layer takes both the forward and backward units as inputs | ||
x = x[:, :, :self.n_hidden] + x[:, :, self.n_hidden:] | ||
# T x N x H | ||
x = self.fc4(x) | ||
# T x N x H | ||
x = self.out(x) | ||
# T x N x n_class | ||
x = x.permute(1, 0, 2) | ||
# N x T x n_class | ||
x = torch.nn.functional.log_softmax(x, dim=2) | ||
# N x T x n_class | ||
return x | ||
discort marked this conversation as resolved.
Show resolved
Hide resolved
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.