-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathexample.py
More file actions
62 lines (53 loc) · 2.75 KB
/
example.py
File metadata and controls
62 lines (53 loc) · 2.75 KB
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
import torch
import argparse
from pi3.utils.basic import load_images_as_tensor, write_ply
from pi3.utils.geometry import depth_edge
from pi3.models.pi3 import Pi3
if __name__ == '__main__':
# --- Argument Parsing ---
parser = argparse.ArgumentParser(description="Run inference with the Pi3 model.")
parser.add_argument("--data_path", type=str, default='examples/skating.mp4',
help="Path to the input image directory or a video file.")
parser.add_argument("--save_path", type=str, default='examples/result.ply',
help="Path to save the output .ply file.")
parser.add_argument("--interval", type=int, default=-1,
help="Interval to sample image. Default: 1 for images dir, 10 for video")
parser.add_argument("--ckpt", type=str, default=None,
help="Path to the model checkpoint file. Default: None")
parser.add_argument("--device", type=str, default='cuda',
help="Device to run inference on ('cuda' or 'cpu'). Default: 'cuda'")
args = parser.parse_args()
if args.interval < 0:
args.interval = 10 if args.data_path.endswith('.mp4') else 1
print(f'Sampling interval: {args.interval}')
# 1. Prepare model
print(f"Loading model...")
device = torch.device(args.device)
if args.ckpt is not None:
model = Pi3().to(device).eval()
if args.ckpt.endswith('.safetensors'):
from safetensors.torch import load_file
weight = load_file(args.ckpt)
else:
weight = torch.load(args.ckpt, map_location=device, weights_only=False)
model.load_state_dict(weight)
else:
model = Pi3.from_pretrained("yyfz233/Pi3").to(device).eval()
# or download checkpoints from `https://huggingface.co/yyfz233/Pi3/resolve/main/model.safetensors`, and `--ckpt ckpts/model.safetensors`
# 2. Prepare input data
# The load_images_as_tensor function will print the loading path
imgs = load_images_as_tensor(args.data_path, interval=args.interval).to(device) # (N, 3, H, W)
# 3. Infer
print("Running model inference...")
dtype = torch.bfloat16 if torch.cuda.get_device_capability()[0] >= 8 else torch.float16
with torch.no_grad():
with torch.amp.autocast('cuda', dtype=dtype):
res = model(imgs[None]) # Add batch dimension
# 4. process mask
masks = torch.sigmoid(res['conf'][..., 0]) > 0.1
non_edge = ~depth_edge(res['local_points'][..., 2], rtol=0.03)
masks = torch.logical_and(masks, non_edge)[0]
# 5. Save points
print(f"Saving point cloud to: {args.save_path}")
write_ply(res['points'][0][masks].cpu(), imgs.permute(0, 2, 3, 1)[masks], args.save_path)
print("Done.")