Spaces:
Sleeping
Sleeping
File size: 5,038 Bytes
ffa9b64 |
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 |
import torch.nn as nn
class MusicCNN(nn.Module):
def __init__(self, num_classes, dropout_rate=0.3, device="cuda"):
super(MusicCNN, self).__init__()
self.device = device
# Convolutional blocks
self.conv_block1 = nn.Sequential(
nn.Conv2d(1, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.Conv2d(32, 32, kernel_size=3, padding=1),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Dropout2d(dropout_rate)
).to(device)
self.conv_block2 = nn.Sequential(
nn.Conv2d(32, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.Conv2d(64, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Dropout2d(dropout_rate)
).to(device)
self.conv_block3 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ReLU(),
nn.MaxPool2d(2, 2),
nn.Dropout2d(dropout_rate)
).to(device)
self.fc_layers = None # Fully connected layers will be initialized later
self.num_classes = num_classes
self.dropout_rate = dropout_rate
def forward(self, x):
x = self.conv_block1(x)
x = self.conv_block2(x)
x = self.conv_block3(x)
# Flatten dynamically
x = x.view(x.size(0), -1)
# Initialize FC layers dynamically
if self.fc_layers is None:
fc_input_size = x.size(1)
self.fc_layers = nn.Sequential(
nn.Linear(fc_input_size, 512),
nn.BatchNorm1d(512),
nn.ReLU(),
nn.Dropout(self.dropout_rate),
nn.Linear(512, 256),
nn.BatchNorm1d(256),
nn.ReLU(),
nn.Dropout(self.dropout_rate),
nn.Linear(256, self.num_classes)
).to(self.device)
x = self.fc_layers(x)
return x
class MusicCRNN2D(nn.Module):
def __init__(self, num_classes, dropout_rate=0.1, gru_hidden_size=32, device="cuda"):
super(MusicCRNN2D, self).__init__()
self.device = device
# Input batch normalization
self.input_bn = nn.BatchNorm2d(1).to(device)
# Convolutional blocks
self.conv_block1 = nn.Sequential(
nn.Conv2d(1, 64, kernel_size=3, padding=1),
nn.BatchNorm2d(64),
nn.ELU(),
nn.MaxPool2d((2, 2)),
nn.Dropout2d(dropout_rate)
).to(device)
self.conv_block2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ELU(),
nn.MaxPool2d((4, 2)),
nn.Dropout2d(dropout_rate)
).to(device)
self.conv_block3 = nn.Sequential(
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ELU(),
nn.MaxPool2d((4, 2)),
nn.Dropout2d(dropout_rate)
).to(device)
self.conv_block4 = nn.Sequential(
nn.Conv2d(128, 128, kernel_size=3, padding=1),
nn.BatchNorm2d(128),
nn.ELU(),
nn.MaxPool2d((4, 2)),
nn.Dropout2d(dropout_rate)
).to(device)
self.gru_stack = None # GRU layers will be initialized later
self.classifier = None
self.num_classes = num_classes
self.dropout_rate = dropout_rate
self.gru_hidden_size = gru_hidden_size
def forward(self, x):
x = self.input_bn(x)
x = self.conv_block1(x)
x = self.conv_block2(x)
x = self.conv_block3(x)
x = self.conv_block4(x)
# Reshape for GRU
batch_size, _, freq, time = x.shape
x = x.permute(0, 3, 1, 2) # (batch, time, channels, freq)
x = x.reshape(batch_size, time, -1)
# Initialize GRU dynamically
if self.gru_stack is None:
gru_input_size = x.size(2)
self.gru_stack = nn.GRU(
input_size=gru_input_size,
hidden_size=self.gru_hidden_size,
batch_first=True,
bidirectional=True,
).to(self.device)
self.classifier = nn.Sequential(
nn.Dropout(self.dropout_rate * 3),
nn.Linear(self.gru_hidden_size * 2, self.num_classes) # * 2 for bidirectional
).to(self.device)
x, _ = self.gru_stack(x)
# Take the last time step
x = x[:, -1, :]
# Classification
x = self.classifier(x)
return x |