File size: 6,747 Bytes
01a383f |
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 |
# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import argparse
import os
import torch
from cosmos1.models.diffusion.inference.inference_utils import add_common_arguments, check_input_frames, validate_args
from cosmos1.models.diffusion.inference.world_generation_pipeline import DiffusionVideo2WorldGenerationPipeline
from cosmos1.utils import log, misc
from cosmos1.utils.io import read_prompts_from_file, save_video
torch.enable_grad(False)
def parse_arguments() -> argparse.Namespace:
parser = argparse.ArgumentParser(description="Video to world generation demo script")
# Add common arguments
add_common_arguments(parser)
# Add video2world specific arguments
parser.add_argument(
"--diffusion_transformer_dir",
type=str,
default="Cosmos-1.0-Diffusion-7B-Video2World",
help="DiT model weights directory name relative to checkpoint_dir",
choices=[
"Cosmos-1.0-Diffusion-7B-Video2World",
"Cosmos-1.0-Diffusion-14B-Video2World",
],
)
parser.add_argument(
"--prompt_upsampler_dir",
type=str,
default="Pixtral-12B",
help="Prompt upsampler weights directory relative to checkpoint_dir",
)
parser.add_argument(
"--input_image_or_video_path",
type=str,
help="Input video/image path for generating a single video",
)
parser.add_argument(
"--num_input_frames",
type=int,
default=1,
help="Number of input frames for video2world prediction",
choices=[1, 9],
)
return parser.parse_args()
def demo(cfg):
"""Run video-to-world generation demo.
This function handles the main video-to-world generation pipeline, including:
- Setting up the random seed for reproducibility
- Initializing the generation pipeline with the provided configuration
- Processing single or multiple prompts/images/videos from input
- Generating videos from prompts and images/videos
- Saving the generated videos and corresponding prompts to disk
Args:
cfg (argparse.Namespace): Configuration namespace containing:
- Model configuration (checkpoint paths, model settings)
- Generation parameters (guidance, steps, dimensions)
- Input/output settings (prompts/images/videos, save paths)
- Performance options (model offloading settings)
The function will save:
- Generated MP4 video files
- Text files containing the processed prompts
If guardrails block the generation, a critical log message is displayed
and the function continues to the next prompt if available.
"""
misc.set_random_seed(cfg.seed)
inference_type = "video2world"
validate_args(cfg, inference_type)
# Initialize video2world generation model pipeline
pipeline = DiffusionVideo2WorldGenerationPipeline(
inference_type=inference_type,
checkpoint_dir=cfg.checkpoint_dir,
checkpoint_name=cfg.diffusion_transformer_dir,
prompt_upsampler_dir=cfg.prompt_upsampler_dir,
enable_prompt_upsampler=not cfg.disable_prompt_upsampler,
offload_network=cfg.offload_diffusion_transformer,
offload_tokenizer=cfg.offload_tokenizer,
offload_text_encoder_model=cfg.offload_text_encoder_model,
offload_prompt_upsampler=cfg.offload_prompt_upsampler,
offload_guardrail_models=cfg.offload_guardrail_models,
guidance=cfg.guidance,
num_steps=cfg.num_steps,
height=cfg.height,
width=cfg.width,
fps=cfg.fps,
num_video_frames=cfg.num_video_frames,
seed=cfg.seed,
num_input_frames=cfg.num_input_frames,
)
# Handle multiple prompts if prompt file is provided
if cfg.batch_input_path:
log.info(f"Reading batch inputs from path: {args.batch_input_path}")
prompts = read_prompts_from_file(cfg.batch_input_path)
else:
# Single prompt case
prompts = [{"prompt": cfg.prompt, "visual_input": cfg.input_image_or_video_path}]
os.makedirs(cfg.video_save_folder, exist_ok=True)
for i, input_dict in enumerate(prompts):
current_prompt = input_dict.get("prompt", None)
if current_prompt is None and cfg.disable_prompt_upsampler:
log.critical("Prompt is missing, skipping world generation.")
continue
current_image_or_video_path = input_dict.get("visual_input", None)
if current_image_or_video_path is None:
log.critical("Visual input is missing, skipping world generation.")
continue
# Check input frames
if not check_input_frames(current_image_or_video_path, cfg.num_input_frames):
continue
# Generate video
generated_output = pipeline.generate(
prompt=current_prompt,
image_or_video_path=current_image_or_video_path,
negative_prompt=cfg.negative_prompt,
)
if generated_output is None:
log.critical("Guardrail blocked video2world generation.")
continue
video, prompt = generated_output
if cfg.batch_input_path:
video_save_path = os.path.join(cfg.video_save_folder, f"{i}.mp4")
prompt_save_path = os.path.join(cfg.video_save_folder, f"{i}.txt")
else:
video_save_path = os.path.join(cfg.video_save_folder, f"{cfg.video_save_name}.mp4")
prompt_save_path = os.path.join(cfg.video_save_folder, f"{cfg.video_save_name}.txt")
# Save video
save_video(
video=video,
fps=cfg.fps,
H=cfg.height,
W=cfg.width,
video_save_quality=5,
video_save_path=video_save_path,
)
# Save prompt to text file alongside video
with open(prompt_save_path, "wb") as f:
f.write(prompt.encode("utf-8"))
log.info(f"Saved video to {video_save_path}")
log.info(f"Saved prompt to {prompt_save_path}")
if __name__ == "__main__":
args = parse_arguments()
demo(args)
|