# Efficient Metal Corrosion Area Detection Model Combining Convolution and Transformer
## 2 Materials and Methods
### Transformer Sequential Encoding  
Metal corrosion area features grow randomly, exhibiting irregular shapes and distributions. Traditional convolutional encoders struggle to extract local features effectively and have limitations in capturing global information. In recent years, the Transformer has been widely applied in the field of digital image processing. It is a specialized model for handling sequential data and was initially used in natural language processing (NLP) for machine translation, achieving state-of-the-art performance in translation tasks. The core of the Transformer encoder lies in its use of a multi-head self-attention mechanism (MSA), which calculates the dependencies between sequences and builds global contextual information representations, thus learning the relationships between sequences [19].  
The Transformer encoder used here is composed of self-attention and multiple perception mechanisms, with the core process illustrated in Figure 5. The basic steps of this encoding process are as follows:  
1. Feature Serialization: The convolutional encoder features with a shape of \(C\times H\times W\) are serialized, where \(C\) represents the number of feature channels, and \(H\times W\) represents the height and width of the feature map. The features are serialized into \(N\) patches of size \(P\times P\), represented by a tensor of size \(N\times C\times P\times P\). The serialized feature vector \(V_{i}\) is expressed by the following equation: \[V_{i}=\textit{reshape}\Big{(}\Big{(}1,C\times P^{2}\Big{)}\Big{)}\] (1)
2. Positional Encoding: Positional information is introduced into the input feature representation by adding a positional encoding, which is learnable. It is expressed by the following equation: \[E_{i}=f(v_{i})=v_{i}W+p_{i}+b\] (2) \[E=[E_{1};E_{2};\cdots;E_{N}]\] (3)  
Figure 4: ResNet50 backbone network sub-module.  
Figure 5: Transformer encoder structure.  
In the equation, \(W\) represents the weight matrix, \(P_{i}\) denotes the positional encoding, \(E_{i}\) is the feature vector with positional encoding added, and \(E\) represents the features after incorporating positional encoding;
3. Self-Attention Calculation: For the sequence elements \(x_{1},x_{2},\cdots,x_{N}\), the attention for the initial element \(x_{i}\) is represented by Equation (4), where \(W_{q},W_{k},W_{v}\) are the model parameters: \[y_{i}=\sum_{j=1}^{N}\frac{e^{x_{i}W_{q}\cdot(x_{j}W_{k})^{T}}}{\sum_{t=1}^{N}e ^{x_{i}W_{q}\cdot(x_{i}W_{k})^{T}}x_{j}W_{v}}\] (4) For the input matrix \(X\in R^{N\times D}\), the self-attention operation can be expressed by Equation (5): \[Y=\mathit{softmax}(XW_{q}\big{(}XW_{k}\big{)}^{T}\big{)}XW_{v}\] (5)
4. Attention Weighted Sum and Layer Normalization (LN): The attention weights are applied to the input feature representations, and the attention information is fused into the original features through a weighted sum followed by normalization;
5. Multi-layer Perceptron (MLP): After attention calculation, the output is passed through a multi-layer perceptron. The role of the MLP is to introduce non-linear mapping and perform feature transformation, fusion, interaction, and dimensional transformation.