File size: 6,071 Bytes
c20d7cc |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
"""Contains `sharp predict` CLI implementation.
For licensing see accompanying LICENSE file.
Copyright (C) 2025 Apple Inc. All Rights Reserved.
"""
from __future__ import annotations
import logging
from pathlib import Path
import click
import numpy as np
import torch
import torch.nn.functional as F
import torch.utils.data
from sharp.models import (
PredictorParams,
RGBGaussianPredictor,
create_predictor,
)
from sharp.utils import io
from sharp.utils import logging as logging_utils
from sharp.utils.gaussians import (
Gaussians3D,
SceneMetaData,
save_ply,
unproject_gaussians,
)
from .render import render_gaussians
LOGGER = logging.getLogger(__name__)
DEFAULT_MODEL_URL = "https://ml-site.cdn-apple.com/models/sharp/sharp_2572gikvuh.pt"
@click.command()
@click.option(
"-i",
"--input-path",
type=click.Path(path_type=Path, exists=True),
help="Path to an image or containing a list of images.",
required=True,
)
@click.option(
"-o",
"--output-path",
type=click.Path(path_type=Path, file_okay=False),
help="Path to save the predicted Gaussians and renderings.",
required=True,
)
@click.option(
"-c",
"--checkpoint-path",
type=click.Path(path_type=Path, dir_okay=False),
default=None,
help="Path to the .pt checkpoint. If not provided, downloads the default model automatically.",
required=False,
)
@click.option(
"--render/--no-render",
"with_rendering",
is_flag=True,
default=False,
help="Whether to render trajectory for checkpoint.",
)
@click.option(
"--device",
type=str,
default="default",
help="Device to run on. ['cpu', 'mps', 'cuda']",
)
@click.option("-v", "--verbose", is_flag=True, help="Activate debug logs.")
def predict_cli(
input_path: Path,
output_path: Path,
checkpoint_path: Path,
with_rendering: bool,
device: str,
verbose: bool,
):
"""Predict Gaussians from input images."""
logging_utils.configure(logging.DEBUG if verbose else logging.INFO)
extensions = io.get_supported_image_extensions()
image_paths = []
if input_path.is_file():
if input_path.suffix in extensions:
image_paths = [input_path]
else:
for ext in extensions:
image_paths.extend(list(input_path.glob(f"**/*{ext}")))
if len(image_paths) == 0:
LOGGER.info("No valid images found. Input was %s.", input_path)
return
LOGGER.info("Processing %d valid image files.", len(image_paths))
if device == "default":
if torch.cuda.is_available():
device = "cuda"
elif torch.mps.is_available():
device = "mps"
else:
device = "cpu"
LOGGER.info("Using device %s", device)
if with_rendering and device != "cuda":
LOGGER.warning("Can only run rendering with gsplat on CUDA. Rendering is disabled.")
with_rendering = False
# Load or download checkpoint
if checkpoint_path is None:
LOGGER.info("No checkpoint provided. Downloading default model from %s", DEFAULT_MODEL_URL)
state_dict = torch.hub.load_state_dict_from_url(DEFAULT_MODEL_URL, progress=True)
else:
LOGGER.info("Loading checkpoint from %s", checkpoint_path)
state_dict = torch.load(checkpoint_path, weights_only=True)
gaussian_predictor = create_predictor(PredictorParams())
gaussian_predictor.load_state_dict(state_dict)
gaussian_predictor.eval()
gaussian_predictor.to(device)
output_path.mkdir(exist_ok=True, parents=True)
for image_path in image_paths:
LOGGER.info("Processing %s", image_path)
image, _, f_px = io.load_rgb(image_path)
height, width = image.shape[:2]
intrinsics = torch.tensor(
[
[f_px, 0, (width - 1) / 2.0, 0],
[0, f_px, (height - 1) / 2.0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
],
device=device,
dtype=torch.float32,
)
gaussians = predict_image(gaussian_predictor, image, f_px, torch.device(device))
LOGGER.info("Saving 3DGS to %s", output_path)
save_ply(gaussians, f_px, (height, width), output_path / f"{image_path.stem}.ply")
if with_rendering:
output_video_path = (output_path / image_path.stem).with_suffix(".mp4")
LOGGER.info("Rendering trajectory to %s", output_video_path)
metadata = SceneMetaData(intrinsics[0, 0].item(), (width, height), "linearRGB")
render_gaussians(gaussians, metadata, output_video_path)
@torch.no_grad()
def predict_image(
predictor: RGBGaussianPredictor,
image: np.ndarray,
f_px: float,
device: torch.device,
) -> Gaussians3D:
"""Predict Gaussians from an image."""
internal_shape = (1536, 1536)
LOGGER.info("Running preprocessing.")
image_pt = torch.from_numpy(image.copy()).float().to(device).permute(2, 0, 1) / 255.0
_, height, width = image_pt.shape
disparity_factor = torch.tensor([f_px / width]).float().to(device)
image_resized_pt = F.interpolate(
image_pt[None],
size=(internal_shape[1], internal_shape[0]),
mode="bilinear",
align_corners=True,
)
# Predict Gaussians in the NDC space.
LOGGER.info("Running inference.")
gaussians_ndc = predictor(image_resized_pt, disparity_factor)
LOGGER.info("Running postprocessing.")
intrinsics = (
torch.tensor(
[
[f_px, 0, width / 2, 0],
[0, f_px, height / 2, 0],
[0, 0, 1, 0],
[0, 0, 0, 1],
]
)
.float()
.to(device)
)
intrinsics_resized = intrinsics.clone()
intrinsics_resized[0] *= internal_shape[0] / width
intrinsics_resized[1] *= internal_shape[1] / height
# Convert Gaussians to metrics space.
gaussians = unproject_gaussians(
gaussians_ndc, torch.eye(4).to(device), intrinsics_resized, internal_shape
)
return gaussians
|