File size: 6,636 Bytes
3a1da90 0ff9928 3a1da90 0ff9928 3a1da90 98c6962 3a1da90 98c6962 0ff9928 98c6962 3a1da90 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
import dataclasses
import logging
from pathlib import Path
from typing import Optional
import numpy as np
import torch
from colorlog import ColoredFormatter
from PIL import Image
from torchvision.transforms import v2
from meanaudio.data.av_utils import ImageInfo, VideoInfo, read_frames, reencode_with_audio
from meanaudio.model.flow_matching import FlowMatching
from meanaudio.model.mean_flow import MeanFlow
from meanaudio.model.networks import MeanAudio, FluxAudio
from meanaudio.model.sequence_config import CONFIG_16K, CONFIG_44K, SequenceConfig
from meanaudio.model.utils.features_utils import FeaturesUtils
from meanaudio.utils.download_utils import download_model_if_needed
log = logging.getLogger()
@dataclasses.dataclass
class ModelConfig:
model_name: str
model_path: Path
vae_path: Path
bigvgan_16k_path: Optional[Path]
mode: str
@property
def seq_cfg(self) -> SequenceConfig:
if self.mode == '16k':
return CONFIG_16K # get sequence config when calling cfg.seq_cfgs
elif self.mode == '44k':
return CONFIG_44K
def download_if_needed(self):
raise NotImplementedError("Downloading models is not supported")
download_model_if_needed(self.model_path)
download_model_if_needed(self.vae_path)
if self.bigvgan_16k_path is not None:
download_model_if_needed(self.bigvgan_16k_path)
fluxaudio_s_full = ModelConfig(model_name='fluxaudio_s_full',
model_path=Path('./weights/fluxaudio_s_full.pth'), # will be specified later
vae_path=Path('./weights/v1-16.pth'),
bigvgan_16k_path=Path('./weights/best_netG.pt'),
mode='16k')
meanaudio_s_full = ModelConfig(model_name='meanaudio_s_full',
model_path=Path('./weights/meanaudio_s_full.pth'), # will be specified later
vae_path=Path('./weights/v1-16.pth'),
bigvgan_16k_path=Path('./weights/best_netG.pt'),
mode='16k')
meanaudio_s_ac = ModelConfig(model_name='meanaudio_s_ac',
model_path=Path('./weights/meanaudio_s_ac.pth'), # will be specified later
vae_path=Path('./weights/v1-16.pth'),
bigvgan_16k_path=Path('./weights/best_netG.pt'),
mode='16k')
meanaudio_l_full = ModelConfig(model_name='meanaudio_l_full',
model_path=Path('./weights/meanaudio_l_full.pth'), # will be specified later
vae_path=Path('./weights/v1-16.pth'),
bigvgan_16k_path=Path('./weights/best_netG.pt'),
mode='16k')
all_model_cfg: dict[str, ModelConfig] = {
'meanaudio_l_full': meanaudio_l_full,
'meanaudio_s_full': meanaudio_s_full,
'meanaudio_s_ac': meanaudio_s_ac,
'fluxaudio_s_full': fluxaudio_s_full,
}
def generate_fm(
text: Optional[list[str]],
*,
negative_text: Optional[list[str]] = None,
feature_utils: FeaturesUtils,
net: FluxAudio,
fm: FlowMatching,
rng: torch.Generator,
cfg_strength: float,
) -> torch.Tensor:
# generate audio with vanilla flow matching
device = feature_utils.device
dtype = feature_utils.dtype
bs = len(text)
if text is not None:
text_features, text_features_c = feature_utils.encode_text(text)
else:
text_features, text_features_c = net.get_empty_string_sequence(bs)
if negative_text is not None:
assert len(negative_text) == bs
negative_text_features = feature_utils.encode_text(negative_text)
else:
negative_text_features = net.get_empty_string_sequence(bs)
x0 = torch.randn(bs,
net.latent_seq_len,
net.latent_dim,
device=device,
dtype=dtype,
generator=rng)
preprocessed_conditions = net.preprocess_conditions(text_features, text_features_c)
empty_conditions = net.get_empty_conditions(
bs, negative_text_features=negative_text_features if negative_text is not None else None)
cfg_ode_wrapper = lambda t, x: net.ode_wrapper(t, x, preprocessed_conditions, empty_conditions,
cfg_strength)
x1 = fm.to_data(cfg_ode_wrapper, x0)
x1 = net.unnormalize(x1)
spec = feature_utils.decode(x1)
audio = feature_utils.vocode(spec)
return audio
def generate_mf(
text: Optional[list[str]],
*,
negative_text: Optional[list[str]] = None,
feature_utils: FeaturesUtils,
net: MeanAudio,
mf: MeanFlow,
rng: torch.Generator,
cfg_strength: float,
) -> torch.Tensor:
# generate audio with mean flow
device = feature_utils.device
dtype = feature_utils.dtype
bs = len(text)
if text is not None:
text_features, text_features_c = feature_utils.encode_text(text)
else:
text_features, text_features_c = net.get_empty_string_sequence(bs)
if negative_text is not None:
assert len(negative_text) == bs
negative_text_features = feature_utils.encode_text(negative_text)
else:
negative_text_features = net.get_empty_string_sequence(bs)
x0 = torch.randn(bs,
net.latent_seq_len,
net.latent_dim,
device=device,
dtype=dtype,
generator=rng)
preprocessed_conditions = net.preprocess_conditions(text_features, text_features_c)
empty_conditions = net.get_empty_conditions(
bs, negative_text_features=negative_text_features if negative_text is not None else None)
cfg_ode_wrapper = lambda t, r, x: net.ode_wrapper(t, r, x, preprocessed_conditions, empty_conditions,
cfg_strength)
x1 = mf.to_data(cfg_ode_wrapper, x0)
x1 = net.unnormalize(x1)
spec = feature_utils.decode(x1)
audio = feature_utils.vocode(spec)
return audio
LOGFORMAT = "[%(log_color)s%(levelname)-8s%(reset)s]: %(log_color)s%(message)s%(reset)s"
def setup_eval_logging(log_level: int = logging.INFO):
logging.root.setLevel(log_level) # set up root logger <=> logging.getLogger().setLevel(log_level)
formatter = ColoredFormatter(LOGFORMAT)
stream = logging.StreamHandler() # to Console
stream.setLevel(log_level)
stream.setFormatter(formatter)
log = logging.getLogger()
log.setLevel(log_level)
log.addHandler(stream) |