Article Image
Article Image
View All Posts
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi

Categorical classification is used where you have data that falls into one of many categories - an example of this would be classifying food - e.g. “hot dog”, “pizza”, “fries” etc…

This article is a followup to this one TensorFlow Binary Classification

You can find the example notebook(s) for this post in the tensorflow-tutorial GitHub repo.

For this small tutorial, I’ve created a python generator that creates images that are either blank, a square, a circle, or a triangle.

Training Images

def data_generator():
    i = 0
        if i >= 1000:
            i = 0
        # our output value will be the one hot encoded version of: 0,1,2,3 - corresponding to our labels - "blank", "square", "circle", "triangle"
        Y = i % 4
        X = np.zeros((image_width, image_height, 1))

        # size of our shape
        radius = int(np.random.uniform(10,20))
        # position of our shape
        center_x = int(np.random.uniform(radius, image_width - radius))
        center_y = int(np.random.uniform(radius, image_height - radius))

        if Y == 1: # generate a square
            X[center_y - radius:center_y + radius, center_x - radius:center_x + radius] = 1
        elif Y == 2: # generate a circle
            for y in range(-radius, radius):
                for x in range(-radius, radius):
                    if x*x + y*y <= radius*radius:
                        X[y+center_y, x+center_x] = 1
        elif Y==3:
            for y in range(-radius, radius):
                for x in range(-radius, radius):
                    if abs(x) < (y+radius)/2:
                        X[y+center_y, x+center_x] = 1
        else: # blank image
        yield X, tf.one_hot(Y, 4)
        i = i + 1

We are now using one-hot encoding for our label:

        yield X, tf.one_hot(Y, 4)

For our four labels this will result in the following labels:

blank =    [1, 0, 0, 0]
square =   [0, 1, 0, 0]
circle =   [0, 0, 1, 0]
triangle = [0, 0, 0, 1]

To get categorical classification working we need to take note of a couple of things:

  1. We need to have the same number of output neurons as our classes - in this case four. We also need to use the softmax activation function. The softmax activation function will make sure the total output from all our neurons sum to 1. We can then use the output from each one as the probability that the input belongs to that class.
  2. We need to use the (CategoricalCrossentropy)[] loss function during our training.

Our simple model looks like this:

model = Sequential([
    Conv2D(8, 3,
           input_shape=(image_width, image_height, 1),
    Dense(4, activation='softmax', name='output')

And when we compile it we specify the loss function that we want to optimise:


This is a slightly more complex problem that the binary classification problem from this post so we need to train for more epochs to get 100% accuracy, but you should reach this in about 10 epochs.

You can test the model pretty easily by feeding in some more random samples from the training set:

# get a batch of samples from the dataset
X, _ = next(iter(train_dataset))
# ask the model to predict the output for our samples
predicted_Y = model.predict(X.numpy())
# work out the max indices
max_indices = tf.argmax(predicted_Y, axis = 1)
# show the images along with the predicted value
plot_images(X, max_indices)
# set the format to 2 decimal places
np.set_printoptions(formatter={'float': lambda x: "{0:0.2f}".format(x)})

If we look at the output from this:


You can see that it is pretty good at predicting the different categories that our images fall into:

array([[1.00, 0.00, 0.00, 0.00],
       [0.00, 1.00, 0.00, 0.00],
       [0.00, 0.00, 1.00, 0.00],
       [0.00, 0.00, 0.05, 0.95],
       [1.00, 0.00, 0.00, 0.00],
       [0.00, 1.00, 0.00, 0.00],
       [0.00, 0.00, 1.00, 0.00],
       [0.00, 0.00, 0.01, 0.99],
       [1.00, 0.00, 0.00, 0.00],
       [0.00, 0.96, 0.04, 0.00]], dtype=float32)

Checkout the full code in the GitHub repo.


Related Posts

TensorFlow Binary Classification - Dive into binary classification with TensorFlow through a hands-on example of training a model to recognize square and circle images. Key aspects discussed include using a single output neuron with a sigmoid activation function and leveraging the BinaryCrossentropy loss function for optimized performance.
Augmented Reality In The Browser - Explore an augmented reality Sudoku solver using browser APIs, TensorFlow js, and image processing techniques to recognize and solve puzzles captured by a device's camera. Understand the steps, including greyscale conversion, thresholding, puzzle location, corner detection, and neural network training for OCR.
Improving My Blog Using AI - Harness the power of AI tools, such as ChatGPT, to elevate your blog by automating tasks like tagging, generating summaries, assessing article relationships, and creating alluring images that keep readers captivated.
Vision Kit and CoreML - This tutorial demonstrates the process of wiring up the iPhone's camera to CoreML with the help of Vision Kit, allowing developers to run machine learning models on camera input and build image classification applications.

Related Videos

Browser-Based Augmented Reality Sudoku Solver using TensorFlow and Image Processing - Learn how to build a Sudoku app using browser APIs and modern techniques, including image processing pipeline, TensorFlow-powered neural networks, and OCR to efficiently identify, extract, and solve Sudoku puzzles.
TensorFlow Lite With and the ESP32 - Master the process of training a TensorFlow Lite model and deploying it on the ESP32 using PlatformIO with this comprehensive tutorial, complete with clear instructions and an informative video.
Voice Controlled Robot using the ESP32 and TensorFlow Lite - Master the process of building a voice-controlled robot with ESP32 and TensorFlow Lite, including creating neural networks, generating training data, and implementing firmware codes for a seamlessly programmed robot.
Build your own Alexa with the ESP32 and TensorFlow Lite - Create an Alexa-styled voice assistant using TensorFlow Lite, ESP32, and, with a detailed explanation of wake word detection, audio capture, and intent recognition
Vision framework and CoreML - Witness the incredible fusion of Apple's Vision framework and Core ML in this object identification demo, and grab the code from GitHub to explore this remarkable project further.
HELP SUPPORT MY WORK: If you're feeling flush then please stop by Patreon Or you can make a one off donation via ko-fi
Want to keep up to date with the latest posts and videos? Subscribe to the newsletter
Blog Logo

Chris Greening


> Image


A collection of slightly mad projects, instructive/educational videos, and generally interesting stuff. Building projects around the Arduino and ESP32 platforms - we'll be exploring AI, Computer Vision, Audio, 3D Printing - it may get a bit eclectic...

View All Posts