-
Notifications
You must be signed in to change notification settings - Fork 1
/
run_alicevision.py
487 lines (381 loc) · 17.3 KB
/
run_alicevision.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
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#!/usr/bin/env python
import re
import commands
import sys
import os
import collections
StepInfo = collections.namedtuple('StepInfo', "name partition qos time nodes tasks command")
def SilentMkdir(theDir):
if not os.path.isdir(theDir):
os.makedirs(theDir)
def run_step(step):
with open("{name}.slurm".format(**step), "w") as SLURM:
SLURM.write("""#!/bin/bash
#SBATCH --job-name={name}
#SBATCH --output={name}.out
#SBATCH --error={name}.err
#SBATCH --partition={partition}
#SBATCH --qos={qos}
#SBATCH --time={time}
#SBATCH --nodes={nodes}
#SBATCH --ntasks-per-node={tasks}
{command}
""".format(**step))
def main():
jobId = 0
print("Prepping Scan, v2.")
print(sys.argv)
print(len(sys.argv))
if (len(sys.argv) != 5):
print("usage: python run_alicevision.py <baseDir> <imgDir> <binDir> <numImages>")
print("Must pass 5 arguments.")
sys.exit(0)
baseDir = sys.argv[1]
srcImageDir = sys.argv[2]
binDir = sys.argv[3]
numImages = int(sys.argv[4])
print("Base dir : %s" % baseDir)
print("Image dir : %s" % srcImageDir)
print("Bin dir : %s" % binDir)
print("Num images: %d" % numImages)
SilentMkdir(baseDir)
# Camera Initialization
SilentMkdir(os.path.join(baseDir, "00_CameraInit"))
# SilentMkdir(baseDir + "/00_CameraInit")
# provide the actual command to the slurm file
binName = os.path.join(binDir, "aliceVision_cameraInit")
dstDir = os.path.join(baseDir, "00_CameraInit/")
cmdLine = binName
cmdLine = cmdLine + " --defaultFieldOfView 45.0 --verboseLevel info --sensorDatabase \"\" --allowSingleView 1"
cmdLine = cmdLine + " --imageFolder \"" + srcImageDir + "\""
cmdLine = cmdLine + " --output \"" + os.path.join(dstDir, "cameraInit.sfm") + "\""
run_step({
"name": "camInit",
"partition": "batch",
"qos": "short",
"time": "00:05:00",
"nodes": "1",
"tasks": "1",
"command": cmdLine
})
# run the slurm file
print("sbatch camInit.slurm")
status, jobId = commands.getstatusoutput("sbatch camInit.slurm")
jobId = int(re.search(r'\d+', jobId).group())
#Feature Extraction
SilentMkdir(os.path.join(baseDir, "01_FeatureExtraction"))
srcSfm = os.path.join(baseDir, "00_CameraInit/cameraInit.sfm")
binName = os.path.join(binDir, "aliceVision_featureExtraction")
dstDir = os.path.join(baseDir, "01_FeatureExtraction/")
cmdLine = "module load CUDA/9.2.148.1 \n" + binName
cmdLine = cmdLine + " --describerTypes sift --forceCpuExtraction True --verboseLevel info --describerPreset normal"
cmdLine = cmdLine + " --rangeStart 0 --rangeSize " + str(numImages)
cmdLine = cmdLine + " --input \"" + srcSfm + "\""
cmdLine = cmdLine + " --output \"" + dstDir + "\""
run_step({
"name": "featExtract",
"partition": "gpu",
"qos": "short",
"time": "00:05:00",
"nodes": "1",
"tasks": "1",
"command": cmdLine
})
# run the slurm file
slurmCmd = ("sbatch --depend=afterany:%d featExtract.slurm" % jobId)
print(slurmCmd)
status, jobId = commands.getstatusoutput(slurmCmd)
jobId = int(re.search(r'\d+', jobId).group())
#Image Matching
SilentMkdir(os.path.join(baseDir, "02_ImageMatching"))
srcSfm = os.path.join(baseDir, "00_CameraInit/cameraInit.sfm")
srcFeatures = os.path.join(baseDir, "01_FeatureExtraction/")
dstMatches = os.path.join(baseDir, "02_ImageMatching/imageMatches.txt")
binName = os.path.join(binDir, "aliceVision_imageMatching")
cmdLine = binName
cmdLine = cmdLine + " --minNbImages 200 --tree "" --maxDescriptors 500 --verboseLevel info --weights "" --nbMatches 50"
cmdLine = cmdLine + " --input \"" + srcSfm + "\""
cmdLine = cmdLine + " --featuresFolder \"" + srcFeatures + "\""
cmdLine = cmdLine + " --output \"" + dstMatches + "\""
run_step({
"name": "imgMatch",
"partition": "batch",
"qos": "short",
"time": "00:05:00",
"nodes": "1",
"tasks": "1",
"command": cmdLine
})
# run the slurm file
slurmCmd = ("sbatch --depend=afterany:%d imgMatch.slurm" % jobId)
print(slurmCmd)
status, jobId = commands.getstatusoutput(slurmCmd)
jobId = int(re.search(r'\d+', jobId).group())
#Feature Matching
SilentMkdir(os.path.join(baseDir, "03_FeatureMatching"))
srcSfm = os.path.join(baseDir, "00_CameraInit/cameraInit.sfm")
srcFeatures = os.path.join(baseDir, "01_FeatureExtraction/")
srcImageMatches = os.path.join(baseDir, "02_ImageMatching/imageMatches.txt")
dstMatches = os.path.join(baseDir, "03_FeatureMatching")
binName = os.path.join(binDir, "aliceVision_featureMatching")
cmdLine = "module load CUDA/9.2.148.1 \n" + binName
cmdLine = cmdLine + " --verboseLevel info --describerTypes sift --maxMatches 0 --exportDebugFiles False --savePutativeMatches False --guidedMatching False"
cmdLine = cmdLine + " --geometricEstimator acransac --geometricFilterType fundamental_matrix --maxIteration 2048 --distanceRatio 0.8"
cmdLine = cmdLine + " --photometricMatchingMethod ANN_L2"
cmdLine = cmdLine + " --imagePairsList \"" + srcImageMatches + "\""
cmdLine = cmdLine + " --input \"" + srcSfm + "\""
cmdLine = cmdLine + " --featuresFolders \"" + srcFeatures + "\""
cmdLine = cmdLine + " --output \"" + dstMatches + "\""
run_step({
"name": "featMatch",
"partition": "gpu",
"qos": "short",
"time": "00:05:00",
"nodes": "1",
"tasks": "1",
"command": cmdLine
})
# run the slurm file
slurmCmd = ("sbatch --depend=afterany:%d featMatch.slurm" % jobId)
print(slurmCmd)
status, jobId = commands.getstatusoutput(slurmCmd)
print(jobId)
jobId = int(re.search(r'\d+', jobId).group())
#Structure from Motion
SilentMkdir(os.path.join(baseDir, "04_StructureFromMotion"))
srcSfm = os.path.join(baseDir, "00_CameraInit/cameraInit.sfm")
srcFeatures = os.path.join(baseDir, "01_FeatureExtraction/")
srcImageMatches = os.path.join(baseDir, "02_ImageMatching/imageMatches.txt")
srcMatches = os.path.join(baseDir, "03_FeatureMatching")
dstDir = os.path.join(baseDir, "04_StructureFromMotion")
binName = os.path.join(binDir, "/aliceVision_incrementalSfM")
cmdLine = "module load CUDA/9.2.148.1 \n" + binName
cmdLine = cmdLine + " --minAngleForLandmark 2.0 --minNumberOfObservationsForTriangulation 2 --maxAngleInitialPair 40.0 --maxNumberOfMatches 0 --localizerEstimator acransac --describerTypes sift --lockScenePreviouslyReconstructed False --localBAGraphDistance 1"
cmdLine = cmdLine + " --initialPairA \"\" --initialPairB \"\" --interFileExtension .ply --useLocalBA True"
cmdLine = cmdLine + " --minInputTrackLength 2 --useOnlyMatchesFromInputFolder False --verboseLevel info --minAngleForTriangulation 3.0 --maxReprojectionError 4.0 --minAngleInitialPair 5.0"
cmdLine = cmdLine + " --input \"" + srcSfm + "\""
cmdLine = cmdLine + " --featuresFolders \"" + srcFeatures + "\""
cmdLine = cmdLine + " --matchesFolders \"" + srcMatches + "\""
cmdLine = cmdLine + " --outputViewsAndPoses \"" + os.path.join(dstDir, "cameras.sfm") + "\""
cmdLine = cmdLine + " --extraInfoFolder \"" + dstDir + "\""
cmdLine = cmdLine + " --output \"" + os.path.join(dstDir, "sfm.abc") + "\""
run_step({
"name": "structMotion",
"partition": "gpu",
"qos": "short",
"time": "00:05:00",
"nodes": "1",
"tasks": "1",
"command": cmdLine
})
# run the slurm file
slurmCmd = ("sbatch --depend=afterany:%d structMotion.slurm" % jobId)
print(slurmCmd)
status, jobId = commands.getstatusoutput(slurmCmd)
jobId = int(re.search(r'\d+', jobId).group())
#Prepare Dense Scene
SilentMkdir(os.path.join(baseDir, "05_PrepareDenseScene"))
srcSfm = os.path.join(baseDir, "04_StructureFromMotion/sfm.abc")
dstDir = os.path.join(baseDir, "05_PrepareDenseScene")
binName = os.path.join(binDir, "aliceVision_prepareDenseScene")
cmdLine = binName
cmdLine = cmdLine + " --verboseLevel info"
cmdLine = cmdLine + " --input \"" + srcSfm + "\""
cmdLine = cmdLine + " --output \"" + dstDir + "\""
run_step({
"name": "prepDense",
"partition": "batch",
"qos": "short",
"time": "00:05:00",
"nodes": "1",
"tasks": "1",
"command": cmdLine
})
# run the slurm file
slurmCmd = ("sbatch --depend=afterany:%d prepDense.slurm" % jobId)
print(slurmCmd)
status, jobId = commands.getstatusoutput(slurmCmd)
jobId = int(re.search(r'\d+', jobId).group())
# Depth Mapping
SilentMkdir(baseDir + "/06_DepthMap")
groupSize = 3
numGroups = (numImages + (groupSize - 1)) / groupSize
srcIni = os.path.join(baseDir, "04_StructureFromMotion/sfm.abc")
binName = os.path.join(binDir, "aliceVision_depthMapEstimation")
imgDir = os.path.join(baseDir, "05_PrepareDenseScene")
dstDir = os.path.join(baseDir, "06_DepthMap")
cmdLine = "module load CUDA/9.2.148.1 \n" + binName
cmdLine = cmdLine + " --sgmGammaC 5.5 --sgmWSH 4 --refineGammaP 8.0 --refineSigma 15 --refineNSamplesHalf 150 --sgmMaxTCams 10 --refineWSH 3 --downscale 2 --refineMaxTCams 6 --verboseLevel info --refineGammaC 15.5 --sgmGammaP 8.0"
cmdLine = cmdLine + " --refineNiters 100 --refineNDepthsToRefine 31 --refineUseTcOrRcPixSize False"
cmdLine = cmdLine + " --input \"" + srcIni + "\""
cmdLine = cmdLine + " --imagesFolder \"" + imgDir + "\""
cmdLine = cmdLine + " --output \"" + dstDir + "\""
groupStart = 0
groupSize = min(groupSize, numImages - groupStart)
print("DepthMap Group %d/%d: %d, %d" % (0, numGroups, groupStart, groupSize))
cmd = cmdLine + (" --rangeStart %d --rangeSize %d" % (groupStart, groupSize))
run_step({
"name": ("depthMap%d" % 0),
"partition": "gpu",
"qos": "short",
"time": "00:15:00",
"nodes": "1",
"tasks": "1",
"command": cmd
})
# run the slurm file
slurmCmd = ("sbatch --depend=afterany:%d depthMap%d.slurm" % (jobId, 0))
print(slurmCmd)
#list for the job ids
jobIdList = list()
status, jobId = commands.getstatusoutput(slurmCmd)
jobId = int(re.search(r'\d+', jobId).group())
jobIdList.append(jobId)
# The rest of the rounds of depth mapping
for groupIter in range(1, numGroups):
groupStart = groupSize * groupIter
groupSize = min(groupSize, numImages - groupStart)
print("DepthMap Group %d/%d: %d, %d" % (groupIter, numGroups, groupStart, groupSize))
cmd = cmdLine + (" --rangeStart %d --rangeSize %d" % (groupStart, groupSize))
run_step({
"name": ("depthMap%d" % groupIter),
"partition": "gpu",
"qos": "short",
"time": "00:15:00",
"nodes": "1",
"tasks": "1",
"command": cmd
})
# run the slurm file
slurmCmd = ("sbatch --depend=after:%d depthMap%d.slurm" % (jobId, groupIter))
print(slurmCmd)
status, jobId = commands.getstatusoutput(slurmCmd)
jobId = int(re.search(r'\d+', jobId).group())
jobIdList.append(jobId)
#Depth Map Filtering
SilentMkdir(os.path.join(baseDir, "07_DepthMapFilter"))
binName = os.path.join(binDir, "aliceVision_depthMapFiltering")
dstDir = os.path.join(baseDir, "07_DepthMapFilter")
srcIni = os.path.join(baseDir, "04_StructureFromMotion/sfm.abc")
srcDepthDir = os.path.join(baseDir, "06_DepthMap")
cmdLine = binName
cmdLine = cmdLine + " --minNumOfConsistentCamsWithLowSimilarity 4"
cmdLine = cmdLine + " --minNumOfConsistentCams 3 --verboseLevel info --pixSizeBall 0"
cmdLine = cmdLine + " --pixSizeBallWithLowSimilarity 0 --nNearestCams 10"
cmdLine = cmdLine + " --input \"" + srcIni + "\""
cmdLine = cmdLine + " --output \"" + dstDir + "\""
cmdLine = cmdLine + " --depthMapsFolder \"" + srcDepthDir + "\""
run_step({
"name": "depthMapFilter",
"partition": "batch",
"qos": "short",
"time": "00:15:00",
"nodes": "1",
"tasks": "1",
"command": cmd
})
# run the slurm file
slurmCmd = "sbatch --depend=afterany"
# for loop over all of the jobs in the job id list
for jid in jobIdList:
slurmCmd = slurmCmd + (":%d" % jid)
slurmCmd = slurmCmd + " depthMapFilter.slurm"
print(slurmCmd)
status, jobId = commands.getstatusoutput(slurmCmd)
jobId = int(re.search(r'\d+', jobId).group())
#Meshing
SilentMkdir(os.path.join(baseDir, "09_Meshing"))
binName = os.path.join(binDir, "aliceVision_meshing")
srcIni = os.path.join(baseDir, "04_StructureFromMotion/sfm.abc")
srcDepthFilterDir = os.path.join(baseDir, "07_DepthMapFilter")
srcDepthMapDir = os.path.join(baseDir, "06_DepthMap")
dstDir = os.path.join(baseDir, "08_Meshing")
cmdLine = binName
cmdLine = cmdLine + " --simGaussianSizeInit 10.0 --maxInputPoints 50000000 --repartition multiResolution"
cmdLine = cmdLine + " --simGaussianSize 10.0 --simFactor 15.0 --voteMarginFactor 4.0 --contributeMarginFactor 2.0 --minStep 2 --pixSizeMarginFinalCoef 4.0 --maxPoints 5000000 --maxPointsPerVoxel 1000000 --angleFactor 15.0 --partitioning singleBlock"
cmdLine = cmdLine + " --minAngleThreshold 1.0 --pixSizeMarginInitCoef 2.0 --refineFuse True --verboseLevel info"
cmdLine = cmdLine + " --input \"" + srcIni + "\""
cmdLine = cmdLine + " --depthMapsFilterFolder \"" + srcDepthFilterDir + "\""
cmdLine = cmdLine + " --depthMapsFolder \"" + srcDepthMapDir + "\""
cmdLine = cmdLine + " --output \"" + os.path.join(dstDir, "mesh.obj") + "\""
run_step({
"name": "mesh",
"partition": "batch",
"qos": "short",
"time": "00:10:00",
"nodes": "1",
"tasks": "1",
"command": cmdLine
})
# run the slurm file
slurmCmd = ("sbatch --depend=afterany:%d mesh.slurm" % jobId)
print(slurmCmd)
status, jobId = commands.getstatusoutput(slurmCmd)
jobId = int(re.search(r'\d+', jobId).group())
# Mesh Filtering
SilentMkdir(os.path.join(baseDir, "09_MeshFiltering"))
# create a new slurm file
slurm = open("meshFilt.slurm", "w+")
# provide sbatch info to the slurm file
slurm.write("#!/bin/bash \n")
slurm.write("#SBATCH --job-name=meshFilt \n")
slurm.write("#SBATCH --output=meshFilt.out \n")
slurm.write("#SBATCH --error=meshFilt.err \n")
slurm.write("#SBATCH --partition=batch \n")
slurm.write("#SBATCH --qos=normal \n")
slurm.write("#SBATCH --time=00:05:00 \n")
slurm.write("#SBATCH --nodes=1 \n")
slurm.write("#SBATCH --ntasks-per-node=1 \n \n")
binName = os.path.join(binDir, "aliceVision_meshFiltering")
srcMesh = os.path.join(baseDir, "08_Meshing/mesh.obj")
dstMesh = os.path.join(baseDir, "09_MeshFiltering/mesh.obj")
cmdLine = binName
cmdLine = cmdLine + " --verboseLevel info --removeLargeTrianglesFactor 60.0 --iterations 5 --keepLargestMeshOnly True"
cmdLine = cmdLine + " --lambda 1.0"
cmdLine = cmdLine + " --input \"" + srcMesh + "\""
cmdLine = cmdLine + " --output \"" + dstMesh + "\""
run_step({
"name": "meshFilt",
"partition": "batch",
"qos": "short",
"time": "00:05:00",
"nodes": "1",
"tasks": "1",
"command": cmdLine
})
# run the slurm file
slurmCmd = ("sbatch --depend=afterany:%d meshFilt.slurm" % jobId)
print(slurmCmd)
status, jobId = commands.getstatusoutput(slurmCmd)
jobId = int(re.search(r'\d+', jobId).group())
# Texturing
SilentMkdir(os.path.join(baseDir, "10_Texturing"))
binName = os.path.join(binDir, "aliceVision_texturing")
srcMesh = os.path.join(baseDir, "09_MeshFiltering/mesh.obj")
srcRecon = os.path.join(baseDir, "08_Meshing/denseReconstruction.bin")
srcIni = os.path.join(baseDir, "04_StructureFromMotion/sfm.abc")
dstDir = os.path.join(baseDir, "10_Texturing")
imgDir = os.path.join(baseDir, "05_PrepareDenseScene")
cmdLine = binName
cmdLine = cmdLine + " --textureSide 8192"
cmdLine = cmdLine + " --downscale 2 --verboseLevel info --padding 15"
cmdLine = cmdLine + " --unwrapMethod Basic --outputTextureFileType png --flipNormals False --fillHoles False"
cmdLine = cmdLine + " --inputDenseReconstruction \"" + srcRecon + "\""
cmdLine = cmdLine + " --inputMesh \"" + srcMesh + "\""
cmdLine = cmdLine + " --input \"" + srcIni + "\""
cmdLine = cmdLine + " --imagesFolder \"" + imgDir + "\""
cmdLine = cmdLine + " --output \"" + dstDir + "\""
run_step({
"name": "text",
"partition": "batch",
"qos": "short",
"time": "00:05:00",
"nodes": "1",
"tasks": "1",
"command": cmdLine
})
# run the slurm file
slurmCmd = ("sbatch --depend=afterany:%d text.slurm" % jobId)
print(slurmCmd)
status, jobId = commands.getstatusoutput(slurmCmd)
main()