-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtrain.py
More file actions
159 lines (138 loc) · 7.38 KB
/
train.py
File metadata and controls
159 lines (138 loc) · 7.38 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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import sys
import argparse
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
# Scene arguments
parser.add_argument('-s', '--source_path', type=str, required=True, help='Path to the source directory')
parser.add_argument('-o', '--output_path', type=str, default=None, help='Path to the output directory')
# Image selection parameters
parser.add_argument('--n_images', type=int, default=None,
help='Number of images to use for optimization, sampled with constant spacing. If not provided, all images will be used.')
parser.add_argument('--image_idx', type=int, nargs='*', default=None,
help='View indices to use for optimization (zero-based indexing). If provided, this will override the --n_images.')
parser.add_argument('--randomize_images', action='store_true',
help='Shuffle training images before sampling with constant spacing. If image_idx is provided, this will be ignored.')
# Dense supervision (Optional)
parser.add_argument('--dense_supervision', action='store_true',
help='Use dense RGB supervision with a COLMAP dataset. Should only be used with --sfm_config posed.')
parser.add_argument('--dense_regul', type=str, default='default', help='Strength of dense regularization. Can be "default", "strong", "weak", or "none".')
# Output mesh parameters
parser.add_argument('--use_multires_tsdf', action='store_true', help='Use multi-resolution TSDF fusion instead of adaptive tetrahedralization for mesh extraction (not recommended).')
parser.add_argument('--no_interpolated_views', action='store_true', help='Disable interpolated views for mesh extraction.')
# SfM config
parser.add_argument('--sfm_config', type=str, default='unposed', help='Config for SfM. Should be "unposed" or "posed".')
# Chart alignment config
parser.add_argument('--alignment_config', type=str, default='default', help='Config for charts alignment')
parser.add_argument('--depth_model', type=str, default="depthanythingv2")
parser.add_argument('--depthanythingv2_checkpoint_dir', type=str, default='./Depth-Anything-V2/checkpoints/')
parser.add_argument('--depthanything_encoder', type=str, default='vitl')
# Free Gaussians config
parser.add_argument('--free_gaussians_config', type=str, default=None,
help='Config for Free Gaussians refinement. '\
'By default, the config used is "default" for sparse supervision, and "long" for dense supervision.'
)
# Multi-resolution TSDF config
parser.add_argument('--tsdf_config', type=str, default='default', help='Config for multi-resolution TSDF fusion')
# Tetrahedralization config
parser.add_argument('--tetra_config', type=str, default='default', help='Config for adaptive tetrahedralization')
parser.add_argument('--tetra_downsample_ratio', type=float, default=0.5,
help='Downsample ratio for tetrahedralization. We recommend starting with 0.5 and then decreasing to 0.25 '\
'if the mesh is too dense, or increasing to 1.0 if the mesh is too sparse.'
)
# Run specific step
parser.add_argument('--sfm_only', action='store_true', help='Only run the SfM step')
parser.add_argument('--alignment_only', action='store_true', help='Only run the chart alignment step')
parser.add_argument('--refinement_only', action='store_true', help='Only run the chart refinement step')
parser.add_argument('--mesh_only', action='store_true', help='Only run the mesh extraction step')
args = parser.parse_args()
# Set output paths
if args.output_path is None:
if args.source_path.endswith(os.sep):
output_dir_name = args.source_path.split(os.sep)[-2]
else:
output_dir_name = args.source_path.split(os.sep)[-1]
args.output_path = os.path.join('output', output_dir_name)
mast3r_scene_path = os.path.join(args.output_path, 'mast3r_sfm')
aligned_charts_path = os.path.join(args.output_path, 'mast3r_sfm')
free_gaussians_path = os.path.join(args.output_path, 'free_gaussians')
tsdf_meshes_path = os.path.join(args.output_path, 'tsdf_meshes')
tetra_meshes_path = os.path.join(args.output_path, 'tetra_meshes')
# Dense supervision (Optional)
if args.dense_supervision:
dense_arg = " ".join([
"--dense_data_path", args.source_path,
])
if args.sfm_config != 'posed':
print("[WARNING] Dense supervision is only supported for posed SfM. Switching to posed SfM.")
args.sfm_config = 'posed'
else:
dense_arg = ""
# Free Gaussians refinement default config
if args.free_gaussians_config is None:
args.free_gaussians_config = 'long' if args.dense_supervision else 'default'
# Defining commands
sfm_command = " ".join([
"python", "scripts/run_sfm.py",
"--source_path", args.source_path,
"--output_path", mast3r_scene_path,
"--config", args.sfm_config,
# "--env", args.sfm_env,
"--n_images" if args.n_images is not None else "", str(args.n_images) if args.n_images is not None else "",
"--image_idx" if args.image_idx is not None else "", " ".join([str(i) for i in args.image_idx]) if args.image_idx is not None else "",
"--randomize_images" if args.randomize_images else "",
])
align_charts_command = " ".join([
"python", "scripts/align_charts.py",
"--source_path", mast3r_scene_path,
"--mast3r_scene", mast3r_scene_path,
"--output_path", aligned_charts_path,
"--config", args.alignment_config,
"--depth_model", args.depth_model,
"--depthanythingv2_checkpoint_dir", args.depthanythingv2_checkpoint_dir,
"--depthanything_encoder", args.depthanything_encoder,
])
refine_free_gaussians_command = " ".join([
"python", "scripts/refine_free_gaussians.py",
"--mast3r_scene", mast3r_scene_path,
"--output_path", free_gaussians_path,
"--config", args.free_gaussians_config,
dense_arg,
"--dense_regul", args.dense_regul,
])
tsdf_command = " ".join([
"python", "scripts/extract_tsdf_mesh.py",
"--mast3r_scene", mast3r_scene_path,
"--model_path", free_gaussians_path,
"--output_path", tsdf_meshes_path,
"--config", args.tsdf_config,
])
tetra_command = " ".join([
"python", "scripts/extract_tetra_mesh.py",
"--mast3r_scene", mast3r_scene_path,
"--model_path", free_gaussians_path,
"--output_path", tetra_meshes_path,
"--config", args.tetra_config,
"--downsample_ratio", str(args.tetra_downsample_ratio),
"--interpolate_views" if not args.no_interpolated_views else "",
dense_arg,
])
# Running commands
run_all = (
(not args.sfm_only)
and (not args.alignment_only)
and (not args.refinement_only)
and (not args.mesh_only)
)
if args.sfm_only or run_all:
os.system(sfm_command)
if args.alignment_only or run_all:
os.system(align_charts_command)
if args.refinement_only or run_all:
os.system(refine_free_gaussians_command)
if args.mesh_only or run_all:
if args.use_multires_tsdf:
os.system(tsdf_command)
else:
os.system(tetra_command)