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
File size: 3,755 Bytes
73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b 73629b7 108888b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | ---
license: other
license_name: health-ai-developer-foundations
license_link: https://developers.google.com/health-ai-developer-foundations/terms
library_name: transformers
pipeline_tag: feature-extraction
tags:
- audio
- medical
- embeddings
- vision-transformer
- distillation
- canon
---
# Distilled HeAR ViT-S Canon model card
**Model documentation:** HeAR (Google Health Acoustic Representations)
## Model information
This package contains a distilled HeAR student model implemented in PyTorch with a ViT-S backbone and Canon layers.
### Description
The model is built for health-acoustic embedding extraction from short audio clips.
- Backbone: ViT-S (`vit_small_patch16_224`)
- Input: single-channel mel+PCEN spectrograms (`[B, 1, 192, 128]`) generated from 2-second audio clips at 16 kHz
- Canon setup: A/B/C/D enabled, 2D Canon, kernel size 4, positional encodings disabled
- Output embedding: `pooler_output` with shape `[B, 384]`
## Files in this package
- `config.json`: model config and `auto_map`
- `configuration_hear_canon.py`: custom `PretrainedConfig`
- `modeling_hear_canon.py`: custom `PreTrainedModel` with integrated audio preprocessing
- `pytorch_model.bin`: distilled student weights
- `preprocessor_config.json`: preprocessing metadata
- `model_shapes.json`: structure and tensor shape inventory
- `training_args.json`: training/checkpoint args captured from the source checkpoint
- `.gitattributes`: git/LFS attributes for model artifacts
- `smoke_test.py`: local verification script
## How to use
Install dependencies:
```bash
pip install -U "transformers>=4.50.0" timm torch scipy soundfile
```
Run local smoke test:
```bash
python3 trained_model_hf_upload/smoke_test.py
```
### Inference from raw audio waveform
```python
import torch
from transformers import AutoModel
model = AutoModel.from_pretrained(
"trained_model_hf_upload",
trust_remote_code=True,
)
model.eval()
# 4 clips, each 2 seconds at 16 kHz => 32000 samples
raw_audio_batch = torch.rand((4, 32000), dtype=torch.float32)
with torch.inference_mode():
out = model(input_values=raw_audio_batch, return_dict=True)
embeddings = out.pooler_output
print(embeddings.shape) # torch.Size([4, 384])
```
### Inference from `.wav` file
```python
import torch
import soundfile as sf
from scipy import signal
from transformers import AutoModel
def load_wav_mono_16k(path: str, target_sr: int = 16000) -> torch.Tensor:
audio, sr = sf.read(path, dtype="float32", always_2d=False)
if audio.ndim == 2:
audio = audio.mean(axis=1)
if sr != target_sr:
new_len = int(round(audio.shape[0] * (target_sr / sr)))
audio = signal.resample(audio, new_len)
return torch.from_numpy(audio).float()
model = AutoModel.from_pretrained("trained_model_hf_upload", trust_remote_code=True)
model.eval()
waveform = load_wav_mono_16k("example.wav")
with torch.inference_mode():
embedding = model.embed_audio(waveform)
print(embedding.shape) # torch.Size([1, 384])
```
### Inference from preprocessed spectrograms
```python
import torch
from transformers import AutoModel
model = AutoModel.from_pretrained("trained_model_hf_upload", trust_remote_code=True)
model.eval()
raw_audio = torch.rand((2, 32000), dtype=torch.float32)
spectrogram = model.preprocess_audio(raw_audio)
with torch.inference_mode():
out = model(pixel_values=spectrogram, return_dict=True)
print(spectrogram.shape) # torch.Size([2, 1, 192, 128])
print(out.pooler_output.shape) # torch.Size([2, 384])
```
## Model architecture overview
- Student model parameters: `22,140,288`
- Embedding dimension: `384`
- Input shape: `[B, 1, 192, 128]`
- Output shape: `[B, 384]`
Detailed tensor shapes are provided in `model_shapes.json`.
|