{ "metadata": { "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.12.12" }, "accelerator": "GPU", "colab": { "gpuType": "T4", "provenance": [], "machine_shape": "hm" }, "kaggle": { "accelerator": "nvidiaTeslaT4", "dataSources": [ { "sourceId": 14628798, "sourceType": "datasetVersion", "datasetId": 1429416 } ], "dockerImageVersionId": 31260, "isInternetEnabled": true, "language": "python", "sourceType": "notebook", "isGpuEnabled": true }, "papermill": { "default_parameters": {}, "duration": 967.270978, "end_time": "2026-01-20T01:22:34.649213", "environment_variables": {}, "exception": null, "input_path": "__notebook__.ipynb", "output_path": "__notebook__.ipynb", "parameters": {}, "start_time": "2026-01-20T01:06:27.378235", "version": "2.6.0" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "71136d03f7394b2093da996ec9377426": { "model_module": "@jupyter-widgets/controls", "model_name": "HBoxModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HBoxModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HBoxView", "box_style": "", "children": [ "IPY_MODEL_ec440aa735ec42a387d5686525d78e12", "IPY_MODEL_cfebe5ba39604457a0d1a76ff8ddb83b", "IPY_MODEL_3389884d59d04f169fbc7df68173ed88" ], "layout": "IPY_MODEL_6dd48df3252c4fd2b25bd339693f6796" } }, "ec440aa735ec42a387d5686525d78e12": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_cca860c624684a7cbb96d7012948945d", "placeholder": "​", "style": "IPY_MODEL_f5122873126e439f899c98a48a14b9da", "value": "Loading weights: 100%" } }, "cfebe5ba39604457a0d1a76ff8ddb83b": { "model_module": "@jupyter-widgets/controls", "model_name": "FloatProgressModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "FloatProgressModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "ProgressView", "bar_style": "success", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_2a5789d0fe934d73b51392c9ac2086ed", "max": 223, "min": 0, "orientation": "horizontal", "style": "IPY_MODEL_d13f592933eb4c6db622a7d350d3f6ea", "value": 223 } }, "3389884d59d04f169fbc7df68173ed88": { "model_module": "@jupyter-widgets/controls", "model_name": "HTMLModel", "model_module_version": "1.5.0", "state": { "_dom_classes": [], "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "HTMLModel", "_view_count": null, "_view_module": "@jupyter-widgets/controls", "_view_module_version": "1.5.0", "_view_name": "HTMLView", "description": "", "description_tooltip": null, "layout": "IPY_MODEL_4d98b95eaa0349978061955b417a534a", "placeholder": "​", "style": "IPY_MODEL_9d9db218417a40f9b215eecd7ce59c09", "value": " 223/223 [00:00<00:00, 1135.77it/s, Materializing param=layernorm.weight]" } }, "6dd48df3252c4fd2b25bd339693f6796": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "cca860c624684a7cbb96d7012948945d": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "f5122873126e439f899c98a48a14b9da": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } }, "2a5789d0fe934d73b51392c9ac2086ed": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "d13f592933eb4c6db622a7d350d3f6ea": { "model_module": "@jupyter-widgets/controls", "model_name": "ProgressStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "ProgressStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "bar_color": null, "description_width": "" } }, "4d98b95eaa0349978061955b417a534a": { "model_module": "@jupyter-widgets/base", "model_name": "LayoutModel", "model_module_version": "1.2.0", "state": { "_model_module": "@jupyter-widgets/base", "_model_module_version": "1.2.0", "_model_name": "LayoutModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "LayoutView", "align_content": null, "align_items": null, "align_self": null, "border": null, "bottom": null, "display": null, "flex": null, "flex_flow": null, "grid_area": null, "grid_auto_columns": null, "grid_auto_flow": null, "grid_auto_rows": null, "grid_column": null, "grid_gap": null, "grid_row": null, "grid_template_areas": null, "grid_template_columns": null, "grid_template_rows": null, "height": null, "justify_content": null, "justify_items": null, "left": null, "margin": null, "max_height": null, "max_width": null, "min_height": null, "min_width": null, "object_fit": null, "object_position": null, "order": null, "overflow": null, "overflow_x": null, "overflow_y": null, "padding": null, "right": null, "top": null, "visibility": null, "width": null } }, "9d9db218417a40f9b215eecd7ce59c09": { "model_module": "@jupyter-widgets/controls", "model_name": "DescriptionStyleModel", "model_module_version": "1.5.0", "state": { "_model_module": "@jupyter-widgets/controls", "_model_module_version": "1.5.0", "_model_name": "DescriptionStyleModel", "_view_count": null, "_view_module": "@jupyter-widgets/base", "_view_module_version": "1.2.0", "_view_name": "StyleView", "description_width": "" } } } } }, "nbformat_minor": 0, "nbformat": 4, "cells": [ { "source": [ "\n" ], "metadata": { "id": "TQHWets3GtP4" }, "cell_type": "code", "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [ "# **dino-mast3r-gs-kg**\n", "\n" ], "metadata": { "id": "qDQLX3PArmh8", "papermill": { "duration": 0.003504, "end_time": "2026-01-20T01:06:31.022336", "exception": false, "start_time": "2026-01-20T01:06:31.018832", "status": "completed" }, "tags": [] } }, { "cell_type": "code", "source": [ "from google.colab import drive\n", "drive.mount('/content/drive')" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8OR_RTeBZC8v", "outputId": "b1b7073b-4def-47cc-fe5c-cff6d42be441" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Drive already mounted at /content/drive; to attempt to forcibly remount, call drive.mount(\"/content/drive\", force_remount=True).\n" ] } ] }, { "cell_type": "code", "source": [ "# MASt3R-based Gaussian Splatting Pipeline\n", "# Preserves: DINO pair selection + Biplet-Square Normalization\n", "# Replaces: ALIKED/LightGlue/COLMAP with MASt3R\n", "\n", "import os\n", "import sys\n", "import gc\n", "import h5py\n", "import numpy as np\n", "import torch\n", "import torch.nn.functional as F\n", "from tqdm import tqdm\n", "from pathlib import Path\n", "import subprocess\n", "from PIL import Image, ImageFilter\n", "import struct\n", "\n", "# Transformers for DINO\n", "from transformers import AutoImageProcessor, AutoModel\n", "\n", "# ============================================================================\n", "# Configuration\n", "# ============================================================================\n", "class Config:\n", " # Feature extraction\n", " N_KEYPOINTS = 8192\n", " IMAGE_SIZE = 1024\n", "\n", " # Pair selection - CRITICAL for memory\n", " GLOBAL_TOPK = 20 # Reduced from 50 - each image pairs with top 20\n", " MIN_MATCHES = 10\n", " RATIO_THR = 1.2\n", "\n", " # Paths\n", " DINO_MODEL = \"facebook/dinov2-base\"\n", "\n", " # MASt3R - Reduced size for memory\n", " MAST3R_MODEL = \"/content/mast3r/checkpoints/MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric.pth\"\n", " MAST3R_IMAGE_SIZE = 224 # Small size to save memory\n", "\n", " # Device\n", " DEVICE = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n", "\n", "# ============================================================================\n", "# Memory Management Utilities\n", "# ============================================================================\n", "\n", "def clear_memory():\n", " \"\"\"Aggressively clear GPU and CPU memory\"\"\"\n", " gc.collect()\n", " if torch.cuda.is_available():\n", " torch.cuda.empty_cache()\n", " torch.cuda.synchronize()\n", "\n", "def get_memory_info():\n", " \"\"\"Get current memory usage\"\"\"\n", " if torch.cuda.is_available():\n", " allocated = torch.cuda.memory_allocated() / 1024**3\n", " reserved = torch.cuda.memory_reserved() / 1024**3\n", " print(f\"GPU Memory - Allocated: {allocated:.2f}GB, Reserved: {reserved:.2f}GB\")\n", "\n", " import psutil\n", " cpu_mem = psutil.virtual_memory().percent\n", " print(f\"CPU Memory Usage: {cpu_mem:.1f}%\")\n", "\n", "# ============================================================================\n", "# Environment Setup\n", "# ============================================================================\n", "\n", "def run_cmd(cmd, check=True, capture=False):\n", " \"\"\"Run command with better error handling\"\"\"\n", " print(f\"Running: {' '.join(cmd)}\")\n", " result = subprocess.run(\n", " cmd,\n", " capture_output=capture,\n", " text=True,\n", " check=False\n", " )\n", " if check and result.returncode != 0:\n", " print(f\"❌ Command failed with code {result.returncode}\")\n", " if capture:\n", " print(f\"STDOUT: {result.stdout}\")\n", " print(f\"STDERR: {result.stderr}\")\n", " return result\n", "\n", "\n", "def setup_base_environment():\n", " \"\"\"Setup base Python environment\"\"\"\n", " print(\"\\n=== Setting up Base Environment ===\")\n", "\n", " # NumPy fix for Python 3.12\n", " print(\"\\n📦 Fixing NumPy...\")\n", " run_cmd([sys.executable, \"-m\", \"pip\", \"uninstall\", \"-y\", \"numpy\"])\n", " run_cmd([sys.executable, \"-m\", \"pip\", \"install\", \"numpy==1.26.4\"])\n", "\n", " # PyTorch\n", " print(\"\\n📦 Installing PyTorch...\")\n", " run_cmd([\n", " sys.executable, \"-m\", \"pip\", \"install\",\n", " \"torch\", \"torchvision\", \"torchaudio\"\n", " ])\n", "\n", " # Core utilities\n", " print(\"\\n📦 Installing core utilities...\")\n", " run_cmd([\n", " sys.executable, \"-m\", \"pip\", \"install\",\n", " \"opencv-python\",\n", " \"pillow\",\n", " \"imageio\",\n", " \"imageio-ffmpeg\",\n", " \"plyfile\",\n", " \"tqdm\",\n", " \"tensorboard\",\n", " \"scipy\", # for rotation conversions and image resizing\n", " \"psutil\" # for memory monitoring\n", " ])\n", "\n", " # Transformers for DINO\n", " print(\"\\n📦 Installing transformers...\")\n", " run_cmd([\n", " sys.executable, \"-m\", \"pip\", \"install\",\n", " \"transformers>=4.45.0\"\n", " ])\n", "\n", " # pycolmap for COLMAP format\n", " print(\"\\n📦 Installing pycolmap...\")\n", " run_cmd([sys.executable, \"-m\", \"pip\", \"install\", \"pycolmap\"])\n", "\n", " print(\"✓ Base environment setup complete!\")\n", "\n", "\n", "def setup_mast3r():\n", " \"\"\"Install and setup MASt3R\"\"\"\n", " print(\"\\n=== Setting up MASt3R ===\")\n", "\n", " os.chdir('/content')\n", "\n", " # Remove existing installation\n", " if os.path.exists('mast3r'):\n", " print(\"Removing existing MASt3R installation...\")\n", " os.system('rm -rf mast3r')\n", "\n", " # Clone repository\n", " print(\"Cloning MASt3R repository...\")\n", " os.system('git clone --recursive https://github.com/naver/mast3r')\n", " os.chdir('/content/mast3r')\n", "\n", " # Check dust3r directory\n", " print(\"Checking dust3r structure...\")\n", " os.system('ls -la dust3r/')\n", "\n", " # Install dust3r\n", " print(\"Installing dust3r...\")\n", " os.system('cd dust3r && python -m pip install -e .')\n", "\n", " # Install croco\n", " print(\"Installing croco...\")\n", " os.system('cd dust3r/croco && python -m pip install -e .')\n", "\n", " # Install requirements\n", " print(\"Installing MASt3R requirements...\")\n", " os.system('pip install -r requirements.txt')\n", "\n", " # Download model weights\n", " print(\"Downloading model weights...\")\n", " os.system('mkdir -p checkpoints')\n", " os.system('wget -P checkpoints/ https://download.europe.naverlabs.com/ComputerVision/MASt3R/MASt3R_ViTLarge_BaseDecoder_512_catmlpdpt_metric.pth')\n", "\n", " # Install additional dependencies\n", " print(\"Installing additional dependencies...\")\n", " os.system('pip install trimesh matplotlib roma')\n", "\n", " # Add to path\n", " sys.path.insert(0, '/content/mast3r')\n", " sys.path.insert(0, '/content/mast3r/dust3r')\n", "\n", " # Verification\n", " print(\"\\n🔍 Verifying MASt3R installation...\")\n", " try:\n", " from mast3r.model import AsymmetricMASt3R\n", " print(\" ✓ MASt3R import: OK\")\n", " except Exception as e:\n", " print(f\" ❌ MASt3R import failed: {e}\")\n", " raise\n", "\n", " print(\"✓ MASt3R setup complete!\")\n", "\n", "def setup_gaussian_splatting():\n", " \"\"\"Setup Gaussian Splatting\"\"\"\n", " print(\"\\n=== Setting up Gaussian Splatting ===\")\n", "\n", " os.chdir('/content')\n", "\n", " WORK_DIR = \"gaussian-splatting\"\n", "\n", " if not os.path.exists(WORK_DIR):\n", " print(\"Cloning Gaussian Splatting repository...\")\n", " run_cmd([\n", " \"git\", \"clone\", \"--recursive\",\n", " \"https://github.com/graphdeco-inria/gaussian-splatting.git\",\n", " WORK_DIR\n", " ])\n", " else:\n", " print(\"✓ Repository already exists\")\n", "\n", " os.chdir(WORK_DIR)\n", "\n", " # Install requirements\n", " print(\"Installing Gaussian Splatting requirements...\")\n", " run_cmd([sys.executable, \"-m\", \"pip\", \"install\", \"-r\", \"requirements.txt\"])\n", "\n", " # Build submodules\n", " print(\"\\n📦 Building Gaussian Splatting submodules...\")\n", "\n", " submodules = {\n", " \"diff-gaussian-rasterization\":\n", " \"https://github.com/graphdeco-inria/diff-gaussian-rasterization.git\",\n", " \"simple-knn\":\n", " \"https://github.com/camenduru/simple-knn.git\"\n", " }\n", "\n", " for name, repo in submodules.items():\n", " print(f\"\\n📦 Installing {name}...\")\n", " path = os.path.join(\"submodules\", name)\n", " if not os.path.exists(path):\n", " run_cmd([\"git\", \"clone\", repo, path])\n", " run_cmd([sys.executable, \"-m\", \"pip\", \"install\", path])\n", "\n", " print(\"✓ Gaussian Splatting setup complete!\")\n" ], "metadata": { "execution": { "iopub.execute_input": "2026-01-20T01:06:37.366201Z", "iopub.status.busy": "2026-01-20T01:06:37.365963Z", "iopub.status.idle": "2026-01-20T01:07:23.639941Z", "shell.execute_reply": "2026-01-20T01:07:23.639152Z" }, "papermill": { "duration": 46.280727, "end_time": "2026-01-20T01:07:23.641872", "exception": false, "start_time": "2026-01-20T01:06:37.361145", "status": "completed" }, "tags": [], "id": "z6syEvB9GtP7" }, "outputs": [], "execution_count": null }, { "cell_type": "code", "source": [ "\n", "setup_base_environment()\n", "clear_memory()\n", "\n", "setup_mast3r()\n", "clear_memory()\n", "\n", "setup_gaussian_splatting()\n", "clear_memory()" ], "metadata": { "trusted": true, "_kg_hide-output": true, "id": "_QAS8as2GtP8", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "5a5a1f0a-6898-4e92-c421-c8af8c8944da" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "=== Setting up Base Environment ===\n", "\n", "📦 Fixing NumPy...\n", "Running: /usr/bin/python3 -m pip uninstall -y numpy\n", "Running: /usr/bin/python3 -m pip install numpy==1.26.4\n", "\n", "📦 Installing PyTorch...\n", "Running: /usr/bin/python3 -m pip install torch torchvision torchaudio\n", "\n", "📦 Installing core utilities...\n", "Running: /usr/bin/python3 -m pip install opencv-python pillow imageio imageio-ffmpeg plyfile tqdm tensorboard scipy psutil\n", "\n", "📦 Installing transformers...\n", "Running: /usr/bin/python3 -m pip install transformers>=4.45.0\n", "\n", "📦 Installing pycolmap...\n", "Running: /usr/bin/python3 -m pip install pycolmap\n", "✓ Base environment setup complete!\n", "\n", "=== Setting up MASt3R ===\n", "Removing existing MASt3R installation...\n", "Cloning MASt3R repository...\n", "Checking dust3r structure...\n", "Installing dust3r...\n", "Installing croco...\n", "Installing MASt3R requirements...\n", "Downloading model weights...\n", "Installing additional dependencies...\n", "\n", "🔍 Verifying MASt3R installation...\n", " ✓ MASt3R import: OK\n", "✓ MASt3R setup complete!\n", "\n", "=== Setting up Gaussian Splatting ===\n", "✓ Repository already exists\n", "Installing Gaussian Splatting requirements...\n", "Running: /usr/bin/python3 -m pip install -r requirements.txt\n", "❌ Command failed with code 1\n", "\n", "📦 Building Gaussian Splatting submodules...\n", "\n", "📦 Installing diff-gaussian-rasterization...\n", "Running: /usr/bin/python3 -m pip install submodules/diff-gaussian-rasterization\n", "\n", "📦 Installing simple-knn...\n", "Running: /usr/bin/python3 -m pip install submodules/simple-knn\n", "✓ Gaussian Splatting setup complete!\n" ] } ], "execution_count": null }, { "cell_type": "code", "source": [], "metadata": { "trusted": true, "id": "H6soprunGtP8" }, "outputs": [], "execution_count": null }, { "cell_type": "code", "source": [ "# ============================================================================\n", "# Step 0: Biplet-Square Normalization (PRESERVED FROM ORIGINAL)\n", "# ============================================================================\n", "\n", "def normalize_image_sizes_biplet(input_dir, output_dir=None, size=1024):\n", " \"\"\"\n", " Generates two square crops (Left & Right or Top & Bottom)\n", " from each image in a directory.\n", " \"\"\"\n", " if output_dir is None:\n", " output_dir = 'output/images_biplet'\n", "\n", " os.makedirs(output_dir, exist_ok=True)\n", "\n", " print(f\"Generating 2 cropped squares (Left/Right or Top/Bottom) for each image...\")\n", " print()\n", "\n", " converted_count = 0\n", " size_stats = {}\n", "\n", " for img_file in sorted(os.listdir(input_dir)):\n", " if not img_file.lower().endswith(('.jpg', '.jpeg', '.png')):\n", " continue\n", "\n", " input_path = os.path.join(input_dir, img_file)\n", "\n", " try:\n", " img = Image.open(input_path)\n", " original_size = img.size\n", "\n", " size_key = f\"{original_size[0]}x{original_size[1]}\"\n", " size_stats[size_key] = size_stats.get(size_key, 0) + 1\n", "\n", " # Generate 2 crops\n", " crops = generate_two_crops(img, size)\n", "\n", " base_name, ext = os.path.splitext(img_file)\n", " for mode, cropped_img in crops.items():\n", " output_path = os.path.join(output_dir, f\"{base_name}_{mode}{ext}\")\n", " cropped_img.save(output_path, quality=95)\n", "\n", " converted_count += 1\n", " print(f\" ✓ {img_file}: {original_size} → 2 square images generated\")\n", "\n", " except Exception as e:\n", " print(f\" ✗ Error processing {img_file}: {e}\")\n", "\n", " print(f\"\\nProcessing complete: {converted_count} source images processed\")\n", " print(f\"Original size distribution: {size_stats}\")\n", " return converted_count\n", "\n", "\n", "def generate_two_crops(img, size):\n", " \"\"\"\n", " Crops the image into a square and returns 2 variations\n", " (Left/Right for landscape, Top/Bottom for portrait).\n", " \"\"\"\n", " width, height = img.size\n", " crop_size = min(width, height)\n", " crops = {}\n", "\n", " if width > height:\n", " # Landscape → Left & Right\n", " positions = {\n", " 'left': 0,\n", " 'right': width - crop_size\n", " }\n", " for mode, x_offset in positions.items():\n", " box = (x_offset, 0, x_offset + crop_size, crop_size)\n", " crops[mode] = img.crop(box).resize(\n", " (size, size),\n", " Image.Resampling.LANCZOS\n", " )\n", "\n", " else:\n", " # Portrait or Square → Top & Bottom\n", " positions = {\n", " 'top': 0,\n", " 'bottom': height - crop_size\n", " }\n", " for mode, y_offset in positions.items():\n", " box = (0, y_offset, crop_size, y_offset + crop_size)\n", " crops[mode] = img.crop(box).resize(\n", " (size, size),\n", " Image.Resampling.LANCZOS\n", " )\n", "\n", " return crops\n", "\n", "# ============================================================================\n", "# Step 1: DINO-based Pair Selection (PRESERVED FROM ORIGINAL)\n", "# ============================================================================\n", "\n", "def load_torch_image(fname, device):\n", " \"\"\"Load image as torch tensor\"\"\"\n", " import torchvision.transforms as T\n", "\n", " img = Image.open(fname).convert('RGB')\n", " transform = T.Compose([\n", " T.ToTensor(),\n", " ])\n", " return transform(img).unsqueeze(0).to(device)\n", "\n", "def extract_dino_global(image_paths, model_path, device):\n", " \"\"\"Extract DINO global descriptors with memory management\"\"\"\n", " print(\"\\n=== Extracting DINO Global Features ===\")\n", " print(\"Initial memory state:\")\n", " get_memory_info()\n", "\n", " processor = AutoImageProcessor.from_pretrained(model_path)\n", " model = AutoModel.from_pretrained(model_path).eval().to(device)\n", "\n", " global_descs = []\n", " batch_size = 2 # Small batch to save memory\n", "\n", " for i in tqdm(range(0, len(image_paths), batch_size)):\n", " batch_paths = image_paths[i:i+batch_size]\n", " batch_imgs = []\n", "\n", " for img_path in batch_paths:\n", " img = load_torch_image(img_path, device)\n", " batch_imgs.append(img)\n", "\n", " batch_tensor = torch.cat(batch_imgs, dim=0)\n", "\n", " with torch.no_grad():\n", " inputs = processor(images=batch_tensor, return_tensors=\"pt\", do_rescale=False).to(device)\n", " outputs = model(**inputs)\n", " desc = F.normalize(outputs.last_hidden_state[:, 1:].max(dim=1)[0], dim=1, p=2)\n", " global_descs.append(desc.cpu())\n", "\n", " # Clear batch memory\n", " del batch_tensor, inputs, outputs, desc\n", " clear_memory()\n", "\n", " global_descs = torch.cat(global_descs, dim=0)\n", "\n", " del model, processor\n", " clear_memory()\n", "\n", " print(\"After DINO extraction:\")\n", " get_memory_info()\n", "\n", " return global_descs\n", "\n", "def build_topk_pairs(global_feats, k, device):\n", " \"\"\"Build top-k similar pairs from global features\"\"\"\n", " g = global_feats.to(device)\n", " sim = g @ g.T\n", " sim.fill_diagonal_(-1)\n", "\n", " N = sim.size(0)\n", " k = min(k, N - 1)\n", "\n", " topk_indices = torch.topk(sim, k, dim=1).indices.cpu()\n", "\n", " pairs = []\n", " for i in range(N):\n", " for j in topk_indices[i]:\n", " j = j.item()\n", " if i < j:\n", " pairs.append((i, j))\n", "\n", " # Remove duplicates\n", " pairs = list(set(pairs))\n", "\n", " return pairs\n", "\n", "def select_diverse_pairs(pairs, max_pairs, num_images):\n", " \"\"\"\n", " Select diverse pairs to ensure good image coverage\n", " Strategy: Select pairs that maximize image coverage\n", " \"\"\"\n", " import random\n", " random.seed(42)\n", "\n", " if len(pairs) <= max_pairs:\n", " return pairs\n", "\n", " print(f\"Selecting {max_pairs} diverse pairs from {len(pairs)} candidates...\")\n", "\n", " # Count how many times each image appears in pairs\n", " image_counts = {i: 0 for i in range(num_images)}\n", " for i, j in pairs:\n", " image_counts[i] += 1\n", " image_counts[j] += 1\n", "\n", " # Sort pairs by: prefer pairs with less-connected images\n", " def pair_score(pair):\n", " i, j = pair\n", " # Lower score = images appear in fewer pairs = more diverse\n", " return image_counts[i] + image_counts[j]\n", "\n", " pairs_scored = [(pair, pair_score(pair)) for pair in pairs]\n", " pairs_scored.sort(key=lambda x: x[1])\n", "\n", " # Select pairs greedily to maximize coverage\n", " selected = []\n", " selected_images = set()\n", "\n", " # Phase 1: Select pairs that add new images (greedy coverage)\n", " for pair, score in pairs_scored:\n", " if len(selected) >= max_pairs:\n", " break\n", " i, j = pair\n", " # Prefer pairs that include new images\n", " if i not in selected_images or j not in selected_images:\n", " selected.append(pair)\n", " selected_images.add(i)\n", " selected_images.add(j)\n", "\n", " # Phase 2: Fill remaining slots with high-similarity pairs\n", " if len(selected) < max_pairs:\n", " remaining = [p for p, s in pairs_scored if p not in selected]\n", " random.shuffle(remaining)\n", " selected.extend(remaining[:max_pairs - len(selected)])\n", "\n", " print(f\"Selected pairs cover {len(selected_images)} / {num_images} images ({100*len(selected_images)/num_images:.1f}%)\")\n", "\n", " return selected\n", "\n", "def get_image_pairs_dino(image_paths, max_pairs=None):\n", " \"\"\"DINO-based pair selection with intelligent limiting\"\"\"\n", " device = Config.DEVICE\n", "\n", " # DINO global features\n", " global_feats = extract_dino_global(image_paths, Config.DINO_MODEL, device)\n", " pairs = build_topk_pairs(global_feats, Config.GLOBAL_TOPK, device)\n", "\n", " print(f\"Initial pairs from DINO: {len(pairs)}\")\n", "\n", " # Apply intelligent pair selection if limit specified\n", " if max_pairs and len(pairs) > max_pairs:\n", " pairs = select_diverse_pairs(pairs, max_pairs, len(image_paths))\n", "\n", " return pairs\n", "\n", "# ============================================================================\n", "# Step 2: MASt3R Reconstruction (REPLACES ALIKED/LIGHTGLUE/COLMAP)\n", "# ============================================================================\n", "\n", "def load_mast3r_model(device='cuda'):\n", " \"\"\"Load MASt3R model\"\"\"\n", " from mast3r.model import AsymmetricMASt3R\n", "\n", " model = AsymmetricMASt3R.from_pretrained(Config.MAST3R_MODEL).to(device)\n", " model.eval()\n", "\n", " print(f\"✓ MASt3R model loaded on {device}\")\n", " return model\n", "\n", "def load_images_for_mast3r(image_paths, size=224):\n", " \"\"\"Load images using DUSt3R's format with reduced size\"\"\"\n", " print(f\"\\n=== Loading images for MASt3R (size={size}) ===\")\n", "\n", " from dust3r.utils.image import load_images\n", "\n", " # Load images using DUSt3R's loader with reduced size\n", " images = load_images(image_paths, size=size, verbose=True)\n", "\n", " return images\n", "\n", "def run_mast3r_pairs(model, image_paths, pairs, device='cuda', batch_size=1, max_pairs=None):\n", " \"\"\"Run MASt3R on selected pairs with memory management\"\"\"\n", " print(\"\\n=== Running MASt3R Reconstruction ===\")\n", " print(\"Initial memory state:\")\n", " get_memory_info()\n", "\n", " from dust3r.inference import inference\n", " from dust3r.cloud_opt import global_aligner, GlobalAlignerMode\n", "\n", " # Limit number of pairs if specified\n", " if max_pairs and len(pairs) > max_pairs:\n", " print(f\"Limiting pairs from {len(pairs)} to {max_pairs}\")\n", " # Select pairs more evenly distributed\n", " step = max(1, len(pairs) // max_pairs)\n", " pairs = pairs[::step][:max_pairs]\n", "\n", " print(f\"Processing {len(pairs)} pairs...\")\n", "\n", " # Load images in smaller size\n", " print(f\"Loading {len(image_paths)} images at {Config.MAST3R_IMAGE_SIZE}x{Config.MAST3R_IMAGE_SIZE}...\")\n", " images = load_images_for_mast3r(image_paths, size=Config.MAST3R_IMAGE_SIZE)\n", "\n", " print(f\"Loaded {len(images)} images\")\n", " print(\"After loading images:\")\n", " get_memory_info()\n", "\n", " # Create all image pairs at once\n", " print(f\"Creating {len(pairs)} image pairs...\")\n", " mast3r_pairs = []\n", " for idx1, idx2 in tqdm(pairs, desc=\"Preparing pairs\"):\n", " mast3r_pairs.append((images[idx1], images[idx2]))\n", "\n", " print(f\"Running MASt3R inference on {len(mast3r_pairs)} pairs...\")\n", "\n", " # Run inference (this returns the dict format we need)\n", " output = inference(mast3r_pairs, model, device, batch_size=batch_size, verbose=True)\n", "\n", " # Clear pairs from memory\n", " del mast3r_pairs\n", " clear_memory()\n", "\n", " print(\"✓ MASt3R inference complete\")\n", " print(\"After inference:\")\n", " get_memory_info()\n", "\n", " # Global alignment\n", " print(\"Running global alignment...\")\n", " scene = global_aligner(\n", " output,\n", " device=device,\n", " mode=GlobalAlignerMode.PointCloudOptimizer\n", " )\n", "\n", " # Clear output after creating scene\n", " del output\n", " clear_memory()\n", "\n", " print(\"Computing global alignment...\")\n", " loss = scene.compute_global_alignment(\n", " init=\"mst\",\n", " niter=150, # Reduced from 300\n", " schedule='cosine',\n", " lr=0.01\n", " )\n", "\n", " print(f\"✓ Global alignment complete (final loss: {loss:.6f})\")\n", " print(\"Final memory state:\")\n", " get_memory_info()\n", "\n", " return scene, images" ], "metadata": { "execution": { "iopub.execute_input": "2026-01-20T01:06:37.366201Z", "iopub.status.busy": "2026-01-20T01:06:37.365963Z", "iopub.status.idle": "2026-01-20T01:07:23.639941Z", "shell.execute_reply": "2026-01-20T01:07:23.639152Z" }, "papermill": { "duration": 46.280727, "end_time": "2026-01-20T01:07:23.641872", "exception": false, "start_time": "2026-01-20T01:06:37.361145", "status": "completed" }, "tags": [], "id": "RLqx_wGGGtP9" }, "outputs": [], "execution_count": null }, { "cell_type": "code", "source": [], "metadata": { "trusted": true, "id": "X_SZiDkLGtP9" }, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [ "# process1 start" ], "metadata": { "id": "sQ_035WxGtP-" } }, { "cell_type": "code", "source": [ "#v26\n", "def extract_colmap_data(scene, image_paths, max_points=1000000):\n", " \"\"\"\n", " Extract COLMAP-compatible camera parameters and 3D points from MASt3R scene\n", "\n", " Args:\n", " scene: MASt3R scene object\n", " image_paths: List of image paths\n", " max_points: Maximum number of 3D points to extract (default: 1M)\n", " \"\"\"\n", " print(\"\\n=== Extracting COLMAP-compatible data ===\")\n", "\n", " # Extract point cloud\n", " pts_all = scene.get_pts3d()\n", " print(f\"pts_all type: {type(pts_all)}\")\n", "\n", " if isinstance(pts_all, list):\n", " print(f\"pts_all is a list with {len(pts_all)} elements\")\n", " if len(pts_all) > 0:\n", " print(f\"First element type: {type(pts_all[0])}\")\n", " if hasattr(pts_all[0], 'shape'):\n", " print(f\"First element shape: {pts_all[0].shape}\")\n", "\n", " pts_all = torch.stack([p if isinstance(p, torch.Tensor) else torch.tensor(p)\n", " for p in pts_all])\n", " print(f\"pts_all shape after conversion: {pts_all.shape}\")\n", "\n", " if len(pts_all.shape) == 4:\n", " print(f\"Found batched point cloud: {pts_all.shape}\")\n", " B, H, W, _ = pts_all.shape\n", " pts3d = pts_all.reshape(-1, 3).detach().cpu().numpy()\n", "\n", " # Extract colors\n", " colors = []\n", " for img_path in image_paths:\n", " img = Image.open(img_path).resize((W, H))\n", " colors.append(np.array(img))\n", " colors = np.stack(colors).reshape(-1, 3) / 255.0\n", " else:\n", " pts3d = pts_all.detach().cpu().numpy() if isinstance(pts_all, torch.Tensor) else pts_all\n", " colors = np.ones((len(pts3d), 3)) * 0.5\n", "\n", " print(f\"✓ Extracted {len(pts3d)} 3D points from {len(image_paths)} images\")\n", "\n", " # **DOWNSAMPLE POINTS TO REDUCE MEMORY USAGE**\n", " if len(pts3d) > max_points:\n", " print(f\"\\n⚠ Downsampling from {len(pts3d)} to {max_points} points to reduce memory usage...\")\n", "\n", " # Remove invalid points first\n", " valid_mask = ~(np.isnan(pts3d).any(axis=1) | np.isinf(pts3d).any(axis=1))\n", " pts3d_valid = pts3d[valid_mask]\n", " colors_valid = colors[valid_mask]\n", "\n", " # Random sampling\n", " indices = np.random.choice(len(pts3d_valid), size=max_points, replace=False)\n", " pts3d = pts3d_valid[indices]\n", " colors = colors_valid[indices]\n", "\n", " print(f\"✓ Downsampled to {len(pts3d)} points\")\n", "\n", " # Extract camera parameters\n", " print(\"Extracting camera parameters...\")\n", "\n", " # 【重要】MASt3Rのポーズはcamera-to-world形式\n", " # COLMAPはworld-to-camera形式を要求するので逆行列が必要\n", " poses_c2w = scene.get_im_poses().detach().cpu().numpy()\n", " print(f\"Retrieved camera-to-world poses: shape {poses_c2w.shape}\")\n", "\n", " # camera-to-world を world-to-camera に変換\n", " poses = []\n", " for i, pose_c2w in enumerate(poses_c2w):\n", " # 4x4行列の逆行列を計算\n", " pose_w2c = np.linalg.inv(pose_c2w)\n", " poses.append(pose_w2c)\n", "\n", " poses = np.array(poses)\n", " print(f\"Converted to world-to-camera poses for COLMAP\")\n", "\n", " # 焦点距離と主点を取得\n", " focals = scene.get_focals().detach().cpu().numpy()\n", " pp = scene.get_principal_points().detach().cpu().numpy()\n", " print(f\"Focals shape: {focals.shape}\")\n", " print(f\"Principal points shape: {pp.shape}\")\n", "\n", " # MASt3Rの処理サイズ(通常224x224)\n", " mast3r_size = 224.0\n", "\n", " cameras = []\n", " for i, img_path in enumerate(image_paths):\n", " img = Image.open(img_path)\n", " W, H = img.size\n", "\n", " # 元画像サイズとのスケール比\n", " scale = W / mast3r_size\n", "\n", " # focalsは[N,1]の形式(fx=fyの等方性カメラ)\n", " if focals.shape[1] == 1:\n", " focal_mast3r = float(focals[i, 0])\n", " fx = fy = focal_mast3r * scale\n", " else:\n", " fx = float(focals[i, 0]) * scale\n", " fy = float(focals[i, 1]) * scale\n", "\n", " # 主点もスケーリング\n", " cx = float(pp[i, 0]) * scale\n", " cy = float(pp[i, 1]) * scale\n", "\n", " camera = {\n", " 'camera_id': i + 1,\n", " 'model': 'PINHOLE',\n", " 'width': W,\n", " 'height': H,\n", " 'params': [fx, fy, cx, cy]\n", " }\n", " cameras.append(camera)\n", "\n", " if i == 0:\n", " print(f\"\\nExample camera 0:\")\n", " print(f\" Image size: {W}x{H}\")\n", " print(f\" MASt3R focal: {focal_mast3r:.2f}, pp: ({pp[i,0]:.2f}, {pp[i,1]:.2f})\")\n", " print(f\" Scaled fx={fx:.2f}, fy={fy:.2f}, cx={cx:.2f}, cy={cy:.2f}\")\n", " print(f\" Pose (first row): {poses[i][0]}\")\n", "\n", " print(f\"\\n✓ Extracted {len(cameras)} cameras and {len(poses)} poses\")\n", "\n", " return pts3d, colors, cameras, poses" ], "metadata": { "execution": { "iopub.execute_input": "2026-01-20T01:07:23.650172Z", "iopub.status.busy": "2026-01-20T01:07:23.649629Z", "iopub.status.idle": "2026-01-20T01:07:23.662741Z", "shell.execute_reply": "2026-01-20T01:07:23.662058Z" }, "papermill": { "duration": 0.018921, "end_time": "2026-01-20T01:07:23.664244", "exception": false, "start_time": "2026-01-20T01:07:23.645323", "status": "completed" }, "tags": [], "id": "WaG8mcM6GtP-" }, "outputs": [], "execution_count": null }, { "cell_type": "code", "source": [ "import struct\n", "from pathlib import Path\n", "\n", "def save_colmap_reconstruction(pts3d, colors, cameras, poses, image_paths, output_dir):\n", " \"\"\"Save reconstruction in COLMAP binary format by writing files directly\"\"\"\n", " print(\"\\n=== Saving COLMAP reconstruction ===\")\n", "\n", " sparse_dir = Path(output_dir) / 'sparse' / '0'\n", " sparse_dir.mkdir(parents=True, exist_ok=True)\n", "\n", " print(f\" Writing COLMAP files directly to {sparse_dir}...\")\n", "\n", " # Write cameras.bin\n", " write_cameras_binary(cameras, sparse_dir / 'cameras.bin')\n", " print(f\" ✓ Wrote {len(cameras)} cameras\")\n", "\n", " # Write images.bin\n", " write_images_binary(image_paths, cameras, poses, sparse_dir / 'images.bin')\n", " print(f\" ✓ Wrote {len(image_paths)} images\")\n", "\n", " # Write points3D.bin\n", " num_points = write_points3d_binary(pts3d, colors, sparse_dir / 'points3D.bin')\n", " print(f\" ✓ Wrote {num_points} 3D points\")\n", "\n", " print(f\"\\n✓ COLMAP reconstruction saved to {sparse_dir}\")\n", " print(f\" Cameras: {len(cameras)}\")\n", " print(f\" Images: {len(image_paths)}\")\n", " print(f\" Points: {num_points}\")\n", "\n", " return sparse_dir\n", "\n", "\n", "def write_cameras_binary(cameras, output_file):\n", " \"\"\"Write cameras.bin in COLMAP binary format\"\"\"\n", " with open(output_file, 'wb') as f:\n", " # Write number of cameras\n", " f.write(struct.pack('Q', len(cameras)))\n", "\n", " for i, cam in enumerate(cameras):\n", " camera_id = cam.get('camera_id', i + 1)\n", "\n", " # Model ID: 1 = PINHOLE\n", " model_id = 1\n", " width = cam['width']\n", " height = cam['height']\n", " params = cam['params'] # [fx, fy, cx, cy]\n", "\n", " f.write(struct.pack('i', camera_id))\n", " f.write(struct.pack('i', model_id))\n", " f.write(struct.pack('Q', width))\n", " f.write(struct.pack('Q', height))\n", "\n", " # Write 4 parameters for PINHOLE model\n", " for param in params[:4]:\n", " f.write(struct.pack('d', param))\n", "\n", "\n", "def write_images_binary(image_paths, cameras, poses, output_file):\n", " \"\"\"Write images.bin in COLMAP binary format\"\"\"\n", " with open(output_file, 'wb') as f:\n", " # Write number of images\n", " f.write(struct.pack('Q', len(image_paths)))\n", "\n", " for i, (img_path, pose) in enumerate(zip(image_paths, poses)):\n", " image_id = i + 1\n", " camera_id = cameras[i].get('camera_id', i + 1)\n", " image_name = os.path.basename(img_path)\n", "\n", " # Extract rotation and translation\n", " R = pose[:3, :3]\n", " t = pose[:3, 3]\n", "\n", " # Convert rotation matrix to quaternion [w, x, y, z]\n", " qvec = rotmat2qvec(R)\n", " tvec = t\n", "\n", " # Write image data\n", " f.write(struct.pack('i', image_id))\n", "\n", " # Write quaternion (4 doubles)\n", " for q in qvec:\n", " f.write(struct.pack('d', float(q)))\n", "\n", " # Write translation vector (3 doubles)\n", " for tv in tvec:\n", " f.write(struct.pack('d', float(tv)))\n", "\n", " # Write camera ID\n", " f.write(struct.pack('i', camera_id))\n", "\n", " # Write image name (null-terminated string)\n", " f.write(image_name.encode('utf-8') + b'\\x00')\n", "\n", " # Write number of 2D points (0 for now, as we don't have 2D-3D correspondences)\n", " f.write(struct.pack('Q', 0))\n", "\n", "\n", "def write_points3d_binary(pts3d, colors, output_file):\n", " \"\"\"Write points3D.bin in COLMAP binary format\"\"\"\n", " # Filter out invalid points\n", " valid_indices = []\n", " for i, pt in enumerate(pts3d):\n", " if not (np.isnan(pt).any() or np.isinf(pt).any()):\n", " valid_indices.append(i)\n", "\n", " with open(output_file, 'wb') as f:\n", " # Write number of points\n", " f.write(struct.pack('Q', len(valid_indices)))\n", "\n", " for idx, point_id in enumerate(valid_indices):\n", " pt = pts3d[point_id]\n", " color = colors[point_id]\n", "\n", " # Write point3D ID\n", " f.write(struct.pack('Q', point_id))\n", "\n", " # Write XYZ coordinates (3 doubles)\n", " for coord in pt:\n", " f.write(struct.pack('d', float(coord)))\n", "\n", " # Write RGB color (3 unsigned chars)\n", " col_int = (color * 255).astype(np.uint8)\n", " for c in col_int:\n", " f.write(struct.pack('B', int(c)))\n", "\n", " # Write error (1 double) - set to 0\n", " f.write(struct.pack('d', 0.0))\n", "\n", " # Write track length (number of images seeing this point)\n", " # Set to 0 as we don't have track information\n", " f.write(struct.pack('Q', 0))\n", "\n", " # Progress indicator\n", " if (idx + 1) % 1000000 == 0:\n", " print(f\" Wrote {idx + 1} / {len(valid_indices)} points...\")\n", "\n", " return len(valid_indices)\n", "\n", "\n", "def rotmat2qvec(R):\n", " \"\"\"\n", " Convert rotation matrix to quaternion in COLMAP format [w, x, y, z]\n", "\n", " Args:\n", " R: 3x3 rotation matrix\n", "\n", " Returns:\n", " qvec: quaternion [w, x, y, z]\n", " \"\"\"\n", " # Ensure R is a numpy array\n", " R = np.asarray(R, dtype=np.float64)\n", "\n", " # Calculate trace\n", " trace = np.trace(R)\n", "\n", " if trace > 0:\n", " s = 0.5 / np.sqrt(trace + 1.0)\n", " w = 0.25 / s\n", " x = (R[2, 1] - R[1, 2]) * s\n", " y = (R[0, 2] - R[2, 0]) * s\n", " z = (R[1, 0] - R[0, 1]) * s\n", " elif R[0, 0] > R[1, 1] and R[0, 0] > R[2, 2]:\n", " s = 2.0 * np.sqrt(1.0 + R[0, 0] - R[1, 1] - R[2, 2])\n", " w = (R[2, 1] - R[1, 2]) / s\n", " x = 0.25 * s\n", " y = (R[0, 1] + R[1, 0]) / s\n", " z = (R[0, 2] + R[2, 0]) / s\n", " elif R[1, 1] > R[2, 2]:\n", " s = 2.0 * np.sqrt(1.0 + R[1, 1] - R[0, 0] - R[2, 2])\n", " w = (R[0, 2] - R[2, 0]) / s\n", " x = (R[0, 1] + R[1, 0]) / s\n", " y = 0.25 * s\n", " z = (R[1, 2] + R[2, 1]) / s\n", " else:\n", " s = 2.0 * np.sqrt(1.0 + R[2, 2] - R[0, 0] - R[1, 1])\n", " w = (R[1, 0] - R[0, 1]) / s\n", " x = (R[0, 2] + R[2, 0]) / s\n", " y = (R[1, 2] + R[2, 1]) / s\n", " z = 0.25 * s\n", "\n", " qvec = np.array([w, x, y, z], dtype=np.float64)\n", "\n", " # Normalize\n", " qvec = qvec / np.linalg.norm(qvec)\n", "\n", " return qvec" ], "metadata": { "execution": { "iopub.execute_input": "2026-01-20T01:07:23.671693Z", "iopub.status.busy": "2026-01-20T01:07:23.671426Z", "iopub.status.idle": "2026-01-20T01:07:23.690446Z", "shell.execute_reply": "2026-01-20T01:07:23.689724Z" }, "papermill": { "duration": 0.024458, "end_time": "2026-01-20T01:07:23.691846", "exception": false, "start_time": "2026-01-20T01:07:23.667388", "status": "completed" }, "tags": [], "id": "QDHVA9SwGtP-" }, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [ "# process1 end" ], "metadata": { "id": "f9mVFJA8GtP-" } }, { "cell_type": "code", "source": [], "metadata": { "trusted": true, "id": "7ZluhU5VGtP-" }, "outputs": [], "execution_count": null }, { "cell_type": "code", "source": [ "# ============================================================================\n", "# Step 3: Gaussian Splatting Training\n", "# ============================================================================\n", "\n", "def train_gaussian_splatting(colmap_dir, image_dir, output_dir, iterations=2000):\n", " \"\"\"Train Gaussian Splatting model\"\"\"\n", " print(\"\\n\" + \"=\"*70)\n", " print(\"Step 6: Training Gaussian Splatting\")\n", " print(\"=\"*70)\n", "\n", " print(\"\\n=== Training Gaussian Splatting ===\")\n", "\n", " # Reduce memory usage with smaller resolution\n", " cmd = [\n", " 'python', 'train.py',\n", " '-s', colmap_dir,\n", " '--images', image_dir,\n", " '-m', output_dir,\n", " '--iterations', str(iterations),\n", " '--test_iterations', '1000', str(iterations),\n", " '--save_iterations', '1000', str(iterations),\n", " '--resolution', '2', # Reduce resolution to 1/2\n", " '--densify_grad_threshold', '0.001', # Higher threshold = fewer Gaussians\n", " '--densification_interval', '200', # Less frequent densification\n", " '--opacity_reset_interval', '5000', # Less frequent reset\n", " ]\n", "\n", " print(f\"Command: {' '.join(cmd)}\\n\")\n", "\n", " result = subprocess.run(\n", " cmd,\n", " cwd='/content/gaussian-splatting',\n", " capture_output=True,\n", " text=True\n", " )\n", "\n", " print(result.stdout)\n", " if result.stderr:\n", " print(\"STDERR:\", result.stderr)\n", "\n", " if result.returncode != 0:\n", " raise RuntimeError(\"Gaussian Splatting training failed\")\n", "\n", " # Check output\n", " if not os.path.exists(os.path.join(output_dir, f'point_cloud/iteration_{iterations}/point_cloud.ply')):\n", " raise RuntimeError(f\"Expected output not found at iteration {iterations}\")\n", "\n", " print(f\"\\n✓ Gaussian Splatting training completed successfully\")\n", " print(f\" Output: {output_dir}\")\n", "\n", " return output_dir" ], "metadata": { "execution": { "iopub.execute_input": "2026-01-20T01:07:23.698951Z", "iopub.status.busy": "2026-01-20T01:07:23.698698Z", "iopub.status.idle": "2026-01-20T01:07:23.707157Z", "shell.execute_reply": "2026-01-20T01:07:23.706462Z" }, "papermill": { "duration": 0.01366, "end_time": "2026-01-20T01:07:23.70848", "exception": false, "start_time": "2026-01-20T01:07:23.69482", "status": "completed" }, "tags": [], "id": "1IIaAUIbGtP-" }, "outputs": [], "execution_count": null }, { "cell_type": "markdown", "source": [], "metadata": { "id": "QFx8G0zvGtP_" } }, { "cell_type": "code", "source": [], "metadata": { "trusted": true, "id": "SUbxuxaqGtP_" }, "outputs": [], "execution_count": null }, { "cell_type": "code", "source": [ "# ============================================================================\n", "# Main Pipeline\n", "# ============================================================================\n", "def main_pipeline(image_dir, output_dir, square_size=224, iterations=2000,\n", " max_images=None, max_pairs=10000, max_points=1000000):\n", " \"\"\"\n", " Main pipeline for DINO matching -> MASt3R -> Gaussian Splatting\n", "\n", " Args:\n", " image_dir: Directory containing input images\n", " output_dir: Directory for output files\n", " square_size: Size to resize images for processing\n", " iterations: Number of training iterations\n", " max_images: Maximum number of images to process (None = all)\n", " max_pairs: Maximum number of image pairs for matching\n", " max_points: Maximum number of 3D points to extract (default: 1M)\n", " \"\"\"\n", " os.makedirs(output_dir, exist_ok=True)\n", "\n", "\n", " # Step 1: Normalize images to biplet-square format\n", " print(\"\\n\" + \"=\"*70)\n", " print(\"Step 1: Biplet-Square Normalization\")\n", " print(\"=\"*70)\n", "\n", " processed_image_dir = os.path.join(output_dir, \"processed_images\")\n", "\n", " # Get original images first\n", " original_image_paths = sorted([\n", " os.path.join(image_dir, f)\n", " for f in os.listdir(image_dir)\n", " if f.lower().endswith(('.jpg', '.jpeg', '.png'))\n", " ])\n", "\n", " # Limit original images if specified\n", " if max_images and len(original_image_paths) > max_images:\n", " print(f\"\\n⚠️ Limiting to {max_images} original images\")\n", " original_image_paths = original_image_paths[:max_images]\n", "\n", " print(f\"Processing {len(original_image_paths)} original images → ~{len(original_image_paths)*2} after biplet-square\")\n", "\n", " # Only process the selected images\n", " temp_dir = os.path.join(output_dir, \"temp_originals\")\n", " os.makedirs(temp_dir, exist_ok=True)\n", "\n", " # Copy selected images to temp directory\n", " for img_path in original_image_paths:\n", " import shutil\n", " shutil.copy(img_path, temp_dir)\n", "\n", " # Process the temp directory\n", " normalize_image_sizes_biplet(\n", " input_dir=temp_dir,\n", " output_dir=processed_image_dir,\n", " size=square_size\n", " )\n", "\n", " # Clean up temp directory\n", " shutil.rmtree(temp_dir)\n", "\n", " # Get processed image paths\n", " image_paths = sorted([\n", " os.path.join(processed_image_dir, f)\n", " for f in os.listdir(processed_image_dir)\n", " if f.lower().endswith(('.jpg', '.jpeg', '.png'))\n", " ])\n", "\n", " print(f\"\\n📸 Processing {len(image_paths)} images (after biplet-square)\")\n", " print(f\"⚠️ Will use maximum {max_pairs} pairs to save memory\")\n", "\n", " # Step 2: DINO-based pair selection\n", " print(\"\\n\" + \"=\"*70)\n", " print(\"Step 2: DINO Pair Selection\")\n", " print(\"=\"*70)\n", "\n", " pairs = get_image_pairs_dino(image_paths, max_pairs=max_pairs)\n", " clear_memory()\n", "\n", " print(f\"✓ Using {len(pairs)} pairs for reconstruction\")\n", "\n", " # Step 3: MASt3R reconstruction\n", " print(\"\\n\" + \"=\"*70)\n", " print(\"Step 3: MASt3R Reconstruction\")\n", " print(\"=\"*70)\n", "\n", " device = Config.DEVICE\n", " model = load_mast3r_model(device)\n", "\n", " scene, mast3r_images = run_mast3r_pairs(\n", " model, image_paths, pairs, device,\n", " max_pairs=None # Already limited in get_image_pairs_dino\n", " )\n", "\n", " # Clear model from memory\n", " del model\n", " clear_memory()\n", "\n", " # Step 4: Extract COLMAP-compatible data\n", " print(\"\\n\" + \"=\"*70)\n", " print(\"Step 4: Converting to COLMAP Format\")\n", " print(\"=\"*70)\n", "\n", " # Extract COLMAP-compatible data with point limit\n", " pts3d, colors, cameras, poses = extract_colmap_data(\n", " scene, image_paths, max_points=max_points\n", " )\n", "\n", " # Clear scene from memory\n", " del scene, mast3r_images\n", " clear_memory()\n", "\n", " # Step 5: Save COLMAP reconstruction\n", " colmap_dir = os.path.join(output_dir, 'colmap')\n", " sparse_dir = save_colmap_reconstruction(\n", " pts3d, colors, cameras, poses, image_paths, colmap_dir\n", " )\n", "\n", " # Clear reconstruction data\n", " del pts3d, colors, cameras, poses\n", " clear_memory()\n", "\n", " # Step 6: Train Gaussian Splatting\n", " print(\"\\n\" + \"=\"*70)\n", " print(\"Step 6: Training Gaussian Splatting\")\n", " print(\"=\"*70)\n", "\n", " gs_output = train_gaussian_splatting(\n", " colmap_dir=colmap_dir,\n", " image_dir=processed_image_dir,\n", " output_dir=output_dir,\n", " iterations=iterations\n", " )\n", "\n", " print(\"\\n\" + \"=\"*70)\n", " print(\"✅ Full Pipeline Successfully Completed!\")\n", " print(\"=\"*70)\n", " print(f\"\\nGaussian Splatting model saved at: {gs_output}\")\n", "\n", " return gs_output" ], "metadata": { "execution": { "iopub.execute_input": "2026-01-20T01:07:23.715836Z", "iopub.status.busy": "2026-01-20T01:07:23.715633Z", "iopub.status.idle": "2026-01-20T01:07:23.726379Z", "shell.execute_reply": "2026-01-20T01:07:23.725735Z" }, "papermill": { "duration": 0.016081, "end_time": "2026-01-20T01:07:23.727745", "exception": false, "start_time": "2026-01-20T01:07:23.711664", "status": "completed" }, "tags": [], "id": "8cnEFkcGGtP_" }, "outputs": [], "execution_count": null }, { "cell_type": "code", "source": [], "metadata": { "trusted": true, "id": "f-_pycuLGtP_" }, "outputs": [], "execution_count": null }, { "cell_type": "code", "source": [ "if __name__ == \"__main__\":\n", "\n", " IMAGE_DIR = \"/content/drive/MyDrive/your_folder/cyprus\"\n", " OUTPUT_DIR = \"/content/output\"\n", "\n", " gs_output = main_pipeline(\n", " image_dir=IMAGE_DIR,\n", " output_dir=OUTPUT_DIR,\n", " square_size=1024,\n", " iterations=3000,\n", " max_images=60,\n", " max_pairs=1000,\n", " max_points=1000000\n", " )\n", "\n", " print(f\"\\n{'='*70}\")\n", " print(\"Pipeline completed successfully!\")\n", " print(f\"{'='*70}\")\n", " print(f\"Gaussian Splatting output: {gs_output}\")" ], "metadata": { "execution": { "iopub.execute_input": "2026-01-20T01:07:23.734887Z", "iopub.status.busy": "2026-01-20T01:07:23.734668Z", "iopub.status.idle": "2026-01-20T01:22:29.19147Z", "shell.execute_reply": "2026-01-20T01:22:29.190842Z" }, "papermill": { "duration": 905.62414, "end_time": "2026-01-20T01:22:29.355023", "exception": false, "start_time": "2026-01-20T01:07:23.730883", "status": "completed" }, "tags": [], "_kg_hide-output": true, "id": "ykrCt63tGtP_", "colab": { "base_uri": "https://localhost:8080/", "height": 1000, "referenced_widgets": [ "71136d03f7394b2093da996ec9377426", "ec440aa735ec42a387d5686525d78e12", "cfebe5ba39604457a0d1a76ff8ddb83b", "3389884d59d04f169fbc7df68173ed88", "6dd48df3252c4fd2b25bd339693f6796", "cca860c624684a7cbb96d7012948945d", "f5122873126e439f899c98a48a14b9da", "2a5789d0fe934d73b51392c9ac2086ed", "d13f592933eb4c6db622a7d350d3f6ea", "4d98b95eaa0349978061955b417a534a", "9d9db218417a40f9b215eecd7ce59c09" ] }, "outputId": "9481270c-4946-48c7-e43d-2de62a49dae1" }, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "\n", "======================================================================\n", "Step 1: Biplet-Square Normalization\n", "======================================================================\n", "Processing 30 original images → ~60 after biplet-square\n", "Generating 2 cropped squares (Left/Right or Top/Bottom) for each image...\n", "\n", " ✓ DSC_6480.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6488.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6492.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6496.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6500.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6508.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6512.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6520.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6524.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6528.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6540.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6548.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6557.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6565.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6569.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6573.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6577.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6585.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6589.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6593.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6597.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6601.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6605.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6609.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6613.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6617.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6621.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6625.JPG: (6048, 4032) → 2 square images generated\n", " ✓ DSC_6629.JPG: (6048, 4032) → 2 square images generated\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.\n", "WARNING:huggingface_hub.utils._http:Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ " ✓ DSC_6633.JPG: (6048, 4032) → 2 square images generated\n", "\n", "Processing complete: 30 source images processed\n", "Original size distribution: {'6048x4032': 30}\n", "\n", "📸 Processing 60 images (after biplet-square)\n", "⚠️ Will use maximum 1000 pairs to save memory\n", "\n", "======================================================================\n", "Step 2: DINO Pair Selection\n", "======================================================================\n", "\n", "=== Extracting DINO Global Features ===\n", "Initial memory state:\n", "GPU Memory - Allocated: 0.02GB, Reserved: 0.07GB\n", "CPU Memory Usage: 22.2%\n" ] }, { "output_type": "display_data", "data": { "text/plain": [ "Loading weights: 0%| | 0/223 [00:00\n", "✓ MASt3R model loaded on cuda\n", "\n", "=== Running MASt3R Reconstruction ===\n", "Initial memory state:\n", "GPU Memory - Allocated: 2.59GB, Reserved: 2.70GB\n", "CPU Memory Usage: 22.2%\n", "Processing 559 pairs...\n", "Loading 60 images at 224x224...\n", "\n", "=== Loading images for MASt3R (size=224) ===\n", ">> Loading a list of 60 images\n", " - adding /content/output/processed_images/DSC_6480_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6480_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6488_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6488_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6492_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6492_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6496_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6496_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6500_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6500_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6508_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6508_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6512_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6512_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6520_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6520_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6524_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6524_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6528_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6528_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6540_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6540_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6548_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6548_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6557_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6557_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6565_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6565_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6569_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6569_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6573_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6573_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6577_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6577_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6585_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6585_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6589_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6589_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6593_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6593_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6597_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6597_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6601_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6601_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6605_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6605_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6609_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6609_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6613_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6613_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6617_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6617_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6621_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6621_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6625_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6625_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6629_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6629_right.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6633_left.JPG with resolution 1024x1024 --> 224x224\n", " - adding /content/output/processed_images/DSC_6633_right.JPG with resolution 1024x1024 --> 224x224\n", " (Found 60 images)\n", "Loaded 60 images\n", "After loading images:\n", "GPU Memory - Allocated: 2.59GB, Reserved: 2.70GB\n", "CPU Memory Usage: 22.2%\n", "Creating 559 image pairs...\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "Preparing pairs: 100%|██████████| 559/559 [00:00<00:00, 1565164.18it/s]\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "Running MASt3R inference on 559 pairs...\n", ">> Inference with model on 559 image pairs\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "\r 0%| | 0/559 [00:00\n", "pts_all is a list with 60 elements\n", "First element type: \n", "First element shape: torch.Size([224, 224, 3])\n", "pts_all shape after conversion: torch.Size([60, 224, 224, 3])\n", "Found batched point cloud: torch.Size([60, 224, 224, 3])\n", "✓ Extracted 3010560 3D points from 60 images\n", "\n", "⚠ Downsampling from 3010560 to 1000000 points to reduce memory usage...\n", "✓ Downsampled to 1000000 points\n", "Extracting camera parameters...\n", "Retrieved camera-to-world poses: shape (60, 4, 4)\n", "Converted to world-to-camera poses for COLMAP\n", "Focals shape: (60, 1)\n", "Principal points shape: (60, 2)\n", "\n", "Example camera 0:\n", " Image size: 1024x1024\n", " MASt3R focal: 390.89, pp: (112.00, 112.00)\n", " Scaled fx=1786.94, fy=1786.94, cx=512.00, cy=512.00\n", " Pose (first row): [ 0.11157481 0.96115595 -0.25244868 0.7679974 ]\n", "\n", "✓ Extracted 60 cameras and 60 poses\n", "\n", "=== Saving COLMAP reconstruction ===\n", " Writing COLMAP files directly to /content/output/colmap/sparse/0...\n", " ✓ Wrote 60 cameras\n", " ✓ Wrote 60 images\n", " Wrote 1000000 / 1000000 points...\n", " ✓ Wrote 1000000 3D points\n", "\n", "✓ COLMAP reconstruction saved to /content/output/colmap/sparse/0\n", " Cameras: 60\n", " Images: 60\n", " Points: 1000000\n", "\n", "======================================================================\n", "Step 6: Training Gaussian Splatting\n", "======================================================================\n", "\n", "======================================================================\n", "Step 6: Training Gaussian Splatting\n", "======================================================================\n", "\n", "=== Training Gaussian Splatting ===\n", "Command: python train.py -s /content/output/colmap --images /content/output/processed_images -m /content/output --iterations 3000 --test_iterations 1000 3000 --save_iterations 1000 3000 --resolution 2 --densify_grad_threshold 0.001 --densification_interval 200 --opacity_reset_interval 5000\n", "\n", "Optimizing /content/output\n", "Output folder: /content/output [04/02 10:54:00]\n", "\n", "Reading camera 1/60\n", "Reading camera 2/60\n", "Reading camera 3/60\n", "Reading camera 4/60\n", "Reading camera 5/60\n", "Reading camera 6/60\n", "Reading camera 7/60\n", "Reading camera 8/60\n", "Reading camera 9/60\n", "Reading camera 10/60\n", "Reading camera 11/60\n", "Reading camera 12/60\n", "Reading camera 13/60\n", "Reading camera 14/60\n", "Reading camera 15/60\n", "Reading camera 16/60\n", "Reading camera 17/60\n", "Reading camera 18/60\n", "Reading camera 19/60\n", "Reading camera 20/60\n", "Reading camera 21/60\n", "Reading camera 22/60\n", "Reading camera 23/60\n", "Reading camera 24/60\n", "Reading camera 25/60\n", "Reading camera 26/60\n", "Reading camera 27/60\n", "Reading camera 28/60\n", "Reading camera 29/60\n", "Reading camera 30/60\n", "Reading camera 31/60\n", "Reading camera 32/60\n", "Reading camera 33/60\n", "Reading camera 34/60\n", "Reading camera 35/60\n", "Reading camera 36/60\n", "Reading camera 37/60\n", "Reading camera 38/60\n", "Reading camera 39/60\n", "Reading camera 40/60\n", "Reading camera 41/60\n", "Reading camera 42/60\n", "Reading camera 43/60\n", "Reading camera 44/60\n", "Reading camera 45/60\n", "Reading camera 46/60\n", "Reading camera 47/60\n", "Reading camera 48/60\n", "Reading camera 49/60\n", "Reading camera 50/60\n", "Reading camera 51/60\n", "Reading camera 52/60\n", "Reading camera 53/60\n", "Reading camera 54/60\n", "Reading camera 55/60\n", "Reading camera 56/60\n", "Reading camera 57/60\n", "Reading camera 58/60\n", "Reading camera 59/60\n", "Reading camera 60/60 [04/02 10:54:00]\n", "Loading Training Cameras [04/02 10:54:00]\n", "Loading Test Cameras [04/02 10:54:02]\n", "Number of points at initialisation : 1000000 [04/02 10:54:02]\n", "\n", "[ITER 1000] Evaluating train: L1 0.06662437468767167 PSNR 21.071295547485352 [04/02 10:55:26]\n", "\n", "[ITER 1000] Saving Gaussians [04/02 10:55:26]\n", "\n", "[ITER 3000] Evaluating train: L1 0.055911998823285104 PSNR 22.38820667266846 [04/02 10:57:58]\n", "\n", "[ITER 3000] Saving Gaussians [04/02 10:57:58]\n", "\n", "Training complete. [04/02 10:58:07]\n", "\n", "STDERR: 2026-02-04 10:53:54.187538: E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:467] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered\n", "WARNING: All log messages before absl::InitializeLog() is called are written to STDERR\n", "E0000 00:00:1770202434.210189 10035 cuda_dnn.cc:8579] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered\n", "E0000 00:00:1770202434.216963 10035 cuda_blas.cc:1407] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered\n", "W0000 00:00:1770202434.235139 10035 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.\n", "W0000 00:00:1770202434.235169 10035 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.\n", "W0000 00:00:1770202434.235171 10035 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.\n", "W0000 00:00:1770202434.235173 10035 computation_placer.cc:177] computation placer already registered. Please check linkage and avoid linking the same target more than once.\n", "2026-02-04 10:53:54.240474: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.\n", "To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.\n", "\n", "Training progress: 0%| | 0/3000 [00:00