├── .gitignore ├── docs ├── add.png ├── delete.png ├── workflow.png └── replacement.png ├── prompt2prompt ├── Roboto-Regular.ttf ├── __pycache__ │ ├── ptp_utils.cpython-39.pyc │ └── attn_control.cpython-39.pyc ├── attn_control.py └── ptp_utils.py ├── utils ├── __pycache__ │ ├── utils.cpython-39.pyc │ └── converter.cpython-39.pyc ├── utils.py └── converter.py ├── suppresseot ├── __pycache__ │ ├── attn_loss.cpython-39.pyc │ ├── wo_utils.cpython-39.pyc │ └── run_and_display.cpython-39.pyc ├── wo_utils.py ├── attn_loss.py └── run_and_display.py ├── auffusion ├── __pycache__ │ └── auffusion_pipeline.cpython-39.pyc └── auffusion_pipeline.py ├── audio_examples └── input_audios │ ├── A woman is giving a speech.wav │ └── After a gunshot, there was a burst of dog barking.wav ├── null_text_inversion ├── __pycache__ │ └── null_text_inversion.cpython-39.pyc └── null_text_inversion.py ├── eval ├── clap_score │ └── cal_clap_score.py └── configs │ └── audiocaps │ ├── edit_replace.csv │ ├── edit_delete.csv │ └── edit_add.csv ├── main.sh ├── requirements.txt ├── main.py ├── readme.md └── LICENSE /.gitignore: -------------------------------------------------------------------------------- 1 | audio_examples/output_audios/* 2 | ckpt/* -------------------------------------------------------------------------------- /docs/add.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/docs/add.png -------------------------------------------------------------------------------- /docs/delete.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/docs/delete.png -------------------------------------------------------------------------------- /docs/workflow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/docs/workflow.png -------------------------------------------------------------------------------- /docs/replacement.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/docs/replacement.png -------------------------------------------------------------------------------- /prompt2prompt/Roboto-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/prompt2prompt/Roboto-Regular.ttf -------------------------------------------------------------------------------- /utils/__pycache__/utils.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/utils/__pycache__/utils.cpython-39.pyc -------------------------------------------------------------------------------- /utils/__pycache__/converter.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/utils/__pycache__/converter.cpython-39.pyc -------------------------------------------------------------------------------- /suppresseot/__pycache__/attn_loss.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/suppresseot/__pycache__/attn_loss.cpython-39.pyc -------------------------------------------------------------------------------- /suppresseot/__pycache__/wo_utils.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/suppresseot/__pycache__/wo_utils.cpython-39.pyc -------------------------------------------------------------------------------- /prompt2prompt/__pycache__/ptp_utils.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/prompt2prompt/__pycache__/ptp_utils.cpython-39.pyc -------------------------------------------------------------------------------- /prompt2prompt/__pycache__/attn_control.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/prompt2prompt/__pycache__/attn_control.cpython-39.pyc -------------------------------------------------------------------------------- /suppresseot/__pycache__/run_and_display.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/suppresseot/__pycache__/run_and_display.cpython-39.pyc -------------------------------------------------------------------------------- /auffusion/__pycache__/auffusion_pipeline.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/auffusion/__pycache__/auffusion_pipeline.cpython-39.pyc -------------------------------------------------------------------------------- /audio_examples/input_audios/A woman is giving a speech.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/audio_examples/input_audios/A woman is giving a speech.wav -------------------------------------------------------------------------------- /null_text_inversion/__pycache__/null_text_inversion.cpython-39.pyc: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/null_text_inversion/__pycache__/null_text_inversion.cpython-39.pyc -------------------------------------------------------------------------------- /audio_examples/input_audios/After a gunshot, there was a burst of dog barking.wav: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NKU-HLT/AudioEditor/HEAD/audio_examples/input_audios/After a gunshot, there was a burst of dog barking.wav -------------------------------------------------------------------------------- /eval/clap_score/cal_clap_score.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from msclap import CLAP 3 | 4 | # Load model (Choose between versions '2022' or '2023') 5 | # The model weight will be downloaded automatically if `model_fp` is not specified 6 | clap_model = CLAP(version = '2023', model_fp = 'CLAP_weights_2023.pth', use_cuda = False) 7 | 8 | # Extract text embeddings 9 | text_embeddings = clap_model.get_text_embeddings(["A baby and a woman laugh"]) 10 | 11 | # Extract audio embeddings 12 | audio_embeddings = clap_model.get_audio_embeddings(["A baby and a woman laugh.wav"]) 13 | 14 | # cosine_similarity = torch.nn.functional.cosine_similarity(audio_embeddings, text_embeddings) 15 | # print("Cosine Similarity:", cosine_similarity.item()) -------------------------------------------------------------------------------- /main.sh: -------------------------------------------------------------------------------- 1 | export HF_ENDPOINT="https://hf-mirror.com" 2 | export HF_HUB_ENABLE_HF_TRANSFER=1 3 | 4 | CUDA_VISIBLE_DEVICES='0' 5 | 6 | ### delete 7 | python main.py --prompt "After a gunshot, there was a burst of dog barking" \ 8 | --audio_path "audio_examples/input_audios/After a gunshot, there was a burst of dog barking.wav" \ 9 | --token_indices "[[10,11]]" \ 10 | --alpha "[1.,]" --cross_retain_steps "[.2,]" 11 | 12 | ### replace 13 | python main.py --prompt "After a thunder, there was a burst of dog barking" \ 14 | --audio_path "audio_examples/input_audios/After a gunshot, there was a burst of dog barking.wav" \ 15 | --token_indices "[[3]]" \ 16 | --alpha "[-0.001,]" --cross_retain_steps "[.2,]" 17 | 18 | ### add 19 | python main.py --prompt "A woman is giving a speech amid applause" \ 20 | --audio_path "audio_examples/input_audios/A woman is giving a speech.wav" \ 21 | --token_indices "[[7,8]]" \ 22 | --alpha "[-0.001,]" --cross_retain_steps "[.2,]" -------------------------------------------------------------------------------- /suppresseot/wo_utils.py: -------------------------------------------------------------------------------- 1 | 2 | import torch 3 | import random 4 | from scipy.spatial.distance import cdist 5 | 6 | CALC_SIMILARITY = False 7 | 8 | def punish_wight(wo_batch, latent_size, alpha, method): 9 | if method == 'weight': 10 | wo_batch *= alpha 11 | elif method in ['alpha', 'beta', 'delete', 'soft-weight']: 12 | u, s, vh = torch.linalg.svd(wo_batch) 13 | u = u[:,:latent_size] 14 | zero_idx = int(latent_size * alpha) 15 | if method == 'alpha': 16 | s[:zero_idx] = 0 17 | elif method == 'beta': 18 | s[zero_idx:] = 0 19 | elif method == 'delete': 20 | s = s[zero_idx:] if zero_idx < latent_size else torch.zeros(latent_size).to(s.device) 21 | u = u[:, zero_idx:] if zero_idx < latent_size else u 22 | vh = vh[zero_idx:, :] if zero_idx < latent_size else vh 23 | elif method == 'soft-weight': 24 | if CALC_SIMILARITY: 25 | _s = s.clone() 26 | _s[zero_idx:] = 0 27 | _wo_batch = u @ torch.diag(_s) @ vh 28 | dist = cdist(wo_batch[:,0].unsqueeze(0).cpu(), _wo_batch[:,0].unsqueeze(0).cpu(), metric='cosine') 29 | print(f'The distance between the word embedding before and after the punishment: {dist}') 30 | if alpha == -.001: 31 | s *= (torch.exp(-.001 * s) * 1.2) # add $ replace 32 | else: 33 | s *= torch.exp(-alpha*s) # delete 34 | 35 | wo_batch = u @ torch.diag(s) @ vh 36 | else: 37 | raise ValueError('Unsupported method') 38 | return wo_batch 39 | 40 | def woword_eot_context(context, token_indices, alpha, method, n): 41 | for i, batch in enumerate(context): 42 | indices = token_indices + [num for num in range(n-1, 77)] 43 | wo_batch = batch[indices] 44 | wo_batch = punish_wight(wo_batch.T, len(indices), alpha, method).T.float() # .float() 45 | batch[indices] = wo_batch 46 | return context 47 | 48 | def woword_reweight(attn, token_indices, alpha): 49 | # if attn.shape[1] > 32 ** 2: # avoid memory overhead 50 | # return attn 51 | latent_size = int(attn.shape[1]**0.5) 52 | assert latent_size**2 == attn.shape[1] 53 | for head_attn in attn: 54 | for indice in token_indices: 55 | wo_attn = head_attn[:, indice].reshape(latent_size, latent_size) 56 | wo_attn *= alpha # same as Reweight of P2P 57 | head_attn[:, indice] = wo_attn.reshape(latent_size**2) 58 | return attn -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | accelerate==0.32.0 2 | aiohttp==3.9.5 3 | aiosignal==1.3.1 4 | asttokens==2.4.1 5 | async-timeout==4.0.3 6 | attrs==23.2.0 7 | audioread==3.0.1 8 | certifi==2024.7.4 9 | cffi==1.16.0 10 | charset-normalizer==3.3.2 11 | cmake==3.30.0 12 | comm==0.2.2 13 | contourpy==1.2.1 14 | cycler==0.12.1 15 | datasets==2.20.0 16 | debugpy==1.8.2 17 | decorator==5.1.1 18 | diffusers==0.18.2 19 | dill==0.3.8 20 | einops==0.8.0 21 | exceptiongroup==1.2.1 22 | executing==2.0.1 23 | filelock==3.15.4 24 | fonttools==4.53.0 25 | frozenlist==1.4.1 26 | fsspec==2024.5.0 27 | hf_transfer==0.1.6 28 | huggingface-hub==0.23.4 29 | idna==3.7 30 | importlib_metadata==8.0.0 31 | importlib_resources==6.4.0 32 | ipykernel==6.29.5 33 | ipython==8.18.1 34 | jedi==0.19.1 35 | Jinja2==3.1.4 36 | joblib==1.4.2 37 | jupyter_client==8.6.2 38 | jupyter_core==5.7.2 39 | kiwisolver==1.4.5 40 | librosa==0.9.2 41 | lit==18.1.8 42 | llvmlite==0.43.0 43 | MarkupSafe==2.1.5 44 | matplotlib==3.9.1 45 | matplotlib-inline==0.1.7 46 | mpmath==1.3.0 47 | multidict==6.0.5 48 | multiprocess==0.70.16 49 | mypy-extensions==1.0.0 50 | nest-asyncio==1.6.0 51 | networkx==3.2.1 52 | numba==0.60.0 53 | numpy==1.26.4 54 | nvidia-cublas-cu11==11.10.3.66 55 | nvidia-cuda-cupti-cu11==11.7.101 56 | nvidia-cuda-nvrtc-cu11==11.7.99 57 | nvidia-cuda-runtime-cu11==11.7.99 58 | nvidia-cudnn-cu11==8.5.0.96 59 | nvidia-cufft-cu11==10.9.0.58 60 | nvidia-curand-cu11==10.2.10.91 61 | nvidia-cusolver-cu11==11.4.0.1 62 | nvidia-cusparse-cu11==11.7.4.91 63 | nvidia-nccl-cu11==2.14.3 64 | nvidia-nvtx-cu11==11.7.91 65 | opencv-python==4.10.0.84 66 | packaging==24.1 67 | pandas==2.2.2 68 | parso==0.8.4 69 | pexpect==4.9.0 70 | pillow==10.4.0 71 | platformdirs==4.2.2 72 | pooch==1.8.2 73 | prompt_toolkit==3.0.47 74 | psutil==6.0.0 75 | ptyprocess==0.7.0 76 | pure-eval==0.2.2 77 | pyarrow==16.1.0 78 | pyarrow-hotfix==0.6 79 | pycparser==2.22 80 | Pygments==2.18.0 81 | pyparsing==3.1.2 82 | pyre-extensions==0.0.29 83 | python-dateutil==2.9.0.post0 84 | pytz==2024.1 85 | PyYAML==6.0.1 86 | pyzmq==26.0.3 87 | regex==2024.5.15 88 | requests==2.32.3 89 | resampy==0.4.3 90 | safetensors==0.4.3 91 | scikit-learn==1.5.1 92 | scipy==1.13.1 93 | six==1.16.0 94 | soundfile==0.12.1 95 | stack-data==0.6.3 96 | sympy==1.12.1 97 | threadpoolctl==3.5.0 98 | tokenizers==0.13.3 99 | torch==2.0.1 100 | torchaudio==2.0.2 101 | torchvision==0.15.2 102 | tornado==6.4.1 103 | tqdm==4.66.4 104 | traitlets==5.14.3 105 | transformers==4.33.3 106 | triton==2.0.0 107 | typing-inspect==0.9.0 108 | typing_extensions==4.12.2 109 | tzdata==2024.1 110 | urllib3==2.2.2 111 | wcwidth==0.2.13 112 | xformers==0.0.20 113 | xxhash==3.4.1 114 | yarl==1.9.4 115 | zipp==3.19.2 116 | -------------------------------------------------------------------------------- /suppresseot/attn_loss.py: -------------------------------------------------------------------------------- 1 | import torch 2 | from torch import nn 3 | from torch.nn import functional as F 4 | 5 | class Loss(torch.nn.Module): 6 | def __init__(self, loss_type='mse'): 7 | super(Loss, self).__init__() 8 | 9 | self.loss_type = loss_type 10 | 11 | self.loss_func = { 12 | 'mse': torch.nn.MSELoss, 13 | 'cosine': torch.nn.CosineSimilarity, 14 | 'mae': torch.nn.L1Loss 15 | }[loss_type]() 16 | 17 | def forward(self, x, y): 18 | if self.loss_type == "cosine": 19 | return 1. - self.loss_func(x, y) 20 | 21 | return self.loss_func(x, y) 22 | 23 | class AttnLoss(nn.Module): 24 | def __init__(self, device, attn_loss_type, n, token_indices, lambda_retain=1., lambda_erase=-1., lambda_self_retain=1., lambda_self_erase=-1.): 25 | super(AttnLoss, self).__init__() 26 | self.device = device 27 | self.prompt_n = n 28 | self.token_indices = token_indices 29 | 30 | self.lambda_retain = lambda_retain 31 | self.lambda_erase = lambda_erase 32 | self.lambda_self_retain = lambda_self_retain 33 | self.lambda_self_erase = lambda_self_erase 34 | 35 | self.retain_loss = Loss(attn_loss_type) 36 | self.erase_loss = Loss(attn_loss_type) 37 | self.self_retain_loss = Loss(attn_loss_type) 38 | self.self_erase_loss = Loss(attn_loss_type) 39 | 40 | def calc_mask(self, attn, threshold=.85): 41 | mask = [] 42 | for i in [num for num in range(1, self.prompt_n-1)]: 43 | _attn = attn[:,:,i].clone() 44 | _attn = 255 * _attn / _attn.max() 45 | _attn = F.interpolate(_attn.unsqueeze(0).unsqueeze(0), size=(256, 256), mode='bilinear') 46 | if i in self.token_indices: 47 | _threshold = threshold 48 | else: 49 | _threshold = threshold + .1 50 | _attn[_attn >= _attn.max() * _threshold] = 255 51 | _attn[_attn < _attn.max() * _threshold] = 0 52 | _attn = F.interpolate(_attn, size=attn.shape[:2], mode='bilinear') 53 | mask += [_attn.squeeze(0).squeeze(0)] 54 | return mask 55 | 56 | def calc_retain_loss(self, attn, attn_erase): 57 | loss = .0 58 | for i in [num for num in range(1, self.prompt_n-1)]: 59 | if i in self.token_indices: 60 | continue 61 | loss += self.retain_loss(attn[:,:,i], attn_erase[:,:,i]) 62 | return loss 63 | 64 | def calc_erase_loss(self, attn, attn_erase): 65 | loss = .0 66 | for i in self.token_indices: 67 | loss += self.erase_loss(attn[:,:,i], attn_erase[:,:,i]) 68 | return loss 69 | 70 | def calc_self_retain_loss(self, self_attn, self_attn_erase, mask): 71 | loss = .0 72 | h, w = mask[0].shape 73 | for i in [num for num in range(1, self.prompt_n-1)]: 74 | if i in self.token_indices: 75 | continue 76 | for j, m in enumerate(mask[i-1].reshape(h*w)): 77 | if m > 0: 78 | loss += self.self_retain_loss(self_attn[:,:,j].view(-1).unsqueeze(0), 79 | self_attn_erase[:,:,j].view(-1).unsqueeze(0)) 80 | return loss 81 | 82 | def calc_self_erase_loss(self, self_attn, self_attn_erase, mask): 83 | loss = .0 84 | h, w = mask[0].shape 85 | for i in self.token_indices: 86 | for j, m in enumerate(mask[i-1].reshape(h*w)): 87 | if m > 0: 88 | loss += self.self_erase_loss(self_attn[:,:,j].view(-1).unsqueeze(0), 89 | self_attn_erase[:,:,j].view(-1).unsqueeze(0)) 90 | return loss 91 | 92 | def forward(self, attn, attn_erase, self_attn, self_attn_erase): 93 | attn, attn_erase, self_attn, self_attn_erase \ 94 | = attn.to(torch.double), attn_erase.to(torch.double), self_attn.to(torch.double), self_attn_erase.to(torch.double) 95 | attn_loss = .0 96 | 97 | if self.lambda_self_retain or self.lambda_self_erase: 98 | mask = self.calc_mask(attn) 99 | 100 | h, w, seq_len = attn.shape 101 | attn = attn.reshape(h*w, seq_len).unsqueeze(0) 102 | attn_erase = attn_erase.reshape(h*w, seq_len).unsqueeze(0) 103 | 104 | if self.lambda_retain: 105 | attn_loss += self.lambda_retain * self.calc_retain_loss(attn, attn_erase) 106 | 107 | if self.lambda_erase: 108 | attn_loss += self.lambda_erase * self.calc_erase_loss(attn, attn_erase) 109 | 110 | if self.lambda_self_retain: 111 | attn_loss += self.lambda_self_retain * self.calc_self_retain_loss(self_attn, self_attn_erase, mask) 112 | 113 | if self.lambda_self_erase: 114 | attn_loss += self.lambda_self_erase * self.calc_self_erase_loss(self_attn, self_attn_erase, mask) 115 | 116 | loss = attn_loss 117 | return loss -------------------------------------------------------------------------------- /eval/configs/audiocaps/edit_replace.csv: -------------------------------------------------------------------------------- 1 | Uid,Caption,edit_Caption 2 | 5er9MoTAogc_10000_20000,A baby crying loudly,A baby laughing loudly 3 | z3gvS28om7o_180000_190000,A woman giving a speech,A man giving a speech 4 | qjwDugPZMjA_210000_220000,A crowd of people applauding,A crowd of people laughing 5 | P4Rf3_mKc4s_30000_40000,A woman is performing a speech,A man is performing a speech 6 | USle1qAiOdA_0_10000,A cat meowing twice,A dog barking twice 7 | DBjQUbJNHwc_30000_40000,A woman is giving a speech,A man is giving a speech 8 | xzQ9Q4UKOjY_90000_100000,A baby is crying,A baby is laughing 9 | zvLcOfhGcGo_0_10000,A person snoring,A person applause 10 | SaUDkGYRZHE_0_10000,A man laughing,A man crying 11 | GDMI4uJfDVw_10000_20000,A woman speaking,A woman crying 12 | UZqMaHTN6SE_0_10000,A telephone rings several times,A sirens rings several times 13 | zpMwBUIuxgY_360000_370000,Typing on a computer keyboard,Typing on a computer Piano 14 | z4omk5iedYc_80000_90000,A toilet is flushed,A engine is flushed 15 | ypY1zYRcfIc_100000_110000,A toilet flushing,A thunder flushing 16 | jEnNUSbVz3c_0_10000,Whistling a tune,Piano a tune 17 | 5p9XLBtfesc_330000_340000,A man speaks followed by applause,A man speaks followed by sirens 18 | Ekn0Q03AFN0_30000_40000,A woman speeches,A woman laughing 19 | zxvv9NC86uI_30000_40000,A young woman talking,A young woman laughing 20 | pvy7D5MvdIE_20000_30000,A man speaks followed by a crowd applauding,A man speaks followed by a crowd sirens 21 | 4kJQOM9BN4g_28000_38000,An engine is accelerating,An thunder is accelerating 22 | Mcvgo80QdlI_10000_20000,A person is snoring steadily,A person is crying steadily 23 | zssgRs7HUmM_30000_40000,A clock ticking,A engine ticking 24 | zD2KfYF4e2Q_10000_20000,Musical whistling,Piano whistling 25 | 8aNj2ItTaIs_180000_190000,Wind blows hard,thunder blows hard 26 | 78ER5FzxYdQ_30000_40000,Birds singing outdoors,dog barking outdoors 27 | yZpTj9YT-IQ_30000_40000,A man speaking into a microphone,A man laughing into a microphone 28 | yc6NJXKEIEk_30000_40000,An engine revving,An sirens revving 29 | Jzc66pasXSg_110000_120000,Repetitive snoring,Repetitive laughing 30 | 4wjKtAs9aMQ_29000_39000,An engine is idling,An thunder is idling 31 | ltQBJIueFT0_30000_40000,A male voice speaking,A male voice laughing 32 | 4Jo3aJriewE_30000_40000,A woman delivers a speech,A man delivers a speech 33 | NFnPqqOeG_A_280000_290000,Large bells ringing,Large thunder ringing 34 | z-3jiJkrDQE_10000_20000,A person is typing on a computer keyboard,A person is typing on a computer Piano 35 | xzQ9Q4UKOjY_90000_100000,A baby is crying,A baby is laughing 36 | qyfZgjNoJlE_0_10000,Continuous laughter,Continuous crying 37 | yvZWpypX7_Y_30000_40000,A woman speaking continuously,A woman applause continuously 38 | eK-tTmyMlgI_0_10000,Birds are chirping,dogs are chirping 39 | yizOr1hPxc8_70000_80000,A helicopter engine running idle,A car engine running idle 40 | 1MUcHcqDJfo_23000_33000,Car engine idling,Helicopter engine idling 41 | C9DKBh_c4xA_30000_40000,An elderly man speaking,An young man speaking 42 | ApqTL3dFTdM_310000_320000,A high pitched engine idling,A high pitched dog idling 43 | AhHW1jNf_cw_30000_40000,A man speaking followed by rustling,A man speaking followed by thunder 44 | twOUIlaeVm8_30000_40000,A baby crying continuously,A baby laughing continuously 45 | K6udqujyKFo_20000_30000,A person laughs,A person applause 46 | ikSxpwLxwKk_200000_210000,Adult female speaking,Little female speaking 47 | 9wFPkAuiOuI_280000_290000,Two men speaking,Two men laughing 48 | ztI9tn8Lfnk_9000_19000,Clicking on a computer keyboard,Clicking on a computer guitar 49 | 91EvnH5ln4Q_30000_40000,A female speaking,A female typing 50 | v-RSiQKNgM8_30000_40000,A sewing machine operating,A sewing engine operating 51 | sLPAcgyETPM_30000_40000,Female speaking,Female laughing 52 | pmeh7ocxLP4_0_10000,Continuous snoring,Continuous raining 53 | DBjQUbJNHwc_30000_40000,A woman is giving a speech,A woman is giving a piano 54 | 95vFDbFrq5g_30000_40000,A male speaking,A male typing 55 | zNLca6Dh6-k_120000_130000,Vibrations from a sewing machine,Vibrations from a sewing engine 56 | zO1h2bKYe58_140000_150000,An idle vehicle engine running,An idle helicopter engine running 57 | jcf9Smq-0Ps_20000_30000,Toilet flushing,Thunder flushing 58 | zNW0z-SHXTM_70000_80000,Humming of an idling engine,Humming of an idling thunder 59 | o7Nra5Cw_jQ_20000_30000,A telephone bell rings,A heavy sirens rings 60 | rGT-FW1kadU_30000_40000,"Heavy, continuous wind","Heavy, continuous whistles" 61 | ui4PHURhahA_100000_110000,A duck quacking,A dog barking 62 | BDDxfr4xf0k_60000_70000,A helicopter running,A car running 63 | 3y-3xiz2U5M_110000_120000,A power tool is in use,A piano kit is in use 64 | -ftxbusleO8_30000_40000,An engine running,An thunder running 65 | EGve_FMNOwE_30000_40000,A woman is speaking,A woman is laughing 66 | IEI10aLmHXM_30000_40000,A man is giving a speech,A man is giving a applause 67 | yWOeusquiJk_20000_30000,A person is snoring,A person is speaking 68 | U3COtQZORMU_20000_30000,A loud engine idling,A loud thunder idling 69 | ozQ428JESb8_110000_120000,A young girl giving a speech,A young girl giving a piano 70 | zNW0z-SHXTM_70000_80000,Humming of an idling engine,Humming of an dog barking 71 | z_zkX-8Kf3Q_0_10000,A crowd applauds,A crowd sirens 72 | gcF9BFScQHU_170000_180000,Multiple bells ringing,Multiple thunder ringing 73 | q4yBn1ECb5U_50000_60000,A toilet flushes,A sirens flushes 74 | yjLvzh0s1Ug_0_10000,A vehicle engine revving several times,A helicopter engine revving several times 75 | zNW0z-SHXTM_70000_80000,Humming of an idling engine,Humming of an idling cat 76 | z2QsWDZMXd0_30000_40000,Ticking of a clock,Ticking of a bell 77 | zRiGmSlWjok_200000_210000,"Loud, continuous applause","Loud, continuous thunder" 78 | ydO6ZIcghzU_0_10000,A person whistling,A person shouting 79 | ApMyWG-5x6M_20000_30000,An engine idles loudly,An car idles loudly 80 | d03ZdivCSKM_30000_40000,A baby cries continuously,A baby laughing continuously 81 | 8As1owPi19k_490000_500000,A man speaking then whistling,A man speaking then raining 82 | zf9Uk3HjD4k_20000_30000,A telephone ringing,A motorbike ringing 83 | Qtdw1gRUsLM_560000_570000,Several large bells ring,Several large clock ring 84 | xFndh2QPZG0_30000_40000,An engine running consistently,An dog running consistently 85 | zssgRs7HUmM_30000_40000,A clock ticking,A bell ticking 86 | UuST6ut8oEY_210000_220000,"A long, loud burp","A long, loud explosion" 87 | h5DUE-hRb5Y_30000_40000,Someone is whistling,Someone is laughing 88 | yPCgybma0Jc_310000_320000,Food frying in a pan,Food frying in a mouth 89 | v-RSiQKNgM8_30000_40000,A sewing machine operating,A sewing gunshot operating 90 | GEBMPrzW0NI_30000_40000,A man is speaking,A man is crying 91 | ns6btoVV_pA_20000_30000,A motorbike engine running idle,A helicopter engine running idle 92 | -------------------------------------------------------------------------------- /prompt2prompt/attn_control.py: -------------------------------------------------------------------------------- 1 | import abc 2 | 3 | import numpy as np 4 | import torch 5 | from IPython.display import display 6 | from PIL import Image 7 | from typing import Union, Tuple, List, Dict, Optional 8 | import matplotlib.pyplot as plt 9 | 10 | from prompt2prompt import ptp_utils 11 | 12 | LOW_RESOURCE = True 13 | NUM_DDIM_STEPS = 100 14 | 15 | class EmptyControl: 16 | 17 | def step_callback(self, x_t): 18 | return x_t 19 | 20 | def between_steps(self): 21 | return 22 | 23 | def __call__(self, attn, is_cross: bool, place_in_unet: str): 24 | return attn 25 | 26 | class AttentionControl(abc.ABC): 27 | 28 | def step_callback(self, x_t): 29 | return x_t 30 | 31 | def between_steps(self): 32 | return 33 | 34 | @property 35 | def num_uncond_att_layers(self): 36 | return self.num_att_layers if LOW_RESOURCE else 0 37 | 38 | @abc.abstractmethod 39 | def forward(self, attn, is_cross: bool, place_in_unet: str): 40 | raise NotImplementedError 41 | 42 | def __call__(self, attn, is_cross: bool, place_in_unet: str): 43 | if self.cur_att_layer >= self.num_uncond_att_layers: 44 | if LOW_RESOURCE: 45 | attn = self.forward(attn, is_cross, place_in_unet) 46 | else: 47 | h = attn.shape[0] 48 | attn[h // 2:] = self.forward(attn[h // 2:], is_cross, place_in_unet) 49 | self.cur_att_layer += 1 50 | if self.cur_att_layer == self.num_att_layers + self.num_uncond_att_layers: 51 | self.cur_att_layer = 0 52 | self.cur_step += 1 53 | self.between_steps() 54 | return attn 55 | 56 | def reset(self): 57 | self.cur_step = 0 58 | self.cur_att_layer = 0 59 | 60 | def __init__(self): 61 | self.cur_step = 0 62 | self.num_att_layers = -1 63 | self.cur_att_layer = 0 64 | 65 | 66 | class SpatialReplace(EmptyControl): 67 | 68 | def step_callback(self, x_t): 69 | if self.cur_step < self.stop_inject: 70 | b = x_t.shape[0] 71 | x_t = x_t[:1].expand(b, *x_t.shape[1:]) 72 | return x_t 73 | 74 | def __init__(self, stop_inject: float): 75 | super(SpatialReplace, self).__init__() 76 | self.stop_inject = int((1 - stop_inject) * NUM_DDIM_STEPS) 77 | 78 | 79 | class AttentionStore(AttentionControl): 80 | 81 | @staticmethod 82 | def get_empty_store(): 83 | return {"down_cross": [], "mid_cross": [], "up_cross": [], 84 | "down_self": [], "mid_self": [], "up_self": []} 85 | 86 | def forward(self, attn, is_cross: bool, place_in_unet: str): 87 | key = f"{place_in_unet}_{'cross' if is_cross else 'self'}" 88 | if attn.shape[1] <= 32 ** 2: # avoid memory overhead 89 | self.step_store[key].append(attn) 90 | return attn 91 | 92 | def between_steps(self): 93 | if len(self.attention_store) == 0: 94 | self.attention_store = self.step_store 95 | else: 96 | for key in self.attention_store: 97 | for i in range(len(self.attention_store[key])): 98 | self.attention_store[key][i] += self.step_store[key][i] 99 | self.step_store = self.get_empty_store() 100 | 101 | def get_average_attention(self): 102 | average_attention = {key: [item / self.cur_step for item in self.attention_store[key]] for key in 103 | self.attention_store} 104 | return average_attention 105 | 106 | def reset(self): 107 | super(AttentionStore, self).reset() 108 | self.step_store = self.get_empty_store() 109 | self.attention_store = {} 110 | 111 | def __init__(self, token_indices: List[int], alpha: float, method: str, cross_retain_steps: float, n: int, iter_each_step: int, max_step_to_erase: int, 112 | lambda_retain=1, lambda_erase=-.5, lambda_self_retain=1, lambda_self_erase=-.5): 113 | super(AttentionStore, self).__init__() 114 | self.step_store = self.get_empty_store() 115 | self.attention_store = {} 116 | self.baseline = True 117 | # for suppression content 118 | self.ddim_inv = False 119 | self.token_indices = token_indices 120 | self.uncond = True 121 | self.alpha = alpha 122 | self.method = method # default: 'soft-weight' 123 | self.i = None 124 | self.cross_retain_steps = cross_retain_steps * NUM_DDIM_STEPS 125 | self.n = n 126 | self.text_embeddings_erase = None 127 | self.iter_each_step = iter_each_step 128 | self.MAX_STEP_TO_ERASE = max_step_to_erase 129 | # lambds of loss 130 | self.lambda_retain = lambda_retain 131 | self.lambda_erase = lambda_erase 132 | self.lambda_self_retain = lambda_self_retain 133 | self.lambda_self_erase = lambda_self_erase 134 | 135 | 136 | def aggregate_attention(prompts, attention_store: AttentionStore, res: List[int], from_where: List[str], is_cross: bool, select: int): 137 | out = [] 138 | attention_maps = attention_store.get_average_attention() 139 | num_pixels = res[0] * res[1] 140 | for location in from_where: 141 | for item in attention_maps[f"{location}_{'cross' if is_cross else 'self'}"]: 142 | if item.shape[1] == num_pixels: 143 | cross_maps = item.reshape(len(prompts), -1, res[0], res[1], item.shape[-1])[select] 144 | out.append(cross_maps) 145 | out = torch.cat(out, dim=0) 146 | out = out.sum(0) / out.shape[0] 147 | return out.cpu() 148 | 149 | def show_cross_attention(stable, prompts, attention_store: AttentionStore, res: List[int], from_where: List[str], select: int = 0, image_size: List[int]=[1024, 256], num_rows: int = 1, font_scale=2, thickness=4, cmap_name="plasma", save_name="null-text+ptp"): 150 | # tokens = stable.tokenizer_list[0].encode(prompts[select]) 151 | # decoder = stable.tokenizer_list[0].decode # auffusion-full 152 | tokens = stable.tokenizer.encode(prompts[select]) 153 | decoder = stable.tokenizer.decode 154 | attention_maps = aggregate_attention(prompts, attention_store, res, from_where, True, select) 155 | images = [] 156 | 157 | cmap = plt.get_cmap(cmap_name) 158 | cmap_r = cmap.reversed() 159 | 160 | for i in range(len(tokens)): 161 | image = attention_maps[:, :, i] 162 | image = 255 * image / image.max() 163 | image = image.unsqueeze(-1).expand(*image.shape, 3) 164 | image = image.numpy().astype(np.uint8) 165 | 166 | image = cmap(np.array(image)[:,:,0])[:, :, :3] 167 | image = (image - image.min()) / (image.max() - image.min()) 168 | image = Image.fromarray(np.uint8(image*255)) 169 | image = np.array(image.resize(image_size)) 170 | 171 | image = ptp_utils.text_under_image(image, decoder(int(tokens[i])), font_scale=font_scale, thickness=thickness) 172 | images.append(image) 173 | ptp_utils.view_images(np.stack(images, axis=0), num_rows=num_rows, save_name=save_name) -------------------------------------------------------------------------------- /eval/configs/audiocaps/edit_delete.csv: -------------------------------------------------------------------------------- 1 | Uid,Caption,edit_Caption 2 | 1AezQf37NWA_50000_60000,A man speaks and then whistles,A man speaks and then 3 | z_cfAqGxyT4_40000_50000,Wind blowing and water splashing,Wind blowing and 4 | X1zntKNqjYE_110000_120000,A man speaking as birds are chirping,A man speaking as 5 | TV2uGumcGIs_190000_200000,A man is speaking as paper is crumpling,A man is speaking as 6 | HIQq6nxHI_M_30000_40000,A motorboat engine running as wind blows into a microphone,A motorboat engine running as into a microphone 7 | sZsLJe0VAvA_540000_550000,A helicopter flying as wind blows into a microphone,A helicopter flying as into a microphone 8 | y9RB_DvaqcI_30000_40000,A crowd of people applauding and cheering,A crowd of people and cheering 9 | 19vv-oL7w5c_0_10000,"Water is falling, splashing and gurgling","Water is falling, splashing and" 10 | 912ulwC5d6M_25000_35000,A baby crying and breathing,A baby crying and 11 | 8e6KCFH_58g_170000_180000,A man speaking and birds chirping,A man speaking and 12 | mPf3TRcdH-E_130000_140000,Birds chirping and tweeting,Birds and tweeting 13 | ugxHt-50Z3M_530000_540000,A woman speaks as food sizzles in a pan,A woman speaks as in a pan 14 | RVUGxY7T0Lc_70000_80000,Ocean waves crashing as wind blows into a microphone,Ocean waves crashing as into a microphone 15 | f2ztuNPUUVk_15000_25000,A woman speaks and a cat meows,A woman speaks and a 16 | gD-RsNa2m-g_140000_150000,Typing on a keyboard with a man speaking,Typing on a keyboard with a 17 | YM8auY6sjAs_140000_150000,An idle vehicle engine running as wind blows into a microphone,An idle vehicle engine running as into a microphone 18 | fQbHCvgLWlY_50000_60000,A man speaks while water splashes,A man speaks while 19 | IfvNGeTUJ6U_10000_20000,A vehicle engine running and revving several times,A vehicle engine running and several times 20 | 8uS54KknUZA_30000_40000,Water trickling while a man speaks,Water trickling while 21 | Fm9MMRvkax0_50000_60000,A girl laughing as a person is snoring,A girl laughing as a person is 22 | 8LjypIOwEns_40000_50000,A powerful engine revs as it idles,A powerful engine revs as it 23 | eiPzGovKdZs_160000_170000,Water splashes and wind blows,Water splashes and 24 | XXVQ1NVI7Bk_100000_110000,Rough sanding and scraping,Rough and scraping 25 | 3oT4NTXrokU_490000_500000,A woman and a child speaking,A woman and a speaking 26 | 1ahjgFjrMbc_30000_40000,A man is speaking and a dog is barking,A man is speaking and a 27 | 2H3miNoF_Z8_30000_40000,A cat meows and hisses,A cat meows and 28 | 20JihmwTTr8_30000_40000,Race cars are passing by,Race are passing by 29 | Awre04t_FEY_15000_25000,Engine running and revving,Engine running and 30 | FmRyBPgemi4_190000_200000,A man is speaking as birds are chirping,A man is speaking as 31 | zZ3FzOY06CI_30000_40000,Gurgling and splashing water,Gurgling and 32 | fKPuVSiWpR4_560000_570000,Male speaking and birds chirping,Male speaking and 33 | f_sOWxFAi1g_30000_40000,Humming of an engine with people speaking,Humming of an engine with 34 | KU-A8g4wCU4_100000_110000,A man talks and then whistles,A man talks and then 35 | 2Sb8aPFdwFU_30000_40000,Water splashing and birds chirping,Water splashing and 36 | ylbRydhutFc_30000_40000,A man talking as pigeons coo and bird wings flap,A man talking as and bird wings flap 37 | l-l9_Byf9hg_240000_250000,A man speaks as water splashes,A man speaks as 38 | 5FrqyZr00wQ_2000_12000,A car horn honks and echoes,A car horn honks and 39 | 9OVV5Gd6pkM_14000_24000,An engine revving as it idles,An engine revving as 40 | LFVvHMNGhmI_30000_40000,Ocean waves crashing as wind blows heavily into a microphone,Ocean waves crashing as heavily into a microphone 41 | yGutSfyK4y8_30000_40000,Water splashing followed by a woman speaking,Water splashing followed by a 42 | mPf3TRcdH-E_130000_140000,Birds chirping and tweeting,Birds and tweeting 43 | B6bTIIl46fA_30000_40000,Water splashes and wind blows as men speak,Water splashes and as men speak 44 | xesCkDs3tQY_30000_40000,A vehicle engine revving and accelerating,A vehicle engine revving and 45 | BFih5FgzcRU_30000_40000,A man speaking with murmuring in the background,A man speaking with in the background 46 | 48sv95kLSVI_530000_540000,An adult male is speaking and typing on a keyboard,An adult male is speaking and 47 | 3i3V8pd6xOg_30000_40000,A woman speaks and a crowd applauds,A woman speaks and a crowd 48 | G-SUDENc2sc_30000_40000,A man and woman speak,A man and speak 49 | n67Nl29gqKk_90000_100000,A baby cries and people speak,A and people speak 50 | qcqZflfGeOw_10000_20000,A baby and a woman laugh,A baby and a woman 51 | soLjCy73csI_0_10000,An aircraft flying in the distance as wind blows into a microphone,An aircraft flying in the distance as into a microphone 52 | eiPzGovKdZs_160000_170000,Water splashes and wind blows,Water splashes and 53 | bS2PSfgW6J4_60000_70000,People laugh and speak,People and speak 54 | B4Aas8bd_Zg_30000_40000,Emergency sirens and muffled speech,and muffled speech 55 | qnIrh8pROYo_20000_30000,Water splashes and a man speaks,Water splashes and a 56 | Qdw-mzhWh-I_30000_40000,Gunshots and explosions,and explosions 57 | 8K2szL8ZtFU_30000_40000,A cat meowing and whining,A cat and whining 58 | y7_FciM9kXY_0_10000,A toilet flushes and water drains,A toilet flushes and 59 | EHBlH_kffh4_0_10000,Humming of engines with people speaking,Humming of engines with 60 | RVUGxY7T0Lc_70000_80000,Ocean waves crashing as wind blows into a microphone,Ocean waves crashing as into a microphone 61 | 3oaXDeQTEPQ_30000_40000,A man is giving a speech and a crowd cheers,A man is giving a speech and a 62 | tjq0Z9IJw7Q_250000_260000,A man speaks as water runs,A man speaks as 63 | VlusD3K-8iY_190000_200000,Birds chirping and singing,Birds and singing 64 | HH9n0qP-HCM_30000_40000,An engine idling with light wind,An engine idling with 65 | HQYDO1uOreg_150000_160000,Whistling with music playing,with music playing 66 | BPNURHcm86I_0_10000,Humming of an engine with sirens ringing,Humming of an engine with 67 | 8KwqaI9PJuk_7000_17000,A crowd applauding and cheering,A crowd and cheering 68 | jtMmwfxDEKA_30000_40000,A baby crying and cooing,A baby crying and 69 | DvJAnh2ywLI_30000_40000,A man speaks as pigeons coo and wings flap,A man speaks as pigeons coo and 70 | 779sKwaX_Hc_120000_130000,A woman speaks and food sizzles,A woman speaks and 71 | Lsv7HQizztA_0_10000,A car engine revs as it passes by,A car as it passes by 72 | DlyUeQGh1hE_110000_120000,A man speaking followed by a crowd of people applauding and cheering,A man speaking followed by a crowd of people and cheering 73 | a6Vih-pDIiI_0_10000,An engine starting and running,An engine and running 74 | 4i7Z_Z8gd7E_120000_130000,A crowd cheers and a man speaks,A crowd cheers and a 75 | y9RB_DvaqcI_30000_40000,A crowd of people applauding and cheering,A crowd of people and cheering 76 | 50Qv80Ykg7U_30000_40000,A woman and child speak,A woman and speak 77 | hUUvJlkjso8_110000_120000,Wind blowing and waves crashing,and waves crashing 78 | mheuTFdwNcE_140000_150000,A crowd of people cheering and applauding,A crowd of people and applauding 79 | G-SUDENc2sc_30000_40000,A man and woman speak,A man and speak 80 | RVUGxY7T0Lc_70000_80000,Ocean waves crashing as wind blows into a microphone,Ocean waves crashing as into a microphone 81 | jpqUA3PKge4_140000_150000,A man speaks as food sizzles,A man speaks as 82 | z8MRUCmCjyE_30000_40000,Continuous clanking and rustling,Continuous clanking and 83 | u0V7wclNZlU_30000_40000,Birds chirping and rustling,Birds and rustling 84 | fRZs8uHQCpg_10000_20000,A mid-size motor vehicle engine is idling and vibrating,A mid-size motor vehicle engine is idling and 85 | 6XwkRqJoffU_30000_40000,Emergency vehicle driving with siren on,Emergency vehicle driving with on 86 | 8sZtN1zgPwA_130000_140000,Water flowing and splashing,Water flowing and 87 | 27hWTLzVf3U_80000_90000,A vehicle passes by,A vehicle passes by 88 | VbCQjFyiR2I_170000_180000,Water flows and splashes,Water flows and 89 | zZ3FzOY06CI_30000_40000,Gurgling and splashing water,Gurgling and 90 | zD7X2XSrEFM_30000_40000,Pigeons coo and flap their wings,Pigeons and flap their wings 91 | KhIivfOIuYI_80000_90000,A man is speaking as food is frying,A man is speaking as food is 92 | yE89ARB4Veo_30000_40000,Pigeons cooing and flapping their wings,Pigeons and flapping their wings 93 | 2JdEPktKxj8_30000_40000,"Dogs are barking and growling, and an adult male speaks",", and an adult male speaks" 94 | 7jx1bu0M9fk_140000_150000,Water splashes and gurgles,Water splashes and 95 | 3Vs6jCyH5vg_10000_20000,Snoring and heavy breathing,Snoring and heavy 96 | ylbRydhutFc_30000_40000,A man talking as pigeons coo and bird wings flap,A as pigeons coo and bird wings flap 97 | nQI-AxPN3vc_30000_40000,Humming of an engine with some rustling,Humming of an engine with some 98 | 4bvp9YmvRmU_30000_40000,People are talking and water is splashing,People are talking and 99 | EcDQzSQ-d_I_30000_40000,A baby crying and a man speaking,A baby crying and 100 | -------------------------------------------------------------------------------- /suppresseot/run_and_display.py: -------------------------------------------------------------------------------- 1 | import torch 2 | import numpy as np 3 | from tqdm import tqdm 4 | from typing import Optional, List 5 | 6 | from prompt2prompt import ptp_utils 7 | from prompt2prompt.attn_control import aggregate_attention 8 | from suppresseot import wo_utils 9 | from suppresseot.attn_loss import AttnLoss 10 | 11 | 12 | LOW_RESOURCE = True 13 | 14 | def update_context(context: torch.Tensor, loss: torch.Tensor, scale: int, factor: float) -> torch.Tensor: 15 | """ 16 | Update the text embeddings according to the attention loss. 17 | 18 | :param context: text embeddings to be updated 19 | :param loss: ours loss 20 | :param factor: factor for update text embeddings. 21 | :return: 22 | """ 23 | grad_cond = torch.autograd.grad(outputs=loss.requires_grad_(True), inputs=[context], retain_graph=False)[0] 24 | context = context - (scale * factor) * grad_cond 25 | return context 26 | 27 | @torch.no_grad() 28 | def text2image_ldm_stable( 29 | model, 30 | prompt: List[str], 31 | controller, 32 | num_inference_steps: int = 50, 33 | guidance_scale: Optional[float] = 7.5, 34 | generator: Optional[torch.Generator] = None, 35 | latent: Optional[torch.FloatTensor] = None, 36 | uncond_embeddings=None, 37 | start_time=100, 38 | return_type='image' 39 | ): 40 | ptp_utils.register_attention_control(model, controller) 41 | height = 256 42 | width = 1024 43 | batch_size = len(prompt) 44 | 45 | # text_input = model.tokenizer_list[0]( # auffusion-full 46 | text_input = model.tokenizer( 47 | prompt, 48 | padding="max_length", 49 | # max_length=model.tokenizer_list[0].model_max_length, # auffusion-full 50 | max_length=model.tokenizer.model_max_length, 51 | truncation=True, 52 | return_tensors="pt", 53 | ) 54 | # text_embeddings = model.text_encoder_list[0](text_input.input_ids.to(model.device))[0] # auffusion-full 55 | text_embeddings = model.text_encoder(text_input.input_ids.to(model.device))[0] 56 | 57 | max_length = text_input.input_ids.shape[-1] 58 | if uncond_embeddings is None: 59 | # uncond_input = model.tokenizer_list[0]([""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt") # auffusion-full 60 | uncond_input = model.tokenizer([""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt") 61 | # uncond_embeddings_ = model.text_encoder_list[0](uncond_input.input_ids.to(model.device))[0] # auffusion-full 62 | uncond_embeddings_ = model.text_encoder(uncond_input.input_ids.to(model.device))[0] 63 | scale = 20 64 | else: 65 | uncond_embeddings_ = None 66 | scale = 5 67 | 68 | latent, _ = ptp_utils.init_latent(latent, model, height, width, generator, batch_size) 69 | 70 | _latent, _latent_erase = latent.clone().to(model.device), latent.clone().to(model.device) 71 | latents = torch.cat([_latent, _latent_erase]) 72 | 73 | attn_loss_func = AttnLoss(model.device, 'cosine', controller.n, controller.token_indices, 74 | controller.lambda_retain, controller.lambda_erase, controller.lambda_self_retain, controller.lambda_self_erase) 75 | 76 | model.scheduler.set_timesteps(num_inference_steps) 77 | # text embedding for erasing 78 | controller.text_embeddings_erase = text_embeddings.clone() 79 | 80 | scale_range = np.linspace(1., .1, len(model.scheduler.timesteps)) 81 | pbar = tqdm(model.scheduler.timesteps[-start_time:], desc='Suppress EOT', ncols=100, colour="red") 82 | for i, t in enumerate(pbar): 83 | if uncond_embeddings_ is None: 84 | context = torch.cat([uncond_embeddings[i].expand(*text_embeddings.shape), text_embeddings]) 85 | if LOW_RESOURCE: 86 | context = (uncond_embeddings[i].expand(*text_embeddings.shape), text_embeddings) 87 | else: 88 | context = torch.cat([uncond_embeddings_, text_embeddings]) 89 | if LOW_RESOURCE: 90 | context = (uncond_embeddings_, text_embeddings) 91 | controller.i = i 92 | 93 | # conditional branch: erase content for text embeddings 94 | if controller.i >= controller.cross_retain_steps: 95 | controller.text_embeddings_erase = \ 96 | wo_utils.woword_eot_context(text_embeddings.clone(), controller.token_indices, controller.alpha, 97 | controller.method, controller.n) 98 | 99 | controller.baseline = True 100 | if controller.MAX_STEP_TO_ERASE > controller.i >= controller.cross_retain_steps and not (controller.text_embeddings_erase == text_embeddings).all() and \ 101 | (attn_loss_func.lambda_retain or attn_loss_func.lambda_erase or attn_loss_func.lambda_self_retain or attn_loss_func.lambda_self_erase): 102 | controller.uncond = False 103 | controller.cur_att_layer = 32 # w=1, skip unconditional branch 104 | controller.attention_store = {} 105 | noise_prediction_text = model.unet(_latent, t, encoder_hidden_states=text_embeddings)["sample"] 106 | attention_maps = aggregate_attention(controller, 16, ["up", "down"], is_cross=True) 107 | self_attention_maps = aggregate_attention(controller, 16, ["up", "down", "mid"], is_cross=False) 108 | 109 | del noise_prediction_text 110 | # update controller.text_embeddings_erase for some timestep 111 | iter = controller.iter_each_step 112 | while iter > 0: 113 | with torch.enable_grad(): 114 | controller.cur_att_layer = 32 # w=1, skip unconditional branch 115 | controller.attention_store = {} 116 | # conditional branch 117 | text_embeddings_erase = controller.text_embeddings_erase.clone().detach().requires_grad_(True) 118 | # forward pass of conditional branch with text_embeddings_erase 119 | noise_prediction_text = model.unet(_latent_erase, t, encoder_hidden_states=text_embeddings_erase)["sample"] 120 | model.unet.zero_grad() 121 | attention_maps_erase = aggregate_attention(controller, 16, ["up", "down", "mid"], is_cross=True) 122 | self_attention_maps_erase = aggregate_attention(controller, 16, ["up", "down", "mid"], is_cross=False) 123 | 124 | # attention loss 125 | loss = attn_loss_func(attention_maps, attention_maps_erase, self_attention_maps, self_attention_maps_erase) 126 | if loss != .0: 127 | pbar.set_postfix({'loss': loss if isinstance(loss, float) else loss.item()}) 128 | text_embeddings_erase = update_context(context=text_embeddings_erase, loss=loss, 129 | scale=scale, factor=np.sqrt(scale_range[i])) 130 | del noise_prediction_text 131 | torch.cuda.empty_cache() 132 | controller.text_embeddings_erase = text_embeddings_erase.clone().detach().requires_grad_(False) 133 | iter -= 1 134 | 135 | # "uncond_embeddings_ is None" for real images, "uncond_embeddings_ is not None" for generated images. 136 | context_erase = (uncond_embeddings[i].expand(*text_embeddings.shape), controller.text_embeddings_erase) \ 137 | if uncond_embeddings_ is None else (uncond_embeddings_, controller.text_embeddings_erase) 138 | controller.attention_store = {} 139 | controller.baseline = False 140 | contexts = [torch.cat([context[0], context_erase[0]]), torch.cat([context[1], context_erase[1]])] 141 | latents = ptp_utils.diffusion_step(model, controller, latents, contexts, t, guidance_scale, low_resource=LOW_RESOURCE) 142 | _latent, _latent_erase = latents 143 | _latent, _latent_erase = _latent.unsqueeze(0), _latent_erase.unsqueeze(0) 144 | 145 | if return_type == 'image': 146 | latents = 1 / 0.18215 * latents.detach() 147 | _image = model.vae.decode(latents)['sample'] 148 | _image = (_image / 2 + 0.5).clamp(0, 1) 149 | null_edit = _image.cpu().permute(0, 2, 3, 1).numpy()[0] 150 | eot_edit = _image.cpu().permute(0, 2, 3, 1).numpy()[1] 151 | image = np.stack((null_edit, eot_edit), axis=0) 152 | else: 153 | image = latents 154 | return image, latent 155 | 156 | def run_and_display(ldm_stable, prompts, controller, latent=None, generator=None, uncond_embeddings=None, args=None): 157 | images, x_t = text2image_ldm_stable(ldm_stable, prompts, controller, latent=latent, 158 | num_inference_steps=args.num_inference_steps, guidance_scale=args.guidance_scale, 159 | generator=generator, uncond_embeddings=uncond_embeddings) 160 | return images, x_t -------------------------------------------------------------------------------- /null_text_inversion/null_text_inversion.py: -------------------------------------------------------------------------------- 1 | # https://github.com/google/prompt-to-prompt 2 | 3 | from typing import Optional, Union, Tuple, List, Callable, Dict 4 | from tqdm import tqdm 5 | import torch 6 | 7 | from diffusers import StableDiffusionPipeline, DDIMScheduler 8 | import torch.nn.functional as nnf 9 | import numpy as np 10 | # import seq_aligner 11 | from torch.optim.adam import Adam 12 | from PIL import Image 13 | 14 | # from prompt2prompt import ptp_utils 15 | 16 | NUM_DDIM_STEPS = 100 17 | GUIDANCE_SCALE_TRAIN = 7.5 18 | 19 | class NullInversion: 20 | 21 | def prev_step(self, model_output: Union[torch.FloatTensor, np.ndarray], timestep: int, 22 | sample: Union[torch.FloatTensor, np.ndarray]): 23 | prev_timestep = timestep - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps 24 | alpha_prod_t = self.scheduler.alphas_cumprod[timestep] 25 | alpha_prod_t_prev = self.scheduler.alphas_cumprod[ 26 | prev_timestep] if prev_timestep >= 0 else self.scheduler.final_alpha_cumprod 27 | beta_prod_t = 1 - alpha_prod_t 28 | pred_original_sample = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 29 | pred_sample_direction = (1 - alpha_prod_t_prev) ** 0.5 * model_output 30 | prev_sample = alpha_prod_t_prev ** 0.5 * pred_original_sample + pred_sample_direction 31 | return prev_sample 32 | 33 | def next_step(self, model_output: Union[torch.FloatTensor, np.ndarray], timestep: int, 34 | sample: Union[torch.FloatTensor, np.ndarray]): 35 | timestep, next_timestep = min( 36 | timestep - self.scheduler.config.num_train_timesteps // self.scheduler.num_inference_steps, 999), timestep 37 | alpha_prod_t = self.scheduler.alphas_cumprod[timestep] if timestep >= 0 else self.scheduler.final_alpha_cumprod 38 | alpha_prod_t_next = self.scheduler.alphas_cumprod[next_timestep] 39 | beta_prod_t = 1 - alpha_prod_t 40 | next_original_sample = (sample - beta_prod_t ** 0.5 * model_output) / alpha_prod_t ** 0.5 41 | next_sample_direction = (1 - alpha_prod_t_next) ** 0.5 * model_output 42 | next_sample = alpha_prod_t_next ** 0.5 * next_original_sample + next_sample_direction 43 | return next_sample 44 | 45 | def get_noise_pred_single(self, latents, t, context): 46 | noise_pred = self.model.unet(latents, t, encoder_hidden_states=context)["sample"] 47 | return noise_pred 48 | 49 | def get_noise_pred(self, latents, t, is_forward=True, context=None): 50 | latents_input = torch.cat([latents] * 2) 51 | if context is None: 52 | context = self.context 53 | guidance_scale = 1 if is_forward else GUIDANCE_SCALE_TRAIN 54 | noise_pred = self.model.unet(latents_input, t, encoder_hidden_states=context)["sample"] 55 | noise_pred_uncond, noise_prediction_text = noise_pred.chunk(2) 56 | noise_pred = noise_pred_uncond + guidance_scale * (noise_prediction_text - noise_pred_uncond) 57 | if is_forward: 58 | latents = self.next_step(noise_pred, t, latents) 59 | else: 60 | latents = self.prev_step(noise_pred, t, latents) 61 | return latents 62 | 63 | @torch.no_grad() 64 | def latent2image(self, latents, return_type='np'): 65 | latents = 1 / 0.18215 * latents.detach() 66 | image = self.model.vae.decode(latents)['sample'] 67 | if return_type == 'np': 68 | image = (image / 2 + 0.5).clamp(0, 1) 69 | image = image.cpu().permute(0, 2, 3, 1).numpy()[0] 70 | return image 71 | 72 | @torch.no_grad() 73 | def image2latent(self, image): 74 | with torch.no_grad(): 75 | if type(image) is Image: 76 | image = np.array(image) 77 | if type(image) is torch.Tensor and image.dim() == 4: 78 | latents = image 79 | else: 80 | image = torch.from_numpy(image).float() / 0.5 - 1 81 | image = image.permute(2, 0, 1).unsqueeze(0).to(self.model.device) 82 | latents = self.model.vae.encode(image)['latent_dist'].mean 83 | latents = latents * 0.18215 84 | return latents 85 | 86 | @torch.no_grad() 87 | def init_prompt(self, prompt: str): 88 | uncond_input = self.tokenizer( 89 | [""], padding="max_length", max_length=self.tokenizer.model_max_length, 90 | return_tensors="pt" 91 | ) 92 | # uncond_embeddings = self.model.text_encoder_list[0](uncond_input.input_ids.to(self.model.device))[0] # auffusion-full 93 | uncond_embeddings = self.model.text_encoder(uncond_input.input_ids.to(self.model.device))[0] 94 | text_input = self.tokenizer( 95 | [prompt], 96 | padding="max_length", 97 | max_length=self.tokenizer.model_max_length, 98 | truncation=True, 99 | return_tensors="pt", 100 | ) 101 | # text_embeddings = self.model.text_encoder_list[0](text_input.input_ids.to(self.model.device))[0] # auffusion-full 102 | text_embeddings = self.model.text_encoder(text_input.input_ids.to(self.model.device))[0] 103 | self.context = torch.cat([uncond_embeddings, text_embeddings]) 104 | self.prompt = prompt 105 | 106 | @torch.no_grad() 107 | def ddim_loop(self, latent): 108 | uncond_embeddings, cond_embeddings = self.context.chunk(2) 109 | all_latent = [latent] 110 | latent = latent.clone().detach() 111 | for i in range(NUM_DDIM_STEPS): 112 | t = self.model.scheduler.timesteps[len(self.model.scheduler.timesteps) - i - 1] 113 | noise_pred = self.get_noise_pred_single(latent, t, cond_embeddings) 114 | latent = self.next_step(noise_pred, t, latent) 115 | all_latent.append(latent) 116 | return all_latent 117 | 118 | @property 119 | def scheduler(self): 120 | return self.model.scheduler 121 | 122 | @torch.no_grad() 123 | def ddim_inversion(self, image): 124 | latent = self.image2latent(image) 125 | image_rec = self.latent2image(latent) 126 | ddim_latents = self.ddim_loop(latent) 127 | return image_rec, ddim_latents 128 | 129 | def null_optimization(self, latents, num_inner_steps, epsilon): 130 | uncond_embeddings, cond_embeddings = self.context.chunk(2) 131 | uncond_embeddings_list = [] 132 | latent_cur = latents[-1] 133 | bar = tqdm(total=num_inner_steps * NUM_DDIM_STEPS) 134 | for i in range(NUM_DDIM_STEPS): 135 | uncond_embeddings = uncond_embeddings.clone().detach() 136 | uncond_embeddings.requires_grad = True 137 | optimizer = Adam([uncond_embeddings], lr=1e-4 * (1. - i / 1000.)) 138 | latent_prev = latents[len(latents) - i - 2] 139 | t = self.model.scheduler.timesteps[i] 140 | with torch.no_grad(): 141 | noise_pred_cond = self.get_noise_pred_single(latent_cur, t, cond_embeddings) 142 | for j in range(num_inner_steps): 143 | noise_pred_uncond = self.get_noise_pred_single(latent_cur, t, uncond_embeddings) 144 | noise_pred = noise_pred_uncond + GUIDANCE_SCALE_TRAIN * (noise_pred_cond - noise_pred_uncond) 145 | latents_prev_rec = self.prev_step(noise_pred, t, latent_cur) 146 | loss = nnf.mse_loss(latents_prev_rec, latent_prev) 147 | optimizer.zero_grad() 148 | loss.backward() 149 | optimizer.step() 150 | loss_item = loss.item() 151 | bar.update() 152 | if loss_item < epsilon + i * 2e-5: 153 | break 154 | for j in range(j + 1, num_inner_steps): 155 | bar.update() 156 | uncond_embeddings_list.append(uncond_embeddings[:1].detach()) 157 | with torch.no_grad(): 158 | context = torch.cat([uncond_embeddings, cond_embeddings]) 159 | latent_cur = self.get_noise_pred(latent_cur, t, False, context) 160 | bar.close() 161 | return uncond_embeddings_list 162 | 163 | def invert(self, image_path: str, prompt: str, offsets=(0, 0, 0, 0), num_inner_steps=10, early_stop_epsilon=1e-5, 164 | inversion='Null-text', verbose=False): 165 | self.init_prompt(prompt) 166 | image_gt = image_path 167 | if verbose: 168 | print("DDIM inversion...") 169 | image_rec, ddim_latents = self.ddim_inversion(image_gt) 170 | 171 | assert inversion in ['NT', 'NPI'] 172 | if inversion == 'NT': 173 | print("Null-text optimization...") 174 | uncond_embeddings = self.null_optimization(ddim_latents, num_inner_steps, early_stop_epsilon) 175 | else: 176 | print("Negative prompt inversion...") 177 | uncond_embeddings, cond_embeddings = self.context.chunk(2) 178 | uncond_embeddings = [cond_embeddings] * NUM_DDIM_STEPS 179 | return (image_gt, image_rec), ddim_latents[-1], uncond_embeddings 180 | 181 | def __init__(self, model): 182 | scheduler = DDIMScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", clip_sample=False, 183 | set_alpha_to_one=False) 184 | self.model = model 185 | # self.tokenizer = self.model.tokenizer_list[0] # auffusion-full 186 | self.tokenizer = self.model.tokenizer 187 | self.model.scheduler.set_timesteps(NUM_DDIM_STEPS) 188 | self.prompt = None 189 | self.context = None -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | import os 2 | import ast 3 | import torch 4 | import argparse 5 | import numpy as np 6 | from scipy.io.wavfile import write 7 | from diffusers.utils.import_utils import is_xformers_available 8 | 9 | from diffusers import StableDiffusionPipeline, DDIMScheduler 10 | from auffusion.auffusion_pipeline import AuffusionPipeline 11 | 12 | from null_text_inversion.null_text_inversion import NullInversion 13 | from utils.converter import load_wav, mel_spectrogram, normalize_spectrogram, denormalize_spectrogram, Generator, get_mel_spectrogram_from_audio 14 | from utils.utils import pad_spec, image_add_color, torch_to_pil, normalize, denormalize 15 | 16 | 17 | from prompt2prompt.attn_control import AttentionStore, show_cross_attention 18 | from suppresseot.run_and_display import run_and_display 19 | 20 | 21 | def parse_args(): 22 | parser = argparse.ArgumentParser() 23 | parser.add_argument("--sd_version", type=str, default='auffusion-full-no-adapter', help='version of stable diffusion model.') 24 | parser.add_argument("--seed", type=int, default=42, help="A seed for reproducible inference.") 25 | parser.add_argument("--output_dir", type=str, default="./audio_examples/output_audios", help="The output directory where the model predictions will be written.") 26 | parser.add_argument('--prompt', type=str, default='After a gunshot, there was a burst of dog barking', help='prompt for generated or real audio') 27 | parser.add_argument('--audio_path', type=str, default='audio_examples/input_audios/After a gunshot, there was a burst of dog barking.wav', help='audio path') 28 | 29 | parser.add_argument("--enable_xformers", default=True, action="store_true", help="Whether or not to use xformers.") 30 | parser.add_argument("--guidance_scale", type=float, default=7.5, help="The scale of guidance.") 31 | parser.add_argument("--num_inference_steps", type=int, default=100, help="Number of inference steps to perform.") 32 | 33 | parser.add_argument("--width", type=int, default=1024, help="Width of the spec.") 34 | parser.add_argument("--height", type=int, default=256, help="Height of the spec.") 35 | parser.add_argument("--sample_rate", type=int, default=16000, help="The sample rate of audio.") 36 | parser.add_argument("--duration", type=int, default=10, help="The duration(s) of audio.") 37 | 38 | 39 | parser.add_argument("--input_dir", type=str, default="./audio_examples/input_audios", help="The input directory where put the audios to be edited.") 40 | parser.add_argument('--inversion', type=str, default='NT', help='NT (Null-text), NPI (Negative-prompt-inversion).') 41 | 42 | parser.add_argument('--token_indices', type=ast.literal_eval, default='[[1,]]', help='index of without words.') 43 | parser.add_argument('--cross_retain_steps', type=ast.literal_eval, default='[.2,]', help='perform the "wo" punish when step >= cross_wo_steps') 44 | parser.add_argument('--alpha', type=ast.literal_eval, default='[1.,]', help="punishment ratio") 45 | parser.add_argument('--iter_each_step', type=int, default=5, help="the number of iteration for each step to update text embedding") 46 | parser.add_argument('--max_step_to_erase', type=int, default=20, help='erase/suppress max step of diffusion model') 47 | parser.add_argument('--method', type=str, default='soft-weight', help='soft-weight, alpha, beta, delete, weight') 48 | 49 | parser.add_argument('--lambda_retain', type=float, default=1., help='lambda for cross attention retain loss') 50 | parser.add_argument('--lambda_erase', type=float, default=-.5, help='lambda for cross attention erase loss') 51 | parser.add_argument('--lambda_self_retain', type=float, default=1., help='lambda for self attention retain loss') 52 | parser.add_argument('--lambda_self_erase', type=float, default=-.5, help='lambda for self attention erase loss') 53 | 54 | args = parser.parse_args() 55 | return args 56 | 57 | def load_model(sd_version, device): 58 | if sd_version == "auffusion-full-no-adapter": 59 | pretrained_model_name_or_path = "ckpt/auffusion-full-no-adapter" 60 | ldm_stable = StableDiffusionPipeline.from_pretrained(pretrained_model_name_or_path).to(device) 61 | elif sd_version == sd_version == "sd_1_4": 62 | pretrained_model_name_or_path = "/home/jiayuhang/.cache/huggingface/hub/models--CompVis--stable-diffusion-v1-4/snapshots/133a221b8aa7292a167afc5127cb63fb5005638b" 63 | scheduler = DDIMScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", clip_sample=False, set_alpha_to_one=False, steps_offset=1) 64 | ldm_stable = StableDiffusionPipeline.from_pretrained(pretrained_model_name_or_path, scheduler=scheduler).to(device) 65 | else: 66 | raise ValueError('Unsupported stable diffusion version') 67 | return ldm_stable 68 | 69 | 70 | def load_1024_256(audio_path): 71 | audio, sampling_rate = load_wav(audio_path) 72 | audio, spec = get_mel_spectrogram_from_audio(audio) 73 | norm_spec = normalize_spectrogram(spec) 74 | norm_spec = pad_spec(norm_spec, 1024) 75 | norm_spec = norm_spec.permute(1, 2, 0).cpu().numpy() # (256, 1024, 3) # reshape 76 | return norm_spec 77 | 78 | 79 | def store_1024_256(output_spec, sample_rate, audio_path): 80 | ### vocoder 81 | device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu') 82 | vocoder = Generator.from_pretrained("ckpt/auffusion-full-no-adapter", subfolder="vocoder") 83 | vocoder = vocoder.to(device=device, dtype=torch.float16) 84 | 85 | norm_spec = output_spec[:, :1000, :] 86 | norm_spec = torch.from_numpy(norm_spec).permute(2, 0, 1).to(device=device) # reshape torch.Size([3, 256, 1000]) 87 | denorm_spec = denormalize_spectrogram(norm_spec) 88 | with torch.autocast("cuda"): 89 | denorm_spec_audio = vocoder.inference(denorm_spec) 90 | write(audio_path, sample_rate, denorm_spec_audio.squeeze()) 91 | print(f"Successfully save {audio_path}") 92 | 93 | 94 | def show_spec(output_spec, audio_path): 95 | device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu') 96 | norm_spec = torch.from_numpy(output_spec).permute(2, 0, 1).to(device=device) 97 | raw_image = image_add_color(torch_to_pil(norm_spec[:,:,:1000])) 98 | raw_image.save(audio_path) 99 | 100 | 101 | def edit_audio(): 102 | args = parse_args() 103 | device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu') 104 | ldm_stable = load_model(args.sd_version, device) 105 | ldm_stable.set_progress_bar_config(disable=True) 106 | if is_xformers_available() and args.enable_xformers: 107 | ldm_stable.enable_xformers_memory_efficient_attention() 108 | 109 | orig_prompt = args.prompt 110 | input_audio_path = args.audio_path 111 | input_audio = load_1024_256(input_audio_path) 112 | os.makedirs(os.path.join(args.output_dir, orig_prompt), exist_ok=True) 113 | 114 | # Null-Text Inversion 115 | null_inversion = NullInversion(ldm_stable) 116 | with torch.autocast("cuda"): 117 | (audio_gt, audio_rec), x_t, uncond_embeddings = null_inversion.invert(input_audio, orig_prompt, inversion=args.inversion, \ 118 | num_inner_steps=args.iter_each_step, verbose=True) 119 | 120 | show_spec(audio_gt, f"audio_examples/output_audios/{orig_prompt}/audio_gt.png") 121 | show_spec(audio_rec, f"audio_examples/output_audios/{orig_prompt}/audio_rec.png") 122 | store_1024_256(audio_gt, args.sample_rate, f"audio_examples/output_audios/{orig_prompt}/audio_gt.wav") 123 | store_1024_256(audio_rec, args.sample_rate, f"audio_examples/output_audios/{orig_prompt}/audio_rec.wav") 124 | 125 | #SuppressEOT 126 | for token_indices in args.token_indices: 127 | for cross_retain_steps in args.cross_retain_steps: 128 | for alpha in args.alpha: 129 | controller = AttentionStore(token_indices, \ 130 | alpha, \ 131 | args.method, \ 132 | cross_retain_steps, \ 133 | len(ldm_stable.tokenizer.encode((orig_prompt))), \ 134 | args.iter_each_step, \ 135 | args.max_step_to_erase, \ 136 | lambda_retain=args.lambda_retain, \ 137 | lambda_erase=args.lambda_erase, \ 138 | lambda_self_retain=args.lambda_self_retain, \ 139 | lambda_self_erase=args.lambda_self_erase) 140 | 141 | with torch.autocast("cuda"): 142 | audio_edit, x_t = run_and_display(ldm_stable, \ 143 | [orig_prompt], \ 144 | controller, \ 145 | latent=x_t, \ 146 | uncond_embeddings=uncond_embeddings, \ 147 | args=args) 148 | 149 | 150 | show_spec(audio_edit[1], f"audio_examples/output_audios/{orig_prompt}/audio_eot_edit.png") 151 | store_1024_256(audio_edit[1], args.sample_rate, f"audio_examples/output_audios/{orig_prompt}/audio_eot_edit.wav") 152 | 153 | 154 | def gen_audio(): 155 | args = parse_args() 156 | device = torch.device('cuda:0') if torch.cuda.is_available() else torch.device('cpu') 157 | ldm_stable = load_model(args.sd_version, device) 158 | ldm_stable.set_progress_bar_config(disable=True) 159 | if is_xformers_available() and args.enable_xformers: 160 | ldm_stable.enable_xformers_memory_efficient_attention() 161 | 162 | generator = torch.Generator(device=device).manual_seed(args.seed) 163 | text_prompt = args.edit_prompt 164 | audio_path = os.path.join(args.input_dir, text_prompt + '.wav') 165 | 166 | with torch.autocast("cuda"): 167 | output = ldm_stable(prompt = text_prompt, \ 168 | num_inference_steps = args.num_inference_steps, \ 169 | guidance_scale = args.guidance_scale, \ 170 | generator = generator, \ 171 | width = args.width, \ 172 | height = args.height, 173 | output_type="pt") 174 | 175 | if args.sd_version == "auffusion": 176 | audio_length = args.sample_rate * args.duration 177 | audio = output.audios[0][:audio_length] 178 | write(audio_path, args.sample_rate, audio) 179 | elif args.sd_version == "auffusion-full-no-adapter": 180 | output_spec = output.images[0].permute(1, 2, 0).cpu().numpy() 181 | store_1024_256(output_spec, args.sample_rate, audio_path) 182 | else: 183 | raise ValueError('Unsupported stable diffusion version') 184 | 185 | 186 | def main(): 187 | # gen_audio() 188 | edit_audio() 189 | 190 | 191 | if __name__=="__main__": 192 | main() -------------------------------------------------------------------------------- /utils/utils.py: -------------------------------------------------------------------------------- 1 | import os, json 2 | import math, random 3 | from multiprocessing import Pool 4 | from tqdm import tqdm 5 | import numpy as np 6 | import torch 7 | import torch.nn as nn 8 | import torch.nn.functional as F 9 | from PIL import Image 10 | import matplotlib.pyplot as plt 11 | from torchvision import transforms 12 | from transformers import CLIPTextModel 13 | from transformers import PretrainedConfig 14 | 15 | 16 | def pad_spec(spec, spec_length, pad_value=0, random_crop=True): # spec: [3, mel_dim, spec_len] 17 | assert spec_length % 8 == 0, "spec_length must be divisible by 8" 18 | if spec.shape[-1] < spec_length: 19 | # pad spec to spec_length 20 | spec = F.pad(spec, (0, spec_length - spec.shape[-1]), value=pad_value) 21 | else: 22 | # random crop 23 | if random_crop: 24 | start = random.randint(0, spec.shape[-1] - spec_length) 25 | spec = spec[:, :, start:start+spec_length] 26 | else: 27 | spec = spec[:, :, :spec_length] 28 | return spec 29 | 30 | 31 | def load_spec(spec_path): 32 | if spec_path.endswith(".pt"): 33 | spec = torch.load(spec_path, map_location="cpu") 34 | elif spec_path.endswith(".npy"): 35 | spec = torch.from_numpy(np.load(spec_path)) 36 | else: 37 | raise ValueError(f"Unknown spec file type {spec_path}") 38 | assert len(spec.shape) == 3, f"spec shape must be [3, mel_dim, spec_len], got {spec.shape}" 39 | if spec.size(0) == 1: 40 | spec = spec.repeat(3, 1, 1) 41 | return spec 42 | 43 | 44 | def random_crop_spec(spec, target_spec_length, pad_value=0, frame_per_sec=100, time_step=5): # spec: [3, mel_dim, spec_len] 45 | assert target_spec_length % 8 == 0, "spec_length must be divisible by 8" 46 | 47 | spec_length = spec.shape[-1] 48 | full_s = math.ceil(spec_length / frame_per_sec / time_step) * time_step # get full seconds(ceil) 49 | start_s = random.randint(0, math.floor(spec_length / frame_per_sec / time_step)) * time_step # random get start seconds 50 | 51 | end_s = min(start_s + math.ceil(target_spec_length / frame_per_sec), full_s) # get end seconds 52 | 53 | spec = spec[:, :, start_s * frame_per_sec : end_s * frame_per_sec] # get spec in seconds(crop more than target_spec_length because ceiling) 54 | 55 | if spec.shape[-1] < target_spec_length: 56 | spec = F.pad(spec, (0, target_spec_length - spec.shape[-1]), value=pad_value) # pad to target_spec_length 57 | else: 58 | spec = spec[:, :, :target_spec_length] # crop to target_spec_length 59 | 60 | return spec, int(start_s), int(end_s), int(full_s) 61 | 62 | 63 | 64 | def load_condion_embed(text_embed_path): 65 | if text_embed_path.endswith(".pt"): 66 | text_embed_list = torch.load(text_embed_path, map_location="cpu") 67 | elif text_embed_path.endswith(".npy"): 68 | text_embed_list = torch.from_numpy(np.load(text_embed_path)) 69 | else: 70 | raise ValueError(f"Unknown text embedding file type {text_embed_path}") 71 | if type(text_embed_list) == list: 72 | text_embed = random.choice(text_embed_list) 73 | if len(text_embed.shape) == 3: # [1, text_len, text_dim] 74 | text_embed = text_embed.squeeze(0) # random choice and return text_emb: [text_len, text_dim] 75 | return text_embed.detach().cpu() 76 | 77 | 78 | def process_condition_embed(cond_emb, max_length): # [text_len, text_dim], Padding 0 and random drop by CFG 79 | if cond_emb.shape[0] < max_length: 80 | cond_emb = F.pad(cond_emb, (0, 0, 0, max_length - cond_emb.shape[0]), value=0) 81 | else: 82 | cond_emb = cond_emb[:max_length, :] 83 | return cond_emb 84 | 85 | 86 | def import_model_class_from_model_name_or_path(pretrained_model_name_or_path: str): 87 | text_encoder_config = PretrainedConfig.from_pretrained( 88 | pretrained_model_name_or_path 89 | ) 90 | model_class = text_encoder_config.architectures[0] 91 | 92 | if model_class == "CLIPTextModel": 93 | from transformers import CLIPTextModel 94 | return CLIPTextModel 95 | if "t5" in model_class.lower(): 96 | from transformers import T5EncoderModel 97 | return T5EncoderModel 98 | if "clap" in model_class.lower(): 99 | from transformers import ClapTextModelWithProjection 100 | return ClapTextModelWithProjection 101 | else: 102 | raise ValueError(f"{model_class} is not supported.") 103 | 104 | 105 | 106 | def str2bool(string): 107 | str2val = {"True": True, "False": False, "true": True, "false": False, "none": False, "None": False} 108 | if string in str2val: 109 | return str2val[string] 110 | else: 111 | raise ValueError(f"Expected one of {set(str2val.keys())}, got {string}") 112 | 113 | 114 | def str2str(string): 115 | if string.lower() == "none" or string.lower() == "null" or string.lower() == "false" or string == "": 116 | return None 117 | else: 118 | return string 119 | 120 | 121 | def json_dump(data_json, json_save_path): 122 | with open(json_save_path, 'w') as f: 123 | json.dump(data_json, f, indent=4) 124 | f.close() 125 | 126 | 127 | def json_load(json_path): 128 | with open(json_path, 'r') as f: 129 | data = json.load(f) 130 | f.close() 131 | return data 132 | 133 | 134 | def load_json_list(path): 135 | with open(path, 'r', encoding='utf-8') as f: 136 | return [json.loads(line) for line in f.readlines()] 137 | 138 | 139 | def save_json_list(data, path): 140 | with open(path, 'w', encoding='utf-8') as f: 141 | for d in data: 142 | f.write(json.dumps(d) + '\n') 143 | 144 | 145 | def multiprocess_function(func, func_args, n_jobs=32): 146 | with Pool(processes=n_jobs) as p: 147 | with tqdm(total=len(func_args)) as pbar: 148 | for i, _ in enumerate(p.imap_unordered(func, func_args)): 149 | pbar.update() 150 | 151 | 152 | def image_add_color(spec_img): 153 | cmap = plt.get_cmap('viridis') 154 | cmap_r = cmap.reversed() 155 | image = cmap(np.array(spec_img)[:,:,0])[:, :, :3] 156 | image = (image - image.min()) / (image.max() - image.min()) 157 | image = Image.fromarray(np.uint8(image*255)) 158 | return image 159 | 160 | 161 | @staticmethod 162 | def pt_to_numpy(images: torch.FloatTensor) -> np.ndarray: 163 | """ 164 | Convert a PyTorch tensor to a NumPy image. 165 | """ 166 | images = images.cpu().permute(0, 2, 3, 1).float().numpy() 167 | return images 168 | 169 | 170 | def numpy_to_pil(images): 171 | """ 172 | Convert a numpy image or a batch of images to a PIL image. 173 | """ 174 | if images.ndim == 3: 175 | images = images[None, ...] 176 | images = (images * 255).round().astype("uint8") 177 | if images.shape[-1] == 1: 178 | # special case for grayscale (single channel) images 179 | pil_images = [Image.fromarray(image.squeeze(), mode="L") for image in images] 180 | else: 181 | pil_images = [Image.fromarray(image) for image in images] 182 | 183 | return pil_images 184 | 185 | ### CODE FOR INPAITING ### 186 | def normalize(images): 187 | """ 188 | Normalize an image array to [-1,1]. 189 | """ 190 | if images.min() >= 0: 191 | return 2.0 * images - 1.0 192 | else: 193 | return images 194 | 195 | def denormalize(images): 196 | """ 197 | Denormalize an image array to [0,1]. 198 | """ 199 | if images.min() < 0: 200 | return (images / 2 + 0.5).clamp(0, 1) 201 | else: 202 | return images.clamp(0, 1) 203 | 204 | 205 | def prepare_mask_and_masked_image(image, mask): 206 | """ 207 | Prepare a binary mask and the masked image. 208 | 209 | Parameters: 210 | - image (torch.Tensor): The input image tensor of shape [3, height, width] with values in the range [0, 1]. 211 | - mask (torch.Tensor): The input mask tensor of shape [1, height, width]. 212 | 213 | Returns: 214 | - tuple: A tuple containing the binary mask and the masked image. 215 | """ 216 | # Noralize image to [0,1] 217 | if image.max() > 1: 218 | image = (image - image.min()) / (image.max() - image.min()) 219 | # Normalize image from [0,1] to [-1,1] 220 | if image.min() >= 0: 221 | image = normalize(image) 222 | # Apply the mask to the image 223 | masked_image = image * (mask < 0.5) 224 | 225 | return mask, masked_image 226 | 227 | 228 | def torch_to_pil(image): 229 | """ 230 | Convert a torch tensor to a PIL image. 231 | """ 232 | if image.min() < 0: 233 | image = denormalize(image) 234 | 235 | return transforms.ToPILImage()(image.cpu().detach().squeeze()) 236 | 237 | 238 | 239 | # class TextEncoderAdapter(nn.Module): 240 | # def __init__(self, hidden_size, cross_attention_dim=768): 241 | # super(TextEncoderAdapter, self).__init__() 242 | # self.hidden_size = hidden_size 243 | # self.cross_attention_dim = cross_attention_dim 244 | # self.proj = nn.Linear(self.hidden_size, self.cross_attention_dim) 245 | # self.norm = torch.nn.LayerNorm(self.cross_attention_dim) 246 | 247 | # def forward(self, x): 248 | # x = self.proj(x) 249 | # x = self.norm(x) 250 | # return x 251 | 252 | # def save_pretrained(self, save_directory, subfolder=""): 253 | # if subfolder: 254 | # save_directory = os.path.join(save_directory, subfolder) 255 | # os.makedirs(save_directory, exist_ok=True) 256 | # ckpt_path = os.path.join(save_directory, "adapter.pt") 257 | # config_path = os.path.join(save_directory, "config.json") 258 | # config = {"hidden_size": self.hidden_size, "cross_attention_dim": self.cross_attention_dim} 259 | # json_dump(config, config_path) 260 | # torch.save(self.state_dict(), ckpt_path) 261 | # print(f"Saving adapter model to {ckpt_path}") 262 | 263 | # @classmethod 264 | # def from_pretrained(cls, load_directory, subfolder=""): 265 | # if subfolder: 266 | # load_directory = os.path.join(load_directory, subfolder) 267 | # ckpt_path = os.path.join(load_directory, "adapter.pt") 268 | # config_path = os.path.join(load_directory, "config.json") 269 | # config = json_load(config_path) 270 | # instance = cls(**config) 271 | # instance.load_state_dict(torch.load(ckpt_path)) 272 | # print(f"Loading adapter model from {ckpt_path}") 273 | # return instance 274 | 275 | 276 | 277 | class ConditionAdapter(nn.Module): 278 | def __init__(self, config): 279 | super(ConditionAdapter, self).__init__() 280 | self.config = config 281 | self.proj = nn.Linear(self.config["condition_dim"], self.config["cross_attention_dim"]) 282 | self.norm = torch.nn.LayerNorm(self.config["cross_attention_dim"]) 283 | print(f"INITIATED: ConditionAdapter: {self.config}") 284 | 285 | def forward(self, x): 286 | x = self.proj(x) 287 | x = self.norm(x) 288 | return x 289 | 290 | @classmethod 291 | def from_pretrained(cls, pretrained_model_name_or_path): 292 | config_path = os.path.join(pretrained_model_name_or_path, "config.json") 293 | ckpt_path = os.path.join(pretrained_model_name_or_path, "condition_adapter.pt") 294 | config = json_load(config_path) 295 | instance = cls(config) 296 | instance.load_state_dict(torch.load(ckpt_path)) 297 | print(f"LOADED: ConditionAdapter from {pretrained_model_name_or_path}") 298 | return instance 299 | 300 | def save_pretrained(self, pretrained_model_name_or_path): 301 | os.makedirs(pretrained_model_name_or_path, exist_ok=True) 302 | config_path = os.path.join(pretrained_model_name_or_path, "config.json") 303 | ckpt_path = os.path.join(pretrained_model_name_or_path, "condition_adapter.pt") 304 | json_dump(self.config, config_path) 305 | torch.save(self.state_dict(), ckpt_path) 306 | print(f"SAVED: ConditionAdapter {self.config['condition_adapter_name']} to {pretrained_model_name_or_path}") 307 | 308 | 309 | # class TextEncoderWrapper(CLIPTextModel): 310 | # def __init__(self, text_encoder, text_encoder_adapter): 311 | # super().__init__(text_encoder.config) 312 | # self.text_encoder = text_encoder 313 | # self.adapter = text_encoder_adapter 314 | 315 | # def forward(self, input_ids, **kwargs): 316 | # outputs = self.text_encoder(input_ids, **kwargs) 317 | # adapted_output = self.adapter(outputs[0]) 318 | # return [adapted_output] # to compatible with last_hidden_state 319 | 320 | -------------------------------------------------------------------------------- /prompt2prompt/ptp_utils.py: -------------------------------------------------------------------------------- 1 | # Copyright 2022 Google LLC 2 | # 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | # 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | # 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | import numpy as np 16 | import torch 17 | from PIL import Image, ImageDraw, ImageFont 18 | import cv2 19 | from typing import Optional, Union, Tuple, List, Callable, Dict 20 | from tqdm.notebook import tqdm 21 | 22 | 23 | def text_under_image(image: np.ndarray, text: str, text_color: Tuple[int, int, int] = (0, 0, 0), font_scale: float = 1.0, thickness: int = 3) -> np.ndarray: 24 | h, w, c = image.shape 25 | offset = int(h * .2) 26 | img = np.ones((h + offset, w, c), dtype=np.uint8) * 255 27 | font = cv2.FONT_HERSHEY_SIMPLEX 28 | img[:h] = image 29 | textsize = cv2.getTextSize(text, font, font_scale, thickness)[0] 30 | text_x, text_y = (w - textsize[0]) // 2, h + offset - textsize[1] // 2 31 | cv2.putText(img, text, (text_x, text_y), font, font_scale, text_color, 2) 32 | return img 33 | 34 | 35 | def view_images(images, num_rows=1, offset_ratio=0.02, save_name="null-text+ptp"): 36 | if type(images) is list: 37 | num_empty = len(images) % num_rows 38 | elif images.ndim == 4: 39 | num_empty = images.shape[0] % num_rows 40 | else: 41 | images = [images] 42 | num_empty = 0 43 | 44 | empty_images = np.ones(images[0].shape, dtype=np.uint8) * 255 45 | images = [image.astype(np.uint8) for image in images] + [empty_images] * num_empty 46 | num_items = len(images) 47 | 48 | h, w, c = images[0].shape 49 | offset = int(h * offset_ratio) 50 | num_cols = num_items // num_rows 51 | image_ = np.ones((h * num_rows + offset * (num_rows - 1), 52 | w * num_cols + offset * (num_cols - 1), 3), dtype=np.uint8) * 255 53 | for i in range(num_rows): 54 | for j in range(num_cols): 55 | image_[i * (h + offset): i * (h + offset) + h:, j * (w + offset): j * (w + offset) + w] = images[ 56 | i * num_cols + j] 57 | 58 | pil_img = Image.fromarray(image_).save(f'{save_name}.png') 59 | 60 | def save_image_grid(img, fname, grid_size): 61 | gw, gh = grid_size 62 | _N, C, H, W = img.shape 63 | assert gw * gh == _N 64 | img = img.reshape(gh, gw, C, H, W) 65 | img = img.transpose(0, 3, 1, 4, 2) 66 | img = img.reshape(gh * H, gw * W, C) 67 | 68 | assert C in [1, 3] 69 | if C == 1: 70 | Image.fromarray(img[:, :, 0], 'L').save(fname) 71 | if C == 3: 72 | Image.fromarray(img, 'RGB').save(fname) 73 | 74 | 75 | def diffusion_step(model, controller, latents, context, t, guidance_scale, low_resource=False): 76 | if low_resource: 77 | controller.uncond = True 78 | noise_pred_uncond = model.unet(latents, t, encoder_hidden_states=context[0])["sample"] 79 | controller.uncond = False 80 | noise_prediction_text = model.unet(latents, t, encoder_hidden_states=context[1])["sample"] 81 | else: 82 | latents_input = torch.cat([latents] * 2) 83 | noise_pred = model.unet(latents_input, t, encoder_hidden_states=context)["sample"] 84 | noise_pred_uncond, noise_prediction_text = noise_pred.chunk(2) 85 | noise_pred = noise_pred_uncond + guidance_scale * (noise_prediction_text - noise_pred_uncond) 86 | latents = model.scheduler.step(noise_pred, t, latents)["prev_sample"] 87 | latents = controller.step_callback(latents) 88 | return latents 89 | 90 | 91 | def latent2image(vae, latents): 92 | latents = 1 / 0.18215 * latents 93 | image = vae.decode(latents)['sample'] 94 | image = (image / 2 + 0.5).clamp(0, 1) 95 | image = image.cpu().permute(0, 2, 3, 1).numpy() 96 | image = (image * 255).astype(np.uint8) 97 | return image 98 | 99 | 100 | def init_latent(latent, model, height, width, generator, batch_size): 101 | if latent is None: 102 | latent = torch.randn( 103 | (1, model.unet.in_channels, height // 8, width // 8), 104 | generator=generator, 105 | ) 106 | latents = latent.expand(batch_size, model.unet.in_channels, height // 8, width // 8).to(model.device) 107 | return latent, latents 108 | 109 | 110 | @torch.no_grad() 111 | def text2image_ldm( 112 | model, 113 | prompt: List[str], 114 | controller, 115 | num_inference_steps: int = 50, 116 | guidance_scale: Optional[float] = 7., 117 | generator: Optional[torch.Generator] = None, 118 | latent: Optional[torch.FloatTensor] = None, 119 | ): 120 | register_attention_control(model, controller) 121 | height = width = 256 122 | batch_size = len(prompt) 123 | 124 | uncond_input = model.tokenizer([""] * batch_size, padding="max_length", max_length=77, return_tensors="pt") 125 | uncond_embeddings = model.bert(uncond_input.input_ids.to(model.device))[0] 126 | 127 | text_input = model.tokenizer(prompt, padding="max_length", max_length=77, return_tensors="pt") 128 | text_embeddings = model.bert(text_input.input_ids.to(model.device))[0] 129 | latent, latents = init_latent(latent, model, height, width, generator, batch_size) 130 | context = torch.cat([uncond_embeddings, text_embeddings]) 131 | 132 | model.scheduler.set_timesteps(num_inference_steps) 133 | for t in tqdm(model.scheduler.timesteps): 134 | latents = diffusion_step(model, controller, latents, context, t, guidance_scale) 135 | 136 | image = latent2image(model.vqvae, latents) 137 | 138 | return image, latent 139 | 140 | 141 | @torch.no_grad() 142 | def text2image_ldm_stable( 143 | model, 144 | prompt: List[str], 145 | controller, 146 | num_inference_steps: int = 50, 147 | guidance_scale: float = 7.5, 148 | generator: Optional[torch.Generator] = None, 149 | latent: Optional[torch.FloatTensor] = None, 150 | low_resource: bool = False, 151 | ): 152 | register_attention_control(model, controller) 153 | height = width = 512 154 | batch_size = len(prompt) 155 | 156 | text_input = model.tokenizer( 157 | prompt, 158 | padding="max_length", 159 | max_length=model.tokenizer.model_max_length, 160 | truncation=True, 161 | return_tensors="pt", 162 | ) 163 | text_embeddings = model.text_encoder(text_input.input_ids.to(model.device))[0] 164 | max_length = text_input.input_ids.shape[-1] 165 | uncond_input = model.tokenizer( 166 | [""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt" 167 | ) 168 | uncond_embeddings = model.text_encoder(uncond_input.input_ids.to(model.device))[0] 169 | 170 | context = [uncond_embeddings, text_embeddings] 171 | if not low_resource: 172 | context = torch.cat(context) 173 | latent, latents = init_latent(latent, model, height, width, generator, batch_size) 174 | 175 | # set timesteps 176 | extra_set_kwargs = {"offset": 1} 177 | model.scheduler.set_timesteps(num_inference_steps, **extra_set_kwargs) 178 | for t in tqdm(model.scheduler.timesteps): 179 | latents = diffusion_step(model, controller, latents, context, t, guidance_scale, low_resource) 180 | 181 | image = latent2image(model.vae, latents) 182 | 183 | return image, latent 184 | 185 | def register_attention_control(model, controller): 186 | def ca_forward(self, place_in_unet): 187 | to_out = self.to_out 188 | if type(to_out) is torch.nn.modules.container.ModuleList: 189 | to_out = self.to_out[0] 190 | else: 191 | to_out = self.to_out 192 | 193 | def forward(hidden_states, encoder_hidden_states=None, attention_mask=None,temb=None,): 194 | is_cross = encoder_hidden_states is not None 195 | 196 | residual = hidden_states 197 | 198 | if self.spatial_norm is not None: 199 | hidden_states = self.spatial_norm(hidden_states, temb) 200 | 201 | input_ndim = hidden_states.ndim 202 | 203 | if input_ndim == 4: 204 | batch_size, channel, height, width = hidden_states.shape 205 | hidden_states = hidden_states.view(batch_size, channel, height * width).transpose(1, 2) 206 | 207 | batch_size, sequence_length, _ = ( 208 | hidden_states.shape if encoder_hidden_states is None else encoder_hidden_states.shape 209 | ) 210 | attention_mask = self.prepare_attention_mask(attention_mask, sequence_length, batch_size) 211 | 212 | if self.group_norm is not None: 213 | hidden_states = self.group_norm(hidden_states.transpose(1, 2)).transpose(1, 2) 214 | 215 | query = self.to_q(hidden_states) 216 | 217 | if encoder_hidden_states is None: 218 | encoder_hidden_states = hidden_states 219 | elif self.norm_cross: 220 | encoder_hidden_states = self.norm_encoder_hidden_states(encoder_hidden_states) 221 | 222 | key = self.to_k(encoder_hidden_states) 223 | value = self.to_v(encoder_hidden_states) 224 | 225 | query = self.head_to_batch_dim(query) 226 | key = self.head_to_batch_dim(key) 227 | value = self.head_to_batch_dim(value) 228 | 229 | attention_probs = self.get_attention_scores(query, key, attention_mask) 230 | attention_probs = controller(attention_probs, is_cross, place_in_unet) 231 | 232 | hidden_states = torch.bmm(attention_probs, value) 233 | hidden_states = self.batch_to_head_dim(hidden_states) 234 | 235 | # linear proj 236 | hidden_states = to_out(hidden_states) 237 | 238 | if input_ndim == 4: 239 | hidden_states = hidden_states.transpose(-1, -2).reshape(batch_size, channel, height, width) 240 | 241 | if self.residual_connection: 242 | hidden_states = hidden_states + residual 243 | 244 | hidden_states = hidden_states / self.rescale_output_factor 245 | 246 | return hidden_states 247 | 248 | return forward 249 | 250 | class DummyController: 251 | 252 | def __call__(self, *args): 253 | return args[0] 254 | 255 | def __init__(self): 256 | self.num_att_layers = 0 257 | self.ddim_inv = True 258 | 259 | if controller is None: 260 | controller = DummyController() 261 | 262 | def register_recr(net_, count, place_in_unet): 263 | if net_.__class__.__name__ == 'Attention': 264 | net_.forward = ca_forward(net_, place_in_unet) 265 | return count + 1 266 | elif hasattr(net_, 'children'): 267 | for net__ in net_.children(): 268 | count = register_recr(net__, count, place_in_unet) 269 | return count 270 | 271 | cross_att_count = 0 272 | sub_nets = model.unet.named_children() 273 | for net in sub_nets: 274 | if "down" in net[0]: 275 | cross_att_count += register_recr(net[1], 0, "down") 276 | elif "up" in net[0]: 277 | cross_att_count += register_recr(net[1], 0, "up") 278 | elif "mid" in net[0]: 279 | cross_att_count += register_recr(net[1], 0, "mid") 280 | 281 | controller.num_att_layers = cross_att_count 282 | 283 | def get_word_inds(text: str, word_place: int, tokenizer): 284 | split_text = text.split(" ") 285 | if type(word_place) is str: 286 | word_place = [i for i, word in enumerate(split_text) if word_place == word] 287 | elif type(word_place) is int: 288 | word_place = [word_place] 289 | out = [] 290 | if len(word_place) > 0: 291 | words_encode = [tokenizer.decode([item]).strip("#") for item in tokenizer.encode(text)][1:-1] 292 | cur_len, ptr = 0, 0 293 | 294 | for i in range(len(words_encode)): 295 | cur_len += len(words_encode[i]) 296 | if ptr in word_place: 297 | out.append(i + 1) 298 | if cur_len >= len(split_text[ptr]): 299 | ptr += 1 300 | cur_len = 0 301 | return np.array(out) 302 | 303 | 304 | def update_alpha_time_word(alpha, bounds: Union[float, Tuple[float, float]], prompt_ind: int, 305 | word_inds: Optional[torch.Tensor] = None): 306 | if type(bounds) is float: 307 | bounds = 0, bounds 308 | start, end = int(bounds[0] * alpha.shape[0]), int(bounds[1] * alpha.shape[0]) 309 | if word_inds is None: 310 | word_inds = torch.arange(alpha.shape[2]) 311 | alpha[: start, prompt_ind, word_inds] = 0 312 | alpha[start: end, prompt_ind, word_inds] = 1 313 | alpha[end:, prompt_ind, word_inds] = 0 314 | return alpha 315 | 316 | 317 | def get_time_words_attention_alpha(prompts, num_steps, 318 | cross_replace_steps: Union[float, Dict[str, Tuple[float, float]]], 319 | tokenizer, max_num_words=77): 320 | if type(cross_replace_steps) is not dict: 321 | cross_replace_steps = {"default_": cross_replace_steps} 322 | if "default_" not in cross_replace_steps: 323 | cross_replace_steps["default_"] = (0., 1.) 324 | alpha_time_words = torch.zeros(num_steps + 1, len(prompts) - 1, max_num_words) 325 | for i in range(len(prompts) - 1): 326 | alpha_time_words = update_alpha_time_word(alpha_time_words, cross_replace_steps["default_"], 327 | i) 328 | for key, item in cross_replace_steps.items(): 329 | if key != "default_": 330 | inds = [get_word_inds(prompts[i], key, tokenizer) for i in range(1, len(prompts))] 331 | for i, ind in enumerate(inds): 332 | if len(ind) > 0: 333 | alpha_time_words = update_alpha_time_word(alpha_time_words, item, i, ind) 334 | alpha_time_words = alpha_time_words.reshape(num_steps + 1, len(prompts) - 1, 1, 1, max_num_words) 335 | return alpha_time_words 336 | -------------------------------------------------------------------------------- /utils/converter.py: -------------------------------------------------------------------------------- 1 | 2 | 3 | import numpy as np 4 | from PIL import Image 5 | 6 | import math 7 | import os 8 | import random 9 | import torch 10 | import json 11 | import torch.utils.data 12 | import numpy as np 13 | import librosa 14 | from librosa.util import normalize 15 | from scipy.io.wavfile import read 16 | from librosa.filters import mel as librosa_mel_fn 17 | 18 | import torch.nn.functional as F 19 | import torch.nn as nn 20 | from torch.nn import Conv1d, ConvTranspose1d, AvgPool1d, Conv2d 21 | from torch.nn.utils import weight_norm, remove_weight_norm, spectral_norm 22 | 23 | MAX_WAV_VALUE = 32768.0 24 | 25 | 26 | def load_wav(full_path): 27 | sampling_rate, data = read(full_path) 28 | return data, sampling_rate 29 | 30 | 31 | def dynamic_range_compression(x, C=1, clip_val=1e-5): 32 | return np.log(np.clip(x, a_min=clip_val, a_max=None) * C) 33 | 34 | 35 | def dynamic_range_decompression(x, C=1): 36 | return np.exp(x) / C 37 | 38 | 39 | def dynamic_range_compression_torch(x, C=1, clip_val=1e-5): 40 | return torch.log(torch.clamp(x, min=clip_val) * C) 41 | 42 | 43 | def dynamic_range_decompression_torch(x, C=1): 44 | return torch.exp(x) / C 45 | 46 | 47 | def spectral_normalize_torch(magnitudes): 48 | output = dynamic_range_compression_torch(magnitudes) 49 | return output 50 | 51 | 52 | def spectral_de_normalize_torch(magnitudes): 53 | output = dynamic_range_decompression_torch(magnitudes) 54 | return output 55 | 56 | 57 | mel_basis = {} 58 | hann_window = {} 59 | 60 | 61 | def mel_spectrogram(y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False): 62 | if torch.min(y) < -1.: 63 | print('min value is ', torch.min(y)) 64 | if torch.max(y) > 1.: 65 | print('max value is ', torch.max(y)) 66 | 67 | global mel_basis, hann_window 68 | if fmax not in mel_basis: 69 | mel = librosa_mel_fn(sr=sampling_rate, n_fft=n_fft, n_mels=num_mels, fmin=fmin, fmax=fmax) 70 | mel_basis[str(fmax)+'_'+str(y.device)] = torch.from_numpy(mel).float().to(y.device) 71 | hann_window[str(y.device)] = torch.hann_window(win_size).to(y.device) 72 | 73 | y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect') 74 | y = y.squeeze(1) 75 | 76 | # complex tensor as default, then use view_as_real for future pytorch compatibility 77 | spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[str(y.device)], 78 | center=center, pad_mode='reflect', normalized=False, onesided=True, return_complex=True) 79 | spec = torch.view_as_real(spec) 80 | spec = torch.sqrt(spec.pow(2).sum(-1)+(1e-9)) 81 | 82 | spec = torch.matmul(mel_basis[str(fmax)+'_'+str(y.device)], spec) 83 | spec = spectral_normalize_torch(spec) 84 | 85 | return spec 86 | 87 | 88 | def spectrogram(y, n_fft, num_mels, sampling_rate, hop_size, win_size, fmin, fmax, center=False): 89 | if torch.min(y) < -1.: 90 | print('min value is ', torch.min(y)) 91 | if torch.max(y) > 1.: 92 | print('max value is ', torch.max(y)) 93 | 94 | global hann_window 95 | hann_window[str(y.device)] = torch.hann_window(win_size).to(y.device) 96 | 97 | y = torch.nn.functional.pad(y.unsqueeze(1), (int((n_fft-hop_size)/2), int((n_fft-hop_size)/2)), mode='reflect') 98 | y = y.squeeze(1) 99 | 100 | # complex tensor as default, then use view_as_real for future pytorch compatibility 101 | spec = torch.stft(y, n_fft, hop_length=hop_size, win_length=win_size, window=hann_window[str(y.device)], 102 | center=center, pad_mode='reflect', normalized=False, onesided=True, return_complex=True) 103 | spec = torch.view_as_real(spec) 104 | spec = torch.sqrt(spec.pow(2).sum(-1)+(1e-9)) 105 | 106 | return spec 107 | 108 | 109 | def normalize_spectrogram( 110 | spectrogram: torch.Tensor, 111 | max_value: float = 200, 112 | min_value: float = 1e-5, 113 | power: float = 1., 114 | inverse: bool = False 115 | ) -> torch.Tensor: 116 | 117 | # Rescale to 0-1 118 | max_value = np.log(max_value) # 5.298317366548036 119 | min_value = np.log(min_value) # -11.512925464970229 120 | 121 | # assert spectrogram.max() <= max_value and spectrogram.min() >= min_value 122 | assert spectrogram.max() <= max_value 123 | assert spectrogram.min() >= min_value 124 | 125 | data = (spectrogram - min_value) / (max_value - min_value) 126 | 127 | # Invert 128 | if inverse: 129 | data = 1 - data 130 | 131 | # Apply the power curve 132 | data = torch.pow(data, power) 133 | 134 | # 1D -> 3D 135 | data = data.repeat(3, 1, 1) 136 | 137 | # Flip Y axis: image origin at the top-left corner, spectrogram origin at the bottom-left corner 138 | data = torch.flip(data, [1]) 139 | 140 | return data 141 | 142 | 143 | 144 | def denormalize_spectrogram( 145 | data: torch.Tensor, 146 | max_value: float = 200, 147 | min_value: float = 1e-5, 148 | power: float = 1, 149 | inverse: bool = False, 150 | ) -> torch.Tensor: 151 | 152 | max_value = np.log(max_value) 153 | min_value = np.log(min_value) 154 | 155 | # Flip Y axis: image origin at the top-left corner, spectrogram origin at the bottom-left corner 156 | data = torch.flip(data, [1]) 157 | 158 | assert len(data.shape) == 3, "Expected 3 dimensions, got {}".format(len(data.shape)) 159 | 160 | if data.shape[0] == 1: 161 | data = data.repeat(3, 1, 1) 162 | 163 | assert data.shape[0] == 3, "Expected 3 channels, got {}".format(data.shape[0]) 164 | data = data[0] 165 | 166 | # Reverse the power curve 167 | data = torch.pow(data, 1 / power) 168 | 169 | # Invert 170 | if inverse: 171 | data = 1 - data 172 | 173 | # Rescale to max value 174 | spectrogram = data * (max_value - min_value) + min_value 175 | 176 | return spectrogram 177 | 178 | 179 | def get_mel_spectrogram_from_audio(audio, device="cuda"): 180 | audio = audio / MAX_WAV_VALUE 181 | audio = librosa.util.normalize(audio) * 0.95 182 | 183 | audio = torch.FloatTensor(audio) 184 | audio = audio.unsqueeze(0) 185 | 186 | if len(audio.shape) == 3: 187 | audio_reduced = audio[:, :, 0] 188 | else: 189 | audio_reduced = audio 190 | # print(f"Processed audio_reduced shape: {audio_reduced.shape}") 191 | target_length = 160000 192 | current_length = audio_reduced.size(1) 193 | if current_length < target_length: 194 | padding = target_length - current_length 195 | audio_reduced = F.pad(audio_reduced, (0, padding), mode='constant', value=0) 196 | elif current_length > target_length: 197 | audio_reduced = audio_reduced[:, :target_length] 198 | # print(f"Processed audio_reduced shape: {audio_reduced.shape}") 199 | 200 | 201 | waveform = audio_reduced.to(device) 202 | spec = mel_spectrogram(waveform, n_fft=2048, num_mels=256, sampling_rate=16000, hop_size=160, win_size=1024, fmin=0, fmax=8000, center=False) 203 | return audio_reduced, spec 204 | 205 | 206 | 207 | LRELU_SLOPE = 0.1 208 | MAX_WAV_VALUE = 32768.0 209 | 210 | 211 | class AttrDict(dict): 212 | def __init__(self, *args, **kwargs): 213 | super(AttrDict, self).__init__(*args, **kwargs) 214 | self.__dict__ = self 215 | 216 | 217 | def get_config(config_path): 218 | config = json.loads(open(config_path).read()) 219 | config = AttrDict(config) 220 | return config 221 | 222 | def init_weights(m, mean=0.0, std=0.01): 223 | classname = m.__class__.__name__ 224 | if classname.find("Conv") != -1: 225 | m.weight.data.normal_(mean, std) 226 | 227 | 228 | def apply_weight_norm(m): 229 | classname = m.__class__.__name__ 230 | if classname.find("Conv") != -1: 231 | weight_norm(m) 232 | 233 | 234 | def get_padding(kernel_size, dilation=1): 235 | return int((kernel_size*dilation - dilation)/2) 236 | 237 | 238 | class ResBlock1(torch.nn.Module): 239 | def __init__(self, h, channels, kernel_size=3, dilation=(1, 3, 5)): 240 | super(ResBlock1, self).__init__() 241 | self.h = h 242 | self.convs1 = nn.ModuleList([ 243 | weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0], 244 | padding=get_padding(kernel_size, dilation[0]))), 245 | weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1], 246 | padding=get_padding(kernel_size, dilation[1]))), 247 | weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[2], 248 | padding=get_padding(kernel_size, dilation[2]))) 249 | ]) 250 | self.convs1.apply(init_weights) 251 | 252 | self.convs2 = nn.ModuleList([ 253 | weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1, 254 | padding=get_padding(kernel_size, 1))), 255 | weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1, 256 | padding=get_padding(kernel_size, 1))), 257 | weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=1, 258 | padding=get_padding(kernel_size, 1))) 259 | ]) 260 | self.convs2.apply(init_weights) 261 | 262 | def forward(self, x): 263 | for c1, c2 in zip(self.convs1, self.convs2): 264 | xt = F.leaky_relu(x, LRELU_SLOPE) 265 | xt = c1(xt) 266 | xt = F.leaky_relu(xt, LRELU_SLOPE) 267 | xt = c2(xt) 268 | x = xt + x 269 | return x 270 | 271 | def remove_weight_norm(self): 272 | for l in self.convs1: 273 | remove_weight_norm(l) 274 | for l in self.convs2: 275 | remove_weight_norm(l) 276 | 277 | 278 | class ResBlock2(torch.nn.Module): 279 | def __init__(self, h, channels, kernel_size=3, dilation=(1, 3)): 280 | super(ResBlock2, self).__init__() 281 | self.h = h 282 | self.convs = nn.ModuleList([ 283 | weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[0], 284 | padding=get_padding(kernel_size, dilation[0]))), 285 | weight_norm(Conv1d(channels, channels, kernel_size, 1, dilation=dilation[1], 286 | padding=get_padding(kernel_size, dilation[1]))) 287 | ]) 288 | self.convs.apply(init_weights) 289 | 290 | def forward(self, x): 291 | for c in self.convs: 292 | xt = F.leaky_relu(x, LRELU_SLOPE) 293 | xt = c(xt) 294 | x = xt + x 295 | return x 296 | 297 | def remove_weight_norm(self): 298 | for l in self.convs: 299 | remove_weight_norm(l) 300 | 301 | 302 | 303 | class Generator(torch.nn.Module): 304 | def __init__(self, h): 305 | super(Generator, self).__init__() 306 | self.h = h 307 | self.num_kernels = len(h.resblock_kernel_sizes) 308 | self.num_upsamples = len(h.upsample_rates) 309 | self.conv_pre = weight_norm(Conv1d(h.num_mels, h.upsample_initial_channel, 7, 1, padding=3)) # change: 80 --> 512 310 | resblock = ResBlock1 if h.resblock == '1' else ResBlock2 311 | 312 | self.ups = nn.ModuleList() 313 | for i, (u, k) in enumerate(zip(h.upsample_rates, h.upsample_kernel_sizes)): 314 | if (k-u) % 2 == 0: 315 | self.ups.append(weight_norm( 316 | ConvTranspose1d(h.upsample_initial_channel//(2**i), h.upsample_initial_channel//(2**(i+1)), 317 | k, u, padding=(k-u)//2))) 318 | else: 319 | self.ups.append(weight_norm( 320 | ConvTranspose1d(h.upsample_initial_channel//(2**i), h.upsample_initial_channel//(2**(i+1)), 321 | k, u, padding=(k-u)//2+1, output_padding=1))) 322 | 323 | 324 | self.resblocks = nn.ModuleList() 325 | for i in range(len(self.ups)): 326 | ch = h.upsample_initial_channel//(2**(i+1)) 327 | for j, (k, d) in enumerate(zip(h.resblock_kernel_sizes, h.resblock_dilation_sizes)): 328 | self.resblocks.append(resblock(h, ch, k, d)) 329 | 330 | self.conv_post = weight_norm(Conv1d(ch, 1, 7, 1, padding=3)) 331 | self.ups.apply(init_weights) 332 | self.conv_post.apply(init_weights) 333 | 334 | def forward(self, x): 335 | x = self.conv_pre(x) 336 | for i in range(self.num_upsamples): 337 | x = F.leaky_relu(x, LRELU_SLOPE) 338 | x = self.ups[i](x) 339 | xs = None 340 | for j in range(self.num_kernels): 341 | if xs is None: 342 | xs = self.resblocks[i*self.num_kernels+j](x) 343 | else: 344 | xs += self.resblocks[i*self.num_kernels+j](x) 345 | x = xs / self.num_kernels 346 | x = F.leaky_relu(x) 347 | x = self.conv_post(x) 348 | x = torch.tanh(x) 349 | 350 | return x 351 | 352 | def remove_weight_norm(self): 353 | for l in self.ups: 354 | remove_weight_norm(l) 355 | for l in self.resblocks: 356 | l.remove_weight_norm() 357 | remove_weight_norm(self.conv_pre) 358 | remove_weight_norm(self.conv_post) 359 | 360 | @classmethod 361 | def from_pretrained(cls, pretrained_model_name_or_path, subfolder=None): 362 | if subfolder is not None: 363 | pretrained_model_name_or_path = os.path.join(pretrained_model_name_or_path, subfolder) 364 | config_path = os.path.join(pretrained_model_name_or_path, "config.json") 365 | ckpt_path = os.path.join(pretrained_model_name_or_path, "vocoder.pt") 366 | 367 | config = get_config(config_path) 368 | vocoder = cls(config) 369 | 370 | state_dict_g = torch.load(ckpt_path) 371 | vocoder.load_state_dict(state_dict_g["generator"]) 372 | vocoder.eval() 373 | vocoder.remove_weight_norm() 374 | return vocoder 375 | 376 | 377 | @torch.no_grad() 378 | def inference(self, mels, lengths=None): 379 | self.eval() 380 | with torch.no_grad(): 381 | wavs = self(mels).squeeze(1) 382 | 383 | wavs = (wavs.cpu().numpy() * MAX_WAV_VALUE).astype("int16") 384 | 385 | if lengths is not None: 386 | wavs = wavs[:, :lengths] 387 | 388 | return wavs 389 | -------------------------------------------------------------------------------- /eval/configs/audiocaps/edit_add.csv: -------------------------------------------------------------------------------- 1 | Uid,Caption,edit_Caption 2 | jI8-_usilWE_0_10000,Motorcycle engine running,"Motorcycle engine running, and communicating" 3 | dYvKBG-8ha0_30000_40000,Helicopter blades spinning,"Helicopter blades spinning, as fireworks" 4 | ui4PHURhahA_100000_110000,A duck quacking,"A duck quacking, as jet engine" 5 | pOwNRM3BM_w_30000_40000,Water running continuously,"Water running continuously, while animals making noises" 6 | 9RtgQ1ElzfM_30000_40000,Water running from a flushed toilet,"Water running from a flushed toilet, as communicating" 7 | WLH87N00WoM_20000_30000,A man speaks followed by vibrations of a power tool,"A man speaks followed by vibrations of a power tool, and jet engine" 8 | -ftxbusleO8_30000_40000,An engine running,"An engine running, as communicating" 9 | ui4PHURhahA_100000_110000,A duck quacking,"A duck quacking, while train horn" 10 | IHnAMVPBoOA_80000_90000,Church bells ringing,"Church bells ringing, and Music plays" 11 | s1szx3qv84E_10000_20000,A bell is ringing,"A bell is ringing, with raining" 12 | ydO6ZIcghzU_0_10000,A person whistling,"A person whistling, with animals making noises" 13 | USle1qAiOdA_0_10000,A cat meowing twice,"A cat meowing twice, while Wind blowing" 14 | zyI4TfScXFA_30000_40000,Humming of a nearby jet engine,"Humming of a nearby jet engine, and raining" 15 | dpnzm2DPoBI_40000_50000,An infant crying,"An infant crying, with child shouts" 16 | jY2qBSkbZmc_230000_240000,Several loud burps,"Several loud burps, with helicopter flying" 17 | zpMwBUIuxgY_360000_370000,Typing on a computer keyboard,"Typing on a computer keyboard, and fireworks" 18 | F-qkeRTl8gY_0_10000,A vehicle engine starting up then running idle,"A vehicle engine starting up then running idle, with animals making noises" 19 | dpnzm2DPoBI_40000_50000,An infant crying,"An infant crying, and Music plays" 20 | zf9Uk3HjD4k_20000_30000,A telephone ringing,"A telephone ringing, with man talking" 21 | 4hjW385XyHE_30000_40000,A cat meows repeatedly,"A cat meows repeatedly, as child shouts" 22 | cfm3g8IHxcM_20000_30000,People are laughing,"People are laughing, as animals making noises" 23 | ktFiy07FH9s_60000_70000,A toilet is flushing,"A toilet is flushing, and Wind blowing" 24 | Gn2hjm9wx60_50000_60000,A speedboat is traveling across water,"A speedboat is traveling across water, with gun shot" 25 | dpnzm2DPoBI_40000_50000,An infant crying,"An infant crying, while man talking" 26 | 4dyO-l0BFOk_380000_390000,A car is accelerating,"A car is accelerating, while man talking" 27 | lz3At8iTA88_30000_40000,A boat engine running,"A boat engine running, as man talking" 28 | ziUoP8L-zkA_230000_240000,An aircraft engine running,"An aircraft engine running, while sound of an engine" 29 | 0m1sUvpvHu0_30000_40000,Pigeons are cooing,"Pigeons are cooing, and communicating" 30 | zvLcOfhGcGo_0_10000,A person snoring,"A person snoring, and sound of an engine" 31 | yrTqAYH5OM0_30000_40000,Snoring occurs in a rhythmic pattern,"Snoring occurs in a rhythmic pattern, while Wind blowing" 32 | GDMI4uJfDVw_10000_20000,A woman speaking,"A woman speaking, and train horn" 33 | zssgRs7HUmM_30000_40000,A clock ticking,"A clock ticking, and plastic creaks" 34 | Hd_HIXu79jY_30000_40000,A man delivering a speech,"A man delivering a speech, with dog barking" 35 | 0Y1dEz_tVlE_30000_40000,A motor vehicle engine is revving,"A motor vehicle engine is revving, with birds chirping" 36 | w-oSwpsXIuU_20000_30000,Multiple people laughing,"Multiple people laughing, while dog barking" 37 | ypY1zYRcfIc_100000_110000,A toilet flushing,"A toilet flushing, while fireworks" 38 | GDMI4uJfDVw_10000_20000,A woman speaking,"A woman speaking, as dog barking" 39 | zvLcOfhGcGo_0_10000,A person snoring,"A person snoring, as animals making noises" 40 | tZF7dJoT-P0_10000_20000,Continuous burping,"Continuous burping, while sound of an engine" 41 | zvLcOfhGcGo_0_10000,A person snoring,"A person snoring, and child shouts" 42 | zssgRs7HUmM_30000_40000,A clock ticking,"A clock ticking, as animals making noises" 43 | ypY1zYRcfIc_100000_110000,A toilet flushing,"A toilet flushing, and clock chiming" 44 | yWOeusquiJk_20000_30000,A person is snoring,"A person is snoring, and pigeon cooing" 45 | zpMwBUIuxgY_360000_370000,Typing on a computer keyboard,"Typing on a computer keyboard, with train horn" 46 | zO1h2bKYe58_140000_150000,An idle vehicle engine running,"An idle vehicle engine running, with pigeon cooing" 47 | yOpYiaqPwwQ_4000_14000,A baby crying repeatedly,"A baby crying repeatedly, with pigeon cooing" 48 | 91EvnH5ln4Q_30000_40000,A female speaking,"A female speaking, while jet engine" 49 | 8pu1qkMTn-Y_30000_40000,An adult male is speaking,"An adult male is speaking, while communicating" 50 | z8jsXi_J0KM_0_10000,Several goats bleat,"Several goats bleat, with dog barking" 51 | SaUDkGYRZHE_0_10000,A man laughing,"A man laughing, as gun shot" 52 | PzPl-WWTpXs_0_10000,Birds chirping continuously,"Birds chirping continuously, as dog barking" 53 | -ftxbusleO8_30000_40000,An engine running,"An engine running, and helicopter flying" 54 | WE-_ALOv8rI_130000_140000,A man speaks followed by whistling,"A man speaks followed by whistling, and Wind blowing" 55 | -ftxbusleO8_30000_40000,An engine running,"An engine running, and dog barking" 56 | ypY1zYRcfIc_100000_110000,A toilet flushing,"A toilet flushing, while dog barking" 57 | 0V0MxUBtx9g_120000_130000,A motorcycle engine is idling,"A motorcycle engine is idling, with jet engine" 58 | Myma5QTDDO4_110000_120000,A man speaking on a microphone,"A man speaking on a microphone, with raining" 59 | yZpTj9YT-IQ_30000_40000,A man speaking into a microphone,"A man speaking into a microphone, as train horn" 60 | yx6sC-GArA0_3000_13000,A woman speaks followed by a dog barking,"A woman speaks followed by a dog barking, as child shouts" 61 | tNUE8iLhGtg_250000_260000,A toy helicopter flying,"A toy helicopter flying, as gun shot" 62 | sLPAcgyETPM_30000_40000,Female speaking,"Female speaking, as animals making noises" 63 | yc6NJXKEIEk_30000_40000,An engine revving,"An engine revving, and man talking" 64 | z_zkX-8Kf3Q_0_10000,A crowd applauds,"A crowd applauds, while Music plays" 65 | zO1h2bKYe58_140000_150000,An idle vehicle engine running,"An idle vehicle engine running, and pigeon cooing" 66 | FRgQXtYWHhY_30000_40000,A helicopter is in flight,"A helicopter is in flight, as gun shot" 67 | ypY1zYRcfIc_100000_110000,A toilet flushing,"A toilet flushing, while child shouts" 68 | 4XHI4u7VidE_30000_40000,A cat is meowing,"A cat is meowing, and fireworks" 69 | KOO6F0sxAnQ_100000_110000,A man speaking followed by typing on a keyboard,"A man speaking followed by typing on a keyboard, as helicopter flying" 70 | zssgRs7HUmM_30000_40000,A clock ticking,"A clock ticking, with Wind blowing" 71 | xoK7YdYm80I_30000_40000,A helicopter flying,"A helicopter flying, and jet engine" 72 | l2-WXh34EV4_20000_30000,A car engine revving,"A car engine revving, and man talking" 73 | kNGXPjQB9RU_0_10000,A person snores,"A person snores, with train horn" 74 | zpMwBUIuxgY_360000_370000,Typing on a computer keyboard,"Typing on a computer keyboard, while helicopter flying" 75 | dpnzm2DPoBI_40000_50000,An infant crying,"An infant crying, with child shouts" 76 | L77rpdzefmI_30000_40000,Male snoring loudly,"Male snoring loudly, with pigeon cooing" 77 | Amf2hhiHXEo_16000_26000,Engine idling consistently,"Engine idling consistently, and animals making noises" 78 | 84hExVbYonU_30000_40000,A female voice speaking on a microphone,"A female voice speaking on a microphone, as child shouts" 79 | 8eZpe3HQaPk_90000_100000,A helicopter is flying,"A helicopter is flying, as Wind blowing" 80 | pmeh7ocxLP4_0_10000,Continuous snoring,"Continuous snoring, with plastic creaks" 81 | 66a2VZZaY0U_2000_12000,An engine revs several times,"An engine revs several times, with plastic creaks" 82 | zNLca6Dh6-k_120000_130000,Vibrations from a sewing machine,"Vibrations from a sewing machine, and fireworks" 83 | 9285qhAlkZw_30000_40000,A woman gives a speech,"A woman gives a speech, with animals making noises" 84 | zaHl7LdouwY_0_10000,An engine idling continuously,"An engine idling continuously, with communicating" 85 | Bt_nfD_TvcU_30000_40000,A woman delivering a speech,"A woman delivering a speech, and plastic creaks" 86 | ypY1zYRcfIc_100000_110000,A toilet flushing,"A toilet flushing, with animals making noises" 87 | VsfKc_e21WQ_30000_40000,Water running quickly,"Water running quickly, with Wind blowing" 88 | CZFMj95X_sQ_70000_80000,Musical whistling followed by a man speaking,"Musical whistling followed by a man speaking, and gun shot" 89 | 51NfcA7gYzs_20000_30000,Several bells ring,"Several bells ring, while jet engine" 90 | 7pyR815qI4Y_140000_150000,An audience clapping,"An audience clapping, as sound of an engine" 91 | 6C-OgqhhxWk_21000_31000,A motor vehicle engine is idling,"A motor vehicle engine is idling, and Music plays" 92 | yizOr1hPxc8_70000_80000,A helicopter engine running idle,"A helicopter engine running idle, with sound of an engine" 93 | 1mEYMdHfCyc_0_10000,A person types on a keyboard,"A person types on a keyboard, with fireworks" 94 | uEsAup9rS1E_80000_90000,An engine buzzing consistently,"An engine buzzing consistently, as sound of an engine" 95 | 4XHI4u7VidE_30000_40000,A cat is meowing,"A cat is meowing, and man talking" 96 | me05JI_gDac_230000_240000,An engine idling consistently,"An engine idling consistently, and jet engine" 97 | 23cYX9DvH10_3000_13000,A vehicle engine is revving up,"A vehicle engine is revving up, with man talking" 98 | ypY1zYRcfIc_100000_110000,A toilet flushing,"A toilet flushing, while child shouts" 99 | WA9ASASYS8Y_30000_40000,A man speaking,"A man speaking, as dog barking" 100 | zsoRpeRkN34_40000_50000,An aircraft engine sharply running,"An aircraft engine sharply running, and jet engine" 101 | yOpYiaqPwwQ_4000_14000,A baby crying repeatedly,"A baby crying repeatedly, and fireworks" 102 | GDMI4uJfDVw_10000_20000,A woman speaking,"A woman speaking, with clock chiming" 103 | yWOeusquiJk_20000_30000,A person is snoring,"A person is snoring, as plastic creaks" 104 | zssgRs7HUmM_30000_40000,A clock ticking,"A clock ticking, with man talking" 105 | 1IjAxJaJUSk_24000_34000,An engine is vibrating,"An engine is vibrating, while Wind blowing" 106 | 4Ko2TdqMgks_0_10000,Rapid typing on a keyboard,"Rapid typing on a keyboard, and Wind blowing" 107 | yPmy-WCeGKs_0_10000,An engine revving continuously,"An engine revving continuously, as gun shot" 108 | EGve_FMNOwE_30000_40000,A woman is speaking,"A woman is speaking, with clock chiming" 109 | WE-_ALOv8rI_130000_140000,A man speaks followed by whistling,"A man speaks followed by whistling, and dog barking" 110 | 4Jo3aJriewE_30000_40000,A woman delivers a speech,"A woman delivers a speech, as clock chiming" 111 | tNUE8iLhGtg_250000_260000,A toy helicopter flying,"A toy helicopter flying, as train horn" 112 | x9J9pyshnJI_30000_40000,A man speaking continuously,"A man speaking continuously, as man talking" 113 | z2QsWDZMXd0_30000_40000,Ticking of a clock,"Ticking of a clock, with raining" 114 | zvLcOfhGcGo_0_10000,A person snoring,"A person snoring, and sound of an engine" 115 | 6DAUg0daI1I_0_10000,A baby continuously crying,"A baby continuously crying, while woman yells" 116 | tKYiWYC8kiM_290000_300000,Bells ringing a tune,"Bells ringing a tune, as dog barking" 117 | yWOeusquiJk_20000_30000,A person is snoring,"A person is snoring, while plastic creaks" 118 | V7Fcv63Frzc_90000_100000,A man speaks then whistles,"A man speaks then whistles, with dog barking" 119 | ypY1zYRcfIc_100000_110000,A toilet flushing,"A toilet flushing, as fireworks" 120 | 3bNJYlKP5-o_10000_20000,Liquid is being sprayed,"Liquid is being sprayed, while sound of an engine" 121 | zssgRs7HUmM_30000_40000,A clock ticking,"A clock ticking, and pigeon cooing" 122 | -3Tlimy9kJI_240000_250000,A steam engine is hissing,"A steam engine is hissing, while jet engine" 123 | 94rJ6d6WGfk_30000_40000,A young woman speaking,"A young woman speaking, while child shouts" 124 | 6DSQhMvqrTQ_400000_410000,Humming from a large engine,"Humming from a large engine, as animals making noises" 125 | zNLca6Dh6-k_120000_130000,Vibrations from a sewing machine,"Vibrations from a sewing machine, as clock chiming" 126 | zNW0z-SHXTM_70000_80000,Humming of an idling engine,"Humming of an idling engine, as plastic creaks" 127 | yrs-SKTAA9I_80000_90000,Typing on a keyboard,"Typing on a keyboard, with helicopter flying" 128 | lAnEeB55y6g_160000_170000,A car engine idling,"A car engine idling, with train horn" 129 | ylQ3is7jdng_110000_120000,Large church bells ring,"Large church bells ring, with birds chirping" 130 | zO1h2bKYe58_140000_150000,An idle vehicle engine running,"An idle vehicle engine running, with raining" 131 | x9J9pyshnJI_30000_40000,A man speaking continuously,"A man speaking continuously, with fireworks" 132 | zyEGay_RV24_30000_40000,A helicopter flying in the distance,"A helicopter flying in the distance, while helicopter flying" 133 | Bt_nfD_TvcU_30000_40000,A woman delivering a speech,"A woman delivering a speech, as pigeon cooing" 134 | GDMI4uJfDVw_10000_20000,A woman speaking,"A woman speaking, as helicopter flying" 135 | dmMPzURfUw8_480000_490000,"Loud, continuous sizzling","Loud, continuous sizzling, with woman yells" 136 | Eag1nz2oUbk_30000_40000,A woman speaking on a microphone,"A woman speaking on a microphone, with communicating" 137 | yHRLqGdgc6I_40000_50000,A person snoring repetitively,"A person snoring repetitively, as sound of an engine" 138 | 5er9MoTAogc_10000_20000,A baby crying loudly,"A baby crying loudly, as communicating" 139 | GEBMPrzW0NI_30000_40000,A man is speaking,"A man is speaking, with woman yells" 140 | zyEGay_RV24_30000_40000,A helicopter flying in the distance,"A helicopter flying in the distance, with fireworks" 141 | yc6NJXKEIEk_30000_40000,An engine revving,"An engine revving, and child shouts" 142 | 1760Zs-Rezw_20000_30000,A goat bleating,"A goat bleating, with plastic creaks" 143 | 91EvnH5ln4Q_30000_40000,A female speaking,"A female speaking, while jet engine" 144 | z3gvS28om7o_180000_190000,A woman giving a speech,"A woman giving a speech, and jet engine" 145 | 7yYAqtNqM78_0_10000,A person burps loudly,"A person burps loudly, with jet engine" 146 | jY2qBSkbZmc_230000_240000,Several loud burps,"Several loud burps, with gun shot" 147 | zsvaHUeivRo_170000_180000,A power tool drilling,"A power tool drilling, while raining" 148 | yrs-SKTAA9I_80000_90000,Typing on a keyboard,"Typing on a keyboard, while Wind blowing" 149 | GDMI4uJfDVw_10000_20000,A woman speaking,"A woman speaking, while Wind blowing" 150 | U5pCL_ogYuc_30000_40000,An idle motorboat engine running,"An idle motorboat engine running, and birds chirping" 151 | WA9ASASYS8Y_30000_40000,A man speaking,"A man speaking, as helicopter flying" 152 | -M2w9Xc4MUw_1000_11000,Tires squealing followed by an engine revving,"Tires squealing followed by an engine revving, and fireworks" 153 | Bt_nfD_TvcU_30000_40000,A woman delivering a speech,"A woman delivering a speech, and Wind blowing" 154 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # AudioEditor: A Training-Free Diffusion-Based Audio Editing Framework 2 | 3 | [Demo Page](https://kiri0824.github.io/AudioEditor-demo-page/) | [Arxiv Paper](https://arxiv.org/pdf/2409.12466) 4 | 5 | Diffusion-based text-to-audio (TTA) generation has made substantial progress, leveraging Latent Diffusion Model (LDM) to produce high-quality, diverse and instruction-relevant audios. However, beyond generation, the task of audio editing remains equally important but has received comparatively little attention. Audio editing tasks face two primary challenges: executing precise edits and preserving the unedited sections. While workflows based on LDMs have effectively addressed these challenges in the field of image processing, similar approaches have been scarcely applied to audio editing. In this paper, we introduce AudioEditor, a training-free audio editing framework built on the pretrained diffusion-based TTA model. AudioEditor incorporates Null-text Inversion and EOT-Suppression methods, enabling the model to preserve original audio features while executing accurate edits. Comprehensive objective and subjective experiments validate the effectiveness of AudioEditor in delivering high-quality audio edits. 6 | 7 |
9 |
50 |
51 | To run the deletion on an audio with a simple example, use the following command:
52 | ```bash
53 | python main.py --prompt "After a gunshot, there was a burst of dog barking" \
54 | --audio_path "audio_examples/input_audios/After a gunshot, there was a burst of dog barking.wav" \
55 | --token_indices "[[10,11]]" \
56 | --alpha "[1.,]" --cross_retain_steps "[.2,]"
57 | ```
58 |
59 | ### 2️⃣ Replace
60 |
61 |
62 |
63 | To run the Replacement on an audio with a simple example, use the following command:
64 | ```bash
65 | python main.py --prompt "After a thunder, there was a burst of dog barking" \
66 | --audio_path "audio_examples/input_audios/After a gunshot, there was a burst of dog barking.wav" \
67 | --token_indices "[[3]]" \
68 | --alpha "[-0.001,]" --cross_retain_steps "[.2,]"
69 | ```
70 |
71 | ### 3️⃣ Add
72 |
73 |
74 | ```bash
75 | python main.py --prompt "A woman is giving a speech amid applause" \
76 | --audio_path "audio_examples/input_audios/A woman is giving a speech.wav" \
77 | --token_indices "[[7,8]]" \
78 | --alpha "[-0.001,]" --cross_retain_steps "[.2,]"
79 | ```
80 | ## 📐 Quantitative comparison
81 |
82 |
83 |
84 |
85 |
86 |
87 | | Edit_Models | 92 |Edit_Type | 93 |Overall Quality | 94 |Similarity with (Regenerated_wavs) | 95 |Similarity with (Original_wavs) | 96 ||||||
| Clap↑ | 99 |IS ↑ | 100 |FD ↓ | 101 |FAD ↓ | 102 |KL ↓ | 103 |FD ↓ | 104 |FAD ↓ | 105 |KL ↓ | 106 |||
| Original_wavs | 111 |add | 112 |51.4% | 113 |5.64 | 114 |44.71 | 115 |5.28 | 116 |1.78 | 117 |- | 118 |- | 119 |- | 120 |
| delete | 123 |51.5% | 124 |4.26 | 125 |51.82 | 126 |6.16 | 127 |1.85 | 128 |- | 129 |- | 130 |- | 131 ||
| replace | 134 |41.6% | 135 |4.41 | 136 |69.92 | 137 |7.88 | 138 |4.56 | 139 |- | 140 |- | 141 |- | 142 ||
| Average | 145 |48.2% | 146 |4.77 | 147 |55.48 | 148 |6.45 | 149 |2.73 | 150 |- | 151 |- | 152 |- | 153 ||
| Regenerated_wavs | 156 |add | 157 |59.7% | 158 |5.96 | 159 |- | 160 |- | 161 |- | 162 |44.71 | 163 |5.28 | 164 |1.36 | 165 |
| delete | 168 |59.1% | 169 |4.47 | 170 |- | 171 |- | 172 |- | 173 |51.82 | 174 |6.16 | 175 |2.39 | 176 ||
| replace | 179 |58.9% | 180 |5.13 | 181 |- | 182 |- | 183 |- | 184 |69.92 | 185 |7.88 | 186 |4.09 | 187 ||
| Average | 190 |59.2% | 191 |5.19 | 192 |- | 193 |- | 194 |- | 195 |55.48 | 196 |6.45 | 197 |2.61 | 198 ||
| SDEdit(baseline) | 201 |add | 202 |58.4% | 203 |6.36 | 204 |27.89 | 205 |2.74 | 206 |0.79 | 207 |36.74 | 208 |3.08 | 209 |1.08 | 210 |
| delete | 213 |53.3% | 214 |5.31 | 215 |55.12 | 216 |6.65 | 217 |1.78 | 218 |40.43 | 219 |6.95 | 220 |0.88 | 221 ||
| replace | 224 |58.6% | 225 |4.99 | 226 |29.76 | 227 |3.24 | 228 |0.80 | 229 |55.21 | 230 |7.00 | 231 |3.40 | 232 ||
| Average | 235 |56.8% | 236 |5.55 | 237 |37.59 | 238 |4.21* | 239 |1.12* | 240 |44.13* | 241 |5.68* | 242 |1.79 | 243 ||
| AudioEditor(ours) | 246 |add | 247 |59.4% | 248 |6.16 | 249 |27.83 | 250 |2.41 | 251 |0.85 | 252 |40.00 | 253 |3.52 | 254 |1.27 | 255 |
| delete | 258 |54.1% | 259 |4.75 | 260 |52.56 | 261 |5.02 | 262 |1.54 | 263 |37.16 | 264 |4.91 | 265 |1.05 | 266 ||
| replace | 269 |58.1% | 270 |5.14 | 271 |28.80 | 272 |3.34 | 273 |0.79 | 274 |59.46 | 275 |7.52 | 276 |3.73 | 277 ||
| Average | 280 |57.6%* | 281 |5.19* | 282 |37.63* | 283 |3.27 | 284 |1.07 | 285 |43.48 | 286 |4.95 | 287 |1.93* | 288 ||