SyncAI.news, a Varaisys broadcasting
Fine-Tune ViT for Image Classification with 馃 Transformers
HF

Hugging Face Blog

路 1 min read

AI LabsHugging Face Blog

Fine-Tune ViT for Image Classification with 馃 Transformers

Just as transformers-based models have revolutionized NLP, we're now seeing an explosion of papers applying them to all sorts of other domains. One of the most revolutionary of these was the Vision Transformer (ViT), which was introduced in June 2021 by a team of researchers at Google Brain.

This paper explored how you can tokenize images, just as you would tokenize sentences, so that they can be passed to transformer models for training. It's quite a simple concept, really...

  1. Split an image into a grid of sub-image patches
  2. Embed each patch with a linear projection
  3. Each embedded patch becomes a token, and the resulting sequence of embedded patches is the sequence you pass to the model.

It turns out that once you've done the above, you can pre-train and fine-tune transformers just as you're used to with NLP tasks. Pretty sweet 馃槑.

In this blog post, we'll walk through how to leverage 馃 datasets to download and process image classification datasets, and then use them to fine-tune a pre-trained ViT with 馃 transformers.

To get started, let's first install both those packages.

pip install datasets transformers

Load a dataset

Let's start by loading a small image classification dataset and taking a look at its structure.

We'll use the beans dataset, which is a collection of pictures of healthy and unhealthy bean leaves. 馃崈

from datasets import load_dataset

ds = load_dataset('beans')
ds

Let's take a look at the 400th example from the 'train' split from the beans dataset. You'll notice each example from the dataset has 3 features:

  1. image_file_path: The str path to the image file that was loaded as image
  2. labels: A datasets.ClassLabel feature, which is an integer representation of the label. (Later you'll see how to get the string class names, don't worry!)
ex = ds['train'][400]
ex
{
  'image': <PIL.JpegImagePlugin ...>,
  'image_file_path': '/root/.cache/.../bean_rust_train.4.jpg',
  'labels': 1
}

Let's take a look at the image 馃憖

image = ex['image']
image
labels.int2str(ex['labels'])

Original source

This story was published by Hugging Face Blog. SyncAI.news shows a preview; the complete article is on the publisher's site.

Read the full story on huggingface.co

Similar News