Reader small image

You're reading from  Automated Machine Learning with AutoKeras

Product typeBook
Published inMay 2021
Reading LevelBeginner
PublisherPackt
ISBN-139781800567641
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Luis Sobrecueva
Luis Sobrecueva
author image
Luis Sobrecueva

Luis Sobrecueva is a senior software engineer and ML/DL practitioner currently working at Cabify. He has been a contributor to the OpenAI project as well as one of the contributors to the AutoKeras project.
Read more about Luis Sobrecueva

Right arrow

Chapter 4: Image Classification and Regression Using AutoKeras

In this chapter, we will focus on the use of AutoKeras applied to images. In Chapter 2, Getting Started with AutoKeras, we got our first contact with deep learning (DL) applied to images, by creating two models (a classifier and a regressor) that recognized handwritten digits. We will now create more complex and powerful image recognizers, examine how they work, and see how to fine-tune them to improve their performance.

After reading this chapter, you will be able to create your own image models and apply them, to solve a wide range of problems in the real world.

As we discussed in Chapter 2, Getting Started with AutoKeras, the most suitable models for recognizing images use a type of neural network called a convolutional neural network (CNN). For the two examples that we will see in this chapter, AutoKeras will also choose CNNs for the creation of its models. So, let's see in a little more detail what these...

Technical requirements

All coding examples in this book are available as Jupyter Notebooks that can be downloaded from the following link: https://github.com/PacktPublishing/Automated-Machine-Learning-with-AutoKeras.

As code cells can be executed, each Notebook can be self-installable by adding a code snippet with the requirements you need. For this reason, at the beginning of each notebook there is a code cell for environmental setup, which installs AutoKeras and its dependencies.

So, to run the coding examples, you only need a computer with Ubuntu/Linux as the operating system and you can install the Jupyter Notebook with this command line:

$ apt-get install python3-pip jupyter-notebook

Alternatively, you can also run these notebooks using Google Colaboratory, in which case you will only need a web browser—see the AutoKeras with Google Colaboratory section in Chapter 2, Getting Started with AutoKeras, for more details. Furthermore, in the Installing AutoKeras...

Creating a CIFAR-10 image classifier

The model we are going to create will classify images from a dataset called Canadian Institute for Advanced Research, 10 classes (CIFAR-10). It contains 60,000 32x32 red, green, blue (RGB) colored images, classified into 10 different classes. It is a collection of images that is commonly used to train ML and computer vision algorithms.

Here are the classes in the dataset:

  • airplane
  • automobile
  • bird
  • cat
  • deer
  • dog
  • frog
  • horse
  • ship
  • truck

In the next screenshot, you can see some random image samples found in the CIFAR-10 dataset:

Figure 4.6 – CIFAR-10 image samples

This a problem considered already solved. It is relatively easy to achieve a classification accuracy close to 80%. For better performance, we must use deep learning CNNs with which a classification precision greater than 90% can be achieved in the test dataset. Let's see how to implement it with...

Creating and fine-tuning a powerful image classifier

We will now use the AutoKeras ImageClassifier class to find the best classification model. Just for this little example, we set max_trials (the maximum number of different Keras models to try) to 2, and we do not set the epochs parameter so that it will use an adaptive number of epochs automatically. For real use, it is recommended to set a large number of trials. The code is shown here:

clf = ak.ImageClassifier(max_trials=2)

Let's run the training to search for the optimal classifier for the CIFAR-10 training dataset, as follows:

clf.fit(x_train, y_train)

Here is the output:

Figure 4.8 – Notebook output of image classifier training

The previous output shows that the accuracy of the training dataset is increasing.

As it has to process thousands of color images, the models that AutoKeras will generate will be more expensive to train, so this process will take hours, even using graphics...

Creating an image regressor to find out the age of people

In this section, we will create a model that will find out the age of a person from an image of their face. For this, we will train the model with a dataset of faces extracted from images of celebrities in Internet Movie Database (IMDb).

As we want to approximate the age, we will use an image regressor for this task.

In the next screenshot, you can see some samples taken from this dataset of celebrity faces:

Figure 4.12 – A few image samples from IMDb faces dataset

Figure 4.12 – A few image samples from IMDb faces dataset

This notebook with the complete source code can be found here: https://github.com/PacktPublishing/Automated-Machine-Learning-with-AutoKeras/blob/main/Chapter04/Chapter4_CelebrityAgeDetector.ipynb.

We will now explain the relevant code cells of the notebook in detail, as follows:

  • Installing AutoKeras: As with the previous example, the first code cell at the top of the notebook is responsible for installing AutoKeras...

Creating and fine-tuning a powerful image regressor

Because we want to predict age, and this is a scalar value, we are going to use AutoKeras ImageRegressor as an age predictor. We set max_trials (the maximum number of different Keras models to try) to 10, and we do not set the epochs parameter so that it will use an adaptive number of epochs automatically. For real use, it is recommended to set a large number of trials. The code is shown here:

reg = ak.ImageRegressor(max_trials=10)

Let's run the training model to search for the optimal regressor for the training dataset, as follows:

reg.fit(train_imgs, train_ages)

Here is the output of the preceding code:

Figure 4.13 – Notebook output of our age predictor training

The previous output shows that the loss for the training dataset is decreasing.

This training process has taken 1 hour in Colaboratory. We have limited the search to 10 architectures (max_trials = 10) and restricted the...

Summary

In this chapter, we have learned how convolutional networks work, how to implement an image classifier, and how to fine-tune it to improve its accuracy. We have also learned how to implement an image regressor and fine-tune it to improve its performance.

Now that we have learned how to work with images, we are ready to move on to the next chapter, where you will learn how to work with text by implementing classification and regression models using AutoKeras.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Automated Machine Learning with AutoKeras
Published in: May 2021Publisher: PacktISBN-13: 9781800567641
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Luis Sobrecueva

Luis Sobrecueva is a senior software engineer and ML/DL practitioner currently working at Cabify. He has been a contributor to the OpenAI project as well as one of the contributors to the AutoKeras project.
Read more about Luis Sobrecueva