| 1 |
# Copyright (c), ETH Zurich and UNC Chapel Hill. |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# Redistribution and use in source and binary forms, with or without |
| 5 |
# modification, are permitted provided that the following conditions are met: |
| 6 |
# |
| 7 |
# * Redistributions of source code must retain the above copyright |
| 8 |
# notice, this list of conditions and the following disclaimer. |
| 9 |
# |
| 10 |
# * Redistributions in binary form must reproduce the above copyright |
| 11 |
# notice, this list of conditions and the following disclaimer in the |
| 12 |
# documentation and/or other materials provided with the distribution. |
| 13 |
# |
| 14 |
# * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of |
| 15 |
# its contributors may be used to endorse or promote products derived |
| 16 |
# from this software without specific prior written permission. |
| 17 |
# |
| 18 |
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 |
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 |
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 |
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 |
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 |
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 |
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 |
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 |
# POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
import argparse |
| 31 |
import hashlib |
| 32 |
import inspect |
| 33 |
import json |
| 34 |
import shutil |
| 35 |
import subprocess |
| 36 |
import tarfile |
| 37 |
import zipfile |
| 38 |
from pathlib import Path |
| 39 |
|
| 40 |
import py7zr |
| 41 |
import requests |
| 42 |
from evaluation.tartanair.tartanair_v2 import ( |
| 43 |
MANIFEST_PATH, |
| 44 |
load_manifest, |
| 45 |
scene_shards, |
| 46 |
shard_name, |
| 47 |
) |
| 48 |
|
| 49 |
import pycolmap |
| 50 |
|
| 51 |
|
| 52 |
def download_file(url: str, target_folder: Path) -> str: |
| 53 |
filename = url.split("/")[-1] |
| 54 |
with requests.get(url, stream=True) as req: |
| 55 |
req.raise_for_status() |
| 56 |
with open(target_folder / filename, "wb") as f: |
| 57 |
for chunk in req.iter_content(chunk_size=8192): |
| 58 |
f.write(chunk) |
| 59 |
return filename |
| 60 |
|
| 61 |
|
| 62 |
def download_eth3d(data_path: Path) -> None: |
| 63 |
for filename, category in [ |
| 64 |
("multi_view_training_dslr_undistorted.7z", "dslr"), |
| 65 |
("multi_view_test_dslr_undistorted.7z", "dslr"), |
| 66 |
("multi_view_training_rig_undistorted.7z", "rig"), |
| 67 |
("multi_view_test_rig_undistorted.7z", "rig"), |
| 68 |
]: |
| 69 |
target_folder = data_path / category |
| 70 |
target_folder.mkdir(parents=True, exist_ok=True) |
| 71 |
|
| 72 |
pycolmap.logging.info( |
| 73 |
f"Downloading ETH3D category={category}, filename={filename}" |
| 74 |
) |
| 75 |
download_file("https://www.eth3d.net/data/" + filename, target_folder) |
| 76 |
|
| 77 |
pycolmap.logging.info( |
| 78 |
f"Extracting ETH3D category={category}, filename={filename}" |
| 79 |
) |
| 80 |
with py7zr.SevenZipFile(target_folder / filename, mode="r") as archive: |
| 81 |
archive.extractall(path=target_folder) |
| 82 |
|
| 83 |
|
| 84 |
def download_imc2023(data_path: Path) -> None: |
| 85 |
data_path.mkdir(parents=True, exist_ok=True) |
| 86 |
|
| 87 |
pycolmap.logging.info("Downloading IMC2023") |
| 88 |
subprocess.check_call( |
| 89 |
[ |
| 90 |
"kaggle", |
| 91 |
"competitions", |
| 92 |
"download", |
| 93 |
"-c", |
| 94 |
"image-matching-challenge-2023", |
| 95 |
"-p", |
| 96 |
str(data_path), |
| 97 |
], |
| 98 |
) |
| 99 |
|
| 100 |
pycolmap.logging.info("Extracting IMC2023") |
| 101 |
with zipfile.ZipFile( |
| 102 |
data_path / "image-matching-challenge-2023.zip", mode="r" |
| 103 |
) as archive: |
| 104 |
archive.extractall(path=data_path) |
| 105 |
|
| 106 |
|
| 107 |
def download_imc2024(data_path: Path) -> None: |
| 108 |
data_path.mkdir(parents=True, exist_ok=True) |
| 109 |
|
| 110 |
pycolmap.logging.info("Downloading IMC2024") |
| 111 |
subprocess.check_call( |
| 112 |
[ |
| 113 |
"kaggle", |
| 114 |
"competitions", |
| 115 |
"download", |
| 116 |
"-c", |
| 117 |
"image-matching-challenge-2024", |
| 118 |
"-p", |
| 119 |
str(data_path), |
| 120 |
], |
| 121 |
) |
| 122 |
|
| 123 |
pycolmap.logging.info("Extracting IMC2024") |
| 124 |
with zipfile.ZipFile( |
| 125 |
data_path / "image-matching-challenge-2024.zip", mode="r" |
| 126 |
) as archive: |
| 127 |
archive.extractall(path=data_path) |
| 128 |
|
| 129 |
# Move all scenes to the "all" category sub-folder. |
| 130 |
category_path = data_path / "train/all" |
| 131 |
category_path.mkdir(parents=True, exist_ok=True) |
| 132 |
for scene in (data_path / "train").iterdir(): |
| 133 |
if str(scene).endswith("/all"): |
| 134 |
continue |
| 135 |
shutil.move(scene, data_path / category_path) |
| 136 |
|
| 137 |
|
| 138 |
def download_imc2025(data_path: Path) -> None: |
| 139 |
data_path.mkdir(parents=True, exist_ok=True) |
| 140 |
|
| 141 |
pycolmap.logging.info("Downloading IMC2025") |
| 142 |
subprocess.check_call( |
| 143 |
[ |
| 144 |
"kaggle", |
| 145 |
"competitions", |
| 146 |
"download", |
| 147 |
"-c", |
| 148 |
"image-matching-challenge-2025", |
| 149 |
"-p", |
| 150 |
str(data_path), |
| 151 |
], |
| 152 |
) |
| 153 |
|
| 154 |
pycolmap.logging.info("Extracting IMC2025") |
| 155 |
with zipfile.ZipFile( |
| 156 |
data_path / "image-matching-challenge-2025.zip", mode="r" |
| 157 |
) as archive: |
| 158 |
archive.extractall(path=data_path) |
| 159 |
|
| 160 |
# Move all scenes to the "all" category sub-folder. |
| 161 |
category_path = data_path / "train/all" |
| 162 |
category_path.mkdir(parents=True, exist_ok=True) |
| 163 |
for scene in (data_path / "train").iterdir(): |
| 164 |
if scene.name == "all": |
| 165 |
continue |
| 166 |
shutil.move(scene, category_path) |
| 167 |
|
| 168 |
|
| 169 |
# TODO: BlendedMVS+ and BlendedMVS++. |
| 170 |
def download_blended_mvs(data_path: Path) -> None: |
| 171 |
target_folder = data_path / "BlendedMVS" |
| 172 |
target_folder.mkdir(parents=True, exist_ok=True) |
| 173 |
|
| 174 |
pycolmap.logging.info("Downloading BlendedMVS") |
| 175 |
for filename in [ |
| 176 |
"BlendedMVS.zip", |
| 177 |
] + [f"BlendedMVS.z{i:02d}" for i in range(1, 16)]: |
| 178 |
download_file( |
| 179 |
"https://github.com/YoYo000/BlendedMVS/releases/download/v1.0.0/" |
| 180 |
+ filename, |
| 181 |
target_folder, |
| 182 |
) |
| 183 |
|
| 184 |
pycolmap.logging.info("Merging BlendedMVS split archive") |
| 185 |
combined_zip = target_folder / "BlendedMVS_combined.zip" |
| 186 |
subprocess.check_call( |
| 187 |
[ |
| 188 |
"zip", |
| 189 |
"-q", |
| 190 |
"-s", |
| 191 |
"0", |
| 192 |
str(target_folder / "BlendedMVS.zip"), |
| 193 |
"--out", |
| 194 |
str(combined_zip), |
| 195 |
] |
| 196 |
) |
| 197 |
|
| 198 |
pycolmap.logging.info("Extracting BlendedMVS") |
| 199 |
try: |
| 200 |
with zipfile.ZipFile(combined_zip, mode="r") as archive: |
| 201 |
archive.extractall(path=data_path) |
| 202 |
finally: |
| 203 |
if combined_zip.exists(): |
| 204 |
combined_zip.unlink() |
| 205 |
|
| 206 |
|
| 207 |
def _sha256(path: Path) -> str: |
| 208 |
digest = hashlib.sha256() |
| 209 |
with open(path, "rb") as fid: |
| 210 |
while chunk := fid.read(1024 * 1024): |
| 211 |
digest.update(chunk) |
| 212 |
return digest.hexdigest() |
| 213 |
|
| 214 |
|
| 215 |
def _extract_tar_safely(archive_path: Path, output_path: Path) -> None: |
| 216 |
output_root = output_path.resolve() |
| 217 |
with tarfile.open(archive_path) as archive: |
| 218 |
for member in archive.getmembers(): |
| 219 |
if member.issym() or member.islnk(): |
| 220 |
raise RuntimeError( |
| 221 |
f"Refusing link in release archive: {member.name}" |
| 222 |
) |
| 223 |
target = (output_path / member.name).resolve() |
| 224 |
if not target.is_relative_to(output_root): |
| 225 |
raise RuntimeError( |
| 226 |
f"Refusing unsafe release archive path: {member.name}" |
| 227 |
) |
| 228 |
if "filter" in inspect.signature(archive.extractall).parameters: |
| 229 |
archive.extractall(output_path, filter="fully_trusted") |
| 230 |
else: |
| 231 |
archive.extractall(output_path) |
| 232 |
|
| 233 |
|
| 234 |
def download_tartanair_v2( |
| 235 |
data_path: Path, |
| 236 |
categories: list[str] | None = None, |
| 237 |
scenes: list[str] | None = None, |
| 238 |
) -> None: |
| 239 |
manifest = load_manifest() |
| 240 |
categories = categories or [] |
| 241 |
scenes = scenes or [] |
| 242 |
shards = scene_shards(manifest) |
| 243 |
selected_shards = [] |
| 244 |
for index, shard_scenes in enumerate(shards): |
| 245 |
if any( |
| 246 |
(not categories or scene.category in categories) |
| 247 |
and (not scenes or scene.name in scenes) |
| 248 |
for scene in shard_scenes |
| 249 |
): |
| 250 |
selected_shards.append(index) |
| 251 |
|
| 252 |
if not selected_shards: |
| 253 |
pycolmap.logging.warning("No TartanAir V2 scenes matched the filters") |
| 254 |
return |
| 255 |
|
| 256 |
data_path.mkdir(parents=True, exist_ok=True) |
| 257 |
archive_path = data_path / ".archives" |
| 258 |
archive_path.mkdir(exist_ok=True) |
| 259 |
release = manifest["release"] |
| 260 |
base_url = ( |
| 261 |
f"https://github.com/{release['repository']}/releases/download/" |
| 262 |
f"{release['tag']}" |
| 263 |
) |
| 264 |
checksum_path = MANIFEST_PATH.with_name("tartanair_v2_checksums.json") |
| 265 |
checksums = json.loads(checksum_path.read_text()) |
| 266 |
for index in selected_shards: |
| 267 |
filename = shard_name(manifest, index) |
| 268 |
target = archive_path / filename |
| 269 |
expected = checksums.get(filename) |
| 270 |
if expected is None: |
| 271 |
raise RuntimeError(f"Missing release checksum for {filename}") |
| 272 |
if not target.exists() or (expected and _sha256(target) != expected): |
| 273 |
temporary = target.with_suffix(target.suffix + ".part") |
| 274 |
if temporary.exists(): |
| 275 |
temporary.unlink() |
| 276 |
pycolmap.logging.info(f"Downloading TartanAir V2 {filename}") |
| 277 |
download_file(f"{base_url}/{filename}", archive_path) |
| 278 |
downloaded = archive_path / filename |
| 279 |
if downloaded != temporary: |
| 280 |
downloaded.replace(temporary) |
| 281 |
if expected and _sha256(temporary) != expected: |
| 282 |
temporary.unlink() |
| 283 |
raise RuntimeError(f"Checksum mismatch for {filename}") |
| 284 |
temporary.replace(target) |
| 285 |
pycolmap.logging.info(f"Extracting TartanAir V2 {filename}") |
| 286 |
_extract_tar_safely(target, data_path) |
| 287 |
|
| 288 |
|
| 289 |
DOWNLOADERS = { |
| 290 |
"eth3d": download_eth3d, |
| 291 |
"imc2023": download_imc2023, |
| 292 |
"imc2024": download_imc2024, |
| 293 |
"imc2025": download_imc2025, |
| 294 |
"blended-mvs": download_blended_mvs, |
| 295 |
"tartanair-v2": download_tartanair_v2, |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
def parse_args() -> argparse.Namespace: |
| 300 |
parser = argparse.ArgumentParser() |
| 301 |
parser.add_argument( |
| 302 |
"--data_path", type=Path, default=Path(__file__).parent / "data" |
| 303 |
) |
| 304 |
parser.add_argument( |
| 305 |
"--datasets", |
| 306 |
nargs="+", |
| 307 |
default=DOWNLOADERS.keys(), |
| 308 |
choices=DOWNLOADERS.keys(), |
| 309 |
) |
| 310 |
parser.add_argument( |
| 311 |
"--categories", |
| 312 |
nargs="+", |
| 313 |
default=[], |
| 314 |
help="TartanAir categories to download; empty downloads all.", |
| 315 |
) |
| 316 |
parser.add_argument( |
| 317 |
"--scenes", |
| 318 |
nargs="+", |
| 319 |
default=[], |
| 320 |
help="TartanAir scenes to download; empty downloads all.", |
| 321 |
) |
| 322 |
return parser.parse_args() |
| 323 |
|
| 324 |
|
| 325 |
def main() -> None: |
| 326 |
args = parse_args() |
| 327 |
|
| 328 |
for dataset in args.datasets: |
| 329 |
if dataset == "tartanair-v2": |
| 330 |
download_tartanair_v2( |
| 331 |
args.data_path / dataset, args.categories, args.scenes |
| 332 |
) |
| 333 |
else: |
| 334 |
DOWNLOADERS[dataset](args.data_path / dataset) |
| 335 |
|
| 336 |
|
| 337 |
if __name__ == "__main__": |
| 338 |
main() |