# Enhancing DR Classification with Swin Transformer and Shifted Window Attention

Meher Boulaabi, Takwa Ben Aïcha Gader, Afef Kacem Echi, Zied Bouraoui

► To cite this version:

Meher Boulaabi, Takwa Ben Aïcha Gader, Afef Kacem Echi, Zied Bouraoui. Enhancing DR Classification with Swin Transformer and Shifted Window Attention. Artificial Intelligence in Medicine - 23rd International Conference, AIME 2025, Jun 2023, Italie, Italy. pp.57-61, 10.1007/978-3-031-95841-0_11. hal-05265424

HAL Id: hal-05265424

https://hal.science/hal-05265424v1

Submitted on 17 Sep 2025

HAL is a multi-disciplinary open access
archive for the deposit and dissemination of sci-
entific research documents, whether they are pub-
lished or not. The documents may come from
teaching and research institutions in France or
abroad, or from public or private research centers.

L'archive ouverte pluridisciplinaire HAL, est
destinée au dépôt et à la diffusion de documents
scientifiques de niveau recherche, publiés ou non,
émanant des établissements d'enseignement et de
recherche français ou étrangers, des laboratoires
publics ou privés.

<figure><img src="image_1.png" /></figure>

Distributed under a Creative Commons Attribution 4.0 International License
Enhancing DR Classification with Swin
Transformer and Shifted Window Attention

Meher Boulaabi¹[0009-0000-6773-2781], Takwa Ben Aïcha
Gader³[0000-0002-3786-3649], and Afef Kacem Echi³[0000-0001-9219-5228]) Zied
Bouraoui²[0000-0002-1662-4163]

¹ University of Tunis, ENSIT-LaTICE Lab - University of Monastir, FSM

² Artois University, CRIL-CNRS, France

³ University of Tunis - ENSIT-LaTICE Lab., Tunisia

**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.

**Keywords:** Diabetic Retinopathy · Swin Transformer · Medical Image Classification

# 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:

**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)^2C$ and $\Omega(\text{W-MSA}) = 4hwC^2 + 2M^2hwC$ 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
<figure><img src="image_2.png" /><figcaption>**Fig. 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.</figcaption></figure>

with shifted window partitioning is the increased number of windows in the sec-
ond configuration, some of which are smaller. This affects spatial dependency
capture. To mitigate this, *cyclic shifting* pads edge windows with neighboring re-
gions, 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).
<table><caption>**Table 1.** Performance on the IDRiD dataset with and without preprocessing.</caption><thead><tr><th rowspan="2">Class</th><th colspan="3">W/o preprocessing</th><th colspan="3">W/ preprocessing</th></tr><tr><th>Precision</th><th>Recall</th><th>F1-Score</th><th>Precision</th><th>Recall</th><th>F1-Score</th></tr></thead><tbody><tr><td>Grade 0</td><td>0.45</td><td>0.38</td><td>0.41</td><td>0.96</td><td>1.00</td><td>0.98</td></tr><tr><td>Grade 1</td><td>0.00</td><td>0.00</td><td>0.00</td><td>1.00</td><td>1.00</td><td>1.00</td></tr><tr><td>Grade 2</td><td>0.37</td><td>0.78</td><td>0.51</td><td>1.00</td><td>0.92</td><td>0.96</td></tr><tr><td>Grade 3</td><td>0.67</td><td>0.21</td><td>0.32</td><td>0.93</td><td>1.00</td><td>0.97</td></tr><tr><td>Grade 4</td><td>1.00</td><td>0.08</td><td>0.14</td><td>1.00</td><td>1.00</td><td>1.00</td></tr></tbody></table>

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.

*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 shows that preprocessing plays a critical role in boosting performance, with notable improvements over models trained without it. These results further underscore the potential of Swin Transformers for medical image analysis, showcasing their precision and reliability in clinical settings.

## 4 Conclusion

We introduced a Swin Transformer-based model for DR classification, addressing class imbalance, pixel similarity, and subtle retinal variations. By integrating
<table><caption>**Table 2.** Class-wise Precision, Recall, and F1-Score for Aptos and IDRiD Datasets</caption><thead><tr><th rowspan="2">Class</th><th colspan="3">Aptos</th><th colspan="3">IDRiD</th></tr><tr><th>Precision</th><th>Recall</th><th>F1-Score</th><th>Precision</th><th>Recall</th><th>F1-Score</th></tr></thead><tbody><tr><td>Grade 0</td><td>0.94</td><td>0.98</td><td>0.96</td><td>0.96</td><td>1.00</td><td>0.98</td></tr><tr><td>Grade 1</td><td>0.86</td><td>0.68</td><td>0.76</td><td>1.00</td><td>1.00</td><td>1.00</td></tr><tr><td>Grade 2</td><td>0.82</td><td>0.89</td><td>0.86</td><td>0.92</td><td>0.92</td><td>0.92</td></tr><tr><td>Grade 3</td><td>0.81</td><td>0.68</td><td>0.74</td><td>0.92</td><td>0.86</td><td>0.89</td></tr><tr><td>Grade 4</td><td>0.88</td><td>0.77</td><td>0.82</td><td>1.00</td><td>1.00</td><td>1.00</td></tr><tr><td>Overall</td><td>0.86</td><td>0.80</td><td>0.83</td><td>0.96</td><td>0.96</td><td>0.96</td></tr></tbody></table>

<table><caption>**Table 3.** Comparison of models for multi-class classification.</caption><thead><tr><th>Method</th><th></th><th>Dataset Accuracy (%)</th></tr></thead><tbody><tr><td>Blended features + DNN [2]</td><td>Aptos</td><td>80.96</td></tr><tr><td>Deep dual-branch model (transfer learning) [3]</td><td>Aptos</td><td>89.60</td></tr><tr><td><strong>Our Method</strong></td><td><strong>Aptos</strong></td><td><strong>89.65</strong></td></tr><tr><td>SqueezeNet classifier [4]</td><td>Idrid</td><td>94.20</td></tr><tr><td>CNN, SqueezeNet, ResNet-50, Inception-V3, DFTSA-Net, CLAHE [1]</td><td>Idrid</td><td>95.00</td></tr><tr><td><strong>Our Method</strong></td><td><strong>Idrid</strong></td><td><strong>97.40</strong></td></tr></tbody></table>

a comprehensive preprocessing pipeline with image cropping, CLAHE, and augmentation, we enhanced feature extraction and model performance.

## Acknowledgments

This work was supported by ANR-22-CE23-0002 ERIANA.

## References

1. Nazir, T., Irtaza, A., Rashid, J., Nawaz, M., Mehmood, T.: Diabetic retinopathy lesions detection using faster-rcnn from retinal images. In: 2020 First International Conference of Smart Systems and Emerging Technologies (SMARTTECH). pp. 38–42. IEEE (2020)

2. Qummar, S., Khan, F.G., Shah, S., Khan, A., Shamshirband, S., Rehman, Z.U., Khan, I.A., Jadoon, W.: A deep learning ensemble approach for diabetic retinopathy detection. Ieee Access 7, 150530–150539 (2019)

3. Shakibania, H., Raoufi, S., Pourafkham, B., Khotanlou, H., Mansoorizadeh, M.: Dual branch deep learning network for detection and stage grading of diabetic retinopathy. Biomedical Signal Processing and Control 93, 106168 (2024)

4. Uppamma, P., Bhattacharya, S.: Diabetic retinopathy detection: A blockchain and african vulture optimization algorithm-based deep learning framework. Electronics 12(3), 742 (2023)