DEV Community

Cover image for Complete Guide to Build Your First CNN Machine Learning Model in Python
Dexter
Dexter

Posted on

Complete Guide to Build Your First CNN Machine Learning Model in Python

In this blog post, we will walk through a step-by-step guide on how to build your first Convolutional Neural Network (CNN) machine learning model in Python. CNNs are widely used for image recognition and classification tasks due to their ability to capture spatial hierarchies in data.

Step 1: Import Necessary Libraries

First, let's import the required libraries for building our CNN model:

import numpy as np
import matplotlib.pyplot as plt
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense
from keras.datasets import mnist
from keras.utils import to_categorical
Enter fullscreen mode Exit fullscreen mode

Step 2: Load and Preprocess Data

For this demo, we will use the MNIST dataset, which consists of handwritten digits. Let's load the dataset and preprocess the data:

# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()

# Normalize the pixel values to be between 0 and 1
x_train = x_train.astype('float32') / 255
x_test = x_test.astype('float32') / 255

# Reshape the input data
x_train = np.expand_dims(x_train, axis=-1)
x_test = np.expand_dims(x_test, axis=-1)

# One-hot encode the target labels
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
Enter fullscreen mode Exit fullscreen mode

Step 3: Build the CNN Model

Now, let's build our CNN model with the following architecture:

  • Convolutional layer with 32 filters, kernel size of 3x3, and ReLU activation
  • Max pooling layer with pool size of 2x2
  • Flatten layer to convert 2D feature maps to 1D
  • Dense layer with 128 units and ReLU activation
  • Output layer with 10 units (number of classes) and softmax activation
model = Sequential([
    Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 1)),
    MaxPooling2D((2, 2)),
    Flatten(),
    Dense(128, activation='relu'),
    Dense(10, activation='softmax')
])

model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
Enter fullscreen mode Exit fullscreen mode

Step 4: Train and Evaluate the Model

Next, let's train the model on the training data and evaluate its performance on the test data:

model.fit(x_train, y_train, batch_size=128, epochs=5, validation_data=(x_test, y_test))

loss, accuracy = model.evaluate(x_test, y_test)
print(f'Test loss: {loss}')
print(f'Test accuracy: {accuracy}')
Enter fullscreen mode Exit fullscreen mode

Conclusion

Congratulations! You have successfully built your first CNN machine learning model in Python using Keras. CNNs are powerful tools for image classification tasks, and with this guide, you are now equipped to explore more advanced CNN architectures and datasets. Happy coding!

Feel free to reach out if you have any questions or need further assistance.

Top comments (0)