-
Notifications
You must be signed in to change notification settings - Fork 0
/
helper3dg.py
executable file
·391 lines (290 loc) · 14.3 KB
/
helper3dg.py
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
#
# Copyright (C) 2023, Inria
# GRAPHDECO research group, https://team.inria.fr/graphdeco
# All rights reserved.
#
# This software is free for non-commercial, research and evaluation use
# under the terms of the LICENSE.md file.
#
# For inquiries contact [email protected]
#
import os
import torch
from random import randint
import random
import sys
import uuid
import time
import json
import numpy as np
import cv2
from tqdm import tqdm
import shutil
sys.path.append("./thirdparty/gaussian_splatting")
from utils.general_utils import safe_state
from argparse import ArgumentParser, Namespace
from arguments import ModelParams, PipelineParams, OptimizationParams, get_combined_args
def getparser():
parser = ArgumentParser(description="Training script parameters")
lp = ModelParams(parser)
op = OptimizationParams(parser) #we put more parameters in optimization params, just for convenience.
pp = PipelineParams(parser)
parser.add_argument('--ip', type=str, default="127.0.0.1")
parser.add_argument('--port', type=int, default=6029)
parser.add_argument('--debug_from', type=int, default=-2)
parser.add_argument('--detect_anomaly', action='store_true', default=False)
parser.add_argument("--save_iterations", nargs="+", type=int, default=[100, 5_000,7000,9_000, 12000,13_000,14_000, 15_000,17_000,18_000,20_000,23_000,25_000, 28_000,30_000])
parser.add_argument("--testing_iterations", nargs="+", type=int, default=[3000,4000,5_000,6_000,7_000,8_000,9_000,10_000,11_000,12000,12_500,13_000, 15_000,17_000,20_000,23_000,25_000, 28_000,30_000,30_500,31_000])
parser.add_argument("--test_iteration", default=-1, type=int)
parser.add_argument("--reload_iteration", default=None, type=str)
parser.add_argument("--quiet", action="store_true")
parser.add_argument("--checkpoint_iterations", nargs="+", type=int, default=[])
parser.add_argument("--start_checkpoint", type=str, default = None)
parser.add_argument("--densify", type=int, default=1, help="densify =1, we control points on N3d dataset")
parser.add_argument("--duration", type=int, default=5, help="5 debug , 50 used")
parser.add_argument("--basicfunction", type=str, default = "gaussian")
parser.add_argument("--rgbfunction", type=str, default = "rgbv1")
parser.add_argument("--rdpip", type=str, default = "v2")
parser.add_argument("--configpath", type=str, default = "None")
parser.add_argument("--exp_name",type=str,default=None)
parser.add_argument("--dataset",type=str,default=None)#表示是哪个dataset
parser.add_argument("--checkpoint",type=str,default=None)
parser.add_argument("--no_wandb", action="store_true")
parser.add_argument("--no_report",action="store_true")
parser.add_argument("--start_iteration",type=int,default=0)
args = parser.parse_args(sys.argv[1:])
args.save_iterations.append(args.iterations)
#config中的参数值会覆盖命令行提供的参数值!
# incase we provide config file not directly pass to the file
if os.path.exists(args.configpath) and args.configpath != "None":
print("overload config from " + args.configpath)
config = json.load(open(args.configpath))
for k in config.keys():
try:
value = getattr(args, k)
newvalue = config[k]
setattr(args, k, newvalue)
except:
print("failed set config: " + k)
print("finish load config from " + args.configpath)
else:
raise ValueError("config file not exist or not provided")
if args.model_path == "":
# print(args.dataset,args.exp_name)
args.model_path = os.path.join("log",os.path.join(args.dataset, args.exp_name ))
print("Optimizing " + args.model_path)
# Initialize system state (RNG)
safe_state(args.quiet)
torch.autograd.set_detect_anomaly(args.detect_anomaly)
if not os.path.exists(args.model_path):
os.makedirs(args.model_path)
return args, lp.extract(args), op.extract(args), pp.extract(args)
def getrenderparts(render_pkg):
return render_pkg["render"], render_pkg["viewspace_points"], render_pkg["visibility_filter"], render_pkg["radii"]
def gettestparse():
parser = ArgumentParser(description="Testing script parameters")
model = ModelParams(parser, sentinel=True)#并不保持Model的默认值,将默认值设为None
pipeline = PipelineParams(parser)
parser.add_argument("--skip_train", action="store_true")
parser.add_argument("--skip_test", action="store_true")
parser.add_argument("--skip_val", action="store_true")
# parser.add_argument("--configpath", type=str, default = "1")
parser.add_argument("--iteration",type=str,default="best") #调用哪个模型去测试
parser.add_argument("--require_segment",default=False,action="store_true") #调用哪个模型去测试
parser.add_argument("--quiet", action="store_true")
#读取cfg里面的值,具体做法是用目前parser非None的值(在命令行中提供的值)去替换cfg里的值。但不会对cfg新增值
args = get_combined_args(parser)
print("Rendering " + args.model_path)
# configpath
safe_state(args.quiet)
# multiview = True if args.valloader.endswith("mv") else False
#将parser的值覆盖成config的值,但对于parser中没有的值不会新加入
# if os.path.exists(args.configpath) and args.configpath != "None":
# print("overload config from " + args.configpath)
# config = json.load(open(args.configpath))
# for k in config.keys():
# try:
# value = getattr(args, k)
# newvalue = config[k]
# setattr(args, k, newvalue)
# except:
# print("failed set config: " + k)
# print("finish load config from " + args.configpath)
# print("args: " + str(args))
# # print(args,model.extract(args), pipeline.extract(args), multiview)
return args, model.extract(args), pipeline.extract(args)
def getcolmapsinglen3d(folder, offset):
folder = os.path.join(folder, "colmap_" + str(offset))
assert os.path.exists(folder)
dbfile = os.path.join(folder, "input.db")
inputimagefolder = os.path.join(folder, "input")
distortedmodel = os.path.join(folder, "distorted/sparse")
step2model = os.path.join(folder, "tmp")
if not os.path.exists(step2model):
os.makedirs(step2model)
manualinputfolder = os.path.join(folder, "manual")
if not os.path.exists(distortedmodel):
os.makedirs(distortedmodel)
featureextract = "colmap feature_extractor --database_path " + dbfile+ " --image_path " + inputimagefolder
exit_code = os.system(featureextract)
if exit_code != 0:
exit(exit_code)
featurematcher = "colmap exhaustive_matcher --database_path " + dbfile
exit_code = os.system(featurematcher)
if exit_code != 0:
exit(exit_code)
# threshold is from https://github.com/google-research/multinerf/blob/5b4d4f64608ec8077222c52fdf814d40acc10bc1/scripts/local_colmap_and_resize.sh#L62
triandmap = "colmap point_triangulator --database_path "+ dbfile + " --image_path "+ inputimagefolder + " --output_path " + distortedmodel \
+ " --input_path " + manualinputfolder + " --Mapper.ba_global_function_tolerance=0.000001"
exit_code = os.system(triandmap)
if exit_code != 0:
exit(exit_code)
print(triandmap)
img_undist_cmd = "colmap" + " image_undistorter --image_path " + inputimagefolder + " --input_path " + distortedmodel + " --output_path " + folder \
+ " --output_type COLMAP"
exit_code = os.system(img_undist_cmd)
if exit_code != 0:
exit(exit_code)
print(img_undist_cmd)
removeinput = "rm -r " + inputimagefolder
exit_code = os.system(removeinput)
if exit_code != 0:
exit(exit_code)
files = os.listdir(folder + "/sparse")
os.makedirs(folder + "/sparse/0", exist_ok=True)
for file in files:
if file == '0':
continue
source_file = os.path.join(folder, "sparse", file)
destination_file = os.path.join(folder, "sparse", "0", file)
shutil.move(source_file, destination_file)
def getcolmapsingleimundistort(folder, offset):
folder = os.path.join(folder, "colmap_" + str(offset))
assert os.path.exists(folder)
dbfile = os.path.join(folder, "input.db")
inputimagefolder = os.path.join(folder, "input")
distortedmodel = os.path.join(folder, "distorted/sparse")
step2model = os.path.join(folder, "tmp")
if not os.path.exists(step2model):
os.makedirs(step2model)
manualinputfolder = os.path.join(folder, "manual")
if not os.path.exists(distortedmodel):
os.makedirs(distortedmodel)
featureextract = "colmap feature_extractor SiftExtraction.max_image_size 6000 --database_path " + dbfile+ " --image_path " + inputimagefolder
exit_code = os.system(featureextract)
if exit_code != 0:
exit(exit_code)
featurematcher = "colmap exhaustive_matcher --database_path " + dbfile
exit_code = os.system(featurematcher)
if exit_code != 0:
exit(exit_code)
triandmap = "colmap point_triangulator --database_path "+ dbfile + " --image_path "+ inputimagefolder + " --output_path " + distortedmodel \
+ " --input_path " + manualinputfolder + " --Mapper.ba_global_function_tolerance=0.000001"
exit_code = os.system(triandmap)
if exit_code != 0:
exit(exit_code)
print(triandmap)
img_undist_cmd = "colmap" + " image_undistorter --image_path " + inputimagefolder + " --input_path " + distortedmodel + " --output_path " + folder \
+ " --output_type COLMAP " # --blank_pixels 1
exit_code = os.system(img_undist_cmd)
if exit_code != 0:
exit(exit_code)
print(img_undist_cmd)
removeinput = "rm -r " + inputimagefolder
exit_code = os.system(removeinput)
if exit_code != 0:
exit(exit_code)
files = os.listdir(folder + "/sparse")
os.makedirs(folder + "/sparse/0", exist_ok=True)
#Copy each file from the source directory to the destination directory
for file in files:
if file == '0':
continue
source_file = os.path.join(folder, "sparse", file)
destination_file = os.path.join(folder, "sparse", "0", file)
shutil.move(source_file, destination_file)
def getcolmapsingleimdistort(folder, offset):
folder = os.path.join(folder, "colmap_" + str(offset))
assert os.path.exists(folder)
dbfile = os.path.join(folder, "input.db")
inputimagefolder = os.path.join(folder, "input")
distortedmodel = os.path.join(folder, "distorted/sparse")
step2model = os.path.join(folder, "tmp")
if not os.path.exists(step2model):
os.makedirs(step2model)
manualinputfolder = os.path.join(folder, "manual")
if not os.path.exists(distortedmodel):
os.makedirs(distortedmodel)
featureextract = "colmap feature_extractor SiftExtraction.max_image_size 6000 --database_path " + dbfile+ " --image_path " + inputimagefolder
exit_code = os.system(featureextract)
if exit_code != 0:
exit(exit_code)
featurematcher = "colmap exhaustive_matcher --database_path " + dbfile
exit_code = os.system(featurematcher)
if exit_code != 0:
exit(exit_code)
triandmap = "colmap point_triangulator --database_path "+ dbfile + " --image_path "+ inputimagefolder + " --output_path " + distortedmodel \
+ " --input_path " + manualinputfolder + " --Mapper.ba_global_function_tolerance=0.000001"
exit_code = os.system(triandmap)
if exit_code != 0:
exit(exit_code)
print(triandmap)
img_undist_cmd = "colmap" + " image_undistorter --image_path " + inputimagefolder + " --input_path " + distortedmodel + " --output_path " + folder \
+ " --output_type COLMAP " # --blank_pixels 1
exit_code = os.system(img_undist_cmd)
if exit_code != 0:
exit(exit_code)
print(img_undist_cmd)
removeinput = "rm -r " + inputimagefolder
exit_code = os.system(removeinput)
if exit_code != 0:
exit(exit_code)
files = os.listdir(folder + "/sparse")
os.makedirs(folder + "/sparse/0", exist_ok=True)
for file in files:
if file == '0':
continue
source_file = os.path.join(folder, "sparse", file)
destination_file = os.path.join(folder, "sparse", "0", file)
shutil.move(source_file, destination_file)
def getcolmapsingletechni(folder, offset):
folder = os.path.join(folder, "colmap_" + str(offset))
assert os.path.exists(folder)
dbfile = os.path.join(folder, "input.db")
inputimagefolder = os.path.join(folder, "input")
distortedmodel = os.path.join(folder, "distorted/sparse")
step2model = os.path.join(folder, "tmp")
if not os.path.exists(step2model):
os.makedirs(step2model)
manualinputfolder = os.path.join(folder, "manual")
if not os.path.exists(distortedmodel):
os.makedirs(distortedmodel)
featureextract = "colmap feature_extractor --database_path " + dbfile+ " --image_path " + inputimagefolder
exit_code = os.system(featureextract)
if exit_code != 0:
exit(exit_code)
featurematcher = "colmap exhaustive_matcher --database_path " + dbfile
exit_code = os.system(featurematcher)
if exit_code != 0:
exit(exit_code)
triandmap = "colmap point_triangulator --database_path "+ dbfile + " --image_path "+ inputimagefolder + " --output_path " + distortedmodel \
+ " --input_path " + manualinputfolder + " --Mapper.ba_global_function_tolerance=0.000001"
exit_code = os.system(triandmap)
if exit_code != 0:
exit(exit_code)
print(triandmap)
img_undist_cmd = "colmap" + " image_undistorter --image_path " + inputimagefolder + " --input_path " + distortedmodel + " --output_path " + folder \
+ " --output_type COLMAP " #
exit_code = os.system(img_undist_cmd)
if exit_code != 0:
exit(exit_code)
print(img_undist_cmd)
files = os.listdir(folder + "/sparse")
os.makedirs(folder + "/sparse/0", exist_ok=True)
for file in files:
if file == '0':
continue
source_file = os.path.join(folder, "sparse", file)
destination_file = os.path.join(folder, "sparse", "0", file)
shutil.move(source_file, destination_file)
return