Spaces:
Running
Running
File size: 4,541 Bytes
9791162 |
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 |
import functools
import torch
import torch.nn.functional as F
import crepe
###########################################################################
# Model definition
###########################################################################
class Crepe(torch.nn.Module):
"""Crepe model definition"""
def __init__(self, model='full'):
super().__init__()
# Model-specific layer parameters
if model == 'full':
in_channels = [1, 1024, 128, 128, 128, 256]
out_channels = [1024, 128, 128, 128, 256, 512]
self.in_features = 2048
elif model == 'tiny':
in_channels = [1, 128, 16, 16, 16, 32]
out_channels = [128, 16, 16, 16, 32, 64]
self.in_features = 256
else:
raise ValueError(f'Model {model} is not supported')
# Shared layer parameters
kernel_sizes = [(512, 1)] + 5 * [(64, 1)]
strides = [(4, 1)] + 5 * [(1, 1)]
# Overload with eps and momentum conversion given by MMdnn
batch_norm_fn = functools.partial(torch.nn.BatchNorm2d,
eps=0.0010000000474974513,
momentum=0.0)
# Layer definitions
self.conv1 = torch.nn.Conv2d(
in_channels=in_channels[0],
out_channels=out_channels[0],
kernel_size=kernel_sizes[0],
stride=strides[0])
self.conv1_BN = batch_norm_fn(
num_features=out_channels[0])
self.conv2 = torch.nn.Conv2d(
in_channels=in_channels[1],
out_channels=out_channels[1],
kernel_size=kernel_sizes[1],
stride=strides[1])
self.conv2_BN = batch_norm_fn(
num_features=out_channels[1])
self.conv3 = torch.nn.Conv2d(
in_channels=in_channels[2],
out_channels=out_channels[2],
kernel_size=kernel_sizes[2],
stride=strides[2])
self.conv3_BN = batch_norm_fn(
num_features=out_channels[2])
self.conv4 = torch.nn.Conv2d(
in_channels=in_channels[3],
out_channels=out_channels[3],
kernel_size=kernel_sizes[3],
stride=strides[3])
self.conv4_BN = batch_norm_fn(
num_features=out_channels[3])
self.conv5 = torch.nn.Conv2d(
in_channels=in_channels[4],
out_channels=out_channels[4],
kernel_size=kernel_sizes[4],
stride=strides[4])
self.conv5_BN = batch_norm_fn(
num_features=out_channels[4])
self.conv6 = torch.nn.Conv2d(
in_channels=in_channels[5],
out_channels=out_channels[5],
kernel_size=kernel_sizes[5],
stride=strides[5])
self.conv6_BN = batch_norm_fn(
num_features=out_channels[5])
self.classifier = torch.nn.Linear(
in_features=self.in_features,
out_features=crepe.PITCH_BINS)
def forward(self, x, embed=False):
# Forward pass through first five layers
x = self.embed(x)
if embed:
return x
# Forward pass through layer six
x = self.layer(x, self.conv6, self.conv6_BN)
# shape=(batch, self.in_features)
x = x.permute(0, 2, 1, 3).reshape(-1, self.in_features)
# Compute logits
return torch.sigmoid(self.classifier(x))
###########################################################################
# Forward pass utilities
###########################################################################
def embed(self, x):
"""Map input audio to pitch embedding"""
# shape=(batch, 1, 1024, 1)
x = x[:, None, :, None]
# Forward pass through first five layers
x = self.layer(x, self.conv1, self.conv1_BN, (0, 0, 254, 254))
x = self.layer(x, self.conv2, self.conv2_BN)
x = self.layer(x, self.conv3, self.conv3_BN)
x = self.layer(x, self.conv4, self.conv4_BN)
x = self.layer(x, self.conv5, self.conv5_BN)
return x
def layer(self, x, conv, batch_norm, padding=(0, 0, 31, 32)):
"""Forward pass through one layer"""
x = F.pad(x, padding)
x = conv(x)
x = F.relu(x)
x = batch_norm(x)
return F.max_pool2d(x, (2, 1), (2, 1))
|