# A Lightweight Crop Pest Detection Method Based on Convolutional Neural Networks
## 2 Materials and Methods
### Coordinate Attention  
The attention mechanism is a common optimization method for lightweight network structure design, and it can make the feature extraction process more focused and suppress  
Figure 5: Comparison of _svish_ and _hard-svish_ function curves. the adverse effects of irrelevant or harmful features on the task. Existing attention methods provide poor guidance for spatial information. For example, SENet [37] only pays attention to channel information, and convolutional block attention module (CBAM) [38] generates the attention maps of spatial information through convolution, but it is difficult to capture spatial information containing long-distance dependencies. However, pest objects in crop pest detection tasks are usually located in the complex environment, and the body of the pest usually has protective colors, as shown in Figure 6. The high degree of noise information in the environment makes it a challenging task to extract pest features from the global features. Therefore, it is particularly important to apply attention mechanisms containing more spatial information to guide the feature extraction process in crop pest detection tasks. Therefore, we introduce coordinate attention for the feature extraction process. In coordinate attention, the modeling process of channel attention is factorized into two pooling operations to embed more spatial information while capturing channel information. The structure of coordinate attention is shown in Figure 7a. The attention map built by coordinate attention contains channel information and spatial information, which can guide feature extraction in both the channel dimension and spatial dimension. In addition, benefiting from the pooling that acts on the spatial locations, the coordinate attention can effectively capture the long-distance dependence between spatial locations. Figure 6: Pests with protective colors in the natural environment. Figure 7: The coordinate attention block and the two types of residual blocks containing the coordinate attention block: (**a**) coordinate attention; (**b**) bottleneck-CA; and (**c**) LightSandGlass-CA. For the input feature map \(f_{in}\in\mathbb{R}^{C\times H\times W}\), coordinate attention uses horizontal and vertical pooling in each channel to encode the spatial information and channel information, as well as capture the long-distance dependence between spatial locations. The detailed process is shown in Equations (8) and (9), where \(f_{in}(\cdot,\cdot,\cdot)\) is the input value at the specified channel and spatial location, \(m(\cdot,\cdot)\) is the output at the specified channel and height after pooling, and \(n(\cdot,\cdot)\) is the output at the specified channel and width after pooling. \[m(c,h)=\frac{1}{W}\sum_{0\leq i\leq W-1}f_{in}(c,h,i) \tag{8}\]  
\[n(c,w)=\frac{1}{H}\sum_{0\leq j\leq{\rm H-1}}f_{in}(c,j,w) \tag{9}\]  
After completing the above encoding of the spatial and channel information channel by channel, the feature map \(m\in\mathbb{R}^{C\times H}\) and feature map \(n\in\mathbb{R}^{C\times W}\) are generated. Then, the dependency relationship between the channel dimension and spatial dimension is modeled, as shown in Equations (10)-(12). Firstly, \(m\) and \(n\) are connected and \([\cdot,\cdot]\) denotes the connecting feature maps. Then, \(f_{t}\in\mathbb{R}^{\frac{C}{\gamma}\times(H+W)}\) is split into \(f_{t}^{h}\in\mathbb{R}^{\frac{C}{\gamma}\times H}\) and \(f_{t}^{w}\in\mathbb{R}^{\frac{C}{\gamma}\times W}\) along the spatial dimension after the convolution \(\theta_{t}\) completes the dimension reduction. Finally, the number of channels of \(f_{t}^{h}\) and \(f_{t}^{w}\) are adjusted to be the same as the input \(f_{in}\) with two convolutional transforms, \(\theta_{h}\) and \(\theta_{w}\), respectively. \(\delta\) and \(\sigma\) are the nonlinear activation functions, and \(M\in\mathbb{R}^{C\times H}\) and \(N\in\mathbb{R}^{C\times W}\) are the direction-aware attention maps.