hansyan commited on
Commit
77b7e99
1 Parent(s): b225d36

Delete run.py

Browse files
Files changed (1) hide show
  1. run.py +0 -162
run.py DELETED
@@ -1,162 +0,0 @@
1
- import argparse
2
- import logging
3
- import os
4
- import time
5
-
6
- import numpy as np
7
- import rembg
8
- import torch
9
- from PIL import Image
10
-
11
- from tsr.system import TSR
12
- from tsr.utils import remove_background, resize_foreground, save_video
13
-
14
-
15
- class Timer:
16
- def __init__(self):
17
- self.items = {}
18
- self.time_scale = 1000.0 # ms
19
- self.time_unit = "ms"
20
-
21
- def start(self, name: str) -> None:
22
- if torch.cuda.is_available():
23
- torch.cuda.synchronize()
24
- self.items[name] = time.time()
25
- logging.info(f"{name} ...")
26
-
27
- def end(self, name: str) -> float:
28
- if name not in self.items:
29
- return
30
- if torch.cuda.is_available():
31
- torch.cuda.synchronize()
32
- start_time = self.items.pop(name)
33
- delta = time.time() - start_time
34
- t = delta * self.time_scale
35
- logging.info(f"{name} finished in {t:.2f}{self.time_unit}.")
36
-
37
-
38
- timer = Timer()
39
-
40
-
41
- logging.basicConfig(
42
- format="%(asctime)s - %(levelname)s - %(message)s", level=logging.INFO
43
- )
44
- parser = argparse.ArgumentParser()
45
- parser.add_argument("image", type=str, nargs="+", help="Path to input image(s).")
46
- parser.add_argument(
47
- "--device",
48
- default="cuda:0",
49
- type=str,
50
- help="Device to use. If no CUDA-compatible device is found, will fallback to 'cpu'. Default: 'cuda:0'",
51
- )
52
- parser.add_argument(
53
- "--pretrained-model-name-or-path",
54
- default="stabilityai/TripoSR",
55
- type=str,
56
- help="Path to the pretrained model. Could be either a huggingface model id is or a local path. Default: 'stabilityai/TripoSR'",
57
- )
58
- parser.add_argument(
59
- "--chunk-size",
60
- default=8192,
61
- type=int,
62
- help="Evaluation chunk size for surface extraction and rendering. Smaller chunk size reduces VRAM usage but increases computation time. 0 for no chunking. Default: 8192",
63
- )
64
- parser.add_argument(
65
- "--mc-resolution",
66
- default=256,
67
- type=int,
68
- help="Marching cubes grid resolution. Default: 256"
69
- )
70
- parser.add_argument(
71
- "--no-remove-bg",
72
- action="store_true",
73
- help="If specified, the background will NOT be automatically removed from the input image, and the input image should be an RGB image with gray background and properly-sized foreground. Default: false",
74
- )
75
- parser.add_argument(
76
- "--foreground-ratio",
77
- default=0.85,
78
- type=float,
79
- help="Ratio of the foreground size to the image size. Only used when --no-remove-bg is not specified. Default: 0.85",
80
- )
81
- parser.add_argument(
82
- "--output-dir",
83
- default="output/",
84
- type=str,
85
- help="Output directory to save the results. Default: 'output/'",
86
- )
87
- parser.add_argument(
88
- "--model-save-format",
89
- default="obj",
90
- type=str,
91
- choices=["obj", "glb"],
92
- help="Format to save the extracted mesh. Default: 'obj'",
93
- )
94
- parser.add_argument(
95
- "--render",
96
- action="store_true",
97
- help="If specified, save a NeRF-rendered video. Default: false",
98
- )
99
- args = parser.parse_args()
100
-
101
- output_dir = args.output_dir
102
- os.makedirs(output_dir, exist_ok=True)
103
-
104
- device = args.device
105
- if not torch.cuda.is_available():
106
- device = "cpu"
107
-
108
- timer.start("Initializing model")
109
- model = TSR.from_pretrained(
110
- args.pretrained_model_name_or_path,
111
- config_name="config.yaml",
112
- weight_name="model.ckpt",
113
- )
114
- model.renderer.set_chunk_size(args.chunk_size)
115
- model.to(device)
116
- timer.end("Initializing model")
117
-
118
- timer.start("Processing images")
119
- images = []
120
-
121
- if args.no_remove_bg:
122
- rembg_session = None
123
- else:
124
- rembg_session = rembg.new_session()
125
-
126
- for i, image_path in enumerate(args.image):
127
- if args.no_remove_bg:
128
- image = np.array(Image.open(image_path).convert("RGB"))
129
- else:
130
- image = remove_background(Image.open(image_path), rembg_session)
131
- image = resize_foreground(image, args.foreground_ratio)
132
- image = np.array(image).astype(np.float32) / 255.0
133
- image = image[:, :, :3] * image[:, :, 3:4] + (1 - image[:, :, 3:4]) * 0.5
134
- image = Image.fromarray((image * 255.0).astype(np.uint8))
135
- if not os.path.exists(os.path.join(output_dir, str(i))):
136
- os.makedirs(os.path.join(output_dir, str(i)))
137
- image.save(os.path.join(output_dir, str(i), f"input.png"))
138
- images.append(image)
139
- timer.end("Processing images")
140
-
141
- for i, image in enumerate(images):
142
- logging.info(f"Running image {i + 1}/{len(images)} ...")
143
-
144
- timer.start("Running model")
145
- with torch.no_grad():
146
- scene_codes = model([image], device=device)
147
- timer.end("Running model")
148
-
149
- if args.render:
150
- timer.start("Rendering")
151
- render_images = model.render(scene_codes, n_views=30, return_type="pil")
152
- for ri, render_image in enumerate(render_images[0]):
153
- render_image.save(os.path.join(output_dir, str(i), f"render_{ri:03d}.png"))
154
- save_video(
155
- render_images[0], os.path.join(output_dir, str(i), f"render.mp4"), fps=30
156
- )
157
- timer.end("Rendering")
158
-
159
- timer.start("Exporting mesh")
160
- meshes = model.extract_mesh(scene_codes, resolution=args.mc_resolution)
161
- meshes[0].export(os.path.join(output_dir, str(i), f"mesh.{args.model_save_format}"))
162
- timer.end("Exporting mesh")