Feature Extraction
Transformers
PyTorch
hear_canon_vit
audio
medical
embeddings
vision-transformer
distillation
canon
custom_code
Instructions to use matthewagi/HeAR-s with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use matthewagi/HeAR-s with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("feature-extraction", model="matthewagi/HeAR-s", trust_remote_code=True)# Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("matthewagi/HeAR-s", trust_remote_code=True, dtype="auto") - Notebooks
- Google Colab
- Kaggle
Switch to student-only 384D embeddings and update model card
Browse files- README.md +40 -48
- config.json +2 -2
- configuration_hear_canon.py +2 -2
- model_shapes.json +2 -14
- modeling_hear_canon.py +2 -4
- pytorch_model.bin +2 -2
- smoke_test.py +1 -1
README.md
CHANGED
|
@@ -13,37 +13,50 @@ tags:
|
|
| 13 |
- canon
|
| 14 |
---
|
| 15 |
|
| 16 |
-
# Distilled HeAR ViT-S Canon
|
| 17 |
|
| 18 |
-
|
| 19 |
|
| 20 |
-
|
| 21 |
-
- Input shape: `[B, 1, 192, 128]` mel+PCEN spectrograms from 2 s audio at 16 kHz
|
| 22 |
-
- Canon configuration: A/B/C/D enabled, 2D Canon (`kernel=4`), no positional encodings
|
| 23 |
-
- Output embedding: `pooler_output` with shape `[B, 512]`
|
| 24 |
|
| 25 |
-
This
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
## Files in this package
|
| 28 |
|
| 29 |
-
- `config.json`: model config
|
| 30 |
- `configuration_hear_canon.py`: custom `PretrainedConfig`
|
| 31 |
-
- `modeling_hear_canon.py`: custom `PreTrainedModel` with
|
| 32 |
-
- `pytorch_model.bin`: distilled student
|
| 33 |
- `preprocessor_config.json`: preprocessing metadata
|
| 34 |
-
- `model_shapes.json`:
|
| 35 |
-
- `training_args.json`:
|
| 36 |
-
- `.gitattributes`: LFS
|
| 37 |
- `smoke_test.py`: local verification script
|
| 38 |
|
| 39 |
-
##
|
|
|
|
|
|
|
| 40 |
|
| 41 |
```bash
|
| 42 |
pip install -U "transformers>=4.50.0" timm torch scipy soundfile
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
python3 trained_model_hf_upload/smoke_test.py
|
| 44 |
```
|
| 45 |
|
| 46 |
-
## Inference from raw audio waveform
|
| 47 |
|
| 48 |
```python
|
| 49 |
import torch
|
|
@@ -62,10 +75,10 @@ with torch.inference_mode():
|
|
| 62 |
out = model(input_values=raw_audio_batch, return_dict=True)
|
| 63 |
|
| 64 |
embeddings = out.pooler_output
|
| 65 |
-
print(embeddings.shape) # torch.Size([4,
|
| 66 |
```
|
| 67 |
|
| 68 |
-
## Inference from `.wav` file
|
| 69 |
|
| 70 |
```python
|
| 71 |
import torch
|
|
@@ -92,10 +105,10 @@ waveform = load_wav_mono_16k("example.wav")
|
|
| 92 |
with torch.inference_mode():
|
| 93 |
embedding = model.embed_audio(waveform)
|
| 94 |
|
| 95 |
-
print(embedding.shape) # torch.Size([1,
|
| 96 |
```
|
| 97 |
|
| 98 |
-
## Inference from preprocessed spectrograms
|
| 99 |
|
| 100 |
```python
|
| 101 |
import torch
|
|
@@ -110,36 +123,15 @@ spectrogram = model.preprocess_audio(raw_audio)
|
|
| 110 |
with torch.inference_mode():
|
| 111 |
out = model(pixel_values=spectrogram, return_dict=True)
|
| 112 |
|
| 113 |
-
print(spectrogram.shape)
|
| 114 |
-
print(out.pooler_output.shape)
|
| 115 |
-
```
|
| 116 |
-
|
| 117 |
-
## After uploading to Hugging Face
|
| 118 |
-
|
| 119 |
-
Replace the local path with your Hub repo id:
|
| 120 |
-
|
| 121 |
-
```python
|
| 122 |
-
from transformers import AutoModel
|
| 123 |
-
|
| 124 |
-
model = AutoModel.from_pretrained(
|
| 125 |
-
"<your-org>/<your-repo>",
|
| 126 |
-
trust_remote_code=True,
|
| 127 |
-
)
|
| 128 |
-
```
|
| 129 |
-
|
| 130 |
-
Upload example:
|
| 131 |
-
|
| 132 |
-
```bash
|
| 133 |
-
huggingface-cli repo create <your-repo> --type model
|
| 134 |
-
huggingface-cli upload <your-org>/<your-repo> trained_model_hf_upload .
|
| 135 |
```
|
| 136 |
|
| 137 |
-
##
|
| 138 |
|
| 139 |
-
- Student parameters: `22,140,288`
|
| 140 |
-
-
|
| 141 |
-
-
|
| 142 |
-
-
|
| 143 |
-
- Pooler output size: `512`
|
| 144 |
|
| 145 |
-
Detailed tensor shapes are
|
|
|
|
| 13 |
- canon
|
| 14 |
---
|
| 15 |
|
| 16 |
+
# Distilled HeAR ViT-S Canon model card
|
| 17 |
|
| 18 |
+
**Model documentation:** HeAR (Google Health Acoustic Representations)
|
| 19 |
|
| 20 |
+
## Model information
|
|
|
|
|
|
|
|
|
|
| 21 |
|
| 22 |
+
This package contains a distilled HeAR student model implemented in PyTorch with a ViT-S backbone and Canon layers.
|
| 23 |
+
|
| 24 |
+
### Description
|
| 25 |
+
|
| 26 |
+
The model is built for health-acoustic embedding extraction from short audio clips.
|
| 27 |
+
|
| 28 |
+
- Backbone: ViT-S (`vit_small_patch16_224`)
|
| 29 |
+
- Input: single-channel mel+PCEN spectrograms (`[B, 1, 192, 128]`) generated from 2-second audio clips at 16 kHz
|
| 30 |
+
- Canon setup: A/B/C/D enabled, 2D Canon, kernel size 4, positional encodings disabled
|
| 31 |
+
- Output embedding: `pooler_output` with shape `[B, 384]`
|
| 32 |
|
| 33 |
## Files in this package
|
| 34 |
|
| 35 |
+
- `config.json`: model config and `auto_map`
|
| 36 |
- `configuration_hear_canon.py`: custom `PretrainedConfig`
|
| 37 |
+
- `modeling_hear_canon.py`: custom `PreTrainedModel` with integrated audio preprocessing
|
| 38 |
+
- `pytorch_model.bin`: distilled student weights
|
| 39 |
- `preprocessor_config.json`: preprocessing metadata
|
| 40 |
+
- `model_shapes.json`: structure and tensor shape inventory
|
| 41 |
+
- `training_args.json`: training/checkpoint args captured from the source checkpoint
|
| 42 |
+
- `.gitattributes`: git/LFS attributes for model artifacts
|
| 43 |
- `smoke_test.py`: local verification script
|
| 44 |
|
| 45 |
+
## How to use
|
| 46 |
+
|
| 47 |
+
Install dependencies:
|
| 48 |
|
| 49 |
```bash
|
| 50 |
pip install -U "transformers>=4.50.0" timm torch scipy soundfile
|
| 51 |
+
```
|
| 52 |
+
|
| 53 |
+
Run local smoke test:
|
| 54 |
+
|
| 55 |
+
```bash
|
| 56 |
python3 trained_model_hf_upload/smoke_test.py
|
| 57 |
```
|
| 58 |
|
| 59 |
+
### Inference from raw audio waveform
|
| 60 |
|
| 61 |
```python
|
| 62 |
import torch
|
|
|
|
| 75 |
out = model(input_values=raw_audio_batch, return_dict=True)
|
| 76 |
|
| 77 |
embeddings = out.pooler_output
|
| 78 |
+
print(embeddings.shape) # torch.Size([4, 384])
|
| 79 |
```
|
| 80 |
|
| 81 |
+
### Inference from `.wav` file
|
| 82 |
|
| 83 |
```python
|
| 84 |
import torch
|
|
|
|
| 105 |
with torch.inference_mode():
|
| 106 |
embedding = model.embed_audio(waveform)
|
| 107 |
|
| 108 |
+
print(embedding.shape) # torch.Size([1, 384])
|
| 109 |
```
|
| 110 |
|
| 111 |
+
### Inference from preprocessed spectrograms
|
| 112 |
|
| 113 |
```python
|
| 114 |
import torch
|
|
|
|
| 123 |
with torch.inference_mode():
|
| 124 |
out = model(pixel_values=spectrogram, return_dict=True)
|
| 125 |
|
| 126 |
+
print(spectrogram.shape) # torch.Size([2, 1, 192, 128])
|
| 127 |
+
print(out.pooler_output.shape) # torch.Size([2, 384])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
```
|
| 129 |
|
| 130 |
+
## Model architecture overview
|
| 131 |
|
| 132 |
+
- Student model parameters: `22,140,288`
|
| 133 |
+
- Embedding dimension: `384`
|
| 134 |
+
- Input shape: `[B, 1, 192, 128]`
|
| 135 |
+
- Output shape: `[B, 384]`
|
|
|
|
| 136 |
|
| 137 |
+
Detailed tensor shapes are provided in `model_shapes.json`.
|
config.json
CHANGED
|
@@ -33,8 +33,8 @@
|
|
| 33 |
"num_channels": 1,
|
| 34 |
"num_hidden_layers": 12,
|
| 35 |
"patch_size": 16,
|
| 36 |
-
"pooled_dim":
|
| 37 |
-
"pooler_output_size":
|
| 38 |
"sample_rate": 16000,
|
| 39 |
"timm_model_name": "vit_small_patch16_224",
|
| 40 |
"torch_dtype": "float32",
|
|
|
|
| 33 |
"num_channels": 1,
|
| 34 |
"num_hidden_layers": 12,
|
| 35 |
"patch_size": 16,
|
| 36 |
+
"pooled_dim": 384,
|
| 37 |
+
"pooler_output_size": 384,
|
| 38 |
"sample_rate": 16000,
|
| 39 |
"timm_model_name": "vit_small_patch16_224",
|
| 40 |
"torch_dtype": "float32",
|
configuration_hear_canon.py
CHANGED
|
@@ -22,8 +22,8 @@ class HearCanonViTConfig(PretrainedConfig):
|
|
| 22 |
num_hidden_layers: int = 12,
|
| 23 |
num_attention_heads: int = 6,
|
| 24 |
intermediate_size: int = 1536,
|
| 25 |
-
pooled_dim: int =
|
| 26 |
-
pooler_output_size: int =
|
| 27 |
hidden_act: str = "gelu",
|
| 28 |
layer_norm_eps: float = 1e-6,
|
| 29 |
sample_rate: int = 16000,
|
|
|
|
| 22 |
num_hidden_layers: int = 12,
|
| 23 |
num_attention_heads: int = 6,
|
| 24 |
intermediate_size: int = 1536,
|
| 25 |
+
pooled_dim: int = 384,
|
| 26 |
+
pooler_output_size: int = 384,
|
| 27 |
hidden_act: str = "gelu",
|
| 28 |
layer_norm_eps: float = 1e-6,
|
| 29 |
sample_rate: int = 16000,
|
model_shapes.json
CHANGED
|
@@ -1,5 +1,6 @@
|
|
| 1 |
{
|
| 2 |
"checkpoint_source": "/home/matt/Documents/HeAR/trained_model.pt",
|
|
|
|
| 3 |
"exported_weights_file": "pytorch_model.bin",
|
| 4 |
"input_spectrogram_shape": [
|
| 5 |
"B",
|
|
@@ -11,19 +12,6 @@
|
|
| 11 |
"B",
|
| 12 |
32000
|
| 13 |
],
|
| 14 |
-
"pooler_output_size": 512,
|
| 15 |
-
"projection_parameters": 197120,
|
| 16 |
-
"projection_state_dict_shapes": {
|
| 17 |
-
"bias": [
|
| 18 |
-
512
|
| 19 |
-
],
|
| 20 |
-
"weight": [
|
| 21 |
-
512,
|
| 22 |
-
384
|
| 23 |
-
]
|
| 24 |
-
},
|
| 25 |
-
"projection_state_dict_size": 2,
|
| 26 |
-
"student_hidden_size": 384,
|
| 27 |
"student_parameters": 22140288,
|
| 28 |
"student_state_dict_shapes": {
|
| 29 |
"blocks.0.block.attn.canon._fallback.conv.bias": [
|
|
@@ -1349,5 +1337,5 @@
|
|
| 1349 |
]
|
| 1350 |
},
|
| 1351 |
"student_state_dict_size": 342,
|
| 1352 |
-
"total_parameters":
|
| 1353 |
}
|
|
|
|
| 1 |
{
|
| 2 |
"checkpoint_source": "/home/matt/Documents/HeAR/trained_model.pt",
|
| 3 |
+
"embedding_dimension": 384,
|
| 4 |
"exported_weights_file": "pytorch_model.bin",
|
| 5 |
"input_spectrogram_shape": [
|
| 6 |
"B",
|
|
|
|
| 12 |
"B",
|
| 13 |
32000
|
| 14 |
],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 15 |
"student_parameters": 22140288,
|
| 16 |
"student_state_dict_shapes": {
|
| 17 |
"blocks.0.block.attn.canon._fallback.conv.bias": [
|
|
|
|
| 1337 |
]
|
| 1338 |
},
|
| 1339 |
"student_state_dict_size": 342,
|
| 1340 |
+
"total_parameters": 22140288
|
| 1341 |
}
|
modeling_hear_canon.py
CHANGED
|
@@ -591,7 +591,7 @@ def _student_features(feats: torch.Tensor) -> torch.Tensor:
|
|
| 591 |
|
| 592 |
|
| 593 |
class HearCanonViTModel(PreTrainedModel):
|
| 594 |
-
"""Distilled HeAR ViT-S model with Canon layers and
|
| 595 |
|
| 596 |
config_class = HearCanonViTConfig
|
| 597 |
base_model_prefix = "student"
|
|
@@ -600,7 +600,6 @@ class HearCanonViTModel(PreTrainedModel):
|
|
| 600 |
def __init__(self, config: HearCanonViTConfig):
|
| 601 |
super().__init__(config)
|
| 602 |
self.student = _build_student(config)
|
| 603 |
-
self.proj = nn.Linear(int(config.hidden_size), int(config.pooler_output_size))
|
| 604 |
self.post_init()
|
| 605 |
|
| 606 |
def preprocess_audio(self, audio: torch.Tensor) -> torch.Tensor:
|
|
@@ -678,8 +677,7 @@ class HearCanonViTModel(PreTrainedModel):
|
|
| 678 |
if isinstance(feats, (list, tuple)):
|
| 679 |
feats = feats[-1]
|
| 680 |
|
| 681 |
-
|
| 682 |
-
pooler_output = self.proj(pooled_student)
|
| 683 |
|
| 684 |
if feats.ndim == 2:
|
| 685 |
last_hidden_state = feats.unsqueeze(1)
|
|
|
|
| 591 |
|
| 592 |
|
| 593 |
class HearCanonViTModel(PreTrainedModel):
|
| 594 |
+
"""Distilled HeAR ViT-S model with Canon layers and 384-D student embeddings."""
|
| 595 |
|
| 596 |
config_class = HearCanonViTConfig
|
| 597 |
base_model_prefix = "student"
|
|
|
|
| 600 |
def __init__(self, config: HearCanonViTConfig):
|
| 601 |
super().__init__(config)
|
| 602 |
self.student = _build_student(config)
|
|
|
|
| 603 |
self.post_init()
|
| 604 |
|
| 605 |
def preprocess_audio(self, audio: torch.Tensor) -> torch.Tensor:
|
|
|
|
| 677 |
if isinstance(feats, (list, tuple)):
|
| 678 |
feats = feats[-1]
|
| 679 |
|
| 680 |
+
pooler_output = _student_features(feats)
|
|
|
|
| 681 |
|
| 682 |
if feats.ndim == 2:
|
| 683 |
last_hidden_state = feats.unsqueeze(1)
|
pytorch_model.bin
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:93683609dd87a36c97dd1a7e47baa4fa50144c5acacf29eceeb0565323c5d8b4
|
| 3 |
+
size 88675195
|
smoke_test.py
CHANGED
|
@@ -1,5 +1,5 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
-
"""Local smoke test for the distilled HeAR ViT-S Canon
|
| 3 |
|
| 4 |
from __future__ import annotations
|
| 5 |
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
+
"""Local smoke test for the distilled HeAR ViT-S Canon model package."""
|
| 3 |
|
| 4 |
from __future__ import annotations
|
| 5 |
|