Spaces:
Runtime error
Runtime error
new unlooper ui
#9
by hugggof - opened
Files changed (10)
- .gitignore-4
- app.py+13-76
- conf/lora/lora.yml+2-2
- conf/vampnet.yml+1-1
- requirements.txt-1
- scripts/exp/train.py+7-7
- scripts/utils/data/augment.py → scripts/utils/augment.pyRenamed+1-1
- scripts/utils/gtzan_embeddings.pyDeleted-263
- scripts/utils/data/maestro-reorg.py → scripts/utils/maestro-reorg.pyRenamed
- vampnet/modules/transformer.py+12-28
181 unmodified lines182183184185186187188181 unmodified lines.git-oldconf/generated/*runs*/
gtzan.zip.gtzan_emb_cache12345645614 unmodified lines21222327242526272825 unmodified lines54555657585960616263646566117 unmodified lines18418518618218718818919059 unmodified lines250251252248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287253254255117 unmodified lines3733743754113763773783796 unmodified lines38638738842438939039139293 unmodified lines48648748852448949049149253 unmodified lines54654754858458558658758858959059154955055168 unmodified lines620621622666667668669670671672673674675676677678679680681682683684685623# huggingface space exclusiveimport os
# print("installing pyharp")# os.system('pip install "pyharp@git+https://github.com/audacitorch/pyharp.git"')# print("installing madmom")os.system('pip install cython')os.system('pip install madmom')
14 unmodified linesfrom vampnet.interface import Interfacefrom vampnet import mask as pmask
from pyharp import ModelCard, build_endpoint# Interface = argbind.bind(Interface)# AudioLoader = argbind.bind(at.data.datasets.AudioLoader)
25 unmodified lines
interface = load_interface()
# dataset = at.data.datasets.AudioDataset(# loader,# sample_rate=interface.codec.sample_rate,# duration=interface.coarse.chunk_size_s,# n_examples=5000,# without_replacement=True,# )
OUT_DIR = Path("gradio-outputs")OUT_DIR.mkdir(exist_ok=True, parents=True)117 unmodified lines mask_temperature=data[masktemp]*10, sampling_temperature=data[sampletemp], mask=mask, sampling_steps=data[num_steps] // 2, sampling_steps=data[num_steps], sample_cutoff=data[sample_cutoff], seed=_seed, )59 unmodified lines return f"saved! your save code is {out_dir.stem}", zip_path
def harp_vamp(_input_audio, _beat_mask_width, _sampletemp):
out_dir = OUT_DIR / str(uuid.uuid4()) out_dir.mkdir() sig = at.AudioSignal(_input_audio) sig = interface.preprocess(sig)
z = interface.encode(sig)
# build the mask mask = pmask.linear_random(z, 1.0) if _beat_mask_width > 0: beat_mask = interface.make_beat_mask( sig, after_beat_s=(_beat_mask_width/1000), ) mask = pmask.mask_and(mask, beat_mask)
# save the mask as a txt file zv, mask_z = interface.coarse_vamp( z, mask=mask, sampling_temperature=_sampletemp, return_mask=True, gen_fn=interface.coarse.generate, )
zv = interface.coarse_to_fine( zv, sampling_temperature=_sampletemp, mask=mask, )
sig = interface.to_signal(zv).cpu() print("done")
sig.write(out_dir / "output.wav")
return sig.path_to_file
with gr.Blocks() as demo:
117 unmodified lines minimum=0, maximum=128, step=1, value=3, value=5, )
6 unmodified lines )
beat_mask_width = gr.Slider( label="beat prompt (ms)", label="beat mask width (in milliseconds)", minimum=0, maximum=200, value=0,93 unmodified lines label="top p (0.0 = off)", minimum=0.0, maximum=1.0, value=0.9 value=0.0 ) typical_filtering = gr.Checkbox( label="typical filtering ",53 unmodified lines
# mask settings with gr.Column():
# lora_choice = gr.Dropdown( # label="lora choice", # choices=list(loras.keys()), # value=LORA_NONE, # visible=False # )
vamp_button = gr.Button("generate (vamp)!!!") output_audio = gr.Audio( label="output audio",68 unmodified lines outputs=[thank_you, download_file] )
# harp stuff harp_inputs = [ input_audio, beat_mask_width, sampletemp, ] build_endpoint( inputs=harp_inputs, output=output_audio, process_fn=harp_vamp, card=ModelCard( name="vampnet", description="Generate variations on music input, based on small prompts around the beat. NOTE: vampnet's has a maximum context length of 10 seconds. Please split all audio clips into 10 second chunks, or processing will result in an error. ", author="Hugo Flores García", tags=["music", "generative"] ), visible=False )
demo.launch()8 unmodified lines9101112121314141516178 unmodified lines
NoamScheduler.warmup: 500
batch_size: 6batch_size: 7num_workers: 7save_iters: [10000, 20000, 30000, 40000, 50000, 100000]save_iters: [10000, 20000, 30000, 40000, 50000]sample_freq: 1000val_freq: 500
31 unmodified lines323334353536373831 unmodified linesVampNet.flash_attn: falseVampNet.dropout: 0.1
AudioLoader.relative_path: ""AudioLoader.relative_path: /data/AudioDataset.loudness_cutoff: -30.0AudioDataset.without_replacement: trueAudioLoader.shuffle: true5 unmodified lines678995 unmodified lineswavebeat @ git+https://github.com/hugofloresgarcia/wavebeatlac @ git+https://github.com/hugofloresgarcia/lac.gitdescript-audiotools @ git+https://github.com/descriptinc/audiotools.git@0.7.2-e git+https://github.com/audacitorch/pyharp.git#egg=pyharptorch_pitch_shift223 unmodified lines22422522622722722822923058 unmodified lines289290291292292293294295112 unmodified lines40840941041141141241341441541641741741841942042142242342342442542623 unmodified lines45045145245345345445545612 unmodified lines469470471472472473474475223 unmodified lines
dtype = torch.bfloat16 if accel.amp else None with accel.autocast(dtype=dtype): z_hat = state.model(z_mask_latent) z_hat = state.model(z_mask_latent, r)
target = codebook_flatten( z[:, vn.n_conditioning_codebooks :, :],58 unmodified lines
z_mask_latent = vn.embedding.from_codes(z_mask, state.codec)
z_hat = state.model(z_mask_latent) z_hat = state.model(z_mask_latent, r)
target = codebook_flatten( z[:, vn.n_conditioning_codebooks :, :],112 unmodified lines
for i in range(len(val_idx)): imputed_noisy[i].cpu().write_audio_to_tb( f"inpainted_prompt/{i}", f"imputed_noisy/{i}", writer, step=state.tracker.step, plot_fn=None, ) imputed[i].cpu().write_audio_to_tb( f"inpainted_middle/{i}", f"imputed/{i}", writer, step=state.tracker.step, plot_fn=None, ) imputed_true[i].cpu().write_audio_to_tb( f"reconstructed/{i}", f"imputed_true/{i}", writer, step=state.tracker.step, plot_fn=None,23 unmodified lines
z_mask_latent = vn.embedding.from_codes(z_mask, state.codec)
z_hat = state.model(z_mask_latent) z_hat = state.model(z_mask_latent, r)
z_pred = torch.softmax(z_hat, dim=1).argmax(dim=1) z_pred = codebook_unflatten(z_pred, n_c=vn.n_predict_codebooks)12 unmodified lines } for k, v in audio_dict.items(): v.cpu().write_audio_to_tb( f"onestep/_{i}.r={r[i]:0.2f}/{k}", f"samples/_{i}.r={r[i]:0.2f}/{k}", writer, step=state.tracker.step, plot_fn=None,63 unmodified lines646566676763 unmodified lines args = argbind.parse_args()
with argbind.scope(args): augment() augment()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263"""TODO: train a linear probeusage: python gtzan_embeddings.py --args.load conf/interface.yml --Interface.device cuda --path_to_gtzan /path/to/gtzan/genres_original --output_dir /path/to/output"""from pathlib import Pathfrom typing import List
import audiotools as atfrom audiotools import AudioSignalimport argbindimport torchimport numpy as npimport zipfileimport json
from vampnet.interface import Interfaceimport tqdm
# bind the Interface to argbindInterface = argbind.bind(Interface)
DEBUG = False
def smart_plotly_export(fig, save_path): img_format = save_path.split('.')[-1] if img_format == 'html': fig.write_html(save_path) elif img_format == 'bytes': return fig.to_image(format='png') #TODO: come back and make this prettier elif img_format == 'numpy': import io from PIL import Image
def plotly_fig2array(fig): #convert Plotly fig to an array fig_bytes = fig.to_image(format="png", width=1200, height=700) buf = io.BytesIO(fig_bytes) img = Image.open(buf) return np.asarray(img) return plotly_fig2array(fig) elif img_format == 'jpeg' or 'png' or 'webp': fig.write_image(save_path) else: raise ValueError("invalid image format")
def dim_reduce(emb, labels, save_path, n_components=3, method='tsne', title=''): """ dimensionality reduction for visualization! saves an html plotly figure to save_path parameters: emb (np.ndarray): the samples to be reduces with shape (samples, features) labels (list): list of labels for embedding save_path (str): path where u wanna save ur figure method (str): umap, tsne, or pca title (str): title for ur figure returns: proj (np.ndarray): projection vector with shape (samples, dimensions) """ import pandas as pd import plotly.express as px if method == 'umap': reducer = umap.UMAP(n_components=n_components) elif method == 'tsne': from sklearn.manifold import TSNE reducer = TSNE(n_components=n_components) elif method == 'pca': from sklearn.decomposition import PCA reducer = PCA(n_components=n_components) else: raise ValueError proj = reducer.fit_transform(emb)
if n_components == 2: df = pd.DataFrame(dict( x=proj[:, 0], y=proj[:, 1], instrument=labels )) fig = px.scatter(df, x='x', y='y', color='instrument', title=title+f"_{method}")
elif n_components == 3: df = pd.DataFrame(dict( x=proj[:, 0], y=proj[:, 1], z=proj[:, 2], instrument=labels )) fig = px.scatter_3d(df, x='x', y='y', z='z', color='instrument', title=title) else: raise ValueError("cant plot more than 3 components")
fig.update_traces(marker=dict(size=6, line=dict(width=1, color='DarkSlateGrey')), selector=dict(mode='markers'))
return smart_plotly_export(fig, save_path)
# per JukeMIR, we want the emebddings from the middle layer?def vampnet_embed(sig: AudioSignal, interface: Interface, layer=10): with torch.inference_mode(): # preprocess the signal sig = interface.preprocess(sig)
# get the coarse vampnet model vampnet = interface.coarse
# get the tokens z = interface.encode(sig)[:, :vampnet.n_codebooks, :] z_latents = vampnet.embedding.from_codes(z, interface.codec)
# do a forward pass through the model, get the embeddings _z, embeddings = vampnet(z_latents, return_activations=True) # print(f"got embeddings with shape {embeddings.shape}") # [layer, batch, time, n_dims] # [20, 1, 600ish, 768]
# squeeze batch dim (1 bc layer should be dim 0) assert embeddings.shape[1] == 1, f"expected batch dim to be 1, got {embeddings.shape[0]}" embeddings = embeddings.squeeze(1)
num_layers = embeddings.shape[0] assert layer < num_layers, f"layer {layer} is out of bounds for model with {num_layers} layers"
# do meanpooling over the time dimension embeddings = embeddings.mean(dim=-2) # [20, 768]
# return the embeddings return embeddings
from dataclasses import dataclass, fields@dataclassclass Embedding: genre: str filename: str embedding: np.ndarray
def save(self, path): """Save the Embedding object to a given path as a zip file.""" with zipfile.ZipFile(path, 'w') as archive: # Save numpy array with archive.open('embedding.npy', 'w') as f: np.save(f, self.embedding)
# Save non-numpy data as json non_numpy_data = {f.name: getattr(self, f.name) for f in fields(self) if f.name != 'embedding'} with archive.open('data.json', 'w') as f: f.write(json.dumps(non_numpy_data).encode('utf-8'))
@classmethod def load(cls, path): """Load the Embedding object from a given zip path.""" with zipfile.ZipFile(path, 'r') as archive: # Load numpy array with archive.open('embedding.npy') as f: embedding = np.load(f)
# Load non-numpy data from json with archive.open('data.json') as f: data = json.loads(f.read().decode('utf-8'))
return cls(embedding=embedding, **data)
@argbind.bind(without_prefix=True)def main( path_to_gtzan: str = None, cache_dir: str = "./.gtzan_emb_cache", output_dir: str = "./gtzan_vampnet_embeddings", layers: List[int] = [1, 3, 5, 7, 9, 11, 13, 15, 17, 19]): path_to_gtzan = Path(path_to_gtzan) assert path_to_gtzan.exists(), f"{path_to_gtzan} does not exist"
cache_dir = Path(cache_dir) output_dir = Path(output_dir) output_dir.mkdir(exist_ok=True, parents=True)
# load our interface # argbind will automatically load the default config, interface = Interface()
# gtzan should have a folder for each genre, so let's get the list of genres genres = [Path(x).name for x in path_to_gtzan.iterdir() if x.is_dir()] print(f"Found {len(genres)} genres") print(f"genres: {genres}")
# collect audio files, genres, and embeddings data = [] for genre in genres: audio_files = list(at.util.find_audio(path_to_gtzan / genre)) print(f"Found {len(audio_files)} audio files for genre {genre}")
for audio_file in tqdm.tqdm(audio_files, desc=f"embedding genre {genre}"): # check if we have a cached embedding for this file cached_path = (cache_dir / f"{genre}_{audio_file.stem}.emb") if cached_path.exists(): # if so, load it if DEBUG: print(f"loading cached embedding for {cached_path.stem}") embedding = Embedding.load(cached_path) data.append(embedding) else: try: sig = AudioSignal(audio_file) except Exception as e: print(f"failed to load {audio_file.name} with error {e}") print(f"skipping {audio_file.name}") continue
# gets the embedding emb = vampnet_embed(sig, interface).cpu().numpy()
# create an embedding we can save/load embedding = Embedding( genre=genre, filename=audio_file.name, embedding=emb ) # cache the embeddings cached_path.parent.mkdir(exist_ok=True, parents=True) embedding.save(cached_path)
# now, let's do a dim reduction on the embeddings # and visualize them.
# collect a list of embeddings and labels embeddings = [d.embedding for d in data] labels = [d.genre for d in data]
# convert the embeddings to a numpy array embeddings = np.stack(embeddings)
# do dimensionality reduction for each layer we're given for layer in tqdm.tqdm(layers, desc="dim reduction"): dim_reduce( embeddings[:, layer, :], labels, save_path=str(output_dir / f'vampnet-gtzan-layer={layer}.html'), n_components=2, method='tsne', title=f'vampnet-gtzan-layer={layer}' )
if __name__ == "__main__": args = argbind.parse_args() with argbind.scope(args): main()409 unmodified lines41041141241341441541341441541620 unmodified lines4374384394424434404414424 unmodified lines4474484494544554504574584514594604614624524534541 unmodified line4564574584704594604614624 unmodified lines46746846948147047147240 unmodified lines51351451552851651751851953253353453552052152252352452553952652752852954354454554653053153253355 unmodified lines58959059160859259359459564 unmodified lines660661662679663664665666254 unmodified lines921922923940924925926927409 unmodified lines def subsequent_mask(self, size): return torch.ones(1, size, size).tril().bool()
def forward(self, x, x_mask, cond=None, src=None, src_mask=None, return_activations: bool = False ): def forward(self, x, x_mask, cond=None, src=None, src_mask=None): """Computes a full transformer stack Parameters ----------20 unmodified lines encoder_decoder_position_bias = None
# Compute transformer layers if return_activations: activations = [] for layer in self.layers: x, position_bias, encoder_decoder_position_bias = layer( x=x,4 unmodified lines position_bias=position_bias, encoder_decoder_position_bias=encoder_decoder_position_bias, ) if return_activations: activations.append(x.detach())
out = self.norm(x) if self.norm is not None else x return self.norm(x) if self.norm is not None else x if return_activations: return out, torch.stack(activations) else: return out
class VampNet(at.ml.BaseModel):1 unmodified line self, n_heads: int = 20, n_layers: int = 16, r_cond_dim: int = 0, r_cond_dim: int = 64, n_codebooks: int = 9, n_conditioning_codebooks: int = 0, latent_dim: int = 8,4 unmodified lines dropout: float = 0.1 ): super().__init__() assert r_cond_dim == 0, f"r_cond_dim must be 0 (not supported), but got {r_cond_dim}" self.n_heads = n_heads self.n_layers = n_layers self.r_cond_dim = r_cond_dim40 unmodified lines ), )
def forward(self, x, return_activations: bool = False): def forward(self, x, cond): x = self.embedding(x) x_mask = torch.ones_like(x, dtype=torch.bool)[:, :1, :].squeeze(1)
x = rearrange(x, "b d n -> b n d") out = self.transformer(x=x, x_mask=x_mask, return_activations=return_activations) if return_activations: out, activations = out cond = self.r_embed(cond)
x = rearrange(x, "b d n -> b n d") out = self.transformer(x=x, x_mask=x_mask, cond=cond) out = rearrange(out, "b n d -> b d n")
out = self.classifier(out, None) # no cond here! out = self.classifier(out, cond)
out = rearrange(out, "b (p c) t -> b p (t c)", c=self.n_predict_codebooks)
if return_activations: return out, activations else: return out return out def r_embed(self, r, max_positions=10000): if self.r_cond_dim > 0:55 unmodified lines top_p=None, return_signal=True, seed: int = None, sample_cutoff: float = 1.0, sample_cutoff: float = 0.5, ): if seed is not None: at.util.seed(seed)64 unmodified lines
# infer from latents # NOTE: this collapses the codebook dimension into the sequence dimension logits = self.forward(latents) # b, prob, seq logits = self.forward(latents, r) # b, prob, seq logits = logits.permute(0, 2, 1) # b, seq, prob b = logits.shape[0]
254 unmodified lines z_mask_latent = torch.rand( batch_size, model.latent_dim * model.n_codebooks, seq_len ).to(device) z_hat = model(z_mask_latent) z_hat = model(z_mask_latent, r)
pred = z_hat.argmax(dim=1) pred = model.embedding.unflatten(pred, n_codebooks=model.n_predict_codebooks).gitignore-4
app.py+13-76
conf/lora/lora.yml+2-2
conf/vampnet.yml+1-1
requirements.txt-1
scripts/exp/train.py+7-7
scripts/utils/data/augment.py → scripts/utils/augment.pyRenamed+1-1
scripts/utils/gtzan_embeddings.pyDeleted-263
scripts/utils/data/maestro-reorg.py → scripts/utils/maestro-reorg.pyRenamed
vampnet/modules/transformer.py+12-28