Giter Club home page Giter Club logo

snac's Introduction

SNAC ๐Ÿฟ

Multi-Scale Neural Audio Codec (SNAC) compresses audio into discrete codes at a low bitrate.

๐ŸŽธ Music samples ๐Ÿ—ฃ๏ธ Speech samples
snac-audio-samples.mp4
speech-samples.mp4

๐ŸŽง More audio samples available at https://hubertsiuzdak.github.io/snac/

Overview

SNAC encodes audio into hierarchical tokens similarly to SoundStream, EnCodec, and DAC (see the image on the left). However, SNAC introduces a simple change where coarse tokens are sampled less frequently, covering a broader time span (see the image on the right).

This can not only save on bitrate, but more importantly this might be very useful for language modeling approaches to audio generation. E.g. with coarse tokens of ~10 Hz and a context window of 2048 you can effectively model a consistent structure of an audio track for ~3 minutes.

snac.png

Pretrained models

Currently, all models support only single audio channel (mono).

Model Bitrate Sample Rate Params Recommended use case
hubertsiuzdak/snac_24khz 0.98 kbps 24 kHz 19.8 M ๐Ÿ—ฃ๏ธ Speech
hubertsiuzdak/snac_32khz 1.9 kbps 32 kHz 54.5 M ๐ŸŽธ Music / Sound Effects
hubertsiuzdak/snac_44khz 2.6 kbps 44 kHz 54.5 M ๐ŸŽธ Music / Sound Effects

Usage

Install it using:

pip install snac

To encode (and decode) audio with SNAC in Python, use the following code:

import torch
from snac import SNAC

model = SNAC.from_pretrained("hubertsiuzdak/snac_32khz").eval().cuda()
audio = torch.randn(1, 1, 32000).cuda()  # placeholder for actual audio with shape (B, 1, T)

with torch.inference_mode():
    codes = model.encode(audio)
    audio_hat = model.decode(codes)

You can also encode and reconstruct in a single call:

with torch.inference_mode():
    audio_hat, codes = model(audio)

โš ๏ธ Note that codes is a list of token sequences of variable lengths, each corresponding to a different temporal resolution.

>>> [code.shape[1] for code in codes]
[12, 24, 48, 96]

Acknowledgements

Module definitions are adapted from the Descript Audio Codec.

snac's People

Contributors

hubertsiuzdak avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

snac's Issues

Example code for reconstruction

Hi, awesome work!

Can you share some example code for reconstructing audio files from the codes?

Can you also provide some numbers how many Tokens one second of speech or music would take?

Have you tried Training a Codec that is okay for music sound effects and speech?

Architectural questions (SNAC vs Vocos)

Hello, why did you switch back to building on DAC-style waveform outputs for SNAC after showing it's possible to completely get rid of aliasing by generating complex spectrograms with Vocos? I'm also curious about the answer to the questions posed here. My best guess for the NoiseBlock is reducing high frequency artifacts with staticky sounds like cymbals?

Thanks!

use of NoiseBlock and depthwise?

Hi!

Interesting work. But I don't understand the following changes:

  • I think you're doing "depthwise" for reducing the number the parameters. Correct?
  • What's the use of NoiseBlock in the decoder?

About the speech synthesis results

Hi, thanks for the nice work!

I have implemented almost the same structure before

Coarse (12.5hz, single codebook) to fine (50 hz, four codebooks)

I have experienced a low reconstruction quality on the first layer when using 12.5 Hz for tts.

Hence, I'm using the first coarse layer with 25 Hz now.

Have you trained the tts model with this codec? If you did, please share your experience!

I think that It may be enough for music generation with 12.5 Hz.

Thanks!

Few questions about the implementation

Hello! Thank you very much for the great work!

I want to try to train a model based on yours, but for speech coding. And I have a few questions.

  1. I see a new release with localMHA. But is it possible to get good results with a model that was in the initial release without localMHA?
  2. I also want to train at different sample rates. Should I change the architecture for different sample rates? For example, strides, dilation, or the number of codes?
  3. And do I understand correctly that to make a causal version of the model, it is enough to make WNConv1d and WNConvTranspose1d causal, that is, to change the padding?

Thank you in advance

create bibtex

Hi Hubert, love your work. Would you mind creating a bibtex for SNAC so we can cite it properly?

import error and state dict load failure

First I encountered

Cell In[1], line 2
      1 import torch
----> 2 from snac import SNAC
      4 model = SNAC.from_pretrained("hubertsiuzdak/snac_32khz").eval().cuda()
      5 audio = torch.randn(1, 1, 32000).cuda()  # B, 1, T

File /itet-stor/tonglu/net_scratch/conda_envs/thesis/lib/python3.11/site-packages/snac/__init__.py:1
----> 1 from .snac import SNAC
      3 __version__ = "1.1.0"

File /itet-stor/tonglu/net_scratch/conda_envs/thesis/lib/python3.11/site-packages/snac/snac.py:8
      5 import torch
      6 from torch import nn
----> 8 from .layers import Encoder, Decoder
      9 from .vq import ResidualVectorQuantize
     12 class SNAC(nn.Module):

File /itet-stor/tonglu/net_scratch/conda_envs/thesis/lib/python3.11/site-packages/snac/layers.py:5
      3 import torch
      4 import torch.nn as nn
----> 5 from torch.nn.utils.parametrizations import weight_norm
      7 from .attention import LocalMHA
     10 class Encoder(nn.Module):

ImportError: cannot import name 'weight_norm' from 'torch.nn.utils.parametrizations'

After modifying layers.py as such

try:
    from torch.nn.utils.parametrizations import weight_norm
except:
    from torch.nn.utils import weight_norm

I encountered

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
Cell In[2], line 17
      7 sample_rate_encodec = 48000
      8 # The number of codebooks used will be determined bythe bandwidth selected.
      9 # E.g. for a bandwidth of 6kbps, `n_q = 8` codebooks are used.
     10 # Supported bandwidths are 1.5kbps (n_q = 2), 3 kbps (n_q = 4), 6 kbps (n_q = 8) and 12 kbps (n_q =16) and 24kbps (n_q=32).
   (...)
     15 
     16 # snac_model = SNAC.from_pretrained("hubertsiuzdak/snac_32khz").eval().cuda()
---> 17 snac_model = SNAC.from_pretrained("hubertsiuzdak/snac_44khz").eval()
     18 sample_rate_snac = 44100

File ~\.conda\envs\thesis\Lib\site-packages\snac\snac.py:97, in SNAC.from_pretrained(cls, repo_id, **kwargs)
     95 state_dict = torch.load(model_path, map_location="cpu")
     96 print(state_dict.keys())
---> 97 model.load_state_dict(state_dict)
     98 model.eval()
     99 return model

File ~\AppData\Roaming\Python\Python311\site-packages\torch\nn\modules\module.py:2041, in Module.load_state_dict(self, state_dict, strict)
   2036         error_msgs.insert(
   2037             0, 'Missing key(s) in state_dict: {}. '.format(
   2038                 ', '.join('"{}"'.format(k) for k in missing_keys)))
   2040 if len(error_msgs) > 0:
-> 2041     raise RuntimeError('Error(s) in loading state_dict for {}:\n\t{}'.format(
   2042                        self.__class__.__name__, "\n\t".join(error_msgs)))
   2043 return _IncompatibleKeys(missing_keys, unexpected_keys)

RuntimeError: Error(s) in loading state_dict for SNAC:
	Missing key(s) in state_dict: "encoder.block.0.weight_g", "encoder.block.0.weight_v", "encoder.block.1.block.0.block.1.weight_g", "encoder.block.1.block.0.block.1.weight_v", "encoder.block.1.block.0.block.3.weight_g", "encoder.block.1.block.0.block.3.weight_v", "encoder.block.1.block.1.block.1.weight_g", "encoder.block.1.block.1.block.1.weight_v", "encoder.block.1.block.1.block.3.weight_g", "encoder.block.1.block.1.block.3.weight_v", "encoder.block.1.block.2.block.1.weight_g", ...
	Unexpected key(s) in state_dict: "encoder.block.0.parametrizations.weight.original0", "encoder.block.0.parametrizations.weight.original1", "encoder.block.1.block.0.block.1.parametrizations.weight.original0", "encoder.block.1.block.0.block.1.parametrizations.weight.original1", "encoder.block.1.block.0.block.3.parametrizations.weight.original0", "encoder.block.1.block.0.block.3.parametrizations.weight.original1", ...

Could you please help me with those issues?

My installed version Pytorch 2.0.0, Torchaudio 2.0.1.


Update: I created a new conda environment with only snac and it worked.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.