|
| 1 | +import torch |
| 2 | +import torch.nn as nn |
| 3 | +import torch.nn.functional as F |
| 4 | +import math |
| 5 | +import comfy.model_management |
| 6 | + |
| 7 | +if 'sinc' in dir(torch): |
| 8 | + sinc = torch.sinc |
| 9 | +else: |
| 10 | + # This code is adopted from adefossez's julius.core.sinc under the MIT License |
| 11 | + # https://adefossez.github.io/julius/julius/core.html |
| 12 | + # LICENSE is in incl_licenses directory. |
| 13 | + def sinc(x: torch.Tensor): |
| 14 | + """ |
| 15 | + Implementation of sinc, i.e. sin(pi * x) / (pi * x) |
| 16 | + __Warning__: Different to julius.sinc, the input is multiplied by `pi`! |
| 17 | + """ |
| 18 | + return torch.where(x == 0, |
| 19 | + torch.tensor(1., device=x.device, dtype=x.dtype), |
| 20 | + torch.sin(math.pi * x) / math.pi / x) |
| 21 | + |
| 22 | + |
| 23 | +# This code is adopted from adefossez's julius.lowpass.LowPassFilters under the MIT License |
| 24 | +# https://adefossez.github.io/julius/julius/lowpass.html |
| 25 | +# LICENSE is in incl_licenses directory. |
| 26 | +def kaiser_sinc_filter1d(cutoff, half_width, kernel_size): # return filter [1,1,kernel_size] |
| 27 | + even = (kernel_size % 2 == 0) |
| 28 | + half_size = kernel_size // 2 |
| 29 | + |
| 30 | + #For kaiser window |
| 31 | + delta_f = 4 * half_width |
| 32 | + A = 2.285 * (half_size - 1) * math.pi * delta_f + 7.95 |
| 33 | + if A > 50.: |
| 34 | + beta = 0.1102 * (A - 8.7) |
| 35 | + elif A >= 21.: |
| 36 | + beta = 0.5842 * (A - 21)**0.4 + 0.07886 * (A - 21.) |
| 37 | + else: |
| 38 | + beta = 0. |
| 39 | + window = torch.kaiser_window(kernel_size, beta=beta, periodic=False) |
| 40 | + |
| 41 | + # ratio = 0.5/cutoff -> 2 * cutoff = 1 / ratio |
| 42 | + if even: |
| 43 | + time = (torch.arange(-half_size, half_size) + 0.5) |
| 44 | + else: |
| 45 | + time = torch.arange(kernel_size) - half_size |
| 46 | + if cutoff == 0: |
| 47 | + filter_ = torch.zeros_like(time) |
| 48 | + else: |
| 49 | + filter_ = 2 * cutoff * window * sinc(2 * cutoff * time) |
| 50 | + # Normalize filter to have sum = 1, otherwise we will have a small leakage |
| 51 | + # of the constant component in the input signal. |
| 52 | + filter_ /= filter_.sum() |
| 53 | + filter = filter_.view(1, 1, kernel_size) |
| 54 | + |
| 55 | + return filter |
| 56 | + |
| 57 | + |
| 58 | +class LowPassFilter1d(nn.Module): |
| 59 | + def __init__(self, |
| 60 | + cutoff=0.5, |
| 61 | + half_width=0.6, |
| 62 | + stride: int = 1, |
| 63 | + padding: bool = True, |
| 64 | + padding_mode: str = 'replicate', |
| 65 | + kernel_size: int = 12): |
| 66 | + # kernel_size should be even number for stylegan3 setup, |
| 67 | + # in this implementation, odd number is also possible. |
| 68 | + super().__init__() |
| 69 | + if cutoff < -0.: |
| 70 | + raise ValueError("Minimum cutoff must be larger than zero.") |
| 71 | + if cutoff > 0.5: |
| 72 | + raise ValueError("A cutoff above 0.5 does not make sense.") |
| 73 | + self.kernel_size = kernel_size |
| 74 | + self.even = (kernel_size % 2 == 0) |
| 75 | + self.pad_left = kernel_size // 2 - int(self.even) |
| 76 | + self.pad_right = kernel_size // 2 |
| 77 | + self.stride = stride |
| 78 | + self.padding = padding |
| 79 | + self.padding_mode = padding_mode |
| 80 | + filter = kaiser_sinc_filter1d(cutoff, half_width, kernel_size) |
| 81 | + self.register_buffer("filter", filter) |
| 82 | + |
| 83 | + #input [B, C, T] |
| 84 | + def forward(self, x): |
| 85 | + _, C, _ = x.shape |
| 86 | + |
| 87 | + if self.padding: |
| 88 | + x = F.pad(x, (self.pad_left, self.pad_right), |
| 89 | + mode=self.padding_mode) |
| 90 | + out = F.conv1d(x, comfy.model_management.cast_to(self.filter.expand(C, -1, -1), dtype=x.dtype, device=x.device), |
| 91 | + stride=self.stride, groups=C) |
| 92 | + |
| 93 | + return out |
| 94 | + |
| 95 | + |
| 96 | +class UpSample1d(nn.Module): |
| 97 | + def __init__(self, ratio=2, kernel_size=None): |
| 98 | + super().__init__() |
| 99 | + self.ratio = ratio |
| 100 | + self.kernel_size = int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size |
| 101 | + self.stride = ratio |
| 102 | + self.pad = self.kernel_size // ratio - 1 |
| 103 | + self.pad_left = self.pad * self.stride + (self.kernel_size - self.stride) // 2 |
| 104 | + self.pad_right = self.pad * self.stride + (self.kernel_size - self.stride + 1) // 2 |
| 105 | + filter = kaiser_sinc_filter1d(cutoff=0.5 / ratio, |
| 106 | + half_width=0.6 / ratio, |
| 107 | + kernel_size=self.kernel_size) |
| 108 | + self.register_buffer("filter", filter) |
| 109 | + |
| 110 | + # x: [B, C, T] |
| 111 | + def forward(self, x): |
| 112 | + _, C, _ = x.shape |
| 113 | + |
| 114 | + x = F.pad(x, (self.pad, self.pad), mode='replicate') |
| 115 | + x = self.ratio * F.conv_transpose1d( |
| 116 | + x, comfy.model_management.cast_to(self.filter.expand(C, -1, -1), dtype=x.dtype, device=x.device), stride=self.stride, groups=C) |
| 117 | + x = x[..., self.pad_left:-self.pad_right] |
| 118 | + |
| 119 | + return x |
| 120 | + |
| 121 | + |
| 122 | +class DownSample1d(nn.Module): |
| 123 | + def __init__(self, ratio=2, kernel_size=None): |
| 124 | + super().__init__() |
| 125 | + self.ratio = ratio |
| 126 | + self.kernel_size = int(6 * ratio // 2) * 2 if kernel_size is None else kernel_size |
| 127 | + self.lowpass = LowPassFilter1d(cutoff=0.5 / ratio, |
| 128 | + half_width=0.6 / ratio, |
| 129 | + stride=ratio, |
| 130 | + kernel_size=self.kernel_size) |
| 131 | + |
| 132 | + def forward(self, x): |
| 133 | + xx = self.lowpass(x) |
| 134 | + |
| 135 | + return xx |
| 136 | + |
| 137 | +class Activation1d(nn.Module): |
| 138 | + def __init__(self, |
| 139 | + activation, |
| 140 | + up_ratio: int = 2, |
| 141 | + down_ratio: int = 2, |
| 142 | + up_kernel_size: int = 12, |
| 143 | + down_kernel_size: int = 12): |
| 144 | + super().__init__() |
| 145 | + self.up_ratio = up_ratio |
| 146 | + self.down_ratio = down_ratio |
| 147 | + self.act = activation |
| 148 | + self.upsample = UpSample1d(up_ratio, up_kernel_size) |
| 149 | + self.downsample = DownSample1d(down_ratio, down_kernel_size) |
| 150 | + |
| 151 | + # x: [B,C,T] |
| 152 | + def forward(self, x): |
| 153 | + x = self.upsample(x) |
| 154 | + x = self.act(x) |
| 155 | + x = self.downsample(x) |
| 156 | + |
| 157 | + return x |
0 commit comments