## 3 Fine-Grained Image-Recognition Method for Fruit Flies
### Multi-Channel Self-Attention Feature Fusion of Fine-Grained Images of Fruit Flies
The calculation formula of matrix \(C\) is as follows:  
\[C_{i,j}=\lambda_{1}\Big{(}F^{1}_{t,j}\Big{)}\cdot\lambda_{2}\Big{(}F^{2}_{t,j} \Big{)} \tag{7}\]  
where \(\lambda_{1}\) and \(\lambda_{2}\) are convolution operations by branches \(Q_{1}\) and \(Q_{2}\), respectively; \(C_{i,j}\) can be understood as the element value in column \(j\), row \(i\), in matrix \(C\), representing the influence of the \(j\) th fine-grained local attention feature map in \(F^{2}_{t}\) on the \(i\) th fine-granular global attention feature map in \(F^{1}_{t}\). To prevent gradient explosion, the \(C\) matrix is obtained line by line through the softmax function of the weight matrix \(\omega\). The calculation formula of the matrix \(\omega\) is as follows:  
\[\omega_{i,j}=\frac{\exp\big{(}C_{i,j}\big{)}}{\sum_{i=1}^{N}\sum_{j=1}^{N}\exp \big{(}C_{i,j}\big{)}} \tag{8}\]  
where the sum of the elements \(\omega_{i,j}\) in each row is 1; the influence of the \(i\)-row element in the \(\omega\) matrix represents the attention feature graph of all the fine-grained images of all the fruit flies in feature \(i\), which is the weight. The steps of multi-channel self-attention feature fusion of fruit fly fine-grained images are as follows:  
Step 1: Unify size of feature maps  
Check the global attention feature map \(F^{1}_{t}\) and local attention feature map \(F^{2}_{t}\), conduct adaptive maximum pooling to reduce the spatial dimension to consistency, and maximum pooling can well preserve the texture information of the fruit fly images [21]; so, \(F^{1}_{t}\) and \(F^{2}_{t}\) are not only aggregates of a large amount of spatial information, but also effectively reduce the calculation amount of the deep learning model and improve the recognition accuracy of fruit fly fine-grained images [22]. Step 2: Channel recalibration  
The expression ability of attention features of the same scale in each channel is different. Calculate the weight of each channel by combining the correlation matrix of attention features with Equation (9) \(\omega_{i}\). Multiply the weight matrix and the attention feature map to obtain a new attention feature representation after channel recalibration \(F^{t1}_{t}\) and \(F^{2}_{t}\), enhancing the expression ability of the fine-grained feature map in the channel. The method of the attention weight acting on fruit fly fine-grained image features can be seen as the process of encoding the input fruit fly features under an information selection mechanism [23]. For a single dimension soft attention weight map, the most common fusion method of attention and fruit fly features is to multiply the corresponding attention feature map using the attention weight in the form of a dot product:  
\[ \begin{array}{l}F^{t1}_{t}=\sum_{i=1}^{N}\omega_{i}\cdot F^{1}_{t,j}\\ F^{2}_{t}=\sum_{i=1}^{N}\omega_{i}\cdot F^{2}_{t,j}\end{array}  \tag{9}\]  
In the deep learning model, the feature mean corresponding to each group of attention weights is introduced. The average attention is a network parameter that represents the average attention of each channel corresponding to the low-level features of all fine-grained fruit fly images. The introduction of the feature mean can extract higher-order features that are more relevant to the fruit fly category, improve the expression ability of the output feature fusion results, and improve the recognition accuracy of fruit fly fine-grained images. For the fine-grained attention feature map and attention weight of each channel \(\omega_{i}\), the mean value of attention can be expressed as \(\omega_{i}\). Then, the fusion method of the attention feature map and channel attention weight in Formula (9) can be rewritten as follows:  
\[ \begin{array}{l}F^{t1}_{t}=\frac{1}{N}\sum_{i=1}^{N}\omega_{i}\cdot\Big{(}F^{ 1}_{t,j}-\bar{\omega}_{i}\Big{)}\\ F^{t2}_{t}=\frac{1}{N}\sum_{i=1}^{N}\omega_{i}\cdot\Big{(}F^{2}_{t,j}-\bar{ \omega}_{i}\Big{)}\end{array}  \tag{10}\]Step 3: Attention feature fusion Sum the weighted attention feature map to obtain the fruit fly characteristics \(T\).