Vinay-113/cifar10h-disagreement-demo
CIFAR-10H Disagreement Prediction
This project trains deep neural networks to predict the full human annotator label distribution for a CIFAR-10 image using the CIFAR-10H dataset. Instead of outputting a single hard class, the model outputs a 10-dimensional distribution q(y|x) that approximates the empirical human distribution p(y|x).
Project Overview
The repository covers the full research workflow:
- download and align CIFAR-10 with CIFAR-10H soft labels
- run sanity checks and dataset visualizations
- pretrain a CIFAR-adapted ResNet-18 backbone on hard-label CIFAR-10
- train soft-label models with multiple heads and multiple losses
- evaluate distribution matching and entropy prediction quality
- run ablations over losses, backbones, and heads
- test robustness to annotator subsampling and image corruptions
- inspect model behavior with Grad-CAM and failure-case analysis
Repository Structure
cifar10h-disagreement/
├── README.md
├── requirements.txt
├── config.py
├── data/
│ ├── download.py
│ └── dataset.py
├── models/
│ ├── backbone.py
│ └── heads.py
├── losses/
│ └── losses.py
├── train.py
├── evaluate.py
├── ablations/
│ ├── run_backbone_init.py
│ ├── run_loss_comparison.py
│ └── run_head_comparison.py
├── robustness/
│ ├── annotator_subsampling.py
│ └── ood_corruptions.py
└── explainability/
├── gradcam.py
└── failure_analysis.pyEnvironment Setup
- Create and activate a Python 3.10+ environment.
- Install dependencies:
pip install -r requirements.txtHugging Face Demo Deployment
This repository now includes a Gradio app at app.py so you can deploy it as a Hugging Face Space.
What the demo needs
The Space code is ready, but it still needs a trained checkpoint. The app looks for a model in this order:
MODEL_CHECKPOINT_PATHMODEL_CHECKPOINT_URLcheckpoints/kl_cifar10_pretrained_mlp_best.pt
The simplest path is:
- Train the baseline model locally or on a GPU machine.
- Upload the resulting checkpoint somewhere downloadable.
- Set
MODEL_CHECKPOINT_URLin your Space variables.
Recommended deployment flow
- Push this repository to GitHub.
- Create a new Hugging Face Space and choose the
GradioSDK. - Upload this repository to the Space, or connect it via GitHub sync.
- Add a Space variable named
MODEL_CHECKPOINT_URLthat points to your trained checkpoint. - Let the Space rebuild automatically.
Local demo run
If you already have a trained checkpoint locally:
MODEL_CHECKPOINT_PATH=checkpoints/kl_cifar10_pretrained_mlp_best.pt python app.pyIf your checkpoint is hosted remotely:
MODEL_CHECKPOINT_URL="https://YOUR_PUBLIC_CHECKPOINT_URL/model.pt" python app.pyThen open http://127.0.0.1:7860.
Optional GitHub to Space sync
After the repo is on GitHub, you can either:
- push directly to the Hugging Face Space repo, or
- set up GitHub Actions to sync to the Space on every push
See the official Hugging Face docs linked at the end of this README.
Data
data/download.py downloads:
- CIFAR-10 via
torchvision.datasets.CIFAR10 - CIFAR-10H annotator counts from the official public release
The project stores raw assets under data/raw/ and writes outputs under:
checkpoints/results/logs/results/evaluations/results/ablations/results/robustness/results/explainability/
Training Design
- Backbone: ResNet-18 adapted for
32x32images with a3x3stride-1 stem and no initial max-pooling. - Backbone initialization modes:
randomcifar10_pretrainedimagenet_pretrained- Heads:
linearmlptemperature- Losses:
kljscosinecomposite
The composite loss adds an entropy-matching penalty to KL divergence so the model is trained to match both class mass and overall human uncertainty.
How To Run
Run the following commands from the repository root.
1. Download data
python data/download.pyExpected outputs:
- CIFAR-10 files under
data/raw/cifar10/ data/raw/cifar10h/cifar10h-counts.npy
2. Build dataset visuals and sanity checks
python data/dataset.pyExpected outputs in results/dataset_analysis/:
entropy_histogram.pngper_class_average_entropy.pngmajority_vote_distribution_matrix.pngentropy_extremes_grid.png
3. Pretrain the backbone on CIFAR-10 hard labels
python models/backbone.pyExpected outputs:
checkpoints/cifar10_pretrained_backbone.ptresults/logs/cifar10_pretraining.csv
4. Train soft-label models
Example:
python train.py --loss kl --backbone_init cifar10_pretrained --head mlpExpected outputs:
checkpoints/{loss}_{backbone_init}_{head}_best.ptresults/logs/{loss}_{backbone_init}_{head}.csv
5. Evaluate trained checkpoints
Example:
python evaluate.py --loss kl --backbone_init cifar10_pretrained --head mlpExpected outputs in results/evaluations/{run_name}/:
metrics.csvpredicted_probabilities.npytrue_probabilities.npyentropy_scatter.pngloss_metrics_grouped_bar.pngqualitative_entropy_grid.png
Also updates:
results/evaluation_summary.csv
6. Run ablations
python ablations/run_backbone_init.py
python ablations/run_loss_comparison.py
python ablations/run_head_comparison.pyExpected outputs:
- summary CSV tables in
results/ablations/... - comparison plots in
results/ablations/...
7. Run robustness analyses
python robustness/annotator_subsampling.py
python robustness/ood_corruptions.pyExpected outputs:
results/robustness/annotator_subsampling/...results/robustness/ood_corruptions/...
8. Run explainability analyses
python explainability/gradcam.py
python explainability/failure_analysis.pyExpected outputs:
- per-image Grad-CAM panels and
gradcam_summary_grid.png - failure-case panels,
failure_summary_grid.png, andfailure_statistics.csv
Required Execution Order
python data/download.pypython data/dataset.pypython models/backbone.pypython train.py --loss kl --backbone_init cifar10_pretrained --head mlppython train.py --loss js --backbone_init cifar10_pretrained --head mlppython train.py --loss cosine --backbone_init cifar10_pretrained --head mlppython train.py --loss composite --backbone_init cifar10_pretrained --head mlppython evaluate.py --loss kl --backbone_init cifar10_pretrained --head mlppython evaluate.py --loss js --backbone_init cifar10_pretrained --head mlppython evaluate.py --loss cosine --backbone_init cifar10_pretrained --head mlppython evaluate.py --loss composite --backbone_init cifar10_pretrained --head mlppython ablations/run_backbone_init.pypython ablations/run_loss_comparison.pypython ablations/run_head_comparison.pypython robustness/annotator_subsampling.pypython robustness/ood_corruptions.pypython explainability/gradcam.pypython explainability/failure_analysis.py
Metrics Reported
The evaluation pipeline computes:
- KL divergence mean and standard deviation
- Jensen-Shannon divergence mean and standard deviation
- cosine similarity mean and standard deviation
- Pearson correlation between true and predicted entropy
- Spearman correlation between true and predicted entropy
- Precision@100, Precision@200, Precision@500 for top-entropy retrieval
Notes
- All seeds are centralized in
config.pyand default to42. - CIFAR-10H splits are deterministic:
6000 / 2000 / 2000. - All training scripts use early stopping on validation KL divergence.
- If predictive entropy under OOD corruption does not increase with severity,
robustness/ood_corruptions.pyreports that result honestly.
Deployment Checklist
Use this exact sequence if you want both GitHub and a public demo:
cd /Users/vinaypatil/Documents/Playground/cifar10h-disagreementgh auth login -h github.comgh repo create cifar10h-disagreement --public --source=. --remote=origin --push- Train a model and keep the checkpoint
checkpoints/kl_cifar10_pretrained_mlp_best.pt - Upload that checkpoint to a public URL or a Hugging Face model repo
- Create a new Hugging Face Space with
Gradio - Copy this repo into the Space or sync it from GitHub
- Add
MODEL_CHECKPOINT_URLin the Space settings - Wait for the automatic rebuild
