File size: 4,258 Bytes
3e56913 | 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 | """Clips of consecutive 4 Hz SimLingo frames with raw 3D labels, for the streaming detector.
A sample is clip_len consecutive frames of one route: uint8 images [T, 3, H, W], padded label rows
[T, MAX_OBJECTS, OBJ_DIM], counts [T], world ego poses [T, 3] (x, y, yaw) and timestamps [T] seconds.
"""
import glob
from pathlib import Path
import numpy as np
import torch
from torch.utils.data import Dataset
from dataloader.detection_data import load_image, route_split
from dataloader.detection_labels import EGO_X, EGO_Y, EGO_YAW, MAX_OBJECTS
FRAME_DT = 0.25
class DetectionClipDataset(Dataset):
def __init__(self, data_root="simlingo_data", split="train", clip_len=4, clip_stride=1, route_filter=None,
val_mod=50):
self.data_root = Path(data_root).resolve()
self.clip_len = int(clip_len)
label_files = sorted(glob.glob(str(self.data_root / "detection_labels/simlingo/*/*/*/Town*/labels.npz")))
routes, index = [], []
for lf in label_files:
route_dir = Path(lf).parent
rel = route_dir.relative_to(self.data_root / "detection_labels")
if route_split(rel, val_mod) != split:
continue
if route_filter is not None and not route_filter(str(rel)):
continue
with np.load(lf) as z:
n = len(z["count"])
if n < self.clip_len:
continue
r = len(routes)
routes.append((str(self.data_root / "data" / rel), lf))
index.extend((r, f) for f in range(0, n - self.clip_len + 1, clip_stride))
self.routes = routes
self.index = np.asarray(index, dtype=np.int32).reshape(-1, 2)
self._cache = {}
def __len__(self):
return len(self.index)
def _labels(self, r):
if r not in self._cache:
if len(self._cache) > 64:
self._cache.clear()
with np.load(self.routes[r][1]) as z:
self._cache[r] = (z["objects"], z["count"], z["ego"])
return self._cache[r]
def __getitem__(self, i):
r, f0 = (int(v) for v in self.index[i])
route_dir = self.routes[r][0]
objects, count, ego = self._labels(r)
frames = range(f0, f0 + self.clip_len)
images = torch.stack([load_image(f"{route_dir}/rgb/{f:04d}.jpg") for f in frames])
rows = np.zeros((self.clip_len, MAX_OBJECTS, objects.shape[-1]), dtype=np.float32)
for t, f in enumerate(frames):
rows[t, :count[f]] = objects[f, :count[f]]
pose = np.stack([ego[f0:f0 + self.clip_len, EGO_X], ego[f0:f0 + self.clip_len, EGO_Y],
ego[f0:f0 + self.clip_len, EGO_YAW]], -1).astype(np.float32)
return dict(
image=images, rows=torch.from_numpy(rows),
count=torch.from_numpy(count[f0:f0 + self.clip_len].astype(np.int64)),
pose=torch.from_numpy(pose),
time=torch.arange(f0, f0 + self.clip_len, dtype=torch.float32) * FRAME_DT,
sample=torch.tensor((r, f0), dtype=torch.int32),
)
def sample_weights(self, walker_weight=5.0, moving_weight=1.0):
"""Per-clip weights from the per-frame flags (detection_labels/frame_flags.npz): a clip counts as
containing walkers / moving cars when any of its frames does."""
with np.load(self.data_root / "detection_labels/frame_flags.npz") as z:
starts = np.concatenate(([0], np.cumsum(z["lengths"])[:-1]))
where = {r: (s, n) for r, s, n in zip(z["routes"], starts, z["lengths"])}
flags = z["flags"]
per_route = []
for route_dir, _ in self.routes:
s, n = where[str(Path(route_dir).relative_to(self.data_root / "data"))]
per_route.append(flags[s:s + n])
f = np.stack([per_route[r][i:i + self.clip_len].max(0) for r, i in self.index]).astype(np.float64)
return 1.0 + (walker_weight - 1.0) * (f[:, 0] > 0) + (moving_weight - 1.0) * (f[:, 1] > 0)
def gt_rows(self, i, t):
"""Raw ground-truth rows of frame t of clip i (for evaluation)."""
r, f0 = (int(v) for v in self.index[i])
objects, count, _ = self._labels(r)
return objects[f0 + t, :count[f0 + t]]
|