Metadata-Version: 2.3
Name: biasid
Version: 1.0.1
Summary: biasid - a tool for analyzing transformation bias and robustness of AI-generated image detectors
Requires-Dist: datasets>=4.8.0
Requires-Dist: joblib>=1.5.3
Requires-Dist: matplotlib>=3.10.0
Requires-Dist: pandas>=3.0.0
Requires-Dist: pillow>=12.1.0
Requires-Dist: scienceplots>=2.2.1
Requires-Dist: torch>=2.10.0
Requires-Dist: torchmetrics>=1.9.0
Requires-Dist: torchvision>=0.25.0
Requires-Dist: tqdm>=4.67.0
Requires-Python: >=3.12
Description-Content-Type: text/markdown

# `biasid` - A Tool for Analyzing Transformation Bias and Robustness of AI-Generated Image Detectors

## Installation
```
pip install biasid
```

## Usage
```python
import matplotlib.pyplot as plt
import torch
from torch import nn
from torchvision.transforms import v2

from biasid import DetectorWrapper, compute_metrics, create_bias_plot, get_score_df
from biasid.datasets import FolderDataset
from biasid.transforms import JPEG


# toy detector, use your own instead
class MyDetector(nn.Module):
    def __init__(self):
        super().__init__()
        self.layer = nn.Linear(3 * 32 * 32, 1)

    def forward(self, images):
        return self.layer(images.flatten(start_dim=1))


if __name__ == "__main__":
    my_detector = MyDetector()

    # wrap your model into DetectorWrapper
    detector = DetectorWrapper(
        model=my_detector,
        preprocessing=v2.CenterCrop(32),
        postprocessing=torch.sigmoid,
    )

    # define datasets
    real = FolderDataset(root="real")
    fake = FolderDataset(root="fake")

    # define a transformation
    jpeg = JPEG()

    # compute scores
    score_df = get_score_df(
        detector=detector,
        real_dataset=real,
        fake_dataset=fake,
        output_dir="output/scores",
        transform=jpeg,
        batch_size=8,
        num_workers=4,
    )

    # compute metrics (with bootstrapping)
    metrics = compute_metrics(score_df)
    metrics.save("output/metrics")

    # visualize results
    create_bias_plot(result=metrics, real_subset="real", fake_subset="fake")
    plt.savefig("output/plots/jpeg.png")
```

## Notes
- It is expected that the real and fake dataset contain the same number of images. If they are semantically aligned, they should have the same order s.t. during bootstrapping the matching is preserved.
