## 2 Methods
### Improved Self-Attention Mechanism  
The Vision Transformer encoder consisted of L (in this study, L was set to 6) stacked encoding modules, each composed of multi-head self-attention (MSA) and a multi-layer perceptron (MLP). The structure of the encoding module is illustrated in Figure 4. The MSA module consisted of \(N_{h}\) (in this paper, \(N_{h}\) was set to 8) individual self-attention heads. The self-attention mechanism was a fundamental component of the Vision Transformer architecture, playing a critical role in establishing pixel-level relationships and capturing global image information. Its architecture is illustrated in Figure 4.  
For the self-attention mechanism, firstly, the input \(Z_{p}\) underwent linear transformations to obtain the query matrix \(Q\), key matrix \(K\), and value matrix \(V\). Secondly, the query  
Figure 4: ViT encoding module and self-attention mechanism. (**a**,**b**) were inspired by [PERSON] et al. (2020) [15] and [PERSON] et al. (2017) [27], respectively. Figure 3: Spatial attention module. matrix \(Q\) was used to compute the attention weight matrix \(A\) in combination with the key matrix \(K\). The computation process is as follows:  
\[A=\left(\alpha\right)_{i,j}=Softmax\left(\frac{Q\otimes K^{T}}{\sqrt{d_{k}}}\right) \tag{2}\]  
In the equation, \(A\) represents the attention weights, where \(\left(\alpha\right)_{ij}\) denotes the similarity between the \(i\)th and \(j\)th pixels. \(\sqrt{d_{k}}\) is the scaling factor given by \(d_{k}=d_{m}/N_{h}\). Finally, the output feature was obtained via matrix multiplication between the attention weight matrix \(A\) and the value matrix \(V\), followed by a residual connection with the feature map \(Z_{p}\). \[Z_{out}=Z_{p}\ \oplus\ A\ \otimes\ V \tag{3}\]  
The computational cost of MSA is \(o(2d_{m}n^{2}+4d_{m}^{2}n)\), where the computation grows quadratically with the number of image tokens or their dimensions, resulting in significant training and inference overhead. Furthermore, in the multi-head attention mechanism, each head was responsible for a sub-layer of the embedding dimension, which may compromise the network's performance, especially when the token embedding dimension is small. The architecture of EMSA is depicted in Figure 5a. Similar to the standard self-attention mechanism, efficient self-attention first obtained the query matrix \(Q\) through linear mapping. To compress the memory, the two-dimensional tokens were transformed into three-dimensional tokens, and then, the size was reduced through depthwise separable convolution (DWconv) to obtain two-dimensional feature maps. The feature maps were further mapped to obtain the key matrix \(K\) and value matrix \(V\). The query matrix \(Q\) was multiplied with the key matrix \(K\), followed by scaling, standard \(1\times 1\) convolution, softmax layer, and Instance Normalization, resulting in the attention weight matrix for the attention mechanism. Figure 5: Multi-head self-attention: (**a**) efficient multi-head self-attention; (**b**) improved efficient multi-head self-attention. (**a**) were inspired by [PERSON] et al. (2020) [20]. Due to EMSA's ability to effectively address issues such as large computational requirements and suboptimal network performance, this study, in response to research requirements, replaced the MSA with the EMSA and introduced improvements tailored for fine-grained image processing tasks with small-sample datasets. The specific enhancement involved substituting Instance Normalization with Batch Normalization within the EMSA framework. This modification aimed to enhance the model's generalization capability on test samples, mitigate overfitting issues, and improve model robustness. The improved EMSA architecture is illustrated in Figure 5b.