#!/usr/bin/env bash set -euo pipefail # Run COLMAP's panorama_sfm.py on frames prepared as: # DATA_ROOT////images/%04d.jpg # # Required: # DATA_ROOT Root containing train/test scene folders. # PANO_SCRIPT Path to COLMAP's python/examples/panorama_sfm.py. # # Optional: # OUT_ROOT Output root. Defaults to DATA_ROOT. # GPU_IDS Space-separated GPU ids. Defaults to "0". # MATCHER COLMAP matcher. Defaults to "sequential". # PANO_RENDER_TYPE Defaults to "non-overlapping". # SPLIT_FILTER train | test | all. Defaults to all. # MIN_IMAGES Minimum panoramic frames per sequence. Defaults to 2. DATA_ROOT="${DATA_ROOT:-}" OUT_ROOT="${OUT_ROOT:-$DATA_ROOT}" PANO_SCRIPT="${PANO_SCRIPT:-}" GPU_IDS="${GPU_IDS:-0}" MATCHER="${MATCHER:-sequential}" PANO_RENDER_TYPE="${PANO_RENDER_TYPE:-non-overlapping}" SPLIT_FILTER="${SPLIT_FILTER:-all}" MIN_IMAGES="${MIN_IMAGES:-2}" MASTER_LOG="$OUT_ROOT/panorama_sfm.log" if [[ -z "$DATA_ROOT" ]]; then echo "[ERROR] DATA_ROOT is required" exit 1 fi if [[ -z "$PANO_SCRIPT" || ! -f "$PANO_SCRIPT" ]]; then echo "[ERROR] PANO_SCRIPT must point to COLMAP's panorama_sfm.py" exit 1 fi if [[ "$SPLIT_FILTER" != "train" && "$SPLIT_FILTER" != "test" && "$SPLIT_FILTER" != "all" ]]; then echo "[ERROR] SPLIT_FILTER must be train, test, or all" exit 1 fi mkdir -p "$OUT_ROOT" IFS=' ' read -r -a GPU_ID_LIST <<< "$GPU_IDS" declare -A GPU_PIDS has_complete_sparse() { local out_dir="$1" [[ -f "$out_dir/sparse/0/cameras.bin" && -f "$out_dir/sparse/0/images.bin" && -f "$out_dir/sparse/0/points3D.bin" ]] } wait_for_free_gpu() { while true; do for gpu in "${GPU_ID_LIST[@]}"; do local pid="${GPU_PIDS[$gpu]-}" if [[ -z "$pid" ]] || ! kill -0 "$pid" 2>/dev/null; then GPU_PIDS[$gpu]="" echo "$gpu" return 0 fi done sleep 3 done } start_colmap_job() { local gpu="$1" local img_dir="$2" local out_dir="$3" local desc="$4" mkdir -p "$out_dir" echo "[INFO] COLMAP start GPU=$gpu $desc" | tee -a "$MASTER_LOG" ( set +e export CUDA_VISIBLE_DEVICES="$gpu" python "$PANO_SCRIPT" \ --input_image_path "$img_dir" \ --output_path "$out_dir" \ --matcher "$MATCHER" \ --pano_render_type "$PANO_RENDER_TYPE" \ > "$out_dir/run.log" 2>&1 status=$? if [[ "$status" -ne 0 ]]; then echo "[WARN] COLMAP failed status=$status $out_dir" >> "$MASTER_LOG" else echo "[INFO] COLMAP done $out_dir" >> "$MASTER_LOG" fi ) & GPU_PIDS["$gpu"]=$! } LIST_TSV="$OUT_ROOT/panorama_sfm_list.tsv" python - <<'PY' "$DATA_ROOT" "$LIST_TSV" "$SPLIT_FILTER" import os import sys data_root, out_path, split_filter = sys.argv[1:4] splits = ("train", "test") if split_filter == "all" else (split_filter,) rows = [] for split in splits: split_dir = os.path.join(data_root, split) if not os.path.isdir(split_dir): continue for scene in sorted(os.listdir(split_dir)): scene_dir = os.path.join(split_dir, scene) if not os.path.isdir(scene_dir): continue for clip_id in sorted(os.listdir(scene_dir)): clip_dir = os.path.join(scene_dir, clip_id) img_dir = os.path.join(clip_dir, "images") if os.path.isdir(img_dir): rows.append((split, scene, clip_id, img_dir)) os.makedirs(os.path.dirname(out_path), exist_ok=True) with open(out_path, "w", encoding="utf-8") as f: for row in rows: f.write("\t".join(row) + "\n") PY echo "[INFO] DATA_ROOT=$DATA_ROOT" | tee -a "$MASTER_LOG" echo "[INFO] OUT_ROOT=$OUT_ROOT" | tee -a "$MASTER_LOG" echo "[INFO] PANO_SCRIPT=$PANO_SCRIPT" | tee -a "$MASTER_LOG" echo "[INFO] GPU_IDS=$GPU_IDS MATCHER=$MATCHER PANO_RENDER_TYPE=$PANO_RENDER_TYPE SPLIT_FILTER=$SPLIT_FILTER" | tee -a "$MASTER_LOG" exec 3< "$LIST_TSV" while IFS=$'\t' read -r split scene clip_id img_dir <&3; do [[ -z "$split" ]] && continue img_count=$(find "$img_dir" -maxdepth 1 -type f \( -iname '*.jpg' -o -iname '*.jpeg' -o -iname '*.png' \) | wc -l | tr -d ' ') if [[ "$img_count" -lt "$MIN_IMAGES" ]]; then echo "[WARN] too few images count=$img_count $img_dir" | tee -a "$MASTER_LOG" continue fi out_dir="$OUT_ROOT/$split/$scene/$clip_id/colmap_nonoverlap" if has_complete_sparse "$out_dir"; then echo "[INFO] skip existing $out_dir" | tee -a "$MASTER_LOG" continue fi gpu=$(wait_for_free_gpu) start_colmap_job "$gpu" "$img_dir" "$out_dir" "$split/$scene/$clip_id images=$img_count" done exec 3<&- for gpu in "${GPU_ID_LIST[@]}"; do pid="${GPU_PIDS[$gpu]-}" if [[ -n "$pid" ]]; then wait "$pid" || true fi done echo "[INFO] done $(date '+%F %T')" | tee -a "$MASTER_LOG"