# Rapid and Accurate Crayfish Sorting by Size and Maturity Based on Improved YOLOv5
## 3 Method
### Improved YOLOv5
#### 3.2.2 Coordinate Attention  
Channel attention has become popular in object detection [32], as seen in SE [33] and CBAM [34]. While SE attention focuses only on the inter-dependencies between channels, neglecting the importance of spatial information, and CBAM attention considers local position information through reducing the input tensor's channel dimensionality and using convolution to obtain local information, it falls short in capturing long-range dependencies present in the feature map. Therefore, this paper introduces the Coordinate Attention (CA) [35], which optimizes the structure of the attention mechanism by incorporating position information while attending to channel information, directional sensitivity, and position information. This mechanism not only achieves fast and accurate localization of the target object but also reduces the model's complexity. Figure 7 shows the Coordinate Attention structure.  
In Coordinate Attention, global average pooling is divided into two parallel 1D feature encodings, which are aggregated horizontally and vertically. This process produces two independent feature maps that incorporate positional information and capture long-range dependencies in a direction-sensitive manner. The Coordinate Attention module comprises two steps. First, embedding coordinates preserves the spatial position information while effectively encoding channel relationships and capturing long-term dependencies. Then, generating Coordinate Attention captures long-range dependencies and channel relationships by incorporating the embedded positional information. To achieve precise positional information for remote spatial interaction within the attention module, a pair of 1D feature  
Figure 6: MHSA layer utilized in BoT module.  
encodings are employed. To encode each channel's information along the horizontal and vertical coordinates, an (H, 1) or (1, W) pooling kernel is utilized on the given input \(x_{c}\), as shown in Equation (1). The c-th channel generates an output that possesses a height of h and a width of \(\omega\), which is given by Equations (2) and (3). By aggregating features along both spatial directions and retaining spatial position information, the Coordinate Attention module achieves remote spatial interaction and improves the model's accuracy in locating objects of interest.  
\[Y_{c}=\frac{1}{H\times W}\sum_{m=1}^{H}\sum_{n=1}^{W}x_{c}(m,n) \tag{1}\]  
\[Y_{h}=\frac{1}{W}\sum_{0\leq m<W}x_{c}(h,m) \tag{2}\]  
\[Y_{w}=\frac{1}{H}\sum_{0\leq n<H}x_{c}(n,w) \tag{3}\]  
Combining the structure illustrated in Figure 7, in the above equation, the input \(x_{c}\) is obtained directly from a convolutional layer, which employs a fixed kernel size. In the current attention module, h and \(\omega\) signify the dimensions of the input feature map, the size of the pooling kernel is denoted by H and W. \(Y_{c}\), \(Y_{h}\), and \(Y_{w}\) correspond to the outputs related to the c-th channel, where \(Y_{h}\) represents the output corresponding to height h and \(Y_{w}\) represents the output corresponding to width \(\omega\).  
After embedding the coordinate information, the two feature maps are combined through concatenation and the number of channels is reduced by applying compression with a Conv2d layer. The information regarding spatial relationships in both the horizontal and vertical orientations is encoded, and the results undergo parallel Conv2d layers to enhance the channel dimensions, followed by the application of a Sigmoid activation function to introduce nonlinearity, and the final Coordinate Attention is obtained by the multiplication of the corresponding elements between the attention maps and the input feature map.