CNN Keras Made Clear in 2 Minutes!

CNN Keras Explained in under 2 minutes

Share This Post

This guide explains cnn keras in simple terms. If you are learning artificial intelligence, neural networks, or deep learnings and want to know what a CNN is, what keras is, and what “cnn keras” stands for, you are in the right place.

What is a CNN?

In machine learning and deep learning, CNN is the acronym of Convolutional Neural Network. It is a type of neural network often used to classify images. That is, you feed images to your neural network, and it will tell you what’s in the picture. For example, you may use a CNN to classify types of food, tell if it sees a car or a truck, and so on.

In machine learning, you have neurons. Those are nodes thar receive data, elaborate it in some way, and pass it on to other neurons they are connected to. In a deep learning network, it is not necessary to have all the neurons in a layer connect to the neurons in the subsequent layer. However, in CNN, that is often the case: neurons are fully connected with each other, which means each neuron can communicate directly with every other.

To make a CNN, you will need multiple layers of neurons. A CNN has one input layer, one or more convolutional layers and pooling layers, and an output layer. If you don’t know or don’t fully understand the concept of these layers, don’t worry. We will see them later on in details, and you will learn how to create your cnn keras image classification network.

What is Keras?

Keras is a library that is part of TensorFlow, the deep learning library created by Google and now open source. Keras is specifically the part of TensorFlow that allows you to create deep learning neural network. It contains all the features you need to create layers and connect them with each other.

Keras goes together with TensorFlow, but it does not have too. It can work with other frameworks as well, such as Theano, PlaidML, or Microsoft Cognitive Toolkit. However, for the purpose of this cnn keras guide, we will focus on TensorFlow, which is by far the most popular.

Keras allows fast prototyping of neural networks, so it is ideal to learn machine learning and run experiments. Furthermore, it can run both on CPU and GPU.

CNN Keras: Build an Image Classification Network

Importing CNN Keras

What do we need to build a CNN with Keras and TensorFlow? For sure, we will need to have TensorFlow correctly installed on your machine, or run this code inside Google Colab which already comes with TensorFlow preinstalled. Then, we will need to import the following classes:

  • keras.models.Sequential to create a model. We could also use the functional API, but the Sequential API is simpler and easier to understand, and works like a charm when you want to create a neural network simply chaining neurons one to the other.
  • keras.layers.Conv2D This is a convolutional 2D network, and it is part of our CNN. In fact, it’s the “C” in “CNN keras”. Note that convolutional layers are available for 2D (to process images), 3D (to process three-dimensional objects), and 1D (to process stream of data, such as text).
  • keras.layers.MaxPool2D is another layer, specifically a type of pooling layer. Pooling simply means extracting the most relevant information from previous layers. In our case, the most relevant information is the max value, but other pooling layers exist (such as minimum, average, etc.).
  • keras.layers.Flatten is another layer. We start with a bidimensional input, the image, and we want to end up with a single-dimension output (which class the image belongs to). Flatten layers indeed flattens the 2-dimensional input into a single-dimension. We should use it right before producing our output.
  • keras.layers.Dense this is a simple neural network layer with simple neurons, and it is what we use for our output.

Considering this, we are now ready to create our network.

The code for CNN Keras

Below you find the full code to build a CNN keras network that works quite well to classify images. You can tweak the variables to adjust it to your problem.

from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Conv2D, MaxPool2D, Flatten, Dense

IMG_WIDTH = 224
IMG_HEIGHT = 223
COLOR_CHANNELS = 3
NUMBER_OF_CLASSES = 10

model = Sequential([
    Conv2D(10, 3, input_shape=(IMG_WIDTH, IMG_HEIGHT, COLOR_CHANNELS), activation="relu"),
    Conv2D(10, 3, activation="relu"),
    MaxPool2D(),
    Conv2D(10, 3, activation="relu"),
    Conv2D(10, 3, activation="relu"),
    MaxPool2D(),
    Flatten(),
    Dense(NUMBER_OF_CLASSES, activation="softmax")
])

model.compile(loss="categorical_crossentropy", metrics=["accuracy"])

Specifically, you can adjust the size of the image you want to provide (width and height) and the number of classes. The number of classes is the number of possible image types you want to train your model to recognize. For example, if your problem is classifying between cars, trucks, and motorbikes, you will have 3 classes.

Behind the CNN Keras Code: How does it work?

An image is a matrix of pixels, and each pixel can be represented as a numerical value indicating its color composition. This is what our CNN Keras will process.

The Convolutional layer (Conv2D) scans the image with the goal of producing an activation map. This activation map is the extraction of the most important features and characteristics out of an image, that will allow our network to classify it. To do that, we have a sliding window (usually 5 pixels per 5 pixels) that scans the image, starting at the top left and moving right and then down. Our sliding window will scan this square of pixels and extract only the most important one, to create a smaller image.

In the image below, we scan with a sliding window that is 3×3 and output a 1×1 (a single pixel). We can tweak both input (kernel size) and output, for example to scan 5×5 and produce 1×1, or scan 5×5 and produce 3×3, and so on. Note that in our example the image slides one pixel at a time, so for example the second pixel on each row is taken in both the first and second pass on that row. We call these passes strides. If you set strides to 2, your sliding window will slide 2 pixels at a time. The sliding window is also called kernel.

CNN Keras: the kernel slides over an image to create an activation map
Our kernel creates an activation map reducing our image with a 3 to 1 ratio. Source: medium.com.

The pooling layer replaces the output of al layer with a summary of its statistics. In our case, we use the max pooling, which replaces a group of inputs with the max value (among them). The following image from O’Reilly media is a great example to make it clearer.

CNN Keras MaxPool
The MaxPool will take the output of the kernel and reduce it to its max value. Since we a pooling layer in place, we can also have a kernel that does not reduce the image (e.g. 3×3 to 3x.3). Source: O’Reilly Media.

Finally, the last thing we need to consider is our activation function, which is ReLU – short for Rectified Linear Unit. The ReLU function simply cancels out all negative numbers to zero, and returns positive numbers unchanged.

f(x) = max(0, x)

As a result, the output of ReLU will be a chart like the one below.

Rectified Linear Unit (ReLU) for CNN keras.
ReLU will set all negative values to zero. Source: wikipedia.org.

Summary

In summary, CNN Keras allows you to create a neural network that can easily classify or categorize images. It does that by scanning the image by moving a narrow window over it, sliding it and analyzing only the pixels seen in that image.

With this technology, you can create a neural network that can classify the type of image easily. You may want to continue learning more about Software Design here.

Picture of Alessandro Maggio

Alessandro Maggio

Project manager, critical-thinker, passionate about networking & coding. I believe that time is the most precious resource we have, and that technology can help us not to waste it. I founded ICTShore.com with the same principle: I share what I learn so that you get value from it faster than I did.
Picture of Alessandro Maggio

Alessandro Maggio

Project manager, critical-thinker, passionate about networking & coding. I believe that time is the most precious resource we have, and that technology can help us not to waste it. I founded ICTShore.com with the same principle: I share what I learn so that you get value from it faster than I did.

Alessandro Maggio

2023-01-12T16:30:00+00:00

Opportunity

AI

2900