-
Notifications
You must be signed in to change notification settings - Fork 739
Add MelResNet Block #705
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
Add MelResNet Block #705
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4e89423
Add MelResNet Block
0d42632
add default value
0a6cd7a
update model and test
3835501
rebase and small changes
89810b9
add pad variable
fa3cb00
update format
2215c6c
update reference in docstrings
b211af5
add underscore name
4b1e481
add underscore name
9eaefd5
add underscore name
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,2 @@ | ||
| from .wav2letter import * | ||
| from ._wavernn import * | ||
vincentqb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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,107 @@ | ||
| from typing import Optional | ||
|
|
||
| from torch import Tensor | ||
| from torch import nn | ||
|
|
||
| __all__ = ["_ResBlock", "_MelResNet"] | ||
|
|
||
|
|
||
| class _ResBlock(nn.Module): | ||
| r"""This is a ResNet block layer. This layer is based on the paper "Deep Residual Learning | ||
| for Image Recognition". Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun. CVPR, 2016. | ||
| It is a block used in WaveRNN. WaveRNN is based on the paper "Efficient Neural Audio Synthesis". | ||
| Nal Kalchbrenner, Erich Elsen, Karen Simonyan, Seb Noury, Norman Casagrande, Edward Lockhart, | ||
| Florian Stimberg, Aaron van den Oord, Sander Dieleman, Koray Kavukcuoglu. arXiv:1802.08435, 2018. | ||
| Args: | ||
| num_dims: the number of compute dimensions in the input (default=128). | ||
| Examples:: | ||
| >>> resblock = _ResBlock(num_dims=128) | ||
| >>> input = torch.rand(10, 128, 512) | ||
| >>> output = resblock(input) | ||
| """ | ||
|
|
||
| def __init__(self, num_dims: int = 128) -> None: | ||
| super().__init__() | ||
|
|
||
| self.resblock_model = nn.Sequential( | ||
| nn.Conv1d(in_channels=num_dims, out_channels=num_dims, kernel_size=1, bias=False), | ||
| nn.BatchNorm1d(num_dims), | ||
| nn.ReLU(inplace=True), | ||
| nn.Conv1d(in_channels=num_dims, out_channels=num_dims, kernel_size=1, bias=False), | ||
| nn.BatchNorm1d(num_dims) | ||
| ) | ||
|
|
||
| def forward(self, x: Tensor) -> Tensor: | ||
| r"""Pass the input through the _ResBlock layer. | ||
| Args: | ||
| x: the input sequence to the _ResBlock layer (required). | ||
| Shape: | ||
| - x: :math:`(N, S, T)`. | ||
| - output: :math:`(N, S, T)`. | ||
| where N is the batch size, S is the number of input sequence, | ||
| T is the length of input sequence. | ||
| """ | ||
|
|
||
| residual = x | ||
| return self.resblock_model(x) + residual | ||
|
|
||
|
|
||
| class _MelResNet(nn.Module): | ||
| r"""This is a MelResNet layer based on a stack of ResBlocks. It is a block used in WaveRNN. | ||
| WaveRNN is based on the paper "Efficient Neural Audio Synthesis". Nal Kalchbrenner, Erich Elsen, | ||
| Karen Simonyan, Seb Noury, Norman Casagrande, Edward Lockhart, Florian Stimberg, Aaron van den Oord, | ||
| Sander Dieleman, Koray Kavukcuoglu. arXiv:1802.08435, 2018. | ||
| Args: | ||
| res_blocks: the number of ResBlock in stack (default=10). | ||
| input_dims: the number of input sequence (default=100). | ||
| hidden_dims: the number of compute dimensions (default=128). | ||
| output_dims: the number of output sequence (default=128). | ||
| pad: the number of kernal size (pad * 2 + 1) in the first Conv1d layer (default=2). | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: typo "kernel" |
||
| Examples:: | ||
| >>> melresnet = _MelResNet(res_blocks=10, input_dims=100, | ||
| hidden_dims=128, output_dims=128, pad=2) | ||
| >>> input = torch.rand(10, 100, 512) | ||
| >>> output = melresnet(input) | ||
| """ | ||
|
|
||
| def __init__(self, res_blocks: int = 10, | ||
| input_dims: int = 100, | ||
| hidden_dims: int = 128, | ||
| output_dims: int = 128, | ||
| pad: int = 2) -> None: | ||
| super().__init__() | ||
|
|
||
| kernel_size = pad * 2 + 1 | ||
| ResBlocks = [] | ||
|
|
||
| for i in range(res_blocks): | ||
| ResBlocks.append(_ResBlock(hidden_dims)) | ||
|
|
||
| self.melresnet_model = nn.Sequential( | ||
| nn.Conv1d(in_channels=input_dims, out_channels=hidden_dims, kernel_size=kernel_size, bias=False), | ||
| nn.BatchNorm1d(hidden_dims), | ||
| nn.ReLU(inplace=True), | ||
| *ResBlocks, | ||
| nn.Conv1d(in_channels=hidden_dims, out_channels=output_dims, kernel_size=1) | ||
| ) | ||
|
|
||
| def forward(self, x: Tensor) -> Tensor: | ||
| r"""Pass the input through the _MelResNet layer. | ||
| Args: | ||
| x: the input sequence to the _MelResNet layer (required). | ||
| Shape: | ||
| - x: :math:`(N, S, T)`. | ||
| - output: :math:`(N, P, T-2*pad)`. | ||
| where N is the batch size, S is the number of input sequence, | ||
| P is the number of ouput sequence, T is the length of input sequence. | ||
| """ | ||
|
|
||
| return self.melresnet_model(x) | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test does not subclass
unittest.TestCaseso it won't run infbcode.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good catch! @jimchen90 -- can you send a follow-up pull request to update this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. I will update it.