The Visual Bias Mitigator is an open-source framework designed to empower researchers in the field of bias mitigation in computer vision. This codebase provides a comprehensive environment where users can easily implement, run, and evaluate existing visual bias mitigation methods.
With the increasing awareness of bias in AI systems, it is crucial for researchers to have access to robust tools that facilitate the exploration and development of mitigation approaches. The Visual Bias Mitigator (VB-Mitigator) serves this purpose by offering:
- Implemented Methods: A collection of established visual bias mitigation methods that can be directly utilized, allowing researchers to replicate and understand their functionality.
- Extensibility: Researchers can exploit this code-base to develop custom bias mitigation approaches tailored to their specific needs. The framework is designed with flexibility in mind, enabling easy integration of new approaches.
- Performance Comparison: The framework facilitates the performance comparison between custom methods and state-of-the-art.
The goal of this hands-on experience is to familiarize you with the core functionalities of the VB-Mitigator. You will learn how to set up an experiment using pre-defined configurations and run one or more of the implemented bias mitigation methods.
Visual Bias Mitigator (VB-Mitigator)¶
The Visual Bias Mitigator is an open-source framework designed to empower researchers in the field of bias mitigation in computer vision. This codebase provides a comprehensive environment where users can easily implement, run, and evaluate existing visual bias mitigation methods.
With the increasing awareness of bias in AI systems, it is crucial for researchers to have access to robust tools that facilitate the exploration and development of mitigation approaches. The Visual Bias Mitigator (VB-Mitigator) serves this purpose by offering:
- 🚀 Implemented Methods: A collection of established visual bias mitigation methods that can be directly utilized, allowing researchers to replicate and understand their functionality.
- 🔧 Extensibility: Researchers can exploit this code-base to develop custom bias mitigation approaches tailored to their specific needs. The framework is designed with flexibility in mind, enabling easy integration of new approaches.
- 📊 Performance Comparison: The framework facilitates the performance comparison between custom methods and state-of-the-art.
The goal of this hands-on experience is to familiarize you with the core functionalities of the VB-Mitigator. You will learn how to set up an experiment using pre-defined configurations and run one or more of the implemented bias mitigation methods.
⏬ Clone the Git Repository¶
!git clone https://github.com/mever-team/vb-mitigator.git
Cloning into 'vb-mitigator'... remote: Enumerating objects: 2135, done. remote: Counting objects: 100% (300/300), done. remote: Compressing objects: 100% (127/127), done. remote: Total 2135 (delta 107), reused 253 (delta 102), pack-reused 1835 (from 1) Receiving objects: 100% (2135/2135), 165.49 MiB | 21.97 MiB/s, done. Resolving deltas: 100% (837/837), done. Updating files: 100% (729/729), done.
📦 Install the requirements¶
Once the installation finishes, Colab will prompt you to restart the session. Click the "RESTART SESSION" button to make the new libraries available. 🔄
%cd vb-mitigator/
!pip install -r requirements.txt > /dev/null
/content/vb-mitigator Running command git clone --filter=blob:none --quiet https://github.com/xinyu1205/recognize-anything.git /tmp/pip-req-build-myax7eol ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts. opencv-python-headless 4.12.0.88 requires numpy<2.3.0,>=2; python_version >= "3.9", but you have numpy 1.26.4 which is incompatible. umap-learn 0.5.9.post2 requires scikit-learn>=1.6, but you have scikit-learn 1.5.1 which is incompatible. tsfresh 0.21.0 requires scipy>=1.14.0; python_version >= "3.10", but you have scipy 1.13.1 which is incompatible. gcsfs 2025.3.2 requires fsspec==2025.3.2, but you have fsspec 2025.3.0 which is incompatible. firebase-admin 6.9.0 requires httpx[http2]==0.28.1, but you have httpx 0.27.2 which is incompatible. dataproc-spark-connect 0.8.2 requires tqdm>=4.67, but you have tqdm 4.66.5 which is incompatible. google-genai 1.25.0 requires httpx<1.0.0,>=0.28.1, but you have httpx 0.27.2 which is incompatible. thinc 8.3.6 requires numpy<3.0.0,>=2.0.0, but you have numpy 1.26.4 which is incompatible.
📂 Set Up the Environment Path¶
Now that the project has been cloned, we need to navigate into the main vb-mitigator project directory.
The next step is crucial for the notebook to work correctly. We will add the project's root directory to Python's path. This tells Python to look for modules and scripts inside this folder, allowing us to import the custom components of the framework in the cells that follow.
%cd vb-mitigator/
import os
os.environ['PYTHONPATH']='.'
[Errno 2] No such file or directory: 'vb-mitigator/' /content/vb-mitigator
🚀 Train a Baseline Model on Biased-MNIST¶
Now for the first experiment, we will train a standard, vanilla model (a regular classifier with no bias mitigation techniques) on a specially prepared dataset called Biased-MNIST. This will establish a baseline performance, showing us how a typical model behaves when confronted with biased data.
The Biased-MNIST Dataset¶
We've created a modified version of the classic MNIST dataset of handwritten digits. Here’s how the bias is introduced:
- Color-Label Correlation: In the training set, each digit label (0-9) is strongly correlated with a specific color. For example, most of '2s' are on red background, and most of '7s' are on blue background.
- Shortcut Learning: A standard model might learn to "cheat" by creating a simple shortcut: if it sees a blue color, it will guess '7' without actually learning to recognize the digit's shape.
- The Test Set: The test set is different; the colors are randomized. A model that has learned the color shortcut will perform very poorly on this test set, revealing its bias.
Running the Training¶
The command below executes the main training script, tools/train.py. We use the --cfg flag to point to configs/toy.yaml, a configuration file specifically set up for this demonstration. It is a "toy" configuration that runs the training for only a few epochs to ensure it completes quickly in this notebook environment.
import torchvision
import matplotlib.pyplot as plt
import numpy as np
from configs.cfg import CFG as cfg
from mitigators import method_to_trainer
cfg.DATASET.TYPE = "biased_mnist"
cfg.MODEL.TYPE = "simple_conv"
trainer = method_to_trainer["erm"](cfg)
train_loader = trainer.dataloaders["train"]
# Get the first batch
dataiter = iter(train_loader)
batch = next(dataiter)
images = batch["inputs"]
labels = batch["targets"]
def show_images(imgs, labels, nrow=8):
# Unnormalize - assuming mean=0.5, std=0.5
# setup a grid using torchvision utils
# use permute to change the dimensions order from CHW to HWC and then convert to numpy.
# initialize and show the plot
# Plot the first batch
show_images(images, labels)
File "/tmp/ipython-input-4-1266785487.py", line 29 show_images(images, labels) ^ IndentationError: expected an indented block after function definition on line 19
# run the experiment
📊 Analyze the Baseline Results¶
Now that the training is complete, let's examine the results. The training script saved all the key metrics into a CSV log file. We'll use the Pandas library to load this file into a DataFrame, which gives us a clean, table-like view of the experiment's outcome.
What to Look For¶
Pay close attention to the train_cls_loss, test_accuracy, and test_loss columns. We expect to see the following pattern, which is a classic sign of shortcut learning:
- Low
train_cls_loss: The training loss should decrease to a very low value. This indicates the model has successfully "learned" the training data by memorizing the easy color-based shortcut. - Low
test_accuracyand Hightest_loss: The accuracy on the test set will be very low, while the corresponding test loss will be high. This proves the model didn't learn the actual shapes of the digits and fails when the color shortcut is removed.
import pandas as pd
# Path to the log file from the previous training run
# Load the data into a pandas DataFrame
# Display the DataFrame
🧪 Applying a Bias Mitigation Method¶
The baseline model clearly failed, learning the color "shortcut" instead of the digits' actual shapes. Now, let's see if we can improve the performance by using a specific bias mitigation technique.
We will modify our configuration file to switch from the standard training (erm) to one of the supported methods (eg., flac or badd).
Instructions¶
- Open the configuration file located at
configs/toy.yaml. - You will need to make two changes:
- Under
EXPERIMENT, change theNAMEfrom"erm"to"<selected_method>". This is important so that our new results are saved to a different directory and don't overwrite our baseline run. - Under
MITIGATOR, change theTYPEfrom"erm"to"<selected_method>". This is the key step that activates the bias mitigation method.
- Under
Modified toy.yaml¶
For example, if you want to use FLAC, the toy.yaml file should look like this:
EXPERIMENT:
NAME: "flac" # <-- CHANGE THIS
TAG: "toy"
PROJECT: "biased_mnist_baselines"
DATASET:
TYPE: "biased_mnist"
BIASES: ["color"]
MITIGATOR:
TYPE: "flac" # <-- AND CHANGE THIS
SOLVER:
BATCH_SIZE: 128
# ... (rest of the file remains the same)
🚀 Re-running Training with the Selected Mitigator¶
Now that you've updated configs/toy.yaml to use the desired method, it's time to run the experiment again.
We'll use the exact same command as before. However, because the configuration file has been changed, the training script will now apply the selected bias mitigation approach. The goal is to see if this method can overcome the color bias and achieve a much higher test_accuracy than our baseline model.
Let's kick off the training and see if it works.
# run the experiment
🏁 Final Analysis: Analyzing the New Results¶
Now for the final step, let's analyze the performance of the selected mitigation method to see if it successfully overcame the bias.
In the cell below, make sure that you replace the name of the method (i.e., flac) with the bias mitigation method you selected in the previous steps.
Review the results in the table, paying close attention to the final epoch's values. Think back to the baseline vanilla model's performance.
- Is the final
test_accuracysignificantly higher than before? - Is the final
test_losssignificantly lower?
A clear "yes" to these questions is the key indicator that the selected method worked, successfully ignoring the color shortcut and learning the true features of the digits.
import pandas as pd
# Path to the log file from the previous training run
# Load the data into a pandas DataFrame
# Display the DataFrame
🔧 Bonus: Implement a Missing Mitigator Function¶
In this task, you'll reimplement the _train_iter() function for the flac mitigator.
- Navigate to the
flac_aidamitigator and implement the_train_iter()function. This function is responsible for executing a single training iteration on a batch. - After completing the implementation, update the
toy.yamlconfiguration file to use the new mitigator. - Finally, run the experiment using the updated setup.
# run the experiment