xizaoqu commited on
Commit ·
a0e6519
1
Parent(s): e04fe21
mideway
Browse files- algorithms/worldmem/df_video.py +48 -48
- algorithms/worldmem/models/attention.py +2 -2
- algorithms/worldmem/models/diffusion.py +9 -9
- algorithms/worldmem/models/dit.py +28 -33
- app.py +25 -13
- configurations/huggingface.yaml +6 -5
algorithms/worldmem/df_video.py
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
|
|
| 1 |
import random
|
| 2 |
import math
|
| 3 |
import numpy as np
|
|
@@ -19,7 +20,7 @@ from .df_base import DiffusionForcingBase
|
|
| 19 |
from .models.vae import VAE_models
|
| 20 |
from .models.diffusion import Diffusion
|
| 21 |
from .models.pose_prediction import PosePredictionNet
|
| 22 |
-
|
| 23 |
|
| 24 |
# Utility Functions
|
| 25 |
def euler_to_rotation_matrix(pitch, yaw):
|
|
@@ -337,27 +338,28 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 337 |
self.n_frames = cfg.n_frames
|
| 338 |
if hasattr(cfg, "n_tokens"):
|
| 339 |
self.n_tokens = cfg.n_tokens // cfg.frame_stack
|
| 340 |
-
self.
|
| 341 |
-
self.pose_cond_dim = cfg
|
| 342 |
|
| 343 |
self.use_plucker = cfg.use_plucker
|
| 344 |
self.relative_embedding = cfg.relative_embedding
|
| 345 |
-
self.
|
| 346 |
-
self.
|
| 347 |
-
self.
|
| 348 |
self.ref_mode = getattr(cfg, "ref_mode", 'sequential')
|
| 349 |
self.log_curve = getattr(cfg, "log_curve", False)
|
| 350 |
-
self.focal_length =
|
| 351 |
self.log_video = cfg.log_video
|
| 352 |
self.self_consistency_eval = getattr(cfg, "self_consistency_eval", False)
|
| 353 |
-
self.next_frame_length = cfg
|
|
|
|
| 354 |
|
| 355 |
super().__init__(cfg)
|
| 356 |
|
| 357 |
def _build_model(self):
|
| 358 |
|
| 359 |
self.diffusion_model = Diffusion(
|
| 360 |
-
reference_length=self.
|
| 361 |
x_shape=self.x_stacked_shape,
|
| 362 |
action_cond_dim=self.action_cond_dim,
|
| 363 |
pose_cond_dim=self.pose_cond_dim,
|
|
@@ -366,19 +368,18 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 366 |
is_dit=True,
|
| 367 |
use_plucker=self.use_plucker,
|
| 368 |
relative_embedding=self.relative_embedding,
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
ref_mode=self.ref_mode
|
| 373 |
)
|
| 374 |
|
| 375 |
-
# self.register_data_mean_std(self.cfg.data_mean, self.cfg.data_std)
|
| 376 |
self.validation_lpips_model = LearnedPerceptualImagePatchSimilarity()
|
| 377 |
-
|
| 378 |
vae = VAE_models["vit-l-20-shallow-encoder"]()
|
| 379 |
self.vae = vae.eval()
|
| 380 |
|
| 381 |
-
self.
|
|
|
|
| 382 |
|
| 383 |
def _generate_noise_levels(self, xs: torch.Tensor, masks = None) -> torch.Tensor:
|
| 384 |
"""
|
|
@@ -422,7 +423,7 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 422 |
for i in range(self.n_frames):
|
| 423 |
input_pose_condition.append(
|
| 424 |
convert_to_plucker(
|
| 425 |
-
torch.cat([c2w_mat[i:i + 1], c2w_mat[-self.
|
| 426 |
0,
|
| 427 |
focal_length=self.focal_length,
|
| 428 |
image_height=xs.shape[-2],image_width=xs.shape[-1]
|
|
@@ -431,7 +432,7 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 431 |
frame_idx_list.append(
|
| 432 |
torch.cat([
|
| 433 |
frame_idx[i:i + 1] - frame_idx[i:i + 1],
|
| 434 |
-
frame_idx[-self.
|
| 435 |
]).clone()
|
| 436 |
)
|
| 437 |
input_pose_condition = torch.cat(input_pose_condition)
|
|
@@ -449,21 +450,21 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 449 |
|
| 450 |
noise_levels = self._generate_noise_levels(xs)
|
| 451 |
|
| 452 |
-
if self.
|
| 453 |
-
noise_levels[-self.
|
| 454 |
-
conditions[-self.
|
| 455 |
|
| 456 |
_, loss = self.diffusion_model(
|
| 457 |
xs,
|
| 458 |
conditions,
|
| 459 |
input_pose_condition,
|
| 460 |
noise_levels=noise_levels,
|
| 461 |
-
reference_length=self.
|
| 462 |
frame_idx=frame_idx_list
|
| 463 |
)
|
| 464 |
|
| 465 |
-
if self.
|
| 466 |
-
loss = loss[:-self.
|
| 467 |
|
| 468 |
loss = self.reweight_loss(loss, None)
|
| 469 |
|
|
@@ -472,7 +473,6 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 472 |
|
| 473 |
return {"loss": loss}
|
| 474 |
|
| 475 |
-
|
| 476 |
def on_validation_epoch_end(self, namespace="validation") -> None:
|
| 477 |
if not self.validation_step_outputs:
|
| 478 |
return
|
|
@@ -577,12 +577,12 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 577 |
x = rearrange(x, "(t b) c h w-> t b c h w", t=total_frames)
|
| 578 |
return x
|
| 579 |
|
| 580 |
-
def _generate_condition_indices(self, curr_frame,
|
| 581 |
"""
|
| 582 |
Generate indices for condition similarity based on the current frame and pose conditions.
|
| 583 |
"""
|
| 584 |
-
if curr_frame <
|
| 585 |
-
random_idx = [i for i in range(curr_frame)] + [0] * (
|
| 586 |
random_idx = np.repeat(np.array(random_idx)[:, None], xs_pred.shape[1], -1)
|
| 587 |
else:
|
| 588 |
# Generate points in a sphere and filter based on field of view
|
|
@@ -614,7 +614,7 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 614 |
])
|
| 615 |
|
| 616 |
random_idx = []
|
| 617 |
-
for _ in range(
|
| 618 |
overlap_ratio = ((in_fov1.bool() & in_fov_list).sum(1)) / in_fov1.sum()
|
| 619 |
|
| 620 |
confidence = overlap_ratio + (curr_frame - frame_idx[:curr_frame]) / curr_frame * (-0.2)
|
|
@@ -674,15 +674,15 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 674 |
|
| 675 |
return input_condition, input_pose_condition, frame_idx_list
|
| 676 |
|
| 677 |
-
def _prepare_noise_levels(self, scheduling_matrix, m, curr_frame, batch_size,
|
| 678 |
"""
|
| 679 |
Prepare noise levels for the current sampling step.
|
| 680 |
"""
|
| 681 |
from_noise_levels = np.concatenate((np.zeros((curr_frame,), dtype=np.int64), scheduling_matrix[m]))[:, None].repeat(batch_size, axis=1)
|
| 682 |
to_noise_levels = np.concatenate((np.zeros((curr_frame,), dtype=np.int64), scheduling_matrix[m + 1]))[:, None].repeat(batch_size, axis=1)
|
| 683 |
-
if
|
| 684 |
-
from_noise_levels = np.concatenate([from_noise_levels, np.zeros((
|
| 685 |
-
to_noise_levels = np.concatenate([to_noise_levels, np.zeros((
|
| 686 |
from_noise_levels = torch.from_numpy(from_noise_levels).to(self.device)
|
| 687 |
to_noise_levels = torch.from_numpy(to_noise_levels).to(self.device)
|
| 688 |
return from_noise_levels, to_noise_levels
|
|
@@ -703,7 +703,7 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 703 |
None: Appends the predicted and ground truth frames to `self.validation_step_outputs`.
|
| 704 |
"""
|
| 705 |
# Preprocess the input batch
|
| 706 |
-
|
| 707 |
xs_raw, conditions, pose_conditions, c2w_mat, frame_idx = self._preprocess_batch(batch)
|
| 708 |
|
| 709 |
|
|
@@ -744,9 +744,9 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 744 |
pbar.set_postfix({"start": start_frame, "end": curr_frame + horizon})
|
| 745 |
|
| 746 |
# Handle condition similarity logic
|
| 747 |
-
if
|
| 748 |
random_idx = self._generate_condition_indices(
|
| 749 |
-
curr_frame,
|
| 750 |
)
|
| 751 |
|
| 752 |
xs_pred = torch.cat([xs_pred, xs_pred[random_idx[:, range(xs_pred.shape[1])], range(xs_pred.shape[1])].clone()], 0)
|
|
@@ -760,7 +760,7 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 760 |
# Perform sampling for each step in the scheduling matrix
|
| 761 |
for m in range(scheduling_matrix.shape[0] - 1):
|
| 762 |
from_noise_levels, to_noise_levels = self._prepare_noise_levels(
|
| 763 |
-
scheduling_matrix, m, curr_frame, batch_size,
|
| 764 |
)
|
| 765 |
|
| 766 |
xs_pred[start_frame:] = self.diffusion_model.sample_step(
|
|
@@ -771,13 +771,13 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 771 |
to_noise_levels[start_frame:],
|
| 772 |
current_frame=curr_frame,
|
| 773 |
mode="validation",
|
| 774 |
-
reference_length=
|
| 775 |
frame_idx=frame_idx_list
|
| 776 |
).cpu()
|
| 777 |
|
| 778 |
# Remove condition similarity frames if applicable
|
| 779 |
-
if
|
| 780 |
-
xs_pred = xs_pred[:-
|
| 781 |
|
| 782 |
curr_frame += horizon
|
| 783 |
pbar.update(horizon)
|
|
@@ -794,7 +794,7 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 794 |
def interactive(self, first_frame, new_actions, first_pose, device,
|
| 795 |
memory_latent_frames, memory_actions, memory_poses, memory_c2w, memory_frame_idx):
|
| 796 |
|
| 797 |
-
|
| 798 |
|
| 799 |
if memory_latent_frames is None:
|
| 800 |
first_frame = torch.from_numpy(first_frame)
|
|
@@ -881,13 +881,13 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 881 |
)
|
| 882 |
|
| 883 |
# Handle condition similarity logic
|
| 884 |
-
if
|
| 885 |
random_idx = self._generate_condition_indices(
|
| 886 |
-
curr_frame,
|
| 887 |
)
|
| 888 |
|
| 889 |
# random_idx = np.unique(random_idx)[:, None]
|
| 890 |
-
#
|
| 891 |
xs_pred = torch.cat([xs_pred, xs_pred[random_idx[:, range(xs_pred.shape[1])], range(xs_pred.shape[1])].clone()], 0)
|
| 892 |
|
| 893 |
# Prepare input conditions and pose conditions
|
|
@@ -899,7 +899,7 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 899 |
# Perform sampling for each step in the scheduling matrix
|
| 900 |
for m in range(scheduling_matrix.shape[0] - 1):
|
| 901 |
from_noise_levels, to_noise_levels = self._prepare_noise_levels(
|
| 902 |
-
scheduling_matrix, m, curr_frame, batch_size,
|
| 903 |
)
|
| 904 |
|
| 905 |
xs_pred[start_frame:] = self.diffusion_model.sample_step(
|
|
@@ -910,13 +910,13 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 910 |
to_noise_levels[start_frame:],
|
| 911 |
current_frame=curr_frame,
|
| 912 |
mode="validation",
|
| 913 |
-
reference_length=
|
| 914 |
frame_idx=frame_idx_list
|
| 915 |
).cpu()
|
| 916 |
|
| 917 |
|
| 918 |
-
if
|
| 919 |
-
xs_pred = xs_pred[:-
|
| 920 |
|
| 921 |
curr_frame += next_horizon
|
| 922 |
pbar.update(next_horizon)
|
|
@@ -926,4 +926,4 @@ class WorldMemMinecraft(DiffusionForcingBase):
|
|
| 926 |
xs_pred = self.decode(xs_pred[n_context_frames:].to(device)).cpu()
|
| 927 |
|
| 928 |
return xs_pred.cpu().numpy(), memory_latent_frames.cpu().numpy(), memory_actions.cpu().numpy(), \
|
| 929 |
-
memory_poses.cpu().numpy(), memory_c2w.cpu().numpy(), memory_frame_idx.cpu().numpy()
|
|
|
|
| 1 |
+
import os
|
| 2 |
import random
|
| 3 |
import math
|
| 4 |
import numpy as np
|
|
|
|
| 20 |
from .models.vae import VAE_models
|
| 21 |
from .models.diffusion import Diffusion
|
| 22 |
from .models.pose_prediction import PosePredictionNet
|
| 23 |
+
import glob
|
| 24 |
|
| 25 |
# Utility Functions
|
| 26 |
def euler_to_rotation_matrix(pitch, yaw):
|
|
|
|
| 338 |
self.n_frames = cfg.n_frames
|
| 339 |
if hasattr(cfg, "n_tokens"):
|
| 340 |
self.n_tokens = cfg.n_tokens // cfg.frame_stack
|
| 341 |
+
self.memory_condition_length = cfg.memory_condition_length
|
| 342 |
+
self.pose_cond_dim = getattr(cfg, "pose_cond_dim", 5)
|
| 343 |
|
| 344 |
self.use_plucker = cfg.use_plucker
|
| 345 |
self.relative_embedding = cfg.relative_embedding
|
| 346 |
+
self.state_embed_only_on_qk = getattr(cfg, "state_embed_only_on_qk", True)
|
| 347 |
+
self.use_memory_attention = getattr(cfg, "use_memory_attention", True)
|
| 348 |
+
self.add_timestamp_embedding = cfg.add_timestamp_embedding
|
| 349 |
self.ref_mode = getattr(cfg, "ref_mode", 'sequential')
|
| 350 |
self.log_curve = getattr(cfg, "log_curve", False)
|
| 351 |
+
self.focal_length = getattr(cfg, "focal_length", 0.35)
|
| 352 |
self.log_video = cfg.log_video
|
| 353 |
self.self_consistency_eval = getattr(cfg, "self_consistency_eval", False)
|
| 354 |
+
self.next_frame_length = getattr(cfg, "next_frame_length", 1)
|
| 355 |
+
self.require_pose_prediction = getattr(cfg, "require_pose_prediction", False)
|
| 356 |
|
| 357 |
super().__init__(cfg)
|
| 358 |
|
| 359 |
def _build_model(self):
|
| 360 |
|
| 361 |
self.diffusion_model = Diffusion(
|
| 362 |
+
reference_length=self.memory_condition_length,
|
| 363 |
x_shape=self.x_stacked_shape,
|
| 364 |
action_cond_dim=self.action_cond_dim,
|
| 365 |
pose_cond_dim=self.pose_cond_dim,
|
|
|
|
| 368 |
is_dit=True,
|
| 369 |
use_plucker=self.use_plucker,
|
| 370 |
relative_embedding=self.relative_embedding,
|
| 371 |
+
state_embed_only_on_qk=self.state_embed_only_on_qk,
|
| 372 |
+
use_memory_attention=self.use_memory_attention,
|
| 373 |
+
add_timestamp_embedding=self.add_timestamp_embedding,
|
| 374 |
ref_mode=self.ref_mode
|
| 375 |
)
|
| 376 |
|
|
|
|
| 377 |
self.validation_lpips_model = LearnedPerceptualImagePatchSimilarity()
|
|
|
|
| 378 |
vae = VAE_models["vit-l-20-shallow-encoder"]()
|
| 379 |
self.vae = vae.eval()
|
| 380 |
|
| 381 |
+
if self.require_pose_prediction:
|
| 382 |
+
self.pose_prediction_model = PosePredictionNet()
|
| 383 |
|
| 384 |
def _generate_noise_levels(self, xs: torch.Tensor, masks = None) -> torch.Tensor:
|
| 385 |
"""
|
|
|
|
| 423 |
for i in range(self.n_frames):
|
| 424 |
input_pose_condition.append(
|
| 425 |
convert_to_plucker(
|
| 426 |
+
torch.cat([c2w_mat[i:i + 1], c2w_mat[-self.memory_condition_length:]]).clone(),
|
| 427 |
0,
|
| 428 |
focal_length=self.focal_length,
|
| 429 |
image_height=xs.shape[-2],image_width=xs.shape[-1]
|
|
|
|
| 432 |
frame_idx_list.append(
|
| 433 |
torch.cat([
|
| 434 |
frame_idx[i:i + 1] - frame_idx[i:i + 1],
|
| 435 |
+
frame_idx[-self.memory_condition_length:] - frame_idx[i:i + 1]
|
| 436 |
]).clone()
|
| 437 |
)
|
| 438 |
input_pose_condition = torch.cat(input_pose_condition)
|
|
|
|
| 450 |
|
| 451 |
noise_levels = self._generate_noise_levels(xs)
|
| 452 |
|
| 453 |
+
if self.memory_condition_length:
|
| 454 |
+
noise_levels[-self.memory_condition_length:] = self.diffusion_model.stabilization_level
|
| 455 |
+
conditions[-self.memory_condition_length:] *= 0
|
| 456 |
|
| 457 |
_, loss = self.diffusion_model(
|
| 458 |
xs,
|
| 459 |
conditions,
|
| 460 |
input_pose_condition,
|
| 461 |
noise_levels=noise_levels,
|
| 462 |
+
reference_length=self.memory_condition_length,
|
| 463 |
frame_idx=frame_idx_list
|
| 464 |
)
|
| 465 |
|
| 466 |
+
if self.memory_condition_length:
|
| 467 |
+
loss = loss[:-self.memory_condition_length]
|
| 468 |
|
| 469 |
loss = self.reweight_loss(loss, None)
|
| 470 |
|
|
|
|
| 473 |
|
| 474 |
return {"loss": loss}
|
| 475 |
|
|
|
|
| 476 |
def on_validation_epoch_end(self, namespace="validation") -> None:
|
| 477 |
if not self.validation_step_outputs:
|
| 478 |
return
|
|
|
|
| 577 |
x = rearrange(x, "(t b) c h w-> t b c h w", t=total_frames)
|
| 578 |
return x
|
| 579 |
|
| 580 |
+
def _generate_condition_indices(self, curr_frame, memory_condition_length, xs_pred, pose_conditions, frame_idx, horizon):
|
| 581 |
"""
|
| 582 |
Generate indices for condition similarity based on the current frame and pose conditions.
|
| 583 |
"""
|
| 584 |
+
if curr_frame < memory_condition_length:
|
| 585 |
+
random_idx = [i for i in range(curr_frame)] + [0] * (memory_condition_length - curr_frame)
|
| 586 |
random_idx = np.repeat(np.array(random_idx)[:, None], xs_pred.shape[1], -1)
|
| 587 |
else:
|
| 588 |
# Generate points in a sphere and filter based on field of view
|
|
|
|
| 614 |
])
|
| 615 |
|
| 616 |
random_idx = []
|
| 617 |
+
for _ in range(memory_condition_length):
|
| 618 |
overlap_ratio = ((in_fov1.bool() & in_fov_list).sum(1)) / in_fov1.sum()
|
| 619 |
|
| 620 |
confidence = overlap_ratio + (curr_frame - frame_idx[:curr_frame]) / curr_frame * (-0.2)
|
|
|
|
| 674 |
|
| 675 |
return input_condition, input_pose_condition, frame_idx_list
|
| 676 |
|
| 677 |
+
def _prepare_noise_levels(self, scheduling_matrix, m, curr_frame, batch_size, memory_condition_length):
|
| 678 |
"""
|
| 679 |
Prepare noise levels for the current sampling step.
|
| 680 |
"""
|
| 681 |
from_noise_levels = np.concatenate((np.zeros((curr_frame,), dtype=np.int64), scheduling_matrix[m]))[:, None].repeat(batch_size, axis=1)
|
| 682 |
to_noise_levels = np.concatenate((np.zeros((curr_frame,), dtype=np.int64), scheduling_matrix[m + 1]))[:, None].repeat(batch_size, axis=1)
|
| 683 |
+
if memory_condition_length:
|
| 684 |
+
from_noise_levels = np.concatenate([from_noise_levels, np.zeros((memory_condition_length, from_noise_levels.shape[-1]), dtype=np.int32)], axis=0)
|
| 685 |
+
to_noise_levels = np.concatenate([to_noise_levels, np.zeros((memory_condition_length, from_noise_levels.shape[-1]), dtype=np.int32)], axis=0)
|
| 686 |
from_noise_levels = torch.from_numpy(from_noise_levels).to(self.device)
|
| 687 |
to_noise_levels = torch.from_numpy(to_noise_levels).to(self.device)
|
| 688 |
return from_noise_levels, to_noise_levels
|
|
|
|
| 703 |
None: Appends the predicted and ground truth frames to `self.validation_step_outputs`.
|
| 704 |
"""
|
| 705 |
# Preprocess the input batch
|
| 706 |
+
memory_condition_length = self.memory_condition_length
|
| 707 |
xs_raw, conditions, pose_conditions, c2w_mat, frame_idx = self._preprocess_batch(batch)
|
| 708 |
|
| 709 |
|
|
|
|
| 744 |
pbar.set_postfix({"start": start_frame, "end": curr_frame + horizon})
|
| 745 |
|
| 746 |
# Handle condition similarity logic
|
| 747 |
+
if memory_condition_length:
|
| 748 |
random_idx = self._generate_condition_indices(
|
| 749 |
+
curr_frame, memory_condition_length, xs_pred, pose_conditions, frame_idx, horizon
|
| 750 |
)
|
| 751 |
|
| 752 |
xs_pred = torch.cat([xs_pred, xs_pred[random_idx[:, range(xs_pred.shape[1])], range(xs_pred.shape[1])].clone()], 0)
|
|
|
|
| 760 |
# Perform sampling for each step in the scheduling matrix
|
| 761 |
for m in range(scheduling_matrix.shape[0] - 1):
|
| 762 |
from_noise_levels, to_noise_levels = self._prepare_noise_levels(
|
| 763 |
+
scheduling_matrix, m, curr_frame, batch_size, memory_condition_length
|
| 764 |
)
|
| 765 |
|
| 766 |
xs_pred[start_frame:] = self.diffusion_model.sample_step(
|
|
|
|
| 771 |
to_noise_levels[start_frame:],
|
| 772 |
current_frame=curr_frame,
|
| 773 |
mode="validation",
|
| 774 |
+
reference_length=memory_condition_length,
|
| 775 |
frame_idx=frame_idx_list
|
| 776 |
).cpu()
|
| 777 |
|
| 778 |
# Remove condition similarity frames if applicable
|
| 779 |
+
if memory_condition_length:
|
| 780 |
+
xs_pred = xs_pred[:-memory_condition_length]
|
| 781 |
|
| 782 |
curr_frame += horizon
|
| 783 |
pbar.update(horizon)
|
|
|
|
| 794 |
def interactive(self, first_frame, new_actions, first_pose, device,
|
| 795 |
memory_latent_frames, memory_actions, memory_poses, memory_c2w, memory_frame_idx):
|
| 796 |
|
| 797 |
+
memory_condition_length = self.memory_condition_length
|
| 798 |
|
| 799 |
if memory_latent_frames is None:
|
| 800 |
first_frame = torch.from_numpy(first_frame)
|
|
|
|
| 881 |
)
|
| 882 |
|
| 883 |
# Handle condition similarity logic
|
| 884 |
+
if memory_condition_length:
|
| 885 |
random_idx = self._generate_condition_indices(
|
| 886 |
+
curr_frame, memory_condition_length, xs_pred, pose_conditions, frame_idx, next_horizon
|
| 887 |
)
|
| 888 |
|
| 889 |
# random_idx = np.unique(random_idx)[:, None]
|
| 890 |
+
# memory_condition_length = len(random_idx)
|
| 891 |
xs_pred = torch.cat([xs_pred, xs_pred[random_idx[:, range(xs_pred.shape[1])], range(xs_pred.shape[1])].clone()], 0)
|
| 892 |
|
| 893 |
# Prepare input conditions and pose conditions
|
|
|
|
| 899 |
# Perform sampling for each step in the scheduling matrix
|
| 900 |
for m in range(scheduling_matrix.shape[0] - 1):
|
| 901 |
from_noise_levels, to_noise_levels = self._prepare_noise_levels(
|
| 902 |
+
scheduling_matrix, m, curr_frame, batch_size, memory_condition_length
|
| 903 |
)
|
| 904 |
|
| 905 |
xs_pred[start_frame:] = self.diffusion_model.sample_step(
|
|
|
|
| 910 |
to_noise_levels[start_frame:],
|
| 911 |
current_frame=curr_frame,
|
| 912 |
mode="validation",
|
| 913 |
+
reference_length=memory_condition_length,
|
| 914 |
frame_idx=frame_idx_list
|
| 915 |
).cpu()
|
| 916 |
|
| 917 |
|
| 918 |
+
if memory_condition_length:
|
| 919 |
+
xs_pred = xs_pred[:-memory_condition_length]
|
| 920 |
|
| 921 |
curr_frame += next_horizon
|
| 922 |
pbar.update(next_horizon)
|
|
|
|
| 926 |
xs_pred = self.decode(xs_pred[n_context_frames:].to(device)).cpu()
|
| 927 |
|
| 928 |
return xs_pred.cpu().numpy(), memory_latent_frames.cpu().numpy(), memory_actions.cpu().numpy(), \
|
| 929 |
+
memory_poses.cpu().numpy(), memory_c2w.cpu().numpy(), memory_frame_idx.cpu().numpy()
|
algorithms/worldmem/models/attention.py
CHANGED
|
@@ -288,12 +288,12 @@ class MemFullAttention(nn.Module):
|
|
| 288 |
|
| 289 |
def forward(self, x: torch.Tensor, relative_embedding=False,
|
| 290 |
extra_condition=None,
|
| 291 |
-
|
| 292 |
reference_length=None):
|
| 293 |
|
| 294 |
B, T, H, W, D = x.shape
|
| 295 |
|
| 296 |
-
if
|
| 297 |
q, k, _ = self.to_qkv(x+extra_condition).chunk(3, dim=-1)
|
| 298 |
_, _, v = self.to_qkv(x).chunk(3, dim=-1)
|
| 299 |
else:
|
|
|
|
| 288 |
|
| 289 |
def forward(self, x: torch.Tensor, relative_embedding=False,
|
| 290 |
extra_condition=None,
|
| 291 |
+
state_embed_only_on_qk=False,
|
| 292 |
reference_length=None):
|
| 293 |
|
| 294 |
B, T, H, W, D = x.shape
|
| 295 |
|
| 296 |
+
if state_embed_only_on_qk:
|
| 297 |
q, k, _ = self.to_qkv(x+extra_condition).chunk(3, dim=-1)
|
| 298 |
_, _, v = self.to_qkv(x).chunk(3, dim=-1)
|
| 299 |
else:
|
algorithms/worldmem/models/diffusion.py
CHANGED
|
@@ -26,9 +26,9 @@ class Diffusion(nn.Module):
|
|
| 26 |
is_dit: bool=False,
|
| 27 |
use_plucker=False,
|
| 28 |
relative_embedding=False,
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
ref_mode='sequential'
|
| 33 |
):
|
| 34 |
super().__init__()
|
|
@@ -54,9 +54,9 @@ class Diffusion(nn.Module):
|
|
| 54 |
self.pose_cond_dim = pose_cond_dim
|
| 55 |
self.use_plucker = use_plucker
|
| 56 |
self.relative_embedding = relative_embedding
|
| 57 |
-
self.
|
| 58 |
-
self.
|
| 59 |
-
self.
|
| 60 |
self.ref_mode = ref_mode
|
| 61 |
|
| 62 |
self._build_model()
|
|
@@ -69,9 +69,9 @@ class Diffusion(nn.Module):
|
|
| 69 |
pose_cond_dim=self.pose_cond_dim, reference_length=self.reference_length,
|
| 70 |
use_plucker=self.use_plucker,
|
| 71 |
relative_embedding=self.relative_embedding,
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
ref_mode=self.ref_mode)
|
| 76 |
else:
|
| 77 |
raise NotImplementedError
|
|
|
|
| 26 |
is_dit: bool=False,
|
| 27 |
use_plucker=False,
|
| 28 |
relative_embedding=False,
|
| 29 |
+
state_embed_only_on_qk=False,
|
| 30 |
+
use_memory_attention=False,
|
| 31 |
+
add_timestamp_embedding=False,
|
| 32 |
ref_mode='sequential'
|
| 33 |
):
|
| 34 |
super().__init__()
|
|
|
|
| 54 |
self.pose_cond_dim = pose_cond_dim
|
| 55 |
self.use_plucker = use_plucker
|
| 56 |
self.relative_embedding = relative_embedding
|
| 57 |
+
self.state_embed_only_on_qk = state_embed_only_on_qk
|
| 58 |
+
self.use_memory_attention = use_memory_attention
|
| 59 |
+
self.add_timestamp_embedding = add_timestamp_embedding
|
| 60 |
self.ref_mode = ref_mode
|
| 61 |
|
| 62 |
self._build_model()
|
|
|
|
| 69 |
pose_cond_dim=self.pose_cond_dim, reference_length=self.reference_length,
|
| 70 |
use_plucker=self.use_plucker,
|
| 71 |
relative_embedding=self.relative_embedding,
|
| 72 |
+
state_embed_only_on_qk=self.state_embed_only_on_qk,
|
| 73 |
+
use_memory_attention=self.use_memory_attention,
|
| 74 |
+
add_timestamp_embedding=self.add_timestamp_embedding,
|
| 75 |
ref_mode=self.ref_mode)
|
| 76 |
else:
|
| 77 |
raise NotImplementedError
|
algorithms/worldmem/models/dit.py
CHANGED
|
@@ -153,8 +153,8 @@ class SpatioTemporalDiTBlock(nn.Module):
|
|
| 153 |
reference_rotary_emb=None,
|
| 154 |
use_plucker=False,
|
| 155 |
relative_embedding=False,
|
| 156 |
-
|
| 157 |
-
|
| 158 |
ref_mode='sequential'
|
| 159 |
):
|
| 160 |
super().__init__()
|
|
@@ -196,8 +196,8 @@ class SpatioTemporalDiTBlock(nn.Module):
|
|
| 196 |
)
|
| 197 |
self.t_adaLN_modulation = nn.Sequential(nn.SiLU(), nn.Linear(hidden_size, 6 * hidden_size, bias=True))
|
| 198 |
|
| 199 |
-
self.
|
| 200 |
-
if self.
|
| 201 |
self.r_norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6)
|
| 202 |
self.ref_type = "full_ref"
|
| 203 |
if self.ref_type == "temporal_ref":
|
|
@@ -233,7 +233,7 @@ class SpatioTemporalDiTBlock(nn.Module):
|
|
| 233 |
|
| 234 |
self.reference_length = reference_length
|
| 235 |
self.relative_embedding = relative_embedding
|
| 236 |
-
self.
|
| 237 |
|
| 238 |
self.ref_mode = ref_mode
|
| 239 |
|
|
@@ -265,7 +265,7 @@ class SpatioTemporalDiTBlock(nn.Module):
|
|
| 265 |
# memory block
|
| 266 |
relative_embedding = self.relative_embedding # and mode == "training"
|
| 267 |
|
| 268 |
-
if self.
|
| 269 |
r_shift_msa, r_scale_msa, r_gate_msa, r_shift_mlp, r_scale_mlp, r_gate_mlp = self.r_adaLN_modulation(c).chunk(6, dim=-1)
|
| 270 |
|
| 271 |
if pose_cond is not None:
|
|
@@ -288,7 +288,7 @@ class SpatioTemporalDiTBlock(nn.Module):
|
|
| 288 |
# if current_frame == 18:
|
| 289 |
# import pdb;pdb.set_trace()
|
| 290 |
|
| 291 |
-
if self.
|
| 292 |
attn_input = x1_zero_frame
|
| 293 |
extra_condition = input_cond
|
| 294 |
else:
|
|
@@ -304,7 +304,7 @@ class SpatioTemporalDiTBlock(nn.Module):
|
|
| 304 |
x = x + gate(self.r_attn(modulate(self.r_norm1(attn_input), r_shift_msa, r_scale_msa),
|
| 305 |
relative_embedding=relative_embedding,
|
| 306 |
extra_condition=extra_condition,
|
| 307 |
-
|
| 308 |
reference_length=reference_length), r_gate_msa)
|
| 309 |
else:
|
| 310 |
# pose_cond *= 0
|
|
@@ -354,9 +354,9 @@ class DiT(nn.Module):
|
|
| 354 |
reference_length=8,
|
| 355 |
use_plucker=False,
|
| 356 |
relative_embedding=False,
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
ref_mode='sequential'
|
| 361 |
):
|
| 362 |
super().__init__()
|
|
@@ -369,9 +369,9 @@ class DiT(nn.Module):
|
|
| 369 |
self.x_embedder = PatchEmbed(input_h, input_w, patch_size, in_channels, hidden_size, flatten=False)
|
| 370 |
self.t_embedder = TimestepEmbedder(hidden_size)
|
| 371 |
|
| 372 |
-
self.
|
| 373 |
-
if self.
|
| 374 |
-
self.
|
| 375 |
|
| 376 |
frame_h, frame_w = self.x_embedder.grid_size
|
| 377 |
|
|
@@ -404,14 +404,14 @@ class DiT(nn.Module):
|
|
| 404 |
reference_rotary_emb=self.reference_rotary_emb,
|
| 405 |
use_plucker=self.use_plucker,
|
| 406 |
relative_embedding=relative_embedding,
|
| 407 |
-
|
| 408 |
-
|
| 409 |
ref_mode=ref_mode
|
| 410 |
)
|
| 411 |
for _ in range(depth)
|
| 412 |
]
|
| 413 |
)
|
| 414 |
-
self.
|
| 415 |
self.final_layer = FinalLayer(hidden_size, patch_size, self.out_channels)
|
| 416 |
self.initialize_weights()
|
| 417 |
|
|
@@ -434,7 +434,7 @@ class DiT(nn.Module):
|
|
| 434 |
nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02)
|
| 435 |
nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02)
|
| 436 |
|
| 437 |
-
if self.
|
| 438 |
if not self.use_plucker:
|
| 439 |
nn.init.normal_(self.position_embedder.mlp[0].weight, std=0.02)
|
| 440 |
nn.init.normal_(self.position_embedder.mlp[2].weight, std=0.02)
|
|
@@ -442,9 +442,9 @@ class DiT(nn.Module):
|
|
| 442 |
nn.init.normal_(self.angle_embedder.mlp[0].weight, std=0.02)
|
| 443 |
nn.init.normal_(self.angle_embedder.mlp[2].weight, std=0.02)
|
| 444 |
|
| 445 |
-
if self.
|
| 446 |
-
nn.init.normal_(self.
|
| 447 |
-
nn.init.normal_(self.
|
| 448 |
|
| 449 |
|
| 450 |
# Zero-out adaLN modulation layers in DiT blocks:
|
|
@@ -454,7 +454,7 @@ class DiT(nn.Module):
|
|
| 454 |
nn.init.constant_(block.t_adaLN_modulation[-1].weight, 0)
|
| 455 |
nn.init.constant_(block.t_adaLN_modulation[-1].bias, 0)
|
| 456 |
|
| 457 |
-
if self.use_plucker and self.
|
| 458 |
nn.init.constant_(block.pose_cond_mlp.weight, 0)
|
| 459 |
nn.init.constant_(block.pose_cond_mlp.bias, 0)
|
| 460 |
|
|
@@ -526,10 +526,10 @@ class DiT(nn.Module):
|
|
| 526 |
pc = self.pose_embedder(pose_cond)
|
| 527 |
pc = pc.permute(1,0,2,3,4)
|
| 528 |
|
| 529 |
-
if torch.is_tensor(frame_idx) and self.
|
| 530 |
bb = frame_idx.shape[1]
|
| 531 |
frame_idx = rearrange(frame_idx, "t b -> (b t)")
|
| 532 |
-
frame_idx = self.
|
| 533 |
frame_idx = rearrange(frame_idx, "(b t) d -> b t d", b=bb)
|
| 534 |
pc = pc + frame_idx[:, :, None, None]
|
| 535 |
|
|
@@ -545,17 +545,12 @@ class DiT(nn.Module):
|
|
| 545 |
x = rearrange(x, "b t h w d -> (b t) h w d")
|
| 546 |
x = self.unpatchify(x) # (N, out_channels, H, W)
|
| 547 |
x = rearrange(x, "(b t) c h w -> b t c h w", t=T)
|
| 548 |
-
|
| 549 |
-
# print("self.blocks[0].pose_cond_mlp.weight:", self.blocks[0].pose_cond_mlp.weight)
|
| 550 |
-
# print("self.blocks[0].r_adaLN_modulation[1].weight:", self.blocks[0].r_adaLN_modulation[1].weight)
|
| 551 |
-
# print("self.blocks[0].t_adaLN_modulation[1].weight:", self.blocks[0].t_adaLN_modulation[1].weight)
|
| 552 |
-
|
| 553 |
return x
|
| 554 |
|
| 555 |
|
| 556 |
def DiT_S_2(action_cond_dim, pose_cond_dim, reference_length,
|
| 557 |
use_plucker, relative_embedding,
|
| 558 |
-
|
| 559 |
ref_mode):
|
| 560 |
return DiT(
|
| 561 |
patch_size=2,
|
|
@@ -567,9 +562,9 @@ ref_mode):
|
|
| 567 |
reference_length=reference_length,
|
| 568 |
use_plucker=use_plucker,
|
| 569 |
relative_embedding=relative_embedding,
|
| 570 |
-
|
| 571 |
-
|
| 572 |
-
|
| 573 |
ref_mode=ref_mode
|
| 574 |
)
|
| 575 |
|
|
|
|
| 153 |
reference_rotary_emb=None,
|
| 154 |
use_plucker=False,
|
| 155 |
relative_embedding=False,
|
| 156 |
+
state_embed_only_on_qk=False,
|
| 157 |
+
use_memory_attention=False,
|
| 158 |
ref_mode='sequential'
|
| 159 |
):
|
| 160 |
super().__init__()
|
|
|
|
| 196 |
)
|
| 197 |
self.t_adaLN_modulation = nn.Sequential(nn.SiLU(), nn.Linear(hidden_size, 6 * hidden_size, bias=True))
|
| 198 |
|
| 199 |
+
self.use_memory_attention = use_memory_attention
|
| 200 |
+
if self.use_memory_attention:
|
| 201 |
self.r_norm1 = nn.LayerNorm(hidden_size, elementwise_affine=False, eps=1e-6)
|
| 202 |
self.ref_type = "full_ref"
|
| 203 |
if self.ref_type == "temporal_ref":
|
|
|
|
| 233 |
|
| 234 |
self.reference_length = reference_length
|
| 235 |
self.relative_embedding = relative_embedding
|
| 236 |
+
self.state_embed_only_on_qk = state_embed_only_on_qk
|
| 237 |
|
| 238 |
self.ref_mode = ref_mode
|
| 239 |
|
|
|
|
| 265 |
# memory block
|
| 266 |
relative_embedding = self.relative_embedding # and mode == "training"
|
| 267 |
|
| 268 |
+
if self.use_memory_attention:
|
| 269 |
r_shift_msa, r_scale_msa, r_gate_msa, r_shift_mlp, r_scale_mlp, r_gate_mlp = self.r_adaLN_modulation(c).chunk(6, dim=-1)
|
| 270 |
|
| 271 |
if pose_cond is not None:
|
|
|
|
| 288 |
# if current_frame == 18:
|
| 289 |
# import pdb;pdb.set_trace()
|
| 290 |
|
| 291 |
+
if self.state_embed_only_on_qk:
|
| 292 |
attn_input = x1_zero_frame
|
| 293 |
extra_condition = input_cond
|
| 294 |
else:
|
|
|
|
| 304 |
x = x + gate(self.r_attn(modulate(self.r_norm1(attn_input), r_shift_msa, r_scale_msa),
|
| 305 |
relative_embedding=relative_embedding,
|
| 306 |
extra_condition=extra_condition,
|
| 307 |
+
state_embed_only_on_qk=self.state_embed_only_on_qk,
|
| 308 |
reference_length=reference_length), r_gate_msa)
|
| 309 |
else:
|
| 310 |
# pose_cond *= 0
|
|
|
|
| 354 |
reference_length=8,
|
| 355 |
use_plucker=False,
|
| 356 |
relative_embedding=False,
|
| 357 |
+
state_embed_only_on_qk=False,
|
| 358 |
+
use_memory_attention=False,
|
| 359 |
+
add_timestamp_embedding=False,
|
| 360 |
ref_mode='sequential'
|
| 361 |
):
|
| 362 |
super().__init__()
|
|
|
|
| 369 |
self.x_embedder = PatchEmbed(input_h, input_w, patch_size, in_channels, hidden_size, flatten=False)
|
| 370 |
self.t_embedder = TimestepEmbedder(hidden_size)
|
| 371 |
|
| 372 |
+
self.add_timestamp_embedding = add_timestamp_embedding
|
| 373 |
+
if self.add_timestamp_embedding:
|
| 374 |
+
self.timestamp_embedding = TimestepEmbedder(hidden_size)
|
| 375 |
|
| 376 |
frame_h, frame_w = self.x_embedder.grid_size
|
| 377 |
|
|
|
|
| 404 |
reference_rotary_emb=self.reference_rotary_emb,
|
| 405 |
use_plucker=self.use_plucker,
|
| 406 |
relative_embedding=relative_embedding,
|
| 407 |
+
state_embed_only_on_qk=state_embed_only_on_qk,
|
| 408 |
+
use_memory_attention=use_memory_attention,
|
| 409 |
ref_mode=ref_mode
|
| 410 |
)
|
| 411 |
for _ in range(depth)
|
| 412 |
]
|
| 413 |
)
|
| 414 |
+
self.use_memory_attention = use_memory_attention
|
| 415 |
self.final_layer = FinalLayer(hidden_size, patch_size, self.out_channels)
|
| 416 |
self.initialize_weights()
|
| 417 |
|
|
|
|
| 434 |
nn.init.normal_(self.t_embedder.mlp[0].weight, std=0.02)
|
| 435 |
nn.init.normal_(self.t_embedder.mlp[2].weight, std=0.02)
|
| 436 |
|
| 437 |
+
if self.use_memory_attention:
|
| 438 |
if not self.use_plucker:
|
| 439 |
nn.init.normal_(self.position_embedder.mlp[0].weight, std=0.02)
|
| 440 |
nn.init.normal_(self.position_embedder.mlp[2].weight, std=0.02)
|
|
|
|
| 442 |
nn.init.normal_(self.angle_embedder.mlp[0].weight, std=0.02)
|
| 443 |
nn.init.normal_(self.angle_embedder.mlp[2].weight, std=0.02)
|
| 444 |
|
| 445 |
+
if self.add_timestamp_embedding:
|
| 446 |
+
nn.init.normal_(self.timestamp_embedding.mlp[0].weight, std=0.02)
|
| 447 |
+
nn.init.normal_(self.timestamp_embedding.mlp[2].weight, std=0.02)
|
| 448 |
|
| 449 |
|
| 450 |
# Zero-out adaLN modulation layers in DiT blocks:
|
|
|
|
| 454 |
nn.init.constant_(block.t_adaLN_modulation[-1].weight, 0)
|
| 455 |
nn.init.constant_(block.t_adaLN_modulation[-1].bias, 0)
|
| 456 |
|
| 457 |
+
if self.use_plucker and self.use_memory_attention:
|
| 458 |
nn.init.constant_(block.pose_cond_mlp.weight, 0)
|
| 459 |
nn.init.constant_(block.pose_cond_mlp.bias, 0)
|
| 460 |
|
|
|
|
| 526 |
pc = self.pose_embedder(pose_cond)
|
| 527 |
pc = pc.permute(1,0,2,3,4)
|
| 528 |
|
| 529 |
+
if torch.is_tensor(frame_idx) and self.add_timestamp_embedding:
|
| 530 |
bb = frame_idx.shape[1]
|
| 531 |
frame_idx = rearrange(frame_idx, "t b -> (b t)")
|
| 532 |
+
frame_idx = self.timestamp_embedding(frame_idx)
|
| 533 |
frame_idx = rearrange(frame_idx, "(b t) d -> b t d", b=bb)
|
| 534 |
pc = pc + frame_idx[:, :, None, None]
|
| 535 |
|
|
|
|
| 545 |
x = rearrange(x, "b t h w d -> (b t) h w d")
|
| 546 |
x = self.unpatchify(x) # (N, out_channels, H, W)
|
| 547 |
x = rearrange(x, "(b t) c h w -> b t c h w", t=T)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 548 |
return x
|
| 549 |
|
| 550 |
|
| 551 |
def DiT_S_2(action_cond_dim, pose_cond_dim, reference_length,
|
| 552 |
use_plucker, relative_embedding,
|
| 553 |
+
state_embed_only_on_qk, use_memory_attention, add_timestamp_embedding,
|
| 554 |
ref_mode):
|
| 555 |
return DiT(
|
| 556 |
patch_size=2,
|
|
|
|
| 562 |
reference_length=reference_length,
|
| 563 |
use_plucker=use_plucker,
|
| 564 |
relative_embedding=relative_embedding,
|
| 565 |
+
state_embed_only_on_qk=state_embed_only_on_qk,
|
| 566 |
+
use_memory_attention=use_memory_attention,
|
| 567 |
+
add_timestamp_embedding=add_timestamp_embedding,
|
| 568 |
ref_mode=ref_mode
|
| 569 |
)
|
| 570 |
|
app.py
CHANGED
|
@@ -34,11 +34,23 @@ def load_custom_checkpoint(algo, checkpoint_path):
|
|
| 34 |
model_path = hf_hub_download(repo_id=repo_id,
|
| 35 |
filename=file_name)
|
| 36 |
ckpt = torch.load(model_path, map_location=torch.device('cpu'))
|
| 37 |
-
|
|
|
|
| 38 |
print("Load: ", model_path)
|
| 39 |
except:
|
| 40 |
ckpt = torch.load(checkpoint_path, map_location=torch.device('cpu'))
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
print("Load: ", checkpoint_path)
|
| 43 |
|
| 44 |
def download_assets_if_needed():
|
|
@@ -233,11 +245,11 @@ def set_context_length(context_length, sampling_context_length_state):
|
|
| 233 |
print("set context length to", worldmem.n_tokens)
|
| 234 |
return sampling_context_length_state
|
| 235 |
|
| 236 |
-
def
|
| 237 |
-
worldmem.
|
| 238 |
-
|
| 239 |
-
print("set memory length to", worldmem.
|
| 240 |
-
return
|
| 241 |
|
| 242 |
def set_next_frame_length(next_frame_length, sampling_next_frame_length_state):
|
| 243 |
worldmem.next_frame_length = next_frame_length
|
|
@@ -509,8 +521,8 @@ with gr.Blocks(css=css) as demo:
|
|
| 509 |
label="Context Length",
|
| 510 |
info="How many previous frames in temporal context window."
|
| 511 |
)
|
| 512 |
-
|
| 513 |
-
minimum=4, maximum=16, value=worldmem.
|
| 514 |
label="Memory Length",
|
| 515 |
info="How many previous frames in memory window. (Recommended: 1, multi-frame generation is not stable yet)"
|
| 516 |
)
|
|
@@ -522,7 +534,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 522 |
|
| 523 |
sampling_timesteps_state = gr.State(worldmem.sampling_timesteps)
|
| 524 |
sampling_context_length_state = gr.State(worldmem.n_tokens)
|
| 525 |
-
|
| 526 |
sampling_next_frame_length_state = gr.State(worldmem.next_frame_length)
|
| 527 |
|
| 528 |
video_frames = gr.State(load_image_as_tensor(selected_image.value)[None].numpy())
|
|
@@ -547,7 +559,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 547 |
|
| 548 |
examples = gr.Examples(
|
| 549 |
examples=example_images,
|
| 550 |
-
inputs=[example_case, image_output, log_output, slider_denoising_step, slider_context_length,
|
| 551 |
cache_examples=False
|
| 552 |
)
|
| 553 |
|
|
@@ -574,7 +586,7 @@ with gr.Blocks(css=css) as demo:
|
|
| 574 |
|
| 575 |
slider_denoising_step.change(fn=set_denoising_steps, inputs=[slider_denoising_step, sampling_timesteps_state], outputs=sampling_timesteps_state)
|
| 576 |
slider_context_length.change(fn=set_context_length, inputs=[slider_context_length, sampling_context_length_state], outputs=sampling_context_length_state)
|
| 577 |
-
|
| 578 |
slider_next_frame_length.change(fn=set_next_frame_length, inputs=[slider_next_frame_length, sampling_next_frame_length_state], outputs=sampling_next_frame_length_state)
|
| 579 |
|
| 580 |
-
demo.launch()
|
|
|
|
| 34 |
model_path = hf_hub_download(repo_id=repo_id,
|
| 35 |
filename=file_name)
|
| 36 |
ckpt = torch.load(model_path, map_location=torch.device('cpu'))
|
| 37 |
+
|
| 38 |
+
algo.load_state_dict(ckpt['state_dict'], strict=True)
|
| 39 |
print("Load: ", model_path)
|
| 40 |
except:
|
| 41 |
ckpt = torch.load(checkpoint_path, map_location=torch.device('cpu'))
|
| 42 |
+
|
| 43 |
+
filtered_state_dict = {}
|
| 44 |
+
for k, v in ckpt['state_dict'].items():
|
| 45 |
+
if "frame_timestep_embedder" in k:
|
| 46 |
+
new_k = k.replace("frame_timestep_embedder", "timestamp_embedding")
|
| 47 |
+
filtered_state_dict[new_k] = v
|
| 48 |
+
else:
|
| 49 |
+
filtered_state_dict[k] = v
|
| 50 |
+
|
| 51 |
+
algo.load_state_dict(filtered_state_dict, strict=True)
|
| 52 |
+
|
| 53 |
+
# algo.load_state_dict(ckpt['state_dict'], strict=True)
|
| 54 |
print("Load: ", checkpoint_path)
|
| 55 |
|
| 56 |
def download_assets_if_needed():
|
|
|
|
| 245 |
print("set context length to", worldmem.n_tokens)
|
| 246 |
return sampling_context_length_state
|
| 247 |
|
| 248 |
+
def set_memory_condition_length(memory_condition_length, sampling_memory_condition_length_state):
|
| 249 |
+
worldmem.memory_condition_length = memory_condition_length
|
| 250 |
+
sampling_memory_condition_length_state = memory_condition_length
|
| 251 |
+
print("set memory length to", worldmem.memory_condition_length)
|
| 252 |
+
return sampling_memory_condition_length_state
|
| 253 |
|
| 254 |
def set_next_frame_length(next_frame_length, sampling_next_frame_length_state):
|
| 255 |
worldmem.next_frame_length = next_frame_length
|
|
|
|
| 521 |
label="Context Length",
|
| 522 |
info="How many previous frames in temporal context window."
|
| 523 |
)
|
| 524 |
+
slider_memory_condition_length = gr.Slider(
|
| 525 |
+
minimum=4, maximum=16, value=worldmem.memory_condition_length, step=1,
|
| 526 |
label="Memory Length",
|
| 527 |
info="How many previous frames in memory window. (Recommended: 1, multi-frame generation is not stable yet)"
|
| 528 |
)
|
|
|
|
| 534 |
|
| 535 |
sampling_timesteps_state = gr.State(worldmem.sampling_timesteps)
|
| 536 |
sampling_context_length_state = gr.State(worldmem.n_tokens)
|
| 537 |
+
sampling_memory_condition_length_state = gr.State(worldmem.memory_condition_length)
|
| 538 |
sampling_next_frame_length_state = gr.State(worldmem.next_frame_length)
|
| 539 |
|
| 540 |
video_frames = gr.State(load_image_as_tensor(selected_image.value)[None].numpy())
|
|
|
|
| 559 |
|
| 560 |
examples = gr.Examples(
|
| 561 |
examples=example_images,
|
| 562 |
+
inputs=[example_case, image_output, log_output, slider_denoising_step, slider_context_length, slider_memory_condition_length],
|
| 563 |
cache_examples=False
|
| 564 |
)
|
| 565 |
|
|
|
|
| 586 |
|
| 587 |
slider_denoising_step.change(fn=set_denoising_steps, inputs=[slider_denoising_step, sampling_timesteps_state], outputs=sampling_timesteps_state)
|
| 588 |
slider_context_length.change(fn=set_context_length, inputs=[slider_context_length, sampling_context_length_state], outputs=sampling_context_length_state)
|
| 589 |
+
slider_memory_condition_length.change(fn=set_memory_condition_length, inputs=[slider_memory_condition_length, sampling_memory_condition_length_state], outputs=sampling_memory_condition_length_state)
|
| 590 |
slider_next_frame_length.change(fn=set_next_frame_length, inputs=[slider_next_frame_length, sampling_next_frame_length_state], outputs=sampling_next_frame_length_state)
|
| 591 |
|
| 592 |
+
demo.launch(share=True)
|
configurations/huggingface.yaml
CHANGED
|
@@ -3,14 +3,15 @@ pose_cond_dim: 5
|
|
| 3 |
use_plucker: true
|
| 4 |
focal_length: 0.35
|
| 5 |
customized_validation: true
|
| 6 |
-
|
| 7 |
log_video: true
|
| 8 |
relative_embedding: true
|
| 9 |
-
|
| 10 |
-
add_pose_embed: false
|
| 11 |
use_domain_adapter: false
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
| 14 |
is_interactive: true
|
| 15 |
diffusion:
|
| 16 |
sampling_timesteps: 20
|
|
|
|
| 3 |
use_plucker: true
|
| 4 |
focal_length: 0.35
|
| 5 |
customized_validation: true
|
| 6 |
+
memory_condition_length: 8
|
| 7 |
log_video: true
|
| 8 |
relative_embedding: true
|
| 9 |
+
state_embed_only_on_qk: true
|
|
|
|
| 10 |
use_domain_adapter: false
|
| 11 |
+
use_memory_attention: true
|
| 12 |
+
add_timestamp_embedding: true
|
| 13 |
+
use_pose_prediction: true
|
| 14 |
+
require_pose_prediction: true
|
| 15 |
is_interactive: true
|
| 16 |
diffusion:
|
| 17 |
sampling_timesteps: 20
|