|
| 1 | +from typing import Literal |
| 2 | + |
| 3 | +from einops import rearrange |
| 4 | +from einops.layers.torch import Rearrange |
| 5 | +from torch import Tensor |
| 6 | +from torch.nn import Module, Sequential, LeakyReLU, MaxPool2d, Linear |
| 7 | +from torchvision.models.vision_transformer import Encoder as ViTEncoder |
| 8 | + |
| 9 | +from ..utils import Conv2d |
| 10 | + |
| 11 | + |
| 12 | +class CNNAudioEncoder(Module): |
| 13 | + """ |
| 14 | + Audio encoder (E_a): Process log mel spectrogram to extract features. |
| 15 | + Input: |
| 16 | + A': (B, F_m, T_a) |
| 17 | + Output: |
| 18 | + E_a: (B, C_f, T) |
| 19 | + """ |
| 20 | + |
| 21 | + def __init__(self, n_features=(32, 64, 64)): |
| 22 | + super().__init__() |
| 23 | + |
| 24 | + n_dim0, n_dim1, n_dim2 = n_features |
| 25 | + |
| 26 | + # (B, 64, 2048) -> (B, 1, 64, 2048) -> (B, 32, 32, 1024) |
| 27 | + self.block0 = Sequential( |
| 28 | + Rearrange("b c t -> b 1 c t"), |
| 29 | + Conv2d(1, n_dim0, kernel_size=3, stride=1, padding=1, build_activation=LeakyReLU), |
| 30 | + MaxPool2d(2) |
| 31 | + ) |
| 32 | + |
| 33 | + # (B, 32, 32, 1024) -> (B, 64, 16, 512) |
| 34 | + self.block1 = Sequential( |
| 35 | + Conv2d(n_dim0, n_dim1, kernel_size=3, stride=1, padding=1, build_activation=LeakyReLU), |
| 36 | + Conv2d(n_dim1, n_dim1, kernel_size=3, stride=1, padding=1, build_activation=LeakyReLU), |
| 37 | + MaxPool2d(2) |
| 38 | + ) |
| 39 | + |
| 40 | + # (B, 64, 16, 512) -> (B, 64, 4, 512) -> (B, 256, 512) |
| 41 | + self.block2 = Sequential( |
| 42 | + Conv2d(n_dim1, n_dim2, kernel_size=(2, 1), stride=1, padding=(1, 0), build_activation=LeakyReLU), |
| 43 | + MaxPool2d((2, 1)), |
| 44 | + Conv2d(n_dim2, n_dim2, kernel_size=(3, 1), stride=1, padding=(1, 0), build_activation=LeakyReLU), |
| 45 | + MaxPool2d((2, 1)), |
| 46 | + Rearrange("b f c t -> b (f c) t") |
| 47 | + ) |
| 48 | + |
| 49 | + def forward(self, audio: Tensor) -> Tensor: |
| 50 | + x = self.block0(audio) |
| 51 | + x = self.block1(x) |
| 52 | + x = self.block2(x) |
| 53 | + return x |
| 54 | + |
| 55 | + |
| 56 | +class SelfAttentionAudioEncoder(Module): |
| 57 | + |
| 58 | + def __init__(self, block_type: Literal["vit_t", "vit_s", "vit_b"], a_cla_feature_in: int = 256, temporal_size: int = 512): |
| 59 | + super().__init__() |
| 60 | + # The ViT configurations are from: |
| 61 | + # https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/vision_transformer.py |
| 62 | + if block_type == "vit_t": |
| 63 | + self.n_features = 192 |
| 64 | + self.block = ViTEncoder( |
| 65 | + seq_length=temporal_size, |
| 66 | + num_layers=12, |
| 67 | + num_heads=3, |
| 68 | + hidden_dim=self.n_features, |
| 69 | + mlp_dim=self.n_features * 4, |
| 70 | + dropout=0., |
| 71 | + attention_dropout=0. |
| 72 | + ) |
| 73 | + elif block_type == "vit_s": |
| 74 | + self.n_features = 384 |
| 75 | + self.block = ViTEncoder( |
| 76 | + seq_length=temporal_size, |
| 77 | + num_layers=12, |
| 78 | + num_heads=6, |
| 79 | + hidden_dim=self.n_features, |
| 80 | + mlp_dim=self.n_features * 4, |
| 81 | + dropout=0., |
| 82 | + attention_dropout=0. |
| 83 | + ) |
| 84 | + elif block_type == "vit_b": |
| 85 | + self.n_features = 768 |
| 86 | + self.block = ViTEncoder( |
| 87 | + seq_length=temporal_size, |
| 88 | + num_layers=12, |
| 89 | + num_heads=12, |
| 90 | + hidden_dim=self.n_features, |
| 91 | + mlp_dim=self.n_features * 4, |
| 92 | + dropout=0., |
| 93 | + attention_dropout=0. |
| 94 | + ) |
| 95 | + else: |
| 96 | + raise ValueError(f"Unknown block type: {block_type}") |
| 97 | + |
| 98 | + self.input_proj = Conv2d(1, self.n_features, kernel_size=(64, 4), stride=(64, 4)) |
| 99 | + self.output_proj = Linear(self.n_features, a_cla_feature_in) |
| 100 | + |
| 101 | + def forward(self, audio: Tensor) -> Tensor: |
| 102 | + x = audio.unsqueeze(1) # (B, 64, 2048) -> (B, 1, 64, 2048) |
| 103 | + x = self.input_proj(x) # (B, 1, 64, 2048) -> (B, feat, 1, 512) |
| 104 | + x = rearrange(x, "b f 1 t -> b t f") # (B, feat, 1, 512) -> (B, 512, feat) |
| 105 | + x = self.block(x) |
| 106 | + x = self.output_proj(x) # (B, 512, feat) -> (B, 512, 256) |
| 107 | + x = x.permute(0, 2, 1) # (B, 512, 256) -> (B, 256, 512) |
| 108 | + return x |
| 109 | + |
| 110 | + |
| 111 | +class AudioFeatureProjection(Module): |
| 112 | + |
| 113 | + def __init__(self, input_feature_dim: int, a_cla_feature_in: int = 256): |
| 114 | + super().__init__() |
| 115 | + self.proj = Linear(input_feature_dim, a_cla_feature_in) |
| 116 | + |
| 117 | + def forward(self, x: Tensor) -> Tensor: |
| 118 | + x = self.proj(x) |
| 119 | + return x.permute(0, 2, 1) |
| 120 | + |
| 121 | + |
| 122 | +def get_audio_encoder(a_cla_feature_in, temporal_size, a_encoder, ae_features): |
| 123 | + if a_encoder == "cnn": |
| 124 | + audio_encoder = CNNAudioEncoder(n_features=ae_features) |
| 125 | + elif a_encoder == "vit_t": |
| 126 | + audio_encoder = SelfAttentionAudioEncoder(block_type="vit_t", a_cla_feature_in=a_cla_feature_in, temporal_size=temporal_size) |
| 127 | + elif a_encoder == "vit_s": |
| 128 | + audio_encoder = SelfAttentionAudioEncoder(block_type="vit_s", a_cla_feature_in=a_cla_feature_in, temporal_size=temporal_size) |
| 129 | + elif a_encoder == "vit_b": |
| 130 | + audio_encoder = SelfAttentionAudioEncoder(block_type="vit_b", a_cla_feature_in=a_cla_feature_in, temporal_size=temporal_size) |
| 131 | + elif a_encoder == "wav2vec2": |
| 132 | + audio_encoder = AudioFeatureProjection(input_feature_dim=1536, a_cla_feature_in=a_cla_feature_in) |
| 133 | + elif a_encoder == "trillsson3": |
| 134 | + audio_encoder = AudioFeatureProjection(input_feature_dim=1280, a_cla_feature_in=a_cla_feature_in) |
| 135 | + else: |
| 136 | + raise ValueError(f"Invalid audio encoder: {a_encoder}") |
| 137 | + return audio_encoder |
0 commit comments