AGT / code /agt /eval /output_grid.py
MRBSTUDIO's picture
Update code and model card
fb3460f verified
Raw History Blame Contribute Delete
1.91 kB
from __future__ import annotations
import numpy as np
from agt.data.grid import ScoringGrid
def to_scoring_grid(frames, grid: ScoringGrid) -> np.ndarray:
frames = np.asarray(frames)
if frames.ndim != 4 or frames.shape[-1] != 3:
raise ValueError(f'Expected (N,H,W,3) renders, got {frames.shape}')
if frames.dtype == np.uint8:
frames = frames.astype(np.float32) / np.float32(255)
elif frames.dtype in (np.float32, np.float64) and np.isfinite(frames).all():
frames = frames.astype(np.float32)
else:
raise ValueError('Renders must be uint8 or finite float32/float64 in [0, 1]')
if frames.shape[1:3] != (grid.height, grid.width):
frames = grid.continuous(frames, batched=True)
return frames
def nearest_index(src: int, dst: int) -> np.ndarray:
return np.minimum(np.floor((np.arange(dst) + .5) * (src / dst)).astype(np.int64), src - 1)
def depth_to_scoring_grid(depth, alpha, grid: ScoringGrid):
depth = np.asarray(depth)
if depth.ndim != 3 or depth.dtype not in (np.float16, np.float32, np.float64):
raise ValueError(f'Expected float (N,H,W) depth, got {depth.dtype} {depth.shape}')
if alpha is not None:
alpha = np.asarray(alpha)
if alpha.shape != depth.shape or alpha.dtype not in (np.float16, np.float32, np.float64):
raise ValueError(f'alpha {alpha.dtype} {alpha.shape} does not match depth {depth.shape}')
out = []
for values in (depth, alpha):
if values is None:
out.append(None)
continue
values = values.astype(np.float32)
if values.shape[1:] != (grid.height, grid.width):
iy = nearest_index(values.shape[1], grid.height)
ix = nearest_index(values.shape[2], grid.width)
values = values[:, iy[:, None], ix[None, :]]
out.append(np.ascontiguousarray(values))
return out[0], out[1]