|
| 1 | +from collections.abc import Generator |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import click |
| 5 | +import torch |
| 6 | +import tqdm |
| 7 | +from depth_pro import Config, DepthPro |
| 8 | +from jaxtyping import UInt8 |
| 9 | +from serde.yaml import from_yaml |
| 10 | +from torchvision.transforms import Compose, ConvertImageDtype, Normalize, Resize |
| 11 | +from video_io.calibration import Calibration |
| 12 | +from video_io.reader import Reader |
| 13 | + |
| 14 | +from ..workspace import Workspace |
| 15 | + |
| 16 | + |
| 17 | +@click.command('generate-pointcloud') |
| 18 | +@click.argument('workspace_root', type=Path) |
| 19 | +@click.argument('video_name', type=str) |
| 20 | +@click.option( |
| 21 | + '--checkpoint', |
| 22 | + type=Path, |
| 23 | + required=True, |
| 24 | + help='DepthPro checkpoint to build the depth estimator from', |
| 25 | +) |
| 26 | +@click.option( |
| 27 | + '--batch-size', |
| 28 | + type=int, |
| 29 | + default=16, |
| 30 | + required=False, |
| 31 | + help='Number of frames to process as a single batch', |
| 32 | +) |
| 33 | +@click.option( |
| 34 | + '--device', |
| 35 | + type=str, |
| 36 | + required=False, |
| 37 | + help='Torch device to use for tensor computations', |
| 38 | +) |
| 39 | +def generate_pointcloud( |
| 40 | + workspace_root: Path, |
| 41 | + video_name: str, |
| 42 | + checkpoint: Path, |
| 43 | + batch_size: int, |
| 44 | + device: str | None, |
| 45 | +) -> None: |
| 46 | + workspace = Workspace.in_directory(workspace_root) |
| 47 | + output = workspace.output / 'points' |
| 48 | + |
| 49 | + for video in workspace.calibrated_videos(): |
| 50 | + if video.name == video_name: |
| 51 | + break |
| 52 | + else: |
| 53 | + raise FileNotFoundError(f'Video {video_name} not found in {workspace.input}.') |
| 54 | + |
| 55 | + calibration = from_yaml(Calibration, video.calibration.read_text()) |
| 56 | + |
| 57 | + main_device = torch.device(device or 'cpu') |
| 58 | + |
| 59 | + reader = Reader(video.location, torch.device('cpu')) |
| 60 | + depth_estimator = DepthEstimator(checkpoint, main_device) |
| 61 | + |
| 62 | + click.echo('Depth estimator created!') |
| 63 | + |
| 64 | + def batched_frames( |
| 65 | + reader: Reader, |
| 66 | + batch_size: int, |
| 67 | + ) -> Generator[torch.Tensor, None, None]: |
| 68 | + while (frames := reader.read_batch(batch_size)) is not None: |
| 69 | + yield frames |
| 70 | + |
| 71 | + progress_bar = tqdm.tqdm( |
| 72 | + range(0, reader.metadata.frames, batch_size), |
| 73 | + desc='Processing batches of frames', |
| 74 | + ) |
| 75 | + |
| 76 | + for i, frames in enumerate(batched_frames(reader, batch_size)): |
| 77 | + depths = depth_estimator.predict(frames.to(main_device)) |
| 78 | + perspective_points = calibration.unproject_depth(depths) |
| 79 | + |
| 80 | + torch.save(perspective_points, output / f'points_{i}.pt') |
| 81 | + |
| 82 | + progress_bar.update() |
| 83 | + |
| 84 | + click.echo('Done!') |
| 85 | + |
| 86 | + |
| 87 | +# TODO: Delete this from here |
| 88 | +class DepthEstimator: |
| 89 | + device: torch.device |
| 90 | + model: DepthPro |
| 91 | + model_config: Config |
| 92 | + to_model: Compose |
| 93 | + |
| 94 | + def __init__(self, checkpoint: Path, device: torch.device) -> None: |
| 95 | + self.device = device |
| 96 | + |
| 97 | + config = Config(checkpoint=checkpoint) |
| 98 | + self.model_config = config |
| 99 | + self.model = DepthPro(config, device, torch.half) |
| 100 | + |
| 101 | + self.to_model = Compose( # type: ignore[no-untyped-call] |
| 102 | + [ |
| 103 | + ConvertImageDtype(torch.half), |
| 104 | + Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5]), # type: ignore[no-untyped-call] |
| 105 | + ] |
| 106 | + ) |
| 107 | + |
| 108 | + def predict( |
| 109 | + self, |
| 110 | + frame_batch: UInt8[torch.Tensor, 'batch 3 height width'], |
| 111 | + ) -> torch.Tensor: |
| 112 | + *_, height, width = frame_batch.shape |
| 113 | + |
| 114 | + result = self.model.predict(self.to_model(frame_batch)) |
| 115 | + |
| 116 | + return ( |
| 117 | + Resize((height, width)) # type: ignore[no-untyped-call] |
| 118 | + .forward(result.depth) |
| 119 | + .to(torch.float32) |
| 120 | + ) |
0 commit comments