(sample: dict[str, bytes])
| 189 | |
| 190 | def make_loader(self): |
| 191 | def construct_sample(sample: dict[str, bytes]) -> dict[str, torch.Tensor | bool]: |
| 192 | try: |
| 193 | required_keys = ["video.mpg", "tracks.npy", "visibility.npy"] |
| 194 | assert all(k in sample for k in required_keys), f"Expected keys {required_keys}, got {sample.keys()}" |
| 195 | |
| 196 | d = { |
| 197 | "tracks": torch.from_numpy(decode_npy(sample["tracks.npy"])), # [t, n_t, 2] |
| 198 | "visibility": torch.from_numpy(decode_npy(sample["visibility.npy"])), # [t, n_t] |
| 199 | } |
| 200 | |
| 201 | sample_out = self.extract_training_sample(d) |
| 202 | if not sample_out.get("valid", True): |
| 203 | return {"valid": False} |
| 204 | sample_out = self.build_targets(sample_out) |
| 205 | if not sample_out.get("valid", True): |
| 206 | return {"valid": False} |
| 207 | |
| 208 | i_f = sample_out.pop("i_frame") |
| 209 | # Retrieve target frame from video |
| 210 | with io.BytesIO(sample["video.mpg"]) as buf, av.open(buf) as container: |
| 211 | c_f = 0 |
| 212 | target_frame = None |
| 213 | for packet in container.demux(): |
| 214 | if not target_frame is None: |
| 215 | break |
| 216 | for frame in packet.decode(): |
| 217 | if c_f == i_f: |
| 218 | target_frame = frame.to_ndarray(format="rgb24") |
| 219 | break |
| 220 | c_f += 1 |
| 221 | assert c_f == i_f, f"{i_f=}, {c_f=}" |
| 222 | # Resize and normalize the target frame |
| 223 | # TODO: augment -> explicit |
| 224 | x: Float[torch.Tensor, "c h w"] = augment( |
| 225 | einops.rearrange(torch.from_numpy(target_frame).float() / 255, "h w c -> 1 c h w"), size=512 |
| 226 | )[ |
| 227 | 0 |
| 228 | ] # [-1, 1] |
| 229 | return sample_out | { |
| 230 | "x": x, |
| 231 | } |
| 232 | except Exception as e: |
| 233 | print(f"Error while constructing sample : {e}") |
| 234 | return {"valid": False} |
| 235 | |
| 236 | dataset = wds.DataPipeline( |
| 237 | wds.ResampledShards(urls=([str(f) for f in Path(self.tar_base).rglob("*.tar")])), |
nothing calls this directly
no test coverage detected