Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

Publication

Multi-Label Image Classification using AutoKeras.
Artificial Intelligence   Latest   Machine Learning

Multi-Label Image Classification using AutoKeras.

Last Updated on January 29, 2024 by Editorial Team

Author(s): Rakesh M K

Originally published on Towards AI.

Image created with Microsoft Bing Image Maker

AutoKeras

AutoKeras is Python’s Keras-based AutoML library for developing Deep Learning models. Built on top of TensorFlow by DATA Lab at Texas A&M University, it offers a solutions for classification, regression, time series forecasting, and much more. This page aims to explain how to solve a multilabel classification problem with minimal code focusing on a familiar CIFAR-10 image dataset.

Environment Setup

As the first step, AutoKeras should be installed and imported along with TensorFlow.

!pip install autokeras

import autokeras as ak
import tensorflow as tf
import pandas as pd

Import and walk through the CIFAR-10 dataset

The CIFAR-10 image dataset is readily available in TensorFlow, which is already split into train and test. There are 60000 32×32 color images in 10 different classes, where 10000 are test images. The different classes of the dataset is as shown below.

CIFAR-10 Dataset. Source: https://www.cs.toronto.edu/~kriz/cifar.html

Let’s import the dataset and walk through it.

'''import image dataset'''
from tensorflow.keras.datasets import cifar10

(xTrain, yTrain), (xTest, yTest) = cifar10.load_data()
print(xTrain.shape)
print(yTrain.shape)
print(xTest.shape)
print(yTest.shape)

(50000, 32, 32, 3) #shape of train data
(50000, 1) #shape of train labels
(10000, 32, 32, 3) #shape of test data
(10000, 1) #shape of test labels

Some random train images and corresponding labels.

import matplotlib.pyplot as plt
import numpy as np

r = np.random.randint(0, 5000,10) # generate 10 random numbers
fig, axes = plt.subplots(1, len(r), figsize=(15, 3))

for idx, i in enumerate(r):
axes[idx].imshow(xTrain[i], cmap=plt.cm.binary)
axes[idx].set_title(yTrain[i])
axes[idx].axis('off')
plt.show()

Initialize and fit the Image Classifier

The classifier can be initialized using ImageClassifier() class. The hyperparameter max_trials determines how many model to try. Setting it to 1 here as it is just a demonstration of how to use the library. overwrite = True will overwrite the previous model if the next one performs better (while max_trials >1) which in turn saves the best model.

# Initialize the image classifier.

tf.random.set_seed(102)
ImgClassifier = ak.ImageClassifier(overwrite=True, max_trials=1)
# Feed the image classifier with training data.
ImgClassifier.fit(xTrain, yTrain, epochs=40)

The process starts by printing the current model architecture and hyperparameters as below. Different layers, optimizer, and learning rate can be observed.

Model Summary and Evaluation

Model architecture can be printed as below.

print(ImgClassifier.export_model().summary())

Prediction and evaluation

# Predict with model on test data.
prediction = ImgClassifier.predict(xTest)
print(prediction)

# Evaluate the model.
print(ImgClassifier.evaluate(xTest, yTest))

For 40 epochs and 1 trial, the model obtained is quite decent. Better models can be obtained by setting max_trials greater than 1 and running the model for more epochs .

Detailed analysis of model performance can be analyzed with scilit-learn library by generating a confusion matrix and classification report using the below code.

'''To print classification report'''
from sklearn.metrics import classification_report
print(classification_report(prediction.astype(int) , yTest))

'''To display confusion matrix'''
from sklearn.metrics import ConfusionMatrixDisplay,confusion_matrix
cM = ConfusionMatrixDisplay(confusion_matrix(prediction.astype(int), yTest), display_labels=None)
cM.plot()

AutoKeras also offers customized search space for advanced users where one can use AutoModel. For more details, visit https://autokeras.com/tutorial/image_classification/#customized-search-space

To read about forecasting using PyCaret AutoML library, visit below page.

Time Series Forecasting using PyCaret

This page explains how to do forecasting using Python’s low-code AutoML library PyCaret.

pub.towardsai.net

Conclusion

From the page, it is evident that the AutoKeras library facilitates the automation of developing deep learning models with minimal code.I hope this page has provided a helpful understanding of the utility and implementation of the library to some extent. By configuring hyperparameters as per specific requirements and leveraging the library’s customizable search space, AutoKeras proves to be a valuable tool for data scientists and ML engineers.

References

  1. https://autokeras.com/
  2. https://www.cs.toronto.edu/~kriz/cifar.html

U+2709️[email protected]

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI

Feedback ↓