Metadata-Version: 2.4
Name: evaladem
Version: 0.1.0
Summary: ADEM: training-free prediction of whether adding a candidate image set to a trusted training set will improve a downstream model
Author: Ümit Mert Çağlar, Alptekin Temizel
License-Expression: MIT
Project-URL: Homepage, https://caglarmert.github.io/ADEM.html
Project-URL: Repository, https://github.com/caglarmert/ADEM
Project-URL: Issues, https://github.com/caglarmert/ADEM/issues
Keywords: data valuation,data quality,synthetic data,dataset curation,computer vision
Classifier: Programming Language :: Python :: 3
Classifier: Intended Audience :: Science/Research
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Image Recognition
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: numpy>=1.23
Requires-Dist: scikit-learn>=1.2
Requires-Dist: pillow>=9.0
Requires-Dist: torch>=2.0
Requires-Dist: torchvision>=0.15
Requires-Dist: transformers>=4.36
Dynamic: license-file

# ADEM: To Add or Not to Add

**[Project page](https://caglarmert.github.io/ADEM.html)** (paper and citation) · **[Code on GitHub](https://github.com/caglarmert/ADEM)**

**ADEM**, the Anticipated Downstream Effect Metric, predicts whether adding a set of new images to a small, correctly
labeled training set will improve a model trained on it, **without training any model**. It scores each candidate
image on task relevance, label correctness and uniqueness against the trusted set, and combines them into one signed
set-level score: above zero, add the candidate set; at or below zero, don't.

On 1151 candidate sets from 46 image classification, semantic segmentation and object detection tasks, ADEM ranks the
candidate sets with a mean Spearman correlation of 0.85 with the gain measured by training, against 0.63 for the
strongest of 20 existing metrics, and is the best metric on all 46 tasks.

`evaladem` is the implementation of ADEM; its code is on [GitHub](https://github.com/caglarmert/ADEM).

## Install

```bash
pip install evaladem
```

or, from this repository:

```bash
pip install git+https://github.com/caglarmert/ADEM.git
```

Python 3.9 or newer. The first run downloads the two frozen encoders ADEM uses, CLIP ViT-B/32 and DINOv2 ViT-S/14, from
Hugging Face (about 700 MB). A GPU is used when available; the CPU works too, more slowly.

## Use

Both folders hold one sub-folder per class; the sub-folder name is the label.

```
trusted/                  candidate/
    cat/  0001.jpg ...        cat/  web_17.png ...
    dog/  0002.jpg ...        dog/  gen_04.png ...
```

```bash
evaladem --trusted trusted/ --candidate candidate/
```

For example, on the paper's Oxford-IIIT Pets test bed (370 trusted images, 37 classes) with 500 new, correctly
labeled images:

```
trusted:   370 images, 37 classes (trusted/)
candidate: 500 images, 37 classes (candidate/)
working... (embedding 870 images on cuda)
  trusted: 370/370
  candidate: 500/500

ADEM = +0.7498  ->  add the candidate set
  worth 423.0 new, correctly labeled images; 1.1 expected wrong labels (share of correct labels 100%); 1% task-irrelevant
cleaned pool: the 494 of 500 candidate images with phi(d) > 0 -> evaladem_output/cleaned_pool
per-image scores -> evaladem_output/scores.csv; set score and settings -> evaladem_output/summary.json
```

The same 500 images with half of their labels wrong score −0.3025 (do not add), and a copy of the trusted set
−0.0008.

Output folder (`--output`, default `evaladem_output`):

| file | content |
|---|---|
| `cleaned_pool/<class>/...` | copies of the candidate images with image utility φ(d) > 0, the images ADEM expects to help (skip with `--no-pool`) |
| `scores.csv` | per candidate image: task relevance r, label correctness q, uniqueness u and utility φ |
| `summary.json` | the set score and its parts, the settings and the values fitted on the trusted set |

Other options: `--device cuda|cpu`, `--batch N`, `--overwrite` (write into a non-empty output folder, replacing an
earlier run's results).

## Reading the score

* **ADEM > 0**: adding the candidate set is expected to improve the model; **ADEM ≤ 0**: it is not. The score is
  signed and has a meaningful zero (an exact copy of the trusted set scores about zero), but it is not a probability
  or a percentage. Its ranking of candidate sets is more reliable than its zero point.
* **φ(d)** is in units of one new, correctly labeled image: about +1 for a new, correctly labeled, task-relevant
  image, 0 for a copy of a trusted image, about −3 for a wrong label and −0.5 for a task-irrelevant image.
* Candidate classes that the trusted set does not have count as wrong labels.

## Python

```python
from evaladem import evaluate
r = evaluate("trusted/", "candidate/", output="evaladem_output")
r["adem"], r["add"], r["images"][0]   # set score, decision, per-image scores

from evaladem import ADEM, represent      # on your own uint8 images (n, W, W, 3) and labels
m = ADEM().fit(represent(x_trusted), y_trusted)
m.set_score(represent(x_candidate), y_candidate)["adem"]
m.image_scores(represent(x_candidate), y_candidate)["phi"]
```

## What it computes

Each image is centre-cropped to a square (and resized to at most 448 px), embedded by CLIP ViT-B/32 and DINOv2
ViT-S/14 on the image as it is and through a Gaussian low-pass filter (standard deviation W/32), and the four unit
embeddings are concatenated. Against the trusted set T alone, each candidate image d gets

* **r(d), task relevance**: its mean similarity to its k = 10 nearest trusted images, ranked among the trusted
  images' own leave-one-out values (a conformal p-value p), r = min(1, p / 0.05);
* **q(d), label correctness**: a logistic-regression probe on T's low-pass embeddings (regularisation and
  temperature by cross-validation on T, T's class prior removed), combined with the share of correct labels of the
  candidate set, estimated by expectation-maximisation;
* **u(d), uniqueness**: the part of d not covered by T, shared among its near-copies in the candidate set;

and the utility φ(d) = r q u − 3 r (1 − q) − ½ (1 − r). The set score averages over the classes
log(1 + Φ<sub>c,1</sub> / N<sub>c</sub><sup>T</sup>) − (Φ<sub>c,2</sub> + Φ<sub>c,3</sub>) / (N<sub>c</sub><sup>T</sup> + N<sub>c</sub><sup>D</sup>),
where the Φ are the three parts of φ summed over the candidate images of class c. The settings k = 10, α = 0.05,
θ = 3, µ = ½ and the quarter in h are fixed, as in the paper; everything else (the probe, its regularisation and
temperature, the reference values of task relevance, h and T's own share of correct labels) is computed from T alone.

## Requirements of the trusted set

At least two classes and at least two images per class; it should be correctly labeled and drawn like the data the
model will be tested on. For segmentation or detection data, put each image in the folder of its primary class, as
the paper does: the majority pixel class (background excluded on object-centric data) or the majority box class.

## Citation

The paper and its BibTeX entry are on the [project page](https://caglarmert.github.io/ADEM.html).

## License

evaladem is released under the MIT License (see `LICENSE`).

The encoder weights are not part of this package: they are downloaded from Hugging Face at first use and are covered
by their own licenses, CLIP ViT-B/32 (`openai/clip-vit-base-patch32`, OpenAI, MIT License) and DINOv2 ViT-S/14
(`facebook/dinov2-small`, Meta, Apache License 2.0).
