# A Lightweight Forest Test Image Recognition Model Based on Improved YOLOv8
## 3 Methods
### CBAM Attention Module  
Given that the background occupies a substantial portion of the images in the utilized dataset, and the predominant targets for detection are small-sized pests, the detection performance of the YOLOv8n algorithm hinges predominantly on the efficiency of the backbone network. To enhance the backbone network's capacity for extracting critical information, we introduce a Convolutional Block Attention Module (CBAM) into the YOLOv8 backbone, as depicted in Figure 5. The CBAM model comprises a Channel Attention Module (CAM) and a Spatial Attention Module (SAM), dedicated to extracting channel and spatial attention, respectively. Through adaptive feature refinement facilitated by channel and spatial attention mechanisms, the model endeavors to identify attention regions within densely populated pest scenarios. This incorporation aims to elevate the model's efficacy in discerning salient features in the presence of prevalent background and small-sized targets, thereby aligning with the rigorous standards of scientific discourse.  
Figure 4: Slim-Neck Structure of YOLOv8.  
Figure 3: (**a**) Structure of the GS Bottleneck module and (**b–d**) Structures of VoV-GSCSP1, VoV-GSCSP2, and VoV-GSCSP3 modules.  
Given the input feature \(\mathrm{F}\in\mathbb{R}^{C\times H\times W}\), where C, H, and W represent the channel number, height, and width of the feature map, respectively. After the Channel Attention Module (CAM), channel attention features denoted as \(\mathrm{M_{c}}\) are obtained, as shown in Equation (1). \(\mathrm{M_{c}}\) is then multiplied with the feature map \(\mathrm{F}\) resulting in the feature \(\mathrm{F^{\prime}}\). Following the Spatial Attention Module (SAM), spatial attention features denoted as \(\mathrm{M_{s}}\) are obtained, as expressed in Equation (2). \(\mathrm{M_{s}}\) is subsequently multiplied with the feature \(\mathrm{F^{\prime}}\), yielding the refined feature \(\mathrm{F^{\prime\prime}}\).  
\[\mathrm{F^{\prime}}=\mathrm{M_{c}}(\mathrm{F})\otimes\mathrm{F} \tag{1}\]  
\[\mathrm{F^{\prime\prime}}=\mathrm{M_{s}}\big{(}\mathrm{F^{\prime}}\big{)} \otimes\mathrm{F^{\prime}} \tag{2}\]  
In this investigation, the CBAM attention module is introduced following the C2f module in the YOLOv8 backbone network, incurring a negligible computational overhead. This strategic enhancement aims to provide the deep network with more accurate feature information, thereby contributing to the reduction in loss values and ensuring precise identification and localization of small targets, especially in the context of forestry pest detection. From an intuitive perspective, during the forward propagation of gradients, crucial channels and spatial information in the feature map receive greater emphasis. This refinement is evident in the final output image, effectively accentuating regions of interest for the detection model and enhancing its ability to discern target objects accurately. This improved approach significantly elevates the model's performance in object detection tasks, demonstrating enhanced potential in effectively handling intricate image scenarios, aligning with the rigorous standards of scientific discourse.