File size: 8,277 Bytes
c64dfa4 cadd88e c64dfa4 cadd88e c64dfa4 cadd88e c64dfa4 cadd88e c64dfa4 cadd88e c64dfa4 |
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
import os
import time
import random
import datetime
import os.path as osp
from functools import partial
import tqdm
from omegaconf import OmegaConf
import torch
import gradio as gr
from mld.config import get_module_config
from mld.data.get_data import get_dataset
from mld.models.modeltype.mld import MLD
from mld.utils.utils import set_seed
from mld.data.humanml.utils.plot_script import plot_3d_motion
os.environ["TOKENIZERS_PARALLELISM"] = "false"
WEBSITE = """
<div class="embed_hidden">
<h1 style='text-align: center'> MotionLCM: Real-time Controllable Motion Generation via Latent Consistency Model </h1>
<h2 style='text-align: center'>
<a href="https://github.com/Dai-Wenxun/" target="_blank"><nobr>Wenxun Dai</nobr><sup>1</sup></a>  
<a href="https://lhchen.top/" target="_blank"><nobr>Ling-Hao Chen</nobr></a><sup>1</sup>  
<a href="https://wangjingbo1219.github.io/" target="_blank"><nobr>Jingbo Wang</nobr></a><sup>2</sup>  
<a href="https://moonsliu.github.io/" target="_blank"><nobr>Jinpeng Liu</nobr></a><sup>1</sup>  
<a href="https://daibo.info/" target="_blank"><nobr>Bo Dai</nobr></a><sup>2</sup>  
<a href="https://andytang15.github.io/" target="_blank"><nobr>Yansong Tang</nobr></a><sup>1</sup>
</h2>
<h2 style='text-align: center'>
<nobr><sup>1</sup>Tsinghua University</nobr>  
<nobr><sup>2</sup>Shanghai AI Laboratory</nobr>
</h2>
</div>
"""
WEBSITE_bottom = """
<div class="embed_hidden">
<p>
Space adapted from <a href="https://huggingface.co/spaces/Mathux/TMR" target="_blank">TMR</a>
and <a href="https://huggingface.co/spaces/MeYourHint/MoMask" target="_blank">MoMask</a>.
</p>
</div>
"""
EXAMPLES = [
"a person does a jump",
"a person waves both arms in the air.",
"The person takes 4 steps backwards.",
"this person bends forward as if to bow.",
"The person was pushed but did not fall.",
"a man walks forward in a snake like pattern.",
"a man paces back and forth along the same line.",
"with arms out to the sides a person walks forward",
"A man bends down and picks something up with his right hand.",
"The man walked forward, spun right on one foot and walked back to his original position.",
"a person slightly bent over with right hand pressing against the air walks forward slowly"
]
if not os.path.exists("./experiments_t2m/"):
os.system("bash prepare/download_pretrained_models.sh")
if not os.path.exists('./deps/glove/'):
os.system("bash prepare/download_glove.sh")
if not os.path.exists('./deps/sentence-t5-large/'):
os.system("bash prepare/prepare_t5.sh")
if not os.path.exists('./deps/t2m/'):
os.system("bash prepare/download_t2m_evaluators.sh")
if not os.path.exists('./datasets/humanml3d/'):
os.system("bash prepare/prepare_tiny_humanml3d.sh")
DEFAULT_TEXT = "A person is "
MAX_VIDEOS = 8
NUM_ROWS = 2
NUM_COLS = MAX_VIDEOS // NUM_ROWS
EXAMPLES_PER_PAGE = 12
T2M_CFG = "./configs_v1/motionlcm_t2m.yaml"
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
print("device: ", device)
cfg = OmegaConf.load(T2M_CFG)
cfg_root = os.path.dirname(T2M_CFG)
cfg_model = get_module_config(cfg.model, cfg.model.target, cfg_root)
cfg = OmegaConf.merge(cfg, cfg_model)
set_seed(cfg.SEED_VALUE)
name_time_str = osp.join(cfg.NAME, datetime.datetime.now().strftime("%Y-%m-%dT%H-%M-%S"))
cfg.output_dir = osp.join(cfg.TEST_FOLDER, name_time_str)
vis_dir = osp.join(cfg.output_dir, 'samples')
os.makedirs(cfg.output_dir, exist_ok=False)
os.makedirs(vis_dir, exist_ok=False)
state_dict = torch.load(cfg.TEST.CHECKPOINTS, map_location="cpu")["state_dict"]
print("Loading checkpoints from {}".format(cfg.TEST.CHECKPOINTS))
is_lcm = False
lcm_key = 'denoiser.time_embedding.cond_proj.weight' # unique key for CFG
if lcm_key in state_dict:
is_lcm = True
time_cond_proj_dim = state_dict[lcm_key].shape[1]
cfg.model.denoiser.params.time_cond_proj_dim = time_cond_proj_dim
print(f'Is LCM: {is_lcm}')
dataset = get_dataset(cfg)
model = MLD(cfg, dataset)
model.to(device)
model.eval()
model.requires_grad_(False)
model.load_state_dict(state_dict)
FPS = eval(f"cfg.DATASET.{cfg.DATASET.NAME.upper()}.FRAME_RATE")
@torch.no_grad()
def generate(text_, motion_len_):
batch = {"text": [text_] * MAX_VIDEOS, "length": [motion_len_] * MAX_VIDEOS}
s = time.time()
joints = model(batch)[0]
runtime_infer = round(time.time() - s, 3)
s = time.time()
path = []
for i in tqdm.tqdm(range(len(joints))):
uid = random.randrange(999999999)
video_path = osp.join(vis_dir, f"sample_{uid}.mp4")
plot_3d_motion(video_path, joints[i].detach().cpu().numpy(), '', fps=FPS)
path.append(video_path)
runtime_draw = round(time.time() - s, 3)
runtime_info = f'Inference {len(joints)} motions, Runtime (Inference): {runtime_infer}s, ' \
f'Runtime (Draw Skeleton): {runtime_draw}s, device: {device} '
return path, runtime_info
def generate_component(generate_function, text_, motion_len_, num_inference_steps_, guidance_scale_):
if text_ == DEFAULT_TEXT or text_ == "" or text_ is None:
return [None] * MAX_VIDEOS + ["Please modify the default text prompt."]
model.cfg.model.scheduler.num_inference_steps = num_inference_steps_
model.guidance_scale = guidance_scale_
motion_len_ = max(36, min(int(float(motion_len_) * FPS), 196))
paths, info = generate_function(text_, motion_len_)
paths = paths + [None] * (MAX_VIDEOS - len(paths))
return paths + [info]
theme = gr.themes.Default(primary_hue="purple", secondary_hue="gray")
generate_and_show = partial(generate_component, generate)
with gr.Blocks(theme=theme) as demo:
gr.HTML(WEBSITE)
videos = []
with gr.Row():
with gr.Column(scale=3):
text = gr.Textbox(
show_label=True,
label="Text prompt",
value=DEFAULT_TEXT,
)
with gr.Row():
with gr.Column(scale=1):
motion_len = gr.Slider(
minimum=1.8,
maximum=9.8,
step=0.2,
value=5.0,
label="Motion length",
info="Motion duration in seconds: [1.8s, 9.8s] (FPS = 20)."
)
with gr.Column(scale=1):
num_inference_steps = gr.Slider(
minimum=1,
maximum=4,
step=1,
value=1,
label="Inference steps",
info="Number of inference steps.",
)
cfg = gr.Slider(
minimum=1,
maximum=15,
step=0.5,
value=7.5,
label="CFG",
info="Classifier-free diffusion guidance.",
)
gen_btn = gr.Button("Generate", variant="primary")
clear = gr.Button("Clear", variant="secondary")
results = gr.Textbox(show_label=True,
label='Inference info (runtime and device)',
info='Real-time inference cannot be achieved using the free CPU. Local GPU deployment is recommended.',
interactive=False)
with gr.Column(scale=2):
examples = gr.Examples(
examples=EXAMPLES,
inputs=[text],
examples_per_page=EXAMPLES_PER_PAGE)
for i in range(NUM_ROWS):
with gr.Row():
for j in range(NUM_COLS):
video = gr.Video(autoplay=True, loop=True)
videos.append(video)
# gr.HTML(WEBSITE_bottom)
gen_btn.click(
fn=generate_and_show,
inputs=[text, motion_len, num_inference_steps, cfg],
outputs=videos + [results],
)
text.submit(
fn=generate_and_show,
inputs=[text, motion_len, num_inference_steps, cfg],
outputs=videos + [results],
)
def clear_videos():
return [None] * MAX_VIDEOS + [DEFAULT_TEXT] + [None]
clear.click(fn=clear_videos, outputs=videos + [text] + [results])
demo.launch()
|