Skip to content

Latest commit

 

History

History
377 lines (269 loc) · 11 KB

README.md

File metadata and controls

377 lines (269 loc) · 11 KB

Flow Matching for Probabilistic Image Geolocalization in Switzerland - CS503 Project

This repo contains the full geolocalization pipeline for the CS503 Visual Intelligence project - Flow Matching for Probabilistic Image Geolocalization in Switzerland. It is organized into the following parts:

  • deterministic-baseline/ — deterministic baselines used to benchmark and generate conditioning signals for the probabilistic model
  • Main pipeline (src/, scripts/, config/, tests/) — flow-based probabilistic geolocalization model conditioned on frozen StreetCLIP embeddings, with optional caption conditioning

Probabilistic Visual Geolocalization

The rest of this README focuses on the main probabilistic method based on flow matching.

What The Model Does

  1. build a deterministic Switzerland-only OSV5M subset
  2. cache frozen image embeddings
  3. optionally cache frozen text embeddings from precomputed captions
  4. train a selected flow model on either 3D sphere coordinates or a local Switzerland-centered 2D map
  5. sample many final locations per image
  6. summarize them with a mode, medoid, and heatmap
  7. evaluate and log everything in W&B

The geolocalization head is selected from the YAML config. The current baseline is simple_plonk_rfm_s2; the PLONK-compatible spherical baseline is plonk_rfm_s2; the local Switzerland-only Euclidean variant is plonk_local_fm_r2.

Repository Layout

  • src/visual_geolocation/: importable package with datasets, models, training, evaluation, inference, geospatial utilities, and W&B helpers
  • scripts/: local CLI wrappers and analysis utilities
  • scripts/slurm/: cluster launch scripts
  • config/experiments/: YAML experiment configs

One Way To Run Experiments

Local:

python scripts/run_experiment.py --config config/experiments/simple_plonk_rfm_s2_image_only.yaml

Cluster:

sbatch scripts/slurm/run_experiment_slurm.sh config/experiments/simple_plonk_rfm_s2_image_only.yaml

That is the main workflow. The other scripts are internal stage utilities used by the launcher.

To run the PLONK-compatible CH image-only baseline:

sbatch scripts/slurm/run_experiment_slurm.sh config/experiments/plonk_rfm_s2_image_only.yaml

To run the local Euclidean PLONK-style CH variant:

sbatch scripts/slurm/run_experiment_slurm.sh config/experiments/plonk_local_fm_r2_image_only.yaml

If you ever want to generate captions ahead of time without training, there is also a helper:

python scripts/generate_captions.py --config config/experiments/caption_experiments/image_text_structured_full.yaml

That helper is optional. Newcomers should still start with scripts/run_experiment.py.

On the cluster, the dedicated caption-generation job is:

sbatch scripts/slurm/run_caption_generation_slurm.sh config/experiments/caption_experiments/image_text_structured_full.yaml

For faster offline caption generation, there is also a sharded multi-GPU version:

sbatch scripts/slurm/run_caption_generation_multi_gpu_slurm.sh config/experiments/caption_experiments/image_text_structured_full.yaml

For fast prompt and model benchmarking on a handful of samples, there is also a dedicated caption-comparison utility:

python scripts/compare_caption_generation.py --config config/experiments/caption_experiments/image_text_free.yaml --samples-per-split 3

On the cluster:

sbatch scripts/slurm/run_caption_compare_slurm.sh config/experiments/caption_experiments/image_text_free.yaml --samples-per-split 3

By default it compares a few val and test samples across multiple VLMs and prompt styles, including:

  • compact cue-fragment prompting
  • closed-vocabulary structured JSON extraction
  • targeted VQA, where several short questions are asked and concatenated

It writes both a raw JSON dump and a Markdown report under the run directory so you can inspect outputs side by side quickly.

Before You Run

There are two different inputs to keep in mind:

  1. the image dataset subset
  2. the optional caption file for multimodal experiments

Dataset

You can either download the prepared CH subset from Hugging Face, or let the launcher build it from OSV5M.

Fast path:

DEST=/scratch/izar/$USER/visual_geolocation_cfm_poc/data/osv5m_ch_subset

hf download mkartofel/osv5m-ch-subset \
  --repo-type dataset \
  --local-dir "$DEST"

tar -C "$DEST" -xf "$DEST/images_train.tar"
tar -C "$DEST" -xf "$DEST/images_val.tar"
tar -C "$DEST" -xf "$DEST/images_test.tar"
tar -C "$DEST" -xf "$DEST/images_original_osv5m_test.tar"

After this, scripts/run_experiment.py will detect the matching local subset and reuse it.

If the subset is not there, the launcher will automatically:

  1. download from osv5m/osv5m-wds as needed
  2. filter to OSV5M samples whose country code is CH
  3. hold out matching samples from the original OSV5M test source split as original_osv5m_test
  4. create a deterministic random 80/10/10 train/val/test split from the remaining source samples using the experiment seed
  5. save it under your scratch data directory

So for image-only experiments, this is enough:

python scripts/run_experiment.py --config config/experiments/simple_plonk_rfm_s2_image_only.yaml

On the first run, it will create:

  • train.json
  • val.json
  • test.json
  • original_osv5m_test.json
  • local image files under images/train, images/val, images/test, and images/original_osv5m_test

If those artifacts already exist and match the config, the launcher reuses them.

Captions

For multimodal experiments, the launcher can generate captions automatically from the local subset before preprocessing.

Default behavior:

  • if conditioning.kind uses text and no caption file exists yet, scripts/run_experiment.py generates one
  • the generated file is stored at:
/scratch/izar/$USER/visual_geolocation_cfm_poc/data/osv5m_ch_subset/captions/generated_captions.jsonl
  • later runs reuse that file by default

The generated caption file also gets a side metadata file storing the caption-generation signature. If you change the caption model or prompts, the launcher will invalidate the old generated captions and rebuild them instead of silently reusing stale ones.

The launcher still supports loading an existing caption file from disk if you want full control:

paths:
  caption_file: /path/to/captions.jsonl
caption_generation:
  enabled: false

What the repo supports after that:

  • matching captions to samples by image filename stem
  • encoding those captions with the frozen StreetCLIP text encoder
  • caching the resulting text embeddings

Supported caption file formats:

  • jsonl
  • parquet

Expected fields include:

  • image_id
  • split
  • one or more caption columns such as:
    • caption_free
    • caption_structured_full
    • caption_structured_climate
    • caption_structured_language
    • caption_structured_vegetation
    • caption_structured_driving_side
    • caption_corrupted_climate
    • caption_shuffled

The image_id must match the local sample image filename stem used in the subset.

Example:

{"image_id":"000123","split":"train","caption_structured_full":"rural road, dry climate, right-hand traffic"}

The built-in generator currently produces:

  • caption_free
  • caption_structured_full
  • caption_structured_climate
  • caption_structured_language
  • caption_structured_vegetation
  • caption_structured_driving_side
  • caption_corrupted_climate
  • caption_shuffled

The current default caption generator is an instruction-following vision-language model, not a plain captioning model. That matters for the structured ablations, because those fields need image-grounded attribute descriptions rather than prompt echoes.

For speed, the generator only produces the caption family needed by the current experiment by default. For example, a structured_full run generates the structured captions and derived corruption controls, but does not also generate free-form captions unless caption_generation.generate_free_caption: true.

The structured generator is designed around observable, conservative attributes with a closed vocabulary, such as scene type, terrain, vegetation, road markings, visible script, road signs, and infrastructure. It intentionally avoids hidden or weakly grounded attributes like inferred country or climate zone.

Experiment Config

Each experiment is defined by one YAML file.

Main sections:

  • experiment
  • paths
  • caption_generation
  • data
  • conditioning
  • encoders
  • train
  • inference
  • evaluation
  • analysis
  • wandb

Conditioning kinds:

  • image_only
  • text_only
  • image_text_concat
  • image_text_sum

Text modes:

  • none
  • free
  • structured_full
  • structured_climate
  • structured_language
  • structured_vegetation
  • structured_driving_side
  • corrupted_climate
  • shuffled

Example multimodal preset:

experiment:
  name: image_text_structured_full
  family: simple_plonk_rfm_s2
  seed: 42
model:
  name: simple_plonk_rfm_s2
conditioning:
  kind: image_text_concat
  text_mode: structured_full
wandb:
  group: multimodal-text-ablation-v1
  tags: [simple-plonk-rfm-s2, image-text, structured-full]

For image_only, keep:

conditioning:
  kind: image_only
  text_mode: none

For multimodal runs, make sure both are true:

  • conditioning.kind uses a text-enabled mode
  • either caption_generation.enabled: true or paths.caption_file points to a real caption file

Output Layout

By default everything goes under:

/scratch/izar/$USER/visual_geolocation_cfm_poc

For one experiment, the main run directory is:

/scratch/izar/$USER/visual_geolocation_cfm_poc/runs/<experiment_name>_seed<seed>

Important outputs:

  • resolved_config.yaml
  • checkpoint_best.pt
  • checkpoint_last.pt
  • metrics.json
  • train_summary.json
  • evaluation/
  • visualizations/
  • comparisons/ when enabled

Visualizations include the existing full-Earth maps plus Switzerland-zoomed companions (*_switzerland_*.png) for regional inspection. Spherical models predict on the full Earth sphere; plonk_local_fm_r2 predicts inside a fixed Switzerland-centered 2D box and converts samples back to lat/lon for the same metrics.

Weights & Biases

The launcher owns W&B. One experiment run creates one W&B run.

Logged namespaces:

  • dataset/*
  • preprocess/*
  • sanity/*
  • train/*
  • eval/<split>/*
  • compare/*
  • viz/*

The run also saves:

  • resolved config
  • manifests
  • best checkpoint
  • evaluation reports
  • per-example JSONL
  • comparison summaries when enabled
  • qualitative images
  • reproducibility bundle

If you are not logged in, set in the config:

wandb:
  enabled: true
  mode: offline

Compare Against Image-Only

To compare a multimodal experiment against an image-only reference, set:

analysis:
  compare_enabled: true
  compare_reference_eval: /path/to/reference/test_per_example.jsonl

The launcher will write comparison outputs into the experiment run directory and log summary deltas to W&B.

Install

python -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pip install -e .