CoolFace
Modelpublic

m3hrdadfi/wav2vec2-base-100k-eating-sound-collection

sourceHugging Faceupdated 5y agoView on Hugging Face
0likes9downloads
Model Card

Eating Sound Classification using Wav2Vec 2.0

How to use

Requirements

bash
# requirement packages
!pip install git+https://github.com/huggingface/datasets.git
!pip install git+https://github.com/huggingface/transformers.git
!pip install torchaudio
!pip install librosa

Prediction

python
import torch
import torch.nn as nn
import torch.nn.functional as F
import torchaudio
from transformers import AutoConfig, Wav2Vec2FeatureExtractor

import librosa
import IPython.display as ipd
import numpy as np
import pandas as pd
python
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model_name_or_path = "m3hrdadfi/wav2vec2-base-100k-eating-sound-collection"
config = AutoConfig.from_pretrained(model_name_or_path)
feature_extractor = Wav2Vec2FeatureExtractor.from_pretrained(model_name_or_path)
sampling_rate = feature_extractor.sampling_rate
model = Wav2Vec2ForSpeechClassification.from_pretrained(model_name_or_path).to(device)
python
def speech_file_to_array_fn(path, sampling_rate):
    speech_array, _sampling_rate = torchaudio.load(path)
    resampler = torchaudio.transforms.Resample(_sampling_rate)
    speech = resampler(speech_array).squeeze().numpy()
    return speech


def predict(path, sampling_rate):
    speech = speech_file_to_array_fn(path, sampling_rate)
    inputs = feature_extractor(speech, sampling_rate=sampling_rate, return_tensors="pt", padding=True)
    inputs = {key: inputs[key].to(device) for key in inputs}

    with torch.no_grad():
        logits = model(**inputs).logits

    scores = F.softmax(logits, dim=1).detach().cpu().numpy()[0]
    outputs = [{"Label": config.id2label[i], "Score": f"{round(score * 100, 3):.1f}%"} for i, score in enumerate(scores)]
    return outputs
python
path = "clips_rd/gummies/gummies_6_04.wav"
outputs = predict(path, sampling_rate)
bash
[
{'Label': 'aloe', 'Score': '0.0%'},
{'Label': 'burger', 'Score': '0.0%'},
{'Label': 'cabbage', 'Score': '0.0%'},
{'Label': 'candied_fruits', 'Score': '0.0%'},
{'Label': 'carrots', 'Score': '0.0%'},
{'Label': 'chips', 'Score': '0.0%'},
{'Label': 'chocolate', 'Score': '0.0%'},
{'Label': 'drinks', 'Score': '0.0%'},
{'Label': 'fries', 'Score': '0.0%'},
{'Label': 'grapes', 'Score': '0.0%'},
{'Label': 'gummies', 'Score': '99.8%'},
{'Label': 'ice-cream', 'Score': '0.0%'},
{'Label': 'jelly', 'Score': '0.1%'},
{'Label': 'noodles', 'Score': '0.0%'},
{'Label': 'pickles', 'Score': '0.0%'},
{'Label': 'pizza', 'Score': '0.0%'},
{'Label': 'ribs', 'Score': '0.0%'},
{'Label': 'salmon', 'Score': '0.0%'},
{'Label': 'soup', 'Score': '0.0%'},
{'Label': 'wings', 'Score': '0.0%'}
]

Evaluation

The following tables summarize the scores obtained by model overall and per each class.

labelprecisionrecallf1-scoresupport
aloe0.9890.8070.889109
burger1.0000.4710.640119
cabbage0.9070.9700.937100
candied_fruits0.9520.9880.970161
carrots0.9700.9920.981132
chips0.9930.9510.972144
chocolate0.8280.9140.86958
drinks0.9820.9480.96558
fries0.9350.7830.852129
grapes0.9650.9400.952116
gummies0.8800.9710.923136
ice-cream0.9530.9720.962145
jelly0.9060.8750.89088
noodles0.8170.8170.81782
pickles0.9330.9600.946174
pizza0.7040.9340.803122
ribs0.7960.7550.77598
salmon0.6470.9700.776100
soup0.9410.8570.89756
wings0.8420.7920.816101
accuracy0.8900.8900.8900
macro avg0.8970.8830.8822228
weighted avg0.9030.8900.8882228

Questions?

Post a Github issue from HERE.