gijs commited on
Commit
095608f
·
verified ·
1 Parent(s): 1f2fb05

Model card: simpler OpenCLIP quick start (PyPI >=4.2.1.dev0, load_audio, sentence prompts, probabilities)

Browse files
Files changed (1) hide show
  1. README.md +19 -22
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 git+https://github.com/mlfoundations/open_clip.git torchaudio soundfile tiktoken
40
  ```
41
 
42
- Audio models need a recent OpenCLIP from the `main` branch (not yet on PyPI releases).
43
-
44
  ```python
45
- import open_clip, soundfile, torch
46
-
47
- model, _, preprocess = open_clip.create_model_and_transforms("hf-hub:laion/clap2-xs-audioxl-s1B")
48
- tokenizer = open_clip.get_tokenizer("hf-hub:laion/clap2-xs-audioxl-s1B")
49
-
50
- wav, sr = soundfile.read("applause.wav", dtype="float32", always_2d=True) # (samples, channels), any rate
51
- patches = preprocess()((torch.from_numpy(wav.T), sr)) # mono mix + resample to 32 kHz + log-mel patches
52
- texts = ["people applauding", "a door being slammed", "a dog barking"]
53
-
54
- with torch.inference_mode():
55
- audio = model.encode_audio({k: v[None] for k, v in patches.items()}, normalize=True)
56
- text = model.encode_text(tokenizer(texts), normalize=True)
57
- print(dict(zip(texts, (audio @ text.T)[0].tolist()))) # cosine similarities
58
  ```
59
 
60
- Example output for an FSD50K clip of applause: `{'people applauding': 0.256, 'a door being slammed': 0.097, 'a dog barking': 0.175}`. Cosine similarities of contrastive models are small in absolute terms; compare them across texts, not against a fixed threshold. For probabilities over a set of labels, use `(model.logit_scale.exp() * audio @ text.T).softmax(-1)`.
61
 
62
  ### Several clips at once
63
 
64
- Clips in a batch have different lengths. Pad the patch dictionaries to the longest clip (padding is masked by `patch_valid`; results equal the one-clip-at-a-time embeddings):
65
 
66
  ```python
67
  from torch.nn.utils.rnn import pad_sequence
68
- pp = preprocess()
69
- items = [pp((torch.from_numpy(w.T), sr)) for w, sr in clips] # list of (samples, channels) arrays and rates
70
- batch = {k: pad_sequence([p[k] for p in items], batch_first=True) for k in items[0]}
71
- audio = model.encode_audio(batch, normalize=True)
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