import packaging to be compatible with setuptools==70.0.0 (#449)
import packaging to be compatible with setuptools==70.0.0
importing the version module
Co-authored-by: Jamie Jamie@Alexandras-MacBook-Pro.local Co-authored-by: Jong Wook Kim jongwook@nyu.edu
CLIP
[Blog] [Paper] [Model Card] [Colab]
CLIP (Contrastive Language-Image Pre-Training) is a neural network trained on a variety of (image, text) pairs. It can be instructed in natural language to predict the most relevant text snippet, given an image, without directly optimizing for the task, similarly to the zero-shot capabilities of GPT-2 and 3. We found CLIP matches the performance of the original ResNet50 on ImageNet “zero-shot” without using any of the original 1.28M labeled examples, overcoming several major challenges in computer vision.
Approach
Usage
First, install PyTorch 1.7.1 (or later) and torchvision, as well as small additional dependencies, and then install this repo as a Python package. On a CUDA GPU machine, the following will do the trick:
Replace
cudatoolkit=11.0
above with the appropriate CUDA version on your machine orcpuonly
when installing on a machine without a GPU.API
The CLIP module
clip
provides the following methods:clip.available_models()
Returns the names of the available CLIP models.
clip.load(name, device=..., jit=False)
Returns the model and the TorchVision transform needed by the model, specified by the model name returned by
clip.available_models()
. It will download the model as necessary. Thename
argument can also be a path to a local checkpoint.The device to run the model can be optionally specified, and the default is to use the first CUDA device if there is any, otherwise the CPU. When
jit
isFalse
, a non-JIT version of the model will be loaded.clip.tokenize(text: Union[str, List[str]], context_length=77)
Returns a LongTensor containing tokenized sequences of given text input(s). This can be used as the input to the model
The model returned by
clip.load()
supports the following methods:model.encode_image(image: Tensor)
Given a batch of images, returns the image features encoded by the vision portion of the CLIP model.
model.encode_text(text: Tensor)
Given a batch of text tokens, returns the text features encoded by the language portion of the CLIP model.
model(image: Tensor, text: Tensor)
Given a batch of images and a batch of text tokens, returns two Tensors, containing the logit scores corresponding to each image and text input. The values are cosine similarities between the corresponding image and text features, times 100.
More Examples
Zero-Shot Prediction
The code below performs zero-shot prediction using CLIP, as shown in Appendix B in the paper. This example takes an image from the CIFAR-100 dataset, and predicts the most likely labels among the 100 textual labels from the dataset.
The output will look like the following (the exact numbers may be slightly different depending on the compute device):
Note that this example uses the
encode_image()
andencode_text()
methods that return the encoded features of given inputs.Linear-probe evaluation
The example below uses scikit-learn to perform logistic regression on image features.
Note that the
C
value should be determined via a hyperparameter sweep using a validation split.See Also