Today’s blog post on multi-label classification with Keras was inspired from an email I received last week from PyImageSearch reader, Switaj.
Switaj writes:
Hi Adrian, thanks for the PyImageSearch blog and sharing your knowledge each week.
I’m building an image fashion search engine and need help.
Using my app a user will upload a photo of clothing they like (ex. shirt, dress, pants, shoes) and my system will return similar items and include links for them to purchase the clothes online.
The problem is that I need to train a classifier to categorize the items into various classes:
Clothing type: Shirts, dresses, pants, shoes, etc.
Color: Red, blue, green, black, etc.
Texture/appearance: Cotton, wool, silk, tweed, etc.
I’ve trained three separate CNNs for each of the three categories and they work really well.
Is there a way to combine the three CNNs into a single network? Or at least train a single network to complete all three classification tasks?
I don’t want to have to apply them individually in a cascade of if/else code that uses a different network depending on the output of a previous classification.
Thanks for your help.
Switaj poses an excellent question:
Is it possible for a Keras deep neural network to return multiple predictions?
And if so, how is it done?
To learn how to perform multi-label classification with Keras, just keep reading.
2020-06-12 Update: This blog post is now TensorFlow 2+ compatible!
Today’s blog post on multi-label classification is broken into four parts.
In the first part, I’ll discuss our multi-label classification dataset (and how you can build your own quickly).
From there we’ll briefly discuss
SmallerVGGNet
, the Keras neural network architecture we’ll be implementing and using for multi-label classification.
We’ll then take our implementation of
SmallerVGGNet
and train it using our multi-label classification dataset.
Finally, we’ll wrap up today’s blog post by testing our network on example images and discuss when multi-label classification is appropriate, including a few caveats you need to look out for.
Our multi-label classification dataset
The dataset we’ll be using in today’s Keras multi-label classification tutorial is meant to mimic Switaj’s question at the top of this post (although slightly simplified for the sake of the blog post).
Our dataset consists of 2,167 images across six categories, including:
Black jeans (344 images)
Blue dress (386 images)
Blue jeans (356 images)
Blue shirt (369 images)
Red dress (380 images)
Red shirt (332 images)
The goal of our Convolutional Neural network will be to predict both color and clothing type.
The entire process of downloading the images and manually removing irrelevant images for each of the six classes took approximately 30 minutes.
When trying to build your own deep learning image datasets, make sure you follow the tutorial linked above — it will give you a huge jumpstart on building your own datasets.
Configuring your development environment
To configure your system for this tutorial, I recommend following either of these tutorials:
Go ahead and visit the “Downloads” section of this blog post to grab the code + files. Once you’ve extracted the zip file, you’ll be presented with the following directory structure:
In the root of the zip, you’re presented with 6 files and 3 directories.
The important files we’re working with (in approximate order of appearance in this article) include:
search_bing_api.py
: This script enables us to quickly build our deep learning image dataset. You do not need to run this script as the dataset of images has been included in the zip archive. I’m simply including this script as a matter of completeness.
train.py
: Once we’ve acquired the data, we’ll use the
train.py
script to train our classifier.
fashion.model
: Our
train.py
script will serialize our Keras model to disk. We will use this model later in the
classify.py
script.
mlb.pickle
: A scikit-learn
MultiLabelBinarizer
pickle file created by
train.py
— this file holds our class names in a convenient serialized data structure.
plot.png
: The training script will generate a
plot.png
image file. If you’re training on your own dataset, you’ll want to check this file for accuracy/loss and overfitting.
: This directory holds our dataset of images. Each class class has its own respective subdirectory. We do this to (1) keep our dataset organized and (2) make it easy to extract the class label name from a given image path.
pyimagesearch
: This is our module containing our Keras neural network. Because this is a module, it contains a properly formatted
__init__.py
. The other file,
smallervggnet.py
contains the code to assemble the neural network itself.
examples
: Seven example images are present in this directory. We’ll use
classify.py
to perform multi-label classification with Keras on each of the example images.
If this seems a lot, don’t worry! We’ll be reviewing the files in the approximate order in which I’ve presented them.
Our Keras network architecture for multi-label classification
The CNN architecture we are using for this tutorial is
As a matter of completeness we are going to implement
SmallerVGGNet
in this guide; however, I’m going to defer any lengthy explanation of the architecture/code to my previous post— please refer to it if you have any questions on the architecture or are simply looking for more detail. If you’re looking to design your own models, you’ll want to pick up a copy of my book, Deep Learning for Computer Vision with Python.
Ensure you’ve used the “Downloads” section at the bottom of this blog post to grab the source code + example images. From there, open up the
# initialize the model along with the input shape to be
# "channels last" and the channels dimension itself
model = Sequential()
inputShape = (height, width, depth)
chanDim = -1
# if we are using "channels first", update the input shape
# and channels dimension
if K.image_data_format() == "channels_first":
inputShape = (depth, height, width)
chanDim = 1
Our class is defined on Line 12. We then define the
build
function on Line 14, responsible for assembling the convolutional neural network.
The
build
method requires four parameters —
width
,
height
,
depth
, and
classes
. The
depth
specifies the number of channels in an input image, and
classes
is the number (integer) of categories/classes (not the class labels themselves). We’ll use these parameters in our training script to instantiate the model with a
96 x 96 x 3
input volume.
The optional argument,
finalAct
(with a default value of
"softmax"
) will be utilized at the end of the network architecture. Changing this value from softmax to sigmoid will enable us to perform multi-label classification with Keras.
Keep in mind that this behavior is differentthan our original implementation of
SmallerVGGNet
in our previous post — we are adding it here so we can control whether we are performing simple classification or multi-class classification.
From there, we enter the body of
build
, initializing the
model
(Line 17) and defaulting to
"channels_last"
architecture on Lines 18 and 19 (with a convenient switch for backends that support
activation (Rectified Linear Unit). We apply batch normalization, max pooling, and 25% dropout.
Dropout is the process of randomly disconnecting nodes from the current layer to the next layer. This process of random disconnects naturally helps the network to reduce overfitting as no one single node in the layer will be responsible for predicting a certain class, object, edge, or corner.
Notice the numbers of filters, kernels, and pool sizes in this code block which work together to progressively reduce the spatial size but increase depth.
Fully connected layers are placed at the end of the network (specified by
Dense
on Lines 57 and 63).
Line 64 is important for our multi-label classification —
finalAct
dictates whether we’ll use
"softmax"
activation for single-label classification or
"sigmoid"
activation in the case of today’s multi-label classification. Refer to Line 14 of this script,
smallervggnet.py
and Line 95 of
train.py
.
Implementing our Keras model for multi-label classification
Now that we have implemented
SmallerVGGNet
, let’s create
train.py
, the script we will use to train our Keras network for multi-label classification.
I urge you to review the previous post upon which today’s
train.py
script is based. In fact, you may want to view them on your screen side-by-side to see the difference and read full explanations. Today’s review will be succinct in comparison.
# set the matplotlib backend so figures can be saved in the background
import matplotlib
matplotlib.use("Agg")
# import the necessary packages
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.preprocessing.image import img_to_array
from sklearn.preprocessing import MultiLabelBinarizer
from sklearn.model_selection import train_test_split
from pyimagesearch.smallervggnet import SmallerVGGNet
import matplotlib.pyplot as plt
from imutils import paths
import tensorflow as tf
import numpy as np
import argparse
import random
import pickle
import cv2
import os
On Lines 2-20 we import the packages and modules required for this script. Line 3 specifies a matplotlib backend so that we can save our plot figure in the background.
I’ll be making the assumption that you have Keras, scikit-learn, matpolotlib, imutils and OpenCV installed at this point. Be sure to refer to the “Configuring your development environment” section above.
Now that (a) your environment is ready, and (b) you’ve imported packages, let’s parse command line arguments:
Command line arguments to a script are like parameters to a function — if you don’t understand this analogy then you need to read up on command line arguments.
We’re working with four command line arguments (Lines 23-32) today:
--dataset
: The path to our dataset.
--model
: The path to our output serialized Keras model.
--labelbin
: The path to our output multi-label binarizer object.
--plot
: The path to our output plot of training loss and accuracy.
Be sure to refer to the previous post as needed for explanations of these arguments.
Let’s move on to initializing some important variables that play critical roles in our training process:
# initialize the number of epochs to train for, initial learning rate,
# batch size, and image dimensions
EPOCHS = 30
INIT_LR = 1e-3
BS = 32
IMAGE_DIMS = (96, 96, 3)
# disable eager execution
tf.compat.v1.disable_eager_execution()
These variables on Lines 36-39 define that:
Our network will train for 75
EPOCHS
in order to learn patterns by incremental improvements via backpropagation.
We’re establishing an initial learning rate of
1e-3
(the default value for the Adam optimizer).
The batch size is
32
. You should adjust this value depending on your GPU capability if you’re using a GPU but I found a batch size of
32
works well for this project.
As stated above, our images are
96 x 96
and contain
3
channels.
Additional information on hyperparamters is provided in the previous post.
Line 42 disables TensorFlow’s Eager Execution mode. We found that this was necessary during our 2020-06-12 Update to achieve the same accuracy as on the original publication date of this article.
From there, the next two code blocks handle loading and preprocessing our training data:
# extract set of class labels from the image path and update the
# labels list
l = label = imagePath.split(os.path.sep)[-2].split("_")
labels.append(l)
First, we load each image into memory (Line 57). Then, we perform preprocessing (an important step of the deep learning pipeline) on Lines 58 and 59. We append the
image
to
data
(Line60).
Lines 64 and 65 handle splitting the image path into multiple labels for our multi-label classification task. After Line 64 is executed, a 2-element list is created and is then appended to the labels list on Line 65. Here’s an example broken down in the terminal so you can see what’s going on during the multi-label parsing:
# scale the raw pixel intensities to the range [0, 1]
data = np.array(data, dtype="float") / 255.0
labels = np.array(labels)
print("[INFO] data matrix: {} images ({:.2f}MB)".format(
len(imagePaths), data.nbytes / (1024 * 1000.0)))
Our
data
list contains images stored as NumPy arrays. In a single line of code, we convert the list to a NumPy array and scale the pixel intensities to the range
[0, 1]
.
We also convert labels to a NumPy array as well.
From there, let’s binarize the labels — the below block is critical for this week’s multi-class classification concept:
# binarize the labels using scikit-learn's special multi-label
# binarizer implementation
print("[INFO] class labels:")
mlb = MultiLabelBinarizer()
labels = mlb.fit_transform(labels)
# loop over each of the possible class labels and show them
for(i, label)inenumerate(mlb.classes_):
print("{}. {}".format(i + 1, label))
In order to binarize our labels for multi-class classification, we need to utilize the scikit-learn library’s MultiLabelBinarizer class. You cannot use the standard
LabelBinarizer
class for multi-class classification. Lines 76 and 77 fit and transform our human-readable labels into a vector that encodes which class(es) are present in the image.
One-hot encoding transforms categorical labels from a single integer to a vector. The same concept applies to Lines 16 and 17 except this is a case of two-hot encoding.
Notice how on Line 17 of the Python shell (not to be confused with the code blocks for
train.py
) two categorical labels are “hot” (represented by a “1” in the array), indicating the presence of each label. In this case “dress” and “red” are hot in the array (Lines 14-17). All other labels have a value of “0”.
Let’s construct the training and testing splits as well as initialize the data augmenter:
Splitting the data for training and testing is common in machine learning practice — I’ve allocated 80% of the images for training data and 20% for testing data. This is handled by scikit-learn on Lines 85 and 86.
Our data augmenter object is initialized on Lines 89-91. Data augmentation is a best practice and a most-likely a “must” if you are working with less than 1,000 images per class.
Next, let’s build the model and initialize the Adam optimizer:
On Lines 109 and 110 we compile the model using binary cross-entropy rather than categorical cross-entropy.
This may seem counterintuitive for multi-label classification; however, the goal is to treat each output label as an independent Bernoulli distribution and we want to penalize each output node independently.
From there we launch the training process with our data augmentation generator (Lines 114-118).
After training is complete we can save our model and label binarizer to disk:
2020-06-12 Update: In order for this plotting snippet to be TensorFlow 2+ compatible the
H.history
dictionary keys are updated to fully spell out “accuracy” sans “acc” (i.e.,
H.history["val_accuracy"]
and
H.history["accuracy"]
). It is semi-confusing that “val” is not spelled out as “validation”; we have to learn to love and live with the API and always remember that it is a work in progress that many developers around the world contribute to.
Accuracy + loss for training and validation is plotted on Lines 131-141. The plot is saved as an image file on Line 142.
In my opinion, the training plot is just as important as the model itself. I typically go through a few iterations of training and viewing the plot before I’m satisfied to share with you on the blog.
I like to save plots to disk during this iterative process for a couple reasons: (1) I’m on a headless server and don’t want to rely on X-forwarding, and (2) I don’t want to forget to save the plot (even if I am using X-forwarding or if I’m on a machine with a graphical desktop).
Recall that we changed the matplotlib backend on Line 3 of the script up above to facilitate saving to disk.
Training a Keras network for multi-label classification
Don’t forget to use the “Downloads” section of this post to download the code, dataset, and pre-trained model (just in case you don’t want to train the model yourself).
If you want to train the model yourself, open a terminal. From there, navigate to the project directory, and execute the following command:
# classify the input image then find the indexes of the two class
# labels with the *largest* probability
print("[INFO] classifying image...")
proba = model.predict(image)[0]
idxs = np.argsort(proba)[::-1][:2]
We load the
model
and multi-label binarizer from disk into memory on Lines 34 and 35.
From there we classify the (preprocessed) input
image
(Line 40) and extract the top two class labels indices (Line 41) by:
Sorting the array indexes by their associated probability in descending order
Grabbing the first two class label indices which are thus the top-2 predictions from our network
You can modify this code to return more class labels if you wish. I would also suggest thresholding the probabilities and only returning labels with > N% confidence.
From there, we’ll prepare the class labels + associated confidence values for overlay on the output image:
# show the probabilities for each of the individual labels
for(label, p)inzip(mlb.classes_, proba):
print("{}: {:.2f}%".format(label, p * 100))
# show the output image
cv2.imshow("Output", output)
cv2.waitKey(0)
The loop on Lines 44-48 draws the top two multi-label predictions and corresponding confidence values on the
output
image.
Similarly, the loop on Lines 51 and 52 prints the all the predictions in the terminal. This is useful for debugging purposes.
Finally, we show the
output
image on the screen (Lines 55 and 56).
Keras multi-label classification results
Let’s put
classify.py
to work using command line arguments. You do not need to modify the code discussed above in order to pass new images through the CNN. Simply use the command line arguments in your terminal as is shown below.
Let’s try an image of a red dress — notice the three command line arguments that are processed at runtime:
Our model is very confident that it sees blue, but slightly less confident that it has encountered a shirt. That being said, this is still a correct multi-label classification!
Let’s see if we can fool our multi-label classifier with blue jeans:
I can’t be 100% sure that these are denim jeans (they look more like leggings/jeggings to me), but our multi-label classifier is!
Let’s try a final example of a black dress (
example_07.jpg
). While our network has learned to predict “black jeans” and “blue jeans” along with both “blue dress” and “red dress”, can it be used to classify a “black dress”?
Oh no — a blunder! Our classifier is reporting that the model is wearing black jeans when she is actually wearing a black dress.
What happened here?
Why are our multi-class predictions incorrect? To find out why, review the summary below.
Summary
In today’s blog post you learned how to perform multi-label classification with Keras.
Performing multi-label classification with Keras is straightforward and includes two primary steps:
Replace the softmax activation at the end of your network with a sigmoid activation
Swap out categorical cross-entropy for binary cross-entropy for your loss function
From there you can train your network as you normally would.
The end result of applying the process above is a multi-class classifier.
You can use your Keras multi-class classifier to predict multiple labels with just a single forward pass.
However, there is a difficulty you need to consider:
You need training data for each combination of categories you would like to predict.
Just like a neural network cannot predict classes it was never trained on, your neural network cannot predict multiple class labels for combinations it has never seen. The reason for this behavior is due to activations of neurons inside the network.
If your network is trained on examples of both (1) black pants and (2) red shirts and now you want to predict “red pants” (where there are no “red pants” images in your dataset), the neurons responsible for detecting “red” and “pants” will fire, but since the network has never seen this combination of data/activations before once they reach the fully-connected layers, your output predictions will very likely be incorrect (i.e., you may encounter “red” or “pants” but very unlikely both).
Again, your network cannot correctly make predictions on data it was never trained on (and you shouldn’t expect it to either). Keep this caveat in mind when training your own Keras networks for multi-label classification.
I hope you enjoyed this post!
To be notified when future posts are published here on PyImageSearch, just enter your email address in the form below!