Spaces:
Sleeping
Sleeping
File size: 7,219 Bytes
7a421a5 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 |
import torch.nn as nn
import torch.nn.functional as F
class dynamics_extractor_full_stereo(nn.Module):
def __init__(self):
super().__init__()
self.conv1d_1 = nn.Conv1d(2, 16, kernel_size=3, padding=1, stride=2)
self.conv1d_2 = nn.Conv1d(16, 16, kernel_size=3, padding=1)
self.conv1d_3 = nn.Conv1d(16, 128, kernel_size=3, padding=1, stride=2)
self.conv1d_4 = nn.Conv1d(128, 128, kernel_size=3, padding=1)
self.conv1d_5 = nn.Conv1d(128, 256, kernel_size=3, padding=1, stride=2)
def forward(self, x):
# original shape: (batchsize, 1, 8280)
# x = x.unsqueeze(1) # shape: (batchsize, 1, 8280)
x = self.conv1d_1(x) # shape: (batchsize, 16, 4140)
x = F.silu(x)
x = self.conv1d_2(x) # shape: (batchsize, 16, 4140)
x = F.silu(x)
x = self.conv1d_3(x) # shape: (batchsize, 128, 2070)
x = F.silu(x)
x = self.conv1d_4(x) # shape: (batchsize, 128, 2070)
x = F.silu(x)
x = self.conv1d_5(x) # shape: (batchsize, 192, 1035)
return x
class melody_extractor_full_mono(nn.Module):
def __init__(self):
super().__init__()
self.conv1d_1 = nn.Conv1d(128, 256, kernel_size=3, padding=0, stride=2)
self.conv1d_2 = nn.Conv1d(256, 256, kernel_size=3, padding=1)
self.conv1d_3 = nn.Conv1d(256, 512, kernel_size=3, padding=1, stride=2)
self.conv1d_4 = nn.Conv1d(512, 512, kernel_size=3, padding=1)
self.conv1d_5 = nn.Conv1d(512, 768, kernel_size=3, padding=1)
def forward(self, x):
# original shape: (batchsize, 12, 1296)
x = self.conv1d_1(x)# shape: (batchsize, 64, 2048)
x = F.silu(x)
x = self.conv1d_2(x) # shape: (batchsize, 64, 2048)
x = F.silu(x)
x = self.conv1d_3(x) # shape: (batchsize, 128, 1024)
x = F.silu(x)
x = self.conv1d_4(x) # shape: (batchsize, 128, 1024)
x = F.silu(x)
x = self.conv1d_5(x) # shape: (batchsize, 768, 1024)
return x
class melody_extractor_mono(nn.Module):
def __init__(self):
super().__init__()
self.conv1d_1 = nn.Conv1d(128, 128, kernel_size=3, padding=0, stride=2)
self.conv1d_2 = nn.Conv1d(128, 192, kernel_size=3, padding=1, stride=2)
self.conv1d_3 = nn.Conv1d(192, 192, kernel_size=3, padding=1)
def forward(self, x):
# original shape: (batchsize, 12, 1296)
x = self.conv1d_1(x)# shape: (batchsize, 64, 2048)
x = F.silu(x)
x = self.conv1d_2(x) # shape: (batchsize, 64, 2048)
x = F.silu(x)
x = self.conv1d_3(x) # shape: (batchsize, 128, 1024)
return x
class melody_extractor_full_stereo(nn.Module):
def __init__(self):
super().__init__()
self.embed = nn.Embedding(num_embeddings=129, embedding_dim=48)
# Four Conv1d layers, each with kernel_size=3, padding=1:
self.conv1 = nn.Conv1d(384, 384, kernel_size=3, padding=1)
self.conv2 = nn.Conv1d(384, 768, kernel_size=3, padding=1)
self.conv3 = nn.Conv1d(768, 768, kernel_size=3, padding=1)
def forward(self, melody_idxs):
# melody_idxs: LongTensor of shape (B, 8, 4096)
B, eight, L = melody_idxs.shape # L == 4096
# 1) Embed:
# (B, 8, 4096) β (B, 8, 4096, 48)
embedded = self.embed(melody_idxs)
# 2) Permute & reshape β (B, 8*48, 4096) = (B, 384, 4096)
x = embedded.permute(0, 1, 3, 2) # (B, 8, 48, 4096)
x = x.reshape(B, eight * 48, L) # (B, 384, 4096)
# 3) Conv1 β (B, 384, 4096)
x = F.silu(self.conv1(x))
# 4) Conv2 β (B, 768, 4096)
x = F.silu(self.conv2(x))
# 5) Conv3 β (B, 768, 4096)
x = F.silu(self.conv3(x))
# Now x is (B, 1536, 4096) and can be sent on to whatever comes next
return x
class melody_extractor_stereo(nn.Module):
def __init__(self):
super().__init__()
self.embed = nn.Embedding(num_embeddings=129, embedding_dim=4)
# Four Conv1d layers, each with kernel_size=3, padding=1:
self.conv1 = nn.Conv1d(32, 64, kernel_size=3, padding=1)
self.conv2 = nn.Conv1d(64, 64, kernel_size=3, padding=0, stride=2)
self.conv3 = nn.Conv1d(64, 128, kernel_size=3, padding=1)
self.conv4 = nn.Conv1d(128, 128, kernel_size=3, padding=1, stride=2)
self.conv5 = nn.Conv1d(128, 256, kernel_size=3, padding=1)
def forward(self, melody_idxs):
# melody_idxs: LongTensor of shape (B, 8, 4096)
B, eight, L = melody_idxs.shape # L == 4096
# 1) Embed:
# (B, 8, 4096) β (B, 8, 4096, 4)
embedded = self.embed(melody_idxs)
# 2) Permute & reshape β (B, 8*4, 4096) = (B, 32, 4096)
x = embedded.permute(0, 1, 3, 2) # (B, 8, 4, 4096)
x = x.reshape(B, eight * 4, L) # (B, 32, 4096)
# 3) Conv1 β (B, 384, 4096)
x = F.silu(self.conv1(x))
# 4) Conv2 β (B, 768, 4096)
x = F.silu(self.conv2(x))
# 5) Conv3 β (B, 768, 4096)
x = F.silu(self.conv3(x))
x = F.silu(self.conv4(x))
x = F.silu(self.conv5(x))
# Now x is (B, 1536, 4096) and can be sent on to whatever comes next
return x
class dynamics_extractor(nn.Module):
def __init__(self):
super().__init__()
self.conv1d_1 = nn.Conv1d(1, 16, kernel_size=3, padding=1, stride=2)
self.conv1d_2 = nn.Conv1d(16, 16, kernel_size=3, padding=1)
self.conv1d_3 = nn.Conv1d(16, 128, kernel_size=3, padding=1, stride=2)
self.conv1d_4 = nn.Conv1d(128, 128, kernel_size=3, padding=1)
self.conv1d_5 = nn.Conv1d(128, 192, kernel_size=3, padding=1, stride=2)
def forward(self, x):
# original shape: (batchsize, 1, 8280)
# x = x.unsqueeze(1) # shape: (batchsize, 1, 8280)
x = self.conv1d_1(x) # shape: (batchsize, 16, 4140)
x = F.silu(x)
x = self.conv1d_2(x) # shape: (batchsize, 16, 4140)
x = F.silu(x)
x = self.conv1d_3(x) # shape: (batchsize, 128, 2070)
x = F.silu(x)
x = self.conv1d_4(x) # shape: (batchsize, 128, 2070)
x = F.silu(x)
x = self.conv1d_5(x) # shape: (batchsize, 192, 1035)
return x
class rhythm_extractor(nn.Module):
def __init__(self):
super().__init__()
self.conv1d_1 = nn.Conv1d(2, 16, kernel_size=3, padding=1)
self.conv1d_2 = nn.Conv1d(16, 64, kernel_size=3, padding=1)
self.conv1d_3 = nn.Conv1d(64, 128, kernel_size=3, padding=1, stride=2)
self.conv1d_4 = nn.Conv1d(128, 128, kernel_size=3, padding=1)
self.conv1d_5 = nn.Conv1d(128, 192, kernel_size=3, padding=1, stride=2)
def forward(self, x):
# original shape: (batchsize, 2, 3000)
x = self.conv1d_1(x)# shape: (batchsize, 64, 3000)
x = F.silu(x)
x = self.conv1d_2(x) # shape: (batchsize, 64, 3000)
x = F.silu(x)
x = self.conv1d_3(x) # shape: (batchsize, 128, 1500)
x = F.silu(x)
x = self.conv1d_4(x) # shape: (batchsize, 128, 1500)
x = F.silu(x)
x = self.conv1d_5(x) # shape:
return x |