CNN Algorithm Code in Python5 Jan 2025 | 6 min read Convolutional neural network algorithm (CNN) is a deep learning algorithm well-suited for image processing. They are composed of convolutional, pooling, and fully connected layers. Convolutional layers play a vital role in Convolutional Neural Networks (CNNs). These layers employ filters that help extract image features such as edges, textures, and shapes. The filters are small matrices applied to the input image in a sliding window fashion. The convolutional layer outputs a feature map containing the filter responses to the input image. The pooling layers are utilized to downsample the feature maps created by the convolutional layers. It helps reduce the network's computational cost and makes it more invariant to small translations and rotations in the input image. Architecture of CNNCNN has various architectures, but first, let us look into base architecture. A Convolutional Neural Network (CNN) comprises three layers: convolutional layers, fully-connected (FC) layer and spooling layers. These layers are arranged in a stacked manner to form a CNN architecture. Apart from these three layers, the dropout layer and the activation function are two more important parameters. These parameters are defined below. The architecture consists of different layers, starting with the convolution layer. Convolution layer:The convolutional layer, also known as the conv layer, is a basic unit of Convolutional Neural Networks (CNNs). It has a significant role in capturing local patterns and features in grid-like data, like images. Let's take a quick look at what a convolutional layer does and its primary components: The first step in a neural network is to extract features from input images using a mathematical operation called convolution. In this layer, a filter of a particular size(MxM) is convolved with the input image. The filter is slid over the input image, and the dot product is computed between the filter and the input image parts corresponding to the filter size (MxM). This process helps to extract key features from the input image. The input undergoes convolution operation in CNN, which passes the result to the next layer.
Pooling Layer:In a Convolutional Neural Network (CNN), the typical structure involves Convolutional Layers followed by Pooling Layers. The Pooling Layer reduces the size of convolved feature maps, reducing computational costs. It is achieved by decreasing the number of connections between layers, and it functions independently on each feature map. There are several types of pooling operations available:
The Pooling Layer connects the Convolutional Layer and the Fully Connected (FC) Layer. Its main purpose is to summarize the features generated by the Convolutional Layer. By pooling, the CNN model can recognize and generalize these features independently. It not only helps in feature abstraction but also reduces the computational demands within the network. Fully Connected LayerAn FC layer comprises weights and biases for each neuron. Each neuron in an FC layer connects to every neuron in the previous layer. During training, the weights and biases are learned. Before entering the fully connected layer, the feature maps obtained from previous convolutional and pooling layers are flattened into a one-dimensional vector to match the one-dimensional input expected by the FC layer. Activation functions like ReLU are applied to the flattened input in the FC layer, introducing non-linearity and allowing the network to capture complex patterns. Fully Connected (FC) layers are crucial in the classification process. They are responsible for extracting high-level features and making decisions based on the features learned in the preceding layers. The output of these layers is typically utilized to determine the probabilities of different classes or regression values, depending on the particular task at hand. DropoutDropout is a technique commonly used in neural networks, including Convolutional Neural Networks (CNNs), to combat overfitting. Overfitting occurs when a model evolves too specialized in learning from the training data and fails to generalize well to unseen data. Overfitting is an expected issue in deep learning models, where the neural network learns noise or specific patterns in the training data that don't generalize to new data. Dropout is employed to address this problem. In dropout, during the training process, a specified fraction of neurons (nodes) in a layer is randomly "dropped out" or deactivated. It means these neurons do not contribute to the forward or backward pass during a particular training iteration. Implementation of CNN Algorithm in PythonThe following is the snippet of code illustrating the implementation of CNN Algorithm in Python Example:Output: Epoch 1/10 1563/1563 [==============================] - 63s 39ms/step - loss: 1.4819 - accuracy: 0.4600 - val_loss: 1.1862 - val_accuracy: 0.5725 Epoch 2/10 1563/1563 [==============================] - 58s 37ms/step - loss: 1.1218 - accuracy: 0.6045 - val_loss: 1.0347 - val_accuracy: 0.6363 Epoch 3/10 1563/1563 [==============================] - 59s 38ms/step - loss: 0.9765 - accuracy: 0.6589 - val_loss: 0.9404 - val_accuracy: 0.6690 Epoch 4/10 1563/1563 [==============================] - 58s 37ms/step - loss: 0.8796 - accuracy: 0.6933 - val_loss: 0.9010 - val_accuracy: 0.6878 Epoch 5/10 1563/1563 [==============================] - 58s 37ms/step - loss: 0.8060 - accuracy: 0.7206 - val_loss: 0.8983 - val_accuracy: 0.6904 Epoch 6/10 1563/1563 [==============================] - 56s 36ms/step - loss: 0.7441 - accuracy: 0.7402 - val_loss: 0.8833 - val_accuracy: 0.6990 Epoch 7/10 1563/1563 [==============================] - 57s 37ms/step - loss: 0.6917 - accuracy: 0.7587 - val_loss: 0.8629 - val_accuracy: 0.7065 Epoch 8/10 1563/1563 [==============================] - 57s 37ms/step - loss: 0.6466 - accuracy: 0.7721 - val_loss: 0.8479 - val_accuracy: 0.7154 Epoch 9/10 1563/1563 [==============================] - 58s 37ms/step - loss: 0.6065 - accuracy: 0.7848 - val_loss: 0.8652 - val_accuracy: 0.7150 Epoch 10/10 1563/1563 [==============================] - 58s 37ms/step - loss: 0.5613 - accuracy: 0.8031 - val_loss: 0.8726 - val_accuracy: 0.7095 313/313 - 3s - loss: 0.8726 - accuracy: 0.7095 - 3s/epoch - 9ms/step Test accuracy: 0.7095000147819519 The Conclusion:CNNs in Python have had a significant effect on the field of computer vision and machine learning. They are highly effective in various applications, such as image recognition, object detection, and segmentation. However, success with CNNs requires access to substantial data, computational resources, and hyperparameter tuning and model interpretation expertise. As deep learning advances, CNNs are expected to remain a cornerstone of modern machine-learning applications. Next TopicBrute-force-algorithm-in-python |
? Python is a high-level, interpreted programming language acknowledged for its clarity and ease. Created through Guido van Rossum and first released in 1991, Python emphasizes code clarity with its high-quality use of big whitespace. It helps with more than one programming paradigm, inclusive of procedural,...
3 min read
A Brief Introduction to Collatz Sequence The Collatz conjecture also called the "3n + 1" conjecture or the Hailstone sequence, is a math problem that has been craving the minds of mathematicians for years. Named after the German mathematician Lothar Collatz, who first introduced it in 1937,...
8 min read
? Python is a high-level programming language known for its readability and ease of use. Behind its simplicity lies a sophisticated interpreter that plays a crucial role in executing Python code. In this article, we'll delve into the inner workings of the Python interpreter, exploring how...
3 min read
? Python is a versatile programming language that has gained immense popularity for its simplicity and readability. If you are starting your Python journey on a Windows machine, you'll likely encounter the need to install additional packages or libraries to enhance your development experience. This is...
6 min read
? Visualizing the connections between variables is crucial for understanding complex datasets. Seaborn's correlation heatmaps offer an elegant way to reveal these patterns, empowering you to identify strongly correlated features and guiding feature engineering and model selection. It uncovers hidden relationships that might otherwise go unnoticed....
3 min read
Data Science is a growing and developing technology and study to extract insights and trends from the data. It involves different steps, including data wrangling, data exploring, analysis, visualization, prediction, and many others. Basically, it starts with data collection, cleaning, processing, modeling, and then evaluating...
7 min read
In this problem, we are given a complete binary tree. A Complete binary tree has two subnodes for every node of the tree except for the leaf nodes. Our task is to count the total number of nodes that the given binary tree has. Let us...
7 min read
In data analysis with Python, the Pandas library is a powerful tool for handling tabular data, offering various functionalities for data manipulation and preprocessing. One common preprocessing task involves missing values, often denoted as NaN. Dropping rows containing NaN values from a DataFrame is a...
25 min read
Python is a beeswax when it comes to libraries that make complex tasks run and for audio manipulation, Pydub is among the best with simplicity and many features. In fact, irrespective of the level of proficiency in audio processing, Pydub is a wonderful tool that can...
9 min read
? Introduction to Functions in Python Definition and Role: In Python, functions are key units of code that encapsulate a set of activities or computations. They act as building blocks for organizing and putting together code, working with particularity and reusability. Functions permit developers to embody rationale into...
9 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India