Swap Analysis: Minimal Pair Probing for Spatial Representations
This repository contains swap_analysis.py, a comprehensive pipeline for evaluating and visualizing how Vision-Language Models (VLMs) represent spatial relationships.
The script works by creating minimal pairs from spatial questions. It takes an original query and swaps the target and reference objects, measuring how the model's hidden states and predictions change in response.
Example Minimal Pair:
- Original: "Is A to the left or right of B?" β Expected:
left - Swapped: "Is B to the left or right of A?" β Expected:
right
π Key Features & Analyses
The script runs inference, extracts hidden states across model layers, and performs the following analyses:
- Difference Vectors (Deltas): Computes $\Delta = \text{feature(swapped)} - \text{feature(original)}$.
- Within-Category Delta Consistency: Measures if all swaps of a specific category (e.g., left $\rightarrow$ right) point in the same direction in the latent space.
- Sign-Corrected Group Consistency: Aligns opposite categories by flipping their vectors to check global axis consistency.
- Cross-Group Delta Alignment: Compares orthogonal dimensions (e.g., $\Delta_{vertical}$ vs. $\Delta_{distance}$) to detect perspective bias.
- Similarity Heatmaps: Generates $6 \times 6$ cross-category cosine similarity matrices based on mean deltas.
- Prediction Statistics: Tracks and visualizes original, swapped, and "both-correct" accuracy trajectories across different data scales.
- PCA Visualizations: Plots 2D and 3D PCA projections of per-sample embeddings and delta vectors.
- Robust Filtering: Isolates analyses to "both-correct" samples to ensure representations are tied to successful spatial understanding.
π€ Supported Models
The script natively supports hidden-state extraction for multiple model architectures, segmented into legacy base models, new large models, and merge-only configurations (for cross-scale plotting).
- Legacy (Qwen2.5-VL-3B scale experiments):
molmo(Molmo-7B-O variants)nvila(NVILA-Lite-2B variants)nvila_synthetic(NVILA mixed-data variants)qwen(Qwen2.5-VL-3B variants)
- New Large Models:
molmo_big(Molmo2-8B)qwen_big(Qwen3-VL-32B-Instruct)qwen_super(Qwen3-VL-235B-A22B-Instruct)big_trio(Molmo2-8B + RoboRefer + Qwen3-VL-32B)
- Merge-Only (Requires
--merge):molmo_all(Combinesmolmoandmolmo_bigoutputs)qwen_all(Combinesqwenandqwen_bigoutputs)nvila_synth_compare(Compares NVILA baselines against synthetic-mix checkpoints)
π Usage
1. Standard Inference
Extract features and generate single-scale analyses for a specific model family.
# Legacy model evaluation
python swap_analysis.py --model_type qwen
# New large model evaluation
python swap_analysis.py --model_type qwen_big
2. Merge Mode (Cross-Scale Analysis)
Once you have run inference on individual scales or models, use the --merge flag to aggregate the JSON/NPZ data and generate cross-scale trajectory plots.
# Combine qwen base scales with qwen_big (Qwen3-32B) results
python swap_analysis.py --model_type qwen_all --merge
Command Line Arguments
| Argument | Description | Default |
|---|---|---|
--model_type |
(Required) The model architecture/family to run. | None |
--data_path |
Path to the EmbSpatial-Bench.tsv dataset. |
/data/.../EmbSpatial-Bench.tsv |
--scales |
Specific scales to process (e.g., vanilla, 80k). If omitted, runs all default scales for the model. |
Model-dependent |
--output_dir |
Base directory for saving CSVs, JSONs, NPZs, and plots. | /data/.../results |
--merge |
Generates cross-scale/cross-model comparison plots from saved data instead of running inference. | False |
--question-type |
mcq for A/B letter answers or short for single-word generation. |
mcq |
--max-samples-per-category |
Limit samples per spatial category for faster debugging/runs. | 200 |
--no-filtering |
Disables filtering of 'Unknown' reference objects in distance queries. | False |
π Output Structure
Results are saved in your specified --output_dir under a subfolder named after the --model_type.
results/{model_type}/
βββ csv/ # Delta heatmaps, prediction rows, and cross-scale summaries
βββ json/ # Consistency metrics, alignments, and validity checks per scale
βββ npz/ # Raw hidden states and delta vectors for offline analysis
βββ plots/ # Visualizations
βββ all/ # Unfiltered analysis plots (PCA, bar charts, heatmaps)
βββ both_correct/ # Strict analysis plots (only pairs where model got both right)
βββ accuracy/ # Grouped and per-category accuracy bar/line charts
π Prerequisites
torchtransformerspandas,numpy,scikit-learnmatplotlib,seaborn,tqdmPillow- Model-specific libraries:
qwen_vl_utils,llava,olmo(depending on the models being tested).