Musica1 / audio_classifier.py
Clemylia's picture
Upload audio_classifier.py with huggingface_hub
66b68b4 verified
import torch
import torch.nn as nn
import torch.nn.functional as F
# Le nombre de classes est tiré de ton dataset.
NUM_CLASSES = 2
class AudioClassifier(nn.Module):
"""
Réseau de Neurones Convolutionnels (CNN) simple pour la classification audio.
C'est l'architecture que nous avons entraînée from scratch.
"""
def __init__(self):
super(AudioClassifier, self).__init__()
self.conv1 = nn.Conv2d(in_channels=1, out_channels=32, kernel_size=(5, 5), padding=2)
self.bn1 = nn.BatchNorm2d(32)
self.pool1 = nn.MaxPool2d(kernel_size=(2, 2))
self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=(3, 3), padding=1)
self.bn2 = nn.BatchNorm2d(64)
self.pool2 = nn.MaxPool2d(kernel_size=(2, 2))
self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(3, 3), padding=1)
self.bn3 = nn.BatchNorm2d(128)
self.pool3 = nn.MaxPool2d(kernel_size=(2, 2))
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc1 = nn.Linear(128 * 1 * 1, NUM_CLASSES)
def forward(self, x):
x = self.pool1(F.relu(self.bn1(self.conv1(x))))
x = self.pool2(F.relu(self.bn2(self.conv2(x))))
x = self.pool3(F.relu(self.bn3(self.conv3(x))))
x = self.avgpool(x)
x = torch.flatten(x, 1)
return self.fc1(x)