main project
This commit is contained in:
@@ -0,0 +1 @@
|
||||
from .models.factory import create_model_from_config, create_model_from_config_path
|
||||
@@ -0,0 +1,122 @@
|
||||
{
|
||||
"model_type": "autoencoder",
|
||||
"sample_size": 65536,
|
||||
"sample_rate": 44100,
|
||||
"audio_channels": 2,
|
||||
"model": {
|
||||
"encoder": {
|
||||
"type": "oobleck",
|
||||
"config": {
|
||||
"in_channels": 2,
|
||||
"channels": 128,
|
||||
"c_mults": [1, 2, 4, 8, 16],
|
||||
"strides": [2, 4, 4, 8, 8],
|
||||
"latent_dim": 128,
|
||||
"use_snake": true
|
||||
}
|
||||
},
|
||||
"decoder": {
|
||||
"type": "oobleck",
|
||||
"config": {
|
||||
"out_channels": 2,
|
||||
"channels": 128,
|
||||
"c_mults": [1, 2, 4, 8, 16],
|
||||
"strides": [2, 4, 4, 8, 8],
|
||||
"latent_dim": 64,
|
||||
"use_snake": true,
|
||||
"final_tanh": false
|
||||
}
|
||||
},
|
||||
"bottleneck": {
|
||||
"type": "vae"
|
||||
},
|
||||
"latent_dim": 64,
|
||||
"downsampling_ratio": 2048,
|
||||
"io_channels": 2
|
||||
},
|
||||
"training": {
|
||||
"learning_rate": 1.5e-4,
|
||||
"warmup_steps": 0,
|
||||
"use_ema": true,
|
||||
"optimizer_configs": {
|
||||
"autoencoder": {
|
||||
"optimizer": {
|
||||
"type": "AdamW",
|
||||
"config": {
|
||||
"betas": [0.8, 0.99],
|
||||
"lr": 1.5e-4,
|
||||
"weight_decay": 1e-3
|
||||
}
|
||||
},
|
||||
"scheduler": {
|
||||
"type": "InverseLR",
|
||||
"config": {
|
||||
"inv_gamma": 200000,
|
||||
"power": 0.5,
|
||||
"warmup": 0.999
|
||||
}
|
||||
}
|
||||
},
|
||||
"discriminator": {
|
||||
"optimizer": {
|
||||
"type": "AdamW",
|
||||
"config": {
|
||||
"betas": [0.8, 0.99],
|
||||
"lr": 3e-4,
|
||||
"weight_decay": 1e-3
|
||||
}
|
||||
},
|
||||
"scheduler": {
|
||||
"type": "InverseLR",
|
||||
"config": {
|
||||
"inv_gamma": 200000,
|
||||
"power": 0.5,
|
||||
"warmup": 0.999
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"loss_configs": {
|
||||
"discriminator": {
|
||||
"type": "encodec",
|
||||
"config": {
|
||||
"filters": 64,
|
||||
"n_ffts": [2048, 1024, 512, 256, 128],
|
||||
"hop_lengths": [512, 256, 128, 64, 32],
|
||||
"win_lengths": [2048, 1024, 512, 256, 128]
|
||||
},
|
||||
"weights": {
|
||||
"adversarial": 0.1,
|
||||
"feature_matching": 5.0
|
||||
}
|
||||
},
|
||||
"spectral": {
|
||||
"type": "mrstft",
|
||||
"config": {
|
||||
"fft_sizes": [2048, 1024, 512, 256, 128, 64, 32],
|
||||
"hop_sizes": [512, 256, 128, 64, 32, 16, 8],
|
||||
"win_lengths": [2048, 1024, 512, 256, 128, 64, 32],
|
||||
"perceptual_weighting": true
|
||||
},
|
||||
"weights": {
|
||||
"mrstft": 1.0
|
||||
}
|
||||
},
|
||||
"time": {
|
||||
"type": "l1",
|
||||
"weights": {
|
||||
"l1": 0.0
|
||||
}
|
||||
},
|
||||
"bottleneck": {
|
||||
"type": "kl",
|
||||
"weights": {
|
||||
"kl": 1e-4
|
||||
}
|
||||
}
|
||||
},
|
||||
"demo": {
|
||||
"demo_every": 10000
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
{
|
||||
"model_type": "mm_diffusion_cond",
|
||||
"sample_size": 397312,
|
||||
"sample_rate": 44100,
|
||||
"audio_channels": 2,
|
||||
"model": {
|
||||
"pretransform": {
|
||||
"type": "autoencoder",
|
||||
"iterate_batch": true,
|
||||
"config": {
|
||||
"encoder": {
|
||||
"type": "oobleck",
|
||||
"config": {
|
||||
"in_channels": 2,
|
||||
"channels": 128,
|
||||
"c_mults": [1, 2, 4, 8, 16],
|
||||
"strides": [2, 4, 4, 8, 8],
|
||||
"latent_dim": 128,
|
||||
"use_snake": true
|
||||
}
|
||||
},
|
||||
"decoder": {
|
||||
"type": "oobleck",
|
||||
"config": {
|
||||
"out_channels": 2,
|
||||
"channels": 128,
|
||||
"c_mults": [1, 2, 4, 8, 16],
|
||||
"strides": [2, 4, 4, 8, 8],
|
||||
"latent_dim": 64,
|
||||
"use_snake": true,
|
||||
"final_tanh": false
|
||||
}
|
||||
},
|
||||
"bottleneck": {
|
||||
"type": "vae"
|
||||
},
|
||||
"latent_dim": 64,
|
||||
"downsampling_ratio": 2048,
|
||||
"io_channels": 2
|
||||
}
|
||||
},
|
||||
"conditioning": {
|
||||
"configs": [
|
||||
{
|
||||
"id": "metaclip_features",
|
||||
"type": "mm_unchang",
|
||||
"config": {
|
||||
"dim": 1024,
|
||||
"output_dim": 1024
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "metaclip_text_features",
|
||||
"type": "mm_unchang",
|
||||
"config": {
|
||||
"dim": 1024,
|
||||
"output_dim": 1024
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "sync_features",
|
||||
"type": "mm_unchang",
|
||||
"config": {
|
||||
"dim": 768,
|
||||
"output_dim": 768
|
||||
}
|
||||
},
|
||||
{
|
||||
"id": "t5_features",
|
||||
"type": "mm_unchang",
|
||||
"config": {
|
||||
"dim": 2048,
|
||||
"output_dim": 2048
|
||||
}
|
||||
}
|
||||
],
|
||||
"cond_dim": 768
|
||||
},
|
||||
"diffusion": {
|
||||
"mm_cond_ids": ["metaclip_features", "sync_features", "metaclip_text_features","t5_features"],
|
||||
"type": "mmdit",
|
||||
"diffusion_objective": "rectified_flow",
|
||||
"config": {
|
||||
"latent_dim":64,
|
||||
"clip_dim":1024,
|
||||
"sync_dim":768,
|
||||
"text_dim":2048,
|
||||
"hidden_dim":1024,
|
||||
"depth":21,
|
||||
"fused_depth":14,
|
||||
"num_heads":16,
|
||||
"latent_seq_len":194,
|
||||
"clip_seq_len":72,
|
||||
"sync_seq_len":216,
|
||||
"v2": true,
|
||||
"kernel_size": 3
|
||||
}
|
||||
},
|
||||
"io_channels": 64
|
||||
},
|
||||
"training": {
|
||||
"use_ema": true,
|
||||
"log_loss_info": false,
|
||||
"cfg_dropout_prob": 0.2,
|
||||
"pre_encoded": true,
|
||||
"timestep_sampler": "logit_normal",
|
||||
"optimizer_configs": {
|
||||
"diffusion": {
|
||||
"optimizer": {
|
||||
"type": "AdamW",
|
||||
"config": {
|
||||
"lr": 5e-5,
|
||||
"betas": [0.9, 0.95],
|
||||
"weight_decay": 1e-4,
|
||||
"eps": 1e-6
|
||||
}
|
||||
},
|
||||
"scheduler": {
|
||||
"type": "InverseLR",
|
||||
"config": {
|
||||
"inv_gamma": 1000000,
|
||||
"power": 0.5,
|
||||
"warmup": 0.99
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"demo": {
|
||||
"demo_every": 5000,
|
||||
"demo_steps": 24,
|
||||
"num_demos": 10,
|
||||
"demo_cond": [
|
||||
"dataset/vggsound/video_latents_t5_clip_npz/test/0Cu33yBwAPg_000060.npz",
|
||||
"dataset/vggsound/video_latents_t5_clip_npz/test/bmKtI808DsU_000009.npz",
|
||||
"dataset/vggsound/video_latents_t5_clip_npz/test/VC0c22cJTbM_000424.npz",
|
||||
"dataset/vggsound/video_latents_t5_clip_npz/test/F3gsbUTdc2U_000090.npz",
|
||||
"dataset/vggsound/video_latents_t5_clip_npz/test/WatvT8A8iug_000100.npz",
|
||||
"dataset/vggsound/video_latents_t5_clip_npz/test/0nvBTp-q7tU_000112.npz",
|
||||
"dataset/vggsound/video_latents_t5_clip_npz/test/3-PFuDkTM48_000080.npz",
|
||||
"dataset/vggsound/video_latents_t5_clip_npz/test/luSAuu-BoPs_000232.npz",
|
||||
"dataset/vggsound/video_latents_t5_clip_npz/test/__8UJxW0aOQ_000002.npz",
|
||||
"dataset/vggsound/video_latents_t5_clip_npz/test/_0m_YMpQayA_000168.npz"
|
||||
],
|
||||
"demo_cfg_scales": [5]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
{
|
||||
"dataset_type": "multimodal_dir",
|
||||
"video_datasets": [
|
||||
{
|
||||
"id": "vggsound",
|
||||
"path": "dataset/vggsound/video_latents_t5_clip_npz/train",
|
||||
"split_path": "dataset/vggsound/split_txt/train_cot.txt"
|
||||
}
|
||||
],
|
||||
"audio_datasets": [
|
||||
{
|
||||
"id": "audiostock",
|
||||
"path": "dataset/Laion-Audio-630k/audiostock_latents_npz",
|
||||
"split_path": "dataset/Laion-Audio-630k/split_txt/cot_audiostock_1.txt"
|
||||
},
|
||||
{
|
||||
"id": "freesound_no_overlap",
|
||||
"path": "dataset/Laion-Audio-630k/freesound_no_overlap_latents_npz",
|
||||
"split_path": "dataset/Laion-Audio-630k/split_txt/cot_freesound.txt"
|
||||
},
|
||||
{
|
||||
"id": "audioset_sl",
|
||||
"path": "dataset/wavcaps/audioset_sl_latents_npz",
|
||||
"split_path": "dataset/wavcaps/split_txt/cot_audio_sl_1.txt"
|
||||
},
|
||||
{
|
||||
"id": "audiocaps",
|
||||
"path": "dataset/1_audiocaps/audiocaps_latents_npz",
|
||||
"split_path": "dataset/1_audiocaps/split_txt/train_cot.txt"
|
||||
},
|
||||
{
|
||||
"id": "bbc",
|
||||
"path": "dataset/Laion-Audio-630k/bbc_latents_npz",
|
||||
"split_path": "dataset/Laion-Audio-630k/split_txt/cot_bbc_1.txt"
|
||||
}
|
||||
],
|
||||
"val_datasets": [
|
||||
{
|
||||
"id": "vggsound",
|
||||
"path": "dataset/vggsound/video_latents_t5_clip_npz/test",
|
||||
"split_path": "dataset/vggsound/split_txt/test_cot.txt"
|
||||
}
|
||||
],
|
||||
"test_datasets": [
|
||||
{
|
||||
"id": "vggsound",
|
||||
"path": "cot_coarse"
|
||||
}
|
||||
],
|
||||
"random_crop": true,
|
||||
"input_type": "prompt"
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
MIT License
|
||||
|
||||
Copyright (c) 2024 Vladimir Iashin
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
||||
@@ -0,0 +1 @@
|
||||
from data_utils.ext.synchformer.synchformer import Synchformer
|
||||
@@ -0,0 +1,84 @@
|
||||
TRAIN:
|
||||
ENABLE: True
|
||||
DATASET: Ssv2
|
||||
BATCH_SIZE: 32
|
||||
EVAL_PERIOD: 5
|
||||
CHECKPOINT_PERIOD: 5
|
||||
AUTO_RESUME: True
|
||||
CHECKPOINT_EPOCH_RESET: True
|
||||
CHECKPOINT_FILE_PATH: /checkpoint/fmetze/neurips_sota/40944587/checkpoints/checkpoint_epoch_00035.pyth
|
||||
DATA:
|
||||
NUM_FRAMES: 16
|
||||
SAMPLING_RATE: 4
|
||||
TRAIN_JITTER_SCALES: [256, 320]
|
||||
TRAIN_CROP_SIZE: 224
|
||||
TEST_CROP_SIZE: 224
|
||||
INPUT_CHANNEL_NUM: [3]
|
||||
MEAN: [0.5, 0.5, 0.5]
|
||||
STD: [0.5, 0.5, 0.5]
|
||||
PATH_TO_DATA_DIR: /private/home/mandelapatrick/slowfast/data/ssv2
|
||||
PATH_PREFIX: /datasets01/SomethingV2/092720/20bn-something-something-v2-frames
|
||||
INV_UNIFORM_SAMPLE: True
|
||||
RANDOM_FLIP: False
|
||||
REVERSE_INPUT_CHANNEL: True
|
||||
USE_RAND_AUGMENT: True
|
||||
RE_PROB: 0.0
|
||||
USE_REPEATED_AUG: False
|
||||
USE_RANDOM_RESIZE_CROPS: False
|
||||
COLORJITTER: False
|
||||
GRAYSCALE: False
|
||||
GAUSSIAN: False
|
||||
SOLVER:
|
||||
BASE_LR: 1e-4
|
||||
LR_POLICY: steps_with_relative_lrs
|
||||
LRS: [1, 0.1, 0.01]
|
||||
STEPS: [0, 20, 30]
|
||||
MAX_EPOCH: 35
|
||||
MOMENTUM: 0.9
|
||||
WEIGHT_DECAY: 5e-2
|
||||
WARMUP_EPOCHS: 0.0
|
||||
OPTIMIZING_METHOD: adamw
|
||||
USE_MIXED_PRECISION: True
|
||||
SMOOTHING: 0.2
|
||||
SLOWFAST:
|
||||
ALPHA: 8
|
||||
VIT:
|
||||
PATCH_SIZE: 16
|
||||
PATCH_SIZE_TEMP: 2
|
||||
CHANNELS: 3
|
||||
EMBED_DIM: 768
|
||||
DEPTH: 12
|
||||
NUM_HEADS: 12
|
||||
MLP_RATIO: 4
|
||||
QKV_BIAS: True
|
||||
VIDEO_INPUT: True
|
||||
TEMPORAL_RESOLUTION: 8
|
||||
USE_MLP: True
|
||||
DROP: 0.0
|
||||
POS_DROPOUT: 0.0
|
||||
DROP_PATH: 0.2
|
||||
IM_PRETRAINED: True
|
||||
HEAD_DROPOUT: 0.0
|
||||
HEAD_ACT: tanh
|
||||
PRETRAINED_WEIGHTS: vit_1k
|
||||
ATTN_LAYER: divided
|
||||
MODEL:
|
||||
NUM_CLASSES: 174
|
||||
ARCH: slow
|
||||
MODEL_NAME: VisionTransformer
|
||||
LOSS_FUNC: cross_entropy
|
||||
TEST:
|
||||
ENABLE: True
|
||||
DATASET: Ssv2
|
||||
BATCH_SIZE: 64
|
||||
NUM_ENSEMBLE_VIEWS: 1
|
||||
NUM_SPATIAL_CROPS: 3
|
||||
DATA_LOADER:
|
||||
NUM_WORKERS: 4
|
||||
PIN_MEMORY: True
|
||||
NUM_GPUS: 8
|
||||
NUM_SHARDS: 4
|
||||
RNG_SEED: 0
|
||||
OUTPUT_DIR: .
|
||||
TENSORBOARD:
|
||||
ENABLE: True
|
||||
@@ -0,0 +1,400 @@
|
||||
import logging
|
||||
from pathlib import Path
|
||||
|
||||
import einops
|
||||
import torch
|
||||
from omegaconf import OmegaConf
|
||||
from timm.layers import trunc_normal_
|
||||
from torch import nn
|
||||
|
||||
from data_utils.ext.synchformer.utils import check_if_file_exists_else_download
|
||||
from data_utils.ext.synchformer.video_model_builder import VisionTransformer
|
||||
|
||||
FILE2URL = {
|
||||
# cfg
|
||||
'motionformer_224_16x4.yaml':
|
||||
'https://raw.githubusercontent.com/facebookresearch/Motionformer/bf43d50/configs/SSV2/motionformer_224_16x4.yaml',
|
||||
'joint_224_16x4.yaml':
|
||||
'https://raw.githubusercontent.com/facebookresearch/Motionformer/bf43d50/configs/SSV2/joint_224_16x4.yaml',
|
||||
'divided_224_16x4.yaml':
|
||||
'https://raw.githubusercontent.com/facebookresearch/Motionformer/bf43d50/configs/SSV2/divided_224_16x4.yaml',
|
||||
# ckpt
|
||||
'ssv2_motionformer_224_16x4.pyth':
|
||||
'https://dl.fbaipublicfiles.com/motionformer/ssv2_motionformer_224_16x4.pyth',
|
||||
'ssv2_joint_224_16x4.pyth':
|
||||
'https://dl.fbaipublicfiles.com/motionformer/ssv2_joint_224_16x4.pyth',
|
||||
'ssv2_divided_224_16x4.pyth':
|
||||
'https://dl.fbaipublicfiles.com/motionformer/ssv2_divided_224_16x4.pyth',
|
||||
}
|
||||
|
||||
|
||||
class MotionFormer(VisionTransformer):
|
||||
''' This class serves three puposes:
|
||||
1. Renames the class to MotionFormer.
|
||||
2. Downloads the cfg from the original repo and patches it if needed.
|
||||
3. Takes care of feature extraction by redefining .forward()
|
||||
- if `extract_features=True` and `factorize_space_time=False`,
|
||||
the output is of shape (B, T, D) where T = 1 + (224 // 16) * (224 // 16) * 8
|
||||
- if `extract_features=True` and `factorize_space_time=True`, the output is of shape (B*S, D)
|
||||
and spatial and temporal transformer encoder layers are used.
|
||||
- if `extract_features=True` and `factorize_space_time=True` as well as `add_global_repr=True`
|
||||
the output is of shape (B, D) and spatial and temporal transformer encoder layers
|
||||
are used as well as the global representation is extracted from segments (extra pos emb
|
||||
is added).
|
||||
'''
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
extract_features: bool = False,
|
||||
ckpt_path: str = None,
|
||||
factorize_space_time: bool = None,
|
||||
agg_space_module: str = None,
|
||||
agg_time_module: str = None,
|
||||
add_global_repr: bool = True,
|
||||
agg_segments_module: str = None,
|
||||
max_segments: int = None,
|
||||
):
|
||||
self.extract_features = extract_features
|
||||
self.ckpt_path = ckpt_path
|
||||
self.factorize_space_time = factorize_space_time
|
||||
|
||||
if self.ckpt_path is not None:
|
||||
check_if_file_exists_else_download(self.ckpt_path, FILE2URL)
|
||||
ckpt = torch.load(self.ckpt_path, map_location='cpu')
|
||||
mformer_ckpt2cfg = {
|
||||
'ssv2_motionformer_224_16x4.pyth': 'motionformer_224_16x4.yaml',
|
||||
'ssv2_joint_224_16x4.pyth': 'joint_224_16x4.yaml',
|
||||
'ssv2_divided_224_16x4.pyth': 'divided_224_16x4.yaml',
|
||||
}
|
||||
# init from motionformer ckpt or from our Stage I ckpt
|
||||
# depending on whether the feat extractor was pre-trained on AVCLIPMoCo or not, we need to
|
||||
# load the state dict differently
|
||||
was_pt_on_avclip = self.ckpt_path.endswith(
|
||||
'.pt') # checks if it is a stage I ckpt (FIXME: a bit generic)
|
||||
if self.ckpt_path.endswith(tuple(mformer_ckpt2cfg.keys())):
|
||||
cfg_fname = mformer_ckpt2cfg[Path(self.ckpt_path).name]
|
||||
elif was_pt_on_avclip:
|
||||
# TODO: this is a hack, we should be able to get the cfg from the ckpt (earlier ckpt didn't have it)
|
||||
s1_cfg = ckpt.get('args', None) # Stage I cfg
|
||||
if s1_cfg is not None:
|
||||
s1_vfeat_extractor_ckpt_path = s1_cfg.model.params.vfeat_extractor.params.ckpt_path
|
||||
# if the stage I ckpt was initialized from a motionformer ckpt or train from scratch
|
||||
if s1_vfeat_extractor_ckpt_path is not None:
|
||||
cfg_fname = mformer_ckpt2cfg[Path(s1_vfeat_extractor_ckpt_path).name]
|
||||
else:
|
||||
cfg_fname = 'divided_224_16x4.yaml'
|
||||
else:
|
||||
cfg_fname = 'divided_224_16x4.yaml'
|
||||
else:
|
||||
raise ValueError(f'ckpt_path {self.ckpt_path} is not supported.')
|
||||
else:
|
||||
was_pt_on_avclip = False
|
||||
cfg_fname = 'divided_224_16x4.yaml'
|
||||
# logging.info(f'No ckpt_path provided, using {cfg_fname} config.')
|
||||
|
||||
if cfg_fname in ['motionformer_224_16x4.yaml', 'divided_224_16x4.yaml']:
|
||||
pos_emb_type = 'separate'
|
||||
elif cfg_fname == 'joint_224_16x4.yaml':
|
||||
pos_emb_type = 'joint'
|
||||
|
||||
self.mformer_cfg_path = Path(__file__).absolute().parent / cfg_fname
|
||||
|
||||
check_if_file_exists_else_download(self.mformer_cfg_path, FILE2URL)
|
||||
mformer_cfg = OmegaConf.load(self.mformer_cfg_path)
|
||||
logging.info(f'Loading MotionFormer config from {self.mformer_cfg_path.absolute()}')
|
||||
|
||||
# patch the cfg (from the default cfg defined in the repo `Motionformer/slowfast/config/defaults.py`)
|
||||
mformer_cfg.VIT.ATTN_DROPOUT = 0.0
|
||||
mformer_cfg.VIT.POS_EMBED = pos_emb_type
|
||||
mformer_cfg.VIT.USE_ORIGINAL_TRAJ_ATTN_CODE = True
|
||||
mformer_cfg.VIT.APPROX_ATTN_TYPE = 'none' # guessing
|
||||
mformer_cfg.VIT.APPROX_ATTN_DIM = 64 # from ckpt['cfg']
|
||||
|
||||
# finally init VisionTransformer with the cfg
|
||||
super().__init__(mformer_cfg)
|
||||
|
||||
# load the ckpt now if ckpt is provided and not from AVCLIPMoCo-pretrained ckpt
|
||||
if (self.ckpt_path is not None) and (not was_pt_on_avclip):
|
||||
_ckpt_load_status = self.load_state_dict(ckpt['model_state'], strict=False)
|
||||
if len(_ckpt_load_status.missing_keys) > 0 or len(
|
||||
_ckpt_load_status.unexpected_keys) > 0:
|
||||
logging.warning(f'Loading exact vfeat_extractor ckpt from {self.ckpt_path} failed.' \
|
||||
f'Missing keys: {_ckpt_load_status.missing_keys}, ' \
|
||||
f'Unexpected keys: {_ckpt_load_status.unexpected_keys}')
|
||||
else:
|
||||
logging.info(f'Loading vfeat_extractor ckpt from {self.ckpt_path} succeeded.')
|
||||
|
||||
if self.extract_features:
|
||||
assert isinstance(self.norm,
|
||||
nn.LayerNorm), 'early x[:, 1:, :] may not be safe for per-tr weights'
|
||||
# pre-logits are Sequential(nn.Linear(emb, emd), act) and `act` is tanh but see the logger
|
||||
self.pre_logits = nn.Identity()
|
||||
# we don't need the classification head (saving memory)
|
||||
self.head = nn.Identity()
|
||||
self.head_drop = nn.Identity()
|
||||
# avoiding code duplication (used only if agg_*_module is TransformerEncoderLayer)
|
||||
transf_enc_layer_kwargs = dict(
|
||||
d_model=self.embed_dim,
|
||||
nhead=self.num_heads,
|
||||
activation=nn.GELU(),
|
||||
batch_first=True,
|
||||
dim_feedforward=self.mlp_ratio * self.embed_dim,
|
||||
dropout=self.drop_rate,
|
||||
layer_norm_eps=1e-6,
|
||||
norm_first=True,
|
||||
)
|
||||
# define adapters if needed
|
||||
if self.factorize_space_time:
|
||||
if agg_space_module == 'TransformerEncoderLayer':
|
||||
self.spatial_attn_agg = SpatialTransformerEncoderLayer(
|
||||
**transf_enc_layer_kwargs)
|
||||
elif agg_space_module == 'AveragePooling':
|
||||
self.spatial_attn_agg = AveragePooling(avg_pattern='BS D t h w -> BS D t',
|
||||
then_permute_pattern='BS D t -> BS t D')
|
||||
if agg_time_module == 'TransformerEncoderLayer':
|
||||
self.temp_attn_agg = TemporalTransformerEncoderLayer(**transf_enc_layer_kwargs)
|
||||
elif agg_time_module == 'AveragePooling':
|
||||
self.temp_attn_agg = AveragePooling(avg_pattern='BS t D -> BS D')
|
||||
elif 'Identity' in agg_time_module:
|
||||
self.temp_attn_agg = nn.Identity()
|
||||
# define a global aggregation layer (aggregarate over segments)
|
||||
self.add_global_repr = add_global_repr
|
||||
if add_global_repr:
|
||||
if agg_segments_module == 'TransformerEncoderLayer':
|
||||
# we can reuse the same layer as for temporal factorization (B, dim_to_agg, D) -> (B, D)
|
||||
# we need to add pos emb (PE) because previously we added the same PE for each segment
|
||||
pos_max_len = max_segments if max_segments is not None else 16 # 16 = 10sec//0.64sec + 1
|
||||
self.global_attn_agg = TemporalTransformerEncoderLayer(
|
||||
add_pos_emb=True,
|
||||
pos_emb_drop=mformer_cfg.VIT.POS_DROPOUT,
|
||||
pos_max_len=pos_max_len,
|
||||
**transf_enc_layer_kwargs)
|
||||
elif agg_segments_module == 'AveragePooling':
|
||||
self.global_attn_agg = AveragePooling(avg_pattern='B S D -> B D')
|
||||
|
||||
if was_pt_on_avclip:
|
||||
# we need to filter out the state_dict of the AVCLIP model (has both A and V extractors)
|
||||
# and keep only the state_dict of the feat extractor
|
||||
ckpt_weights = dict()
|
||||
for k, v in ckpt['state_dict'].items():
|
||||
if k.startswith(('module.v_encoder.', 'v_encoder.')):
|
||||
k = k.replace('module.', '').replace('v_encoder.', '')
|
||||
ckpt_weights[k] = v
|
||||
_load_status = self.load_state_dict(ckpt_weights, strict=False)
|
||||
if len(_load_status.missing_keys) > 0 or len(_load_status.unexpected_keys) > 0:
|
||||
logging.warning(f'Loading exact vfeat_extractor ckpt from {self.ckpt_path} failed. \n' \
|
||||
f'Missing keys ({len(_load_status.missing_keys)}): ' \
|
||||
f'{_load_status.missing_keys}, \n' \
|
||||
f'Unexpected keys ({len(_load_status.unexpected_keys)}): ' \
|
||||
f'{_load_status.unexpected_keys} \n' \
|
||||
f'temp_attn_agg are expected to be missing if ckpt was pt contrastively.')
|
||||
else:
|
||||
logging.info(f'Loading vfeat_extractor ckpt from {self.ckpt_path} succeeded.')
|
||||
|
||||
# patch_embed is not used in MotionFormer, only patch_embed_3d, because cfg.VIT.PATCH_SIZE_TEMP > 1
|
||||
# but it used to calculate the number of patches, so we need to set keep it
|
||||
self.patch_embed.requires_grad_(False)
|
||||
|
||||
def forward(self, x):
|
||||
'''
|
||||
x is of shape (B, S, C, T, H, W) where S is the number of segments.
|
||||
'''
|
||||
# Batch, Segments, Channels, T=frames, Height, Width
|
||||
B, S, C, T, H, W = x.shape
|
||||
# Motionformer expects a tensor of shape (1, B, C, T, H, W).
|
||||
# The first dimension (1) is a dummy dimension to make the input tensor and won't be used:
|
||||
# see `video_model_builder.video_input`.
|
||||
# x = x.unsqueeze(0) # (1, B, S, C, T, H, W)
|
||||
|
||||
orig_shape = (B, S, C, T, H, W)
|
||||
x = x.view(B * S, C, T, H, W) # flatten batch and segments
|
||||
x = self.forward_segments(x, orig_shape=orig_shape)
|
||||
# unpack the segments (using rest dimensions to support different shapes e.g. (BS, D) or (BS, t, D))
|
||||
x = x.view(B, S, *x.shape[1:])
|
||||
# x is now of shape (B*S, D) or (B*S, t, D) if `self.temp_attn_agg` is `Identity`
|
||||
|
||||
return x # x is (B, S, ...)
|
||||
|
||||
def forward_segments(self, x, orig_shape: tuple) -> torch.Tensor:
|
||||
'''x is of shape (1, BS, C, T, H, W) where S is the number of segments.'''
|
||||
x, x_mask = self.forward_features(x)
|
||||
|
||||
assert self.extract_features
|
||||
|
||||
# (BS, T, D) where T = 1 + (224 // 16) * (224 // 16) * 8
|
||||
x = x[:,
|
||||
1:, :] # without the CLS token for efficiency (should be safe for LayerNorm and FC)
|
||||
x = self.norm(x)
|
||||
x = self.pre_logits(x)
|
||||
if self.factorize_space_time:
|
||||
x = self.restore_spatio_temp_dims(x, orig_shape) # (B*S, D, t, h, w) <- (B*S, t*h*w, D)
|
||||
|
||||
x = self.spatial_attn_agg(x, x_mask) # (B*S, t, D)
|
||||
x = self.temp_attn_agg(
|
||||
x) # (B*S, D) or (BS, t, D) if `self.temp_attn_agg` is `Identity`
|
||||
|
||||
return x
|
||||
|
||||
def restore_spatio_temp_dims(self, feats: torch.Tensor, orig_shape: tuple) -> torch.Tensor:
|
||||
'''
|
||||
feats are of shape (B*S, T, D) where T = 1 + (224 // 16) * (224 // 16) * 8
|
||||
Our goal is to make them of shape (B*S, t, h, w, D) where h, w are the spatial dimensions.
|
||||
From `self.patch_embed_3d`, it follows that we could reshape feats with:
|
||||
`feats.transpose(1, 2).view(B*S, D, t, h, w)`
|
||||
'''
|
||||
B, S, C, T, H, W = orig_shape
|
||||
D = self.embed_dim
|
||||
|
||||
# num patches in each dimension
|
||||
t = T // self.patch_embed_3d.z_block_size
|
||||
h = self.patch_embed_3d.height
|
||||
w = self.patch_embed_3d.width
|
||||
|
||||
feats = feats.permute(0, 2, 1) # (B*S, D, T)
|
||||
feats = feats.view(B * S, D, t, h, w) # (B*S, D, t, h, w)
|
||||
|
||||
return feats
|
||||
|
||||
|
||||
class BaseEncoderLayer(nn.TransformerEncoderLayer):
|
||||
'''
|
||||
This is a wrapper around nn.TransformerEncoderLayer that adds a CLS token
|
||||
to the sequence and outputs the CLS token's representation.
|
||||
This base class parents both SpatialEncoderLayer and TemporalEncoderLayer for the RGB stream
|
||||
and the FrequencyEncoderLayer and TemporalEncoderLayer for the audio stream stream.
|
||||
We also, optionally, add a positional embedding to the input sequence which
|
||||
allows to reuse it for global aggregation (of segments) for both streams.
|
||||
'''
|
||||
|
||||
def __init__(self,
|
||||
add_pos_emb: bool = False,
|
||||
pos_emb_drop: float = None,
|
||||
pos_max_len: int = None,
|
||||
*args_transformer_enc,
|
||||
**kwargs_transformer_enc):
|
||||
super().__init__(*args_transformer_enc, **kwargs_transformer_enc)
|
||||
self.cls_token = nn.Parameter(torch.zeros(1, 1, self.self_attn.embed_dim))
|
||||
trunc_normal_(self.cls_token, std=.02)
|
||||
|
||||
# add positional embedding
|
||||
self.add_pos_emb = add_pos_emb
|
||||
if add_pos_emb:
|
||||
self.pos_max_len = 1 + pos_max_len # +1 (for CLS)
|
||||
self.pos_emb = nn.Parameter(torch.zeros(1, self.pos_max_len, self.self_attn.embed_dim))
|
||||
self.pos_drop = nn.Dropout(pos_emb_drop)
|
||||
trunc_normal_(self.pos_emb, std=.02)
|
||||
|
||||
self.apply(self._init_weights)
|
||||
|
||||
def forward(self, x: torch.Tensor, x_mask: torch.Tensor = None):
|
||||
''' x is of shape (B, N, D); if provided x_mask is of shape (B, N)'''
|
||||
batch_dim = x.shape[0]
|
||||
|
||||
# add CLS token
|
||||
cls_tokens = self.cls_token.expand(batch_dim, -1, -1) # expanding to match batch dimension
|
||||
x = torch.cat((cls_tokens, x), dim=-2) # (batch_dim, 1+seq_len, D)
|
||||
if x_mask is not None:
|
||||
cls_mask = torch.ones((batch_dim, 1), dtype=torch.bool,
|
||||
device=x_mask.device) # 1=keep; 0=mask
|
||||
x_mask_w_cls = torch.cat((cls_mask, x_mask), dim=-1) # (batch_dim, 1+seq_len)
|
||||
B, N = x_mask_w_cls.shape
|
||||
# torch expects (N, N) or (B*num_heads, N, N) mask (sadness ahead); torch masks
|
||||
x_mask_w_cls = x_mask_w_cls.reshape(B, 1, 1, N)\
|
||||
.expand(-1, self.self_attn.num_heads, N, -1)\
|
||||
.reshape(B * self.self_attn.num_heads, N, N)
|
||||
assert x_mask_w_cls.dtype == x_mask_w_cls.bool().dtype, 'x_mask_w_cls.dtype != bool'
|
||||
x_mask_w_cls = ~x_mask_w_cls # invert mask (1=mask)
|
||||
else:
|
||||
x_mask_w_cls = None
|
||||
|
||||
# add positional embedding
|
||||
if self.add_pos_emb:
|
||||
seq_len = x.shape[
|
||||
1] # (don't even think about moving it before the CLS token concatenation)
|
||||
assert seq_len <= self.pos_max_len, f'Seq len ({seq_len}) > pos_max_len ({self.pos_max_len})'
|
||||
x = x + self.pos_emb[:, :seq_len, :]
|
||||
x = self.pos_drop(x)
|
||||
|
||||
# apply encoder layer (calls nn.TransformerEncoderLayer.forward);
|
||||
x = super().forward(src=x, src_mask=x_mask_w_cls) # (batch_dim, 1+seq_len, D)
|
||||
|
||||
# CLS token is expected to hold spatial information for each frame
|
||||
x = x[:, 0, :] # (batch_dim, D)
|
||||
|
||||
return x
|
||||
|
||||
def _init_weights(self, m):
|
||||
if isinstance(m, nn.Linear):
|
||||
trunc_normal_(m.weight, std=.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 {'cls_token', 'pos_emb'}
|
||||
|
||||
|
||||
class SpatialTransformerEncoderLayer(BaseEncoderLayer):
|
||||
''' Aggregates spatial dimensions by applying attention individually to each frame. '''
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def forward(self, x: torch.Tensor, x_mask: torch.Tensor = None) -> torch.Tensor:
|
||||
''' x is of shape (B*S, D, t, h, w) where S is the number of segments.
|
||||
if specified x_mask (B*S, t, h, w), 0=masked, 1=kept
|
||||
Returns a tensor of shape (B*S, t, D) pooling spatial information for each frame. '''
|
||||
BS, D, t, h, w = x.shape
|
||||
|
||||
# time as a batch dimension and flatten spatial dimensions as sequence
|
||||
x = einops.rearrange(x, 'BS D t h w -> (BS t) (h w) D')
|
||||
# similar to mask
|
||||
if x_mask is not None:
|
||||
x_mask = einops.rearrange(x_mask, 'BS t h w -> (BS t) (h w)')
|
||||
|
||||
# apply encoder layer (BaseEncoderLayer.forward) - it will add CLS token and output its representation
|
||||
x = super().forward(x=x, x_mask=x_mask) # (B*S*t, D)
|
||||
|
||||
# reshape back to (B*S, t, D)
|
||||
x = einops.rearrange(x, '(BS t) D -> BS t D', BS=BS, t=t)
|
||||
|
||||
# (B*S, t, D)
|
||||
return x
|
||||
|
||||
|
||||
class TemporalTransformerEncoderLayer(BaseEncoderLayer):
|
||||
''' Aggregates temporal dimension with attention. Also used with pos emb as global aggregation
|
||||
in both streams. '''
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def forward(self, x):
|
||||
''' x is of shape (B*S, t, D) where S is the number of segments.
|
||||
Returns a tensor of shape (B*S, D) pooling temporal information. '''
|
||||
BS, t, D = x.shape
|
||||
|
||||
# apply encoder layer (BaseEncoderLayer.forward) - it will add CLS token and output its representation
|
||||
x = super().forward(x) # (B*S, D)
|
||||
|
||||
return x # (B*S, D)
|
||||
|
||||
|
||||
class AveragePooling(nn.Module):
|
||||
|
||||
def __init__(self, avg_pattern: str, then_permute_pattern: str = None) -> None:
|
||||
''' patterns are e.g. "bs t d -> bs d" '''
|
||||
super().__init__()
|
||||
# TODO: need to register them as buffers (but fails because these are strings)
|
||||
self.reduce_fn = 'mean'
|
||||
self.avg_pattern = avg_pattern
|
||||
self.then_permute_pattern = then_permute_pattern
|
||||
|
||||
def forward(self, x: torch.Tensor, x_mask: torch.Tensor = None) -> torch.Tensor:
|
||||
x = einops.reduce(x, self.avg_pattern, self.reduce_fn)
|
||||
if self.then_permute_pattern is not None:
|
||||
x = einops.rearrange(x, self.then_permute_pattern)
|
||||
return x
|
||||
@@ -0,0 +1,55 @@
|
||||
import logging
|
||||
from typing import Any, Mapping
|
||||
|
||||
import torch
|
||||
from torch import nn
|
||||
|
||||
from data_utils.ext.synchformer.motionformer import MotionFormer
|
||||
|
||||
|
||||
class Synchformer(nn.Module):
|
||||
|
||||
def __init__(self):
|
||||
super().__init__()
|
||||
|
||||
self.vfeat_extractor = MotionFormer(extract_features=True,
|
||||
factorize_space_time=True,
|
||||
agg_space_module='TransformerEncoderLayer',
|
||||
agg_time_module='torch.nn.Identity',
|
||||
add_global_repr=False)
|
||||
|
||||
# self.vfeat_extractor = instantiate_from_config(vfeat_extractor)
|
||||
# self.afeat_extractor = instantiate_from_config(afeat_extractor)
|
||||
# # bridging the s3d latent dim (1024) into what is specified in the config
|
||||
# # to match e.g. the transformer dim
|
||||
# self.vproj = instantiate_from_config(vproj)
|
||||
# self.aproj = instantiate_from_config(aproj)
|
||||
# self.transformer = instantiate_from_config(transformer)
|
||||
|
||||
def forward(self, vis):
|
||||
B, S, Tv, C, H, W = vis.shape
|
||||
vis = vis.permute(0, 1, 3, 2, 4, 5) # (B, S, C, Tv, H, W)
|
||||
# feat extractors return a tuple of segment-level and global features (ignored for sync)
|
||||
# (B, S, tv, D), e.g. (B, 7, 8, 768)
|
||||
vis = self.vfeat_extractor(vis)
|
||||
return vis
|
||||
|
||||
def load_state_dict(self, sd: Mapping[str, Any], strict: bool = True):
|
||||
# discard all entries except vfeat_extractor
|
||||
sd = {k: v for k, v in sd.items() if k.startswith('vfeat_extractor')}
|
||||
|
||||
return super().load_state_dict(sd, strict)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
model = Synchformer().cuda().eval()
|
||||
sd = torch.load('./ext_weights/synchformer_state_dict.pth', weights_only=True)
|
||||
model.load_state_dict(sd)
|
||||
|
||||
vid = torch.randn(2, 7, 16, 3, 224, 224).cuda()
|
||||
features = model.extract_vfeats(vid, for_loop=False).detach().cpu()
|
||||
print(features.shape)
|
||||
|
||||
# extract and save the state dict only
|
||||
# sd = torch.load('./ext_weights/sync_model_audioset.pt')['model']
|
||||
# torch.save(sd, './ext_weights/synchformer_state_dict.pth')
|
||||
@@ -0,0 +1,92 @@
|
||||
from hashlib import md5
|
||||
from pathlib import Path
|
||||
|
||||
import requests
|
||||
from tqdm import tqdm
|
||||
|
||||
PARENT_LINK = 'https://a3s.fi/swift/v1/AUTH_a235c0f452d648828f745589cde1219a'
|
||||
FNAME2LINK = {
|
||||
# S3: Synchability: AudioSet (run 2)
|
||||
'24-01-22T20-34-52.pt':
|
||||
f'{PARENT_LINK}/sync/sync_models/24-01-22T20-34-52/24-01-22T20-34-52.pt',
|
||||
'cfg-24-01-22T20-34-52.yaml':
|
||||
f'{PARENT_LINK}/sync/sync_models/24-01-22T20-34-52/cfg-24-01-22T20-34-52.yaml',
|
||||
# S2: Synchformer: AudioSet (run 2)
|
||||
'24-01-04T16-39-21.pt':
|
||||
f'{PARENT_LINK}/sync/sync_models/24-01-04T16-39-21/24-01-04T16-39-21.pt',
|
||||
'cfg-24-01-04T16-39-21.yaml':
|
||||
f'{PARENT_LINK}/sync/sync_models/24-01-04T16-39-21/cfg-24-01-04T16-39-21.yaml',
|
||||
# S2: Synchformer: AudioSet (run 1)
|
||||
'23-08-28T11-23-23.pt':
|
||||
f'{PARENT_LINK}/sync/sync_models/23-08-28T11-23-23/23-08-28T11-23-23.pt',
|
||||
'cfg-23-08-28T11-23-23.yaml':
|
||||
f'{PARENT_LINK}/sync/sync_models/23-08-28T11-23-23/cfg-23-08-28T11-23-23.yaml',
|
||||
# S2: Synchformer: LRS3 (run 2)
|
||||
'23-12-23T18-33-57.pt':
|
||||
f'{PARENT_LINK}/sync/sync_models/23-12-23T18-33-57/23-12-23T18-33-57.pt',
|
||||
'cfg-23-12-23T18-33-57.yaml':
|
||||
f'{PARENT_LINK}/sync/sync_models/23-12-23T18-33-57/cfg-23-12-23T18-33-57.yaml',
|
||||
# S2: Synchformer: VGS (run 2)
|
||||
'24-01-02T10-00-53.pt':
|
||||
f'{PARENT_LINK}/sync/sync_models/24-01-02T10-00-53/24-01-02T10-00-53.pt',
|
||||
'cfg-24-01-02T10-00-53.yaml':
|
||||
f'{PARENT_LINK}/sync/sync_models/24-01-02T10-00-53/cfg-24-01-02T10-00-53.yaml',
|
||||
# SparseSync: ft VGGSound-Full
|
||||
'22-09-21T21-00-52.pt':
|
||||
f'{PARENT_LINK}/sync/sync_models/22-09-21T21-00-52/22-09-21T21-00-52.pt',
|
||||
'cfg-22-09-21T21-00-52.yaml':
|
||||
f'{PARENT_LINK}/sync/sync_models/22-09-21T21-00-52/cfg-22-09-21T21-00-52.yaml',
|
||||
# SparseSync: ft VGGSound-Sparse
|
||||
'22-07-28T15-49-45.pt':
|
||||
f'{PARENT_LINK}/sync/sync_models/22-07-28T15-49-45/22-07-28T15-49-45.pt',
|
||||
'cfg-22-07-28T15-49-45.yaml':
|
||||
f'{PARENT_LINK}/sync/sync_models/22-07-28T15-49-45/cfg-22-07-28T15-49-45.yaml',
|
||||
# SparseSync: only pt on LRS3
|
||||
'22-07-13T22-25-49.pt':
|
||||
f'{PARENT_LINK}/sync/sync_models/22-07-13T22-25-49/22-07-13T22-25-49.pt',
|
||||
'cfg-22-07-13T22-25-49.yaml':
|
||||
f'{PARENT_LINK}/sync/sync_models/22-07-13T22-25-49/cfg-22-07-13T22-25-49.yaml',
|
||||
# SparseSync: feature extractors
|
||||
'ResNetAudio-22-08-04T09-51-04.pt':
|
||||
f'{PARENT_LINK}/sync/ResNetAudio-22-08-04T09-51-04.pt', # 2s
|
||||
'ResNetAudio-22-08-03T23-14-49.pt':
|
||||
f'{PARENT_LINK}/sync/ResNetAudio-22-08-03T23-14-49.pt', # 3s
|
||||
'ResNetAudio-22-08-03T23-14-28.pt':
|
||||
f'{PARENT_LINK}/sync/ResNetAudio-22-08-03T23-14-28.pt', # 4s
|
||||
'ResNetAudio-22-06-24T08-10-33.pt':
|
||||
f'{PARENT_LINK}/sync/ResNetAudio-22-06-24T08-10-33.pt', # 5s
|
||||
'ResNetAudio-22-06-24T17-31-07.pt':
|
||||
f'{PARENT_LINK}/sync/ResNetAudio-22-06-24T17-31-07.pt', # 6s
|
||||
'ResNetAudio-22-06-24T23-57-11.pt':
|
||||
f'{PARENT_LINK}/sync/ResNetAudio-22-06-24T23-57-11.pt', # 7s
|
||||
'ResNetAudio-22-06-25T04-35-42.pt':
|
||||
f'{PARENT_LINK}/sync/ResNetAudio-22-06-25T04-35-42.pt', # 8s
|
||||
}
|
||||
|
||||
|
||||
def check_if_file_exists_else_download(path, fname2link=FNAME2LINK, chunk_size=1024):
|
||||
'''Checks if file exists, if not downloads it from the link to the path'''
|
||||
path = Path(path)
|
||||
if not path.exists():
|
||||
path.parent.mkdir(exist_ok=True, parents=True)
|
||||
link = fname2link.get(path.name, None)
|
||||
if link is None:
|
||||
raise ValueError(f'Cant find the checkpoint file: {path}.',
|
||||
f'Please download it manually and ensure the path exists.')
|
||||
with requests.get(fname2link[path.name], stream=True) as r:
|
||||
total_size = int(r.headers.get('content-length', 0))
|
||||
with tqdm(total=total_size, unit='B', unit_scale=True) as pbar:
|
||||
with open(path, 'wb') as f:
|
||||
for data in r.iter_content(chunk_size=chunk_size):
|
||||
if data:
|
||||
f.write(data)
|
||||
pbar.update(chunk_size)
|
||||
|
||||
|
||||
def get_md5sum(path):
|
||||
hash_md5 = md5()
|
||||
with open(path, 'rb') as f:
|
||||
for chunk in iter(lambda: f.read(4096 * 8), b''):
|
||||
hash_md5.update(chunk)
|
||||
md5sum = hash_md5.hexdigest()
|
||||
return md5sum
|
||||
@@ -0,0 +1,277 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
# Copyright 2020 Ross Wightman
|
||||
# Modified Model definition
|
||||
|
||||
from collections import OrderedDict
|
||||
from functools import partial
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from timm.layers import trunc_normal_
|
||||
|
||||
from data_utils.ext.synchformer import vit_helper
|
||||
|
||||
|
||||
class VisionTransformer(nn.Module):
|
||||
""" Vision Transformer with support for patch or hybrid CNN input stage """
|
||||
|
||||
def __init__(self, cfg):
|
||||
super().__init__()
|
||||
self.img_size = cfg.DATA.TRAIN_CROP_SIZE
|
||||
self.patch_size = cfg.VIT.PATCH_SIZE
|
||||
self.in_chans = cfg.VIT.CHANNELS
|
||||
if cfg.TRAIN.DATASET == "Epickitchens":
|
||||
self.num_classes = [97, 300]
|
||||
else:
|
||||
self.num_classes = cfg.MODEL.NUM_CLASSES
|
||||
self.embed_dim = cfg.VIT.EMBED_DIM
|
||||
self.depth = cfg.VIT.DEPTH
|
||||
self.num_heads = cfg.VIT.NUM_HEADS
|
||||
self.mlp_ratio = cfg.VIT.MLP_RATIO
|
||||
self.qkv_bias = cfg.VIT.QKV_BIAS
|
||||
self.drop_rate = cfg.VIT.DROP
|
||||
self.drop_path_rate = cfg.VIT.DROP_PATH
|
||||
self.head_dropout = cfg.VIT.HEAD_DROPOUT
|
||||
self.video_input = cfg.VIT.VIDEO_INPUT
|
||||
self.temporal_resolution = cfg.VIT.TEMPORAL_RESOLUTION
|
||||
self.use_mlp = cfg.VIT.USE_MLP
|
||||
self.num_features = self.embed_dim
|
||||
norm_layer = partial(nn.LayerNorm, eps=1e-6)
|
||||
self.attn_drop_rate = cfg.VIT.ATTN_DROPOUT
|
||||
self.head_act = cfg.VIT.HEAD_ACT
|
||||
self.cfg = cfg
|
||||
|
||||
# Patch Embedding
|
||||
self.patch_embed = vit_helper.PatchEmbed(img_size=224,
|
||||
patch_size=self.patch_size,
|
||||
in_chans=self.in_chans,
|
||||
embed_dim=self.embed_dim)
|
||||
|
||||
# 3D Patch Embedding
|
||||
self.patch_embed_3d = vit_helper.PatchEmbed3D(img_size=self.img_size,
|
||||
temporal_resolution=self.temporal_resolution,
|
||||
patch_size=self.patch_size,
|
||||
in_chans=self.in_chans,
|
||||
embed_dim=self.embed_dim,
|
||||
z_block_size=self.cfg.VIT.PATCH_SIZE_TEMP)
|
||||
self.patch_embed_3d.proj.weight.data = torch.zeros_like(
|
||||
self.patch_embed_3d.proj.weight.data)
|
||||
|
||||
# Number of patches
|
||||
if self.video_input:
|
||||
num_patches = self.patch_embed.num_patches * self.temporal_resolution
|
||||
else:
|
||||
num_patches = self.patch_embed.num_patches
|
||||
self.num_patches = num_patches
|
||||
|
||||
# CLS token
|
||||
self.cls_token = nn.Parameter(torch.zeros(1, 1, self.embed_dim))
|
||||
trunc_normal_(self.cls_token, std=.02)
|
||||
|
||||
# Positional embedding
|
||||
self.pos_embed = nn.Parameter(
|
||||
torch.zeros(1, self.patch_embed.num_patches + 1, self.embed_dim))
|
||||
self.pos_drop = nn.Dropout(p=cfg.VIT.POS_DROPOUT)
|
||||
trunc_normal_(self.pos_embed, std=.02)
|
||||
|
||||
if self.cfg.VIT.POS_EMBED == "joint":
|
||||
self.st_embed = nn.Parameter(torch.zeros(1, num_patches + 1, self.embed_dim))
|
||||
trunc_normal_(self.st_embed, std=.02)
|
||||
elif self.cfg.VIT.POS_EMBED == "separate":
|
||||
self.temp_embed = nn.Parameter(torch.zeros(1, self.temporal_resolution, self.embed_dim))
|
||||
|
||||
# Layer Blocks
|
||||
dpr = [x.item() for x in torch.linspace(0, self.drop_path_rate, self.depth)]
|
||||
if self.cfg.VIT.ATTN_LAYER == "divided":
|
||||
self.blocks = nn.ModuleList([
|
||||
vit_helper.DividedSpaceTimeBlock(
|
||||
attn_type=cfg.VIT.ATTN_LAYER,
|
||||
dim=self.embed_dim,
|
||||
num_heads=self.num_heads,
|
||||
mlp_ratio=self.mlp_ratio,
|
||||
qkv_bias=self.qkv_bias,
|
||||
drop=self.drop_rate,
|
||||
attn_drop=self.attn_drop_rate,
|
||||
drop_path=dpr[i],
|
||||
norm_layer=norm_layer,
|
||||
) for i in range(self.depth)
|
||||
])
|
||||
else:
|
||||
self.blocks = nn.ModuleList([
|
||||
vit_helper.Block(attn_type=cfg.VIT.ATTN_LAYER,
|
||||
dim=self.embed_dim,
|
||||
num_heads=self.num_heads,
|
||||
mlp_ratio=self.mlp_ratio,
|
||||
qkv_bias=self.qkv_bias,
|
||||
drop=self.drop_rate,
|
||||
attn_drop=self.attn_drop_rate,
|
||||
drop_path=dpr[i],
|
||||
norm_layer=norm_layer,
|
||||
use_original_code=self.cfg.VIT.USE_ORIGINAL_TRAJ_ATTN_CODE)
|
||||
for i in range(self.depth)
|
||||
])
|
||||
self.norm = norm_layer(self.embed_dim)
|
||||
|
||||
# MLP head
|
||||
if self.use_mlp:
|
||||
hidden_dim = self.embed_dim
|
||||
if self.head_act == 'tanh':
|
||||
# logging.info("Using TanH activation in MLP")
|
||||
act = nn.Tanh()
|
||||
elif self.head_act == 'gelu':
|
||||
# logging.info("Using GELU activation in MLP")
|
||||
act = nn.GELU()
|
||||
else:
|
||||
# logging.info("Using ReLU activation in MLP")
|
||||
act = nn.ReLU()
|
||||
self.pre_logits = nn.Sequential(
|
||||
OrderedDict([
|
||||
('fc', nn.Linear(self.embed_dim, hidden_dim)),
|
||||
('act', act),
|
||||
]))
|
||||
else:
|
||||
self.pre_logits = nn.Identity()
|
||||
|
||||
# Classifier Head
|
||||
self.head_drop = nn.Dropout(p=self.head_dropout)
|
||||
if isinstance(self.num_classes, (list, )) and len(self.num_classes) > 1:
|
||||
for a, i in enumerate(range(len(self.num_classes))):
|
||||
setattr(self, "head%d" % a, nn.Linear(self.embed_dim, self.num_classes[i]))
|
||||
else:
|
||||
self.head = nn.Linear(self.embed_dim,
|
||||
self.num_classes) if self.num_classes > 0 else nn.Identity()
|
||||
|
||||
# Initialize weights
|
||||
self.apply(self._init_weights)
|
||||
|
||||
def _init_weights(self, m):
|
||||
if isinstance(m, nn.Linear):
|
||||
trunc_normal_(m.weight, std=.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):
|
||||
if self.cfg.VIT.POS_EMBED == "joint":
|
||||
return {'pos_embed', 'cls_token', 'st_embed'}
|
||||
else:
|
||||
return {'pos_embed', 'cls_token', 'temp_embed'}
|
||||
|
||||
def get_classifier(self):
|
||||
return self.head
|
||||
|
||||
def reset_classifier(self, num_classes, global_pool=''):
|
||||
self.num_classes = num_classes
|
||||
self.head = (nn.Linear(self.embed_dim, num_classes) if num_classes > 0 else nn.Identity())
|
||||
|
||||
def forward_features(self, x):
|
||||
# if self.video_input:
|
||||
# x = x[0]
|
||||
B = x.shape[0]
|
||||
|
||||
# Tokenize input
|
||||
# if self.cfg.VIT.PATCH_SIZE_TEMP > 1:
|
||||
# for simplicity of mapping between content dimensions (input x) and token dims (after patching)
|
||||
# we use the same trick as for AST (see modeling_ast.ASTModel.forward for the details):
|
||||
|
||||
# apply patching on input
|
||||
x = self.patch_embed_3d(x)
|
||||
tok_mask = None
|
||||
|
||||
# else:
|
||||
# tok_mask = None
|
||||
# # 2D tokenization
|
||||
# if self.video_input:
|
||||
# x = x.permute(0, 2, 1, 3, 4)
|
||||
# (B, T, C, H, W) = x.shape
|
||||
# x = x.reshape(B * T, C, H, W)
|
||||
|
||||
# x = self.patch_embed(x)
|
||||
|
||||
# if self.video_input:
|
||||
# (B2, T2, D2) = x.shape
|
||||
# x = x.reshape(B, T * T2, D2)
|
||||
|
||||
# Append CLS token
|
||||
cls_tokens = self.cls_token.expand(B, -1, -1)
|
||||
x = torch.cat((cls_tokens, x), dim=1)
|
||||
# if tok_mask is not None:
|
||||
# # prepend 1(=keep) to the mask to account for the CLS token as well
|
||||
# tok_mask = torch.cat((torch.ones_like(tok_mask[:, [0]]), tok_mask), dim=1)
|
||||
|
||||
# Interpolate positinoal embeddings
|
||||
# if self.cfg.DATA.TRAIN_CROP_SIZE != 224:
|
||||
# pos_embed = self.pos_embed
|
||||
# N = pos_embed.shape[1] - 1
|
||||
# npatch = int((x.size(1) - 1) / self.temporal_resolution)
|
||||
# class_emb = pos_embed[:, 0]
|
||||
# pos_embed = pos_embed[:, 1:]
|
||||
# dim = x.shape[-1]
|
||||
# pos_embed = torch.nn.functional.interpolate(
|
||||
# pos_embed.reshape(1, int(math.sqrt(N)), int(math.sqrt(N)), dim).permute(0, 3, 1, 2),
|
||||
# scale_factor=math.sqrt(npatch / N),
|
||||
# mode='bicubic',
|
||||
# )
|
||||
# pos_embed = pos_embed.permute(0, 2, 3, 1).view(1, -1, dim)
|
||||
# new_pos_embed = torch.cat((class_emb.unsqueeze(0), pos_embed), dim=1)
|
||||
# else:
|
||||
new_pos_embed = self.pos_embed
|
||||
npatch = self.patch_embed.num_patches
|
||||
|
||||
# Add positional embeddings to input
|
||||
if self.video_input:
|
||||
if self.cfg.VIT.POS_EMBED == "separate":
|
||||
cls_embed = self.pos_embed[:, 0, :].unsqueeze(1)
|
||||
tile_pos_embed = new_pos_embed[:, 1:, :].repeat(1, self.temporal_resolution, 1)
|
||||
tile_temporal_embed = self.temp_embed.repeat_interleave(npatch, 1)
|
||||
total_pos_embed = tile_pos_embed + tile_temporal_embed
|
||||
total_pos_embed = torch.cat([cls_embed, total_pos_embed], dim=1)
|
||||
x = x + total_pos_embed
|
||||
elif self.cfg.VIT.POS_EMBED == "joint":
|
||||
x = x + self.st_embed
|
||||
else:
|
||||
# image input
|
||||
x = x + new_pos_embed
|
||||
|
||||
# Apply positional dropout
|
||||
x = self.pos_drop(x)
|
||||
|
||||
# Encoding using transformer layers
|
||||
for i, blk in enumerate(self.blocks):
|
||||
x = blk(x,
|
||||
seq_len=npatch,
|
||||
num_frames=self.temporal_resolution,
|
||||
approx=self.cfg.VIT.APPROX_ATTN_TYPE,
|
||||
num_landmarks=self.cfg.VIT.APPROX_ATTN_DIM,
|
||||
tok_mask=tok_mask)
|
||||
|
||||
### v-iashin: I moved it to the forward pass
|
||||
# x = self.norm(x)[:, 0]
|
||||
# x = self.pre_logits(x)
|
||||
###
|
||||
return x, tok_mask
|
||||
|
||||
# def forward(self, x):
|
||||
# x = self.forward_features(x)
|
||||
# ### v-iashin: here. This should leave the same forward output as before
|
||||
# x = self.norm(x)[:, 0]
|
||||
# x = self.pre_logits(x)
|
||||
# ###
|
||||
# x = self.head_drop(x)
|
||||
# if isinstance(self.num_classes, (list, )) and len(self.num_classes) > 1:
|
||||
# output = []
|
||||
# for head in range(len(self.num_classes)):
|
||||
# x_out = getattr(self, "head%d" % head)(x)
|
||||
# if not self.training:
|
||||
# x_out = torch.nn.functional.softmax(x_out, dim=-1)
|
||||
# output.append(x_out)
|
||||
# return output
|
||||
# else:
|
||||
# x = self.head(x)
|
||||
# if not self.training:
|
||||
# x = torch.nn.functional.softmax(x, dim=-1)
|
||||
# return x
|
||||
@@ -0,0 +1,399 @@
|
||||
#!/usr/bin/env python3
|
||||
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
|
||||
# Copyright 2020 Ross Wightman
|
||||
# Modified Model definition
|
||||
"""Video models."""
|
||||
|
||||
import math
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
from einops import rearrange, repeat
|
||||
from timm.layers import to_2tuple
|
||||
from torch import einsum
|
||||
from torch.nn import functional as F
|
||||
|
||||
default_cfgs = {
|
||||
'vit_1k':
|
||||
'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-vitjx/jx_vit_base_p16_224-80ecf9dd.pth',
|
||||
'vit_1k_large':
|
||||
'https://github.com/rwightman/pytorch-image-models/releases/download/v0.1-vitjx/jx_vit_large_p16_224-4ee7a4dc.pth',
|
||||
}
|
||||
|
||||
|
||||
def qkv_attn(q, k, v, tok_mask: torch.Tensor = None):
|
||||
sim = einsum('b i d, b j d -> b i j', q, k)
|
||||
# apply masking if provided, tok_mask is (B*S*H, N): 1s - keep; sim is (B*S*H, H, N, N)
|
||||
if tok_mask is not None:
|
||||
BSH, N = tok_mask.shape
|
||||
sim = sim.masked_fill(tok_mask.view(BSH, 1, N) == 0,
|
||||
float('-inf')) # 1 - broadcasts across N
|
||||
attn = sim.softmax(dim=-1)
|
||||
out = einsum('b i j, b j d -> b i d', attn, v)
|
||||
return out
|
||||
|
||||
|
||||
class DividedAttention(nn.Module):
|
||||
|
||||
def __init__(self, dim, num_heads=8, qkv_bias=False, attn_drop=0., proj_drop=0.):
|
||||
super().__init__()
|
||||
self.num_heads = num_heads
|
||||
head_dim = dim // num_heads
|
||||
self.scale = head_dim**-0.5
|
||||
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
|
||||
self.proj = nn.Linear(dim, dim)
|
||||
|
||||
# init to zeros
|
||||
self.qkv.weight.data.fill_(0)
|
||||
self.qkv.bias.data.fill_(0)
|
||||
self.proj.weight.data.fill_(1)
|
||||
self.proj.bias.data.fill_(0)
|
||||
|
||||
self.attn_drop = nn.Dropout(attn_drop)
|
||||
self.proj_drop = nn.Dropout(proj_drop)
|
||||
|
||||
def forward(self, x, einops_from, einops_to, tok_mask: torch.Tensor = None, **einops_dims):
|
||||
# num of heads variable
|
||||
h = self.num_heads
|
||||
|
||||
# project x to q, k, v vaalues
|
||||
q, k, v = self.qkv(x).chunk(3, dim=-1)
|
||||
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> (b h) n d', h=h), (q, k, v))
|
||||
if tok_mask is not None:
|
||||
# replicate token mask across heads (b, n) -> (b, h, n) -> (b*h, n) -- same as qkv but w/o d
|
||||
assert len(tok_mask.shape) == 2
|
||||
tok_mask = tok_mask.unsqueeze(1).expand(-1, h, -1).reshape(-1, tok_mask.shape[1])
|
||||
|
||||
# Scale q
|
||||
q *= self.scale
|
||||
|
||||
# Take out cls_q, cls_k, cls_v
|
||||
(cls_q, q_), (cls_k, k_), (cls_v, v_) = map(lambda t: (t[:, 0:1], t[:, 1:]), (q, k, v))
|
||||
# the same for masking
|
||||
if tok_mask is not None:
|
||||
cls_mask, mask_ = tok_mask[:, 0:1], tok_mask[:, 1:]
|
||||
else:
|
||||
cls_mask, mask_ = None, None
|
||||
|
||||
# let CLS token attend to key / values of all patches across time and space
|
||||
cls_out = qkv_attn(cls_q, k, v, tok_mask=tok_mask)
|
||||
|
||||
# rearrange across time or space
|
||||
q_, k_, v_ = map(lambda t: rearrange(t, f'{einops_from} -> {einops_to}', **einops_dims),
|
||||
(q_, k_, v_))
|
||||
|
||||
# expand CLS token keys and values across time or space and concat
|
||||
r = q_.shape[0] // cls_k.shape[0]
|
||||
cls_k, cls_v = map(lambda t: repeat(t, 'b () d -> (b r) () d', r=r), (cls_k, cls_v))
|
||||
|
||||
k_ = torch.cat((cls_k, k_), dim=1)
|
||||
v_ = torch.cat((cls_v, v_), dim=1)
|
||||
|
||||
# the same for masking (if provided)
|
||||
if tok_mask is not None:
|
||||
# since mask does not have the latent dim (d), we need to remove it from einops dims
|
||||
mask_ = rearrange(mask_, f'{einops_from} -> {einops_to}'.replace(' d', ''),
|
||||
**einops_dims)
|
||||
cls_mask = repeat(cls_mask, 'b () -> (b r) ()',
|
||||
r=r) # expand cls_mask across time or space
|
||||
mask_ = torch.cat((cls_mask, mask_), dim=1)
|
||||
|
||||
# attention
|
||||
out = qkv_attn(q_, k_, v_, tok_mask=mask_)
|
||||
|
||||
# merge back time or space
|
||||
out = rearrange(out, f'{einops_to} -> {einops_from}', **einops_dims)
|
||||
|
||||
# concat back the cls token
|
||||
out = torch.cat((cls_out, out), dim=1)
|
||||
|
||||
# merge back the heads
|
||||
out = rearrange(out, '(b h) n d -> b n (h d)', h=h)
|
||||
|
||||
## to out
|
||||
x = self.proj(out)
|
||||
x = self.proj_drop(x)
|
||||
return x
|
||||
|
||||
|
||||
class DividedSpaceTimeBlock(nn.Module):
|
||||
|
||||
def __init__(self,
|
||||
dim=768,
|
||||
num_heads=12,
|
||||
attn_type='divided',
|
||||
mlp_ratio=4.,
|
||||
qkv_bias=False,
|
||||
drop=0.,
|
||||
attn_drop=0.,
|
||||
drop_path=0.,
|
||||
act_layer=nn.GELU,
|
||||
norm_layer=nn.LayerNorm):
|
||||
super().__init__()
|
||||
|
||||
self.einops_from_space = 'b (f n) d'
|
||||
self.einops_to_space = '(b f) n d'
|
||||
self.einops_from_time = 'b (f n) d'
|
||||
self.einops_to_time = '(b n) f d'
|
||||
|
||||
self.norm1 = norm_layer(dim)
|
||||
|
||||
self.attn = DividedAttention(dim,
|
||||
num_heads=num_heads,
|
||||
qkv_bias=qkv_bias,
|
||||
attn_drop=attn_drop,
|
||||
proj_drop=drop)
|
||||
|
||||
self.timeattn = DividedAttention(dim,
|
||||
num_heads=num_heads,
|
||||
qkv_bias=qkv_bias,
|
||||
attn_drop=attn_drop,
|
||||
proj_drop=drop)
|
||||
|
||||
# self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
|
||||
self.drop_path = nn.Identity()
|
||||
self.norm2 = norm_layer(dim)
|
||||
mlp_hidden_dim = int(dim * mlp_ratio)
|
||||
self.mlp = Mlp(in_features=dim,
|
||||
hidden_features=mlp_hidden_dim,
|
||||
act_layer=act_layer,
|
||||
drop=drop)
|
||||
self.norm3 = norm_layer(dim)
|
||||
|
||||
def forward(self,
|
||||
x,
|
||||
seq_len=196,
|
||||
num_frames=8,
|
||||
approx='none',
|
||||
num_landmarks=128,
|
||||
tok_mask: torch.Tensor = None):
|
||||
time_output = self.timeattn(self.norm3(x),
|
||||
self.einops_from_time,
|
||||
self.einops_to_time,
|
||||
n=seq_len,
|
||||
tok_mask=tok_mask)
|
||||
time_residual = x + time_output
|
||||
|
||||
space_output = self.attn(self.norm1(time_residual),
|
||||
self.einops_from_space,
|
||||
self.einops_to_space,
|
||||
f=num_frames,
|
||||
tok_mask=tok_mask)
|
||||
space_residual = time_residual + self.drop_path(space_output)
|
||||
|
||||
x = space_residual
|
||||
x = x + self.drop_path(self.mlp(self.norm2(x)))
|
||||
return x
|
||||
|
||||
|
||||
class Mlp(nn.Module):
|
||||
|
||||
def __init__(self,
|
||||
in_features,
|
||||
hidden_features=None,
|
||||
out_features=None,
|
||||
act_layer=nn.GELU,
|
||||
drop=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
|
||||
|
||||
|
||||
class PatchEmbed(nn.Module):
|
||||
""" Image to Patch Embedding
|
||||
"""
|
||||
|
||||
def __init__(self, img_size=224, patch_size=16, in_chans=3, embed_dim=768):
|
||||
super().__init__()
|
||||
img_size = img_size if type(img_size) is tuple else to_2tuple(img_size)
|
||||
patch_size = img_size if type(patch_size) is tuple else to_2tuple(patch_size)
|
||||
num_patches = (img_size[1] // patch_size[1]) * (img_size[0] // patch_size[0])
|
||||
self.img_size = img_size
|
||||
self.patch_size = patch_size
|
||||
self.num_patches = num_patches
|
||||
|
||||
self.proj = nn.Conv2d(in_chans, embed_dim, kernel_size=patch_size, stride=patch_size)
|
||||
|
||||
def forward(self, x):
|
||||
B, C, H, W = x.shape
|
||||
x = self.proj(x).flatten(2).transpose(1, 2)
|
||||
return x
|
||||
|
||||
|
||||
class PatchEmbed3D(nn.Module):
|
||||
""" Image to Patch Embedding """
|
||||
|
||||
def __init__(self,
|
||||
img_size=224,
|
||||
temporal_resolution=4,
|
||||
in_chans=3,
|
||||
patch_size=16,
|
||||
z_block_size=2,
|
||||
embed_dim=768,
|
||||
flatten=True):
|
||||
super().__init__()
|
||||
self.height = (img_size // patch_size)
|
||||
self.width = (img_size // patch_size)
|
||||
### v-iashin: these two are incorrect
|
||||
# self.frames = (temporal_resolution // z_block_size)
|
||||
# self.num_patches = self.height * self.width * self.frames
|
||||
self.z_block_size = z_block_size
|
||||
###
|
||||
self.proj = nn.Conv3d(in_chans,
|
||||
embed_dim,
|
||||
kernel_size=(z_block_size, patch_size, patch_size),
|
||||
stride=(z_block_size, patch_size, patch_size))
|
||||
self.flatten = flatten
|
||||
|
||||
def forward(self, x):
|
||||
B, C, T, H, W = x.shape
|
||||
x = self.proj(x)
|
||||
if self.flatten:
|
||||
x = x.flatten(2).transpose(1, 2)
|
||||
return x
|
||||
|
||||
|
||||
class HeadMLP(nn.Module):
|
||||
|
||||
def __init__(self, n_input, n_classes, n_hidden=512, p=0.1):
|
||||
super(HeadMLP, self).__init__()
|
||||
self.n_input = n_input
|
||||
self.n_classes = n_classes
|
||||
self.n_hidden = n_hidden
|
||||
if n_hidden is None:
|
||||
# use linear classifier
|
||||
self.block_forward = nn.Sequential(nn.Dropout(p=p),
|
||||
nn.Linear(n_input, n_classes, bias=True))
|
||||
else:
|
||||
# use simple MLP classifier
|
||||
self.block_forward = nn.Sequential(nn.Dropout(p=p),
|
||||
nn.Linear(n_input, n_hidden, bias=True),
|
||||
nn.BatchNorm1d(n_hidden), nn.ReLU(inplace=True),
|
||||
nn.Dropout(p=p),
|
||||
nn.Linear(n_hidden, n_classes, bias=True))
|
||||
print(f"Dropout-NLP: {p}")
|
||||
|
||||
def forward(self, x):
|
||||
return self.block_forward(x)
|
||||
|
||||
|
||||
def _conv_filter(state_dict, patch_size=16):
|
||||
""" convert patch embedding weight from manual patchify + linear proj to conv"""
|
||||
out_dict = {}
|
||||
for k, v in state_dict.items():
|
||||
if 'patch_embed.proj.weight' in k:
|
||||
v = v.reshape((v.shape[0], 3, patch_size, patch_size))
|
||||
out_dict[k] = v
|
||||
return out_dict
|
||||
|
||||
|
||||
def adapt_input_conv(in_chans, conv_weight, agg='sum'):
|
||||
conv_type = conv_weight.dtype
|
||||
conv_weight = conv_weight.float()
|
||||
O, I, J, K = conv_weight.shape
|
||||
if in_chans == 1:
|
||||
if I > 3:
|
||||
assert conv_weight.shape[1] % 3 == 0
|
||||
# For models with space2depth stems
|
||||
conv_weight = conv_weight.reshape(O, I // 3, 3, J, K)
|
||||
conv_weight = conv_weight.sum(dim=2, keepdim=False)
|
||||
else:
|
||||
if agg == 'sum':
|
||||
print("Summing conv1 weights")
|
||||
conv_weight = conv_weight.sum(dim=1, keepdim=True)
|
||||
else:
|
||||
print("Averaging conv1 weights")
|
||||
conv_weight = conv_weight.mean(dim=1, keepdim=True)
|
||||
elif in_chans != 3:
|
||||
if I != 3:
|
||||
raise NotImplementedError('Weight format not supported by conversion.')
|
||||
else:
|
||||
if agg == 'sum':
|
||||
print("Summing conv1 weights")
|
||||
repeat = int(math.ceil(in_chans / 3))
|
||||
conv_weight = conv_weight.repeat(1, repeat, 1, 1)[:, :in_chans, :, :]
|
||||
conv_weight *= (3 / float(in_chans))
|
||||
else:
|
||||
print("Averaging conv1 weights")
|
||||
conv_weight = conv_weight.mean(dim=1, keepdim=True)
|
||||
conv_weight = conv_weight.repeat(1, in_chans, 1, 1)
|
||||
conv_weight = conv_weight.to(conv_type)
|
||||
return conv_weight
|
||||
|
||||
|
||||
def load_pretrained(model,
|
||||
cfg=None,
|
||||
num_classes=1000,
|
||||
in_chans=3,
|
||||
filter_fn=None,
|
||||
strict=True,
|
||||
progress=False):
|
||||
# Load state dict
|
||||
assert (f"{cfg.VIT.PRETRAINED_WEIGHTS} not in [vit_1k, vit_1k_large]")
|
||||
state_dict = torch.hub.load_state_dict_from_url(url=default_cfgs[cfg.VIT.PRETRAINED_WEIGHTS])
|
||||
|
||||
if filter_fn is not None:
|
||||
state_dict = filter_fn(state_dict)
|
||||
|
||||
input_convs = 'patch_embed.proj'
|
||||
if input_convs is not None and in_chans != 3:
|
||||
if isinstance(input_convs, str):
|
||||
input_convs = (input_convs, )
|
||||
for input_conv_name in input_convs:
|
||||
weight_name = input_conv_name + '.weight'
|
||||
try:
|
||||
state_dict[weight_name] = adapt_input_conv(in_chans,
|
||||
state_dict[weight_name],
|
||||
agg='avg')
|
||||
print(
|
||||
f'Converted input conv {input_conv_name} pretrained weights from 3 to {in_chans} channel(s)'
|
||||
)
|
||||
except NotImplementedError as e:
|
||||
del state_dict[weight_name]
|
||||
strict = False
|
||||
print(
|
||||
f'Unable to convert pretrained {input_conv_name} weights, using random init for this layer.'
|
||||
)
|
||||
|
||||
classifier_name = 'head'
|
||||
label_offset = cfg.get('label_offset', 0)
|
||||
pretrain_classes = 1000
|
||||
if num_classes != pretrain_classes:
|
||||
# completely discard fully connected if model num_classes doesn't match pretrained weights
|
||||
del state_dict[classifier_name + '.weight']
|
||||
del state_dict[classifier_name + '.bias']
|
||||
strict = False
|
||||
elif label_offset > 0:
|
||||
# special case for pretrained weights with an extra background class in pretrained weights
|
||||
classifier_weight = state_dict[classifier_name + '.weight']
|
||||
state_dict[classifier_name + '.weight'] = classifier_weight[label_offset:]
|
||||
classifier_bias = state_dict[classifier_name + '.bias']
|
||||
state_dict[classifier_name + '.bias'] = classifier_bias[label_offset:]
|
||||
|
||||
loaded_state = state_dict
|
||||
self_state = model.state_dict()
|
||||
all_names = set(self_state.keys())
|
||||
saved_names = set([])
|
||||
for name, param in loaded_state.items():
|
||||
param = param
|
||||
if 'module.' in name:
|
||||
name = name.replace('module.', '')
|
||||
if name in self_state.keys() and param.shape == self_state[name].shape:
|
||||
saved_names.add(name)
|
||||
self_state[name].copy_(param)
|
||||
else:
|
||||
print(f"didnt load: {name} of shape: {param.shape}")
|
||||
print("Missing Keys:")
|
||||
print(all_names - saved_names)
|
||||
@@ -0,0 +1,18 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
class PadCrop(nn.Module):
|
||||
def __init__(self, crop_size=None, pad_mode='constant', pad_value=0):
|
||||
super().__init__()
|
||||
self.crop_size = crop_size
|
||||
self.pad_mode = pad_mode
|
||||
self.pad_value = pad_value
|
||||
|
||||
def forward(self, x):
|
||||
# Simple implementation - you can customize this
|
||||
if self.crop_size:
|
||||
return x[:, :, :self.crop_size]
|
||||
return x
|
||||
|
||||
# Export PadCrop
|
||||
__all__ = ['PadCrop']
|
||||
@@ -0,0 +1,173 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
|
||||
class Audio_Text(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
self.cots = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['caption']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
# print(label,'debug1!!!!!!!!!')
|
||||
self.cots.append(record['caption_cot'])
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int):
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
cot = self.cots[idx]
|
||||
audio_path = os.path.join(self.root, f'{video_id}.wav')
|
||||
if not os.path.exists(audio_path):
|
||||
audio_path = os.path.join(self.root, f'{video_id}.flac')
|
||||
if not os.path.exists(audio_path):
|
||||
raise RuntimeError(f'Audio is not exist {audio_path}')
|
||||
audio_chunk, sample_rate = torchaudio.load(audio_path)
|
||||
if len(audio_chunk.shape) != 2:
|
||||
raise RuntimeError(f'error audio shape {video_id}')
|
||||
|
||||
abs_max = audio_chunk[0].abs().max()
|
||||
|
||||
if abs_max <= 1e-6:
|
||||
if audio_chunk.shape[0] > 1 and audio_chunk[1].abs().max() > 1e-6:
|
||||
audio_chunk = audio_chunk[1:2]
|
||||
else:
|
||||
raise RuntimeError(f'Audio is silent {video_id}')
|
||||
|
||||
# ensure the stereo audio
|
||||
if audio_chunk.shape[0] < 2:
|
||||
audio_chunk = audio_chunk.repeat(2, 1)
|
||||
elif audio_chunk.shape[0] > 2:
|
||||
audio_chunk = audio_chunk[:2]
|
||||
|
||||
# resample
|
||||
if sample_rate == self.sample_rate:
|
||||
audio_chunk = audio_chunk
|
||||
else:
|
||||
if sample_rate not in self.resampler:
|
||||
# https://pytorch.org/audio/stable/tutorials/audio_resampling_tutorial.html#kaiser-best
|
||||
self.resampler[sample_rate] = torchaudio.transforms.Resample(
|
||||
sample_rate,
|
||||
self.sample_rate,
|
||||
lowpass_filter_width=64,
|
||||
rolloff=0.9475937167399596,
|
||||
resampling_method='sinc_interp_kaiser',
|
||||
beta=14.769656459379492,
|
||||
)
|
||||
audio_chunk = self.resampler[sample_rate](audio_chunk)
|
||||
|
||||
if audio_chunk.shape[1] < self.expected_audio_length:
|
||||
# zero-padding audio
|
||||
padding_length = self.expected_audio_length - audio_chunk.shape[1]
|
||||
# 创建 padding 张量,大小为 [batch_size, padding_length],值为0
|
||||
padding = torch.zeros(audio_chunk.shape[0], padding_length)
|
||||
# 将原始音频和 padding 沿第 1 维度拼接在一起
|
||||
audio_chunk = torch.cat((audio_chunk, padding), dim=1)
|
||||
# raise RuntimeError(f'Audio too short {video_id}')
|
||||
audio_chunk = audio_chunk[:,:self.expected_audio_length]
|
||||
assert audio_chunk.shape == (2, 397312), f'error shape:{video_id},{audio_chunk.shape}'
|
||||
# print(label,'debug2!!!!!!!!!')
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'caption_cot': cot,
|
||||
'audio': audio_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int):
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/train",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_224_latents_text/train"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,315 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
def save_tensor_as_image(tensor, save_path):
|
||||
"""
|
||||
将形状为 (1, 3, H, W) 的 RGB 图像数组保存为图片文件。
|
||||
|
||||
:param tensor: 输入的 NumPy 数组 (1, 3, H, W)。
|
||||
:param save_path: 图片保存路径。
|
||||
"""
|
||||
# # 移除批次维度,变成 (3, H, W)
|
||||
# tensor = tensor.squeeze(0)
|
||||
|
||||
# 交换轴顺序,变为 (H, W, 3)
|
||||
image_array = np.transpose(tensor, (1, 2, 0))
|
||||
|
||||
# 检查数组是否为合适的数据类型
|
||||
if image_array.dtype != np.uint8:
|
||||
# 如果不是 uint8,首先标准化,然后转换
|
||||
image_array = (image_array - image_array.min()) / (image_array.max() - image_array.min()) * 255
|
||||
image_array = image_array.astype(np.uint8)
|
||||
|
||||
# 创建图像对象
|
||||
image = Image.fromarray(image_array)
|
||||
|
||||
# 保存图片
|
||||
image.save(save_path)
|
||||
print(f"Image saved to {save_path}")
|
||||
|
||||
def pad_to_square(video_tensor):
|
||||
# 验证输入的形状
|
||||
if len(video_tensor.shape) != 4:
|
||||
raise ValueError("Input tensor must have shape (l, c, h, w)")
|
||||
|
||||
l, c, h, w = video_tensor.shape
|
||||
max_side = max(h, w)
|
||||
|
||||
# 计算每一维度需要的填充量:(left, right, top, bottom)
|
||||
pad_h = max_side - h
|
||||
pad_w = max_side - w
|
||||
|
||||
# 创建padding tuple (left, right, top, bottom)
|
||||
# 因为图像的填充是作用在最后两个维度 h 和 w 上,所以我们需要指定这两个维度的填充
|
||||
padding = (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
|
||||
|
||||
# 使用F.pad对视频张量进行填充操作
|
||||
# 填充参数为 (left, right, top, bottom)
|
||||
video_padded = F.pad(video_tensor, pad=padding, mode='constant', value=0)
|
||||
|
||||
return video_padded
|
||||
|
||||
class Audioset(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
self.caption_t5s = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['label']
|
||||
caption_t5 = record['caption_t5']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
self.caption_t5s.append(caption_t5)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Lambda(pad_to_square), # 先填充为正方形
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
self.clip_processor = AutoProcessor.from_pretrained("useful_ckpts/metaclip-huge")
|
||||
self.sync_transform = v2.Compose([
|
||||
v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.CenterCrop(_SYNC_SIZE),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
||||
])
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
caption_t5 = self.caption_t5s[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(self.root / (video_id + '.mp4'))
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_SYNC_FPS * self.duration_sec),
|
||||
frame_rate=_SYNC_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
# reader.add_basic_audio_stream(frames_per_chunk=2**30,)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
sync_chunk = data_chunk[1]
|
||||
audio_path = os.path.join("dataset/3_Audioset/audios/sound",video_id+'.wav')
|
||||
assert os.path.exists(audio_path), f'{audio_path} not exists'
|
||||
audio_chunk, sr = torchaudio.load(audio_path)
|
||||
# audio_chunk = data_chunk[2]
|
||||
if len(audio_chunk.shape) != 2:
|
||||
raise RuntimeError(f'error audio shape {video_id}')
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
|
||||
if sync_chunk is None:
|
||||
raise RuntimeError(f'Sync video returned None {video_id}')
|
||||
sample_rate = int(sr)
|
||||
# audio_chunk = audio_chunk.transpose(0, 1)
|
||||
abs_max = audio_chunk[0].abs().max()
|
||||
# audio_chunk = audio_chunk.mean(dim=0) # mono
|
||||
# if self.normalize_audio:
|
||||
# abs_max = audio_chunk.abs().max()
|
||||
# audio_chunk = audio_chunk / abs_max * 0.95
|
||||
if abs_max <= 1e-6:
|
||||
if audio_chunk.shape[0] > 1 and audio_chunk[1].abs().max() > 1e-6:
|
||||
audio_chunk = audio_chunk[1:2]
|
||||
else:
|
||||
raise RuntimeError(f'Audio is silent {video_id}')
|
||||
|
||||
# ensure the stereo audio
|
||||
if audio_chunk.shape[0] < 2:
|
||||
audio_chunk = audio_chunk.repeat(2, 1)
|
||||
|
||||
# resample
|
||||
if sample_rate == self.sample_rate:
|
||||
audio_chunk = audio_chunk
|
||||
else:
|
||||
if sample_rate not in self.resampler:
|
||||
# https://pytorch.org/audio/stable/tutorials/audio_resampling_tutorial.html#kaiser-best
|
||||
self.resampler[sample_rate] = torchaudio.transforms.Resample(
|
||||
sample_rate,
|
||||
self.sample_rate,
|
||||
lowpass_filter_width=64,
|
||||
rolloff=0.9475937167399596,
|
||||
resampling_method='sinc_interp_kaiser',
|
||||
beta=14.769656459379492,
|
||||
)
|
||||
audio_chunk = self.resampler[sample_rate](audio_chunk)
|
||||
|
||||
if audio_chunk.shape[1] < self.expected_audio_length:
|
||||
# zero-padding audio
|
||||
padding_length = self.expected_audio_length - audio_chunk.shape[1]
|
||||
# 创建 padding 张量,大小为 [batch_size, padding_length],值为0
|
||||
padding = torch.zeros(audio_chunk.shape[0], padding_length)
|
||||
# 将原始音频和 padding 沿第 1 维度拼接在一起
|
||||
audio_chunk = torch.cat((audio_chunk, padding), dim=1)
|
||||
# raise RuntimeError(f'Audio too short {video_id}')
|
||||
audio_chunk = audio_chunk[:,:self.expected_audio_length]
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(last_frame.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = pad_to_square(clip_chunk)
|
||||
# save_image(clip_chunk[0] / 255.0,'square.png')
|
||||
# clip_chunk = self.clip_transform(clip_chunk)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
clip_chunk = self.clip_processor(images=clip_chunk, return_tensors="pt")["pixel_values"]
|
||||
# log.info(clip_chunk.shape)
|
||||
# save_tensor_as_image(clip_chunk[0].numpy(),'scale.png')
|
||||
# log.info(clip_chunk[0])
|
||||
# clip_chunk = outputs
|
||||
# text_ids = outputs["input_ids"]
|
||||
# temp_img = clip_chunk[0].permute(1, 2, 0) * 255
|
||||
# save_image(clip_chunk[0],'scale.png')
|
||||
sync_chunk = sync_chunk[:self.sync_expected_length]
|
||||
if sync_chunk.shape[0] != self.sync_expected_length:
|
||||
# padding using the last frame, but no more than 2
|
||||
current_length = sync_chunk.shape[0]
|
||||
last_frame = sync_chunk[-1]
|
||||
# 重复最后一帧以进行填充
|
||||
padding = last_frame.repeat(self.sync_expected_length - current_length, 1, 1, 1)
|
||||
assert self.sync_expected_length - current_length < 12, f'sync can pad no more than 2 while {self.sync_expected_length - current_length}'
|
||||
sync_chunk = torch.cat((sync_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'Sync video wrong length {video_id}, '
|
||||
# f'expected {self.sync_expected_length}, '
|
||||
# f'got {sync_chunk.shape[0]}')
|
||||
|
||||
sync_chunk = self.sync_transform(sync_chunk)
|
||||
assert audio_chunk.shape[1] == self.expected_audio_length and clip_chunk.shape[0] == self.clip_expected_length \
|
||||
and sync_chunk.shape[0] == self.sync_expected_length, 'error processed data shape'
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'caption_t5': caption_t5,
|
||||
'audio': audio_chunk,
|
||||
'clip_video': clip_chunk,
|
||||
'sync_video': sync_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = Audioset(
|
||||
# root="dataset/3_Audioset/video/sound",
|
||||
# tsv_path="dataset/3_Audioset/split_txt/unbalanced_sound_filtered_aligned_novgg_noout.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="dataset/3_Audioset/video_text_latents/"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,268 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
def save_tensor_as_image(tensor, save_path):
|
||||
"""
|
||||
将形状为 (1, 3, H, W) 的 RGB 图像数组保存为图片文件。
|
||||
|
||||
:param tensor: 输入的 NumPy 数组 (1, 3, H, W)。
|
||||
:param save_path: 图片保存路径。
|
||||
"""
|
||||
# # 移除批次维度,变成 (3, H, W)
|
||||
# tensor = tensor.squeeze(0)
|
||||
|
||||
# 交换轴顺序,变为 (H, W, 3)
|
||||
image_array = np.transpose(tensor, (1, 2, 0))
|
||||
|
||||
# 检查数组是否为合适的数据类型
|
||||
if image_array.dtype != np.uint8:
|
||||
# 如果不是 uint8,首先标准化,然后转换
|
||||
image_array = (image_array - image_array.min()) / (image_array.max() - image_array.min()) * 255
|
||||
image_array = image_array.astype(np.uint8)
|
||||
|
||||
# 创建图像对象
|
||||
image = Image.fromarray(image_array)
|
||||
|
||||
# 保存图片
|
||||
image.save(save_path)
|
||||
print(f"Image saved to {save_path}")
|
||||
|
||||
def pad_to_square(video_tensor):
|
||||
# 验证输入的形状
|
||||
if len(video_tensor.shape) != 4:
|
||||
raise ValueError("Input tensor must have shape (l, c, h, w)")
|
||||
|
||||
l, c, h, w = video_tensor.shape
|
||||
max_side = max(h, w)
|
||||
|
||||
# 计算每一维度需要的填充量:(left, right, top, bottom)
|
||||
pad_h = max_side - h
|
||||
pad_w = max_side - w
|
||||
|
||||
# 创建padding tuple (left, right, top, bottom)
|
||||
# 因为图像的填充是作用在最后两个维度 h 和 w 上,所以我们需要指定这两个维度的填充
|
||||
padding = (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
|
||||
|
||||
# 使用F.pad对视频张量进行填充操作
|
||||
# 填充参数为 (left, right, top, bottom)
|
||||
video_padded = F.pad(video_tensor, pad=padding, mode='constant', value=0)
|
||||
|
||||
return video_padded
|
||||
|
||||
class Audioset(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
duration_sec: float = 10.0,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.captions = []
|
||||
self.videos = []
|
||||
self.caption_t5s = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
with open(tsv_path.replace('.csv','.txt')) as file:
|
||||
paths = file.readlines()
|
||||
for record, path in zip(df_list,paths):
|
||||
id = Path(record['id']).stem
|
||||
# if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
caption = record['caption']
|
||||
caption_t5 = record['caption_t5']
|
||||
path = path.strip()
|
||||
part = Path(path).parent
|
||||
video_id = Path(path).stem[1:]
|
||||
video_path = os.path.join('dataset/3_Audioset/video',part,f'{video_id}.mp4')
|
||||
assert os.path.exists(video_path), 'video must exist'
|
||||
# if id in videos:
|
||||
self.captions.append(caption)
|
||||
self.caption_t5s.append(caption_t5)
|
||||
# self.labels[id] = label
|
||||
self.videos.append(video_path)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
assert len(self.captions) == len(self.caption_t5s) and len(self.captions) == len(self.videos), 'error length'
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Lambda(pad_to_square), # 先填充为正方形
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
self.clip_processor = AutoProcessor.from_pretrained("useful_ckpts/metaclip-huge")
|
||||
self.sync_transform = v2.Compose([
|
||||
v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.CenterCrop(_SYNC_SIZE),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
||||
])
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_path = self.videos[idx]
|
||||
video_id = 'Y'+str(Path(video_path).stem)
|
||||
caption = self.captions[idx]
|
||||
caption_t5 = self.caption_t5s[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(video_path)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_SYNC_FPS * self.duration_sec),
|
||||
frame_rate=_SYNC_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
sync_chunk = data_chunk[1]
|
||||
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
# if clip_chunk.shape[0] < self.clip_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'CLIP video too short {video_id}, expected {self.clip_expected_length}, got {clip_chunk.shape[0]}'
|
||||
# )
|
||||
|
||||
if sync_chunk is None:
|
||||
raise RuntimeError(f'Sync video returned None {video_id}')
|
||||
# if sync_chunk.shape[0] < self.sync_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'Sync video too short {video_id}, expected {self.sync_expected_length}, got {sync_chunk.shape[0]}'
|
||||
# )
|
||||
|
||||
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(clip_chunk.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = pad_to_square(clip_chunk)
|
||||
# save_image(clip_chunk[0] / 255.0,'square.png')
|
||||
# clip_chunk = self.clip_transform(clip_chunk)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
clip_chunk = self.clip_processor(images=clip_chunk, return_tensors="pt")["pixel_values"]
|
||||
# log.info(clip_chunk.shape)
|
||||
# save_tensor_as_image(clip_chunk[0].numpy(),'scale.png')
|
||||
# log.info(clip_chunk[0])
|
||||
# clip_chunk = outputs
|
||||
# text_ids = outputs["input_ids"]
|
||||
# temp_img = clip_chunk[0].permute(1, 2, 0) * 255
|
||||
# save_image(clip_chunk[0],'scale.png')
|
||||
sync_chunk = sync_chunk[:self.sync_expected_length]
|
||||
if sync_chunk.shape[0] != self.sync_expected_length:
|
||||
# padding using the last frame, but no more than 2
|
||||
current_length = sync_chunk.shape[0]
|
||||
last_frame = sync_chunk[-1]
|
||||
# 重复最后一帧以进行填充
|
||||
padding = last_frame.repeat(self.sync_expected_length - current_length, 1, 1, 1)
|
||||
assert self.sync_expected_length - current_length < 12, f'sync can pad no more than 2 while {self.sync_expected_length - current_length}'
|
||||
sync_chunk = torch.cat((sync_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'Sync video wrong length {video_id}, '
|
||||
# f'expected {self.sync_expected_length}, '
|
||||
# f'got {sync_chunk.shape[0]}')
|
||||
|
||||
sync_chunk = self.sync_transform(sync_chunk)
|
||||
assert clip_chunk.shape[0] == self.clip_expected_length and sync_chunk.shape[0] == self.sync_expected_length, 'error processed data shape'
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': caption,
|
||||
'caption_t5': caption_t5,
|
||||
'clip_video': clip_chunk,
|
||||
'sync_video': sync_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.captions)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/train",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_224_latents_text/train"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,298 @@
|
||||
from typing import Literal, Optional
|
||||
import json
|
||||
import open_clip
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
from einops import rearrange
|
||||
from open_clip import create_model_from_pretrained
|
||||
from torchvision.transforms import Normalize
|
||||
from ThinkSound.models.factory import create_model_from_config
|
||||
from ThinkSound.models.utils import load_ckpt_state_dict
|
||||
from ThinkSound.training.utils import copy_state_dict
|
||||
from transformers import AutoModel
|
||||
from transformers import AutoProcessor
|
||||
from transformers import T5EncoderModel, AutoTokenizer
|
||||
import logging
|
||||
from data_utils.ext.synchformer import Synchformer
|
||||
import os
|
||||
|
||||
log = logging.getLogger()
|
||||
|
||||
def patch_clip(clip_model):
|
||||
# a hack to make it output last hidden states
|
||||
# https://github.com/mlfoundations/open_clip/blob/fc5a37b72d705f760ebbc7915b84729816ed471f/src/open_clip/model.py#L269
|
||||
def new_get_text_features(self, input_ids=None, attention_mask=None, position_ids=None,
|
||||
output_attentions: Optional[bool] = None,
|
||||
output_hidden_states: Optional[bool] = None,
|
||||
return_dict: Optional[bool] = None):
|
||||
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
||||
output_hidden_states = (
|
||||
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
||||
)
|
||||
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
||||
|
||||
text_outputs = self.text_model(
|
||||
input_ids=input_ids,
|
||||
attention_mask=attention_mask,
|
||||
position_ids=position_ids,
|
||||
output_attentions=output_attentions,
|
||||
output_hidden_states=output_hidden_states,
|
||||
return_dict=return_dict,
|
||||
)
|
||||
last_hidden_state = text_outputs[0]
|
||||
pooled_output = text_outputs[1]
|
||||
text_features = self.text_projection(pooled_output)
|
||||
|
||||
return text_features, last_hidden_state
|
||||
|
||||
clip_model.get_text_features = new_get_text_features.__get__(clip_model)
|
||||
return clip_model
|
||||
|
||||
|
||||
class FeaturesUtils(nn.Module):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
vae_ckpt: Optional[str] = None,
|
||||
vae_config: Optional[str] = None,
|
||||
synchformer_ckpt: Optional[str] = None,
|
||||
enable_conditions: bool = True,
|
||||
need_vae_encoder: bool = True,
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
if enable_conditions:
|
||||
# Try to use local models first, fallback to online
|
||||
try:
|
||||
# Import folder_paths to get ComfyUI models directory
|
||||
import folder_paths
|
||||
models_dir = folder_paths.models_dir
|
||||
print(f"🔍 DEBUG: Using ComfyUI models directory: {models_dir}")
|
||||
except ImportError:
|
||||
# Fallback if folder_paths not available
|
||||
models_dir = None
|
||||
print("⚠️ WARNING: folder_paths not available, using online models")
|
||||
|
||||
# MetaCLIP model paths
|
||||
if models_dir:
|
||||
metaclip_local_path = os.path.join(models_dir, "thinksound", "metaclip-h14-fullcc2.5b")
|
||||
if os.path.exists(metaclip_local_path):
|
||||
print(f"✅ Using local MetaCLIP model: {metaclip_local_path}")
|
||||
metaclip_path = metaclip_local_path
|
||||
else:
|
||||
print(f"❌ Local MetaCLIP not found at {metaclip_local_path}, using online")
|
||||
metaclip_path = "facebook/metaclip-h14-fullcc2.5b"
|
||||
else:
|
||||
metaclip_path = "facebook/metaclip-h14-fullcc2.5b"
|
||||
|
||||
# T5 model paths
|
||||
if models_dir:
|
||||
t5_local_path = os.path.join(models_dir, "t5-v1_1-xl")
|
||||
if os.path.exists(t5_local_path):
|
||||
print(f"✅ Using local T5 model: {t5_local_path}")
|
||||
t5_path = t5_local_path
|
||||
else:
|
||||
print(f"❌ Local T5 not found at {t5_local_path}, using online")
|
||||
t5_path = "google/t5-v1_1-xl"
|
||||
else:
|
||||
t5_path = "google/t5-v1_1-xl"
|
||||
|
||||
# Load models with local/online paths
|
||||
try:
|
||||
print(f"🔄 Loading MetaCLIP model from: {metaclip_path}")
|
||||
self.clip_model = AutoModel.from_pretrained(
|
||||
metaclip_path,
|
||||
local_files_only=(models_dir is not None and os.path.exists(metaclip_path))
|
||||
)
|
||||
self.clip_model = patch_clip(self.clip_model)
|
||||
|
||||
print(f"🔄 Loading MetaCLIP processor from: {metaclip_path}")
|
||||
self.clip_processor = AutoProcessor.from_pretrained(
|
||||
metaclip_path,
|
||||
local_files_only=(models_dir is not None and os.path.exists(metaclip_path))
|
||||
)
|
||||
print("✅ MetaCLIP model and processor loaded successfully")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to load MetaCLIP: {e}")
|
||||
print("🔄 Trying without local_files_only flag...")
|
||||
try:
|
||||
self.clip_model = AutoModel.from_pretrained(metaclip_path)
|
||||
self.clip_model = patch_clip(self.clip_model)
|
||||
self.clip_processor = AutoProcessor.from_pretrained(metaclip_path)
|
||||
print("✅ MetaCLIP loaded with fallback method")
|
||||
except Exception as e2:
|
||||
print(f"❌ Failed to load MetaCLIP with fallback: {e2}")
|
||||
raise
|
||||
|
||||
try:
|
||||
print(f"🔄 Loading T5 tokenizer from: {t5_path}")
|
||||
self.t5_tokenizer = AutoTokenizer.from_pretrained(
|
||||
t5_path,
|
||||
local_files_only=(models_dir is not None and os.path.exists(t5_path))
|
||||
)
|
||||
|
||||
print(f"🔄 Loading T5 model from: {t5_path}")
|
||||
self.t5_model = T5EncoderModel.from_pretrained(
|
||||
t5_path,
|
||||
local_files_only=(models_dir is not None and os.path.exists(t5_path))
|
||||
)
|
||||
print("✅ T5 model and tokenizer loaded successfully")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to load T5: {e}")
|
||||
print("🔄 Trying without local_files_only flag...")
|
||||
try:
|
||||
self.t5_tokenizer = AutoTokenizer.from_pretrained(t5_path)
|
||||
self.t5_model = T5EncoderModel.from_pretrained(t5_path)
|
||||
print("✅ T5 loaded with fallback method")
|
||||
except Exception as e2:
|
||||
print(f"❌ Failed to load T5 with fallback: {e2}")
|
||||
raise
|
||||
|
||||
# Load Synchformer
|
||||
print(f"🔄 Loading Synchformer from: {synchformer_ckpt}")
|
||||
self.synchformer = Synchformer()
|
||||
|
||||
# Load state dict to CPU first
|
||||
synch_state_dict = torch.load(synchformer_ckpt, weights_only=True, map_location='cpu')
|
||||
self.synchformer.load_state_dict(synch_state_dict)
|
||||
|
||||
# Set to eval mode
|
||||
self.synchformer.eval()
|
||||
print("✅ Synchformer loaded successfully")
|
||||
|
||||
# self.tokenizer = open_clip.get_tokenizer('ViT-H-14-378-quickgelu') # same as 'ViT-H-14'
|
||||
else:
|
||||
self.clip_model = None
|
||||
self.synchformer = None
|
||||
self.tokenizer = None
|
||||
|
||||
if vae_ckpt is not None:
|
||||
print(f"🔄 Loading VAE config from: {vae_config}")
|
||||
with open(vae_config) as f:
|
||||
vae_config = json.load(f)
|
||||
self.vae = create_model_from_config(vae_config)
|
||||
print(f"🔄 Loading VAE checkpoint from: {vae_ckpt}")
|
||||
# Load checkpoint
|
||||
copy_state_dict(self.vae, load_ckpt_state_dict(vae_ckpt,prefix='autoencoder.'))#,prefix='autoencoder.'
|
||||
print("✅ VAE loaded successfully")
|
||||
else:
|
||||
print("ℹ️ VAE not loaded in FeatureUtils (vae_ckpt=None)")
|
||||
self.vae = None
|
||||
|
||||
def compile(self):
|
||||
if self.clip_model is not None:
|
||||
self.clip_model.encode_image = torch.compile(self.clip_model.encode_image)
|
||||
self.clip_model.encode_text = torch.compile(self.clip_model.encode_text)
|
||||
if self.synchformer is not None:
|
||||
self.synchformer = torch.compile(self.synchformer)
|
||||
|
||||
|
||||
def train(self, mode: bool) -> None:
|
||||
return super().train(False)
|
||||
|
||||
@torch.inference_mode()
|
||||
def encode_video_with_clip(self, x: torch.Tensor, batch_size: int = -1) -> torch.Tensor:
|
||||
assert self.clip_model is not None, 'CLIP is not loaded'
|
||||
# x: (B, T, C, H, W) H/W: 384
|
||||
b, t, c, h, w = x.shape
|
||||
|
||||
assert c == 3 and h == 224 and w == 224
|
||||
|
||||
# Ensure input tensor matches clip model dtype
|
||||
target_dtype = next(self.clip_model.parameters()).dtype
|
||||
if x.dtype != target_dtype:
|
||||
print(f"🔧 Converting clip input from {x.dtype} to {target_dtype}")
|
||||
x = x.to(dtype=target_dtype)
|
||||
|
||||
# x = self.clip_preprocess(x)
|
||||
x = rearrange(x, 'b t c h w -> (b t) c h w')
|
||||
outputs = []
|
||||
if batch_size < 0:
|
||||
batch_size = b * t
|
||||
for i in range(0, b * t, batch_size):
|
||||
outputs.append(self.clip_model.get_image_features(x[i:i + batch_size]))
|
||||
x = torch.cat(outputs, dim=0)
|
||||
# x = self.clip_model.encode_image(x, normalize=True)
|
||||
x = rearrange(x, '(b t) d -> b t d', b=b)
|
||||
return x
|
||||
|
||||
@torch.inference_mode()
|
||||
def encode_video_with_sync(self, x: torch.Tensor, batch_size: int = -1) -> torch.Tensor:
|
||||
assert self.synchformer is not None, 'Synchformer is not loaded'
|
||||
# x: (B, T, C, H, W) H/W: 384
|
||||
b, t, c, h, w = x.shape
|
||||
assert c == 3 and h == 224 and w == 224
|
||||
|
||||
# Simple approach like original - let PyTorch handle dtype naturally
|
||||
print(f"🔧 Sync input: {x.shape} {x.dtype}")
|
||||
|
||||
# partition the video
|
||||
segment_size = 16
|
||||
step_size = 8
|
||||
num_segments = (t - segment_size) // step_size + 1
|
||||
segments = []
|
||||
for i in range(num_segments):
|
||||
segments.append(x[:, i * step_size:i * step_size + segment_size])
|
||||
x = torch.stack(segments, dim=1) # (B, S, T, C, H, W)
|
||||
|
||||
outputs = []
|
||||
if batch_size < 0:
|
||||
batch_size = b
|
||||
x = rearrange(x, 'b s t c h w -> (b s) 1 t c h w')
|
||||
for i in range(0, b * num_segments, batch_size):
|
||||
batch_input = x[i:i + batch_size]
|
||||
outputs.append(self.synchformer(batch_input))
|
||||
x = torch.cat(outputs, dim=0)
|
||||
x = rearrange(x, '(b s) 1 t d -> b (s t) d', b=b)
|
||||
return x
|
||||
|
||||
@torch.inference_mode()
|
||||
def encode_text(self, text: list[str]) -> torch.Tensor:
|
||||
assert self.clip_model is not None, 'CLIP is not loaded'
|
||||
# assert self.tokenizer is not None, 'Tokenizer is not loaded'
|
||||
# x: (B, L)
|
||||
tokens = self.clip_processor(text=text, truncation=True, max_length=77, padding="max_length",return_tensors="pt").to(self.device)
|
||||
|
||||
# Ensure tokens match model dtype
|
||||
target_dtype = next(self.clip_model.parameters()).dtype
|
||||
for key in tokens:
|
||||
if tokens[key].dtype.is_floating_point and tokens[key].dtype != target_dtype:
|
||||
tokens[key] = tokens[key].to(dtype=target_dtype)
|
||||
|
||||
return self.clip_model.get_text_features(**tokens)
|
||||
|
||||
@torch.inference_mode()
|
||||
def encode_t5_text(self, text: list[str]) -> torch.Tensor:
|
||||
assert self.t5_model is not None, 'T5 model is not loaded'
|
||||
assert self.t5_tokenizer is not None, 'T5 Tokenizer is not loaded'
|
||||
# x: (B, L)
|
||||
inputs = self.t5_tokenizer(text,
|
||||
truncation=True,
|
||||
max_length=77,
|
||||
padding="max_length",
|
||||
return_tensors="pt").to(self.device)
|
||||
|
||||
# Ensure inputs match model dtype
|
||||
target_dtype = next(self.t5_model.parameters()).dtype
|
||||
for key in inputs:
|
||||
if inputs[key].dtype.is_floating_point and inputs[key].dtype != target_dtype:
|
||||
inputs[key] = inputs[key].to(dtype=target_dtype)
|
||||
|
||||
return self.t5_model(**inputs).last_hidden_state
|
||||
|
||||
@torch.inference_mode()
|
||||
def encode_audio(self, x) -> torch.Tensor:
|
||||
x = self.vae.encode(x)
|
||||
return x
|
||||
|
||||
@property
|
||||
def device(self):
|
||||
return next(self.parameters()).device
|
||||
|
||||
@property
|
||||
def dtype(self):
|
||||
return next(self.parameters()).dtype
|
||||
@@ -0,0 +1,259 @@
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 384
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
|
||||
class VGGSound(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
videos = sorted(os.listdir(self.root))
|
||||
videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
# videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['caption']
|
||||
if id in videos:
|
||||
# self.labels.append(label)
|
||||
self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
else:
|
||||
missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
|
||||
self.sync_transform = v2.Compose([
|
||||
v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.CenterCrop(_SYNC_SIZE),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
||||
])
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(self.root / (video_id + '.mp4'))
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_SYNC_FPS * self.duration_sec),
|
||||
frame_rate=_SYNC_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_audio_stream(frames_per_chunk=2**30,)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
sync_chunk = data_chunk[1]
|
||||
audio_chunk = data_chunk[2]
|
||||
if len(audio_chunk.shape) != 2:
|
||||
raise RuntimeError(f'error audio shape {video_id}')
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
# if clip_chunk.shape[0] < self.clip_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'CLIP video too short {video_id}, expected {self.clip_expected_length}, got {clip_chunk.shape[0]}'
|
||||
# )
|
||||
|
||||
if sync_chunk is None:
|
||||
raise RuntimeError(f'Sync video returned None {video_id}')
|
||||
# if sync_chunk.shape[0] < self.sync_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'Sync video too short {video_id}, expected {self.sync_expected_length}, got {sync_chunk.shape[0]}'
|
||||
# )
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
# process audio
|
||||
sample_rate = int(reader.get_out_stream_info(2).sample_rate)
|
||||
audio_chunk = audio_chunk.transpose(0, 1)
|
||||
abs_max = audio_chunk[0].abs().max()
|
||||
# audio_chunk = audio_chunk.mean(dim=0) # mono
|
||||
# if self.normalize_audio:
|
||||
# abs_max = audio_chunk.abs().max()
|
||||
# audio_chunk = audio_chunk / abs_max * 0.95
|
||||
if abs_max <= 1e-6:
|
||||
if audio_chunk.shape[0] > 1 and audio_chunk[1].abs().max() > 1e-6:
|
||||
audio_chunk = audio_chunk[1:2]
|
||||
else:
|
||||
raise RuntimeError(f'Audio is silent {video_id}')
|
||||
|
||||
|
||||
# if abs_max <= 1e-6:
|
||||
# raise RuntimeError(f'Audio is silent {video_id}')
|
||||
|
||||
# ensure the stereo audio
|
||||
if audio_chunk.shape[0] < 2:
|
||||
audio_chunk = audio_chunk.repeat(2, 1)
|
||||
|
||||
# resample
|
||||
if sample_rate == self.sample_rate:
|
||||
audio_chunk = audio_chunk
|
||||
else:
|
||||
if sample_rate not in self.resampler:
|
||||
# https://pytorch.org/audio/stable/tutorials/audio_resampling_tutorial.html#kaiser-best
|
||||
self.resampler[sample_rate] = torchaudio.transforms.Resample(
|
||||
sample_rate,
|
||||
self.sample_rate,
|
||||
lowpass_filter_width=64,
|
||||
rolloff=0.9475937167399596,
|
||||
resampling_method='sinc_interp_kaiser',
|
||||
beta=14.769656459379492,
|
||||
)
|
||||
audio_chunk = self.resampler[sample_rate](audio_chunk)
|
||||
|
||||
if audio_chunk.shape[1] < self.expected_audio_length:
|
||||
# zero-padding audio
|
||||
padding_length = self.expected_audio_length - audio_chunk.shape[1]
|
||||
# 创建 padding 张量,大小为 [batch_size, padding_length],值为0
|
||||
padding = torch.zeros(audio_chunk.shape[0], padding_length)
|
||||
# 将原始音频和 padding 沿第 1 维度拼接在一起
|
||||
audio_chunk = torch.cat((audio_chunk, padding), dim=1)
|
||||
# raise RuntimeError(f'Audio too short {video_id}')
|
||||
audio_chunk = audio_chunk[:,:self.expected_audio_length]
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(last_frame.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = self.clip_transform(clip_chunk)
|
||||
# temp_img = clip_chunk[0].permute(1, 2, 0) * 255
|
||||
# save_image(clip_chunk[0],'scale.png')
|
||||
sync_chunk = sync_chunk[:self.sync_expected_length]
|
||||
if sync_chunk.shape[0] != self.sync_expected_length:
|
||||
# padding using the last frame, but no more than 2
|
||||
current_length = sync_chunk.shape[0]
|
||||
last_frame = sync_chunk[-1]
|
||||
# 重复最后一帧以进行填充
|
||||
padding = last_frame.repeat(self.sync_expected_length - current_length, 1, 1, 1)
|
||||
assert self.sync_expected_length - current_length < 12, f'sync can pad no more than 2 while {self.sync_expected_length - current_length}'
|
||||
sync_chunk = torch.cat((sync_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'Sync video wrong length {video_id}, '
|
||||
# f'expected {self.sync_expected_length}, '
|
||||
# f'got {sync_chunk.shape[0]}')
|
||||
|
||||
sync_chunk = self.sync_transform(sync_chunk)
|
||||
assert audio_chunk.shape[1] == self.expected_audio_length and clip_chunk.shape[0] == self.clip_expected_length \
|
||||
and sync_chunk.shape[0] == self.sync_expected_length, 'error processed data shape'
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'audio': audio_chunk,
|
||||
'clip_video': clip_chunk,
|
||||
'sync_video': sync_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/test",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_latents_text/test"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,320 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
def save_tensor_as_image(tensor, save_path):
|
||||
"""
|
||||
将形状为 (1, 3, H, W) 的 RGB 图像数组保存为图片文件。
|
||||
|
||||
:param tensor: 输入的 NumPy 数组 (1, 3, H, W)。
|
||||
:param save_path: 图片保存路径。
|
||||
"""
|
||||
# # 移除批次维度,变成 (3, H, W)
|
||||
# tensor = tensor.squeeze(0)
|
||||
|
||||
# 交换轴顺序,变为 (H, W, 3)
|
||||
image_array = np.transpose(tensor, (1, 2, 0))
|
||||
|
||||
# 检查数组是否为合适的数据类型
|
||||
if image_array.dtype != np.uint8:
|
||||
# 如果不是 uint8,首先标准化,然后转换
|
||||
image_array = (image_array - image_array.min()) / (image_array.max() - image_array.min()) * 255
|
||||
image_array = image_array.astype(np.uint8)
|
||||
|
||||
# 创建图像对象
|
||||
image = Image.fromarray(image_array)
|
||||
|
||||
# 保存图片
|
||||
image.save(save_path)
|
||||
print(f"Image saved to {save_path}")
|
||||
|
||||
def pad_to_square(video_tensor):
|
||||
# 验证输入的形状
|
||||
if len(video_tensor.shape) != 4:
|
||||
raise ValueError("Input tensor must have shape (l, c, h, w)")
|
||||
|
||||
l, c, h, w = video_tensor.shape
|
||||
max_side = max(h, w)
|
||||
|
||||
# 计算每一维度需要的填充量:(left, right, top, bottom)
|
||||
pad_h = max_side - h
|
||||
pad_w = max_side - w
|
||||
|
||||
# 创建padding tuple (left, right, top, bottom)
|
||||
# 因为图像的填充是作用在最后两个维度 h 和 w 上,所以我们需要指定这两个维度的填充
|
||||
padding = (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
|
||||
|
||||
# 使用F.pad对视频张量进行填充操作
|
||||
# 填充参数为 (left, right, top, bottom)
|
||||
video_padded = F.pad(video_tensor, pad=padding, mode='constant', value=0)
|
||||
|
||||
return video_padded
|
||||
|
||||
class VGGSound(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['label']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Lambda(pad_to_square), # 先填充为正方形
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
self.clip_processor = AutoProcessor.from_pretrained("facebook/metaclip-h14-fullcc2.5b")
|
||||
self.sync_transform = v2.Compose([
|
||||
v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.CenterCrop(_SYNC_SIZE),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
||||
])
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(self.root / (video_id + '.mp4'))
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_SYNC_FPS * self.duration_sec),
|
||||
frame_rate=_SYNC_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_audio_stream(frames_per_chunk=2**30,)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
sync_chunk = data_chunk[1]
|
||||
audio_chunk = data_chunk[2]
|
||||
if len(audio_chunk.shape) != 2:
|
||||
raise RuntimeError(f'error audio shape {video_id}')
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
# if clip_chunk.shape[0] < self.clip_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'CLIP video too short {video_id}, expected {self.clip_expected_length}, got {clip_chunk.shape[0]}'
|
||||
# )
|
||||
|
||||
if sync_chunk is None:
|
||||
raise RuntimeError(f'Sync video returned None {video_id}')
|
||||
# if sync_chunk.shape[0] < self.sync_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'Sync video too short {video_id}, expected {self.sync_expected_length}, got {sync_chunk.shape[0]}'
|
||||
# )
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
# process audio
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
sample_rate = int(reader.get_out_stream_info(2).sample_rate)
|
||||
audio_chunk = audio_chunk.transpose(0, 1)
|
||||
abs_max = audio_chunk[0].abs().max()
|
||||
# audio_chunk = audio_chunk.mean(dim=0) # mono
|
||||
# if self.normalize_audio:
|
||||
# abs_max = audio_chunk.abs().max()
|
||||
# audio_chunk = audio_chunk / abs_max * 0.95
|
||||
if abs_max <= 1e-6:
|
||||
if audio_chunk.shape[0] > 1 and audio_chunk[1].abs().max() > 1e-6:
|
||||
audio_chunk = audio_chunk[1:2]
|
||||
else:
|
||||
raise RuntimeError(f'Audio is silent {video_id}')
|
||||
|
||||
# ensure the stereo audio
|
||||
if audio_chunk.shape[0] < 2:
|
||||
audio_chunk = audio_chunk.repeat(2, 1)
|
||||
|
||||
# resample
|
||||
if sample_rate == self.sample_rate:
|
||||
audio_chunk = audio_chunk
|
||||
else:
|
||||
if sample_rate not in self.resampler:
|
||||
# https://pytorch.org/audio/stable/tutorials/audio_resampling_tutorial.html#kaiser-best
|
||||
self.resampler[sample_rate] = torchaudio.transforms.Resample(
|
||||
sample_rate,
|
||||
self.sample_rate,
|
||||
lowpass_filter_width=64,
|
||||
rolloff=0.9475937167399596,
|
||||
resampling_method='sinc_interp_kaiser',
|
||||
beta=14.769656459379492,
|
||||
)
|
||||
audio_chunk = self.resampler[sample_rate](audio_chunk)
|
||||
|
||||
if audio_chunk.shape[1] < self.expected_audio_length:
|
||||
# zero-padding audio
|
||||
padding_length = self.expected_audio_length - audio_chunk.shape[1]
|
||||
# 创建 padding 张量,大小为 [batch_size, padding_length],值为0
|
||||
padding = torch.zeros(audio_chunk.shape[0], padding_length)
|
||||
# 将原始音频和 padding 沿第 1 维度拼接在一起
|
||||
audio_chunk = torch.cat((audio_chunk, padding), dim=1)
|
||||
# raise RuntimeError(f'Audio too short {video_id}')
|
||||
audio_chunk = audio_chunk[:,:self.expected_audio_length]
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(last_frame.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = pad_to_square(clip_chunk)
|
||||
# save_image(clip_chunk[0] / 255.0,'square.png')
|
||||
# clip_chunk = self.clip_transform(clip_chunk)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
clip_chunk = self.clip_processor(images=clip_chunk, return_tensors="pt")["pixel_values"]
|
||||
# log.info(clip_chunk.shape)
|
||||
# save_tensor_as_image(clip_chunk[0].numpy(),'scale.png')
|
||||
# log.info(clip_chunk[0])
|
||||
# clip_chunk = outputs
|
||||
# text_ids = outputs["input_ids"]
|
||||
# temp_img = clip_chunk[0].permute(1, 2, 0) * 255
|
||||
# save_image(clip_chunk[0],'scale.png')
|
||||
sync_chunk = sync_chunk[:self.sync_expected_length]
|
||||
if sync_chunk.shape[0] != self.sync_expected_length:
|
||||
# padding using the last frame, but no more than 2
|
||||
current_length = sync_chunk.shape[0]
|
||||
last_frame = sync_chunk[-1]
|
||||
# 重复最后一帧以进行填充
|
||||
padding = last_frame.repeat(self.sync_expected_length - current_length, 1, 1, 1)
|
||||
assert self.sync_expected_length - current_length < 12, f'sync can pad no more than 2 while {self.sync_expected_length - current_length}'
|
||||
sync_chunk = torch.cat((sync_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'Sync video wrong length {video_id}, '
|
||||
# f'expected {self.sync_expected_length}, '
|
||||
# f'got {sync_chunk.shape[0]}')
|
||||
|
||||
sync_chunk = self.sync_transform(sync_chunk)
|
||||
assert audio_chunk.shape[1] == self.expected_audio_length and clip_chunk.shape[0] == self.clip_expected_length \
|
||||
and sync_chunk.shape[0] == self.sync_expected_length, 'error processed data shape'
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'audio': audio_chunk,
|
||||
'clip_video': clip_chunk,
|
||||
'sync_video': sync_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/train",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_224_latents_text/train"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,275 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
def save_tensor_as_image(tensor, save_path):
|
||||
"""
|
||||
将形状为 (1, 3, H, W) 的 RGB 图像数组保存为图片文件。
|
||||
|
||||
:param tensor: 输入的 NumPy 数组 (1, 3, H, W)。
|
||||
:param save_path: 图片保存路径。
|
||||
"""
|
||||
# # 移除批次维度,变成 (3, H, W)
|
||||
# tensor = tensor.squeeze(0)
|
||||
|
||||
# 交换轴顺序,变为 (H, W, 3)
|
||||
image_array = np.transpose(tensor, (1, 2, 0))
|
||||
|
||||
# 检查数组是否为合适的数据类型
|
||||
if image_array.dtype != np.uint8:
|
||||
# 如果不是 uint8,首先标准化,然后转换
|
||||
image_array = (image_array - image_array.min()) / (image_array.max() - image_array.min()) * 255
|
||||
image_array = image_array.astype(np.uint8)
|
||||
|
||||
# 创建图像对象
|
||||
image = Image.fromarray(image_array)
|
||||
|
||||
# 保存图片
|
||||
image.save(save_path)
|
||||
print(f"Image saved to {save_path}")
|
||||
|
||||
def pad_to_square(video_tensor):
|
||||
# 验证输入的形状
|
||||
if len(video_tensor.shape) != 4:
|
||||
raise ValueError("Input tensor must have shape (l, c, h, w)")
|
||||
|
||||
l, c, h, w = video_tensor.shape
|
||||
max_side = max(h, w)
|
||||
|
||||
# 计算每一维度需要的填充量:(left, right, top, bottom)
|
||||
pad_h = max_side - h
|
||||
pad_w = max_side - w
|
||||
|
||||
# 创建padding tuple (left, right, top, bottom)
|
||||
# 因为图像的填充是作用在最后两个维度 h 和 w 上,所以我们需要指定这两个维度的填充
|
||||
padding = (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
|
||||
|
||||
# 使用F.pad对视频张量进行填充操作
|
||||
# 填充参数为 (left, right, top, bottom)
|
||||
video_padded = F.pad(video_tensor, pad=padding, mode='constant', value=0)
|
||||
|
||||
return video_padded
|
||||
|
||||
class VGGSound(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
self.caption_cot = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['caption']
|
||||
caption_cot = record['caption_cot']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
self.caption_cot.append(caption_cot)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Lambda(pad_to_square), # 先填充为正方形
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
self.clip_processor = AutoProcessor.from_pretrained("facebook/metaclip-h14-fullcc2.5b")
|
||||
self.sync_transform = v2.Compose([
|
||||
v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.CenterCrop(_SYNC_SIZE),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
||||
])
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
caption_cot = self.caption_cot[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(self.root / (video_id + '.mp4'))
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_SYNC_FPS * self.duration_sec),
|
||||
frame_rate=_SYNC_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
# reader.add_basic_audio_stream(frames_per_chunk=2**30,)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
sync_chunk = data_chunk[1]
|
||||
# audio_chunk = data_chunk[2]
|
||||
# if len(audio_chunk.shape) != 2:
|
||||
# raise RuntimeError(f'error audio shape {video_id}')
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
# if clip_chunk.shape[0] < self.clip_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'CLIP video too short {video_id}, expected {self.clip_expected_length}, got {clip_chunk.shape[0]}'
|
||||
# )
|
||||
|
||||
if sync_chunk is None:
|
||||
raise RuntimeError(f'Sync video returned None {video_id}')
|
||||
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
# assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(last_frame.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = pad_to_square(clip_chunk)
|
||||
# save_image(clip_chunk[0] / 255.0,'square.png')
|
||||
# clip_chunk = self.clip_transform(clip_chunk)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
clip_chunk = self.clip_processor(images=clip_chunk, return_tensors="pt")["pixel_values"]
|
||||
# log.info(clip_chunk.shape)
|
||||
# save_tensor_as_image(clip_chunk[0].numpy(),'scale.png')
|
||||
# log.info(clip_chunk[0])
|
||||
# clip_chunk = outputs
|
||||
# text_ids = outputs["input_ids"]
|
||||
# temp_img = clip_chunk[0].permute(1, 2, 0) * 255
|
||||
# save_image(clip_chunk[0],'scale.png')
|
||||
sync_chunk = sync_chunk[:self.sync_expected_length]
|
||||
if sync_chunk.shape[0] != self.sync_expected_length:
|
||||
# padding using the last frame, but no more than 2
|
||||
current_length = sync_chunk.shape[0]
|
||||
last_frame = sync_chunk[-1]
|
||||
# 重复最后一帧以进行填充
|
||||
padding = last_frame.repeat(self.sync_expected_length - current_length, 1, 1, 1)
|
||||
# assert self.sync_expected_length - current_length < 12, f'sync can pad no more than 2 while {self.sync_expected_length - current_length}'
|
||||
sync_chunk = torch.cat((sync_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'Sync video wrong length {video_id}, '
|
||||
# f'expected {self.sync_expected_length}, '
|
||||
# f'got {sync_chunk.shape[0]}')
|
||||
|
||||
sync_chunk = self.sync_transform(sync_chunk)
|
||||
# assert audio_chunk.shape[1] == self.expected_audio_length and clip_chunk.shape[0] == self.clip_expected_length \
|
||||
# and sync_chunk.shape[0] == self.sync_expected_length, 'error processed data shape'
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
# 'audio': audio_chunk,
|
||||
'clip_video': clip_chunk,
|
||||
'sync_video': sync_chunk,
|
||||
'caption_cot': caption_cot,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/train",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_224_latents_text/train"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,223 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
def save_tensor_as_image(tensor, save_path):
|
||||
"""
|
||||
将形状为 (1, 3, H, W) 的 RGB 图像数组保存为图片文件。
|
||||
|
||||
:param tensor: 输入的 NumPy 数组 (1, 3, H, W)。
|
||||
:param save_path: 图片保存路径。
|
||||
"""
|
||||
# # 移除批次维度,变成 (3, H, W)
|
||||
# tensor = tensor.squeeze(0)
|
||||
|
||||
# 交换轴顺序,变为 (H, W, 3)
|
||||
image_array = np.transpose(tensor, (1, 2, 0))
|
||||
|
||||
# 检查数组是否为合适的数据类型
|
||||
if image_array.dtype != np.uint8:
|
||||
# 如果不是 uint8,首先标准化,然后转换
|
||||
image_array = (image_array - image_array.min()) / (image_array.max() - image_array.min()) * 255
|
||||
image_array = image_array.astype(np.uint8)
|
||||
|
||||
# 创建图像对象
|
||||
image = Image.fromarray(image_array)
|
||||
|
||||
# 保存图片
|
||||
image.save(save_path)
|
||||
print(f"Image saved to {save_path}")
|
||||
|
||||
def pad_to_square(video_tensor):
|
||||
# 验证输入的形状
|
||||
if len(video_tensor.shape) != 4:
|
||||
raise ValueError("Input tensor must have shape (l, c, h, w)")
|
||||
|
||||
l, c, h, w = video_tensor.shape
|
||||
max_side = max(h, w)
|
||||
|
||||
# 计算每一维度需要的填充量:(left, right, top, bottom)
|
||||
pad_h = max_side - h
|
||||
pad_w = max_side - w
|
||||
|
||||
# 创建padding tuple (left, right, top, bottom)
|
||||
# 因为图像的填充是作用在最后两个维度 h 和 w 上,所以我们需要指定这两个维度的填充
|
||||
padding = (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
|
||||
|
||||
# 使用F.pad对视频张量进行填充操作
|
||||
# 填充参数为 (left, right, top, bottom)
|
||||
video_padded = F.pad(video_tensor, pad=padding, mode='constant', value=0)
|
||||
|
||||
return video_padded
|
||||
|
||||
class VGGSound(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['label']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Lambda(pad_to_square), # 先填充为正方形
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
self.clip_processor = AutoProcessor.from_pretrained("useful_ckpts/metaclip-huge")
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(self.root / (video_id + '.mp4'))
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
|
||||
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(last_frame.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = pad_to_square(clip_chunk)
|
||||
# save_image(clip_chunk[0] / 255.0,'square.png')
|
||||
# clip_chunk = self.clip_transform(clip_chunk)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
clip_chunk = self.clip_processor(images=clip_chunk, return_tensors="pt")["pixel_values"]
|
||||
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'clip_video': clip_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/train",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_224_latents_text/train"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,109 @@
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 384
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
|
||||
class VGGSound(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.cots = []
|
||||
self.videos = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
# if os.path.exists(f'{save_dir}/{id}.pth'):
|
||||
# continue
|
||||
# try:
|
||||
# torch.load(f'{save_dir}/{id}.pth')
|
||||
# continue
|
||||
# except:
|
||||
# print(f'error load file: {save_dir}/{id}.pth')
|
||||
# os.system(f'rm -f {save_dir}/{id}.pth')
|
||||
label = record['caption']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
self.cots.append(record['caption_cot'])
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
|
||||
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
cot = self.cots[idx]
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'caption_cot': cot
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/test",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_latents_text/test"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,173 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
|
||||
class Audio_Text(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
self.cots = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['caption']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
# print(label,'debug1!!!!!!!!!')
|
||||
self.cots.append(record['caption_cot'])
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int):
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
cot = self.cots[idx]
|
||||
audio_path = os.path.join(self.root, f'{video_id}.wav')
|
||||
if not os.path.exists(audio_path):
|
||||
audio_path = os.path.join(self.root, f'{video_id}.flac')
|
||||
if not os.path.exists(audio_path):
|
||||
raise RuntimeError(f'Audio is not exist {audio_path}')
|
||||
audio_chunk, sample_rate = torchaudio.load(audio_path)
|
||||
if len(audio_chunk.shape) != 2:
|
||||
raise RuntimeError(f'error audio shape {video_id}')
|
||||
|
||||
abs_max = audio_chunk[0].abs().max()
|
||||
|
||||
if abs_max <= 1e-6:
|
||||
if audio_chunk.shape[0] > 1 and audio_chunk[1].abs().max() > 1e-6:
|
||||
audio_chunk = audio_chunk[1:2]
|
||||
else:
|
||||
raise RuntimeError(f'Audio is silent {video_id}')
|
||||
|
||||
# ensure the stereo audio
|
||||
if audio_chunk.shape[0] < 2:
|
||||
audio_chunk = audio_chunk.repeat(2, 1)
|
||||
elif audio_chunk.shape[0] > 2:
|
||||
audio_chunk = audio_chunk[:2]
|
||||
|
||||
# resample
|
||||
if sample_rate == self.sample_rate:
|
||||
audio_chunk = audio_chunk
|
||||
else:
|
||||
if sample_rate not in self.resampler:
|
||||
# https://pytorch.org/audio/stable/tutorials/audio_resampling_tutorial.html#kaiser-best
|
||||
self.resampler[sample_rate] = torchaudio.transforms.Resample(
|
||||
sample_rate,
|
||||
self.sample_rate,
|
||||
lowpass_filter_width=64,
|
||||
rolloff=0.9475937167399596,
|
||||
resampling_method='sinc_interp_kaiser',
|
||||
beta=14.769656459379492,
|
||||
)
|
||||
audio_chunk = self.resampler[sample_rate](audio_chunk)
|
||||
|
||||
if audio_chunk.shape[1] < self.expected_audio_length:
|
||||
# zero-padding audio
|
||||
padding_length = self.expected_audio_length - audio_chunk.shape[1]
|
||||
# 创建 padding 张量,大小为 [batch_size, padding_length],值为0
|
||||
padding = torch.zeros(audio_chunk.shape[0], padding_length)
|
||||
# 将原始音频和 padding 沿第 1 维度拼接在一起
|
||||
audio_chunk = torch.cat((audio_chunk, padding), dim=1)
|
||||
# raise RuntimeError(f'Audio too short {video_id}')
|
||||
audio_chunk = audio_chunk[:,:self.expected_audio_length]
|
||||
assert audio_chunk.shape == (2, 397312), f'error shape:{video_id},{audio_chunk.shape}'
|
||||
# print(label,'debug2!!!!!!!!!')
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'caption_cot': cot,
|
||||
'audio': audio_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int):
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/train",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_224_latents_text/train"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,315 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
def save_tensor_as_image(tensor, save_path):
|
||||
"""
|
||||
将形状为 (1, 3, H, W) 的 RGB 图像数组保存为图片文件。
|
||||
|
||||
:param tensor: 输入的 NumPy 数组 (1, 3, H, W)。
|
||||
:param save_path: 图片保存路径。
|
||||
"""
|
||||
# # 移除批次维度,变成 (3, H, W)
|
||||
# tensor = tensor.squeeze(0)
|
||||
|
||||
# 交换轴顺序,变为 (H, W, 3)
|
||||
image_array = np.transpose(tensor, (1, 2, 0))
|
||||
|
||||
# 检查数组是否为合适的数据类型
|
||||
if image_array.dtype != np.uint8:
|
||||
# 如果不是 uint8,首先标准化,然后转换
|
||||
image_array = (image_array - image_array.min()) / (image_array.max() - image_array.min()) * 255
|
||||
image_array = image_array.astype(np.uint8)
|
||||
|
||||
# 创建图像对象
|
||||
image = Image.fromarray(image_array)
|
||||
|
||||
# 保存图片
|
||||
image.save(save_path)
|
||||
print(f"Image saved to {save_path}")
|
||||
|
||||
def pad_to_square(video_tensor):
|
||||
# 验证输入的形状
|
||||
if len(video_tensor.shape) != 4:
|
||||
raise ValueError("Input tensor must have shape (l, c, h, w)")
|
||||
|
||||
l, c, h, w = video_tensor.shape
|
||||
max_side = max(h, w)
|
||||
|
||||
# 计算每一维度需要的填充量:(left, right, top, bottom)
|
||||
pad_h = max_side - h
|
||||
pad_w = max_side - w
|
||||
|
||||
# 创建padding tuple (left, right, top, bottom)
|
||||
# 因为图像的填充是作用在最后两个维度 h 和 w 上,所以我们需要指定这两个维度的填充
|
||||
padding = (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
|
||||
|
||||
# 使用F.pad对视频张量进行填充操作
|
||||
# 填充参数为 (left, right, top, bottom)
|
||||
video_padded = F.pad(video_tensor, pad=padding, mode='constant', value=0)
|
||||
|
||||
return video_padded
|
||||
|
||||
class Audioset(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
self.caption_t5s = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['label']
|
||||
caption_t5 = record['caption_t5']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
self.caption_t5s.append(caption_t5)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Lambda(pad_to_square), # 先填充为正方形
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
self.clip_processor = AutoProcessor.from_pretrained("useful_ckpts/metaclip-huge")
|
||||
self.sync_transform = v2.Compose([
|
||||
v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.CenterCrop(_SYNC_SIZE),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
||||
])
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
caption_t5 = self.caption_t5s[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(self.root / (video_id + '.mp4'))
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_SYNC_FPS * self.duration_sec),
|
||||
frame_rate=_SYNC_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
# reader.add_basic_audio_stream(frames_per_chunk=2**30,)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
sync_chunk = data_chunk[1]
|
||||
audio_path = os.path.join("dataset/3_Audioset/audios/sound",video_id+'.wav')
|
||||
assert os.path.exists(audio_path), f'{audio_path} not exists'
|
||||
audio_chunk, sr = torchaudio.load(audio_path)
|
||||
# audio_chunk = data_chunk[2]
|
||||
if len(audio_chunk.shape) != 2:
|
||||
raise RuntimeError(f'error audio shape {video_id}')
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
|
||||
if sync_chunk is None:
|
||||
raise RuntimeError(f'Sync video returned None {video_id}')
|
||||
sample_rate = int(sr)
|
||||
# audio_chunk = audio_chunk.transpose(0, 1)
|
||||
abs_max = audio_chunk[0].abs().max()
|
||||
# audio_chunk = audio_chunk.mean(dim=0) # mono
|
||||
# if self.normalize_audio:
|
||||
# abs_max = audio_chunk.abs().max()
|
||||
# audio_chunk = audio_chunk / abs_max * 0.95
|
||||
if abs_max <= 1e-6:
|
||||
if audio_chunk.shape[0] > 1 and audio_chunk[1].abs().max() > 1e-6:
|
||||
audio_chunk = audio_chunk[1:2]
|
||||
else:
|
||||
raise RuntimeError(f'Audio is silent {video_id}')
|
||||
|
||||
# ensure the stereo audio
|
||||
if audio_chunk.shape[0] < 2:
|
||||
audio_chunk = audio_chunk.repeat(2, 1)
|
||||
|
||||
# resample
|
||||
if sample_rate == self.sample_rate:
|
||||
audio_chunk = audio_chunk
|
||||
else:
|
||||
if sample_rate not in self.resampler:
|
||||
# https://pytorch.org/audio/stable/tutorials/audio_resampling_tutorial.html#kaiser-best
|
||||
self.resampler[sample_rate] = torchaudio.transforms.Resample(
|
||||
sample_rate,
|
||||
self.sample_rate,
|
||||
lowpass_filter_width=64,
|
||||
rolloff=0.9475937167399596,
|
||||
resampling_method='sinc_interp_kaiser',
|
||||
beta=14.769656459379492,
|
||||
)
|
||||
audio_chunk = self.resampler[sample_rate](audio_chunk)
|
||||
|
||||
if audio_chunk.shape[1] < self.expected_audio_length:
|
||||
# zero-padding audio
|
||||
padding_length = self.expected_audio_length - audio_chunk.shape[1]
|
||||
# 创建 padding 张量,大小为 [batch_size, padding_length],值为0
|
||||
padding = torch.zeros(audio_chunk.shape[0], padding_length)
|
||||
# 将原始音频和 padding 沿第 1 维度拼接在一起
|
||||
audio_chunk = torch.cat((audio_chunk, padding), dim=1)
|
||||
# raise RuntimeError(f'Audio too short {video_id}')
|
||||
audio_chunk = audio_chunk[:,:self.expected_audio_length]
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(last_frame.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = pad_to_square(clip_chunk)
|
||||
# save_image(clip_chunk[0] / 255.0,'square.png')
|
||||
# clip_chunk = self.clip_transform(clip_chunk)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
clip_chunk = self.clip_processor(images=clip_chunk, return_tensors="pt")["pixel_values"]
|
||||
# log.info(clip_chunk.shape)
|
||||
# save_tensor_as_image(clip_chunk[0].numpy(),'scale.png')
|
||||
# log.info(clip_chunk[0])
|
||||
# clip_chunk = outputs
|
||||
# text_ids = outputs["input_ids"]
|
||||
# temp_img = clip_chunk[0].permute(1, 2, 0) * 255
|
||||
# save_image(clip_chunk[0],'scale.png')
|
||||
sync_chunk = sync_chunk[:self.sync_expected_length]
|
||||
if sync_chunk.shape[0] != self.sync_expected_length:
|
||||
# padding using the last frame, but no more than 2
|
||||
current_length = sync_chunk.shape[0]
|
||||
last_frame = sync_chunk[-1]
|
||||
# 重复最后一帧以进行填充
|
||||
padding = last_frame.repeat(self.sync_expected_length - current_length, 1, 1, 1)
|
||||
assert self.sync_expected_length - current_length < 12, f'sync can pad no more than 2 while {self.sync_expected_length - current_length}'
|
||||
sync_chunk = torch.cat((sync_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'Sync video wrong length {video_id}, '
|
||||
# f'expected {self.sync_expected_length}, '
|
||||
# f'got {sync_chunk.shape[0]}')
|
||||
|
||||
sync_chunk = self.sync_transform(sync_chunk)
|
||||
assert audio_chunk.shape[1] == self.expected_audio_length and clip_chunk.shape[0] == self.clip_expected_length \
|
||||
and sync_chunk.shape[0] == self.sync_expected_length, 'error processed data shape'
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'caption_t5': caption_t5,
|
||||
'audio': audio_chunk,
|
||||
'clip_video': clip_chunk,
|
||||
'sync_video': sync_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = Audioset(
|
||||
# root="dataset/3_Audioset/video/sound",
|
||||
# tsv_path="dataset/3_Audioset/split_txt/unbalanced_sound_filtered_aligned_novgg_noout.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="dataset/3_Audioset/video_text_latents/"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,268 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
def save_tensor_as_image(tensor, save_path):
|
||||
"""
|
||||
将形状为 (1, 3, H, W) 的 RGB 图像数组保存为图片文件。
|
||||
|
||||
:param tensor: 输入的 NumPy 数组 (1, 3, H, W)。
|
||||
:param save_path: 图片保存路径。
|
||||
"""
|
||||
# # 移除批次维度,变成 (3, H, W)
|
||||
# tensor = tensor.squeeze(0)
|
||||
|
||||
# 交换轴顺序,变为 (H, W, 3)
|
||||
image_array = np.transpose(tensor, (1, 2, 0))
|
||||
|
||||
# 检查数组是否为合适的数据类型
|
||||
if image_array.dtype != np.uint8:
|
||||
# 如果不是 uint8,首先标准化,然后转换
|
||||
image_array = (image_array - image_array.min()) / (image_array.max() - image_array.min()) * 255
|
||||
image_array = image_array.astype(np.uint8)
|
||||
|
||||
# 创建图像对象
|
||||
image = Image.fromarray(image_array)
|
||||
|
||||
# 保存图片
|
||||
image.save(save_path)
|
||||
print(f"Image saved to {save_path}")
|
||||
|
||||
def pad_to_square(video_tensor):
|
||||
# 验证输入的形状
|
||||
if len(video_tensor.shape) != 4:
|
||||
raise ValueError("Input tensor must have shape (l, c, h, w)")
|
||||
|
||||
l, c, h, w = video_tensor.shape
|
||||
max_side = max(h, w)
|
||||
|
||||
# 计算每一维度需要的填充量:(left, right, top, bottom)
|
||||
pad_h = max_side - h
|
||||
pad_w = max_side - w
|
||||
|
||||
# 创建padding tuple (left, right, top, bottom)
|
||||
# 因为图像的填充是作用在最后两个维度 h 和 w 上,所以我们需要指定这两个维度的填充
|
||||
padding = (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
|
||||
|
||||
# 使用F.pad对视频张量进行填充操作
|
||||
# 填充参数为 (left, right, top, bottom)
|
||||
video_padded = F.pad(video_tensor, pad=padding, mode='constant', value=0)
|
||||
|
||||
return video_padded
|
||||
|
||||
class Audioset(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
duration_sec: float = 10.0,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.captions = []
|
||||
self.videos = []
|
||||
self.caption_t5s = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
with open(tsv_path.replace('.csv','.txt')) as file:
|
||||
paths = file.readlines()
|
||||
for record, path in zip(df_list,paths):
|
||||
id = Path(record['id']).stem
|
||||
# if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
caption = record['caption']
|
||||
caption_t5 = record['caption_t5']
|
||||
path = path.strip()
|
||||
part = Path(path).parent
|
||||
video_id = Path(path).stem[1:]
|
||||
video_path = os.path.join('dataset/3_Audioset/video',part,f'{video_id}.mp4')
|
||||
assert os.path.exists(video_path), 'video must exist'
|
||||
# if id in videos:
|
||||
self.captions.append(caption)
|
||||
self.caption_t5s.append(caption_t5)
|
||||
# self.labels[id] = label
|
||||
self.videos.append(video_path)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
assert len(self.captions) == len(self.caption_t5s) and len(self.captions) == len(self.videos), 'error length'
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Lambda(pad_to_square), # 先填充为正方形
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
self.clip_processor = AutoProcessor.from_pretrained("useful_ckpts/metaclip-huge")
|
||||
self.sync_transform = v2.Compose([
|
||||
v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.CenterCrop(_SYNC_SIZE),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
||||
])
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_path = self.videos[idx]
|
||||
video_id = 'Y'+str(Path(video_path).stem)
|
||||
caption = self.captions[idx]
|
||||
caption_t5 = self.caption_t5s[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(video_path)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_SYNC_FPS * self.duration_sec),
|
||||
frame_rate=_SYNC_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
sync_chunk = data_chunk[1]
|
||||
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
# if clip_chunk.shape[0] < self.clip_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'CLIP video too short {video_id}, expected {self.clip_expected_length}, got {clip_chunk.shape[0]}'
|
||||
# )
|
||||
|
||||
if sync_chunk is None:
|
||||
raise RuntimeError(f'Sync video returned None {video_id}')
|
||||
# if sync_chunk.shape[0] < self.sync_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'Sync video too short {video_id}, expected {self.sync_expected_length}, got {sync_chunk.shape[0]}'
|
||||
# )
|
||||
|
||||
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(clip_chunk.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = pad_to_square(clip_chunk)
|
||||
# save_image(clip_chunk[0] / 255.0,'square.png')
|
||||
# clip_chunk = self.clip_transform(clip_chunk)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
clip_chunk = self.clip_processor(images=clip_chunk, return_tensors="pt")["pixel_values"]
|
||||
# log.info(clip_chunk.shape)
|
||||
# save_tensor_as_image(clip_chunk[0].numpy(),'scale.png')
|
||||
# log.info(clip_chunk[0])
|
||||
# clip_chunk = outputs
|
||||
# text_ids = outputs["input_ids"]
|
||||
# temp_img = clip_chunk[0].permute(1, 2, 0) * 255
|
||||
# save_image(clip_chunk[0],'scale.png')
|
||||
sync_chunk = sync_chunk[:self.sync_expected_length]
|
||||
if sync_chunk.shape[0] != self.sync_expected_length:
|
||||
# padding using the last frame, but no more than 2
|
||||
current_length = sync_chunk.shape[0]
|
||||
last_frame = sync_chunk[-1]
|
||||
# 重复最后一帧以进行填充
|
||||
padding = last_frame.repeat(self.sync_expected_length - current_length, 1, 1, 1)
|
||||
assert self.sync_expected_length - current_length < 12, f'sync can pad no more than 2 while {self.sync_expected_length - current_length}'
|
||||
sync_chunk = torch.cat((sync_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'Sync video wrong length {video_id}, '
|
||||
# f'expected {self.sync_expected_length}, '
|
||||
# f'got {sync_chunk.shape[0]}')
|
||||
|
||||
sync_chunk = self.sync_transform(sync_chunk)
|
||||
assert clip_chunk.shape[0] == self.clip_expected_length and sync_chunk.shape[0] == self.sync_expected_length, 'error processed data shape'
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': caption,
|
||||
'caption_t5': caption_t5,
|
||||
'clip_video': clip_chunk,
|
||||
'sync_video': sync_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.captions)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/train",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_224_latents_text/train"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,269 @@
|
||||
from typing import Literal, Optional
|
||||
import json
|
||||
import open_clip
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
from einops import rearrange
|
||||
from open_clip import create_model_from_pretrained
|
||||
from torchvision.transforms import Normalize
|
||||
from ThinkSound.models.factory import create_model_from_config
|
||||
from ThinkSound.models.utils import load_ckpt_state_dict
|
||||
from ThinkSound.training.utils import copy_state_dict
|
||||
from transformers import AutoModel
|
||||
from transformers import AutoProcessor
|
||||
from transformers import T5EncoderModel, AutoTokenizer
|
||||
import logging
|
||||
from data_utils.ext.synchformer import Synchformer
|
||||
import os
|
||||
|
||||
log = logging.getLogger()
|
||||
|
||||
def patch_clip(clip_model):
|
||||
# a hack to make it output last hidden states
|
||||
# https://github.com/mlfoundations/open_clip/blob/fc5a37b72d705f760ebbc7915b84729816ed471f/src/open_clip/model.py#L269
|
||||
def new_get_text_features(self, input_ids=None, attention_mask=None, position_ids=None,
|
||||
output_attentions: Optional[bool] = None,
|
||||
output_hidden_states: Optional[bool] = None,
|
||||
return_dict: Optional[bool] = None):
|
||||
output_attentions = output_attentions if output_attentions is not None else self.config.output_attentions
|
||||
output_hidden_states = (
|
||||
output_hidden_states if output_hidden_states is not None else self.config.output_hidden_states
|
||||
)
|
||||
return_dict = return_dict if return_dict is not None else self.config.use_return_dict
|
||||
|
||||
text_outputs = self.text_model(
|
||||
input_ids=input_ids,
|
||||
attention_mask=attention_mask,
|
||||
position_ids=position_ids,
|
||||
output_attentions=output_attentions,
|
||||
output_hidden_states=output_hidden_states,
|
||||
return_dict=return_dict,
|
||||
)
|
||||
last_hidden_state = text_outputs[0]
|
||||
pooled_output = text_outputs[1]
|
||||
text_features = self.text_projection(pooled_output)
|
||||
|
||||
return text_features, last_hidden_state
|
||||
|
||||
clip_model.get_text_features = new_get_text_features.__get__(clip_model)
|
||||
return clip_model
|
||||
|
||||
|
||||
class FeaturesUtils(nn.Module):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
vae_ckpt: Optional[str] = None,
|
||||
vae_config: Optional[str] = None,
|
||||
synchformer_ckpt: Optional[str] = None,
|
||||
enable_conditions: bool = True,
|
||||
need_vae_encoder: bool = True,
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
if enable_conditions:
|
||||
# Try to use local models first, fallback to online
|
||||
try:
|
||||
# Import folder_paths to get ComfyUI models directory
|
||||
import folder_paths
|
||||
models_dir = folder_paths.models_dir
|
||||
print(f"🔍 DEBUG: Using ComfyUI models directory: {models_dir}")
|
||||
except ImportError:
|
||||
# Fallback if folder_paths not available
|
||||
models_dir = None
|
||||
print("⚠️ WARNING: folder_paths not available, using online models")
|
||||
|
||||
# MetaCLIP model paths
|
||||
if models_dir:
|
||||
metaclip_local_path = os.path.join(models_dir, "thinksound", "metaclip-h14-fullcc2.5b")
|
||||
if os.path.exists(metaclip_local_path):
|
||||
print(f"✅ Using local MetaCLIP model: {metaclip_local_path}")
|
||||
metaclip_path = metaclip_local_path
|
||||
else:
|
||||
print(f"❌ Local MetaCLIP not found at {metaclip_local_path}, using online")
|
||||
metaclip_path = "facebook/metaclip-h14-fullcc2.5b"
|
||||
else:
|
||||
metaclip_path = "facebook/metaclip-h14-fullcc2.5b"
|
||||
|
||||
# T5 model paths
|
||||
if models_dir:
|
||||
t5_local_path = os.path.join(models_dir, "t5-v1_1-xl")
|
||||
if os.path.exists(t5_local_path):
|
||||
print(f"✅ Using local T5 model: {t5_local_path}")
|
||||
t5_path = t5_local_path
|
||||
else:
|
||||
print(f"❌ Local T5 not found at {t5_local_path}, using online")
|
||||
t5_path = "google/t5-v1_1-xl"
|
||||
else:
|
||||
t5_path = "google/t5-v1_1-xl"
|
||||
|
||||
# Load models with local/online paths
|
||||
try:
|
||||
print(f"🔄 Loading MetaCLIP model from: {metaclip_path}")
|
||||
self.clip_model = AutoModel.from_pretrained(
|
||||
metaclip_path,
|
||||
local_files_only=(models_dir is not None and os.path.exists(metaclip_path))
|
||||
)
|
||||
self.clip_model = patch_clip(self.clip_model)
|
||||
|
||||
print(f"🔄 Loading MetaCLIP processor from: {metaclip_path}")
|
||||
self.clip_processor = AutoProcessor.from_pretrained(
|
||||
metaclip_path,
|
||||
local_files_only=(models_dir is not None and os.path.exists(metaclip_path))
|
||||
)
|
||||
print("✅ MetaCLIP model and processor loaded successfully")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to load MetaCLIP: {e}")
|
||||
print("🔄 Trying without local_files_only flag...")
|
||||
try:
|
||||
self.clip_model = AutoModel.from_pretrained(metaclip_path)
|
||||
self.clip_model = patch_clip(self.clip_model)
|
||||
self.clip_processor = AutoProcessor.from_pretrained(metaclip_path)
|
||||
print("✅ MetaCLIP loaded with fallback method")
|
||||
except Exception as e2:
|
||||
print(f"❌ Failed to load MetaCLIP with fallback: {e2}")
|
||||
raise
|
||||
|
||||
try:
|
||||
print(f"🔄 Loading T5 tokenizer from: {t5_path}")
|
||||
self.t5_tokenizer = AutoTokenizer.from_pretrained(
|
||||
t5_path,
|
||||
local_files_only=(models_dir is not None and os.path.exists(t5_path))
|
||||
)
|
||||
|
||||
print(f"🔄 Loading T5 model from: {t5_path}")
|
||||
self.t5_model = T5EncoderModel.from_pretrained(
|
||||
t5_path,
|
||||
local_files_only=(models_dir is not None and os.path.exists(t5_path))
|
||||
)
|
||||
print("✅ T5 model and tokenizer loaded successfully")
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Failed to load T5: {e}")
|
||||
print("🔄 Trying without local_files_only flag...")
|
||||
try:
|
||||
self.t5_tokenizer = AutoTokenizer.from_pretrained(t5_path)
|
||||
self.t5_model = T5EncoderModel.from_pretrained(t5_path)
|
||||
print("✅ T5 loaded with fallback method")
|
||||
except Exception as e2:
|
||||
print(f"❌ Failed to load T5 with fallback: {e2}")
|
||||
raise
|
||||
|
||||
# Load Synchformer
|
||||
print(f"🔄 Loading Synchformer from: {synchformer_ckpt}")
|
||||
self.synchformer = Synchformer()
|
||||
self.synchformer.load_state_dict(
|
||||
torch.load(synchformer_ckpt, weights_only=True, map_location='cpu'))
|
||||
print("✅ Synchformer loaded successfully")
|
||||
|
||||
# self.tokenizer = open_clip.get_tokenizer('ViT-H-14-378-quickgelu') # same as 'ViT-H-14'
|
||||
else:
|
||||
self.clip_model = None
|
||||
self.synchformer = None
|
||||
self.tokenizer = None
|
||||
|
||||
if vae_ckpt is not None:
|
||||
print(f"🔄 Loading VAE config from: {vae_config}")
|
||||
with open(vae_config) as f:
|
||||
vae_config = json.load(f)
|
||||
self.vae = create_model_from_config(vae_config)
|
||||
print(f"🔄 Loading VAE checkpoint from: {vae_ckpt}")
|
||||
# Load checkpoint
|
||||
copy_state_dict(self.vae, load_ckpt_state_dict(vae_ckpt,prefix='autoencoder.'))#,prefix='autoencoder.'
|
||||
print("✅ VAE loaded successfully")
|
||||
else:
|
||||
self.vae = None
|
||||
|
||||
def compile(self):
|
||||
if self.clip_model is not None:
|
||||
self.clip_model.encode_image = torch.compile(self.clip_model.encode_image)
|
||||
self.clip_model.encode_text = torch.compile(self.clip_model.encode_text)
|
||||
if self.synchformer is not None:
|
||||
self.synchformer = torch.compile(self.synchformer)
|
||||
|
||||
|
||||
def train(self, mode: bool) -> None:
|
||||
return super().train(False)
|
||||
|
||||
@torch.inference_mode()
|
||||
def encode_video_with_clip(self, x: torch.Tensor, batch_size: int = -1) -> torch.Tensor:
|
||||
assert self.clip_model is not None, 'CLIP is not loaded'
|
||||
# x: (B, T, C, H, W) H/W: 384
|
||||
b, t, c, h, w = x.shape
|
||||
|
||||
assert c == 3 and h == 224 and w == 224
|
||||
# x = self.clip_preprocess(x)
|
||||
x = rearrange(x, 'b t c h w -> (b t) c h w')
|
||||
outputs = []
|
||||
if batch_size < 0:
|
||||
batch_size = b * t
|
||||
for i in range(0, b * t, batch_size):
|
||||
outputs.append(self.clip_model.get_image_features(x[i:i + batch_size]))
|
||||
x = torch.cat(outputs, dim=0)
|
||||
# x = self.clip_model.encode_image(x, normalize=True)
|
||||
x = rearrange(x, '(b t) d -> b t d', b=b)
|
||||
return x
|
||||
|
||||
@torch.inference_mode()
|
||||
def encode_video_with_sync(self, x: torch.Tensor, batch_size: int = -1) -> torch.Tensor:
|
||||
assert self.synchformer is not None, 'Synchformer is not loaded'
|
||||
# x: (B, T, C, H, W) H/W: 384
|
||||
b, t, c, h, w = x.shape
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
assert c == 3 and h == 224 and w == 224
|
||||
|
||||
# partition the video
|
||||
segment_size = 16
|
||||
step_size = 8
|
||||
num_segments = (t - segment_size) // step_size + 1
|
||||
segments = []
|
||||
for i in range(num_segments):
|
||||
segments.append(x[:, i * step_size:i * step_size + segment_size])
|
||||
x = torch.stack(segments, dim=1) # (B, S, T, C, H, W)
|
||||
|
||||
outputs = []
|
||||
if batch_size < 0:
|
||||
batch_size = b
|
||||
x = rearrange(x, 'b s t c h w -> (b s) 1 t c h w')
|
||||
for i in range(0, b * num_segments, batch_size):
|
||||
outputs.append(self.synchformer(x[i:i + batch_size]))
|
||||
x = torch.cat(outputs, dim=0)
|
||||
x = rearrange(x, '(b s) 1 t d -> b (s t) d', b=b)
|
||||
return x
|
||||
|
||||
@torch.inference_mode()
|
||||
def encode_text(self, text: list[str]) -> torch.Tensor:
|
||||
assert self.clip_model is not None, 'CLIP is not loaded'
|
||||
# assert self.tokenizer is not None, 'Tokenizer is not loaded'
|
||||
# x: (B, L)
|
||||
tokens = self.clip_processor(text=text, truncation=True, max_length=77, padding="max_length",return_tensors="pt").to(self.device)
|
||||
return self.clip_model.get_text_features(**tokens)
|
||||
|
||||
@torch.inference_mode()
|
||||
def encode_t5_text(self, text: list[str]) -> torch.Tensor:
|
||||
assert self.t5_model is not None, 'T5 model is not loaded'
|
||||
assert self.t5_tokenizer is not None, 'T5 Tokenizer is not loaded'
|
||||
# x: (B, L)
|
||||
inputs = self.t5_tokenizer(text,
|
||||
truncation=True,
|
||||
max_length=77,
|
||||
padding="max_length",
|
||||
return_tensors="pt").to(self.device)
|
||||
return self.t5_model(**inputs).last_hidden_state
|
||||
|
||||
@torch.inference_mode()
|
||||
def encode_audio(self, x) -> torch.Tensor:
|
||||
x = self.vae.encode(x)
|
||||
return x
|
||||
|
||||
@property
|
||||
def device(self):
|
||||
return next(self.parameters()).device
|
||||
|
||||
@property
|
||||
def dtype(self):
|
||||
return next(self.parameters()).dtype
|
||||
@@ -0,0 +1,259 @@
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 384
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
|
||||
class VGGSound(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
videos = sorted(os.listdir(self.root))
|
||||
videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
# videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['caption']
|
||||
if id in videos:
|
||||
# self.labels.append(label)
|
||||
self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
else:
|
||||
missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
|
||||
self.sync_transform = v2.Compose([
|
||||
v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.CenterCrop(_SYNC_SIZE),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
||||
])
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(self.root / (video_id + '.mp4'))
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_SYNC_FPS * self.duration_sec),
|
||||
frame_rate=_SYNC_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_audio_stream(frames_per_chunk=2**30,)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
sync_chunk = data_chunk[1]
|
||||
audio_chunk = data_chunk[2]
|
||||
if len(audio_chunk.shape) != 2:
|
||||
raise RuntimeError(f'error audio shape {video_id}')
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
# if clip_chunk.shape[0] < self.clip_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'CLIP video too short {video_id}, expected {self.clip_expected_length}, got {clip_chunk.shape[0]}'
|
||||
# )
|
||||
|
||||
if sync_chunk is None:
|
||||
raise RuntimeError(f'Sync video returned None {video_id}')
|
||||
# if sync_chunk.shape[0] < self.sync_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'Sync video too short {video_id}, expected {self.sync_expected_length}, got {sync_chunk.shape[0]}'
|
||||
# )
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
# process audio
|
||||
sample_rate = int(reader.get_out_stream_info(2).sample_rate)
|
||||
audio_chunk = audio_chunk.transpose(0, 1)
|
||||
abs_max = audio_chunk[0].abs().max()
|
||||
# audio_chunk = audio_chunk.mean(dim=0) # mono
|
||||
# if self.normalize_audio:
|
||||
# abs_max = audio_chunk.abs().max()
|
||||
# audio_chunk = audio_chunk / abs_max * 0.95
|
||||
if abs_max <= 1e-6:
|
||||
if audio_chunk.shape[0] > 1 and audio_chunk[1].abs().max() > 1e-6:
|
||||
audio_chunk = audio_chunk[1:2]
|
||||
else:
|
||||
raise RuntimeError(f'Audio is silent {video_id}')
|
||||
|
||||
|
||||
# if abs_max <= 1e-6:
|
||||
# raise RuntimeError(f'Audio is silent {video_id}')
|
||||
|
||||
# ensure the stereo audio
|
||||
if audio_chunk.shape[0] < 2:
|
||||
audio_chunk = audio_chunk.repeat(2, 1)
|
||||
|
||||
# resample
|
||||
if sample_rate == self.sample_rate:
|
||||
audio_chunk = audio_chunk
|
||||
else:
|
||||
if sample_rate not in self.resampler:
|
||||
# https://pytorch.org/audio/stable/tutorials/audio_resampling_tutorial.html#kaiser-best
|
||||
self.resampler[sample_rate] = torchaudio.transforms.Resample(
|
||||
sample_rate,
|
||||
self.sample_rate,
|
||||
lowpass_filter_width=64,
|
||||
rolloff=0.9475937167399596,
|
||||
resampling_method='sinc_interp_kaiser',
|
||||
beta=14.769656459379492,
|
||||
)
|
||||
audio_chunk = self.resampler[sample_rate](audio_chunk)
|
||||
|
||||
if audio_chunk.shape[1] < self.expected_audio_length:
|
||||
# zero-padding audio
|
||||
padding_length = self.expected_audio_length - audio_chunk.shape[1]
|
||||
# 创建 padding 张量,大小为 [batch_size, padding_length],值为0
|
||||
padding = torch.zeros(audio_chunk.shape[0], padding_length)
|
||||
# 将原始音频和 padding 沿第 1 维度拼接在一起
|
||||
audio_chunk = torch.cat((audio_chunk, padding), dim=1)
|
||||
# raise RuntimeError(f'Audio too short {video_id}')
|
||||
audio_chunk = audio_chunk[:,:self.expected_audio_length]
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(last_frame.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = self.clip_transform(clip_chunk)
|
||||
# temp_img = clip_chunk[0].permute(1, 2, 0) * 255
|
||||
# save_image(clip_chunk[0],'scale.png')
|
||||
sync_chunk = sync_chunk[:self.sync_expected_length]
|
||||
if sync_chunk.shape[0] != self.sync_expected_length:
|
||||
# padding using the last frame, but no more than 2
|
||||
current_length = sync_chunk.shape[0]
|
||||
last_frame = sync_chunk[-1]
|
||||
# 重复最后一帧以进行填充
|
||||
padding = last_frame.repeat(self.sync_expected_length - current_length, 1, 1, 1)
|
||||
assert self.sync_expected_length - current_length < 12, f'sync can pad no more than 2 while {self.sync_expected_length - current_length}'
|
||||
sync_chunk = torch.cat((sync_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'Sync video wrong length {video_id}, '
|
||||
# f'expected {self.sync_expected_length}, '
|
||||
# f'got {sync_chunk.shape[0]}')
|
||||
|
||||
sync_chunk = self.sync_transform(sync_chunk)
|
||||
assert audio_chunk.shape[1] == self.expected_audio_length and clip_chunk.shape[0] == self.clip_expected_length \
|
||||
and sync_chunk.shape[0] == self.sync_expected_length, 'error processed data shape'
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'audio': audio_chunk,
|
||||
'clip_video': clip_chunk,
|
||||
'sync_video': sync_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/test",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_latents_text/test"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,320 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
def save_tensor_as_image(tensor, save_path):
|
||||
"""
|
||||
将形状为 (1, 3, H, W) 的 RGB 图像数组保存为图片文件。
|
||||
|
||||
:param tensor: 输入的 NumPy 数组 (1, 3, H, W)。
|
||||
:param save_path: 图片保存路径。
|
||||
"""
|
||||
# # 移除批次维度,变成 (3, H, W)
|
||||
# tensor = tensor.squeeze(0)
|
||||
|
||||
# 交换轴顺序,变为 (H, W, 3)
|
||||
image_array = np.transpose(tensor, (1, 2, 0))
|
||||
|
||||
# 检查数组是否为合适的数据类型
|
||||
if image_array.dtype != np.uint8:
|
||||
# 如果不是 uint8,首先标准化,然后转换
|
||||
image_array = (image_array - image_array.min()) / (image_array.max() - image_array.min()) * 255
|
||||
image_array = image_array.astype(np.uint8)
|
||||
|
||||
# 创建图像对象
|
||||
image = Image.fromarray(image_array)
|
||||
|
||||
# 保存图片
|
||||
image.save(save_path)
|
||||
print(f"Image saved to {save_path}")
|
||||
|
||||
def pad_to_square(video_tensor):
|
||||
# 验证输入的形状
|
||||
if len(video_tensor.shape) != 4:
|
||||
raise ValueError("Input tensor must have shape (l, c, h, w)")
|
||||
|
||||
l, c, h, w = video_tensor.shape
|
||||
max_side = max(h, w)
|
||||
|
||||
# 计算每一维度需要的填充量:(left, right, top, bottom)
|
||||
pad_h = max_side - h
|
||||
pad_w = max_side - w
|
||||
|
||||
# 创建padding tuple (left, right, top, bottom)
|
||||
# 因为图像的填充是作用在最后两个维度 h 和 w 上,所以我们需要指定这两个维度的填充
|
||||
padding = (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
|
||||
|
||||
# 使用F.pad对视频张量进行填充操作
|
||||
# 填充参数为 (left, right, top, bottom)
|
||||
video_padded = F.pad(video_tensor, pad=padding, mode='constant', value=0)
|
||||
|
||||
return video_padded
|
||||
|
||||
class VGGSound(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['label']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Lambda(pad_to_square), # 先填充为正方形
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
self.clip_processor = AutoProcessor.from_pretrained("facebook/metaclip-h14-fullcc2.5b")
|
||||
self.sync_transform = v2.Compose([
|
||||
v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.CenterCrop(_SYNC_SIZE),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
||||
])
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(self.root / (video_id + '.mp4'))
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_SYNC_FPS * self.duration_sec),
|
||||
frame_rate=_SYNC_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_audio_stream(frames_per_chunk=2**30,)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
sync_chunk = data_chunk[1]
|
||||
audio_chunk = data_chunk[2]
|
||||
if len(audio_chunk.shape) != 2:
|
||||
raise RuntimeError(f'error audio shape {video_id}')
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
# if clip_chunk.shape[0] < self.clip_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'CLIP video too short {video_id}, expected {self.clip_expected_length}, got {clip_chunk.shape[0]}'
|
||||
# )
|
||||
|
||||
if sync_chunk is None:
|
||||
raise RuntimeError(f'Sync video returned None {video_id}')
|
||||
# if sync_chunk.shape[0] < self.sync_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'Sync video too short {video_id}, expected {self.sync_expected_length}, got {sync_chunk.shape[0]}'
|
||||
# )
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
# process audio
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
sample_rate = int(reader.get_out_stream_info(2).sample_rate)
|
||||
audio_chunk = audio_chunk.transpose(0, 1)
|
||||
abs_max = audio_chunk[0].abs().max()
|
||||
# audio_chunk = audio_chunk.mean(dim=0) # mono
|
||||
# if self.normalize_audio:
|
||||
# abs_max = audio_chunk.abs().max()
|
||||
# audio_chunk = audio_chunk / abs_max * 0.95
|
||||
if abs_max <= 1e-6:
|
||||
if audio_chunk.shape[0] > 1 and audio_chunk[1].abs().max() > 1e-6:
|
||||
audio_chunk = audio_chunk[1:2]
|
||||
else:
|
||||
raise RuntimeError(f'Audio is silent {video_id}')
|
||||
|
||||
# ensure the stereo audio
|
||||
if audio_chunk.shape[0] < 2:
|
||||
audio_chunk = audio_chunk.repeat(2, 1)
|
||||
|
||||
# resample
|
||||
if sample_rate == self.sample_rate:
|
||||
audio_chunk = audio_chunk
|
||||
else:
|
||||
if sample_rate not in self.resampler:
|
||||
# https://pytorch.org/audio/stable/tutorials/audio_resampling_tutorial.html#kaiser-best
|
||||
self.resampler[sample_rate] = torchaudio.transforms.Resample(
|
||||
sample_rate,
|
||||
self.sample_rate,
|
||||
lowpass_filter_width=64,
|
||||
rolloff=0.9475937167399596,
|
||||
resampling_method='sinc_interp_kaiser',
|
||||
beta=14.769656459379492,
|
||||
)
|
||||
audio_chunk = self.resampler[sample_rate](audio_chunk)
|
||||
|
||||
if audio_chunk.shape[1] < self.expected_audio_length:
|
||||
# zero-padding audio
|
||||
padding_length = self.expected_audio_length - audio_chunk.shape[1]
|
||||
# 创建 padding 张量,大小为 [batch_size, padding_length],值为0
|
||||
padding = torch.zeros(audio_chunk.shape[0], padding_length)
|
||||
# 将原始音频和 padding 沿第 1 维度拼接在一起
|
||||
audio_chunk = torch.cat((audio_chunk, padding), dim=1)
|
||||
# raise RuntimeError(f'Audio too short {video_id}')
|
||||
audio_chunk = audio_chunk[:,:self.expected_audio_length]
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(last_frame.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = pad_to_square(clip_chunk)
|
||||
# save_image(clip_chunk[0] / 255.0,'square.png')
|
||||
# clip_chunk = self.clip_transform(clip_chunk)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
clip_chunk = self.clip_processor(images=clip_chunk, return_tensors="pt")["pixel_values"]
|
||||
# log.info(clip_chunk.shape)
|
||||
# save_tensor_as_image(clip_chunk[0].numpy(),'scale.png')
|
||||
# log.info(clip_chunk[0])
|
||||
# clip_chunk = outputs
|
||||
# text_ids = outputs["input_ids"]
|
||||
# temp_img = clip_chunk[0].permute(1, 2, 0) * 255
|
||||
# save_image(clip_chunk[0],'scale.png')
|
||||
sync_chunk = sync_chunk[:self.sync_expected_length]
|
||||
if sync_chunk.shape[0] != self.sync_expected_length:
|
||||
# padding using the last frame, but no more than 2
|
||||
current_length = sync_chunk.shape[0]
|
||||
last_frame = sync_chunk[-1]
|
||||
# 重复最后一帧以进行填充
|
||||
padding = last_frame.repeat(self.sync_expected_length - current_length, 1, 1, 1)
|
||||
assert self.sync_expected_length - current_length < 12, f'sync can pad no more than 2 while {self.sync_expected_length - current_length}'
|
||||
sync_chunk = torch.cat((sync_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'Sync video wrong length {video_id}, '
|
||||
# f'expected {self.sync_expected_length}, '
|
||||
# f'got {sync_chunk.shape[0]}')
|
||||
|
||||
sync_chunk = self.sync_transform(sync_chunk)
|
||||
assert audio_chunk.shape[1] == self.expected_audio_length and clip_chunk.shape[0] == self.clip_expected_length \
|
||||
and sync_chunk.shape[0] == self.sync_expected_length, 'error processed data shape'
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'audio': audio_chunk,
|
||||
'clip_video': clip_chunk,
|
||||
'sync_video': sync_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/train",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_224_latents_text/train"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,275 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
def save_tensor_as_image(tensor, save_path):
|
||||
"""
|
||||
将形状为 (1, 3, H, W) 的 RGB 图像数组保存为图片文件。
|
||||
|
||||
:param tensor: 输入的 NumPy 数组 (1, 3, H, W)。
|
||||
:param save_path: 图片保存路径。
|
||||
"""
|
||||
# # 移除批次维度,变成 (3, H, W)
|
||||
# tensor = tensor.squeeze(0)
|
||||
|
||||
# 交换轴顺序,变为 (H, W, 3)
|
||||
image_array = np.transpose(tensor, (1, 2, 0))
|
||||
|
||||
# 检查数组是否为合适的数据类型
|
||||
if image_array.dtype != np.uint8:
|
||||
# 如果不是 uint8,首先标准化,然后转换
|
||||
image_array = (image_array - image_array.min()) / (image_array.max() - image_array.min()) * 255
|
||||
image_array = image_array.astype(np.uint8)
|
||||
|
||||
# 创建图像对象
|
||||
image = Image.fromarray(image_array)
|
||||
|
||||
# 保存图片
|
||||
image.save(save_path)
|
||||
print(f"Image saved to {save_path}")
|
||||
|
||||
def pad_to_square(video_tensor):
|
||||
# 验证输入的形状
|
||||
if len(video_tensor.shape) != 4:
|
||||
raise ValueError("Input tensor must have shape (l, c, h, w)")
|
||||
|
||||
l, c, h, w = video_tensor.shape
|
||||
max_side = max(h, w)
|
||||
|
||||
# 计算每一维度需要的填充量:(left, right, top, bottom)
|
||||
pad_h = max_side - h
|
||||
pad_w = max_side - w
|
||||
|
||||
# 创建padding tuple (left, right, top, bottom)
|
||||
# 因为图像的填充是作用在最后两个维度 h 和 w 上,所以我们需要指定这两个维度的填充
|
||||
padding = (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
|
||||
|
||||
# 使用F.pad对视频张量进行填充操作
|
||||
# 填充参数为 (left, right, top, bottom)
|
||||
video_padded = F.pad(video_tensor, pad=padding, mode='constant', value=0)
|
||||
|
||||
return video_padded
|
||||
|
||||
class VGGSound(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
self.caption_cot = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['caption']
|
||||
caption_cot = record['caption_cot']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
self.caption_cot.append(caption_cot)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Lambda(pad_to_square), # 先填充为正方形
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
self.clip_processor = AutoProcessor.from_pretrained("facebook/metaclip-h14-fullcc2.5b")
|
||||
self.sync_transform = v2.Compose([
|
||||
v2.Resize(_SYNC_SIZE, interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.CenterCrop(_SYNC_SIZE),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
v2.Normalize(mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]),
|
||||
])
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
caption_cot = self.caption_cot[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(self.root / (video_id + '.mp4'))
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_SYNC_FPS * self.duration_sec),
|
||||
frame_rate=_SYNC_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
# reader.add_basic_audio_stream(frames_per_chunk=2**30,)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
sync_chunk = data_chunk[1]
|
||||
# audio_chunk = data_chunk[2]
|
||||
# if len(audio_chunk.shape) != 2:
|
||||
# raise RuntimeError(f'error audio shape {video_id}')
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
# if clip_chunk.shape[0] < self.clip_expected_length:
|
||||
# raise RuntimeError(
|
||||
# f'CLIP video too short {video_id}, expected {self.clip_expected_length}, got {clip_chunk.shape[0]}'
|
||||
# )
|
||||
|
||||
if sync_chunk is None:
|
||||
raise RuntimeError(f'Sync video returned None {video_id}')
|
||||
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
# assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(last_frame.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = pad_to_square(clip_chunk)
|
||||
# save_image(clip_chunk[0] / 255.0,'square.png')
|
||||
# clip_chunk = self.clip_transform(clip_chunk)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
clip_chunk = self.clip_processor(images=clip_chunk, return_tensors="pt")["pixel_values"]
|
||||
# log.info(clip_chunk.shape)
|
||||
# save_tensor_as_image(clip_chunk[0].numpy(),'scale.png')
|
||||
# log.info(clip_chunk[0])
|
||||
# clip_chunk = outputs
|
||||
# text_ids = outputs["input_ids"]
|
||||
# temp_img = clip_chunk[0].permute(1, 2, 0) * 255
|
||||
# save_image(clip_chunk[0],'scale.png')
|
||||
sync_chunk = sync_chunk[:self.sync_expected_length]
|
||||
if sync_chunk.shape[0] != self.sync_expected_length:
|
||||
# padding using the last frame, but no more than 2
|
||||
current_length = sync_chunk.shape[0]
|
||||
last_frame = sync_chunk[-1]
|
||||
# 重复最后一帧以进行填充
|
||||
padding = last_frame.repeat(self.sync_expected_length - current_length, 1, 1, 1)
|
||||
# assert self.sync_expected_length - current_length < 12, f'sync can pad no more than 2 while {self.sync_expected_length - current_length}'
|
||||
sync_chunk = torch.cat((sync_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'Sync video wrong length {video_id}, '
|
||||
# f'expected {self.sync_expected_length}, '
|
||||
# f'got {sync_chunk.shape[0]}')
|
||||
|
||||
sync_chunk = self.sync_transform(sync_chunk)
|
||||
# assert audio_chunk.shape[1] == self.expected_audio_length and clip_chunk.shape[0] == self.clip_expected_length \
|
||||
# and sync_chunk.shape[0] == self.sync_expected_length, 'error processed data shape'
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
# 'audio': audio_chunk,
|
||||
'clip_video': clip_chunk,
|
||||
'sync_video': sync_chunk,
|
||||
'caption_cot': caption_cot,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/train",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_224_latents_text/train"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,223 @@
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
from PIL import Image
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
from transformers import AutoProcessor
|
||||
import torch.nn.functional as F
|
||||
import numpy as np
|
||||
|
||||
import logging
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 224
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
def save_tensor_as_image(tensor, save_path):
|
||||
"""
|
||||
将形状为 (1, 3, H, W) 的 RGB 图像数组保存为图片文件。
|
||||
|
||||
:param tensor: 输入的 NumPy 数组 (1, 3, H, W)。
|
||||
:param save_path: 图片保存路径。
|
||||
"""
|
||||
# # 移除批次维度,变成 (3, H, W)
|
||||
# tensor = tensor.squeeze(0)
|
||||
|
||||
# 交换轴顺序,变为 (H, W, 3)
|
||||
image_array = np.transpose(tensor, (1, 2, 0))
|
||||
|
||||
# 检查数组是否为合适的数据类型
|
||||
if image_array.dtype != np.uint8:
|
||||
# 如果不是 uint8,首先标准化,然后转换
|
||||
image_array = (image_array - image_array.min()) / (image_array.max() - image_array.min()) * 255
|
||||
image_array = image_array.astype(np.uint8)
|
||||
|
||||
# 创建图像对象
|
||||
image = Image.fromarray(image_array)
|
||||
|
||||
# 保存图片
|
||||
image.save(save_path)
|
||||
print(f"Image saved to {save_path}")
|
||||
|
||||
def pad_to_square(video_tensor):
|
||||
# 验证输入的形状
|
||||
if len(video_tensor.shape) != 4:
|
||||
raise ValueError("Input tensor must have shape (l, c, h, w)")
|
||||
|
||||
l, c, h, w = video_tensor.shape
|
||||
max_side = max(h, w)
|
||||
|
||||
# 计算每一维度需要的填充量:(left, right, top, bottom)
|
||||
pad_h = max_side - h
|
||||
pad_w = max_side - w
|
||||
|
||||
# 创建padding tuple (left, right, top, bottom)
|
||||
# 因为图像的填充是作用在最后两个维度 h 和 w 上,所以我们需要指定这两个维度的填充
|
||||
padding = (pad_w // 2, pad_w - pad_w // 2, pad_h // 2, pad_h - pad_h // 2)
|
||||
|
||||
# 使用F.pad对视频张量进行填充操作
|
||||
# 填充参数为 (left, right, top, bottom)
|
||||
video_padded = F.pad(video_tensor, pad=padding, mode='constant', value=0)
|
||||
|
||||
return video_padded
|
||||
|
||||
class VGGSound(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
sample_rate: int = 44_100,
|
||||
duration_sec: float = 9.0,
|
||||
audio_samples: Optional[int] = 397312,
|
||||
normalize_audio: bool = False,
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
self.normalize_audio = normalize_audio
|
||||
if audio_samples is None:
|
||||
self.audio_samples = int(sample_rate * duration_sec)
|
||||
else:
|
||||
self.audio_samples = audio_samples
|
||||
effective_duration = audio_samples / sample_rate
|
||||
# make sure the duration is close enough, within 15ms
|
||||
assert abs(effective_duration - duration_sec) < 0.015, \
|
||||
f'audio_samples {audio_samples} does not match duration_sec {duration_sec}'
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.videos = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
if os.path.exists(f'{save_dir}/{id}.pth'): continue
|
||||
label = record['label']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
self.sample_rate = sample_rate
|
||||
self.duration_sec = duration_sec
|
||||
|
||||
self.expected_audio_length = self.audio_samples
|
||||
self.clip_expected_length = int(_CLIP_FPS * self.duration_sec)
|
||||
self.sync_expected_length = int(_SYNC_FPS * self.duration_sec)
|
||||
|
||||
self.clip_transform = v2.Compose([
|
||||
v2.Lambda(pad_to_square), # 先填充为正方形
|
||||
v2.Resize((_CLIP_SIZE, _CLIP_SIZE), interpolation=v2.InterpolationMode.BICUBIC),
|
||||
v2.ToImage(),
|
||||
v2.ToDtype(torch.float32, scale=True),
|
||||
])
|
||||
self.clip_processor = AutoProcessor.from_pretrained("useful_ckpts/metaclip-huge")
|
||||
|
||||
self.resampler = {}
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
|
||||
reader = StreamingMediaDecoder(self.root / (video_id + '.mp4'))
|
||||
reader.add_basic_video_stream(
|
||||
frames_per_chunk=int(_CLIP_FPS * self.duration_sec),
|
||||
frame_rate=_CLIP_FPS,
|
||||
format='rgb24',
|
||||
)
|
||||
|
||||
reader.fill_buffer()
|
||||
data_chunk = reader.pop_chunks()
|
||||
|
||||
clip_chunk = data_chunk[0]
|
||||
if clip_chunk is None:
|
||||
raise RuntimeError(f'CLIP video returned None {video_id}')
|
||||
|
||||
|
||||
# truncate the video
|
||||
clip_chunk = clip_chunk[:self.clip_expected_length]
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if clip_chunk.shape[0] != self.clip_expected_length:
|
||||
current_length = clip_chunk.shape[0]
|
||||
padding_needed = self.clip_expected_length - current_length
|
||||
|
||||
# Check that padding needed is no more than 2
|
||||
assert padding_needed < 4, f'Padding no more than 2 frames allowed, but {padding_needed} needed'
|
||||
|
||||
# If assertion passes, proceed with padding
|
||||
if padding_needed > 0:
|
||||
last_frame = clip_chunk[-1]
|
||||
log.info(last_frame.shape)
|
||||
# Repeat the last frame to reach the expected length
|
||||
padding = last_frame.repeat(padding_needed, 1, 1, 1)
|
||||
clip_chunk = torch.cat((clip_chunk, padding), dim=0)
|
||||
# raise RuntimeError(f'CLIP video wrong length {video_id}, '
|
||||
# f'expected {self.clip_expected_length}, '
|
||||
# f'got {clip_chunk.shape[0]}')
|
||||
|
||||
# save_image(clip_chunk[0] / 255.0,'ori.png')
|
||||
clip_chunk = pad_to_square(clip_chunk)
|
||||
# save_image(clip_chunk[0] / 255.0,'square.png')
|
||||
# clip_chunk = self.clip_transform(clip_chunk)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
clip_chunk = self.clip_processor(images=clip_chunk, return_tensors="pt")["pixel_values"]
|
||||
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'clip_video': clip_chunk,
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/train",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_224_latents_text/train"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,109 @@
|
||||
import logging
|
||||
import os
|
||||
from pathlib import Path
|
||||
from typing import Optional, Union
|
||||
|
||||
import pandas as pd
|
||||
import torch
|
||||
import torchaudio
|
||||
from torch.utils.data.dataset import Dataset
|
||||
from torchvision.transforms import v2
|
||||
from torio.io import StreamingMediaDecoder
|
||||
from torchvision.utils import save_image
|
||||
|
||||
log = logging.getLogger()
|
||||
|
||||
_CLIP_SIZE = 384
|
||||
_CLIP_FPS = 8.0
|
||||
|
||||
_SYNC_SIZE = 224
|
||||
_SYNC_FPS = 25.0
|
||||
|
||||
|
||||
class VGGSound(Dataset):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
root: Union[str, Path],
|
||||
*,
|
||||
tsv_path: Union[str, Path] = 'dataset/vggsound/split_txt/train_caption.csv',
|
||||
start_row: Optional[int] = None,
|
||||
end_row: Optional[int] = None,
|
||||
save_dir: str = 'data/vggsound/video_latents_text/train'
|
||||
):
|
||||
self.root = Path(root)
|
||||
|
||||
# videos = sorted(os.listdir(self.root))
|
||||
# videos = set([Path(v).stem for v in videos]) # remove extensions
|
||||
videos = []
|
||||
self.labels = []
|
||||
self.cots = []
|
||||
self.videos = []
|
||||
missing_videos = []
|
||||
# read the tsv for subset information
|
||||
df_list = pd.read_csv(tsv_path, sep=',', dtype={'id': str}).to_dict('records')
|
||||
|
||||
# 控制处理的行范围
|
||||
if start_row is not None and end_row is not None:
|
||||
df_list = df_list[start_row:end_row]
|
||||
|
||||
for record in df_list:
|
||||
id = record['id']
|
||||
# if os.path.exists(f'{save_dir}/{id}.pth'):
|
||||
# continue
|
||||
# try:
|
||||
# torch.load(f'{save_dir}/{id}.pth')
|
||||
# continue
|
||||
# except:
|
||||
# print(f'error load file: {save_dir}/{id}.pth')
|
||||
# os.system(f'rm -f {save_dir}/{id}.pth')
|
||||
label = record['caption']
|
||||
# if id in videos:
|
||||
self.labels.append(label)
|
||||
self.cots.append(record['caption_cot'])
|
||||
# self.labels[id] = label
|
||||
self.videos.append(id)
|
||||
# else:
|
||||
# missing_videos.append(id)
|
||||
|
||||
log.info(f'{len(videos)} videos found in {root}')
|
||||
log.info(f'{len(self.videos)} videos found in {tsv_path}')
|
||||
log.info(f'{len(missing_videos)} videos missing in {root}')
|
||||
|
||||
|
||||
|
||||
|
||||
def sample(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
video_id = self.videos[idx]
|
||||
label = self.labels[idx]
|
||||
cot = self.cots[idx]
|
||||
data = {
|
||||
'id': video_id,
|
||||
'caption': label,
|
||||
'caption_cot': cot
|
||||
}
|
||||
|
||||
return data
|
||||
|
||||
def __getitem__(self, idx: int) -> dict[str, torch.Tensor]:
|
||||
try:
|
||||
return self.sample(idx)
|
||||
except Exception as e:
|
||||
log.error(f'Error loading video {self.videos[idx]}: {e}')
|
||||
return None
|
||||
|
||||
def __len__(self):
|
||||
return len(self.labels)
|
||||
|
||||
|
||||
# dataset = VGGSound(
|
||||
# root="data/vggsound/video/test",
|
||||
# tsv_path="data/vggsound/split_txt/temp.csv",
|
||||
# sample_rate=44100,
|
||||
# duration_sec=9.0,
|
||||
# audio_samples=397312,
|
||||
# start_row=0,
|
||||
# end_row=None,
|
||||
# save_dir="data/vggsound/video_latents_text/test"
|
||||
# )
|
||||
# dataset[0]
|
||||
@@ -0,0 +1,195 @@
|
||||
import numpy as np
|
||||
import torch
|
||||
import typing as tp
|
||||
import math
|
||||
from torchaudio import transforms as T
|
||||
|
||||
from .utils import prepare_audio
|
||||
from .sampling import sample, sample_k, sample_rf
|
||||
from ..data.utils import PadCrop
|
||||
|
||||
def generate_diffusion_cond(
|
||||
model,
|
||||
steps: int = 250,
|
||||
cfg_scale=6,
|
||||
conditioning: dict = None,
|
||||
conditioning_tensors: tp.Optional[dict] = None,
|
||||
negative_conditioning: dict = None,
|
||||
negative_conditioning_tensors: tp.Optional[dict] = None,
|
||||
batch_size: int = 1,
|
||||
sample_size: int = 2097152,
|
||||
sample_rate: int = 48000,
|
||||
seed: int = -1,
|
||||
device: str = "cuda",
|
||||
init_audio: tp.Optional[tp.Tuple[int, torch.Tensor]] = None,
|
||||
init_noise_level: float = 1.0,
|
||||
mask_args: dict = None,
|
||||
return_latents = False,
|
||||
**sampler_kwargs
|
||||
) -> torch.Tensor:
|
||||
"""
|
||||
Generate audio from a prompt using a diffusion model.
|
||||
|
||||
Args:
|
||||
model: The diffusion model to use for generation.
|
||||
steps: The number of diffusion steps to use.
|
||||
cfg_scale: Classifier-free guidance scale
|
||||
conditioning: A dictionary of conditioning parameters to use for generation.
|
||||
conditioning_tensors: A dictionary of precomputed conditioning tensors to use for generation.
|
||||
batch_size: The batch size to use for generation.
|
||||
sample_size: The length of the audio to generate, in samples.
|
||||
sample_rate: The sample rate of the audio to generate (Deprecated, now pulled from the model directly)
|
||||
seed: The random seed to use for generation, or -1 to use a random seed.
|
||||
device: The device to use for generation.
|
||||
init_audio: A tuple of (sample_rate, audio) to use as the initial audio for generation.
|
||||
init_noise_level: The noise level to use when generating from an initial audio sample.
|
||||
return_latents: Whether to return the latents used for generation instead of the decoded audio.
|
||||
**sampler_kwargs: Additional keyword arguments to pass to the sampler.
|
||||
"""
|
||||
|
||||
# The length of the output in audio samples
|
||||
audio_sample_size = sample_size
|
||||
|
||||
# If this is latent diffusion, change sample_size instead to the downsampled latent size
|
||||
if model.pretransform is not None:
|
||||
sample_size = sample_size // model.pretransform.downsampling_ratio
|
||||
|
||||
# Seed
|
||||
# The user can explicitly set the seed to deterministically generate the same output. Otherwise, use a random seed.
|
||||
seed = seed if seed != -1 else np.random.randint(0, 2**32 - 1, dtype=np.uint32)
|
||||
print(seed)
|
||||
torch.manual_seed(seed)
|
||||
# Define the initial noise immediately after setting the seed
|
||||
noise = torch.randn([batch_size, model.io_channels, sample_size], device=device)
|
||||
|
||||
torch.backends.cuda.matmul.allow_tf32 = False
|
||||
torch.backends.cudnn.allow_tf32 = False
|
||||
torch.backends.cuda.matmul.allow_fp16_reduced_precision_reduction = False
|
||||
torch.backends.cudnn.benchmark = False
|
||||
import ipdb
|
||||
# ipdb.set_trace()
|
||||
# Conditioning
|
||||
assert conditioning is not None or conditioning_tensors is not None, "Must provide either conditioning or conditioning_tensors"
|
||||
if conditioning_tensors is None:
|
||||
conditioning_tensors = model.conditioner(conditioning, device)
|
||||
conditioning_inputs = model.get_conditioning_inputs(conditioning_tensors)
|
||||
|
||||
if negative_conditioning is not None or negative_conditioning_tensors is not None:
|
||||
|
||||
if negative_conditioning_tensors is None:
|
||||
negative_conditioning_tensors = model.conditioner(negative_conditioning, device)
|
||||
|
||||
negative_conditioning_tensors = model.get_conditioning_inputs(negative_conditioning_tensors, negative=True)
|
||||
else:
|
||||
negative_conditioning_tensors = {}
|
||||
|
||||
if init_audio is not None:
|
||||
# The user supplied some initial audio (for inpainting or variation). Let us prepare the input audio.
|
||||
in_sr, init_audio = init_audio
|
||||
|
||||
io_channels = model.io_channels
|
||||
|
||||
# For latent models, set the io_channels to the autoencoder's io_channels
|
||||
if model.pretransform is not None:
|
||||
io_channels = model.pretransform.io_channels
|
||||
|
||||
# Prepare the initial audio for use by the model
|
||||
init_audio = prepare_audio(init_audio, in_sr=in_sr, target_sr=model.sample_rate, target_length=audio_sample_size, target_channels=io_channels, device=device)
|
||||
|
||||
# For latent models, encode the initial audio into latents
|
||||
if model.pretransform is not None:
|
||||
init_audio = model.pretransform.encode(init_audio)
|
||||
|
||||
init_audio = init_audio.repeat(batch_size, 1, 1)
|
||||
else:
|
||||
# The user did not supply any initial audio for inpainting or variation. Generate new output from scratch.
|
||||
init_audio = None
|
||||
init_noise_level = None
|
||||
mask_args = None
|
||||
|
||||
# Inpainting mask
|
||||
if init_audio is not None and mask_args is not None:
|
||||
# Cut and paste init_audio according to cropfrom, pastefrom, pasteto
|
||||
# This is helpful for forward and reverse outpainting
|
||||
cropfrom = math.floor(mask_args["cropfrom"]/100.0 * sample_size)
|
||||
pastefrom = math.floor(mask_args["pastefrom"]/100.0 * sample_size)
|
||||
pasteto = math.ceil(mask_args["pasteto"]/100.0 * sample_size)
|
||||
assert pastefrom < pasteto, "Paste From should be less than Paste To"
|
||||
croplen = pasteto - pastefrom
|
||||
if cropfrom + croplen > sample_size:
|
||||
croplen = sample_size - cropfrom
|
||||
cropto = cropfrom + croplen
|
||||
pasteto = pastefrom + croplen
|
||||
cutpaste = init_audio.new_zeros(init_audio.shape)
|
||||
cutpaste[:, :, pastefrom:pasteto] = init_audio[:,:,cropfrom:cropto]
|
||||
#print(cropfrom, cropto, pastefrom, pasteto)
|
||||
init_audio = cutpaste
|
||||
# Build a soft mask (list of floats 0 to 1, the size of the latent) from the given args
|
||||
mask = build_mask(sample_size, mask_args)
|
||||
mask = mask.to(device)
|
||||
elif init_audio is not None and mask_args is None:
|
||||
# variations
|
||||
sampler_kwargs["sigma_max"] = init_noise_level
|
||||
mask = None
|
||||
else:
|
||||
mask = None
|
||||
|
||||
model_dtype = next(model.model.parameters()).dtype
|
||||
noise = noise.type(model_dtype)
|
||||
conditioning_inputs = {k: v.type(model_dtype) if v is not None else v for k, v in conditioning_inputs.items()}
|
||||
# Now the generative AI part:
|
||||
# k-diffusion denoising process go!
|
||||
diff_objective = model.diffusion_objective
|
||||
if diff_objective == "v":
|
||||
# k-diffusion denoising process go!
|
||||
# sampled = sample(model.model, noise, steps, 0, **conditioning_inputs)
|
||||
sampled = sample_k(model.model, noise, init_audio, mask, steps, **sampler_kwargs, **conditioning_inputs, **negative_conditioning_tensors, cfg_scale=cfg_scale, batch_cfg=True, rescale_cfg=True, device=device)
|
||||
elif diff_objective == "rectified_flow":
|
||||
|
||||
if "sigma_min" in sampler_kwargs:
|
||||
del sampler_kwargs["sigma_min"]
|
||||
|
||||
if "sampler_type" in sampler_kwargs:
|
||||
del sampler_kwargs["sampler_type"]
|
||||
|
||||
sampled = sample_rf(model.model, noise, init_data=init_audio, steps=steps, **sampler_kwargs, **conditioning_inputs, **negative_conditioning_tensors, cfg_scale=cfg_scale, batch_cfg=True, rescale_cfg=True, device=device)
|
||||
|
||||
# v-diffusion:
|
||||
#sampled = sample(model.model, noise, steps, 0, **conditioning_tensors, embedding_scale=cfg_scale)
|
||||
del noise
|
||||
del conditioning_tensors
|
||||
del conditioning_inputs
|
||||
torch.cuda.empty_cache()
|
||||
# Denoising process done.
|
||||
# If this is latent diffusion, decode latents back into audio
|
||||
if model.pretransform is not None and not return_latents:
|
||||
#cast sampled latents to pretransform dtype
|
||||
sampled = sampled.to(next(model.pretransform.parameters()).dtype)
|
||||
sampled = model.pretransform.decode(sampled)
|
||||
|
||||
# Return audio
|
||||
return sampled
|
||||
|
||||
# builds a softmask given the parameters
|
||||
# returns array of values 0 to 1, size sample_size, where 0 means noise / fresh generation, 1 means keep the input audio,
|
||||
# and anything between is a mixture of old/new
|
||||
# ideally 0.5 is half/half mixture but i haven't figured this out yet
|
||||
def build_mask(sample_size, mask_args):
|
||||
maskstart = math.floor(mask_args["maskstart"]/100.0 * sample_size)
|
||||
maskend = math.ceil(mask_args["maskend"]/100.0 * sample_size)
|
||||
softnessL = round(mask_args["softnessL"]/100.0 * sample_size)
|
||||
softnessR = round(mask_args["softnessR"]/100.0 * sample_size)
|
||||
marination = mask_args["marination"]
|
||||
# use hann windows for softening the transition (i don't know if this is correct)
|
||||
hannL = torch.hann_window(softnessL*2, periodic=False)[:softnessL]
|
||||
hannR = torch.hann_window(softnessR*2, periodic=False)[softnessR:]
|
||||
# build the mask.
|
||||
mask = torch.zeros((sample_size))
|
||||
mask[maskstart:maskend] = 1
|
||||
mask[maskstart:maskstart+softnessL] = hannL
|
||||
mask[maskend-softnessR:maskend] = hannR
|
||||
# marination finishes the inpainting early in the denoising schedule, and lets audio get changed in the final rounds
|
||||
if marination > 0:
|
||||
mask = mask * (1-marination)
|
||||
#print(mask)
|
||||
return mask
|
||||
@@ -0,0 +1,232 @@
|
||||
import torch
|
||||
import math
|
||||
from tqdm import trange, tqdm
|
||||
|
||||
import k_diffusion as K
|
||||
|
||||
# Define the noise schedule and sampling loop
|
||||
def get_alphas_sigmas(t):
|
||||
"""Returns the scaling factors for the clean image (alpha) and for the
|
||||
noise (sigma), given a timestep."""
|
||||
return torch.cos(t * math.pi / 2), torch.sin(t * math.pi / 2)
|
||||
|
||||
def alpha_sigma_to_t(alpha, sigma):
|
||||
"""Returns a timestep, given the scaling factors for the clean image and for
|
||||
the noise."""
|
||||
return torch.atan2(sigma, alpha) / math.pi * 2
|
||||
|
||||
def t_to_alpha_sigma(t):
|
||||
"""Returns the scaling factors for the clean image and for the noise, given
|
||||
a timestep."""
|
||||
return torch.cos(t * math.pi / 2), torch.sin(t * math.pi / 2)
|
||||
|
||||
|
||||
@torch.no_grad()
|
||||
def sample_discrete_euler(model, x, steps, sigma_max=1, **extra_args):
|
||||
"""Draws samples from a model given starting noise. Euler method"""
|
||||
|
||||
# Make tensor of ones to broadcast the single t values
|
||||
ts = x.new_ones([x.shape[0]])
|
||||
|
||||
# Create the noise schedule
|
||||
t = torch.linspace(sigma_max, 0, steps + 1)
|
||||
|
||||
#alphas, sigmas = 1-t, t
|
||||
|
||||
for t_curr, t_prev in tqdm(zip(t[:-1], t[1:])):
|
||||
# Broadcast the current timestep to the correct shape
|
||||
t_curr_tensor = t_curr * torch.ones(
|
||||
(x.shape[0],), dtype=x.dtype, device=x.device
|
||||
)
|
||||
dt = t_prev - t_curr # we solve backwards in our formulation
|
||||
x = x + dt * model(x, t_curr_tensor, **extra_args) #.denoise(x, denoiser, t_curr_tensor, cond, uc)
|
||||
|
||||
# If we are on the last timestep, output the denoised image
|
||||
return x
|
||||
|
||||
@torch.no_grad()
|
||||
def sample(model, x, steps, eta, **extra_args):
|
||||
"""Draws samples from a model given starting noise. v-diffusion"""
|
||||
ts = x.new_ones([x.shape[0]])
|
||||
|
||||
# Create the noise schedule
|
||||
t = torch.linspace(1, 0, steps + 1)[:-1]
|
||||
|
||||
alphas, sigmas = get_alphas_sigmas(t)
|
||||
|
||||
# The sampling loop
|
||||
for i in trange(steps):
|
||||
|
||||
# Get the model output (v, the predicted velocity)
|
||||
with torch.cuda.amp.autocast():
|
||||
v = model(x, ts * t[i], **extra_args).float()
|
||||
|
||||
# Predict the noise and the denoised image
|
||||
pred = x * alphas[i] - v * sigmas[i]
|
||||
eps = x * sigmas[i] + v * alphas[i]
|
||||
|
||||
# If we are not on the last timestep, compute the noisy image for the
|
||||
# next timestep.
|
||||
if i < steps - 1:
|
||||
# If eta > 0, adjust the scaling factor for the predicted noise
|
||||
# downward according to the amount of additional noise to add
|
||||
ddim_sigma = eta * (sigmas[i + 1]**2 / sigmas[i]**2).sqrt() * \
|
||||
(1 - alphas[i]**2 / alphas[i + 1]**2).sqrt()
|
||||
adjusted_sigma = (sigmas[i + 1]**2 - ddim_sigma**2).sqrt()
|
||||
|
||||
# Recombine the predicted noise and predicted denoised image in the
|
||||
# correct proportions for the next step
|
||||
x = pred * alphas[i + 1] + eps * adjusted_sigma
|
||||
|
||||
# Add the correct amount of fresh noise
|
||||
if eta:
|
||||
x += torch.randn_like(x) * ddim_sigma
|
||||
|
||||
# If we are on the last timestep, output the denoised image
|
||||
return pred
|
||||
|
||||
# Soft mask inpainting is just shrinking hard (binary) mask inpainting
|
||||
# Given a float-valued soft mask (values between 0 and 1), get the binary mask for this particular step
|
||||
def get_bmask(i, steps, mask):
|
||||
strength = (i+1)/(steps)
|
||||
# convert to binary mask
|
||||
bmask = torch.where(mask<=strength,1,0)
|
||||
return bmask
|
||||
|
||||
def make_cond_model_fn(model, cond_fn):
|
||||
def cond_model_fn(x, sigma, **kwargs):
|
||||
with torch.enable_grad():
|
||||
x = x.detach().requires_grad_()
|
||||
denoised = model(x, sigma, **kwargs)
|
||||
cond_grad = cond_fn(x, sigma, denoised=denoised, **kwargs).detach()
|
||||
cond_denoised = denoised.detach() + cond_grad * K.utils.append_dims(sigma**2, x.ndim)
|
||||
return cond_denoised
|
||||
return cond_model_fn
|
||||
|
||||
# Uses k-diffusion from https://github.com/crowsonkb/k-diffusion
|
||||
# init_data is init_audio as latents (if this is latent diffusion)
|
||||
# For sampling, set both init_data and mask to None
|
||||
# For variations, set init_data
|
||||
# For inpainting, set both init_data & mask
|
||||
def sample_k(
|
||||
model_fn,
|
||||
noise,
|
||||
init_data=None,
|
||||
mask=None,
|
||||
steps=100,
|
||||
sampler_type="dpmpp-2m-sde",
|
||||
sigma_min=0.5,
|
||||
sigma_max=50,
|
||||
rho=1.0, device="cuda",
|
||||
callback=None,
|
||||
cond_fn=None,
|
||||
**extra_args
|
||||
):
|
||||
|
||||
denoiser = K.external.VDenoiser(model_fn)
|
||||
|
||||
if cond_fn is not None:
|
||||
denoiser = make_cond_model_fn(denoiser, cond_fn)
|
||||
|
||||
# Make the list of sigmas. Sigma values are scalars related to the amount of noise each denoising step has
|
||||
sigmas = K.sampling.get_sigmas_polyexponential(steps, sigma_min, sigma_max, rho, device=device)
|
||||
# Scale the initial noise by sigma
|
||||
noise = noise * sigmas[0]
|
||||
|
||||
wrapped_callback = callback
|
||||
|
||||
if mask is None and init_data is not None:
|
||||
# VARIATION (no inpainting)
|
||||
# set the initial latent to the init_data, and noise it with initial sigma
|
||||
x = init_data + noise
|
||||
elif mask is not None and init_data is not None:
|
||||
# INPAINTING
|
||||
bmask = get_bmask(0, steps, mask)
|
||||
# initial noising
|
||||
input_noised = init_data + noise
|
||||
# set the initial latent to a mix of init_data and noise, based on step 0's binary mask
|
||||
x = input_noised * bmask + noise * (1-bmask)
|
||||
# define the inpainting callback function (Note: side effects, it mutates x)
|
||||
# See https://github.com/crowsonkb/k-diffusion/blob/master/k_diffusion/sampling.py#L596C13-L596C105
|
||||
# callback({'x': x, 'i': i, 'sigma': sigmas[i], 'sigma_hat': sigmas[i], 'denoised': denoised})
|
||||
# This is called immediately after `denoised = model(x, sigmas[i] * s_in, **extra_args)`
|
||||
def inpainting_callback(args):
|
||||
i = args["i"]
|
||||
x = args["x"]
|
||||
sigma = args["sigma"]
|
||||
#denoised = args["denoised"]
|
||||
# noise the init_data input with this step's appropriate amount of noise
|
||||
input_noised = init_data + torch.randn_like(init_data) * sigma
|
||||
# shrinking hard mask
|
||||
bmask = get_bmask(i, steps, mask)
|
||||
# mix input_noise with x, using binary mask
|
||||
new_x = input_noised * bmask + x * (1-bmask)
|
||||
# mutate x
|
||||
x[:,:,:] = new_x[:,:,:]
|
||||
# wrap together the inpainting callback and the user-submitted callback.
|
||||
if callback is None:
|
||||
wrapped_callback = inpainting_callback
|
||||
else:
|
||||
wrapped_callback = lambda args: (inpainting_callback(args), callback(args))
|
||||
else:
|
||||
# SAMPLING
|
||||
# set the initial latent to noise
|
||||
x = noise
|
||||
|
||||
|
||||
with torch.cuda.amp.autocast():
|
||||
if sampler_type == "k-heun":
|
||||
return K.sampling.sample_heun(denoiser, x, sigmas, disable=False, callback=wrapped_callback, extra_args=extra_args)
|
||||
elif sampler_type == "k-lms":
|
||||
return K.sampling.sample_lms(denoiser, x, sigmas, disable=False, callback=wrapped_callback, extra_args=extra_args)
|
||||
elif sampler_type == "k-dpmpp-2s-ancestral":
|
||||
return K.sampling.sample_dpmpp_2s_ancestral(denoiser, x, sigmas, disable=False, callback=wrapped_callback, extra_args=extra_args)
|
||||
elif sampler_type == "k-dpm-2":
|
||||
return K.sampling.sample_dpm_2(denoiser, x, sigmas, disable=False, callback=wrapped_callback, extra_args=extra_args)
|
||||
elif sampler_type == "k-dpm-fast":
|
||||
return K.sampling.sample_dpm_fast(denoiser, x, sigma_min, sigma_max, steps, disable=False, callback=wrapped_callback, extra_args=extra_args)
|
||||
elif sampler_type == "k-dpm-adaptive":
|
||||
return K.sampling.sample_dpm_adaptive(denoiser, x, sigma_min, sigma_max, rtol=0.01, atol=0.01, disable=False, callback=wrapped_callback, extra_args=extra_args)
|
||||
elif sampler_type == "dpmpp-2m-sde":
|
||||
return K.sampling.sample_dpmpp_2m_sde(denoiser, x, sigmas, disable=False, callback=wrapped_callback, extra_args=extra_args)
|
||||
elif sampler_type == "dpmpp-3m-sde":
|
||||
return K.sampling.sample_dpmpp_3m_sde(denoiser, x, sigmas, disable=False, callback=wrapped_callback, extra_args=extra_args)
|
||||
|
||||
# Uses discrete Euler sampling for rectified flow models
|
||||
# init_data is init_audio as latents (if this is latent diffusion)
|
||||
# For sampling, set both init_data and mask to None
|
||||
# For variations, set init_data
|
||||
# For inpainting, set both init_data & mask
|
||||
def sample_rf(
|
||||
model_fn,
|
||||
noise,
|
||||
init_data=None,
|
||||
steps=100,
|
||||
sigma_max=1,
|
||||
device="cuda",
|
||||
callback=None,
|
||||
cond_fn=None,
|
||||
**extra_args
|
||||
):
|
||||
|
||||
if sigma_max > 1:
|
||||
sigma_max = 1
|
||||
|
||||
if cond_fn is not None:
|
||||
denoiser = make_cond_model_fn(denoiser, cond_fn)
|
||||
|
||||
wrapped_callback = callback
|
||||
|
||||
if init_data is not None:
|
||||
# VARIATION (no inpainting)
|
||||
# Interpolate the init data and the noise for init audio
|
||||
x = init_data * (1 - sigma_max) + noise * sigma_max
|
||||
else:
|
||||
# SAMPLING
|
||||
# set the initial latent to noise
|
||||
x = noise
|
||||
|
||||
with torch.cuda.amp.autocast():
|
||||
# TODO: Add callback support
|
||||
#return sample_discrete_euler(model_fn, x, steps, sigma_max, callback=wrapped_callback, **extra_args)
|
||||
return sample_discrete_euler(model_fn, x, steps, sigma_max, **extra_args)
|
||||
@@ -0,0 +1,35 @@
|
||||
from ..data.utils import PadCrop
|
||||
|
||||
from torchaudio import transforms as T
|
||||
|
||||
def set_audio_channels(audio, target_channels):
|
||||
if target_channels == 1:
|
||||
# Convert to mono
|
||||
audio = audio.mean(1, keepdim=True)
|
||||
elif target_channels == 2:
|
||||
# Convert to stereo
|
||||
if audio.shape[1] == 1:
|
||||
audio = audio.repeat(1, 2, 1)
|
||||
elif audio.shape[1] > 2:
|
||||
audio = audio[:, :2, :]
|
||||
return audio
|
||||
|
||||
def prepare_audio(audio, in_sr, target_sr, target_length, target_channels, device):
|
||||
|
||||
audio = audio.to(device)
|
||||
|
||||
if in_sr != target_sr:
|
||||
resample_tf = T.Resample(in_sr, target_sr).to(device)
|
||||
audio = resample_tf(audio)
|
||||
|
||||
audio = PadCrop(target_length, randomize=False)(audio)
|
||||
|
||||
# Add batch dimension
|
||||
if audio.dim() == 1:
|
||||
audio = audio.unsqueeze(0).unsqueeze(0)
|
||||
elif audio.dim() == 2:
|
||||
audio = audio.unsqueeze(0)
|
||||
|
||||
audio = set_audio_channels(audio, target_channels)
|
||||
|
||||
return audio
|
||||
@@ -0,0 +1 @@
|
||||
from .factory import create_model_from_config, create_model_from_config_path
|
||||
@@ -0,0 +1,800 @@
|
||||
import torch
|
||||
import math
|
||||
import numpy as np
|
||||
|
||||
from torch import nn
|
||||
from torch.nn import functional as F
|
||||
from torchaudio import transforms as T
|
||||
from alias_free_torch import Activation1d
|
||||
from dac.nn.layers import WNConv1d, WNConvTranspose1d
|
||||
from typing import Literal, Dict, Any
|
||||
|
||||
from ..inference.sampling import sample
|
||||
from ..inference.utils import prepare_audio
|
||||
from .blocks import SnakeBeta
|
||||
from .bottleneck import Bottleneck, DiscreteBottleneck
|
||||
from .diffusion import ConditionedDiffusionModel, DAU1DCondWrapper, UNet1DCondWrapper, DiTWrapper
|
||||
from .factory import create_pretransform_from_config, create_bottleneck_from_config
|
||||
from .pretransforms import Pretransform
|
||||
|
||||
def checkpoint(function, *args, **kwargs):
|
||||
kwargs.setdefault("use_reentrant", False)
|
||||
return torch.utils.checkpoint.checkpoint(function, *args, **kwargs)
|
||||
|
||||
def get_activation(activation: Literal["elu", "snake", "none"], antialias=False, channels=None) -> nn.Module:
|
||||
if activation == "elu":
|
||||
act = nn.ELU()
|
||||
elif activation == "snake":
|
||||
act = SnakeBeta(channels)
|
||||
elif activation == "none":
|
||||
act = nn.Identity()
|
||||
else:
|
||||
raise ValueError(f"Unknown activation {activation}")
|
||||
|
||||
if antialias:
|
||||
act = Activation1d(act)
|
||||
|
||||
return act
|
||||
|
||||
class ResidualUnit(nn.Module):
|
||||
def __init__(self, in_channels, out_channels, dilation, use_snake=False, antialias_activation=False):
|
||||
super().__init__()
|
||||
|
||||
self.dilation = dilation
|
||||
|
||||
padding = (dilation * (7-1)) // 2
|
||||
|
||||
self.layers = nn.Sequential(
|
||||
get_activation("snake" if use_snake else "elu", antialias=antialias_activation, channels=out_channels),
|
||||
WNConv1d(in_channels=in_channels, out_channels=out_channels,
|
||||
kernel_size=7, dilation=dilation, padding=padding),
|
||||
get_activation("snake" if use_snake else "elu", antialias=antialias_activation, channels=out_channels),
|
||||
WNConv1d(in_channels=out_channels, out_channels=out_channels,
|
||||
kernel_size=1)
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
res = x
|
||||
|
||||
#x = checkpoint(self.layers, x)
|
||||
x = self.layers(x)
|
||||
|
||||
return x + res
|
||||
|
||||
class EncoderBlock(nn.Module):
|
||||
def __init__(self, in_channels, out_channels, stride, use_snake=False, antialias_activation=False):
|
||||
super().__init__()
|
||||
|
||||
self.layers = nn.Sequential(
|
||||
ResidualUnit(in_channels=in_channels,
|
||||
out_channels=in_channels, dilation=1, use_snake=use_snake),
|
||||
ResidualUnit(in_channels=in_channels,
|
||||
out_channels=in_channels, dilation=3, use_snake=use_snake),
|
||||
ResidualUnit(in_channels=in_channels,
|
||||
out_channels=in_channels, dilation=9, use_snake=use_snake),
|
||||
get_activation("snake" if use_snake else "elu", antialias=antialias_activation, channels=in_channels),
|
||||
WNConv1d(in_channels=in_channels, out_channels=out_channels,
|
||||
kernel_size=2*stride, stride=stride, padding=math.ceil(stride/2)),
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
return self.layers(x)
|
||||
|
||||
class DecoderBlock(nn.Module):
|
||||
def __init__(self, in_channels, out_channels, stride, use_snake=False, antialias_activation=False, use_nearest_upsample=False):
|
||||
super().__init__()
|
||||
|
||||
if use_nearest_upsample:
|
||||
upsample_layer = nn.Sequential(
|
||||
nn.Upsample(scale_factor=stride, mode="nearest"),
|
||||
WNConv1d(in_channels=in_channels,
|
||||
out_channels=out_channels,
|
||||
kernel_size=2*stride,
|
||||
stride=1,
|
||||
bias=False,
|
||||
padding='same')
|
||||
)
|
||||
else:
|
||||
upsample_layer = WNConvTranspose1d(in_channels=in_channels,
|
||||
out_channels=out_channels,
|
||||
kernel_size=2*stride, stride=stride, padding=math.ceil(stride/2))
|
||||
|
||||
self.layers = nn.Sequential(
|
||||
get_activation("snake" if use_snake else "elu", antialias=antialias_activation, channels=in_channels),
|
||||
upsample_layer,
|
||||
ResidualUnit(in_channels=out_channels, out_channels=out_channels,
|
||||
dilation=1, use_snake=use_snake),
|
||||
ResidualUnit(in_channels=out_channels, out_channels=out_channels,
|
||||
dilation=3, use_snake=use_snake),
|
||||
ResidualUnit(in_channels=out_channels, out_channels=out_channels,
|
||||
dilation=9, use_snake=use_snake),
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
return self.layers(x)
|
||||
|
||||
class OobleckEncoder(nn.Module):
|
||||
def __init__(self,
|
||||
in_channels=2,
|
||||
channels=128,
|
||||
latent_dim=32,
|
||||
c_mults = [1, 2, 4, 8],
|
||||
strides = [2, 4, 8, 8],
|
||||
use_snake=False,
|
||||
antialias_activation=False
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
c_mults = [1] + c_mults
|
||||
|
||||
self.depth = len(c_mults)
|
||||
|
||||
layers = [
|
||||
WNConv1d(in_channels=in_channels, out_channels=c_mults[0] * channels, kernel_size=7, padding=3)
|
||||
]
|
||||
|
||||
for i in range(self.depth-1):
|
||||
layers += [EncoderBlock(in_channels=c_mults[i]*channels, out_channels=c_mults[i+1]*channels, stride=strides[i], use_snake=use_snake)]
|
||||
|
||||
layers += [
|
||||
get_activation("snake" if use_snake else "elu", antialias=antialias_activation, channels=c_mults[-1] * channels),
|
||||
WNConv1d(in_channels=c_mults[-1]*channels, out_channels=latent_dim, kernel_size=3, padding=1)
|
||||
]
|
||||
|
||||
self.layers = nn.Sequential(*layers)
|
||||
|
||||
def forward(self, x):
|
||||
return self.layers(x)
|
||||
|
||||
|
||||
class OobleckDecoder(nn.Module):
|
||||
def __init__(self,
|
||||
out_channels=2,
|
||||
channels=128,
|
||||
latent_dim=32,
|
||||
c_mults = [1, 2, 4, 8],
|
||||
strides = [2, 4, 8, 8],
|
||||
use_snake=False,
|
||||
antialias_activation=False,
|
||||
use_nearest_upsample=False,
|
||||
final_tanh=True):
|
||||
super().__init__()
|
||||
|
||||
c_mults = [1] + c_mults
|
||||
|
||||
self.depth = len(c_mults)
|
||||
|
||||
layers = [
|
||||
WNConv1d(in_channels=latent_dim, out_channels=c_mults[-1]*channels, kernel_size=7, padding=3),
|
||||
]
|
||||
|
||||
for i in range(self.depth-1, 0, -1):
|
||||
layers += [DecoderBlock(
|
||||
in_channels=c_mults[i]*channels,
|
||||
out_channels=c_mults[i-1]*channels,
|
||||
stride=strides[i-1],
|
||||
use_snake=use_snake,
|
||||
antialias_activation=antialias_activation,
|
||||
use_nearest_upsample=use_nearest_upsample
|
||||
)
|
||||
]
|
||||
|
||||
layers += [
|
||||
get_activation("snake" if use_snake else "elu", antialias=antialias_activation, channels=c_mults[0] * channels),
|
||||
WNConv1d(in_channels=c_mults[0] * channels, out_channels=out_channels, kernel_size=7, padding=3, bias=False),
|
||||
nn.Tanh() if final_tanh else nn.Identity()
|
||||
]
|
||||
|
||||
self.layers = nn.Sequential(*layers)
|
||||
|
||||
def forward(self, x):
|
||||
return self.layers(x)
|
||||
|
||||
|
||||
class DACEncoderWrapper(nn.Module):
|
||||
def __init__(self, in_channels=1, **kwargs):
|
||||
super().__init__()
|
||||
|
||||
from dac.model.dac import Encoder as DACEncoder
|
||||
|
||||
latent_dim = kwargs.pop("latent_dim", None)
|
||||
|
||||
encoder_out_dim = kwargs["d_model"] * (2 ** len(kwargs["strides"]))
|
||||
self.encoder = DACEncoder(d_latent=encoder_out_dim, **kwargs)
|
||||
self.latent_dim = latent_dim
|
||||
|
||||
# Latent-dim support was added to DAC after this was first written, and implemented differently, so this is for backwards compatibility
|
||||
self.proj_out = nn.Conv1d(self.encoder.enc_dim, latent_dim, kernel_size=1) if latent_dim is not None else nn.Identity()
|
||||
|
||||
if in_channels != 1:
|
||||
self.encoder.block[0] = WNConv1d(in_channels, kwargs.get("d_model", 64), kernel_size=7, padding=3)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.encoder(x)
|
||||
x = self.proj_out(x)
|
||||
return x
|
||||
|
||||
class DACDecoderWrapper(nn.Module):
|
||||
def __init__(self, latent_dim, out_channels=1, **kwargs):
|
||||
super().__init__()
|
||||
|
||||
from dac.model.dac import Decoder as DACDecoder
|
||||
|
||||
self.decoder = DACDecoder(**kwargs, input_channel = latent_dim, d_out=out_channels)
|
||||
|
||||
self.latent_dim = latent_dim
|
||||
|
||||
def forward(self, x):
|
||||
return self.decoder(x)
|
||||
|
||||
class AudioAutoencoder(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
encoder,
|
||||
decoder,
|
||||
latent_dim,
|
||||
downsampling_ratio,
|
||||
sample_rate,
|
||||
io_channels=2,
|
||||
bottleneck: Bottleneck = None,
|
||||
pretransform: Pretransform = None,
|
||||
in_channels = None,
|
||||
out_channels = None,
|
||||
soft_clip = False
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.downsampling_ratio = downsampling_ratio
|
||||
self.sample_rate = sample_rate
|
||||
|
||||
self.latent_dim = latent_dim
|
||||
self.io_channels = io_channels
|
||||
self.in_channels = io_channels
|
||||
self.out_channels = io_channels
|
||||
|
||||
self.min_length = self.downsampling_ratio
|
||||
|
||||
if in_channels is not None:
|
||||
self.in_channels = in_channels
|
||||
|
||||
if out_channels is not None:
|
||||
self.out_channels = out_channels
|
||||
|
||||
self.bottleneck = bottleneck
|
||||
|
||||
self.encoder = encoder
|
||||
|
||||
self.decoder = decoder
|
||||
|
||||
self.pretransform = pretransform
|
||||
|
||||
self.soft_clip = soft_clip
|
||||
|
||||
self.is_discrete = self.bottleneck is not None and self.bottleneck.is_discrete
|
||||
|
||||
def encode(self, audio, return_info=False, skip_pretransform=False, iterate_batch=False, **kwargs):
|
||||
|
||||
info = {}
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
if self.pretransform is not None and not skip_pretransform:
|
||||
if self.pretransform.enable_grad:
|
||||
if iterate_batch:
|
||||
audios = []
|
||||
for i in range(audio.shape[0]):
|
||||
audios.append(self.pretransform.encode(audio[i:i+1]))
|
||||
audio = torch.cat(audios, dim=0)
|
||||
else:
|
||||
audio = self.pretransform.encode(audio)
|
||||
else:
|
||||
with torch.no_grad():
|
||||
if iterate_batch:
|
||||
audios = []
|
||||
for i in range(audio.shape[0]):
|
||||
audios.append(self.pretransform.encode(audio[i:i+1]))
|
||||
audio = torch.cat(audios, dim=0)
|
||||
else:
|
||||
audio = self.pretransform.encode(audio)
|
||||
|
||||
if self.encoder is not None:
|
||||
if iterate_batch:
|
||||
latents = []
|
||||
for i in range(audio.shape[0]):
|
||||
latents.append(self.encoder(audio[i:i+1]))
|
||||
latents = torch.cat(latents, dim=0)
|
||||
else:
|
||||
latents = self.encoder(audio)
|
||||
else:
|
||||
latents = audio
|
||||
|
||||
if self.bottleneck is not None:
|
||||
# TODO: Add iterate batch logic, needs to merge the info dicts
|
||||
latents, bottleneck_info = self.bottleneck.encode(latents, return_info=True, **kwargs)
|
||||
|
||||
info.update(bottleneck_info)
|
||||
|
||||
if return_info:
|
||||
return latents, info
|
||||
|
||||
return latents
|
||||
|
||||
def decode(self, latents, iterate_batch=False, **kwargs):
|
||||
|
||||
if self.bottleneck is not None:
|
||||
if iterate_batch:
|
||||
decoded = []
|
||||
for i in range(latents.shape[0]):
|
||||
decoded.append(self.bottleneck.decode(latents[i:i+1]))
|
||||
latents = torch.cat(decoded, dim=0)
|
||||
else:
|
||||
latents = self.bottleneck.decode(latents)
|
||||
|
||||
if iterate_batch:
|
||||
decoded = []
|
||||
for i in range(latents.shape[0]):
|
||||
decoded.append(self.decoder(latents[i:i+1]))
|
||||
decoded = torch.cat(decoded, dim=0)
|
||||
else:
|
||||
decoded = self.decoder(latents, **kwargs)
|
||||
|
||||
if self.pretransform is not None:
|
||||
if self.pretransform.enable_grad:
|
||||
if iterate_batch:
|
||||
decodeds = []
|
||||
for i in range(decoded.shape[0]):
|
||||
decodeds.append(self.pretransform.decode(decoded[i:i+1]))
|
||||
decoded = torch.cat(decodeds, dim=0)
|
||||
else:
|
||||
decoded = self.pretransform.decode(decoded)
|
||||
else:
|
||||
with torch.no_grad():
|
||||
if iterate_batch:
|
||||
decodeds = []
|
||||
for i in range(latents.shape[0]):
|
||||
decodeds.append(self.pretransform.decode(decoded[i:i+1]))
|
||||
decoded = torch.cat(decodeds, dim=0)
|
||||
else:
|
||||
decoded = self.pretransform.decode(decoded)
|
||||
|
||||
if self.soft_clip:
|
||||
decoded = torch.tanh(decoded)
|
||||
|
||||
return decoded
|
||||
|
||||
def decode_tokens(self, tokens, **kwargs):
|
||||
'''
|
||||
Decode discrete tokens to audio
|
||||
Only works with discrete autoencoders
|
||||
'''
|
||||
|
||||
assert isinstance(self.bottleneck, DiscreteBottleneck), "decode_tokens only works with discrete autoencoders"
|
||||
|
||||
latents = self.bottleneck.decode_tokens(tokens, **kwargs)
|
||||
|
||||
return self.decode(latents, **kwargs)
|
||||
|
||||
|
||||
def preprocess_audio_for_encoder(self, audio, in_sr):
|
||||
'''
|
||||
Preprocess single audio tensor (Channels x Length) to be compatible with the encoder.
|
||||
If the model is mono, stereo audio will be converted to mono.
|
||||
Audio will be silence-padded to be a multiple of the model's downsampling ratio.
|
||||
Audio will be resampled to the model's sample rate.
|
||||
The output will have batch size 1 and be shape (1 x Channels x Length)
|
||||
'''
|
||||
return self.preprocess_audio_list_for_encoder([audio], [in_sr])
|
||||
|
||||
def preprocess_audio_list_for_encoder(self, audio_list, in_sr_list):
|
||||
'''
|
||||
Preprocess a [list] of audio (Channels x Length) into a batch tensor to be compatable with the encoder.
|
||||
The audio in that list can be of different lengths and channels.
|
||||
in_sr can be an integer or list. If it's an integer it will be assumed it is the input sample_rate for every audio.
|
||||
All audio will be resampled to the model's sample rate.
|
||||
Audio will be silence-padded to the longest length, and further padded to be a multiple of the model's downsampling ratio.
|
||||
If the model is mono, all audio will be converted to mono.
|
||||
The output will be a tensor of shape (Batch x Channels x Length)
|
||||
'''
|
||||
batch_size = len(audio_list)
|
||||
if isinstance(in_sr_list, int):
|
||||
in_sr_list = [in_sr_list]*batch_size
|
||||
assert len(in_sr_list) == batch_size, "list of sample rates must be the same length of audio_list"
|
||||
new_audio = []
|
||||
max_length = 0
|
||||
# resample & find the max length
|
||||
for i in range(batch_size):
|
||||
audio = audio_list[i]
|
||||
in_sr = in_sr_list[i]
|
||||
if len(audio.shape) == 3 and audio.shape[0] == 1:
|
||||
# batchsize 1 was given by accident. Just squeeze it.
|
||||
audio = audio.squeeze(0)
|
||||
elif len(audio.shape) == 1:
|
||||
# Mono signal, channel dimension is missing, unsqueeze it in
|
||||
audio = audio.unsqueeze(0)
|
||||
assert len(audio.shape)==2, "Audio should be shape (Channels x Length) with no batch dimension"
|
||||
# Resample audio
|
||||
if in_sr != self.sample_rate:
|
||||
resample_tf = T.Resample(in_sr, self.sample_rate).to(audio.device)
|
||||
audio = resample_tf(audio)
|
||||
new_audio.append(audio)
|
||||
if audio.shape[-1] > max_length:
|
||||
max_length = audio.shape[-1]
|
||||
# Pad every audio to the same length, multiple of model's downsampling ratio
|
||||
padded_audio_length = max_length + (self.min_length - (max_length % self.min_length)) % self.min_length
|
||||
for i in range(batch_size):
|
||||
# Pad it & if necessary, mixdown/duplicate stereo/mono channels to support model
|
||||
new_audio[i] = prepare_audio(new_audio[i], in_sr=in_sr, target_sr=in_sr, target_length=padded_audio_length,
|
||||
target_channels=self.in_channels, device=new_audio[i].device).squeeze(0)
|
||||
# convert to tensor
|
||||
return torch.stack(new_audio)
|
||||
|
||||
def encode_audio(self, audio, chunked=False, overlap=32, chunk_size=128, **kwargs):
|
||||
'''
|
||||
Encode audios into latents. Audios should already be preprocesed by preprocess_audio_for_encoder.
|
||||
If chunked is True, split the audio into chunks of a given maximum size chunk_size, with given overlap.
|
||||
Overlap and chunk_size params are both measured in number of latents (not audio samples)
|
||||
# and therefore you likely could use the same values with decode_audio.
|
||||
A overlap of zero will cause discontinuity artefacts. Overlap should be => receptive field size.
|
||||
Every autoencoder will have a different receptive field size, and thus ideal overlap.
|
||||
You can determine it empirically by diffing unchunked vs chunked output and looking at maximum diff.
|
||||
The final chunk may have a longer overlap in order to keep chunk_size consistent for all chunks.
|
||||
Smaller chunk_size uses less memory, but more compute.
|
||||
The chunk_size vs memory tradeoff isn't linear, and possibly depends on the GPU and CUDA version
|
||||
For example, on a A6000 chunk_size 128 is overall faster than 256 and 512 even though it has more chunks
|
||||
'''
|
||||
if not chunked:
|
||||
# default behavior. Encode the entire audio in parallel
|
||||
return self.encode(audio, **kwargs)
|
||||
else:
|
||||
# CHUNKED ENCODING
|
||||
# samples_per_latent is just the downsampling ratio (which is also the upsampling ratio)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
samples_per_latent = self.downsampling_ratio
|
||||
total_size = audio.shape[2] # in samples
|
||||
print(f'audio shape: {audio.shape}')
|
||||
batch_size = audio.shape[0]
|
||||
chunk_size *= samples_per_latent # converting metric in latents to samples
|
||||
overlap *= samples_per_latent # converting metric in latents to samples
|
||||
hop_size = chunk_size - overlap
|
||||
chunks = []
|
||||
for i in range(0, total_size - chunk_size + 1, hop_size):
|
||||
chunk = audio[:,:,i:i+chunk_size]
|
||||
chunks.append(chunk)
|
||||
if i+chunk_size != total_size:
|
||||
# Final chunk
|
||||
chunk = audio[:,:,-chunk_size:]
|
||||
chunks.append(chunk)
|
||||
chunks = torch.stack(chunks)
|
||||
num_chunks = chunks.shape[0]
|
||||
# Note: y_size might be a different value from the latent length used in diffusion training
|
||||
# because we can encode audio of varying lengths
|
||||
# However, the audio should've been padded to a multiple of samples_per_latent by now.
|
||||
y_size = total_size // samples_per_latent
|
||||
# Create an empty latent, we will populate it with chunks as we encode them
|
||||
y_final = torch.zeros((batch_size,self.latent_dim,y_size)).to(audio.device)
|
||||
print(f'y_final shape: {y_final.shape}')
|
||||
for i in range(num_chunks):
|
||||
x_chunk = chunks[i,:]
|
||||
# encode the chunk
|
||||
y_chunk = self.encode(x_chunk)
|
||||
print(f'y_chunk shape: {y_chunk.shape}')
|
||||
# figure out where to put the audio along the time domain
|
||||
if i == num_chunks-1:
|
||||
# final chunk always goes at the end
|
||||
t_end = y_size
|
||||
t_start = t_end - y_chunk.shape[2]
|
||||
else:
|
||||
t_start = i * hop_size // samples_per_latent
|
||||
t_end = t_start + chunk_size // samples_per_latent
|
||||
# remove the edges of the overlaps
|
||||
ol = overlap//samples_per_latent//2
|
||||
chunk_start = 0
|
||||
chunk_end = y_chunk.shape[2]
|
||||
if i > 0:
|
||||
# no overlap for the start of the first chunk
|
||||
t_start += ol
|
||||
chunk_start += ol
|
||||
if i < num_chunks-1:
|
||||
# no overlap for the end of the last chunk
|
||||
t_end -= ol
|
||||
chunk_end -= ol
|
||||
# paste the chunked audio into our y_final output audio
|
||||
y_final[:,:,t_start:t_end] = y_chunk[:,:,chunk_start:chunk_end]
|
||||
return y_final
|
||||
|
||||
def decode_audio(self, latents, chunked=False, overlap=32, chunk_size=128, **kwargs):
|
||||
'''
|
||||
Decode latents to audio.
|
||||
If chunked is True, split the latents into chunks of a given maximum size chunk_size, with given overlap, both of which are measured in number of latents.
|
||||
A overlap of zero will cause discontinuity artefacts. Overlap should be => receptive field size.
|
||||
Every autoencoder will have a different receptive field size, and thus ideal overlap.
|
||||
You can determine it empirically by diffing unchunked vs chunked audio and looking at maximum diff.
|
||||
The final chunk may have a longer overlap in order to keep chunk_size consistent for all chunks.
|
||||
Smaller chunk_size uses less memory, but more compute.
|
||||
The chunk_size vs memory tradeoff isn't linear, and possibly depends on the GPU and CUDA version
|
||||
For example, on a A6000 chunk_size 128 is overall faster than 256 and 512 even though it has more chunks
|
||||
'''
|
||||
if not chunked:
|
||||
# default behavior. Decode the entire latent in parallel
|
||||
return self.decode(latents, **kwargs)
|
||||
else:
|
||||
# chunked decoding
|
||||
hop_size = chunk_size - overlap
|
||||
total_size = latents.shape[2]
|
||||
batch_size = latents.shape[0]
|
||||
chunks = []
|
||||
for i in range(0, total_size - chunk_size + 1, hop_size):
|
||||
chunk = latents[:,:,i:i+chunk_size]
|
||||
chunks.append(chunk)
|
||||
if i+chunk_size != total_size:
|
||||
# Final chunk
|
||||
chunk = latents[:,:,-chunk_size:]
|
||||
chunks.append(chunk)
|
||||
chunks = torch.stack(chunks)
|
||||
num_chunks = chunks.shape[0]
|
||||
# samples_per_latent is just the downsampling ratio
|
||||
samples_per_latent = self.downsampling_ratio
|
||||
# Create an empty waveform, we will populate it with chunks as decode them
|
||||
y_size = total_size * samples_per_latent
|
||||
y_final = torch.zeros((batch_size,self.out_channels,y_size)).to(latents.device)
|
||||
for i in range(num_chunks):
|
||||
x_chunk = chunks[i,:]
|
||||
# decode the chunk
|
||||
y_chunk = self.decode(x_chunk)
|
||||
# figure out where to put the audio along the time domain
|
||||
if i == num_chunks-1:
|
||||
# final chunk always goes at the end
|
||||
t_end = y_size
|
||||
t_start = t_end - y_chunk.shape[2]
|
||||
else:
|
||||
t_start = i * hop_size * samples_per_latent
|
||||
t_end = t_start + chunk_size * samples_per_latent
|
||||
# remove the edges of the overlaps
|
||||
ol = (overlap//2) * samples_per_latent
|
||||
chunk_start = 0
|
||||
chunk_end = y_chunk.shape[2]
|
||||
if i > 0:
|
||||
# no overlap for the start of the first chunk
|
||||
t_start += ol
|
||||
chunk_start += ol
|
||||
if i < num_chunks-1:
|
||||
# no overlap for the end of the last chunk
|
||||
t_end -= ol
|
||||
chunk_end -= ol
|
||||
# paste the chunked audio into our y_final output audio
|
||||
y_final[:,:,t_start:t_end] = y_chunk[:,:,chunk_start:chunk_end]
|
||||
return y_final
|
||||
|
||||
|
||||
class DiffusionAutoencoder(AudioAutoencoder):
|
||||
def __init__(
|
||||
self,
|
||||
diffusion: ConditionedDiffusionModel,
|
||||
diffusion_downsampling_ratio,
|
||||
*args,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
self.diffusion = diffusion
|
||||
|
||||
self.min_length = self.downsampling_ratio * diffusion_downsampling_ratio
|
||||
|
||||
if self.encoder is not None:
|
||||
# Shrink the initial encoder parameters to avoid saturated latents
|
||||
with torch.no_grad():
|
||||
for param in self.encoder.parameters():
|
||||
param *= 0.5
|
||||
|
||||
def decode(self, latents, steps=100):
|
||||
|
||||
upsampled_length = latents.shape[2] * self.downsampling_ratio
|
||||
|
||||
if self.bottleneck is not None:
|
||||
latents = self.bottleneck.decode(latents)
|
||||
|
||||
if self.decoder is not None:
|
||||
latents = self.decode(latents)
|
||||
|
||||
# Upsample latents to match diffusion length
|
||||
if latents.shape[2] != upsampled_length:
|
||||
latents = F.interpolate(latents, size=upsampled_length, mode='nearest')
|
||||
|
||||
noise = torch.randn(latents.shape[0], self.io_channels, upsampled_length, device=latents.device)
|
||||
decoded = sample(self.diffusion, noise, steps, 0, input_concat_cond=latents)
|
||||
|
||||
if self.pretransform is not None:
|
||||
if self.pretransform.enable_grad:
|
||||
decoded = self.pretransform.decode(decoded)
|
||||
else:
|
||||
with torch.no_grad():
|
||||
decoded = self.pretransform.decode(decoded)
|
||||
|
||||
return decoded
|
||||
|
||||
# AE factories
|
||||
|
||||
def create_encoder_from_config(encoder_config: Dict[str, Any]):
|
||||
encoder_type = encoder_config.get("type", None)
|
||||
assert encoder_type is not None, "Encoder type must be specified"
|
||||
|
||||
if encoder_type == "oobleck":
|
||||
encoder = OobleckEncoder(
|
||||
**encoder_config["config"]
|
||||
)
|
||||
|
||||
elif encoder_type == "seanet":
|
||||
from encodec.modules import SEANetEncoder
|
||||
seanet_encoder_config = encoder_config["config"]
|
||||
|
||||
#SEANet encoder expects strides in reverse order
|
||||
seanet_encoder_config["ratios"] = list(reversed(seanet_encoder_config.get("ratios", [2, 2, 2, 2, 2])))
|
||||
encoder = SEANetEncoder(
|
||||
**seanet_encoder_config
|
||||
)
|
||||
elif encoder_type == "dac":
|
||||
dac_config = encoder_config["config"]
|
||||
|
||||
encoder = DACEncoderWrapper(**dac_config)
|
||||
elif encoder_type == "local_attn":
|
||||
from .local_attention import TransformerEncoder1D
|
||||
|
||||
local_attn_config = encoder_config["config"]
|
||||
|
||||
encoder = TransformerEncoder1D(
|
||||
**local_attn_config
|
||||
)
|
||||
else:
|
||||
raise ValueError(f"Unknown encoder type {encoder_type}")
|
||||
|
||||
requires_grad = encoder_config.get("requires_grad", True)
|
||||
if not requires_grad:
|
||||
for param in encoder.parameters():
|
||||
param.requires_grad = False
|
||||
|
||||
return encoder
|
||||
|
||||
def create_decoder_from_config(decoder_config: Dict[str, Any]):
|
||||
decoder_type = decoder_config.get("type", None)
|
||||
assert decoder_type is not None, "Decoder type must be specified"
|
||||
|
||||
if decoder_type == "oobleck":
|
||||
decoder = OobleckDecoder(
|
||||
**decoder_config["config"]
|
||||
)
|
||||
elif decoder_type == "seanet":
|
||||
from encodec.modules import SEANetDecoder
|
||||
|
||||
decoder = SEANetDecoder(
|
||||
**decoder_config["config"]
|
||||
)
|
||||
elif decoder_type == "dac":
|
||||
dac_config = decoder_config["config"]
|
||||
|
||||
decoder = DACDecoderWrapper(**dac_config)
|
||||
elif decoder_type == "local_attn":
|
||||
from .local_attention import TransformerDecoder1D
|
||||
|
||||
local_attn_config = decoder_config["config"]
|
||||
|
||||
decoder = TransformerDecoder1D(
|
||||
**local_attn_config
|
||||
)
|
||||
else:
|
||||
raise ValueError(f"Unknown decoder type {decoder_type}")
|
||||
|
||||
requires_grad = decoder_config.get("requires_grad", True)
|
||||
if not requires_grad:
|
||||
for param in decoder.parameters():
|
||||
param.requires_grad = False
|
||||
|
||||
return decoder
|
||||
|
||||
def create_autoencoder_from_config(config: Dict[str, Any]):
|
||||
|
||||
ae_config = config["model"]
|
||||
|
||||
encoder = create_encoder_from_config(ae_config["encoder"])
|
||||
decoder = create_decoder_from_config(ae_config["decoder"])
|
||||
|
||||
bottleneck = ae_config.get("bottleneck", None)
|
||||
|
||||
latent_dim = ae_config.get("latent_dim", None)
|
||||
assert latent_dim is not None, "latent_dim must be specified in model config"
|
||||
downsampling_ratio = ae_config.get("downsampling_ratio", None)
|
||||
assert downsampling_ratio is not None, "downsampling_ratio must be specified in model config"
|
||||
io_channels = ae_config.get("io_channels", None)
|
||||
assert io_channels is not None, "io_channels must be specified in model config"
|
||||
sample_rate = config.get("sample_rate", None)
|
||||
assert sample_rate is not None, "sample_rate must be specified in model config"
|
||||
|
||||
in_channels = ae_config.get("in_channels", None)
|
||||
out_channels = ae_config.get("out_channels", None)
|
||||
|
||||
pretransform = ae_config.get("pretransform", None)
|
||||
|
||||
if pretransform is not None:
|
||||
pretransform = create_pretransform_from_config(pretransform, sample_rate)
|
||||
|
||||
if bottleneck is not None:
|
||||
bottleneck = create_bottleneck_from_config(bottleneck)
|
||||
|
||||
soft_clip = ae_config["decoder"].get("soft_clip", False)
|
||||
|
||||
return AudioAutoencoder(
|
||||
encoder,
|
||||
decoder,
|
||||
io_channels=io_channels,
|
||||
latent_dim=latent_dim,
|
||||
downsampling_ratio=downsampling_ratio,
|
||||
sample_rate=sample_rate,
|
||||
bottleneck=bottleneck,
|
||||
pretransform=pretransform,
|
||||
in_channels=in_channels,
|
||||
out_channels=out_channels,
|
||||
soft_clip=soft_clip
|
||||
)
|
||||
|
||||
def create_diffAE_from_config(config: Dict[str, Any]):
|
||||
|
||||
diffae_config = config["model"]
|
||||
|
||||
if "encoder" in diffae_config:
|
||||
encoder = create_encoder_from_config(diffae_config["encoder"])
|
||||
else:
|
||||
encoder = None
|
||||
|
||||
if "decoder" in diffae_config:
|
||||
decoder = create_decoder_from_config(diffae_config["decoder"])
|
||||
else:
|
||||
decoder = None
|
||||
|
||||
diffusion_model_type = diffae_config["diffusion"]["type"]
|
||||
|
||||
if diffusion_model_type == "DAU1d":
|
||||
diffusion = DAU1DCondWrapper(**diffae_config["diffusion"]["config"])
|
||||
elif diffusion_model_type == "adp_1d":
|
||||
diffusion = UNet1DCondWrapper(**diffae_config["diffusion"]["config"])
|
||||
elif diffusion_model_type == "dit":
|
||||
diffusion = DiTWrapper(**diffae_config["diffusion"]["config"])
|
||||
|
||||
latent_dim = diffae_config.get("latent_dim", None)
|
||||
assert latent_dim is not None, "latent_dim must be specified in model config"
|
||||
downsampling_ratio = diffae_config.get("downsampling_ratio", None)
|
||||
assert downsampling_ratio is not None, "downsampling_ratio must be specified in model config"
|
||||
io_channels = diffae_config.get("io_channels", None)
|
||||
assert io_channels is not None, "io_channels must be specified in model config"
|
||||
sample_rate = config.get("sample_rate", None)
|
||||
assert sample_rate is not None, "sample_rate must be specified in model config"
|
||||
|
||||
bottleneck = diffae_config.get("bottleneck", None)
|
||||
|
||||
pretransform = diffae_config.get("pretransform", None)
|
||||
|
||||
if pretransform is not None:
|
||||
pretransform = create_pretransform_from_config(pretransform, sample_rate)
|
||||
|
||||
if bottleneck is not None:
|
||||
bottleneck = create_bottleneck_from_config(bottleneck)
|
||||
|
||||
diffusion_downsampling_ratio = None,
|
||||
|
||||
if diffusion_model_type == "DAU1d":
|
||||
diffusion_downsampling_ratio = np.prod(diffae_config["diffusion"]["config"]["strides"])
|
||||
elif diffusion_model_type == "adp_1d":
|
||||
diffusion_downsampling_ratio = np.prod(diffae_config["diffusion"]["config"]["factors"])
|
||||
elif diffusion_model_type == "dit":
|
||||
diffusion_downsampling_ratio = 1
|
||||
|
||||
return DiffusionAutoencoder(
|
||||
encoder=encoder,
|
||||
decoder=decoder,
|
||||
diffusion=diffusion,
|
||||
io_channels=io_channels,
|
||||
sample_rate=sample_rate,
|
||||
latent_dim=latent_dim,
|
||||
downsampling_ratio=downsampling_ratio,
|
||||
diffusion_downsampling_ratio=diffusion_downsampling_ratio,
|
||||
bottleneck=bottleneck,
|
||||
pretransform=pretransform
|
||||
)
|
||||
@@ -0,0 +1,430 @@
|
||||
from functools import reduce
|
||||
import math
|
||||
import numpy as np
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import functional as F
|
||||
|
||||
from torch.backends.cuda import sdp_kernel
|
||||
from packaging import version
|
||||
|
||||
from dac.nn.layers import Snake1d
|
||||
|
||||
class ResidualBlock(nn.Module):
|
||||
def __init__(self, main, skip=None):
|
||||
super().__init__()
|
||||
self.main = nn.Sequential(*main)
|
||||
self.skip = skip if skip else nn.Identity()
|
||||
|
||||
def forward(self, input):
|
||||
return self.main(input) + self.skip(input)
|
||||
|
||||
class ResConvBlock(ResidualBlock):
|
||||
def __init__(self, c_in, c_mid, c_out, is_last=False, kernel_size=5, conv_bias=True, use_snake=False):
|
||||
skip = None if c_in == c_out else nn.Conv1d(c_in, c_out, 1, bias=False)
|
||||
super().__init__([
|
||||
nn.Conv1d(c_in, c_mid, kernel_size, padding=kernel_size//2, bias=conv_bias),
|
||||
nn.GroupNorm(1, c_mid),
|
||||
Snake1d(c_mid) if use_snake else nn.GELU(),
|
||||
nn.Conv1d(c_mid, c_out, kernel_size, padding=kernel_size//2, bias=conv_bias),
|
||||
nn.GroupNorm(1, c_out) if not is_last else nn.Identity(),
|
||||
(Snake1d(c_out) if use_snake else nn.GELU()) if not is_last else nn.Identity(),
|
||||
], skip)
|
||||
|
||||
class SelfAttention1d(nn.Module):
|
||||
def __init__(self, c_in, n_head=1, dropout_rate=0.):
|
||||
super().__init__()
|
||||
assert c_in % n_head == 0
|
||||
self.norm = nn.GroupNorm(1, c_in)
|
||||
self.n_head = n_head
|
||||
self.qkv_proj = nn.Conv1d(c_in, c_in * 3, 1)
|
||||
self.out_proj = nn.Conv1d(c_in, c_in, 1)
|
||||
self.dropout = nn.Dropout(dropout_rate, inplace=True)
|
||||
|
||||
self.use_flash = torch.cuda.is_available() and version.parse(torch.__version__) >= version.parse('2.0.0')
|
||||
|
||||
if not self.use_flash:
|
||||
return
|
||||
|
||||
device_properties = torch.cuda.get_device_properties(torch.device('cuda'))
|
||||
|
||||
if device_properties.major == 8 and device_properties.minor == 0:
|
||||
# Use flash attention for A100 GPUs
|
||||
self.sdp_kernel_config = (True, False, False)
|
||||
else:
|
||||
# Don't use flash attention for other GPUs
|
||||
self.sdp_kernel_config = (False, True, True)
|
||||
|
||||
def forward(self, input):
|
||||
n, c, s = input.shape
|
||||
qkv = self.qkv_proj(self.norm(input))
|
||||
qkv = qkv.view(
|
||||
[n, self.n_head * 3, c // self.n_head, s]).transpose(2, 3)
|
||||
q, k, v = qkv.chunk(3, dim=1)
|
||||
scale = k.shape[3]**-0.25
|
||||
|
||||
if self.use_flash:
|
||||
with sdp_kernel(*self.sdp_kernel_config):
|
||||
y = F.scaled_dot_product_attention(q, k, v, is_causal=False).contiguous().view([n, c, s])
|
||||
else:
|
||||
att = ((q * scale) @ (k.transpose(2, 3) * scale)).softmax(3)
|
||||
y = (att @ v).transpose(2, 3).contiguous().view([n, c, s])
|
||||
|
||||
|
||||
return input + self.dropout(self.out_proj(y))
|
||||
|
||||
class SkipBlock(nn.Module):
|
||||
def __init__(self, *main):
|
||||
super().__init__()
|
||||
self.main = nn.Sequential(*main)
|
||||
|
||||
def forward(self, input):
|
||||
return torch.cat([self.main(input), input], dim=1)
|
||||
|
||||
class FourierFeatures(nn.Module):
|
||||
def __init__(self, in_features, out_features, std=1.):
|
||||
super().__init__()
|
||||
assert out_features % 2 == 0
|
||||
self.weight = nn.Parameter(torch.randn(
|
||||
[out_features // 2, in_features]) * std)
|
||||
|
||||
def forward(self, input):
|
||||
f = 2 * math.pi * input @ self.weight.T
|
||||
return torch.cat([f.cos(), f.sin()], dim=-1)
|
||||
|
||||
def expand_to_planes(input, shape):
|
||||
return input[..., None].repeat([1, 1, shape[2]])
|
||||
|
||||
_kernels = {
|
||||
'linear':
|
||||
[1 / 8, 3 / 8, 3 / 8, 1 / 8],
|
||||
'cubic':
|
||||
[-0.01171875, -0.03515625, 0.11328125, 0.43359375,
|
||||
0.43359375, 0.11328125, -0.03515625, -0.01171875],
|
||||
'lanczos3':
|
||||
[0.003689131001010537, 0.015056144446134567, -0.03399861603975296,
|
||||
-0.066637322306633, 0.13550527393817902, 0.44638532400131226,
|
||||
0.44638532400131226, 0.13550527393817902, -0.066637322306633,
|
||||
-0.03399861603975296, 0.015056144446134567, 0.003689131001010537]
|
||||
}
|
||||
|
||||
class Downsample1d(nn.Module):
|
||||
def __init__(self, kernel='linear', pad_mode='reflect', channels_last=False):
|
||||
super().__init__()
|
||||
self.pad_mode = pad_mode
|
||||
kernel_1d = torch.tensor(_kernels[kernel])
|
||||
self.pad = kernel_1d.shape[0] // 2 - 1
|
||||
self.register_buffer('kernel', kernel_1d)
|
||||
self.channels_last = channels_last
|
||||
|
||||
def forward(self, x):
|
||||
if self.channels_last:
|
||||
x = x.permute(0, 2, 1)
|
||||
x = F.pad(x, (self.pad,) * 2, self.pad_mode)
|
||||
weight = x.new_zeros([x.shape[1], x.shape[1], self.kernel.shape[0]])
|
||||
indices = torch.arange(x.shape[1], device=x.device)
|
||||
weight[indices, indices] = self.kernel.to(weight)
|
||||
x = F.conv1d(x, weight, stride=2)
|
||||
if self.channels_last:
|
||||
x = x.permute(0, 2, 1)
|
||||
return x
|
||||
|
||||
|
||||
class Upsample1d(nn.Module):
|
||||
def __init__(self, kernel='linear', pad_mode='reflect', channels_last=False):
|
||||
super().__init__()
|
||||
self.pad_mode = pad_mode
|
||||
kernel_1d = torch.tensor(_kernels[kernel]) * 2
|
||||
self.pad = kernel_1d.shape[0] // 2 - 1
|
||||
self.register_buffer('kernel', kernel_1d)
|
||||
self.channels_last = channels_last
|
||||
|
||||
def forward(self, x):
|
||||
if self.channels_last:
|
||||
x = x.permute(0, 2, 1)
|
||||
x = F.pad(x, ((self.pad + 1) // 2,) * 2, self.pad_mode)
|
||||
weight = x.new_zeros([x.shape[1], x.shape[1], self.kernel.shape[0]])
|
||||
indices = torch.arange(x.shape[1], device=x.device)
|
||||
weight[indices, indices] = self.kernel.to(weight)
|
||||
x = F.conv_transpose1d(x, weight, stride=2, padding=self.pad * 2 + 1)
|
||||
if self.channels_last:
|
||||
x = x.permute(0, 2, 1)
|
||||
return x
|
||||
|
||||
def Downsample1d_2(
|
||||
in_channels: int, out_channels: int, factor: int, kernel_multiplier: int = 2
|
||||
) -> nn.Module:
|
||||
assert kernel_multiplier % 2 == 0, "Kernel multiplier must be even"
|
||||
|
||||
return nn.Conv1d(
|
||||
in_channels=in_channels,
|
||||
out_channels=out_channels,
|
||||
kernel_size=factor * kernel_multiplier + 1,
|
||||
stride=factor,
|
||||
padding=factor * (kernel_multiplier // 2),
|
||||
)
|
||||
|
||||
|
||||
def Upsample1d_2(
|
||||
in_channels: int, out_channels: int, factor: int, use_nearest: bool = False
|
||||
) -> nn.Module:
|
||||
|
||||
if factor == 1:
|
||||
return nn.Conv1d(
|
||||
in_channels=in_channels, out_channels=out_channels, kernel_size=3, padding=1
|
||||
)
|
||||
|
||||
if use_nearest:
|
||||
return nn.Sequential(
|
||||
nn.Upsample(scale_factor=factor, mode="nearest"),
|
||||
nn.Conv1d(
|
||||
in_channels=in_channels,
|
||||
out_channels=out_channels,
|
||||
kernel_size=3,
|
||||
padding=1,
|
||||
),
|
||||
)
|
||||
else:
|
||||
return nn.ConvTranspose1d(
|
||||
in_channels=in_channels,
|
||||
out_channels=out_channels,
|
||||
kernel_size=factor * 2,
|
||||
stride=factor,
|
||||
padding=factor // 2 + factor % 2,
|
||||
output_padding=factor % 2,
|
||||
)
|
||||
|
||||
def zero_init(layer):
|
||||
nn.init.zeros_(layer.weight)
|
||||
if layer.bias is not None:
|
||||
nn.init.zeros_(layer.bias)
|
||||
return layer
|
||||
|
||||
def rms_norm(x, scale, eps):
|
||||
dtype = reduce(torch.promote_types, (x.dtype, scale.dtype, torch.float32))
|
||||
mean_sq = torch.mean(x.to(dtype)**2, dim=-1, keepdim=True)
|
||||
scale = scale.to(dtype) * torch.rsqrt(mean_sq + eps)
|
||||
return x * scale.to(x.dtype)
|
||||
|
||||
#rms_norm = torch.compile(rms_norm)
|
||||
|
||||
class AdaRMSNorm(nn.Module):
|
||||
def __init__(self, features, cond_features, eps=1e-6):
|
||||
super().__init__()
|
||||
self.eps = eps
|
||||
self.linear = zero_init(nn.Linear(cond_features, features, bias=False))
|
||||
|
||||
def extra_repr(self):
|
||||
return f"eps={self.eps},"
|
||||
|
||||
def forward(self, x, cond):
|
||||
return rms_norm(x, self.linear(cond)[:, None, :] + 1, self.eps)
|
||||
|
||||
def normalize(x, eps=1e-4):
|
||||
dim = list(range(1, x.ndim))
|
||||
n = torch.linalg.vector_norm(x, dim=dim, keepdim=True)
|
||||
alpha = np.sqrt(n.numel() / x.numel())
|
||||
return x / torch.add(eps, n, alpha=alpha)
|
||||
|
||||
class ForcedWNConv1d(nn.Module):
|
||||
def __init__(self, in_channels, out_channels, kernel_size=1):
|
||||
super().__init__()
|
||||
self.weight = nn.Parameter(torch.randn([out_channels, in_channels, kernel_size]))
|
||||
|
||||
def forward(self, x):
|
||||
if self.training:
|
||||
with torch.no_grad():
|
||||
self.weight.copy_(normalize(self.weight))
|
||||
|
||||
fan_in = self.weight[0].numel()
|
||||
|
||||
w = normalize(self.weight) / math.sqrt(fan_in)
|
||||
|
||||
return F.conv1d(x, w, padding='same')
|
||||
|
||||
# Kernels
|
||||
|
||||
use_compile = True
|
||||
|
||||
def compile(function, *args, **kwargs):
|
||||
if not use_compile:
|
||||
return function
|
||||
try:
|
||||
return torch.compile(function, *args, **kwargs)
|
||||
except RuntimeError:
|
||||
return function
|
||||
|
||||
|
||||
@compile
|
||||
def linear_geglu(x, weight, bias=None):
|
||||
x = x @ weight.mT
|
||||
if bias is not None:
|
||||
x = x + bias
|
||||
x, gate = x.chunk(2, dim=-1)
|
||||
return x * F.gelu(gate)
|
||||
|
||||
|
||||
@compile
|
||||
def rms_norm(x, scale, eps):
|
||||
dtype = reduce(torch.promote_types, (x.dtype, scale.dtype, torch.float32))
|
||||
mean_sq = torch.mean(x.to(dtype)**2, dim=-1, keepdim=True)
|
||||
scale = scale.to(dtype) * torch.rsqrt(mean_sq + eps)
|
||||
return x * scale.to(x.dtype)
|
||||
|
||||
# Layers
|
||||
|
||||
class LinearGEGLU(nn.Linear):
|
||||
def __init__(self, in_features, out_features, bias=True):
|
||||
super().__init__(in_features, out_features * 2, bias=bias)
|
||||
self.out_features = out_features
|
||||
|
||||
def forward(self, x):
|
||||
return linear_geglu(x, self.weight, self.bias)
|
||||
|
||||
|
||||
class RMSNorm(nn.Module):
|
||||
def __init__(self, shape, fix_scale = False, eps=1e-6):
|
||||
super().__init__()
|
||||
self.eps = eps
|
||||
|
||||
if fix_scale:
|
||||
self.register_buffer("scale", torch.ones(shape))
|
||||
else:
|
||||
self.scale = nn.Parameter(torch.ones(shape))
|
||||
|
||||
def extra_repr(self):
|
||||
return f"shape={tuple(self.scale.shape)}, eps={self.eps}"
|
||||
|
||||
def forward(self, x):
|
||||
return rms_norm(x, self.scale, self.eps)
|
||||
|
||||
def snake_beta(x, alpha, beta):
|
||||
return x + (1.0 / (beta + 0.000000001)) * pow(torch.sin(x * alpha), 2)
|
||||
|
||||
# try:
|
||||
# snake_beta = torch.compile(snake_beta)
|
||||
# except RuntimeError:
|
||||
# pass
|
||||
|
||||
# Adapted from https://github.com/NVIDIA/BigVGAN/blob/main/activations.py under MIT license
|
||||
# License available in LICENSES/LICENSE_NVIDIA.txt
|
||||
class SnakeBeta(nn.Module):
|
||||
|
||||
def __init__(self, in_features, alpha=1.0, alpha_trainable=True, alpha_logscale=True):
|
||||
super(SnakeBeta, self).__init__()
|
||||
self.in_features = in_features
|
||||
|
||||
# initialize alpha
|
||||
self.alpha_logscale = alpha_logscale
|
||||
if self.alpha_logscale: # log scale alphas initialized to zeros
|
||||
self.alpha = nn.Parameter(torch.zeros(in_features) * alpha)
|
||||
self.beta = nn.Parameter(torch.zeros(in_features) * alpha)
|
||||
else: # linear scale alphas initialized to ones
|
||||
self.alpha = nn.Parameter(torch.ones(in_features) * alpha)
|
||||
self.beta = nn.Parameter(torch.ones(in_features) * alpha)
|
||||
|
||||
self.alpha.requires_grad = alpha_trainable
|
||||
self.beta.requires_grad = alpha_trainable
|
||||
|
||||
self.no_div_by_zero = 0.000000001
|
||||
|
||||
def forward(self, x):
|
||||
alpha = self.alpha.unsqueeze(0).unsqueeze(-1) # line up with x to [B, C, T]
|
||||
beta = self.beta.unsqueeze(0).unsqueeze(-1)
|
||||
if self.alpha_logscale:
|
||||
alpha = torch.exp(alpha)
|
||||
beta = torch.exp(beta)
|
||||
x = snake_beta(x, alpha, beta)
|
||||
|
||||
return x
|
||||
|
||||
class ChannelLastConv1d(nn.Conv1d):
|
||||
|
||||
def forward(self, x: torch.Tensor) -> torch.Tensor:
|
||||
x = x.permute(0, 2, 1)
|
||||
x = super().forward(x)
|
||||
x = x.permute(0, 2, 1)
|
||||
return x
|
||||
|
||||
|
||||
# https://github.com/Stability-AI/sd3-ref
|
||||
class MLP(nn.Module):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
dim: int,
|
||||
hidden_dim: int,
|
||||
multiple_of: int = 256,
|
||||
):
|
||||
"""
|
||||
Initialize the FeedForward module.
|
||||
|
||||
Args:
|
||||
dim (int): Input dimension.
|
||||
hidden_dim (int): Hidden dimension of the feedforward layer.
|
||||
multiple_of (int): Value to ensure hidden dimension is a multiple of this value.
|
||||
|
||||
Attributes:
|
||||
w1 (ColumnParallelLinear): Linear transformation for the first layer.
|
||||
w2 (RowParallelLinear): Linear transformation for the second layer.
|
||||
w3 (ColumnParallelLinear): Linear transformation for the third layer.
|
||||
|
||||
"""
|
||||
super().__init__()
|
||||
hidden_dim = int(2 * hidden_dim / 3)
|
||||
hidden_dim = multiple_of * ((hidden_dim + multiple_of - 1) // multiple_of)
|
||||
|
||||
self.w1 = nn.Linear(dim, hidden_dim, bias=False)
|
||||
self.w2 = nn.Linear(hidden_dim, dim, bias=False)
|
||||
self.w3 = nn.Linear(dim, hidden_dim, bias=False)
|
||||
|
||||
def forward(self, x):
|
||||
return self.w2(F.silu(self.w1(x)) * self.w3(x))
|
||||
|
||||
|
||||
class ConvMLP(nn.Module):
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
dim: int,
|
||||
hidden_dim: int,
|
||||
multiple_of: int = 256,
|
||||
kernel_size: int = 3,
|
||||
padding: int = 1,
|
||||
):
|
||||
"""
|
||||
Initialize the FeedForward module.
|
||||
|
||||
Args:
|
||||
dim (int): Input dimension.
|
||||
hidden_dim (int): Hidden dimension of the feedforward layer.
|
||||
multiple_of (int): Value to ensure hidden dimension is a multiple of this value.
|
||||
|
||||
Attributes:
|
||||
w1 (ColumnParallelLinear): Linear transformation for the first layer.
|
||||
w2 (RowParallelLinear): Linear transformation for the second layer.
|
||||
w3 (ColumnParallelLinear): Linear transformation for the third layer.
|
||||
|
||||
"""
|
||||
super().__init__()
|
||||
hidden_dim = int(2 * hidden_dim / 3)
|
||||
hidden_dim = multiple_of * ((hidden_dim + multiple_of - 1) // multiple_of)
|
||||
|
||||
self.w1 = ChannelLastConv1d(dim,
|
||||
hidden_dim,
|
||||
bias=False,
|
||||
kernel_size=kernel_size,
|
||||
padding=padding)
|
||||
self.w2 = ChannelLastConv1d(hidden_dim,
|
||||
dim,
|
||||
bias=False,
|
||||
kernel_size=kernel_size,
|
||||
padding=padding)
|
||||
self.w3 = ChannelLastConv1d(dim,
|
||||
hidden_dim,
|
||||
bias=False,
|
||||
kernel_size=kernel_size,
|
||||
padding=padding)
|
||||
|
||||
def forward(self, x):
|
||||
return self.w2(F.silu(self.w1(x)) * self.w3(x))
|
||||
@@ -0,0 +1,355 @@
|
||||
import numpy as np
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import functional as F
|
||||
|
||||
from einops import rearrange
|
||||
from vector_quantize_pytorch import ResidualVQ, FSQ
|
||||
from dac.nn.quantize import ResidualVectorQuantize as DACResidualVQ
|
||||
|
||||
class Bottleneck(nn.Module):
|
||||
def __init__(self, is_discrete: bool = False):
|
||||
super().__init__()
|
||||
|
||||
self.is_discrete = is_discrete
|
||||
|
||||
def encode(self, x, return_info=False, **kwargs):
|
||||
raise NotImplementedError
|
||||
|
||||
def decode(self, x):
|
||||
raise NotImplementedError
|
||||
|
||||
class DiscreteBottleneck(Bottleneck):
|
||||
def __init__(self, num_quantizers, codebook_size, tokens_id):
|
||||
super().__init__(is_discrete=True)
|
||||
|
||||
self.num_quantizers = num_quantizers
|
||||
self.codebook_size = codebook_size
|
||||
self.tokens_id = tokens_id
|
||||
|
||||
def decode_tokens(self, codes, **kwargs):
|
||||
raise NotImplementedError
|
||||
|
||||
class TanhBottleneck(Bottleneck):
|
||||
def __init__(self):
|
||||
super().__init__(is_discrete=False)
|
||||
self.tanh = nn.Tanh()
|
||||
|
||||
def encode(self, x, return_info=False):
|
||||
info = {}
|
||||
|
||||
x = torch.tanh(x)
|
||||
|
||||
if return_info:
|
||||
return x, info
|
||||
else:
|
||||
return x
|
||||
|
||||
def decode(self, x):
|
||||
return x
|
||||
|
||||
def vae_sample(mean, scale):
|
||||
stdev = nn.functional.softplus(scale) + 1e-4
|
||||
var = stdev * stdev
|
||||
logvar = torch.log(var)
|
||||
latents = torch.randn_like(mean) * stdev + mean
|
||||
|
||||
kl = (mean * mean + var - logvar - 1).sum(1).mean()
|
||||
|
||||
return latents, kl
|
||||
|
||||
class VAEBottleneck(Bottleneck):
|
||||
def __init__(self):
|
||||
super().__init__(is_discrete=False)
|
||||
|
||||
def encode(self, x, return_info=False, **kwargs):
|
||||
info = {}
|
||||
|
||||
mean, scale = x.chunk(2, dim=1)
|
||||
|
||||
x, kl = vae_sample(mean, scale)
|
||||
|
||||
info["kl"] = kl
|
||||
|
||||
if return_info:
|
||||
return x, info
|
||||
else:
|
||||
return x
|
||||
|
||||
def decode(self, x):
|
||||
return x
|
||||
|
||||
def compute_mean_kernel(x, y):
|
||||
kernel_input = (x[:, None] - y[None]).pow(2).mean(2) / x.shape[-1]
|
||||
return torch.exp(-kernel_input).mean()
|
||||
|
||||
def compute_mmd(latents):
|
||||
latents_reshaped = latents.permute(0, 2, 1).reshape(-1, latents.shape[1])
|
||||
noise = torch.randn_like(latents_reshaped)
|
||||
|
||||
latents_kernel = compute_mean_kernel(latents_reshaped, latents_reshaped)
|
||||
noise_kernel = compute_mean_kernel(noise, noise)
|
||||
latents_noise_kernel = compute_mean_kernel(latents_reshaped, noise)
|
||||
|
||||
mmd = latents_kernel + noise_kernel - 2 * latents_noise_kernel
|
||||
return mmd.mean()
|
||||
|
||||
class WassersteinBottleneck(Bottleneck):
|
||||
def __init__(self, noise_augment_dim: int = 0, bypass_mmd: bool = False):
|
||||
super().__init__(is_discrete=False)
|
||||
|
||||
self.noise_augment_dim = noise_augment_dim
|
||||
self.bypass_mmd = bypass_mmd
|
||||
|
||||
def encode(self, x, return_info=False):
|
||||
info = {}
|
||||
|
||||
if self.training and return_info:
|
||||
if self.bypass_mmd:
|
||||
mmd = torch.tensor(0.0)
|
||||
else:
|
||||
mmd = compute_mmd(x)
|
||||
|
||||
info["mmd"] = mmd
|
||||
|
||||
if return_info:
|
||||
return x, info
|
||||
|
||||
return x
|
||||
|
||||
def decode(self, x):
|
||||
|
||||
if self.noise_augment_dim > 0:
|
||||
noise = torch.randn(x.shape[0], self.noise_augment_dim,
|
||||
x.shape[-1]).type_as(x)
|
||||
x = torch.cat([x, noise], dim=1)
|
||||
|
||||
return x
|
||||
|
||||
class L2Bottleneck(Bottleneck):
|
||||
def __init__(self):
|
||||
super().__init__(is_discrete=False)
|
||||
|
||||
def encode(self, x, return_info=False):
|
||||
info = {}
|
||||
|
||||
x = F.normalize(x, dim=1)
|
||||
|
||||
if return_info:
|
||||
return x, info
|
||||
else:
|
||||
return x
|
||||
|
||||
def decode(self, x):
|
||||
return F.normalize(x, dim=1)
|
||||
|
||||
class RVQBottleneck(DiscreteBottleneck):
|
||||
def __init__(self, **quantizer_kwargs):
|
||||
super().__init__(num_quantizers = quantizer_kwargs["num_quantizers"], codebook_size = quantizer_kwargs["codebook_size"], tokens_id = "quantizer_indices")
|
||||
self.quantizer = ResidualVQ(**quantizer_kwargs)
|
||||
self.num_quantizers = quantizer_kwargs["num_quantizers"]
|
||||
|
||||
def encode(self, x, return_info=False, **kwargs):
|
||||
info = {}
|
||||
|
||||
x = rearrange(x, "b c n -> b n c")
|
||||
x, indices, loss = self.quantizer(x)
|
||||
x = rearrange(x, "b n c -> b c n")
|
||||
|
||||
info["quantizer_indices"] = indices
|
||||
info["quantizer_loss"] = loss.mean()
|
||||
|
||||
if return_info:
|
||||
return x, info
|
||||
else:
|
||||
return x
|
||||
|
||||
def decode(self, x):
|
||||
return x
|
||||
|
||||
def decode_tokens(self, codes, **kwargs):
|
||||
latents = self.quantizer.get_outputs_from_indices(codes)
|
||||
|
||||
return self.decode(latents, **kwargs)
|
||||
|
||||
class RVQVAEBottleneck(DiscreteBottleneck):
|
||||
def __init__(self, **quantizer_kwargs):
|
||||
super().__init__(num_quantizers = quantizer_kwargs["num_quantizers"], codebook_size = quantizer_kwargs["codebook_size"], tokens_id = "quantizer_indices")
|
||||
self.quantizer = ResidualVQ(**quantizer_kwargs)
|
||||
self.num_quantizers = quantizer_kwargs["num_quantizers"]
|
||||
|
||||
def encode(self, x, return_info=False):
|
||||
info = {}
|
||||
|
||||
x, kl = vae_sample(*x.chunk(2, dim=1))
|
||||
|
||||
info["kl"] = kl
|
||||
|
||||
x = rearrange(x, "b c n -> b n c")
|
||||
x, indices, loss = self.quantizer(x)
|
||||
x = rearrange(x, "b n c -> b c n")
|
||||
|
||||
info["quantizer_indices"] = indices
|
||||
info["quantizer_loss"] = loss.mean()
|
||||
|
||||
if return_info:
|
||||
return x, info
|
||||
else:
|
||||
return x
|
||||
|
||||
def decode(self, x):
|
||||
return x
|
||||
|
||||
def decode_tokens(self, codes, **kwargs):
|
||||
latents = self.quantizer.get_outputs_from_indices(codes)
|
||||
|
||||
return self.decode(latents, **kwargs)
|
||||
|
||||
class DACRVQBottleneck(DiscreteBottleneck):
|
||||
def __init__(self, quantize_on_decode=False, noise_augment_dim=0, **quantizer_kwargs):
|
||||
super().__init__(num_quantizers = quantizer_kwargs["n_codebooks"], codebook_size = quantizer_kwargs["codebook_size"], tokens_id = "codes")
|
||||
self.quantizer = DACResidualVQ(**quantizer_kwargs)
|
||||
self.num_quantizers = quantizer_kwargs["n_codebooks"]
|
||||
self.quantize_on_decode = quantize_on_decode
|
||||
self.noise_augment_dim = noise_augment_dim
|
||||
|
||||
def encode(self, x, return_info=False, **kwargs):
|
||||
info = {}
|
||||
|
||||
info["pre_quantizer"] = x
|
||||
|
||||
if self.quantize_on_decode:
|
||||
return x, info if return_info else x
|
||||
|
||||
z, codes, latents, commitment_loss, codebook_loss = self.quantizer(x, **kwargs)
|
||||
|
||||
output = {
|
||||
"z": z,
|
||||
"codes": codes,
|
||||
"latents": latents,
|
||||
"vq/commitment_loss": commitment_loss,
|
||||
"vq/codebook_loss": codebook_loss,
|
||||
}
|
||||
|
||||
output["vq/commitment_loss"] /= self.num_quantizers
|
||||
output["vq/codebook_loss"] /= self.num_quantizers
|
||||
|
||||
info.update(output)
|
||||
|
||||
if return_info:
|
||||
return output["z"], info
|
||||
|
||||
return output["z"]
|
||||
|
||||
def decode(self, x):
|
||||
|
||||
if self.quantize_on_decode:
|
||||
x = self.quantizer(x)[0]
|
||||
|
||||
if self.noise_augment_dim > 0:
|
||||
noise = torch.randn(x.shape[0], self.noise_augment_dim,
|
||||
x.shape[-1]).type_as(x)
|
||||
x = torch.cat([x, noise], dim=1)
|
||||
|
||||
return x
|
||||
|
||||
def decode_tokens(self, codes, **kwargs):
|
||||
latents, _, _ = self.quantizer.from_codes(codes)
|
||||
|
||||
return self.decode(latents, **kwargs)
|
||||
|
||||
class DACRVQVAEBottleneck(DiscreteBottleneck):
|
||||
def __init__(self, quantize_on_decode=False, **quantizer_kwargs):
|
||||
super().__init__(num_quantizers = quantizer_kwargs["n_codebooks"], codebook_size = quantizer_kwargs["codebook_size"], tokens_id = "codes")
|
||||
self.quantizer = DACResidualVQ(**quantizer_kwargs)
|
||||
self.num_quantizers = quantizer_kwargs["n_codebooks"]
|
||||
self.quantize_on_decode = quantize_on_decode
|
||||
|
||||
def encode(self, x, return_info=False, n_quantizers: int = None):
|
||||
info = {}
|
||||
|
||||
mean, scale = x.chunk(2, dim=1)
|
||||
|
||||
x, kl = vae_sample(mean, scale)
|
||||
|
||||
info["pre_quantizer"] = x
|
||||
info["kl"] = kl
|
||||
|
||||
if self.quantize_on_decode:
|
||||
return x, info if return_info else x
|
||||
|
||||
z, codes, latents, commitment_loss, codebook_loss = self.quantizer(x, n_quantizers=n_quantizers)
|
||||
|
||||
output = {
|
||||
"z": z,
|
||||
"codes": codes,
|
||||
"latents": latents,
|
||||
"vq/commitment_loss": commitment_loss,
|
||||
"vq/codebook_loss": codebook_loss,
|
||||
}
|
||||
|
||||
output["vq/commitment_loss"] /= self.num_quantizers
|
||||
output["vq/codebook_loss"] /= self.num_quantizers
|
||||
|
||||
info.update(output)
|
||||
|
||||
if return_info:
|
||||
return output["z"], info
|
||||
|
||||
return output["z"]
|
||||
|
||||
def decode(self, x):
|
||||
|
||||
if self.quantize_on_decode:
|
||||
x = self.quantizer(x)[0]
|
||||
|
||||
return x
|
||||
|
||||
def decode_tokens(self, codes, **kwargs):
|
||||
latents, _, _ = self.quantizer.from_codes(codes)
|
||||
|
||||
return self.decode(latents, **kwargs)
|
||||
|
||||
class FSQBottleneck(DiscreteBottleneck):
|
||||
def __init__(self, noise_augment_dim=0, **kwargs):
|
||||
super().__init__(num_quantizers = kwargs.get("num_codebooks", 1), codebook_size = np.prod(kwargs["levels"]), tokens_id = "quantizer_indices")
|
||||
|
||||
self.noise_augment_dim = noise_augment_dim
|
||||
|
||||
self.quantizer = FSQ(**kwargs, allowed_dtypes=[torch.float16, torch.float32, torch.float64])
|
||||
|
||||
def encode(self, x, return_info=False):
|
||||
info = {}
|
||||
|
||||
orig_dtype = x.dtype
|
||||
x = x.float()
|
||||
|
||||
x = rearrange(x, "b c n -> b n c")
|
||||
x, indices = self.quantizer(x)
|
||||
x = rearrange(x, "b n c -> b c n")
|
||||
|
||||
x = x.to(orig_dtype)
|
||||
|
||||
# Reorder indices to match the expected format
|
||||
indices = rearrange(indices, "b n q -> b q n")
|
||||
|
||||
info["quantizer_indices"] = indices
|
||||
|
||||
if return_info:
|
||||
return x, info
|
||||
else:
|
||||
return x
|
||||
|
||||
def decode(self, x):
|
||||
|
||||
if self.noise_augment_dim > 0:
|
||||
noise = torch.randn(x.shape[0], self.noise_augment_dim,
|
||||
x.shape[-1]).type_as(x)
|
||||
x = torch.cat([x, noise], dim=1)
|
||||
|
||||
return x
|
||||
|
||||
def decode_tokens(self, tokens, **kwargs):
|
||||
latents = self.quantizer.indices_to_codes(tokens)
|
||||
|
||||
return self.decode(latents, **kwargs)
|
||||
@@ -0,0 +1,545 @@
|
||||
# Copied from https://github.com/facebookresearch/audiocraft/blob/main/audiocraft/modules/codebooks_patterns.py under MIT License
|
||||
# License available in LICENSES/LICENSE_META.txt
|
||||
|
||||
from collections import namedtuple
|
||||
from dataclasses import dataclass
|
||||
from functools import lru_cache
|
||||
import logging
|
||||
import typing as tp
|
||||
|
||||
from abc import ABC, abstractmethod
|
||||
import torch
|
||||
|
||||
LayoutCoord = namedtuple('LayoutCoord', ['t', 'q']) # (timestep, codebook index)
|
||||
PatternLayout = tp.List[tp.List[LayoutCoord]] # Sequence of coordinates
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@dataclass
|
||||
class Pattern:
|
||||
"""Base implementation of a pattern over a sequence with multiple codebooks.
|
||||
|
||||
The codebook pattern consists in a layout, defining for each sequence step
|
||||
the list of coordinates of each codebook timestep in the resulting interleaved sequence.
|
||||
The first item of the pattern is always an empty list in order to properly insert a special token
|
||||
to start with. For convenience, we also keep track of ``n_q`` the number of codebooks used for the pattern
|
||||
and ``timesteps`` the number of timesteps corresponding to the original sequence.
|
||||
|
||||
The pattern provides convenient methods to build and revert interleaved sequences from it:
|
||||
``build_pattern_sequence`` maps a given a dense input tensor of multi-codebook sequence from [B, K, T]
|
||||
to the interleaved sequence of shape [B, K, S] applying the pattern, with B being the batch size,
|
||||
K being the number of codebooks, T the number of original timesteps and S the number of sequence steps
|
||||
for the output sequence. The unfilled positions are replaced with a special token and the built sequence
|
||||
is returned along with a mask indicating valid tokens.
|
||||
``revert_pattern_sequence`` maps back an interleaved sequence of shape [B, K, S] to the original alignment
|
||||
of codebooks across timesteps to an output tensor of shape [B, K, T], using again a special token and a mask
|
||||
to fill and specify invalid positions if needed.
|
||||
See the dedicated methods for more details.
|
||||
"""
|
||||
# Pattern layout, for each sequence step, we have a list of coordinates
|
||||
# corresponding to the original codebook timestep and position.
|
||||
# The first list is always an empty list in order to properly insert
|
||||
# a special token to start with.
|
||||
layout: PatternLayout
|
||||
timesteps: int
|
||||
n_q: int
|
||||
|
||||
def __post_init__(self):
|
||||
assert len(self.layout) > 0
|
||||
self._validate_layout()
|
||||
self._build_reverted_sequence_scatter_indexes = lru_cache(100)(self._build_reverted_sequence_scatter_indexes)
|
||||
self._build_pattern_sequence_scatter_indexes = lru_cache(100)(self._build_pattern_sequence_scatter_indexes)
|
||||
logger.info("New pattern, time steps: %d, sequence steps: %d", self.timesteps, len(self.layout))
|
||||
|
||||
def _validate_layout(self):
|
||||
"""Runs checks on the layout to ensure a valid pattern is defined.
|
||||
A pattern is considered invalid if:
|
||||
- Multiple timesteps for a same codebook are defined in the same sequence step
|
||||
- The timesteps for a given codebook are not in ascending order as we advance in the sequence
|
||||
(this would mean that we have future timesteps before past timesteps).
|
||||
"""
|
||||
q_timesteps = {q: 0 for q in range(self.n_q)}
|
||||
for s, seq_coords in enumerate(self.layout):
|
||||
if len(seq_coords) > 0:
|
||||
qs = set()
|
||||
for coord in seq_coords:
|
||||
qs.add(coord.q)
|
||||
last_q_timestep = q_timesteps[coord.q]
|
||||
assert coord.t >= last_q_timestep, \
|
||||
f"Past timesteps are found in the sequence for codebook = {coord.q} at step {s}"
|
||||
q_timesteps[coord.q] = coord.t
|
||||
# each sequence step contains at max 1 coordinate per codebook
|
||||
assert len(qs) == len(seq_coords), \
|
||||
f"Multiple entries for a same codebook are found at step {s}"
|
||||
|
||||
@property
|
||||
def num_sequence_steps(self):
|
||||
return len(self.layout) - 1
|
||||
|
||||
@property
|
||||
def max_delay(self):
|
||||
max_t_in_seq_coords = 0
|
||||
for seq_coords in self.layout[1:]:
|
||||
for coords in seq_coords:
|
||||
max_t_in_seq_coords = max(max_t_in_seq_coords, coords.t + 1)
|
||||
return max_t_in_seq_coords - self.timesteps
|
||||
|
||||
@property
|
||||
def valid_layout(self):
|
||||
valid_step = len(self.layout) - self.max_delay
|
||||
return self.layout[:valid_step]
|
||||
|
||||
def starts_with_special_token(self):
|
||||
return self.layout[0] == []
|
||||
|
||||
def get_sequence_coords_with_timestep(self, t: int, q: tp.Optional[int] = None):
|
||||
"""Get codebook coordinates in the layout that corresponds to the specified timestep t
|
||||
and optionally to the codebook q. Coordinates are returned as a tuple with the sequence step
|
||||
and the actual codebook coordinates.
|
||||
"""
|
||||
assert t <= self.timesteps, "provided timesteps is greater than the pattern's number of timesteps"
|
||||
if q is not None:
|
||||
assert q <= self.n_q, "provided number of codebooks is greater than the pattern's number of codebooks"
|
||||
coords = []
|
||||
for s, seq_codes in enumerate(self.layout):
|
||||
for code in seq_codes:
|
||||
if code.t == t and (q is None or code.q == q):
|
||||
coords.append((s, code))
|
||||
return coords
|
||||
|
||||
def get_steps_with_timestep(self, t: int, q: tp.Optional[int] = None) -> tp.List[int]:
|
||||
return [step for step, coords in self.get_sequence_coords_with_timestep(t, q)]
|
||||
|
||||
def get_first_step_with_timesteps(self, t: int, q: tp.Optional[int] = None) -> tp.Optional[int]:
|
||||
steps_with_timesteps = self.get_steps_with_timestep(t, q)
|
||||
return steps_with_timesteps[0] if len(steps_with_timesteps) > 0 else None
|
||||
|
||||
def _build_pattern_sequence_scatter_indexes(self, timesteps: int, n_q: int, keep_only_valid_steps: bool,
|
||||
device: tp.Union[torch.device, str] = 'cpu'):
|
||||
"""Build scatter indexes corresponding to the pattern, up to the provided sequence_steps.
|
||||
|
||||
Args:
|
||||
timesteps (int): Maximum number of timesteps steps to consider.
|
||||
keep_only_valid_steps (bool): Restrict the pattern layout to match only valid steps.
|
||||
device (torch.device or str): Device for created tensors.
|
||||
Returns:
|
||||
indexes (torch.Tensor): Indexes corresponding to the sequence, of shape [K, S].
|
||||
mask (torch.Tensor): Mask corresponding to indexes that matches valid indexes, of shape [K, S].
|
||||
"""
|
||||
assert n_q == self.n_q, f"invalid number of codebooks for the sequence and the pattern: {n_q} != {self.n_q}"
|
||||
assert timesteps <= self.timesteps, "invalid number of timesteps used to build the sequence from the pattern"
|
||||
# use the proper layout based on whether we limit ourselves to valid steps only or not,
|
||||
# note that using the valid_layout will result in a truncated sequence up to the valid steps
|
||||
ref_layout = self.valid_layout if keep_only_valid_steps else self.layout
|
||||
# single item indexing being super slow with pytorch vs. numpy, so we use numpy here
|
||||
indexes = torch.zeros(n_q, len(ref_layout), dtype=torch.long).numpy()
|
||||
mask = torch.zeros(n_q, len(ref_layout), dtype=torch.bool).numpy()
|
||||
# fill indexes with last sequence step value that will correspond to our special token
|
||||
# the last value is n_q * timesteps as we have flattened z and append special token as the last token
|
||||
# which will correspond to the index: n_q * timesteps
|
||||
indexes[:] = n_q * timesteps
|
||||
# iterate over the pattern and fill scattered indexes and mask
|
||||
for s, sequence_coords in enumerate(ref_layout):
|
||||
for coords in sequence_coords:
|
||||
if coords.t < timesteps:
|
||||
indexes[coords.q, s] = coords.t + coords.q * timesteps
|
||||
mask[coords.q, s] = 1
|
||||
indexes = torch.from_numpy(indexes).to(device)
|
||||
mask = torch.from_numpy(mask).to(device)
|
||||
return indexes, mask
|
||||
|
||||
def build_pattern_sequence(self, z: torch.Tensor, special_token: int, keep_only_valid_steps: bool = False):
|
||||
"""Build sequence corresponding to the pattern from the input tensor z.
|
||||
The sequence is built using up to sequence_steps if specified, and non-pattern
|
||||
coordinates are filled with the special token.
|
||||
|
||||
Args:
|
||||
z (torch.Tensor): Input tensor of multi-codebooks sequence, of shape [B, K, T].
|
||||
special_token (int): Special token used to fill non-pattern coordinates in the new sequence.
|
||||
keep_only_valid_steps (bool): Build a sequence from the pattern up to valid (= fully defined) steps.
|
||||
Steps that are beyond valid steps will be replaced by the special_token in that case.
|
||||
Returns:
|
||||
values (torch.Tensor): Interleaved sequence matching the pattern, of shape [B, K, S] with S
|
||||
corresponding either to the sequence_steps if provided, otherwise to the length of the pattern.
|
||||
indexes (torch.Tensor): Indexes corresponding to the interleaved sequence, of shape [K, S].
|
||||
mask (torch.Tensor): Mask corresponding to indexes that matches valid indexes of shape [K, S].
|
||||
"""
|
||||
B, K, T = z.shape
|
||||
indexes, mask = self._build_pattern_sequence_scatter_indexes(
|
||||
T, K, keep_only_valid_steps=keep_only_valid_steps, device=str(z.device)
|
||||
)
|
||||
z = z.view(B, -1)
|
||||
# we append the special token as the last index of our flattened z tensor
|
||||
z = torch.cat([z, torch.zeros_like(z[:, :1]) + special_token], dim=1)
|
||||
values = z[:, indexes.view(-1)]
|
||||
values = values.view(B, K, indexes.shape[-1])
|
||||
return values, indexes, mask
|
||||
|
||||
def _build_reverted_sequence_scatter_indexes(self, sequence_steps: int, n_q: int,
|
||||
keep_only_valid_steps: bool = False,
|
||||
is_model_output: bool = False,
|
||||
device: tp.Union[torch.device, str] = 'cpu'):
|
||||
"""Builds scatter indexes required to retrieve the original multi-codebook sequence
|
||||
from interleaving pattern.
|
||||
|
||||
Args:
|
||||
sequence_steps (int): Sequence steps.
|
||||
n_q (int): Number of codebooks.
|
||||
keep_only_valid_steps (bool): Build a sequence from the pattern up to valid (= fully defined) steps.
|
||||
Steps that are beyond valid steps will be replaced by the special_token in that case.
|
||||
is_model_output (bool): Whether to keep the sequence item corresponding to initial special token or not.
|
||||
device (torch.device or str): Device for created tensors.
|
||||
Returns:
|
||||
indexes (torch.Tensor): Indexes for reconstructing the output, of shape [K, T].
|
||||
mask (torch.Tensor): Mask corresponding to indexes that matches valid indexes of shape [K, T].
|
||||
"""
|
||||
ref_layout = self.valid_layout if keep_only_valid_steps else self.layout
|
||||
# TODO(jade): Do we want to further truncate to only valid timesteps here as well?
|
||||
timesteps = self.timesteps
|
||||
assert n_q == self.n_q, f"invalid number of codebooks for the sequence and the pattern: {n_q} != {self.n_q}"
|
||||
assert sequence_steps <= len(ref_layout), \
|
||||
f"sequence to revert is longer than the defined pattern: {sequence_steps} > {len(ref_layout)}"
|
||||
|
||||
# ensure we take the appropriate indexes to keep the model output from the first special token as well
|
||||
if is_model_output and self.starts_with_special_token():
|
||||
ref_layout = ref_layout[1:]
|
||||
|
||||
# single item indexing being super slow with pytorch vs. numpy, so we use numpy here
|
||||
indexes = torch.zeros(n_q, timesteps, dtype=torch.long).numpy()
|
||||
mask = torch.zeros(n_q, timesteps, dtype=torch.bool).numpy()
|
||||
# fill indexes with last sequence step value that will correspond to our special token
|
||||
indexes[:] = n_q * sequence_steps
|
||||
for s, sequence_codes in enumerate(ref_layout):
|
||||
if s < sequence_steps:
|
||||
for code in sequence_codes:
|
||||
if code.t < timesteps:
|
||||
indexes[code.q, code.t] = s + code.q * sequence_steps
|
||||
mask[code.q, code.t] = 1
|
||||
indexes = torch.from_numpy(indexes).to(device)
|
||||
mask = torch.from_numpy(mask).to(device)
|
||||
return indexes, mask
|
||||
|
||||
def revert_pattern_sequence(self, s: torch.Tensor, special_token: int, keep_only_valid_steps: bool = False):
|
||||
"""Revert a sequence built from the pattern back to the original multi-codebook sequence without interleaving.
|
||||
The sequence is reverted using up to timesteps if specified, and non-pattern coordinates
|
||||
are filled with the special token.
|
||||
|
||||
Args:
|
||||
s (torch.Tensor): Interleaved sequence tensor obtained from the pattern, of shape [B, K, S].
|
||||
special_token (int or float): Special token used to fill non-pattern coordinates in the new sequence.
|
||||
Returns:
|
||||
values (torch.Tensor): Interleaved sequence matching the pattern, of shape [B, K, T] with T
|
||||
corresponding either to the timesteps if provided, or the total timesteps in pattern otherwise.
|
||||
indexes (torch.Tensor): Indexes corresponding to the interleaved sequence, of shape [K, T].
|
||||
mask (torch.Tensor): Mask corresponding to indexes that matches valid indexes of shape [K, T].
|
||||
"""
|
||||
B, K, S = s.shape
|
||||
indexes, mask = self._build_reverted_sequence_scatter_indexes(
|
||||
S, K, keep_only_valid_steps, is_model_output=False, device=str(s.device)
|
||||
)
|
||||
s = s.view(B, -1)
|
||||
# we append the special token as the last index of our flattened z tensor
|
||||
s = torch.cat([s, torch.zeros_like(s[:, :1]) + special_token], dim=1)
|
||||
values = s[:, indexes.view(-1)]
|
||||
values = values.view(B, K, indexes.shape[-1])
|
||||
return values, indexes, mask
|
||||
|
||||
def revert_pattern_logits(self, logits: torch.Tensor, special_token: float, keep_only_valid_steps: bool = False):
|
||||
"""Revert model logits obtained on a sequence built from the pattern
|
||||
back to a tensor matching the original sequence.
|
||||
|
||||
This method is similar to ``revert_pattern_sequence`` with the following specificities:
|
||||
1. It is designed to work with the extra cardinality dimension
|
||||
2. We return the logits for the first sequence item that matches the special_token and
|
||||
which matching target in the original sequence is the first item of the sequence,
|
||||
while we skip the last logits as there is no matching target
|
||||
"""
|
||||
B, card, K, S = logits.shape
|
||||
indexes, mask = self._build_reverted_sequence_scatter_indexes(
|
||||
S, K, keep_only_valid_steps, is_model_output=True, device=logits.device
|
||||
)
|
||||
logits = logits.reshape(B, card, -1)
|
||||
# we append the special token as the last index of our flattened z tensor
|
||||
logits = torch.cat([logits, torch.zeros_like(logits[:, :, :1]) + special_token], dim=-1) # [B, card, K x S]
|
||||
values = logits[:, :, indexes.view(-1)]
|
||||
values = values.view(B, card, K, indexes.shape[-1])
|
||||
return values, indexes, mask
|
||||
|
||||
|
||||
class CodebooksPatternProvider(ABC):
|
||||
"""Abstraction around providing pattern for interleaving codebooks.
|
||||
|
||||
The CodebooksPatternProvider abstraction allows to implement various strategies to
|
||||
define interleaving pattern of sequences composed of multiple codebooks. For a given
|
||||
number of codebooks `n_q`, the pattern provider can generate a specified pattern
|
||||
corresponding to a sequence of `T` timesteps with `n_q` parallel codebooks. This pattern
|
||||
can be used to construct a new sequence from the original codes respecting the specified
|
||||
pattern. The pattern is defined as a list of list of code coordinates, code coordinate
|
||||
being a tuple with the original timestep and codebook to build the new sequence.
|
||||
Note that all patterns must start with an empty list that is then used to insert a first
|
||||
sequence step of special tokens in the newly generated sequence.
|
||||
|
||||
Args:
|
||||
n_q (int): number of codebooks.
|
||||
cached (bool): if True, patterns for a given length are cached. In general
|
||||
that should be true for efficiency reason to avoid synchronization points.
|
||||
"""
|
||||
def __init__(self, n_q: int, cached: bool = True):
|
||||
assert n_q > 0
|
||||
self.n_q = n_q
|
||||
self.get_pattern = lru_cache(100)(self.get_pattern) # type: ignore
|
||||
|
||||
@abstractmethod
|
||||
def get_pattern(self, timesteps: int) -> Pattern:
|
||||
"""Builds pattern with specific interleaving between codebooks.
|
||||
|
||||
Args:
|
||||
timesteps (int): Total number of timesteps.
|
||||
"""
|
||||
raise NotImplementedError()
|
||||
|
||||
|
||||
class DelayedPatternProvider(CodebooksPatternProvider):
|
||||
"""Provider for delayed pattern across delayed codebooks.
|
||||
Codebooks are delayed in the sequence and sequence steps will contain codebooks
|
||||
from different timesteps.
|
||||
|
||||
Example:
|
||||
Taking timesteps=4 and n_q=3, delays=None, the multi-codebook sequence:
|
||||
[[1, 2, 3, 4],
|
||||
[1, 2, 3, 4],
|
||||
[1, 2, 3, 4]]
|
||||
The resulting sequence obtained from the returned pattern is:
|
||||
[[S, 1, 2, 3, 4],
|
||||
[S, S, 1, 2, 3],
|
||||
[S, S, S, 1, 2]]
|
||||
(with S being a special token)
|
||||
|
||||
Args:
|
||||
n_q (int): Number of codebooks.
|
||||
delays (list of int, optional): Delay for each of the codebooks.
|
||||
If delays not defined, each codebook is delayed by 1 compared to the previous one.
|
||||
flatten_first (int): Flatten the first N timesteps.
|
||||
empty_initial (int): Prepend with N empty list of coordinates.
|
||||
"""
|
||||
def __init__(self, n_q: int, delays: tp.Optional[tp.List[int]] = None,
|
||||
flatten_first: int = 0, empty_initial: int = 0):
|
||||
super().__init__(n_q)
|
||||
if delays is None:
|
||||
delays = list(range(n_q))
|
||||
self.delays = delays
|
||||
self.flatten_first = flatten_first
|
||||
self.empty_initial = empty_initial
|
||||
assert len(self.delays) == self.n_q
|
||||
assert sorted(self.delays) == self.delays
|
||||
|
||||
def get_pattern(self, timesteps: int) -> Pattern:
|
||||
omit_special_token = self.empty_initial < 0
|
||||
out: PatternLayout = [] if omit_special_token else [[]]
|
||||
max_delay = max(self.delays)
|
||||
if self.empty_initial:
|
||||
out += [[] for _ in range(self.empty_initial)]
|
||||
if self.flatten_first:
|
||||
for t in range(min(timesteps, self.flatten_first)):
|
||||
for q in range(self.n_q):
|
||||
out.append([LayoutCoord(t, q)])
|
||||
for t in range(self.flatten_first, timesteps + max_delay):
|
||||
v = []
|
||||
for q, delay in enumerate(self.delays):
|
||||
t_for_q = t - delay
|
||||
if t_for_q >= self.flatten_first:
|
||||
v.append(LayoutCoord(t_for_q, q))
|
||||
out.append(v)
|
||||
return Pattern(out, n_q=self.n_q, timesteps=timesteps)
|
||||
|
||||
|
||||
class ParallelPatternProvider(DelayedPatternProvider):
|
||||
"""Provider for parallel pattern across codebooks.
|
||||
This pattern provider is a special case of the delayed pattern with actually no delay,
|
||||
hence delays=repeat(0, n_q).
|
||||
|
||||
Args:
|
||||
n_q (int): Number of codebooks.
|
||||
empty_initial (int): Prepend with N empty list of coordinates.
|
||||
"""
|
||||
def __init__(self, n_q: int, empty_initial: int = 0):
|
||||
super().__init__(n_q, [0] * n_q, empty_initial=empty_initial)
|
||||
|
||||
|
||||
class UnrolledPatternProvider(CodebooksPatternProvider):
|
||||
"""Provider for unrolling codebooks pattern.
|
||||
This pattern provider enables to represent the codebook flattened completely or only to some extend
|
||||
while also specifying a given delay between the flattened codebooks representation, allowing to
|
||||
unroll the codebooks in the sequence.
|
||||
|
||||
Example:
|
||||
1. Flattening of the codebooks.
|
||||
By default, the pattern provider will fully flatten the codebooks such as flattening=range(n_q),
|
||||
taking n_q = 3 and timesteps = 4:
|
||||
[[1, 2, 3, 4],
|
||||
[1, 2, 3, 4],
|
||||
[1, 2, 3, 4]]
|
||||
will result into:
|
||||
[[S, S, 1, S, S, 2, S, S, 3, S, S, 4],
|
||||
[S, 1, S, S, 2, S, S, 3, S, S, 4, S],
|
||||
[1, S, S, 2, S, S, 3, S, S, 4, S, S]]
|
||||
2. Partial flattening of the codebooks. The ``flattening`` parameter allows to specify the inner step
|
||||
for each of the codebook, allowing to define which codebook to flatten (or keep in parallel), for example
|
||||
taking n_q = 3, timesteps = 4 and flattening = [0, 1, 1]:
|
||||
[[1, 2, 3, 4],
|
||||
[1, 2, 3, 4],
|
||||
[1, 2, 3, 4]]
|
||||
will result into:
|
||||
[[S, 1, S, S, 2, S, S, 3, S, S, 4, S],
|
||||
[S, 1, S, S, 2, S, S, 3, S, S, 4, S],
|
||||
[1, S, S, 2, S, S, 3, S, S, 4, S, S]]
|
||||
3. Flattening with delay. The ``delay`` parameter allows to further unroll the sequence of codebooks
|
||||
allowing to specify the delay per codebook. Note that the delay between codebooks flattened to the
|
||||
same inner timestep should be coherent. For example, taking n_q = 3, timesteps = 4, flattening = [0, 1, 1]
|
||||
and delays = [0, 3, 3]:
|
||||
[[1, 2, 3, 4],
|
||||
[1, 2, 3, 4],
|
||||
[1, 2, 3, 4]]
|
||||
will result into:
|
||||
[[S, S, S, 1, S, 2, S, 3, S, 4],
|
||||
[S, S, S, 1, S, 2, S, 3, S, 4],
|
||||
[1, 2, 3, S, 4, S, 5, S, 6, S]]
|
||||
|
||||
Args:
|
||||
n_q (int): Number of codebooks.
|
||||
flattening (list of int, optional): Flattening schema over the codebooks. If not defined,
|
||||
the codebooks will be flattened to 1 codebook per step, meaning that the sequence will
|
||||
have n_q extra steps for each timestep.
|
||||
delays (list of int, optional): Delay for each of the codebooks. If not defined,
|
||||
no delay is added and therefore will default to [0] * ``n_q``.
|
||||
Note that two codebooks that will be flattened to the same inner step
|
||||
should have the same delay, otherwise the pattern is considered as invalid.
|
||||
"""
|
||||
FlattenedCodebook = namedtuple('FlattenedCodebook', ['codebooks', 'delay'])
|
||||
|
||||
def __init__(self, n_q: int, flattening: tp.Optional[tp.List[int]] = None,
|
||||
delays: tp.Optional[tp.List[int]] = None):
|
||||
super().__init__(n_q)
|
||||
if flattening is None:
|
||||
flattening = list(range(n_q))
|
||||
if delays is None:
|
||||
delays = [0] * n_q
|
||||
assert len(flattening) == n_q
|
||||
assert len(delays) == n_q
|
||||
assert sorted(flattening) == flattening
|
||||
assert sorted(delays) == delays
|
||||
self._flattened_codebooks = self._build_flattened_codebooks(delays, flattening)
|
||||
self.max_delay = max(delays)
|
||||
|
||||
def _build_flattened_codebooks(self, delays: tp.List[int], flattening: tp.List[int]):
|
||||
"""Build a flattened codebooks representation as a dictionary of inner step
|
||||
and the actual codebook indices corresponding to the flattened codebook. For convenience, we
|
||||
also store the delay associated to the flattened codebook to avoid maintaining an extra mapping.
|
||||
"""
|
||||
flattened_codebooks: dict = {}
|
||||
for q, (inner_step, delay) in enumerate(zip(flattening, delays)):
|
||||
if inner_step not in flattened_codebooks:
|
||||
flat_codebook = UnrolledPatternProvider.FlattenedCodebook(codebooks=[q], delay=delay)
|
||||
else:
|
||||
flat_codebook = flattened_codebooks[inner_step]
|
||||
assert flat_codebook.delay == delay, (
|
||||
"Delay and flattening between codebooks is inconsistent: ",
|
||||
"two codebooks flattened to the same position should have the same delay."
|
||||
)
|
||||
flat_codebook.codebooks.append(q)
|
||||
flattened_codebooks[inner_step] = flat_codebook
|
||||
return flattened_codebooks
|
||||
|
||||
@property
|
||||
def _num_inner_steps(self):
|
||||
"""Number of inner steps to unroll between timesteps in order to flatten the codebooks.
|
||||
"""
|
||||
return max([inner_step for inner_step in self._flattened_codebooks.keys()]) + 1
|
||||
|
||||
def num_virtual_steps(self, timesteps: int) -> int:
|
||||
return timesteps * self._num_inner_steps + 1
|
||||
|
||||
def get_pattern(self, timesteps: int) -> Pattern:
|
||||
"""Builds pattern for delay across codebooks.
|
||||
|
||||
Args:
|
||||
timesteps (int): Total number of timesteps.
|
||||
"""
|
||||
# the PatternLayout is built as a tuple of sequence position and list of coordinates
|
||||
# so that it can be reordered properly given the required delay between codebooks of given timesteps
|
||||
indexed_out: list = [(-1, [])]
|
||||
max_timesteps = timesteps + self.max_delay
|
||||
for t in range(max_timesteps):
|
||||
# for each timestep, we unroll the flattened codebooks,
|
||||
# emitting the sequence step with the corresponding delay
|
||||
for step in range(self._num_inner_steps):
|
||||
if step in self._flattened_codebooks:
|
||||
# we have codebooks at this virtual step to emit
|
||||
step_codebooks = self._flattened_codebooks[step]
|
||||
t_for_q = t + step_codebooks.delay
|
||||
coords = [LayoutCoord(t, q) for q in step_codebooks.codebooks]
|
||||
if t_for_q < max_timesteps and t < max_timesteps:
|
||||
indexed_out.append((t_for_q, coords))
|
||||
else:
|
||||
# there is no codebook in this virtual step so we emit an empty list
|
||||
indexed_out.append((t, []))
|
||||
out = [coords for _, coords in sorted(indexed_out)]
|
||||
return Pattern(out, n_q=self.n_q, timesteps=timesteps)
|
||||
|
||||
|
||||
class CoarseFirstPattern(CodebooksPatternProvider):
|
||||
"""First generates all the codebooks #1 (e.g. coarser), then the remaining ones,
|
||||
potentially with delays.
|
||||
|
||||
..Warning:: You must always generate the full training duration at test time, for instance,
|
||||
30 seconds, as otherwise, the fine codebooks will start being generated in an unexpected
|
||||
location. This is due to the non causality of the remaining codebooks with respect to
|
||||
the first ones.
|
||||
|
||||
Args:
|
||||
n_q (int): Number of codebooks.
|
||||
delays (list of int, optional): Delay for each of the codebooks.
|
||||
If delays not defined, each codebook is delayed by 1 compared to the previous one.
|
||||
"""
|
||||
def __init__(self, n_q: int, delays: tp.Optional[tp.List[int]] = None):
|
||||
super().__init__(n_q)
|
||||
if delays is None:
|
||||
delays = [0] * (n_q - 1)
|
||||
self.delays = delays
|
||||
assert len(self.delays) == self.n_q - 1
|
||||
assert sorted(self.delays) == self.delays
|
||||
|
||||
def get_pattern(self, timesteps: int) -> Pattern:
|
||||
out: PatternLayout = [[]]
|
||||
for t in range(timesteps):
|
||||
out.append([LayoutCoord(t, 0)])
|
||||
max_delay = max(self.delays)
|
||||
for t in range(timesteps + max_delay):
|
||||
v = []
|
||||
for q, delay in enumerate(self.delays):
|
||||
t_for_q = t - delay
|
||||
if t_for_q >= 0:
|
||||
v.append(LayoutCoord(t_for_q, q + 1))
|
||||
out.append(v)
|
||||
return Pattern(out, n_q=self.n_q, timesteps=timesteps)
|
||||
|
||||
|
||||
class MusicLMPattern(CodebooksPatternProvider):
|
||||
"""Almost MusicLM style pattern. This is equivalent to full flattening
|
||||
but in a different order.
|
||||
|
||||
Args:
|
||||
n_q (int): Number of codebooks.
|
||||
group_by (int): Number of codebooks to group together.
|
||||
"""
|
||||
def __init__(self, n_q: int, group_by: int = 2):
|
||||
super().__init__(n_q)
|
||||
self.group_by = group_by
|
||||
|
||||
def get_pattern(self, timesteps: int) -> Pattern:
|
||||
out: PatternLayout = [[]]
|
||||
for offset in range(0, self.n_q, self.group_by):
|
||||
for t in range(timesteps):
|
||||
for q in range(offset, offset + self.group_by):
|
||||
out.append([LayoutCoord(t, q)])
|
||||
return Pattern(out, n_q=self.n_q, timesteps=timesteps)
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,920 @@
|
||||
import torch
|
||||
from torch import nn
|
||||
from torch.nn import functional as F
|
||||
from functools import partial
|
||||
import numpy as np
|
||||
import typing as tp
|
||||
|
||||
from .blocks import ResConvBlock, FourierFeatures, Upsample1d, Upsample1d_2, Downsample1d, Downsample1d_2, SelfAttention1d, SkipBlock, expand_to_planes
|
||||
from .conditioners import MultiConditioner, create_multi_conditioner_from_conditioning_config
|
||||
# from .dit import DiffusionTransformer
|
||||
from .mmdit import MMmodule
|
||||
from .factory import create_pretransform_from_config
|
||||
from .pretransforms import Pretransform
|
||||
from ..inference.generation import generate_diffusion_cond
|
||||
|
||||
from time import time
|
||||
|
||||
class Profiler:
|
||||
|
||||
def __init__(self):
|
||||
self.ticks = [[time(), None]]
|
||||
|
||||
def tick(self, msg):
|
||||
self.ticks.append([time(), msg])
|
||||
|
||||
def __repr__(self):
|
||||
rep = 80 * "=" + "\n"
|
||||
for i in range(1, len(self.ticks)):
|
||||
msg = self.ticks[i][1]
|
||||
ellapsed = self.ticks[i][0] - self.ticks[i - 1][0]
|
||||
rep += msg + f": {ellapsed*1000:.2f}ms\n"
|
||||
rep += 80 * "=" + "\n\n\n"
|
||||
return rep
|
||||
|
||||
class DiffusionModel(nn.Module):
|
||||
def __init__(self, *args, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
def forward(self, x, t, **kwargs):
|
||||
raise NotImplementedError()
|
||||
|
||||
class DiffusionModelWrapper(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
model: DiffusionModel,
|
||||
io_channels,
|
||||
sample_size,
|
||||
sample_rate,
|
||||
min_input_length,
|
||||
pretransform: tp.Optional[Pretransform] = None,
|
||||
):
|
||||
super().__init__()
|
||||
self.io_channels = io_channels
|
||||
self.sample_size = sample_size
|
||||
self.sample_rate = sample_rate
|
||||
self.min_input_length = min_input_length
|
||||
|
||||
self.model = model
|
||||
|
||||
if pretransform is not None:
|
||||
self.pretransform = pretransform
|
||||
else:
|
||||
self.pretransform = None
|
||||
|
||||
def forward(self, x, t, **kwargs):
|
||||
return self.model(x, t, **kwargs)
|
||||
|
||||
class ConditionedDiffusionModel(nn.Module):
|
||||
def __init__(self,
|
||||
*args,
|
||||
supports_cross_attention: bool = False,
|
||||
supports_input_concat: bool = False,
|
||||
supports_global_cond: bool = False,
|
||||
supports_prepend_cond: bool = False,
|
||||
**kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self.supports_cross_attention = supports_cross_attention
|
||||
self.supports_input_concat = supports_input_concat
|
||||
self.supports_global_cond = supports_global_cond
|
||||
self.supports_prepend_cond = supports_prepend_cond
|
||||
|
||||
def forward(self,
|
||||
x: torch.Tensor,
|
||||
t: torch.Tensor,
|
||||
cross_attn_cond: torch.Tensor = None,
|
||||
cross_attn_mask: torch.Tensor = None,
|
||||
input_concat_cond: torch.Tensor = None,
|
||||
global_embed: torch.Tensor = None,
|
||||
prepend_cond: torch.Tensor = None,
|
||||
prepend_cond_mask: torch.Tensor = None,
|
||||
cfg_scale: float = 1.0,
|
||||
cfg_dropout_prob: float = 0.0,
|
||||
batch_cfg: bool = False,
|
||||
rescale_cfg: bool = False,
|
||||
**kwargs):
|
||||
raise NotImplementedError()
|
||||
|
||||
class ConditionedDiffusionModelWrapper(nn.Module):
|
||||
"""
|
||||
A diffusion model that takes in conditioning
|
||||
"""
|
||||
def __init__(
|
||||
self,
|
||||
model: ConditionedDiffusionModel,
|
||||
conditioner: MultiConditioner,
|
||||
io_channels,
|
||||
sample_rate,
|
||||
min_input_length: int,
|
||||
diffusion_objective: tp.Literal["v", "rectified_flow"] = "v",
|
||||
pretransform: tp.Optional[Pretransform] = None,
|
||||
cross_attn_cond_ids: tp.List[str] = [],
|
||||
global_cond_ids: tp.List[str] = [],
|
||||
input_concat_ids: tp.List[str] = [],
|
||||
prepend_cond_ids: tp.List[str] = [],
|
||||
add_cond_ids: tp.List[str] = [],
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.model = model
|
||||
self.conditioner = conditioner
|
||||
self.io_channels = io_channels
|
||||
self.sample_rate = sample_rate
|
||||
self.diffusion_objective = diffusion_objective
|
||||
self.pretransform = pretransform
|
||||
self.cross_attn_cond_ids = cross_attn_cond_ids
|
||||
self.global_cond_ids = global_cond_ids
|
||||
self.input_concat_ids = input_concat_ids
|
||||
self.prepend_cond_ids = prepend_cond_ids
|
||||
self.add_cond_ids = add_cond_ids
|
||||
self.min_input_length = min_input_length
|
||||
|
||||
def get_conditioning_inputs(self, conditioning_tensors: tp.Dict[str, tp.Any], negative=False):
|
||||
cross_attention_input = None
|
||||
cross_attention_masks = None
|
||||
global_cond = None
|
||||
input_concat_cond = None
|
||||
prepend_cond = None
|
||||
prepend_cond_mask = None
|
||||
add_input = None
|
||||
|
||||
if len(self.cross_attn_cond_ids) > 0:
|
||||
# Concatenate all cross-attention inputs over the sequence dimension
|
||||
# Assumes that the cross-attention inputs are of shape (batch, seq, channels)
|
||||
cross_attention_input = []
|
||||
cross_attention_masks = []
|
||||
|
||||
for key in self.cross_attn_cond_ids:
|
||||
cross_attn_in, cross_attn_mask = conditioning_tensors[key]
|
||||
|
||||
# Add sequence dimension if it's not there
|
||||
if len(cross_attn_in.shape) == 2:
|
||||
cross_attn_in = cross_attn_in.unsqueeze(1)
|
||||
# cross_attn_mask = cross_attn_mask.unsqueeze(1)
|
||||
|
||||
cross_attention_input.append(cross_attn_in)
|
||||
cross_attention_masks.append(cross_attn_mask)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
cross_attention_input = torch.cat(cross_attention_input, dim=1)
|
||||
cross_attention_masks = torch.cat(cross_attention_masks, dim=1)
|
||||
|
||||
if len(self.add_cond_ids) > 0:
|
||||
# Concatenate all cross-attention inputs over the sequence dimension
|
||||
# Assumes that the cross-attention inputs are of shape (batch, seq, channels)
|
||||
add_input = []
|
||||
|
||||
for key in self.add_cond_ids:
|
||||
add_in, _ = conditioning_tensors[key]
|
||||
|
||||
# Add sequence dimension if it's not there
|
||||
if len(add_in.shape) == 2:
|
||||
add_in = add_in.unsqueeze(1)
|
||||
|
||||
add_input.append(add_in)
|
||||
|
||||
add_input = torch.cat(add_input, dim=1)
|
||||
|
||||
if len(self.global_cond_ids) > 0:
|
||||
# Concatenate all global conditioning inputs over the channel dimension
|
||||
# Assumes that the global conditioning inputs are of shape (batch, channels)
|
||||
global_conds = []
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
for key in self.global_cond_ids:
|
||||
global_cond_input = conditioning_tensors[key][0]
|
||||
|
||||
global_conds.append(global_cond_input)
|
||||
|
||||
# Concatenate over the channel dimension
|
||||
if global_conds[0].shape[-1] == 768:
|
||||
global_cond = torch.cat(global_conds, dim=-1)
|
||||
else:
|
||||
global_cond = sum(global_conds)
|
||||
|
||||
# global_cond = torch.cat(global_conds, dim=-1)
|
||||
|
||||
if len(global_cond.shape) == 3:
|
||||
global_cond = global_cond.squeeze(1)
|
||||
|
||||
if len(self.input_concat_ids) > 0:
|
||||
# Concatenate all input concat conditioning inputs over the channel dimension
|
||||
# Assumes that the input concat conditioning inputs are of shape (batch, channels, seq)
|
||||
input_concat_cond = torch.cat([conditioning_tensors[key][0] for key in self.input_concat_ids], dim=1)
|
||||
|
||||
if len(self.prepend_cond_ids) > 0:
|
||||
# Concatenate all prepend conditioning inputs over the sequence dimension
|
||||
# Assumes that the prepend conditioning inputs are of shape (batch, seq, channels)
|
||||
prepend_conds = []
|
||||
prepend_cond_masks = []
|
||||
|
||||
for key in self.prepend_cond_ids:
|
||||
prepend_cond_input, prepend_cond_mask = conditioning_tensors[key]
|
||||
prepend_conds.append(prepend_cond_input)
|
||||
prepend_cond_masks.append(prepend_cond_mask)
|
||||
|
||||
prepend_cond = torch.cat(prepend_conds, dim=1)
|
||||
prepend_cond_mask = torch.cat(prepend_cond_masks, dim=1)
|
||||
|
||||
if negative:
|
||||
return {
|
||||
"negative_cross_attn_cond": cross_attention_input,
|
||||
"negative_cross_attn_mask": cross_attention_masks,
|
||||
"negative_global_cond": global_cond,
|
||||
"negative_input_concat_cond": input_concat_cond
|
||||
}
|
||||
else:
|
||||
return {
|
||||
"cross_attn_cond": cross_attention_input,
|
||||
"cross_attn_mask": cross_attention_masks,
|
||||
"global_cond": global_cond,
|
||||
"input_concat_cond": input_concat_cond,
|
||||
"prepend_cond": prepend_cond,
|
||||
"prepend_cond_mask": prepend_cond_mask,
|
||||
"add_cond": add_input
|
||||
}
|
||||
|
||||
def forward(self, x: torch.Tensor, t: torch.Tensor, cond: tp.Dict[str, tp.Any], **kwargs):
|
||||
return self.model(x, t, **self.get_conditioning_inputs(cond), **kwargs)
|
||||
|
||||
def generate(self, *args, **kwargs):
|
||||
return generate_diffusion_cond(self, *args, **kwargs)
|
||||
|
||||
class UNetCFG1DWrapper(ConditionedDiffusionModel):
|
||||
def __init__(
|
||||
self,
|
||||
*args,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__(supports_cross_attention=True, supports_global_cond=True, supports_input_concat=True)
|
||||
|
||||
self.model = UNetCFG1d(*args, **kwargs)
|
||||
|
||||
with torch.no_grad():
|
||||
for param in self.model.parameters():
|
||||
param *= 0.5
|
||||
|
||||
def forward(self,
|
||||
x,
|
||||
t,
|
||||
cross_attn_cond=None,
|
||||
cross_attn_mask=None,
|
||||
input_concat_cond=None,
|
||||
global_cond=None,
|
||||
cfg_scale=1.0,
|
||||
cfg_dropout_prob: float = 0.0,
|
||||
batch_cfg: bool = False,
|
||||
rescale_cfg: bool = False,
|
||||
negative_cross_attn_cond=None,
|
||||
negative_cross_attn_mask=None,
|
||||
negative_global_cond=None,
|
||||
negative_input_concat_cond=None,
|
||||
prepend_cond=None,
|
||||
prepend_cond_mask=None,
|
||||
**kwargs):
|
||||
p = Profiler()
|
||||
|
||||
p.tick("start")
|
||||
|
||||
channels_list = None
|
||||
if input_concat_cond is not None:
|
||||
channels_list = [input_concat_cond]
|
||||
|
||||
outputs = self.model(
|
||||
x,
|
||||
t,
|
||||
embedding=cross_attn_cond,
|
||||
embedding_mask=cross_attn_mask,
|
||||
features=global_cond,
|
||||
channels_list=channels_list,
|
||||
embedding_scale=cfg_scale,
|
||||
embedding_mask_proba=cfg_dropout_prob,
|
||||
batch_cfg=batch_cfg,
|
||||
rescale_cfg=rescale_cfg,
|
||||
negative_embedding=negative_cross_attn_cond,
|
||||
negative_embedding_mask=negative_cross_attn_mask,
|
||||
**kwargs)
|
||||
|
||||
p.tick("UNetCFG1D forward")
|
||||
|
||||
#print(f"Profiler: {p}")
|
||||
return outputs
|
||||
|
||||
class UNet1DCondWrapper(ConditionedDiffusionModel):
|
||||
def __init__(
|
||||
self,
|
||||
*args,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__(supports_cross_attention=False, supports_global_cond=True, supports_input_concat=True)
|
||||
|
||||
self.model = UNet1d(*args, **kwargs)
|
||||
|
||||
with torch.no_grad():
|
||||
for param in self.model.parameters():
|
||||
param *= 0.5
|
||||
|
||||
def forward(self,
|
||||
x,
|
||||
t,
|
||||
input_concat_cond=None,
|
||||
global_cond=None,
|
||||
cross_attn_cond=None,
|
||||
cross_attn_mask=None,
|
||||
prepend_cond=None,
|
||||
prepend_cond_mask=None,
|
||||
cfg_scale=1.0,
|
||||
cfg_dropout_prob: float = 0.0,
|
||||
batch_cfg: bool = False,
|
||||
rescale_cfg: bool = False,
|
||||
negative_cross_attn_cond=None,
|
||||
negative_cross_attn_mask=None,
|
||||
negative_global_cond=None,
|
||||
negative_input_concat_cond=None,
|
||||
**kwargs):
|
||||
|
||||
channels_list = None
|
||||
if input_concat_cond is not None:
|
||||
|
||||
# Interpolate input_concat_cond to the same length as x
|
||||
if input_concat_cond.shape[2] != x.shape[2]:
|
||||
input_concat_cond = F.interpolate(input_concat_cond, (x.shape[2], ), mode='nearest')
|
||||
|
||||
channels_list = [input_concat_cond]
|
||||
|
||||
outputs = self.model(
|
||||
x,
|
||||
t,
|
||||
features=global_cond,
|
||||
channels_list=channels_list,
|
||||
**kwargs)
|
||||
|
||||
return outputs
|
||||
|
||||
class UNet1DUncondWrapper(DiffusionModel):
|
||||
def __init__(
|
||||
self,
|
||||
in_channels,
|
||||
*args,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.model = UNet1d(in_channels=in_channels, *args, **kwargs)
|
||||
|
||||
self.io_channels = in_channels
|
||||
|
||||
with torch.no_grad():
|
||||
for param in self.model.parameters():
|
||||
param *= 0.5
|
||||
|
||||
def forward(self, x, t, **kwargs):
|
||||
return self.model(x, t, **kwargs)
|
||||
|
||||
class DAU1DCondWrapper(ConditionedDiffusionModel):
|
||||
def __init__(
|
||||
self,
|
||||
*args,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__(supports_cross_attention=False, supports_global_cond=False, supports_input_concat=True)
|
||||
|
||||
self.model = DiffusionAttnUnet1D(*args, **kwargs)
|
||||
|
||||
with torch.no_grad():
|
||||
for param in self.model.parameters():
|
||||
param *= 0.5
|
||||
|
||||
def forward(self,
|
||||
x,
|
||||
t,
|
||||
input_concat_cond=None,
|
||||
cross_attn_cond=None,
|
||||
cross_attn_mask=None,
|
||||
global_cond=None,
|
||||
cfg_scale=1.0,
|
||||
cfg_dropout_prob: float = 0.0,
|
||||
batch_cfg: bool = False,
|
||||
rescale_cfg: bool = False,
|
||||
negative_cross_attn_cond=None,
|
||||
negative_cross_attn_mask=None,
|
||||
negative_global_cond=None,
|
||||
negative_input_concat_cond=None,
|
||||
prepend_cond=None,
|
||||
**kwargs):
|
||||
|
||||
return self.model(x, t, cond = input_concat_cond)
|
||||
|
||||
class DiffusionAttnUnet1D(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
io_channels = 2,
|
||||
depth=14,
|
||||
n_attn_layers = 6,
|
||||
channels = [128, 128, 256, 256] + [512] * 10,
|
||||
cond_dim = 0,
|
||||
cond_noise_aug = False,
|
||||
kernel_size = 5,
|
||||
learned_resample = False,
|
||||
strides = [2] * 13,
|
||||
conv_bias = True,
|
||||
use_snake = False
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.cond_noise_aug = cond_noise_aug
|
||||
|
||||
self.io_channels = io_channels
|
||||
|
||||
if self.cond_noise_aug:
|
||||
self.rng = torch.quasirandom.SobolEngine(1, scramble=True)
|
||||
|
||||
self.timestep_embed = FourierFeatures(1, 16)
|
||||
|
||||
attn_layer = depth - n_attn_layers
|
||||
|
||||
strides = [1] + strides
|
||||
|
||||
block = nn.Identity()
|
||||
|
||||
conv_block = partial(ResConvBlock, kernel_size=kernel_size, conv_bias = conv_bias, use_snake=use_snake)
|
||||
|
||||
for i in range(depth, 0, -1):
|
||||
c = channels[i - 1]
|
||||
stride = strides[i-1]
|
||||
if stride > 2 and not learned_resample:
|
||||
raise ValueError("Must have stride 2 without learned resampling")
|
||||
|
||||
if i > 1:
|
||||
c_prev = channels[i - 2]
|
||||
add_attn = i >= attn_layer and n_attn_layers > 0
|
||||
block = SkipBlock(
|
||||
Downsample1d_2(c_prev, c_prev, stride) if (learned_resample or stride == 1) else Downsample1d("cubic"),
|
||||
conv_block(c_prev, c, c),
|
||||
SelfAttention1d(
|
||||
c, c // 32) if add_attn else nn.Identity(),
|
||||
conv_block(c, c, c),
|
||||
SelfAttention1d(
|
||||
c, c // 32) if add_attn else nn.Identity(),
|
||||
conv_block(c, c, c),
|
||||
SelfAttention1d(
|
||||
c, c // 32) if add_attn else nn.Identity(),
|
||||
block,
|
||||
conv_block(c * 2 if i != depth else c, c, c),
|
||||
SelfAttention1d(
|
||||
c, c // 32) if add_attn else nn.Identity(),
|
||||
conv_block(c, c, c),
|
||||
SelfAttention1d(
|
||||
c, c // 32) if add_attn else nn.Identity(),
|
||||
conv_block(c, c, c_prev),
|
||||
SelfAttention1d(c_prev, c_prev //
|
||||
32) if add_attn else nn.Identity(),
|
||||
Upsample1d_2(c_prev, c_prev, stride) if learned_resample else Upsample1d(kernel="cubic")
|
||||
)
|
||||
else:
|
||||
cond_embed_dim = 16 if not self.cond_noise_aug else 32
|
||||
block = nn.Sequential(
|
||||
conv_block((io_channels + cond_dim) + cond_embed_dim, c, c),
|
||||
conv_block(c, c, c),
|
||||
conv_block(c, c, c),
|
||||
block,
|
||||
conv_block(c * 2, c, c),
|
||||
conv_block(c, c, c),
|
||||
conv_block(c, c, io_channels, is_last=True),
|
||||
)
|
||||
self.net = block
|
||||
|
||||
with torch.no_grad():
|
||||
for param in self.net.parameters():
|
||||
param *= 0.5
|
||||
|
||||
def forward(self, x, t, cond=None, cond_aug_scale=None):
|
||||
|
||||
timestep_embed = expand_to_planes(self.timestep_embed(t[:, None]), x.shape)
|
||||
|
||||
inputs = [x, timestep_embed]
|
||||
|
||||
if cond is not None:
|
||||
if cond.shape[2] != x.shape[2]:
|
||||
cond = F.interpolate(cond, (x.shape[2], ), mode='linear', align_corners=False)
|
||||
|
||||
if self.cond_noise_aug:
|
||||
# Get a random number between 0 and 1, uniformly sampled
|
||||
if cond_aug_scale is None:
|
||||
aug_level = self.rng.draw(cond.shape[0])[:, 0].to(cond)
|
||||
else:
|
||||
aug_level = torch.tensor([cond_aug_scale]).repeat([cond.shape[0]]).to(cond)
|
||||
|
||||
# Add noise to the conditioning signal
|
||||
cond = cond + torch.randn_like(cond) * aug_level[:, None, None]
|
||||
|
||||
# Get embedding for noise cond level, reusing timestamp_embed
|
||||
aug_level_embed = expand_to_planes(self.timestep_embed(aug_level[:, None]), x.shape)
|
||||
|
||||
inputs.append(aug_level_embed)
|
||||
|
||||
inputs.append(cond)
|
||||
|
||||
outputs = self.net(torch.cat(inputs, dim=1))
|
||||
|
||||
return outputs
|
||||
|
||||
class DiTWrapper(ConditionedDiffusionModel):
|
||||
def __init__(
|
||||
self,
|
||||
*args,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__(supports_cross_attention=True, supports_global_cond=False, supports_input_concat=False)
|
||||
|
||||
self.model = DiffusionTransformer(*args, **kwargs)
|
||||
|
||||
with torch.no_grad():
|
||||
for param in self.model.parameters():
|
||||
param *= 0.5
|
||||
|
||||
def forward(self,
|
||||
x,
|
||||
t,
|
||||
cross_attn_cond=None,
|
||||
cross_attn_mask=None,
|
||||
negative_cross_attn_cond=None,
|
||||
negative_cross_attn_mask=None,
|
||||
input_concat_cond=None,
|
||||
negative_input_concat_cond=None,
|
||||
global_cond=None,
|
||||
negative_global_cond=None,
|
||||
prepend_cond=None,
|
||||
prepend_cond_mask=None,
|
||||
cfg_scale=1.0,
|
||||
cfg_dropout_prob: float = 0.0,
|
||||
batch_cfg: bool = True,
|
||||
rescale_cfg: bool = False,
|
||||
scale_phi: float = 0.0,
|
||||
**kwargs):
|
||||
|
||||
assert batch_cfg, "batch_cfg must be True for DiTWrapper"
|
||||
#assert negative_input_concat_cond is None, "negative_input_concat_cond is not supported for DiTWrapper"
|
||||
|
||||
return self.model(
|
||||
x,
|
||||
t,
|
||||
cross_attn_cond=cross_attn_cond,
|
||||
cross_attn_cond_mask=cross_attn_mask,
|
||||
negative_cross_attn_cond=negative_cross_attn_cond,
|
||||
negative_cross_attn_mask=negative_cross_attn_mask,
|
||||
input_concat_cond=input_concat_cond,
|
||||
prepend_cond=prepend_cond,
|
||||
prepend_cond_mask=prepend_cond_mask,
|
||||
cfg_scale=cfg_scale,
|
||||
cfg_dropout_prob=cfg_dropout_prob,
|
||||
scale_phi=scale_phi,
|
||||
global_embed=global_cond,
|
||||
**kwargs)
|
||||
|
||||
class MMDiTWrapper(ConditionedDiffusionModel):
|
||||
def __init__(
|
||||
self,
|
||||
*args,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__(supports_cross_attention=True, supports_global_cond=False, supports_input_concat=False)
|
||||
|
||||
self.model = MMmodule(*args, **kwargs)
|
||||
|
||||
# with torch.no_grad():
|
||||
# for param in self.model.parameters():
|
||||
# param *= 0.5
|
||||
|
||||
def forward(self,
|
||||
x,
|
||||
t,
|
||||
clip_f,
|
||||
sync_f,
|
||||
text_f,
|
||||
inpaint_masked_input=None,
|
||||
t5_features=None,
|
||||
metaclip_global_text_features=None,
|
||||
cfg_scale=1.0,
|
||||
cfg_dropout_prob: float = 0.0,
|
||||
batch_cfg: bool = True,
|
||||
rescale_cfg: bool = False,
|
||||
scale_phi: float = 0.0,
|
||||
**kwargs):
|
||||
|
||||
# breakpoint()
|
||||
assert batch_cfg, "batch_cfg must be True for DiTWrapper"
|
||||
#assert negative_input_concat_cond is None, "negative_input_concat_cond is not supported for DiTWrapper"
|
||||
|
||||
return self.model(
|
||||
latent=x,
|
||||
t=t,
|
||||
clip_f=clip_f,
|
||||
sync_f=sync_f,
|
||||
text_f=text_f,
|
||||
inpaint_masked_input=inpaint_masked_input,
|
||||
t5_features=t5_features,
|
||||
metaclip_global_text_features=metaclip_global_text_features,
|
||||
cfg_scale=cfg_scale,
|
||||
cfg_dropout_prob=cfg_dropout_prob,
|
||||
scale_phi=scale_phi,
|
||||
**kwargs)
|
||||
|
||||
class MMConditionedDiffusionModelWrapper(ConditionedDiffusionModel):
|
||||
"""
|
||||
A diffusion model that takes in conditioning
|
||||
"""
|
||||
def __init__(
|
||||
self,
|
||||
model: MMmodule,
|
||||
conditioner: MultiConditioner,
|
||||
io_channels,
|
||||
sample_rate,
|
||||
min_input_length: int,
|
||||
diffusion_objective: tp.Literal["v", "rectified_flow"] = "v",
|
||||
pretransform: tp.Optional[Pretransform] = None,
|
||||
cross_attn_cond_ids: tp.List[str] = [],
|
||||
global_cond_ids: tp.List[str] = [],
|
||||
input_concat_ids: tp.List[str] = [],
|
||||
prepend_cond_ids: tp.List[str] = [],
|
||||
add_cond_ids: tp.List[str] = [],
|
||||
mm_cond_ids: tp.List[str] = [],
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.model = model
|
||||
self.conditioner = conditioner
|
||||
self.io_channels = io_channels
|
||||
self.sample_rate = sample_rate
|
||||
self.diffusion_objective = diffusion_objective
|
||||
self.pretransform = pretransform
|
||||
self.cross_attn_cond_ids = cross_attn_cond_ids
|
||||
self.global_cond_ids = global_cond_ids
|
||||
self.input_concat_ids = input_concat_ids
|
||||
self.prepend_cond_ids = prepend_cond_ids
|
||||
self.add_cond_ids = add_cond_ids
|
||||
self.min_input_length = min_input_length
|
||||
self.mm_cond_ids = mm_cond_ids
|
||||
|
||||
assert len(self.cross_attn_cond_ids) == 0, "cross_attn_cond_ids is not supported for MMDiTWrapper"
|
||||
assert len(self.global_cond_ids) == 0, "global_cond_ids is not supported for MMDiTWrapper"
|
||||
assert len(self.input_concat_ids) == 0, "input_concat_ids is not supported for MMDiTWrapper"
|
||||
assert len(self.prepend_cond_ids) == 0, "prepend_cond_ids is not supported for MMDiTWrapper"
|
||||
assert len(self.add_cond_ids) == 0, "add_cond_ids is not supported for MMDiTWrapper"
|
||||
assert len(self.mm_cond_ids) > 0, "mm_cond_ids must be specified for MMDiTWrapper"
|
||||
assert "metaclip_features" in self.mm_cond_ids, "clip_f must be specified in mm_cond_ids for MMDiTWrapper"
|
||||
assert "sync_features" in self.mm_cond_ids, "sync_features must be specified in mm_cond_ids for MMDiTWrapper"
|
||||
assert "metaclip_text_features" in self.mm_cond_ids, "metaclip_text_features must be specified in mm_cond_ids for MMDiTWrapper"
|
||||
# assert len(self.mm_cond_ids) == 3, "mm_cond_ids must be clip_f sync_f text_f for MMDiTWrapper"
|
||||
|
||||
def get_conditioning_inputs(self, conditioning_tensors: tp.Dict[str, tp.Any], negative=False):
|
||||
assert negative == False, "negative conditioning is not supported for MMDiTWrapper"
|
||||
cross_attention_input = None
|
||||
cross_attention_masks = None
|
||||
global_cond = None
|
||||
input_concat_cond = None
|
||||
prepend_cond = None
|
||||
prepend_cond_mask = None
|
||||
add_input = None
|
||||
inpaint_masked_input = None
|
||||
t5_features = None
|
||||
metaclip_global_text_features = None
|
||||
clip_f = conditioning_tensors["metaclip_features"]
|
||||
sync_f = conditioning_tensors["sync_features"]
|
||||
text_f = conditioning_tensors["metaclip_text_features"]
|
||||
if 'inpaint_masked_input' in conditioning_tensors.keys():
|
||||
inpaint_masked_input = conditioning_tensors["inpaint_masked_input"]
|
||||
if 't5_features' in conditioning_tensors.keys():
|
||||
t5_features = conditioning_tensors["t5_features"]
|
||||
if 'metaclip_global_text_features' in conditioning_tensors.keys():
|
||||
metaclip_global_text_features = conditioning_tensors["metaclip_global_text_features"]
|
||||
return {
|
||||
"clip_f": clip_f,
|
||||
"sync_f": sync_f,
|
||||
"text_f": text_f,
|
||||
"inpaint_masked_input": inpaint_masked_input,
|
||||
"t5_features": t5_features,
|
||||
"metaclip_global_text_features": metaclip_global_text_features
|
||||
}
|
||||
|
||||
def forward(self, x: torch.Tensor, t: torch.Tensor, cond: tp.Dict[str, tp.Any], **kwargs):
|
||||
# breakpoint()
|
||||
# print(kwargs)
|
||||
return self.model(x=x, t=t, **self.get_conditioning_inputs(cond), **kwargs)
|
||||
|
||||
def generate(self, *args, **kwargs):
|
||||
return generate_diffusion_cond(self, *args, **kwargs)
|
||||
|
||||
class DiTUncondWrapper(DiffusionModel):
|
||||
def __init__(
|
||||
self,
|
||||
io_channels,
|
||||
*args,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.model = DiffusionTransformer(io_channels=io_channels, *args, **kwargs)
|
||||
|
||||
self.io_channels = io_channels
|
||||
|
||||
with torch.no_grad():
|
||||
for param in self.model.parameters():
|
||||
param *= 0.5
|
||||
|
||||
def forward(self, x, t, **kwargs):
|
||||
return self.model(x, t, **kwargs)
|
||||
|
||||
def create_diffusion_uncond_from_config(config: tp.Dict[str, tp.Any]):
|
||||
diffusion_uncond_config = config["model"]
|
||||
|
||||
model_type = diffusion_uncond_config.get('type', None)
|
||||
|
||||
diffusion_config = diffusion_uncond_config.get('config', {})
|
||||
|
||||
assert model_type is not None, "Must specify model type in config"
|
||||
|
||||
pretransform = diffusion_uncond_config.get("pretransform", None)
|
||||
|
||||
sample_size = config.get("sample_size", None)
|
||||
assert sample_size is not None, "Must specify sample size in config"
|
||||
|
||||
sample_rate = config.get("sample_rate", None)
|
||||
assert sample_rate is not None, "Must specify sample rate in config"
|
||||
|
||||
if pretransform is not None:
|
||||
pretransform = create_pretransform_from_config(pretransform, sample_rate)
|
||||
min_input_length = pretransform.downsampling_ratio
|
||||
else:
|
||||
min_input_length = 1
|
||||
|
||||
if model_type == 'DAU1d':
|
||||
|
||||
model = DiffusionAttnUnet1D(
|
||||
**diffusion_config
|
||||
)
|
||||
|
||||
elif model_type == "adp_uncond_1d":
|
||||
|
||||
model = UNet1DUncondWrapper(
|
||||
**diffusion_config
|
||||
)
|
||||
|
||||
elif model_type == "dit":
|
||||
model = DiTUncondWrapper(
|
||||
**diffusion_config
|
||||
)
|
||||
|
||||
else:
|
||||
raise NotImplementedError(f'Unknown model type: {model_type}')
|
||||
|
||||
return DiffusionModelWrapper(model,
|
||||
io_channels=model.io_channels,
|
||||
sample_size=sample_size,
|
||||
sample_rate=sample_rate,
|
||||
pretransform=pretransform,
|
||||
min_input_length=min_input_length)
|
||||
|
||||
def create_diffusion_infill_from_config(config: tp.Dict[str, tp.Any]):
|
||||
diffusion_uncond_config = config["model"]
|
||||
|
||||
|
||||
diffusion_config = diffusion_uncond_config.get('diffusion', {})
|
||||
model_type = diffusion_config.get('type', None)
|
||||
model_config = diffusion_config.get("config",{})
|
||||
assert model_type is not None, "Must specify model type in config"
|
||||
|
||||
pretransform = diffusion_uncond_config.get("pretransform", None)
|
||||
|
||||
sample_size = config.get("sample_size", None)
|
||||
assert sample_size is not None, "Must specify sample size in config"
|
||||
|
||||
sample_rate = config.get("sample_rate", None)
|
||||
assert sample_rate is not None, "Must specify sample rate in config"
|
||||
|
||||
if pretransform is not None:
|
||||
pretransform = create_pretransform_from_config(pretransform, sample_rate)
|
||||
min_input_length = pretransform.downsampling_ratio
|
||||
else:
|
||||
min_input_length = 1
|
||||
|
||||
if model_type == 'DAU1d':
|
||||
|
||||
model = DiffusionAttnUnet1D(
|
||||
**model_config
|
||||
)
|
||||
|
||||
elif model_type == "adp_uncond_1d":
|
||||
|
||||
model = UNet1DUncondWrapper(
|
||||
io_channels = io_channels,
|
||||
**model_config
|
||||
)
|
||||
elif model_type == "dit":
|
||||
model = DiTUncondWrapper(
|
||||
**model_config
|
||||
)
|
||||
|
||||
else:
|
||||
raise NotImplementedError(f'Unknown model type: {model_type}')
|
||||
|
||||
return DiffusionModelWrapper(model,
|
||||
io_channels=model.io_channels,
|
||||
sample_size=sample_size,
|
||||
sample_rate=sample_rate,
|
||||
pretransform=pretransform,
|
||||
min_input_length=min_input_length)
|
||||
|
||||
def create_diffusion_cond_from_config(config: tp.Dict[str, tp.Any]):
|
||||
|
||||
model_config = config["model"]
|
||||
|
||||
model_type = config["model_type"]
|
||||
|
||||
diffusion_config = model_config.get('diffusion', None)
|
||||
assert diffusion_config is not None, "Must specify diffusion config"
|
||||
|
||||
diffusion_model_type = diffusion_config.get('type', None)
|
||||
assert diffusion_model_type is not None, "Must specify diffusion model type"
|
||||
|
||||
diffusion_model_config = diffusion_config.get('config', None)
|
||||
assert diffusion_model_config is not None, "Must specify diffusion model config"
|
||||
|
||||
if diffusion_model_type == 'adp_cfg_1d':
|
||||
diffusion_model = UNetCFG1DWrapper(**diffusion_model_config)
|
||||
elif diffusion_model_type == 'adp_1d':
|
||||
diffusion_model = UNet1DCondWrapper(**diffusion_model_config)
|
||||
elif diffusion_model_type == 'dit':
|
||||
diffusion_model = DiTWrapper(**diffusion_model_config)
|
||||
elif diffusion_model_type == 'mmdit':
|
||||
diffusion_model = MMDiTWrapper(**diffusion_model_config)
|
||||
|
||||
io_channels = model_config.get('io_channels', None)
|
||||
assert io_channels is not None, "Must specify io_channels in model config"
|
||||
|
||||
sample_rate = config.get('sample_rate', None)
|
||||
assert sample_rate is not None, "Must specify sample_rate in config"
|
||||
|
||||
diffusion_objective = diffusion_config.get('diffusion_objective', 'v')
|
||||
|
||||
conditioning_config = model_config.get('conditioning', None)
|
||||
|
||||
conditioner = None
|
||||
if conditioning_config is not None:
|
||||
conditioner = create_multi_conditioner_from_conditioning_config(conditioning_config)
|
||||
|
||||
cross_attention_ids = diffusion_config.get('cross_attention_cond_ids', [])
|
||||
add_cond_ids = diffusion_config.get('add_cond_ids', [])
|
||||
global_cond_ids = diffusion_config.get('global_cond_ids', [])
|
||||
input_concat_ids = diffusion_config.get('input_concat_ids', [])
|
||||
prepend_cond_ids = diffusion_config.get('prepend_cond_ids', [])
|
||||
mm_cond_ids = diffusion_config.get('mm_cond_ids', [])
|
||||
|
||||
pretransform = model_config.get("pretransform", None)
|
||||
|
||||
if pretransform is not None:
|
||||
pretransform = create_pretransform_from_config(pretransform, sample_rate)
|
||||
min_input_length = pretransform.downsampling_ratio
|
||||
else:
|
||||
min_input_length = 1
|
||||
|
||||
if diffusion_model_type == "adp_cfg_1d" or diffusion_model_type == "adp_1d":
|
||||
min_input_length *= np.prod(diffusion_model_config["factors"])
|
||||
elif diffusion_model_type == "dit":
|
||||
min_input_length *= diffusion_model.model.patch_size
|
||||
|
||||
# Get the proper wrapper class
|
||||
|
||||
extra_kwargs = {}
|
||||
|
||||
if model_type == "mm_diffusion_cond":
|
||||
wrapper_fn = MMConditionedDiffusionModelWrapper
|
||||
extra_kwargs["diffusion_objective"] = diffusion_objective
|
||||
extra_kwargs["mm_cond_ids"] = mm_cond_ids
|
||||
|
||||
if model_type == "diffusion_cond" or model_type == "diffusion_cond_inpaint" or model_type == 'diffusion_infill':
|
||||
wrapper_fn = ConditionedDiffusionModelWrapper
|
||||
extra_kwargs["diffusion_objective"] = diffusion_objective
|
||||
|
||||
elif model_type == "diffusion_prior":
|
||||
prior_type = model_config.get("prior_type", None)
|
||||
assert prior_type is not None, "Must specify prior_type in diffusion prior model config"
|
||||
|
||||
if prior_type == "mono_stereo":
|
||||
from .diffusion_prior import MonoToStereoDiffusionPrior
|
||||
wrapper_fn = MonoToStereoDiffusionPrior
|
||||
|
||||
return wrapper_fn(
|
||||
diffusion_model,
|
||||
conditioner,
|
||||
min_input_length=min_input_length,
|
||||
sample_rate=sample_rate,
|
||||
cross_attn_cond_ids=cross_attention_ids,
|
||||
global_cond_ids=global_cond_ids,
|
||||
input_concat_ids=input_concat_ids,
|
||||
prepend_cond_ids=prepend_cond_ids,
|
||||
add_cond_ids=add_cond_ids,
|
||||
pretransform=pretransform,
|
||||
io_channels=io_channels,
|
||||
**extra_kwargs
|
||||
)
|
||||
@@ -0,0 +1,439 @@
|
||||
import typing as tp
|
||||
|
||||
import torch
|
||||
# from beartype.typing import Tuple
|
||||
from einops import rearrange
|
||||
from torch import nn
|
||||
from torch.nn import functional as F
|
||||
from x_transformers import ContinuousTransformerWrapper, Encoder
|
||||
#from .mmmodules.model.low_level import MLP, ChannelLastConv1d, ConvMLP
|
||||
from .blocks import FourierFeatures
|
||||
from .transformer import ContinuousTransformer
|
||||
from .utils import mask_from_frac_lengths, resample
|
||||
class DiffusionTransformer(nn.Module):
|
||||
def __init__(self,
|
||||
io_channels=32,
|
||||
patch_size=1,
|
||||
embed_dim=768,
|
||||
cond_token_dim=0,
|
||||
project_cond_tokens=True,
|
||||
global_cond_dim=0,
|
||||
project_global_cond=True,
|
||||
input_concat_dim=0,
|
||||
prepend_cond_dim=0,
|
||||
cond_ctx_dim=0,
|
||||
depth=12,
|
||||
num_heads=8,
|
||||
transformer_type: tp.Literal["x-transformers", "continuous_transformer","mm_transformer"] = "x-transformers",
|
||||
global_cond_type: tp.Literal["prepend", "adaLN"] = "prepend",
|
||||
frac_lengths_mask = (0.7, 1.),
|
||||
ctx_drop: float = 0.1,
|
||||
add_token_dim=0,
|
||||
use_mlp=False,
|
||||
**kwargs):
|
||||
|
||||
super().__init__()
|
||||
|
||||
self.cond_token_dim = cond_token_dim
|
||||
|
||||
# Timestep embeddings
|
||||
timestep_features_dim = 256
|
||||
|
||||
self.timestep_features = FourierFeatures(1, timestep_features_dim)
|
||||
|
||||
self.to_timestep_embed = nn.Sequential(
|
||||
nn.Linear(timestep_features_dim, embed_dim, bias=True),
|
||||
nn.SiLU(),
|
||||
nn.Linear(embed_dim, embed_dim, bias=True),
|
||||
)
|
||||
self.use_mlp = use_mlp
|
||||
if cond_token_dim > 0:
|
||||
# Conditioning tokens
|
||||
cond_embed_dim = cond_token_dim if not project_cond_tokens else embed_dim
|
||||
self.to_cond_embed = nn.Sequential(
|
||||
nn.Linear(cond_token_dim, cond_embed_dim, bias=False),
|
||||
nn.SiLU(),
|
||||
nn.Linear(cond_embed_dim, cond_embed_dim, bias=False)
|
||||
)
|
||||
else:
|
||||
cond_embed_dim = 0
|
||||
|
||||
if global_cond_dim > 0:
|
||||
# Global conditioning
|
||||
global_embed_dim = global_cond_dim if not project_global_cond else embed_dim
|
||||
self.to_global_embed = nn.Sequential(
|
||||
nn.Linear(global_cond_dim, global_embed_dim, bias=False),
|
||||
nn.SiLU(),
|
||||
nn.Linear(global_embed_dim, global_embed_dim, bias=False)
|
||||
)
|
||||
|
||||
if add_token_dim > 0:
|
||||
# Conditioning tokens
|
||||
|
||||
add_embed_dim = add_token_dim if not project_cond_tokens else embed_dim
|
||||
self.to_add_embed = nn.Sequential(
|
||||
nn.SiLU(),
|
||||
ConvMLP(add_embed_dim, add_embed_dim * 4, kernel_size=3, padding=1),
|
||||
)
|
||||
else:
|
||||
add_embed_dim = 0
|
||||
|
||||
if cond_ctx_dim > 0:
|
||||
self.ctx_linear = nn.Linear(cond_ctx_dim*2, cond_ctx_dim, bias=True)
|
||||
self.frac_lengths_mask = frac_lengths_mask
|
||||
self.ctx_drop = ctx_drop
|
||||
|
||||
if prepend_cond_dim > 0:
|
||||
# Prepend conditioning
|
||||
self.to_prepend_embed = nn.Sequential(
|
||||
nn.Linear(prepend_cond_dim, embed_dim, bias=False),
|
||||
nn.SiLU(),
|
||||
nn.Linear(embed_dim, embed_dim, bias=False)
|
||||
)
|
||||
|
||||
self.input_concat_dim = input_concat_dim
|
||||
|
||||
dim_in = io_channels + self.input_concat_dim
|
||||
|
||||
self.patch_size = patch_size
|
||||
|
||||
# Transformer
|
||||
|
||||
self.transformer_type = transformer_type
|
||||
|
||||
self.global_cond_type = global_cond_type
|
||||
print("######################")
|
||||
print(f'global type: {global_cond_type}')
|
||||
print("######################")
|
||||
if self.transformer_type == "x-transformers":
|
||||
self.transformer = ContinuousTransformerWrapper(
|
||||
dim_in=dim_in * patch_size,
|
||||
dim_out=io_channels * patch_size,
|
||||
max_seq_len=0, #Not relevant without absolute positional embeds
|
||||
attn_layers = Encoder(
|
||||
dim=embed_dim,
|
||||
depth=depth,
|
||||
heads=num_heads,
|
||||
attn_flash = True,
|
||||
cross_attend = cond_token_dim > 0,
|
||||
dim_context=None if cond_embed_dim == 0 else cond_embed_dim,
|
||||
zero_init_branch_output=True,
|
||||
use_abs_pos_emb = False,
|
||||
rotary_pos_emb=True,
|
||||
ff_swish = True,
|
||||
ff_glu = True,
|
||||
**kwargs
|
||||
)
|
||||
)
|
||||
|
||||
elif self.transformer_type == "continuous_transformer":
|
||||
|
||||
global_dim = None
|
||||
|
||||
if self.global_cond_type == "adaLN":
|
||||
# The global conditioning is projected to the embed_dim already at this point
|
||||
global_dim = embed_dim
|
||||
|
||||
self.transformer = ContinuousTransformer(
|
||||
dim=embed_dim,
|
||||
depth=depth,
|
||||
dim_heads=embed_dim // num_heads,
|
||||
dim_in=dim_in * patch_size,
|
||||
dim_out=io_channels * patch_size,
|
||||
cross_attend = cond_token_dim > 0,
|
||||
cond_token_dim = cond_embed_dim,
|
||||
global_cond_dim=global_dim,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
else:
|
||||
raise ValueError(f"Unknown transformer type: {self.transformer_type}")
|
||||
|
||||
self.preprocess_conv = nn.Conv1d(dim_in, dim_in, 1, bias=False)
|
||||
nn.init.zeros_(self.preprocess_conv.weight)
|
||||
self.postprocess_conv = nn.Conv1d(io_channels, io_channels, 1, bias=False)
|
||||
nn.init.zeros_(self.postprocess_conv.weight)
|
||||
|
||||
def _forward(
|
||||
self,
|
||||
x,
|
||||
t,
|
||||
mask=None,
|
||||
cross_attn_cond=None,
|
||||
cross_attn_cond_mask=None,
|
||||
input_concat_cond=None,
|
||||
global_embed=None,
|
||||
prepend_cond=None,
|
||||
prepend_cond_mask=None,
|
||||
add_cond=None,
|
||||
add_masks=None,
|
||||
# x_ctx=None,
|
||||
return_info=False,
|
||||
**kwargs):
|
||||
|
||||
if cross_attn_cond is not None:
|
||||
cross_attn_cond = self.to_cond_embed(cross_attn_cond)
|
||||
if global_embed is not None:
|
||||
# Project the global conditioning to the embedding dimension
|
||||
global_embed = self.to_global_embed(global_embed)
|
||||
if len(global_embed.shape) == 3:
|
||||
global_embed = torch.max(global_embed, dim=1).values
|
||||
|
||||
prepend_inputs = None
|
||||
prepend_mask = None
|
||||
prepend_length = 0
|
||||
if prepend_cond is not None:
|
||||
# Project the prepend conditioning to the embedding dimension
|
||||
prepend_cond = self.to_prepend_embed(prepend_cond)
|
||||
|
||||
prepend_inputs = prepend_cond
|
||||
if prepend_cond_mask is not None:
|
||||
prepend_mask = prepend_cond_mask
|
||||
|
||||
if input_concat_cond is not None:
|
||||
|
||||
# Interpolate input_concat_cond to the same length as x
|
||||
if input_concat_cond.shape[2] != x.shape[2]:
|
||||
input_concat_cond = F.interpolate(input_concat_cond, (x.shape[2], ), mode='nearest-exact')
|
||||
|
||||
x = torch.cat([x, input_concat_cond], dim=1)
|
||||
|
||||
if add_cond is not None:
|
||||
# Interpolate input_concat_cond to the same length as x
|
||||
|
||||
if self.use_mlp:
|
||||
add_cond = self.to_add_embed(add_cond)
|
||||
if add_cond.shape[1] != x.shape[2]:
|
||||
# add_cond = add_cond.transpose(1,2)
|
||||
# add_cond = F.interpolate(add_cond, (x.shape[2], ), mode='nearest-exact')
|
||||
# add_cond = add_cond.transpose(1,2)
|
||||
add_cond = resample(add_cond, x)
|
||||
|
||||
# Get the batch of timestep embeddings
|
||||
timestep_embed = self.to_timestep_embed(self.timestep_features(t[:, None])) # (b, embed_dim)
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
# Timestep embedding is considered a global embedding. Add to the global conditioning if it exists
|
||||
if global_embed is not None:
|
||||
global_embed = global_embed + timestep_embed
|
||||
else:
|
||||
global_embed = timestep_embed
|
||||
|
||||
# Add the global_embed to the prepend inputs if there is no global conditioning support in the transformer
|
||||
if self.global_cond_type == "prepend":
|
||||
if prepend_inputs is None:
|
||||
# Prepend inputs are just the global embed, and the mask is all ones
|
||||
prepend_inputs = global_embed.unsqueeze(1)
|
||||
prepend_mask = torch.ones((x.shape[0], 1), device=x.device, dtype=torch.bool)
|
||||
else:
|
||||
# Prepend inputs are the prepend conditioning + the global embed
|
||||
prepend_inputs = torch.cat([prepend_inputs, global_embed.unsqueeze(1)], dim=1)
|
||||
prepend_mask = torch.cat([prepend_mask, torch.ones((x.shape[0], 1), device=x.device, dtype=torch.bool)], dim=1)
|
||||
|
||||
prepend_length = prepend_inputs.shape[1]
|
||||
|
||||
x = self.preprocess_conv(x) + x
|
||||
x = rearrange(x, "b c t -> b t c")
|
||||
|
||||
|
||||
extra_args = {}
|
||||
|
||||
if self.global_cond_type == "adaLN":
|
||||
extra_args["global_cond"] = global_embed
|
||||
|
||||
if self.patch_size > 1:
|
||||
x = rearrange(x, "b (t p) c -> b t (c p)", p=self.patch_size)
|
||||
|
||||
if self.transformer_type == "x-transformers":
|
||||
output = self.transformer(x, prepend_embeds=prepend_inputs, context=cross_attn_cond, context_mask=cross_attn_cond_mask, add_cond=add_cond, mask=mask, prepend_mask=prepend_mask, **extra_args, **kwargs)
|
||||
elif self.transformer_type == "continuous_transformer":
|
||||
output = self.transformer(x, prepend_embeds=prepend_inputs, context=cross_attn_cond, add_cond=add_cond, context_mask=cross_attn_cond_mask, mask=mask, prepend_mask=prepend_mask, return_info=return_info, **extra_args, **kwargs)
|
||||
|
||||
if return_info:
|
||||
output, info = output
|
||||
elif self.transformer_type == "mm_transformer":
|
||||
output = self.transformer(x, context=cross_attn_cond, mask=mask, context_mask=cross_attn_cond_mask, **extra_args, **kwargs)
|
||||
|
||||
output = rearrange(output, "b t c -> b c t")[:,:,prepend_length:]
|
||||
|
||||
if self.patch_size > 1:
|
||||
output = rearrange(output, "b (c p) t -> b c (t p)", p=self.patch_size)
|
||||
|
||||
output = self.postprocess_conv(output) + output
|
||||
|
||||
if return_info:
|
||||
return output, info
|
||||
|
||||
return output
|
||||
|
||||
def forward(
|
||||
self,
|
||||
x,
|
||||
t,
|
||||
cross_attn_cond=None,
|
||||
cross_attn_cond_mask=None,
|
||||
negative_cross_attn_cond=None,
|
||||
negative_cross_attn_mask=None,
|
||||
input_concat_cond=None,
|
||||
global_embed=None,
|
||||
negative_global_embed=None,
|
||||
prepend_cond=None,
|
||||
prepend_cond_mask=None,
|
||||
add_cond=None,
|
||||
cfg_scale=1.0,
|
||||
cfg_dropout_prob=0.0,
|
||||
causal=False,
|
||||
scale_phi=0.0,
|
||||
mask=None,
|
||||
x_ctx=None,
|
||||
ctx_mask=None,
|
||||
return_info=False,
|
||||
**kwargs):
|
||||
|
||||
assert causal == False, "Causal mode is not supported for DiffusionTransformer"
|
||||
bsz, a, b = x.shape
|
||||
|
||||
if cross_attn_cond_mask is not None:
|
||||
cross_attn_cond_mask = cross_attn_cond_mask.bool()
|
||||
|
||||
cross_attn_cond_mask = None # Temporarily disabling conditioning masks due to kernel issue for flash attention
|
||||
|
||||
if prepend_cond_mask is not None:
|
||||
prepend_cond_mask = prepend_cond_mask.bool()
|
||||
|
||||
# CFG dropout
|
||||
if cfg_dropout_prob > 0.0:
|
||||
if cross_attn_cond is not None:
|
||||
null_embed = torch.zeros_like(cross_attn_cond, device=cross_attn_cond.device)
|
||||
dropout_mask = torch.bernoulli(torch.full((cross_attn_cond.shape[0], 1, 1), cfg_dropout_prob, device=cross_attn_cond.device)).to(torch.bool)
|
||||
cross_attn_cond = torch.where(dropout_mask, null_embed, cross_attn_cond)
|
||||
|
||||
if prepend_cond is not None:
|
||||
null_embed = torch.zeros_like(prepend_cond, device=prepend_cond.device)
|
||||
dropout_mask = torch.bernoulli(torch.full((prepend_cond.shape[0], 1, 1), cfg_dropout_prob, device=prepend_cond.device)).to(torch.bool)
|
||||
prepend_cond = torch.where(dropout_mask, null_embed, prepend_cond)
|
||||
|
||||
if add_cond is not None:
|
||||
null_embed = torch.zeros_like(add_cond, device=add_cond.device)
|
||||
dropout_mask = torch.bernoulli(torch.full((add_cond.shape[0], 1, 1), cfg_dropout_prob, device=add_cond.device)).to(torch.bool)
|
||||
add_cond = torch.where(dropout_mask, null_embed, add_cond)
|
||||
|
||||
if cfg_scale != 1.0 and (cross_attn_cond is not None or prepend_cond is not None or add_cond is not None):
|
||||
# Classifier-free guidance
|
||||
# Concatenate conditioned and unconditioned inputs on the batch dimension
|
||||
batch_inputs = torch.cat([x, x], dim=0)
|
||||
batch_timestep = torch.cat([t, t], dim=0)
|
||||
|
||||
if global_embed is not None:
|
||||
batch_global_cond = torch.cat([global_embed, global_embed], dim=0)
|
||||
else:
|
||||
batch_global_cond = None
|
||||
|
||||
if input_concat_cond is not None:
|
||||
batch_input_concat_cond = torch.cat([input_concat_cond, input_concat_cond], dim=0)
|
||||
else:
|
||||
batch_input_concat_cond = None
|
||||
|
||||
batch_cond = None
|
||||
batch_cond_masks = None
|
||||
|
||||
# Handle CFG for cross-attention conditioning
|
||||
if cross_attn_cond is not None:
|
||||
|
||||
null_embed = torch.zeros_like(cross_attn_cond, device=cross_attn_cond.device)
|
||||
|
||||
# For negative cross-attention conditioning, replace the null embed with the negative cross-attention conditioning
|
||||
if negative_cross_attn_cond is not None:
|
||||
|
||||
# If there's a negative cross-attention mask, set the masked tokens to the null embed
|
||||
if negative_cross_attn_mask is not None:
|
||||
negative_cross_attn_mask = negative_cross_attn_mask.to(torch.bool).unsqueeze(2)
|
||||
|
||||
negative_cross_attn_cond = torch.where(negative_cross_attn_mask, negative_cross_attn_cond, null_embed)
|
||||
|
||||
batch_cond = torch.cat([cross_attn_cond, negative_cross_attn_cond], dim=0)
|
||||
|
||||
else:
|
||||
batch_cond = torch.cat([cross_attn_cond, null_embed], dim=0)
|
||||
|
||||
if cross_attn_cond_mask is not None:
|
||||
batch_cond_masks = torch.cat([cross_attn_cond_mask, cross_attn_cond_mask], dim=0)
|
||||
|
||||
batch_prepend_cond = None
|
||||
batch_prepend_cond_mask = None
|
||||
|
||||
if prepend_cond is not None:
|
||||
|
||||
null_embed = torch.zeros_like(prepend_cond, device=prepend_cond.device)
|
||||
|
||||
batch_prepend_cond = torch.cat([prepend_cond, null_embed], dim=0)
|
||||
|
||||
if prepend_cond_mask is not None:
|
||||
batch_prepend_cond_mask = torch.cat([prepend_cond_mask, prepend_cond_mask], dim=0)
|
||||
|
||||
batch_add_cond = None
|
||||
|
||||
# Handle CFG for cross-attention conditioning
|
||||
if add_cond is not None:
|
||||
|
||||
null_embed = torch.zeros_like(add_cond, device=add_cond.device)
|
||||
|
||||
|
||||
batch_add_cond = torch.cat([add_cond, null_embed], dim=0)
|
||||
|
||||
|
||||
if mask is not None:
|
||||
batch_masks = torch.cat([mask, mask], dim=0)
|
||||
else:
|
||||
batch_masks = None
|
||||
|
||||
batch_output = self._forward(
|
||||
batch_inputs,
|
||||
batch_timestep,
|
||||
cross_attn_cond=batch_cond,
|
||||
cross_attn_cond_mask=batch_cond_masks,
|
||||
mask = batch_masks,
|
||||
# x_ctx=x_ctx,
|
||||
input_concat_cond=batch_input_concat_cond,
|
||||
global_embed = batch_global_cond,
|
||||
prepend_cond = batch_prepend_cond,
|
||||
prepend_cond_mask = batch_prepend_cond_mask,
|
||||
add_cond = batch_add_cond,
|
||||
return_info = return_info,
|
||||
**kwargs)
|
||||
|
||||
if return_info:
|
||||
batch_output, info = batch_output
|
||||
|
||||
cond_output, uncond_output = torch.chunk(batch_output, 2, dim=0)
|
||||
cfg_output = uncond_output + (cond_output - uncond_output) * cfg_scale
|
||||
|
||||
# CFG Rescale
|
||||
if scale_phi != 0.0:
|
||||
cond_out_std = cond_output.std(dim=1, keepdim=True)
|
||||
out_cfg_std = cfg_output.std(dim=1, keepdim=True)
|
||||
output = scale_phi * (cfg_output * (cond_out_std/out_cfg_std)) + (1-scale_phi) * cfg_output
|
||||
else:
|
||||
output = cfg_output
|
||||
|
||||
if return_info:
|
||||
return output, info
|
||||
|
||||
return output
|
||||
|
||||
else:
|
||||
return self._forward(
|
||||
x,
|
||||
t,
|
||||
cross_attn_cond=cross_attn_cond,
|
||||
cross_attn_cond_mask=cross_attn_cond_mask,
|
||||
input_concat_cond=input_concat_cond,
|
||||
global_embed=global_embed,
|
||||
prepend_cond=prepend_cond,
|
||||
prepend_cond_mask=prepend_cond_mask,
|
||||
add_cond=add_cond,
|
||||
# x_ctx=x_ctx,
|
||||
mask=mask,
|
||||
return_info=return_info,
|
||||
**kwargs
|
||||
)
|
||||
@@ -0,0 +1,84 @@
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
|
||||
# https://github.com/facebookresearch/DiT
|
||||
|
||||
from typing import Union
|
||||
|
||||
import torch
|
||||
from einops import rearrange
|
||||
from torch import Tensor
|
||||
|
||||
# Ref: https://github.com/black-forest-labs/flux/blob/main/src/flux/math.py
|
||||
# Ref: https://github.com/lucidrains/rotary-embedding-torch
|
||||
|
||||
|
||||
def compute_rope_rotations(length: int,
|
||||
dim: int,
|
||||
theta: int,
|
||||
*,
|
||||
freq_scaling: float = 1.0,
|
||||
device: Union[torch.device, str] = 'cpu') -> Tensor:
|
||||
assert dim % 2 == 0
|
||||
|
||||
with torch.amp.autocast(device_type='cuda', enabled=False):
|
||||
pos = torch.arange(length, dtype=torch.float32, device=device)
|
||||
freqs = 1.0 / (theta**(torch.arange(0, dim, 2, dtype=torch.float32, device=device) / dim))
|
||||
freqs *= freq_scaling
|
||||
|
||||
rot = torch.einsum('..., f -> ... f', pos, freqs)
|
||||
rot = torch.stack([torch.cos(rot), -torch.sin(rot), torch.sin(rot), torch.cos(rot)], dim=-1)
|
||||
rot = rearrange(rot, 'n d (i j) -> 1 n d i j', i=2, j=2)
|
||||
return rot
|
||||
|
||||
|
||||
def apply_rope(x: Tensor, rot: Tensor) -> tuple[Tensor, Tensor]:
|
||||
with torch.amp.autocast(device_type='cuda', enabled=False):
|
||||
_x = x.float()
|
||||
_x = _x.view(*_x.shape[:-1], -1, 1, 2)
|
||||
x_out = rot[..., 0] * _x[..., 0] + rot[..., 1] * _x[..., 1]
|
||||
return x_out.reshape(*x.shape).to(dtype=x.dtype)
|
||||
|
||||
|
||||
class TimestepEmbedder(nn.Module):
|
||||
"""
|
||||
Embeds scalar timesteps into vector representations.
|
||||
"""
|
||||
|
||||
def __init__(self, dim, frequency_embedding_size, max_period):
|
||||
super().__init__()
|
||||
self.mlp = nn.Sequential(
|
||||
nn.Linear(frequency_embedding_size, dim),
|
||||
nn.SiLU(),
|
||||
nn.Linear(dim, dim),
|
||||
)
|
||||
self.dim = dim
|
||||
self.max_period = max_period
|
||||
assert dim % 2 == 0, 'dim must be even.'
|
||||
|
||||
with torch.autocast('cuda', enabled=False):
|
||||
freqs = 1.0 / (10000**(torch.arange(0, frequency_embedding_size, 2, dtype=torch.float32) /
|
||||
frequency_embedding_size))
|
||||
freq_scale = 10000 / max_period
|
||||
freqs = freq_scale * freqs
|
||||
self.register_buffer('freqs', freqs, persistent=False)
|
||||
|
||||
def timestep_embedding(self, t):
|
||||
"""
|
||||
Create sinusoidal timestep embeddings.
|
||||
:param t: a 1-D Tensor of N indices, one per batch element.
|
||||
These may be fractional.
|
||||
:param dim: the dimension of the output.
|
||||
:param max_period: controls the minimum frequency of the embeddings.
|
||||
:return: an (N, D) Tensor of positional embeddings.
|
||||
"""
|
||||
# https://github.com/openai/glide-text2im/blob/main/glide_text2im/nn.py
|
||||
|
||||
args = t[:, None].float() * self.freqs[None]
|
||||
embedding = torch.cat([torch.cos(args), torch.sin(args)], dim=-1)
|
||||
return embedding
|
||||
|
||||
def forward(self, t):
|
||||
t_freq = self.timestep_embedding(t).to(t.dtype)
|
||||
t_emb = self.mlp(t_freq)
|
||||
return t_emb
|
||||
@@ -0,0 +1,156 @@
|
||||
import json
|
||||
|
||||
def create_model_from_config(model_config):
|
||||
model_type = model_config.get('model_type', None)
|
||||
|
||||
assert model_type is not None, 'model_type must be specified in model config'
|
||||
|
||||
if model_type == 'autoencoder':
|
||||
from .autoencoders import create_autoencoder_from_config
|
||||
return create_autoencoder_from_config(model_config)
|
||||
elif model_type == 'diffusion_uncond':
|
||||
from .diffusion import create_diffusion_uncond_from_config
|
||||
return create_diffusion_uncond_from_config(model_config)
|
||||
# elif model_type == 'diffusion_infill':
|
||||
# from .diffusion import create_diffusion_infill_from_config
|
||||
# return create_diffusion_infill_from_config(model_config)
|
||||
elif model_type == 'diffusion_cond' or model_type == 'diffusion_cond_inpaint' or model_type == "diffusion_prior" or model_type == "diffusion_infill" or model_type == "mm_diffusion_cond":
|
||||
from .diffusion import create_diffusion_cond_from_config
|
||||
return create_diffusion_cond_from_config(model_config)
|
||||
elif model_type == 'diffusion_autoencoder':
|
||||
from .autoencoders import create_diffAE_from_config
|
||||
return create_diffAE_from_config(model_config)
|
||||
elif model_type == 'lm':
|
||||
from .lm import create_audio_lm_from_config
|
||||
return create_audio_lm_from_config(model_config)
|
||||
else:
|
||||
raise NotImplementedError(f'Unknown model type: {model_type}')
|
||||
|
||||
def create_model_from_config_path(model_config_path):
|
||||
with open(model_config_path) as f:
|
||||
model_config = json.load(f)
|
||||
|
||||
return create_model_from_config(model_config)
|
||||
|
||||
def create_pretransform_from_config(pretransform_config, sample_rate):
|
||||
pretransform_type = pretransform_config.get('type', None)
|
||||
|
||||
assert pretransform_type is not None, 'type must be specified in pretransform config'
|
||||
|
||||
if pretransform_type == 'autoencoder':
|
||||
from .autoencoders import create_autoencoder_from_config
|
||||
from .pretransforms import AutoencoderPretransform
|
||||
|
||||
# Create fake top-level config to pass sample rate to autoencoder constructor
|
||||
# This is a bit of a hack but it keeps us from re-defining the sample rate in the config
|
||||
autoencoder_config = {"sample_rate": sample_rate, "model": pretransform_config["config"]}
|
||||
autoencoder = create_autoencoder_from_config(autoencoder_config)
|
||||
|
||||
scale = pretransform_config.get("scale", 1.0)
|
||||
model_half = pretransform_config.get("model_half", False)
|
||||
iterate_batch = pretransform_config.get("iterate_batch", False)
|
||||
chunked = pretransform_config.get("chunked", False)
|
||||
|
||||
pretransform = AutoencoderPretransform(autoencoder, scale=scale, model_half=model_half, iterate_batch=iterate_batch, chunked=chunked)
|
||||
elif pretransform_type == 'wavelet':
|
||||
from .pretransforms import WaveletPretransform
|
||||
|
||||
wavelet_config = pretransform_config["config"]
|
||||
channels = wavelet_config["channels"]
|
||||
levels = wavelet_config["levels"]
|
||||
wavelet = wavelet_config["wavelet"]
|
||||
|
||||
pretransform = WaveletPretransform(channels, levels, wavelet)
|
||||
elif pretransform_type == 'pqmf':
|
||||
from .pretransforms import PQMFPretransform
|
||||
pqmf_config = pretransform_config["config"]
|
||||
pretransform = PQMFPretransform(**pqmf_config)
|
||||
elif pretransform_type == 'dac_pretrained':
|
||||
from .pretransforms import PretrainedDACPretransform
|
||||
pretrained_dac_config = pretransform_config["config"]
|
||||
pretransform = PretrainedDACPretransform(**pretrained_dac_config)
|
||||
elif pretransform_type == "audiocraft_pretrained":
|
||||
from .pretransforms import AudiocraftCompressionPretransform
|
||||
|
||||
audiocraft_config = pretransform_config["config"]
|
||||
pretransform = AudiocraftCompressionPretransform(**audiocraft_config)
|
||||
else:
|
||||
raise NotImplementedError(f'Unknown pretransform type: {pretransform_type}')
|
||||
|
||||
enable_grad = pretransform_config.get('enable_grad', False)
|
||||
pretransform.enable_grad = enable_grad
|
||||
|
||||
pretransform.eval().requires_grad_(pretransform.enable_grad)
|
||||
|
||||
return pretransform
|
||||
|
||||
def create_bottleneck_from_config(bottleneck_config):
|
||||
bottleneck_type = bottleneck_config.get('type', None)
|
||||
|
||||
assert bottleneck_type is not None, 'type must be specified in bottleneck config'
|
||||
|
||||
if bottleneck_type == 'tanh':
|
||||
from .bottleneck import TanhBottleneck
|
||||
bottleneck = TanhBottleneck()
|
||||
elif bottleneck_type == 'vae':
|
||||
from .bottleneck import VAEBottleneck
|
||||
bottleneck = VAEBottleneck()
|
||||
elif bottleneck_type == 'rvq':
|
||||
from .bottleneck import RVQBottleneck
|
||||
|
||||
quantizer_params = {
|
||||
"dim": 128,
|
||||
"codebook_size": 1024,
|
||||
"num_quantizers": 8,
|
||||
"decay": 0.99,
|
||||
"kmeans_init": True,
|
||||
"kmeans_iters": 50,
|
||||
"threshold_ema_dead_code": 2,
|
||||
}
|
||||
|
||||
quantizer_params.update(bottleneck_config["config"])
|
||||
|
||||
bottleneck = RVQBottleneck(**quantizer_params)
|
||||
elif bottleneck_type == "dac_rvq":
|
||||
from .bottleneck import DACRVQBottleneck
|
||||
|
||||
bottleneck = DACRVQBottleneck(**bottleneck_config["config"])
|
||||
|
||||
elif bottleneck_type == 'rvq_vae':
|
||||
from .bottleneck import RVQVAEBottleneck
|
||||
|
||||
quantizer_params = {
|
||||
"dim": 128,
|
||||
"codebook_size": 1024,
|
||||
"num_quantizers": 8,
|
||||
"decay": 0.99,
|
||||
"kmeans_init": True,
|
||||
"kmeans_iters": 50,
|
||||
"threshold_ema_dead_code": 2,
|
||||
}
|
||||
|
||||
quantizer_params.update(bottleneck_config["config"])
|
||||
|
||||
bottleneck = RVQVAEBottleneck(**quantizer_params)
|
||||
|
||||
elif bottleneck_type == 'dac_rvq_vae':
|
||||
from .bottleneck import DACRVQVAEBottleneck
|
||||
bottleneck = DACRVQVAEBottleneck(**bottleneck_config["config"])
|
||||
elif bottleneck_type == 'l2_norm':
|
||||
from .bottleneck import L2Bottleneck
|
||||
bottleneck = L2Bottleneck()
|
||||
elif bottleneck_type == "wasserstein":
|
||||
from .bottleneck import WassersteinBottleneck
|
||||
bottleneck = WassersteinBottleneck(**bottleneck_config.get("config", {}))
|
||||
elif bottleneck_type == "fsq":
|
||||
from .bottleneck import FSQBottleneck
|
||||
bottleneck = FSQBottleneck(**bottleneck_config["config"])
|
||||
else:
|
||||
raise NotImplementedError(f'Unknown bottleneck type: {bottleneck_type}')
|
||||
|
||||
requires_grad = bottleneck_config.get('requires_grad', True)
|
||||
if not requires_grad:
|
||||
for param in bottleneck.parameters():
|
||||
param.requires_grad = False
|
||||
|
||||
return bottleneck
|
||||
@@ -0,0 +1,278 @@
|
||||
import torch
|
||||
|
||||
from einops import rearrange
|
||||
from torch import nn
|
||||
|
||||
from .blocks import AdaRMSNorm
|
||||
from .transformer import Attention, FeedForward, RotaryEmbedding, LayerNorm
|
||||
|
||||
def checkpoint(function, *args, **kwargs):
|
||||
kwargs.setdefault("use_reentrant", False)
|
||||
return torch.utils.checkpoint.checkpoint(function, *args, **kwargs)
|
||||
|
||||
# Adapted from https://github.com/lucidrains/local-attention/blob/master/local_attention/transformer.py
|
||||
class ContinuousLocalTransformer(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
*,
|
||||
dim,
|
||||
depth,
|
||||
dim_in = None,
|
||||
dim_out = None,
|
||||
causal = False,
|
||||
local_attn_window_size = 64,
|
||||
heads = 8,
|
||||
ff_mult = 2,
|
||||
cond_dim = 0,
|
||||
cross_attn_cond_dim = 0,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
dim_head = dim//heads
|
||||
|
||||
self.layers = nn.ModuleList([])
|
||||
|
||||
self.project_in = nn.Linear(dim_in, dim) if dim_in is not None else nn.Identity()
|
||||
|
||||
self.project_out = nn.Linear(dim, dim_out) if dim_out is not None else nn.Identity()
|
||||
|
||||
self.local_attn_window_size = local_attn_window_size
|
||||
|
||||
self.cond_dim = cond_dim
|
||||
|
||||
self.cross_attn_cond_dim = cross_attn_cond_dim
|
||||
|
||||
self.rotary_pos_emb = RotaryEmbedding(max(dim_head // 2, 32))
|
||||
|
||||
for _ in range(depth):
|
||||
|
||||
self.layers.append(nn.ModuleList([
|
||||
AdaRMSNorm(dim, cond_dim, eps=1e-8) if cond_dim > 0 else LayerNorm(dim),
|
||||
Attention(
|
||||
dim=dim,
|
||||
dim_heads=dim_head,
|
||||
causal=causal,
|
||||
zero_init_output=True,
|
||||
natten_kernel_size=local_attn_window_size,
|
||||
),
|
||||
Attention(
|
||||
dim=dim,
|
||||
dim_heads=dim_head,
|
||||
dim_context = cross_attn_cond_dim,
|
||||
zero_init_output=True
|
||||
) if self.cross_attn_cond_dim > 0 else nn.Identity(),
|
||||
AdaRMSNorm(dim, cond_dim, eps=1e-8) if cond_dim > 0 else LayerNorm(dim),
|
||||
FeedForward(dim = dim, mult = ff_mult, no_bias=True)
|
||||
]))
|
||||
|
||||
def forward(self, x, mask = None, cond = None, cross_attn_cond = None, cross_attn_cond_mask = None, prepend_cond = None):
|
||||
|
||||
x = checkpoint(self.project_in, x)
|
||||
|
||||
if prepend_cond is not None:
|
||||
x = torch.cat([prepend_cond, x], dim=1)
|
||||
|
||||
pos_emb = self.rotary_pos_emb.forward_from_seq_len(x.shape[1])
|
||||
|
||||
for attn_norm, attn, xattn, ff_norm, ff in self.layers:
|
||||
|
||||
residual = x
|
||||
if cond is not None:
|
||||
x = checkpoint(attn_norm, x, cond)
|
||||
else:
|
||||
x = checkpoint(attn_norm, x)
|
||||
|
||||
x = checkpoint(attn, x, mask = mask, rotary_pos_emb=pos_emb) + residual
|
||||
|
||||
if cross_attn_cond is not None:
|
||||
x = checkpoint(xattn, x, context=cross_attn_cond, context_mask=cross_attn_cond_mask) + x
|
||||
|
||||
residual = x
|
||||
|
||||
if cond is not None:
|
||||
x = checkpoint(ff_norm, x, cond)
|
||||
else:
|
||||
x = checkpoint(ff_norm, x)
|
||||
|
||||
x = checkpoint(ff, x) + residual
|
||||
|
||||
return checkpoint(self.project_out, x)
|
||||
|
||||
class TransformerDownsampleBlock1D(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
in_channels,
|
||||
embed_dim = 768,
|
||||
depth = 3,
|
||||
heads = 12,
|
||||
downsample_ratio = 2,
|
||||
local_attn_window_size = 64,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.downsample_ratio = downsample_ratio
|
||||
|
||||
self.transformer = ContinuousLocalTransformer(
|
||||
dim=embed_dim,
|
||||
depth=depth,
|
||||
heads=heads,
|
||||
local_attn_window_size=local_attn_window_size,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
self.project_in = nn.Linear(in_channels, embed_dim, bias=False) if in_channels != embed_dim else nn.Identity()
|
||||
|
||||
self.project_down = nn.Linear(embed_dim * self.downsample_ratio, embed_dim, bias=False)
|
||||
|
||||
|
||||
def forward(self, x):
|
||||
|
||||
x = checkpoint(self.project_in, x)
|
||||
|
||||
# Compute
|
||||
x = self.transformer(x)
|
||||
|
||||
# Trade sequence length for channels
|
||||
x = rearrange(x, "b (n r) c -> b n (c r)", r=self.downsample_ratio)
|
||||
|
||||
# Project back to embed dim
|
||||
x = checkpoint(self.project_down, x)
|
||||
|
||||
return x
|
||||
|
||||
class TransformerUpsampleBlock1D(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
in_channels,
|
||||
embed_dim,
|
||||
depth = 3,
|
||||
heads = 12,
|
||||
upsample_ratio = 2,
|
||||
local_attn_window_size = 64,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.upsample_ratio = upsample_ratio
|
||||
|
||||
self.transformer = ContinuousLocalTransformer(
|
||||
dim=embed_dim,
|
||||
depth=depth,
|
||||
heads=heads,
|
||||
local_attn_window_size = local_attn_window_size,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
self.project_in = nn.Linear(in_channels, embed_dim, bias=False) if in_channels != embed_dim else nn.Identity()
|
||||
|
||||
self.project_up = nn.Linear(embed_dim, embed_dim * self.upsample_ratio, bias=False)
|
||||
|
||||
def forward(self, x):
|
||||
|
||||
# Project to embed dim
|
||||
x = checkpoint(self.project_in, x)
|
||||
|
||||
# Project to increase channel dim
|
||||
x = checkpoint(self.project_up, x)
|
||||
|
||||
# Trade channels for sequence length
|
||||
x = rearrange(x, "b n (c r) -> b (n r) c", r=self.upsample_ratio)
|
||||
|
||||
# Compute
|
||||
x = self.transformer(x)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class TransformerEncoder1D(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
in_channels,
|
||||
out_channels,
|
||||
embed_dims = [96, 192, 384, 768],
|
||||
heads = [12, 12, 12, 12],
|
||||
depths = [3, 3, 3, 3],
|
||||
ratios = [2, 2, 2, 2],
|
||||
local_attn_window_size = 64,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
layers = []
|
||||
|
||||
for layer in range(len(depths)):
|
||||
prev_dim = embed_dims[layer - 1] if layer > 0 else embed_dims[0]
|
||||
|
||||
layers.append(
|
||||
TransformerDownsampleBlock1D(
|
||||
in_channels = prev_dim,
|
||||
embed_dim = embed_dims[layer],
|
||||
heads = heads[layer],
|
||||
depth = depths[layer],
|
||||
downsample_ratio = ratios[layer],
|
||||
local_attn_window_size = local_attn_window_size,
|
||||
**kwargs
|
||||
)
|
||||
)
|
||||
|
||||
self.layers = nn.Sequential(*layers)
|
||||
|
||||
self.project_in = nn.Linear(in_channels, embed_dims[0], bias=False)
|
||||
self.project_out = nn.Linear(embed_dims[-1], out_channels, bias=False)
|
||||
|
||||
def forward(self, x):
|
||||
x = rearrange(x, "b c n -> b n c")
|
||||
x = checkpoint(self.project_in, x)
|
||||
x = self.layers(x)
|
||||
x = checkpoint(self.project_out, x)
|
||||
x = rearrange(x, "b n c -> b c n")
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class TransformerDecoder1D(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
in_channels,
|
||||
out_channels,
|
||||
embed_dims = [768, 384, 192, 96],
|
||||
heads = [12, 12, 12, 12],
|
||||
depths = [3, 3, 3, 3],
|
||||
ratios = [2, 2, 2, 2],
|
||||
local_attn_window_size = 64,
|
||||
**kwargs
|
||||
):
|
||||
|
||||
super().__init__()
|
||||
|
||||
layers = []
|
||||
|
||||
for layer in range(len(depths)):
|
||||
prev_dim = embed_dims[layer - 1] if layer > 0 else embed_dims[0]
|
||||
|
||||
layers.append(
|
||||
TransformerUpsampleBlock1D(
|
||||
in_channels = prev_dim,
|
||||
embed_dim = embed_dims[layer],
|
||||
heads = heads[layer],
|
||||
depth = depths[layer],
|
||||
upsample_ratio = ratios[layer],
|
||||
local_attn_window_size = local_attn_window_size,
|
||||
**kwargs
|
||||
)
|
||||
)
|
||||
|
||||
self.layers = nn.Sequential(*layers)
|
||||
|
||||
self.project_in = nn.Linear(in_channels, embed_dims[0], bias=False)
|
||||
self.project_out = nn.Linear(embed_dims[-1], out_channels, bias=False)
|
||||
|
||||
def forward(self, x):
|
||||
x = rearrange(x, "b c n -> b n c")
|
||||
x = checkpoint(self.project_in, x)
|
||||
x = self.layers(x)
|
||||
x = checkpoint(self.project_out, x)
|
||||
x = rearrange(x, "b n c -> b c n")
|
||||
return x
|
||||
@@ -0,0 +1,578 @@
|
||||
import logging
|
||||
from dataclasses import dataclass
|
||||
from typing import Optional
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
import sys
|
||||
from .embeddings import compute_rope_rotations
|
||||
from .embeddings import TimestepEmbedder
|
||||
from .blocks import MLP, ChannelLastConv1d, ConvMLP
|
||||
from .transformer_layers import (FinalBlock, JointBlock, MMDitSingleBlock)
|
||||
from .utils import resample
|
||||
|
||||
log = logging.getLogger()
|
||||
|
||||
|
||||
@dataclass
|
||||
class PreprocessedConditions:
|
||||
clip_f: torch.Tensor
|
||||
sync_f: torch.Tensor
|
||||
text_f: torch.Tensor
|
||||
clip_f_c: torch.Tensor
|
||||
text_f_c: torch.Tensor
|
||||
|
||||
|
||||
class MMmodule(nn.Module):
|
||||
|
||||
def __init__(self,
|
||||
*,
|
||||
latent_dim: int,
|
||||
clip_dim: int,
|
||||
sync_dim: int,
|
||||
text_dim: int,
|
||||
hidden_dim: int,
|
||||
depth: int,
|
||||
fused_depth: int,
|
||||
num_heads: int,
|
||||
mlp_ratio: float = 4.0,
|
||||
latent_seq_len: int,
|
||||
clip_seq_len: int,
|
||||
sync_seq_len: int,
|
||||
text_seq_len: int = 77,
|
||||
latent_mean: Optional[torch.Tensor] = None,
|
||||
latent_std: Optional[torch.Tensor] = None,
|
||||
empty_string_feat: Optional[torch.Tensor] = None,
|
||||
v2: bool = False,
|
||||
kernel_size: int = 7,
|
||||
sync_kernel: int = 7,
|
||||
use_inpaint: bool = False,
|
||||
use_mlp: bool = False,
|
||||
cross_attend: bool = False,
|
||||
add_video: bool = False,
|
||||
triple_fusion: bool = False,
|
||||
gated_video: bool = False) -> None:
|
||||
super().__init__()
|
||||
|
||||
self.v2 = v2
|
||||
self.latent_dim = latent_dim
|
||||
self._latent_seq_len = latent_seq_len
|
||||
self._clip_seq_len = clip_seq_len
|
||||
self._sync_seq_len = sync_seq_len
|
||||
self._text_seq_len = text_seq_len
|
||||
self.hidden_dim = hidden_dim
|
||||
self.num_heads = num_heads
|
||||
self.cross_attend = cross_attend
|
||||
self.add_video = add_video
|
||||
self.gated_video = gated_video
|
||||
self.triple_fusion = triple_fusion
|
||||
self.use_inpaint = use_inpaint
|
||||
if self.gated_video:
|
||||
self.gated_mlp = nn.Sequential(
|
||||
nn.LayerNorm(hidden_dim * 2),
|
||||
nn.Linear(hidden_dim*2, hidden_dim * 4, bias=False),
|
||||
nn.SiLU(),
|
||||
nn.Linear(hidden_dim * 4, hidden_dim, bias=False),
|
||||
nn.Sigmoid()
|
||||
)
|
||||
# 初始化最后一层权重为零,促进初始均匀融合
|
||||
nn.init.zeros_(self.gated_mlp[3].weight)
|
||||
if self.triple_fusion:
|
||||
self.gated_mlp_v = nn.Sequential(
|
||||
nn.LayerNorm(hidden_dim * 3),
|
||||
nn.Linear(hidden_dim*3, hidden_dim * 4, bias=False),
|
||||
nn.SiLU(),
|
||||
nn.Linear(hidden_dim * 4, hidden_dim, bias=False),
|
||||
nn.Sigmoid()
|
||||
)
|
||||
self.gated_mlp_t = nn.Sequential(
|
||||
nn.LayerNorm(hidden_dim * 3),
|
||||
nn.Linear(hidden_dim*3, hidden_dim * 4, bias=False),
|
||||
nn.SiLU(),
|
||||
nn.Linear(hidden_dim * 4, hidden_dim, bias=False),
|
||||
nn.Sigmoid()
|
||||
)
|
||||
nn.init.zeros_(self.gated_mlp_v[3].weight)
|
||||
nn.init.zeros_(self.gated_mlp_t[3].weight)
|
||||
if v2:
|
||||
padding_size = (kernel_size - 1) // 2
|
||||
if use_inpaint:
|
||||
self.audio_input_proj = nn.Sequential(
|
||||
ChannelLastConv1d(latent_dim*2, hidden_dim, kernel_size=kernel_size, padding=padding_size),
|
||||
nn.SiLU(),
|
||||
ConvMLP(hidden_dim, hidden_dim * 4, kernel_size=kernel_size, padding=padding_size),
|
||||
)
|
||||
else:
|
||||
self.audio_input_proj = nn.Sequential(
|
||||
ChannelLastConv1d(latent_dim, hidden_dim, kernel_size=kernel_size, padding=padding_size),
|
||||
nn.SiLU(),
|
||||
ConvMLP(hidden_dim, hidden_dim * 4, kernel_size=kernel_size, padding=padding_size),
|
||||
)
|
||||
|
||||
self.clip_input_proj = nn.Sequential(
|
||||
nn.Linear(clip_dim, hidden_dim),
|
||||
nn.SiLU(),
|
||||
ConvMLP(hidden_dim, hidden_dim * 4, kernel_size=3, padding=1),
|
||||
)
|
||||
sync_pad = (sync_kernel - 1) // 2
|
||||
self.sync_input_proj = nn.Sequential(
|
||||
ChannelLastConv1d(sync_dim, hidden_dim, kernel_size=sync_kernel, padding=sync_pad),
|
||||
nn.SiLU(),
|
||||
ConvMLP(hidden_dim, hidden_dim * 4, kernel_size=3, padding=1),
|
||||
)
|
||||
|
||||
self.text_input_proj = nn.Sequential(
|
||||
nn.Linear(text_dim, hidden_dim),
|
||||
nn.SiLU(),
|
||||
MLP(hidden_dim, hidden_dim * 4),
|
||||
)
|
||||
else:
|
||||
self.audio_input_proj = nn.Sequential(
|
||||
ChannelLastConv1d(latent_dim, hidden_dim, kernel_size=7, padding=3),
|
||||
nn.SELU(),
|
||||
ConvMLP(hidden_dim, hidden_dim * 4, kernel_size=7, padding=3),
|
||||
)
|
||||
|
||||
self.clip_input_proj = nn.Sequential(
|
||||
nn.Linear(clip_dim, hidden_dim),
|
||||
ConvMLP(hidden_dim, hidden_dim * 4, kernel_size=3, padding=1),
|
||||
)
|
||||
|
||||
self.sync_input_proj = nn.Sequential(
|
||||
ChannelLastConv1d(sync_dim, hidden_dim, kernel_size=7, padding=3),
|
||||
nn.SELU(),
|
||||
ConvMLP(hidden_dim, hidden_dim * 4, kernel_size=3, padding=1),
|
||||
)
|
||||
|
||||
self.text_input_proj = nn.Sequential(
|
||||
nn.Linear(text_dim, hidden_dim),
|
||||
MLP(hidden_dim, hidden_dim * 4),
|
||||
)
|
||||
|
||||
self.clip_cond_proj = nn.Linear(hidden_dim, hidden_dim)
|
||||
if use_mlp:
|
||||
self.text_cond_proj = nn.Sequential(
|
||||
nn.Linear(1024, hidden_dim),
|
||||
MLP(hidden_dim, hidden_dim * 4),
|
||||
)
|
||||
else:
|
||||
self.text_cond_proj = nn.Linear(1024, hidden_dim)
|
||||
self.global_cond_mlp = MLP(hidden_dim, hidden_dim * 4)
|
||||
# each synchformer output segment has 8 feature frames
|
||||
self.sync_pos_emb = nn.Parameter(torch.zeros((1, 1, 8, sync_dim)))
|
||||
|
||||
self.final_layer = FinalBlock(hidden_dim, latent_dim)
|
||||
|
||||
if v2:
|
||||
self.t_embed = TimestepEmbedder(hidden_dim,
|
||||
frequency_embedding_size=hidden_dim,
|
||||
max_period=1)
|
||||
else:
|
||||
self.t_embed = TimestepEmbedder(hidden_dim,
|
||||
frequency_embedding_size=256,
|
||||
max_period=10000)
|
||||
self.joint_blocks = nn.ModuleList([
|
||||
JointBlock(hidden_dim,
|
||||
num_heads,
|
||||
mlp_ratio=mlp_ratio,
|
||||
pre_only=(i == depth - fused_depth - 1)) for i in range(depth - fused_depth)
|
||||
])
|
||||
|
||||
self.fused_blocks = nn.ModuleList([
|
||||
MMDitSingleBlock(hidden_dim, num_heads, mlp_ratio=mlp_ratio, kernel_size=kernel_size, padding=padding_size, cross_attend=cross_attend)
|
||||
for i in range(fused_depth)
|
||||
])
|
||||
|
||||
if empty_string_feat is None:
|
||||
empty_string_feat = torch.zeros((77, 1024))
|
||||
|
||||
empty_t5_feat = torch.zeros((77, 2048))
|
||||
|
||||
self.empty_string_feat = nn.Parameter(empty_string_feat, requires_grad=False)
|
||||
self.empty_t5_feat = nn.Parameter(empty_t5_feat, requires_grad=False)
|
||||
self.empty_clip_feat = nn.Parameter(torch.zeros(1, clip_dim), requires_grad=True)
|
||||
self.empty_sync_feat = nn.Parameter(torch.zeros(1, sync_dim), requires_grad=True)
|
||||
|
||||
self.initialize_weights()
|
||||
self.initialize_rotations()
|
||||
|
||||
def initialize_rotations(self):
|
||||
base_freq = 1.0
|
||||
latent_rot = compute_rope_rotations(self._latent_seq_len,
|
||||
self.hidden_dim // self.num_heads,
|
||||
10000,
|
||||
freq_scaling=base_freq,
|
||||
device=self.device)
|
||||
clip_rot = compute_rope_rotations(self._clip_seq_len,
|
||||
self.hidden_dim // self.num_heads,
|
||||
10000,
|
||||
freq_scaling=base_freq * self._latent_seq_len /
|
||||
self._clip_seq_len,
|
||||
device=self.device)
|
||||
|
||||
self.register_buffer('latent_rot', latent_rot, persistent=False)
|
||||
self.register_buffer('clip_rot', clip_rot, persistent=False)
|
||||
|
||||
def update_seq_lengths(self, latent_seq_len: int, clip_seq_len: int, sync_seq_len: int) -> None:
|
||||
self._latent_seq_len = latent_seq_len
|
||||
self._clip_seq_len = clip_seq_len
|
||||
self._sync_seq_len = sync_seq_len
|
||||
self.initialize_rotations()
|
||||
|
||||
def initialize_weights(self):
|
||||
|
||||
def _basic_init(module):
|
||||
if isinstance(module, nn.Linear):
|
||||
torch.nn.init.xavier_uniform_(module.weight)
|
||||
if module.bias is not None:
|
||||
nn.init.constant_(module.bias, 0)
|
||||
|
||||
self.apply(_basic_init)
|
||||
|
||||
# Initialize timestep embedding MLP:
|
||||
nn.init.normal_(self.t_embed.mlp[0].weight, std=0.02)
|
||||
nn.init.normal_(self.t_embed.mlp[2].weight, std=0.02)
|
||||
|
||||
# Zero-out adaLN modulation layers in DiT blocks:
|
||||
for block in self.joint_blocks:
|
||||
nn.init.constant_(block.latent_block.adaLN_modulation[-1].weight, 0)
|
||||
nn.init.constant_(block.latent_block.adaLN_modulation[-1].bias, 0)
|
||||
nn.init.constant_(block.clip_block.adaLN_modulation[-1].weight, 0)
|
||||
nn.init.constant_(block.clip_block.adaLN_modulation[-1].bias, 0)
|
||||
nn.init.constant_(block.text_block.adaLN_modulation[-1].weight, 0)
|
||||
nn.init.constant_(block.text_block.adaLN_modulation[-1].bias, 0)
|
||||
for block in self.fused_blocks:
|
||||
nn.init.constant_(block.adaLN_modulation[-1].weight, 0)
|
||||
nn.init.constant_(block.adaLN_modulation[-1].bias, 0)
|
||||
|
||||
# Zero-out output layers:
|
||||
nn.init.constant_(self.final_layer.adaLN_modulation[-1].weight, 0)
|
||||
nn.init.constant_(self.final_layer.adaLN_modulation[-1].bias, 0)
|
||||
nn.init.constant_(self.final_layer.conv.weight, 0)
|
||||
nn.init.constant_(self.final_layer.conv.bias, 0)
|
||||
|
||||
# empty string feat shall be initialized by a CLIP encoder
|
||||
nn.init.constant_(self.sync_pos_emb, 0)
|
||||
nn.init.constant_(self.empty_clip_feat, 0)
|
||||
nn.init.constant_(self.empty_sync_feat, 0)
|
||||
|
||||
def preprocess_conditions(self, clip_f: torch.Tensor, sync_f: torch.Tensor,
|
||||
text_f: torch.Tensor, t5_features: torch.Tensor, metaclip_global_text_features: torch.Tensor) -> PreprocessedConditions:
|
||||
"""
|
||||
cache computations that do not depend on the latent/time step
|
||||
i.e., the features are reused over steps during inference
|
||||
"""
|
||||
# breakpoint()
|
||||
assert clip_f.shape[1] == self._clip_seq_len, f'{clip_f.shape=} {self._clip_seq_len=}'
|
||||
assert sync_f.shape[1] == self._sync_seq_len, f'{sync_f.shape=} {self._sync_seq_len=}'
|
||||
assert text_f.shape[1] == self._text_seq_len, f'{text_f.shape=} {self._text_seq_len=}'
|
||||
|
||||
bs = clip_f.shape[0]
|
||||
|
||||
# B * num_segments (24) * 8 * 768
|
||||
num_sync_segments = self._sync_seq_len // 8
|
||||
sync_f = sync_f.view(bs, num_sync_segments, 8, -1) + self.sync_pos_emb
|
||||
sync_f = sync_f.flatten(1, 2) # (B, VN, D)
|
||||
|
||||
# extend vf to match x
|
||||
clip_f = self.clip_input_proj(clip_f) # (B, VN, D)
|
||||
sync_f = self.sync_input_proj(sync_f) # (B, VN, D)
|
||||
|
||||
if t5_features is not None:
|
||||
|
||||
if metaclip_global_text_features is not None:
|
||||
text_f_c = self.text_cond_proj(metaclip_global_text_features) # (B, D)
|
||||
else:
|
||||
text_f_c = self.text_cond_proj(text_f.mean(dim=1)) # (B, D)
|
||||
# 计算填充长度
|
||||
padding_size = t5_features.size(2) - text_f.size(2) # 渴望填充的数量
|
||||
# 当确实需要填充的时候,确保填充是正数
|
||||
if padding_size > 0:
|
||||
# 填充 text_f 的特征维度两侧
|
||||
text_f = F.pad(text_f, pad=(0, padding_size), mode='constant', value=0) # 在最后一个维度上进行填充
|
||||
else:
|
||||
text_f = text_f # 如果填充长度不是正数,则不需要填充
|
||||
text_concat = torch.cat((text_f, t5_features), dim=1)
|
||||
text_f = self.text_input_proj(text_concat) # (B, VN, D)
|
||||
else:
|
||||
text_f = self.text_input_proj(text_f) # (B, VN, D)
|
||||
if metaclip_global_text_features is not None:
|
||||
text_f_c = self.text_cond_proj(metaclip_global_text_features) # (B, D)
|
||||
else:
|
||||
text_f_c = self.text_cond_proj(text_f.mean(dim=1)) # (B, D)
|
||||
|
||||
# upsample the sync features to match the audio
|
||||
sync_f = sync_f.transpose(1, 2) # (B, D, VN)
|
||||
# sync_f = resample(sync_f, self._latent_seq_len)
|
||||
sync_f = F.interpolate(sync_f, size=self._latent_seq_len, mode='nearest-exact')
|
||||
sync_f = sync_f.transpose(1, 2) # (B, N, D)
|
||||
|
||||
# get conditional features from the clip side
|
||||
clip_f_c = self.clip_cond_proj(clip_f.mean(dim=1)) # (B, D)
|
||||
|
||||
return PreprocessedConditions(clip_f=clip_f,
|
||||
sync_f=sync_f,
|
||||
text_f=text_f,
|
||||
clip_f_c=clip_f_c,
|
||||
text_f_c=text_f_c)
|
||||
|
||||
def predict_flow(self, latent: torch.Tensor, t: torch.Tensor,
|
||||
conditions: PreprocessedConditions, inpaint_masked_input=None, cfg_scale:float=1.0,cfg_dropout_prob:float=0.0,scale_phi:float=0.0
|
||||
) -> torch.Tensor:
|
||||
"""
|
||||
for non-cacheable computations
|
||||
"""
|
||||
# print(f'cfg_scale: {cfg_scale}, cfg_dropout_prob: {cfg_dropout_prob}, scale_phi: {scale_phi}')
|
||||
assert latent.shape[1] == self._latent_seq_len, f'{latent.shape=} {self._latent_seq_len=}'
|
||||
empty_conditions = None
|
||||
if inpaint_masked_input is not None:
|
||||
inpaint_masked_input = inpaint_masked_input.transpose(1,2)
|
||||
clip_f = conditions.clip_f
|
||||
sync_f = conditions.sync_f
|
||||
text_f = conditions.text_f
|
||||
clip_f_c = conditions.clip_f_c
|
||||
text_f_c = conditions.text_f_c
|
||||
|
||||
# breakpoint()
|
||||
if inpaint_masked_input is not None:
|
||||
latent = torch.cat([latent,inpaint_masked_input],dim=2)
|
||||
latent = self.audio_input_proj(latent) # (B, N, D)
|
||||
global_c = self.global_cond_mlp(clip_f_c + text_f_c) # (B, D)
|
||||
# global_c = text_f_c
|
||||
global_c = self.t_embed(t).unsqueeze(1) + global_c.unsqueeze(1) # (B, D)
|
||||
extended_c = global_c + sync_f
|
||||
|
||||
for block in self.joint_blocks:
|
||||
latent, clip_f, text_f = block(latent, clip_f, text_f, global_c, extended_c,
|
||||
self.latent_rot, self.clip_rot) # (B, N, D)
|
||||
if self.add_video:
|
||||
if clip_f.shape[1] != latent.shape[1]:
|
||||
clip_f = resample(clip_f, latent)
|
||||
|
||||
if self.triple_fusion:
|
||||
text_f = torch.mean(text_f, dim=1, keepdim=True) # (bsz, 1, D)
|
||||
text_f = text_f.expand(-1,latent.shape[1], -1) # (T_audio, D)
|
||||
fusion = torch.concat((latent, clip_f, text_f),dim=-1)
|
||||
gate_v = self.gated_mlp_v(fusion)
|
||||
gate_t = self.gated_mlp_t(fusion)
|
||||
# modulated_latent = gate * latent # 非对称设计
|
||||
latent = latent + gate_v * clip_f + gate_t * text_f
|
||||
elif self.gated_video:
|
||||
fusion = torch.concat((latent, clip_f),dim=-1)
|
||||
gate = self.gated_mlp(fusion)
|
||||
modulated_latent = gate * latent # 非对称设计
|
||||
latent = latent + modulated_latent
|
||||
else:
|
||||
latent = latent + clip_f
|
||||
|
||||
for block in self.fused_blocks:
|
||||
if self.cross_attend:
|
||||
latent = block(latent, extended_c, self.latent_rot, context=text_f)
|
||||
else:
|
||||
latent = block(latent, extended_c, self.latent_rot)
|
||||
|
||||
# should be extended_c; this is a minor implementation error #55
|
||||
flow = self.final_layer(latent, extended_c) # (B, N, out_dim), remove t
|
||||
return flow
|
||||
|
||||
def forward(self, latent: torch.Tensor, t: torch.Tensor, clip_f: torch.Tensor, sync_f: torch.Tensor,
|
||||
text_f: torch.Tensor, inpaint_masked_input, t5_features, metaclip_global_text_features, cfg_scale:float,cfg_dropout_prob:float,scale_phi:float) -> torch.Tensor:
|
||||
"""
|
||||
latent: (B, N, C)
|
||||
vf: (B, T, C_V)
|
||||
t: (B,)
|
||||
"""
|
||||
# breakpoint()
|
||||
# print(f'cfg_scale: {cfg_scale}, cfg_dropout_prob: {cfg_dropout_prob}, scale_phi: {scale_phi}')
|
||||
if self.use_inpaint and inpaint_masked_input is None:
|
||||
inpaint_masked_input = torch.zeros_like(latent, device=latent.device)
|
||||
latent = latent.permute(0, 2, 1)
|
||||
|
||||
if cfg_dropout_prob > 0.0:
|
||||
if inpaint_masked_input is not None:
|
||||
null_embed = torch.zeros_like(inpaint_masked_input,device=latent.device)
|
||||
dropout_mask = torch.bernoulli(torch.full((inpaint_masked_input.shape[0], 1, 1), cfg_dropout_prob, device=latent.device)).to(torch.bool)
|
||||
inpaint_masked_input = torch.where(dropout_mask, null_embed, inpaint_masked_input)
|
||||
|
||||
null_embed = torch.zeros_like(clip_f,device=latent.device)
|
||||
dropout_mask = torch.bernoulli(torch.full((clip_f.shape[0], 1, 1), cfg_dropout_prob, device=latent.device)).to(torch.bool)
|
||||
# clip_f = torch.where(dropout_mask, null_embed, clip_f)
|
||||
clip_f = torch.where(dropout_mask, self.empty_clip_feat, clip_f)
|
||||
null_embed = torch.zeros_like(sync_f,device=latent.device)
|
||||
dropout_mask = torch.bernoulli(torch.full((sync_f.shape[0], 1, 1), cfg_dropout_prob, device=latent.device)).to(torch.bool)
|
||||
# sync_f = torch.where(dropout_mask, null_embed, sync_f)
|
||||
sync_f = torch.where(dropout_mask, self.empty_sync_feat, sync_f)
|
||||
null_embed = torch.zeros_like(text_f,device=latent.device)
|
||||
dropout_mask = torch.bernoulli(torch.full((text_f.shape[0], 1, 1), cfg_dropout_prob, device=latent.device)).to(torch.bool)
|
||||
# text_f = torch.where(dropout_mask, null_embed, text_f)
|
||||
text_f = torch.where(dropout_mask, self.empty_string_feat, text_f)
|
||||
if t5_features is not None:
|
||||
null_embed = torch.zeros_like(t5_features,device=latent.device)
|
||||
dropout_mask = torch.bernoulli(torch.full((t5_features.shape[0], 1, 1), cfg_dropout_prob, device=latent.device)).to(torch.bool)
|
||||
# t5_features = torch.where(dropout_mask, null_embed, t5_features)
|
||||
t5_features = torch.where(dropout_mask, self.empty_t5_feat, t5_features)
|
||||
if metaclip_global_text_features is not None:
|
||||
null_embed = torch.zeros_like(metaclip_global_text_features,device=latent.device)
|
||||
dropout_mask = torch.bernoulli(torch.full((metaclip_global_text_features.shape[0], 1), cfg_dropout_prob, device=latent.device)).to(torch.bool)
|
||||
metaclip_global_text_features = torch.where(dropout_mask, null_embed, metaclip_global_text_features)
|
||||
# null_embed = torch.zeros_like(clip_f_c,device=latent.device)
|
||||
# dropout_mask = torch.bernoulli(torch.full((clip_f_c.shape[0], 1), cfg_dropout_prob, device=latent.device)).to(torch.bool)
|
||||
# clip_f_c = torch.where(dropout_mask, null_embed, clip_f_c)
|
||||
# null_embed = torch.zeros_like(text_f_c,device=latent.device)
|
||||
# dropout_mask = torch.bernoulli(torch.full((text_f_c.shape[0], 1), cfg_dropout_prob, device=latent.device)).to(torch.bool)
|
||||
# text_f_c = torch.where(dropout_mask, null_embed, text_f_c)
|
||||
|
||||
if cfg_scale != 1.0:
|
||||
# empty_conditions = self.get_empty_conditions(latent.shape[0])
|
||||
# breakpoint()
|
||||
bsz = latent.shape[0]
|
||||
latent = torch.cat([latent,latent], dim=0)
|
||||
if inpaint_masked_input is not None:
|
||||
empty_inpaint_masked_input = torch.zeros_like(inpaint_masked_input, device=latent.device)
|
||||
inpaint_masked_input = torch.cat([inpaint_masked_input,empty_inpaint_masked_input], dim=0)
|
||||
t = torch.cat([t, t], dim=0)
|
||||
empty_clip_f = torch.zeros_like(clip_f, device=latent.device)
|
||||
empty_sync_f = torch.zeros_like(sync_f, device=latent.device)
|
||||
empty_text_f = torch.zeros_like(text_f, device=latent.device)
|
||||
|
||||
# clip_f = torch.cat([clip_f,empty_clip_f], dim=0)
|
||||
# sync_f = torch.cat([sync_f,empty_sync_f], dim=0)
|
||||
# text_f = torch.cat([text_f,empty_text_f], dim=0)
|
||||
clip_f = safe_cat(clip_f,self.get_empty_clip_sequence(bsz), dim=0, match_dim=1)
|
||||
sync_f = safe_cat(sync_f,self.get_empty_sync_sequence(bsz), dim=0, match_dim=1)
|
||||
text_f = safe_cat(text_f,self.get_empty_string_sequence(bsz), dim=0, match_dim=1)
|
||||
if t5_features is not None:
|
||||
empty_t5_features = torch.zeros_like(t5_features, device=latent.device)
|
||||
# t5_features = torch.cat([t5_features,empty_t5_features], dim=0)
|
||||
t5_features = torch.cat([t5_features,self.get_empty_t5_sequence(bsz)], dim=0)
|
||||
if metaclip_global_text_features is not None:
|
||||
empty_metaclip_global_text_features = torch.zeros_like(metaclip_global_text_features, device=latent.device)
|
||||
metaclip_global_text_features = torch.cat([metaclip_global_text_features,empty_metaclip_global_text_features], dim=0)
|
||||
# metaclip_global_text_features = torch.cat([metaclip_global_text_features,metaclip_global_text_features], dim=0)
|
||||
# clip_f_c = torch.cat([clip_f_c,empty_clip_f_c], dim=0)
|
||||
# text_f_c = torch.cat([text_f_c,empty_text_f_c], dim=0)
|
||||
|
||||
conditions = self.preprocess_conditions(clip_f, sync_f, text_f, t5_features, metaclip_global_text_features)
|
||||
flow = self.predict_flow(latent, t, conditions, inpaint_masked_input, cfg_scale,cfg_dropout_prob,scale_phi)
|
||||
if cfg_scale != 1.0:
|
||||
cond_output, uncond_output = torch.chunk(flow, 2, dim=0)
|
||||
cfg_output = uncond_output + (cond_output - uncond_output) * cfg_scale
|
||||
if scale_phi != 0.0:
|
||||
cond_out_std = cond_output.std(dim=1, keepdim=True)
|
||||
out_cfg_std = cfg_output.std(dim=1, keepdim=True)
|
||||
flow = scale_phi * (cfg_output * (cond_out_std/out_cfg_std)) + (1-scale_phi) * cfg_output
|
||||
else:
|
||||
flow = cfg_output
|
||||
flow = flow.permute(0, 2, 1)
|
||||
return flow
|
||||
|
||||
def get_empty_string_sequence(self, bs: int) -> torch.Tensor:
|
||||
return self.empty_string_feat.unsqueeze(0).expand(bs, -1, -1)
|
||||
|
||||
def get_empty_t5_sequence(self, bs: int) -> torch.Tensor:
|
||||
return self.empty_t5_feat.unsqueeze(0).expand(bs, -1, -1)
|
||||
|
||||
def get_empty_clip_sequence(self, bs: int) -> torch.Tensor:
|
||||
return self.empty_clip_feat.unsqueeze(0).expand(bs, self._clip_seq_len, -1)
|
||||
|
||||
def get_empty_sync_sequence(self, bs: int) -> torch.Tensor:
|
||||
return self.empty_sync_feat.unsqueeze(0).expand(bs, self._sync_seq_len, -1)
|
||||
|
||||
def get_empty_conditions(
|
||||
self,
|
||||
bs: int,
|
||||
*,
|
||||
negative_text_features: Optional[torch.Tensor] = None) -> PreprocessedConditions:
|
||||
if negative_text_features is not None:
|
||||
empty_text = negative_text_features
|
||||
else:
|
||||
empty_text = self.get_empty_string_sequence(1)
|
||||
|
||||
empty_clip = self.get_empty_clip_sequence(1)
|
||||
empty_sync = self.get_empty_sync_sequence(1)
|
||||
conditions = self.preprocess_conditions(empty_clip, empty_sync, empty_text)
|
||||
conditions.clip_f = conditions.clip_f.expand(bs, -1, -1)
|
||||
conditions.sync_f = conditions.sync_f.expand(bs, -1, -1)
|
||||
conditions.clip_f_c = conditions.clip_f_c.expand(bs, -1)
|
||||
if negative_text_features is None:
|
||||
conditions.text_f = conditions.text_f.expand(bs, -1, -1)
|
||||
conditions.text_f_c = conditions.text_f_c.expand(bs, -1)
|
||||
|
||||
return conditions
|
||||
|
||||
def load_weights(self, src_dict) -> None:
|
||||
if 't_embed.freqs' in src_dict:
|
||||
del src_dict['t_embed.freqs']
|
||||
if 'latent_rot' in src_dict:
|
||||
del src_dict['latent_rot']
|
||||
if 'clip_rot' in src_dict:
|
||||
del src_dict['clip_rot']
|
||||
|
||||
self.load_state_dict(src_dict, strict=True)
|
||||
|
||||
@property
|
||||
def device(self) -> torch.device:
|
||||
return self.empty_clip_feat.device
|
||||
|
||||
@property
|
||||
def latent_seq_len(self) -> int:
|
||||
return self._latent_seq_len
|
||||
|
||||
@property
|
||||
def clip_seq_len(self) -> int:
|
||||
return self._clip_seq_len
|
||||
|
||||
@property
|
||||
def sync_seq_len(self) -> int:
|
||||
return self._sync_seq_len
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def truncate_to_target(tensor, target_size, dim=1):
|
||||
current_size = tensor.size(dim)
|
||||
if current_size > target_size:
|
||||
slices = [slice(None)] * tensor.dim()
|
||||
slices[dim] = slice(0, target_size)
|
||||
return tensor[slices]
|
||||
return tensor
|
||||
|
||||
def pad_to_target(tensor, target_size, dim=1, pad_value=0):
|
||||
current_size = tensor.size(dim)
|
||||
if current_size < target_size:
|
||||
pad_size = target_size - current_size
|
||||
|
||||
pad_config = [0, 0] * tensor.dim()
|
||||
pad_index = 2 * (tensor.dim() - dim - 1) + 1
|
||||
pad_config[pad_index] = pad_size
|
||||
|
||||
return torch.nn.functional.pad(tensor, pad_config, value=pad_value)
|
||||
return tensor
|
||||
|
||||
|
||||
def safe_cat(tensor1, tensor2, dim=0, match_dim=1):
|
||||
|
||||
target_size = tensor2.size(match_dim)
|
||||
|
||||
if tensor1.size(match_dim) > target_size:
|
||||
tensor1 = truncate_to_target(tensor1, target_size, match_dim)
|
||||
|
||||
else:
|
||||
tensor1 = pad_to_target(tensor1, target_size, match_dim)
|
||||
|
||||
return torch.cat([tensor1, tensor2], dim=dim)
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
import json
|
||||
|
||||
from .factory import create_model_from_config
|
||||
from .utils import load_ckpt_state_dict
|
||||
|
||||
from huggingface_hub import hf_hub_download
|
||||
|
||||
def get_pretrained_model(name: str):
|
||||
|
||||
model_config_path = hf_hub_download(name, filename="model_config.json", repo_type='model')
|
||||
|
||||
with open(model_config_path) as f:
|
||||
model_config = json.load(f)
|
||||
|
||||
model = create_model_from_config(model_config)
|
||||
|
||||
# Try to download the model.safetensors file first, if it doesn't exist, download the model.ckpt file
|
||||
try:
|
||||
model_ckpt_path = hf_hub_download(name, filename="model.safetensors", repo_type='model')
|
||||
except Exception as e:
|
||||
model_ckpt_path = hf_hub_download(name, filename="model.ckpt", repo_type='model')
|
||||
|
||||
model.load_state_dict(load_ckpt_state_dict(model_ckpt_path))
|
||||
|
||||
return model, model_config
|
||||
@@ -0,0 +1,258 @@
|
||||
import torch
|
||||
from einops import rearrange
|
||||
from torch import nn
|
||||
|
||||
class Pretransform(nn.Module):
|
||||
def __init__(self, enable_grad, io_channels, is_discrete):
|
||||
super().__init__()
|
||||
|
||||
self.is_discrete = is_discrete
|
||||
self.io_channels = io_channels
|
||||
self.encoded_channels = None
|
||||
self.downsampling_ratio = None
|
||||
|
||||
self.enable_grad = enable_grad
|
||||
|
||||
def encode(self, x):
|
||||
raise NotImplementedError
|
||||
|
||||
def decode(self, z):
|
||||
raise NotImplementedError
|
||||
|
||||
def tokenize(self, x):
|
||||
raise NotImplementedError
|
||||
|
||||
def decode_tokens(self, tokens):
|
||||
raise NotImplementedError
|
||||
|
||||
class AutoencoderPretransform(Pretransform):
|
||||
def __init__(self, model, scale=1.0, model_half=False, iterate_batch=False, chunked=False):
|
||||
super().__init__(enable_grad=False, io_channels=model.io_channels, is_discrete=model.bottleneck is not None and model.bottleneck.is_discrete)
|
||||
self.model = model
|
||||
self.model.requires_grad_(False).eval()
|
||||
self.scale=scale
|
||||
self.downsampling_ratio = model.downsampling_ratio
|
||||
self.io_channels = model.io_channels
|
||||
self.sample_rate = model.sample_rate
|
||||
|
||||
self.model_half = model_half
|
||||
self.iterate_batch = iterate_batch
|
||||
|
||||
self.encoded_channels = model.latent_dim
|
||||
|
||||
self.chunked = chunked
|
||||
self.num_quantizers = model.bottleneck.num_quantizers if model.bottleneck is not None and model.bottleneck.is_discrete else None
|
||||
self.codebook_size = model.bottleneck.codebook_size if model.bottleneck is not None and model.bottleneck.is_discrete else None
|
||||
|
||||
if self.model_half:
|
||||
self.model.half()
|
||||
|
||||
def encode(self, x, **kwargs):
|
||||
|
||||
if self.model_half:
|
||||
x = x.half()
|
||||
self.model.to(torch.float16)
|
||||
|
||||
encoded = self.model.encode_audio(x, chunked=self.chunked, iterate_batch=self.iterate_batch, **kwargs)
|
||||
|
||||
if self.model_half:
|
||||
encoded = encoded.float()
|
||||
|
||||
return encoded / self.scale
|
||||
|
||||
def decode(self, z, **kwargs):
|
||||
z = z * self.scale
|
||||
|
||||
if self.model_half:
|
||||
z = z.half()
|
||||
self.model.to(torch.float16)
|
||||
|
||||
decoded = self.model.decode_audio(z, chunked=self.chunked, iterate_batch=self.iterate_batch, **kwargs)
|
||||
|
||||
if self.model_half:
|
||||
decoded = decoded.float()
|
||||
|
||||
return decoded
|
||||
|
||||
def tokenize(self, x, **kwargs):
|
||||
assert self.model.is_discrete, "Cannot tokenize with a continuous model"
|
||||
|
||||
_, info = self.model.encode(x, return_info = True, **kwargs)
|
||||
|
||||
return info[self.model.bottleneck.tokens_id]
|
||||
|
||||
def decode_tokens(self, tokens, **kwargs):
|
||||
assert self.model.is_discrete, "Cannot decode tokens with a continuous model"
|
||||
|
||||
return self.model.decode_tokens(tokens, **kwargs)
|
||||
|
||||
def load_state_dict(self, state_dict, strict=True):
|
||||
self.model.load_state_dict(state_dict, strict=strict)
|
||||
|
||||
class WaveletPretransform(Pretransform):
|
||||
def __init__(self, channels, levels, wavelet):
|
||||
super().__init__(enable_grad=False, io_channels=channels, is_discrete=False)
|
||||
|
||||
from .wavelets import WaveletEncode1d, WaveletDecode1d
|
||||
|
||||
self.encoder = WaveletEncode1d(channels, levels, wavelet)
|
||||
self.decoder = WaveletDecode1d(channels, levels, wavelet)
|
||||
|
||||
self.downsampling_ratio = 2 ** levels
|
||||
self.io_channels = channels
|
||||
self.encoded_channels = channels * self.downsampling_ratio
|
||||
|
||||
def encode(self, x):
|
||||
return self.encoder(x)
|
||||
|
||||
def decode(self, z):
|
||||
return self.decoder(z)
|
||||
|
||||
class PQMFPretransform(Pretransform):
|
||||
def __init__(self, attenuation=100, num_bands=16):
|
||||
# TODO: Fix PQMF to take in in-channels
|
||||
super().__init__(enable_grad=False, io_channels=1, is_discrete=False)
|
||||
from .pqmf import PQMF
|
||||
self.pqmf = PQMF(attenuation, num_bands)
|
||||
|
||||
|
||||
def encode(self, x):
|
||||
# x is (Batch x Channels x Time)
|
||||
x = self.pqmf.forward(x)
|
||||
# pqmf.forward returns (Batch x Channels x Bands x Time)
|
||||
# but Pretransform needs Batch x Channels x Time
|
||||
# so concatenate channels and bands into one axis
|
||||
return rearrange(x, "b c n t -> b (c n) t")
|
||||
|
||||
def decode(self, x):
|
||||
# x is (Batch x (Channels Bands) x Time), convert back to (Batch x Channels x Bands x Time)
|
||||
x = rearrange(x, "b (c n) t -> b c n t", n=self.pqmf.num_bands)
|
||||
# returns (Batch x Channels x Time)
|
||||
return self.pqmf.inverse(x)
|
||||
|
||||
class PretrainedDACPretransform(Pretransform):
|
||||
def __init__(self, model_type="44khz", model_bitrate="8kbps", scale=1.0, quantize_on_decode: bool = True, chunked=True):
|
||||
super().__init__(enable_grad=False, io_channels=1, is_discrete=True)
|
||||
|
||||
import dac
|
||||
|
||||
model_path = dac.utils.download(model_type=model_type, model_bitrate=model_bitrate)
|
||||
|
||||
self.model = dac.DAC.load(model_path)
|
||||
|
||||
self.quantize_on_decode = quantize_on_decode
|
||||
|
||||
if model_type == "44khz":
|
||||
self.downsampling_ratio = 512
|
||||
else:
|
||||
self.downsampling_ratio = 320
|
||||
|
||||
self.io_channels = 1
|
||||
|
||||
self.scale = scale
|
||||
|
||||
self.chunked = chunked
|
||||
|
||||
self.encoded_channels = self.model.latent_dim
|
||||
|
||||
self.num_quantizers = self.model.n_codebooks
|
||||
|
||||
self.codebook_size = self.model.codebook_size
|
||||
|
||||
def encode(self, x):
|
||||
|
||||
latents = self.model.encoder(x)
|
||||
|
||||
if self.quantize_on_decode:
|
||||
output = latents
|
||||
else:
|
||||
z, _, _, _, _ = self.model.quantizer(latents, n_quantizers=self.model.n_codebooks)
|
||||
output = z
|
||||
|
||||
if self.scale != 1.0:
|
||||
output = output / self.scale
|
||||
|
||||
return output
|
||||
|
||||
def decode(self, z):
|
||||
|
||||
if self.scale != 1.0:
|
||||
z = z * self.scale
|
||||
|
||||
if self.quantize_on_decode:
|
||||
z, _, _, _, _ = self.model.quantizer(z, n_quantizers=self.model.n_codebooks)
|
||||
|
||||
return self.model.decode(z)
|
||||
|
||||
def tokenize(self, x):
|
||||
return self.model.encode(x)[1]
|
||||
|
||||
def decode_tokens(self, tokens):
|
||||
latents = self.model.quantizer.from_codes(tokens)
|
||||
return self.model.decode(latents)
|
||||
|
||||
class AudiocraftCompressionPretransform(Pretransform):
|
||||
def __init__(self, model_type="facebook/encodec_32khz", scale=1.0, quantize_on_decode: bool = True):
|
||||
super().__init__(enable_grad=False, io_channels=1, is_discrete=True)
|
||||
|
||||
try:
|
||||
from audiocraft.models import CompressionModel
|
||||
except ImportError:
|
||||
raise ImportError("Audiocraft is not installed. Please install audiocraft to use Audiocraft models.")
|
||||
|
||||
self.model = CompressionModel.get_pretrained(model_type)
|
||||
|
||||
self.quantize_on_decode = quantize_on_decode
|
||||
|
||||
self.downsampling_ratio = round(self.model.sample_rate / self.model.frame_rate)
|
||||
|
||||
self.sample_rate = self.model.sample_rate
|
||||
|
||||
self.io_channels = self.model.channels
|
||||
|
||||
self.scale = scale
|
||||
|
||||
#self.encoded_channels = self.model.latent_dim
|
||||
|
||||
self.num_quantizers = self.model.num_codebooks
|
||||
|
||||
self.codebook_size = self.model.cardinality
|
||||
|
||||
self.model.to(torch.float16).eval().requires_grad_(False)
|
||||
|
||||
def encode(self, x):
|
||||
|
||||
assert False, "Audiocraft compression models do not support continuous encoding"
|
||||
|
||||
# latents = self.model.encoder(x)
|
||||
|
||||
# if self.quantize_on_decode:
|
||||
# output = latents
|
||||
# else:
|
||||
# z, _, _, _, _ = self.model.quantizer(latents, n_quantizers=self.model.n_codebooks)
|
||||
# output = z
|
||||
|
||||
# if self.scale != 1.0:
|
||||
# output = output / self.scale
|
||||
|
||||
# return output
|
||||
|
||||
def decode(self, z):
|
||||
|
||||
assert False, "Audiocraft compression models do not support continuous decoding"
|
||||
|
||||
# if self.scale != 1.0:
|
||||
# z = z * self.scale
|
||||
|
||||
# if self.quantize_on_decode:
|
||||
# z, _, _, _, _ = self.model.quantizer(z, n_quantizers=self.model.n_codebooks)
|
||||
|
||||
# return self.model.decode(z)
|
||||
|
||||
def tokenize(self, x):
|
||||
with torch.cuda.amp.autocast(enabled=False):
|
||||
return self.model.encode(x.to(torch.float16))[0]
|
||||
|
||||
def decode_tokens(self, tokens):
|
||||
with torch.cuda.amp.autocast(enabled=False):
|
||||
return self.model.decode(tokens)
|
||||
@@ -0,0 +1,821 @@
|
||||
from functools import reduce, partial
|
||||
from packaging import version
|
||||
|
||||
from einops import rearrange, repeat
|
||||
from einops.layers.torch import Rearrange
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from torch import nn, einsum
|
||||
from torch.cuda.amp import autocast
|
||||
from typing import Callable, Literal
|
||||
|
||||
try:
|
||||
from flash_attn import flash_attn_func, flash_attn_kvpacked_func
|
||||
except ImportError as e:
|
||||
print(e)
|
||||
print('flash_attn not installed, disabling Flash Attention')
|
||||
flash_attn_kvpacked_func = None
|
||||
flash_attn_func = None
|
||||
|
||||
try:
|
||||
import natten
|
||||
except ImportError:
|
||||
natten = None
|
||||
|
||||
def checkpoint(function, *args, **kwargs):
|
||||
kwargs.setdefault("use_reentrant", False)
|
||||
return torch.utils.checkpoint.checkpoint(function, *args, **kwargs)
|
||||
|
||||
|
||||
# Copied and modified from https://github.com/lucidrains/x-transformers/blob/main/x_transformers/attend.py under MIT License
|
||||
# License can be found in LICENSES/LICENSE_XTRANSFORMERS.txt
|
||||
|
||||
def create_causal_mask(i, j, device):
|
||||
return torch.ones((i, j), device = device, dtype = torch.bool).triu(j - i + 1)
|
||||
|
||||
def or_reduce(masks):
|
||||
head, *body = masks
|
||||
for rest in body:
|
||||
head = head | rest
|
||||
return head
|
||||
|
||||
# positional embeddings
|
||||
|
||||
class AbsolutePositionalEmbedding(nn.Module):
|
||||
def __init__(self, dim, max_seq_len):
|
||||
super().__init__()
|
||||
self.scale = dim ** -0.5
|
||||
self.max_seq_len = max_seq_len
|
||||
self.emb = nn.Embedding(max_seq_len, dim)
|
||||
|
||||
def forward(self, x, pos = None, seq_start_pos = None):
|
||||
seq_len, device = x.shape[1], x.device
|
||||
assert seq_len <= self.max_seq_len, f'you are passing in a sequence length of {seq_len} but your absolute positional embedding has a max sequence length of {self.max_seq_len}'
|
||||
|
||||
if pos is None:
|
||||
pos = torch.arange(seq_len, device = device)
|
||||
|
||||
if seq_start_pos is not None:
|
||||
pos = (pos - seq_start_pos[..., None]).clamp(min = 0)
|
||||
|
||||
pos_emb = self.emb(pos)
|
||||
pos_emb = pos_emb * self.scale
|
||||
return pos_emb
|
||||
|
||||
class ScaledSinusoidalEmbedding(nn.Module):
|
||||
def __init__(self, dim, theta = 10000):
|
||||
super().__init__()
|
||||
assert (dim % 2) == 0, 'dimension must be divisible by 2'
|
||||
self.scale = nn.Parameter(torch.ones(1) * dim ** -0.5)
|
||||
|
||||
half_dim = dim // 2
|
||||
freq_seq = torch.arange(half_dim).float() / half_dim
|
||||
inv_freq = theta ** -freq_seq
|
||||
self.register_buffer('inv_freq', inv_freq, persistent = False)
|
||||
|
||||
def forward(self, x, pos = None, seq_start_pos = None):
|
||||
seq_len, device = x.shape[1], x.device
|
||||
|
||||
if pos is None:
|
||||
pos = torch.arange(seq_len, device = device)
|
||||
|
||||
if seq_start_pos is not None:
|
||||
pos = pos - seq_start_pos[..., None]
|
||||
|
||||
emb = einsum('i, j -> i j', pos, self.inv_freq)
|
||||
emb = torch.cat((emb.sin(), emb.cos()), dim = -1)
|
||||
return emb * self.scale
|
||||
|
||||
class RotaryEmbedding(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
dim,
|
||||
use_xpos = False,
|
||||
scale_base = 512,
|
||||
interpolation_factor = 1.,
|
||||
base = 10000,
|
||||
base_rescale_factor = 1.
|
||||
):
|
||||
super().__init__()
|
||||
# proposed by reddit user bloc97, to rescale rotary embeddings to longer sequence length without fine-tuning
|
||||
# has some connection to NTK literature
|
||||
# https://www.reddit.com/r/LocalLLaMA/comments/14lz7j5/ntkaware_scaled_rope_allows_llama_models_to_have/
|
||||
base *= base_rescale_factor ** (dim / (dim - 2))
|
||||
|
||||
inv_freq = 1. / (base ** (torch.arange(0, dim, 2).float() / dim))
|
||||
self.register_buffer('inv_freq', inv_freq)
|
||||
|
||||
assert interpolation_factor >= 1.
|
||||
self.interpolation_factor = interpolation_factor
|
||||
|
||||
if not use_xpos:
|
||||
self.register_buffer('scale', None)
|
||||
return
|
||||
|
||||
scale = (torch.arange(0, dim, 2) + 0.4 * dim) / (1.4 * dim)
|
||||
|
||||
self.scale_base = scale_base
|
||||
self.register_buffer('scale', scale)
|
||||
|
||||
def forward_from_seq_len(self, seq_len):
|
||||
device = self.inv_freq.device
|
||||
|
||||
t = torch.arange(seq_len, device = device)
|
||||
return self.forward(t)
|
||||
|
||||
@autocast(enabled = False)
|
||||
def forward(self, t):
|
||||
device = self.inv_freq.device
|
||||
|
||||
t = t.to(torch.float32)
|
||||
|
||||
t = t / self.interpolation_factor
|
||||
|
||||
freqs = torch.einsum('i , j -> i j', t, self.inv_freq)
|
||||
freqs = torch.cat((freqs, freqs), dim = -1)
|
||||
|
||||
if self.scale is None:
|
||||
return freqs, 1.
|
||||
|
||||
power = (torch.arange(seq_len, device = device) - (seq_len // 2)) / self.scale_base
|
||||
scale = self.scale ** rearrange(power, 'n -> n 1')
|
||||
scale = torch.cat((scale, scale), dim = -1)
|
||||
|
||||
return freqs, scale
|
||||
|
||||
def rotate_half(x):
|
||||
x = rearrange(x, '... (j d) -> ... j d', j = 2)
|
||||
x1, x2 = x.unbind(dim = -2)
|
||||
return torch.cat((-x2, x1), dim = -1)
|
||||
|
||||
@autocast(enabled = False)
|
||||
def apply_rotary_pos_emb(t, freqs, scale = 1):
|
||||
out_dtype = t.dtype
|
||||
|
||||
# cast to float32 if necessary for numerical stability
|
||||
dtype = reduce(torch.promote_types, (t.dtype, freqs.dtype, torch.float32))
|
||||
rot_dim, seq_len = freqs.shape[-1], t.shape[-2]
|
||||
freqs, t = freqs.to(dtype), t.to(dtype)
|
||||
freqs = freqs[-seq_len:, :]
|
||||
|
||||
if t.ndim == 4 and freqs.ndim == 3:
|
||||
freqs = rearrange(freqs, 'b n d -> b 1 n d')
|
||||
|
||||
# partial rotary embeddings, Wang et al. GPT-J
|
||||
t, t_unrotated = t[..., :rot_dim], t[..., rot_dim:]
|
||||
t = (t * freqs.cos() * scale) + (rotate_half(t) * freqs.sin() * scale)
|
||||
|
||||
t, t_unrotated = t.to(out_dtype), t_unrotated.to(out_dtype)
|
||||
|
||||
return torch.cat((t, t_unrotated), dim = -1)
|
||||
|
||||
# norms
|
||||
class LayerNorm(nn.Module):
|
||||
def __init__(self, dim, bias=False, fix_scale=False):
|
||||
"""
|
||||
bias-less layernorm has been shown to be more stable. most newer models have moved towards rmsnorm, also bias-less
|
||||
"""
|
||||
super().__init__()
|
||||
|
||||
if fix_scale:
|
||||
self.register_buffer("gamma", torch.ones(dim))
|
||||
else:
|
||||
self.gamma = nn.Parameter(torch.ones(dim))
|
||||
|
||||
if bias:
|
||||
self.beta = nn.Parameter(torch.zeros(dim))
|
||||
else:
|
||||
self.register_buffer("beta", torch.zeros(dim))
|
||||
|
||||
|
||||
def forward(self, x):
|
||||
return F.layer_norm(x, x.shape[-1:], weight=self.gamma, bias=self.beta)
|
||||
|
||||
# feedforward
|
||||
|
||||
class GLU(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
dim_in,
|
||||
dim_out,
|
||||
activation: Callable,
|
||||
use_conv = False,
|
||||
conv_kernel_size = 3,
|
||||
):
|
||||
super().__init__()
|
||||
self.act = activation
|
||||
self.proj = nn.Linear(dim_in, dim_out * 2) if not use_conv else nn.Conv1d(dim_in, dim_out * 2, conv_kernel_size, padding = (conv_kernel_size // 2))
|
||||
self.use_conv = use_conv
|
||||
|
||||
def forward(self, x):
|
||||
if self.use_conv:
|
||||
x = rearrange(x, 'b n d -> b d n')
|
||||
x = self.proj(x)
|
||||
x = rearrange(x, 'b d n -> b n d')
|
||||
else:
|
||||
x = self.proj(x)
|
||||
|
||||
x, gate = x.chunk(2, dim = -1)
|
||||
return x * self.act(gate)
|
||||
|
||||
class FeedForward(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
dim,
|
||||
dim_out = None,
|
||||
mult = 4,
|
||||
no_bias = False,
|
||||
glu = True,
|
||||
use_conv = False,
|
||||
conv_kernel_size = 3,
|
||||
zero_init_output = True,
|
||||
):
|
||||
super().__init__()
|
||||
inner_dim = int(dim * mult)
|
||||
|
||||
# Default to SwiGLU
|
||||
|
||||
activation = nn.SiLU()
|
||||
|
||||
dim_out = dim if dim_out is None else dim_out
|
||||
|
||||
if glu:
|
||||
linear_in = GLU(dim, inner_dim, activation)
|
||||
else:
|
||||
linear_in = nn.Sequential(
|
||||
Rearrange('b n d -> b d n') if use_conv else nn.Identity(),
|
||||
nn.Linear(dim, inner_dim, bias = not no_bias) if not use_conv else nn.Conv1d(dim, inner_dim, conv_kernel_size, padding = (conv_kernel_size // 2), bias = not no_bias),
|
||||
Rearrange('b n d -> b d n') if use_conv else nn.Identity(),
|
||||
activation
|
||||
)
|
||||
|
||||
linear_out = nn.Linear(inner_dim, dim_out, bias = not no_bias) if not use_conv else nn.Conv1d(inner_dim, dim_out, conv_kernel_size, padding = (conv_kernel_size // 2), bias = not no_bias)
|
||||
|
||||
# init last linear layer to 0
|
||||
if zero_init_output:
|
||||
nn.init.zeros_(linear_out.weight)
|
||||
if not no_bias:
|
||||
nn.init.zeros_(linear_out.bias)
|
||||
|
||||
|
||||
self.ff = nn.Sequential(
|
||||
linear_in,
|
||||
Rearrange('b d n -> b n d') if use_conv else nn.Identity(),
|
||||
linear_out,
|
||||
Rearrange('b n d -> b d n') if use_conv else nn.Identity(),
|
||||
)
|
||||
|
||||
def forward(self, x):
|
||||
return self.ff(x)
|
||||
|
||||
class Attention(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
dim,
|
||||
dim_heads = 64,
|
||||
dim_context = None,
|
||||
causal = False,
|
||||
zero_init_output=True,
|
||||
qk_norm: Literal['l2', 'ln', 'none'] = 'none',
|
||||
natten_kernel_size = None
|
||||
):
|
||||
super().__init__()
|
||||
self.dim = dim
|
||||
self.dim_heads = dim_heads
|
||||
self.causal = causal
|
||||
|
||||
dim_kv = dim_context if dim_context is not None else dim
|
||||
|
||||
self.num_heads = dim // dim_heads
|
||||
self.kv_heads = dim_kv // dim_heads
|
||||
|
||||
if dim_context is not None:
|
||||
self.to_q = nn.Linear(dim, dim, bias=False)
|
||||
self.to_kv = nn.Linear(dim_kv, dim_kv * 2, bias=False)
|
||||
else:
|
||||
self.to_qkv = nn.Linear(dim, dim * 3, bias=False)
|
||||
|
||||
self.to_out = nn.Linear(dim, dim, bias=False)
|
||||
|
||||
if zero_init_output:
|
||||
nn.init.zeros_(self.to_out.weight)
|
||||
|
||||
self.qk_norm = qk_norm
|
||||
|
||||
if self.qk_norm == "ln":
|
||||
self.q_norm = nn.LayerNorm(dim_heads, elementwise_affine=True, eps=1.0e-6)
|
||||
self.k_norm = nn.LayerNorm(dim_heads, elementwise_affine=True, eps=1.0e-6)
|
||||
elif self.qk_norm == 'rns':
|
||||
self.q_norm = nn.RMSNorm(dim_heads)
|
||||
self.k_norm = nn.RMSNorm(dim_heads)
|
||||
|
||||
# Using 1d neighborhood attention
|
||||
self.natten_kernel_size = natten_kernel_size
|
||||
if natten_kernel_size is not None:
|
||||
return
|
||||
|
||||
self.use_pt_flash = torch.cuda.is_available() and version.parse(torch.__version__) >= version.parse('2.0.0')
|
||||
|
||||
self.use_fa_flash = torch.cuda.is_available() and flash_attn_func is not None
|
||||
|
||||
self.sdp_kwargs = dict(
|
||||
enable_flash = True,
|
||||
enable_math = True,
|
||||
enable_mem_efficient = True
|
||||
)
|
||||
|
||||
def flash_attn(
|
||||
self,
|
||||
q,
|
||||
k,
|
||||
v,
|
||||
mask = None,
|
||||
causal = None
|
||||
):
|
||||
batch, heads, q_len, _, k_len, device = *q.shape, k.shape[-2], q.device
|
||||
kv_heads = k.shape[1]
|
||||
# Recommended for multi-query single-key-value attention by Tri Dao
|
||||
# kv shape torch.Size([1, 512, 64]) -> torch.Size([1, 8, 512, 64])
|
||||
|
||||
if heads != kv_heads:
|
||||
# Repeat interleave kv_heads to match q_heads
|
||||
heads_per_kv_head = heads // kv_heads
|
||||
k, v = map(lambda t: t.repeat_interleave(heads_per_kv_head, dim = 1), (k, v))
|
||||
|
||||
if k.ndim == 3:
|
||||
k = rearrange(k, 'b ... -> b 1 ...').expand_as(q)
|
||||
|
||||
if v.ndim == 3:
|
||||
v = rearrange(v, 'b ... -> b 1 ...').expand_as(q)
|
||||
|
||||
causal = self.causal if causal is None else causal
|
||||
|
||||
if q_len == 1 and causal:
|
||||
causal = False
|
||||
|
||||
if mask is not None:
|
||||
assert mask.ndim == 4
|
||||
mask = mask.expand(batch, heads, q_len, k_len)
|
||||
|
||||
# handle kv cache - this should be bypassable in updated flash attention 2
|
||||
|
||||
if k_len > q_len and causal:
|
||||
causal_mask = self.create_causal_mask(q_len, k_len, device = device)
|
||||
if mask is None:
|
||||
mask = ~causal_mask
|
||||
else:
|
||||
mask = mask & ~causal_mask
|
||||
causal = False
|
||||
|
||||
# manually handle causal mask, if another mask was given
|
||||
|
||||
row_is_entirely_masked = None
|
||||
|
||||
if mask is not None and causal:
|
||||
causal_mask = self.create_causal_mask(q_len, k_len, device = device)
|
||||
mask = mask & ~causal_mask
|
||||
|
||||
# protect against an entire row being masked out
|
||||
|
||||
row_is_entirely_masked = ~mask.any(dim = -1)
|
||||
mask[..., 0] = mask[..., 0] | row_is_entirely_masked
|
||||
|
||||
causal = False
|
||||
|
||||
with torch.backends.cuda.sdp_kernel(**self.sdp_kwargs):
|
||||
out = F.scaled_dot_product_attention(
|
||||
q, k, v,
|
||||
attn_mask = mask,
|
||||
is_causal = causal
|
||||
)
|
||||
|
||||
# for a row that is entirely masked out, should zero out the output of that row token
|
||||
|
||||
if row_is_entirely_masked is not None:
|
||||
out = out.masked_fill(row_is_entirely_masked[..., None], 0.)
|
||||
|
||||
return out
|
||||
|
||||
def forward(
|
||||
self,
|
||||
x,
|
||||
context = None,
|
||||
mask = None,
|
||||
context_mask = None,
|
||||
rotary_pos_emb = None,
|
||||
causal = None
|
||||
):
|
||||
h, kv_h, has_context = self.num_heads, self.kv_heads, context is not None
|
||||
kv_input = context if has_context else x
|
||||
|
||||
if hasattr(self, 'to_q'):
|
||||
# Use separate linear projections for q and k/v
|
||||
q = self.to_q(x)
|
||||
q = rearrange(q, 'b n (h d) -> b h n d', h = h)
|
||||
|
||||
k, v = self.to_kv(kv_input).chunk(2, dim=-1)
|
||||
|
||||
k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = kv_h), (k, v))
|
||||
else:
|
||||
# Use fused linear projection
|
||||
q, k, v = self.to_qkv(x).chunk(3, dim=-1)
|
||||
q, k, v = map(lambda t: rearrange(t, 'b n (h d) -> b h n d', h = h), (q, k, v))
|
||||
|
||||
# Normalize q and k for cosine sim attention
|
||||
if self.qk_norm == "l2":
|
||||
q = F.normalize(q, dim=-1)
|
||||
k = F.normalize(k, dim=-1)
|
||||
elif self.qk_norm == "ln":
|
||||
q = self.q_norm(q)
|
||||
k = self.k_norm(k)
|
||||
elif self.qk_norm == "rns":
|
||||
q = self.q_norm(q)
|
||||
k = self.k_norm(k)
|
||||
|
||||
if rotary_pos_emb is not None and not has_context:
|
||||
freqs, _ = rotary_pos_emb
|
||||
|
||||
q_dtype = q.dtype
|
||||
k_dtype = k.dtype
|
||||
|
||||
q = q.to(torch.float32)
|
||||
k = k.to(torch.float32)
|
||||
freqs = freqs.to(torch.float32)
|
||||
|
||||
q = apply_rotary_pos_emb(q, freqs)
|
||||
k = apply_rotary_pos_emb(k, freqs)
|
||||
|
||||
q = q.to(q_dtype)
|
||||
k = k.to(k_dtype)
|
||||
|
||||
input_mask = context_mask
|
||||
|
||||
if input_mask is None and not has_context:
|
||||
input_mask = mask
|
||||
|
||||
# determine masking
|
||||
masks = []
|
||||
final_attn_mask = None # The mask that will be applied to the attention matrix, taking all masks into account
|
||||
|
||||
if input_mask is not None:
|
||||
input_mask = rearrange(input_mask, 'b j -> b 1 1 j')
|
||||
masks.append(~input_mask)
|
||||
|
||||
# Other masks will be added here later
|
||||
|
||||
if len(masks) > 0:
|
||||
final_attn_mask = ~or_reduce(masks)
|
||||
|
||||
n, device = q.shape[-2], q.device
|
||||
|
||||
causal = self.causal if causal is None else causal
|
||||
|
||||
if n == 1 and causal:
|
||||
causal = False
|
||||
|
||||
if self.natten_kernel_size is not None:
|
||||
if natten is None:
|
||||
raise ImportError('natten not installed, please install natten to use neighborhood attention')
|
||||
|
||||
dtype_in = q.dtype
|
||||
q, k, v = map(lambda t: t.to(torch.float32), (q, k, v))
|
||||
|
||||
attn = natten.functional.natten1dqk(q, k, kernel_size = self.natten_kernel_size, dilation=1)
|
||||
|
||||
if final_attn_mask is not None:
|
||||
attn = attn.masked_fill(final_attn_mask, -torch.finfo(attn.dtype).max)
|
||||
|
||||
attn = F.softmax(attn, dim=-1, dtype=torch.float32)
|
||||
|
||||
out = natten.functional.natten1dav(attn, v, kernel_size = self.natten_kernel_size, dilation=1).to(dtype_in)
|
||||
|
||||
# Prioritize Flash Attention 2
|
||||
elif self.use_fa_flash:
|
||||
assert final_attn_mask is None, 'masking not yet supported for Flash Attention 2'
|
||||
# Flash Attention 2 requires FP16 inputs
|
||||
fa_dtype_in = q.dtype
|
||||
q, k, v = map(lambda t: rearrange(t, 'b h n d -> b n h d').to(torch.float16), (q, k, v))
|
||||
|
||||
out = flash_attn_func(q, k, v, causal = causal)
|
||||
|
||||
out = rearrange(out.to(fa_dtype_in), 'b n h d -> b h n d')
|
||||
|
||||
# Fall back to PyTorch implementation
|
||||
elif self.use_pt_flash:
|
||||
out = self.flash_attn(q, k, v, causal = causal, mask = final_attn_mask)
|
||||
|
||||
else:
|
||||
# Fall back to custom implementation
|
||||
|
||||
if h != kv_h:
|
||||
# Repeat interleave kv_heads to match q_heads
|
||||
heads_per_kv_head = h // kv_h
|
||||
k, v = map(lambda t: t.repeat_interleave(heads_per_kv_head, dim = 1), (k, v))
|
||||
|
||||
scale = 1. / (q.shape[-1] ** 0.5)
|
||||
|
||||
kv_einsum_eq = 'b j d' if k.ndim == 3 else 'b h j d'
|
||||
|
||||
dots = einsum(f'b h i d, {kv_einsum_eq} -> b h i j', q, k) * scale
|
||||
|
||||
i, j, dtype = *dots.shape[-2:], dots.dtype
|
||||
|
||||
mask_value = -torch.finfo(dots.dtype).max
|
||||
|
||||
if final_attn_mask is not None:
|
||||
dots = dots.masked_fill(~final_attn_mask, mask_value)
|
||||
|
||||
if causal:
|
||||
causal_mask = self.create_causal_mask(i, j, device = device)
|
||||
dots = dots.masked_fill(causal_mask, mask_value)
|
||||
|
||||
attn = F.softmax(dots, dim=-1, dtype=torch.float32)
|
||||
attn = attn.type(dtype)
|
||||
|
||||
out = einsum(f'b h i j, {kv_einsum_eq} -> b h i d', attn, v)
|
||||
|
||||
# merge heads
|
||||
out = rearrange(out, ' b h n d -> b n (h d)')
|
||||
|
||||
# Communicate between heads
|
||||
|
||||
# with autocast(enabled = False):
|
||||
# out_dtype = out.dtype
|
||||
# out = out.to(torch.float32)
|
||||
# out = self.to_out(out).to(out_dtype)
|
||||
out = self.to_out(out)
|
||||
|
||||
if mask is not None:
|
||||
mask = rearrange(mask, 'b n -> b n 1')
|
||||
out = out.masked_fill(~mask, 0.)
|
||||
|
||||
return out
|
||||
|
||||
class ConformerModule(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
dim,
|
||||
norm_kwargs = {},
|
||||
):
|
||||
|
||||
super().__init__()
|
||||
|
||||
self.dim = dim
|
||||
|
||||
self.in_norm = LayerNorm(dim, **norm_kwargs)
|
||||
self.pointwise_conv = nn.Conv1d(dim, dim, kernel_size=1, bias=False)
|
||||
self.glu = GLU(dim, dim, nn.SiLU())
|
||||
self.depthwise_conv = nn.Conv1d(dim, dim, kernel_size=17, groups=dim, padding=8, bias=False)
|
||||
self.mid_norm = LayerNorm(dim, **norm_kwargs) # This is a batch norm in the original but I don't like batch norm
|
||||
self.swish = nn.SiLU()
|
||||
self.pointwise_conv_2 = nn.Conv1d(dim, dim, kernel_size=1, bias=False)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.in_norm(x)
|
||||
x = rearrange(x, 'b n d -> b d n')
|
||||
x = self.pointwise_conv(x)
|
||||
x = rearrange(x, 'b d n -> b n d')
|
||||
x = self.glu(x)
|
||||
x = rearrange(x, 'b n d -> b d n')
|
||||
x = self.depthwise_conv(x)
|
||||
x = rearrange(x, 'b d n -> b n d')
|
||||
x = self.mid_norm(x)
|
||||
x = self.swish(x)
|
||||
x = rearrange(x, 'b n d -> b d n')
|
||||
x = self.pointwise_conv_2(x)
|
||||
x = rearrange(x, 'b d n -> b n d')
|
||||
|
||||
return x
|
||||
|
||||
class TransformerBlock(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
dim,
|
||||
dim_heads = 64,
|
||||
cross_attend = False,
|
||||
dim_context = None,
|
||||
global_cond_dim = None,
|
||||
causal = False,
|
||||
zero_init_branch_outputs = True,
|
||||
conformer = False,
|
||||
layer_ix = -1,
|
||||
remove_norms = False,
|
||||
attn_kwargs = {},
|
||||
ff_kwargs = {},
|
||||
norm_kwargs = {}
|
||||
):
|
||||
|
||||
super().__init__()
|
||||
self.dim = dim
|
||||
self.dim_heads = dim_heads
|
||||
self.cross_attend = cross_attend
|
||||
self.dim_context = dim_context
|
||||
self.causal = causal
|
||||
|
||||
self.pre_norm = LayerNorm(dim, **norm_kwargs) if not remove_norms else nn.Identity()
|
||||
|
||||
self.self_attn = Attention(
|
||||
dim,
|
||||
dim_heads = dim_heads,
|
||||
causal = causal,
|
||||
zero_init_output=zero_init_branch_outputs,
|
||||
**attn_kwargs
|
||||
)
|
||||
|
||||
if cross_attend:
|
||||
self.cross_attend_norm = LayerNorm(dim, **norm_kwargs) if not remove_norms else nn.Identity()
|
||||
self.cross_attn = Attention(
|
||||
dim,
|
||||
dim_heads = dim_heads,
|
||||
dim_context=dim_context,
|
||||
causal = causal,
|
||||
zero_init_output=zero_init_branch_outputs,
|
||||
**attn_kwargs
|
||||
)
|
||||
|
||||
self.ff_norm = LayerNorm(dim, **norm_kwargs) if not remove_norms else nn.Identity()
|
||||
self.ff = FeedForward(dim, zero_init_output=zero_init_branch_outputs, **ff_kwargs)
|
||||
|
||||
self.layer_ix = layer_ix
|
||||
|
||||
self.conformer = ConformerModule(dim, norm_kwargs=norm_kwargs) if conformer else None
|
||||
|
||||
self.global_cond_dim = global_cond_dim
|
||||
|
||||
if global_cond_dim is not None:
|
||||
self.to_scale_shift_gate = nn.Sequential(
|
||||
nn.SiLU(),
|
||||
nn.Linear(global_cond_dim, dim * 6, bias=False)
|
||||
)
|
||||
|
||||
nn.init.zeros_(self.to_scale_shift_gate[1].weight)
|
||||
#nn.init.zeros_(self.to_scale_shift_gate_self[1].bias)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
x,
|
||||
context = None,
|
||||
global_cond=None,
|
||||
mask = None,
|
||||
context_mask = None,
|
||||
rotary_pos_emb = None
|
||||
):
|
||||
if self.global_cond_dim is not None and self.global_cond_dim > 0 and global_cond is not None:
|
||||
|
||||
scale_self, shift_self, gate_self, scale_ff, shift_ff, gate_ff = self.to_scale_shift_gate(global_cond).unsqueeze(1).chunk(6, dim = -1)
|
||||
|
||||
# self-attention with adaLN
|
||||
residual = x
|
||||
x = self.pre_norm(x)
|
||||
x = x * (1 + scale_self) + shift_self
|
||||
x = self.self_attn(x, mask = mask, rotary_pos_emb = rotary_pos_emb)
|
||||
x = x * torch.sigmoid(1 - gate_self)
|
||||
x = x + residual
|
||||
|
||||
if context is not None:
|
||||
x = x + self.cross_attn(self.cross_attend_norm(x), context = context, context_mask = context_mask)
|
||||
|
||||
if self.conformer is not None:
|
||||
x = x + self.conformer(x)
|
||||
|
||||
# feedforward with adaLN
|
||||
residual = x
|
||||
x = self.ff_norm(x)
|
||||
x = x * (1 + scale_ff) + shift_ff
|
||||
x = self.ff(x)
|
||||
x = x * torch.sigmoid(1 - gate_ff)
|
||||
x = x + residual
|
||||
|
||||
else:
|
||||
x = x + self.self_attn(self.pre_norm(x), mask = mask, rotary_pos_emb = rotary_pos_emb)
|
||||
|
||||
if context is not None:
|
||||
x = x + self.cross_attn(self.cross_attend_norm(x), context = context, context_mask = context_mask)
|
||||
|
||||
if self.conformer is not None:
|
||||
x = x + self.conformer(x)
|
||||
|
||||
x = x + self.ff(self.ff_norm(x))
|
||||
|
||||
return x
|
||||
|
||||
class ContinuousTransformer(nn.Module):
|
||||
def __init__(
|
||||
self,
|
||||
dim,
|
||||
depth,
|
||||
*,
|
||||
dim_in = None,
|
||||
dim_out = None,
|
||||
dim_heads = 64,
|
||||
cross_attend=False,
|
||||
cond_token_dim=None,
|
||||
global_cond_dim=None,
|
||||
causal=False,
|
||||
rotary_pos_emb=True,
|
||||
zero_init_branch_outputs=True,
|
||||
conformer=False,
|
||||
use_sinusoidal_emb=False,
|
||||
use_abs_pos_emb=False,
|
||||
abs_pos_emb_max_length=10000,
|
||||
**kwargs
|
||||
):
|
||||
|
||||
super().__init__()
|
||||
|
||||
self.dim = dim
|
||||
self.depth = depth
|
||||
self.causal = causal
|
||||
self.layers = nn.ModuleList([])
|
||||
|
||||
self.project_in = nn.Linear(dim_in, dim, bias=False) if dim_in is not None else nn.Identity()
|
||||
self.project_out = nn.Linear(dim, dim_out, bias=False) if dim_out is not None else nn.Identity()
|
||||
|
||||
if rotary_pos_emb:
|
||||
self.rotary_pos_emb = RotaryEmbedding(max(dim_heads // 2, 32))
|
||||
else:
|
||||
self.rotary_pos_emb = None
|
||||
|
||||
self.use_sinusoidal_emb = use_sinusoidal_emb
|
||||
if use_sinusoidal_emb:
|
||||
self.pos_emb = ScaledSinusoidalEmbedding(dim)
|
||||
|
||||
self.use_abs_pos_emb = use_abs_pos_emb
|
||||
if use_abs_pos_emb:
|
||||
self.pos_emb = AbsolutePositionalEmbedding(dim, abs_pos_emb_max_length)
|
||||
|
||||
for i in range(depth):
|
||||
self.layers.append(
|
||||
TransformerBlock(
|
||||
dim,
|
||||
dim_heads = dim_heads,
|
||||
cross_attend = cross_attend,
|
||||
dim_context = cond_token_dim,
|
||||
global_cond_dim = global_cond_dim,
|
||||
causal = causal,
|
||||
zero_init_branch_outputs = zero_init_branch_outputs,
|
||||
conformer=conformer,
|
||||
layer_ix=i,
|
||||
**kwargs
|
||||
)
|
||||
)
|
||||
|
||||
def forward(
|
||||
self,
|
||||
x,
|
||||
mask = None,
|
||||
prepend_embeds = None,
|
||||
prepend_mask = None,
|
||||
add_cond = None,
|
||||
global_cond = None,
|
||||
return_info = False,
|
||||
**kwargs
|
||||
):
|
||||
batch, seq, device = *x.shape[:2], x.device
|
||||
|
||||
info = {
|
||||
"hidden_states": [],
|
||||
}
|
||||
|
||||
x = self.project_in(x)
|
||||
if add_cond is not None:
|
||||
x = x + add_cond
|
||||
|
||||
if prepend_embeds is not None:
|
||||
prepend_length, prepend_dim = prepend_embeds.shape[1:]
|
||||
|
||||
assert prepend_dim == x.shape[-1], 'prepend dimension must match sequence dimension'
|
||||
|
||||
x = torch.cat((prepend_embeds, x), dim = -2)
|
||||
|
||||
if prepend_mask is not None or mask is not None:
|
||||
mask = mask if mask is not None else torch.ones((batch, seq), device = device, dtype = torch.bool)
|
||||
prepend_mask = prepend_mask if prepend_mask is not None else torch.ones((batch, prepend_length), device = device, dtype = torch.bool)
|
||||
|
||||
mask = torch.cat((prepend_mask, mask), dim = -1)
|
||||
|
||||
|
||||
# Attention layers
|
||||
|
||||
if self.rotary_pos_emb is not None:
|
||||
rotary_pos_emb = self.rotary_pos_emb.forward_from_seq_len(x.shape[1])
|
||||
else:
|
||||
rotary_pos_emb = None
|
||||
|
||||
if self.use_sinusoidal_emb or self.use_abs_pos_emb:
|
||||
x = x + self.pos_emb(x)
|
||||
|
||||
# Iterate over the transformer layers
|
||||
for layer in self.layers:
|
||||
#x = layer(x, rotary_pos_emb = rotary_pos_emb, global_cond=global_cond, **kwargs)
|
||||
x = checkpoint(layer, x, rotary_pos_emb = rotary_pos_emb, global_cond=global_cond, **kwargs)
|
||||
|
||||
if return_info:
|
||||
info["hidden_states"].append(x)
|
||||
|
||||
x = self.project_out(x)
|
||||
|
||||
if return_info:
|
||||
return x, info
|
||||
|
||||
return x
|
||||
@@ -0,0 +1,271 @@
|
||||
from typing import Optional
|
||||
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
import torch.nn.functional as F
|
||||
from einops import rearrange
|
||||
from einops.layers.torch import Rearrange
|
||||
|
||||
from .embeddings import apply_rope
|
||||
from .blocks import MLP, ChannelLastConv1d, ConvMLP
|
||||
try:
|
||||
from flash_attn import flash_attn_func, flash_attn_kvpacked_func
|
||||
print('flash_attn installed, using Flash Attention')
|
||||
except ImportError as e:
|
||||
print(e)
|
||||
print('flash_attn not installed, disabling Flash Attention')
|
||||
flash_attn_kvpacked_func = None
|
||||
flash_attn_func = None
|
||||
|
||||
def modulate(x: torch.Tensor, shift: torch.Tensor, scale: torch.Tensor):
|
||||
return x * (1 + scale) + shift
|
||||
|
||||
|
||||
def attention(q: torch.Tensor, k: torch.Tensor, v: torch.Tensor):
|
||||
# training will crash without these contiguous calls and the CUDNN limitation
|
||||
# I believe this is related to https://github.com/pytorch/pytorch/issues/133974
|
||||
# unresolved at the time of writing
|
||||
fa_dtype_in = q.dtype
|
||||
|
||||
q = q.contiguous()
|
||||
k = k.contiguous()
|
||||
v = v.contiguous()
|
||||
out = F.scaled_dot_product_attention(q, k, v)
|
||||
out = rearrange(out, 'b h n d -> b n (h d)').contiguous()
|
||||
return out
|
||||
q, k, v = map(lambda t: rearrange(t, 'b h n d -> b n h d').to(torch.bfloat16), (q, k, v))
|
||||
# print(f"q dtype: {q.dtype}")
|
||||
# print(f"k dtype: {k.dtype}")
|
||||
# print(f"v dtype: {v.dtype}")
|
||||
# breakpoint()
|
||||
out = flash_attn_func(q, k, v)
|
||||
out = rearrange(out.to(fa_dtype_in), 'b n h d -> b n (h d)')
|
||||
# out = rearrange(out.to(fa_dtype_in), 'b h n d -> b n (h d)').contiguous()
|
||||
return out
|
||||
|
||||
|
||||
class SelfAttention(nn.Module):
|
||||
|
||||
def __init__(self, dim: int, nheads: int):
|
||||
super().__init__()
|
||||
self.dim = dim
|
||||
self.nheads = nheads
|
||||
|
||||
self.qkv = nn.Linear(dim, dim * 3, bias=True)
|
||||
self.q_norm = nn.RMSNorm(dim // nheads)
|
||||
self.k_norm = nn.RMSNorm(dim // nheads)
|
||||
|
||||
self.split_into_heads = Rearrange('b n (h d j) -> b h n d j',
|
||||
h=nheads,
|
||||
d=dim // nheads,
|
||||
j=3)
|
||||
|
||||
def pre_attention(
|
||||
self, x: torch.Tensor,
|
||||
rot: Optional[torch.Tensor]) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
# x: batch_size * n_tokens * n_channels
|
||||
qkv = self.qkv(x)
|
||||
q, k, v = self.split_into_heads(qkv).chunk(3, dim=-1)
|
||||
q = q.squeeze(-1)
|
||||
k = k.squeeze(-1)
|
||||
v = v.squeeze(-1)
|
||||
q = self.q_norm(q)
|
||||
k = self.k_norm(k)
|
||||
|
||||
if rot is not None:
|
||||
q = apply_rope(q, rot)
|
||||
k = apply_rope(k, rot)
|
||||
|
||||
return q, k, v
|
||||
|
||||
def forward(
|
||||
self,
|
||||
x: torch.Tensor, # batch_size * n_tokens * n_channels
|
||||
) -> torch.Tensor:
|
||||
q, v, k = self.pre_attention(x)
|
||||
out = attention(q, k, v)
|
||||
return out
|
||||
|
||||
class CrossAttention(nn.Module):
|
||||
|
||||
def __init__(self, dim: int, nheads: int):
|
||||
super().__init__()
|
||||
self.dim = dim
|
||||
self.nheads = nheads
|
||||
|
||||
self.to_q = nn.Linear(dim, dim, bias=False)
|
||||
self.to_kv = nn.Linear(dim, dim * 2, bias=False)
|
||||
self.q_norm = nn.RMSNorm(dim // nheads)
|
||||
self.k_norm = nn.RMSNorm(dim // nheads)
|
||||
|
||||
self.split_q_into_heads = Rearrange('b n (h d) -> b h n d',
|
||||
h=nheads,
|
||||
d=dim // nheads)
|
||||
self.split_kv_into_heads = Rearrange('b n (h d j) -> b h n d j',
|
||||
h=nheads,
|
||||
d=dim // nheads,
|
||||
j=2)
|
||||
|
||||
def pre_attention(
|
||||
self, x: torch.Tensor,
|
||||
context: Optional[torch.Tensor]) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
|
||||
# x: batch_size * n_tokens * n_channels
|
||||
q = self.to_q(x)
|
||||
kv = self.to_kv(context)
|
||||
q = self.split_q_into_heads(q)
|
||||
k, v = self.split_kv_into_heads(kv).chunk(2, dim=-1)
|
||||
q = q.squeeze(-1)
|
||||
k = k.squeeze(-1)
|
||||
v = v.squeeze(-1)
|
||||
q = self.q_norm(q)
|
||||
k = self.k_norm(k)
|
||||
|
||||
|
||||
return q, k, v
|
||||
|
||||
def forward(
|
||||
self,
|
||||
x: torch.Tensor, context=None
|
||||
) -> torch.Tensor:
|
||||
q, v, k = self.pre_attention(x, context=context)
|
||||
out = attention(q, k, v)
|
||||
return out
|
||||
|
||||
|
||||
class MMDitSingleBlock(nn.Module):
|
||||
|
||||
def __init__(self,
|
||||
dim: int,
|
||||
nhead: int,
|
||||
mlp_ratio: float = 4.0,
|
||||
pre_only: bool = False,
|
||||
kernel_size: int = 7,
|
||||
padding: int = 3,
|
||||
cross_attend: bool = False):
|
||||
super().__init__()
|
||||
self.norm1 = nn.LayerNorm(dim, elementwise_affine=False)
|
||||
self.attn = SelfAttention(dim, nhead)
|
||||
if cross_attend:
|
||||
self.cross_attn = CrossAttention(dim, nhead)
|
||||
self.pre_only = pre_only
|
||||
if pre_only:
|
||||
self.adaLN_modulation = nn.Sequential(nn.SiLU(), nn.Linear(dim, 2 * dim, bias=True))
|
||||
else:
|
||||
if kernel_size == 1:
|
||||
self.linear1 = nn.Linear(dim, dim)
|
||||
else:
|
||||
self.linear1 = ChannelLastConv1d(dim, dim, kernel_size=kernel_size, padding=padding)
|
||||
self.norm2 = nn.LayerNorm(dim, elementwise_affine=False)
|
||||
|
||||
if kernel_size == 1:
|
||||
self.ffn = MLP(dim, int(dim * mlp_ratio))
|
||||
else:
|
||||
self.ffn = ConvMLP(dim,
|
||||
int(dim * mlp_ratio),
|
||||
kernel_size=kernel_size,
|
||||
padding=padding)
|
||||
|
||||
self.adaLN_modulation = nn.Sequential(nn.SiLU(), nn.Linear(dim, 6 * dim, bias=True))
|
||||
|
||||
def pre_attention(self, x: torch.Tensor, c: torch.Tensor, rot: Optional[torch.Tensor]):
|
||||
# x: BS * N * D
|
||||
# cond: BS * D
|
||||
modulation = self.adaLN_modulation(c)
|
||||
if self.pre_only:
|
||||
(shift_msa, scale_msa) = modulation.chunk(2, dim=-1)
|
||||
gate_msa = shift_mlp = scale_mlp = gate_mlp = None
|
||||
else:
|
||||
(shift_msa, scale_msa, gate_msa, shift_mlp, scale_mlp,
|
||||
gate_mlp) = modulation.chunk(6, dim=-1)
|
||||
|
||||
x = modulate(self.norm1(x), shift_msa, scale_msa)
|
||||
q, k, v = self.attn.pre_attention(x, rot)
|
||||
return (q, k, v), (gate_msa, shift_mlp, scale_mlp, gate_mlp)
|
||||
|
||||
def post_attention(self, x: torch.Tensor, attn_out: torch.Tensor, c: tuple[torch.Tensor], context=None):
|
||||
if self.pre_only:
|
||||
return x
|
||||
|
||||
(gate_msa, shift_mlp, scale_mlp, gate_mlp) = c
|
||||
x = x + self.linear1(attn_out) * gate_msa
|
||||
|
||||
if context is not None:
|
||||
x = x + self.cross_attn(x, context=context)
|
||||
|
||||
r = modulate(self.norm2(x), shift_mlp, scale_mlp)
|
||||
x = x + self.ffn(r) * gate_mlp
|
||||
|
||||
return x
|
||||
|
||||
def forward(self, x: torch.Tensor, cond: torch.Tensor,
|
||||
rot: Optional[torch.Tensor], context: torch.Tensor = None) -> torch.Tensor:
|
||||
# x: BS * N * D
|
||||
# cond: BS * D
|
||||
x_qkv, x_conditions = self.pre_attention(x, cond, rot)
|
||||
attn_out = attention(*x_qkv)
|
||||
x = self.post_attention(x, attn_out, x_conditions, context = context)
|
||||
|
||||
return x
|
||||
|
||||
|
||||
class JointBlock(nn.Module):
|
||||
|
||||
def __init__(self, dim: int, nhead: int, mlp_ratio: float = 4.0, pre_only: bool = False):
|
||||
super().__init__()
|
||||
self.pre_only = pre_only
|
||||
self.latent_block = MMDitSingleBlock(dim,
|
||||
nhead,
|
||||
mlp_ratio,
|
||||
pre_only=False,
|
||||
kernel_size=3,
|
||||
padding=1)
|
||||
self.clip_block = MMDitSingleBlock(dim,
|
||||
nhead,
|
||||
mlp_ratio,
|
||||
pre_only=pre_only,
|
||||
kernel_size=3,
|
||||
padding=1)
|
||||
self.text_block = MMDitSingleBlock(dim, nhead, mlp_ratio, pre_only=pre_only, kernel_size=1)
|
||||
|
||||
def forward(self, latent: torch.Tensor, clip_f: torch.Tensor, text_f: torch.Tensor,
|
||||
global_c: torch.Tensor, extended_c: torch.Tensor, latent_rot: torch.Tensor,
|
||||
clip_rot: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor]:
|
||||
# latent: BS * N1 * D
|
||||
# clip_f: BS * N2 * D
|
||||
# c: BS * (1/N) * D
|
||||
x_qkv, x_mod = self.latent_block.pre_attention(latent, extended_c, latent_rot)
|
||||
c_qkv, c_mod = self.clip_block.pre_attention(clip_f, global_c, clip_rot)
|
||||
t_qkv, t_mod = self.text_block.pre_attention(text_f, global_c, rot=None)
|
||||
|
||||
latent_len = latent.shape[1]
|
||||
clip_len = clip_f.shape[1]
|
||||
text_len = text_f.shape[1]
|
||||
|
||||
joint_qkv = [torch.cat([x_qkv[i], c_qkv[i], t_qkv[i]], dim=2) for i in range(3)]
|
||||
|
||||
attn_out = attention(*joint_qkv)
|
||||
x_attn_out = attn_out[:, :latent_len]
|
||||
c_attn_out = attn_out[:, latent_len:latent_len + clip_len]
|
||||
t_attn_out = attn_out[:, latent_len + clip_len:]
|
||||
|
||||
latent = self.latent_block.post_attention(latent, x_attn_out, x_mod)
|
||||
if not self.pre_only:
|
||||
clip_f = self.clip_block.post_attention(clip_f, c_attn_out, c_mod)
|
||||
text_f = self.text_block.post_attention(text_f, t_attn_out, t_mod)
|
||||
|
||||
return latent, clip_f, text_f
|
||||
|
||||
|
||||
class FinalBlock(nn.Module):
|
||||
|
||||
def __init__(self, dim, out_dim):
|
||||
super().__init__()
|
||||
self.adaLN_modulation = nn.Sequential(nn.SiLU(), nn.Linear(dim, 2 * dim, bias=True))
|
||||
self.norm = nn.LayerNorm(dim, elementwise_affine=False)
|
||||
self.conv = ChannelLastConv1d(dim, out_dim, kernel_size=7, padding=3)
|
||||
|
||||
def forward(self, latent, c):
|
||||
shift, scale = self.adaLN_modulation(c).chunk(2, dim=-1)
|
||||
latent = modulate(self.norm(latent), shift, scale)
|
||||
latent = self.conv(latent)
|
||||
return latent
|
||||
@@ -0,0 +1,200 @@
|
||||
import torch
|
||||
from safetensors.torch import load_file
|
||||
from torch import nn, Tensor, einsum, IntTensor, FloatTensor, BoolTensor
|
||||
#from torchcubicspline import natural_cubic_spline_coeffs, NaturalCubicSpline
|
||||
from torch.nn.utils import remove_weight_norm
|
||||
|
||||
def load_ckpt_state_dict(ckpt_path, prefix=None):
|
||||
if ckpt_path.endswith(".safetensors"):
|
||||
state_dict = load_file(ckpt_path)
|
||||
else:
|
||||
state_dict = torch.load(ckpt_path, map_location="cpu")["state_dict"]
|
||||
|
||||
# 过滤特定前缀的state_dict
|
||||
filtered_state_dict = {k.replace(f'{prefix}',''): v for k, v in state_dict.items() if k.startswith(prefix)} if prefix is not None else state_dict
|
||||
|
||||
return filtered_state_dict
|
||||
|
||||
def remove_weight_norm_from_model(model):
|
||||
for module in model.modules():
|
||||
if hasattr(module, "weight"):
|
||||
print(f"Removing weight norm from {module}")
|
||||
remove_weight_norm(module)
|
||||
|
||||
return model
|
||||
|
||||
# Sampling functions copied from https://github.com/facebookresearch/audiocraft/blob/main/audiocraft/utils/utils.py under MIT license
|
||||
# License can be found in LICENSES/LICENSE_META.txt
|
||||
|
||||
def multinomial(input: torch.Tensor, num_samples: int, replacement=False, *, generator=None):
|
||||
"""torch.multinomial with arbitrary number of dimensions, and number of candidates on the last dimension.
|
||||
|
||||
Args:
|
||||
input (torch.Tensor): The input tensor containing probabilities.
|
||||
num_samples (int): Number of samples to draw.
|
||||
replacement (bool): Whether to draw with replacement or not.
|
||||
Keywords args:
|
||||
generator (torch.Generator): A pseudorandom number generator for sampling.
|
||||
Returns:
|
||||
torch.Tensor: Last dimension contains num_samples indices
|
||||
sampled from the multinomial probability distribution
|
||||
located in the last dimension of tensor input.
|
||||
"""
|
||||
|
||||
if num_samples == 1:
|
||||
q = torch.empty_like(input).exponential_(1, generator=generator)
|
||||
return torch.argmax(input / q, dim=-1, keepdim=True).to(torch.int64)
|
||||
|
||||
input_ = input.reshape(-1, input.shape[-1])
|
||||
output_ = torch.multinomial(input_, num_samples=num_samples, replacement=replacement, generator=generator)
|
||||
output = output_.reshape(*list(input.shape[:-1]), -1)
|
||||
return output
|
||||
|
||||
|
||||
def sample_top_k(probs: torch.Tensor, k: int) -> torch.Tensor:
|
||||
"""Sample next token from top K values along the last dimension of the input probs tensor.
|
||||
|
||||
Args:
|
||||
probs (torch.Tensor): Input probabilities with token candidates on the last dimension.
|
||||
k (int): The k in “top-k”.
|
||||
Returns:
|
||||
torch.Tensor: Sampled tokens.
|
||||
"""
|
||||
top_k_value, _ = torch.topk(probs, k, dim=-1)
|
||||
min_value_top_k = top_k_value[..., [-1]]
|
||||
probs *= (probs >= min_value_top_k).float()
|
||||
probs.div_(probs.sum(dim=-1, keepdim=True))
|
||||
next_token = multinomial(probs, num_samples=1)
|
||||
return next_token
|
||||
|
||||
|
||||
def sample_top_p(probs: torch.Tensor, p: float) -> torch.Tensor:
|
||||
"""Sample next token from top P probabilities along the last dimension of the input probs tensor.
|
||||
|
||||
Args:
|
||||
probs (torch.Tensor): Input probabilities with token candidates on the last dimension.
|
||||
p (int): The p in “top-p”.
|
||||
Returns:
|
||||
torch.Tensor: Sampled tokens.
|
||||
"""
|
||||
probs_sort, probs_idx = torch.sort(probs, dim=-1, descending=True)
|
||||
probs_sum = torch.cumsum(probs_sort, dim=-1)
|
||||
mask = probs_sum - probs_sort > p
|
||||
probs_sort *= (~mask).float()
|
||||
probs_sort.div_(probs_sort.sum(dim=-1, keepdim=True))
|
||||
next_token = multinomial(probs_sort, num_samples=1)
|
||||
next_token = torch.gather(probs_idx, -1, next_token)
|
||||
return next_token
|
||||
|
||||
def next_power_of_two(n):
|
||||
return 2 ** (n - 1).bit_length()
|
||||
|
||||
def next_multiple_of_64(n):
|
||||
return ((n + 63) // 64) * 64
|
||||
|
||||
|
||||
# mask construction helpers
|
||||
|
||||
def mask_from_start_end_indices(
|
||||
seq_len: int,
|
||||
start: Tensor,
|
||||
end: Tensor
|
||||
):
|
||||
assert start.shape == end.shape
|
||||
device = start.device
|
||||
|
||||
seq = torch.arange(seq_len, device = device, dtype = torch.long)
|
||||
seq = seq.reshape(*((-1,) * start.ndim), seq_len)
|
||||
seq = seq.expand(*start.shape, seq_len)
|
||||
|
||||
mask = seq >= start[..., None].long()
|
||||
mask &= seq < end[..., None].long()
|
||||
return mask
|
||||
|
||||
def mask_from_frac_lengths(
|
||||
seq_len: int,
|
||||
frac_lengths: Tensor
|
||||
):
|
||||
device = frac_lengths.device
|
||||
|
||||
lengths = (frac_lengths * seq_len).long()
|
||||
max_start = seq_len - lengths
|
||||
|
||||
rand = torch.zeros_like(frac_lengths, device = device).float().uniform_(0, 1)
|
||||
start = (max_start * rand).clamp(min = 0)
|
||||
end = start + lengths
|
||||
|
||||
return mask_from_start_end_indices(seq_len, start, end)
|
||||
|
||||
def _build_spline(video_feat, video_t, target_t):
|
||||
# 三次样条插值核心实现
|
||||
coeffs = natural_cubic_spline_coeffs(video_t, video_feat.permute(0,2,1))
|
||||
spline = NaturalCubicSpline(coeffs)
|
||||
return spline.evaluate(target_t).permute(0,2,1)
|
||||
|
||||
def resample(video_feat, audio_latent):
|
||||
"""
|
||||
9s
|
||||
video_feat: [B, 72, D]
|
||||
audio_latent: [B, D', 194] or int
|
||||
"""
|
||||
B, Tv, D = video_feat.shape
|
||||
|
||||
if isinstance(audio_latent, torch.Tensor):
|
||||
# audio_latent is a tensor
|
||||
if audio_latent.shape[1] != D:
|
||||
Ta = audio_latent.shape[1]
|
||||
else:
|
||||
Ta = audio_latent.shape[2]
|
||||
elif isinstance(audio_latent, int):
|
||||
# audio_latent is an int
|
||||
Ta = audio_latent
|
||||
else:
|
||||
raise TypeError("audio_latent must be either a tensor or an int")
|
||||
|
||||
# 构建时间戳 (关键改进点)
|
||||
video_time = torch.linspace(0, 9, Tv, device=video_feat.device)
|
||||
audio_time = torch.linspace(0, 9, Ta, device=video_feat.device)
|
||||
|
||||
# 三维化处理 (Batch, Feature, Time)
|
||||
video_feat = video_feat.permute(0, 2, 1) # [B, D, Tv]
|
||||
|
||||
# 三次样条插值
|
||||
aligned_video = _build_spline(video_feat, video_time, audio_time) # [B, D, Ta]
|
||||
return aligned_video.permute(0, 2, 1) # [B, Ta, D]
|
||||
|
||||
|
||||
def copy_state_dict(model, state_dict):
|
||||
"""Load state_dict to model, but only for keys that match exactly.
|
||||
|
||||
Args:
|
||||
model (nn.Module): model to load state_dict.
|
||||
state_dict (OrderedDict): state_dict to load.
|
||||
"""
|
||||
model_state_dict = model.state_dict()
|
||||
|
||||
# 创建一个列表存储不匹配的参数
|
||||
missing_keys = []
|
||||
unexpected_keys = []
|
||||
# 手动加载并检查不匹配的参数
|
||||
for key in state_dict:
|
||||
if key not in model_state_dict:
|
||||
unexpected_keys.append(key)
|
||||
elif state_dict[key].shape != model_state_dict[key].shape:
|
||||
unexpected_keys.append(key)
|
||||
|
||||
for key in model_state_dict:
|
||||
if key not in state_dict:
|
||||
missing_keys.append(key)
|
||||
|
||||
# 打印不匹配的参数
|
||||
print("Missing keys in state_dict:", missing_keys)
|
||||
print("Unexpected keys in state_dict:", unexpected_keys)
|
||||
for key in state_dict:
|
||||
if key in model_state_dict and state_dict[key].shape == model_state_dict[key].shape:
|
||||
if isinstance(state_dict[key], torch.nn.Parameter):
|
||||
# backwards compatibility for serialized parameters
|
||||
state_dict[key] = state_dict[key].data
|
||||
model_state_dict[key] = state_dict[key]
|
||||
|
||||
model.load_state_dict(model_state_dict, strict=False)
|
||||
@@ -0,0 +1 @@
|
||||
from .factory import create_training_wrapper_from_config, create_demo_callback_from_config
|
||||
@@ -0,0 +1,504 @@
|
||||
import torch
|
||||
import torchaudio
|
||||
import wandb
|
||||
from einops import rearrange
|
||||
from safetensors.torch import save_file, save_model
|
||||
from ema_pytorch import EMA
|
||||
from .losses.auraloss import SumAndDifferenceSTFTLoss, MultiResolutionSTFTLoss, SpatialSTFTLoss
|
||||
# import pytorch_lightning as pl
|
||||
import lightning as L
|
||||
from lightning.pytorch.callbacks import Callback
|
||||
from ..models.autoencoders import AudioAutoencoder
|
||||
from ..models.bottleneck import VAEBottleneck, RVQBottleneck, DACRVQBottleneck, DACRVQVAEBottleneck, RVQVAEBottleneck, WassersteinBottleneck
|
||||
from .losses import MultiLoss, AuralossLoss, ValueLoss, L1Loss
|
||||
from .utils import create_optimizer_from_config, create_scheduler_from_config
|
||||
|
||||
|
||||
from pytorch_lightning.utilities.rank_zero import rank_zero_only
|
||||
from aeiou.viz import pca_point_cloud, audio_spectrogram_image, tokens_spectrogram_image
|
||||
|
||||
class AutoencoderTrainingWrapper(L.LightningModule):
|
||||
def __init__(
|
||||
self,
|
||||
autoencoder: AudioAutoencoder,
|
||||
lr: float = 1e-4,
|
||||
warmup_steps: int = 0,
|
||||
encoder_freeze_on_warmup: bool = False,
|
||||
sample_rate=48000,
|
||||
loss_config: dict = None,
|
||||
optimizer_configs: dict = None,
|
||||
use_ema: bool = True,
|
||||
ema_copy = None,
|
||||
force_input_mono = False,
|
||||
latent_mask_ratio = 0.0,
|
||||
teacher_model: AudioAutoencoder = None
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.automatic_optimization = False
|
||||
|
||||
self.autoencoder = autoencoder
|
||||
|
||||
self.warmed_up = False
|
||||
self.warmup_steps = warmup_steps
|
||||
self.encoder_freeze_on_warmup = encoder_freeze_on_warmup
|
||||
self.lr = lr
|
||||
|
||||
self.force_input_mono = force_input_mono
|
||||
|
||||
self.teacher_model = teacher_model
|
||||
|
||||
if optimizer_configs is None:
|
||||
optimizer_configs ={
|
||||
"autoencoder": {
|
||||
"optimizer": {
|
||||
"type": "AdamW",
|
||||
"config": {
|
||||
"lr": lr,
|
||||
"betas": (.8, .99)
|
||||
}
|
||||
}
|
||||
},
|
||||
"discriminator": {
|
||||
"optimizer": {
|
||||
"type": "AdamW",
|
||||
"config": {
|
||||
"lr": lr,
|
||||
"betas": (.8, .99)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
self.optimizer_configs = optimizer_configs
|
||||
|
||||
if loss_config is None:
|
||||
scales = [2048, 1024, 512, 256, 128, 64, 32]
|
||||
hop_sizes = []
|
||||
win_lengths = []
|
||||
overlap = 0.75
|
||||
for s in scales:
|
||||
hop_sizes.append(int(s * (1 - overlap)))
|
||||
win_lengths.append(s)
|
||||
|
||||
loss_config = {
|
||||
"discriminator": {
|
||||
"type": "encodec",
|
||||
"config": {
|
||||
"n_ffts": scales,
|
||||
"hop_lengths": hop_sizes,
|
||||
"win_lengths": win_lengths,
|
||||
"filters": 32
|
||||
},
|
||||
"weights": {
|
||||
"adversarial": 0.1,
|
||||
"feature_matching": 5.0,
|
||||
}
|
||||
},
|
||||
"spectral": {
|
||||
"type": "mrstft",
|
||||
"config": {
|
||||
"fft_sizes": scales,
|
||||
"hop_sizes": hop_sizes,
|
||||
"win_lengths": win_lengths,
|
||||
"perceptual_weighting": True
|
||||
},
|
||||
"weights": {
|
||||
"mrstft": 1.0,
|
||||
}
|
||||
},
|
||||
"time": {
|
||||
"type": "l1",
|
||||
"config": {},
|
||||
"weights": {
|
||||
"l1": 0.0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.loss_config = loss_config
|
||||
|
||||
# Spectral reconstruction loss
|
||||
|
||||
stft_loss_args = loss_config['spectral']['config']
|
||||
|
||||
if self.autoencoder.out_channels == 2:
|
||||
self.sdstft = SumAndDifferenceSTFTLoss(sample_rate=sample_rate, **stft_loss_args)
|
||||
self.lrstft = MultiResolutionSTFTLoss(sample_rate=sample_rate, **stft_loss_args)
|
||||
elif self.autoencoder.out_channels == 4:
|
||||
# self.sdstft = SpatialSTFTLoss(sample_rate=sample_rate, **stft_loss_args)
|
||||
self.sdstft = MultiResolutionSTFTLoss(sample_rate=sample_rate, **stft_loss_args)
|
||||
else:
|
||||
self.sdstft = MultiResolutionSTFTLoss(sample_rate=sample_rate, **stft_loss_args)
|
||||
|
||||
# Discriminator
|
||||
|
||||
if loss_config['discriminator']['type'] == 'oobleck':
|
||||
self.discriminator = OobleckDiscriminator(**loss_config['discriminator']['config'])
|
||||
elif loss_config['discriminator']['type'] == 'encodec':
|
||||
self.discriminator = EncodecDiscriminator(in_channels=self.autoencoder.out_channels, **loss_config['discriminator']['config'])
|
||||
elif loss_config['discriminator']['type'] == 'dac':
|
||||
self.discriminator = DACGANLoss(channels=self.autoencoder.out_channels, sample_rate=sample_rate, **loss_config['discriminator']['config'])
|
||||
|
||||
self.gen_loss_modules = []
|
||||
|
||||
# Adversarial and feature matching losses
|
||||
self.gen_loss_modules += [
|
||||
ValueLoss(key='loss_adv', weight=self.loss_config['discriminator']['weights']['adversarial'], name='loss_adv'),
|
||||
ValueLoss(key='feature_matching_distance', weight=self.loss_config['discriminator']['weights']['feature_matching'], name='feature_matching'),
|
||||
]
|
||||
|
||||
if self.teacher_model is not None:
|
||||
# Distillation losses
|
||||
|
||||
stft_loss_weight = self.loss_config['spectral']['weights']['mrstft'] * 0.25
|
||||
self.gen_loss_modules += [
|
||||
AuralossLoss(self.sdstft, 'reals', 'decoded', name='mrstft_loss', weight=stft_loss_weight), # Reconstruction loss
|
||||
AuralossLoss(self.sdstft, 'decoded', 'teacher_decoded', name='mrstft_loss_distill', weight=stft_loss_weight), # Distilled model's decoder is compatible with teacher's decoder
|
||||
AuralossLoss(self.sdstft, 'reals', 'own_latents_teacher_decoded', name='mrstft_loss_own_latents_teacher', weight=stft_loss_weight), # Distilled model's encoder is compatible with teacher's decoder
|
||||
AuralossLoss(self.sdstft, 'reals', 'teacher_latents_own_decoded', name='mrstft_loss_teacher_latents_own', weight=stft_loss_weight) # Teacher's encoder is compatible with distilled model's decoder
|
||||
]
|
||||
|
||||
else:
|
||||
|
||||
# Reconstruction loss
|
||||
self.gen_loss_modules += [
|
||||
AuralossLoss(self.sdstft, 'reals', 'decoded', name='mrstft_loss', weight=self.loss_config['spectral']['weights']['mrstft']),
|
||||
]
|
||||
|
||||
if self.autoencoder.out_channels == 2:
|
||||
|
||||
# Add left and right channel reconstruction losses in addition to the sum and difference
|
||||
self.gen_loss_modules += [
|
||||
AuralossLoss(self.lrstft, 'reals_left', 'decoded_left', name='stft_loss_left', weight=self.loss_config['spectral']['weights']['mrstft']/2),
|
||||
AuralossLoss(self.lrstft, 'reals_right', 'decoded_right', name='stft_loss_right', weight=self.loss_config['spectral']['weights']['mrstft']/2),
|
||||
]
|
||||
elif self.autoencoder.out_channels == 4:
|
||||
# self.gen_loss_modules += [
|
||||
# AuralossLoss(self.lrstft, 'reals', 'decoded', name='stft_loss', weight=self.loss_config['spectral']['weights']['mrstft']),
|
||||
# ]
|
||||
# Add left and right channel reconstruction losses in addition to the sum and difference
|
||||
self.gen_loss_modules += [
|
||||
AuralossLoss(self.sdstft, 'reals_w', 'decoded_w', name='stft_loss_w', weight=self.loss_config['spectral']['weights']['mrstft']/4),
|
||||
AuralossLoss(self.sdstft, 'reals_x', 'decoded_x', name='stft_loss_x', weight=self.loss_config['spectral']['weights']['mrstft']/4),
|
||||
AuralossLoss(self.sdstft, 'reals_y', 'decoded_y', name='stft_loss_y', weight=self.loss_config['spectral']['weights']['mrstft']/4),
|
||||
AuralossLoss(self.sdstft, 'reals_z', 'decoded_z', name='stft_loss_z', weight=self.loss_config['spectral']['weights']['mrstft']/4),
|
||||
]
|
||||
|
||||
self.gen_loss_modules += [
|
||||
AuralossLoss(self.sdstft, 'reals', 'decoded', name='mrstft_loss', weight=self.loss_config['spectral']['weights']['mrstft']),
|
||||
]
|
||||
|
||||
if self.loss_config['time']['weights']['l1'] > 0.0:
|
||||
self.gen_loss_modules.append(L1Loss(key_a='reals', key_b='decoded', weight=self.loss_config['time']['weights']['l1'], name='l1_time_loss'))
|
||||
|
||||
if self.autoencoder.bottleneck is not None:
|
||||
self.gen_loss_modules += create_loss_modules_from_bottleneck(self.autoencoder.bottleneck, self.loss_config)
|
||||
|
||||
self.losses_gen = MultiLoss(self.gen_loss_modules)
|
||||
|
||||
self.disc_loss_modules = [
|
||||
ValueLoss(key='loss_dis', weight=1.0, name='discriminator_loss'),
|
||||
]
|
||||
|
||||
self.losses_disc = MultiLoss(self.disc_loss_modules)
|
||||
|
||||
# Set up EMA for model weights
|
||||
self.autoencoder_ema = None
|
||||
|
||||
self.use_ema = use_ema
|
||||
|
||||
if self.use_ema:
|
||||
self.autoencoder_ema = EMA(
|
||||
self.autoencoder,
|
||||
ema_model=ema_copy,
|
||||
beta=0.9999,
|
||||
power=3/4,
|
||||
update_every=1,
|
||||
update_after_step=1
|
||||
)
|
||||
|
||||
self.latent_mask_ratio = latent_mask_ratio
|
||||
|
||||
def configure_optimizers(self):
|
||||
|
||||
opt_gen = create_optimizer_from_config(self.optimizer_configs['autoencoder']['optimizer'], self.autoencoder.parameters())
|
||||
opt_disc = create_optimizer_from_config(self.optimizer_configs['discriminator']['optimizer'], self.discriminator.parameters())
|
||||
|
||||
if "scheduler" in self.optimizer_configs['autoencoder'] and "scheduler" in self.optimizer_configs['discriminator']:
|
||||
sched_gen = create_scheduler_from_config(self.optimizer_configs['autoencoder']['scheduler'], opt_gen)
|
||||
sched_disc = create_scheduler_from_config(self.optimizer_configs['discriminator']['scheduler'], opt_disc)
|
||||
return [opt_gen, opt_disc], [sched_gen, sched_disc]
|
||||
|
||||
return [opt_gen, opt_disc]
|
||||
|
||||
def training_step(self, batch, batch_idx):
|
||||
reals, _ = batch
|
||||
|
||||
# Remove extra dimension added by WebDataset
|
||||
if reals.ndim == 4 and reals.shape[0] == 1:
|
||||
reals = reals[0]
|
||||
|
||||
if self.global_step >= self.warmup_steps:
|
||||
self.warmed_up = True
|
||||
|
||||
loss_info = {}
|
||||
|
||||
loss_info["reals"] = reals
|
||||
|
||||
encoder_input = reals
|
||||
|
||||
if self.force_input_mono and encoder_input.shape[1] > 1:
|
||||
encoder_input = encoder_input.mean(dim=1, keepdim=True)
|
||||
|
||||
loss_info["encoder_input"] = encoder_input
|
||||
|
||||
data_std = encoder_input.std()
|
||||
|
||||
if self.warmed_up and self.encoder_freeze_on_warmup:
|
||||
with torch.no_grad():
|
||||
latents, encoder_info = self.autoencoder.encode(encoder_input, return_info=True)
|
||||
else:
|
||||
latents, encoder_info = self.autoencoder.encode(encoder_input, return_info=True)
|
||||
|
||||
loss_info["latents"] = latents
|
||||
|
||||
loss_info.update(encoder_info)
|
||||
|
||||
# Encode with teacher model for distillation
|
||||
if self.teacher_model is not None:
|
||||
with torch.no_grad():
|
||||
teacher_latents = self.teacher_model.encode(encoder_input, return_info=False)
|
||||
loss_info['teacher_latents'] = teacher_latents
|
||||
|
||||
# Optionally mask out some latents for noise resistance
|
||||
if self.latent_mask_ratio > 0.0:
|
||||
mask = torch.rand_like(latents) < self.latent_mask_ratio
|
||||
latents = torch.where(mask, torch.zeros_like(latents), latents)
|
||||
decoded = self.autoencoder.decode(latents)
|
||||
|
||||
loss_info["decoded"] = decoded
|
||||
|
||||
if self.autoencoder.out_channels == 2:
|
||||
loss_info["decoded_left"] = decoded[:, 0:1, :]
|
||||
loss_info["decoded_right"] = decoded[:, 1:2, :]
|
||||
loss_info["reals_left"] = reals[:, 0:1, :]
|
||||
loss_info["reals_right"] = reals[:, 1:2, :]
|
||||
elif self.autoencoder.out_channels == 4:
|
||||
loss_info["decoded_w"] = decoded[:, 0:1, :]
|
||||
loss_info["decoded_x"] = decoded[:, 1:2, :]
|
||||
loss_info["decoded_y"] = decoded[:, 2:3, :]
|
||||
loss_info["decoded_z"] = decoded[:, 3:4, :]
|
||||
loss_info["reals_w"] = reals[:, 0:1, :]
|
||||
loss_info["reals_x"] = reals[:, 1:2, :]
|
||||
loss_info["reals_y"] = reals[:, 2:3, :]
|
||||
loss_info["reals_z"] = reals[:, 3:4, :]
|
||||
|
||||
# Distillation
|
||||
if self.teacher_model is not None:
|
||||
with torch.no_grad():
|
||||
teacher_decoded = self.teacher_model.decode(teacher_latents)
|
||||
own_latents_teacher_decoded = self.teacher_model.decode(latents) #Distilled model's latents decoded by teacher
|
||||
teacher_latents_own_decoded = self.autoencoder.decode(teacher_latents) #Teacher's latents decoded by distilled model
|
||||
|
||||
loss_info['teacher_decoded'] = teacher_decoded
|
||||
loss_info['own_latents_teacher_decoded'] = own_latents_teacher_decoded
|
||||
loss_info['teacher_latents_own_decoded'] = teacher_latents_own_decoded
|
||||
|
||||
|
||||
if self.warmed_up:
|
||||
loss_dis, loss_adv, feature_matching_distance = self.discriminator.loss(reals, decoded)
|
||||
else:
|
||||
loss_dis = torch.tensor(0.).to(reals)
|
||||
loss_adv = torch.tensor(0.).to(reals)
|
||||
feature_matching_distance = torch.tensor(0.).to(reals)
|
||||
|
||||
loss_info["loss_dis"] = loss_dis
|
||||
loss_info["loss_adv"] = loss_adv
|
||||
loss_info["feature_matching_distance"] = feature_matching_distance
|
||||
|
||||
opt_gen, opt_disc = self.optimizers()
|
||||
|
||||
lr_schedulers = self.lr_schedulers()
|
||||
|
||||
sched_gen = None
|
||||
sched_disc = None
|
||||
|
||||
if lr_schedulers is not None:
|
||||
sched_gen, sched_disc = lr_schedulers
|
||||
|
||||
# Train the discriminator
|
||||
if self.global_step % 2 and self.warmed_up:
|
||||
loss, losses = self.losses_disc(loss_info)
|
||||
|
||||
log_dict = {
|
||||
'train/disc_lr': opt_disc.param_groups[0]['lr']
|
||||
}
|
||||
|
||||
opt_disc.zero_grad()
|
||||
self.manual_backward(loss)
|
||||
|
||||
|
||||
opt_disc.step()
|
||||
|
||||
if sched_disc is not None:
|
||||
# sched step every step
|
||||
sched_disc.step()
|
||||
|
||||
# Train the generator
|
||||
else:
|
||||
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
loss, losses = self.losses_gen(loss_info)
|
||||
|
||||
if self.use_ema:
|
||||
self.autoencoder_ema.update()
|
||||
|
||||
opt_gen.zero_grad()
|
||||
self.manual_backward(loss)
|
||||
opt_gen.step()
|
||||
|
||||
if sched_gen is not None:
|
||||
# scheduler step every step
|
||||
sched_gen.step()
|
||||
|
||||
log_dict = {
|
||||
'train/loss': loss.detach(),
|
||||
'train/latent_std': latents.std().detach(),
|
||||
'train/data_std': data_std.detach(),
|
||||
'train/gen_lr': opt_gen.param_groups[0]['lr']
|
||||
}
|
||||
|
||||
for loss_name, loss_value in losses.items():
|
||||
log_dict[f'train/{loss_name}'] = loss_value.detach()
|
||||
|
||||
self.log_dict(log_dict, prog_bar=True, on_step=True)
|
||||
|
||||
return loss
|
||||
|
||||
def export_model(self, path, use_safetensors=False):
|
||||
if self.autoencoder_ema is not None:
|
||||
model = self.autoencoder_ema.ema_model
|
||||
else:
|
||||
model = self.autoencoder
|
||||
|
||||
if use_safetensors:
|
||||
save_model(model, path)
|
||||
else:
|
||||
torch.save({"state_dict": model.state_dict()}, path)
|
||||
|
||||
|
||||
class AutoencoderDemoCallback(Callback):
|
||||
def __init__(
|
||||
self,
|
||||
demo_dl,
|
||||
demo_every=2000,
|
||||
sample_size=65536,
|
||||
sample_rate=48000
|
||||
):
|
||||
super().__init__()
|
||||
self.demo_every = demo_every
|
||||
self.demo_samples = sample_size
|
||||
self.demo_dl = iter(demo_dl)
|
||||
self.sample_rate = sample_rate
|
||||
self.last_demo_step = -1
|
||||
|
||||
@rank_zero_only
|
||||
@torch.no_grad()
|
||||
def on_train_batch_end(self, trainer, module, outputs, batch, batch_idx):
|
||||
if (trainer.global_step - 1) % self.demo_every != 0 or self.last_demo_step == trainer.global_step:
|
||||
return
|
||||
|
||||
self.last_demo_step = trainer.global_step
|
||||
|
||||
module.eval()
|
||||
|
||||
try:
|
||||
demo_reals, _ = next(self.demo_dl)
|
||||
|
||||
# Remove extra dimension added by WebDataset
|
||||
if demo_reals.ndim == 4 and demo_reals.shape[0] == 1:
|
||||
demo_reals = demo_reals[0]
|
||||
|
||||
encoder_input = demo_reals
|
||||
|
||||
encoder_input = encoder_input.to(module.device)
|
||||
|
||||
if module.force_input_mono:
|
||||
encoder_input = encoder_input.mean(dim=1, keepdim=True)
|
||||
|
||||
demo_reals = demo_reals.to(module.device)
|
||||
|
||||
with torch.no_grad():
|
||||
if module.use_ema:
|
||||
|
||||
latents = module.autoencoder_ema.ema_model.encode(encoder_input)
|
||||
|
||||
fakes = module.autoencoder_ema.ema_model.decode(latents)
|
||||
else:
|
||||
latents = module.autoencoder.encode(encoder_input)
|
||||
|
||||
fakes = module.autoencoder.decode(latents)
|
||||
|
||||
#Interleave reals and fakes
|
||||
reals_fakes = rearrange([demo_reals, fakes], 'i b d n -> (b i) d n')
|
||||
|
||||
# Put the demos together
|
||||
reals_fakes = rearrange(reals_fakes, 'b d n -> d (b n)')
|
||||
|
||||
log_dict = {}
|
||||
|
||||
filename = f'demos/recon_{trainer.global_step:08}.wav'
|
||||
reals_fakes = reals_fakes.to(torch.float32).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
|
||||
torchaudio.save(filename, reals_fakes, self.sample_rate)
|
||||
|
||||
log_dict[f'recon'] = wandb.Audio(filename,
|
||||
sample_rate=self.sample_rate,
|
||||
caption=f'Reconstructed')
|
||||
|
||||
log_dict[f'embeddings_3dpca'] = pca_point_cloud(latents)
|
||||
log_dict[f'embeddings_spec'] = wandb.Image(tokens_spectrogram_image(latents))
|
||||
|
||||
log_dict[f'recon_melspec_left'] = wandb.Image(audio_spectrogram_image(reals_fakes))
|
||||
|
||||
trainer.logger.experiment.log(log_dict)
|
||||
except Exception as e:
|
||||
print(f'{type(e).__name__}: {e}')
|
||||
raise e
|
||||
finally:
|
||||
module.train()
|
||||
|
||||
def create_loss_modules_from_bottleneck(bottleneck, loss_config):
|
||||
losses = []
|
||||
|
||||
if isinstance(bottleneck, VAEBottleneck) or isinstance(bottleneck, DACRVQVAEBottleneck) or isinstance(bottleneck, RVQVAEBottleneck):
|
||||
try:
|
||||
kl_weight = loss_config['bottleneck']['weights']['kl']
|
||||
except:
|
||||
kl_weight = 1e-6
|
||||
|
||||
kl_loss = ValueLoss(key='kl', weight=kl_weight, name='kl_loss')
|
||||
losses.append(kl_loss)
|
||||
|
||||
if isinstance(bottleneck, RVQBottleneck) or isinstance(bottleneck, RVQVAEBottleneck):
|
||||
quantizer_loss = ValueLoss(key='quantizer_loss', weight=1.0, name='quantizer_loss')
|
||||
losses.append(quantizer_loss)
|
||||
|
||||
if isinstance(bottleneck, DACRVQBottleneck) or isinstance(bottleneck, DACRVQVAEBottleneck):
|
||||
codebook_loss = ValueLoss(key='vq/codebook_loss', weight=1.0, name='codebook_loss')
|
||||
commitment_loss = ValueLoss(key='vq/commitment_loss', weight=0.25, name='commitment_loss')
|
||||
losses.append(codebook_loss)
|
||||
losses.append(commitment_loss)
|
||||
|
||||
if isinstance(bottleneck, WassersteinBottleneck):
|
||||
try:
|
||||
mmd_weight = loss_config['bottleneck']['weights']['mmd']
|
||||
except:
|
||||
mmd_weight = 100
|
||||
|
||||
mmd_loss = ValueLoss(key='mmd', weight=mmd_weight, name='mmd_loss')
|
||||
losses.append(mmd_loss)
|
||||
|
||||
return losses
|
||||
@@ -0,0 +1,599 @@
|
||||
# import pytorch_lightning as pl
|
||||
import lightning as L
|
||||
from lightning.pytorch.callbacks import Callback
|
||||
import sys, gc
|
||||
import random
|
||||
import torch
|
||||
import torchaudio
|
||||
import typing as tp
|
||||
import wandb
|
||||
from aeiou.viz import audio_spectrogram_image
|
||||
from ema_pytorch import EMA
|
||||
from einops import rearrange
|
||||
from safetensors.torch import save_file
|
||||
from torch import optim
|
||||
from torch.nn import functional as F
|
||||
from pytorch_lightning.utilities.rank_zero import rank_zero_only
|
||||
from ..inference.sampling import get_alphas_sigmas, sample, sample_discrete_euler
|
||||
from ..models.diffusion import DiffusionModelWrapper, ConditionedDiffusionModelWrapper
|
||||
from ..models.autoencoders import DiffusionAutoencoder
|
||||
from .autoencoders import create_loss_modules_from_bottleneck
|
||||
from .losses import MSELoss, MultiLoss
|
||||
from .utils import create_optimizer_from_config, create_scheduler_from_config, generate_mask, generate_channel_mask
|
||||
import os
|
||||
from pathlib import Path
|
||||
from time import time
|
||||
import numpy as np
|
||||
|
||||
class Profiler:
|
||||
|
||||
def __init__(self):
|
||||
self.ticks = [[time(), None]]
|
||||
|
||||
def tick(self, msg):
|
||||
self.ticks.append([time(), msg])
|
||||
|
||||
def __repr__(self):
|
||||
rep = 80 * "=" + "\n"
|
||||
for i in range(1, len(self.ticks)):
|
||||
msg = self.ticks[i][1]
|
||||
ellapsed = self.ticks[i][0] - self.ticks[i - 1][0]
|
||||
rep += msg + f": {ellapsed*1000:.2f}ms\n"
|
||||
rep += 80 * "=" + "\n\n\n"
|
||||
return rep
|
||||
|
||||
class DiffusionCondTrainingWrapper(L.LightningModule):
|
||||
'''
|
||||
Wrapper for training a conditional audio diffusion model.
|
||||
'''
|
||||
def __init__(
|
||||
self,
|
||||
model: ConditionedDiffusionModelWrapper,
|
||||
lr: float = None,
|
||||
mask_padding: bool = False,
|
||||
mask_padding_dropout: float = 0.0,
|
||||
use_ema: bool = True,
|
||||
log_loss_info: bool = False,
|
||||
optimizer_configs: dict = None,
|
||||
diffusion_objective: tp.Literal["rectified_flow", "v"] = "v",
|
||||
pre_encoded: bool = False,
|
||||
cfg_dropout_prob = 0.1,
|
||||
timestep_sampler: tp.Literal["uniform", "logit_normal"] = "uniform",
|
||||
max_mask_segments = 0,
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.diffusion = model
|
||||
|
||||
if use_ema:
|
||||
self.diffusion_ema = EMA(
|
||||
self.diffusion.model,
|
||||
beta=0.9999,
|
||||
power=3/4,
|
||||
update_every=1,
|
||||
update_after_step=1,
|
||||
include_online_model=False
|
||||
)
|
||||
else:
|
||||
self.diffusion_ema = None
|
||||
|
||||
self.mask_padding = mask_padding
|
||||
self.mask_padding_dropout = mask_padding_dropout
|
||||
|
||||
self.cfg_dropout_prob = cfg_dropout_prob
|
||||
|
||||
self.rng = torch.quasirandom.SobolEngine(1, scramble=True)
|
||||
|
||||
self.timestep_sampler = timestep_sampler
|
||||
|
||||
self.diffusion_objective = model.diffusion_objective
|
||||
print(f'Training in the {self.diffusion_objective} formulation with timestep sampler: {timestep_sampler}')
|
||||
|
||||
self.max_mask_segments = max_mask_segments
|
||||
|
||||
self.loss_modules = [
|
||||
MSELoss("output",
|
||||
"targets",
|
||||
weight=1.0,
|
||||
mask_key="padding_mask" if self.mask_padding else None,
|
||||
name="mse_loss"
|
||||
)
|
||||
]
|
||||
|
||||
self.losses = MultiLoss(self.loss_modules)
|
||||
|
||||
self.log_loss_info = log_loss_info
|
||||
|
||||
assert lr is not None or optimizer_configs is not None, "Must specify either lr or optimizer_configs in training config"
|
||||
|
||||
if optimizer_configs is None:
|
||||
optimizer_configs = {
|
||||
"diffusion": {
|
||||
"optimizer": {
|
||||
"type": "Adam",
|
||||
"config": {
|
||||
"lr": lr
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else:
|
||||
if lr is not None:
|
||||
print(f"WARNING: learning_rate and optimizer_configs both specified in config. Ignoring learning_rate and using optimizer_configs.")
|
||||
|
||||
self.optimizer_configs = optimizer_configs
|
||||
|
||||
self.pre_encoded = pre_encoded
|
||||
|
||||
def configure_optimizers(self):
|
||||
diffusion_opt_config = self.optimizer_configs['diffusion']
|
||||
opt_diff = create_optimizer_from_config(diffusion_opt_config['optimizer'], self.diffusion.parameters())
|
||||
|
||||
if "scheduler" in diffusion_opt_config:
|
||||
sched_diff = create_scheduler_from_config(diffusion_opt_config['scheduler'], opt_diff)
|
||||
sched_diff_config = {
|
||||
"scheduler": sched_diff,
|
||||
"interval": "step"
|
||||
}
|
||||
return [opt_diff], [sched_diff_config]
|
||||
|
||||
return [opt_diff]
|
||||
|
||||
def training_step(self, batch, batch_idx):
|
||||
reals, metadata = batch
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
p = Profiler()
|
||||
if reals.ndim == 4 and reals.shape[0] == 1:
|
||||
reals = reals[0]
|
||||
|
||||
loss_info = {}
|
||||
|
||||
diffusion_input = reals
|
||||
if not self.pre_encoded:
|
||||
loss_info["audio_reals"] = diffusion_input
|
||||
|
||||
p.tick("setup")
|
||||
|
||||
with torch.amp.autocast('cuda'):
|
||||
|
||||
conditioning = self.diffusion.conditioner(metadata, self.device)
|
||||
|
||||
|
||||
video_exist = torch.stack([item['video_exist'] for item in metadata],dim=0)
|
||||
conditioning['metaclip_features'][~video_exist] = self.diffusion.model.model.empty_clip_feat
|
||||
conditioning['sync_features'][~video_exist] = self.diffusion.model.model.empty_sync_feat
|
||||
# If mask_padding is on, randomly drop the padding masks to allow for learning silence padding
|
||||
use_padding_mask = self.mask_padding and random.random() > self.mask_padding_dropout
|
||||
|
||||
# Create batch tensor of attention masks from the "mask" field of the metadata array
|
||||
if use_padding_mask:
|
||||
padding_masks = torch.stack([md["padding_mask"][0] for md in metadata], dim=0).to(self.device) # Shape (batch_size, sequence_length)
|
||||
|
||||
p.tick("conditioning")
|
||||
|
||||
if self.diffusion.pretransform is not None:
|
||||
self.diffusion.pretransform.to(self.device)
|
||||
|
||||
if not self.pre_encoded:
|
||||
with torch.amp.autocast('cuda') and torch.set_grad_enabled(self.diffusion.pretransform.enable_grad):
|
||||
self.diffusion.pretransform.train(self.diffusion.pretransform.enable_grad)
|
||||
|
||||
diffusion_input = self.diffusion.pretransform.encode(diffusion_input)
|
||||
p.tick("pretransform")
|
||||
|
||||
# If mask_padding is on, interpolate the padding masks to the size of the pretransformed input
|
||||
if use_padding_mask:
|
||||
padding_masks = F.interpolate(padding_masks.unsqueeze(1).float(), size=diffusion_input.shape[2], mode="nearest").squeeze(1).bool()
|
||||
else:
|
||||
# Apply scale to pre-encoded latents if needed, as the pretransform encode function will not be run
|
||||
if hasattr(self.diffusion.pretransform, "scale") and self.diffusion.pretransform.scale != 1.0:
|
||||
diffusion_input = diffusion_input / self.diffusion.pretransform.scale
|
||||
|
||||
if self.max_mask_segments > 0:
|
||||
# Max mask size is the full sequence length
|
||||
max_mask_length = diffusion_input.shape[2]
|
||||
|
||||
# Create a mask of random length for a random slice of the input
|
||||
masked_input, mask = self.random_mask(diffusion_input, max_mask_length)
|
||||
|
||||
conditioning['inpaint_mask'] = [mask]
|
||||
conditioning['inpaint_masked_input'] = masked_input
|
||||
|
||||
if self.timestep_sampler == "uniform":
|
||||
# Draw uniformly distributed continuous timesteps
|
||||
t = self.rng.draw(reals.shape[0])[:, 0].to(self.device)
|
||||
elif self.timestep_sampler == "logit_normal":
|
||||
t = torch.sigmoid(torch.randn(reals.shape[0], device=self.device))
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
# Calculate the noise schedule parameters for those timesteps
|
||||
if self.diffusion_objective == "v":
|
||||
alphas, sigmas = get_alphas_sigmas(t)
|
||||
elif self.diffusion_objective == "rectified_flow":
|
||||
alphas, sigmas = 1-t, t
|
||||
|
||||
# Combine the ground truth data and the noise
|
||||
alphas = alphas[:, None, None]
|
||||
sigmas = sigmas[:, None, None]
|
||||
noise = torch.randn_like(diffusion_input)
|
||||
noised_inputs = diffusion_input * alphas + noise * sigmas
|
||||
|
||||
if self.diffusion_objective == "v":
|
||||
targets = noise * alphas - diffusion_input * sigmas
|
||||
elif self.diffusion_objective == "rectified_flow":
|
||||
targets = noise - diffusion_input
|
||||
|
||||
p.tick("noise")
|
||||
|
||||
extra_args = {}
|
||||
|
||||
if use_padding_mask:
|
||||
extra_args["mask"] = padding_masks
|
||||
|
||||
with torch.amp.autocast('cuda'):
|
||||
p.tick("amp")
|
||||
output = self.diffusion(noised_inputs, t, cond=conditioning, cfg_dropout_prob = self.cfg_dropout_prob, **extra_args)
|
||||
p.tick("diffusion")
|
||||
|
||||
loss_info.update({
|
||||
"output": output,
|
||||
"targets": targets,
|
||||
"padding_mask": padding_masks if use_padding_mask else None,
|
||||
})
|
||||
|
||||
loss, losses = self.losses(loss_info)
|
||||
|
||||
p.tick("loss")
|
||||
|
||||
if self.log_loss_info:
|
||||
# Loss debugging logs
|
||||
num_loss_buckets = 10
|
||||
bucket_size = 1 / num_loss_buckets
|
||||
loss_all = F.mse_loss(output, targets, reduction="none")
|
||||
|
||||
sigmas = rearrange(self.all_gather(sigmas), "w b c n -> (w b) c n").squeeze()
|
||||
|
||||
# gather loss_all across all GPUs
|
||||
loss_all = rearrange(self.all_gather(loss_all), "w b c n -> (w b) c n")
|
||||
|
||||
# Bucket loss values based on corresponding sigma values, bucketing sigma values by bucket_size
|
||||
loss_all = torch.stack([loss_all[(sigmas >= i) & (sigmas < i + bucket_size)].mean() for i in torch.arange(0, 1, bucket_size).to(self.device)])
|
||||
|
||||
# Log bucketed losses with corresponding sigma bucket values, if it's not NaN
|
||||
debug_log_dict = {
|
||||
f"model/loss_all_{i/num_loss_buckets:.1f}": loss_all[i].detach() for i in range(num_loss_buckets) if not torch.isnan(loss_all[i])
|
||||
}
|
||||
|
||||
self.log_dict(debug_log_dict)
|
||||
|
||||
|
||||
log_dict = {
|
||||
'train/loss': loss.detach(),
|
||||
'train/std_data': diffusion_input.std(),
|
||||
'train/lr': self.trainer.optimizers[0].param_groups[0]['lr']
|
||||
}
|
||||
|
||||
for loss_name, loss_value in losses.items():
|
||||
log_dict[f"train/{loss_name}"] = loss_value.detach()
|
||||
|
||||
self.log_dict(log_dict, prog_bar=True, on_step=True)
|
||||
p.tick("log")
|
||||
#print(f"Profiler: {p}")
|
||||
return loss
|
||||
|
||||
def validation_step(self, batch, batch_idx):
|
||||
reals, metadata = batch
|
||||
# breakpoint()
|
||||
if reals.ndim == 4 and reals.shape[0] == 1:
|
||||
reals = reals[0]
|
||||
|
||||
loss_info = {}
|
||||
|
||||
diffusion_input = reals
|
||||
|
||||
if not self.pre_encoded:
|
||||
loss_info["audio_reals"] = diffusion_input
|
||||
|
||||
|
||||
with torch.amp.autocast('cuda'):
|
||||
|
||||
conditioning = self.diffusion.conditioner(metadata, self.device)
|
||||
|
||||
video_exist = torch.stack([item['video_exist'] for item in metadata],dim=0)
|
||||
conditioning['metaclip_features'][~video_exist] = self.diffusion.model.model.empty_clip_feat
|
||||
conditioning['sync_features'][~video_exist] = self.diffusion.model.model.empty_sync_feat
|
||||
|
||||
if self.diffusion.pretransform is not None:
|
||||
|
||||
if not self.pre_encoded:
|
||||
self.diffusion.pretransform.to(self.device)
|
||||
with torch.amp.autocast('cuda') and torch.set_grad_enabled(self.diffusion.pretransform.enable_grad):
|
||||
self.diffusion.pretransform.train(self.diffusion.pretransform.enable_grad)
|
||||
|
||||
diffusion_input = self.diffusion.pretransform.encode(diffusion_input)
|
||||
else:
|
||||
# Apply scale to pre-encoded latents if needed, as the pretransform encode function will not be run
|
||||
if hasattr(self.diffusion.pretransform, "scale") and self.diffusion.pretransform.scale != 1.0:
|
||||
diffusion_input = diffusion_input / self.diffusion.pretransform.scale
|
||||
if self.max_mask_segments > 0:
|
||||
# Max mask size is the full sequence length
|
||||
max_mask_length = diffusion_input.shape[2]
|
||||
|
||||
# Create a mask of random length for a random slice of the input
|
||||
masked_input, mask = self.random_mask(diffusion_input, max_mask_length)
|
||||
|
||||
conditioning['inpaint_mask'] = [mask]
|
||||
conditioning['inpaint_masked_input'] = masked_input
|
||||
if self.timestep_sampler == "uniform":
|
||||
# Draw uniformly distributed continuous timesteps
|
||||
t = self.rng.draw(reals.shape[0])[:, 0].to(self.device)
|
||||
elif self.timestep_sampler == "logit_normal":
|
||||
t = torch.sigmoid(torch.randn(reals.shape[0], device=self.device))
|
||||
|
||||
# Calculate the noise schedule parameters for those timesteps
|
||||
if self.diffusion_objective == "v":
|
||||
alphas, sigmas = get_alphas_sigmas(t)
|
||||
elif self.diffusion_objective == "rectified_flow":
|
||||
alphas, sigmas = 1-t, t
|
||||
|
||||
# Combine the ground truth data and the noise
|
||||
alphas = alphas[:, None, None]
|
||||
sigmas = sigmas[:, None, None]
|
||||
noise = torch.randn_like(diffusion_input)
|
||||
noised_inputs = diffusion_input * alphas + noise * sigmas
|
||||
|
||||
if self.diffusion_objective == "v":
|
||||
targets = noise * alphas - diffusion_input * sigmas
|
||||
elif self.diffusion_objective == "rectified_flow":
|
||||
targets = noise - diffusion_input
|
||||
|
||||
|
||||
with torch.amp.autocast('cuda'):
|
||||
output = self.diffusion(noised_inputs, t, cond=conditioning, cfg_dropout_prob = 0.0)
|
||||
|
||||
loss_info.update({
|
||||
"output": output,
|
||||
"targets": targets,
|
||||
})
|
||||
|
||||
loss, losses = self.losses(loss_info)
|
||||
|
||||
|
||||
log_dict = {
|
||||
'val_loss': loss.detach(),
|
||||
}
|
||||
|
||||
self.log_dict(log_dict, prog_bar=True, batch_size=diffusion_input.size(0))
|
||||
|
||||
def predict_step(self, batch, batch_idx):
|
||||
reals, metadata = batch
|
||||
ids = [item['id'] for item in metadata]
|
||||
batch_size, length = reals.shape[0], reals.shape[2]
|
||||
print(f"Predicting {batch_size} samples with length {length} for ids: {ids}")
|
||||
with torch.amp.autocast('cuda'):
|
||||
conditioning = self.diffusion.conditioner(metadata, self.device)
|
||||
|
||||
video_exist = torch.stack([item['video_exist'] for item in metadata],dim=0)
|
||||
conditioning['metaclip_features'][~video_exist] = self.diffusion.model.model.empty_clip_feat
|
||||
conditioning['sync_features'][~video_exist] = self.diffusion.model.model.empty_sync_feat
|
||||
|
||||
cond_inputs = self.diffusion.get_conditioning_inputs(conditioning)
|
||||
if batch_size > 1:
|
||||
noise_list = []
|
||||
for _ in range(batch_size):
|
||||
noise_1 = torch.randn([1, self.diffusion.io_channels, length]).to(self.device) # 每次生成推进RNG状态
|
||||
noise_list.append(noise_1)
|
||||
noise = torch.cat(noise_list, dim=0)
|
||||
else:
|
||||
noise = torch.randn([batch_size, self.diffusion.io_channels, length]).to(self.device)
|
||||
with torch.amp.autocast('cuda'):
|
||||
|
||||
model = self.diffusion.model
|
||||
if self.diffusion_objective == "v":
|
||||
fakes = sample(model, noise, 24, 0, **cond_inputs, cfg_scale=5, batch_cfg=True)
|
||||
elif self.diffusion_objective == "rectified_flow":
|
||||
import time
|
||||
start_time = time.time()
|
||||
fakes = sample_discrete_euler(model, noise, 24, **cond_inputs, cfg_scale=5, batch_cfg=True)
|
||||
end_time = time.time()
|
||||
execution_time = end_time - start_time
|
||||
print(f"执行时间: {execution_time:.2f} 秒")
|
||||
if self.diffusion.pretransform is not None:
|
||||
fakes = self.diffusion.pretransform.decode(fakes)
|
||||
|
||||
audios = fakes.to(torch.float32).div(torch.max(torch.abs(fakes))).clamp(-1, 1).mul(32767).to(torch.int16).cpu()
|
||||
return audios
|
||||
# # Put the demos together
|
||||
# fakes = rearrange(fakes, 'b d n -> d (b n)')
|
||||
|
||||
def random_mask(self, sequence, max_mask_length):
|
||||
b, _, sequence_length = sequence.size()
|
||||
|
||||
# Create a mask tensor for each batch element
|
||||
masks = []
|
||||
|
||||
for i in range(b):
|
||||
mask_type = random.randint(0, 2)
|
||||
|
||||
if mask_type == 0: # Random mask with multiple segments
|
||||
num_segments = random.randint(1, self.max_mask_segments)
|
||||
max_segment_length = max_mask_length // num_segments
|
||||
|
||||
segment_lengths = random.sample(range(1, max_segment_length + 1), num_segments)
|
||||
|
||||
mask = torch.ones((1, 1, sequence_length))
|
||||
for length in segment_lengths:
|
||||
mask_start = random.randint(0, sequence_length - length)
|
||||
mask[:, :, mask_start:mask_start + length] = 0
|
||||
|
||||
elif mask_type == 1: # Full mask
|
||||
mask = torch.zeros((1, 1, sequence_length))
|
||||
|
||||
elif mask_type == 2: # Causal mask
|
||||
mask = torch.ones((1, 1, sequence_length))
|
||||
mask_length = random.randint(1, max_mask_length)
|
||||
mask[:, :, -mask_length:] = 0
|
||||
|
||||
mask = mask.to(sequence.device)
|
||||
masks.append(mask)
|
||||
|
||||
# Concatenate the mask tensors into a single tensor
|
||||
mask = torch.cat(masks, dim=0).to(sequence.device)
|
||||
|
||||
# Apply the mask to the sequence tensor for each batch element
|
||||
masked_sequence = sequence * mask
|
||||
|
||||
return masked_sequence, mask
|
||||
|
||||
def on_before_zero_grad(self, *args, **kwargs):
|
||||
if self.diffusion_ema is not None:
|
||||
self.diffusion_ema.update()
|
||||
|
||||
def export_model(self, path, use_safetensors=False):
|
||||
if self.diffusion_ema is not None:
|
||||
self.diffusion.model = self.diffusion_ema.ema_model
|
||||
|
||||
if use_safetensors:
|
||||
save_file(self.diffusion.state_dict(), path)
|
||||
else:
|
||||
torch.save({"state_dict": self.diffusion.state_dict()}, path)
|
||||
|
||||
class DiffusionCondDemoCallback(Callback):
|
||||
def __init__(self,
|
||||
demo_every=2000,
|
||||
num_demos=8,
|
||||
sample_size=65536,
|
||||
demo_steps=250,
|
||||
sample_rate=48000,
|
||||
demo_conditioning: tp.Optional[tp.Dict[str, tp.Any]] = {},
|
||||
demo_cfg_scales: tp.Optional[tp.List[int]] = [3, 5, 7],
|
||||
demo_cond_from_batch: bool = False,
|
||||
display_audio_cond: bool = False
|
||||
):
|
||||
super().__init__()
|
||||
|
||||
self.demo_every = demo_every
|
||||
self.num_demos = num_demos
|
||||
self.demo_samples = sample_size
|
||||
self.demo_steps = demo_steps
|
||||
self.sample_rate = sample_rate
|
||||
self.last_demo_step = -1
|
||||
self.demo_conditioning = demo_conditioning
|
||||
self.demo_cfg_scales = demo_cfg_scales
|
||||
|
||||
# If true, the callback will use the metadata from the batch to generate the demo conditioning
|
||||
self.demo_cond_from_batch = demo_cond_from_batch
|
||||
|
||||
# If true, the callback will display the audio conditioning
|
||||
self.display_audio_cond = display_audio_cond
|
||||
|
||||
@rank_zero_only
|
||||
@torch.no_grad()
|
||||
def on_train_batch_end(self, trainer, module: DiffusionCondTrainingWrapper, outputs, batch, batch_idx):
|
||||
|
||||
if (trainer.global_step - 1) % self.demo_every != 0 or self.last_demo_step == trainer.global_step:
|
||||
return
|
||||
|
||||
module.eval()
|
||||
|
||||
print(f"Generating demo")
|
||||
self.last_demo_step = trainer.global_step
|
||||
|
||||
demo_samples = self.demo_samples
|
||||
|
||||
demo_cond = self.demo_conditioning
|
||||
|
||||
if self.demo_cond_from_batch:
|
||||
# Get metadata from the batch
|
||||
demo_cond = batch[1][:self.num_demos]
|
||||
|
||||
if '.pth' in demo_cond[0]:
|
||||
demo_cond_data = []
|
||||
for path in demo_cond:
|
||||
# info = {}
|
||||
data = torch.load(path, weights_only=True)
|
||||
if 'caption_t5' not in data.keys():
|
||||
data['caption_t5'] = data['caption']
|
||||
data['seconds_start'] = 0
|
||||
data['seconds_total'] = 10
|
||||
demo_cond_data.append(data)
|
||||
demo_cond = demo_cond_data
|
||||
elif '.npz' in demo_cond[0]:
|
||||
demo_cond_data = []
|
||||
for path in demo_cond:
|
||||
# info = {}
|
||||
npz_data = np.load(path,allow_pickle=True)
|
||||
data = {key: npz_data[key] for key in npz_data.files}
|
||||
for key in data.keys():
|
||||
# print(key)
|
||||
if isinstance(data[key], np.ndarray) and np.issubdtype(data[key].dtype, np.number):
|
||||
data[key] = torch.from_numpy(data[key])
|
||||
|
||||
demo_cond_data.append(data)
|
||||
demo_cond = demo_cond_data
|
||||
if module.diffusion.pretransform is not None:
|
||||
demo_samples = demo_samples // module.diffusion.pretransform.downsampling_ratio
|
||||
|
||||
noise = torch.randn([self.num_demos, module.diffusion.io_channels, demo_samples]).to(module.device)
|
||||
|
||||
try:
|
||||
print("Getting conditioning")
|
||||
with torch.amp.autocast('cuda'):
|
||||
conditioning = module.diffusion.conditioner(demo_cond, module.device)
|
||||
|
||||
cond_inputs = module.diffusion.get_conditioning_inputs(conditioning)
|
||||
|
||||
log_dict = {}
|
||||
|
||||
if self.display_audio_cond:
|
||||
audio_inputs = torch.cat([cond["audio"] for cond in demo_cond], dim=0)
|
||||
audio_inputs = rearrange(audio_inputs, 'b d n -> d (b n)')
|
||||
|
||||
filename = f'demo_audio_cond_{trainer.global_step:08}.wav'
|
||||
audio_inputs = audio_inputs.to(torch.float32).mul(32767).to(torch.int16).cpu()
|
||||
torchaudio.save(filename, audio_inputs, self.sample_rate)
|
||||
log_dict[f'demo_audio_cond'] = wandb.Audio(filename, sample_rate=self.sample_rate, caption="Audio conditioning")
|
||||
log_dict[f"demo_audio_cond_melspec_left"] = wandb.Image(audio_spectrogram_image(audio_inputs))
|
||||
trainer.logger.experiment.log(log_dict)
|
||||
|
||||
for cfg_scale in self.demo_cfg_scales:
|
||||
|
||||
print(f"Generating demo for cfg scale {cfg_scale}")
|
||||
|
||||
with torch.amp.autocast('cuda'):
|
||||
# model = module.diffusion_ema.model if module.diffusion_ema is not None else module.diffusion.model
|
||||
model = module.diffusion.model
|
||||
|
||||
if module.diffusion_objective == "v":
|
||||
fakes = sample(model, noise, self.demo_steps, 0, **cond_inputs, cfg_scale=cfg_scale, batch_cfg=True)
|
||||
elif module.diffusion_objective == "rectified_flow":
|
||||
fakes = sample_discrete_euler(model, noise, self.demo_steps, **cond_inputs, cfg_scale=cfg_scale, batch_cfg=True)
|
||||
|
||||
if module.diffusion.pretransform is not None:
|
||||
fakes = module.diffusion.pretransform.decode(fakes)
|
||||
|
||||
# Put the demos together
|
||||
fakes = rearrange(fakes, 'b d n -> d (b n)')
|
||||
|
||||
log_dict = {}
|
||||
|
||||
filename = f'demos/demo_cfg_{cfg_scale}_{trainer.global_step:08}.wav'
|
||||
fakes = fakes.div(torch.max(torch.abs(fakes))).mul(32767).to(torch.int16).cpu()
|
||||
torchaudio.save(filename, fakes, self.sample_rate)
|
||||
|
||||
log_dict[f'demo_cfg_{cfg_scale}'] = wandb.Audio(filename,
|
||||
sample_rate=self.sample_rate,
|
||||
caption=f'Reconstructed')
|
||||
|
||||
log_dict[f'demo_melspec_left_cfg_{cfg_scale}'] = wandb.Image(audio_spectrogram_image(fakes))
|
||||
trainer.logger.experiment.log(log_dict)
|
||||
|
||||
del fakes
|
||||
|
||||
except Exception as e:
|
||||
raise e
|
||||
finally:
|
||||
gc.collect()
|
||||
torch.cuda.empty_cache()
|
||||
module.train()
|
||||
@@ -0,0 +1,54 @@
|
||||
import torch
|
||||
from torch.nn import Parameter
|
||||
from ..models.factory import create_model_from_config
|
||||
|
||||
def create_training_wrapper_from_config(model_config, model):
|
||||
model_type = model_config.get('model_type', None)
|
||||
assert model_type is not None, 'model_type must be specified in model config'
|
||||
|
||||
training_config = model_config.get('training', None)
|
||||
assert training_config is not None, 'training config must be specified in model config'
|
||||
if model_type == 'mm_diffusion_cond':
|
||||
from .diffusion import DiffusionCondTrainingWrapper
|
||||
return DiffusionCondTrainingWrapper(
|
||||
model,
|
||||
lr=training_config.get("learning_rate", None),
|
||||
mask_padding=training_config.get("mask_padding", False),
|
||||
mask_padding_dropout=training_config.get("mask_padding_dropout", 0.0),
|
||||
use_ema = training_config.get("use_ema", True),
|
||||
log_loss_info=training_config.get("log_loss_info", False),
|
||||
optimizer_configs=training_config.get("optimizer_configs", None),
|
||||
pre_encoded=training_config.get("pre_encoded", False),
|
||||
diffusion_objective=training_config.get("diffusion_objective","v"),
|
||||
cfg_dropout_prob = training_config.get("cfg_dropout_prob", 0.1),
|
||||
timestep_sampler = training_config.get("timestep_sampler", "uniform"),
|
||||
max_mask_segments = training_config.get("max_mask_segments", 0)
|
||||
)
|
||||
else:
|
||||
raise NotImplementedError(f'Unknown model type: {model_type}')
|
||||
|
||||
def create_demo_callback_from_config(model_config, **kwargs):
|
||||
model_type = model_config.get('model_type', None)
|
||||
assert model_type is not None, 'model_type must be specified in model config'
|
||||
|
||||
training_config = model_config.get('training', None)
|
||||
assert training_config is not None, 'training config must be specified in model config'
|
||||
|
||||
demo_config = training_config.get("demo", {})
|
||||
|
||||
if model_type == 'mm_diffusion_cond':
|
||||
from .diffusion import DiffusionCondDemoCallback
|
||||
|
||||
return DiffusionCondDemoCallback(
|
||||
demo_every=demo_config.get("demo_every", 2000),
|
||||
sample_size=model_config["sample_size"],
|
||||
sample_rate=model_config["sample_rate"],
|
||||
demo_steps=demo_config.get("demo_steps", 250),
|
||||
num_demos=demo_config["num_demos"],
|
||||
demo_cfg_scales=demo_config["demo_cfg_scales"],
|
||||
demo_conditioning=demo_config.get("demo_cond", {}),
|
||||
demo_cond_from_batch=demo_config.get("demo_cond_from_batch", False),
|
||||
display_audio_cond=demo_config.get("display_audio_cond", False),
|
||||
)
|
||||
else:
|
||||
raise NotImplementedError(f'Unknown model type: {model_type}')
|
||||
@@ -0,0 +1 @@
|
||||
from .losses import *
|
||||
@@ -0,0 +1,691 @@
|
||||
# Copied and modified from https://github.com/csteinmetz1/auraloss/blob/main/auraloss/freq.py under Apache License 2.0
|
||||
# You can find the license at LICENSES/LICENSE_AURALOSS.txt
|
||||
|
||||
import torch
|
||||
import numpy as np
|
||||
from typing import List, Any
|
||||
import scipy.signal
|
||||
|
||||
def apply_reduction(losses, reduction="none"):
|
||||
"""Apply reduction to collection of losses."""
|
||||
if reduction == "mean":
|
||||
losses = losses.mean()
|
||||
elif reduction == "sum":
|
||||
losses = losses.sum()
|
||||
return losses
|
||||
|
||||
def compute_direction(w, x, y, z):
|
||||
# 计算各个声道的权重
|
||||
phi = torch.atan2(y, x)
|
||||
theta = torch.atan2(torch.sqrt(x**2 + y**2), z)
|
||||
return phi.unsqueeze(1), theta.unsqueeze(1)
|
||||
|
||||
def get_window(win_type: str, win_length: int):
|
||||
"""Return a window function.
|
||||
|
||||
Args:
|
||||
win_type (str): Window type. Can either be one of the window function provided in PyTorch
|
||||
['hann_window', 'bartlett_window', 'blackman_window', 'hamming_window', 'kaiser_window']
|
||||
or any of the windows provided by [SciPy](https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.windows.get_window.html).
|
||||
win_length (int): Window length
|
||||
|
||||
Returns:
|
||||
win: The window as a 1D torch tensor
|
||||
"""
|
||||
|
||||
try:
|
||||
win = getattr(torch, win_type)(win_length)
|
||||
except:
|
||||
win = torch.from_numpy(scipy.signal.windows.get_window(win_type, win_length))
|
||||
|
||||
return win
|
||||
|
||||
class SumAndDifference(torch.nn.Module):
|
||||
"""Sum and difference signal extraction module."""
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize sum and difference extraction module."""
|
||||
super(SumAndDifference, self).__init__()
|
||||
|
||||
def forward(self, x):
|
||||
"""Calculate forward propagation.
|
||||
|
||||
Args:
|
||||
x (Tensor): Predicted signal (B, #channels, #samples).
|
||||
Returns:
|
||||
Tensor: Sum signal.
|
||||
Tensor: Difference signal.
|
||||
"""
|
||||
if not (x.size(1) == 2): # inputs must be stereo
|
||||
raise ValueError(f"Input must be stereo: {x.size(1)} channel(s).")
|
||||
|
||||
sum_sig = self.sum(x).unsqueeze(1)
|
||||
diff_sig = self.diff(x).unsqueeze(1)
|
||||
|
||||
return sum_sig, diff_sig
|
||||
|
||||
@staticmethod
|
||||
def sum(x):
|
||||
return x[:, 0, :] + x[:, 1, :]
|
||||
|
||||
@staticmethod
|
||||
def diff(x):
|
||||
return x[:, 0, :] - x[:, 1, :]
|
||||
|
||||
|
||||
class FIRFilter(torch.nn.Module):
|
||||
"""FIR pre-emphasis filtering module.
|
||||
|
||||
Args:
|
||||
filter_type (str): Shape of the desired FIR filter ("hp", "fd", "aw"). Default: "hp"
|
||||
coef (float): Coefficient value for the filter tap (only applicable for "hp" and "fd"). Default: 0.85
|
||||
ntaps (int): Number of FIR filter taps for constructing A-weighting filters. Default: 101
|
||||
plot (bool): Plot the magnitude respond of the filter. Default: False
|
||||
|
||||
Based upon the perceptual loss pre-empahsis filters proposed by
|
||||
[Wright & Välimäki, 2019](https://arxiv.org/abs/1911.08922).
|
||||
|
||||
A-weighting filter - "aw"
|
||||
First-order highpass - "hp"
|
||||
Folded differentiator - "fd"
|
||||
|
||||
Note that the default coefficeint value of 0.85 is optimized for
|
||||
a sampling rate of 44.1 kHz, considering adjusting this value at differnt sampling rates.
|
||||
"""
|
||||
|
||||
def __init__(self, filter_type="hp", coef=0.85, fs=44100, ntaps=101, plot=False):
|
||||
"""Initilize FIR pre-emphasis filtering module."""
|
||||
super(FIRFilter, self).__init__()
|
||||
self.filter_type = filter_type
|
||||
self.coef = coef
|
||||
self.fs = fs
|
||||
self.ntaps = ntaps
|
||||
self.plot = plot
|
||||
|
||||
import scipy.signal
|
||||
|
||||
if ntaps % 2 == 0:
|
||||
raise ValueError(f"ntaps must be odd (ntaps={ntaps}).")
|
||||
|
||||
if filter_type == "hp":
|
||||
self.fir = torch.nn.Conv1d(1, 1, kernel_size=3, bias=False, padding=1)
|
||||
self.fir.weight.requires_grad = False
|
||||
self.fir.weight.data = torch.tensor([1, -coef, 0]).view(1, 1, -1)
|
||||
elif filter_type == "fd":
|
||||
self.fir = torch.nn.Conv1d(1, 1, kernel_size=3, bias=False, padding=1)
|
||||
self.fir.weight.requires_grad = False
|
||||
self.fir.weight.data = torch.tensor([1, 0, -coef]).view(1, 1, -1)
|
||||
elif filter_type == "aw":
|
||||
# Definition of analog A-weighting filter according to IEC/CD 1672.
|
||||
f1 = 20.598997
|
||||
f2 = 107.65265
|
||||
f3 = 737.86223
|
||||
f4 = 12194.217
|
||||
A1000 = 1.9997
|
||||
|
||||
NUMs = [(2 * np.pi * f4) ** 2 * (10 ** (A1000 / 20)), 0, 0, 0, 0]
|
||||
DENs = np.polymul(
|
||||
[1, 4 * np.pi * f4, (2 * np.pi * f4) ** 2],
|
||||
[1, 4 * np.pi * f1, (2 * np.pi * f1) ** 2],
|
||||
)
|
||||
DENs = np.polymul(
|
||||
np.polymul(DENs, [1, 2 * np.pi * f3]), [1, 2 * np.pi * f2]
|
||||
)
|
||||
|
||||
# convert analog filter to digital filter
|
||||
b, a = scipy.signal.bilinear(NUMs, DENs, fs=fs)
|
||||
|
||||
# compute the digital filter frequency response
|
||||
w_iir, h_iir = scipy.signal.freqz(b, a, worN=512, fs=fs)
|
||||
|
||||
# then we fit to 101 tap FIR filter with least squares
|
||||
taps = scipy.signal.firls(ntaps, w_iir, abs(h_iir), fs=fs)
|
||||
|
||||
# now implement this digital FIR filter as a Conv1d layer
|
||||
self.fir = torch.nn.Conv1d(
|
||||
1, 1, kernel_size=ntaps, bias=False, padding=ntaps // 2
|
||||
)
|
||||
self.fir.weight.requires_grad = False
|
||||
self.fir.weight.data = torch.tensor(taps.astype("float32")).view(1, 1, -1)
|
||||
|
||||
if plot:
|
||||
from .plotting import compare_filters
|
||||
compare_filters(b, a, taps, fs=fs)
|
||||
|
||||
def forward(self, input, target):
|
||||
"""Calculate forward propagation.
|
||||
Args:
|
||||
input (Tensor): Predicted signal (B, #channels, #samples).
|
||||
target (Tensor): Groundtruth signal (B, #channels, #samples).
|
||||
Returns:
|
||||
Tensor: Filtered signal.
|
||||
"""
|
||||
input = torch.nn.functional.conv1d(
|
||||
input, self.fir.weight.data, padding=self.ntaps // 2
|
||||
)
|
||||
target = torch.nn.functional.conv1d(
|
||||
target, self.fir.weight.data, padding=self.ntaps // 2
|
||||
)
|
||||
return input, target
|
||||
|
||||
class SpectralConvergenceLoss(torch.nn.Module):
|
||||
"""Spectral convergence loss module.
|
||||
|
||||
See [Arik et al., 2018](https://arxiv.org/abs/1808.06719).
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
super(SpectralConvergenceLoss, self).__init__()
|
||||
|
||||
def forward(self, x_mag, y_mag):
|
||||
return (torch.norm(y_mag - x_mag, p="fro", dim=[-1, -2]) / torch.norm(y_mag, p="fro", dim=[-1, -2])).mean()
|
||||
|
||||
class STFTMagnitudeLoss(torch.nn.Module):
|
||||
"""STFT magnitude loss module.
|
||||
|
||||
See [Arik et al., 2018](https://arxiv.org/abs/1808.06719)
|
||||
and [Engel et al., 2020](https://arxiv.org/abs/2001.04643v1)
|
||||
|
||||
Log-magnitudes are calculated with `log(log_fac*x + log_eps)`, where `log_fac` controls the
|
||||
compression strength (larger value results in more compression), and `log_eps` can be used
|
||||
to control the range of the compressed output values (e.g., `log_eps>=1` ensures positive
|
||||
output values). The default values `log_fac=1` and `log_eps=0` correspond to plain log-compression.
|
||||
|
||||
Args:
|
||||
log (bool, optional): Log-scale the STFT magnitudes,
|
||||
or use linear scale. Default: True
|
||||
log_eps (float, optional): Constant value added to the magnitudes before evaluating the logarithm.
|
||||
Default: 0.0
|
||||
log_fac (float, optional): Constant multiplication factor for the magnitudes before evaluating the logarithm.
|
||||
Default: 1.0
|
||||
distance (str, optional): Distance function ["L1", "L2"]. Default: "L1"
|
||||
reduction (str, optional): Reduction of the loss elements. Default: "mean"
|
||||
"""
|
||||
|
||||
def __init__(self, log=True, log_eps=0.0, log_fac=1.0, distance="L1", reduction="mean"):
|
||||
super(STFTMagnitudeLoss, self).__init__()
|
||||
|
||||
self.log = log
|
||||
self.log_eps = log_eps
|
||||
self.log_fac = log_fac
|
||||
|
||||
if distance == "L1":
|
||||
self.distance = torch.nn.L1Loss(reduction=reduction)
|
||||
elif distance == "L2":
|
||||
self.distance = torch.nn.MSELoss(reduction=reduction)
|
||||
else:
|
||||
raise ValueError(f"Invalid distance: '{distance}'.")
|
||||
|
||||
def forward(self, x_mag, y_mag):
|
||||
if self.log:
|
||||
x_mag = torch.log(self.log_fac * x_mag + self.log_eps)
|
||||
y_mag = torch.log(self.log_fac * y_mag + self.log_eps)
|
||||
return self.distance(x_mag, y_mag)
|
||||
|
||||
|
||||
class STFTLoss(torch.nn.Module):
|
||||
"""STFT loss module.
|
||||
|
||||
See [Yamamoto et al. 2019](https://arxiv.org/abs/1904.04472).
|
||||
|
||||
Args:
|
||||
fft_size (int, optional): FFT size in samples. Default: 1024
|
||||
hop_size (int, optional): Hop size of the FFT in samples. Default: 256
|
||||
win_length (int, optional): Length of the FFT analysis window. Default: 1024
|
||||
window (str, optional): Window to apply before FFT, can either be one of the window function provided in PyTorch
|
||||
['hann_window', 'bartlett_window', 'blackman_window', 'hamming_window', 'kaiser_window']
|
||||
or any of the windows provided by [SciPy](https://docs.scipy.org/doc/scipy/reference/generated/scipy.signal.windows.get_window.html).
|
||||
Default: 'hann_window'
|
||||
w_sc (float, optional): Weight of the spectral convergence loss term. Default: 1.0
|
||||
w_log_mag (float, optional): Weight of the log magnitude loss term. Default: 1.0
|
||||
w_lin_mag_mag (float, optional): Weight of the linear magnitude loss term. Default: 0.0
|
||||
w_phs (float, optional): Weight of the spectral phase loss term. Default: 0.0
|
||||
sample_rate (int, optional): Sample rate. Required when scale = 'mel'. Default: None
|
||||
scale (str, optional): Optional frequency scaling method, options include:
|
||||
['mel', 'chroma']
|
||||
Default: None
|
||||
n_bins (int, optional): Number of scaling frequency bins. Default: None.
|
||||
perceptual_weighting (bool, optional): Apply perceptual A-weighting (Sample rate must be supplied). Default: False
|
||||
scale_invariance (bool, optional): Perform an optimal scaling of the target. Default: False
|
||||
eps (float, optional): Small epsilon value for stablity. Default: 1e-8
|
||||
output (str, optional): Format of the loss returned.
|
||||
'loss' : Return only the raw, aggregate loss term.
|
||||
'full' : Return the raw loss, plus intermediate loss terms.
|
||||
Default: 'loss'
|
||||
reduction (str, optional): Specifies the reduction to apply to the output:
|
||||
'none': no reduction will be applied,
|
||||
'mean': the sum of the output will be divided by the number of elements in the output,
|
||||
'sum': the output will be summed.
|
||||
Default: 'mean'
|
||||
mag_distance (str, optional): Distance function ["L1", "L2"] for the magnitude loss terms.
|
||||
device (str, optional): Place the filterbanks on specified device. Default: None
|
||||
|
||||
Returns:
|
||||
loss:
|
||||
Aggreate loss term. Only returned if output='loss'. By default.
|
||||
loss, sc_mag_loss, log_mag_loss, lin_mag_loss, phs_loss:
|
||||
Aggregate and intermediate loss terms. Only returned if output='full'.
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
fft_size: int = 1024,
|
||||
hop_size: int = 256,
|
||||
win_length: int = 1024,
|
||||
window: str = "hann_window",
|
||||
w_sc: float = 1.0,
|
||||
w_log_mag: float = 1.0,
|
||||
w_lin_mag: float = 0.0,
|
||||
w_phs: float = 0.0,
|
||||
sample_rate: float = None,
|
||||
scale: str = None,
|
||||
n_bins: int = None,
|
||||
perceptual_weighting: bool = False,
|
||||
scale_invariance: bool = False,
|
||||
eps: float = 1e-8,
|
||||
output: str = "loss",
|
||||
reduction: str = "mean",
|
||||
mag_distance: str = "L1",
|
||||
device: Any = None,
|
||||
**kwargs
|
||||
):
|
||||
super().__init__()
|
||||
self.fft_size = fft_size
|
||||
self.hop_size = hop_size
|
||||
self.win_length = win_length
|
||||
self.window = get_window(window, win_length)
|
||||
self.w_sc = w_sc
|
||||
self.w_log_mag = w_log_mag
|
||||
self.w_lin_mag = w_lin_mag
|
||||
self.w_phs = w_phs
|
||||
self.sample_rate = sample_rate
|
||||
self.scale = scale
|
||||
self.n_bins = n_bins
|
||||
self.perceptual_weighting = perceptual_weighting
|
||||
self.scale_invariance = scale_invariance
|
||||
self.eps = eps
|
||||
self.output = output
|
||||
self.reduction = reduction
|
||||
self.mag_distance = mag_distance
|
||||
self.device = device
|
||||
|
||||
self.phs_used = bool(self.w_phs)
|
||||
|
||||
self.spectralconv = SpectralConvergenceLoss()
|
||||
self.logstft = STFTMagnitudeLoss(
|
||||
log=True,
|
||||
reduction=reduction,
|
||||
distance=mag_distance,
|
||||
**kwargs
|
||||
)
|
||||
self.linstft = STFTMagnitudeLoss(
|
||||
log=False,
|
||||
reduction=reduction,
|
||||
distance=mag_distance,
|
||||
**kwargs
|
||||
)
|
||||
|
||||
# setup mel filterbank
|
||||
if scale is not None:
|
||||
try:
|
||||
import librosa.filters
|
||||
except Exception as e:
|
||||
print(e)
|
||||
print("Try `pip install auraloss[all]`.")
|
||||
|
||||
if self.scale == "mel":
|
||||
assert sample_rate != None # Must set sample rate to use mel scale
|
||||
assert n_bins <= fft_size # Must be more FFT bins than Mel bins
|
||||
fb = librosa.filters.mel(sr=sample_rate, n_fft=fft_size, n_mels=n_bins)
|
||||
fb = torch.tensor(fb).unsqueeze(0)
|
||||
|
||||
elif self.scale == "chroma":
|
||||
assert sample_rate != None # Must set sample rate to use chroma scale
|
||||
assert n_bins <= fft_size # Must be more FFT bins than chroma bins
|
||||
fb = librosa.filters.chroma(
|
||||
sr=sample_rate, n_fft=fft_size, n_chroma=n_bins
|
||||
)
|
||||
|
||||
else:
|
||||
raise ValueError(
|
||||
f"Invalid scale: {self.scale}. Must be 'mel' or 'chroma'."
|
||||
)
|
||||
|
||||
self.register_buffer("fb", fb)
|
||||
|
||||
if scale is not None and device is not None:
|
||||
self.fb = self.fb.to(self.device) # move filterbank to device
|
||||
|
||||
if self.perceptual_weighting:
|
||||
if sample_rate is None:
|
||||
raise ValueError(
|
||||
f"`sample_rate` must be supplied when `perceptual_weighting = True`."
|
||||
)
|
||||
self.prefilter = FIRFilter(filter_type="aw", fs=sample_rate)
|
||||
|
||||
def stft(self, x):
|
||||
"""Perform STFT.
|
||||
Args:
|
||||
x (Tensor): Input signal tensor (B, T).
|
||||
|
||||
Returns:
|
||||
Tensor: x_mag, x_phs
|
||||
Magnitude and phase spectra (B, fft_size // 2 + 1, frames).
|
||||
"""
|
||||
x_stft = torch.stft(
|
||||
x,
|
||||
self.fft_size,
|
||||
self.hop_size,
|
||||
self.win_length,
|
||||
self.window,
|
||||
return_complex=True,
|
||||
)
|
||||
x_mag = torch.sqrt(
|
||||
torch.clamp((x_stft.real**2) + (x_stft.imag**2), min=self.eps)
|
||||
)
|
||||
|
||||
# torch.angle is expensive, so it is only evaluated if the values are used in the loss
|
||||
if self.phs_used:
|
||||
x_phs = torch.angle(x_stft)
|
||||
else:
|
||||
x_phs = None
|
||||
|
||||
return x_mag, x_phs
|
||||
|
||||
def forward(self, input: torch.Tensor, target: torch.Tensor):
|
||||
bs, chs, seq_len = input.size()
|
||||
|
||||
if self.perceptual_weighting: # apply optional A-weighting via FIR filter
|
||||
# since FIRFilter only support mono audio we will move channels to batch dim
|
||||
input = input.view(bs * chs, 1, -1)
|
||||
target = target.view(bs * chs, 1, -1)
|
||||
|
||||
# now apply the filter to both
|
||||
self.prefilter.to(input.device)
|
||||
input, target = self.prefilter(input, target)
|
||||
|
||||
# now move the channels back
|
||||
input = input.view(bs, chs, -1)
|
||||
target = target.view(bs, chs, -1)
|
||||
|
||||
# compute the magnitude and phase spectra of input and target
|
||||
self.window = self.window.to(input.device)
|
||||
|
||||
x_mag, x_phs = self.stft(input.view(-1, input.size(-1)))
|
||||
y_mag, y_phs = self.stft(target.view(-1, target.size(-1)))
|
||||
|
||||
# apply relevant transforms
|
||||
if self.scale is not None:
|
||||
self.fb = self.fb.to(input.device)
|
||||
x_mag = torch.matmul(self.fb, x_mag)
|
||||
y_mag = torch.matmul(self.fb, y_mag)
|
||||
|
||||
# normalize scales
|
||||
if self.scale_invariance:
|
||||
alpha = (x_mag * y_mag).sum([-2, -1]) / ((y_mag**2).sum([-2, -1]))
|
||||
y_mag = y_mag * alpha.unsqueeze(-1)
|
||||
|
||||
# compute loss terms
|
||||
sc_mag_loss = self.spectralconv(x_mag, y_mag) if self.w_sc else 0.0
|
||||
log_mag_loss = self.logstft(x_mag, y_mag) if self.w_log_mag else 0.0
|
||||
lin_mag_loss = self.linstft(x_mag, y_mag) if self.w_lin_mag else 0.0
|
||||
phs_loss = torch.nn.functional.mse_loss(x_phs, y_phs) if self.phs_used else 0.0
|
||||
|
||||
# combine loss terms
|
||||
loss = (
|
||||
(self.w_sc * sc_mag_loss)
|
||||
+ (self.w_log_mag * log_mag_loss)
|
||||
+ (self.w_lin_mag * lin_mag_loss)
|
||||
+ (self.w_phs * phs_loss)
|
||||
)
|
||||
|
||||
loss = apply_reduction(loss, reduction=self.reduction)
|
||||
|
||||
if self.output == "loss":
|
||||
return loss
|
||||
elif self.output == "full":
|
||||
return loss, sc_mag_loss, log_mag_loss, lin_mag_loss, phs_loss
|
||||
|
||||
class MultiResolutionSTFTLoss(torch.nn.Module):
|
||||
"""Multi resolution STFT loss module.
|
||||
|
||||
See [Yamamoto et al., 2019](https://arxiv.org/abs/1910.11480)
|
||||
|
||||
Args:
|
||||
fft_sizes (list): List of FFT sizes.
|
||||
hop_sizes (list): List of hop sizes.
|
||||
win_lengths (list): List of window lengths.
|
||||
window (str, optional): Window to apply before FFT, options include:
|
||||
'hann_window', 'bartlett_window', 'blackman_window', 'hamming_window', 'kaiser_window']
|
||||
Default: 'hann_window'
|
||||
w_sc (float, optional): Weight of the spectral convergence loss term. Default: 1.0
|
||||
w_log_mag (float, optional): Weight of the log magnitude loss term. Default: 1.0
|
||||
w_lin_mag (float, optional): Weight of the linear magnitude loss term. Default: 0.0
|
||||
w_phs (float, optional): Weight of the spectral phase loss term. Default: 0.0
|
||||
sample_rate (int, optional): Sample rate. Required when scale = 'mel'. Default: None
|
||||
scale (str, optional): Optional frequency scaling method, options include:
|
||||
['mel', 'chroma']
|
||||
Default: None
|
||||
n_bins (int, optional): Number of mel frequency bins. Required when scale = 'mel'. Default: None.
|
||||
scale_invariance (bool, optional): Perform an optimal scaling of the target. Default: False
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
fft_sizes: List[int] = [1024, 2048, 512],
|
||||
hop_sizes: List[int] = [120, 240, 50],
|
||||
win_lengths: List[int] = [600, 1200, 240],
|
||||
window: str = "hann_window",
|
||||
w_sc: float = 1.0,
|
||||
w_log_mag: float = 1.0,
|
||||
w_lin_mag: float = 0.0,
|
||||
w_phs: float = 0.0,
|
||||
sample_rate: float = None,
|
||||
scale: str = None,
|
||||
n_bins: int = None,
|
||||
perceptual_weighting: bool = False,
|
||||
scale_invariance: bool = False,
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__()
|
||||
assert len(fft_sizes) == len(hop_sizes) == len(win_lengths) # must define all
|
||||
self.fft_sizes = fft_sizes
|
||||
self.hop_sizes = hop_sizes
|
||||
self.win_lengths = win_lengths
|
||||
|
||||
self.stft_losses = torch.nn.ModuleList()
|
||||
for fs, ss, wl in zip(fft_sizes, hop_sizes, win_lengths):
|
||||
self.stft_losses += [
|
||||
STFTLoss(
|
||||
fs,
|
||||
ss,
|
||||
wl,
|
||||
window,
|
||||
w_sc,
|
||||
w_log_mag,
|
||||
w_lin_mag,
|
||||
w_phs,
|
||||
sample_rate,
|
||||
scale,
|
||||
n_bins,
|
||||
perceptual_weighting,
|
||||
scale_invariance,
|
||||
**kwargs,
|
||||
)
|
||||
]
|
||||
|
||||
def forward(self, x, y):
|
||||
mrstft_loss = 0.0
|
||||
sc_mag_loss, log_mag_loss, lin_mag_loss, phs_loss = [], [], [], []
|
||||
# import ipdb
|
||||
# ipdb.set_trace()
|
||||
for f in self.stft_losses:
|
||||
if f.output == "full": # extract just first term
|
||||
tmp_loss = f(x, y)
|
||||
mrstft_loss += tmp_loss[0]
|
||||
sc_mag_loss.append(tmp_loss[1])
|
||||
log_mag_loss.append(tmp_loss[2])
|
||||
lin_mag_loss.append(tmp_loss[3])
|
||||
phs_loss.append(tmp_loss[4])
|
||||
else:
|
||||
mrstft_loss += f(x, y)
|
||||
|
||||
mrstft_loss /= len(self.stft_losses)
|
||||
|
||||
if f.output == "loss":
|
||||
return mrstft_loss
|
||||
else:
|
||||
return mrstft_loss, sc_mag_loss, log_mag_loss, lin_mag_loss, phs_loss
|
||||
|
||||
|
||||
class SumAndDifferenceSTFTLoss(torch.nn.Module):
|
||||
"""Sum and difference sttereo STFT loss module.
|
||||
|
||||
See [Steinmetz et al., 2020](https://arxiv.org/abs/2010.10291)
|
||||
|
||||
Args:
|
||||
fft_sizes (List[int]): List of FFT sizes.
|
||||
hop_sizes (List[int]): List of hop sizes.
|
||||
win_lengths (List[int]): List of window lengths.
|
||||
window (str, optional): Window function type.
|
||||
w_sum (float, optional): Weight of the sum loss component. Default: 1.0
|
||||
w_diff (float, optional): Weight of the difference loss component. Default: 1.0
|
||||
perceptual_weighting (bool, optional): Apply perceptual A-weighting (Sample rate must be supplied). Default: False
|
||||
mel_stft (bool, optional): Use Multi-resoltuion mel spectrograms. Default: False
|
||||
n_mel_bins (int, optional): Number of mel bins to use when mel_stft = True. Default: 128
|
||||
sample_rate (float, optional): Audio sample rate. Default: None
|
||||
output (str, optional): Format of the loss returned.
|
||||
'loss' : Return only the raw, aggregate loss term.
|
||||
'full' : Return the raw loss, plus intermediate loss terms.
|
||||
Default: 'loss'
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
fft_sizes: List[int],
|
||||
hop_sizes: List[int],
|
||||
win_lengths: List[int],
|
||||
window: str = "hann_window",
|
||||
w_sum: float = 1.0,
|
||||
w_diff: float = 1.0,
|
||||
output: str = "loss",
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__()
|
||||
self.sd = SumAndDifference()
|
||||
self.w_sum = w_sum
|
||||
self.w_diff = w_diff
|
||||
self.output = output
|
||||
self.mrstft = MultiResolutionSTFTLoss(
|
||||
fft_sizes,
|
||||
hop_sizes,
|
||||
win_lengths,
|
||||
window,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
def forward(self, input: torch.Tensor, target: torch.Tensor):
|
||||
"""This loss function assumes batched input of stereo audio in the time domain.
|
||||
|
||||
Args:
|
||||
input (torch.Tensor): Input tensor with shape (batch size, 2, seq_len).
|
||||
target (torch.Tensor): Target tensor with shape (batch size, 2, seq_len).
|
||||
|
||||
Returns:
|
||||
loss (torch.Tensor): Aggreate loss term. Only returned if output='loss'.
|
||||
loss (torch.Tensor), sum_loss (torch.Tensor), diff_loss (torch.Tensor):
|
||||
Aggregate and intermediate loss terms. Only returned if output='full'.
|
||||
"""
|
||||
assert input.shape == target.shape # must have same shape
|
||||
bs, chs, seq_len = input.size()
|
||||
|
||||
# compute sum and difference signals for both
|
||||
input_sum, input_diff = self.sd(input)
|
||||
target_sum, target_diff = self.sd(target)
|
||||
|
||||
# compute error in STFT domain
|
||||
sum_loss = self.mrstft(input_sum, target_sum)
|
||||
diff_loss = self.mrstft(input_diff, target_diff)
|
||||
loss = ((self.w_sum * sum_loss) + (self.w_diff * diff_loss)) / 2
|
||||
|
||||
if self.output == "loss":
|
||||
return loss
|
||||
elif self.output == "full":
|
||||
return loss, sum_loss, diff_loss
|
||||
|
||||
class SpatialSTFTLoss(torch.nn.Module):
|
||||
"""Sum and difference sttereo STFT loss module.
|
||||
|
||||
See [Steinmetz et al., 2020](https://arxiv.org/abs/2010.10291)
|
||||
|
||||
Args:
|
||||
fft_sizes (List[int]): List of FFT sizes.
|
||||
hop_sizes (List[int]): List of hop sizes.
|
||||
win_lengths (List[int]): List of window lengths.
|
||||
window (str, optional): Window function type.
|
||||
w_sum (float, optional): Weight of the sum loss component. Default: 1.0
|
||||
w_diff (float, optional): Weight of the difference loss component. Default: 1.0
|
||||
perceptual_weighting (bool, optional): Apply perceptual A-weighting (Sample rate must be supplied). Default: False
|
||||
mel_stft (bool, optional): Use Multi-resoltuion mel spectrograms. Default: False
|
||||
n_mel_bins (int, optional): Number of mel bins to use when mel_stft = True. Default: 128
|
||||
sample_rate (float, optional): Audio sample rate. Default: None
|
||||
output (str, optional): Format of the loss returned.
|
||||
'loss' : Return only the raw, aggregate loss term.
|
||||
'full' : Return the raw loss, plus intermediate loss terms.
|
||||
Default: 'loss'
|
||||
"""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
fft_sizes: List[int],
|
||||
hop_sizes: List[int],
|
||||
win_lengths: List[int],
|
||||
window: str = "hann_window",
|
||||
w_phi: float = 1.0,
|
||||
w_theta: float = 1.0,
|
||||
output: str = "loss",
|
||||
**kwargs,
|
||||
):
|
||||
super().__init__()
|
||||
self.w_phi = w_phi
|
||||
self.w_theta = w_theta
|
||||
self.output = output
|
||||
self.mrstft = MultiResolutionSTFTLoss(
|
||||
fft_sizes,
|
||||
hop_sizes,
|
||||
win_lengths,
|
||||
window,
|
||||
**kwargs,
|
||||
)
|
||||
|
||||
|
||||
def forward(self, input: torch.Tensor, target: torch.Tensor):
|
||||
"""This loss function assumes batched input of stereo audio in the time domain.
|
||||
|
||||
Args:
|
||||
input (torch.Tensor): Input tensor with shape (batch size, 2, seq_len).
|
||||
target (torch.Tensor): Target tensor with shape (batch size, 2, seq_len).
|
||||
|
||||
Returns:
|
||||
loss (torch.Tensor): Aggreate loss term. Only returned if output='loss'.
|
||||
loss (torch.Tensor), sum_loss (torch.Tensor), diff_loss (torch.Tensor):
|
||||
Aggregate and intermediate loss terms. Only returned if output='full'.
|
||||
"""
|
||||
assert input.shape == target.shape # must have same shape
|
||||
bs, chs, seq_len = input.size()
|
||||
|
||||
w_o, x_o, y_o, z_o = input[:, 0], input[:, 1], input[:, 2], input[:, 3]
|
||||
w_r, x_r, y_r, z_r = target[:, 0], target[:, 1], target[:, 2], target[:, 3]
|
||||
|
||||
phi_o, theta_o = compute_direction(w_o, x_o, y_o, z_o)
|
||||
phi_r, theta_r = compute_direction(w_r, x_r, y_r, z_r)
|
||||
|
||||
# compute error in STFT domain
|
||||
phi_loss = self.mrstft(phi_o, phi_r)
|
||||
theta_loss = self.mrstft(theta_o, theta_r)
|
||||
loss = ((self.w_phi * phi_loss) + (self.w_theta * theta_loss)) / 2
|
||||
|
||||
if self.output == "loss":
|
||||
return loss
|
||||
elif self.output == "full":
|
||||
return loss, sum_loss, diff_loss
|
||||
@@ -0,0 +1,100 @@
|
||||
import typing as tp
|
||||
|
||||
from torch.nn import functional as F
|
||||
from torch import nn
|
||||
|
||||
class LossModule(nn.Module):
|
||||
def __init__(self, name: str, weight: float = 1.0):
|
||||
super().__init__()
|
||||
|
||||
self.name = name
|
||||
self.weight = weight
|
||||
|
||||
def forward(self, info, *args, **kwargs):
|
||||
raise NotImplementedError
|
||||
|
||||
class ValueLoss(LossModule):
|
||||
def __init__(self, key: str, name, weight: float = 1.0):
|
||||
super().__init__(name=name, weight=weight)
|
||||
|
||||
self.key = key
|
||||
|
||||
def forward(self, info):
|
||||
return self.weight * info[self.key]
|
||||
|
||||
class L1Loss(LossModule):
|
||||
def __init__(self, key_a: str, key_b: str, weight: float = 1.0, mask_key: str = None, name: str = 'l1_loss'):
|
||||
super().__init__(name=name, weight=weight)
|
||||
|
||||
self.key_a = key_a
|
||||
self.key_b = key_b
|
||||
|
||||
self.mask_key = mask_key
|
||||
|
||||
def forward(self, info):
|
||||
mse_loss = F.l1_loss(info[self.key_a], info[self.key_b], reduction='none')
|
||||
|
||||
if self.mask_key is not None and self.mask_key in info:
|
||||
mse_loss = mse_loss[info[self.mask_key]]
|
||||
|
||||
mse_loss = mse_loss.mean()
|
||||
|
||||
return self.weight * mse_loss
|
||||
|
||||
class MSELoss(LossModule):
|
||||
def __init__(self, key_a: str, key_b: str, weight: float = 1.0, mask_key: str = None, name: str = 'mse_loss'):
|
||||
super().__init__(name=name, weight=weight)
|
||||
|
||||
self.key_a = key_a
|
||||
self.key_b = key_b
|
||||
|
||||
self.mask_key = mask_key
|
||||
|
||||
def forward(self, info):
|
||||
mse_loss = F.mse_loss(info[self.key_a], info[self.key_b], reduction='none')
|
||||
if self.mask_key is not None and self.mask_key in info and info[self.mask_key] is not None:
|
||||
mask = info[self.mask_key]
|
||||
|
||||
if mask.ndim == 2 and mse_loss.ndim == 3:
|
||||
mask = mask.unsqueeze(1)
|
||||
|
||||
if mask.shape[1] != mse_loss.shape[1]:
|
||||
mask = mask.repeat(1, mse_loss.shape[1], 1)
|
||||
|
||||
mse_loss = mse_loss[mask]
|
||||
|
||||
mse_loss = mse_loss.mean()
|
||||
|
||||
return self.weight * mse_loss
|
||||
|
||||
class AuralossLoss(LossModule):
|
||||
def __init__(self, auraloss_module, input_key: str, target_key: str, name: str, weight: float = 1):
|
||||
super().__init__(name, weight)
|
||||
|
||||
self.auraloss_module = auraloss_module
|
||||
|
||||
self.input_key = input_key
|
||||
self.target_key = target_key
|
||||
|
||||
def forward(self, info):
|
||||
loss = self.auraloss_module(info[self.input_key], info[self.target_key])
|
||||
|
||||
return self.weight * loss
|
||||
|
||||
class MultiLoss(nn.Module):
|
||||
def __init__(self, losses: tp.List[LossModule]):
|
||||
super().__init__()
|
||||
|
||||
self.losses = nn.ModuleList(losses)
|
||||
|
||||
def forward(self, info):
|
||||
total_loss = 0
|
||||
|
||||
losses = {}
|
||||
|
||||
for loss_module in self.losses:
|
||||
module_loss = loss_module(info)
|
||||
total_loss += module_loss
|
||||
losses[loss_module.name] = module_loss
|
||||
|
||||
return total_loss, losses
|
||||
@@ -0,0 +1,200 @@
|
||||
import torch
|
||||
import os
|
||||
from torch import nn, Tensor, einsum, IntTensor, FloatTensor, BoolTensor
|
||||
import random
|
||||
|
||||
|
||||
|
||||
def get_rank():
|
||||
"""Get rank of current process."""
|
||||
|
||||
print(os.environ.keys())
|
||||
|
||||
if "SLURM_PROCID" in os.environ:
|
||||
return int(os.environ["SLURM_PROCID"])
|
||||
|
||||
if not torch.distributed.is_available() or not torch.distributed.is_initialized():
|
||||
return 0
|
||||
|
||||
return torch.distributed.get_rank()
|
||||
|
||||
class InverseLR(torch.optim.lr_scheduler._LRScheduler):
|
||||
"""Implements an inverse decay learning rate schedule with an optional exponential
|
||||
warmup. When last_epoch=-1, sets initial lr as lr.
|
||||
inv_gamma is the number of steps/epochs required for the learning rate to decay to
|
||||
(1 / 2)**power of its original value.
|
||||
Args:
|
||||
optimizer (Optimizer): Wrapped optimizer.
|
||||
inv_gamma (float): Inverse multiplicative factor of learning rate decay. Default: 1.
|
||||
power (float): Exponential factor of learning rate decay. Default: 1.
|
||||
warmup (float): Exponential warmup factor (0 <= warmup < 1, 0 to disable)
|
||||
Default: 0.
|
||||
final_lr (float): The final learning rate. Default: 0.
|
||||
last_epoch (int): The index of last epoch. Default: -1.
|
||||
verbose (bool): If ``True``, prints a message to stdout for
|
||||
each update. Default: ``False``.
|
||||
"""
|
||||
|
||||
def __init__(self, optimizer, inv_gamma=1., power=1., warmup=0., final_lr=0.,
|
||||
last_epoch=-1, verbose=False):
|
||||
self.inv_gamma = inv_gamma
|
||||
self.power = power
|
||||
if not 0. <= warmup < 1:
|
||||
raise ValueError('Invalid value for warmup')
|
||||
self.warmup = warmup
|
||||
self.final_lr = final_lr
|
||||
super().__init__(optimizer, last_epoch, verbose)
|
||||
|
||||
def get_lr(self):
|
||||
if not self._get_lr_called_within_step:
|
||||
import warnings
|
||||
warnings.warn("To get the last learning rate computed by the scheduler, "
|
||||
"please use `get_last_lr()`.")
|
||||
|
||||
return self._get_closed_form_lr()
|
||||
|
||||
def _get_closed_form_lr(self):
|
||||
warmup = 1 - self.warmup ** (self.last_epoch + 1)
|
||||
lr_mult = (1 + self.last_epoch / self.inv_gamma) ** -self.power
|
||||
return [warmup * max(self.final_lr, base_lr * lr_mult)
|
||||
for base_lr in self.base_lrs]
|
||||
|
||||
def copy_state_dict(model, state_dict):
|
||||
"""Load state_dict to model, but only for keys that match exactly.
|
||||
|
||||
Args:
|
||||
model (nn.Module): model to load state_dict.
|
||||
state_dict (OrderedDict): state_dict to load.
|
||||
"""
|
||||
model_state_dict = model.state_dict()
|
||||
|
||||
# 创建一个列表存储不匹配的参数
|
||||
missing_keys = []
|
||||
unexpected_keys = []
|
||||
# 手动加载并检查不匹配的参数
|
||||
for key in state_dict:
|
||||
if key not in model_state_dict:
|
||||
unexpected_keys.append(key)
|
||||
elif state_dict[key].shape != model_state_dict[key].shape:
|
||||
unexpected_keys.append(key)
|
||||
|
||||
for key in model_state_dict:
|
||||
if key not in state_dict:
|
||||
missing_keys.append(key)
|
||||
|
||||
# 打印不匹配的参数
|
||||
print("Missing keys in state_dict:", missing_keys)
|
||||
print("Unexpected keys in state_dict:", unexpected_keys)
|
||||
for key in state_dict:
|
||||
if key in model_state_dict and state_dict[key].shape == model_state_dict[key].shape:
|
||||
if isinstance(state_dict[key], torch.nn.Parameter):
|
||||
# backwards compatibility for serialized parameters
|
||||
state_dict[key] = state_dict[key].data
|
||||
model_state_dict[key] = state_dict[key]
|
||||
|
||||
model.load_state_dict(model_state_dict, strict=False)
|
||||
|
||||
def create_optimizer_from_config(optimizer_config, parameters):
|
||||
"""Create optimizer from config.
|
||||
|
||||
Args:
|
||||
parameters (iterable): parameters to optimize.
|
||||
optimizer_config (dict): optimizer config.
|
||||
|
||||
Returns:
|
||||
torch.optim.Optimizer: optimizer.
|
||||
"""
|
||||
|
||||
optimizer_type = optimizer_config["type"]
|
||||
|
||||
if optimizer_type == "FusedAdam":
|
||||
from deepspeed.ops.adam import FusedAdam
|
||||
optimizer = FusedAdam(parameters, **optimizer_config["config"])
|
||||
else:
|
||||
optimizer_fn = getattr(torch.optim, optimizer_type)
|
||||
optimizer = optimizer_fn(parameters, **optimizer_config["config"])
|
||||
return optimizer
|
||||
|
||||
def create_scheduler_from_config(scheduler_config, optimizer):
|
||||
"""Create scheduler from config.
|
||||
|
||||
Args:
|
||||
scheduler_config (dict): scheduler config.
|
||||
optimizer (torch.optim.Optimizer): optimizer.
|
||||
|
||||
Returns:
|
||||
torch.optim.lr_scheduler._LRScheduler: scheduler.
|
||||
"""
|
||||
if scheduler_config["type"] == "InverseLR":
|
||||
scheduler_fn = InverseLR
|
||||
else:
|
||||
scheduler_fn = getattr(torch.optim.lr_scheduler, scheduler_config["type"])
|
||||
scheduler = scheduler_fn(optimizer, **scheduler_config["config"])
|
||||
return scheduler
|
||||
|
||||
# mask construction helpers
|
||||
|
||||
def mask_from_start_end_indices(
|
||||
seq_len: int,
|
||||
start: Tensor,
|
||||
end: Tensor
|
||||
):
|
||||
assert start.shape == end.shape
|
||||
device = start.device
|
||||
|
||||
seq = torch.arange(seq_len, device = device, dtype = torch.long)
|
||||
seq = seq.reshape(*((-1,) * start.ndim), seq_len)
|
||||
seq = seq.expand(*start.shape, seq_len)
|
||||
|
||||
mask = seq >= start[..., None].long()
|
||||
mask &= seq < end[..., None].long()
|
||||
return mask
|
||||
|
||||
def mask_from_frac_lengths(
|
||||
seq_len: int,
|
||||
frac_lengths: Tensor
|
||||
):
|
||||
device = frac_lengths.device
|
||||
|
||||
lengths = (frac_lengths * seq_len).long()
|
||||
max_start = seq_len - lengths
|
||||
|
||||
rand = torch.zeros_like(frac_lengths, device = device).float().uniform_(0, 1)
|
||||
start = (max_start * rand).clamp(min = 0)
|
||||
end = start + lengths
|
||||
|
||||
return mask_from_start_end_indices(seq_len, start, end)
|
||||
|
||||
def generate_mask(batch_size, seq_len, frac_lengths, min_span_len):
|
||||
# 计算需要掩盖的起始数量
|
||||
n_mask = (frac_lengths * seq_len // min_span_len).long() # 每个 span 为 10
|
||||
# 初始化掩码张量,初始为全 0(未掩盖)
|
||||
mask_tensor = torch.zeros((batch_size, seq_len), device=frac_lengths.device, dtype=torch.bool)
|
||||
|
||||
for b in range(batch_size):
|
||||
# 随机挑选起始帧
|
||||
start_frames = random.sample(range(0, seq_len - min_span_len + 1), n_mask[b]) # 0 到 seq_len-10 的范围
|
||||
|
||||
for start in start_frames:
|
||||
# 将 span 为 10 的区域标记为 1(掩盖)
|
||||
mask_tensor[b, start:start + 10] = 1.0
|
||||
|
||||
return mask_tensor
|
||||
|
||||
def generate_channel_mask(diffusion_input):
|
||||
|
||||
# 如果 r_drop 小于 threshold,则对每个样本选择一个随机声道进行完全 mask
|
||||
batchsize, num_channels, dim = diffusion_input.shape
|
||||
for i in range(batchsize):
|
||||
channel_means = torch.mean(torch.abs(diffusion_input[i]), dim=1) # Mean of the absolute values for each channel
|
||||
# Determine if any channel is 'small enough'
|
||||
if torch.all(channel_means > 0.01):
|
||||
# If all channels are not 'small enough', apply the mask
|
||||
channel = torch.randint(num_channels, (1,)).item()
|
||||
diffusion_input[i, channel, :] = 1e-8 # Mask the channel by setting its values
|
||||
else:
|
||||
# Optionally log that at least one channel is 'small enough' and no mask is applied
|
||||
print(f"Sample {i}: At least one channel is 'small enough', skipping masking.")
|
||||
|
||||
return diffusion_input
|
||||
|
||||
Reference in New Issue
Block a user