[파이썬] fastai 멀티 레이블 분류

Fastai is a deep learning library built on top of PyTorch. It simplifies the process of creating and training neural networks by providing high-level abstractions and easy-to-use APIs. In this blog post, we will explore how to perform multi-label classification using Fastai in Python.

What is Multi-label Classification?

In traditional classification tasks, each instance is assigned to a single label or class. However, in multi-label classification, each instance can be assigned to multiple labels or classes simultaneously. For example, in an image classification task, an image can contain multiple objects, and we may want to predict the presence of each object in the image.

Fastai for Multi-label Classification

Fastai provides a simple and efficient way to perform multi-label classification tasks. Let’s see how we can use Fastai to train a multi-label classifier.

Install Fastai

Before getting started, make sure you have Fastai installed. You can install it using pip:

pip install fastai

Load and Prepare the Data

The first step is to load and prepare the data for training. Fastai provides convenient APIs to load data from various sources such as image folders, CSV files, or data frames.

For multi-label classification, the dataset should have one column for each label, where each label can be either 0 or 1 indicating the presence or absence of that label.

Let’s assume we have a CSV file with the following structure:

Image Label1 Label2
image1.jpg 1 0
image2.jpg 0 1
image3.jpg 1 1

We can use Fastai’s ImageDataLoaders.from_csv method to load the data:

from fastai.vision.all import *

# Define path to your dataset
path = Path("/path/to/dataset")

# Load the data from CSV file
data = ImageDataLoaders.from_csv(path, folder="images", csv_fname="labels.csv")

Define the Model

Next, we need to define the model architecture. Fastai provides various pre-trained models that we can use as a starting point and fine-tune according to our needs. We can choose a suitable pre-trained model and customize the fully connected layers for multi-label classification.

learn = cnn_learner(data, resnet34, metrics=partial(accuracy_multi, thresh=0.5))

Here, we are using the resnet34 architecture as the base model and specifying accuracy_multi as the evaluation metric.

Train the Model

Once the data and model are ready, we can train the model using the fit_one_cycle method.

learn.fine_tune(4)

Make Predictions

After training, we can use the model to make predictions on new instances. We can use the predict method to get the predicted labels for a given image.

img = PILImage.create("/path/to/test/image.jpg")
preds = learn.predict(img)

Conclusion

Fastai simplifies the process of performing multi-label classification tasks in Python. With its easy-to-use APIs and pre-trained models, we can quickly build and train models for various multi-label classification tasks. So, if you are working on a project that requires predicting multiple labels, give Fastai a try and see how it can streamline your workflow.

Happy coding!