• Stars
    star
    601
  • Rank 74,537 (Top 2 %)
  • Language
    Jupyter Notebook
  • License
    MIT License
  • Created over 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

CLIP-like model evaluation

CLIP Benchmark

pypi

The goal of this repo is to evaluate CLIP-like models on a standard set of datasets on different tasks such as zero-shot classification and zero-shot retrieval, and captioning.

Below we show the average rank (1 is the best, lower is better) of different CLIP models, evaluated on different datasets.

benchmark.png

The current detailed results of the benchmark can be seen here or directly in the notebook.

Features

How to install?

pip install clip-benchmark

How to use?

To evaluate we recommend to create a models.txt like

ViT-B-32,openai

to get the list of datasets

wget https://raw.githubusercontent.com/LAION-AI/CLIP_benchmark/main/benchmark/webdatasets.txt

Then to run

clip_benchmark eval --pretrained_model models.txt \
    --dataset "webdatasets.txt" \
    --dataset_root "https://huggingface.co/datasets/clip-benchmark/wds_{dataset_cleaned}/tree/main" \
    --output "benchmark_{dataset}_{pretrained}_{model}_{language}_{task}.json"

Then to get the full table

clip_benchmark build benchmark_*.json --output benchmark.csv

Command line interface (CLI)

The easiest way to benchmark the models is using the CLI, clip_benchmark. You can specify the model to use, the dataset and the task to evaluate on. Once it is done, evaluation is performed and the results are written into a JSON file.

Using other models than openclip

It is possible to use other models than openclip ones. For example japanese-clip is supported

Here is an example of use

>>> python3 clip_benchmark/cli.py eval \
  --model_type "ja_clip" \ # flag to use japanese-clip
  --pretrained "rinna/japanese-cloob-vit-b-16" \ # now, we have `rinna/japanese-cloob-vit-b-16` or `rinna/japanese-clip-vit-b-16`. 
  --language "jp" \
  --task "zeroshot_classification"  \
  --dataset "imagenet1k"  \
  --dataset_root {ROOT_PATH} 

>>> cat result.json
{"dataset": "imagenet1k", "model": "ViT-B-32-quickgelu", "pretrained": "rinna/japanese-cloob-vit-b-16", "task": "zeroshot_classification", "metrics": {"acc1": 0.54636, "acc5": 0.72856, "mean_per_class_recall": 0.54522}, "language": "jp"}

How to add other CLIP models

Please follow these steps:

  1. Add a identity file to load model in clip_benchmark/models
  2. Define a loading function, that returns a tuple (model, transform, tokenizer). Please see clip_benchmark/models/open_clip.py as an example.
  3. Add the function into TYPE2FUNC in clip_benchmark/models/__init__.py

Remarks:

  • The new tokenizer/model must enable to do the following things as https://github.com/openai/CLIP#usage
    • tokenizer(texts).to(device) ... texts is a list of string
    • model.encode_text(tokenized_texts) ... tokenized_texts is a output from tokenizer(texts).to(device)
    • model.encode_image(images) ... images is a image tensor by the transform

CIFAR-10 example

Here is an example for CIFAR-10 zero-shot classification using OpenCLIP's pre-trained model on LAION-400m:

clip_benchmark eval --dataset=cifar10 --task=zeroshot_classification --pretrained=laion400m_e32 --model=ViT-B-32-quickgelu --output=result.json --batch_size=64

By default, the dataset is downloaded into --dataset_root, which by default is root.

Here is the content of result.json after the evaluation is done:

{
    "dataset": "cifar10", "model": "ViT-B-32-quickgelu", 
    "pretrained": "laion400m_e32", "task": "zeroshot_classification",
    "metrics": {"acc1": 0.9074, "acc5": 0.998}
}

VOC2007 example

Here is another example with VOC2007, which is a multi-label classification dataset.

clip_benchmark eval --dataset=voc2007_multilabel --task=zeroshot_classification --pretrained=laion400m_e32 --model=ViT-B-32-quickgelu --output=result.json --batch_size=64

Here is the content of result.json after the evaluation is done:

{"dataset": "voc2007_multilabel", "model": "ViT-B-32-quickgelu", "pretrained": "laion400m_e32", "task": "zeroshot_classification", "metrics": {"mean_average_precision": 0.7627869844436646}}

Here, we compute the mean average precision or mAP, more details about that metric here in the context of multi-label classification.

VTAB example

Here is an example on how to run it on VTAB classification tasks. First, you need to install VTAB's dedicated package.

pip install task_adaptation==0.1

Then, you can run it by providing the full dataset name. Example with eurosat:

clip_benchmark eval --dataset=vtab/eurosat --task=zeroshot_classification --pretrained=laion400m_e32 --model=ViT-B-32-quickgelu --output=result.json --batch_size=64

See clip_benchmark/datasets/builder.py#L634 for the full list of VTAB dataset collection.

TensorFlow dataset example

Here is an example on how to run it on Tensorflow datasets. First, you need to install tfds-nightly and timm.

pip install timm tfds-nightly

The name of the dataset follows the template tfds/<DATASET_NAME>.

Example with cifar10:

clip_benchmark eval --dataset=tfds/cifar10 --task=zeroshot_classification --pretrained=laion400m_e32 --model=ViT-B-32-quickgelu --output=result.json --batch_size=64

COCO captions retrieval example

Here is an example for COCO captions zero-shot retrieval:

clip_benchmark eval --dataset=mscoco_captions --task=zeroshot_retrieval --pretrained=laion400m_e32 --model=ViT-B-32-quickgelu --output=result.json --batch_size=64

Note that for using COCO, you also need to install pycocotools (e.g., using pip install pycocotools).

COCO captions captioning example

Here is an example for COCO captions captioning task:

clip_benchmark eval --dataset=mscoco_captions --task=captioning --model=coca_ViT-L-14 --output=result.json --pretrained mscoco_finetuned_laion2b_s13b_b90k

Note that for using COCO, you also need to install pycocotools (e.g., using pip install pycocotools) and pycocoevalcap.

Multilingual evaluation

We also provide datasets for evaluating multilingual models (see e.g. https://github.com/mlfoundations/open_clip#vit-b32-xlm-roberta-base) by specifying --language.

For ImageNet-1k (zero-shot classification):

  • clip_benchmark eval --model xlm-roberta-base-ViT-B-32 --pretrained laion5b_s13b_b90k --dataset=imagenet1k --output=result.json --batch_size=64 --language=<LANG>, where <LANG> can be among zh (chinese), it (italian), jp (japanese), en (english), ar (arabic).
  • We also support Babel ImageNet classnames and prompts (https://github.com/gregor-ge/Babel-ImageNet), which can be used as the following: clip_benchmark eval --model xlm-roberta-base-ViT-B-32 --pretrained laion5b_s13b_b90k --dataset=babel_imagenet --output=result.json --batch_size=64 --language=<LANG>, where <LANG> is a two letter string from the ISO language code list. Supported values for language are: 'ne', 'id', 'de', 'nl', 'af', 'he', 'sq', 'uz', 'kn', 'ku', 'ta', 'lv', 'ko', 'ug', 'br', 'el', 'su', 'kk', 'sk', 'gl', 'om', 'fa', 'jv', 'cs', 'lo', 'hy', 'xh', 'hr', 'so', 'gu', 'am', 'ar', 'sa', 'ca', 'is', 'it', 'sv', 'ga', 'bg', 'vi', 'sd', 'ur', 'km', 'pl', 'hu', 'sr', 'fr', 'hi', 'fy', 'et', 'bs', 'sw', 'az', 'mk', 'es', 'mn', 'ja', 'tl', 'tr', 'gd', 'ro', 'mg', 'mr', 'sl', 'pt', 'lt', 'no', 'yi', 'uk', 'ky', 'ka', 'bn', 'or', 'my', 'en', 'ps', 'fi', 'zh', 'da', 'ml', 'be', 'eo', 'ha', 'eu', 'as', 'te', 'th', 'cy', 'si', 'ru', 'la', 'pa', 'ms'

for COCO (zero-shot retrieval):

  • clip_benchmark eval --model xlm-roberta-base-ViT-B-32 --pretrained laion5b_s13b_b90k --dataset=multilingual_mscoco_captions --output=result.json --batch_size=64 --language=<LANG>, where <LANG> can be among es (spanish), it (italian), jp (japanese), ko (korean), pl (polish), ru (russian), tr (Turkish), zh (chinese), en (english).

For Flickr-30k (zero-shot retrieval)

  • clip_benchmark eval --model xlm-roberta-base-ViT-B-32 --pretrained laion5b_s13b_b90k --dataset=flickr30k --output=result.json --batch_size=64 --language=<LANG>, where <LANG> can be among en (english), zh (chinese).

For Flickr-8k (zero-shot retrieval)

  • clip_benchmark eval --model xlm-roberta-base-ViT-B-32 --pretrained laion5b_s13b_b90k --dataset=flickr8k --output=result.json --batch_size=64 --language=<LANG>, where <LANG> can be among en (english), zh (chinese).

Compositionality evaluation

For Sugar Crepe:

clip_benchmark eval --model ViT-B-32 --pretrained laion400m_e32 --dataset=sugar_crepe/<TASK> --output=result.json

where <TASK> can be among add_att, add_obj, replace_att, replace_obj, replace_rel, swap_att, swap_obj. To evaluate on all the tasks together, you can do:

clip_benchmark eval --model ViT-B-32 --pretrained laion400m_e32 --dataset=sugar_crepe --output=result.json

Webdataset example

Here is an example on how to run it on webdatasets. First, you need to install webdataset.

pip install webdataset

Creating a webdataset

You can either convert an already supported CLIP_benchmark dataset to webdataset format, or manually create your own with the same file structure. For already supported datasets use the CLI command clip_benchmark_export_wds as in this example:

$ clip_benchmark_export_wds --dataset cifar10 --plit train --dataset_root DATA_DIR/ --output wds_cifar10/
$ clip_benchmark_export_wds --dataset cifar10 --split test --dataset_root DATA_DIR/ --output wds_cifar10/

which will convert the train and test splits for CIFAR-10 (downloaded to DATA_DIR/) and save the webdataset to wds_cifar10/ (upload to Huggingface Hub must be done manually for now). Retrieval datasets are also supported with the --retrieval flag.

For other datasets, data must be stored with the following file structure:

root_dir/
    train/
        nshards.txt
        0.tar
        1.tar
        ...
    test/
        nshards.txt
        0.tar
        ...
    classnames.txt
    zeroshot_classification_templates.txt
    dataset_type.txt

Each split should be contained in its own folder and nshards.txt should contain a single integer corresponding to the number of TAR files. The TAR files should follow webdataset format, with an image file (.webp, .png, or .jpg) and a label (.cls) for each example. Classnames and templates are required for zeroshot classification evaluation, with each classname or template on its own line. Dataset type is required for distinguishing zeroshot retrieval evaluation: the file should just contain the text retrieval.

Evaluating on a webdataset

The name of the dataset follows the template wds/<DATASET_NAME>. Note that the dataset name currently only affects the name in the results output - classnames and templates are loaded directly from the included files. The dataset root directory can be either a local path to the root_dir as specified above, or an HTTP URL pointing to a Huggingface Hub dataset file tree.

Example with vtab/cifar10 (zero-shot classification):

  • local: clip_benchmark eval --dataset wds/vtab/cifar10 --dataset_root ROOT_DIR/wds_vtab-cifar10/
  • remote: clip_benchmark eval --dataset wds/vtab/cifar10 --dataset_root https://huggingface.co/datasets/clip-benchmark/wds_{dataset_cleaned}/tree/main

Example with mscoco_captions (retrieval):

  • local: clip_benchmark eval --dataset=wds/mscoco_captions --dataset_root ROOT_DIR/wds_vtab-mscoco_captions/ --task=zeroshot_retrieval
  • remote: clip_benchmark eval --dataset=wds/mscoco_captions --dataset_root="https://huggingface.co/datasets/clip-benchmark/wds_{dataset_cleaned}/tree/main" --task=zeroshot_retrieval

Example with mscoco_captions (captioning):

  • local: clip_benchmark eval --dataset=wds/mscoco_captions --dataset_root ROOT_DIR/wds_vtab-mscoco_captions/ --task=captioning
  • remote: clip_benchmark eval --dataset=wds/mscoco_captions --dataset_root="https://huggingface.co/datasets/clip-benchmark/wds_{dataset_cleaned}/tree/main" --task=captioning

All other arguments remain the same as in the other examples. See https://huggingface.co/clip-benchmark for a full list of datasets that have already been uploaded to Huggingface.

Evaluate mulitple models on multiple datasets

For the purpose of benchmarking, it is possible to run the CLI with multiple pre-trained models on multiple datasets.

Pretrained models and datasets list as arguments

For models, we can provide list of pretrained model names in the form of 'model,pretrained' (so model and pretrained are comma separated). For datasets, we can provide a list of datasets. For languages, we can provide a list of languages. Example:

clip_benchmark eval --pretrained_model  ViT-B-32-quickgelu,laion400m_e32 ViT-L-14,laion400m_e32  \
--dataset cifar10 cifar100 --dataset_root "clip_benchmark_datasets/{dataset}" --language en jp \
 --output "{dataset}_{pretrained}_{model}_{language}_{task}.json"

Note that --dataset_root and --output can be now in the form of a template that depends on the dataset/model/language/task (for --output) and dataset name (for --dataset_root).

Note that If the benchmark fails at some point, it is possible to resume it by skipping already evaluated models using --skip_existing.

Pretrained models and datasets list as files

We can also provide a path to files with models (each line is in the form of 'model,pretrained' where model and pretrained are comma separated) and datasets list (one dataset per line):

clip_benchmark eval --pretrained_model  benchmark/models.txt \
--dataset benchmark/datasets.txt --dataset_root "clip_benchmark_datasets/{dataset}"  \
 --output "{dataset}_{pretrained}_{model}_{language}_{task}.json"

Examples are available in benchmark/datasets.txt and benchmark/models.txt

Model and dataset collections

We can also provide model collection names (openai, openclip_base, openclip_multilingual, openclip_full are supported) or dataset collection names (vtab, vtab+, retrieval, imagenet_robustness are supported):

clip_benchmark eval --pretrained_model openai openclip_base  --dataset vtab+ retrieval \
--dataset_root "clip_benchmark_datasets/{dataset}" --not quiet \
--output "{dataset}_{pretrained}_{model}_{language}_{task}.json"

See clip_benchmark/models.py#L6 and clip_benchmark/datasets/builder.py#L634 for more information about the collections.

Custom templates / prompts / classnames

It is also possible to use custom prompts by providing a custom template file and/or a custom classname file. For instance:

clip_benchmark eval --dataset "imagenet1k" --model ViT-B-32 --pretrained laion400m_e32 --custom_template_file <path> --custom_classname_file <path>

The template file can be either in the usual format https://github.com/LAION-AI/CLIP_benchmark/blob/main/clip_benchmark/datasets/en_zeroshot_classification_templates.json or in the CuPL format https://github.com/LAION-AI/CLIP_benchmark/blob/main/clip_benchmark/datasets/cupl_prompts.json to have class-specific prompts. In the case of the CuPL format, the classnames file will not be used, thus one only needs to provide the template file --custom_template_file.

For instance, the prompts from the CuPL paper https://arxiv.org/abs/2209.03320 for ImagetNet-1k can be used this way :

clip_benchmark eval --dataset "imagenet1k" --model ViT-B-32 --pretrained laion400m_e32 --custom_template_file cupl_prompts.json

Development

For development, you can also do this:

git clone https://github.com/LAION-AI/CLIP_benchmark
cd CLIP_benchmark
python setup.py install

Credits

  • Thanks to OpenCLIP authors, zero-shot accuracy code is adapted from there and pre-trained models are used in the command line interface.

  • Thanks to SLIP authors, some zero-shot templates and classnames are from there.

  • Thanks to Wise-ft authors, Imagenet robustness datasets code is adapted from there

  • Thanks to LiT authors, some zero-shot templates and classnames of VTAB datasets are from there.

  • Thanks to Sugar Crepe authors for compositionality tasks evaluation on COCO

  • Thanks to Babel ImageNet authors for multilingual evaluation of ImageNet-1k zero-shot classification.

  • Thanks to ImageNet-W authors for ImageNet-W evaluation

  • Thanks to CuPL for CuPL prompts.

  • Thanks to PyCOCOevalcap and @gpucce for COCO captions image captioning evaluation.

  • Thanks to @li-xirong for chinese Flickr-30k/FLickr-8k.

  • Thanks to Chinese CLIP authors for chinese ImageNet-1k classnames/prompts (zero-shot classification).

  • Thanks to @rinnakk and @mkshing for japanese ImageNet-1k classnames/prompts (zero-shot classification) and japanese CLIP support.

  • Thanks to @KhalidAlt for arabic ImageNet-1k classnames/prompts (zero-shot classification).

  • Thanks to @djghosh13 for WebDataset support.

  • Thanks to @FreddeFrallan for for multilingual COCO.

  • Thanks to @mitchellnw for linear probing support.

  • This package was created with Cookiecutter and the audreyr/cookiecutter-pypackage project template. Thanks to the author.

More Repositories

1

Open-Assistant

OpenAssistant is a chat-based assistant that understands tasks, can interact with third-party systems, and retrieve information dynamically to do so.
Python
37,011
star
2

audio-dataset

Audio Dataset for training CLAP and other models
Python
616
star
3

dalle2-laion

Pretrained Dalle2 from laion
Python
499
star
4

CLAP

Contrastive Language-Audio Pretraining
Python
479
star
5

natural_voice_assistant

Python
439
star
6

laion-3d

Collect large 3d dataset and build models
253
star
7

phenaki

A phenaki reproduction using pytorch.
Python
218
star
8

aesthetic-predictor

A linear estimator on top of clip to predict the aesthetic quality of pictures
Jupyter Notebook
199
star
9

Open-Instruction-Generalist

Open Instruction Generalist is an assistant trained on massive synthetic instructions to perform many millions of tasks
Python
195
star
10

ldm-finetune

Home of `erlich` and `ongo`. Finetune latent-diffusion/glid-3-xl text2image on your own data.
Python
169
star
11

scaling-laws-openclip

Reproducible scaling laws for contrastive language-image learning (https://arxiv.org/abs/2212.07143)
Jupyter Notebook
152
star
12

CLIP-based-NSFW-Detector

Python
135
star
13

laion-datasets

Description and pointers of laion datasets
HTML
131
star
14

laion-dreams

Aim for the moon. If you miss, you may hit a star.
121
star
15

laion.ai

HTML
110
star
16

AIW

Alice in Wonderland code base for experiments and raw experiments data
Python
108
star
17

LAION-5B-WatermarkDetection

Python
102
star
18

video-clip

Let's make a video clip
92
star
19

Open-GIA

O-GIA is an umbrella for research, infrastructure and projects ecosystem that should provide open source, reproducible datasets, models, applications & safety tools for Open Generalist Interactive Agents (O-GIA). O-GIA systems will act in collaboration with human or autonomously, supporting various kind of validated decision making and assistance.
91
star
20

General-GPT

Jupyter Notebook
64
star
21

Discord-Scrapers

Implementation of a discord channel scraper to generate datasets.
Python
60
star
22

Text-to-speech

Python
58
star
23

Big-Interleaved-Dataset

Big-Interleaved-Dataset
Python
57
star
24

riverbed

Tools for content datamining and NLP at scale
Python
41
star
25

OCR-ensemble

Jupyter Notebook
38
star
26

Conditional-Pretraining-of-Large-Language-Models

Python
37
star
27

interesting-text-datasets

33
star
28

blade2blade

Adversarial Training and SFT for Bot Safety Models
Python
32
star
29

temporal-embedding-aggregation

Aggregating embeddings over time
Python
31
star
30

deep-image-diffusion-prior

Inverts CLIP text embeds to image embeds and visualizes with deep-image-prior.
Jupyter Notebook
31
star
31

watermark-detection

A repository containing datasets and tools to train a watermark classifier.
Python
31
star
32

medical

This repository will be a summary and outlook on all our open, medical, AI advancements.
Jupyter Notebook
28
star
33

Anh

Anh - LAION's multilingual assistant datasets and models
Python
27
star
34

laion50BU

Un-*** 50 billions multimodality dataset
24
star
35

conditioned-prior

(wip) Use LAION-AI's CLIP "conditoned prior" to generate CLIP image embeds from CLIP text embeds.
Python
18
star
36

LAION-SAFETY

An open toolbox for NSFW & toxicity detection
Jupyter Notebook
16
star
37

opendream

Frontend (and soon also midleware and backend) for a new, opensource image generation platform.
TypeScript
14
star
38

laion5B-paper

Building the laion5B paper
13
star
39

laion-dedup

Python
13
star
40

notebooks

A collection of generative and training notebooks getting mirrored to google colab.
Jupyter Notebook
12
star
41

laionide

This repository contains training code and checkpoitns for finetuning glide.
Python
12
star
42

super-resolution

This is the LAION repository for creating open super-resolution models with the help of LAION-5B subsets.
11
star
43

dataset-spec

Describe the format of image/text datasets
Python
10
star
44

LAION-PEOPLE

This project provides a data set with bounding boxes, body poses, 3D face meshes & captions of people from our LAION-2.2B. Additionally it provides clusters based on the poses and face meshes and pose-related captions based on these cluster assignments.
10
star
45

image-deduplication-testset

HTML
8
star
46

project-menu

Projects at LAION
8
star
47

laion-ai.github.io

laion github website
Svelte
6
star
48

dataset-usage

This repository is a summary of all systems and scientific papers that use LAION datasets.
6
star
49

repository-overview

This repository will give a quick overview of all projects and repositories from LAION.
5
star
50

LionizeR

Experiments with Summarization, Long Context and Retrieval
Python
4
star
51

KAISER

Knowledge Acquisition and Interlinking via Semantic Embeddings and Reasoning
4
star
52

lucidrains-projects

A summary of all lucidrains repositores and links to training / research approaches by LAION or other communities.
Jupyter Notebook
3
star
53

decentralized-learning

A basic setup for decentralized-learning that can be used for training future DALLE/CLIP/CLAP models.
3
star
54

diffusion-prior

DALL-E2 diffusion prior
Python
3
star
55

GIF

General / Global Inference Framework
Python
3
star
56

website

This is the development repository of the LAION-AI website.
HTML
3
star
57

safety-pipeline

A collection of safety classifiers and models to process image and texts.
Python
3
star
58

NeoGen

3
star
59

laion5b-subsets

Creating subsets from laion5b via embeddings search
Jupyter Notebook
2
star
60

human_artifacts

A repo containing images for artifact annotation.
2
star
61

public-relations

All media / publicity on LAION and related stuff!
2
star
62

public-domain-images

A collection of public domain images donated for ML training.
2
star
63

math_problems-step-by-step_solutions

Here we provide and collect many functions to generate math problem and step by step solutions for LLM training
Python
2
star
64

language-models

2
star
65

dataset-inference

The new repository for the genral inference pipeline.
Python
2
star
66

introduction-resources

Recommended intro resources
2
star
67

balanced-laion5b

This repository shall help finding a good distribution for huge datasets like LAION-5B for more efficient training.
2
star
68

hand-inference

A model to run hand inference on a cluster.
Jupyter Notebook
2
star
69

BUD-E_V1.0

BUD-E (Buddy) is an open-source voice assistant framework that facilitates seamless interaction with AI models and APIs, enabling the creation and integration of diverse skills for educational and research applications.
1
star
70

laion5b-bias

This repository is a collection of found biases in the LAION-5B dataset.
1
star
71

dataset-tasks

datasets that should be downloaded & converted to our standard training formart.
1
star
72

LAION-AUDIO

This repository contains prompts & best practices to annotate audio clips with a very high degree of details using Audio-Language-Models
1
star
73

AIW_webpage

Alice in Wonderland project and initiative webpage
1
star