![1IVK-GUd7A-l4YGFVuRrIhg.jpeg](https://quantinsightsnetwork.com/wp-content/uploads/2024/01/1IVK-GUd7A-l4YGFVuRrIhg-678x381.jpeg)
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 solution for classification, regression, timeseries forecasting and much more. This page aims to explain how to solve a multilabel classification problem with minimal code focusing on familiar CIFAR-10 image dataset.
Environment Setup
As the first step AutoKeras should be installed and imported along with TensorFlow.
!pip install autokerasimport autokeras as ak
import tensorflow as tf
import pandas as pd
Import and walk through CIFAR-10 dataset
CIFAR-10 image dataset is readily availabe 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.
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 npr = 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 start 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 obataine is quite decent. Better models can be obtained by setting max_trials
greater than 1 and runnuing the model for more epochs
.
Detailed analysis on model performance can be analysed with scilit-learn
library by generating confusion matrix and classification report using 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.
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.
Be the first to comment