fix: resize inputs to model img_size, use infer_and_save in CLI
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
parent
0481955cfd
commit
329db3fc43
@ -12,17 +12,11 @@ import argparse
|
|||||||
import time
|
import time
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
import mlx.core as mx
|
from corridorkey_mlx.inference.pipeline import (
|
||||||
|
DEFAULT_CHECKPOINT,
|
||||||
from corridorkey_mlx.inference.pipeline import DEFAULT_CHECKPOINT, DEFAULT_IMG_SIZE, load_model
|
DEFAULT_IMG_SIZE,
|
||||||
from corridorkey_mlx.io.image import (
|
infer_and_save,
|
||||||
load_alpha_hint,
|
load_model,
|
||||||
load_image,
|
|
||||||
postprocess_alpha,
|
|
||||||
postprocess_foreground,
|
|
||||||
preprocess,
|
|
||||||
save_alpha,
|
|
||||||
save_foreground,
|
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@ -56,38 +50,14 @@ def main() -> None:
|
|||||||
model = load_model(args.checkpoint, args.img_size)
|
model = load_model(args.checkpoint, args.img_size)
|
||||||
print(f" Model loaded in {time.perf_counter() - t0:.2f}s")
|
print(f" Model loaded in {time.perf_counter() - t0:.2f}s")
|
||||||
|
|
||||||
print(f"Preprocessing {args.image}...")
|
print(f"Running inference on {args.image}...")
|
||||||
rgb = load_image(args.image)
|
|
||||||
alpha_hint = load_alpha_hint(args.hint)
|
|
||||||
x = preprocess(rgb, alpha_hint)
|
|
||||||
# materialize input — mx.eval is MLX lazy graph eval, not Python eval
|
|
||||||
mx.eval(x) # noqa: S307
|
|
||||||
|
|
||||||
print("Running inference...")
|
|
||||||
t0 = time.perf_counter()
|
t0 = time.perf_counter()
|
||||||
outputs = model(x)
|
results = infer_and_save(model, args.image, args.hint, args.output_dir)
|
||||||
# materialize outputs — mx.eval is MLX lazy graph eval, not Python eval
|
print(f" Inference + save in {time.perf_counter() - t0:.2f}s")
|
||||||
mx.eval(outputs) # noqa: S307
|
|
||||||
print(f" Inference in {time.perf_counter() - t0:.2f}s")
|
|
||||||
|
|
||||||
# Print summary
|
print(f"\nSaved: {args.output_dir / 'alpha.png'}, {args.output_dir / 'foreground.png'}")
|
||||||
print("\nOutput tensors:")
|
print(f" Alpha shape: {results['alpha'].shape}")
|
||||||
for key, arr in outputs.items():
|
print(f" Foreground shape: {results['foreground'].shape}")
|
||||||
print(
|
|
||||||
f" {key:<20s} shape={arr.shape} "
|
|
||||||
f"min={float(mx.min(arr)):.4f} max={float(mx.max(arr)):.4f}"
|
|
||||||
)
|
|
||||||
|
|
||||||
# Save results
|
|
||||||
args.output_dir.mkdir(parents=True, exist_ok=True)
|
|
||||||
alpha_arr = postprocess_alpha(outputs["alpha_final"])
|
|
||||||
fg_arr = postprocess_foreground(outputs["fg_final"])
|
|
||||||
|
|
||||||
alpha_path = args.output_dir / "alpha.png"
|
|
||||||
fg_path = args.output_dir / "foreground.png"
|
|
||||||
save_alpha(alpha_arr, alpha_path)
|
|
||||||
save_foreground(fg_arr, fg_path)
|
|
||||||
print(f"\nSaved: {alpha_path}, {fg_path}")
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
@ -53,11 +53,10 @@ def infer(
|
|||||||
Returns:
|
Returns:
|
||||||
Model output dict with all intermediate and final tensors.
|
Model output dict with all intermediate and final tensors.
|
||||||
"""
|
"""
|
||||||
rgb = load_image(image_path)
|
img_size = model.backbone.img_size
|
||||||
alpha_hint = load_alpha_hint(alpha_hint_path)
|
rgb = load_image(image_path, img_size=img_size)
|
||||||
|
alpha_hint = load_alpha_hint(alpha_hint_path, img_size=img_size)
|
||||||
x = preprocess(rgb, alpha_hint)
|
x = preprocess(rgb, alpha_hint)
|
||||||
# materialize input
|
|
||||||
mx.eval(x) # noqa: S307
|
|
||||||
outputs = model(x)
|
outputs = model(x)
|
||||||
# materialize all outputs
|
# materialize all outputs
|
||||||
mx.eval(outputs) # noqa: S307
|
mx.eval(outputs) # noqa: S307
|
||||||
|
|||||||
@ -19,15 +19,29 @@ IMAGENET_MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32)
|
|||||||
IMAGENET_STD = np.array([0.229, 0.224, 0.225], dtype=np.float32)
|
IMAGENET_STD = np.array([0.229, 0.224, 0.225], dtype=np.float32)
|
||||||
|
|
||||||
|
|
||||||
def load_image(path: str | Path) -> np.ndarray:
|
def load_image(path: str | Path, img_size: int | None = None) -> np.ndarray:
|
||||||
"""Load image as RGB float32 array in [0, 1] range, shape (H, W, 3)."""
|
"""Load image as RGB float32 array in [0, 1] range, shape (H, W, 3).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
path: Path to image file.
|
||||||
|
img_size: If provided, resize to (img_size, img_size) using bicubic interpolation.
|
||||||
|
"""
|
||||||
img = Image.open(path).convert("RGB")
|
img = Image.open(path).convert("RGB")
|
||||||
|
if img_size is not None:
|
||||||
|
img = img.resize((img_size, img_size), Image.BICUBIC)
|
||||||
return np.asarray(img, dtype=np.float32) / 255.0
|
return np.asarray(img, dtype=np.float32) / 255.0
|
||||||
|
|
||||||
|
|
||||||
def load_alpha_hint(path: str | Path) -> np.ndarray:
|
def load_alpha_hint(path: str | Path, img_size: int | None = None) -> np.ndarray:
|
||||||
"""Load alpha hint as grayscale float32 array in [0, 1], shape (H, W, 1)."""
|
"""Load alpha hint as grayscale float32 array in [0, 1], shape (H, W, 1).
|
||||||
|
|
||||||
|
Args:
|
||||||
|
path: Path to alpha hint image file.
|
||||||
|
img_size: If provided, resize to (img_size, img_size) using bicubic interpolation.
|
||||||
|
"""
|
||||||
img = Image.open(path).convert("L")
|
img = Image.open(path).convert("L")
|
||||||
|
if img_size is not None:
|
||||||
|
img = img.resize((img_size, img_size), Image.BICUBIC)
|
||||||
return np.asarray(img, dtype=np.float32)[:, :, np.newaxis] / 255.0
|
return np.asarray(img, dtype=np.float32)[:, :, np.newaxis] / 255.0
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user