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 |
Programming languages are the foundation of software, which is applied during the development of software applications, websites, games, and a number of other computer-based products. Among the available languages, GDScript and Python are distinct, though they cater to different needs based on a developer's preference. GDScript...
9 min read
Geocoding is the process of associate places described by their addresses with specific coordinates that allows placing some markers on the map or positioning the points in the Geographic Information System. Python has many ways of performing geocoding and one of the most popular tools is...
4 min read
Facebook scraping refers to the automatic collection of data obtained from the social networking platform. Individuals and organizations frequently use ready-made web scraping tools or create their own scrapers to accomplish this. After the data has been gathered, it is cleaned up and arranged into an...
19 min read
? Introduction Designing animations in Python can not only be entertaining but also fulfill educational purposes. Python, which offers numerous libraries that can help you to create animations from very simple GIFs to more complex interactive visualizations, is a powerful language that is the most versatile of...
4 min read
Skipping a Line of Code (LoC) in Python Skipping a line of code in Python might seem unusual, but it serves various practical purposes, especially when writing, testing, or refining code. Below are some common scenarios where skipping a line becomes useful: Debugging While debugging, you might want to...
4 min read
An Introduction to Robot Operating System (ROS) In ROS (Robot Operating System), a publisher allows nodes to send messages to a specific topic that other nodes can subscribe to. Using Python, publishers are created with the `rospy` library. A ROS publisher defines the topic, message type, and...
7 min read
Functions in programming play a crucial role in encapsulating reusable blocks of code. In Python, applying a function to each element of a list involves systematically processing each item in the list using a specific operation or transformation defined by a function. This approach is...
8 min read
Introduction: In this tutorial we are learning about how to . When processing XML data in Python, making it readable and structured can improve the understandability and manageability of your code. Printing the XML nicely or formatting it with appropriate indentation and line breaks is an...
5 min read
Introduction The Python development environment provides an abundant set of pre-created modules and functions that give developers complete power to create and operate powerful and efficient applications. One such module, os, gives access to the OS underneath and operates as a link between these systems and...
4 min read
Introduction Data manipulation is a fundamental aspect of data analysis, and Python's Pandas library is a powerful tool for this purpose. One particularly useful feature of Pandas is the str.extract() method, which allows you to extract substrings from a Series of strings using regular expressions. In...
3 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