ilyassmoummad/ProtoCLR

Model

ProtoCLR

0

32 commits

2 linked in READMEs

updated Mar 5, 2025

See the code

README

ProtoCLR

This repository contains a CvT-13 Convolutional Vision Transformer model trained from scratch on the Xeno-Canto dataset, specifically on 6-second audio segments sampled at 16 kHz. The model is trained on Mel spectrograms of bird sounds using ProtoCLR (Prototypical Contrastive Loss) for 300 epochs and can be used as a feature extractor for bird audio classification and related tasks.

Files

  • cvt.py: Defines the CvT-13 model architecture.
  • protoclr.pth: Pre-trained model weights for ProtoCLR.
  • config/: Configuration files for CvT-13 setup.
  • mel_spectrogram.py: Contains the MelSpectrogramProcessor class, which converts audio waveforms into Mel spectrograms, a format suitable for model input.

Setup

  1. Clone this repository: Clone the repository and navigate into the project directory: git clone https://huggingface.co/ilyassmoummad/ProtoCLR cd ProtoCLR/

  2. Install dependencies: Ensure you have the required Python packages, including torch and any other dependencies listed in requirements.txt.

    pip install -r requirements.txt
    

Usage

  1. Prepare the Audio:
    To ensure compatibility with the model, follow these preprocessing steps for your audio files:

    • Mono Channel (Mandatory):
      If the audio has multiple channels, convert it to a single mono channel by averaging the channels.
    • Sample Rate (Mandatory):
      Resample the audio to a consistent sample rate of 16 kHz.
    • Padding (Recommended):
      For audio files shorter than 6 seconds, pad with zeros or repeat the audio until it reaches a length of 6 seconds.
    • Chunking (Recommended):
      For audio files longer than 6 seconds, split them into chunks of 6 seconds each for better processing.
  2. Process the Audio:
    Use the MelSpectrogramProcessor (from melspectrogram.py) to transform the prepared audio into a Mel spectrogram, a format suitable for model input, as demonstrated in the following example.

Example Code

The following example demonstrates loading, processing, and running inference on an audio file:

import torch
from cvt import cvt13  # Import model architecture
from melspectrogram import MelSpectrogramProcessor  # Import Mel spectrogram processor

# Initialize the preprocessor and model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
preprocessor = MelSpectrogramProcessor(device=device)
model = cvt13()

# Load weights trained using Cross-Entropy
model.load_state_dict(torch.load("ce.pth", map_location="cpu")['encoder'])

# Load weights trained using SimCLR (self-supervised contrastive learning)
model.load_state_dict(torch.load("simclr.pth", map_location="cpu"))

# Load weights trained using SupCon (supervised contrastive learning)
model.load_state_dict(torch.load("supcon.pth", map_location="cpu"))

# Load weights trained using ProtoCLR (supervised contrastive learning using prototypes)
model.load_state_dict(torch.load("protoclr.pth", map_location="cpu"))

# Optional: Move the model to GPU for faster processing if available using : model = model.to('cuda') , for instance.
model = model.to(device)
model.eval()

# Load and preprocess a sample audio waveform
def load_waveform(file_path):
    # Replace this with your specific audio loading function
    # For example, using torchaudio to load and resample
    pass

waveform = load_waveform("path/to/audio.wav").to(device)  # Load your audio file here and convert it to a PyTorch tensor.

# Ensure waveform is sampled at 16 kHz, then pad/chunk as needed for 6s length
input_tensor = preprocessor.process(waveform).unsqueeze(0)  # Add batch dimension

# Run the model on the preprocessed audio
with torch.no_grad():
    output = model(input_tensor)
    print("Model output shape:", output.shape)

Model Performance Comparison

The following table presents the classification accuracy of various models on one-shot and five-shot bird sound classification tasks, evaluated across different soundscape datasets.

ModelTraining Params (M)PERNESUHHHSNSSWSNEMean
Random Guessing-0.751.123.705.261.041.782.22
1-Shot Classification
BirdAVES-biox-base957.41±1.026.4±2.313.2±3.19.84±3.58.74±0.614.1±3.113.2
BirdAVES-bioxn-large3167.59±0.827.2±3.613.7±2.912.5±3.610.0±1.414.5±3.214.2
BioLingual1536.21±1.137.5±2.917.8±3.517.6±5.122.5±4.026.4±3.421.3
Perch809.10±5.342.4±4.919.8±5.026.7±9.822.3±3.329.1±5.924.9
CE (Ours)239.55±1.541.3±3.619.7±4.725.2±5.717.8±1.431.5±5.424.2
SimCLR (Ours)197.85±1.131.2±2.414.9±2.919.0±3.810.6±1.124.0±4.117.9
SupCon (Ours)198.53±1.139.8±6.018.8±3.020.4±6.912.6±1.623.2±3.120.5
ProtoCLR (Ours)199.23±1.638.6±5.118.4±2.321.2±7.315.5±2.325.8±5.221.4
5-Shot Classification
BirdAVES-biox-base9511.6±0.839.7±1.822.5±2.422.1±3.316.1±1.728.3±2.323.3
BirdAVES-bioxn-large31615.0±0.942.6±2.723.7±3.828.4±2.418.3±1.827.3±2.325.8
BioLingual15313.6±1.365.2±1.431.0±2.934.3±3.543.9±0.949.9±2.339.6
Perch8021.2±1.271.7±1.539.5±3.052.5±5.948.0±1.959.7±1.848.7
CE (Ours)2321.4±1.369.2±1.835.6±3.448.2±5.539.9±1.157.5±2.345.3
SimCLR (Ours)1915.4±1.054.0±1.823.0±2.332.8±4.022.0±1.240.7±2.431.3
SupCon (Ours)1917.2±1.364.6±2.434.1±2.942.5±2.930.8±0.848.1±2.439.5
ProtoCLR (Ours)1919.2±1.167.9±2.836.1±4.348.0±4.334.6±2.348.6±2.842.4

For additional details, please see the pre-print on arXiv and the official GitHub repository.

Citation

If you use our model in your research, please cite the following paper:

@misc{moummad2024dirlbs,
      title={Domain-Invariant Representation Learning of Bird Sounds}, 
      author={Ilyass Moummad and Romain Serizel and Emmanouil Benetos and Nicolas Farrugia},
      year={2024},
      eprint={2409.08589},
      archivePrefix={arXiv},
      primaryClass={cs.SD},
      url={https://arxiv.org/abs/2409.08589}, 
}
Bioacoustics
feature-extraction
pytorch

Contributors

ilyassmoummad

32 commits

ilyassmoummad/ProtoCLR

Model

ProtoCLR

0

32 commits

2 linked in READMEs

updated Mar 5, 2025

See the code

README

ProtoCLR

This repository contains a CvT-13 Convolutional Vision Transformer model trained from scratch on the Xeno-Canto dataset, specifically on 6-second audio segments sampled at 16 kHz. The model is trained on Mel spectrograms of bird sounds using ProtoCLR (Prototypical Contrastive Loss) for 300 epochs and can be used as a feature extractor for bird audio classification and related tasks.

Files

  • cvt.py: Defines the CvT-13 model architecture.
  • protoclr.pth: Pre-trained model weights for ProtoCLR.
  • config/: Configuration files for CvT-13 setup.
  • mel_spectrogram.py: Contains the MelSpectrogramProcessor class, which converts audio waveforms into Mel spectrograms, a format suitable for model input.

Setup

  1. Clone this repository: Clone the repository and navigate into the project directory: git clone https://huggingface.co/ilyassmoummad/ProtoCLR cd ProtoCLR/

  2. Install dependencies: Ensure you have the required Python packages, including torch and any other dependencies listed in requirements.txt.

    pip install -r requirements.txt
    

Usage

  1. Prepare the Audio:
    To ensure compatibility with the model, follow these preprocessing steps for your audio files:

    • Mono Channel (Mandatory):
      If the audio has multiple channels, convert it to a single mono channel by averaging the channels.
    • Sample Rate (Mandatory):
      Resample the audio to a consistent sample rate of 16 kHz.
    • Padding (Recommended):
      For audio files shorter than 6 seconds, pad with zeros or repeat the audio until it reaches a length of 6 seconds.
    • Chunking (Recommended):
      For audio files longer than 6 seconds, split them into chunks of 6 seconds each for better processing.
  2. Process the Audio:
    Use the MelSpectrogramProcessor (from melspectrogram.py) to transform the prepared audio into a Mel spectrogram, a format suitable for model input, as demonstrated in the following example.

Example Code

The following example demonstrates loading, processing, and running inference on an audio file:

import torch
from cvt import cvt13  # Import model architecture
from melspectrogram import MelSpectrogramProcessor  # Import Mel spectrogram processor

# Initialize the preprocessor and model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
preprocessor = MelSpectrogramProcessor(device=device)
model = cvt13()

# Load weights trained using Cross-Entropy
model.load_state_dict(torch.load("ce.pth", map_location="cpu")['encoder'])

# Load weights trained using SimCLR (self-supervised contrastive learning)
model.load_state_dict(torch.load("simclr.pth", map_location="cpu"))

# Load weights trained using SupCon (supervised contrastive learning)
model.load_state_dict(torch.load("supcon.pth", map_location="cpu"))

# Load weights trained using ProtoCLR (supervised contrastive learning using prototypes)
model.load_state_dict(torch.load("protoclr.pth", map_location="cpu"))

# Optional: Move the model to GPU for faster processing if available using : model = model.to('cuda') , for instance.
model = model.to(device)
model.eval()

# Load and preprocess a sample audio waveform
def load_waveform(file_path):
    # Replace this with your specific audio loading function
    # For example, using torchaudio to load and resample
    pass

waveform = load_waveform("path/to/audio.wav").to(device)  # Load your audio file here and convert it to a PyTorch tensor.

# Ensure waveform is sampled at 16 kHz, then pad/chunk as needed for 6s length
input_tensor = preprocessor.process(waveform).unsqueeze(0)  # Add batch dimension

# Run the model on the preprocessed audio
with torch.no_grad():
    output = model(input_tensor)
    print("Model output shape:", output.shape)

Model Performance Comparison

The following table presents the classification accuracy of various models on one-shot and five-shot bird sound classification tasks, evaluated across different soundscape datasets.

ModelTraining Params (M)PERNESUHHHSNSSWSNEMean
Random Guessing-0.751.123.705.261.041.782.22
1-Shot Classification
BirdAVES-biox-base957.41±1.026.4±2.313.2±3.19.84±3.58.74±0.614.1±3.113.2
BirdAVES-bioxn-large3167.59±0.827.2±3.613.7±2.912.5±3.610.0±1.414.5±3.214.2
BioLingual1536.21±1.137.5±2.917.8±3.517.6±5.122.5±4.026.4±3.421.3
Perch809.10±5.342.4±4.919.8±5.026.7±9.822.3±3.329.1±5.924.9
CE (Ours)239.55±1.541.3±3.619.7±4.725.2±5.717.8±1.431.5±5.424.2
SimCLR (Ours)197.85±1.131.2±2.414.9±2.919.0±3.810.6±1.124.0±4.117.9
SupCon (Ours)198.53±1.139.8±6.018.8±3.020.4±6.912.6±1.623.2±3.120.5
ProtoCLR (Ours)199.23±1.638.6±5.118.4±2.321.2±7.315.5±2.325.8±5.221.4
5-Shot Classification
BirdAVES-biox-base9511.6±0.839.7±1.822.5±2.422.1±3.316.1±1.728.3±2.323.3
BirdAVES-bioxn-large31615.0±0.942.6±2.723.7±3.828.4±2.418.3±1.827.3±2.325.8
BioLingual15313.6±1.365.2±1.431.0±2.934.3±3.543.9±0.949.9±2.339.6
Perch8021.2±1.271.7±1.539.5±3.052.5±5.948.0±1.959.7±1.848.7
CE (Ours)2321.4±1.369.2±1.835.6±3.448.2±5.539.9±1.157.5±2.345.3
SimCLR (Ours)1915.4±1.054.0±1.823.0±2.332.8±4.022.0±1.240.7±2.431.3
SupCon (Ours)1917.2±1.364.6±2.434.1±2.942.5±2.930.8±0.848.1±2.439.5
ProtoCLR (Ours)1919.2±1.167.9±2.836.1±4.348.0±4.334.6±2.348.6±2.842.4

For additional details, please see the pre-print on arXiv and the official GitHub repository.

Citation

If you use our model in your research, please cite the following paper:

@misc{moummad2024dirlbs,
      title={Domain-Invariant Representation Learning of Bird Sounds}, 
      author={Ilyass Moummad and Romain Serizel and Emmanouil Benetos and Nicolas Farrugia},
      year={2024},
      eprint={2409.08589},
      archivePrefix={arXiv},
      primaryClass={cs.SD},
      url={https://arxiv.org/abs/2409.08589}, 
}
Bioacoustics
feature-extraction
pytorch

Contributors

ilyassmoummad

32 commits