Spaces:
Runtime error
Runtime error
initial commit
Browse filesThis view is limited to 50 files because it contains too many changes. See raw diff
Files changed (50)
- .gitattributes+1
- .gitignoreAdded+2
- Examples/Beethoven.wavAdded+3
- Examples/Beethoven_arcade.wavAdded+3
- Examples/Beethoven_piano.wavAdded+3
- Examples/Cat.wavAdded+3
- Examples/Cat_dog.wavAdded+3
- Examples/ModalJazz.wavAdded+3
- Examples/ModalJazz_banjo.wavAdded+3
- app.pyAdded+271
- audioldm/__init__.pyAdded+8
- audioldm/__main__.pyAdded+183
- audioldm/audio/__init__.pyAdded+2
- audioldm/audio/audio_processing.pyAdded+100
- audioldm/audio/stft.pyAdded+180
- audioldm/audio/tools.pyAdded+85
- audioldm/clap/__init__.pyAdded
- audioldm/clap/encoders.pyAdded+171
- audioldm/clap/open_clip/__init__.pyAdded+25
- audioldm/clap/open_clip/bert.pyAdded+40
- audioldm/clap/open_clip/bpe_simple_vocab_16e6.txt.gzAdded+3
- audioldm/clap/open_clip/factory.pyAdded+279
- audioldm/clap/open_clip/feature_fusion.pyAdded+192
- audioldm/clap/open_clip/htsat.pyAdded+1308
- audioldm/clap/open_clip/linear_probe.pyAdded+66
- audioldm/clap/open_clip/loss.pyAdded+398
- audioldm/clap/open_clip/model.pyAdded+936
- audioldm/clap/open_clip/model_configs/HTSAT-base.jsonAdded+23
- audioldm/clap/open_clip/model_configs/HTSAT-large.jsonAdded+23
- audioldm/clap/open_clip/model_configs/HTSAT-tiny-win-1536.jsonAdded+23
- audioldm/clap/open_clip/model_configs/HTSAT-tiny.jsonAdded+23
- audioldm/clap/open_clip/model_configs/PANN-10.jsonAdded+23
- audioldm/clap/open_clip/model_configs/PANN-14-fmax-18k.jsonAdded+23
- audioldm/clap/open_clip/model_configs/PANN-14-fmax-8k-20s.jsonAdded+23
- audioldm/clap/open_clip/model_configs/PANN-14-tiny-transformer.jsonAdded+23
- audioldm/clap/open_clip/model_configs/PANN-14-win-1536.jsonAdded+23
- audioldm/clap/open_clip/model_configs/PANN-14.jsonAdded+23
- audioldm/clap/open_clip/model_configs/PANN-6.jsonAdded+23
- audioldm/clap/open_clip/model_configs/RN101-quickgelu.jsonAdded+22
- audioldm/clap/open_clip/model_configs/RN101.jsonAdded+21
- audioldm/clap/open_clip/model_configs/RN50-quickgelu.jsonAdded+22
- audioldm/clap/open_clip/model_configs/RN50.jsonAdded+21
- audioldm/clap/open_clip/model_configs/RN50x16.jsonAdded+21
- audioldm/clap/open_clip/model_configs/RN50x4.jsonAdded+21
- audioldm/clap/open_clip/model_configs/ViT-B-16.jsonAdded+16
- audioldm/clap/open_clip/model_configs/ViT-B-32-quickgelu.jsonAdded+17
- audioldm/clap/open_clip/model_configs/ViT-B-32.jsonAdded+16
- audioldm/clap/open_clip/model_configs/ViT-L-14.jsonAdded+16
- audioldm/clap/open_clip/openai.pyAdded+159
- audioldm/clap/open_clip/pann_model.pyAdded+704
32 unmodified lines33343532 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text32 unmodified lines3334353632 unmodified lines*.zip filter=lfs diff=lfs merge=lfs -text*.zst filter=lfs diff=lfs merge=lfs -text*tfevents* filter=lfs diff=lfs merge=lfs -text*.wav filter=lfs diff=lfs merge=lfs -text
12*.pyc
123version https://git-lfs.github.com/spec/v1oid sha256:30a6a087a9e0eb87422aa3b48ad966eabb1dfe105d73a25d356b71d3aee31493size 4828972123version https://git-lfs.github.com/spec/v1oid sha256:ccd929b93c15706f2102a27973d490a84ce0eb97faba6a92ece0c6d81ed2c26esize 1794746123version https://git-lfs.github.com/spec/v1oid sha256:5787c31b0b3c78dec33d651d437364785713042e7cfce2290cf4baf01f65ac6fsize 1794746123version https://git-lfs.github.com/spec/v1oid sha256:27b43763a8d9ac90dc78285ed9817b16524f24b4f4d1aa399616f1a04d4a9fd9size 1920508123version https://git-lfs.github.com/spec/v1oid sha256:90a97dc229eeccef307dd40db97fb09cc439ce0b45a320fd84b2ea6b03d0deb2size 327822123version https://git-lfs.github.com/spec/v1oid sha256:846a77046d21ebc3996841404eede9d56797c82b3414025e1ccafe586eaf2959size 9153322123version https://git-lfs.github.com/spec/v1oid sha256:122e0078c0bf2fc96425071706fe0e8674c93cc1d2787fd02c0e2c0f12de5cc5size 6802106123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271import gradio as grimport randomimport torchimport torchaudiofrom torch import inference_modefrom tempfile import NamedTemporaryFileimport numpy as npfrom models import load_modelimport utilsfrom inversion_utils import inversion_forward_process, inversion_reverse_process
def randomize_seed_fn(seed, randomize_seed): if randomize_seed: seed = random.randint(0, np.iinfo(np.int32).max) torch.manual_seed(seed) return seed
def invert(x0, prompt_src, num_diffusion_steps, cfg_scale_src): # , ldm_stable): ldm_stable.model.scheduler.set_timesteps(num_diffusion_steps, device=device)
with inference_mode(): w0 = ldm_stable.vae_encode(x0)
# find Zs and wts - forward process _, zs, wts = inversion_forward_process(ldm_stable, w0, etas=1, prompts=[prompt_src], cfg_scales=[cfg_scale_src], prog_bar=True, num_inference_steps=num_diffusion_steps, numerical_fix=True) return zs, wts
def sample(zs, wts, steps, prompt_tar, tstart, cfg_scale_tar): # , ldm_stable): # reverse process (via Zs and wT) tstart = torch.tensor(tstart, dtype=torch.int) skip = steps - tstart w0, _ = inversion_reverse_process(ldm_stable, xT=wts, skips=steps - skip, etas=1., prompts=[prompt_tar], neg_prompts=[""], cfg_scales=[cfg_scale_tar], prog_bar=True, zs=zs[:int(steps - skip)])
# vae decode image with inference_mode(): x0_dec = ldm_stable.vae_decode(w0) if x0_dec.dim() < 4: x0_dec = x0_dec[None, :, :, :]
with torch.no_grad(): audio = ldm_stable.decode_to_mel(x0_dec)
f = NamedTemporaryFile("wb", suffix=".wav", delete=False) torchaudio.save(f.name, audio, sample_rate=16000)
return f.name
def edit(input_audio, model_id: str, do_inversion: bool, wts: gr.State, zs: gr.State, saved_inv_model: str, source_prompt="", target_prompt="", steps=200, cfg_scale_src=3.5, cfg_scale_tar=12, t_start=90, randomize_seed=True):
global ldm_stable, current_loaded_model print(f'current loaded model: {ldm_stable.model_id}') if model_id != current_loaded_model: print(f'Changing model to {model_id}...') current_loaded_model = model_id ldm_stable = None ldm_stable = load_model(model_id, device, steps)
# If the inversion was done for a different model, we need to re-run the inversion if not do_inversion and (saved_inv_model is None or saved_inv_model != model_id): do_inversion = True
x0 = utils.load_audio(input_audio, ldm_stable.get_fn_STFT(), device=device)
if do_inversion or randomize_seed: # always re-run inversion zs_tensor, wts_tensor = invert(x0=x0, prompt_src=source_prompt, num_diffusion_steps=steps, cfg_scale_src=cfg_scale_src) wts = gr.State(value=wts_tensor) zs = gr.State(value=zs_tensor) saved_inv_model = model_id do_inversion = False
output = sample(zs.value, wts.value, steps, prompt_tar=target_prompt, tstart=t_start, cfg_scale_tar=cfg_scale_tar)
return output, wts, zs, saved_inv_model, do_inversion
current_loaded_model = "cvssp/audioldm2-music"# current_loaded_model = "cvssp/audioldm2-music"device = torch.device("cuda" if torch.cuda.is_available() else "cpu")ldm_stable = load_model(current_loaded_model, device, 200) # deafult model
def get_example(): case = [ ['Examples/Beethoven.wav', '', 'A recording of an arcade game soundtrack.', 90, 'cvssp/audioldm2-music', '27s', 'Examples/Beethoven_arcade.wav', ], ['Examples/Beethoven.wav', 'A high quality recording of wind instruments and strings playing.', 'A high quality recording of a piano playing.', 90, 'cvssp/audioldm2-music', '27s', 'Examples/Beethoven_piano.wav', ], ['Examples/ModalJazz.wav', 'Trumpets playing alongside a piano, bass and drums in an upbeat old-timey cool jazz song.', 'A banjo playing alongside a piano, bass and drums in an upbeat old-timey cool country song.', 90, 'cvssp/audioldm2-music', '106s', 'Examples/ModalJazz_banjo.wav',], ['Examples/Cat.wav', '', 'A dog barking.', 150, 'cvssp/audioldm2-large', '10s', 'Examples/Cat_dog.wav',] ] return case
intro = """<h1 style="font-weight: 1400; text-align: center; margin-bottom: 7px;">Zero-Shot Text-Based Audio Editing Using DDPM Inversion</h1><h3 style="margin-bottom: 10px; text-align: center;"> <a href="https://arxiv.org/abs/2402.10009">[Paper]</a> | <a href="https://hilamanor.github.io/AudioEditing/">[Project page]</a> | <a href="https://github.com/HilaManor/AudioEditingCode">[Code]</a></h3><p style="font-size:large">Demo for the text-based editing method introduced in:<a href="https://arxiv.org/abs/2402.10009" style="text-decoration: underline;" target="_blank"> Zero-Shot Unsupervised and Text-Based Audio Editing Using DDPM Inversion </a> </p><p style="font-size:larger"><b>Instructions:</b><br>Provide an input audio and a target prompt to edit the audio. <br>T<sub>start</sub> is used to control the tradeoff between fidelity to the original signal and text-adhearance.Lower value -> favor fidelity. Higher value -> apply a stronger edit.<br>Make sure that you use an AudioLDM2 version that is suitable for your input audio.For example, use the music version for music and the large version for general audio.</p><p style="font-size:larger">You can additionally provide a source prompt to guide even further the editing process.</p><p style="font-size:larger">Longer input will take more time.</p><p style="font-size: 0.9rem; margin: 0rem; line-height: 1.2em; margin-top:1em">For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings.<a href="https://huggingface.co/spaces/hilamanor/audioEditing?duplicate=true"><img style="margin-top: 0em; margin-bottom: 0em; display:inline" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" ></a></p>
"""
with gr.Blocks(css='style.css') as demo: def reset_do_inversion(): do_inversion = gr.State(value=True) return do_inversion
gr.HTML(intro) wts = gr.State() zs = gr.State() saved_inv_model = gr.State() # current_loaded_model = gr.State(value="cvssp/audioldm2-music") # ldm_stable = load_model("cvssp/audioldm2-music", device, 200) # ldm_stable = gr.State(value=ldm_stable) do_inversion = gr.State(value=True) # To save some runtime when editing the same thing over and over
with gr.Row(): with gr.Column(): src_prompt = gr.Textbox(label="OPTIONAL: Source Prompt", lines=2, interactive=True, placeholder="Optional: Describe the original audio input",) input_audio = gr.Audio(sources=["upload", "microphone"], type="filepath", label="Input Audio", interactive=True, scale=1)
with gr.Column(): tar_prompt = gr.Textbox(label="Target Prompt", placeholder="Describe your desired edited output", lines=2, interactive=True) output_audio = gr.Audio(label="Edited Audio", interactive=False, scale=1)
with gr.Row(): with gr.Column(): submit = gr.Button("Edit")
with gr.Row(): t_start = gr.Slider(minimum=30, maximum=160, value=110, step=1, label="T-start", interactive=True, scale=3, info="Higher T-start -> stronger edit. Lower T-start -> more similar to original audio.") model_id = gr.Dropdown(label="AudioLDM2 Version", choices=["cvssp/audioldm2", "cvssp/audioldm2-large", "cvssp/audioldm2-music"], info="Choose a checkpoint suitable for your intended audio and edit.", value="cvssp/audioldm2-music", interactive=True, type="value", scale=2) with gr.Accordion("More Options", open=False):
with gr.Row(): cfg_scale_src = gr.Number(value=3, minimum=0.5, maximum=25, precision=None, label="Source Guidance Scale", interactive=True, scale=1) cfg_scale_tar = gr.Number(value=12, minimum=0.5, maximum=25, precision=None, label="Target Guidance Scale", interactive=True, scale=1) steps = gr.Number(value=200, precision=0, minimum=20, maximum=1000, label="Num Diffusion Steps", interactive=True, scale=1) with gr.Row(): seed = gr.Number(value=0, precision=0, label="Seed", interactive=True) randomize_seed = gr.Checkbox(label='Randomize seed', value=False) length = gr.Number(label="Length", interactive=False, visible=False)
def change_tstart_range(steps): t_start.maximum = int(160/200 * steps) t_start.minimum = int(30/200 * steps) if t_start.value > t_start.maximum: t_start.value = t_start.maximum if t_start.value < t_start.minimum: t_start.value = t_start.minimum return t_start
submit.click( fn=randomize_seed_fn, inputs=[seed, randomize_seed], outputs=[seed], queue=False).then( fn=edit, inputs=[input_audio, model_id, do_inversion, # current_loaded_model, ldm_stable, wts, zs, saved_inv_model, src_prompt, tar_prompt, steps, cfg_scale_src, cfg_scale_tar, t_start, randomize_seed ], outputs=[output_audio, wts, zs, saved_inv_model, do_inversion] # , current_loaded_model, ldm_stable], )
# If sources changed we have to rerun inversion input_audio.change(fn=reset_do_inversion, outputs=[do_inversion]) src_prompt.change(fn=reset_do_inversion, outputs=[do_inversion]) model_id.change(fn=reset_do_inversion, outputs=[do_inversion]) steps.change(fn=change_tstart_range, inputs=[steps], outputs=[t_start])
gr.Examples( label="Examples", examples=get_example(), inputs=[input_audio, src_prompt, tar_prompt, t_start, model_id, length, output_audio], outputs=[output_audio] )
demo.queue() demo.launch()12345678from .ldm import LatentDiffusionfrom .utils import seed_everything, save_wave, get_time, get_durationfrom .pipeline import *
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183#!/usr/bin/python3import osfrom audioldm import text_to_audio, style_transfer, build_model, save_wave, get_time, round_up_duration, get_durationimport argparse
CACHE_DIR = os.getenv( "AUDIOLDM_CACHE_DIR", os.path.join(os.path.expanduser("~"), ".cache/audioldm"))
parser = argparse.ArgumentParser()
parser.add_argument( "--mode", type=str, required=False, default="generation", help="generation: text-to-audio generation; transfer: style transfer", choices=["generation", "transfer"])
parser.add_argument( "-t", "--text", type=str, required=False, default="", help="Text prompt to the model for audio generation",)
parser.add_argument( "-f", "--file_path", type=str, required=False, default=None, help="(--mode transfer): Original audio file for style transfer; Or (--mode generation): the guidance audio file for generating simialr audio",)
parser.add_argument( "--transfer_strength", type=float, required=False, default=0.5, help="A value between 0 and 1. 0 means original audio without transfer, 1 means completely transfer to the audio indicated by text",)
parser.add_argument( "-s", "--save_path", type=str, required=False, help="The path to save model output", default="./output",)
parser.add_argument( "--model_name", type=str, required=False, help="The checkpoint you gonna use", default="audioldm-m-full", choices=["audioldm-s-full", "audioldm-l-full", "audioldm-s-full-v2","audioldm-m-text-ft", "audioldm-s-text-ft", "audioldm-m-full"])
parser.add_argument( "-ckpt", "--ckpt_path", type=str, required=False, help="The path to the pretrained .ckpt model", default=None,)
parser.add_argument( "-b", "--batchsize", type=int, required=False, default=1, help="Generate how many samples at the same time",)
parser.add_argument( "--ddim_steps", type=int, required=False, default=200, help="The sampling step for DDIM",)
parser.add_argument( "-gs", "--guidance_scale", type=float, required=False, default=2.5, help="Guidance scale (Large => better quality and relavancy to text; Small => better diversity)",)
parser.add_argument( "-dur", "--duration", type=float, required=False, default=10.0, help="The duration of the samples",)
parser.add_argument( "-n", "--n_candidate_gen_per_text", type=int, required=False, default=3, help="Automatic quality control. This number control the number of candidates (e.g., generate three audios and choose the best to show you). A Larger value usually lead to better quality with heavier computation",)
parser.add_argument( "--seed", type=int, required=False, default=42, help="Change this value (any integer number) will lead to a different generation result.",)
args = parser.parse_args()
if(args.ckpt_path is not None): print("Warning: ckpt_path has no effect after version 0.0.20.") assert args.duration % 2.5 == 0, "Duration must be a multiple of 2.5"
mode = args.modeif(mode == "generation" and args.file_path is not None): mode = "generation_audio_to_audio" if(len(args.text) > 0): print("Warning: You have specified the --file_path. --text will be ignored") args.text = "" save_path = os.path.join(args.save_path, mode)
if(args.file_path is not None): save_path = os.path.join(save_path, os.path.basename(args.file_path.split(".")[0]))
text = args.textrandom_seed = args.seedduration = args.durationguidance_scale = args.guidance_scalen_candidate_gen_per_text = args.n_candidate_gen_per_text
os.makedirs(save_path, exist_ok=True)audioldm = build_model(model_name=args.model_name)
if(args.mode == "generation"): waveform = text_to_audio( audioldm, text, args.file_path, random_seed, duration=duration, guidance_scale=guidance_scale, ddim_steps=args.ddim_steps, n_candidate_gen_per_text=n_candidate_gen_per_text, batchsize=args.batchsize, ) elif(args.mode == "transfer"): assert args.file_path is not None assert os.path.exists(args.file_path), "The original audio file \'%s\' for style transfer does not exist." % args.file_path waveform = style_transfer( audioldm, text, args.file_path, args.transfer_strength, random_seed, duration=duration, guidance_scale=guidance_scale, ddim_steps=args.ddim_steps, batchsize=args.batchsize, ) waveform = waveform[:,None,:]
save_wave(waveform, save_path, name="%s_%s" % (get_time(), text))12from .tools import wav_to_fbank, read_wav_filefrom .stft import TacotronSTFT123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100import torchimport numpy as npimport librosa.util as librosa_utilfrom scipy.signal import get_window
def window_sumsquare( window, n_frames, hop_length, win_length, n_fft, dtype=np.float32, norm=None,): """ # from librosa 0.6 Compute the sum-square envelope of a window function at a given hop length.
This is used to estimate modulation effects induced by windowing observations in short-time fourier transforms.
Parameters ---------- window : string, tuple, number, callable, or list-like Window specification, as in `get_window`
n_frames : int > 0 The number of analysis frames
hop_length : int > 0 The number of samples to advance between frames
win_length : [optional] The length of the window function. By default, this matches `n_fft`.
n_fft : int > 0 The length of each analysis frame.
dtype : np.dtype The data type of the output
Returns ------- wss : np.ndarray, shape=`(n_fft + hop_length * (n_frames - 1))` The sum-squared envelope of the window function """ if win_length is None: win_length = n_fft
n = n_fft + hop_length * (n_frames - 1) x = np.zeros(n, dtype=dtype)
# Compute the squared window at the desired length win_sq = get_window(window, win_length, fftbins=True) win_sq = librosa_util.normalize(win_sq, norm=norm) ** 2 win_sq = librosa_util.pad_center(win_sq, n_fft)
# Fill the envelope for i in range(n_frames): sample = i * hop_length x[sample : min(n, sample + n_fft)] += win_sq[: max(0, min(n_fft, n - sample))] return x
def griffin_lim(magnitudes, stft_fn, n_iters=30): """ PARAMS ------ magnitudes: spectrogram magnitudes stft_fn: STFT class with transform (STFT) and inverse (ISTFT) methods """
angles = np.angle(np.exp(2j * np.pi * np.random.rand(*magnitudes.size()))) angles = angles.astype(np.float32) angles = torch.autograd.Variable(torch.from_numpy(angles)) signal = stft_fn.inverse(magnitudes, angles).squeeze(1)
for i in range(n_iters): _, angles = stft_fn.transform(signal) signal = stft_fn.inverse(magnitudes, angles).squeeze(1) return signal
def dynamic_range_compression(x, normalize_fun=torch.log, C=1, clip_val=1e-5): """ PARAMS ------ C: compression factor """ return normalize_fun(torch.clamp(x, min=clip_val) * C)
def dynamic_range_decompression(x, C=1): """ PARAMS ------ C: compression factor used to compress """ return torch.exp(x) / C123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180import torchimport torch.nn.functional as Fimport numpy as npfrom scipy.signal import get_windowfrom librosa.util import pad_center, tinyfrom librosa.filters import mel as librosa_mel_fn
from audioldm.audio.audio_processing import ( dynamic_range_compression, dynamic_range_decompression, window_sumsquare,)
class STFT(torch.nn.Module): """adapted from Prem Seetharaman's https://github.com/pseeth/pytorch-stft"""
def __init__(self, filter_length, hop_length, win_length, window="hann"): super(STFT, self).__init__() self.filter_length = filter_length self.hop_length = hop_length self.win_length = win_length self.window = window self.forward_transform = None scale = self.filter_length / self.hop_length fourier_basis = np.fft.fft(np.eye(self.filter_length))
cutoff = int((self.filter_length / 2 + 1)) fourier_basis = np.vstack( [np.real(fourier_basis[:cutoff, :]), np.imag(fourier_basis[:cutoff, :])] )
forward_basis = torch.FloatTensor(fourier_basis[:, None, :]) inverse_basis = torch.FloatTensor( np.linalg.pinv(scale * fourier_basis).T[:, None, :] )
if window is not None: assert filter_length >= win_length # get window and zero center pad it to filter_length fft_window = get_window(window, win_length, fftbins=True) fft_window = pad_center(fft_window, size=filter_length) fft_window = torch.from_numpy(fft_window).float()
# window the bases forward_basis *= fft_window inverse_basis *= fft_window
self.register_buffer("forward_basis", forward_basis.float()) self.register_buffer("inverse_basis", inverse_basis.float())
def transform(self, input_data): num_batches = input_data.size(0) num_samples = input_data.size(1)
self.num_samples = num_samples
# similar to librosa, reflect-pad the input input_data = input_data.view(num_batches, 1, num_samples) input_data = F.pad( input_data.unsqueeze(1), (int(self.filter_length / 2), int(self.filter_length / 2), 0, 0), mode="reflect", ) input_data = input_data.squeeze(1)
forward_transform = F.conv1d( input_data, torch.autograd.Variable(self.forward_basis, requires_grad=False), stride=self.hop_length, padding=0, ).cpu()
cutoff = int((self.filter_length / 2) + 1) real_part = forward_transform[:, :cutoff, :] imag_part = forward_transform[:, cutoff:, :]
magnitude = torch.sqrt(real_part**2 + imag_part**2) phase = torch.autograd.Variable(torch.atan2(imag_part.data, real_part.data))
return magnitude, phase
def inverse(self, magnitude, phase): recombine_magnitude_phase = torch.cat( [magnitude * torch.cos(phase), magnitude * torch.sin(phase)], dim=1 )
inverse_transform = F.conv_transpose1d( recombine_magnitude_phase, torch.autograd.Variable(self.inverse_basis, requires_grad=False), stride=self.hop_length, padding=0, )
if self.window is not None: window_sum = window_sumsquare( self.window, magnitude.size(-1), hop_length=self.hop_length, win_length=self.win_length, n_fft=self.filter_length, dtype=np.float32, ) # remove modulation effects approx_nonzero_indices = torch.from_numpy( np.where(window_sum > tiny(window_sum))[0] ) window_sum = torch.autograd.Variable( torch.from_numpy(window_sum), requires_grad=False ) window_sum = window_sum inverse_transform[:, :, approx_nonzero_indices] /= window_sum[ approx_nonzero_indices ]
# scale by hop ratio inverse_transform *= float(self.filter_length) / self.hop_length
inverse_transform = inverse_transform[:, :, int(self.filter_length / 2) :] inverse_transform = inverse_transform[:, :, : -int(self.filter_length / 2) :]
return inverse_transform
def forward(self, input_data): self.magnitude, self.phase = self.transform(input_data) reconstruction = self.inverse(self.magnitude, self.phase) return reconstruction
class TacotronSTFT(torch.nn.Module): def __init__( self, filter_length, hop_length, win_length, n_mel_channels, sampling_rate, mel_fmin, mel_fmax, ): super(TacotronSTFT, self).__init__() self.n_mel_channels = n_mel_channels self.sampling_rate = sampling_rate self.stft_fn = STFT(filter_length, hop_length, win_length) mel_basis = librosa_mel_fn( sr=sampling_rate, n_fft=filter_length, n_mels=n_mel_channels, fmin=mel_fmin, fmax=mel_fmax ) mel_basis = torch.from_numpy(mel_basis).float() self.register_buffer("mel_basis", mel_basis)
def spectral_normalize(self, magnitudes, normalize_fun): output = dynamic_range_compression(magnitudes, normalize_fun) return output
def spectral_de_normalize(self, magnitudes): output = dynamic_range_decompression(magnitudes) return output
def mel_spectrogram(self, y, normalize_fun=torch.log): """Computes mel-spectrograms from a batch of waves PARAMS ------ y: Variable(torch.FloatTensor) with shape (B, T) in range [-1, 1]
RETURNS ------- mel_output: torch.FloatTensor of shape (B, n_mel_channels, T) """ assert torch.min(y.data) >= -1, torch.min(y.data) assert torch.max(y.data) <= 1, torch.max(y.data)
magnitudes, phases = self.stft_fn.transform(y) magnitudes = magnitudes.data mel_output = torch.matmul(self.mel_basis, magnitudes) mel_output = self.spectral_normalize(mel_output, normalize_fun) energy = torch.norm(magnitudes, dim=1)
log_magnitudes = self.spectral_normalize(magnitudes, normalize_fun)
return mel_output, log_magnitudes, energy12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485import torchimport numpy as npimport torchaudio
def get_mel_from_wav(audio, _stft): audio = torch.clip(torch.FloatTensor(audio).unsqueeze(0), -1, 1) audio = torch.autograd.Variable(audio, requires_grad=False) melspec, log_magnitudes_stft, energy = _stft.mel_spectrogram(audio) melspec = torch.squeeze(melspec, 0).numpy().astype(np.float32) log_magnitudes_stft = ( torch.squeeze(log_magnitudes_stft, 0).numpy().astype(np.float32) ) energy = torch.squeeze(energy, 0).numpy().astype(np.float32) return melspec, log_magnitudes_stft, energy
def _pad_spec(fbank, target_length=1024): n_frames = fbank.shape[0] p = target_length - n_frames # cut and pad if p > 0: m = torch.nn.ZeroPad2d((0, 0, 0, p)) fbank = m(fbank) elif p < 0: fbank = fbank[0:target_length, :]
if fbank.size(-1) % 2 != 0: fbank = fbank[..., :-1]
return fbank
def pad_wav(waveform, segment_length): waveform_length = waveform.shape[-1] assert waveform_length > 100, "Waveform is too short, %s" % waveform_length if segment_length is None or waveform_length == segment_length: return waveform elif waveform_length > segment_length: return waveform[:segment_length] elif waveform_length < segment_length: temp_wav = np.zeros((1, segment_length)) temp_wav[:, :waveform_length] = waveform return temp_wav
def normalize_wav(waveform): waveform = waveform - np.mean(waveform) waveform = waveform / (np.max(np.abs(waveform)) + 1e-8) return waveform * 0.5
def read_wav_file(filename, segment_length): # waveform, sr = librosa.load(filename, sr=None, mono=True) # 4 times slower waveform, sr = torchaudio.load(filename) # Faster!!! waveform = torchaudio.functional.resample(waveform, orig_freq=sr, new_freq=16000) waveform = waveform.numpy()[0, ...] waveform = normalize_wav(waveform) waveform = waveform[None, ...] waveform = pad_wav(waveform, segment_length) waveform = waveform / np.max(np.abs(waveform)) waveform = 0.5 * waveform return waveform
def wav_to_fbank(filename, target_length=1024, fn_STFT=None): assert fn_STFT is not None
# mixup waveform = read_wav_file(filename, target_length * 160) # hop size is 160
waveform = waveform[0, ...] waveform = torch.FloatTensor(waveform)
fbank, log_magnitudes_stft, energy = get_mel_from_wav(waveform, fn_STFT)
fbank = torch.FloatTensor(fbank.T) log_magnitudes_stft = torch.FloatTensor(log_magnitudes_stft.T)
fbank, log_magnitudes_stft = _pad_spec(fbank, target_length), _pad_spec( log_magnitudes_stft, target_length )
return fbank, log_magnitudes_stft, waveform123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171import torchimport torch.nn as nnfrom audioldm.clap.open_clip import create_modelfrom audioldm.clap.training.data import get_audio_featuresimport torchaudiofrom transformers import RobertaTokenizerimport torch.nn.functional as F
class CLAPAudioEmbeddingClassifierFreev2(nn.Module): def __init__( self, pretrained_path="", key="class", sampling_rate=16000, embed_mode="audio", amodel = "HTSAT-tiny", unconditional_prob=0.1, random_mute=False, max_random_mute_portion=0.5, training_mode=True, ): super().__init__()
self.key = key self.device = "cpu" self.precision = "fp32" self.amodel = amodel # or 'PANN-14' self.tmodel = "roberta" # the best text encoder in our training self.enable_fusion = False # False if you do not want to use the fusion model self.fusion_type = "aff_2d" self.pretrained = pretrained_path self.embed_mode = embed_mode self.embed_mode_orig = embed_mode self.sampling_rate = sampling_rate self.unconditional_prob = unconditional_prob self.random_mute = random_mute self.tokenize = RobertaTokenizer.from_pretrained("roberta-base") self.max_random_mute_portion = max_random_mute_portion self.training_mode = training_mode self.model, self.model_cfg = create_model( self.amodel, self.tmodel, self.pretrained, precision=self.precision, device=self.device, enable_fusion=self.enable_fusion, fusion_type=self.fusion_type, ) for p in self.model.parameters(): p.requires_grad = False
self.model.eval()
def get_unconditional_condition(self, batchsize): self.unconditional_token = self.model.get_text_embedding( self.tokenizer(["", ""]) )[0:1] return torch.cat([self.unconditional_token.unsqueeze(0)] * batchsize, dim=0)
def batch_to_list(self, batch): ret = [] for i in range(batch.size(0)): ret.append(batch[i]) return ret
def make_decision(self, probability): if float(torch.rand(1)) < probability: return True else: return False
def random_uniform(self, start, end): val = torch.rand(1).item() return start + (end - start) * val
def _random_mute(self, waveform): # waveform: [bs, t-steps] t_steps = waveform.size(-1) for i in range(waveform.size(0)): mute_size = int( self.random_uniform(0, end=int(t_steps * self.max_random_mute_portion)) ) mute_start = int(self.random_uniform(0, t_steps - mute_size)) waveform[i, mute_start : mute_start + mute_size] = 0 return waveform
def cos_similarity(self, waveform, text): # waveform: [bs, t_steps] with torch.no_grad(): self.embed_mode = "audio" print(text) audio_emb = self(waveform.cuda()) self.embed_mode = "text" text_emb = self(text) similarity = F.cosine_similarity(audio_emb, text_emb, dim=2) return similarity.squeeze()
def forward(self, batch, key=None): # If you want this conditioner to be unconditional, set self.unconditional_prob = 1.0 # If you want this conditioner to be fully conditional, set self.unconditional_prob = 0.0 if self.model.training == True and not self.training_mode: print( "The pretrained CLAP model should always be in eval mode. Reloading model just in case you change the parameters." ) self.model, self.model_cfg = create_model( self.amodel, self.tmodel, self.pretrained, precision=self.precision, device="cuda", enable_fusion=self.enable_fusion, fusion_type=self.fusion_type, ) for p in self.model.parameters(): p.requires_grad = False self.model.eval()
# the 'fusion' truncate mode can be changed to 'rand_trunc' if run in unfusion mode if self.embed_mode == "audio": with torch.no_grad(): audio_dict_list = [] assert ( self.sampling_rate == 16000 ), "We only support 16000 sampling rate" if self.random_mute: batch = self._random_mute(batch) # batch: [bs, 1, t-samples] batch = torchaudio.functional.resample( batch, orig_freq=self.sampling_rate, new_freq=48000 ) for waveform in self.batch_to_list(batch): audio_dict = {} audio_dict = get_audio_features( audio_dict, waveform, 480000, data_truncating="fusion", data_filling="repeatpad", audio_cfg=self.model_cfg["audio_cfg"], ) audio_dict_list.append(audio_dict) # [bs, 512] embed = self.model.get_audio_embedding(audio_dict_list) elif self.embed_mode == "text": with torch.no_grad(): # the 'fusion' truncate mode can be changed to 'rand_trunc' if run in unfusion mode text_data = self.tokenizer(batch) embed = self.model.get_text_embedding(text_data)
embed = embed.unsqueeze(1) self.unconditional_token = self.model.get_text_embedding( self.tokenizer(["", ""]) )[0:1]
for i in range(embed.size(0)): if self.make_decision(self.unconditional_prob): embed[i] = self.unconditional_token
# [bs, 1, 512] return embed.detach()
def tokenizer(self, text): result = self.tokenize( text, padding="max_length", truncation=True, max_length=512, return_tensors="pt", ) return {k: v.squeeze(0) for k, v in result.items()}12345678910111213141516171819202122232425from .factory import ( list_models, create_model, create_model_and_transforms, add_model_config,)from .loss import ClipLoss, gather_features, LPLoss, lp_gather_features, LPMetricsfrom .model import ( CLAP, CLAPTextCfg, CLAPVisionCfg, CLAPAudioCfp, convert_weights_to_fp16, trace_model,)from .openai import load_openai_model, list_openai_modelsfrom .pretrained import ( list_pretrained, list_pretrained_tag_models, list_pretrained_model_tags, get_pretrained_url, download_pretrained,)from .tokenizer import SimpleTokenizer, tokenizefrom .transform import image_transform12345678910111213141516171819202122232425262728293031323334353637383940from transformers import BertTokenizer, BertModel
tokenizer = BertTokenizer.from_pretrained("bert-base-uncased")model = BertModel.from_pretrained("bert-base-uncased")text = "Replace me by any text you'd like."
def bert_embeddings(text): # text = "Replace me by any text you'd like." encoded_input = tokenizer(text, return_tensors="pt") output = model(**encoded_input) return output
from transformers import RobertaTokenizer, RobertaModel
tokenizer = RobertaTokenizer.from_pretrained("roberta-base")model = RobertaModel.from_pretrained("roberta-base")text = "Replace me by any text you'd like."
def Roberta_embeddings(text): # text = "Replace me by any text you'd like." encoded_input = tokenizer(text, return_tensors="pt") output = model(**encoded_input) return output
from transformers import BartTokenizer, BartModel
tokenizer = BartTokenizer.from_pretrained("facebook/bart-base")model = BartModel.from_pretrained("facebook/bart-base")text = "Replace me by any text you'd like."
def bart_embeddings(text): # text = "Replace me by any text you'd like." encoded_input = tokenizer(text, return_tensors="pt") output = model(**encoded_input) return output123version https://git-lfs.github.com/spec/v1oid sha256:924691ac288e54409236115652ad4aa250f48203de50a9e4722a6ecd48d6804asize 1356917123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279import jsonimport loggingimport osimport pathlibimport refrom copy import deepcopyfrom pathlib import Path
import torch
from .model import CLAP, convert_weights_to_fp16from .openai import load_openai_modelfrom .pretrained import get_pretrained_url, download_pretrainedfrom .transform import image_transform
_MODEL_CONFIG_PATHS = [Path(__file__).parent / f"model_configs/"]_MODEL_CONFIGS = {} # directory (model_name: config) of model architecture configsCACHE_DIR = os.getenv("AUDIOLDM_CACHE_DIR", "~/.cache/audioldm")
def _natural_key(string_): return [int(s) if s.isdigit() else s for s in re.split(r"(\d+)", string_.lower())]
def _rescan_model_configs(): global _MODEL_CONFIGS
config_ext = (".json",) config_files = [] for config_path in _MODEL_CONFIG_PATHS: if config_path.is_file() and config_path.suffix in config_ext: config_files.append(config_path) elif config_path.is_dir(): for ext in config_ext: config_files.extend(config_path.glob(f"*{ext}"))
for cf in config_files: if os.path.basename(cf)[0] == ".": continue # Ignore hidden files
with open(cf, "r") as f: model_cfg = json.load(f) if all(a in model_cfg for a in ("embed_dim", "audio_cfg", "text_cfg")): _MODEL_CONFIGS[cf.stem] = model_cfg
_MODEL_CONFIGS = { k: v for k, v in sorted(_MODEL_CONFIGS.items(), key=lambda x: _natural_key(x[0])) }
_rescan_model_configs() # initial populate of model config registry
def load_state_dict(checkpoint_path: str, map_location="cpu", skip_params=True): checkpoint = torch.load(checkpoint_path, map_location=map_location) if isinstance(checkpoint, dict) and "state_dict" in checkpoint: state_dict = checkpoint["state_dict"] else: state_dict = checkpoint if skip_params: if next(iter(state_dict.items()))[0].startswith("module"): state_dict = {k[7:]: v for k, v in state_dict.items()} # for k in state_dict: # if k.startswith('transformer'): # v = state_dict.pop(k) # state_dict['text_branch.' + k[12:]] = v return state_dict
def create_model( amodel_name: str, tmodel_name: str, pretrained: str = "", precision: str = "fp32", device: torch.device = torch.device("cpu"), jit: bool = False, force_quick_gelu: bool = False, openai_model_cache_dir: str = os.path.expanduser(f"{CACHE_DIR}/clip"), skip_params=True, pretrained_audio: str = "", pretrained_text: str = "", enable_fusion: bool = False, fusion_type: str = "None" # pretrained_image: bool = False,): amodel_name = amodel_name.replace( "/", "-" ) # for callers using old naming with / in ViT names pretrained_orig = pretrained pretrained = pretrained.lower() if pretrained == "openai": if amodel_name in _MODEL_CONFIGS: logging.info(f"Loading {amodel_name} model config.") model_cfg = deepcopy(_MODEL_CONFIGS[amodel_name]) else: logging.error( f"Model config for {amodel_name} not found; available models {list_models()}." ) raise RuntimeError(f"Model config for {amodel_name} not found.")
logging.info(f"Loading pretrained ViT-B-16 text encoder from OpenAI.") # Hard Code in model name model_cfg["text_cfg"]["model_type"] = tmodel_name model = load_openai_model( "ViT-B-16", model_cfg, device=device, jit=jit, cache_dir=openai_model_cache_dir, enable_fusion=enable_fusion, fusion_type=fusion_type, ) # See https://discuss.pytorch.org/t/valueerror-attemting-to-unscale-fp16-gradients/81372 if precision == "amp" or precision == "fp32": model = model.float() else: if amodel_name in _MODEL_CONFIGS: logging.info(f"Loading {amodel_name} model config.") model_cfg = deepcopy(_MODEL_CONFIGS[amodel_name]) else: logging.error( f"Model config for {amodel_name} not found; available models {list_models()}." ) raise RuntimeError(f"Model config for {amodel_name} not found.")
if force_quick_gelu: # override for use of QuickGELU on non-OpenAI transformer models model_cfg["quick_gelu"] = True
# if pretrained_image: # if 'timm_amodel_name' in model_cfg.get('vision_cfg', {}): # # pretrained weight loading for timm models set via vision_cfg # model_cfg['vision_cfg']['timm_model_pretrained'] = True # else: # assert False, 'pretrained image towers currently only supported for timm models' model_cfg["text_cfg"]["model_type"] = tmodel_name model_cfg["enable_fusion"] = enable_fusion model_cfg["fusion_type"] = fusion_type model = CLAP(**model_cfg)
if pretrained: checkpoint_path = "" url = get_pretrained_url(amodel_name, pretrained) if url: checkpoint_path = download_pretrained(url, root=openai_model_cache_dir) elif os.path.exists(pretrained_orig): checkpoint_path = pretrained_orig if checkpoint_path: logging.info( f"Loading pretrained {amodel_name}-{tmodel_name} weights ({pretrained})." ) ckpt = load_state_dict(checkpoint_path, skip_params=True) model.load_state_dict(ckpt) param_names = [n for n, p in model.named_parameters()] # for n in param_names: # print(n, "\t", "Loaded" if n in ckpt else "Unloaded") else: logging.warning( f"Pretrained weights ({pretrained}) not found for model {amodel_name}." ) raise RuntimeError( f"Pretrained weights ({pretrained}) not found for model {amodel_name}." )
if pretrained_audio: if amodel_name.startswith("PANN"): if "Cnn14_mAP" in pretrained_audio: # official checkpoint audio_ckpt = torch.load(pretrained_audio, map_location="cpu") audio_ckpt = audio_ckpt["model"] keys = list(audio_ckpt.keys()) for key in keys: if ( "spectrogram_extractor" not in key and "logmel_extractor" not in key ): v = audio_ckpt.pop(key) audio_ckpt["audio_branch." + key] = v elif os.path.basename(pretrained_audio).startswith( "PANN" ): # checkpoint trained via HTSAT codebase audio_ckpt = torch.load(pretrained_audio, map_location="cpu") audio_ckpt = audio_ckpt["state_dict"] keys = list(audio_ckpt.keys()) for key in keys: if key.startswith("sed_model"): v = audio_ckpt.pop(key) audio_ckpt["audio_branch." + key[10:]] = v elif os.path.basename(pretrained_audio).startswith( "finetuned" ): # checkpoint trained via linear probe codebase audio_ckpt = torch.load(pretrained_audio, map_location="cpu") else: raise ValueError("Unknown audio checkpoint") elif amodel_name.startswith("HTSAT"): if "HTSAT_AudioSet_Saved" in pretrained_audio: # official checkpoint audio_ckpt = torch.load(pretrained_audio, map_location="cpu") audio_ckpt = audio_ckpt["state_dict"] keys = list(audio_ckpt.keys()) for key in keys: if key.startswith("sed_model") and ( "spectrogram_extractor" not in key and "logmel_extractor" not in key ): v = audio_ckpt.pop(key) audio_ckpt["audio_branch." + key[10:]] = v elif os.path.basename(pretrained_audio).startswith( "HTSAT" ): # checkpoint trained via HTSAT codebase audio_ckpt = torch.load(pretrained_audio, map_location="cpu") audio_ckpt = audio_ckpt["state_dict"] keys = list(audio_ckpt.keys()) for key in keys: if key.startswith("sed_model"): v = audio_ckpt.pop(key) audio_ckpt["audio_branch." + key[10:]] = v elif os.path.basename(pretrained_audio).startswith( "finetuned" ): # checkpoint trained via linear probe codebase audio_ckpt = torch.load(pretrained_audio, map_location="cpu") else: raise ValueError("Unknown audio checkpoint") else: raise f"this audio encoder pretrained checkpoint is not support"
model.load_state_dict(audio_ckpt, strict=False) logging.info( f"Loading pretrained {amodel_name} weights ({pretrained_audio})." ) param_names = [n for n, p in model.named_parameters()] for n in param_names: print(n, "\t", "Loaded" if n in audio_ckpt else "Unloaded")
model.to(device=device) if precision == "fp16": assert device.type != "cpu" convert_weights_to_fp16(model)
if jit: model = torch.jit.script(model)
return model, model_cfg
def create_model_and_transforms( model_name: str, pretrained: str = "", precision: str = "fp32", device: torch.device = torch.device("cpu"), jit: bool = False, force_quick_gelu: bool = False, # pretrained_image: bool = False,): model = create_model( model_name, pretrained, precision, device, jit, force_quick_gelu=force_quick_gelu, # pretrained_image=pretrained_image ) preprocess_train = image_transform(model.visual.image_size, is_train=True) preprocess_val = image_transform(model.visual.image_size, is_train=False) return model, preprocess_train, preprocess_val
def list_models(): """enumerate available model architectures based on config files""" return list(_MODEL_CONFIGS.keys())
def add_model_config(path): """add model config path or file and update registry""" if not isinstance(path, Path): path = Path(path) _MODEL_CONFIG_PATHS.append(path) _rescan_model_configs()123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192"""Feature Fusion for Varible-Length Data ProcessingAFF/iAFF is referred and modified from https://github.com/YimianDai/open-aff/blob/master/aff_pytorch/aff_net/fusion.pyAccording to the paper: Yimian Dai et al, Attentional Feature Fusion, IEEE Winter Conference on Applications of Computer Vision, WACV 2021"""
import torchimport torch.nn as nn
class DAF(nn.Module): """ 直接相加 DirectAddFuse """
def __init__(self): super(DAF, self).__init__()
def forward(self, x, residual): return x + residual
class iAFF(nn.Module): """ 多特征融合 iAFF """
def __init__(self, channels=64, r=4, type="2D"): super(iAFF, self).__init__() inter_channels = int(channels // r)
if type == "1D": # 本地注意力 self.local_att = nn.Sequential( nn.Conv1d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(inter_channels), nn.ReLU(inplace=True), nn.Conv1d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(channels), )
# 全局注意力 self.global_att = nn.Sequential( nn.AdaptiveAvgPool1d(1), nn.Conv1d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(inter_channels), nn.ReLU(inplace=True), nn.Conv1d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(channels), )
# 第二次本地注意力 self.local_att2 = nn.Sequential( nn.Conv1d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(inter_channels), nn.ReLU(inplace=True), nn.Conv1d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(channels), ) # 第二次全局注意力 self.global_att2 = nn.Sequential( nn.AdaptiveAvgPool1d(1), nn.Conv1d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(inter_channels), nn.ReLU(inplace=True), nn.Conv1d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(channels), ) elif type == "2D": # 本地注意力 self.local_att = nn.Sequential( nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(inter_channels), nn.ReLU(inplace=True), nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(channels), )
# 全局注意力 self.global_att = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(inter_channels), nn.ReLU(inplace=True), nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(channels), )
# 第二次本地注意力 self.local_att2 = nn.Sequential( nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(inter_channels), nn.ReLU(inplace=True), nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(channels), ) # 第二次全局注意力 self.global_att2 = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(inter_channels), nn.ReLU(inplace=True), nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(channels), ) else: raise f"the type is not supported"
self.sigmoid = nn.Sigmoid()
def forward(self, x, residual): flag = False xa = x + residual if xa.size(0) == 1: xa = torch.cat([xa, xa], dim=0) flag = True xl = self.local_att(xa) xg = self.global_att(xa) xlg = xl + xg wei = self.sigmoid(xlg) xi = x * wei + residual * (1 - wei)
xl2 = self.local_att2(xi) xg2 = self.global_att(xi) xlg2 = xl2 + xg2 wei2 = self.sigmoid(xlg2) xo = x * wei2 + residual * (1 - wei2) if flag: xo = xo[0].unsqueeze(0) return xo
class AFF(nn.Module): """ 多特征融合 AFF """
def __init__(self, channels=64, r=4, type="2D"): super(AFF, self).__init__() inter_channels = int(channels // r)
if type == "1D": self.local_att = nn.Sequential( nn.Conv1d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(inter_channels), nn.ReLU(inplace=True), nn.Conv1d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(channels), ) self.global_att = nn.Sequential( nn.AdaptiveAvgPool1d(1), nn.Conv1d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(inter_channels), nn.ReLU(inplace=True), nn.Conv1d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm1d(channels), ) elif type == "2D": self.local_att = nn.Sequential( nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(inter_channels), nn.ReLU(inplace=True), nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(channels), ) self.global_att = nn.Sequential( nn.AdaptiveAvgPool2d(1), nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(inter_channels), nn.ReLU(inplace=True), nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0), nn.BatchNorm2d(channels), ) else: raise f"the type is not supported."
self.sigmoid = nn.Sigmoid()
def forward(self, x, residual): flag = False xa = x + residual if xa.size(0) == 1: xa = torch.cat([xa, xa], dim=0) flag = True xl = self.local_att(xa) xg = self.global_att(xa) xlg = xl + xg wei = self.sigmoid(xlg) xo = 2 * x * wei + 2 * residual * (1 - wei) if flag: xo = xo[0].unsqueeze(0) return xo123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308# Ke Chen# knutchen@ucsd.edu# HTS-AT: A HIERARCHICAL TOKEN-SEMANTIC AUDIO TRANSFORMER FOR SOUND CLASSIFICATION AND DETECTION# Some layers designed on the model# below codes are based and referred from https://github.com/microsoft/Swin-Transformer# Swin Transformer for Computer Vision: https://arxiv.org/pdf/2103.14030.pdf
import torchimport torch.nn as nnimport torch.nn.functional as Ffrom itertools import repeatimport collections.abcimport mathimport warnings
from torch.nn.init import _calculate_fan_in_and_fan_outimport torch.utils.checkpoint as checkpoint
import random
from torchlibrosa.stft import Spectrogram, LogmelFilterBankfrom torchlibrosa.augmentation import SpecAugmentation
from itertools import repeatfrom .utils import do_mixup, interpolate
from .feature_fusion import iAFF, AFF, DAF
# from PyTorch internalsdef _ntuple(n): def parse(x): if isinstance(x, collections.abc.Iterable): return x return tuple(repeat(x, n))
return parse
to_1tuple = _ntuple(1)to_2tuple = _ntuple(2)to_3tuple = _ntuple(3)to_4tuple = _ntuple(4)to_ntuple = _ntuple
def drop_path(x, drop_prob: float = 0.0, training: bool = False): """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks). This is the same as the DropConnect impl I created for EfficientNet, etc networks, however, the original name is misleading as 'Drop Connect' is a different form of dropout in a separate paper... See discussion: https://github.com/tensorflow/tpu/issues/494#issuecomment-532968956 ... I've opted for changing the layer and argument names to 'drop path' rather than mix DropConnect as a layer name and use 'survival rate' as the argument. """ if drop_prob == 0.0 or not training: return x keep_prob = 1 - drop_prob shape = (x.shape[0],) + (1,) * ( x.ndim - 1 ) # work with diff dim tensors, not just 2D ConvNets random_tensor = keep_prob + torch.rand(shape, dtype=x.dtype, device=x.device) random_tensor.floor_() # binarize output = x.div(keep_prob) * random_tensor return output
class DropPath(nn.Module): """Drop paths (Stochastic Depth) per sample (when applied in main path of residual blocks)."""
def __init__(self, drop_prob=None): super(DropPath, self).__init__() self.drop_prob = drop_prob
def forward(self, x): return drop_path(x, self.drop_prob, self.training)
class PatchEmbed(nn.Module): """2D Image to Patch Embedding"""
def __init__( self, img_size=224, patch_size=16, in_chans=3, embed_dim=768, norm_layer=None, flatten=True, patch_stride=16, enable_fusion=False, fusion_type="None", ): super().__init__() img_size = to_2tuple(img_size) patch_size = to_2tuple(patch_size) patch_stride = to_2tuple(patch_stride) self.img_size = img_size self.patch_size = patch_size self.patch_stride = patch_stride self.grid_size = ( img_size[0] // patch_stride[0], img_size[1] // patch_stride[1], ) self.num_patches = self.grid_size[0] * self.grid_size[1] self.flatten = flatten self.in_chans = in_chans self.embed_dim = embed_dim
self.enable_fusion = enable_fusion self.fusion_type = fusion_type
padding = ( (patch_size[0] - patch_stride[0]) // 2, (patch_size[1] - patch_stride[1]) // 2, )
if (self.enable_fusion) and (self.fusion_type == "channel_map"): self.proj = nn.Conv2d( in_chans * 4, embed_dim, kernel_size=patch_size, stride=patch_stride, padding=padding, ) else: self.proj = nn.Conv2d( in_chans, embed_dim, kernel_size=patch_size, stride=patch_stride, padding=padding, ) self.norm = norm_layer(embed_dim) if norm_layer else nn.Identity()
if (self.enable_fusion) and ( self.fusion_type in ["daf_2d", "aff_2d", "iaff_2d"] ): self.mel_conv2d = nn.Conv2d( in_chans, embed_dim, kernel_size=(patch_size[0], patch_size[1] * 3), stride=(patch_stride[0], patch_stride[1] * 3), padding=padding, ) if self.fusion_type == "daf_2d": self.fusion_model = DAF() elif self.fusion_type == "aff_2d": self.fusion_model = AFF(channels=embed_dim, type="2D") elif self.fusion_type == "iaff_2d": self.fusion_model = iAFF(channels=embed_dim, type="2D")
def forward(self, x, longer_idx=None): if (self.enable_fusion) and ( self.fusion_type in ["daf_2d", "aff_2d", "iaff_2d"] ): global_x = x[:, 0:1, :, :]
# global processing B, C, H, W = global_x.shape assert ( H == self.img_size[0] and W == self.img_size[1] ), f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})." global_x = self.proj(global_x) TW = global_x.size(-1) if len(longer_idx) > 0: # local processing local_x = x[longer_idx, 1:, :, :].contiguous() B, C, H, W = local_x.shape local_x = local_x.view(B * C, 1, H, W) local_x = self.mel_conv2d(local_x) local_x = local_x.view( B, C, local_x.size(1), local_x.size(2), local_x.size(3) ) local_x = local_x.permute((0, 2, 3, 1, 4)).contiguous().flatten(3) TB, TC, TH, _ = local_x.size() if local_x.size(-1) < TW: local_x = torch.cat( [ local_x, torch.zeros( (TB, TC, TH, TW - local_x.size(-1)), device=global_x.device, ), ], dim=-1, ) else: local_x = local_x[:, :, :, :TW]
global_x[longer_idx] = self.fusion_model(global_x[longer_idx], local_x) x = global_x else: B, C, H, W = x.shape assert ( H == self.img_size[0] and W == self.img_size[1] ), f"Input image size ({H}*{W}) doesn't match model ({self.img_size[0]}*{self.img_size[1]})." x = self.proj(x)
if self.flatten: x = x.flatten(2).transpose(1, 2) # BCHW -> BNC x = self.norm(x) return x
class Mlp(nn.Module): """MLP as used in Vision Transformer, MLP-Mixer and related networks"""
def __init__( self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.0, ): super().__init__() out_features = out_features or in_features hidden_features = hidden_features or in_features self.fc1 = nn.Linear(in_features, hidden_features) self.act = act_layer() self.fc2 = nn.Linear(hidden_features, out_features) self.drop = nn.Dropout(drop)
def forward(self, x): x = self.fc1(x) x = self.act(x) x = self.drop(x) x = self.fc2(x) x = self.drop(x) return x
def _no_grad_trunc_normal_(tensor, mean, std, a, b): # Cut & paste from PyTorch official master until it's in a few official releases - RW # Method based on https://people.sc.fsu.edu/~jburkardt/presentations/truncated_normal.pdf def norm_cdf(x): # Computes standard normal cumulative distribution function return (1.0 + math.erf(x / math.sqrt(2.0))) / 2.0
if (mean < a - 2 * std) or (mean > b + 2 * std): warnings.warn( "mean is more than 2 std from [a, b] in nn.init.trunc_normal_. " "The distribution of values may be incorrect.", stacklevel=2, )
with torch.no_grad(): # Values are generated by using a truncated uniform distribution and # then using the inverse CDF for the normal distribution. # Get upper and lower cdf values l = norm_cdf((a - mean) / std) u = norm_cdf((b - mean) / std)
# Uniformly fill tensor with values from [l, u], then translate to # [2l-1, 2u-1]. tensor.uniform_(2 * l - 1, 2 * u - 1)
# Use inverse cdf transform for normal distribution to get truncated # standard normal tensor.erfinv_()
# Transform to proper mean, std tensor.mul_(std * math.sqrt(2.0)) tensor.add_(mean)
# Clamp to ensure it's in the proper range tensor.clamp_(min=a, max=b) return tensor
def trunc_normal_(tensor, mean=0.0, std=1.0, a=-2.0, b=2.0): # type: (Tensor, float, float, float, float) -> Tensor r"""Fills the input Tensor with values drawn from a truncated normal distribution. The values are effectively drawn from the normal distribution :math:`\mathcal{N}(\text{mean}, \text{std}^2)` with values outside :math:`[a, b]` redrawn until they are within the bounds. The method used for generating the random values works best when :math:`a \leq \text{mean} \leq b`. Args: tensor: an n-dimensional `torch.Tensor` mean: the mean of the normal distribution std: the standard deviation of the normal distribution a: the minimum cutoff value b: the maximum cutoff value Examples: >>> w = torch.empty(3, 5) >>> nn.init.trunc_normal_(w) """ return _no_grad_trunc_normal_(tensor, mean, std, a, b)
def variance_scaling_(tensor, scale=1.0, mode="fan_in", distribution="normal"): fan_in, fan_out = _calculate_fan_in_and_fan_out(tensor) if mode == "fan_in": denom = fan_in elif mode == "fan_out": denom = fan_out elif mode == "fan_avg": denom = (fan_in + fan_out) / 2
variance = scale / denom
if distribution == "truncated_normal": # constant is stddev of standard normal truncated to (-2, 2) trunc_normal_(tensor, std=math.sqrt(variance) / 0.87962566103423978) elif distribution == "normal": tensor.normal_(std=math.sqrt(variance)) elif distribution == "uniform": bound = math.sqrt(3 * variance) tensor.uniform_(-bound, bound) else: raise ValueError(f"invalid distribution {distribution}")
def lecun_normal_(tensor): variance_scaling_(tensor, mode="fan_in", distribution="truncated_normal")
def window_partition(x, window_size): """ Args: x: (B, H, W, C) window_size (int): window size Returns: windows: (num_windows*B, window_size, window_size, C) """ B, H, W, C = x.shape x = x.view(B, H // window_size, window_size, W // window_size, window_size, C) windows = ( x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size, window_size, C) ) return windows
def window_reverse(windows, window_size, H, W): """ Args: windows: (num_windows*B, window_size, window_size, C) window_size (int): Window size H (int): Height of image W (int): Width of image Returns: x: (B, H, W, C) """ B = int(windows.shape[0] / (H * W / window_size / window_size)) x = windows.view( B, H // window_size, W // window_size, window_size, window_size, -1 ) x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1) return x
class WindowAttention(nn.Module): r"""Window based multi-head self attention (W-MSA) module with relative position bias. It supports both of shifted and non-shifted window. Args: dim (int): Number of input channels. window_size (tuple[int]): The height and width of the window. num_heads (int): Number of attention heads. qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set attn_drop (float, optional): Dropout ratio of attention weight. Default: 0.0 proj_drop (float, optional): Dropout ratio of output. Default: 0.0 """
def __init__( self, dim, window_size, num_heads, qkv_bias=True, qk_scale=None, attn_drop=0.0, proj_drop=0.0, ):
super().__init__() self.dim = dim self.window_size = window_size # Wh, Ww self.num_heads = num_heads head_dim = dim // num_heads self.scale = qk_scale or head_dim**-0.5
# define a parameter table of relative position bias self.relative_position_bias_table = nn.Parameter( torch.zeros((2 * window_size[0] - 1) * (2 * window_size[1] - 1), num_heads) ) # 2*Wh-1 * 2*Ww-1, nH
# get pair-wise relative position index for each token inside the window coords_h = torch.arange(self.window_size[0]) coords_w = torch.arange(self.window_size[1]) coords = torch.stack(torch.meshgrid([coords_h, coords_w])) # 2, Wh, Ww coords_flatten = torch.flatten(coords, 1) # 2, Wh*Ww relative_coords = ( coords_flatten[:, :, None] - coords_flatten[:, None, :] ) # 2, Wh*Ww, Wh*Ww relative_coords = relative_coords.permute( 1, 2, 0 ).contiguous() # Wh*Ww, Wh*Ww, 2 relative_coords[:, :, 0] += self.window_size[0] - 1 # shift to start from 0 relative_coords[:, :, 1] += self.window_size[1] - 1 relative_coords[:, :, 0] *= 2 * self.window_size[1] - 1 relative_position_index = relative_coords.sum(-1) # Wh*Ww, Wh*Ww self.register_buffer("relative_position_index", relative_position_index)
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias) self.attn_drop = nn.Dropout(attn_drop) self.proj = nn.Linear(dim, dim) self.proj_drop = nn.Dropout(proj_drop)
trunc_normal_(self.relative_position_bias_table, std=0.02) self.softmax = nn.Softmax(dim=-1)
def forward(self, x, mask=None): """ Args: x: input features with shape of (num_windows*B, N, C) mask: (0/-inf) mask with shape of (num_windows, Wh*Ww, Wh*Ww) or None """ B_, N, C = x.shape qkv = ( self.qkv(x) .reshape(B_, N, 3, self.num_heads, C // self.num_heads) .permute(2, 0, 3, 1, 4) ) q, k, v = ( qkv[0], qkv[1], qkv[2], ) # make torchscript happy (cannot use tensor as tuple)
q = q * self.scale attn = q @ k.transpose(-2, -1)
relative_position_bias = self.relative_position_bias_table[ self.relative_position_index.view(-1) ].view( self.window_size[0] * self.window_size[1], self.window_size[0] * self.window_size[1], -1, ) # Wh*Ww,Wh*Ww,nH relative_position_bias = relative_position_bias.permute( 2, 0, 1 ).contiguous() # nH, Wh*Ww, Wh*Ww attn = attn + relative_position_bias.unsqueeze(0)
if mask is not None: nW = mask.shape[0] attn = attn.view(B_ // nW, nW, self.num_heads, N, N) + mask.unsqueeze( 1 ).unsqueeze(0) attn = attn.view(-1, self.num_heads, N, N) attn = self.softmax(attn) else: attn = self.softmax(attn)
attn = self.attn_drop(attn)
x = (attn @ v).transpose(1, 2).reshape(B_, N, C) x = self.proj(x) x = self.proj_drop(x) return x, attn
def extra_repr(self): return f"dim={self.dim}, window_size={self.window_size}, num_heads={self.num_heads}"
# We use the model based on Swintransformer Block, therefore we can use the swin-transformer pretrained modelclass SwinTransformerBlock(nn.Module): r"""Swin Transformer Block. Args: dim (int): Number of input channels. input_resolution (tuple[int]): Input resulotion. num_heads (int): Number of attention heads. window_size (int): Window size. shift_size (int): Shift size for SW-MSA. mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set. drop (float, optional): Dropout rate. Default: 0.0 attn_drop (float, optional): Attention dropout rate. Default: 0.0 drop_path (float, optional): Stochastic depth rate. Default: 0.0 act_layer (nn.Module, optional): Activation layer. Default: nn.GELU norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm """
def __init__( self, dim, input_resolution, num_heads, window_size=7, shift_size=0, mlp_ratio=4.0, qkv_bias=True, qk_scale=None, drop=0.0, attn_drop=0.0, drop_path=0.0, act_layer=nn.GELU, norm_layer=nn.LayerNorm, norm_before_mlp="ln", ): super().__init__() self.dim = dim self.input_resolution = input_resolution self.num_heads = num_heads self.window_size = window_size self.shift_size = shift_size self.mlp_ratio = mlp_ratio self.norm_before_mlp = norm_before_mlp if min(self.input_resolution) <= self.window_size: # if window size is larger than input resolution, we don't partition windows self.shift_size = 0 self.window_size = min(self.input_resolution) assert ( 0 <= self.shift_size < self.window_size ), "shift_size must in 0-window_size"
self.norm1 = norm_layer(dim) self.attn = WindowAttention( dim, window_size=to_2tuple(self.window_size), num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop, )
self.drop_path = DropPath(drop_path) if drop_path > 0.0 else nn.Identity() if self.norm_before_mlp == "ln": self.norm2 = nn.LayerNorm(dim) elif self.norm_before_mlp == "bn": self.norm2 = lambda x: nn.BatchNorm1d(dim)(x.transpose(1, 2)).transpose( 1, 2 ) else: raise NotImplementedError mlp_hidden_dim = int(dim * mlp_ratio) self.mlp = Mlp( in_features=dim, hidden_features=mlp_hidden_dim, act_layer=act_layer, drop=drop, )
if self.shift_size > 0: # calculate attention mask for SW-MSA H, W = self.input_resolution img_mask = torch.zeros((1, H, W, 1)) # 1 H W 1 h_slices = ( slice(0, -self.window_size), slice(-self.window_size, -self.shift_size), slice(-self.shift_size, None), ) w_slices = ( slice(0, -self.window_size), slice(-self.window_size, -self.shift_size), slice(-self.shift_size, None), ) cnt = 0 for h in h_slices: for w in w_slices: img_mask[:, h, w, :] = cnt cnt += 1
mask_windows = window_partition( img_mask, self.window_size ) # nW, window_size, window_size, 1 mask_windows = mask_windows.view(-1, self.window_size * self.window_size) attn_mask = mask_windows.unsqueeze(1) - mask_windows.unsqueeze(2) attn_mask = attn_mask.masked_fill( attn_mask != 0, float(-100.0) ).masked_fill(attn_mask == 0, float(0.0)) else: attn_mask = None
self.register_buffer("attn_mask", attn_mask)
def forward(self, x): # pdb.set_trace() H, W = self.input_resolution # print("H: ", H) # print("W: ", W) # pdb.set_trace() B, L, C = x.shape # assert L == H * W, "input feature has wrong size"
shortcut = x x = self.norm1(x) x = x.view(B, H, W, C)
# cyclic shift if self.shift_size > 0: shifted_x = torch.roll( x, shifts=(-self.shift_size, -self.shift_size), dims=(1, 2) ) else: shifted_x = x
# partition windows x_windows = window_partition( shifted_x, self.window_size ) # nW*B, window_size, window_size, C x_windows = x_windows.view( -1, self.window_size * self.window_size, C ) # nW*B, window_size*window_size, C
# W-MSA/SW-MSA attn_windows, attn = self.attn( x_windows, mask=self.attn_mask ) # nW*B, window_size*window_size, C
# merge windows attn_windows = attn_windows.view(-1, self.window_size, self.window_size, C) shifted_x = window_reverse(attn_windows, self.window_size, H, W) # B H' W' C
# reverse cyclic shift if self.shift_size > 0: x = torch.roll( shifted_x, shifts=(self.shift_size, self.shift_size), dims=(1, 2) ) else: x = shifted_x x = x.view(B, H * W, C)
# FFN x = shortcut + self.drop_path(x) x = x + self.drop_path(self.mlp(self.norm2(x)))
return x, attn
def extra_repr(self): return ( f"dim={self.dim}, input_resolution={self.input_resolution}, num_heads={self.num_heads}, " f"window_size={self.window_size}, shift_size={self.shift_size}, mlp_ratio={self.mlp_ratio}" )
class PatchMerging(nn.Module): r"""Patch Merging Layer. Args: input_resolution (tuple[int]): Resolution of input feature. dim (int): Number of input channels. norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm """
def __init__(self, input_resolution, dim, norm_layer=nn.LayerNorm): super().__init__() self.input_resolution = input_resolution self.dim = dim self.reduction = nn.Linear(4 * dim, 2 * dim, bias=False) self.norm = norm_layer(4 * dim)
def forward(self, x): """ x: B, H*W, C """ H, W = self.input_resolution B, L, C = x.shape assert L == H * W, "input feature has wrong size" assert H % 2 == 0 and W % 2 == 0, f"x size ({H}*{W}) are not even."
x = x.view(B, H, W, C)
x0 = x[:, 0::2, 0::2, :] # B H/2 W/2 C x1 = x[:, 1::2, 0::2, :] # B H/2 W/2 C x2 = x[:, 0::2, 1::2, :] # B H/2 W/2 C x3 = x[:, 1::2, 1::2, :] # B H/2 W/2 C x = torch.cat([x0, x1, x2, x3], -1) # B H/2 W/2 4*C x = x.view(B, -1, 4 * C) # B H/2*W/2 4*C
x = self.norm(x) x = self.reduction(x)
return x
def extra_repr(self): return f"input_resolution={self.input_resolution}, dim={self.dim}"
class BasicLayer(nn.Module): """A basic Swin Transformer layer for one stage. Args: dim (int): Number of input channels. input_resolution (tuple[int]): Input resolution. depth (int): Number of blocks. num_heads (int): Number of attention heads. window_size (int): Local window size. mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. qkv_bias (bool, optional): If True, add a learnable bias to query, key, value. Default: True qk_scale (float | None, optional): Override default qk scale of head_dim ** -0.5 if set. drop (float, optional): Dropout rate. Default: 0.0 attn_drop (float, optional): Attention dropout rate. Default: 0.0 drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0 norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False. """
def __init__( self, dim, input_resolution, depth, num_heads, window_size, mlp_ratio=4.0, qkv_bias=True, qk_scale=None, drop=0.0, attn_drop=0.0, drop_path=0.0, norm_layer=nn.LayerNorm, downsample=None, use_checkpoint=False, norm_before_mlp="ln", ):
super().__init__() self.dim = dim self.input_resolution = input_resolution self.depth = depth self.use_checkpoint = use_checkpoint
# build blocks self.blocks = nn.ModuleList( [ SwinTransformerBlock( dim=dim, input_resolution=input_resolution, num_heads=num_heads, window_size=window_size, shift_size=0 if (i % 2 == 0) else window_size // 2, mlp_ratio=mlp_ratio, qkv_bias=qkv_bias, qk_scale=qk_scale, drop=drop, attn_drop=attn_drop, drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path, norm_layer=norm_layer, norm_before_mlp=norm_before_mlp, ) for i in range(depth) ] )
# patch merging layer if downsample is not None: self.downsample = downsample( input_resolution, dim=dim, norm_layer=norm_layer ) else: self.downsample = None
def forward(self, x): attns = [] for blk in self.blocks: if self.use_checkpoint: x = checkpoint.checkpoint(blk, x) else: x, attn = blk(x) if not self.training: attns.append(attn.unsqueeze(0)) if self.downsample is not None: x = self.downsample(x) if not self.training: attn = torch.cat(attns, dim=0) attn = torch.mean(attn, dim=0) return x, attn
def extra_repr(self): return f"dim={self.dim}, input_resolution={self.input_resolution}, depth={self.depth}"
# The Core of HTSATclass HTSAT_Swin_Transformer(nn.Module): r"""HTSAT based on the Swin Transformer Args: spec_size (int | tuple(int)): Input Spectrogram size. Default 256 patch_size (int | tuple(int)): Patch size. Default: 4 path_stride (iot | tuple(int)): Patch Stride for Frequency and Time Axis. Default: 4 in_chans (int): Number of input image channels. Default: 1 (mono) num_classes (int): Number of classes for classification head. Default: 527 embed_dim (int): Patch embedding dimension. Default: 96 depths (tuple(int)): Depth of each HTSAT-Swin Transformer layer. num_heads (tuple(int)): Number of attention heads in different layers. window_size (int): Window size. Default: 8 mlp_ratio (float): Ratio of mlp hidden dim to embedding dim. Default: 4 qkv_bias (bool): If True, add a learnable bias to query, key, value. Default: True qk_scale (float): Override default qk scale of head_dim ** -0.5 if set. Default: None drop_rate (float): Dropout rate. Default: 0 attn_drop_rate (float): Attention dropout rate. Default: 0 drop_path_rate (float): Stochastic depth rate. Default: 0.1 norm_layer (nn.Module): Normalization layer. Default: nn.LayerNorm. ape (bool): If True, add absolute position embedding to the patch embedding. Default: False patch_norm (bool): If True, add normalization after patch embedding. Default: True use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False config (module): The configuration Module from config.py """
def __init__( self, spec_size=256, patch_size=4, patch_stride=(4, 4), in_chans=1, num_classes=527, embed_dim=96, depths=[2, 2, 6, 2], num_heads=[4, 8, 16, 32], window_size=8, mlp_ratio=4.0, qkv_bias=True, qk_scale=None, drop_rate=0.0, attn_drop_rate=0.0, drop_path_rate=0.1, norm_layer=nn.LayerNorm, ape=False, patch_norm=True, use_checkpoint=False, norm_before_mlp="ln", config=None, enable_fusion=False, fusion_type="None", **kwargs, ): super(HTSAT_Swin_Transformer, self).__init__()
self.config = config self.spec_size = spec_size self.patch_stride = patch_stride self.patch_size = patch_size self.window_size = window_size self.embed_dim = embed_dim self.depths = depths self.ape = ape self.in_chans = in_chans self.num_classes = num_classes self.num_heads = num_heads self.num_layers = len(self.depths) self.num_features = int(self.embed_dim * 2 ** (self.num_layers - 1))
self.drop_rate = drop_rate self.attn_drop_rate = attn_drop_rate self.drop_path_rate = drop_path_rate
self.qkv_bias = qkv_bias self.qk_scale = None
self.patch_norm = patch_norm self.norm_layer = norm_layer if self.patch_norm else None self.norm_before_mlp = norm_before_mlp self.mlp_ratio = mlp_ratio
self.use_checkpoint = use_checkpoint
self.enable_fusion = enable_fusion self.fusion_type = fusion_type
# process mel-spec ; used only once self.freq_ratio = self.spec_size // self.config.mel_bins window = "hann" center = True pad_mode = "reflect" ref = 1.0 amin = 1e-10 top_db = None self.interpolate_ratio = 32 # Downsampled ratio # Spectrogram extractor self.spectrogram_extractor = Spectrogram( n_fft=config.window_size, hop_length=config.hop_size, win_length=config.window_size, window=window, center=center, pad_mode=pad_mode, freeze_parameters=True, ) # Logmel feature extractor self.logmel_extractor = LogmelFilterBank( sr=config.sample_rate, n_fft=config.window_size, n_mels=config.mel_bins, fmin=config.fmin, fmax=config.fmax, ref=ref, amin=amin, top_db=top_db, freeze_parameters=True, ) # Spec augmenter self.spec_augmenter = SpecAugmentation( time_drop_width=64, time_stripes_num=2, freq_drop_width=8, freq_stripes_num=2, ) # 2 2 self.bn0 = nn.BatchNorm2d(self.config.mel_bins)
# split spctrogram into non-overlapping patches self.patch_embed = PatchEmbed( img_size=self.spec_size, patch_size=self.patch_size, in_chans=self.in_chans, embed_dim=self.embed_dim, norm_layer=self.norm_layer, patch_stride=patch_stride, enable_fusion=self.enable_fusion, fusion_type=self.fusion_type, )
num_patches = self.patch_embed.num_patches patches_resolution = self.patch_embed.grid_size self.patches_resolution = patches_resolution
# absolute position embedding if self.ape: self.absolute_pos_embed = nn.Parameter( torch.zeros(1, num_patches, self.embed_dim) ) trunc_normal_(self.absolute_pos_embed, std=0.02)
self.pos_drop = nn.Dropout(p=self.drop_rate)
# stochastic depth dpr = [ x.item() for x in torch.linspace(0, self.drop_path_rate, sum(self.depths)) ] # stochastic depth decay rule
# build layers self.layers = nn.ModuleList() for i_layer in range(self.num_layers): layer = BasicLayer( dim=int(self.embed_dim * 2**i_layer), input_resolution=( patches_resolution[0] // (2**i_layer), patches_resolution[1] // (2**i_layer), ), depth=self.depths[i_layer], num_heads=self.num_heads[i_layer], window_size=self.window_size, mlp_ratio=self.mlp_ratio, qkv_bias=self.qkv_bias, qk_scale=self.qk_scale, drop=self.drop_rate, attn_drop=self.attn_drop_rate, drop_path=dpr[ sum(self.depths[:i_layer]) : sum(self.depths[: i_layer + 1]) ], norm_layer=self.norm_layer, downsample=PatchMerging if (i_layer < self.num_layers - 1) else None, use_checkpoint=use_checkpoint, norm_before_mlp=self.norm_before_mlp, ) self.layers.append(layer)
self.norm = self.norm_layer(self.num_features) self.avgpool = nn.AdaptiveAvgPool1d(1) self.maxpool = nn.AdaptiveMaxPool1d(1)
SF = ( self.spec_size // (2 ** (len(self.depths) - 1)) // self.patch_stride[0] // self.freq_ratio ) self.tscam_conv = nn.Conv2d( in_channels=self.num_features, out_channels=self.num_classes, kernel_size=(SF, 3), padding=(0, 1), ) self.head = nn.Linear(num_classes, num_classes)
if (self.enable_fusion) and ( self.fusion_type in ["daf_1d", "aff_1d", "iaff_1d"] ): self.mel_conv1d = nn.Sequential( nn.Conv1d(64, 64, kernel_size=5, stride=3, padding=2), nn.BatchNorm1d(64), ) if self.fusion_type == "daf_1d": self.fusion_model = DAF() elif self.fusion_type == "aff_1d": self.fusion_model = AFF(channels=64, type="1D") elif self.fusion_type == "iaff_1d": self.fusion_model = iAFF(channels=64, type="1D")
self.apply(self._init_weights)
def _init_weights(self, m): if isinstance(m, nn.Linear): trunc_normal_(m.weight, std=0.02) if isinstance(m, nn.Linear) and m.bias is not None: nn.init.constant_(m.bias, 0) elif isinstance(m, nn.LayerNorm): nn.init.constant_(m.bias, 0) nn.init.constant_(m.weight, 1.0)
@torch.jit.ignore def no_weight_decay(self): return {"absolute_pos_embed"}
@torch.jit.ignore def no_weight_decay_keywords(self): return {"relative_position_bias_table"}
def forward_features(self, x, longer_idx=None): # A deprecated optimization for using a hierarchical output from different blocks
frames_num = x.shape[2] x = self.patch_embed(x, longer_idx=longer_idx) if self.ape: x = x + self.absolute_pos_embed x = self.pos_drop(x) for i, layer in enumerate(self.layers): x, attn = layer(x) # for x x = self.norm(x) B, N, C = x.shape SF = frames_num // (2 ** (len(self.depths) - 1)) // self.patch_stride[0] ST = frames_num // (2 ** (len(self.depths) - 1)) // self.patch_stride[1] x = x.permute(0, 2, 1).contiguous().reshape(B, C, SF, ST) B, C, F, T = x.shape # group 2D CNN c_freq_bin = F // self.freq_ratio x = x.reshape(B, C, F // c_freq_bin, c_freq_bin, T) x = x.permute(0, 1, 3, 2, 4).contiguous().reshape(B, C, c_freq_bin, -1) # get latent_output fine_grained_latent_output = torch.mean(x, dim=2) fine_grained_latent_output = interpolate( fine_grained_latent_output.permute(0, 2, 1).contiguous(), 8 * self.patch_stride[1], )
latent_output = self.avgpool(torch.flatten(x, 2)) latent_output = torch.flatten(latent_output, 1)
# display the attention map, if needed
x = self.tscam_conv(x) x = torch.flatten(x, 2) # B, C, T
fpx = interpolate( torch.sigmoid(x).permute(0, 2, 1).contiguous(), 8 * self.patch_stride[1] )
x = self.avgpool(x) x = torch.flatten(x, 1)
output_dict = { "framewise_output": fpx, # already sigmoided "clipwise_output": torch.sigmoid(x), "fine_grained_embedding": fine_grained_latent_output, "embedding": latent_output, }
return output_dict
def crop_wav(self, x, crop_size, spe_pos=None): time_steps = x.shape[2] tx = torch.zeros(x.shape[0], x.shape[1], crop_size, x.shape[3]).to(x.device) for i in range(len(x)): if spe_pos is None: crop_pos = random.randint(0, time_steps - crop_size - 1) else: crop_pos = spe_pos tx[i][0] = x[i, 0, crop_pos : crop_pos + crop_size, :] return tx
# Reshape the wavform to a img size, if you want to use the pretrained swin transformer model def reshape_wav2img(self, x): B, C, T, F = x.shape target_T = int(self.spec_size * self.freq_ratio) target_F = self.spec_size // self.freq_ratio assert ( T <= target_T and F <= target_F ), "the wav size should less than or equal to the swin input size" # to avoid bicubic zero error if T < target_T: x = nn.functional.interpolate( x, (target_T, x.shape[3]), mode="bicubic", align_corners=True ) if F < target_F: x = nn.functional.interpolate( x, (x.shape[2], target_F), mode="bicubic", align_corners=True ) x = x.permute(0, 1, 3, 2).contiguous() x = x.reshape( x.shape[0], x.shape[1], x.shape[2], self.freq_ratio, x.shape[3] // self.freq_ratio, ) # print(x.shape) x = x.permute(0, 1, 3, 2, 4).contiguous() x = x.reshape(x.shape[0], x.shape[1], x.shape[2] * x.shape[3], x.shape[4]) return x
# Repeat the wavform to a img size, if you want to use the pretrained swin transformer model def repeat_wat2img(self, x, cur_pos): B, C, T, F = x.shape target_T = int(self.spec_size * self.freq_ratio) target_F = self.spec_size // self.freq_ratio assert ( T <= target_T and F <= target_F ), "the wav size should less than or equal to the swin input size" # to avoid bicubic zero error if T < target_T: x = nn.functional.interpolate( x, (target_T, x.shape[3]), mode="bicubic", align_corners=True ) if F < target_F: x = nn.functional.interpolate( x, (x.shape[2], target_F), mode="bicubic", align_corners=True ) x = x.permute(0, 1, 3, 2).contiguous() # B C F T x = x[:, :, :, cur_pos : cur_pos + self.spec_size] x = x.repeat(repeats=(1, 1, 4, 1)) return x
def forward( self, x: torch.Tensor, mixup_lambda=None, infer_mode=False, device=None ): # out_feat_keys: List[str] = None):
if self.enable_fusion and x["longer"].sum() == 0: # if no audio is longer than 10s, then randomly select one audio to be longer x["longer"][torch.randint(0, x["longer"].shape[0], (1,))] = True
if not self.enable_fusion: x = x["waveform"].to(device=device, non_blocking=True) x = self.spectrogram_extractor(x) # (batch_size, 1, time_steps, freq_bins) x = self.logmel_extractor(x) # (batch_size, 1, time_steps, mel_bins) x = x.transpose(1, 3) x = self.bn0(x) x = x.transpose(1, 3) if self.training: x = self.spec_augmenter(x)
if self.training and mixup_lambda is not None: x = do_mixup(x, mixup_lambda)
x = self.reshape_wav2img(x) output_dict = self.forward_features(x) else: longer_list = x["longer"].to(device=device, non_blocking=True) x = x["mel_fusion"].to(device=device, non_blocking=True) x = x.transpose(1, 3) x = self.bn0(x) x = x.transpose(1, 3) longer_list_idx = torch.where(longer_list)[0] if self.fusion_type in ["daf_1d", "aff_1d", "iaff_1d"]: new_x = x[:, 0:1, :, :].clone().contiguous() if len(longer_list_idx) > 0: # local processing fusion_x_local = x[longer_list_idx, 1:, :, :].clone().contiguous() FB, FC, FT, FF = fusion_x_local.size() fusion_x_local = fusion_x_local.view(FB * FC, FT, FF) fusion_x_local = torch.permute( fusion_x_local, (0, 2, 1) ).contiguous() fusion_x_local = self.mel_conv1d(fusion_x_local) fusion_x_local = fusion_x_local.view( FB, FC, FF, fusion_x_local.size(-1) ) fusion_x_local = ( torch.permute(fusion_x_local, (0, 2, 1, 3)) .contiguous() .flatten(2) ) if fusion_x_local.size(-1) < FT: fusion_x_local = torch.cat( [ fusion_x_local, torch.zeros( (FB, FF, FT - fusion_x_local.size(-1)), device=device, ), ], dim=-1, ) else: fusion_x_local = fusion_x_local[:, :, :FT] # 1D fusion new_x = new_x.squeeze(1).permute((0, 2, 1)).contiguous() new_x[longer_list_idx] = self.fusion_model( new_x[longer_list_idx], fusion_x_local ) x = new_x.permute((0, 2, 1)).contiguous()[:, None, :, :] else: x = new_x
elif self.fusion_type in ["daf_2d", "aff_2d", "iaff_2d", "channel_map"]: x = x # no change
if self.training: x = self.spec_augmenter(x) if self.training and mixup_lambda is not None: x = do_mixup(x, mixup_lambda)
x = self.reshape_wav2img(x) output_dict = self.forward_features(x, longer_idx=longer_list_idx)
# if infer_mode: # # in infer mode. we need to handle different length audio input # frame_num = x.shape[2] # target_T = int(self.spec_size * self.freq_ratio) # repeat_ratio = math.floor(target_T / frame_num) # x = x.repeat(repeats=(1,1,repeat_ratio,1)) # x = self.reshape_wav2img(x) # output_dict = self.forward_features(x) # else: # if x.shape[2] > self.freq_ratio * self.spec_size: # if self.training: # x = self.crop_wav(x, crop_size=self.freq_ratio * self.spec_size) # x = self.reshape_wav2img(x) # output_dict = self.forward_features(x) # else: # # Change: Hard code here # overlap_size = (x.shape[2] - 1) // 4 # output_dicts = [] # crop_size = (x.shape[2] - 1) // 2 # for cur_pos in range(0, x.shape[2] - crop_size - 1, overlap_size): # tx = self.crop_wav(x, crop_size = crop_size, spe_pos = cur_pos) # tx = self.reshape_wav2img(tx) # output_dicts.append(self.forward_features(tx)) # clipwise_output = torch.zeros_like(output_dicts[0]["clipwise_output"]).float().to(x.device) # framewise_output = torch.zeros_like(output_dicts[0]["framewise_output"]).float().to(x.device) # for d in output_dicts: # clipwise_output += d["clipwise_output"] # framewise_output += d["framewise_output"] # clipwise_output = clipwise_output / len(output_dicts) # framewise_output = framewise_output / len(output_dicts) # output_dict = { # 'framewise_output': framewise_output, # 'clipwise_output': clipwise_output # } # else: # this part is typically used, and most easy one # x = self.reshape_wav2img(x) # output_dict = self.forward_features(x) # x = self.head(x)
# We process the data in the dataloader part, in that here we only consider the input_T < fixed_T
return output_dict
def create_htsat_model(audio_cfg, enable_fusion=False, fusion_type="None"): try:
assert audio_cfg.model_name in [ "tiny", "base", "large", ], "model name for HTS-AT is wrong!" if audio_cfg.model_name == "tiny": model = HTSAT_Swin_Transformer( spec_size=256, patch_size=4, patch_stride=(4, 4), num_classes=audio_cfg.class_num, embed_dim=96, depths=[2, 2, 6, 2], num_heads=[4, 8, 16, 32], window_size=8, config=audio_cfg, enable_fusion=enable_fusion, fusion_type=fusion_type, ) elif audio_cfg.model_name == "base": model = HTSAT_Swin_Transformer( spec_size=256, patch_size=4, patch_stride=(4, 4), num_classes=audio_cfg.class_num, embed_dim=128, depths=[2, 2, 12, 2], num_heads=[4, 8, 16, 32], window_size=8, config=audio_cfg, enable_fusion=enable_fusion, fusion_type=fusion_type, ) elif audio_cfg.model_name == "large": model = HTSAT_Swin_Transformer( spec_size=256, patch_size=4, patch_stride=(4, 4), num_classes=audio_cfg.class_num, embed_dim=256, depths=[2, 2, 12, 2], num_heads=[4, 8, 16, 32], window_size=8, config=audio_cfg, enable_fusion=enable_fusion, fusion_type=fusion_type, )
return model except: raise RuntimeError( f"Import Model for {audio_cfg.model_name} not found, or the audio cfg parameters are not enough." )123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566import numpy as npimport torch.nn.functional as Ffrom torch import nnfrom .model import MLPLayers
class LinearProbe(nn.Module): def __init__(self, model, mlp, freeze, in_ch, out_ch, act=None): """ Args: model: nn.Module mlp: bool, if True, then use the MLP layer as the linear probe module freeze: bool, if Ture, then freeze all the CLAP model's layers when training the linear probe in_ch: int, the output channel from CLAP model out_ch: int, the output channel from linear probe (class_num) act: torch.nn.functional, the activation function before the loss function """ super().__init__() in_ch = 512 self.clap_model = model self.clap_model.text_branch = None # to save memory self.freeze = freeze if mlp: self.lp_layer = MLPLayers(units=[in_ch, in_ch * 2, out_ch]) else: self.lp_layer = nn.Linear(in_ch, out_ch)
if self.freeze: for param in self.clap_model.parameters(): param.requires_grad = False
if act == "None": self.act = None elif act == "relu": self.act = nn.ReLU() elif act == "elu": self.act = nn.ELU() elif act == "prelu": self.act = nn.PReLU(num_parameters=in_ch) elif act == "softmax": self.act = nn.Softmax(dim=-1) elif act == "sigmoid": self.act = nn.Sigmoid()
def forward(self, x, mix_lambda=None, device=None): """ Args: x: waveform, torch.tensor [batch, t_samples] / batch of mel_spec and longer list mix_lambda: torch.tensor [batch], the mixup lambda Returns: class_prob: torch.tensor [batch, class_num]
""" # batchnorm cancel grandient if self.freeze: self.clap_model.eval()
x = self.clap_model.audio_projection( self.clap_model.audio_branch(x, mixup_lambda=mix_lambda, device=device)[ "embedding" ] ) out = self.lp_layer(x) if self.act is not None: out = self.act(out) return out123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398from multiprocessing.sharedctypes import Valueimport torchimport torch.distributed.nnfrom torch import distributed as dist, nn as nnfrom torch.nn import functional as Fimport numpy as npfrom sklearn.metrics import average_precision_score, roc_auc_score, accuracy_score
try: import horovod.torch as hvdexcept ImportError: hvd = None
def gather_features( audio_features, text_features, audio_features_mlp=None, text_features_mlp=None, local_loss=False, gather_with_grad=False, rank=0, world_size=1, use_horovod=False, mlp_loss=False,): if use_horovod: assert hvd is not None, "Please install horovod" if gather_with_grad: all_audio_features = hvd.allgather(audio_features) all_text_features = hvd.allgather(text_features) if mlp_loss: all_audio_features_mlp = hvd.allgather(audio_features_mlp) all_text_features_mlp = hvd.allgather(text_features_mlp) else: with torch.no_grad(): all_audio_features = hvd.allgather(audio_features) all_text_features = hvd.allgather(text_features) if mlp_loss: all_audio_features_mlp = hvd.allgather(audio_features_mlp) all_text_features_mlp = hvd.allgather(text_features_mlp) if not local_loss: # ensure grads for local rank when all_* features don't have a gradient gathered_audio_features = list( all_audio_features.chunk(world_size, dim=0) ) gathered_text_features = list( all_text_features.chunk(world_size, dim=0) ) gathered_audio_features[rank] = audio_features gathered_text_features[rank] = text_features all_audio_features = torch.cat(gathered_audio_features, dim=0) all_text_features = torch.cat(gathered_text_features, dim=0) if mlp_loss: gathered_audio_features_mlp = list( all_audio_features_mlp.chunk(world_size, dim=0) ) gathered_text_features_mlp = list( all_text_features_mlp.chunk(world_size, dim=0) ) gathered_audio_features_mlp[rank] = audio_features_mlp gathered_text_features_mlp[rank] = text_features_mlp all_audio_features_mlp = torch.cat( gathered_audio_features_mlp, dim=0 ) all_text_features_mlp = torch.cat(gathered_text_features_mlp, dim=0) else: # We gather tensors from all gpus if gather_with_grad: all_audio_features = torch.cat( torch.distributed.nn.all_gather(audio_features), dim=0 ) all_text_features = torch.cat( torch.distributed.nn.all_gather(text_features), dim=0 ) if mlp_loss: all_audio_features_mlp = torch.cat( torch.distributed.nn.all_gather(audio_features_mlp), dim=0 ) all_text_features_mlp = torch.cat( torch.distributed.nn.all_gather(text_features_mlp), dim=0 ) else: gathered_audio_features = [ torch.zeros_like(audio_features) for _ in range(world_size) ] gathered_text_features = [ torch.zeros_like(text_features) for _ in range(world_size) ] dist.all_gather(gathered_audio_features, audio_features) dist.all_gather(gathered_text_features, text_features) if mlp_loss: gathered_audio_features_mlp = [ torch.zeros_like(audio_features_mlp) for _ in range(world_size) ] gathered_text_features_mlp = [ torch.zeros_like(text_features_mlp) for _ in range(world_size) ] dist.all_gather(gathered_audio_features_mlp, audio_features_mlp) dist.all_gather(gathered_text_features_mlp, text_features_mlp) if not local_loss: # ensure grads for local rank when all_* features don't have a gradient gathered_audio_features[rank] = audio_features gathered_text_features[rank] = text_features if mlp_loss: gathered_audio_features_mlp[rank] = audio_features_mlp gathered_text_features_mlp[rank] = text_features_mlp
all_audio_features = torch.cat(gathered_audio_features, dim=0) all_text_features = torch.cat(gathered_text_features, dim=0) if mlp_loss: all_audio_features_mlp = torch.cat(gathered_audio_features_mlp, dim=0) all_text_features_mlp = torch.cat(gathered_text_features_mlp, dim=0) if mlp_loss: return ( all_audio_features, all_text_features, all_audio_features_mlp, all_text_features_mlp, ) else: return all_audio_features, all_text_features
class ClipLoss(nn.Module): def __init__( self, local_loss=False, gather_with_grad=False, cache_labels=False, rank=0, world_size=1, use_horovod=False, mlp_loss=False, weight_loss_kappa=0, ): super().__init__() self.local_loss = local_loss self.gather_with_grad = gather_with_grad self.cache_labels = cache_labels self.rank = rank self.world_size = world_size self.use_horovod = use_horovod self.mlp_loss = mlp_loss self.weighted_loss = bool(weight_loss_kappa != 0) self.weight_loss_kappa = weight_loss_kappa # cache state self.prev_num_logits = 0 self.labels = {}
def forward( self, audio_features, text_features, logit_scale_a, logit_scale_t=None, audio_features_mlp=None, text_features_mlp=None, ): device = audio_features.device if self.mlp_loss: if self.world_size > 1: ( all_audio_features, all_text_features, all_audio_features_mlp, all_text_features_mlp, ) = gather_features( audio_features=audio_features, text_features=text_features, audio_features_mlp=audio_features_mlp, text_features_mlp=text_features_mlp, local_loss=self.local_loss, gather_with_grad=self.gather_with_grad, rank=self.rank, world_size=self.world_size, use_horovod=self.use_horovod, mlp_loss=self.mlp_loss, ) if self.local_loss: a_logits_per_audio = ( logit_scale_a * audio_features @ all_text_features_mlp.T ) a_logits_per_text = ( logit_scale_a * text_features_mlp @ all_audio_features.T ) t_logits_per_audio = ( logit_scale_t * audio_features_mlp @ all_text_features.T ) t_logits_per_text = ( logit_scale_t * text_features @ all_audio_features_mlp.T ) else: a_logits_per_audio = ( logit_scale_a * all_audio_features @ all_text_features_mlp.T ) a_logits_per_text = a_logits_per_audio.T t_logits_per_audio = ( logit_scale_t * all_audio_features_mlp @ all_text_features.T ) t_logits_per_text = t_logits_per_audio.T else: a_logits_per_audio = ( logit_scale_a * audio_features @ text_features_mlp.T ) a_logits_per_text = logit_scale_a * text_features_mlp @ audio_features.T t_logits_per_audio = ( logit_scale_t * audio_features_mlp @ text_features.T ) t_logits_per_text = logit_scale_t * text_features @ audio_features_mlp.T
# calculated ground-truth and cache if enabled num_logits = a_logits_per_audio.shape[0] if self.prev_num_logits != num_logits or device not in self.labels: labels = torch.arange(num_logits, device=device, dtype=torch.long) if self.world_size > 1 and self.local_loss: labels = labels + num_logits * self.rank if self.cache_labels: self.labels[device] = labels self.prev_num_logits = num_logits else: labels = self.labels[device]
if not self.weighted_loss: total_loss = ( F.cross_entropy(a_logits_per_audio, labels) + F.cross_entropy(a_logits_per_text, labels) + F.cross_entropy(t_logits_per_audio, labels) + F.cross_entropy(t_logits_per_text, labels) ) / 4 else: audio_weight = (audio_features @ audio_features.T).detach() audio_weight = ( torch.exp( torch.sum(audio_weight, axis=1) / (self.weight_loss_kappa * len(audio_weight)) ) ).detach() text_weight = (text_features @ text_features.T).detach() text_weight = ( torch.exp( torch.sum(text_weight, axis=1) / (self.weight_loss_kappa * len(text_features)) ) ).detach() total_loss = ( F.cross_entropy(a_logits_per_audio, labels, weight=audio_weight) + F.cross_entropy(a_logits_per_text, labels, weight=audio_weight) + F.cross_entropy(t_logits_per_audio, labels, weight=text_weight) + F.cross_entropy(t_logits_per_text, labels, weight=text_weight) ) / 4 else: if self.world_size > 1: all_audio_features, all_text_features = gather_features( audio_features=audio_features, text_features=text_features, local_loss=self.local_loss, gather_with_grad=self.gather_with_grad, rank=self.rank, world_size=self.world_size, use_horovod=self.use_horovod, mlp_loss=self.mlp_loss, )
if self.local_loss: logits_per_audio = ( logit_scale_a * audio_features @ all_text_features.T ) logits_per_text = ( logit_scale_a * text_features @ all_audio_features.T ) else: logits_per_audio = ( logit_scale_a * all_audio_features @ all_text_features.T ) logits_per_text = logits_per_audio.T else: logits_per_audio = logit_scale_a * audio_features @ text_features.T logits_per_text = logit_scale_a * text_features @ audio_features.T
# calculated ground-truth and cache if enabled num_logits = logits_per_audio.shape[0] if self.prev_num_logits != num_logits or device not in self.labels: labels = torch.arange(num_logits, device=device, dtype=torch.long) if self.world_size > 1 and self.local_loss: labels = labels + num_logits * self.rank if self.cache_labels: self.labels[device] = labels self.prev_num_logits = num_logits else: labels = self.labels[device] if not self.weighted_loss: total_loss = ( F.cross_entropy(logits_per_audio, labels) + F.cross_entropy(logits_per_text, labels) ) / 2 else: audio_weight = (all_audio_features @ all_audio_features.T).detach() audio_weight = ( torch.exp( torch.sum(audio_weight, axis=1) / (self.weight_loss_kappa * len(all_audio_features)) ) ).detach() text_weight = (all_text_features @ all_text_features.T).detach() text_weight = ( torch.exp( torch.sum(text_weight, axis=1) / (self.weight_loss_kappa * len(all_text_features)) ) ).detach() total_loss = ( F.cross_entropy(logits_per_audio, labels, weight=text_weight) + F.cross_entropy(logits_per_text, labels, weight=audio_weight) ) / 2 return total_loss
def lp_gather_features(pred, target, world_size=1, use_horovod=False): if use_horovod: assert hvd is not None, "Please install horovod" with torch.no_grad(): all_preds = hvd.allgather(pred) all_targets = hvd.allgath(target) else: gathered_preds = [torch.zeros_like(pred) for _ in range(world_size)] gathered_targets = [torch.zeros_like(target) for _ in range(world_size)]
dist.all_gather(gathered_preds, pred) dist.all_gather(gathered_targets, target) all_preds = torch.cat(gathered_preds, dim=0) all_targets = torch.cat(gathered_targets, dim=0)
return all_preds, all_targets
def get_map(pred, target): pred = torch.sigmoid(pred).numpy() target = target.numpy() return np.mean(average_precision_score(target, pred, average=None))
def get_acc(pred, target): pred = torch.argmax(pred, 1).numpy() target = torch.argmax(target, 1).numpy() return accuracy_score(target, pred)
def get_mauc(pred, target): pred = torch.sigmoid(pred).numpy() target = target.numpy() return np.mean(roc_auc_score(target, pred, average=None))
class LPMetrics(object): def __init__(self, metric_names=["map", "acc", "mauc"]): self.metrics = [] for name in metric_names: self.metrics.append(self.get_metric(name)) self.metric_names = metric_names
def get_metric(self, name): if name == "map": return get_map elif name == "acc": return get_acc elif name == "mauc": return get_mauc else: raise ValueError(f"the metric should be at least one of [map, acc, mauc]")
def evaluate_mertics(self, pred, target): metric_dict = {} for i in range(len(self.metric_names)): metric_dict[self.metric_names[i]] = self.metrics[i](pred, target) return metric_dict
def calc_celoss(pred, target): target = torch.argmax(target, 1).long() return nn.CrossEntropyLoss()(pred, target)
class LPLoss(nn.Module): def __init__(self, loss_name): super().__init__() if loss_name == "bce": self.loss_func = nn.BCEWithLogitsLoss() elif loss_name == "ce": self.loss_func = calc_celoss elif loss_name == "mse": self.loss_func = nn.MSELoss() else: raise ValueError(f"the loss func should be at least one of [bce, ce, mse]")
def forward(self, pred, target): loss = self.loss_func(pred, target) return loss123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936""" CLAP Model
Adapted from CLIP: https://github.com/openai/CLIP. Originally MIT License, Copyright (c) 2021 OpenAI.Adapted to the Audio Task."""
from collections import OrderedDictfrom dataclasses import dataclassfrom email.mime import audiofrom typing import Tuple, Union, Callable, Optional
import numpy as npimport torchimport torch.nn.functional as Ffrom torch import nn
from .timm_model import TimmModelimport loggingfrom .utils import freeze_batch_norm_2d
from .pann_model import create_pann_modelfrom .htsat import create_htsat_modelfrom transformers import BertModel, RobertaModel, BartModelfrom transformers.tokenization_utils_base import BatchEncoding
class MLPLayers(nn.Module): def __init__(self, units=[512, 512, 512], nonlin=nn.ReLU(), dropout=0.1): super(MLPLayers, self).__init__() self.nonlin = nonlin self.dropout = dropout
sequence = [] for u0, u1 in zip(units[:-1], units[1:]): sequence.append(nn.Linear(u0, u1)) sequence.append(self.nonlin) sequence.append(nn.Dropout(self.dropout)) sequence = sequence[:-2]
self.sequential = nn.Sequential(*sequence)
def forward(self, X): X = self.sequential(X) return X
class Bottleneck(nn.Module): expansion = 4
def __init__(self, inplanes, planes, stride=1): super().__init__()
# all conv layers have stride 1. an avgpool is performed after the second convolution when stride > 1 self.conv1 = nn.Conv2d(inplanes, planes, 1, bias=False) self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, 3, padding=1, bias=False) self.bn2 = nn.BatchNorm2d(planes)
self.avgpool = nn.AvgPool2d(stride) if stride > 1 else nn.Identity()
self.conv3 = nn.Conv2d(planes, planes * self.expansion, 1, bias=False) self.bn3 = nn.BatchNorm2d(planes * self.expansion)
self.relu = nn.ReLU(inplace=True) self.downsample = None self.stride = stride
if stride > 1 or inplanes != planes * Bottleneck.expansion: # downsampling layer is prepended with an avgpool, and the subsequent convolution has stride 1 self.downsample = nn.Sequential( OrderedDict( [ ("-1", nn.AvgPool2d(stride)), ( "0", nn.Conv2d( inplanes, planes * self.expansion, 1, stride=1, bias=False, ), ), ("1", nn.BatchNorm2d(planes * self.expansion)), ] ) )
def forward(self, x: torch.Tensor): identity = x
out = self.relu(self.bn1(self.conv1(x))) out = self.relu(self.bn2(self.conv2(out))) out = self.avgpool(out) out = self.bn3(self.conv3(out))
if self.downsample is not None: identity = self.downsample(x)
out += identity out = self.relu(out) return out
class AttentionPool2d(nn.Module): def __init__( self, spacial_dim: int, embed_dim: int, num_heads: int, output_dim: int = None ): super().__init__() self.positional_embedding = nn.Parameter( torch.randn(spacial_dim**2 + 1, embed_dim) / embed_dim**0.5 ) self.k_proj = nn.Linear(embed_dim, embed_dim) self.q_proj = nn.Linear(embed_dim, embed_dim) self.v_proj = nn.Linear(embed_dim, embed_dim) self.c_proj = nn.Linear(embed_dim, output_dim or embed_dim) self.num_heads = num_heads
def forward(self, x): x = x.reshape(x.shape[0], x.shape[1], x.shape[2] * x.shape[3]).permute( 2, 0, 1 ) # NCHW -> (HW)NC x = torch.cat([x.mean(dim=0, keepdim=True), x], dim=0) # (HW+1)NC x = x + self.positional_embedding[:, None, :].to(x.dtype) # (HW+1)NC x, _ = F.multi_head_attention_forward( query=x, key=x, value=x, embed_dim_to_check=x.shape[-1], num_heads=self.num_heads, q_proj_weight=self.q_proj.weight, k_proj_weight=self.k_proj.weight, v_proj_weight=self.v_proj.weight, in_proj_weight=None, in_proj_bias=torch.cat( [self.q_proj.bias, self.k_proj.bias, self.v_proj.bias] ), bias_k=None, bias_v=None, add_zero_attn=False, dropout_p=0, out_proj_weight=self.c_proj.weight, out_proj_bias=self.c_proj.bias, use_separate_proj_weight=True, training=self.training, need_weights=False, )
return x[0]
class ModifiedResNet(nn.Module): """ A ResNet class that is similar to torchvision's but contains the following changes: - There are now 3 "stem" convolutions as opposed to 1, with an average pool instead of a max pool. - Performs anti-aliasing strided convolutions, where an avgpool is prepended to convolutions with stride > 1 - The final pooling layer is a QKV attention instead of an average pool """
def __init__(self, layers, output_dim, heads, image_size=224, width=64): super().__init__() self.output_dim = output_dim self.image_size = image_size
# the 3-layer stem self.conv1 = nn.Conv2d( 3, width // 2, kernel_size=3, stride=2, padding=1, bias=False ) self.bn1 = nn.BatchNorm2d(width // 2) self.conv2 = nn.Conv2d( width // 2, width // 2, kernel_size=3, padding=1, bias=False ) self.bn2 = nn.BatchNorm2d(width // 2) self.conv3 = nn.Conv2d(width // 2, width, kernel_size=3, padding=1, bias=False) self.bn3 = nn.BatchNorm2d(width) self.avgpool = nn.AvgPool2d(2) self.relu = nn.ReLU(inplace=True)
# residual layers self._inplanes = width # this is a *mutable* variable used during construction self.layer1 = self._make_layer(width, layers[0]) self.layer2 = self._make_layer(width * 2, layers[1], stride=2) self.layer3 = self._make_layer(width * 4, layers[2], stride=2) self.layer4 = self._make_layer(width * 8, layers[3], stride=2)
embed_dim = width * 32 # the ResNet feature dimension self.attnpool = AttentionPool2d(image_size // 32, embed_dim, heads, output_dim)
self.init_parameters()
def _make_layer(self, planes, blocks, stride=1): layers = [Bottleneck(self._inplanes, planes, stride)]
self._inplanes = planes * Bottleneck.expansion for _ in range(1, blocks): layers.append(Bottleneck(self._inplanes, planes))
return nn.Sequential(*layers)
def init_parameters(self): if self.attnpool is not None: std = self.attnpool.c_proj.in_features**-0.5 nn.init.normal_(self.attnpool.q_proj.weight, std=std) nn.init.normal_(self.attnpool.k_proj.weight, std=std) nn.init.normal_(self.attnpool.v_proj.weight, std=std) nn.init.normal_(self.attnpool.c_proj.weight, std=std)
for resnet_block in [self.layer1, self.layer2, self.layer3, self.layer4]: for name, param in resnet_block.named_parameters(): if name.endswith("bn3.weight"): nn.init.zeros_(param)
def lock(self, unlocked_groups=0, freeze_bn_stats=False): assert ( unlocked_groups == 0 ), "partial locking not currently supported for this model" for param in self.parameters(): param.requires_grad = False if freeze_bn_stats: freeze_batch_norm_2d(self)
def stem(self, x): for conv, bn in [ (self.conv1, self.bn1), (self.conv2, self.bn2), (self.conv3, self.bn3), ]: x = self.relu(bn(conv(x))) x = self.avgpool(x) return x
def forward(self, x): x = self.stem(x) x = self.layer1(x) x = self.layer2(x) x = self.layer3(x) x = self.layer4(x) x = self.attnpool(x)
return x
class LayerNorm(nn.LayerNorm): """Subclass torch's LayerNorm to handle fp16."""
def forward(self, x: torch.Tensor): orig_type = x.dtype x = F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps) return x.to(orig_type)
class QuickGELU(nn.Module): # NOTE This is slower than nn.GELU or nn.SiLU and uses more GPU memory def forward(self, x: torch.Tensor): return x * torch.sigmoid(1.702 * x)
class ResidualAttentionBlock(nn.Module): def __init__(self, d_model: int, n_head: int, act_layer: Callable = nn.GELU): super().__init__()
self.attn = nn.MultiheadAttention(d_model, n_head) self.ln_1 = LayerNorm(d_model) self.mlp = nn.Sequential( OrderedDict( [ ("c_fc", nn.Linear(d_model, d_model * 4)), ("gelu", act_layer()), ("c_proj", nn.Linear(d_model * 4, d_model)), ] ) ) self.ln_2 = LayerNorm(d_model)
def attention(self, x: torch.Tensor, attn_mask: Optional[torch.Tensor] = None): return self.attn(x, x, x, need_weights=False, attn_mask=attn_mask)[0]
def forward(self, x: torch.Tensor, attn_mask: Optional[torch.Tensor] = None): x = x + self.attention(self.ln_1(x), attn_mask=attn_mask) x = x + self.mlp(self.ln_2(x)) return x
class Transformer(nn.Module): def __init__( self, width: int, layers: int, heads: int, act_layer: Callable = nn.GELU ): super().__init__() self.width = width self.layers = layers self.resblocks = nn.ModuleList( [ ResidualAttentionBlock(width, heads, act_layer=act_layer) for _ in range(layers) ] )
def forward(self, x: torch.Tensor, attn_mask: Optional[torch.Tensor] = None): for r in self.resblocks: x = r(x, attn_mask=attn_mask) return x
class VisualTransformer(nn.Module): def __init__( self, image_size: int, patch_size: int, width: int, layers: int, heads: int, output_dim: int, act_layer: Callable = nn.GELU, ): super().__init__() self.image_size = image_size self.output_dim = output_dim self.conv1 = nn.Conv2d( in_channels=3, out_channels=width, kernel_size=patch_size, stride=patch_size, bias=False, )
scale = width**-0.5 self.class_embedding = nn.Parameter(scale * torch.randn(width)) self.positional_embedding = nn.Parameter( scale * torch.randn((image_size // patch_size) ** 2 + 1, width) ) self.ln_pre = LayerNorm(width)
self.text_branch = Transformer(width, layers, heads, act_layer=act_layer)
self.ln_post = LayerNorm(width) self.proj = nn.Parameter(scale * torch.randn(width, output_dim))
def lock(self, unlocked_groups=0, freeze_bn_stats=False): assert ( unlocked_groups == 0 ), "partial locking not currently supported for this model" for param in self.parameters(): param.requires_grad = False
def forward(self, x: torch.Tensor): x = self.conv1(x) # shape = [*, width, grid, grid] x = x.reshape(x.shape[0], x.shape[1], -1) # shape = [*, width, grid ** 2] x = x.permute(0, 2, 1) # shape = [*, grid ** 2, width] x = torch.cat( [ self.class_embedding.to(x.dtype) + torch.zeros( x.shape[0], 1, x.shape[-1], dtype=x.dtype, device=x.device ), x, ], dim=1, ) # shape = [*, grid ** 2 + 1, width] x = x + self.positional_embedding.to(x.dtype) x = self.ln_pre(x)
x = x.permute(1, 0, 2) # NLD -> LND x = self.text_branch(x) x = x.permute(1, 0, 2) # LND -> NLD
x = self.ln_post(x[:, 0, :])
if self.proj is not None: x = x @ self.proj
return x
@dataclassclass CLAPVisionCfg: layers: Union[Tuple[int, int, int, int], int] = 12 width: int = 768 patch_size: int = 16 image_size: Union[Tuple[int, int], int] = 224 timm_model_name: str = ( None # a valid model name overrides layers, width, patch_size ) timm_model_pretrained: bool = ( False # use (imagenet) pretrained weights for named model ) timm_pool: str = ( "avg" # feature pooling for timm model ('abs_attn', 'rot_attn', 'avg', '') ) timm_proj: str = ( "linear" # linear projection for timm model output ('linear', 'mlp', '') )
# Audio Config Class@dataclassclass CLAPAudioCfp: model_type: str = "PANN" model_name: str = "Cnn14" sample_rate: int = 48000 # Param audio_length: int = 1024 window_size: int = 1024 hop_size: int = 1024 fmin: int = 50 fmax: int = 14000 class_num: int = 527 mel_bins: int = 64 clip_samples: int = 480000
@dataclassclass CLAPTextCfg: context_length: int vocab_size: int width: int heads: int layers: int model_type: str
class CLAP(nn.Module): def __init__( self, embed_dim: int, audio_cfg: CLAPAudioCfp, text_cfg: CLAPTextCfg, quick_gelu: bool = False, enable_fusion: bool = False, fusion_type: str = "None", joint_embed_shape: int = 512, mlp_act: str = "relu", ): super().__init__() if isinstance(audio_cfg, dict): audio_cfg = CLAPAudioCfp(**audio_cfg) if isinstance(text_cfg, dict): text_cfg = CLAPTextCfg(**text_cfg)
self.audio_cfg = audio_cfg self.text_cfg = text_cfg self.enable_fusion = enable_fusion self.fusion_type = fusion_type self.joint_embed_shape = joint_embed_shape self.mlp_act = mlp_act
self.context_length = text_cfg.context_length
# OpenAI models are pretrained w/ QuickGELU but native nn.GELU is both faster and more # memory efficient in recent PyTorch releases (>= 1.10). # NOTE: timm models always use native GELU regardless of quick_gelu flag. act_layer = QuickGELU if quick_gelu else nn.GELU
if mlp_act == "relu": mlp_act_layer = nn.ReLU() elif mlp_act == "gelu": mlp_act_layer = nn.GELU() else: raise NotImplementedError
# audio branch # audio branch parameters if audio_cfg.model_type == "PANN": self.audio_branch = create_pann_model(audio_cfg, enable_fusion, fusion_type) elif audio_cfg.model_type == "HTSAT": self.audio_branch = create_htsat_model( audio_cfg, enable_fusion, fusion_type ) else: logging.error(f"Model config for {audio_cfg.model_type} not found") raise RuntimeError(f"Model config for {audio_cfg.model_type} not found.")
# text branch # text branch parameters if text_cfg.model_type == "transformer": self.text_branch = Transformer( width=text_cfg.width, layers=text_cfg.layers, heads=text_cfg.heads, act_layer=act_layer, ) self.vocab_size = text_cfg.vocab_size self.token_embedding = nn.Embedding(text_cfg.vocab_size, text_cfg.width) self.positional_embedding = nn.Parameter( torch.empty(self.context_length, text_cfg.width) ) self.ln_final = LayerNorm(text_cfg.width) self.text_transform = MLPLayers( units=[ self.joint_embed_shape, self.joint_embed_shape, self.joint_embed_shape, ], dropout=0.1, ) self.text_projection = nn.Sequential( nn.Linear(text_cfg.width, self.joint_embed_shape), mlp_act_layer, nn.Linear(self.joint_embed_shape, self.joint_embed_shape), ) elif text_cfg.model_type == "bert": self.text_branch = BertModel.from_pretrained("bert-base-uncased") self.text_transform = MLPLayers( units=[ self.joint_embed_shape, self.joint_embed_shape, self.joint_embed_shape, ], dropout=0.1, ) self.text_projection = nn.Sequential( nn.Linear(768, self.joint_embed_shape), mlp_act_layer, nn.Linear(self.joint_embed_shape, self.joint_embed_shape), ) elif text_cfg.model_type == "roberta": self.text_branch = RobertaModel.from_pretrained("roberta-base") self.text_transform = MLPLayers( units=[ self.joint_embed_shape, self.joint_embed_shape, self.joint_embed_shape, ], dropout=0.1, ) self.text_projection = nn.Sequential( nn.Linear(768, self.joint_embed_shape), mlp_act_layer, nn.Linear(self.joint_embed_shape, self.joint_embed_shape), ) elif text_cfg.model_type == "bart": self.text_branch = BartModel.from_pretrained("facebook/bart-base") self.text_transform = MLPLayers( units=[ self.joint_embed_shape, self.joint_embed_shape, self.joint_embed_shape, ], dropout=0.1, ) self.text_projection = nn.Sequential( nn.Linear(768, self.joint_embed_shape), mlp_act_layer, nn.Linear(self.joint_embed_shape, self.joint_embed_shape), ) else: logging.error(f"Model config for {text_cfg.model_type} not found") raise RuntimeError(f"Model config for {text_cfg.model_type} not found.") self.text_branch_type = text_cfg.model_type # text branch parameters
# audio branch parameters self.audio_transform = MLPLayers( units=[ self.joint_embed_shape, self.joint_embed_shape, self.joint_embed_shape, ], dropout=0.1, )
# below here is text branch parameters
# ============================================================================================================ self.audio_projection = nn.Sequential( nn.Linear(embed_dim, self.joint_embed_shape), mlp_act_layer, nn.Linear(self.joint_embed_shape, self.joint_embed_shape), )
self.logit_scale_a = nn.Parameter(torch.ones([]) * np.log(1 / 0.07)) self.logit_scale_t = nn.Parameter(torch.ones([]) * np.log(1 / 0.07)) self.register_buffer("attn_mask", self.build_attention_mask(), persistent=False)
self.init_text_branch_parameters()
def init_text_branch_parameters(self): if self.text_branch_type == "transformer": nn.init.normal_(self.token_embedding.weight, std=0.02) nn.init.normal_(self.positional_embedding, std=0.01) proj_std = (self.text_branch.width**-0.5) * ( (2 * self.text_branch.layers) ** -0.5 ) attn_std = self.text_branch.width**-0.5 fc_std = (2 * self.text_branch.width) ** -0.5 for block in self.text_branch.resblocks: nn.init.normal_(block.attn.in_proj_weight, std=attn_std) nn.init.normal_(block.attn.out_proj.weight, std=proj_std) nn.init.normal_(block.mlp.c_fc.weight, std=fc_std) nn.init.normal_(block.mlp.c_proj.weight, std=proj_std) if self.text_branch_type == "bert" or self.text_branch_type == "roberta": width = self.text_branch.embeddings.word_embeddings.weight.shape[-1] elif self.text_branch_type == "bart": width = self.text_branch.shared.weight.shape[-1] else: width = self.text_branch.width nn.init.constant_(self.logit_scale_a, np.log(1 / 0.07)) nn.init.constant_(self.logit_scale_t, np.log(1 / 0.07))
# deprecated # if hasattr(self.visual, 'init_parameters'): # self.visual.init_parameters()
# if self.text_projection is not None: # nn.init.normal_(self.text_projection, std=width**-0.5)
def build_attention_mask(self): # lazily create causal attention mask, with full attention between the vision tokens # pytorch uses additive attention mask; fill with -inf mask = torch.empty(self.context_length, self.context_length) mask.fill_(float("-inf")) mask.triu_(1) # zero out the lower diagonal return mask
def encode_audio(self, audio, device): return self.audio_branch( audio, mixup_lambda=None, device=device ) # mix lambda needs to add
# def list_of_dict_of_tensor2dict_of_tensor(self, x, device): # tmp = {} # for k in x[0].keys(): # tmp[k] = [] # for i in range(len(x)): # tmp[k].append(x[i][k][:77]) # for k in x[0].keys(): # tmp[k] = torch.tensor(tmp[k]).to(device=device, non_blocking=True) # return tmp
def encode_text(self, text, device): if self.text_branch_type == "transformer": text = text.to(device=device, non_blocking=True) x = self.token_embedding(text) # [batch_size, n_ctx, d_model]
x = x + self.positional_embedding x = x.permute(1, 0, 2) # NLD -> LND x = self.text_branch(x, attn_mask=self.attn_mask) x = x.permute(1, 0, 2) # LND -> NLD x = self.ln_final(x)
# x.shape = [batch_size, n_ctx, transformer.width] # take features from the eot embedding (eot_token is the highest number in each sequence) x = self.text_projection(x[torch.arange(x.shape[0]), text.argmax(dim=-1)]) elif self.text_branch_type == "bert": # text = self.list_of_dict_of_tensor2dict_of_tensor(text, device) # text = BatchEncoding(text) x = self.text_branch( input_ids=text["input_ids"].to(device=device, non_blocking=True), attention_mask=text["attention_mask"].to( device=device, non_blocking=True ), token_type_ids=text["token_type_ids"].to( device=device, non_blocking=True ), )["pooler_output"] x = self.text_projection(x) elif self.text_branch_type == "roberta": x = self.text_branch( input_ids=text["input_ids"].to(device=device, non_blocking=True), attention_mask=text["attention_mask"].to( device=device, non_blocking=True ), )["pooler_output"] x = self.text_projection(x) elif self.text_branch_type == "bart": x = torch.mean( self.text_branch( input_ids=text["input_ids"].to(device=device, non_blocking=True), attention_mask=text["attention_mask"].to( device=device, non_blocking=True ), )["encoder_last_hidden_state"], axis=1, ) x = self.text_projection(x) else: logging.error(f"Model type {self.text_branch_type} not found") raise RuntimeError(f"Model type {self.text_branch_type} not found.") return x
def forward(self, audio, text, device=None): """Forward audio and text into the CLAP
Parameters ---------- audio: torch.Tensor (batch_size, audio_length) the time-domain audio input / the batch of mel_spec and longer list. text: torch.Tensor () // need to add the text token input """ if device is None: if audio is not None: device = audio.device elif text is not None: device = text.device if audio is None and text is None: # a hack to get the logit scale return self.logit_scale_a.exp(), self.logit_scale_t.exp() elif audio is None: return self.encode_text(text, device=device) elif text is None: return self.audio_projection( self.encode_audio(audio, device=device)["embedding"] ) audio_features = self.audio_projection( self.encode_audio(audio, device=device)["embedding"] ) audio_features = F.normalize(audio_features, dim=-1)
text_features = self.encode_text(text, device=device) # print("text_features", text_features) # print("text_features.shape", text_features.shape) # print("text_features.type", type(text_features)) text_features = F.normalize(text_features, dim=-1)
audio_features_mlp = self.audio_transform(audio_features) text_features_mlp = self.text_transform(text_features) # Four outputs: audio features (basic & MLP), text features (basic & MLP) return ( audio_features, text_features, audio_features_mlp, text_features_mlp, self.logit_scale_a.exp(), self.logit_scale_t.exp(), )
def get_logit_scale(self): return self.logit_scale_a.exp(), self.logit_scale_t.exp()
def get_text_embedding(self, data): """Get the text embedding from the model
Parameters ---------- data: torch.Tensor a tensor of text embedding
Returns ---------- text_embed: torch.Tensor a tensor of text_embeds (N, D)
""" device = next(self.parameters()).device for k in data: data[k] = data[k].to(device) if len(data[k].size()) < 2: data[k] = data[k].unsqueeze(0) text_embeds = self.encode_text(data, device=device) text_embeds = F.normalize(text_embeds, dim=-1)
return text_embeds
def get_audio_embedding(self, data): """Get the audio embedding from the model
Parameters ---------- data: a list of dict the audio input dict list from 'get_audio_feature' method
Returns ---------- audio_embed: torch.Tensor a tensor of audio_embeds (N, D)
""" device = next(self.parameters()).device input_dict = {} keys = data[0].keys() for k in keys: input_dict[k] = torch.cat([d[k].unsqueeze(0) for d in data], dim=0).to( device )
audio_embeds = self.audio_projection( self.encode_audio(input_dict, device=device)["embedding"] ) audio_embeds = F.normalize(audio_embeds, dim=-1)
return audio_embeds
def audio_infer(self, audio, hopsize=None, device=None): """Forward one audio and produce the audio embedding
Parameters ---------- audio: (audio_length) the time-domain audio input, notice that it must be only one input hopsize: int the overlap hopsize as the sliding window
Returns ---------- output_dict: { key: [n, (embedding_shape)] if "HTS-AT" or key: [(embedding_shape)] if "PANN" } the list of key values of the audio branch
"""
assert not self.training, "the inference mode must be run at eval stage" output_dict = {} # PANN if self.audio_cfg.model_type == "PANN": audio_input = audio.unsqueeze(dim=0) output_dict[key] = self.encode_audio(audio_input, device=device)[ key ].squeeze(dim=0) elif self.audio_cfg.model_type == "HTSAT": # repeat audio_len = len(audio) k = self.audio_cfg.clip_samples // audio_len if k > 1: audio = audio.repeat(k) audio_len = len(audio)
if hopsize is None: hopsize = min(hopsize, audio_len)
if audio_len > self.audio_cfg.clip_samples: audio_input = [ audio[pos : pos + self.audio_cfg.clip_samples].clone() for pos in range( 0, audio_len - self.audio_cfg.clip_samples, hopsize ) ] audio_input.append(audio[-self.audio_cfg.clip_samples :].clone()) audio_input = torch.stack(audio_input) output_dict[key] = self.encode_audio(audio_input, device=device)[key] else: audio_input = audio.unsqueeze(dim=0) output_dict[key] = self.encode_audio(audio_input, device=device)[ key ].squeeze(dim=0)
return output_dict
def convert_weights_to_fp16(model: nn.Module): """Convert applicable model parameters to fp16"""
def _convert_weights_to_fp16(l): if isinstance(l, (nn.Conv1d, nn.Conv2d, nn.Linear)): l.weight.data = l.weight.data.half() if l.bias is not None: l.bias.data = l.bias.data.half()
if isinstance(l, nn.MultiheadAttention): for attr in [ *[f"{s}_proj_weight" for s in ["in", "q", "k", "v"]], "in_proj_bias", "bias_k", "bias_v", ]: tensor = getattr(l, attr) if tensor is not None: tensor.data = tensor.data.half()
for name in ["text_projection", "proj"]: if hasattr(l, name): attr = getattr(l, name) if attr is not None: attr.data = attr.data.half()
model.apply(_convert_weights_to_fp16)
# Ignore the state dict of the vision partdef build_model_from_openai_state_dict( state_dict: dict, model_cfg, enable_fusion: bool = False, fusion_type: str = "None"):
embed_dim = model_cfg["embed_dim"] audio_cfg = model_cfg["audio_cfg"] text_cfg = model_cfg["text_cfg"] context_length = state_dict["positional_embedding"].shape[0] vocab_size = state_dict["token_embedding.weight"].shape[0] transformer_width = state_dict["ln_final.weight"].shape[0] transformer_heads = transformer_width // 64 transformer_layers = len( set( k.split(".")[2] for k in state_dict if k.startswith(f"transformer.resblocks") ) )
audio_cfg = CLAPAudioCfp(**audio_cfg) text_cfg = CLAPTextCfg(**text_cfg)
model = CLAP( embed_dim, audio_cfg=audio_cfg, text_cfg=text_cfg, quick_gelu=True, # OpenAI models were trained with QuickGELU enable_fusion=enable_fusion, fusion_type=fusion_type, ) state_dict["logit_scale_a"] = state_dict["logit_scale"] state_dict["logit_scale_t"] = state_dict["logit_scale"] pop_keys = list(state_dict.keys())[::] # pop the visual branch saved weights for key in pop_keys: if key.startswith("visual."): state_dict.pop(key, None)
for key in ["logit_scale", "input_resolution", "context_length", "vocab_size"]: state_dict.pop(key, None)
# not use fp16 # convert_weights_to_fp16(model) model.load_state_dict(state_dict, strict=False) return model.eval()
def trace_model(model, batch_size=256, device=torch.device("cpu")): model.eval() audio_length = model.audio_cfg.audio_length example_audio = torch.ones((batch_size, audio_length), device=device) example_text = torch.zeros( (batch_size, model.context_length), dtype=torch.int, device=device ) model = torch.jit.trace_module( model, inputs=dict( forward=(example_audio, example_text), encode_text=(example_text,), encode_image=(example_audio,), ), ) model.audio_cfg.audio_length = audio_length # Question: what does this do? return model1234567891011121314151617181920212223{ "embed_dim": 1024, "audio_cfg": { "audio_length": 1024, "clip_samples": 480000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1024, "hop_size": 480, "fmin": 50, "fmax": 14000, "class_num": 527, "model_type": "HTSAT", "model_name": "base" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}1234567891011121314151617181920212223{ "embed_dim": 2048, "audio_cfg": { "audio_length": 1024, "clip_samples": 480000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1024, "hop_size": 480, "fmin": 50, "fmax": 14000, "class_num": 527, "model_type": "HTSAT", "model_name": "large" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}1234567891011121314151617181920212223{ "embed_dim": 768, "audio_cfg": { "audio_length": 1024, "clip_samples": 480000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1536, "hop_size": 480, "fmin": 50, "fmax": 14000, "class_num": 527, "model_type": "HTSAT", "model_name": "tiny" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}1234567891011121314151617181920212223{ "embed_dim": 768, "audio_cfg": { "audio_length": 1024, "clip_samples": 480000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1024, "hop_size": 480, "fmin": 50, "fmax": 14000, "class_num": 527, "model_type": "HTSAT", "model_name": "tiny" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}1234567891011121314151617181920212223{ "embed_dim": 1024, "audio_cfg": { "audio_length": 1024, "clip_samples": 480000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1024, "hop_size": 480, "fmin": 50, "fmax": 14000, "class_num": 527, "model_type": "PANN", "model_name": "Cnn10" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}1234567891011121314151617181920212223{ "embed_dim": 2048, "audio_cfg": { "audio_length": 1024, "clip_samples": 480000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1024, "hop_size": 480, "fmin": 50, "fmax": 18000, "class_num": 527, "model_type": "PANN", "model_name": "Cnn14" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}1234567891011121314151617181920212223{ "embed_dim": 2048, "audio_cfg": { "audio_length": 1024, "clip_samples": 960000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1024, "hop_size": 360, "fmin": 50, "fmax": 8000, "class_num": 527, "model_type": "PANN", "model_name": "Cnn14" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}1234567891011121314151617181920212223{ "embed_dim": 2048, "audio_cfg": { "audio_length": 1024, "clip_samples": 480000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1024, "hop_size": 480, "fmin": 50, "fmax": 14000, "class_num": 527, "model_type": "PANN", "model_name": "Cnn14" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 4 }}1234567891011121314151617181920212223{ "embed_dim": 2048, "audio_cfg": { "audio_length": 1024, "clip_samples": 480000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1536, "hop_size": 480, "fmin": 50, "fmax": 14000, "class_num": 527, "model_type": "PANN", "model_name": "Cnn14" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}1234567891011121314151617181920212223{ "embed_dim": 2048, "audio_cfg": { "audio_length": 1024, "clip_samples": 480000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1024, "hop_size": 480, "fmin": 50, "fmax": 14000, "class_num": 527, "model_type": "PANN", "model_name": "Cnn14" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}1234567891011121314151617181920212223{ "embed_dim": 512, "audio_cfg": { "audio_length": 1024, "clip_samples": 480000, "mel_bins": 64, "sample_rate": 48000, "window_size": 1024, "hop_size": 480, "fmin": 50, "fmax": 14000, "class_num": 527, "model_type": "PANN", "model_name": "Cnn6" }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}12345678910111213141516171819202122{ "embed_dim": 512, "quick_gelu": true, "vision_cfg": { "image_size": 224, "layers": [ 3, 4, 23, 3 ], "width": 64, "patch_size": null }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}123456789101112131415161718192021{ "embed_dim": 512, "vision_cfg": { "image_size": 224, "layers": [ 3, 4, 23, 3 ], "width": 64, "patch_size": null }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}12345678910111213141516171819202122{ "embed_dim": 1024, "quick_gelu": true, "vision_cfg": { "image_size": 224, "layers": [ 3, 4, 6, 3 ], "width": 64, "patch_size": null }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}123456789101112131415161718192021{ "embed_dim": 1024, "vision_cfg": { "image_size": 224, "layers": [ 3, 4, 6, 3 ], "width": 64, "patch_size": null }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}123456789101112131415161718192021{ "embed_dim": 768, "vision_cfg": { "image_size": 384, "layers": [ 6, 8, 18, 8 ], "width": 96, "patch_size": null }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 768, "heads": 12, "layers": 12 }}123456789101112131415161718192021{ "embed_dim": 640, "vision_cfg": { "image_size": 288, "layers": [ 4, 6, 10, 6 ], "width": 80, "patch_size": null }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 640, "heads": 10, "layers": 12 }}12345678910111213141516{ "embed_dim": 512, "vision_cfg": { "image_size": 224, "layers": 12, "width": 768, "patch_size": 16 }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}1234567891011121314151617{ "embed_dim": 512, "quick_gelu": true, "vision_cfg": { "image_size": 224, "layers": 12, "width": 768, "patch_size": 32 }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}12345678910111213141516{ "embed_dim": 512, "vision_cfg": { "image_size": 224, "layers": 12, "width": 768, "patch_size": 32 }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 512, "heads": 8, "layers": 12 }}12345678910111213141516{ "embed_dim": 768, "vision_cfg": { "image_size": 224, "layers": 24, "width": 1024, "patch_size": 14 }, "text_cfg": { "context_length": 77, "vocab_size": 49408, "width": 768, "heads": 12, "layers": 12 }}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159""" OpenAI pretrained model functions
Adapted from https://github.com/openai/CLIP. Originally MIT License, Copyright (c) 2021 OpenAI."""
import osimport warningsfrom typing import Union, List
import torch
from .model import build_model_from_openai_state_dictfrom .pretrained import ( get_pretrained_url, list_pretrained_tag_models, download_pretrained,)
__all__ = ["list_openai_models", "load_openai_model"]
CACHE_DIR = os.getenv("AUDIOLDM_CACHE_DIR", "~/.cache")
def list_openai_models() -> List[str]: """Returns the names of available CLIP models""" return list_pretrained_tag_models("openai")
def load_openai_model( name: str, model_cfg, device: Union[str, torch.device] = "cuda" if torch.cuda.is_available() else "cpu", jit=True, cache_dir=os.path.expanduser(f"{CACHE_DIR}/clip"), enable_fusion: bool = False, fusion_type: str = "None",): """Load a CLIP model, preserve its text pretrained part, and set in the CLAP model
Parameters ---------- name : str A model name listed by `clip.available_models()`, or the path to a model checkpoint containing the state_dict device : Union[str, torch.device] The device to put the loaded model jit : bool Whether to load the optimized JIT model (default) or more hackable non-JIT model.
Returns ------- model : torch.nn.Module The CLAP model preprocess : Callable[[PIL.Image], torch.Tensor] A torchvision transform that converts a PIL image into a tensor that the returned model can take as its input """ if get_pretrained_url(name, "openai"): model_path = download_pretrained( get_pretrained_url(name, "openai"), root=cache_dir ) elif os.path.isfile(name): model_path = name else: raise RuntimeError( f"Model {name} not found; available models = {list_openai_models()}" )
try: # loading JIT archive model = torch.jit.load(model_path, map_location=device if jit else "cpu").eval() state_dict = None except RuntimeError: # loading saved state dict if jit: warnings.warn( f"File {model_path} is not a JIT archive. Loading as a state dict instead" ) jit = False state_dict = torch.load(model_path, map_location="cpu")
if not jit: try: model = build_model_from_openai_state_dict( state_dict or model.state_dict(), model_cfg, enable_fusion, fusion_type ).to(device) except KeyError: sd = {k[7:]: v for k, v in state_dict["state_dict"].items()} model = build_model_from_openai_state_dict( sd, model_cfg, enable_fusion, fusion_type ).to(device)
if str(device) == "cpu": model.float() return model
# patch the device names device_holder = torch.jit.trace( lambda: torch.ones([]).to(torch.device(device)), example_inputs=[] ) device_node = [ n for n in device_holder.graph.findAllNodes("prim::Constant") if "Device" in repr(n) ][-1]
def patch_device(module): try: graphs = [module.graph] if hasattr(module, "graph") else [] except RuntimeError: graphs = []
if hasattr(module, "forward1"): graphs.append(module.forward1.graph)
for graph in graphs: for node in graph.findAllNodes("prim::Constant"): if "value" in node.attributeNames() and str(node["value"]).startswith( "cuda" ): node.copyAttributes(device_node)
model.apply(patch_device) patch_device(model.encode_audio) patch_device(model.encode_text)
# patch dtype to float32 on CPU if str(device) == "cpu": float_holder = torch.jit.trace( lambda: torch.ones([]).float(), example_inputs=[] ) float_input = list(float_holder.graph.findNode("aten::to").inputs())[1] float_node = float_input.node()
def patch_float(module): try: graphs = [module.graph] if hasattr(module, "graph") else [] except RuntimeError: graphs = []
if hasattr(module, "forward1"): graphs.append(module.forward1.graph)
for graph in graphs: for node in graph.findAllNodes("aten::to"): inputs = list(node.inputs()) for i in [ 1, 2, ]: # dtype can be the second or third argument to aten::to() if inputs[i].node()["value"] == 5: inputs[i].node().copyAttributes(float_node)
model.apply(patch_float) patch_float(model.encode_audio) patch_float(model.encode_text) model.float()
model.audio_branch.audio_length = model.audio_cfg.audio_length return model123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704# PANNs: Large-Scale Pretrained Audio Neural Networks for Audio Pattern Recognition# Reference from https://github.com/qiuqiangkong/audioset_tagging_cnn# Some layers are re-designed for CLAPimport os
os.environ["NUMBA_CACHE_DIR"] = "/tmp/"
import torchimport torch.nn as nnimport torch.nn.functional as Ffrom torchlibrosa.stft import Spectrogram, LogmelFilterBankfrom torchlibrosa.augmentation import SpecAugmentation
from .utils import do_mixup, interpolate, pad_framewise_outputfrom .feature_fusion import iAFF, AFF, DAF
def init_layer(layer): """Initialize a Linear or Convolutional layer.""" nn.init.xavier_uniform_(layer.weight)
if hasattr(layer, "bias"): if layer.bias is not None: layer.bias.data.fill_(0.0)
def init_bn(bn): """Initialize a Batchnorm layer.""" bn.bias.data.fill_(0.0) bn.weight.data.fill_(1.0)
class ConvBlock(nn.Module): def __init__(self, in_channels, out_channels):
super(ConvBlock, self).__init__()
self.conv1 = nn.Conv2d( in_channels=in_channels, out_channels=out_channels, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False, )
self.conv2 = nn.Conv2d( in_channels=out_channels, out_channels=out_channels, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False, )
self.bn1 = nn.BatchNorm2d(out_channels) self.bn2 = nn.BatchNorm2d(out_channels)
self.init_weight()
def init_weight(self): init_layer(self.conv1) init_layer(self.conv2) init_bn(self.bn1) init_bn(self.bn2)
def forward(self, input, pool_size=(2, 2), pool_type="avg"):
x = input x = F.relu_(self.bn1(self.conv1(x))) x = F.relu_(self.bn2(self.conv2(x))) if pool_type == "max": x = F.max_pool2d(x, kernel_size=pool_size) elif pool_type == "avg": x = F.avg_pool2d(x, kernel_size=pool_size) elif pool_type == "avg+max": x1 = F.avg_pool2d(x, kernel_size=pool_size) x2 = F.max_pool2d(x, kernel_size=pool_size) x = x1 + x2 else: raise Exception("Incorrect argument!")
return x
class ConvBlock5x5(nn.Module): def __init__(self, in_channels, out_channels):
super(ConvBlock5x5, self).__init__()
self.conv1 = nn.Conv2d( in_channels=in_channels, out_channels=out_channels, kernel_size=(5, 5), stride=(1, 1), padding=(2, 2), bias=False, )
self.bn1 = nn.BatchNorm2d(out_channels)
self.init_weight()
def init_weight(self): init_layer(self.conv1) init_bn(self.bn1)
def forward(self, input, pool_size=(2, 2), pool_type="avg"):
x = input x = F.relu_(self.bn1(self.conv1(x))) if pool_type == "max": x = F.max_pool2d(x, kernel_size=pool_size) elif pool_type == "avg": x = F.avg_pool2d(x, kernel_size=pool_size) elif pool_type == "avg+max": x1 = F.avg_pool2d(x, kernel_size=pool_size) x2 = F.max_pool2d(x, kernel_size=pool_size) x = x1 + x2 else: raise Exception("Incorrect argument!")
return x
class AttBlock(nn.Module): def __init__(self, n_in, n_out, activation="linear", temperature=1.0): super(AttBlock, self).__init__()
self.activation = activation self.temperature = temperature self.att = nn.Conv1d( in_channels=n_in, out_channels=n_out, kernel_size=1, stride=1, padding=0, bias=True, ) self.cla = nn.Conv1d( in_channels=n_in, out_channels=n_out, kernel_size=1, stride=1, padding=0, bias=True, )
self.bn_att = nn.BatchNorm1d(n_out) self.init_weights()
def init_weights(self): init_layer(self.att) init_layer(self.cla) init_bn(self.bn_att)
def forward(self, x): # x: (n_samples, n_in, n_time) norm_att = torch.softmax(torch.clamp(self.att(x), -10, 10), dim=-1) cla = self.nonlinear_transform(self.cla(x)) x = torch.sum(norm_att * cla, dim=2) return x, norm_att, cla
def nonlinear_transform(self, x): if self.activation == "linear": return x elif self.activation == "sigmoid": return torch.sigmoid(x)
class Cnn14(nn.Module): def __init__( self, sample_rate, window_size, hop_size, mel_bins, fmin, fmax, classes_num, enable_fusion=False, fusion_type="None", ):
super(Cnn14, self).__init__()
window = "hann" center = True pad_mode = "reflect" ref = 1.0 amin = 1e-10 top_db = None
self.enable_fusion = enable_fusion self.fusion_type = fusion_type
# Spectrogram extractor self.spectrogram_extractor = Spectrogram( n_fft=window_size, hop_length=hop_size, win_length=window_size, window=window, center=center, pad_mode=pad_mode, freeze_parameters=True, )
# Logmel feature extractor self.logmel_extractor = LogmelFilterBank( sr=sample_rate, n_fft=window_size, n_mels=mel_bins, fmin=fmin, fmax=fmax, ref=ref, amin=amin, top_db=top_db, freeze_parameters=True, )
# Spec augmenter self.spec_augmenter = SpecAugmentation( time_drop_width=64, time_stripes_num=2, freq_drop_width=8, freq_stripes_num=2, )
self.bn0 = nn.BatchNorm2d(64)
if (self.enable_fusion) and (self.fusion_type == "channel_map"): self.conv_block1 = ConvBlock(in_channels=4, out_channels=64) else: self.conv_block1 = ConvBlock(in_channels=1, out_channels=64) self.conv_block2 = ConvBlock(in_channels=64, out_channels=128) self.conv_block3 = ConvBlock(in_channels=128, out_channels=256) self.conv_block4 = ConvBlock(in_channels=256, out_channels=512) self.conv_block5 = ConvBlock(in_channels=512, out_channels=1024) self.conv_block6 = ConvBlock(in_channels=1024, out_channels=2048)
self.fc1 = nn.Linear(2048, 2048, bias=True) self.fc_audioset = nn.Linear(2048, classes_num, bias=True)
if (self.enable_fusion) and ( self.fusion_type in ["daf_1d", "aff_1d", "iaff_1d"] ): self.mel_conv1d = nn.Sequential( nn.Conv1d(64, 64, kernel_size=5, stride=3, padding=2), nn.BatchNorm1d(64), # No Relu ) if self.fusion_type == "daf_1d": self.fusion_model = DAF() elif self.fusion_type == "aff_1d": self.fusion_model = AFF(channels=64, type="1D") elif self.fusion_type == "iaff_1d": self.fusion_model = iAFF(channels=64, type="1D")
if (self.enable_fusion) and ( self.fusion_type in ["daf_2d", "aff_2d", "iaff_2d"] ): self.mel_conv2d = nn.Sequential( nn.Conv2d(1, 64, kernel_size=(5, 5), stride=(6, 2), padding=(2, 2)), nn.BatchNorm2d(64), nn.ReLU(inplace=True), )
if self.fusion_type == "daf_2d": self.fusion_model = DAF() elif self.fusion_type == "aff_2d": self.fusion_model = AFF(channels=64, type="2D") elif self.fusion_type == "iaff_2d": self.fusion_model = iAFF(channels=64, type="2D") self.init_weight()
def init_weight(self): init_bn(self.bn0) init_layer(self.fc1) init_layer(self.fc_audioset)
def forward(self, input, mixup_lambda=None, device=None): """ Input: (batch_size, data_length)"""
if self.enable_fusion and input["longer"].sum() == 0: # if no audio is longer than 10s, then randomly select one audio to be longer input["longer"][torch.randint(0, input["longer"].shape[0], (1,))] = True
if not self.enable_fusion: x = self.spectrogram_extractor( input["waveform"].to(device=device, non_blocking=True) ) # (batch_size, 1, time_steps, freq_bins) x = self.logmel_extractor(x) # (batch_size, 1, time_steps, mel_bins)
x = x.transpose(1, 3) x = self.bn0(x) x = x.transpose(1, 3) else: longer_list = input["longer"].to(device=device, non_blocking=True) x = input["mel_fusion"].to(device=device, non_blocking=True) longer_list_idx = torch.where(longer_list)[0] x = x.transpose(1, 3) x = self.bn0(x) x = x.transpose(1, 3) if self.fusion_type in ["daf_1d", "aff_1d", "iaff_1d"]: new_x = x[:, 0:1, :, :].clone().contiguous() # local processing if len(longer_list_idx) > 0: fusion_x_local = x[longer_list_idx, 1:, :, :].clone().contiguous() FB, FC, FT, FF = fusion_x_local.size() fusion_x_local = fusion_x_local.view(FB * FC, FT, FF) fusion_x_local = torch.permute( fusion_x_local, (0, 2, 1) ).contiguous() fusion_x_local = self.mel_conv1d(fusion_x_local) fusion_x_local = fusion_x_local.view( FB, FC, FF, fusion_x_local.size(-1) ) fusion_x_local = ( torch.permute(fusion_x_local, (0, 2, 1, 3)) .contiguous() .flatten(2) ) if fusion_x_local.size(-1) < FT: fusion_x_local = torch.cat( [ fusion_x_local, torch.zeros( (FB, FF, FT - fusion_x_local.size(-1)), device=device, ), ], dim=-1, ) else: fusion_x_local = fusion_x_local[:, :, :FT] # 1D fusion new_x = new_x.squeeze(1).permute((0, 2, 1)).contiguous() new_x[longer_list_idx] = self.fusion_model( new_x[longer_list_idx], fusion_x_local ) x = new_x.permute((0, 2, 1)).contiguous()[:, None, :, :] else: x = new_x elif self.fusion_type in ["daf_2d", "aff_2d", "iaff_2d", "channel_map"]: x = x # no change
if self.training: x = self.spec_augmenter(x) # Mixup on spectrogram if self.training and mixup_lambda is not None: x = do_mixup(x, mixup_lambda) if (self.enable_fusion) and ( self.fusion_type in ["daf_2d", "aff_2d", "iaff_2d"] ): global_x = x[:, 0:1, :, :]
# global processing B, C, H, W = global_x.shape global_x = self.conv_block1(global_x, pool_size=(2, 2), pool_type="avg") if len(longer_list_idx) > 0: local_x = x[longer_list_idx, 1:, :, :].contiguous() TH = global_x.size(-2) # local processing B, C, H, W = local_x.shape local_x = local_x.view(B * C, 1, H, W) local_x = self.mel_conv2d(local_x) local_x = local_x.view( B, C, local_x.size(1), local_x.size(2), local_x.size(3) ) local_x = local_x.permute((0, 2, 1, 3, 4)).contiguous().flatten(2, 3) TB, TC, _, TW = local_x.size() if local_x.size(-2) < TH: local_x = torch.cat( [ local_x, torch.zeros( (TB, TC, TH - local_x.size(-2), TW), device=global_x.device, ), ], dim=-2, ) else: local_x = local_x[:, :, :TH, :]
global_x[longer_list_idx] = self.fusion_model( global_x[longer_list_idx], local_x ) x = global_x else: x = self.conv_block1(x, pool_size=(2, 2), pool_type="avg")
x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block2(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block3(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block4(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block5(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block6(x, pool_size=(1, 1), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = torch.mean(x, dim=3)
latent_x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1) latent_x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1) latent_x = latent_x1 + latent_x2 latent_x = latent_x.transpose(1, 2) latent_x = F.relu_(self.fc1(latent_x)) latent_output = interpolate(latent_x, 32)
(x1, _) = torch.max(x, dim=2) x2 = torch.mean(x, dim=2) x = x1 + x2 x = F.dropout(x, p=0.5, training=self.training) x = F.relu_(self.fc1(x)) embedding = F.dropout(x, p=0.5, training=self.training) clipwise_output = torch.sigmoid(self.fc_audioset(x))
output_dict = { "clipwise_output": clipwise_output, "embedding": embedding, "fine_grained_embedding": latent_output, } return output_dict
class Cnn6(nn.Module): def __init__( self, sample_rate, window_size, hop_size, mel_bins, fmin, fmax, classes_num, enable_fusion=False, fusion_type="None", ):
super(Cnn6, self).__init__()
window = "hann" center = True pad_mode = "reflect" ref = 1.0 amin = 1e-10 top_db = None
self.enable_fusion = enable_fusion self.fusion_type = fusion_type
# Spectrogram extractor self.spectrogram_extractor = Spectrogram( n_fft=window_size, hop_length=hop_size, win_length=window_size, window=window, center=center, pad_mode=pad_mode, freeze_parameters=True, )
# Logmel feature extractor self.logmel_extractor = LogmelFilterBank( sr=sample_rate, n_fft=window_size, n_mels=mel_bins, fmin=fmin, fmax=fmax, ref=ref, amin=amin, top_db=top_db, freeze_parameters=True, )
# Spec augmenter self.spec_augmenter = SpecAugmentation( time_drop_width=64, time_stripes_num=2, freq_drop_width=8, freq_stripes_num=2, )
self.bn0 = nn.BatchNorm2d(64)
self.conv_block1 = ConvBlock5x5(in_channels=1, out_channels=64) self.conv_block2 = ConvBlock5x5(in_channels=64, out_channels=128) self.conv_block3 = ConvBlock5x5(in_channels=128, out_channels=256) self.conv_block4 = ConvBlock5x5(in_channels=256, out_channels=512)
self.fc1 = nn.Linear(512, 512, bias=True) self.fc_audioset = nn.Linear(512, classes_num, bias=True)
self.init_weight()
def init_weight(self): init_bn(self.bn0) init_layer(self.fc1) init_layer(self.fc_audioset)
def forward(self, input, mixup_lambda=None, device=None): """ Input: (batch_size, data_length)"""
x = self.spectrogram_extractor(input) # (batch_size, 1, time_steps, freq_bins) x = self.logmel_extractor(x) # (batch_size, 1, time_steps, mel_bins)
x = x.transpose(1, 3) x = self.bn0(x) x = x.transpose(1, 3)
if self.training: x = self.spec_augmenter(x)
# Mixup on spectrogram if self.training and mixup_lambda is not None: x = do_mixup(x, mixup_lambda)
x = self.conv_block1(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block2(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block3(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block4(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = torch.mean(x, dim=3)
latent_x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1) latent_x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1) latent_x = latent_x1 + latent_x2 latent_x = latent_x.transpose(1, 2) latent_x = F.relu_(self.fc1(latent_x)) latent_output = interpolate(latent_x, 16)
(x1, _) = torch.max(x, dim=2) x2 = torch.mean(x, dim=2) x = x1 + x2 x = F.dropout(x, p=0.5, training=self.training) x = F.relu_(self.fc1(x)) embedding = F.dropout(x, p=0.5, training=self.training) clipwise_output = torch.sigmoid(self.fc_audioset(x))
output_dict = { "clipwise_output": clipwise_output, "embedding": embedding, "fine_grained_embedding": latent_output, }
return output_dict
class Cnn10(nn.Module): def __init__( self, sample_rate, window_size, hop_size, mel_bins, fmin, fmax, classes_num, enable_fusion=False, fusion_type="None", ):
super(Cnn10, self).__init__()
window = "hann" center = True pad_mode = "reflect" ref = 1.0 amin = 1e-10 top_db = None
self.enable_fusion = enable_fusion self.fusion_type = fusion_type
# Spectrogram extractor self.spectrogram_extractor = Spectrogram( n_fft=window_size, hop_length=hop_size, win_length=window_size, window=window, center=center, pad_mode=pad_mode, freeze_parameters=True, )
# Logmel feature extractor self.logmel_extractor = LogmelFilterBank( sr=sample_rate, n_fft=window_size, n_mels=mel_bins, fmin=fmin, fmax=fmax, ref=ref, amin=amin, top_db=top_db, freeze_parameters=True, )
# Spec augmenter self.spec_augmenter = SpecAugmentation( time_drop_width=64, time_stripes_num=2, freq_drop_width=8, freq_stripes_num=2, )
self.bn0 = nn.BatchNorm2d(64)
self.conv_block1 = ConvBlock(in_channels=1, out_channels=64) self.conv_block2 = ConvBlock(in_channels=64, out_channels=128) self.conv_block3 = ConvBlock(in_channels=128, out_channels=256) self.conv_block4 = ConvBlock(in_channels=256, out_channels=512) self.conv_block5 = ConvBlock(in_channels=512, out_channels=1024)
self.fc1 = nn.Linear(1024, 1024, bias=True) self.fc_audioset = nn.Linear(1024, classes_num, bias=True)
self.init_weight()
def init_weight(self): init_bn(self.bn0) init_layer(self.fc1) init_layer(self.fc_audioset)
def forward(self, input, mixup_lambda=None, device=None): """ Input: (batch_size, data_length)"""
x = self.spectrogram_extractor(input) # (batch_size, 1, time_steps, freq_bins) x = self.logmel_extractor(x) # (batch_size, 1, time_steps, mel_bins)
x = x.transpose(1, 3) x = self.bn0(x) x = x.transpose(1, 3)
if self.training: x = self.spec_augmenter(x)
# Mixup on spectrogram if self.training and mixup_lambda is not None: x = do_mixup(x, mixup_lambda)
x = self.conv_block1(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block2(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block3(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block4(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = self.conv_block5(x, pool_size=(2, 2), pool_type="avg") x = F.dropout(x, p=0.2, training=self.training) x = torch.mean(x, dim=3)
latent_x1 = F.max_pool1d(x, kernel_size=3, stride=1, padding=1) latent_x2 = F.avg_pool1d(x, kernel_size=3, stride=1, padding=1) latent_x = latent_x1 + latent_x2 latent_x = latent_x.transpose(1, 2) latent_x = F.relu_(self.fc1(latent_x)) latent_output = interpolate(latent_x, 32)
(x1, _) = torch.max(x, dim=2) x2 = torch.mean(x, dim=2) x = x1 + x2 x = F.dropout(x, p=0.5, training=self.training) x = F.relu_(self.fc1(x)) embedding = F.dropout(x, p=0.5, training=self.training) clipwise_output = torch.sigmoid(self.fc_audioset(x))
output_dict = { "clipwise_output": clipwise_output, "embedding": embedding, "fine_grained_embedding": latent_output, }
return output_dict
def create_pann_model(audio_cfg, enable_fusion=False, fusion_type="None"): try: ModelProto = eval(audio_cfg.model_name) model = ModelProto( sample_rate=audio_cfg.sample_rate, window_size=audio_cfg.window_size, hop_size=audio_cfg.hop_size, mel_bins=audio_cfg.mel_bins, fmin=audio_cfg.fmin, fmax=audio_cfg.fmax, classes_num=audio_cfg.class_num, enable_fusion=enable_fusion, fusion_type=fusion_type, ) return model except: raise RuntimeError( f"Import Model for {audio_cfg.model_name} not found, or the audio cfg parameters are not enough." ).gitattributes+1
.gitignoreAdded+2
Examples/Beethoven.wavAdded+3
Examples/Beethoven_arcade.wavAdded+3
Examples/Beethoven_piano.wavAdded+3
Examples/Cat.wavAdded+3
Examples/Cat_dog.wavAdded+3
Examples/ModalJazz.wavAdded+3
Examples/ModalJazz_banjo.wavAdded+3
app.pyAdded+271
audioldm/__init__.pyAdded+8
audioldm/__main__.pyAdded+183
audioldm/audio/__init__.pyAdded+2
audioldm/audio/audio_processing.pyAdded+100
audioldm/audio/stft.pyAdded+180
audioldm/audio/tools.pyAdded+85
audioldm/clap/__init__.pyAdded
audioldm/clap/encoders.pyAdded+171
audioldm/clap/open_clip/__init__.pyAdded+25
audioldm/clap/open_clip/bert.pyAdded+40
audioldm/clap/open_clip/bpe_simple_vocab_16e6.txt.gzAdded+3
audioldm/clap/open_clip/factory.pyAdded+279
audioldm/clap/open_clip/feature_fusion.pyAdded+192
audioldm/clap/open_clip/htsat.pyAdded+1308
audioldm/clap/open_clip/linear_probe.pyAdded+66
audioldm/clap/open_clip/loss.pyAdded+398
audioldm/clap/open_clip/model.pyAdded+936
audioldm/clap/open_clip/model_configs/HTSAT-base.jsonAdded+23
audioldm/clap/open_clip/model_configs/HTSAT-large.jsonAdded+23
audioldm/clap/open_clip/model_configs/HTSAT-tiny-win-1536.jsonAdded+23
audioldm/clap/open_clip/model_configs/HTSAT-tiny.jsonAdded+23
audioldm/clap/open_clip/model_configs/PANN-10.jsonAdded+23
audioldm/clap/open_clip/model_configs/PANN-14-fmax-18k.jsonAdded+23
audioldm/clap/open_clip/model_configs/PANN-14-fmax-8k-20s.jsonAdded+23
audioldm/clap/open_clip/model_configs/PANN-14-tiny-transformer.jsonAdded+23
audioldm/clap/open_clip/model_configs/PANN-14-win-1536.jsonAdded+23
audioldm/clap/open_clip/model_configs/PANN-14.jsonAdded+23
audioldm/clap/open_clip/model_configs/PANN-6.jsonAdded+23
audioldm/clap/open_clip/model_configs/RN101-quickgelu.jsonAdded+22
audioldm/clap/open_clip/model_configs/RN101.jsonAdded+21
audioldm/clap/open_clip/model_configs/RN50-quickgelu.jsonAdded+22
audioldm/clap/open_clip/model_configs/RN50.jsonAdded+21
audioldm/clap/open_clip/model_configs/RN50x16.jsonAdded+21
audioldm/clap/open_clip/model_configs/RN50x4.jsonAdded+21
audioldm/clap/open_clip/model_configs/ViT-B-16.jsonAdded+16
audioldm/clap/open_clip/model_configs/ViT-B-32-quickgelu.jsonAdded+17
audioldm/clap/open_clip/model_configs/ViT-B-32.jsonAdded+16
audioldm/clap/open_clip/model_configs/ViT-L-14.jsonAdded+16
audioldm/clap/open_clip/openai.pyAdded+159
audioldm/clap/open_clip/pann_model.pyAdded+704