Skip to content
sepantaghPublic

About

Question-conditioned visual grounding using CLIP cross-attention heatmaps to generate bounding box interventions for downstream VLM (Qwen2.5-VL) Visual Question Answering.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Question-Conditioned Attention Boxer for VQA Grounding

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.


File Structure & File Explanations

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

Brief File Descriptions:

  • config.yaml: Houses configuration details, path constants, model selections, hyperparameters, and directory outputs.
  • models/attention_boxer.py: Contains the model classes. QuestionConditionedBoxer utilizes CLIP to condition image features on questions, passing them through CrossAttentionBoxer to 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.

How to Run the Project

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
./run

Alternatively, you can run the steps individually:

1. Dataset Generation

Generates synthetic shapes, colors, letters, and spatial coordinates for grounding task JSONs and images.

python -m utils.dataset_gen

2. Model Training

Trains the QuestionConditionedBoxer model. Real-time loss history and plots are updated at the end of each epoch.

python -m scripts.train_boxer

3. Threshold Tuning

Tunes the optimal threshold that converts heatmaps into bounding boxes on the validation dataset.

python -m scripts.tune_box_threshold

4. Grounding Evaluation

Computes Mean IoU, IoU threshold accuracy (0.3/0.5), and Center-in-Target accuracy on the test set.

python -m scripts.evaluate_grounding

5. VQA Downstream Evaluation

Evaluates 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

6. Plotting Loss Curves

(Optional) Standalone script to plot the training history loss curve from the saved JSON log file.

python -m scripts.plot_loss_curves

7. Visualization of VQA Failure Modes

Generates 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_failures

8. VQA Error Analysis

Runs Exploratory Data Analysis (EDA) on incorrect VQA predictions, breaking down error rates by target features (shapes, colors, positions).

python -m scripts.analyze_vqa_errors

About

Question-conditioned visual grounding using CLIP cross-attention heatmaps to generate bounding box interventions for downstream VLM (Qwen2.5-VL) Visual Question Answering.

Topics

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages