File size: 15,081 Bytes
01c7703 |
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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 |
"""
Infinite World - Action-Conditioned Video Generation Inference Script
======================================================================
A standalone inference script for generating long videos with action control.
"""
import sys
import os
import cv2
import math
import torch
import random
import json
import datetime
import importlib
import numpy as np
from PIL import Image
from omegaconf import OmegaConf
import torch.distributed as dist
import torchvision.transforms as transforms
import re
# Add project root to path
PROJECT_ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.insert(0, PROJECT_ROOT)
from infworld.utils.prepare_dataloader import get_obj_from_str
from infworld.utils.data_utils import get_first_clip_from_video, save_silent_video
from infworld.utils.dataset_utils import is_vid, is_img
# ============================================================================
# Action Mapping Dictionaries
# ============================================================================
MOVE_ACTION_MAP = {
'no-op': 0,
'go forward': 1,
'go back': 2,
'go left': 3,
'go right': 4,
'go forward and go left': 5,
'go forward and go right': 6,
'go back and go left': 7,
'go back and go right': 8,
'uncertain': 9
}
VIEW_ACTION_MAP = {
'no-op': 0,
'turn up': 1,
'turn down': 2,
'turn left': 3,
'turn right': 4,
'turn up and turn left': 5,
'turn up and turn right': 6,
'turn down and turn left': 7,
'turn down and turn right': 8,
'uncertain': 9
}
# ============================================================================
# Utility Functions
# ============================================================================
def extract_ckpt_step(path):
"""Extract checkpoint step number from path."""
match = re.search(r'checkpoint-(\d+)\.ckpt', path)
return int(match.group(1)) if match else 0
def resize_and_center_crop(image, target_size):
"""Resize image and center crop to target size."""
orig_h, orig_w = image.shape[:2]
target_h, target_w = target_size
scale = max(target_h / orig_h, target_w / orig_w)
final_h = math.ceil(scale * orig_h)
final_w = math.ceil(scale * orig_w)
resized = cv2.resize(image, (final_w, final_h), interpolation=cv2.INTER_AREA)
tensor = torch.from_numpy(resized)[None, ...].permute(0, 3, 1, 2).contiguous()
cropped = transforms.functional.center_crop(tensor, target_size)
return cropped[:, :, None, :, :] # [1, C, 1, H, W]
def setup_seed(seed):
"""Set random seeds for reproducibility."""
torch.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
np.random.seed(seed)
random.seed(seed)
torch.backends.cudnn.deterministic = True
def torch_gc():
"""Clear GPU memory cache."""
torch.cuda.empty_cache()
torch.cuda.ipc_collect()
def load_action_sequence(action_path):
"""Load action sequence from JSON file."""
with open(action_path, 'r') as f:
actions = json.load(f)
move_indices = [MOVE_ACTION_MAP[a['move']] for a in actions]
view_indices = [VIEW_ACTION_MAP[a['view']] for a in actions]
return move_indices, view_indices
def load_condition_image(image_path, bucket_config):
"""Load and preprocess condition image."""
if is_vid(image_path):
frames = get_first_clip_from_video(image_path, clip_len=1)
elif is_img(image_path):
image = cv2.imread(image_path)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
frames = [image]
else:
raise ValueError(f'Unsupported file format: {image_path}')
processed_frames = []
for frame in frames:
ratio = frame.shape[0] / frame.shape[1]
closest_bucket = sorted(bucket_config.keys(), key=lambda x: abs(float(x) - ratio))[0]
target_h, target_w = bucket_config[closest_bucket][0]
tensor = resize_and_center_crop(frame, (target_h, target_w))
tensor = (tensor / 255 - 0.5) * 2 # Normalize to [-1, 1]
processed_frames.append(tensor)
return torch.cat(processed_frames, dim=2)
# ============================================================================
# Distributed Setup (support single-GPU without torchrun to avoid port conflict)
# ============================================================================
def setup_distributed():
"""Setup distributed or single-GPU mode."""
if 'RANK' in os.environ:
# Launched by torchrun or similar
rank = int(os.environ['RANK'])
world_size = int(os.environ.get('WORLD_SIZE', 1))
local_rank = int(os.environ.get('LOCAL_RANK', rank % torch.cuda.device_count()))
torch.cuda.set_device(local_rank)
dist.init_process_group(backend="nccl", timeout=datetime.timedelta(seconds=3600*24))
global_rank = dist.get_rank()
num_processes = dist.get_world_size()
return local_rank, global_rank, num_processes, True # use_cp_init=True
else:
# Single process (no torchrun) - avoid port conflict, no dist init
local_rank = 0
global_rank = 0
num_processes = 1
torch.cuda.set_device(local_rank)
return local_rank, global_rank, num_processes, False # use_cp_init=False
local_rank, global_rank, num_processes, use_dist = setup_distributed()
print(f"[InfWorld] local_rank: {local_rank} | global_rank: {global_rank} | world_size: {num_processes}")
# Context parallel setup
context_parallel_size = 1
import infworld.context_parallel.context_parallel_util as cp_util
if use_dist:
from infworld.context_parallel.context_parallel_util import init_context_parallel, get_dp_size, get_dp_rank
init_context_parallel(context_parallel_size=context_parallel_size, global_rank=global_rank, world_size=num_processes)
dp_rank = get_dp_rank()
dp_size = get_dp_size()
else:
# Single process: set globals so get_dp_rank/get_dp_size work without dist
cp_util.dp_rank = 0
cp_util.dp_size = 1
cp_util.cp_rank = 0
cp_util.cp_size = 1
dp_rank = 0
dp_size = 1
enable_context_parallel = (context_parallel_size > 1)
# ============================================================================
# Configuration
# ============================================================================
# Inference settings
GLOBAL_SEED = 42
setup_seed(GLOBAL_SEED + global_rank)
TEXT_CFG_SCALE = 5.0
NUM_SAMPLING_STEPS = 30
SHIFT = 7 # PX256: 3, PX627: 7, PX960: 11
NUM_CHUNKS = 13 # Number of video chunks to generate
HIGH_QUALITY_SAVE = True
# Paths - checkpoint_path is read from config (configs/infworld_config.yaml)
# Model config - use standalone config
CONFIG_PATH = os.path.join(PROJECT_ROOT, 'configs', 'infworld_config.yaml')
PROMPTS_YAML = os.path.join(PROJECT_ROOT, 'prompts', 'demo.yaml')
BUCKET_CONFIG_NAME = 'ASPECT_RATIO_627_F64'
# Output directory
OUTPUT_BASE = os.path.join(PROJECT_ROOT, 'outputs')
# Negative prompt for generation quality
NEGATIVE_PROMPT = "many cars, crowds, Vivid hues, overexposed, static, blurry details, subtitles, style, work, artwork, image, still, overall grayish, worst quality, low quality, JPEG compression artifacts, ugly, incomplete, extra fingers, poorly drawn hands, poorly drawn face, deformed, disfigured, deformed limbs, fused fingers, motionless image, cluttered background, three legs, crowded background, walking backwards."
# ============================================================================
# Main Inference Loop
# ============================================================================
def resolve_path(path, root=PROJECT_ROOT):
"""Resolve path: if relative, join with project root."""
if path is None:
return path
path = str(path).strip()
if not os.path.isabs(path):
path = os.path.join(root, path)
return path
def load_dit_state_dict(checkpoint_path):
"""Load DiT state dict from .ckpt (torch) or .safetensors."""
checkpoint_path = resolve_path(checkpoint_path)
if checkpoint_path.endswith(".safetensors"):
from safetensors.torch import load_file
state_dict = load_file(checkpoint_path)
else:
state_dict = torch.load(checkpoint_path, map_location="cpu")
if "state_dict" in state_dict:
state_dict = state_dict["state_dict"]
return state_dict
def main():
torch_gc()
config_path = CONFIG_PATH
args = OmegaConf.load(config_path)
checkpoint_path = resolve_path(args.get("checkpoint_path", "checkpoints/models/diffusion_pytorch_model.safetensors"))
ckpt_step = extract_ckpt_step(checkpoint_path)
# Create output directory
output_dir = os.path.join(OUTPUT_BASE, f"infworld-ckpt{ckpt_step}-step{NUM_SAMPLING_STEPS}-cfg{TEXT_CFG_SCALE}")
os.makedirs(output_dir, exist_ok=True)
print(f"[InfWorld] Loading checkpoint: {checkpoint_path}")
print(f"[InfWorld] Config: {config_path}")
print(f"[InfWorld] Output directory: {output_dir}")
# Resolve relative paths in config for models that load from disk
if hasattr(args, "vae_cfg") and "vae_pth" in args.vae_cfg:
args.vae_cfg.vae_pth = resolve_path(args.vae_cfg.vae_pth)
if hasattr(args, "text_encoder_cfg"):
if "checkpoint_path" in args.text_encoder_cfg:
args.text_encoder_cfg.checkpoint_path = resolve_path(args.text_encoder_cfg.checkpoint_path)
if "tokenizer_path" in args.text_encoder_cfg:
args.text_encoder_cfg.tokenizer_path = resolve_path(args.text_encoder_cfg.tokenizer_path)
# Initialize models
print("[InfWorld] Loading VAE...")
vae = get_obj_from_str(args.vae_target)(**args.vae_cfg).to(local_rank)
print("[InfWorld] Loading Text Encoder...")
text_encoder = get_obj_from_str(args.text_encoder_target)(device=local_rank, **args.text_encoder_cfg)
text_encoder.t5.model.to(local_rank)
print("[InfWorld] Loading Scheduler...")
scheduler = get_obj_from_str(args.scheduler_target)(**args.val_scheduler_cfg)
scheduler.num_sampling_steps = NUM_SAMPLING_STEPS
scheduler.shift = SHIFT
print("[InfWorld] Loading DiT Model...")
dtype = getattr(torch, args.amp_dtype)
dit = get_obj_from_str(args.model_target)(
out_channels=vae.out_channels,
caption_channels=text_encoder.output_dim,
model_max_length=text_encoder.model_max_length,
enable_context_parallel=enable_context_parallel,
**args.model_cfg
).to(dtype)
dit.eval()
# Load DiT checkpoint (from config)
state_dict = load_dit_state_dict(args.checkpoint_path)
# Remove position embeddings (will be recomputed)
state_dict.pop("pos_embed_temporal", None)
state_dict.pop("pos_embed", None)
missing, unexpected = dit.load_state_dict(state_dict, strict=False)
print(f"[InfWorld] Model loaded! Missing: {len(missing)}, Unexpected: {len(unexpected)}")
dit.to(local_rank)
# Load bucket config
from infworld.configs import bucket_config as bucket_config_module
bucket_config = getattr(bucket_config_module, BUCKET_CONFIG_NAME)
# Load prompts
prompts_path = os.path.abspath(PROMPTS_YAML)
target_prompts = OmegaConf.load(prompts_path).prompts
print(f"[InfWorld] Loaded {len(target_prompts)} prompts")
# Process each prompt
for task_idx, (prompt, image_path, action_path) in enumerate(target_prompts):
if task_idx % dp_size != dp_rank:
continue
if not os.path.exists(image_path):
print(f"[InfWorld] Skipping task {task_idx}: Image not found - {image_path}")
continue
if not os.path.exists(action_path):
print(f"[InfWorld] Skipping task {task_idx}: Action not found - {action_path}")
continue
print(f"[InfWorld] Task {task_idx}: {prompt[:50]}...")
# Load condition image
cond_video = load_condition_image(image_path, bucket_config).to(local_rank)
with torch.no_grad():
cond_latent = vae.encode(cond_video)
# Load action sequence
move_indices, view_indices = load_action_sequence(action_path)
# Initialize video buffer
video_buffer = cond_video.clone().cpu()
# Latent size for generation
latent_size = list(cond_latent.shape)
latent_size[2] = 21 # Output frames per chunk
latent_size = torch.Size(latent_size)
# Generate video chunks
for chunk_idx in range(NUM_CHUNKS):
print(f"[InfWorld] Generating chunk {chunk_idx + 1}/{NUM_CHUNKS}")
with torch.no_grad():
current_cond = video_buffer.to(local_rank)
current_latent = vae.encode(current_cond)
# Get action slice for current chunk
curr_start = video_buffer.shape[2] - 1
curr_end = curr_start + args.validation_data.num_frames
move = torch.tensor(move_indices[curr_start:curr_end], dtype=torch.long, device=local_rank)
view = torch.tensor(view_indices[curr_start:curr_end], dtype=torch.long, device=local_rank)
# Pad if needed
num_frames = args.validation_data.num_frames
if move.shape[0] < num_frames:
pad_len = num_frames - move.shape[0]
move = torch.cat([move, torch.zeros(pad_len, dtype=torch.long, device=local_rank)])
view = torch.cat([view, torch.zeros(pad_len, dtype=torch.long, device=local_rank)])
additional_args = {
"image_cond": current_latent,
"move": move.unsqueeze(0),
"view": view.unsqueeze(0),
}
torch_gc()
with torch.no_grad():
samples = scheduler.sample(
model=dit,
text_encoder=text_encoder,
null_embedder=dit.y_embedder,
z_size=latent_size,
prompts=[prompt],
guidance_scale=TEXT_CFG_SCALE,
negative_prompts=[NEGATIVE_PROMPT],
device=torch.device(local_rank),
additional_args=additional_args,
)
decoded_chunk = vae.decode(samples).cpu()
video_buffer = torch.cat([video_buffer, decoded_chunk[:, :, 1:]], dim=2)
print(f"[InfWorld] Chunk {chunk_idx + 1} done. Total frames: {video_buffer.shape[2]}")
torch_gc()
# Save final video
video_name = f"{task_idx:04d}_{prompt[:30].replace(' ', '_')}"
save_path = os.path.join(output_dir, video_name)
quality = 10 if HIGH_QUALITY_SAVE else 5
save_silent_video(video_buffer.to(local_rank), save_path, fps=30, quality=quality)
print(f"[InfWorld] Saved: {save_path}.mp4")
if __name__ == "__main__":
main()
|