musicnn

repository·master·Indexed 20 days ago

https://github.com/jordipons/musicnn

A set of pre-trained, musically motivated convolutional neural networks for music audio tagging. It enables developers to predict musical tags and extract 'taggrams' (temporal tag evolutions) from audio files. The library includes models trained on the MagnaTagATune (MTT) and Million Song Dataset (MSD), providing both musicnn-specific architectures and VGG baselines. It features a Python API and a command-line interface for tag prediction and intermediate feature extraction.

Tokens
4.8K
Snippets
16
Records
23
Agent score
67%

What's inside musicnn

  1. Available musicnn and vgg models

    master

    The library provides several pre-trained models based on two different training datasets: MTT (MagnaTagATune) and MSD (Million Song Dataset).

    musicnn models

    These are musically motivated CNNs with a dense mid-end and temporal-pooling back-end.

    • 'MTT_musicnn': Trained on the MTT dataset.
    • 'MSD_musicnn': Trained on the MSD dataset.
    • 'MSD_musicnn_big': A larger version of the MSD model with more capacity (x512 filters in the mid-end and 500 units in the back-end). Requires installation from source (python setup.py install).

    vgg models

    These are baseline models adapted from computer vision CNNs (VGG architecture) trained on audio-spectrograms.

    • 'MTT_vgg': Trained on the MTT dataset.
    • 'MSD_vgg': Trained on the MSD dataset.
    • Note: vgg models do not support custom input_length; they must be set to 3.
  2. Understand the supported audio formats and codecs

    master

    The musicnn library uses librosa for audio reading. librosa primarily relies on soundfile, falling back to audioread for codecs not supported by soundfile (such as MP3 and certain WAV variants).

    To ensure compatibility, verify that your audio files use codecs supported by libsndfile.

  3. Identify best layers for transfer learning

    master

    When performing transfer learning with musicnn, the following layers are recommended based on preliminary experiments and visualizations:

    • taggram: Provides high-level music information.
    • max_pool: Provides a relatively sparse acoustic representation of the music audio.
  4. Install musicnn

    master

    You can install musicnn via pip for standard usage. If you require larger models or want to access the full documentation and Jupyter notebooks, install directly from the source repository.

    pip install musicnn

    To install from source for larger models and documentation:

    git clone https://github.com/jordipons/musicnn.git
    python setup.py install
  5. Understand the musicnn model architecture and feature layers

    master

    The musicnn model is composed of three main parts, each providing different levels of feature extraction:

    1. Front-end (Musically motivated CNN): Processes log-mel spectrograms. It produces timbral features (using vertical filters for timbral traces) and temporal features (using horizontal filters for temporal dependencies). These can be concatenated to form front-end features.
    2. Mid-end (Dense layers): Extracts higher-level representations from front-end features using residual and dense connections. This produces layers like cnn1, cnn2, and cnn3.
    3. Back-end (Temporal-pooling): Summarizes temporal content into fixed-size representations to predict tags. This includes pooling layers like mean_pool and max_pool, and the penultimate layer.

    Note that while front-end and mid-end features typically have frame-level temporal resolution, the back-end features (due to temporal pooling) have a temporal resolution corresponding to the model's input window (default is 3 seconds).

  6. Compute song-level tag likelihood from a Taggram

    master

    A Taggram represents the temporal evolution of tag likelihoods. To derive a single song-level likelihood for each tag, you can average the Taggram values across the time dimension using numpy.mean.

    import numpy as np
    
    # Assuming taggram is the output from musicnn.extractor.extractor
    # averaging the Taggram through time
    tags_likelihood_mean = np.mean(taggram, axis=0)
  7. Understand the structure of VGG features

    master

    When extract_features=True is used, the features dictionary contains tensors representing the output of different layers.

    Tensor Dimensions

    Most features are 3D tensors with dimensions: (time, frequency, #filters).

    Layer Specifics

    • pool1 through pool4: These are 3D tensors. For example, features['pool1'].shape might return (time, frequency, filters).
    • pool5: This layer is unique because the max-pooling operators have summarized the frequency content into a single value. Instead of a 3D tensor, musicnn returns a 2D representation with dimensions: (time, #filters).

    Model Architecture Notes

    • All CNN layers use same padding.
    • All max-pooling layers use a stride of 2x2, except for the last max-pooling layer which uses a stride of 4x4.
  8. Optimize model performance by adjusting batch size

    master

    If your model is running slowly even when using a GPU, you can increase the processing speed by setting a larger batch size. By default, the batch size is set to 1 in ./musicnn/configuration.py to ensure computational safety, but it can be increased for better performance.

    # In ./musicnn/configuration.py
    BATCH_SIZE = 1  # Increase this value to improve speed
  9. Predict top music tags with musicnn.tagger.top_tags()

    master

    Use musicnn.tagger.top_tags() to predict the most likely musical tags for a given audio file. This is useful for high-level classification of music clips.

    Parameters

    • file_name (string): Path to the music file.
    • model (string): The model to use. Options include 'MTT_musicnn', 'MTT_vgg', 'MSD_musicnn', 'MSD_musicnn_big', or 'MSD_vgg'.
      • Note: 'MSD_musicnn_big' requires installation from source via python setup.py install.
    • topN (integer): The number of most likely tags to return.
    • input_length (float): Length in seconds of the input spectrogram patches. Recommended value is 3.
      • Note: vgg models require input_length to be exactly 3.
    • input_overlap (float): Amount of overlap in seconds between patches.
    • print_tags (boolean): If True, prints the tags to the console.
    • save_tags (string): Path where to store/save the tags.

    Returns

    • tags (list): A list of the topN most likely tags.
    tags = musicnn.tagger.top_tags(file_name, model='MTT_musicnn', topN=3, input_length=3, input_overlap=None, print_tags=True, save_tags=False)
  10. Extract taggrams and features with musicnn.extractor.extractor()

    master

    Use musicnn.extractor.extractor() to obtain the taggram (the temporal evolution of tag likelihoods) and the intermediate model features (activations of different layers) for a music clip.

    Parameters

    • file_name (string): Path to the music file.
    • model (string): The model to use. Options include 'MTT_musicnn', 'MTT_vgg', 'MSD_musicnn', 'MSD_musicnn_big', or 'MSD_vgg'.
      • Note: 'MSD_musicnn_big' requires installation from source via python setup.py install.
    • input_length (float): Length in seconds of the input spectrogram patches. Recommended value is 3.
      • Note: vgg models require input_length to be exactly 3.
    • input_overlap (float): Amount of overlap in seconds between patches.
    • extract_features (boolean): If True, extracts the intermediate representations of the model.

    Returns

    • taggram (2D np.ndarray): A matrix of shape (time, tags) expressing the temporal evolution of tag likelihoods.
    • tags (list): The list of tags corresponding to the indices in the taggram.
    • features (dictionary): If extract_features=True, a dictionary containing layer activations.
      • Keys for musicnn models: ['timbral', 'temporal', 'cnn1', 'cnn2', 'cnn3', 'mean_pool', 'max_pool', 'penultimate']
      • Keys for vgg models: ['pool1', 'pool2', 'pool3', 'pool4', 'pool5']
    taggram, tags, features = musicnn.extractor.extractor(file_name, model='MTT_musicnn', input_length=3, input_overlap=None, extract_features=True)
  11. Extract the Taggram using Python

    master

    The Taggram represents the temporal evolution of musical tags. You can compute it using the extractor function from musicnn.extractor. This returns both the taggram data and the list of tags used.

    from musicnn.extractor import extractor
    
    taggram, tags = extractor('./audio/example.mp3', model='MTT_musicnn')
  12. Predict top tags using Python

    master

    Use the top_tags function from musicnn.tagger to estimate the most prominent musical tags for an audio file. You can specify the model and the number of tags (topN) to return.

    from musicnn.tagger import top_tags
    
    # Predict top 10 tags using the 'MTT_musicnn' model
    tags = top_tags('./audio/example.mp3', model='MTT_musicnn', topN=10)
    print(tags)