Upload ./RepCodec/repcodec/layers/conv_layer.py with huggingface_hub
Browse files
RepCodec/repcodec/layers/conv_layer.py
ADDED
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Copyright (c) ByteDance, Inc. and its affiliates.
|
2 |
+
# Copyright (c) Chutong Meng
|
3 |
+
#
|
4 |
+
# This source code is licensed under the CC BY-NC license found in the
|
5 |
+
# LICENSE file in the root directory of this source tree.
|
6 |
+
# Based on AudioDec (https://github.com/facebookresearch/AudioDec)
|
7 |
+
|
8 |
+
import torch.nn as nn
|
9 |
+
|
10 |
+
|
11 |
+
class Conv1d1x1(nn.Conv1d):
|
12 |
+
"""1x1 Conv1d."""
|
13 |
+
|
14 |
+
def __init__(self, in_channels, out_channels, bias=True):
|
15 |
+
super(Conv1d1x1, self).__init__(in_channels, out_channels, kernel_size=1, bias=bias)
|
16 |
+
|
17 |
+
|
18 |
+
class Conv1d(nn.Module):
|
19 |
+
def __init__(
|
20 |
+
self,
|
21 |
+
in_channels: int,
|
22 |
+
out_channels: int,
|
23 |
+
kernel_size: int,
|
24 |
+
stride: int = 1,
|
25 |
+
padding: int = -1,
|
26 |
+
dilation: int = 1,
|
27 |
+
groups: int = 1,
|
28 |
+
bias: bool = True
|
29 |
+
):
|
30 |
+
super().__init__()
|
31 |
+
self.in_channels = in_channels
|
32 |
+
self.out_channels = out_channels
|
33 |
+
self.kernel_size = kernel_size
|
34 |
+
if padding < 0:
|
35 |
+
padding = (kernel_size - 1) // 2 * dilation
|
36 |
+
self.dilation = dilation
|
37 |
+
self.conv = nn.Conv1d(
|
38 |
+
in_channels=in_channels,
|
39 |
+
out_channels=out_channels,
|
40 |
+
kernel_size=kernel_size,
|
41 |
+
stride=stride,
|
42 |
+
padding=padding,
|
43 |
+
dilation=dilation,
|
44 |
+
groups=groups,
|
45 |
+
bias=bias,
|
46 |
+
)
|
47 |
+
|
48 |
+
def forward(self, x):
|
49 |
+
"""
|
50 |
+
Args:
|
51 |
+
x (Tensor): Float tensor variable with the shape (B, C, T).
|
52 |
+
Returns:
|
53 |
+
Tensor: Float tensor variable with the shape (B, C, T).
|
54 |
+
"""
|
55 |
+
x = self.conv(x)
|
56 |
+
return x
|
57 |
+
|
58 |
+
|
59 |
+
class ConvTranspose1d(nn.Module):
|
60 |
+
def __init__(
|
61 |
+
self,
|
62 |
+
in_channels: int,
|
63 |
+
out_channels: int,
|
64 |
+
kernel_size: int,
|
65 |
+
stride: int,
|
66 |
+
padding=-1,
|
67 |
+
output_padding=-1,
|
68 |
+
groups=1,
|
69 |
+
bias=True,
|
70 |
+
):
|
71 |
+
super().__init__()
|
72 |
+
if padding < 0:
|
73 |
+
padding = (stride + 1) // 2
|
74 |
+
if output_padding < 0:
|
75 |
+
output_padding = 1 if stride % 2 else 0
|
76 |
+
self.deconv = nn.ConvTranspose1d(
|
77 |
+
in_channels=in_channels,
|
78 |
+
out_channels=out_channels,
|
79 |
+
kernel_size=kernel_size,
|
80 |
+
stride=stride,
|
81 |
+
padding=padding,
|
82 |
+
output_padding=output_padding,
|
83 |
+
groups=groups,
|
84 |
+
bias=bias,
|
85 |
+
)
|
86 |
+
|
87 |
+
def forward(self, x):
|
88 |
+
"""
|
89 |
+
Args:
|
90 |
+
x (Tensor): Float tensor variable with the shape (B, C, T).
|
91 |
+
Returns:
|
92 |
+
Tensor: Float tensor variable with the shape (B, C', T').
|
93 |
+
"""
|
94 |
+
x = self.deconv(x)
|
95 |
+
return x
|