Feature Extraction
Transformers
Safetensors
bearings
audio
spatial-audio
ambisonics
self-supervised
sound-event-localization
custom_code
Instructions to use labhamlet/bearings with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use labhamlet/bearings with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="labhamlet/bearings", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("labhamlet/bearings", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
Upload Bearings checkpoint
Browse files- model.safetensors +2 -2
- modeling_bearings.py +45 -12
model.safetensors
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:83cffe91cf2c20b3fa59763dc7b3e04d6d991302137d00a3662eba282cf7a0d5
|
| 3 |
+
size 44552864
|
modeling_bearings.py
CHANGED
|
@@ -17,7 +17,6 @@ from typing import Optional
|
|
| 17 |
import torch
|
| 18 |
import torch.nn.functional as F
|
| 19 |
from torch import nn
|
| 20 |
-
from torchaudio.transforms import MelScale, Spectrogram
|
| 21 |
from transformers import PreTrainedModel
|
| 22 |
from transformers.utils import ModelOutput
|
| 23 |
|
|
@@ -30,20 +29,54 @@ from .configuration_bearings import BearingsConfig
|
|
| 30 |
|
| 31 |
class FeatureExtractor(nn.Module):
|
| 32 |
"""Input (B, 4, N) ACN/SN3D FOA -> (B, 7, M, T): log-mel of W, Y, Z, X
|
| 33 |
-
and the per-band energy-normalised active intensity vector.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
def __init__(self, sample_rate, n_fft, win_length, hop_length, f_min, f_max,
|
| 36 |
n_mels, power=2.0):
|
| 37 |
super().__init__()
|
|
|
|
|
|
|
| 38 |
self.power = power
|
| 39 |
self.eps = 1e-6
|
| 40 |
-
self.
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
|
| 48 |
def _intensity_energy(self, spec):
|
| 49 |
W, YZX = spec[:, [0]], spec[:, 1:]
|
|
@@ -52,10 +85,10 @@ class FeatureExtractor(nn.Module):
|
|
| 52 |
return I, E
|
| 53 |
|
| 54 |
def forward(self, audio):
|
| 55 |
-
spec = self.
|
| 56 |
-
logmel = torch.log(self.
|
| 57 |
I, E = self._intensity_energy(spec)
|
| 58 |
-
aiv = self.
|
| 59 |
return torch.cat([logmel, aiv], dim=1) # (B, 7, M, T)
|
| 60 |
|
| 61 |
|
|
|
|
| 17 |
import torch
|
| 18 |
import torch.nn.functional as F
|
| 19 |
from torch import nn
|
|
|
|
| 20 |
from transformers import PreTrainedModel
|
| 21 |
from transformers.utils import ModelOutput
|
| 22 |
|
|
|
|
| 29 |
|
| 30 |
class FeatureExtractor(nn.Module):
|
| 31 |
"""Input (B, 4, N) ACN/SN3D FOA -> (B, 7, M, T): log-mel of W, Y, Z, X
|
| 32 |
+
and the per-band energy-normalised active intensity vector.
|
| 33 |
+
|
| 34 |
+
Plain torch.stft + matmul, equivalent to torchaudio's Spectrogram
|
| 35 |
+
(center, reflect pad, onesided, power=None) followed by MelScale (htk,
|
| 36 |
+
no norm). The Hann window and the mel filterbank are persistent buffers
|
| 37 |
+
shipped in model.safetensors, so nothing is computed at construction
|
| 38 |
+
time: transformers >= 5 builds models on the meta device, where
|
| 39 |
+
torchaudio's filterbank constructor fails.
|
| 40 |
+
"""
|
| 41 |
|
| 42 |
def __init__(self, sample_rate, n_fft, win_length, hop_length, f_min, f_max,
|
| 43 |
n_mels, power=2.0):
|
| 44 |
super().__init__()
|
| 45 |
+
self.sample_rate, self.n_fft, self.win_length = sample_rate, n_fft, win_length
|
| 46 |
+
self.hop_length, self.f_min, self.f_max, self.n_mels = hop_length, f_min, f_max, n_mels
|
| 47 |
self.power = power
|
| 48 |
self.eps = 1e-6
|
| 49 |
+
self.register_buffer("window", torch.zeros(win_length), persistent=True)
|
| 50 |
+
self.register_buffer("fb", torch.zeros(n_fft // 2 + 1, n_mels), persistent=True)
|
| 51 |
+
if self.fb.device.type != "meta":
|
| 52 |
+
self.reset_buffers()
|
| 53 |
+
|
| 54 |
+
@torch.no_grad()
|
| 55 |
+
def reset_buffers(self):
|
| 56 |
+
"""Recompute window and filterbank from the hyper-parameters (same
|
| 57 |
+
values as the shipped buffers; torchaudio htk mel scale)."""
|
| 58 |
+
dev = self.fb.device
|
| 59 |
+
self.window.copy_(torch.hann_window(self.win_length, periodic=True, device=dev))
|
| 60 |
+
n_freqs = self.n_fft // 2 + 1
|
| 61 |
+
all_freqs = torch.linspace(0, self.sample_rate // 2, n_freqs, device=dev)
|
| 62 |
+
hz_to_mel = lambda f: 2595.0 * math.log10(1.0 + f / 700.0)
|
| 63 |
+
m_pts = torch.linspace(hz_to_mel(self.f_min), hz_to_mel(self.f_max), self.n_mels + 2, device=dev)
|
| 64 |
+
f_pts = 700.0 * (10.0 ** (m_pts / 2595.0) - 1.0)
|
| 65 |
+
f_diff = f_pts[1:] - f_pts[:-1]
|
| 66 |
+
slopes = f_pts.unsqueeze(0) - all_freqs.unsqueeze(1) # (n_freqs, n_mels + 2)
|
| 67 |
+
down = (-1.0 * slopes[:, :-2]) / f_diff[:-1]
|
| 68 |
+
up = slopes[:, 2:] / f_diff[1:]
|
| 69 |
+
self.fb.copy_(torch.clamp(torch.min(down, up), min=0.0))
|
| 70 |
+
|
| 71 |
+
def _stft(self, audio):
|
| 72 |
+
B, C, N = audio.shape
|
| 73 |
+
spec = torch.stft(audio.reshape(B * C, N), self.n_fft, self.hop_length, self.win_length,
|
| 74 |
+
self.window, center=True, pad_mode="reflect", normalized=False,
|
| 75 |
+
onesided=True, return_complex=True)
|
| 76 |
+
return spec.reshape(B, C, spec.shape[-2], spec.shape[-1]) # (B, C, F, T)
|
| 77 |
+
|
| 78 |
+
def _mel(self, x): # (B, C, F, T) real
|
| 79 |
+
return torch.matmul(x.transpose(-1, -2), self.fb).transpose(-1, -2)
|
| 80 |
|
| 81 |
def _intensity_energy(self, spec):
|
| 82 |
W, YZX = spec[:, [0]], spec[:, 1:]
|
|
|
|
| 85 |
return I, E
|
| 86 |
|
| 87 |
def forward(self, audio):
|
| 88 |
+
spec = self._stft(audio) # complex (B, 4, F, T)
|
| 89 |
+
logmel = torch.log(self._mel(torch.abs(spec) ** self.power) + self.eps)
|
| 90 |
I, E = self._intensity_energy(spec)
|
| 91 |
+
aiv = self._mel(I) / (self._mel(E) + self.eps) # (B, 3, M, T)
|
| 92 |
return torch.cat([logmel, aiv], dim=1) # (B, 7, M, T)
|
| 93 |
|
| 94 |
|