Zero-shot image classification
Zero-shot image classification is a task that involves classifying images into different categories using a model that was not explicitly trained on data containing labeled examples from those specific categories.
Traditionally, image classification requires training a model on a specific set of labeled images, and this model learns to “map” certain image features to labels. When there’s a need to use such model for a classification task that introduces a new set of labels, fine-tuning is required to “recalibrate” the model.
In contrast, zero-shot or open vocabulary image classification models are typically multi-modal models that have been trained on a large dataset of images and associated descriptions. These models learn aligned vision-language representations that can be used for many downstream tasks including zero-shot image classification.
This is a more flexible approach to image classification that allows models to generalize to new and unseen categories without the need for additional training data and enables users to query images with free-form text descriptions of their target objects .
In this guide you’ll learn how to:
create a zero-shot image classification pipeline
run zero-shot image classification inference by hand
Before you begin, make sure you have all the necessary libraries installed:
Copied
Zero-shot image classification pipeline
The simplest way to try out inference with a model supporting zero-shot image classification is to use the corresponding pipeline(). Instantiate a pipeline from a checkpoint on the BOINC AI Hub:
Copied
Next, choose an image you’d like to classify.
Copied
Pass the image and the candidate object labels to the pipeline. Here we pass the image directly; other suitable options include a local path to an image or an image url. The candidate labels can be simple words like in this example, or more descriptive.
Copied
Zero-shot image classification by hand
Now that you’ve seen how to use the zero-shot image classification pipeline, let’s take a look how you can run zero-shot image classification manually.
Start by loading the model and associated processor from a checkpoint on the BOINC AI Hub. Here we’ll use the same checkpoint as before:
Copied
Let’s take a different image to switch things up.
Copied
Use the processor to prepare the inputs for the model. The processor combines an image processor that prepares the image for the model by resizing and normalizing it, and a tokenizer that takes care of the text inputs.
Copied
Pass the inputs through the model, and post-process the results:
Copied
Last updated