# Resizer Swin Transformer-Based Classification Using sMRI for Alzheimer's Disease
## 3 Methodology
### Model Structure
#### 3.2.4. Swin Transformer  
The Swin Transformer utilizes the within-window calculation of self-attention to increase modeling efficiency. Beginning in the top-left corner, the window evenly and non-overlappingly divides the image into sections. Assuming that there are \(M\times M\) patches in a window, the next module utilizes a different window than the previous layer and moves an \(\left(\left|\frac{M}{2}\right|,\left|\frac{M}{2}\right|\right)\) patch from the original window when the window-based self-attention module completes its computation. The calculation process of the Swin Transformer block is as follows:  
\[\hat{\mathbf{Z}}^{l}= \text{W-MSA}\,\left(\text{LN}\!\left(\mathbf{Z}^{l-1}\right)\right)+ \left(\mathbf{Z}^{l-1}\right),\] \[\mathbf{Z}^{l}= \text{MLP}\,\left(\text{LN}\!\left(\hat{\mathbf{Z}}^{l}\right)\right) +\hat{\mathbf{Z}}^{l},\]  
Figure 3: Resizer module in our proposed model for resizing images. The original size of the image is (181, 217), and the bilinear Resizer up-samples the image to a size of (224, 224) as the input for the Swin Transformer.  
\[\dot{\mathbf{Z}}^{l+1}= \text{SW-MSA}\left(\text{LN}\Big{(}\mathbf{Z}^{l}\Big{)}\right)+\mathbf{Z} ^{l},\] \[\mathbf{Z}^{l+1}= \text{MLP}\Big{(}\text{LN}\Big{(}\mathbf{Z}^{l+1}\Big{)}\Big{)}+\mathbf{Z} ^{l+1} \tag{3}\]  
where \(\dot{\mathbf{Z}}^{l}\) and \(\mathbf{Z}^{l}\) are multi-headed self-attentive based on the window and shifted window, respectively, and characterized by the output of the multilayer perceptron (MLP) module. Window movement enables the reciprocal learning of patches between several windows, thus achieving the goal of global modeling. Figure 4 depicts the model of the Swin Transformer structure as well as the calculation procedure of the block.  
Window multi-head self-attention (W-MSA) adds a relative position bias \(\mathbf{B}\in\mathbb{R}^{M^{2}\times M^{2}}\) for each head in the calculation of multi-headed self-attention as follows:  
\[\text{Attention}(\mathbf{Q},\mathbf{K},\mathbf{V})=\text{Softmax}\Big{(}\mathbf{Q}\frac{\mathbf{K }^{T}}{\sqrt{d}}+\mathbf{B}\Big{)}\mathbf{V} \tag{4}\]  
where \(\mathbf{Q}\), \(\mathbf{K}\), and \(\mathbf{V}\in\mathbb{R}^{M^{2}\times d}\) are the query, key, and value matrices, respectively. \(d\) is the dimension of the key. The number of patches in the window is \(M^{2}\). Given that the relative location is within \([-M+1,M-1]\), the bias matrix \(\hat{\mathbf{B}}\in\mathbb{R}^{(2M-1)\times(2M-1)}\) is set. The values of \(\mathbf{B}\) are derived from \(\mathbf{B}\). \(\mathbf{Q}\), \(\mathbf{K}\), and \(\mathbf{V}\) are calculated from \(\mathbf{W}_{\mathbf{Q}}\), \(\mathbf{W}_{\mathbf{K}}\), and \(\mathbf{W}_{\mathbf{v}}\) by applying a linear transformation of \(\mathbf{Z}^{l-1}\).  
On the other hand, shifted-window multi-head self-attention (SW-MSA) uses the circular window movement rule to compute the multi-headed self-attentiveness.  
Figure 4: Swin Transformer and its block structure. (**a**) The overall structure of the Swin Transformer. A Swin Transformer block contains two attention calculations. The specific computation process is shown in (**b**).