DEV Community

CNN: A Beginner's Guide to Image Recognition with Python

Category: Machine Learning | Deep Learning | Computer Vision

  1. Introduction

Images contain different patterns such as edges, shapes, textures and objects. Deep Learning provides techniques that allow computers to learn these visual features automatically.

A Convolutional Neural Network (CNN) is a deep learning model designed especially for image processing and computer vision.

CNNs are widely used in image recognition and classification applications.

  1. What is a CNN?

A Convolutional Neural Network is a type of Artificial Neural Network that is especially effective for image-like grid data.

It uses filters to detect local patterns and builds increasingly complex features through multiple layers.

For example, a CNN can learn to identify edges, shapes and objects in an image.

  1. How Does a CNN Work?

A CNN processes an image through different layers.

Input Image: The original image is given to the model.

Convolution: Filters move across the image to detect patterns.

ReLU Activation: Adds non-linearity to the model.

Pooling: Reduces the spatial dimensions of feature maps.

Dense Layer: Uses learned features to make a prediction.

CNN Architecture

Input Image → Convolution + ReLU → Pooling → Dense Classifier → Prediction

Convolution Formula

Feature Map = Input Image * Filter

The convolution operation applies a filter to local image regions and calculates responses to identify important features.

  1. Main CNN Layers

A. Convolution Layer

Detects patterns such as edges, shapes and textures using filters.

B. ReLU Activation Layer

Introduces non-linearity and helps the network learn complex patterns.

C. Pooling Layer

Reduces the dimensions of feature maps while retaining important information.

D. Flatten Layer

Converts feature maps into a one-dimensional vector.

E. Dense Layer

Uses the extracted features to produce the final prediction.

  1. Python Implementation

We can implement a simple CNN using TensorFlow and Keras.

In this example, we use the CIFAR-10 dataset, which contains 32 × 32 colour images belonging to 10 classes.

Step 1: Install TensorFlow

pip install tensorflow

Step 2: Python Program

import tensorflow as tf
from tensorflow.keras import layers, models

Load CIFAR-10 dataset

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data()

Normalize pixel values from 0-255 to 0-1

x_train = x_train.astype("float32") / 255.0
x_test = x_test.astype("float32") / 255.0

Create CNN model

model = models.Sequential([
layers.Input(shape=(32, 32, 3)),

layers.Conv2D(32, (3, 3), activation="relu"),
layers.MaxPooling2D((2, 2)),

layers.Conv2D(64, (3, 3), activation="relu"),
layers.MaxPooling2D((2, 2)),

layers.Flatten(),
layers.Dense(64, activation="relu"),
layers.Dense(10, activation="softmax")
Enter fullscreen mode Exit fullscreen mode

])

Compile the model

model.compile(
optimizer="adam",
loss="sparse_categorical_crossentropy",
metrics=["accuracy"]
)

Display model architecture

model.summary()

Optional training

model.fit(

x_train, y_train,

epochs=5,

validation_data=(x_test, y_test)

)

Step 3: Expected Output

When you execute the program, model.summary() displays:

Layer names and types.

Output shapes.

Number of parameters.

Total trainable parameters.

If you uncomment and execute the training section, the model trains for five epochs. Accuracy depends on training settings and runtime.

Note: Five epochs are used here for learning purposes and are not intended to produce state-of-the-art accuracy.

  1. Real-Life Applications of CNN

CNNs are used in many fields:

Face and object recognition.

Medical image analysis.

Quality inspection in manufacturing.

Traffic-sign recognition.

Image classification in mobile applications.

  1. Advantages of CNN

Automatically learns image features.

Captures spatial patterns.

Reduces the need for manually designed image features.

Can achieve strong performance on visual tasks.

  1. Limitations of CNN

Training may require substantial data.

Can require high computing power.

Models can be difficult to interpret.

Performance depends on data quality.

Can be sensitive to changes in image conditions.

  1. Conclusion

Convolutional Neural Networks are among the most important deep learning architectures used in computer vision.

Their convolution and pooling operations help models learn useful visual features from raw images.

Understanding CNN architecture provides a strong foundation for learning advanced image recognition and deep learning techniques.

  1. References and Further Reading

TensorFlow Learning Resources

Scikit-learn Documentation

GeeksforGeeks – Computer Science

Top comments (0)