Enhancing DR Classification with Swin Transformer and Shifted Window Attention
Meher Boulaabi, Takwa Ben Aïcha Gader, Afef Kacem Echi Zied Bouraoui · 2025
Read Enhancing DR Classification with Swin Transformer and Shifted Window Attention by Meher Boulaabi, Takwa Ben Aïcha Gader, Afef Kacem Echi Zied Bouraoui (2025) in the Tower of Babel, a free online library of public-domain works preserved in clean, searchable text. No sign-up. The full text is served as a plain-text file at a predictable URL, where text is present: https://antarctic-labs.com/tower-of-babel/arxiv-2504-15317.txt.
Introduction
Meher Boulaabi and co-authors wrote this 2025 paper on grading diabetic retinopathy, an eye disease that can blind people with diabetes. Early grades are hard to spot, and photos vary in quality, so machines still miss cases. The team pairs a strong cleaning pipeline for the photos with the Swin Transformer, a model that reads images in small shifted windows.
Cleaning steps crop the photo, lift local contrast, and add varied copies to balance the classes. On two public data sets, Aptos and IDRiD, the system scored about 90 and 97 percent correct. The authors say the gains were strongest for early-stage disease, where fast care helps most. That focus matters most for patients whose sight can still be saved.
Catalog record
| CREATOR | Meher Boulaabi, Takwa Ben Aïcha Gader, Afef Kacem Echi Zied Bouraoui |
|---|---|
| YEAR | 2025 |
| COLLECTION | PAPERS |
| FORMAT | TXT |
| FILE SIZE | 10 KB |
| VERSION | 1.0 |
| LICENSE | http://creativecommons.org/licenses/by/4.0/ |
| RIGHTS | CC BY |
| SOURCE | arXiv |
Source: https://arxiv.org/abs/2504-15317
Browse the full library · About the Tower of Babel
Full text
The opening of the work, from the library's hosted text:
Enhancing DR Classification with Swin Transformer and Shifted Window Attention Meher Boulaabi, Takwa Ben Aïcha Gader, Afef Kacem Echi Zied Bouraoui Abstract Diabetic retinopathy (DR) is a leading cause of blindness worldwide, underscoring the importance of early detection for effective treatment. However, automated DR classification remains challenging due to variations in image quality, class imbalance, and pixel-level similarities that hinder model training. To address these issues, we propose a robust preprocessing pipeline incorporating image cropping, Contrast-Limited Adaptive Histogram Equalization (CLAHE), and targeted data augmentation to improve model generalization and resilience. Our approach leverages the Swin Transformer, which utilizes hierarchical token processing and shifted window attention to efficiently capture fine-grained features while maintaining linear computational complexity. We validate our method on the Aptos and IDRiD datasets for multi-class DR classification, achieving accuracy rates of 89.65% and 97.40%, respectively. These results demonstrate the effectiveness of our model, particularly in detecting early-stage DR, highlighting its potential for improving automated retinal screening in clinical settings. 1 Introduction Diabetic Retinopathy (DR) is a microvascular complication of diabetes and the leading preventable cause of blindness among the working-age population worldwide. Early detection and timely intervention are essential to preventing vision loss. The emergence of deep learning revolutionized DR detection by enabling automated feature learning directly from raw images. Transformer-based architectures have recently emerged as powerful tools in medical image analysis, offering a compelling alternative to traditional deep learning models. Vision Transformers (ViTs), in particular, leverage self-attention mechanisms to capture long-range dependencies, enabling rich representations of both local and global features. However, standard ViTs are limited by their quadratic computational complexity with respect to image size, making them inefficient for high-resolution medical images. To overcome this limitation, we employ Swin Transformers, which introduce a hierarchical architecture with shifted window-based self-attention. This approach reduces complexity to linear with image size, enhancing computational efficiency for large-scale visual data. The shifted windows also improve feature extraction by effectively modeling both local details and global context, while maintaining scalability. For diabetic retinopathy (DR) classification, we implement a comprehensive pipeline that includes dataset preparation, preprocessing, and augmentation. We apply image cropping to eliminate irrelevant background regions, reducing noise and enhancing focus on clinically relevant features. Contrast-Limited Adaptive Histogram Equalization (CLAHE) is used to improve local contrast and highlight subtle structures, aiding feature extraction. To address class imbalance, we apply data augmentation techniques such as rotation and horizontal flipping, enriching the training data and improving generalization. Together, these steps contribute to more accurate and robust DR classification across varying severity levels. 2 Methodology Our model’s architecture is illustrated in Figure 1 . The Swin Transformer serves as the backbone. Unlike ViTs, which process images in a global self-attention framework, the Swin Transformer applies shifted window-based self-attention, reducing computational complexity and improving efficiency for high-resolution medical images. Below, we outline its key components: Figure 1: DR classification pipeline. The input images undergo preprocessing with CLAHE and data augmentation to enhance robustness. Images are divided into patches using PatchExtract and projected into a high-dimensional space via PatchEmbedding. These embeddings pass through transformer blocks, where Module 1 applies standard window-based self-attention and Module 2 uses shifted window attention for better cross-window learning. Residual connections and MLP layers with dropout ensure stability and improved feature extraction. Finally, PatchMerging reduces spatial dimensions, followed by global average pooling and a dense layer with softmax classifier. Swin Transformer Block: Each consists of a shifted window-based Multi-Head Self-Attention (MSA) module, followed by a two-layer Multi-Layer Perceptron (MLP) with a GELU activation function. Each MSA module and MLP is preceded by a LayerNorm (LN) layer, and residual connections are incorporated to improve gradient flow and prevent vanishing gradients. Window-Based Self-Attention (W-MSA): Unlike traditional self-attention mechanisms, W-MSA operates within local non-overlapping windows, significantly reducing computational overhead. Given an image of size h\times w with each window containing M\times M patches, the computational complexity for global MSA versus window-based MSA is \Omega(\text{MSA})=4hwC^{2}+2(hw)^{2}C and \Omega(\text{W-MSA})=4hwC^{2}+2M^{2}hwC where C is the embedding dimension. Global self-attention computation, as in the standard Vision ViT, is computationally expensive for large hw , whereas window-based self-attention is more scalable. Shifted Window Partitioning in Successive Blocks (SW-MSA). In consecutive blocks, the first module applies regular window partitioning, while the second module shifts the windows, allowing for improved feature continuity and better spatial representation. The forward propagation for two consecutive Swin Transformer blocks is given as \hat{z}^{l}=\text{W-MSA}(\text{LN}(z^{l-1}))+z^{l-1} , z^{l}=\text{MLP}(\text{LN}(\hat{z}^{l}))+\hat{z}^{l} , \hat{z}^{l+1}=\text{SW-MSA}(\text{LN}(z^{l}))+z^{l} , and z^{l+1}=\text{MLP}(\text{LN}(\hat{z}^{l+1}))+\hat{z}^{l+1} , where z^{l-1} denotes the input feature representations from the preceding layer. A challenge with shifted window partitioning is the increased number of windows in the second configuration, some of which are smaller. This affects spatial dependency capture. To mitigate this, cyclic shifting pads edge windows with neighboring regions, ensuring feature continuity and improving spatial processing. This shifted partitioning strategy improves the ability of the model to capture long-range dependencies while maintaining computational efficiency. 3 Experiments This section analyzes the performance of our model against baseline methods on the APTOS and IDRiD datasets, widely used for diabetic retinopathy detection. Datasets. We work on grading DR using the IDRiD and APTOS datasets, focusing on classifying the five severity levels. These include level 0 (no apparent retinopathy), level 1 (mild non-proliferative diabetic retinopathy), level 2 (moderate), level 3 (severe), and level 4 (proliferative diabetic retinopathy, or PDR). Although the two datasets differ in terms of image quality, quantity and characteristics, they share the same grading scale. We applied the same classification algorithm to both datasets and introduced variations to ensure robust performance and generalizability across different data sources. Data Preprocessing. To enhance image quality and improve model performance, we applied a consistent preprocessing pipeline to the IDRiD and APTOS datasets First, image cropping was performed to remove extraneous background elements while preserving the essential circular structure of the eye, ensuring the model focused on relevant retinal features. Next, CLAHE was used to enhance contrast and visibility, particularly in areas with irregular illumination, improving feature clarity in DR images. Both datasets were then organized into training, validation, and testing sets. To improve generalization, data augmentation techniques, including random rotations (up to 360 degrees) and horizontal flips, were applied to simulate real-world imaging variability and reduce overfitting risks Table 1 highlights the impact of preprocessing, demonstrating its role in enhancing classification by addressing pixel similarity and image quality variability. Table 1: Performance on the IDRiD dataset with and without preprocessing. \multirow 2*Class W/o preprocessing W/ preprocessing Precision Recall F1-Score Precision Recall F1-Score Grade 0 0.45 0.38 0.41 0.96 1.00 0.98 Grade 1 0.00 0.00 0.00 1.00 1.00 1.00 Grade 2 0.37 0.78 0.51 1.00 0.92 0.96 Grade 3 0.67 0.21 0.32 0.93 1.00 0.97 Grade 4 1.00 0.08 0.14 1.00 1.00 1.00 Training. The model is trained using a patch size of (16,16) and a learning rate of 1\times 10^{-3} and a batch size of 32 for 200 epochs. Softmax activation and Cross-Entropy loss are used for classification, with the AdamW optimizer and early stopping set to 15 epochs. Results. Table 2 highlights the model’s strong specificity and sensitivity, confirming its suitability for real-world clinical applications. It also demonstrates the effectiveness of our model and preprocessing pipeline in accurately classifying all DR severity levels, from grade 0 to grade 4. Additionally, Table 3