File size: 14,478 Bytes
7870cc2 20dc17e 7870cc2 e0d9754 7870cc2 | 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 | """Cache utilities for precomputed pipeline results.
This module provides functions for loading and saving cached pipeline results,
enabling instant display of results for example images without running the
expensive pipeline (GDINO+SAM segmentation, feature extraction, matching) on CPU.
Cache Structure (v2.0 - supports filtering):
cached_results/
βββ {image_stem}_{extractor}/
β βββ predictions.json # ALL matches with location/body_part
β βββ segmentation.png # Segmentation visualization
β βββ cropped.png # Cropped snow leopard image
β βββ keypoints.png # Extracted keypoints visualization
β βββ pairwise/
β βββ {catalog_id}.npz # NPZ data for ALL matches
β βββ ... # (visualizations generated on-demand)
"""
import copy
import json
import logging
from pathlib import Path
import numpy as np
from PIL import Image
from snowleopard_reid.visualization import (
draw_matched_keypoints,
draw_side_by_side_comparison,
)
logger = logging.getLogger(__name__)
# Cache directory - use absolute path based on module location
# This ensures consistent path resolution regardless of cwd
PROJECT_ROOT = Path(__file__).parent.parent.parent
CACHE_DIR = PROJECT_ROOT / "cached_results"
def get_cache_key(image_path: Path | str, extractor: str) -> str:
"""Generate cache key from image stem and extractor.
Args:
image_path: Path to the query image
extractor: Feature extractor name (e.g., 'sift', 'superpoint')
Returns:
Cache key string in format "{image_stem}_{extractor}"
"""
image_path = Path(image_path)
return f"{image_path.stem}_{extractor}"
def get_cache_dir(image_path: Path | str, extractor: str) -> Path:
"""Get cache directory for an image/extractor combination.
Args:
image_path: Path to the query image
extractor: Feature extractor name
Returns:
Path to the cache directory
"""
return CACHE_DIR / get_cache_key(image_path, extractor)
def is_cached(image_path: Path | str, extractor: str) -> bool:
"""Check if results are cached for this image/extractor combination.
Args:
image_path: Path to the query image
extractor: Feature extractor name
Returns:
True if all required cache files exist
"""
cache_dir = get_cache_dir(image_path, extractor)
predictions_file = cache_dir / "predictions.json"
if not predictions_file.exists():
return False
# Check for required visualization files
required_files = [
"segmentation.png",
"cropped.png",
"keypoints.png",
]
for filename in required_files:
if not (cache_dir / filename).exists():
return False
return True
def load_cached_results(image_path: Path | str, extractor: str) -> dict:
"""Load all cached results for an image/extractor combination.
Args:
image_path: Path to the query image
extractor: Feature extractor name
Returns:
Dictionary containing:
- predictions: Full pipeline predictions dict
- segmentation_image: PIL Image of segmentation overlay
- cropped_image: PIL Image of cropped snow leopard
- keypoints_image: PIL Image of extracted keypoints
- pairwise_dir: Path to directory with match visualizations
Raises:
FileNotFoundError: If cache files don't exist
"""
cache_dir = get_cache_dir(image_path, extractor)
if not cache_dir.exists():
raise FileNotFoundError(f"Cache directory not found: {cache_dir}")
predictions_file = cache_dir / "predictions.json"
if not predictions_file.exists():
raise FileNotFoundError(f"Predictions file not found: {predictions_file}")
# Load predictions JSON
with open(predictions_file) as f:
predictions = json.load(f)
# Load visualization images
segmentation_image = Image.open(cache_dir / "segmentation.png")
cropped_image = Image.open(cache_dir / "cropped.png")
keypoints_image = Image.open(cache_dir / "keypoints.png")
return {
"predictions": predictions,
"segmentation_image": segmentation_image,
"cropped_image": cropped_image,
"keypoints_image": keypoints_image,
"pairwise_dir": cache_dir / "pairwise",
}
def load_cached_match_visualizations(
pairwise_dir: Path,
matches: list[dict],
) -> tuple[dict, dict]:
"""Load cached match and clean comparison visualizations.
Args:
pairwise_dir: Path to pairwise visualizations directory
matches: List of match dictionaries with rank and catalog_id
Returns:
Tuple of (match_visualizations, clean_comparison_visualizations)
Both are dicts mapping rank -> PIL Image
"""
match_visualizations = {}
clean_comparison_visualizations = {}
for match in matches:
rank = match["rank"]
catalog_id = match["catalog_id"]
# Load match visualization
match_path = pairwise_dir / f"rank_{rank:02d}_{catalog_id}_match.png"
if match_path.exists():
match_visualizations[rank] = Image.open(match_path)
# Load clean comparison visualization
clean_path = pairwise_dir / f"rank_{rank:02d}_{catalog_id}_clean.png"
if clean_path.exists():
clean_comparison_visualizations[rank] = Image.open(clean_path)
return match_visualizations, clean_comparison_visualizations
def save_cache_results(
image_path: Path | str,
extractor: str,
predictions: dict,
segmentation_image: Image.Image,
cropped_image: Image.Image,
keypoints_image: Image.Image,
match_visualizations: dict[int, Image.Image],
clean_comparison_visualizations: dict[int, Image.Image],
matches: list[dict],
) -> Path:
"""Save pipeline results to cache.
Args:
image_path: Path to the original query image
extractor: Feature extractor name
predictions: Full pipeline predictions dictionary
segmentation_image: PIL Image of segmentation overlay
cropped_image: PIL Image of cropped snow leopard
keypoints_image: PIL Image of extracted keypoints
match_visualizations: Dict mapping rank -> match visualization PIL Image
clean_comparison_visualizations: Dict mapping rank -> clean comparison PIL Image
matches: List of match dictionaries with rank and catalog_id
Returns:
Path to the cache directory
"""
cache_dir = get_cache_dir(image_path, extractor)
cache_dir.mkdir(parents=True, exist_ok=True)
# Save predictions JSON
predictions_file = cache_dir / "predictions.json"
with open(predictions_file, "w") as f:
json.dump(predictions, f, indent=2)
logger.info(f"Saved predictions: {predictions_file}")
# Save visualization images
segmentation_image.save(cache_dir / "segmentation.png")
cropped_image.save(cache_dir / "cropped.png")
keypoints_image.save(cache_dir / "keypoints.png")
logger.info(f"Saved visualization images to {cache_dir}")
# Save pairwise match visualizations
pairwise_dir = cache_dir / "pairwise"
pairwise_dir.mkdir(exist_ok=True)
for match in matches:
rank = match["rank"]
catalog_id = match["catalog_id"]
# Save match visualization
if rank in match_visualizations:
match_path = pairwise_dir / f"rank_{rank:02d}_{catalog_id}_match.png"
match_visualizations[rank].save(match_path)
# Save clean comparison visualization
if rank in clean_comparison_visualizations:
clean_path = pairwise_dir / f"rank_{rank:02d}_{catalog_id}_clean.png"
clean_comparison_visualizations[rank].save(clean_path)
logger.info(f"Saved {len(match_visualizations)} pairwise visualizations")
return cache_dir
def clear_cache(image_path: Path | str = None, extractor: str = None) -> None:
"""Clear cache directory.
Args:
image_path: If provided, only clear cache for this image
extractor: If provided with image_path, only clear specific cache
"""
import shutil
if image_path and extractor:
# Clear specific cache
cache_dir = get_cache_dir(image_path, extractor)
if cache_dir.exists():
shutil.rmtree(cache_dir)
logger.info(f"Cleared cache: {cache_dir}")
elif CACHE_DIR.exists():
# Clear all caches
shutil.rmtree(CACHE_DIR)
logger.info(f"Cleared all caches: {CACHE_DIR}")
def get_cache_summary() -> dict:
"""Get summary of cached results.
Returns:
Dictionary with cache statistics
"""
if not CACHE_DIR.exists():
return {"total_cached": 0, "total_size_mb": 0, "cached_items": []}
cached_items = []
total_size = 0
for cache_dir in CACHE_DIR.iterdir():
if cache_dir.is_dir():
# Calculate size
size = sum(f.stat().st_size for f in cache_dir.rglob("*") if f.is_file())
total_size += size
# Parse cache key
parts = cache_dir.name.rsplit("_", 1)
if len(parts) == 2:
image_stem, extractor = parts
else:
image_stem, extractor = cache_dir.name, "unknown"
cached_items.append({
"image_stem": image_stem,
"extractor": extractor,
"size_mb": size / (1024 * 1024),
"path": str(cache_dir),
})
return {
"total_cached": len(cached_items),
"total_size_mb": total_size / (1024 * 1024),
"cached_items": cached_items,
}
def filter_cached_matches(
all_matches: list[dict],
filter_locations: list[str] | None = None,
filter_body_parts: list[str] | None = None,
top_k: int = 5,
) -> list[dict]:
"""Filter cached matches by location/body_part and return top-k.
Args:
all_matches: List of all cached match dictionaries
filter_locations: List of locations to filter by (e.g., ["skycrest_valley"])
filter_body_parts: List of body parts to filter by (e.g., ["head", "right_flank"])
top_k: Number of top matches to return after filtering
Returns:
List of filtered and re-ranked match dictionaries
"""
# Make a deep copy to avoid modifying the original
filtered = [copy.deepcopy(m) for m in all_matches]
if filter_locations:
filtered = [m for m in filtered if m.get("location") in filter_locations]
if filter_body_parts:
filtered = [m for m in filtered if m.get("body_part") in filter_body_parts]
# Re-sort by wasserstein (descending - higher is better)
filtered = sorted(filtered, key=lambda x: x.get("wasserstein", 0), reverse=True)
# Re-assign ranks for the filtered top-k
for i, match in enumerate(filtered[:top_k]):
match["rank"] = i + 1
return filtered[:top_k]
def generate_visualizations_from_npz(
pairwise_dir: Path,
matches: list[dict],
cropped_image_path: Path | str,
) -> tuple[dict, dict]:
"""Generate match visualizations on-demand from cached NPZ data.
Args:
pairwise_dir: Path to directory containing NPZ pairwise data files
matches: List of filtered match dictionaries with catalog_id and filepath
cropped_image_path: Path to the cropped query image
Returns:
Tuple of (match_visualizations, clean_comparison_visualizations)
Both are dicts mapping rank -> PIL Image
"""
match_visualizations = {}
clean_comparison_visualizations = {}
cropped_image_path = Path(cropped_image_path)
for match in matches:
rank = match["rank"]
catalog_id = match["catalog_id"]
# Resolve relative path to absolute (handles both relative and absolute paths)
filepath = match["filepath"]
if not Path(filepath).is_absolute():
catalog_image_path = PROJECT_ROOT / filepath
else:
catalog_image_path = Path(filepath)
# Look for NPZ file by catalog_id
npz_path = pairwise_dir / f"{catalog_id}.npz"
if npz_path.exists():
try:
pairwise_data = np.load(npz_path, allow_pickle=True)
# Generate matched keypoints visualization
match_viz = draw_matched_keypoints(
query_image_path=cropped_image_path,
catalog_image_path=catalog_image_path,
query_keypoints=pairwise_data["query_keypoints"],
catalog_keypoints=pairwise_data["catalog_keypoints"],
match_scores=pairwise_data["match_scores"],
max_matches=100,
)
match_visualizations[rank] = match_viz
# Generate clean side-by-side comparison
clean_viz = draw_side_by_side_comparison(
query_image_path=cropped_image_path,
catalog_image_path=catalog_image_path,
)
clean_comparison_visualizations[rank] = clean_viz
except Exception as e:
logger.warning(
f"Failed to generate visualization for {catalog_id}: {e}"
)
else:
logger.warning(f"NPZ file not found for {catalog_id}: {npz_path}")
return match_visualizations, clean_comparison_visualizations
def extract_location_body_part_from_filepath(filepath: str) -> tuple[str, str]:
"""Extract location and body_part from catalog image filepath.
Expected filepath format:
.../database/{location}/{individual}/images/{body_part}/{filename}
Args:
filepath: Path to catalog image
Returns:
Tuple of (location, body_part)
"""
parts = Path(filepath).parts
# Find "database" in path and extract location (next part) and body_part
try:
db_idx = parts.index("database")
location = parts[db_idx + 1] if db_idx + 1 < len(parts) else "unknown"
# Find "images" in path and get body_part (next part)
img_idx = parts.index("images")
body_part = parts[img_idx + 1] if img_idx + 1 < len(parts) else "unknown"
return location, body_part
except (ValueError, IndexError):
return "unknown", "unknown"
|