Feature Extraction
OpenCLIP
Safetensors
English
clap2
audio
clap
contrastive
audio-text-retrieval
zero-shot-audio-classification
Instructions to use laion/clap2-xs-audioxl-s1B with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- OpenCLIP
How to use laion/clap2-xs-audioxl-s1B with OpenCLIP:
import open_clip model, preprocess_train, preprocess_val = open_clip.create_model_and_transforms('hf-hub:laion/clap2-xs-audioxl-s1B') tokenizer = open_clip.get_tokenizer('hf-hub:laion/clap2-xs-audioxl-s1B') - Notebooks
- Google Colab
- Kaggle
Model card: simpler OpenCLIP quick start (PyPI >=4.2.1.dev0, load_audio, sentence prompts, probabilities)
Browse files
README.md
CHANGED
|
@@ -36,39 +36,36 @@ Scores are in %, higher is better. MAEB-30 is the mean over the 30 tasks of the
|
|
| 36 |
## Quick start
|
| 37 |
|
| 38 |
```bash
|
| 39 |
-
pip install
|
| 40 |
```
|
| 41 |
|
| 42 |
-
Audio models need a recent OpenCLIP from the `main` branch (not yet on PyPI releases).
|
| 43 |
-
|
| 44 |
```python
|
| 45 |
-
import
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
with torch.
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
print(dict(zip(texts, (audio @ text.T)[0].tolist()))) # cosine similarities
|
| 58 |
```
|
| 59 |
|
| 60 |
-
|
| 61 |
|
| 62 |
### Several clips at once
|
| 63 |
|
| 64 |
-
|
| 65 |
|
| 66 |
```python
|
| 67 |
from torch.nn.utils.rnn import pad_sequence
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
```
|
| 73 |
|
| 74 |
### With Transformers
|
|
|
|
| 36 |
## Quick start
|
| 37 |
|
| 38 |
```bash
|
| 39 |
+
pip install "open_clip_torch>=4.2.1.dev0" soundfile
|
| 40 |
```
|
| 41 |
|
|
|
|
|
|
|
| 42 |
```python
|
| 43 |
+
import torch, open_clip
|
| 44 |
+
from open_clip.audio.io import load_audio
|
| 45 |
+
|
| 46 |
+
hub = "hf-hub:laion/clap2-xs-audioxl-s1B"
|
| 47 |
+
model, preprocess = open_clip.create_model_from_pretrained(hub)
|
| 48 |
+
tokenizer = open_clip.get_tokenizer(hub)
|
| 49 |
+
|
| 50 |
+
audio = preprocess()(load_audio("dog.wav")) # any sample rate and channel count
|
| 51 |
+
texts = ["A dog barks.", "A door slams shut.", "People are applauding."]
|
| 52 |
+
with torch.no_grad():
|
| 53 |
+
logits, _ = model.get_logits({k: v[None] for k, v in audio.items()}, tokenizer(texts))
|
| 54 |
+
print({t: round(p, 3) for t, p in zip(texts, logits.softmax(-1)[0].tolist())})
|
|
|
|
| 55 |
```
|
| 56 |
|
| 57 |
+
Output for an FSD50K clip of a dog barking: `{'A dog barks.': 1.0, 'A door slams shut.': 0.0, 'People are applauding.': 0.0}`. For embeddings, use `model.encode_audio({k: v[None] for k, v in audio.items()}, normalize=True)` and `model.encode_text(tokenizer(texts), normalize=True)`.
|
| 58 |
|
| 59 |
### Several clips at once
|
| 60 |
|
| 61 |
+
Pad the patch dictionaries to the longest clip. The padding is masked by `patch_valid`, so the embeddings equal the one-clip-at-a-time ones:
|
| 62 |
|
| 63 |
```python
|
| 64 |
from torch.nn.utils.rnn import pad_sequence
|
| 65 |
+
items = [preprocess()(load_audio(f)) for f in ["dog.wav", "rain.wav"]]
|
| 66 |
+
batch = {k: pad_sequence([x[k] for x in items], batch_first=True) for k in items[0]}
|
| 67 |
+
with torch.no_grad():
|
| 68 |
+
audio_embeds = model.encode_audio(batch, normalize=True)
|
| 69 |
```
|
| 70 |
|
| 71 |
### With Transformers
|