This project implements a question-conditioned visual grounding model designed to locate relevant visual regions for Visual Question Answering (VQA). The model (QuestionConditionedBoxer) leverages a frozen CLIP backbone to extract visual and textual features. A cross-attention module matches the question text features with spatially aware image patches to generate an attention heatmap. The predicted heatmap is converted into a bounding box, which is used to crop/highlight target regions for a downstream Visual Language Model (Qwen2.5-VL-3B-Instruct) to answer VQA questions.
riml-attention-boxer/
│
├── config.yaml # Central configuration file for paths, training hyperparameters, etc.
│
├── models/
│ └── attention_boxer.py # Main network architecture (QuestionConditionedBoxer, CrossAttentionBoxer)
│
├── utils/
│ ├── config.py # YAML config parser and dataclass definitions
│ ├── data_loader.py # Dataset loader for VQA Grounding
│ ├── dataset_gen.py # Synthetic grounding dataset generator
│ └── primitives.py # Visual primitive shapes drawing utility
│
├── scripts/
│ ├── train_boxer.py # Training script for the Attention Boxer module
│ ├── tune_box_threshold.py # Finds the optimal heatmap-to-box threshold
│ ├── evaluate_grounding.py # Evaluates grounding accuracy (IoU, center-in-target)
│ ├── evaluate_vqa.py # Runs downstream VQA evaluations using Qwen2.5-VL
│ ├── plot_loss_curves.py # Plotting script for training and validation loss curves
│ ├── analyze_vqa_errors.py # Performs Exploratory Data Analysis (EDA) on VQA errors
│ └── visualize_vqa_failures.py # Visualizes incorrect VQA predictions by mapping log entries back to test.json
│
├── data/ # Automatically generated dataset folder (images & JSONs)
│ └── images/
│
└── checkpoints/ # Saved training models, histories, threshold jsons, and plots
config.yaml: Houses configuration details, path constants, model selections, hyperparameters, and directory outputs.models/attention_boxer.py: Contains the model classes.QuestionConditionedBoxerutilizes CLIP to condition image features on questions, passing them throughCrossAttentionBoxerto output a 2D spatial heatmap.utils/dataset_gen.py: Generates a synthetic dataset of colored primitive shapes with characters overlaid in random grid positions.utils/data_loader.py: Creates PyTorch DataLoaders to serve the image, question text, and target heatmap coordinates.scripts/train_boxer.py: Implements training loops, model optimization, loss tracking, and validation checks.scripts/tune_box_threshold.py: Evaluates different spatial grounding threshold cutoffs on the validation set.scripts/evaluate_grounding.py: Measures validation and test grounding precision metrics.scripts/evaluate_vqa.py: Evaluates the downstream VQA performance with different bounding box interventions.scripts/visualize_vqa_failures.py: Generates failure case comparison plots mapping original inputs and heatmaps.scripts/analyze_vqa_errors.py: Provides EDA summaries grouping classification errors by characteristics.
You can run the entire pipeline sequentially (dataset generation, training, tuning, evaluation, error analysis, and failure visualization) using the provided run script:
# Make the script executable
chmod +x run
# Run the pipeline
./runAlternatively, you can run the steps individually:
Generates synthetic shapes, colors, letters, and spatial coordinates for grounding task JSONs and images.
python -m utils.dataset_genTrains the QuestionConditionedBoxer model. Real-time loss history and plots are updated at the end of each epoch.
python -m scripts.train_boxerTunes the optimal threshold that converts heatmaps into bounding boxes on the validation dataset.
python -m scripts.tune_box_thresholdComputes Mean IoU, IoU threshold accuracy (0.3/0.5), and Center-in-Target accuracy on the test set.
python -m scripts.evaluate_groundingEvaluates Qwen2.5-VL downstream VQA accuracy using cropped regions. Detailed logs are exported to the path specified in config.yaml.
python -m scripts.evaluate_vqa(Optional) Standalone script to plot the training history loss curve from the saved JSON log file.
python -m scripts.plot_loss_curvesGenerates side-by-side visualization panels (Original Image, Attention Heatmap, and Bounding Box comparison) showcasing samples where the downstream VQA model made incorrect predictions.
python -m scripts.visualize_vqa_failuresRuns Exploratory Data Analysis (EDA) on incorrect VQA predictions, breaking down error rates by target features (shapes, colors, positions).
python -m scripts.analyze_vqa_errors