## 3 Methods
### External Attention  
In image processing, most pixels are only closely related to a small number of elements. The standard attention score matrix has a lot of redundancy, and self-attention mechanisms can only focus on intra-sample correlations. To enable features to be more accurately implemented and to generate inter-sample correlations, [PERSON] et al. [29] proposed the external attention mechanism. This is an attention mechanism constructed by two linear layers and two normalized layers, which not only takes into account the correlation between all samples but also reduces the computational complexity. The calculation steps of self-attention are written as follows in Equation (9):  
\[ \begin{array}{ll}A=(a)_{i,j}=soft\max(QK^{T})&a_{i,j}=\overrightarrow{q_{i}} \cdot\overrightarrow{k_{j}}\\ F_{out}=AV&f_{i}=\sum a_{i,j}v_{j}\end{array}  \tag{9}\]  
The similarity calculation in self-attention can be understood as projecting the samples onto a specific subspace. Here, represents the attention score matrix, where represents the correlation between the \(i\)-th pixel of the query and the \(j\)-th pixel of the key. Then, we weight and sum each to obtain the final attention score. EA, on the other hand, uses a shared subspace to calculate the attention score, as in Equation (10). \[ \begin{array}{ll}A=(a)_{i,j}=Norm(FM^{T})&a_{i,j}=\overrightarrow{q_{i}} \cdot\overrightarrow{m_{j}}\\ F_{out}=AM&f_{i}=\sum a_{i,j}m_{j}\end{array}  \tag{10}\]  
All queries are projected onto the subspace \(M\) to calculate \((a)_{i,j}\) and then weighted and summed with \(m_{j}\) to reconstruct \(f_{j}\). The matrix \(A\) is inferred from prior knowledge of the dataset and is normalized using double norm. In contrast to the self-attention mechanism, \((a)_{i,j}\) in the external attention mechanism represents the similarity between the \(i\)-th pixel and the \(j\)-th row of \(M\), where \(M\in R^{5\times d}\) is the memory unit that serves as a shared base space, similar to the x, y, and z axis in a 3D coordinate system. It can represent features from all feature spaces. Because it is shared, it can model the relationships between all samples, and it is calculated with each pixel, forming connections with each pixel as well. The memory matrix \(M\) is initialized randomly and records the memory of theentire training set. The external attention mechanism is computed using the following Equations (11)-(12), where \(M_{k}\) and \(M_{v}\) represent the parameter matrices of two linear transformations, similar to \(K\) and \(V\) in the attention mechanism. As \(M\) is a shared space, during each training sample, the features in \(M_{k}\) and \(M_{v}\) are updated and iterated through the similarity matrix in \(A\), providing the ability to model the correlation between different samples. The computational complexity of the external attention is \(O(dSN)\), where \(d\) is the feature dimension, \(N\) is the number of elements, and \(S\) is the number of elements in the memory unit \(M\). \[A=DoubleNorm({FM_{k}}^{T}) \tag{11}\]  
\[F_{out}=AM_{v}. \tag{12}\]  
As shown in Figure 3, in the self-attention mechanism, normalization is usually performed using Softmax normalization along each row. However, this can lead to issues in which a single column in query or key with extremely large or small values can cause the corresponding column in the attention matrix to be extremely large or small as well, leading to difficulty in representing the similarities between pixels. Therefore, the external attention mechanism uses double normalization to eliminate this effect. As in Equations (13)-(15), in order to prevent a column in the attention map from being too large or too small, EA first applies Softmax to the query matrix and to the result of matrix multiplication, column by column, which prevents the values inside the feature map from being too large or too small.