# A Hybrid Deep Learning Approach for Cotton Plant Disease Detection Using BERT-ResNet-PSO
## 3 The Proposed Hybrid Deep Learning Approach
### BERT-Based Encoder for Image Segmentation Dataset
#### 3.2.6 Final Classification Output  
The final classification of cotton leaf diseases is achieved using the PSO-optimized and ensemble-enhanced ResNet model. Evaluation metrics like Accuracy, Precision, Recall, and F1-Score are used to measure performance. These metrics are defined as follows:  
\[\text{Accuracy}=\frac{TP+TN}{TP+TN+FP+FN}\]  
\[\text{Precision}=\frac{TP}{TP+FP}\]  
\[\text{Recall}=\frac{TP}{TP+FN}\]  
\[\text{F1-Score}=2\times\frac{\text{Precision}\times\text{Recall}}{\text{ Precision}+\text{Recall}}\]  
where \(TP\), \(FP\), \(TN\), and \(FN\) stand for true positives, false positives, true negatives, and false negatives, respectively. These metrics provide a comprehensive evaluation of the model's performance in classifying the diseased regions in cotton leaf images. Algorithm 1 is developed for segmenting the cotton leaf background to allow disease region identification using a BERT-based neural network encoder. The first step is to process the raw images by resizing, normalizing and applying a Gaussian blur to decrease the noise, and then extracting the patches from the image. We put positional encodings on each patch and then put them through transformer encoder layers. Next, the encoded features are passed into a segmentation head to produce a segmentation mask that highlights diseased areas. We use a combination of Dice Loss and Cross Entropy Loss to optimize the segmentation process. ```
Objective: To segment the cotton leaf from the background and detect diseased regions using a BERT-based encoder. Step 1: Image Processing Input: Raw cotton leaf images I. Resize: Each image I to a fixed dimension (HXW). Normalize the pixel values to the range [0, 1]. Apply Gaussian Blur to reduce noise. \(I_{norm}=\frac{I-I_{min}}{I_{max}-I_{min}}\) Step 2: Patch Extraction and Embedding Divide the pre-processed image \(I_{norm}\) into N patches \(P_{i}\) of size p \(\times\) p \(P_{i}=\)\(Patch(I_{norm},\ i,p)\) Linearly embed each patch \(P_{i}\) and add positional encodings. \(E_{i}=\)\(Linear\)\((P_{i})+PositionEmbedding(i)\) Step 3: Transformer Encoding Pass the embedded particle \(E_{i}\) through a series of transformer encoder layers. \(Z_{i}=\)\(TransformationEncoder(E_{i})\) Step 4: Segmentation Mask Prediction Feed the encoded features \(Z_{i}\) into a segmentation head to generate the segmentation mask M \(M=SegmentationHead(Z)\) Calculate the segmentation loss using a combination of Dice loss and Cross-Entropy loss. \(L_{seg}=\lambda_{1}\times Dice\)\(Loss(M,G)+\lambda_{2}\times\)\(Cross\)\(Entropy\)\(Loss(M,G)\) Output: Segmentation mask M highlighting diseased regions. ```  
**Algorithm 2**Feature Mapping Using ResNet  
Algorithm 2 is developed for extracting features from the segmented image using a ResNet architecture. Feature maps are generated by passing the concatenated form of the segmented image and its corresponding mask through a ResNet model. Each of these feature maps is 'condensed' via global average pooling into a feature vector, which summarizes the important image properties of the input image. This vectorizes this image into a smaller feature vector that can be used in further analysis of the segmented image. ```
Objective: To extract and map features from the segmented image using a ResNet architecture. Step 1: Image Preparation Input: Segmentate image \(I_{seg}\) and corresponding segmentation mask M from Algorithm 1 Concatenate: The segmented image \(I_{seg}\) with the segmentation mask M. \(I_{concatenate}=\)\(I_{seg}\oplus M\) Step 2: ResNet Feature Extraction Pass the concatenated image \(I_{concat}\) through the ResNet architecture to extract feature maps \(F_{ResNet}\) \(F_{ResNet}\) \(=\)\(ResNet(I_{concat})\) Step 3: Global Average Pooling Apply global average pooling to the feature maps to obtain a condensed feature vector \(F_{GAP}\) \(F_{GAP}=\frac{1}{H\times W}\sum\limits_{i=1}^{H}\sum\limits_{j=1}^{w}F_{ResNet}( i,j)\) Output: Feature vector \(F_{GAP}\) representing the input image.