DEV Community

Cover image for Building a Convolutional Neural Network with Keras
Niladri Das
Niladri Das

Posted on

Building a Convolutional Neural Network with Keras

Real-world Project:

Self-Driving Car Simulation: This involves creating a machine-learning model that can drive a car in a simulated environment. This project is particularly relevant for _Tesla_. You can use libraries like TensorFlow and Keras and a simulator like Udacity's self-driving car simulator.

Here, I have tried to explain, how we'll walk through the process of building a Convolutional Neural Network (CNN) using Keras. CNNs are a type of deep learning model that is especially good at processing images.

  • Who should follow this process?

The processes we've completed so far in this computer practical lab research are typically done by a Machine Learning Engineer or a Data Scientist. These professionals are responsible for designing and implementing machine learning models like the Convolutional Neural Network (CNN) we've built.

We define the architecture of the model (i.e., the type and sequence of layers in the model), compile the model with a loss function and an optimizer, and then train the model on a dataset.

We've defined the architecture of a CNN using Keras, a popular high-level neural networks API. This model is designed for image processing tasks, which is a common application of CNNs.

Setting Up Keras

First, you'll need to install Keras. You can do this with pip:

pip install keras
Enter fullscreen mode Exit fullscreen mode

You'll also need to install a backend for Keras. We recommend TensorFlow:

pip install tensorflow
Enter fullscreen mode Exit fullscreen mode

The Data

For this example, we're using placeholder data. In a real-world scenario, you would replace this with your actual data loading code.

A neural network is a computational model inspired by the structure and function of the human brain's neural networks. It consists of interconnected nodes, called neurons, organized in layers. Each neuron receives input signals, processes them, and generates an output signal, which may be passed to other neurons. Neural networks are capable of learning complex patterns and relationships from data through a process called training.

import numpy as np

# Load your data here
X_train = np.random.rand(1000, 160, 320, 3)  # 1000 images of size 160x320x3
y_train = np.random.rand(1000)  # 1000 labels
Enter fullscreen mode Exit fullscreen mode

In a real-world scenario, you would likely load your data from files, preprocess it (e.g., normalize or standardize it, handle missing values, etc.), and split it into training and validation sets.

Building the Model

We start by defining a Sequential model. This type of model is appropriate for a plain stack of layers where each layer has exactly one input tensor and one output tensor.

from keras.models import Sequential
from keras.layers import Flatten, Dense, Lambda, Cropping2D
from keras.layers import Conv2D
from keras.layers import Input

model = Sequential()
model.add(Input(shape=(160,320,3)))
model.add(Lambda(lambda x: x / 255.0 - 0.5))
model.add(Cropping2D(cropping=((70,25),(0,0))))
model.add(Conv2D(24, (5,5), strides=(2,2), activation="relu"))
model.add(Conv2D(36, (5,5), strides=(2,2), activation="relu"))
model.add(Conv2D(48, (5,5), strides=(2,2), activation="relu"))
model.add(Conv2D(64, (3,3), activation="relu"))
model.add(Conv2D(64, (3,3), activation="relu"))
model.add(Flatten())
model.add(Dense(100))
model.add(Dense(50))
model.add(Dense(10))
model.add(Dense(1))
Enter fullscreen mode Exit fullscreen mode

Convolutional Neural Networks (CNNs) are a specific type of neural network designed for processing structured grid data such as images. They are particularly powerful for tasks like image classification, object detection, and image segmentation. CNNs derive their name from the mathematical operation they heavily rely on, called convolution.

Training the Model

Next, we compile the model with a mean squared error loss function and the Adam optimizer. Then we train the model on our data.

model.compile(loss='mse', optimizer='adam')
Enter fullscreen mode Exit fullscreen mode

Once the model is compiled, we can train it on our data.

model.fit(X_train, y_train, validation_split=0.2, shuffle=True, epochs=5)
Enter fullscreen mode Exit fullscreen mode

CNNs are composed of multiple layers, including convolutional layers, pooling layers, and fully connected layers. The convolutional layers apply convolution operations to the input data, extracting features such as edges, textures, and shapes. The pooling layers downsample the feature maps generated by the convolutional layers, reducing their spatial dimensions while retaining important information. Finally, the fully connected layers combine the features learned by the previous layers to make predictions.

Evaluating the Model

After training, it's important to evaluate the model's performance. This could involve calculating accuracy, precision, recall, F1 score, or other relevant metrics. It could also involve visualizing the model's predictions or errors. In this example, we're not doing any evaluation, but you should definitely include this step in your real-world projects.

Saving the Model

Finally, we save the trained model to a file. This allows us to load the model later without having to retrain it.

model.save('model.keras')
Enter fullscreen mode Exit fullscreen mode

CNNs have revolutionized the field of computer vision by automatically learning hierarchical representations of visual data, allowing them to achieve state-of-the-art performance on various image-related tasks. Their effectiveness comes from their ability to automatically learn hierarchical patterns and features directly from raw pixel data, without the need for manual feature engineering.

Training Outcome

The output you're seeing is the training progress of our model. Each "Epoch" represents a complete pass through your training data. The "loss" is the value of the loss function for your training data, and "val_loss" is the value of the loss function for your validation data. The goal is to minimize these values.

Training data

From the output, the model is learning as the loss decreases over epochs. However, the validation loss is not decreasing significantly, which might indicate that the model is not generalizing well to unseen data.

NOTE: You might want to consider adjusting your model architecture, tuning hyperparameters, or using more data for training to improve the model's performance.

Conclusion

The process we've completed so far is called "defining the model architecture" or "building the model".

In this process, we've defined a Sequential model using Keras and added various layers to it. These layers include an Input layer, a Lambda layer for normalization, a Cropping2D layer for cropping the images, several Conv2D layers for convolution operations, a Flatten layer to flatten the output of the convolution layers, and Dense layers which are fully connected layers.

This model architecture is typically used for image processing tasks and is a common setup for Convolutional Neural Networks (CNNs).

We've walked through the process of building, training, and saving a CNN using Keras. This is a basic example, and there's a lot more you can do with Keras and CNNs. Happy coding!

Remember, to follow for Machine Learning or Data Science projects, you can also follow me on LinkedIn, my social account 𝕏, GitHub platform and https://www.youtube.com/@niladrridas to study more! 🚀💻

If you find any bugs please tap to reveal my email or DM me on 𝕏 🫶

Top comments (0)