runnable quick-start example
Browse files- example_sr_op.py +100 -0
example_sr_op.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
Genesis quick start — super-resolution + outpainting on a live Esri tile.
|
| 4 |
+
|
| 5 |
+
Downloads the main JiT-B/16 checkpoints from https://huggingface.co/MVRL/genesis,
|
| 6 |
+
fetches one real Esri World Imagery tile (Sydney Opera House / Circular Quay,
|
| 7 |
+
zoom 16), then:
|
| 8 |
+
|
| 9 |
+
1. SR: the 256x256 parent tile (z16) -> 512x512 mosaic of its four z17
|
| 10 |
+
children in a single forward pass -> genesis_sr_out.png
|
| 11 |
+
2. OP: the tile viewed as a 2x2 quadrant mosaic (each quadrant is the
|
| 12 |
+
footprint of one real z17 child); keep the upper-left quadrant real,
|
| 13 |
+
outpaint the other three -> genesis_op_out.png
|
| 14 |
+
|
| 15 |
+
Run from the root of a https://github.com/subash-khanal/genesis checkout,
|
| 16 |
+
with its environment active (`uv sync && source .venv/bin/activate`):
|
| 17 |
+
|
| 18 |
+
python example_sr_op.py
|
| 19 |
+
|
| 20 |
+
Sampling uses the paper eval setting: 50 steps, cfg = 1.0 (the loader
|
| 21 |
+
defaults). EMA weights are loaded automatically.
|
| 22 |
+
|
| 23 |
+
Imagery: Esri World Imagery — Source: Esri, Maxar, Earthstar Geographics,
|
| 24 |
+
and the GIS User Community.
|
| 25 |
+
"""
|
| 26 |
+
import os
|
| 27 |
+
import sys
|
| 28 |
+
|
| 29 |
+
# Repo root: this file at the genesis repo root, or run from a checkout.
|
| 30 |
+
REPO = os.path.dirname(os.path.abspath(__file__))
|
| 31 |
+
if not os.path.isdir(os.path.join(REPO, "src")):
|
| 32 |
+
REPO = os.getcwd()
|
| 33 |
+
assert os.path.isdir(os.path.join(REPO, "src")), \
|
| 34 |
+
"Run this script from the root of a genesis checkout (github.com/subash-khanal/genesis)."
|
| 35 |
+
|
| 36 |
+
# Same sys.path recipe as demos/*.py — demos/ goes LAST (highest priority)
|
| 37 |
+
# so its `utils/` package is not shadowed by src/*/utils.py.
|
| 38 |
+
sys.path.insert(0, os.path.join(REPO, "src"))
|
| 39 |
+
sys.path.insert(0, os.path.join(REPO, "demos"))
|
| 40 |
+
|
| 41 |
+
import torch
|
| 42 |
+
from huggingface_hub import hf_hub_download
|
| 43 |
+
from PIL import Image
|
| 44 |
+
|
| 45 |
+
from utils.genesis_common import ( # demos/utils/genesis_common.py
|
| 46 |
+
fetch_tile_at_zoom,
|
| 47 |
+
inference_device,
|
| 48 |
+
load_op_denoiser,
|
| 49 |
+
load_sr_denoiser,
|
| 50 |
+
outpaint_context_white_holes_preview,
|
| 51 |
+
run_outpainting,
|
| 52 |
+
run_superresolution,
|
| 53 |
+
)
|
| 54 |
+
|
| 55 |
+
HF_REPO = "MVRL/genesis"
|
| 56 |
+
ARCH = "JiT-B/16" # main B/16 checkpoints (H/16 SR alone is 23 GB)
|
| 57 |
+
|
| 58 |
+
# Fixed scenic spot: Sydney Opera House / Circular Quay waterfront.
|
| 59 |
+
LAT, LON, ZOOM = -33.8568, 151.2153, 16 # training zoom levels are 10..19
|
| 60 |
+
|
| 61 |
+
|
| 62 |
+
def main() -> None:
|
| 63 |
+
device = inference_device() # cuda if available, else cpu
|
| 64 |
+
print(f"Device: {device}")
|
| 65 |
+
|
| 66 |
+
# 1. Checkpoints from the HF Hub (cached under ~/.cache/huggingface).
|
| 67 |
+
# The main SR model is DINOv3-conditioned; the main OP model is no-DINO.
|
| 68 |
+
sr_ckpt = hf_hub_download(HF_REPO, "main/superresolution/main_B16/sr-full-tile-stage3-step0800000.ckpt")
|
| 69 |
+
op_ckpt = hf_hub_download(HF_REPO, "main/outpainting/main_B16/op-new-stage3-step0800000.ckpt")
|
| 70 |
+
dino = hf_hub_download(HF_REPO, "dinov3_vitl16_pretrain_sat493m-eadcf0ff.pth")
|
| 71 |
+
|
| 72 |
+
# 2. One real 256x256 Esri World Imagery tile as the input.
|
| 73 |
+
tile = fetch_tile_at_zoom(LON, LAT, ZOOM) # PIL RGB, 256x256
|
| 74 |
+
tile.save("genesis_sr_in.png")
|
| 75 |
+
|
| 76 |
+
# 3. Super-resolution: z16 parent -> 512x512 mosaic of its 4 z17 children.
|
| 77 |
+
# target_zoom is the CHILD zoom level (feeds the model's GSD embedder).
|
| 78 |
+
sr_model, _ = load_sr_denoiser(sr_ckpt, ARCH, dino_weights=dino, device=device)
|
| 79 |
+
sr_out = run_superresolution(sr_model, tile, device, target_zoom=ZOOM + 1)
|
| 80 |
+
sr_out.save("genesis_sr_out.png")
|
| 81 |
+
print("Saved genesis_sr_in.png (256 parent) and genesis_sr_out.png (512 SR mosaic)")
|
| 82 |
+
|
| 83 |
+
del sr_model # free memory before loading the OP model
|
| 84 |
+
if device.type == "cuda":
|
| 85 |
+
torch.cuda.empty_cache()
|
| 86 |
+
|
| 87 |
+
# 4. Outpainting: 2x2 quadrant mosaic — 1 real quadrant, 3 masked holes.
|
| 88 |
+
# Mask convention: white (255) = hole to generate, black (0) = known.
|
| 89 |
+
mask = Image.new("L", (256, 256), 255) # everything is a hole ...
|
| 90 |
+
mask.paste(0, (0, 0, 128, 128)) # ... except the upper-left quadrant
|
| 91 |
+
|
| 92 |
+
op_model, _ = load_op_denoiser(op_ckpt, ARCH, dino_weights="", device=device)
|
| 93 |
+
outpaint_context_white_holes_preview(tile, mask).save("genesis_op_in.png")
|
| 94 |
+
op_out = run_outpainting(op_model, tile, mask, device, tile_zoom=ZOOM)
|
| 95 |
+
op_out.save("genesis_op_out.png")
|
| 96 |
+
print("Saved genesis_op_in.png (masked input) and genesis_op_out.png (outpainted)")
|
| 97 |
+
|
| 98 |
+
|
| 99 |
+
if __name__ == "__main__":
|
| 100 |
+
main()
|