audio-sed


Nameaudio-sed JSON
Version 0.0.1 PyPI version JSON
download
home_pagehttps://github.com/Shiro-LK/SoundEventDetection
SummaryImplementation of audio SED architecture for tensorflow and pytorch.
upload_time2024-04-07 19:21:53
maintainerNone
docs_urlNone
authorShiro-LK
requires_pythonNone
licenseMIT License
keywords torch tensorflow audio_sed
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Sound Event Detection

This repository implement a class which allow to build classifier for audio signal following the SED (Sound Event Detection) architecture.
The model create as well mel spectrogram and use CNN backbone.
This structure has been used during the birdcall competition which allow me to reach the 3th position in 2021.

This is implemented on pytorch and tensorflow, however the tensorflow version has not been tested for training purpose (only inference). I would advice to use only the pytorch version.

# Installation 

> pip install audio-sed

if you use tensorflow:

> pip install tflibrosa

# Examples

## Pytorch
```{python}
import torch
import timm
from torch import nn
import numpy as np
from audio_sed.sed_config import ConfigSED
from audio_sed.pytorch.sed_models import AudioClassifierSequenceSED, AudioSED, shape_from_backbone

def load_model(model_name, num_classe, cfg_sed):
    backbone = timm.create_model( model_name, pretrained=False)
    if "efficientnet" in   model_name:
        backbone.global_pool =  nn.Identity()
        in_feat = backbone.classifier.in_features
        backbone.classifier = nn.Identity()
    elif "convnext" in cfg.model_name:
        in_feat = backbone.head.fc.in_features
        backbone.head = nn.Identity()

    in_features = shape_from_backbone(inputs=torch.as_tensor(np.random.uniform(0, 1, (1, int(5 * cfg_sed.sample_rate)))).float(), model=backbone, 
                                      use_logmel=True, config_sed = cfg_sed.__dict__)[2] # (batch size, channels, num_steps, y_axis) 
    print("Num timestamps features:",in_features)
    model = AudioSED(backbone, num_classes=[num_classe], in_features=in_feat, hidden_size=1024, activation= 'sigmoid', use_logmel=True, 
                spectrogram_augmentation = None, apply_attention="step", drop_rate = [0.5, 0.5], config_sed= cfg_sed.__dict__)

    model2 = AudioClassifierSequenceSED(model)
    
    return model, model2

cfg_sed =  ConfigSED(window='hann', center=True, pad_mode='reflect', windows_size=1024, hop_size=320,
                sample_rate=32000, mel_bins=128, fmin=50, fmax=16000, ref=1.0, amin=1e-10, top_db=None)

model_5, model = load_model(model_name="tf_efficientnet_b2_ns", num_classe=575, cfg_sed=cfg_sed)
inputs = torch.as_tensor(np.random.uniform(0,1, (20*32000)).reshape(1,4,-1)).float()
with torch.no_grad():
    o = model(inputs)
print(o[0]['clipwise'])
```

## Tensorflow 

```{python}
import tensorflow as tf
from audio_sed.sed_config import ConfigSED
from audio_sed.tensorflow.sed_models import AudioClassifierSequenceSED as AudioClassifierSequenceSEDTF, AudioSED as AudioSEDTF, shape_from_backbone as shape_from_backboneTF



def load_modeltf(model_name, num_classe, cfg_sed):
    backbone =tf.keras.applications.efficientnet.EfficientNetB2(
    include_top=False)   
    if "efficientnet" in   model_name:
        in_feat = backbone.layers[-1].output.shape[-1] 
    elif "convnext" in cfg.model_name:
        in_feat = backbone.layers[-1].output.shape[-1]
    # batch size, num_steps, y_axis, channels
    in_features = shape_from_backboneTF(inputs=np.random.uniform(0, 1, (1, int(5 * cfg_sed.sample_rate))), model=backbone, use_logmel=True, config_sed = cfg_sed.__dict__)[1]
    print("Num timestamps features:",in_features)
    model = AudioSEDTF(backbone, num_classes=[num_classe], in_features=in_feat, hidden_size=1024, activation= 'sigmoid', use_logmel=True, 
                spectrogram_augmentation = None, apply_attention="step", drop_rate = [0.5, 0.5], config_sed= cfg_sed.__dict__)

    model = AudioClassifierSequenceSEDTF(model)
    
    return model

cfg_sed =  ConfigSED(window='hann', center=True, pad_mode='reflect', windows_size=1024, hop_size=320,
                sample_rate=32000, mel_bins=128, fmin=50, fmax=16000, ref=1.0, amin=1e-10, top_db=None)

inputs = np.random.uniform(0,1, (1, 4, 5*32000))
o_tf = model_tf.predict(inputs)
print(o_tf[0])

```

# Examples 2:

You can find [here](https://github.com/Shiro-LK/Portfolio-project/tree/main/BirdsCall_Detection) an example of how this model is used with a GUI for inference with some checkpoint available.



# Citation 

- [PANNs: Large-Scale Pretrained Audio Neural Networks for Audio Pattern Recognition](https://arxiv.org/abs/1912.10211)
- PANNs model: https://github.com/qiuqiangkong/audioset_tagging_cnn
- https://github.com/Shiro-LK/tflibrosa


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/Shiro-LK/SoundEventDetection",
    "name": "audio-sed",
    "maintainer": null,
    "docs_url": null,
    "requires_python": null,
    "maintainer_email": null,
    "keywords": "torch, tensorflow, audio_sed",
    "author": "Shiro-LK",
    "author_email": "shirosaki94@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/a3/82/ff3fc05daaa4b381091a166a75e37e44b5008202a61b97fe32d065eb82ff/audio_sed-0.0.1.tar.gz",
    "platform": null,
    "description": "# Sound Event Detection\n\nThis repository implement a class which allow to build classifier for audio signal following the SED (Sound Event Detection) architecture.\nThe model create as well mel spectrogram and use CNN backbone.\nThis structure has been used during the birdcall competition which allow me to reach the 3th position in 2021.\n\nThis is implemented on pytorch and tensorflow, however the tensorflow version has not been tested for training purpose (only inference). I would advice to use only the pytorch version.\n\n# Installation \n\n> pip install audio-sed\n\nif you use tensorflow:\n\n> pip install tflibrosa\n\n# Examples\n\n## Pytorch\n```{python}\nimport torch\nimport timm\nfrom torch import nn\nimport numpy as np\nfrom audio_sed.sed_config import ConfigSED\nfrom audio_sed.pytorch.sed_models import AudioClassifierSequenceSED, AudioSED, shape_from_backbone\n\ndef load_model(model_name, num_classe, cfg_sed):\n    backbone = timm.create_model( model_name, pretrained=False)\n    if \"efficientnet\" in   model_name:\n        backbone.global_pool =  nn.Identity()\n        in_feat = backbone.classifier.in_features\n        backbone.classifier = nn.Identity()\n    elif \"convnext\" in cfg.model_name:\n        in_feat = backbone.head.fc.in_features\n        backbone.head = nn.Identity()\n\n    in_features = shape_from_backbone(inputs=torch.as_tensor(np.random.uniform(0, 1, (1, int(5 * cfg_sed.sample_rate)))).float(), model=backbone, \n                                      use_logmel=True, config_sed = cfg_sed.__dict__)[2] # (batch size, channels, num_steps, y_axis) \n    print(\"Num timestamps features:\",in_features)\n    model = AudioSED(backbone, num_classes=[num_classe], in_features=in_feat, hidden_size=1024, activation= 'sigmoid', use_logmel=True, \n                spectrogram_augmentation = None, apply_attention=\"step\", drop_rate = [0.5, 0.5], config_sed= cfg_sed.__dict__)\n\n    model2 = AudioClassifierSequenceSED(model)\n    \n    return model, model2\n\ncfg_sed =  ConfigSED(window='hann', center=True, pad_mode='reflect', windows_size=1024, hop_size=320,\n                sample_rate=32000, mel_bins=128, fmin=50, fmax=16000, ref=1.0, amin=1e-10, top_db=None)\n\nmodel_5, model = load_model(model_name=\"tf_efficientnet_b2_ns\", num_classe=575, cfg_sed=cfg_sed)\ninputs = torch.as_tensor(np.random.uniform(0,1, (20*32000)).reshape(1,4,-1)).float()\nwith torch.no_grad():\n    o = model(inputs)\nprint(o[0]['clipwise'])\n```\n\n## Tensorflow \n\n```{python}\nimport tensorflow as tf\nfrom audio_sed.sed_config import ConfigSED\nfrom audio_sed.tensorflow.sed_models import AudioClassifierSequenceSED as AudioClassifierSequenceSEDTF, AudioSED as AudioSEDTF, shape_from_backbone as shape_from_backboneTF\n\n\n\ndef load_modeltf(model_name, num_classe, cfg_sed):\n    backbone =tf.keras.applications.efficientnet.EfficientNetB2(\n    include_top=False)   \n    if \"efficientnet\" in   model_name:\n        in_feat = backbone.layers[-1].output.shape[-1] \n    elif \"convnext\" in cfg.model_name:\n        in_feat = backbone.layers[-1].output.shape[-1]\n    # batch size, num_steps, y_axis, channels\n    in_features = shape_from_backboneTF(inputs=np.random.uniform(0, 1, (1, int(5 * cfg_sed.sample_rate))), model=backbone, use_logmel=True, config_sed = cfg_sed.__dict__)[1]\n    print(\"Num timestamps features:\",in_features)\n    model = AudioSEDTF(backbone, num_classes=[num_classe], in_features=in_feat, hidden_size=1024, activation= 'sigmoid', use_logmel=True, \n                spectrogram_augmentation = None, apply_attention=\"step\", drop_rate = [0.5, 0.5], config_sed= cfg_sed.__dict__)\n\n    model = AudioClassifierSequenceSEDTF(model)\n    \n    return model\n\ncfg_sed =  ConfigSED(window='hann', center=True, pad_mode='reflect', windows_size=1024, hop_size=320,\n                sample_rate=32000, mel_bins=128, fmin=50, fmax=16000, ref=1.0, amin=1e-10, top_db=None)\n\ninputs = np.random.uniform(0,1, (1, 4, 5*32000))\no_tf = model_tf.predict(inputs)\nprint(o_tf[0])\n\n```\n\n# Examples 2:\n\nYou can find [here](https://github.com/Shiro-LK/Portfolio-project/tree/main/BirdsCall_Detection) an example of how this model is used with a GUI for inference with some checkpoint available.\n\n\n\n# Citation \n\n- [PANNs: Large-Scale Pretrained Audio Neural Networks for Audio Pattern Recognition](https://arxiv.org/abs/1912.10211)\n- PANNs model: https://github.com/qiuqiangkong/audioset_tagging_cnn\n- https://github.com/Shiro-LK/tflibrosa\n\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "Implementation of audio SED architecture for tensorflow and pytorch.",
    "version": "0.0.1",
    "project_urls": {
        "Download": "https://github.com/Shiro-LK/SoundEventDetection.git",
        "Homepage": "https://github.com/Shiro-LK/SoundEventDetection"
    },
    "split_keywords": [
        "torch",
        " tensorflow",
        " audio_sed"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c68831edf8c8fade92f0f4042235e7e54bc330706952c899207786601a0676ee",
                "md5": "42d6aac15f4b5c1816701b9ce73901fc",
                "sha256": "2edf4805b9cd876a1515e173250b00b53c6b3f2498eeab012798823d45296085"
            },
            "downloads": -1,
            "filename": "audio_sed-0.0.1-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "42d6aac15f4b5c1816701b9ce73901fc",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 12239,
            "upload_time": "2024-04-07T19:21:52",
            "upload_time_iso_8601": "2024-04-07T19:21:52.040995Z",
            "url": "https://files.pythonhosted.org/packages/c6/88/31edf8c8fade92f0f4042235e7e54bc330706952c899207786601a0676ee/audio_sed-0.0.1-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "a382ff3fc05daaa4b381091a166a75e37e44b5008202a61b97fe32d065eb82ff",
                "md5": "b91a15427772bdd18118fc71a91821b1",
                "sha256": "003fb115bb0eefaa8ff8aa9bf4db6b4363dadc6dfba65dec4dcaa0b3104e4e02"
            },
            "downloads": -1,
            "filename": "audio_sed-0.0.1.tar.gz",
            "has_sig": false,
            "md5_digest": "b91a15427772bdd18118fc71a91821b1",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 9970,
            "upload_time": "2024-04-07T19:21:53",
            "upload_time_iso_8601": "2024-04-07T19:21:53.356081Z",
            "url": "https://files.pythonhosted.org/packages/a3/82/ff3fc05daaa4b381091a166a75e37e44b5008202a61b97fe32d065eb82ff/audio_sed-0.0.1.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-04-07 19:21:53",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "Shiro-LK",
    "github_project": "SoundEventDetection",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "lcname": "audio-sed"
}
        
Elapsed time: 0.23738s