BonanDing commited on
Commit
a76aea2
·
1 Parent(s): 27f451f

Match DeMemWM online dynamic stream readout

Browse files
Files changed (1) hide show
  1. algorithms/dememwm/df_video.py +20 -27
algorithms/dememwm/df_video.py CHANGED
@@ -20,8 +20,8 @@ from algorithms.common.metrics import (
20
  )
21
  from datasets.video.memory_selection import (
22
  _event_triggered_anchor_candidates_from_deltas,
23
- _nearest_unique_event_anchors,
24
  _pose_delta_values,
 
25
  _select_anchor,
26
  _select_dynamic_by_policy,
27
  _select_revisit,
@@ -331,6 +331,7 @@ def _new_online_event_cache(capacity=None):
331
  return {
332
  "stop": 0,
333
  "capacity": None if capacity is None else max(0, int(capacity)),
 
334
  "vectors": None,
335
  "d_vis": None,
336
  "d_pose": None,
@@ -403,7 +404,7 @@ def _append_online_event_values(cached, values, old_stop: int, stop: int, capaci
403
  return torch.cat([cached, values.to(device=cached.device)], dim=0)
404
 
405
 
406
- def _extend_online_event_cache(cache, latents, actions, poses, stop: int) -> None:
407
  if latents is None:
408
  return
409
  old_stop = int(cache["stop"])
@@ -451,6 +452,18 @@ def _extend_online_event_cache(cache, latents, actions, poses, stop: int) -> Non
451
  cache["d_act"] = _append_online_event_values(cache["d_act"], d_act, old_stop, target_stop, capacity)
452
  cache["stop"] = target_stop
453
 
 
 
 
 
 
 
 
 
 
 
 
 
454
 
455
  def _select_online_event_dynamic_from_cache(
456
  cache,
@@ -458,7 +471,6 @@ def _select_online_event_dynamic_from_cache(
458
  count: int,
459
  cfg,
460
  reference_frames=None,
461
- excluded=None,
462
  ) -> np.ndarray:
463
  if count <= 0:
464
  return np.empty((0,), dtype=np.int64)
@@ -471,24 +483,9 @@ def _select_online_event_dynamic_from_cache(
471
  return np.empty((0,), dtype=np.int64)
472
 
473
  stop = min(max(0, int(start_frame)), int(cache["stop"]))
474
- if stop <= 0 or cache["d_vis"] is None:
475
- return np.empty((0,), dtype=np.int64)
476
-
477
- frames = np.arange(0, stop, dtype=np.int64)
478
- event_anchors = _event_triggered_anchor_candidates_from_deltas(
479
- frames,
480
- cache["d_vis"][:stop],
481
- cache["d_pose"][:stop],
482
- cache["d_act"][:stop],
483
- cfg,
484
- )
485
- if excluded is not None and len(excluded) > 0 and len(event_anchors) > 0:
486
- event_anchors = event_anchors[~np.isin(event_anchors, np.asarray(excluded, dtype=np.int64))]
487
- if len(event_anchors) == 0:
488
- return np.empty((0,), dtype=np.int64)
489
-
490
- references = np.asarray([int(start_frame)] if reference_frames is None or len(reference_frames) == 0 else reference_frames, dtype=np.int64)
491
- return _nearest_unique_event_anchors(event_anchors, references, count)
492
 
493
 
494
  def _memory_noise_levels_for_streams(cfg, diffusion_model, query_noise_levels, stream_lengths, mode: str):
@@ -1570,7 +1567,8 @@ class DeMemWMMinecraft(DiffusionForcingBase):
1570
  source_latents[:, batch_i],
1571
  source_actions[:, batch_i],
1572
  source_poses[:, batch_i, :5],
1573
- start_frame,
 
1574
  )
1575
  selected["dynamic"] = _select_online_event_dynamic_from_cache(
1576
  cache,
@@ -1578,11 +1576,6 @@ class DeMemWMMinecraft(DiffusionForcingBase):
1578
  dynamic_count,
1579
  memory_selection_cfg,
1580
  reference_frames=selected["revisit"] if len(selected["revisit"]) > 0 else None,
1581
- excluded=(
1582
- np.concatenate([selected["anchor"], selected["revisit"]])
1583
- if len(selected["anchor"]) or len(selected["revisit"])
1584
- else None
1585
- ),
1586
  )
1587
  if memory_sheet_records is not None and int(target_positions[0]) in memory_sheet_targets:
1588
  memory_sheet_records[batch_i].append(
 
20
  )
21
  from datasets.video.memory_selection import (
22
  _event_triggered_anchor_candidates_from_deltas,
 
23
  _pose_delta_values,
24
+ _select_dynamic_from_stream,
25
  _select_anchor,
26
  _select_dynamic_by_policy,
27
  _select_revisit,
 
331
  return {
332
  "stop": 0,
333
  "capacity": None if capacity is None else max(0, int(capacity)),
334
+ "anchors": np.empty((0,), dtype=np.int64),
335
  "vectors": None,
336
  "d_vis": None,
337
  "d_pose": None,
 
404
  return torch.cat([cached, values.to(device=cached.device)], dim=0)
405
 
406
 
407
+ def _extend_online_event_cache(cache, latents, actions, poses, stop: int, cfg=None) -> None:
408
  if latents is None:
409
  return
410
  old_stop = int(cache["stop"])
 
452
  cache["d_act"] = _append_online_event_values(cache["d_act"], d_act, old_stop, target_stop, capacity)
453
  cache["stop"] = target_stop
454
 
455
+ frames = np.arange(0, target_stop, dtype=np.int64)
456
+ event_anchors = _event_triggered_anchor_candidates_from_deltas(
457
+ frames,
458
+ cache["d_vis"][:target_stop],
459
+ cache["d_pose"][:target_stop],
460
+ cache["d_act"][:target_stop],
461
+ cfg,
462
+ )
463
+ stream = np.concatenate([np.asarray([0], dtype=np.int64), event_anchors.astype(np.int64, copy=False)])
464
+ stream = stream[(stream >= 0) & (stream < target_stop)]
465
+ cache["anchors"] = np.unique(np.sort(stream.astype(np.int64, copy=False)))
466
+
467
 
468
  def _select_online_event_dynamic_from_cache(
469
  cache,
 
471
  count: int,
472
  cfg,
473
  reference_frames=None,
 
474
  ) -> np.ndarray:
475
  if count <= 0:
476
  return np.empty((0,), dtype=np.int64)
 
483
  return np.empty((0,), dtype=np.int64)
484
 
485
  stop = min(max(0, int(start_frame)), int(cache["stop"]))
486
+ anchors = np.asarray(cache.get("anchors", np.empty((0,), dtype=np.int64)), dtype=np.int64)
487
+ eligible_stream = anchors[(anchors >= 0) & (anchors < stop)]
488
+ return _select_dynamic_from_stream(eligible_stream, reference_frames, count)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
 
490
 
491
  def _memory_noise_levels_for_streams(cfg, diffusion_model, query_noise_levels, stream_lengths, mode: str):
 
1567
  source_latents[:, batch_i],
1568
  source_actions[:, batch_i],
1569
  source_poses[:, batch_i, :5],
1570
+ curr_frame,
1571
+ memory_selection_cfg,
1572
  )
1573
  selected["dynamic"] = _select_online_event_dynamic_from_cache(
1574
  cache,
 
1576
  dynamic_count,
1577
  memory_selection_cfg,
1578
  reference_frames=selected["revisit"] if len(selected["revisit"]) > 0 else None,
 
 
 
 
 
1579
  )
1580
  if memory_sheet_records is not None and int(target_positions[0]) in memory_sheet_targets:
1581
  memory_sheet_records[batch_i].append(