MCPcopy Create free account
hub / github.com/apple/ml-sharp / predict_image

Function predict_image

src/sharp/cli/predict.py:159–206  ·  view source on GitHub ↗

Predict Gaussians from an image.

(
    predictor: RGBGaussianPredictor,
    image: np.ndarray,
    f_px: float,
    device: torch.device,
)

Source from the content-addressed store, hash-verified

157
158@torch.no_grad()
159def predict_image(
160 predictor: RGBGaussianPredictor,
161 image: np.ndarray,
162 f_px: float,
163 device: torch.device,
164) -> Gaussians3D:
165 """Predict Gaussians from an image."""
166 internal_shape = (1536, 1536)
167
168 LOGGER.info("Running preprocessing.")
169 image_pt = torch.from_numpy(image.copy()).float().to(device).permute(2, 0, 1) / 255.0
170 _, height, width = image_pt.shape
171 disparity_factor = torch.tensor([f_px / width]).float().to(device)
172
173 image_resized_pt = F.interpolate(
174 image_pt[None],
175 size=(internal_shape[1], internal_shape[0]),
176 mode="bilinear",
177 align_corners=True,
178 )
179
180 # Predict Gaussians in the NDC space.
181 LOGGER.info("Running inference.")
182 gaussians_ndc = predictor(image_resized_pt, disparity_factor)
183
184 LOGGER.info("Running postprocessing.")
185 intrinsics = (
186 torch.tensor(
187 [
188 [f_px, 0, width / 2, 0],
189 [0, f_px, height / 2, 0],
190 [0, 0, 1, 0],
191 [0, 0, 0, 1],
192 ]
193 )
194 .float()
195 .to(device)
196 )
197 intrinsics_resized = intrinsics.clone()
198 intrinsics_resized[0] *= internal_shape[0] / width
199 intrinsics_resized[1] *= internal_shape[1] / height
200
201 # Convert Gaussians to metrics space.
202 gaussians = unproject_gaussians(
203 gaussians_ndc, torch.eye(4).to(device), intrinsics_resized, internal_shape
204 )
205
206 return gaussians

Callers 1

predict_cliFunction · 0.85

Calls 2

unproject_gaussiansFunction · 0.90
toMethod · 0.80

Tested by

no test coverage detected