Prepare DeMemWM pose and image metadata
Browse files
algorithms/dememwm/df_video.py
CHANGED
|
@@ -48,6 +48,16 @@ def _segment_value_to_int(value, key):
|
|
| 48 |
return int(value)
|
| 49 |
|
| 50 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
def _preprocess_dememwm_latent_batch(batch):
|
| 52 |
memory_segments = {
|
| 53 |
key: _segment_value_to_int(batch["memory_segments"][key], key)
|
|
@@ -61,8 +71,10 @@ def _preprocess_dememwm_latent_batch(batch):
|
|
| 61 |
# Latent dataset batches are already VAE-encoded: B x T_all x C x H_lat x W_lat.
|
| 62 |
latents = rearrange(batch["latents"], "b t c ... -> t b c ...").contiguous()
|
| 63 |
actions = rearrange(batch["actions"], "b t d -> t b d").contiguous()
|
|
|
|
| 64 |
poses = rearrange(batch["poses"], "b t d -> t b d").contiguous()
|
| 65 |
frame_indices = rearrange(batch["frame_indices"], "b t -> t b").contiguous()
|
|
|
|
| 66 |
|
| 67 |
segment_slices = {}
|
| 68 |
start = 0
|
|
@@ -104,6 +116,7 @@ def _preprocess_dememwm_latent_batch(batch):
|
|
| 104 |
"actions": actions,
|
| 105 |
"action_conditions": action_conditions,
|
| 106 |
"poses": poses,
|
|
|
|
| 107 |
"frame_indices": frame_indices,
|
| 108 |
"memory_segments": memory_segments,
|
| 109 |
"segment_lengths": segment_lengths,
|
|
@@ -116,7 +129,7 @@ def _preprocess_dememwm_latent_batch(batch):
|
|
| 116 |
"target_tensors": target_tensors,
|
| 117 |
"stream_tensors": stream_tensors,
|
| 118 |
"memory_masks": memory_masks,
|
| 119 |
-
"image_hw":
|
| 120 |
}
|
| 121 |
|
| 122 |
|
|
|
|
| 48 |
return int(value)
|
| 49 |
|
| 50 |
|
| 51 |
+
def _normalize_dememwm_image_hw(image_hw, batch_size):
|
| 52 |
+
image_hw = image_hw if torch.is_tensor(image_hw) else torch.as_tensor(image_hw)
|
| 53 |
+
image_hw = image_hw.to(dtype=torch.long)
|
| 54 |
+
if image_hw.ndim == 1:
|
| 55 |
+
image_hw = image_hw.unsqueeze(0)
|
| 56 |
+
if image_hw.shape[0] == 1 and batch_size != 1:
|
| 57 |
+
image_hw = image_hw.expand(batch_size, -1)
|
| 58 |
+
return image_hw.contiguous()
|
| 59 |
+
|
| 60 |
+
|
| 61 |
def _preprocess_dememwm_latent_batch(batch):
|
| 62 |
memory_segments = {
|
| 63 |
key: _segment_value_to_int(batch["memory_segments"][key], key)
|
|
|
|
| 71 |
# Latent dataset batches are already VAE-encoded: B x T_all x C x H_lat x W_lat.
|
| 72 |
latents = rearrange(batch["latents"], "b t c ... -> t b c ...").contiguous()
|
| 73 |
actions = rearrange(batch["actions"], "b t d -> t b d").contiguous()
|
| 74 |
+
# Dataset poses are the frame-memory geometry metadata in packed T_all x B x 5 order.
|
| 75 |
poses = rearrange(batch["poses"], "b t d -> t b d").contiguous()
|
| 76 |
frame_indices = rearrange(batch["frame_indices"], "b t -> t b").contiguous()
|
| 77 |
+
image_hw = _normalize_dememwm_image_hw(batch["image_hw"], latents.shape[1])
|
| 78 |
|
| 79 |
segment_slices = {}
|
| 80 |
start = 0
|
|
|
|
| 116 |
"actions": actions,
|
| 117 |
"action_conditions": action_conditions,
|
| 118 |
"poses": poses,
|
| 119 |
+
"frame_memory_pose": poses,
|
| 120 |
"frame_indices": frame_indices,
|
| 121 |
"memory_segments": memory_segments,
|
| 122 |
"segment_lengths": segment_lengths,
|
|
|
|
| 129 |
"target_tensors": target_tensors,
|
| 130 |
"stream_tensors": stream_tensors,
|
| 131 |
"memory_masks": memory_masks,
|
| 132 |
+
"image_hw": image_hw,
|
| 133 |
}
|
| 134 |
|
| 135 |
|
tests/test_dememwm_latent_dataset.py
CHANGED
|
@@ -179,10 +179,44 @@ class DeMemWMLatentDatasetTests(unittest.TestCase):
|
|
| 179 |
self.assertEqual(preprocessed["target_tensors"]["action_conditions"][:, 0, 0].tolist(), [0.0, 101.0])
|
| 180 |
self.assertEqual(preprocessed["stream_tensors"]["dynamic"]["action_conditions"][:, 0, 0].tolist(), [0.0, 0.0, 0.0])
|
| 181 |
self.assertEqual(preprocessed["stream_tensors"]["dynamic"]["poses"][:, 0, 0].tolist(), [203.0, 204.0, 205.0])
|
|
|
|
|
|
|
| 182 |
self.assertEqual(preprocessed["stream_tensors"]["dynamic"]["frame_indices"][:, 0].tolist(), [13, 14, 15])
|
| 183 |
self.assertIs(preprocessed["memory_masks"]["dynamic"], batch["memory_masks"]["dynamic"])
|
| 184 |
self.assertEqual(tuple(preprocessed["memory_masks"]["dynamic"].shape), (1, 3))
|
| 185 |
self.assertEqual(preprocessed["memory_masks"]["dynamic"].device.type, "cpu")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
def test_dataset_returns_target_anchor_dynamic_revisit_contract(self):
|
| 188 |
with tempfile.TemporaryDirectory() as tmp:
|
|
@@ -240,6 +274,8 @@ class DeMemWMLatentDatasetTests(unittest.TestCase):
|
|
| 240 |
self.assertTrue(torch.equal(preprocessed["action_conditions"][3:], torch.zeros_like(preprocessed["actions"][3:])))
|
| 241 |
self.assertTrue(torch.equal(preprocessed["actions"][3:], torch.ones_like(preprocessed["actions"][3:])))
|
| 242 |
self.assertEqual(tuple(preprocessed["poses"].shape), (9, 1, 5))
|
|
|
|
|
|
|
| 243 |
self.assertEqual(tuple(preprocessed["frame_indices"].shape), (9, 1))
|
| 244 |
self.assertEqual(preprocessed["memory_segments"], sample["memory_segments"])
|
| 245 |
self.assertEqual(preprocessed["segment_lengths"], sample["memory_segments"])
|
|
|
|
| 179 |
self.assertEqual(preprocessed["target_tensors"]["action_conditions"][:, 0, 0].tolist(), [0.0, 101.0])
|
| 180 |
self.assertEqual(preprocessed["stream_tensors"]["dynamic"]["action_conditions"][:, 0, 0].tolist(), [0.0, 0.0, 0.0])
|
| 181 |
self.assertEqual(preprocessed["stream_tensors"]["dynamic"]["poses"][:, 0, 0].tolist(), [203.0, 204.0, 205.0])
|
| 182 |
+
self.assertEqual(preprocessed["frame_memory_pose"][:, 0, 0].tolist(), [200.0, 201.0, 202.0, 203.0, 204.0, 205.0])
|
| 183 |
+
self.assertIs(preprocessed["frame_memory_pose"], preprocessed["poses"])
|
| 184 |
self.assertEqual(preprocessed["stream_tensors"]["dynamic"]["frame_indices"][:, 0].tolist(), [13, 14, 15])
|
| 185 |
self.assertIs(preprocessed["memory_masks"]["dynamic"], batch["memory_masks"]["dynamic"])
|
| 186 |
self.assertEqual(tuple(preprocessed["memory_masks"]["dynamic"].shape), (1, 3))
|
| 187 |
self.assertEqual(preprocessed["memory_masks"]["dynamic"].device.type, "cpu")
|
| 188 |
+
self.assertEqual(preprocessed["image_hw"].tolist(), [[360, 640]])
|
| 189 |
+
self.assertEqual(preprocessed["image_hw"].dtype, torch.long)
|
| 190 |
+
|
| 191 |
+
def test_preprocess_expands_shared_image_hw_metadata(self):
|
| 192 |
+
from algorithms.dememwm.df_video import _preprocess_dememwm_latent_batch
|
| 193 |
+
|
| 194 |
+
batch = {
|
| 195 |
+
"latents": torch.zeros((2, 1, 1, 1, 1), dtype=torch.float32),
|
| 196 |
+
"actions": torch.zeros((2, 1, 1), dtype=torch.float32),
|
| 197 |
+
"poses": torch.arange(10, dtype=torch.float32).view(2, 1, 5),
|
| 198 |
+
"frame_indices": torch.zeros((2, 1), dtype=torch.long),
|
| 199 |
+
"memory_segments": {
|
| 200 |
+
"target": torch.tensor([1, 1]),
|
| 201 |
+
"anchor": torch.tensor([0, 0]),
|
| 202 |
+
"dynamic": torch.tensor([0, 0]),
|
| 203 |
+
"revisit": torch.tensor([0, 0]),
|
| 204 |
+
},
|
| 205 |
+
"memory_masks": {
|
| 206 |
+
"target": torch.ones((2, 1), dtype=torch.bool),
|
| 207 |
+
"anchor": torch.zeros((2, 0), dtype=torch.bool),
|
| 208 |
+
"dynamic": torch.zeros((2, 0), dtype=torch.bool),
|
| 209 |
+
"revisit": torch.zeros((2, 0), dtype=torch.bool),
|
| 210 |
+
},
|
| 211 |
+
"image_hw": torch.tensor([720, 1280], dtype=torch.long),
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
preprocessed = _preprocess_dememwm_latent_batch(batch)
|
| 215 |
+
|
| 216 |
+
self.assertEqual(tuple(preprocessed["frame_memory_pose"].shape), (1, 2, 5))
|
| 217 |
+
self.assertEqual(preprocessed["frame_memory_pose"][:, :, 0].tolist(), [[0.0, 5.0]])
|
| 218 |
+
self.assertEqual(tuple(preprocessed["image_hw"].shape), (2, 2))
|
| 219 |
+
self.assertEqual(preprocessed["image_hw"].tolist(), [[720, 1280], [720, 1280]])
|
| 220 |
|
| 221 |
def test_dataset_returns_target_anchor_dynamic_revisit_contract(self):
|
| 222 |
with tempfile.TemporaryDirectory() as tmp:
|
|
|
|
| 274 |
self.assertTrue(torch.equal(preprocessed["action_conditions"][3:], torch.zeros_like(preprocessed["actions"][3:])))
|
| 275 |
self.assertTrue(torch.equal(preprocessed["actions"][3:], torch.ones_like(preprocessed["actions"][3:])))
|
| 276 |
self.assertEqual(tuple(preprocessed["poses"].shape), (9, 1, 5))
|
| 277 |
+
self.assertIs(preprocessed["frame_memory_pose"], preprocessed["poses"])
|
| 278 |
+
self.assertEqual(preprocessed["frame_memory_pose"][:7, 0, 0].tolist(), [0.0, 1.0, 2.0, -6.0, -5.0, -2.0, -1.0])
|
| 279 |
self.assertEqual(tuple(preprocessed["frame_indices"].shape), (9, 1))
|
| 280 |
self.assertEqual(preprocessed["memory_segments"], sample["memory_segments"])
|
| 281 |
self.assertEqual(preprocessed["segment_lengths"], sample["memory_segments"])
|