MCPcopy
hub / github.com/descriptinc/descript-audio-codec / main

Function main

scripts/compute_entropy.py:11–44  ·  view source on GitHub ↗
(
    folder: str,
    model_path: str,
    n_samples: int = 1024,
    device: str = "cuda",
)

Source from the content-addressed store, hash-verified

9
10@argbind.bind(without_prefix=True, positional=True)
11def main(
12 folder: str,
13 model_path: str,
14 n_samples: int = 1024,
15 device: str = "cuda",
16):
17 files = at.util.find_audio(folder)[:n_samples]
18 signals = [
19 at.AudioSignal.salient_excerpt(f, loudness_cutoff=-20, duration=1.0)
20 for f in files
21 ]
22
23 with torch.no_grad():
24 model = dac.model.DAC.load(model_path).to(device)
25 model.eval()
26
27 codes = []
28 for x in tqdm.tqdm(signals):
29 x = x.to(model.device)
30 o = model.encode(x.audio_data, x.sample_rate)
31 codes.append(o["codes"].cpu())
32
33 codes = torch.cat(codes, dim=-1)
34 entropy = []
35
36 for i in range(codes.shape[1]):
37 codes_ = codes[0, i, :]
38 counts = torch.bincount(codes_)
39 counts = (counts / counts.sum()).clamp(1e-10)
40 entropy.append(-(counts * counts.log()).sum().item() * np.log2(np.e))
41
42 pct = sum(entropy) / (10 * len(entropy))
43 print(f"Entropy for each codebook: {entropy}")
44 print(f"Effective percentage: {pct * 100}%")
45
46
47if __name__ == "__main__":

Callers 1

compute_entropy.pyFile · 0.70

Calls 2

loadMethod · 0.80
encodeMethod · 0.80

Tested by

no test coverage detected