Skip to main content

A minimal, but effective implementation of CLIP (Contrastive Language-Image Pretraining) in PyTorch

Project description

simple-clip

Simple implementation of CLIP (Contrastive Language-Image Pretraining) in PyTorch.

3d5d1009-6e3d-4570-8fd9-ee8f588003e7

CLIP

CLIP (Contrastive Language-Image Pretraining) by OpenAI is a model that unifies text and image understanding through a contrastive learning approach. It employs two neural networks, one for image processing and another for text processing, which are jointly trained on a large dataset of images and their corresponding textual descriptions. This training enables the model to understand and link visual content with natural language. CLIP's distinctive feature is its zero-shot learning capability, allowing it to generalize across various visual tasks without task-specific training, solely based on textual prompts. This makes it highly adaptable for diverse applications in AI, from image classification to complex visual reasoning tasks.

Results

All experiments used ResNet50 and Distill BERT as respectively image and text encoders. Models were first trained on smaller datasets, such as COCO to validate the approach. Later on, they were trained on combined COCO and sbucaptions data and a yfcc7m subset.

Models were evaluated in zero-shot fashion, where text queries were constructed as "a photo of {label_name}". For ImageNet, we used the 50k validation dataset.

ImageNet results surpassed the zero-shot scaling trend, by a few points, signalling a potential for smaller but more diverse and information dense datasets. This is in line with https://arxiv.org/abs/2205.01397, where authors determined that the main contributing factor in model quality and robustness for the CLIP objective are more diverse training distribution. In other words, data quality and diversity >> data quantity.

Training Datasets Training steps Text Encoder Image Encoder Eval dataset Top1 % Top5 % Top10 %
yfcc7m + coco + sbucaptions 57,800 distilbert-base-uncased ResNet-50 STL-10 93.75 - -
yfcc7m + coco + sbucaptions 57,800 distilbert-base-uncased ResNet-50 ImageNet 37.10 63.04 71.70

Trained CLIP model can be found here.

The yfcc7m + coco + sbucaptions dataset has around 8M samples in total, where 7M comes from yfcc7m, 810k from sbucaptions and 110k from coco.

Links to notebooks with ImageNet and STL results.

Usage

Instalation

$ pip install simple-clip

Code currently supports ResNet18, ResNet50 and an experimental version of the EfficientNet model as image encoders. Resnet50 was used in all experiments as the image encoder. Distill BERT (distilbert-base-uncased) was used as the text encoder in all experiments.

Supported datasets are textcap, coco, sbucaptions and yfcc7m.

Examples

yfcc7m CLIP was trained with this command (around 7M samples):

train_clip --dataset_name yfcc7m --fp16_precision --batch_size 256 --log_every_n_steps 50 --image_size 224 --learning_rate 1e-4 --imagenet_eval

Combined coco + textcaptions + sbucaptions CLIP was trained using (around 1M samples):

train_clip --dataset_name combined --fp16_precision --batch_size 256 --log_every_n_steps 50 --image_size 224 --learning_rate 1e-4 --imagenet_eval

Detailed options

Once the code is setup, run the following command with optinos listed below: train_clip [args...]⬇️

options:
  -h, --help            show this help message and exit
  --dataset_path DATASET_PATH
                        Path where datasets will be saved
  --dataset_name {textcap,coco,sbucaptions,combined,yfcc7m}
                        Dataset name
  --image_encoder_name {resnet18,resnet50,efficientnet}
                        image model architecture: resnet18, resnet50 or efficientnet (default: resnet50)
  --text_encoder_name {distilbert-base-uncased}
                        text model architecture: distilbert-base-uncased (default: distilbert-base-uncased)
  -save_model_dir SAVE_MODEL_DIR
                        Path where models
  --num_epochs NUM_EPOCHS
                        Number of epochs for training
  --image_size IMAGE_SIZE
                        Image size
  -b BATCH_SIZE, --batch_size BATCH_SIZE
                        Batch size
  -lr LEARNING_RATE, --learning_rate LEARNING_RATE
  -wd WEIGHT_DECAY, --weight_decay WEIGHT_DECAY
  --fp16_precision      Whether to use 16-bit precision for GPU training
  --imagenet_eval       Whether to evaluate on imagenet validation dataset. Required huggingface imagenet-1k dataset.
  --imagenet_eval_steps IMAGENET_EVAL_STEPS
                        Evaluate on imagenet every N steps
  --log_every_n_steps LOG_EVERY_N_STEPS
                        Log every n steps
  --ckpt_path CKPT_PATH
                        Specify path to relic_model.pth to resume training

Citation

@misc{radford2021learning,
      title={Learning Transferable Visual Models From Natural Language Supervision}, 
      author={Alec Radford and Jong Wook Kim and Chris Hallacy and Aditya Ramesh and Gabriel Goh and Sandhini Agarwal and Girish Sastry and Amanda Askell and Pamela Mishkin and Jack Clark and Gretchen Krueger and Ilya Sutskever},
      year={2021},
      eprint={2103.00020},
      archivePrefix={arXiv},
      primaryClass={cs.CV}
}

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple-clip-0.1.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

simple_clip-0.1.0-py3-none-any.whl (14.3 kB view details)

Uploaded Python 3

File details

Details for the file simple-clip-0.1.0.tar.gz.

File metadata

  • Download URL: simple-clip-0.1.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for simple-clip-0.1.0.tar.gz
Algorithm Hash digest
SHA256 749a15593eeca51863ff4ac59ffaaf224651b4ff20c010db8a1f5e222b3ab5ff
MD5 f688468b8641944cd2b3697933d86e2d
BLAKE2b-256 46f004e5ba382f43bc8f952001339d58e72b228e019cf73b3ab54f99d13bcf1e

See more details on using hashes here.

File details

Details for the file simple_clip-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: simple_clip-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.13

File hashes

Hashes for simple_clip-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a1a916c29842e5d6b7fcf71a645689bde61cd1b2c659e2b6f3b11773c47adc13
MD5 aebd3d8260e0c609ef4c9a05ddf9bad5
BLAKE2b-256 56a6cc3deed30897834c28b2721b2601eaa4b42d0349eeac9a8417f387505491

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page