# Tomato Leaf Disease Classification by Combining EfficientNetv2 and a Swin Transformer
## 2 Methods
### Swin Transformer Backbone  
While the EfficientNetV2 branch leverages the enhanced EfficientNetV2 network for extracting local feature information, it struggles to adequately capture global feature information, particularly concerning large areas of disease in tomato images. In contrast to convolutional operations, which are renowned for their efficacy in extracting local features, the Swin Transformer excels in global modeling, thereby adeptly capturing global dependencies. Consequently, to preserve spatial information more effectively in tomato disease images, we introduce the Swin Transformer branch to complement the feature extraction process. The Swin Transformer is a visual model based on the Transformer architecture that enhances the applicability and generalizability of Transformers through techniques such as blockwise self-attention, hierarchical structure, and multiscale fusion. Its primary feature lies in its adoption of a hierarchical Transformer structure, where feature maps at various scales are constructed by merging image blocks. Moreover, it employs a shifted window mechanism to limit the computational scope of self-attention, facilitating cross-window connections. This hierarchical structure enables the Swin Transformer to adapt to various scale visual tasks, while the shifted window mechanism maintains linear computational complexity proportional to the image size. In the initial stage of this process, a 224 \(\times\) 224 \(\times\) 3 image is input into the patch partition and divided into multiple nonoverlapping patches of size 4 \(\times\) 4, where each patch has the dimensions of 4 \(\times\) 4 \(\times\) 3. In Stage 1, the partitioned patches are input into a linear embedding layer to transform the feature dimension to C and then fed into a Swin Transformer block. The Swin Transformer block performs self-attention calculations to extract image features, and the resulting output is passed to the next stage. For Stages 2 to 4, the process involves patch merging, where the feature maps input from the previous stage are merged into 2 \(\times\) 2 sizes to combine adjacent windows. These merged feature maps are then input into the Swin Transformer block to generate hierarchical feature representations. During this process, the patch merging layer handles downsampling and dimensionality increase, whereas the Swin Transformer block extracts features from the images. The Swin Transformer block module consists of a window-based multi-head self-attention (W-MSA) module, a shifted window-based multi-head self-attention (SW-MSA) module, a multilayer perceptron (MLP) module with a nonlinear GELU activation function, a residual connectivity layer, a normalization function, and the Swin Transformer block itself. The SW-MSA module is the core of the Swin Transformer block, which calculates self-attention by alternating between standard windows and shifted windows at different stages. This design enables the model to effectively capture global information while ensuring computational efficiency. In the process of self-attention computation, the model employs relative positional encoding to compute similarity and confines the attention  
Figure 3: The improved Fused-MBConv schematic diagram. Note: \(\oplus\) stands for channel-level addition. Figure 2: The improved MBConv schematic diagram. Note: \(\oplus\) stands for channel-level addition. calculation within each window, thereby mitigating computational overheads. The formula for self-attention calculation with relative positional encoding is as follows:  
\[Attention(Q,K,V)= Softmax\left(\frac{Q\times K^{T}}{\sqrt{d_{k}}}+B\right)V \tag{1}\]  
where \(B\) is the relative position code \(B\)\(\in\)\(R^{M^{2}\times M^{2}}\). \(M^{2}\) indicates the number of patches in the window. \(Q\), \(K\), \(V\)\(\in\)\(R^{M^{2}\times d}\), where \(d\) is the dimension of the matrix. \(\sqrt{d_{k}}\) is the scale factor used to avoid the influence of dot product variance, and \(k\) is the number of queries.