Repository navigation
Expand file tree
/
Copy pathdemo.py
More file actions
128 lines (106 loc) · 4.41 KB
/
Copy pathdemo.py
File metadata and controls
128 lines (106 loc) · 4.41 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from __future__ import annotations
import argparse
import subprocess
import sys
from pathlib import Path
REPO_ROOT = Path(__file__).resolve().parent
EXAMPLE_INPUTS = {
'ex1': {
'map': None,
'prompt': "examples/prompt1.txt",
},
'ex2': {
'map': None,
'prompt': "examples/prompt2.txt",
},
'ex3': {
'map': "examples/segmap3.png",
'prompt': "examples/prompt3.txt",
},
'ex4': {
'map': "examples/segmap2.png",
'prompt': "examples/prompt4.txt",
},
}
def latest_log_dir() -> Path:
candidates = [p for p in (REPO_ROOT / "logs").iterdir() if p.is_dir()]
if not candidates:
raise RuntimeError("No stage1 log directory was created.")
return max(candidates, key=lambda p: p.stat().st_mtime)
def available_examples() -> str:
return ", ".join(sorted(EXAMPLE_INPUTS))
def resolve_input_path(path: str | Path) -> Path:
path = Path(path)
if not path.is_absolute():
path = REPO_ROOT / path
return path.resolve()
def read_prompt_file(path: Path) -> list[str]:
if not path.exists():
raise FileNotFoundError(f"Missing prompt file: {path}")
prompts = [line.strip() for line in path.read_text(encoding="utf-8").splitlines()]
prompts = [line for line in prompts if line]
if not prompts:
raise ValueError(f"Prompt file is empty: {path}")
return prompts
def example_stage1_args(example_name: str, seed: int | None, scale_level: int, size: list[int], use_preset_noise: bool) -> list[str]:
example = EXAMPLE_INPUTS[example_name]
stage1_args = []
if seed is not None:
stage1_args.extend(["--seed", str(seed)])
stage1_args.extend(["--scale_level", str(scale_level)])
stage1_args.extend(["--size", *map(str, size)])
if example["map"] is not None:
map_path = resolve_input_path(example["map"])
if not map_path.exists():
raise FileNotFoundError(f"Missing map for example '{example_name}': {map_path}")
map_path = str(map_path)
else:
map_path = "rand" # Use random segmentation map if not specified
stage1_args.extend(["--segmap", map_path])
prompt_path = resolve_input_path(example["prompt"])
prompts = read_prompt_file(prompt_path)
stage1_args.append("--prompt")
stage1_args.extend(prompts)
if not use_preset_noise:
stage1_args.append("--no_preset_noise")
return stage1_args
def main() -> int:
parser = argparse.ArgumentParser(description="Run Map2World coarse generation followed by the detail enhancer.")
parser.add_argument("example", help=f"Example name. Available: {available_examples()}")
parser.add_argument('--size', type=int, nargs="+", default=[2,2,1])
parser.add_argument('--scale_level', type=int, default=2)
parser.add_argument("--seed", type=int, default=None, help="Defaults to a random seed generated by stage1.")
parser.add_argument("--skip_stage2", action="store_true")
parser.add_argument("--stage2_mode", default="doublex2", choices=("singlex2", "doublex2", "singlex4", "doublex4"))
parser.add_argument("--no_stage1_preset_noise", dest="stage1_preset_noise", action="store_false")
parser.add_argument("--no_stage2_noise_subsample", dest="stage2_noise_subsample", action="store_false")
parser.set_defaults(stage1_preset_noise=True, stage2_noise_subsample=True)
args = parser.parse_args()
if args.example not in EXAMPLE_INPUTS:
parser.error(f"Unknown example '{args.example}'. Available examples: {available_examples()}")
stage1_cmd = [
sys.executable,
str(REPO_ROOT / "stage1_SceneGeneration.py"),
*example_stage1_args(args.example, args.seed, args.scale_level, args.size, args.stage1_preset_noise),
]
print("[demo:stage1] " + " ".join(stage1_cmd))
subprocess.run(stage1_cmd, cwd=str(REPO_ROOT), check=True)
stage1_dir = latest_log_dir()
print(f"[demo] stage1 output: {stage1_dir}")
if args.skip_stage2:
return 0
stage2_cmd = [
sys.executable,
str(REPO_ROOT / "stage2_DetailEnhancer.py"),
str(stage1_dir),
"--mode",
args.stage2_mode,
"--render_formats",
"gaussian",
]
if not args.stage2_noise_subsample:
stage2_cmd.append("--no_noise_subsample")
print("[demo:stage2] " + " ".join(stage2_cmd))
return subprocess.run(stage2_cmd, cwd=str(REPO_ROOT)).returncode
if __name__ == "__main__":
raise SystemExit(main())