CNN Algorithm Code in PythonConvolutional 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 |
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