File size: 23,285 Bytes
0a82b18 5b3b1fd 0a82b18 5b3b1fd 0a82b18 09a106d 0a82b18 b345965 0a82b18 09a106d 0a82b18 5b3b1fd 0a82b18 5b3b1fd 0a82b18 5b3b1fd 0a82b18 5b3b1fd 0a82b18 5b3b1fd 0a82b18 5b3b1fd 0a82b18 |
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 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 |
import gradio as gr
import torch
import faiss
import numpy as np
import pandas as pd
import folium
from PIL import Image
from pathlib import Path
import torchvision.transforms as tfm
from torchvision.transforms import functional as F
import logging
import sys
import io
import base64
import random
import ast
import webdataset as wds
import os
import pickle
from functools import lru_cache
import tarfile
from huggingface_hub import hf_hub_download, snapshot_download
from models.apl_model_dinov2 import DINOv2FeatureExtractor
sys.path.append(str(Path("image-matching-models")))
sys.path.append(str(Path("image-matching-models/matching/third_party")))
import util_matching
from matching import get_matcher
logging.basicConfig(level=logging.INFO)
HF_TOKEN = os.getenv("HF_TOKEN") # Set this as a secret in your Space
def ensure_files_exist():
Path("./faiss_index").mkdir(exist_ok=True)
Path("./data/webdataset_shards").mkdir(parents=True, exist_ok=True)
"""Check for required files and download if missing"""
# 1. Check FAISS index
if not Path("faiss_index/faiss_index_2021.bin").exists():
print("Downloading FAISS index...")
hf_hub_download(
repo_id='pawlo2013/EarthLoc2_FAISS',
filename="faiss_index.bin",
local_dir="./faiss_index",
token=HF_TOKEN,
repo_type="dataset"
)
# 2. Check WebDataset shards
shard_dir = Path("data/webdataset_shards")
required_shards = [f"shard-{i:06d}.tar" for i in range(11)] # Adjust range as needed
required_indices = [f"{s}.index" for s in required_shards]
missing_files = [
f for f in required_shards + required_indices
if not (shard_dir / f).exists()
]
if missing_files:
print(f"Downloading {len(missing_files)} missing shard files...")
snapshot_download(
repo_id="pawlo2013/EarthLoc_2021_Database",
local_dir=shard_dir,
allow_patterns="*.tar*", # Gets both .tar and .tar.index
token=HF_TOKEN,
repo_type="dataset"
)
# --- Integration Point ---
# Call this BEFORE loading any models or datasets
ensure_files_exist()
# --- Paths and device ---
MODEL_CHECKPOINT_PATH = Path("weights/best_model_95.6.torch")
FAISS_INDEX_PATH = Path("faiss_index/faiss_index.bin")
CSV_MAPPING_PATH = Path("faiss_index/faiss_index_webdataset.csv") # Updated CSV with shards
DEVICE = "cpu"
MATCHING_IMG_SIZE = 512
logging.info(f"Using device: {DEVICE}")
for path, desc in [
(MODEL_CHECKPOINT_PATH, "Model checkpoint"),
(FAISS_INDEX_PATH, "FAISS index"),
(CSV_MAPPING_PATH, "Path mapping CSV"),
]:
if not path.exists():
raise FileNotFoundError(f"{desc} not found at: {path}")
MODEL_NAME = "xfeat_steerers"
matcher = get_matcher(MODEL_NAME, device=DEVICE, max_num_keypoints=2048)
if MODEL_NAME == "xfeat_steerers":
matcher.model.dev = DEVICE
# Load mapping CSV with keys and shard paths
mapping_df = pd.read_csv(CSV_MAPPING_PATH, index_col="faiss_index")
parsed = mapping_df["key"].str.extract(r"@(?P<z>\d{1,2})_(?P<r>\d{1,5})_(?P<c>\d{1,5})@").astype("int32")
mapping_df = mapping_df.join(parsed)
logging.info(f"Loaded mapping CSV with {len(mapping_df)} entries.")
# Cache for opened shards: {shard_path: WebDataset object}
shard_cache = {}
def get_shard_dataset(shard_path):
"""Load or get cached WebDataset for a shard path."""
if shard_path not in shard_cache:
shard_cache[shard_path] = wds.WebDataset(shard_path, handler=wds.warn_and_continue)
return shard_cache[shard_path]
@lru_cache(maxsize=100)
def load_index(index_path):
with open(index_path, "rb") as f:
return pickle.load(f)
def load_image_from_shard(key):
row = mapping_df[mapping_df["key"] == key]
if row.empty:
return None
shard_path = row.iloc[0]["shard_path"]
index_path = shard_path + ".index"
if not os.path.exists(index_path):
return _load_via_linear_scan(shard_path, key) # Fallback
try:
index = load_index(index_path)
offset = index.get(key)
if offset is None:
return None
with open(shard_path, "rb") as f:
f.seek(offset)
with tarfile.open(fileobj=f) as tar:
member = tar.next()
if member and member.name.startswith(key):
jpg_file = tar.extractfile(member)
return Image.open(io.BytesIO(jpg_file.read())).convert("RGB")
return None
except Exception as e:
logging.error(f"Error loading {key}: {str(e)}")
return _load_via_linear_scan(shard_path, key) # Fallback on error
# Fallback linear scan (original method)
def _load_via_linear_scan(shard_path, key):
dataset = get_shard_dataset(shard_path)
for sample in dataset:
if sample["__key__"] == key:
if img_bytes := sample.get("jpg"):
return Image.open(io.BytesIO(img_bytes)).convert("RGB")
return None
def pil_to_base64(image):
"""Convert a PIL image to a base64-encoded string for HTML embedding."""
buffered = io.BytesIO()
image.save(buffered, format="PNG")
img_str = base64.b64encode(buffered.getvalue()).decode("utf-8")
return f"data:image/png;base64,{img_str}"
def create_map(
final_footprint, candidate_num, filename, inliers, query_footprint=None
):
"""
Create and return a Folium map (as HTML string) showing the final footprint (blue)
and optionally the query's ground truth footprint (orange).
"""
if final_footprint:
center = final_footprint[0]
zoom = 10
elif query_footprint:
center = query_footprint[0]
zoom = 10
else:
center = [0, 0]
zoom = 2
m = folium.Map(location=center, zoom_start=zoom)
if query_footprint:
folium.Polygon(
locations=query_footprint,
popup="Ground Truth Query Footprint",
color="orange",
fill=True,
fill_color="orange",
fill_opacity=0.4,
).add_to(m)
if final_footprint:
footprint_text = "\n".join(
[f"({lat:.4f}, {lon:.4f})" for lat, lon in final_footprint]
)
popup_text = (
f"Predicted Footprint:<br>{footprint_text}<br><br>"
f"Candidate: {candidate_num}<br>Inliers: {inliers}"
)
folium.Polygon(
locations=final_footprint,
popup=popup_text,
color="blue",
fill=True,
fill_color="cyan",
fill_opacity=0.5,
).add_to(m)
folium.Marker(
location=[final_footprint[0][0], final_footprint[0][1]],
popup=f"Footprint Coordinates:<br>{footprint_text}",
icon=folium.Icon(color="blue"),
).add_to(m)
elif not query_footprint:
folium.Marker(
location=[0, 0],
popup="No valid location found.",
icon=folium.Icon(color="red"),
).add_to(m)
return m._repr_html_()
def parse_zoom_row_col_from_key(key: str):
"""
Extract zoom, row, col from the complex key string.
The key format is like:
2021_10_30_90_@34,30714@92,81250@35,46067@92,81250@35,46067@94,21875@34,30714@94,21875@10_0404_0776@2021@34,88391@93,51562@16489@0@
The zoom, row, col are expected to be in the last underscore-separated fields,
specifically the last three fields before the final '@' or at the end.
This function tries to extract zoom, row, col as integers.
"""
try:
# Split by underscore
parts = util_matching.get_image_metadata_from_path(key)
image_id_str = parts[9] # 9th field: image_id
parts = image_id_str.split("_")
zoom = int(parts[0])
row = int(parts[1])
col = int(parts[2])
return zoom, row, col
except Exception as e:
raise ValueError(f"Failed to parse zoom,row,col from key: {key}") from e
def get_surrounding_tiles_sharded(candidate_key, zoom):
"""
Given a candidate key, find all keys in mapping_df with the same zoom,
and row/col within Β±4 offsets, then load images from shards.
Return list of (img, (row, col), key) sorted by row, col.
"""
try:
zoom, row, col = parse_zoom_row_col_from_key(candidate_key)
except Exception as e:
logging.warning(f"Failed to parse candidate key {candidate_key}: {e}")
return []
row_offsets = [-4, 0, 4]
col_offsets = [-4, 0, 4]
desired_rows = {row + r for r in row_offsets}
desired_cols = {col + c for c in col_offsets}
# ββ 2. Vectorised filter ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
mask = (
(mapping_df["z"] == zoom) &
(mapping_df["r"].isin(desired_rows)) &
(mapping_df["c"].isin(desired_cols))
)
matched_rows = mapping_df[mask]
tiles = []
seen_positions = set() # Track (row, col) to avoid duplicates
for _, row_data in matched_rows.iterrows():
k = row_data["key"]
try:
_, r, c = parse_zoom_row_col_from_key(k)
except Exception:
continue
if (r, c) in seen_positions:
continue # Skip duplicate position
img = load_image_from_shard(k)
if img is not None:
tiles.append((img, (r, c), k))
seen_positions.add((r, c))
tiles.sort(key=lambda t: (t[1][0], t[1][1]))
return tiles
def compose_tiles_ordered_sharded(tiles, tile_size, candidate_indices):
"""
Compose a 3x3 grid image from tiles loaded from shards.
Missing tiles replaced with blank.
"""
candidate_row, candidate_col = candidate_indices
grid_img = Image.new("RGB", (tile_size[0] * 3, tile_size[1] * 3))
blank = Image.new("RGB", tile_size, color=(0, 0, 0))
tile_dict = {(rc[0], rc[1]): img for img, rc, key in tiles if img is not None}
for i, row_offset in enumerate([-4, 0, 4]):
for j, col_offset in enumerate([-4, 0, 4]):
desired_row = candidate_row + row_offset
desired_col = candidate_col + col_offset
img = tile_dict.get((desired_row, desired_col), blank)
if img.mode != "RGB":
img = img.convert("RGB")
img_resized = tfm.Resize(tile_size, antialias=True)(img).copy()
grid_img.paste(img_resized, (j * tile_size[0], i * tile_size[1]))
return grid_img
def run_matching(query_image, candidate_image, base_footprint):
local_fm = None
viz_params = None
for iteration in range(4):
(
num_inliers,
local_fm,
predicted_footprint,
pretty_footprint,
) = util_matching.estimate_footprint(
local_fm,
query_image,
candidate_image,
matcher,
base_footprint,
HW=MATCHING_IMG_SIZE,
viz_params=viz_params,
)
if num_inliers == -1 or num_inliers is None:
return -1, []
if hasattr(predicted_footprint, "tolist"):
best_footprint = predicted_footprint.tolist()
else:
best_footprint = predicted_footprint
return num_inliers, best_footprint
# --- Load assets ---
logging.info("Loading assets. This may take a moment...")
try:
model = DINOv2FeatureExtractor(
model_type="vit_base_patch14_reg4_dinov2.lvd142m",
num_of_layers_to_unfreeze=0,
desc_dim=768,
aggregator_type="SALAD",
)
logging.info(f"Loading model checkpoint from {MODEL_CHECKPOINT_PATH}...")
model_state_dict = torch.load(MODEL_CHECKPOINT_PATH, map_location=DEVICE)
model.load_state_dict(model_state_dict)
model = model.to(DEVICE)
model.eval()
logging.info("DINOv2 model and checkpoint loaded successfully.")
except Exception as e:
logging.error(f"Failed to load the model: {e}")
raise
faiss_index = faiss.read_index(str(FAISS_INDEX_PATH))
num_db_images = faiss_index.ntotal // 4
logging.info(
f"FAISS index loaded. Contains {faiss_index.ntotal} vectors for {num_db_images} unique images."
)
image_transform = tfm.Compose(
[
tfm.Resize((model.image_size, model.image_size), antialias=True),
tfm.ToTensor(),
tfm.Normalize(
mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]
),
]
)
logging.info("Assets loaded. Gradio app is ready.")
# --- Core app logic ---
def search_and_retrieve(
query_image: Image.Image, query_footprint_str: str, num_results: int
):
progress = gr.Progress()
query_footprint = None
if query_footprint_str:
try:
print(query_footprint_str)
query_footprint = ast.literal_eval(query_footprint_str)
query_footprint = [list(coord) for coord in query_footprint]
except (ValueError, SyntaxError):
logging.warning("Could not parse query footprint string.")
query_footprint = None
if query_image is None:
yield create_map(None, None, None, None), None
return
progress(0.1, desc="Preprocessing query")
if query_image.mode == "RGBA":
query_image = query_image.convert("RGB")
image_tensor = image_transform(query_image).to(DEVICE)
with torch.no_grad():
descriptor = model(image_tensor.unsqueeze(0))
descriptor_np = descriptor.cpu().numpy()
progress(0.2, desc=f"Searching database for {num_results} neighbors")
distances, indices = faiss_index.search(descriptor_np, num_results)
flat_indices = indices.flatten()
global_best_inliers = -1
global_best_footprint = None
global_candidate_num = None
global_filename = None
global_best_display_image = None
candidate_infos = []
processed_image_indices = set()
query_tensor = tfm.ToTensor()(
tfm.Resize((MATCHING_IMG_SIZE, MATCHING_IMG_SIZE), antialias=True)(query_image)
)
progress(0.4, desc="Processing candidates")
for faiss_idx in flat_indices:
image_index = faiss_idx % num_db_images
best_rotation_index = faiss_idx // num_db_images
query_tensor = F.rotate(query_tensor, [0, -90, -180, -270][best_rotation_index] )
if image_index in processed_image_indices:
continue
processed_image_indices.add(image_index)
candidate_num = len(candidate_infos) + 1
try:
candidate_row = mapping_df.loc[int(image_index)]
except Exception as e:
logging.warning(f"Failed to get candidate info for index {image_index}: {e}")
continue
candidate_key = candidate_row["key"]
candidate_path_str = candidate_row["local_path"]
shard_path = candidate_row['shard_path']
base_footprint = util_matching.path_to_footprint(Path(candidate_path_str))
try:
parts = util_matching.get_image_metadata_from_path(candidate_key)
image_id_str = parts[9] # 9th field: image_id
parts = image_id_str.split("_")
zoom = int(parts[0])
candidate_row_idx = int(parts[1])
candidate_col_idx = int(parts[2])
except Exception as e:
logging.warning(f"Failed to parse candidate key {candidate_key}: {e}")
continue
debug_dir = Path("debug_tiles")
debug_dir.mkdir(exist_ok=True)
tiles = get_surrounding_tiles_sharded(candidate_key, zoom)
composite_img = compose_tiles_ordered_sharded(
tiles, (1024, 1024), (candidate_row_idx, candidate_col_idx)
)
display_img = F.rotate(
composite_img, [0, 90, 180, 270][best_rotation_index]
)
candidate_img_tensor = tfm.ToTensor()(composite_img)
candidate_img_tensor = tfm.Resize(
(MATCHING_IMG_SIZE * 3, MATCHING_IMG_SIZE * 3), antialias=True
)(candidate_img_tensor)
candidate_img_tensor = candidate_img_tensor.to(DEVICE)
progress(
0.5 + len(candidate_infos) / num_results * 0.4,
desc=f"Running matching for candidate {candidate_num}",
)
best_inliers, best_footprint = run_matching(
query_tensor, candidate_img_tensor, base_footprint
)
if best_inliers > -1:
candidate_infos.append(
{
"candidate_num": candidate_num,
"filename": Path(candidate_path_str).name,
"inliers": best_inliers,
"display_image": display_img,
"footprint": best_footprint,
}
)
if best_inliers > global_best_inliers:
global_best_inliers = best_inliers
global_best_footprint = best_footprint
global_candidate_num = candidate_num
global_filename = Path(candidate_path_str).name
global_best_display_image = display_img
progress(0.9, desc="Finalizing results")
folium_map_html = create_map(
global_best_footprint,
global_candidate_num,
global_filename,
global_best_inliers,
query_footprint=query_footprint,
)
progress(1, desc="Done")
yield folium_map_html, None
yield folium_map_html, global_best_display_image
# --- Gradio app setup ---
if __name__ == "__main__":
example_list = []
google_examples = []
queries_folder = Path("./data/queries")
if queries_folder.exists() and queries_folder.is_dir():
image_extensions = ["*.jpg", "*.jpeg", "*.png"]
image_files = []
for ext in image_extensions:
image_files.extend(queries_folder.glob(ext))
if image_files:
num_examples = min(10, len(image_files))
random_examples = random.sample(image_files, num_examples)
example_list = [
[str(p), str(util_matching.get_footprint_from_path(p))]
for p in random_examples
]
logging.info(
f"Loaded {len(example_list)} examples for Gradio with footprints."
)
else:
logging.warning(
f"No images found in the examples folder: {queries_folder}"
)
else:
logging.warning(f"Examples folder not found: {queries_folder}")
google_folder = Path("./data/google_maps_queries")
if google_folder.exists() and google_folder.is_dir():
image_extensions = ["*.jpg", "*.jpeg", "*.png"]
google_files = []
for ext in image_extensions:
google_files.extend(google_folder.glob(ext))
if google_files:
num_google = min(10, len(google_files))
google_examples = [
[str(p), str(p.stem).split("_")[0]] # Empty footprint for Google Maps
for p in random.sample(google_files, num_google)
]
model_description = """
## Model Details
This is a public API for inference of the EarthLoc2 model, which implements the amazing works of:
- EarthLoc (https://earthloc-and-earthmatch.github.io/)
- EarthMatch (https://earthloc-and-earthmatch.github.io/)
- AstroLoc (https://astro-loc.github.io/)
### Architecture
- DINOv2 base with SALAD aggregator out dim = 3072
- FAISS index ~ 8gb, indexes 161496 * 4 images (4 rotated versions) from 2021
### Training
- Trained on the original EarthLoc dataset (zooms 9,10,11), in range -60,60 latitude, polar regions not supported
- Training included additional queries which were not part of the test/val sets
- 5000 iterations with a batch size of 96
### Performance
- Achieves R@10 = 90.6 on the original EarthLoc test and val sets (when retrieving against whole db as is)
- Overall performance is around 10% worse than AstroLoc (https://9d214e4bc329a5c3f9.gradio.live/)
- Works well on satelite images between 1000 sq.km and 50000 sq.km, smaller or higher areas will not produce good results.
### Matching
- Uses the Xfeat_steerers matcher with 2048 maximal number of keypoints, we recommend Master with 2048 if you have access to GPU (we are too poor for it).
"""
theme = gr.themes.Soft(
primary_hue=gr.themes.colors.blue,
font=gr.themes.GoogleFont("Inter"),
).set(
button_primary_background_fill="*primary_900",
button_primary_background_fill_hover="*primary_700",
)
with gr.Blocks(theme=theme) as demo:
gr.Markdown("# Aerial Photography Locator ")
with gr.Row():
with gr.Column(scale=2):
image_input = gr.Image(
type="pil",
label="Aerial Photos of Earth",
height=400,
)
hidden_footprint_text = gr.Textbox(
visible=False, label="Query Footprint"
)
slider = gr.Slider(
minimum=1,
maximum=20,
step=1,
value=10,
label="Number of Candidates to Process",
info=(
"The higher this number to more likely the model is to find a match, "
"however it takes longer to find it. Expect around 5 second more compute per candidate."
),
)
submit_btn = gr.Button("Localize Image", variant="primary")
with gr.Row():
with gr.Column():
if example_list:
gr.Markdown("### ISS Example Queries")
gr.Examples(
examples=example_list,
inputs=[image_input, hidden_footprint_text],
examples_per_page=5,
cache_examples=False,
)
with gr.Column():
if google_examples:
gr.Markdown("### Google Maps Example Queries")
gr.Examples(
examples=google_examples,
inputs=[image_input, hidden_footprint_text],
examples_per_page=5,
cache_examples=False,
)
with gr.Column(scale=2):
map_output = gr.HTML(label="Final Footprint Map")
image_output = gr.Image(
type="pil",
label="Best Matching Candidate",
height=400,
show_download_button=True,
)
gr.Markdown(model_description)
submit_btn.click(
fn=search_and_retrieve,
inputs=[image_input, hidden_footprint_text, slider],
outputs=[map_output, image_output],
)
demo.launch(share=True) |