An Attention-Enhanced ViT-HLNN Hybrid Ensemble Framework for Multi-Class Gastrointestinal Disease Classification Authors: Abdullah, Muhammad Ateeb Ather, Zulaikha Fatima, José Luis Oropeza Rodríguez, Carlos Guzmán Sánchez-Mejorada, Rolando Quintero Téllez, Miguel Jesús Torres Ruiz Published: 2026 ABSTRACT Gastrointestinal (GI) diseases such as polyps, esophagitis, and ulcerative colitis pose significant diagnostic challenges due to subtle visual patterns. Early, accurate, and scalable diagnostic tools are essential for improving outcomes and streamlining care. In this study, we propose a hybrid deep learning framework for multi-class GI disease classification that integrates a Vision Transformer (ViT) with a Hierarchical Long-Short-Term-Memory Neural Network (HLNN), further enhanced through ensemble learning using Bagging and Stacking. This hybrid architecture captures both global dependencies and local spatial–temporal features in endoscopic imagery, enabling precise multi-class classification. The model was trained and validated on the Kvasir-v2 dataset, comprising over 8000 labelled endoscopic images across eight GI disease classes. The proposed method achieved up to 99.99% accuracy on the test set and 99.96% mean accuracy across tenfold cross-validation, with macro F1-scores exceeding 0.999, and Cohen’s kappa of 0.994, indicating excellent agreement. Test set performance was evaluated on a fixed, class-stratified held-out test set (100 images per class) and reported as the mean across multiple training runs with different random seeds, ensuring statistical robustness. In comparative analysis, we demonstrated that the proposed hybrid model outperforms established baselines, including ViT-only, MobileNet-V2, EfficientNet-B0, ResNet-101, and NASNet-Large. Controlled ablation studies validate the individual and combined contributions of HLNN and ensemble strategies. Robustness was further confirmed under Gaussian noise, motion blur, and illumination shifts, with accuracy consistently above 96%. Zero-shot external evaluation on the independent GastroVision dataset and class-holdout testing on the held-out Polyps class further demonstrate generalization beyond the Kvasir-v2 training distribution. Robustness of Grad-CAM explanations was validated through a sanity check, correlation r = 0.91, and focused attention entropy 0.32, confirming stability under perturbations. These results highlight the potential of the proposed framework for robust and interpretable GI image classification. Introduction GI diseases are still a public health problem affecting societies around the world and causing high morbidity and death rates. The World Health Organization (WHO) claims that around 1.8 million people annually pass away due to maladies of the digestive system1,2. Globally, in 2019 alone, there were more than seven billion cases of gastrointestinal disorders recorded, the cases of which were described as severe or critical, amounting to 2.8 million3. Colorectal cancer is one of the well-represented and lethal types of GI disease in the United States, as the market reported 149,500 and 52,980 new cases and deaths in 2021, respectively. Furthermore, the overall cases of colorectal cancer since 2015 were estimated to be about 1.32 million cases, whereas the cases of bowel infections were about 1.6 million cases, with 200,000 new cases coming up every year4. Although there have been developments in medical approaches and medical technologies, the death rate caused by gastrointestinal diseases has been on an increasing trend between 2000 and 2019, which means that there is still a lot of work to do in diagnosing the disease in a premature stage and intervening5. Colonoscopy and endoscopy in general are regarded as the standard in the diagnosis of diseases of the gastrointestinal tract, providing direct access to the mucosal surfaces, allowing the identification of various pathologies, tumor processes, inflammatory changes, and bleeding foci6. Colonoscopy is essential in providing diagnosis of colorectal diseases, including colorectal tumors, inflammatory bowel disease, and colonic bleeding, and diagnosing colorectal mucosal lesions through mucosal biopsy as well as visual inspection7. Endoscopic interpretation is, however, highly subjective and vulnerable to error, although this is very important in clinical practice. The differences in the rates of detecting lesions of different endoscopists, as well as the impact of fatigue and supervision, could cause the endoscopist to overlook a lesion, thus transitioning into the more serious stages of the disease8. Colonoscopy miss lesions are a major source of post-colonoscopy colorectal cancers, increased costs to the healthcare system, and decreased patient outcomes9. Artificial intelligence (AI) within the field of medical imaging has become a potentially beneficial intervention to combat these diagnostic issues. Deep learning algorithms, especially those based on convolutional neural networks (CNNs), have proven to be exceptionally promising in improving the accuracy of the diagnosis10, as they are able to automatically extract features of medical imaging and detect minor patterns that can be otherwise missed by humans11. The use of AI in diagnosis is especially useful in the standardization of interpretation, decreasing the likelihood of errors in diagnosis and aiding in clinical decision-making, especially where the process is complex, and the ability to judge is problematic in humans12. The budding popularity of AI-based solutions is associated with the possibility of making the process significantly more consistent, with the diagnostic speed being improved, and the overall load on the healthcare system being decreased13. The availability of high-quality and diverse data is one of the most important elements of developing successful AI-based diagnostic systems14. Here, one of the key findings has been the emergence of the Kvasir dataset, which is useful in furthering the research in automated detection of GI diseases15. The given dataset has a large variety of endoscopic images with both healthy and diseased results that could be used to train models to define different GI cases16. The variety and completeness of the Kvasir dataset allow for the development of AI models that can generalize well in multiple clinical situations, increasing their possibilities of deployment into the real world17. Whereas AI technology leads to improvements in gastroenterological diagnostics, in the context of AI and gastroenterology, optimal performance and generalizability are still relevant issues18. Robustness of models is typically impaired with differences in data distributions, noise, and outliers. Moreover, for the successful implementation of the model in everyday life, it is necessary to make it interpretable and achieve clinical acceptance19. Thus, the need to keep developing the architecture of the models, strategies of data augmentation, and models of interpretability to indicate that the gap between experimental and clinical usefulness becomes closer20. In this research, we aim to contribute to the development of a more reliable and effective AI-based diagnostic tool for GI diseases using the Kvasir dataset. In our research, we have aimed at using advanced deep learning structures, including CNNs and ViT, to increase diagnostic accuracy and stability. We also use model optimization measures, such as data augmentation and transfer learning, in order to incorporate the variance in the data and to make better generalizations on unseen cases. In addition, the final trained model is applied on mobile platforms with the help of TFLite, which allows us to classify diseases on devices in real-time. This portable setup will make the diagnostic device available in a wide range of locations, even in low-resource areas; this will make the diagnosis of GI diseases available to more health practitioners around the world. The following are the study motivation, aim, and objectives:Motivation:Existing AI-based gastrointestinal diagnostic systems face challenges in robustness, generalization across datasets, and reliable interpretation of complex lesion patterns.Aim:To develop a hybrid deep learning framework that improves multi-class classification performance for gastrointestinal diseases using endoscopic imagery.Objectives:To integrate transformer-based global representation learning with hierarchical learning mechanisms for improved representation learning.To enhance model robustness using data augmentation and transfer learning strategies.To evaluate model performance using cross-validation, external validation, and robustness testing.To indicate interpretability through attention-based visualization techniques. Motivation:Existing AI-based gastrointestinal diagnostic systems face challenges in robustness, generalization across datasets, and reliable interpretation of complex lesion patterns. Motivation: Existing AI-based gastrointestinal diagnostic systems face challenges in robustness, generalization across datasets, and reliable interpretation of complex lesion patterns. Aim: Aim: To develop a hybrid deep learning framework that improves multi-class classification performance for gastrointestinal diseases using endoscopic imagery. To develop a hybrid deep learning framework that improves multi-class classification performance for gastrointestinal diseases using endoscopic imagery. Objectives:To integrate transformer-based global representation learning with hierarchical learning mechanisms for improved representation learning.To enhance model robustness using data augmentation and transfer learning strategies.To evaluate model performance using cross-validation, external validation, and robustness testing.To indicate interpretability through attention-based visualization techniques. Objectives:To integrate transformer-based global representation learning with hierarchical learning mechanisms for improved representation learning.To enhance model robustness using data augmentation and transfer learning strategies.To evaluate model performance using cross-validation, external validation, and robustness testing.To indicate interpretability through attention-based visualization techniques. To integrate transformer-based global representation learning with hierarchical learning mechanisms for improved representation learning. To integrate transformer-based global representation learning with hierarchical learning mechanisms for improved representation learning. To enhance model robustness using data augmentation and transfer learning strategies. To enhance model robustness using data augmentation and transfer learning strategies. To evaluate model performance using cross-validation, external validation, and robustness testing. To evaluate model performance using cross-validation, external validation, and robustness testing. To indicate interpretability through attention-based visualization techniques. To indicate interpretability through attention-based visualization techniques. The main contributions of this study are summarized as follows:A novel hybrid model is proposed that integrates a Vision Transformer (ViT) with a Hierarchical Learning Neural Network (HLNN) and ensemble learning, enabling accurate multi-class classification of gastrointestinal (GI) diseases across endoscopic datasets.A class-specific image enhancement pipeline is introduced using Contrast-Limited Adaptive Histogram Equalization (CLAHE) and gamma correction, along with balanced augmentation strategies, to improve lesion visibility and address class imbalance.The model is compressed to a compact architecture (45 MB) through post-training quantization, achieving efficient inference (65 ms per image) on resource-constrained devices.Explainability is incorporated using Grad-CAM visualizations, with heatmaps evaluated using localization performance metrics to assess interpretability. A novel hybrid model is proposed that integrates a Vision Transformer (ViT) with a Hierarchical Learning Neural Network (HLNN) and ensemble learning, enabling accurate multi-class classification of gastrointestinal (GI) diseases across endoscopic datasets. A novel hybrid model is proposed that integrates a Vision Transformer (ViT) with a Hierarchical Learning Neural Network (HLNN) and ensemble learning, enabling accurate multi-class classification of gastrointestinal (GI) diseases across endoscopic datasets. A class-specific image enhancement pipeline is introduced using Contrast-Limited Adaptive Histogram Equalization (CLAHE) and gamma correction, along with balanced augmentation strategies, to improve lesion visibility and address class imbalance. A class-specific image enhancement pipeline is introduced using Contrast-Limited Adaptive Histogram Equalization (CLAHE) and gamma correction, along with balanced augmentation strategies, to improve lesion visibility and address class imbalance. The model is compressed to a compact architecture (45 MB) through post-training quantization, achieving efficient inference (65 ms per image) on resource-constrained devices. The model is compressed to a compact architecture (45 MB) through post-training quantization, achieving efficient inference (65 ms per image) on resource-constrained devices. Explainability is incorporated using Grad-CAM visualizations, with heatmaps evaluated using localization performance metrics to assess interpretability. Explainability is incorporated using Grad-CAM visualizations, with heatmaps evaluated using localization performance metrics to assess interpretability. To the best of our knowledge, this is the first framework to combine ViT global attention, hierarchical recurrent encoding, and dual ensemble strategies (bagging + stacking) within a single system, optimized for mobile deployment and evaluated across class-holdout generalization, cross-validation, external datasets, and clinical analysis. By leveraging the comprehensive and diverse Kvasir dataset, this study aims to advance the development of accurate and interpretable AI-driven diagnostic tools for gastrointestinal disease detection. Through the exploration of advanced model architecture and the incorporation of robust validation techniques, the goal is to contribute to reliable and practical solutions that support clinicians in making informed diagnostic decisions, thereby reducing healthcare burdens. The rest of this paper consists of sections such as Sect. “Related work”, which describes related work about medical imaging in gastrointestinal disease detection, Sect. “Methodology” describes details of materials and methods used to propose the new framework, and evaluation metrics to evaluate the model accuracy and robustness. Section “Results” contains information about mobile deployment and validation of the proposed framework in a real-world environment, Sect. “Discussion and analysis” describes clinical validation analysis, and Sect. “Conclusion” describes our whole research’s discussion and analysis, as well as Sect. Conclusion concludes the research. Related work Recently, the integration of artificial intelligence into GI disease detection has catalyzed remarkable progress in endoscopic imaging analysis, particularly with the rise of deep learning (DL) and explainable AI (XAI). These advancements enable automated, high-accuracy diagnosis of conditions such as esophagitis, polyps, ulcers, and tumors. CNNs, ViTs, ensemble models, and optimization techniques have been widely explored across public datasets like Kvasir, Hyper-Kvasir, CVC-ClinicDB, and Kvasir-Capsule. Moreover, the incorporation of saliency-based visualization tools such as Grad-CAM, SHAP, and LIME has enhanced model interpretability, a crucial factor for future decision-support systems. This section provides a detailed and sequential overview of recent contributions in this rapidly evolving field. Deep learning with explainability for esophagitis and OOD detection A notable benchmark combined GoogLeNet, ResNet-50, and MobileNet V2/V3 with Grad-CAM and SHAP for esophagitis detection. GoogLeNet yielded the highest F1 score, while MobileNet V3 produced the most confident predictions. However, the study lacked comparisons with alternative XAI methods and did not address class imbalance21. An innovative shift toward out-of-distribution (OOD) detection introduced the Nearest Centroid Distance Deficit (NCDD) method, which differentiated in-distribution (ID) from OOD samples across the Kvasir2 and Gastrovision datasets. It outperformed traditional OOD methods in terms of AUC and FPR95, though partial feature overlap remained a challenge22. In the wireless capsule endoscopy domain, a hybrid framework using Moth-Crow optimization and DNNs reduced annotation burden and improved performance but was hindered by overfitting and generalizability limitations23. Similarly, the GestroNet model combined MobileNet-V2, deep saliency maps, and whale optimization, achieving 99.61% accuracy across multiple datasets, though it lacked real-time readiness and robust analyses24. Hybrid optimization and transformer-based models for polyp segmentation and classification For polyp segmentation, a hybrid of fully convolutional networks and SwinV2 Transformer yielded high mDice scores on Kvasir-SEG and CVC-ClinicDB datasets. However, potential evaluation data leakage was not examined25. A hybrid Vision Transformer with shifted windows reached 95.42% classification accuracy on Kvasir V2 and Hyper-Kvasir yet struggled to resolve fine-grained class distinctions26. To improve training efficiency and reduce overfitting, an Intelligent Learning Rate Controller (ILRC) dynamically optimized learning rates for EfficientNet-B0 and InceptionV3, achieving 99.906% accuracy on Kvasir-Capsule. The method, however, introduced added training complexity27. A two-phase transfer learning approach utilizing InceptionResNetV2, Xception, and VGG16 attained 99.5% accuracy in polyp detection, although esophagitis severity classification suffered28. Ensemble and capsule networks for robust GI image analysis Ensemble models also showed promise. A weighted ensemble of NASNet-Large, ResNet-101, and Xception with class-based recall fusion and Grad-CAM achieved 94.125% accuracy, albeit at a high computational cost29. The proposed GIT-Net is an ensemble deep learning framework for gastrointestinal tract image classification that combines multiple CNN backbones through weighted feature aggregation to improve robustness across endoscopic classes. Their approach demonstrated enhanced classification accuracy on GI datasets but relies on convolutional architectures and handcrafted ensemble weighting, limiting scalability to transformer-based representations30. The Spatial-Attention ConvMixer (SAC) enhanced feature representation and attained 93.37% accuracy on the Kvasir dataset. Yet, its generalizability across datasets remains unexplored31. The Patch-and-Amplify Capsule Network reached 93.4% accuracy through robust patch-level feature extraction, though it was sensitive to real-world image variability32. Real-time and telemedicine-focused models with explainability integration TeleXGI, tailored for telemedicine, combined ResNet50 and MobileNetV2 with XAI methods like Grad-CAM, LIME, and saliency maps, achieving 98.8% accuracy. However, low-quality input images led to performance degradation33. EfficientNet variants B1 and B0 showed accuracies of 94.75% and 94.31%, respectively, but failed to distinguish visually similar disease categories34. The Hexa-Categorization model integrated ShuffleNet with UNet to simultaneously classify and segment images with 99.38% accuracy, though it lacked sophisticated attention mechanisms35. Triple Fusion Attention (TFA) fused convolutional, adaptive, and attention-based modules, improving classification but at the cost of scalability due to architectural complexity36. GIT-NET, an ensemble of DenseNet201, InceptionV3, and ResNet50, reached 95% accuracy on Kvasir V2 but incurred substantial inference latency37. Attention-enhanced CNNs and transformer-based advances in GI imaging LG-CNN, enhanced with an Endoscopy-Lesion Attention (ELA) module, improved precision on Hyper-Kvasir, though class imbalance was not explicitly mitigated38. In transfer learning, a comparative evaluation found VGG19 outperforming ResNet50V2 and EfficientNetV2 on the Kvasir dataset, achieving 88.6% accuracy, although optimization instability persisted39. Lastly, ViT and Data-efficient Image Transformer (DeiT) models surpassed traditional CNNs across medical imaging datasets. ViT attained high MCC scores of 0.9259 for Chest X-rays, 0.9360 for Kvasir, and 0.3705 for Kvasir-Capsule, highlighting its potential for diagnostic applications40. Conclusion of related work and identified research gaps Despite significant advancements, several critical limitations persist in existing gastrointestinal disease classification frameworks:Many models exhibit overfitting, particularly when trained on limited or imbalanced datasets, reducing their reliability.Several approaches fail to maintain performance across different datasets due to domain shifts and dataset-specific feature learning.Ensemble and deep architectures often incur high computational costs, limiting their scalability and practical usability.Many frameworks are not optimized for efficient inference, restricting their applicability in time-sensitive scenarios.Although explainability techniques such as Grad-CAM and SHAP are used, they are often not deeply integrated into the model design or evaluation.Few studies assess model performance under noisy, low-quality, or artifact-prone imaging conditions commonly encountered in endoscopy. Many models exhibit overfitting, particularly when trained on limited or imbalanced datasets, reducing their reliability. Many models exhibit overfitting, particularly when trained on limited or imbalanced datasets, reducing their reliability. Several approaches fail to maintain performance across different datasets due to domain shifts and dataset-specific feature learning. Several approaches fail to maintain performance across different datasets due to domain shifts and dataset-specific feature learning. Ensemble and deep architectures often incur high computational costs, limiting their scalability and practical usability. Ensemble and deep architectures often incur high computational costs, limiting their scalability and practical usability. Many frameworks are not optimized for efficient inference, restricting their applicability in time-sensitive scenarios. Many frameworks are not optimized for efficient inference, restricting their applicability in time-sensitive scenarios. Although explainability techniques such as Grad-CAM and SHAP are used, they are often not deeply integrated into the model design or evaluation. Although explainability techniques such as Grad-CAM and SHAP are used, they are often not deeply integrated into the model design or evaluation. Few studies assess model performance under noisy, low-quality, or artifact-prone imaging conditions commonly encountered in endoscopy. Few studies assess model performance under noisy, low-quality, or artifact-prone imaging conditions commonly encountered in endoscopy. The While individual components of the proposed system Vision Transformers for medical imaging, LSTM-based hierarchical feature refinement, and ensemble learning for classification robustness have been explored separately in prior literature, the methodological novelty of this work lies in their principled integration within a unified, end-to-end trainable framework specifically optimized for gastrointestinal endoscopic analysis. Specifically, this study introduces:Hierarchical recurrent modeling of ViT patch embeddings for spatial dependency learning without temporal assumptions, treating ordered patch sequences as structured representations of lesion morphology and boundary transitions.A dual-ensemble strategy combining bagging for variance reduction and stacking with a LightGBM meta-learner for error complementarity capture, trained explicitly on validation predictions to learn optimal fusion weights.A deployment-ready explainability pipeline validated through clinician annotations, multi-method corroboration (Grad-CAM, SHAP, LIME), and quantitative faithfulness metrics rather than qualitative visualization alone. Hierarchical recurrent modeling of ViT patch embeddings for spatial dependency learning without temporal assumptions, treating ordered patch sequences as structured representations of lesion morphology and boundary transitions. Hierarchical recurrent modeling of ViT patch embeddings for spatial dependency learning without temporal assumptions, treating ordered patch sequences as structured representations of lesion morphology and boundary transitions. A dual-ensemble strategy combining bagging for variance reduction and stacking with a LightGBM meta-learner for error complementarity capture, trained explicitly on validation predictions to learn optimal fusion weights. A dual-ensemble strategy combining bagging for variance reduction and stacking with a LightGBM meta-learner for error complementarity capture, trained explicitly on validation predictions to learn optimal fusion weights. A deployment-ready explainability pipeline validated through clinician annotations, multi-method corroboration (Grad-CAM, SHAP, LIME), and quantitative faithfulness metrics rather than qualitative visualization alone. A deployment-ready explainability pipeline validated through clinician annotations, multi-method corroboration (Grad-CAM, SHAP, LIME), and quantitative faithfulness metrics rather than qualitative visualization alone. To our knowledge, this is among the first frameworks to integrate Vision Transformer (ViT)-based global attention, hierarchical recurrent encoding of patch sequences, and dual ensemble strategies within a unified system optimized for mobile deployment, evaluated via class-holdout generalization, cross-dataset zero-shot testing, and robustness analyses. Methodology The study aims to develop a GI disease detection AI diagnostic tool and to address model performance as well as real-world applicability, using the Kvasir dataset as shown in Fig. 1. Over 8000 labelled endoscopic images of different GI disorders used in the Kvasir dataset were used to train and evaluate the models. Data preprocessing was performed by resizing, augmentation, and normalization, and dividing it into training, validation, and test subsets to make a robust evaluation of the model. A hybrid framework consisting of CNNs and ViTs was adopted during model development. To capture local features, CNN was utilized, and the ViT captured global dependencies to take advantage of their complementary advantages in the classification. The model was trained on the basis of cross-entropy loss and optimized by the Adam optimizer, including such techniques as learning rate scheduling, early stopping, and regularization to avoid overfitting and promote generalization.Fig. 1Proposed research framework diagram for gastrointestinal disease detection. Proposed research framework diagram for gastrointestinal disease detection. The model was next transferred into the TensorFlow Lite proprietary form so that it could be deployed on any mobile device easily. This made it possible to detect the disease in real-time with a mobile application so that clinicians could share endoscopic images and receive immediate display feedback. To make the app close to real-life events, healthcare professionals tested the app and included the feedback findings to enhance usability and functionality. Additionally, optimization methods such as quantization and pruning were used to decrease model size and make inference fast on-device. In general, the given approach illustrates the possibilities of AI helping to raise the level and availability of diagnostics, especially in settings with limited resources, and paves the future advances in the interpretation of models and their functioning in various clinics. Although our achieved accuracy and F1-scores are notably high, they remain technically explainable based on the properties of the Kvasir-v2 dataset and the applied modelling techniques. Kvasir-v2 is a curated, balanced, and high-quality dataset with limited noise, low label ambiguity, and strong visual separability between classes, which supports high classification ceilings. In addition, our hybrid ViT-HLNN model employs transformer-based global attention, hierarchical temporal encoding, and ensemble stacking, which significantly reduce error variance and increase decision robustness. Strict preventive measures were applied to eliminate data leakage. All preprocessing steps, including CLAHE, gamma correction, resizing, and augmentation, were executed exclusively inside the training pipeline after dataset partitioning. No preprocessed images from training were ever reused in validation or test sets. The independent 10% test set remained untouched throughout training and cross-validation. Therefore, the reported results reflect genuine model performance rather than train–test overlap or leakage. Dataset description The Kvasir dataset can be regarded as one of the major contributions to the field of computer-aided GI disease detection because it provides a wide range of well-annotated endoscopic photos of the bowel41. This dataset consists of image data from the Vestre Viken Health Trust (VV) and the Cancer Registry of Norway (CRN) and is developed in collaboration with both organizations to support the development and benchmarking of algorithm object detection applied to medical image data, which contains images of both normal properties as well as pathological results. The primary mission of the Kvasir dataset is to facilitate automated disease-specific detection, classification, and reporting research on GI diseases, thereby overcoming the long-standing challenge of the limited number of publicly available, expert-verified, and scalable medical image datasets. Due to the quality of its annotations and clinical utility, Kvasir v2 has already established a significant presence as the default in machine learning, deep learning, and computer vision model evaluation in the field of healthcare. Kvasir v2 encloses 8000 high-resolution endoscope images, covering eight clinically relevant classes, encompassing pathological conditions and particular anatomical landmarks. The picture sizes are varied between “720576” and “19201072” pixels, making the tool flexible in the hands of a researcher carrying out different activities related to image processing. The dataset can be used in classification, feature extraction, transfer learning, and training deep neural networks, among others. Table 1 shows the class-wise distribution of the dataset, which is summarized below:Table 1Class-wise summary of the Kvasir v2 dataset.Class nameDescriptionImage countDyed-lifted-polypsPolyps that are stained and lifted using submucosal injection for better visualization during removal1000Dyed-resection-marginsImages showing tissue boundaries after polyp resection, stained for clarity and margin inspection1000EsophagitisInflammation of the esophageal lining typically presents redness or ulceration1000Normal-cecumThe normal anatomical structure of the cecum, the beginning of the large intestine1000Normal-pylorusImages of a healthy pylorus, the valve between the stomach and small intestine1000Normal-z-lineGastroesophageal junction, where the esophagus meets the stomach1000PolypsAbnormal mucosal growth is often benign but potentially precancerous1000Ulcerative-colitisChronic inflammatory condition of the colon with characteristic mucosal damage1000Total8000 Table 1Class-wise summary of the Kvasir v2 dataset.Class nameDescriptionImage countDyed-lifted-polypsPolyps that are stained and lifted using submucosal injection for better visualization during removal1000Dyed-resection-marginsImages showing tissue boundaries after polyp resection, stained for clarity and margin inspection1000EsophagitisInflammation of the esophageal lining typically presents redness or ulceration1000Normal-cecumThe normal anatomical structure of the cecum, the beginning of the large intestine1000Normal-pylorusImages of a healthy pylorus, the valve between the stomach and small intestine1000Normal-z-lineGastroesophageal junction, where the esophagus meets the stomach1000PolypsAbnormal mucosal growth is often benign but potentially precancerous1000Ulcerative-colitisChronic inflammatory condition of the colon with characteristic mucosal damage1000Total8000 Class-wise summary of the Kvasir v2 dataset. Originally, the broader Kvasir dataset comprised over 11,000 endoscopic images, with an imbalanced class distribution reflecting the natural clinical frequency of findings, more polyp and normal anatomical cases. However, in the Kvasir v2 release, the dataset was carefully curated and balanced by selecting 1000 representative images per class, enabling fair comparisons across machine learning models and standardized evaluation protocols. Data preprocessing To indicate robust model performance and improve generalization, we applied a series of preprocessing and data augmentation steps to the Kvasir dataset images, as shown in Fig. 2. In the following sections, we describe these steps in detail, outlining the transformations applied to the raw images and the augmentation techniques used to enhance dataset diversity and reduce overfitting. Although Kvasir-v2 is already numerically balanced (1000 images per class), augmentation was not used to change class quantities. Instead, augmentation was applied to increase intra-class visual variability, reduce overfitting, and simulate real clinical complexity. This approach improved generalization on subtle disease categories such as ulcerative colitis and esophagitis without altering the original dataset balance.Fig. 2Data preprocessing pipeline for the Kvasir dataset. Data preprocessing pipeline for the Kvasir dataset. Aspect ratio-preserving resizing Endoscopic images in the Kvasir dataset have varied resolutions, ranging from 1920 × 1072 to 720 × 576. To standardize input sizes for CNNs without introducing distortion, images are resized to 224 × 224 pixels while preserving their aspect ratios. Unlike direct resizing, which introduces geometric distortion and alters lesion morphology, aspect-ratio–preserving resizing with padding was adopted to maintain anatomical fidelity of endoscopic structures. Distortion particularly affects small polyps, Z-line boundaries, and ulcerative textures, which are critical for diagnosis. Preserving the original aspect ratio indicates that spatial relationships and lesion shapes remain clinically consistent, improving both classification stability and Grad-CAM localization reliability. Let “I” be the input image, where w is the width, and h is the height. The resizing procedure is as follows: Aspect Ratio Calculation:The aspect ratio r is defined as shown in Eq. 1:1\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$r = \frac{w}{h}$$\end{document}White padding pixel value 255 was selected instead of black to prevent artificial high-contrast edges that can bias early convolutional filters and attention heads. Black padding introduces strong gradients at image boundaries, which were observed to attract Grad-CAM attention and marginally reduce lesion-focused activation. White padding better matches the overall illumination statistics of mucosal backgrounds, resulting in more stable attention maps.Conditional Scaling:Depending on r, the image is resized to fit within a 224 × 224 square as shown in Eqs. 2 and 3:2\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$w^{\prime} = 224,h^{\prime} = \left[ \frac{224}{r} \right]$$\end{document}Width is fixed to 224.Height is scaled proportionally to preserve aspect ratio.3\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$h^{\prime} = 224,w^{\prime} = [224 \times r]$$\end{document}Height is fixed to 224.Width is scaled accordingly.Padding: The resized image is placed in the centre of a 224 × 224 white canvas as shown in Eq. 4:4\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{pad} \left( {x,y} \right) = \left\{ {\begin{array}{*{20}c} {I\left( {x + x_{offset} , y + y_{offset} } \right),} \\ {255} \\ \end{array} } \right.\;if \left( {x,y} \right) \in ROI otherwise$$\end{document}where:ROI is the region where the resized image fits.The rest of the canvas (padding) is filled with white (255).Offsets to center the image as shown in Eq. 5:5\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$x_{offset} = \frac{{224 - w^{\prime}}}{2},y_{offset} = \frac{{224 - h^{\prime}}}{2}$$\end{document} Aspect Ratio Calculation:The aspect ratio r is defined as shown in Eq. 1:1\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$r = \frac{w}{h}$$\end{document}White padding pixel value 255 was selected instead of black to prevent artificial high-contrast edges that can bias early convolutional filters and attention heads. Black padding introduces strong gradients at image boundaries, which were observed to attract Grad-CAM attention and marginally reduce lesion-focused activation. White padding better matches the overall illumination statistics of mucosal backgrounds, resulting in more stable attention maps. Aspect Ratio Calculation: The aspect ratio r is defined as shown in Eq. 1:1\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$r = \frac{w}{h}$$\end{document} White padding pixel value 255 was selected instead of black to prevent artificial high-contrast edges that can bias early convolutional filters and attention heads. Black padding introduces strong gradients at image boundaries, which were observed to attract Grad-CAM attention and marginally reduce lesion-focused activation. White padding better matches the overall illumination statistics of mucosal backgrounds, resulting in more stable attention maps. Conditional Scaling:Depending on r, the image is resized to fit within a 224 × 224 square as shown in Eqs. 2 and 3:2\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$w^{\prime} = 224,h^{\prime} = \left[ \frac{224}{r} \right]$$\end{document}Width is fixed to 224.Height is scaled proportionally to preserve aspect ratio.3\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$h^{\prime} = 224,w^{\prime} = [224 \times r]$$\end{document}Height is fixed to 224.Width is scaled accordingly. Conditional Scaling: Depending on r, the image is resized to fit within a 224 × 224 square as shown in Eqs. 2 and 3:2\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$w^{\prime} = 224,h^{\prime} = \left[ \frac{224}{r} \right]$$\end{document}Width is fixed to 224.Height is scaled proportionally to preserve aspect ratio.3\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$h^{\prime} = 224,w^{\prime} = [224 \times r]$$\end{document}Height is fixed to 224.Width is scaled accordingly. 2\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$w^{\prime} = 224,h^{\prime} = \left[ \frac{224}{r} \right]$$\end{document}Width is fixed to 224.Height is scaled proportionally to preserve aspect ratio. 2\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$w^{\prime} = 224,h^{\prime} = \left[ \frac{224}{r} \right]$$\end{document}Width is fixed to 224.Height is scaled proportionally to preserve aspect ratio. Width is fixed to 224. Width is fixed to 224. Height is scaled proportionally to preserve aspect ratio. Height is scaled proportionally to preserve aspect ratio. 3\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$h^{\prime} = 224,w^{\prime} = [224 \times r]$$\end{document}Height is fixed to 224.Width is scaled accordingly. 3\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$h^{\prime} = 224,w^{\prime} = [224 \times r]$$\end{document}Height is fixed to 224.Width is scaled accordingly. Height is fixed to 224. Height is fixed to 224. Width is scaled accordingly. Width is scaled accordingly. Padding: The resized image is placed in the centre of a 224 × 224 white canvas as shown in Eq. 4:4\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{pad} \left( {x,y} \right) = \left\{ {\begin{array}{*{20}c} {I\left( {x + x_{offset} , y + y_{offset} } \right),} \\ {255} \\ \end{array} } \right.\;if \left( {x,y} \right) \in ROI otherwise$$\end{document}where:ROI is the region where the resized image fits.The rest of the canvas (padding) is filled with white (255).Offsets to center the image as shown in Eq. 5:5\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$x_{offset} = \frac{{224 - w^{\prime}}}{2},y_{offset} = \frac{{224 - h^{\prime}}}{2}$$\end{document} Padding: The resized image is placed in the centre of a 224 × 224 white canvas as shown in Eq. 4:4\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{pad} \left( {x,y} \right) = \left\{ {\begin{array}{*{20}c} {I\left( {x + x_{offset} , y + y_{offset} } \right),} \\ {255} \\ \end{array} } \right.\;if \left( {x,y} \right) \in ROI otherwise$$\end{document}where:ROI is the region where the resized image fits.The rest of the canvas (padding) is filled with white (255).Offsets to center the image as shown in Eq. 5:5\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$x_{offset} = \frac{{224 - w^{\prime}}}{2},y_{offset} = \frac{{224 - h^{\prime}}}{2}$$\end{document} ROI is the region where the resized image fits. ROI is the region where the resized image fits. The rest of the canvas (padding) is filled with white (255). The rest of the canvas (padding) is filled with white (255). Offsets to center the image as shown in Eq. 5:5\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$x_{offset} = \frac{{224 - w^{\prime}}}{2},y_{offset} = \frac{{224 - h^{\prime}}}{2}$$\end{document} Offsets to center the image as shown in Eq. 5:5\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$x_{offset} = \frac{{224 - w^{\prime}}}{2},y_{offset} = \frac{{224 - h^{\prime}}}{2}$$\end{document} To indicate minimal distortion from the original aspect ratio, as shown in Eq. 6:6\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\Delta r = \frac{{\left| {r - r^{\prime}} \right|}}{r}$$\end{document}If \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\Delta r > 0.01$$\end{document}, the image is flagged for manual review.This affects about 0.7% of the dataset.(d) Class-Specific Colour EnhancementEndoscopic images often suffer from uneven illumination and low contrast. Two adaptive colour corrections are applied to address these issues:(e) Contrast-Limited Adaptive Histogram Equalization (CLAHE)CLAHE operates on the luminance channel (Y in YCbCr colour space) with class-dependent clip limits to balance noise suppression and contrast enhancement as shown in Eq. 7. Table 2 below shows class-specific parameters for CLAHE. The terms “Normal,” “Dyed abnormal,” and “Non-dyed abnormal” are not new dataset labels. They are internal enhancement groups used only to assign CLAHE and gamma parameters. All experiments, evaluations, and metrics were performed strictly on the original eight Kvasir-v2 classes without merging or relabeling.7\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$CLAHE\left( {I_{Y} ;clip} \right) = Interpolate\left( {HistEq_{{\text{tiles }}} \left( {I_{Y} } \right)} \right)$$\end{document}Table 2Class-specific parameters for CLAHE.ClassClip limitTile gridKvasir-v2 classes includedRationaleNormal0.0058 × 8Normal-cecum, Normal-pylorus, Normal-z-lineSubtle enhancements for healthy tissueDyed abnormal0.00756 × 6Dyed-lifted-polyps, Dyed-resection-marginsHigher contrast for dye-highlighted lesionsNon-dyed abnormal0.014 × 4Polyps, Esophagitis, Ulcerative ColitisAggressive enhancement for low-contrast lesionsThese enhancement groups are internal preprocessing categories used solely for parameter assignment and do not alter the original eight-class Kvasir-v2 labels.(f) Gamma CorrectionNonlinear intensity mapping adjusts for lighting variability as shown in Eq. 8:8\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{gamma} = 255 \times \left( \frac{I}{255} \right)^{\gamma }$$\end{document}Class-specific optimal gamma values are determined via grid search, as shown in Table 3:Table 3Class-specific optimal gamma values.Classγ rangeOptimal γNormal[0.99, 1.01]1.00 (no correction)Dyed abnormal[0.98, 1.02]1.01Non-dyed abnormal[0.95, 1.05]1.03Clip-limit values were selected through controlled pilot experiments on the training set using grid search. Values were optimized to maximize macro-F1 while preventing noise amplification. Lower clip limits were optimal for normal mucosa to avoid artificial texture, whereas higher limits improved visibility of inflamed or non-dyed lesions. Final values were fixed before full model training and remained unchanged across all experiments.(g) Normalization and Dataset CurationImages are converted into PyTorch tensors and normalized using ImageNet statistics as shown in Eq. 9:9\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{norm} = \frac{I - \mu }{\sigma }$$\end{document}where:I is the input RGB image, with pixel values in the range [0, 1].μ = [0.485, 0.456, 0.406] is the mean per channel (R, G, B).σ = [0.229, 0.224, 0.225] is the standard deviation per channel (R, G, B).This normalization is done channel-wise.(h) Storage ProtocolProcessed images are stored after being unnormalized, as shown in Eq. 10:10\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{{\text{storage }}} = clip\left( {I_{{\text{norm }}} \times \sigma + \mu ,0,255} \right)$$\end{document} If \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\Delta r > 0.01$$\end{document}, the image is flagged for manual review. If \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\Delta r > 0.01$$\end{document}, the image is flagged for manual review. This affects about 0.7% of the dataset. This affects about 0.7% of the dataset. (d) Class-Specific Colour EnhancementEndoscopic images often suffer from uneven illumination and low contrast. Two adaptive colour corrections are applied to address these issues: Class-Specific Colour Enhancement Endoscopic images often suffer from uneven illumination and low contrast. Two adaptive colour corrections are applied to address these issues: (e) Contrast-Limited Adaptive Histogram Equalization (CLAHE)CLAHE operates on the luminance channel (Y in YCbCr colour space) with class-dependent clip limits to balance noise suppression and contrast enhancement as shown in Eq. 7. Table 2 below shows class-specific parameters for CLAHE. The terms “Normal,” “Dyed abnormal,” and “Non-dyed abnormal” are not new dataset labels. They are internal enhancement groups used only to assign CLAHE and gamma parameters. All experiments, evaluations, and metrics were performed strictly on the original eight Kvasir-v2 classes without merging or relabeling.7\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$CLAHE\left( {I_{Y} ;clip} \right) = Interpolate\left( {HistEq_{{\text{tiles }}} \left( {I_{Y} } \right)} \right)$$\end{document}Table 2Class-specific parameters for CLAHE.ClassClip limitTile gridKvasir-v2 classes includedRationaleNormal0.0058 × 8Normal-cecum, Normal-pylorus, Normal-z-lineSubtle enhancements for healthy tissueDyed abnormal0.00756 × 6Dyed-lifted-polyps, Dyed-resection-marginsHigher contrast for dye-highlighted lesionsNon-dyed abnormal0.014 × 4Polyps, Esophagitis, Ulcerative ColitisAggressive enhancement for low-contrast lesionsThese enhancement groups are internal preprocessing categories used solely for parameter assignment and do not alter the original eight-class Kvasir-v2 labels. Contrast-Limited Adaptive Histogram Equalization (CLAHE) CLAHE operates on the luminance channel (Y in YCbCr colour space) with class-dependent clip limits to balance noise suppression and contrast enhancement as shown in Eq. 7. Table 2 below shows class-specific parameters for CLAHE. The terms “Normal,” “Dyed abnormal,” and “Non-dyed abnormal” are not new dataset labels. They are internal enhancement groups used only to assign CLAHE and gamma parameters. All experiments, evaluations, and metrics were performed strictly on the original eight Kvasir-v2 classes without merging or relabeling.7\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$CLAHE\left( {I_{Y} ;clip} \right) = Interpolate\left( {HistEq_{{\text{tiles }}} \left( {I_{Y} } \right)} \right)$$\end{document}Table 2Class-specific parameters for CLAHE.ClassClip limitTile gridKvasir-v2 classes includedRationaleNormal0.0058 × 8Normal-cecum, Normal-pylorus, Normal-z-lineSubtle enhancements for healthy tissueDyed abnormal0.00756 × 6Dyed-lifted-polyps, Dyed-resection-marginsHigher contrast for dye-highlighted lesionsNon-dyed abnormal0.014 × 4Polyps, Esophagitis, Ulcerative ColitisAggressive enhancement for low-contrast lesions Table 2Class-specific parameters for CLAHE.ClassClip limitTile gridKvasir-v2 classes includedRationaleNormal0.0058 × 8Normal-cecum, Normal-pylorus, Normal-z-lineSubtle enhancements for healthy tissueDyed abnormal0.00756 × 6Dyed-lifted-polyps, Dyed-resection-marginsHigher contrast for dye-highlighted lesionsNon-dyed abnormal0.014 × 4Polyps, Esophagitis, Ulcerative ColitisAggressive enhancement for low-contrast lesions Class-specific parameters for CLAHE. These enhancement groups are internal preprocessing categories used solely for parameter assignment and do not alter the original eight-class Kvasir-v2 labels. (f) Gamma CorrectionNonlinear intensity mapping adjusts for lighting variability as shown in Eq. 8:8\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{gamma} = 255 \times \left( \frac{I}{255} \right)^{\gamma }$$\end{document}Class-specific optimal gamma values are determined via grid search, as shown in Table 3:Table 3Class-specific optimal gamma values.Classγ rangeOptimal γNormal[0.99, 1.01]1.00 (no correction)Dyed abnormal[0.98, 1.02]1.01Non-dyed abnormal[0.95, 1.05]1.03Clip-limit values were selected through controlled pilot experiments on the training set using grid search. Values were optimized to maximize macro-F1 while preventing noise amplification. Lower clip limits were optimal for normal mucosa to avoid artificial texture, whereas higher limits improved visibility of inflamed or non-dyed lesions. Final values were fixed before full model training and remained unchanged across all experiments. Gamma Correction Nonlinear intensity mapping adjusts for lighting variability as shown in Eq. 8:8\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{gamma} = 255 \times \left( \frac{I}{255} \right)^{\gamma }$$\end{document} Class-specific optimal gamma values are determined via grid search, as shown in Table 3:Table 3Class-specific optimal gamma values.Classγ rangeOptimal γNormal[0.99, 1.01]1.00 (no correction)Dyed abnormal[0.98, 1.02]1.01Non-dyed abnormal[0.95, 1.05]1.03 Table 3Class-specific optimal gamma values.Classγ rangeOptimal γNormal[0.99, 1.01]1.00 (no correction)Dyed abnormal[0.98, 1.02]1.01Non-dyed abnormal[0.95, 1.05]1.03 Class-specific optimal gamma values. Clip-limit values were selected through controlled pilot experiments on the training set using grid search. Values were optimized to maximize macro-F1 while preventing noise amplification. Lower clip limits were optimal for normal mucosa to avoid artificial texture, whereas higher limits improved visibility of inflamed or non-dyed lesions. Final values were fixed before full model training and remained unchanged across all experiments. (g) Normalization and Dataset CurationImages are converted into PyTorch tensors and normalized using ImageNet statistics as shown in Eq. 9:9\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{norm} = \frac{I - \mu }{\sigma }$$\end{document}where:I is the input RGB image, with pixel values in the range [0, 1].μ = [0.485, 0.456, 0.406] is the mean per channel (R, G, B).σ = [0.229, 0.224, 0.225] is the standard deviation per channel (R, G, B).This normalization is done channel-wise. Normalization and Dataset Curation Images are converted into PyTorch tensors and normalized using ImageNet statistics as shown in Eq. 9:9\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{norm} = \frac{I - \mu }{\sigma }$$\end{document}where:I is the input RGB image, with pixel values in the range [0, 1].μ = [0.485, 0.456, 0.406] is the mean per channel (R, G, B).σ = [0.229, 0.224, 0.225] is the standard deviation per channel (R, G, B).This normalization is done channel-wise. I is the input RGB image, with pixel values in the range [0, 1]. I is the input RGB image, with pixel values in the range [0, 1]. μ = [0.485, 0.456, 0.406] is the mean per channel (R, G, B). μ = [0.485, 0.456, 0.406] is the mean per channel (R, G, B). σ = [0.229, 0.224, 0.225] is the standard deviation per channel (R, G, B). σ = [0.229, 0.224, 0.225] is the standard deviation per channel (R, G, B). This normalization is done channel-wise. This normalization is done channel-wise. (h) Storage ProtocolProcessed images are stored after being unnormalized, as shown in Eq. 10:10\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{{\text{storage }}} = clip\left( {I_{{\text{norm }}} \times \sigma + \mu ,0,255} \right)$$\end{document} Storage Protocol Processed images are stored after being unnormalized, as shown in Eq. 10:10\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{{\text{storage }}} = clip\left( {I_{{\text{norm }}} \times \sigma + \mu ,0,255} \right)$$\end{document} Images are saved as lossless JPEGs in class-stratified directories processed/train/normal/. Metadata, including original dimensions, Δr, and γ, is logged in a CSV file for full reproducibility. Resizing and CLAHE are optimized using OpenCV’s GPU-accelerated pipelines to maximize speed. All preprocessed images are serialized to prevent redundant computation during model training. General and class-specific data augmentation To improve the robustness and generalization of the deep learning model, a comprehensive data augmentation strategy was applied to the class-balanced Kvasir v2 dataset, which contains 8000 endoscopic images across eight categories. Despite this balance, real-world imaging variability due to factors such as camera angle, lighting, anatomical differences, and motion necessitated augmentation to simulate such conditions and reduce overfitting. General augmentation techniques, applied uniformly across all classes, included random rotation, horizontal and vertical flipping, zooming, and random cropping. These were performed dynamically during training via an online pipeline, exposing the model to varied versions of the data across epochs and enabling it to learn more invariant features42–44. Rotation angles up to ± 20°, Gaussian blur with σ ≤ 1.2, and contrast jitter of ± 15% were applied. These included higher rotation angles, broader zoom and aspect ratio variations, and colour jittering with brightness adjustments as shown in Table 4. This enriched visual diversity within these classes helps the model learn more discriminative features for accurate classification.Table 4Class-wise augmentation strategy summary.Class nameGeneral augmentationClass-specific augmentationDyed-lifted-polypsRotation, flipping, zoom, and croppingModerate zoom + rotationDyed-resection-marginsRotation, flipping, zoom, and croppingContrast and brightness shiftsEsophagitisRotation, flipping, zoom, and croppingHigh-angle rotation, contrast jitterNormal-cecumRotation, flipping, zoom, and cropping–Normal-pylorusRotation, flipping, zoom, and cropping–Normal-z-lineRotation, flipping, zoom, and cropping–PolypsRotation, flipping, zoom, and croppingMinor random cropping and zoom to simulate detection challengesUlcerative-colitisRotation, flipping, zoom, and croppingAggressive zoom, rotation, and brightness variation Table 4Class-wise augmentation strategy summary.Class nameGeneral augmentationClass-specific augmentationDyed-lifted-polypsRotation, flipping, zoom, and croppingModerate zoom + rotationDyed-resection-marginsRotation, flipping, zoom, and croppingContrast and brightness shiftsEsophagitisRotation, flipping, zoom, and croppingHigh-angle rotation, contrast jitterNormal-cecumRotation, flipping, zoom, and cropping–Normal-pylorusRotation, flipping, zoom, and cropping–Normal-z-lineRotation, flipping, zoom, and cropping–PolypsRotation, flipping, zoom, and croppingMinor random cropping and zoom to simulate detection challengesUlcerative-colitisRotation, flipping, zoom, and croppingAggressive zoom, rotation, and brightness variation Class-wise augmentation strategy summary. Class-specific augmentation intensity was determined through controlled pilot tests. Early experiments demonstrated that visually complex classes, such as ulcerative colitis, esophagitis, and dyed-resection lesions, benefited from increased rotation, zoom, and brightness shifts, while normal anatomical classes required minimal transformation. This strategy improved feature learning stability and was validated by our ablation study, where removing class-specific augmentation resulted in a measurable performance decline. Normalization and standardization The pixel values of the images were normalized to a [0, 1] range by dividing each pixel by 255, and further standardization was done using the mean and standard deviation of the dataset, as shown in Eq. 11.11\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$I_{{\text{Standardized }}} = \frac{I - \mu }{\sigma }$$\end{document}where:I is the original pixel intensity.μ is the mean pixel value across the dataset.σ is the standard deviation of pixel values across the dataset. I is the original pixel intensity. I is the original pixel intensity. μ is the mean pixel value across the dataset. μ is the mean pixel value across the dataset. σ is the standard deviation of pixel values across the dataset. σ is the standard deviation of pixel values across the dataset. Splitting the dataset The full Kvasir-v2 dataset, consisting of 8000 labeled endoscopic images across 8 gastrointestinal classes, was split using the following protocol such as 80% Training Set 6400 images, 10% Validation Set 800 images and 10% Test Set 800 images to indicate robust performance evaluation, the dataset was split using stratified sampling, maintaining equal class distribution across all subsets as 100 images per class in validation and test sets, and 800 per class in training. The test set was fixed and never resampled. To assess statistical robustness, model training was repeated five times using different random seeds affecting weight initialization and data augmentation order only. Performance metrics are reported as the mean across runs evaluated on the same held-out test set. Although standard bootstrapping samples 100% of the data size, we intentionally sampled 80% to reduce redundancy while preserving diversity, a strategy commonly adopted in ensemble learning, as shown in Table 5.Table 5Dataset distribution.SplitOriginal imagesPer classAfter augmentation (effective samples per epoch)Training64008006400 (online augmented variants per epoch)Validation800100800 (no augmentation)Test800100800 (no augmentation) Table 5Dataset distribution.SplitOriginal imagesPer classAfter augmentation (effective samples per epoch)Training64008006400 (online augmented variants per epoch)Validation800100800 (no augmentation)Test800100800 (no augmentation) Dataset distribution. This strategy minimized the risk of class imbalance during model optimization. For held-out test evaluation, performance was measured on a fixed, class-stratified test set, where each class was represented by 100 samples. To assess statistical robustness, model training was repeated using multiple random seeds, and test performance was reported as the mean across runs evaluated on the same fixed test set. In addition, to validate the model’s consistency and generalizability, tenfold cross-validation was performed exclusively on the training and validation data. Each fold preserved class balance and was rotated to serve as a temporary validation set, with final cross-validation metrics averaged across folds. It is important to note that data augmentation was applied using an online strategy during training, meaning that augmented samples were generated dynamically at each epoch rather than being stored as additional images. As a result, the total number of training samples remains 6400 per epoch; however, the effective diversity of the dataset is significantly increased due to stochastic transformations. Formally, if N denotes the number of original training samples and T represents the set of augmentation transformations, the effective training distribution can be expressed as TN × T over multiple epochs, without altering the dataset size. Validation and test sets were not augmented to indicate unbiased performance evaluation. Noise reduction For noisy images, a Gaussian blur was applied to reduce high-frequency noise, ensuring smoother feature extraction. Additionally, edge detection using Sobel was applied to highlight important features like polyp boundaries. Quality check and final inspection A final inspection was done to indicate the quality of the images, ensuring the lesions and polyps were clearly visible after preprocessing. Any images that failed the inspection due to excessive distortion or poor quality after augmentation were logged for review. Proposed model architecture The proposed architecture combines advanced deep learning modules to perform high-precision classification of GI conditions using endoscopic imagery. Our hybrid pipeline integrates a ViT for global representation learning, an HLNN for local and temporal context modelling, and a dual-layer ensemble framework incorporating both Bagging and Stacking to enhance prediction robustness45,46 as shown in Fig. 3. These components are trained and fine-tuned to work harmoniously on the Kvasir dataset and optimized for real-time mobile deployment.Fig. 3HLNN with an ensemble learning model architecture. HLNN with an ensemble learning model architecture. The proposed framework is intentionally modular rather than arbitrarily complex. Each component was introduced to address a specific limitation observed in baseline models: ViT captures global context, HLNN models structure spatial dependencies between patch embeddings, and ensemble learning reduces variance and class-specific error modes. Importantly, the manuscript already reports an ablation study demonstrating that removing any major module leads to a consistent degradation in macro-F1 rather than marginal or unstable gains. All components are mandatory for deployment; instead, the full configuration represents a performance-oriented upper bound, while simplified variants remain viable for constrained settings. The proposed hybrid architecture is designed to leverage complementary representational strengths rather than relying on a simple additive combination of models. Specifically, the Vision Transformer (ViT) captures long-range spatial dependencies and global contextual relationships across gastrointestinal images, which are often critical for identifying diffuse pathological patterns. In contrast, the HLNN component enhances nonlinear decision boundaries and improves class separability in feature space. The attention mechanism further refines this integration by dynamically weighting feature contributions, enabling the model to prioritize diagnostically relevant regions. This design indicates that each component contributes distinct and non-redundant information, thereby improving overall discriminative capability beyond a naive ensemble effect. The proposed architecture is not a simple feature extraction pipeline followed by classification. Instead, it is a sequential hybrid learning framework in which the Vision Transformer (ViT) performs global contextual representation learning, producing structured patch embeddings that retain spatial relationships. These embeddings are then processed by the HLNN module, which models inter-patch dependencies and refines feature representations through hierarchical sequence learning. The final classification is performed using fully connected layers operating on the fused ViT–HLNN representation, followed by Softmax prediction. Therefore, ViT serves as a representation of learning backbone within an end-to-end trainable system, rather than acting as a standalone or fixed feature extractor. Vision transformer for global feature extraction The ViT module captures global spatial dependencies in the input images through the following process: for Patch Embedding and Projection, the Input images are divided into 16 × 16 patches, and each flattened into vectors, then projected linearly using a learnable weight matrix as shown in Eq. 12:12\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$x_{i} = {\text{ Flatten }}\left( {{\text{ Patch }}_{i} } \right) \cdot W_{{\text{patch }}}$$\end{document} For Multi-Head Self-Attention, Spatial relationships across patches are learned via scaled dot-product attention across multiple heads, as shown in Eq. 13:13\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$Attention\left( {Q,K,V} \right)\mathop - \limits_{ \downarrow } softmax\left( {\frac{{QK^{T} }}{{\sqrt {d_{k} } }}} \right)V$$\end{document} For dynamic positional encoding, unlike fixed positional encodings, we introduce data-aware dynamic embeddings to better align spatial features in medical images. To prevent overfitting in the dynamic positional encoding module, a dropout rate of 0.1 was applied after positional embedding addition during training. This value was fixed across all experiments and aligned with the Transformer block dropout for architectural consistency. For the Transformer Encoder, each block consists of layer normalization, attention, MLP layers, and dropout for regularization. The ViT is initialized using ImageNet-pretrained weights and fine-tuned on Kvasir. A standard Vision Transformer (ViT) backbone was selected instead of lightweight Transformer variants such as MobileViT or EfficientFormer to prioritize global contextual modelling and attention interpretability over raw architectural compactness. Gastrointestinal lesions often exhibit subtle, long-range contextual cues that benefit from full self-attention across image patches. While lightweight Transformers are optimized for efficiency, they typically restrict attention scope or rely on hybrid convolutional shortcuts, which we found less effective for modelling diffuse inflammatory patterns and boundary-level lesion context. Computational efficiency was instead addressed through post-training quantization, pruning, and TensorFlow Lite deployment, achieving real-time inference (65 ms/image) without compromising representational fidelity or explainability. For an input image of size 224 × 224, the ViT divides the image into 16 × 16 patches, resulting in 196 patch tokens, each represented by a 768-dimensional embedding. These tokens form a structured feature sequence that is passed as input to the HLNN module for hierarchical dependency modelling. This representation is particularly suitable for downstream sequential modelling, as the ordered patch embeddings preserve the spatial continuity required for HLNN-based dependency learning. Hierarchical LSTM-based neural network (HLNN) The HLNN processes ViT embeddings to extract hierarchically structured temporal features and refine them with attention mechanisms. HLNN is designed as a two-level LSTM-based model. Level 1 is a bi-directional LSTM that encodes sequences of patch embeddings from ViT. Level 2 is a second LSTM layer that models’ high-level abstractions and learns temporal alignment across clinical regions and lesion boundaries. The HLNN module employs a Bi-LSTM hierarchy for temporal feature encoding and then applies an attention layer to refine spatial–temporal importance values. The classification head consists of two fully connected layers with 512 and 256 neurons, respectively, each followed by Leaky ReLU activation and dropout, and a final Softmax output layer for eight-class prediction. The multi-head attention mentioned in the architecture does not replace LSTM operations; rather, it acts as a refinement layer on LSTM outputs, allowing the model to highlight clinically relevant patches. This resolves the architectural ambiguity between LSTM-based learning and attention integration. For the Hierarchical Attention Mechanism, the Attention weights are applied over both LSTM layers as shown in Eq. 14:14\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$HierarchicalAttention\left( X \right) = \mathop \sum \limits_{i } \alpha_{i} \cdot X_{i}$$\end{document}where:Xi ​are feature vectors from different semantic regions.αi​ are learned attention scores that emphasize important regions, polyps, and inflammation zones. Xi ​are feature vectors from different semantic regions. Xi ​are feature vectors from different semantic regions. αi​ are learned attention scores that emphasize important regions, polyps, and inflammation zones. αi​ are learned attention scores that emphasize important regions, polyps, and inflammation zones. For Adaptive Feature Fusion, HLNN outputs are fused with ViT outputs using a learned weighted sum as shown in Eq. 15:15\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$X_{final} = \mathop \sum \limits_{j} \lambda_{j} \cdot X_{j}$$\end{document}where:Xj​ are the outputs from different models or layers with HLNN and ViT.λ are learnable fusion weights determining each feature’s contribution.The final classification stage consists of fully connected layers that operate on the fused ViT–HLNN representation, followed by a Softmax layer for eight-class prediction. The entire pipeline is trained end-to-end, ensuring that both ViT and HLNN contribute directly to the final decision-making process. Xj​ are the outputs from different models or layers with HLNN and ViT. Xj​ are the outputs from different models or layers with HLNN and ViT. λ are learnable fusion weights determining each feature’s contribution. λ are learnable fusion weights determining each feature’s contribution. The final classification stage consists of fully connected layers that operate on the fused ViT–HLNN representation, followed by a Softmax layer for eight-class prediction. The entire pipeline is trained end-to-end, ensuring that both ViT and HLNN contribute directly to the final decision-making process. The final classification stage consists of fully connected layers that operate on the fused ViT–HLNN representation, followed by a Softmax layer for eight-class prediction. The entire pipeline is trained end-to-end, ensuring that both ViT and HLNN contribute directly to the final decision-making process. Although HLNN/LSTM architectures are traditionally applied to temporal data, their use in this work is motivated by the ordered spatial structure of ViT patch embeddings rather than any temporal or biological assumption. When arranged in a fixed raster order, patch tokens form a spatially coherent sequence that preserves local continuity, such as lesion boundaries and texture transitions. In this framework, the HLNN functions as a structured spatial dependency encoder, enabling the modeling of progressive morphological changes across adjacent regions that are not explicitly captured by independent patch processing. The choice of HLNN was guided by fair experimental evaluation, where it was assessed against parameter-matched alternatives under identical training conditions and demonstrated favorable performance and overall model efficiency within the proposed modular framework. Although LSTM architectures are typically designed for temporal sequences, their use here is motivated by the ordered spatial structure of Vision Transformer (ViT) patch embeddings rather than any temporal assumption. When patch tokens are arranged in raster-scan order (left-to-right, top-to-bottom), they form a spatially coherent sequence where adjacent tokens correspond to neighboring anatomical regions, enabling the modeling of local continuity and progressive morphological transitions. In this framework, the HLNN acts as a spatial dependency encoder. The bidirectional LSTM captures (i) local texture continuity within homogeneous regions, (ii) transition gradients across lesion boundaries, and (iii) long-range relationships between distant anatomical structures. This complements ViT’s global self-attention, which models long-range dependencies but does not explicitly enforce spatial continuity. The hierarchical two-level design enables progressive abstraction: the first layer encodes local patch neighborhoods, while the second aggregates these into global lesion-level representations. This approach aligns with prior spatial applications of recurrent models, such as Row–Column LSTM in histopathology, Spatial LSTM in radiology, and Structured LSTM for segmentation. To validate this design, controlled experiments compared HLNN with parameter-matched alternatives under identical conditions: a 6-layer Transformer encoder (97.8% accuracy), MLP-Mixer (97.2%), and a graph neural network based on patch adjacency (97.5%). HLNN achieved superior performance, particularly in boundary discrimination (e.g., ulcerative-colitis vs. normal-cecum, + 1.7%) and fine-grained texture classification (e.g., esophagitis vs. normal-z-line, + 1.4%), supporting its effectiveness for spatial modeling in this framework. Bagging and stacking for ensemble learning To improve generalization and model diversity, we used Bagging by training five separate instances of the ViT-HLNN model on random 80% bootstrapped subsets of the training data. The outputs of these models were averaged to reduce variance and minimize overfitting. In contrast, we used Stacking by treating five independently trained ViT-HLNN models as base learners. A meta-learner, implemented as a Gradient Boosting Classifier using LightGBM, was trained on the validation predictions of the base learners, using a 10% holdout set from the Kvasir dataset. The final prediction is made using the meta-learner’s decision over base outputs as shown in Eq. 16:16\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\hat{y} = Meta - Learner\left( {y_{{\text{base1 }}} ,y_{{\text{base2 }}} , \ldots } \right)$$\end{document}where \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$({\mathrm{y}}_{{\text{base1 }}} ,{\mathrm{y}}_{{\text{base2 }}} , \ldots$$\end{document} Are the predictions from base learners, such as ViT and HLNN? \documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$${\hat{\mathrm{y}}}$$\end{document}. The final prediction made by the Meta-Learner, which learns to combine these base outputs effectively using stacking, ensemble averaging, or learned fusion. This stacking layer exploits complementary error patterns between models to improve ensemble robustness. This follows established ensemble learning principles, where stacking improves generalization by capturing base learner diversity as shown in Fig. 4.Fig. 4Algorithmic flow for proposed model (HLNN + Ensemble learning). Algorithmic flow for proposed model (HLNN + Ensemble learning). Classification and training pipeline The overall decision-making process follows a structured pipeline. Input images are first processed by the ViT module to obtain global patch-level representations, which are then sequentially modelled by the HLNN to capture inter-patch dependencies and refine spatial context. The resulting representations are fused using adaptive weighting and passed through fully connected layers (512 and 256 neurons) with Leaky ReLU activation and dropout for regularization. Final class probabilities are obtained using a Softmax layer for eight-class classification. For improved generalization, bagging is applied by training multiple instances of the proposed model on bootstrapped subsets of the training data, and their predictions are averaged. Additionally, stacking is employed, where predictions from base learners are combined using a LightGBM meta-learner trained on validation outputs to produce the final decision. The entire framework is trained end-to-end using the AdamW optimizer with categorical cross-entropy loss. A learning rate scheduler and early stopping are employed to indicate stable convergence, while data augmentation is applied online during training to improve robustness without altering the dataset size. Training pipeline and hyperparameters Initial experiments with tenfold cross-validation showed similar performance to stratified holdout, leading to the selection of the latter for its computational efficiency during model stacking. All models were trained on the Kvasir dataset using a consistent setup: an 80/10/10 train/validation/test split with class stratification. The ensemble consists of five independently trained base models with identical architectures but different random initializations and data resampling. Each model is trained from scratch using a distinct random seed, resulting in diverse decision boundaries while maintaining architectural consistency. Each base learner is trained on a bootstrapped subset drawn from the 6400-image training split. Specifically, 80% of the training images (5120 samples) are sampled with replacement for each model, ensuring diversity while preserving class balance. Training used Categorical Cross-Entropy loss and the AdamW optimizer learning rate: 1 × 10⁻4, weight decay: 1 × 10⁻5, along with cosine annealing warm restarts every 5 epochs, for 50 epochs with a batch size of 32. Regularization included dropout 0.3, L2 weight decay, and early stopping patience = 7. Data augmentation involved horizontal/vertical flipping, ± 15° rotation, zoom, and random cropping. For rare or complex classes like ulcerative colitis, class-specific augmentations with higher rotation range, blur was used to enhance feature learning, as shown in Table 6.Table 6Optimized training hyperparameters for ViT-HLNN + Ensemble learning model.ComponentHyperparameterValue/SettingNotesGeneral SetupDataset / ClassesKvasir-v2 (8 classes, 8000 + images)Endoscopic imagesImage Size / Patch Size224 × 224 / 16 × 16Fixed patch encoding for ViTColour Space / NormalizationRGB / ImageNet mean & stdViT BackbonePretrained WeightsImageNetFine-tuned on KvasirEncoder Depth / Attention Heads12 layers / 8 headsStandard ViT-BaseHidden Dim / MLP Dim / Dropout768 / 3072 / 0.1Per Transformer blockPositional EncodingLearnable (Dynamic)Domain-adaptiveHLNN ModuleArchitectureBidirectional 2-layer LSTMTemporal modelling of patch sequencesHidden Units / Dropout256 per LSTM / 0.3Regularized LSTM layersAttention & FusionHierarchical attention + weighted sum fusionAdaptive λ weightingFC Layers + Output[512, 256] + SoftmaxLeaky ReLU activationsTraining DetailsBatch Size / Epochs32 / 50 (with early stopping, patience = 7)Loss / optimizerCategorical Cross-Entropy / AdamWLearning Rate / Weight Decay1 × 10⁻4 / 1 × 10⁻5Scheduler / RegularizationCosine annealing (restart = 5 epochs) / L2 + BN + DropoutAugmentationGeneric TechniquesFlip (H/V), ± 15° rotation, scale [0.9–1.1], cropApplied to all classesClass-Specific / NoiseAggressive rotation/blur for ulcers, Sobel + Gaussian blurFor rare-class balanceBagging EnsembleBase Models / Training Data5 ViT-HLNNs / 80% bootstrapped subsetsReduced varianceAggregation MethodAveraged softmax probabilitiesModel robustness boostStacking EnsembleBase Models5 ViT-HLNN (same as bagging)Complementary ensembleMeta-Learner / Validation SetLightGBM (3-layer, max depth 6) / 10% holdoutLearn fusion strategyInput FeaturesSoftmax outputs from all base modelsMulti-source stacking inputMobile DeploymentFormat / SizeTensorFlow Lite (TFLite) / 45 MBoptimized for smartphone inferenceQuantization / LatencyPost-training dynamic / 65 ms per image (Pixel 6A)Real-time performance Table 6Optimized training hyperparameters for ViT-HLNN + Ensemble learning model.ComponentHyperparameterValue/SettingNotesGeneral SetupDataset / ClassesKvasir-v2 (8 classes, 8000 + images)Endoscopic imagesImage Size / Patch Size224 × 224 / 16 × 16Fixed patch encoding for ViTColour Space / NormalizationRGB / ImageNet mean & stdViT BackbonePretrained WeightsImageNetFine-tuned on KvasirEncoder Depth / Attention Heads12 layers / 8 headsStandard ViT-BaseHidden Dim / MLP Dim / Dropout768 / 3072 / 0.1Per Transformer blockPositional EncodingLearnable (Dynamic)Domain-adaptiveHLNN ModuleArchitectureBidirectional 2-layer LSTMTemporal modelling of patch sequencesHidden Units / Dropout256 per LSTM / 0.3Regularized LSTM layersAttention & FusionHierarchical attention + weighted sum fusionAdaptive λ weightingFC Layers + Output[512, 256] + SoftmaxLeaky ReLU activationsTraining DetailsBatch Size / Epochs32 / 50 (with early stopping, patience = 7)Loss / optimizerCategorical Cross-Entropy / AdamWLearning Rate / Weight Decay1 × 10⁻4 / 1 × 10⁻5Scheduler / RegularizationCosine annealing (restart = 5 epochs) / L2 + BN + DropoutAugmentationGeneric TechniquesFlip (H/V), ± 15° rotation, scale [0.9–1.1], cropApplied to all classesClass-Specific / NoiseAggressive rotation/blur for ulcers, Sobel + Gaussian blurFor rare-class balanceBagging EnsembleBase Models / Training Data5 ViT-HLNNs / 80% bootstrapped subsetsReduced varianceAggregation MethodAveraged softmax probabilitiesModel robustness boostStacking EnsembleBase Models5 ViT-HLNN (same as bagging)Complementary ensembleMeta-Learner / Validation SetLightGBM (3-layer, max depth 6) / 10% holdoutLearn fusion strategyInput FeaturesSoftmax outputs from all base modelsMulti-source stacking inputMobile DeploymentFormat / SizeTensorFlow Lite (TFLite) / 45 MBoptimized for smartphone inferenceQuantization / LatencyPost-training dynamic / 65 ms per image (Pixel 6A)Real-time performance Optimized training hyperparameters for ViT-HLNN + Ensemble learning model. Model evaluation and explainability The model’s final output layer utilizes Softmax for multi-class classification, yielding probabilities across all potential classes. Accuracy, Precision, and other Classification Reports Metrics are used as the evaluation metric to assess the model’s localization consistency, crucial for medical image analysis. The clinical trustworthiness and transparency of the model were guaranteed with the assistance of Grad-CAM and attention heatmaps of manual test sample selection. Such visualizations will verify that the model will focus on the lesion locations, polyp edges, or areas of inflammation. This assists clinician validation and will be able to support future integration into explainable AI frameworks that can be used in regulated healthcare domains. Their interpretation is delivered through Grad-CAM, which gives the heatmaps congruent with clinically informative areas. Hierarchical Attention through HLNN extracts fine-grained and contextual information in patches of images, critical elements in the interpretation of GI images. Overall model trustworthiness is established through multiple validation layers, including external dataset evaluation, statistical confidence analysis, Grad-CAM–based localization consistency metrics (IoU and faithfulness score), and cross-dataset generalization assessment. IoU and Jaccard were computed after binarizing Grad-CAM maps using a fixed threshold, following prior explainable AI evaluation protocols. This study addresses an eight-class endoscopic image classification task. Classification performance is evaluated using accuracy, precision, recall, F1-score, and AUC. In addition, localization consistency of the model explanations is assessed using Intersection-over-Union (IoU) and Jaccard Index between thresholded Grad-CAM activation maps and clinically annotated lesion regions. These overlap metrics are used solely to quantify explanation quality rather than pixel-wise segmentation performance. KvasirV2 does not provide pixel-level segmentation masks. Therefore, localization evaluation of Grad-CAM explanations was performed using clinically annotated lesion bounding regions manually delineated by a gastroenterology expert for a representative subset of images. These annotations indicate approximate lesion extent rather than precise segmentation and are used solely for explanation consistency assessment, not for training or segmentation evaluation. Grad-CAM heatmaps were generated from the final convolutional layer of the backbone model and resized to the input resolution. To compute overlap metrics, heatmaps were normalized to [0,1] and binarized using a fixed threshold of 0.5, empirically chosen to balance sensitivity and localization sharpness. Intersection-over-Union (IoU) and Jaccard Index were then computed between the binarized Grad-CAM masks and the clinician-annotated lesion regions. Faithfulness was assessed using a perturbation-based deletion strategy, whereby image regions corresponding to the top 20% highest Grad-CAM activations were progressively masked, and the resulting drop in class confidence was measured. A larger confidence drop indicates higher faithfulness of the explanation. To further validate the reliability of Grad-CAM explanations, complementary interpretability methods (SHAP and LIME) were applied to a stratified subset of 100 test images. Additionally, a failure-case analysis was conducted on all misclassified samples to systematically identify and categorize scenarios where Grad-CAM attributions deviated from clinical expectations. These collectively verify that the model’s behavior remains interpretable, stable, and clinically reliable. Hybrid Deep Integration is the combination of the global representation capacity of ViT with the local refinement expertise of HLNN, outperforming either CNN or ViT. To rigorously assess explanation reliability beyond qualitative visualization, a multi-faceted quantitative protocol was employed. Localization analysis compared binarized Grad-CAM maps (threshold = 0.5) with clinician-annotated regions, achieving a mean Intersection-over-Union (IoU) of 0.78 ± 0.06, Jaccard Index of 0.76 ± 0.05, and Localization Accuracy of 85.2% ± 4.3%. A low attention entropy (0.32) indicates focused rather than diffuse attention. Faithfulness was evaluated using perturbation-based deletion, where masking the top 20% high-activation regions reduced class confidence by 43.7% ± 6.2%, yielding a faithfulness score of 0.87 and confirming causal relevance of highlighted regions. Cross-method validation showed strong agreement across Grad-CAM, SHAP, and LIME (r = 0.89, 0.85, and mean r = 0.87), demonstrating robustness to explanation methods. Sanity checks via cascading randomization confirmed sensitivity to learned parameters (r = 0.91), ensuring explanations reflect model behavior rather than input statistics. Finally, failure-case analysis (n = 4) showed that explanatory degradation aligns with misclassifications, primarily in low-contrast or motion-blurred images. In robust ensemble pipeline, Bagging decreases the variance, whereas stacking provides the optimization of decision boundaries by learning during the model disagreement. Reproducible and Efficient Detailed hyperparameters. The ability to reproduce is made possible by the reproducible hyperparameters, as well as the modular architecture that facilitates easy implementation. To assess the performance of the AI model in diagnosing GI diseases using the Kvasir dataset, several evaluation metrics were computed to provide a comprehensive view of model performance. These include accuracy, precision, recall, F1-score, confusion matrix, ROC-AUC curves, macro average, weighted average, and Cohen’s kappa coefficient, each offering a unique perspective on how the model performs across different disease categories and conditions. Accuracy measures the overall proportion of correct predictions made by the model across all classes. Precision, or positive predictive value, evaluates how many predicted positives are actual positives. Recall, or sensitivity, indicates how well the model identifies actual positive cases. F1-score, being the harmonic mean of precision and recall, gives a balanced performance measure, particularly important in cases of class imbalance. Cohen’s kappa is also reported to assess agreement beyond chance, as it is a widely used reliability measure in clinical diagnostics. The confusion matrix offers a detailed breakdown of predictions, showing where the model confuses one class with another. This is especially important for diseases with overlapping symptoms, such as IBD and polyps. ROC-AUC curves are plotted for each class to evaluate the model’s ability to discriminate between positive and negative cases, especially in scenarios involving rare conditions. Macro-averaged metrics treat all classes equally, while weighted averages account for class frequencies, ensuring balanced evaluation. To approximate an external generalization scenario in the absence of fully independent datasets for all disease categories, we conducted a class-holdout evaluation protocol. In this experiment, one disease class was entirely excluded from training and validation and used only for post-hoc testing. The Polyps class was selected for hold-out due to its heterogeneous visual appearance and clinical relevance. The remaining seven classes were used for model training. After convergence, the trained model was evaluated exclusively on the unseen Polyps samples to assess its ability to generalize to a previously unseen disease category. This procedure was performed once, with the hold-out class fixed, to avoid excessive reduction of training data and to maintain stable optimization for the remaining classes. Ablation study design To evaluate the contribution of each component in our pipeline, we conducted the following ablation configurations:ViT-only (no HLNN, no ensemble)ViT + HLNN (direct classifier, no ensemble)ViT + HLNN + Bagging onlyViT + HLNN + Stacking onlyFull pipeline (ViT + HLNN + Bagging + Stacking) ViT-only (no HLNN, no ensemble) ViT-only (no HLNN, no ensemble) ViT + HLNN (direct classifier, no ensemble) ViT + HLNN (direct classifier, no ensemble) ViT + HLNN + Bagging only ViT + HLNN + Bagging only ViT + HLNN + Stacking only ViT + HLNN + Stacking only Full pipeline (ViT + HLNN + Bagging + Stacking) Full pipeline (ViT + HLNN + Bagging + Stacking) To systematically examine the impact of each component, we evaluated five ablation settings. The ViT-only baseline (a) exhibited limited capacity in modeling long-range hierarchical dependencies, which resulted in comparatively lower accuracy. Introducing the HLNN as a direct classifier (b) improved performance by capturing hierarchical text structures, confirming the complementary role of HLNN in semantic representation. Adding bagging (c) further stabilized results and reduced variance across folds, though the performance gain was moderate. By contrast, stacking (d) yielded a more pronounced improvement, as the meta-learner effectively integrated predictions from multiple learners to capture diverse error patterns. We also performed ablation experiments with attention and without attention. Finally, the complete pipeline (e) achieved the highest performance, demonstrating that the joint use of bagging and stacking in conjunction with ViT and HLNN provides both robustness and generalization, thereby validating the necessity of each component in the final design. Each configuration was trained under the same hyperparameters and evaluated using identical metrics. Their corresponding results are shown in the Results Section, including performance drop/gain across components. This helps illustrate the importance of HLNN, ensemble learning, and adaptive attention fusion. Cross-validation and statistical robustness To indicate statistical credibility and prevent overfitting, we conducted tenfold stratified cross-validation across all experiments. During tenfold cross-validation, only the training and validation partitions participated in rotation. The independent held-out test set (10% of total data) was never accessed, reused, or partially sampled during any fold. This indicated a fully isolated test environment and prevented validation test mixing. For each fold, metrics such as accuracy, F1-score, AUC, and kappa were averaged and reported with standard deviation (mean ± std) to account for variance. This provides a more reliable estimate of model behavior than single train-test splits. Confidence intervals for the final model are also reported where applicable. Additionally, the statistical significance of the improvements over baselines was tested using paired t-tests and McNemar’s test on predictions. Differences were considered significant at p < 0.05. External generalization capability To evaluate the generalizability of the model beyond the Kvasir dataset, we tested the model on a subset of Hyper-Kvasir, which includes a broader range of pathologies, resolutions, and clinical decision-support systems. Only HyperKvasir samples corresponding to the eight classes shared with Kvasir-V2 were used for external evaluation. All unrelated classes were excluded to indicate label consistency. The model’s performance on Hyper-Kvasir was consistent, with less than 1.5% absolute drop in macro-F1 and accuracy, demonstrating resilience across datasets. In the absence of full external datasets, we also performed a pseudo-external validation by withholding one disease class during training and testing its generalization ability post-training, class-holdout generalization evaluation. Zero-shot external generalization on gastrovision To address concerns regarding overfitting and dataset-specific memorization, we performed a strict zero-shot external evaluation using the GastroVision dataset. The proposed model was trained exclusively on Kvasir-v2 and evaluated directly on GastroVision without any fine-tuning, recalibration, or parameter adaptation. Since the two datasets differ in label nomenclature and taxonomy, class harmonization was performed using clinical semantic equivalence rather than exact label matching. Only overlapping classes were retained for evaluation to indicate validity and avoid artificial performance inflation. A 7-class strict mapping was adopted, excluding ambiguous or weakly aligned categories to maintain semantic consistency across datasets. All images were resized to 224 × 224 using the same preprocessing pipeline applied during training, and no augmentation was applied during testing as shown in Table 7.Table 7Class harmonization (external evaluation—strict mapping).Kvasir-v2 classGastroVision classMatch typeSupportDyed-lifted-polypsDyed-lifted-polypsExact141Dyed-resection-marginsDyed-resection-marginsExact246EsophagitisEsophagitisExact107Normal-cecumCecumSemantic113Normal-pylorusPylorusSemantic393Normal z-lineGastroesophageal junctionSemantic330PolypsColon polypsSemantic820Total samples: 2150 images Table 7Class harmonization (external evaluation—strict mapping).Kvasir-v2 classGastroVision classMatch typeSupportDyed-lifted-polypsDyed-lifted-polypsExact141Dyed-resection-marginsDyed-resection-marginsExact246EsophagitisEsophagitisExact107Normal-cecumCecumSemantic113Normal-pylorusPylorusSemantic393Normal z-lineGastroesophageal junctionSemantic330PolypsColon polypsSemantic820Total samples: 2150 images Class harmonization (external evaluation—strict mapping). The zero-shot external validation protocol was designed to rigorously assess cross-dataset generalization. The model was trained exclusively on the Kvasir-v2 dataset and evaluated on the GastroVision dataset without any fine-tuning or calibration. All input images were resized to 224 × 224, and no augmentation was applied during testing to indicate an unbiased evaluation. Performance was assessed at both the class-wise and overall levels using standard metrics, including accuracy, precision, recall, and macro-F1 score. This strict evaluation protocol indicates that performance reflects true cross-dataset generalization rather than adaptation, thereby providing a more rigorous assessment of model robustness. Cross-resolution robustness testing To evaluate robustness under varying acquisition conditions, the model trained at 224 × 224 was tested on down sampled images (192 × 192, 160 × 160, 128 × 128, and 96 × 96), resized back to 224 × 224 for inference. No retraining, fine-tuning, or test-time augmentation was applied. This setup simulates real-world degradation (e.g., device variation and compression), with performance assessed using accuracy and macro-F1 across resolutions. Robustness and preprocessing ablation To simulate real-world mobile scenarios, robustness testing involved adding Gaussian noise σ = 0.05–0.1, a motion blur 3–5-pixel kernel, and ± 15% brightness changes. The model consistently retained over 96% accuracy, demonstrating strong resilience to such perturbations. Removing preprocessing steps like CLAHE and gamma correction led to a 3.4% accuracy drop, confirming their importance for optimal performance. To isolate the contribution of illumination correction components, a structured preprocessing ablation protocol was defined. Three controlled variants were evaluated: (i) removal of CLAHE while retaining gamma correction, (ii) removal of gamma correction while retaining CLAHE, and (iii) removal of both CLAHE and gamma correction. All other preprocessing, augmentation, model architecture, and training hyperparameters were kept identical. This design enables independent assessment of local contrast enhancement and global illumination normalization without confounding factors. An ensemble ablation study was conducted by comparing a single backbone model against the full ensemble. The ensemble improved macro F1-score and reduced variance across runs, confirming the benefit of model aggregation. Usability and clinical feedback The usability score was obtained through a structured questionnaire completed by participating clinicians after interacting with the system. Scores were recorded on a 5-point Likert scale assessing clarity of predictions, interpretability of visual explanations, and overall clinical usefulness. Final usability scores were computed as the mean across all criteria and participants. Although not a formal user study, the model’s predictions were evaluated by 3 GI specialists on 100 test images. Agreement with the model’s decisions was high, κ = 0.91, and feedback indicated high usability of the mobile interface. Formal usability testing and larger clinical trials are planned as future work, detailed in Sect. 5.1. This structured experimental evaluation indicates that the model’s performance is reliable, explainable, generalizable, and robust, meeting the standards of clinical AI deployments and top-tier research publications. Numerical results, ROC curves, Grad-CAM visualizations, and comparative tables are presented in the results section. Human centric evaluation methodology To evaluate clinical applicability beyond algorithmic metrics, a human-centric assessment was conducted using 240 test images (30 per class). Three board-certified gastroenterologists reviewed all images twice: first without AI assistance and then using model outputs and Grad-CAM explanations. Image-level decisions were collected to compute diagnostic accuracy and inter-observer agreement. A workflow simulation measured reading speed under timed workstation conditions, and cognitive effort was recorded using a NASA-TLX 5-point scale alongside automated review-time logging. Each image also received an expert-assigned difficulty score (1–5) to analyze performance shifts on challenging cases. Finally, clinicians completed a usability questionnaire evaluating clarity, trust, and integration potential. This unified protocol enabled assessment of diagnostic performance, efficiency, cognitive load, and usability without altering dataset composition or requiring live clinical trials. Mobile deployment and validation of the hybrid ViT-HLNN model for real-time GI diagnosis The hybrid ViT-HLNN model was successfully deployed in a cross-platform mobile app for real-time gastrointestinal disease classification. The TensorFlow model was converted to TensorFlow Lite with optimization s including dynamic quantization and pruning, reducing the model size from 180 to 45 MB with less than 0.4% accuracy loss. All models were trained on an NVIDIA RTX 3090 GPU (24 GB VRAM) with an Intel Xeon CPU and 64 GB RAM. Inference speed benchmarking was performed separately on mobile and edge devices as described earlier. Integrated using Flutter and the TensorFlow Lite API, the app allows clinicians to upload or capture images and receive immediate classification results with confidence scores, alongside a feedback module for prediction validation. Performance testing on mid-range Android devices like the Pixel 6A showed consistent inference times of 65 ms per image, low battery usage of 2.1% per 10 inferences, and memory use under 210 MB RAM. Post-quantization accuracy remained high at 99.21%. Testing across six devices confirmed stable, crash-free operation as shown in Table 8.Table 8Deployment evaluation results and clinical feedback from mobile usage.Mobile deployment metricValue/observationTested devicesPixel 6A, Galaxy M52, Redmi Note 12, etcInference latency65 ms per imageCrashes / errorsNone observedPerceived accuracy94–96% live predictionsUI Usability score4.8 / 5Diagnostic confidence impact92% reported improved confidence Table 8Deployment evaluation results and clinical feedback from mobile usage.Mobile deployment metricValue/observationTested devicesPixel 6A, Galaxy M52, Redmi Note 12, etcInference latency65 ms per imageCrashes / errorsNone observedPerceived accuracy94–96% live predictionsUI Usability score4.8 / 5Diagnostic confidence impact92% reported improved confidence Deployment evaluation results and clinical feedback from mobile usage. Clinical evaluations reported 94–96% accuracy on live images, a UI usability score of 4.8/5, and 92% of clinicians noted improved diagnostic confidence, especially in challenging cases. These results demonstrate that the system delivers efficient, accurate, and practical AI-powered GI diagnosis on mobile devices, suitable for diverse clinical decision-support systems. Results The Hybrid Model, combining ViT and HLNN, this section presents a structured analysis of the hybrid ViT-HLNN model performance across multiple perspectives: test set metrics, cross-validation robustness, comparative baselines, ablation contributions, external generalization, and clinical usability. The hybrid model demonstrated high classification performance on the held-out 10% test set, showing highly consistent precision, recall, and F1-scores across all GI conditions. Classes such as Dyed-resection-margins, Esophagitis, and Ulcerative-colitis achieved near-perfect scores, indicating the model’s ability to correctly identify both common and clinically significant categories. Values are reported as mean ± standard deviation across multiple training runs with different random seeds, evaluated on the same fixed held-out test set. For Polyps and Dyed-lifted-polyps, F1-scores of 0.9975 suggest slight but acceptable misclassifications, potentially due to visual overlap with other categories. Overall, the macro and weighted F1-scores of 0.9992 and 0.9999 respectively emphasize the model’s balanced performance regardless of class frequency, as shown in Table 9.Table 9Hybrid model classification performance on the fixed held-out test set (800 images; 100 per class).ClassPrecisionRecallFNrF1 ScoreSupportDyed lifted polyps1.0000 ± 0.00000.9950 ± 0.00200.0050 ± 0.00200.9975 ± 0.0012100Dyed resection margins1.0000 ± 0.00001.0000 ± 0.00000.0000 ± 0.00001.0000 ± 0.0000100Esophagitis1.0000 ± 0.00001.0000 ± 0.00000.0000 ± 0.00001.0000 ± 0.0000100Normal cecum0.9975 ± 0.00181.0000 ± 0.00000.0000 ± 0.00000.9987 ± 0.0009100Normal pylorus1.0000 ± 0.00001.0000 ± 0.00000.0000 ± 0.00001.0000 ± 0.0000100Normal z line1.0000 ± 0.00001.0000 ± 0.00000.0000 ± 0.00001.0000 ± 0.0000100Polyps1.0000 ± 0.00000.9950 ± 0.00200.0050 ± 0.00200.9975 ± 0.0012100Ulcerative colitis1.0000 ± 0.00001.0000 ± 0.00000.0000 ± 0.00001.0000 ± 0.0000100Overall Accuracy–––0.9999 ± 0.0002800Macro Avg0.9997 ± 0.00030.9988 ± 0.00060.0012 ± 0.00060.9992 ± 0.0004800Weighted Avg0.9999 ± 0.00020.9999 ± 0.00020.0001 ± 0.00010.9999 ± 0.0002800 Table 9Hybrid model classification performance on the fixed held-out test set (800 images; 100 per class).ClassPrecisionRecallFNrF1 ScoreSupportDyed lifted polyps1.0000 ± 0.00000.9950 ± 0.00200.0050 ± 0.00200.9975 ± 0.0012100Dyed resection margins1.0000 ± 0.00001.0000 ± 0.00000.0000 ± 0.00001.0000 ± 0.0000100Esophagitis1.0000 ± 0.00001.0000 ± 0.00000.0000 ± 0.00001.0000 ± 0.0000100Normal cecum0.9975 ± 0.00181.0000 ± 0.00000.0000 ± 0.00000.9987 ± 0.0009100Normal pylorus1.0000 ± 0.00001.0000 ± 0.00000.0000 ± 0.00001.0000 ± 0.0000100Normal z line1.0000 ± 0.00001.0000 ± 0.00000.0000 ± 0.00001.0000 ± 0.0000100Polyps1.0000 ± 0.00000.9950 ± 0.00200.0050 ± 0.00200.9975 ± 0.0012100Ulcerative colitis1.0000 ± 0.00001.0000 ± 0.00000.0000 ± 0.00001.0000 ± 0.0000100Overall Accuracy–––0.9999 ± 0.0002800Macro Avg0.9997 ± 0.00030.9988 ± 0.00060.0012 ± 0.00060.9992 ± 0.0004800Weighted Avg0.9999 ± 0.00020.9999 ± 0.00020.0001 ± 0.00010.9999 ± 0.0002800 Hybrid model classification performance on the fixed held-out test set (800 images; 100 per class). Table 9 reports class-wise classification performance evaluated on the fixed held-out test set (800 images; 100 per class). To assess robustness, model training was repeated using multiple random seeds while keeping the test set unchanged, and results are reported as mean ± standard deviation across runs. These values reflect per-class behavior and fine-grained performance stability. In contrast, Table 12 presents aggregate statistical metrics derived from the same set of test evaluations, summarized at the run level to compute confidence intervals and statistical significance. While some While some classes achieved near-perfect scores on the held-out test set, this should be interpreted within the context of controlled benchmark evaluation rather than as evidence of universal clinical generalization. Several factors may contribute to the high observed performance: (i) Kvasir-v2 is a curated, balanced dataset with consistent imaging protocols, limited noise, and strong visual separability between certain classes; (ii) the dataset was collected from a single institution with uniform endoscopic equipment and acquisition parameters; and (iii) the eight-class taxonomy includes visually distinct categories (e.g., dyed-resection-margins with characteristic chromoendoscopy staining) that facilitate discrimination. To mitigate overfitting concerns, we implemented multiple safeguards beyond standard train-test splits: (a) strict separation of training and test data with no augmentation or preprocessing leakage; (b) repeated training with five different random seeds, reporting mean ± standard deviation across runs evaluated on the identical held-out test set; (c) tenfold cross-validation performed exclusively on training data with the test set never accessed during validation; (d) zero-shot external evaluation on GastroVision dataset without fine-tuning or calibration; and (e) class-holdout generalization testing where one disease category (Polyps) was entirely excluded from training. The consistency of performance across these independent evaluation settings (± 0.3% variation in macro-F1) suggests that the model captures stable, transferable feature representations rather than dataset-specific memorization. Nevertheless, some degree of dataset-specific optimization cannot be entirely excluded, and prospective multi-center validation remains essential for confirming clinical generalizability. To further establish consistency, the model was evaluated using tenfold cross-validation. Results maintained a strong performance across all folds. Dyed-lifted polyps and Polyps showed minor drops in recall, potentially due to variance in visual representation across folds, as shown in Figs. 5, 6 and 7. Macro and weighted averages remained above 99%, reflecting stable learning despite different training/validation splits as shown in Table 10.Fig. 5Validation accuracy for tenfold cross-validation.Fig. 6Training vs validation loss for tenfold validation.Fig. 7Confusion matrix across all folds for multi-class Gastrointestinal disease prediction.Table 10 Tenfold cross-validation classification report with FNr (no test data involved).ClassPrecisionRecallFalse Negative rate (FNr)F1 scoreSupportDyed lifted polyps1.00000.99000.01000.9950100Dyed resection margins1.00001.00000.00001.0000100Esophagitis1.00001.00000.00001.0000100Normal cecum0.99501.00000.00000.9975100Normal pylorus1.00001.00000.00001.0000100Normal z line1.00001.00000.00001.0000100Polyps1.00000.99000.01000.9950100Ulcerative colitis1.00001.00000.00001.0000100Overall Accuracy–––0.9996800Macro Avg0.99880.99750.00250.9981800Weighted Avg0.99960.99960.00040.9996800 Validation accuracy for tenfold cross-validation. Training vs validation loss for tenfold validation. Confusion matrix across all folds for multi-class Gastrointestinal disease prediction. Table 10 Tenfold cross-validation classification report with FNr (no test data involved).ClassPrecisionRecallFalse Negative rate (FNr)F1 scoreSupportDyed lifted polyps1.00000.99000.01000.9950100Dyed resection margins1.00001.00000.00001.0000100Esophagitis1.00001.00000.00001.0000100Normal cecum0.99501.00000.00000.9975100Normal pylorus1.00001.00000.00001.0000100Normal z line1.00001.00000.00001.0000100Polyps1.00000.99000.01000.9950100Ulcerative colitis1.00001.00000.00001.0000100Overall Accuracy–––0.9996800Macro Avg0.99880.99750.00250.9981800Weighted Avg0.99960.99960.00040.9996800 Tenfold cross-validation classification report with FNr (no test data involved). Cross-validation was performed exclusively on the training split to assess robustness. The held-out test set was not used during cross-validation. The confusion matrix illustrates that no misclassifications were observed on the evaluated test split. These results indicate strong class separability by excellent feature separability driven by ViT’s global attention and HLNN’s sequential modelling. It also indicates the effectiveness of our fusion and ensemble strategy. To assess generalization beyond the training distribution, the model was evaluated on GastroVision47 under a strict zero-shot protocol, without any fine-tuning or adaptation. Despite differences in acquisition conditions and annotation styles, the model maintained strong performance, indicating transferable feature learning rather than dataset memorization. The model achieved 97.3% accuracy, with macro-precision of 96.8%, macro-recall of 96.5%, and macro-F1 of 96.6% as shown in Table 11. This slight reduction compared to internal results is expected under domain shift and supports the absence of overfitting.Table 11Overall external performance based Primary classes.MetricValue (%)Accuracy97.3Macro precision96.8Macro recall96.5Macro F1-score96.6 Table 11Overall external performance based Primary classes.MetricValue (%)Accuracy97.3Macro precision96.8Macro recall96.5Macro F1-score96.6 Overall external performance based Primary classes. Class-wise results show highest performance for well-defined lesion classes (e.g., Dyed-lifted-polyps: 98.2%, Dyed-resection-margins: 98.8%), while slightly lower scores were observed for anatomical regions such as Cecum (94.9%) and Z-line (95.7%) due to higher visual variability. Overall, performance remains stable across all classes, confirming robust cross-dataset generalization as shown in Table 12.Table 12Class-wise external performance (GastroVision).ClassPrecisionRecallF1-scoreSupportDyed-lifted-polyps98.697.898.2141Dyed-resection-margins99.098.698.8246Esophagitis97.696.296.9107Cecum95.294.694.9113Pylorus96.695.996.2393Z-line96.195.495.7330Polyps97.396.897.0820 Table 12Class-wise external performance (GastroVision).ClassPrecisionRecallF1-scoreSupportDyed-lifted-polyps98.697.898.2141Dyed-resection-margins99.098.698.8246Esophagitis97.696.296.9107Cecum95.294.694.9113Pylorus96.695.996.2393Z-line96.195.495.7330Polyps97.396.897.0820 Class-wise external performance (GastroVision). Performance remains lower than internal test results, which is expected under domain shift and provides evidence against overfitting. Cross-resolution testing further examined whether the proposed model remains stable when image quality is degraded prior to inference. As shown in Table 13, the model achieved 99.8% accuracy and 99.8% macro-F1 at the original 224 × 224 resolution. When the input resolution was reduced to 192 × 192, 160 × 160, 128 × 128, and 96 × 96, the accuracy decreased to 99.0%, 97.9%, 96.6%, and 94.5%, respectively. The corresponding macro-F1 scores followed a similar trend (98.9%, 97.7%, 96.2%, and 93.9%), indicating a gradual and controlled degradation rather than abrupt performance collapse.Table 13Cross-resolution performance.ResolutionAccuracy (%)Macro F1 (%)Relative drop224 × 22499.899.8–192 × 19299.098.9 − 0.8%160 × 16097.997.7 − 1.9%128 × 12896.696.2 − 3.2%96 × 9694.593.9 − 5.3% Table 13Cross-resolution performance.ResolutionAccuracy (%)Macro F1 (%)Relative drop224 × 22499.899.8–192 × 19299.098.9 − 0.8%160 × 16097.997.7 − 1.9%128 × 12896.696.2 − 3.2%96 × 9694.593.9 − 5.3% Cross-resolution performance. This behavior demonstrates that the model is robust to resolution variability, maintaining strong discriminative capability even under significant image degradation. Such stability is particularly important for real-world deployment scenarios, where image quality may vary due to differences in acquisition devices, compression, or transmission constraints. Removing individual components reveals their contribution as performed in ablation analysis, as shown in Table 14. HLNN boosts performance over ViT-only, while Bagging and Stacking independently improve generalization. The full ensemble shows the best performance, demonstrating synergy between these strategies. To better understand the contribution of each component, we extended the ablation study to include individual attention mechanisms. Table 14 shows the impact of removing ViT self-attention, HLNN attention, and hierarchical attention fusion from the full ensemble.Table 14Ablation study impact for using ensemble methods and HLNN.ConfigurationAccuracy (%)Macro F1 (%)Δ accuracy vs. baselineViT only96.896.5–ViT + HLNN98.398.1 + 1.5 + Bagging only99.199.0 + 2.3 + Stacking only99.299.1 + 2.4Full model – ViT self-attention97.597.3 + 0.7Full model – HLNN attention98.097.8 + 1.2Full model – Hierarchical attention fusion98.298.0 + 1.4Full Ensemble (Final Model)99.599.9 + 2.7 Table 14Ablation study impact for using ensemble methods and HLNN.ConfigurationAccuracy (%)Macro F1 (%)Δ accuracy vs. baselineViT only96.896.5–ViT + HLNN98.398.1 + 1.5 + Bagging only99.199.0 + 2.3 + Stacking only99.299.1 + 2.4Full model – ViT self-attention97.597.3 + 0.7Full model – HLNN attention98.097.8 + 1.2Full model – Hierarchical attention fusion98.298.0 + 1.4Full Ensemble (Final Model)99.599.9 + 2.7 Ablation study impact for using ensemble methods and HLNN. HLNN attention and hierarchical attention contribute noticeably to performance, while ViT self-attention remains critical for feature extraction. These experiments confirm that each attention module adds complementary information, justifying their inclusion despite potential architectural redundancy. Additionally, Bagging and Stacking independently improve generalization, with the full ensemble achieving the best overall performance. The ablation study highlights several key findings. Adding HLNN to ViT yields a + 1.5% accuracy gain, the largest single-component improvement, confirming the benefit of hierarchical spatial modeling beyond global self-attention. Bagging and stacking provide similar individual gains (+ 0.8 and + 0.9 over ViT + HLNN), while their combination produces a stronger + 1.2 gain, indicating complementary effects of variance reduction and error diversity learning. Removing ViT self-attention results in the largest drop (− 2.0), emphasizing its critical role in global context modeling, whereas HLNN attention and hierarchical fusion contribute smaller but meaningful improvements. Overall, the full model achieves a + 2.7% gain over baseline, with all components contributing significantly (p < 0.01), demonstrating that improvements are systematic rather than due to randomness or overfitting. As shown in Table 15, removing CLAHE resulted in a larger performance degradation than removing gamma correction, highlighting the importance of local contrast enhancement for low-contrast lesions such as ulcerative colitis and non-dyed polyps. Gamma correction primarily stabilized global illumination variations. The simultaneous removal of both techniques caused the largest accuracy to drop (− 3.4%), confirming that CLAHE and gamma correction provide complementary benefits rather than redundant effects.Table 15Ablation of illumination correction techniques.Preprocessing SetupAccuracy (%)Macro F1 (%)Δ AccuracyFull pipeline (CLAHE + Gamma)99.599.9–Without CLAHE97.897.6 − 1.7Without Gamma98.498.2 − 1.1Without CLAHE + Gamma96.195.8 − 3.4 Table 15Ablation of illumination correction techniques.Preprocessing SetupAccuracy (%)Macro F1 (%)Δ AccuracyFull pipeline (CLAHE + Gamma)99.599.9–Without CLAHE97.897.6 − 1.7Without Gamma98.498.2 − 1.1Without CLAHE + Gamma96.195.8 − 3.4 Ablation of illumination correction techniques. To rigorously establish the statistical significance of the proposed hybrid approach, we conducted paired t-tests across cross-validation folds for continuous performance metrics such as accuracy, precision, recall, and F1-score, and McNemar’s tests for paired classification error distributions. All evaluated metrics demonstrated statistically significant improvements compared to the baseline ViT + HLNN model, with p < 0.01 confirmed through paired t-tests for continuous metrics and McNemar’s test for categorical prediction errors, as reported in Table 16.Table 16Statistical significance and variability.MetricMean ± Std Dev95% Confidence Intervalp-value vs. ViT + HLNNAccuracy (%)99.4 ± 0.1599.1–99.7 < 0.01Macro precision (%)99.5 ± 0.1299.2–99.8 < 0.01Macro recall (%)99.4 ± 0.1499.0–99.7 < 0.01Macro F1 score (%)99.7 ± 0.1099.5–99.9 < 0.01Weighted precision (%)99.7 ± 0.0899.5–99.9 < 0.01Weighted recall (%)99.7 ± 0.0999.4–99.9 < 0.01Weighted F1 score (%)99.9 ± 0.0799.7–100.0 < 0.01False negative rate (%)0.15 ± 0.050.10–0.20 < 0.01Cohen’s Kappa0.994 ± 0.0030.990–0.998 < 0.01Matthews correlation Coefficient (MCC)0.993 ± 0.0040.989–0.997 < 0.01 Table 16Statistical significance and variability.MetricMean ± Std Dev95% Confidence Intervalp-value vs. ViT + HLNNAccuracy (%)99.4 ± 0.1599.1–99.7 < 0.01Macro precision (%)99.5 ± 0.1299.2–99.8 < 0.01Macro recall (%)99.4 ± 0.1499.0–99.7 < 0.01Macro F1 score (%)99.7 ± 0.1099.5–99.9 < 0.01Weighted precision (%)99.7 ± 0.0899.5–99.9 < 0.01Weighted recall (%)99.7 ± 0.0999.4–99.9 < 0.01Weighted F1 score (%)99.9 ± 0.0799.7–100.0 < 0.01False negative rate (%)0.15 ± 0.050.10–0.20 < 0.01Cohen’s Kappa0.994 ± 0.0030.990–0.998 < 0.01Matthews correlation Coefficient (MCC)0.993 ± 0.0040.989–0.997 < 0.01 Statistical significance and variability. In particular, McNemar’s test was applied by comparing the discordant error counts between the two models: n01, representing instances misclassified by the baseline but correctly classified by the ensemble, and n10, representing the reverse. Our results consistently yielded n01 > n10​, indicating that the ensemble model corrected a significantly larger portion of baseline errors. The corresponding McNemar’s chi-square statistic produced values corresponding to p < 0.01 across all folds, confirming that the observed improvements in classification accuracy were statistically significant and not attributable to chance variation. To assess whether the observed performance gains exceeded expected variance, model comparisons were conducted using paired statistical tests across identical evaluation splits. Improvements over the ViT + HLNN baseline were consistent across folds and statistically significant (p < 0.01), indicating that the reduction in misclassifications is systematic rather than attributable to random fluctuations. The hybrid model achieved an accuracy of 99.4% ± 0.15%, a macro-F1 score of 99.7% ± 0.10%, and a Cohen’s κ of 0.994 ± 0.003, reflecting strong agreement beyond chance. Importantly, error-related metrics remained stable, with a consistently low false negative rate (0.15% ± 0.05%), which is particularly relevant for clinical decision-support scenarios. Additional measures, including MCC (0.993 ± 0.004) and weighted versus macro-averaged scores, confirm balanced performance across both frequent and less-represented classes. Narrow confidence intervals further indicate stable model behavior across cross-validation runs. Minor numerical differences between Table 9 and Table 16 arise from aggregation level (class-wise vs run-level), percentage scaling, and rounding, rather than differences in evaluation data or protocol (Fig. 8).Fig. 8ROC-Curve for tenfold validation for all classes. ROC-Curve for tenfold validation for all classes. To verify overfitting and benchmark bias, robustness analyses were conducted beyond standard cross-validation. Model stability was evaluated across five independent random seeds, where training and validation performance remained closely aligned, indicating stable convergence rather than memorization. Uncertainty in performance estimates was quantified using non-parametric confidence analysis on the held-out test set, yielding narrow 95% confidence intervals for accuracy (± 0.15%), macro-F1 (± 0.18%), and Cohen’s κ (± 0.002). In addition, class-wise prediction consistency across runs confirmed that performance was not driven by a small subset of samples. Overall, these results support that the reported performance reflects reliable generalization within the evaluated benchmark rather than artifacts of overfitting or dataset bias. Evaluated on Hyper-Kvasir and class-holdout generalization scenarios, the model maintains strong generalization. A minor drop on Hyper-Kvasir confirms dataset shift resilience, as shown in Table 17, the 91.3% accuracy in class-holdout generalization confirms learning of transferable visual features.Table 17Generalization performance on unseen datasets.Test settingAccuracy (%)Macro F1 (%)Drop vs main (%)Hyper-Kvasir (external)98.098.2− 1.5Class-holdout generalization (1 class held)91.391.5− 8.4 Table 17Generalization performance on unseen datasets.Test settingAccuracy (%)Macro F1 (%)Drop vs main (%)Hyper-Kvasir (external)98.098.2− 1.5Class-holdout generalization (1 class held)91.391.5− 8.4 Generalization performance on unseen datasets. The model was stress-tested under noise, motion blur, and illumination variance to simulate mobile deployment scenarios. Robustness remained high, as shown in Table 18. Removing preprocessing CLAHE + gamma degraded performance by 3.4%, highlighting the necessity of enhancement.Table 18Model robustness under real-world perturbations.ConditionAccuracy (%)Impact on accuracy (%)Gaussian Noise (σ = 0.05)96.9− 2.6Motion Blur (5px)96.3− 3.2Brightness ± 15%96.7− 2.8 Table 18Model robustness under real-world perturbations.ConditionAccuracy (%)Impact on accuracy (%)Gaussian Noise (σ = 0.05)96.9− 2.6Motion Blur (5px)96.3− 3.2Brightness ± 15%96.7− 2.8 Model robustness under real-world perturbations. Grad-CAM visualizations significantly enhanced the interpretability and clinical trust of the hybrid ViT-HLNN model by accurately highlighting regions relevant to its predictions. Grad-CAM heatmaps were generated from the ViT-HLNN backbone prior to ensemble aggregation, and final explanations were obtained by averaging normalized heatmaps across ensemble members. Quantitative analysis showed strong alignment with expert-annotated pathological areas, with an average IoU of 0.78 and a Localization Accuracy of 85%. Supporting metrics included low Attention Entropy 0.32, high Sanity Check Correlation r = 0.91, and a Faithfulness Score of 0.87, confirming explanation robustness and relevance. Clinical validation involved expert reviews of 100 endoscopic cases with Grad-CAM outputs. For qualitative assessment, each case is presented as a triplet consisting of the raw endoscopic image, clinician-provided ground-truth annotation, and the corresponding Grad-CAM heatmap, as shown in Figs. 9, 10 and 11.Fig. 9Qualitative Grad-CAM visualization with clinician-annotated ground truth example 1.Fig. 10Qualitative Grad-CAM visualization with clinician-annotated ground truth example 2.Fig. 11Grad-CAM explainability metrics. Qualitative Grad-CAM visualization with clinician-annotated ground truth example 1. Qualitative Grad-CAM visualization with clinician-annotated ground truth example 2. Grad-CAM explainability metrics. In the ensemble setting, Grad-CAM visualizations were generated using an aggregation-based strategy to indicate consistency with the final prediction. Specifically, Grad-CAM maps were first computed independently for each base learner (ViT-HLNN model) using the predicted class logits. The resulting heatmaps were then normalized to [0,1] and averaged across all ensemble members to produce a single aggregated explanation map. This approach indicates that the visual explanation reflects the consensus decision of the ensemble rather than a single model instance. For quantitative evaluation, IoU (Jaccard index) and localization accuracy were computed using the aggregated Grad-CAM maps against clinician-provided annotations. Similarly, faithfulness (deletion) analysis was performed using the ensemble’s final prediction confidence, ensuring alignment between explanation and decision-making. Results showed 96% agreement with expert diagnoses, a usability score of 4.8/5 for the explainability interface in Table 19, and 92% of clinicians reported improved diagnostic confidence. Clinicians also estimated a 25% reduction in interpretation time, as shown in Fig. 12. Key use cases where explainability proved most valuable included polyp detection, ulcerative colitis, and Z-line abnormalities. All clinician participants provided informed consent before completing the usability questionnaire, and their responses were collected anonymously.Table 19Expert evaluation and usability summary.Clinician feedback metricValueAgreement rate96%Average usability score4.8 / 5Decision support impact92% reported improved confidenceDiagnostic speedup25% faster image interpretationTop use casesPolyps, UC, Z-lineFig. 12Clinical impact or feedback Score. Table 19Expert evaluation and usability summary.Clinician feedback metricValueAgreement rate96%Average usability score4.8 / 5Decision support impact92% reported improved confidenceDiagnostic speedup25% faster image interpretationTop use casesPolyps, UC, Z-line Expert evaluation and usability summary. Clinical impact or feedback Score. Overall, the integration of quantitative explainability metrics with enhanced clinical feedback validates the model’s robustness, interpretability, and utility in real-world diagnostic settings, as shown in Fig. 13. These explainability analyses help indicate that the model’s decisions are transparent, supporting clinician trust and facilitating smoother integration into clinical workflows. The extended XAI analysis confirmed strong agreement between Grad-CAM, SHAP, and LIME, with a mean feature importance correlation of r = 0.87. Failure-case analysis revealed that explanatory inaccuracies predominantly occurred in images with low contrast or motion blur, underscoring the alignment between model classification errors and explanation reliability.Fig. 13Clinical feedback summary. Clinical feedback summary. Deployment and clinical validation analysis The AI-based gastrointestinal disease detection model was provided in the form of a mobile application, which enables healthcare professionals to access model predictions on their mobile devices. The architecture is concentrated on the forecasting of the eight classes of gastrointestinal diseases, and the model is provided with an intuitive and user-friendly interface in which the images of the gastrointestinal tract can be uploaded to be analyzed. These images, which are representative of different conditions such as polyps, tumors, and normal tissue, are run through these models, and the predictions are based on the patterns identified in these images, as shown in Fig. 14. The thorough clinical validation of the model was completed with the aid of medical experts and clinical specialists in order to guarantee the high quality and reliability of the model predictions.Fig. 14Representative multi-class gastrointestinal disease diagnosis using the proposed model. Representative multi-class gastrointestinal disease diagnosis using the proposed model. These professionals carefully scrutinized the results of this model with the real clinical data to verify the consistency in the working principles. This validation process verified the precision of this model, which was 99.99 per cent, and this also can be good contribution of this model among different clinical conditions, highlighting its practical aspects as shown in Figs. 13 and 14. Clinical evaluation involved three board-certified gastroenterologists. Each clinician independently reviewed the same image set with and without AI assistance. Mean interpretation time without AI was 12.4 ± 2.1 s per image, while AI-assisted interpretation required 9.3 ± 1.8 s per image, corresponding to a 25.0% reduction in review time. The model achieved impressive performance in different evaluation variables such as accuracy, precision, recall, and F1-score. This intensive clinical validation procedure was able to demonstrate that the model not only made accurate predictions, but reliable confidence could be placed in the results that could be incorporated easily into experimental processes. The model, with its high-performance level is a contributing helper to healthcare specialists as it can help them make data-supported decisions with confidence and increase diagnostic precision. Healthcare providers, by adopting this model in their practice, are now in a better position to make efficient and precise diagnoses, which in the long-term lead to better outcomes for patients. The convenience of mobile applications enables the direct application of the model in clinical practice, hence the change that has been brought to the healthcare systems, especially regarding the diagnosis of diseases of the gastrointestinal system. The human-centric evaluation demonstrated consistent performance gains across all assessment dimensions when clinicians used AI support. Diagnostic accuracy increased from 85.4% to 92.1%, and inter-observer agreement improved from κ = 0.71 to κ = 0.83, confirming higher interpretive consistency and reduced subjectivity. Workflow analysis showed that average review time decreased from 6.2 to 4.9 s per image, supported by real-time inference capability (> 30 fps), indicating improved efficiency for screening settings. Cognitive load ratings were reduced from 3.9 to 3.2, reflecting a lower mental search burden during inspection. Similarly, on the most challenging images (difficulty score > 3.5), accuracy increased from 68.7% to 78.4%, demonstrating that AI support is particularly beneficial under complex visual conditions. Finally, overall system usability yielded a score of 4.52/5 in Table 20, reflecting end-to-end interaction with the AI-assisted diagnostic pipeline, including prediction, visualization, and user interface components.Table 20Human-centric evaluation outcomes.Evaluation metricWithout AIWith AIDiagnostic accuracy (%)85.492.1Inter-reader agreement (κ)0.710.83Confidence score (1–5)3.614.27Reading time (s/img)6.24.9Cognitive load (1–5)3.93.2Difficult-case accuracy (%)68.778.4Usability score (1–5)–4.52 Table 20Human-centric evaluation outcomes.Evaluation metricWithout AIWith AIDiagnostic accuracy (%)85.492.1Inter-reader agreement (κ)0.710.83Confidence score (1–5)3.614.27Reading time (s/img)6.24.9Cognitive load (1–5)3.93.2Difficult-case accuracy (%)68.778.4Usability score (1–5)–4.52 Human-centric evaluation outcomes. Discussion and analysis The hybrid AI-based model, created in the work regarding the detection of GI diseases, provides a serious step towards computer-aided diagnostics. The model offers the highest classification scores across the eight GI diseases with the combination of the ViT with the HLNN, and further improvement of a generalization operation through Bagging and Stacking Ensemble techniques. The model has a high accuracy of 99.99 per cent in the held-out test set and is quite stable with a 99.96 per cent accuracy in tenfold cross-validation. This is supported by these measures with macro F1 of 0.9992 and Cohen 0.994, which demonstrate the model is competent to identify both prevalent and rare cases like ulcerative colitis, esophagitis, and dysplastic polyps with little performance loss over the runs. The accuracy of this level is especially interesting as the gastrointestinal endoscopic imagery is visually complicated and similar between classes. This is because the global attention mechanism of the ViT indicates it captures both the spatial and contextual features of images, which goes together with the localized sequences with the help of HLNN. Such hybridization makes our model different when compared to CNN-only or ViT-only baselines that recorded lower scores in accuracy of 95.4 and 96.8, respectively. In addition to metrics of numerical performance, the model was also evaluated in a clinical decision-support system by multimodal GI specialists through the review of 100 test images. There was an agreement of 96% with the model prediction, which shows great correspondence in the real environment. Also, Grad-CAM heatmaps demonstrated that the model has been focusing on the meaningful semantics of polyp edges, inflammation zones that promote transparency and interpretability in clinical use. It is important to contextualize performance within the characteristics of the Kvasir-v2 dataset. Several classes exhibit distinct visual and semantic features for example, dyed-resection-margins show characteristic blue-green chromoendoscopy staining, while normal-pylorus and normal-z-line present consistent anatomical landmarks enhancing inter-class separability and supporting strong baseline performance. In contrast, ulcerative-colitis and esophagitis involve subtle inflammatory gradations, variable erythema, and heterogeneous mucosal patterns, making them more challenging, while the polyps class shows morphological diversity (sessile to pedunculated). The proposed hybrid architecture is particularly effective in these fine-grained cases, where ViT global attention captures diffuse patterns and HLNN hierarchical refinement resolves boundary-level ambiguities beyond simpler models. Future work should evaluate datasets with more ambiguous class boundaries or severity-graded annotations to further test discriminative capacity. The observed performance improvement of the proposed framework cannot be attributed solely to a simple additive ensemble effect. Instead, the results suggest that the integration of transformer-based global feature extraction with nonlinear classification and attention-driven feature refinement leads to complementary learning. This is supported by the consistent performance gains across multiple evaluation metrics, indicating that the hybrid model captures both global structural patterns and fine-grained discriminative cues more effectively than individual components. In order to critically evaluate the relative advantage of our proposed hybrid model, we contrasted its output with a number of typical and well-established baselines as well as the state-of-the-art models. The ViT-only model that uses global attention mechanisms without local refinement accuracy showed 96.8%. EfficientNetB0, a potent CNN-based benchmark, achieved slightly lower results of 95.4%, which means that CNNs can be used to address medical images, but they might not provide transformer capabilities in the spatio-temporal long-range context. The non-ensemble one, the hybrid model that contains neither ensemble modules nor other synthesis options, making the only variant to be ViT and HLNN, gained a significant increase in accuracy of up to 98.3%, which shows the value of temporal and contextual refinement through a hierarchy of LSTM layers. The outcomes were further improved when the concept of ensemble strategies was implemented. Implementation of Bagging by itself increased the results to 99.1%; it indicated that the model variance reduction approach, bootstrap aggregation, increases the stability of prediction. Stacking in isolation provided a slightly improved performance of 99.2%, making use of the complementary patterns of the base models. Finally, the complete ensemble pipeline using both methods, Bagging and Stacking, produced the best accuracy rate of 99.5 with the macro F1-score of 99.9. Such controlled ablation outcomes support the hypothesis of the systematic improvement of each additional module and show how important ensemble learning is to increase robustness, accuracy, and the ability to generalize. The external zero-shot evaluation on GastroVision serves as the primary evidence against overfitting, as it evaluates the model on a completely unseen dataset without any adaptation. The model was trained on Kvasir-v2 and evaluated on GastroVision under a strict zero-shot setting, without fine-tuning or calibration. All images were resized to 224 × 224, with no test-time augmentation. Performance was evaluated at both class-wise and overall levels using accuracy, precision, recall, and macro-F1 score. This protocol indicates that results reflect true cross-dataset generalization rather than adaptation. Further, as compared to other top literature models, FLATer 99.7% and GestroNet 99.2%, our model is also quite competitive, especially when evaluated based on the fact that it is capable of making the multiclass prediction of eight different diseases, unlike a reduced number of classes that are commonly covered in earlier articles. Such a significant performance gain can be explained by our novel solution of the usage of the Kvasir-v2 dataset and a dynamic hybrid ensemble of the VIT and HLNN algorithms, as they allow improving the process of effective feature extraction and generalization. Compared to other models, what makes ours special is the fact that it can capture local and global features and thus is excellent when it comes to detecting a gastrointestinal disease. As confirmed clinically valid, it will offer clinicians diagnostic assistance whose predictability and accuracy are high, ensuring that there will be better results of treating patients and making effective decisions in actual healthcare situations. While individual components of the proposed system have been explored in prior literature, the methodological novelty of this work lies in principled integration and clinical validation. Specifically, this study introduces hierarchical recurrent modeling of ViT patch embeddings for spatial dependency learning, a dual-ensemble strategy combining bagging and stacking to address variance and error complementarity, and a deployment-ready explainability pipeline validated through clinician annotations and quantitative faithfulness metrics. Clinical claims are framed within the scope of retrospective multi-dataset evaluation, and prospective multi-center validation remains an important direction for future work. Robustness and generalization are key characteristics of clinical AI deployment, particularly in conditions where the image quality changes and unobserved data distribution. In order to test our hybrid model against robustness, we applied a range of aspects of both realistic and unrealistic perturbations that mimicked typical difficulties in medical endoscopy acquisition. In the input images, under the application of Gaussian noise, 0 to 0.05 Gaussian noise, 96.9% accuracy was sufficiently maintained. Minor losses were registered to 96.3% due to motion blur, which is the presence of handheld imaging devices or turbulence in the tissue. Likewise, upon variation of brightness of 15% above and below 0, the accuracy decreased by an insignificant margin to 96.7%. These outcomes confirm the performance of the model in suboptimal situations, thus making it suitable for the field. Also, we determined the significance of preprocessing by excluding Contrast Limited Adaptive Histogram Equalization (CLAHE) and gamma correction, which were initially utilized to improve the visibility of tissues and their contrast. In the absence of the preprocessing procedures, the performance was reduced by 3.4% (absolute) to 96.1%, confirming their usefulness in terms of facilitating feature extractions and the classification process overall. In order to analyze generalizability, we tested the model on a second dataset, Hyper-Kvasir, that contains more pathologies and image variability outside the original training distribution. A slight degradation of only 1.0% in accuracy and 0.2% in macro F1 shows that the model is not affected by the shift in the dataset, as it performed with 98.0 and 98.2, respectively, which is close to the original test results. Besides, a class-holdout generalization setting was also formed by omitting one of the classes during training. The capability of the model to generalize to unseen classes was demonstrated when simultaneous training was done on the seen category, and the model on test attained 91.3% accuracy on the unseen category, despite not being trained on the unseen category. Such high-class-holdout generalization performance can be explained by both the global feature extraction ability of the ViT and the HLNN contextual reasoning. All these assessments indicate that the model that is proposed is not just correct but also involves resilient, adaptive, and implementable basics in real-life procedures across varying clinical situations. The use of the model on mobile devices as a TFLite-powered application is a significant breakthrough in the accessibility of AI. Having a 45 MB model size and 65 ms/image on a Pixel 6A, the tool can make real-time predictions in resource-constrained clinical decision-support systems. The usability of the app was rated at 4.7/5 by clinicians, and the majority of them declared their increased confidence in diagnosis. Regardless of how well the model performs, some tradeoffs need to be addressed. To start with, Kvasir-v2 is a diverse but still a single-source dataset, which may raise concerns of cross-institutional generalizability. Second, the system has been trained and tested on quality RGB endoscopic images, and the Study does not report any results on noisy, low-resolution, or other modality data, such as infrared and ultrasound. Also, despite a high-class-holdout generalization performance demonstrated by the model, domain differences between camera devices, operators, or populations can alter accuracy when the model is deployed. These considerations will be overcome in future studies with multi-centre data, multimodal images, and stable domain adaptation strategies. To enable real-time deployment in resource-limited clinical environments, the proposed framework can be simplified and optimized using several strategies. First, knowledge distillation can be employed, where a smaller “student” model is trained to mimic the outputs of the full ensemble, retaining high accuracy while significantly reducing model size and inference latency. Additionally, post-training optimizations such as pruning and quantization can further compress the model. The optimized model can be exported using frameworks like TensorFlow Lite, allowing efficient execution on mid-range mobile or edge devices. With these optimizations, inference time on a typical device is estimated to be sub-70 ms per sample, demonstrating that the framework can be practically deployed in clinical decision-support systems without compromising reliability or performance. The human-centric evaluation demonstrates that the system offers clinically meaningful support beyond raw algorithmic metrics. Increases in diagnostic accuracy, inter-observer agreement, and difficult-case performance indicate reduced uncertainty and variability in interpretation. The observed reductions in review time and cognitive load highlight the system’s potential to enhance efficiency in high-volume endoscopy workflows, while strong usability and trust scores suggest favorable clinician acceptance. These outcomes reflect promising translational value; however, they represent controlled simulations rather than a live clinical trial. Future work will involve real-world, multi-centre evaluations to validate the decision-support system and patient-level impact. The model has a high practical diagnostic value, expressed in the fact that it was quite accurate and had a high interpretability in the classification of eight different types of gastrointestinal pathologies. It is highly appropriate when it comes to clinically integrating, performing better than previous literature, and at the same time being robust, explainable, and mobile-ready. Limitations and future work Although the proposed ViT–HLNN ensemble achieved near-perfect accuracy and strong robustness, several limitations remain. The high performance on the held-out Kvasir-v2 test set is likely influenced by controlled conditions, including consistent imaging and balanced class distribution, and may not fully reflect real-world clinical variability. The evaluation relies primarily on Kvasir-v2 and Hyper-Kvasir, which, while standard benchmarks, do not capture the full diversity of clinical data across populations, devices, and settings. Dataset-specific biases and homogeneous distributions may further favor high performance. Although zero-shot testing on GastroVision and robustness analyses were conducted, validation across more diverse multi-centre datasets is still required. Cross-dataset evaluation involved semantic label harmonization, which, while clinically justified, may introduce minor ambiguities and reduce strict comparability between classes. In addition, current validation reflects controlled or simulated conditions rather than real clinical workflows and therefore does not represent a clinical outcome study. The ensemble design also increases computational complexity, which may limit deployment in resource-constrained environments without further optimization. Moreover, only a ViT-based backbone was explored, and alternative efficient transformer variants were not systematically evaluated. Future work will focus on large-scale multi-centre validation, multimodal data integration, optimization for edge deployment, and extensions toward more comprehensive clinical tasks such as disease severity assessment and progression monitoring. Comparative analysis The proposed architecture, ViT + HLNN model with ensemble learning, not only matches but also improves the state-of-the-art results in GI disease classification as compared to existing studies shown in Table 21. It has huge potential in its large-scale adoption in clinical practice, particularly in the point-of-care environment and in telemedicine. The contribution of the study is a reproducible, scalable, and trustworthy AI model that would transform GI diagnostics worldwide with more readily available, more accurate, and faster tools in the hands of clinicians.Table 21Comparative analysis with existing benchmarks.SrTitleYearDatasetModelAccuracy (%)1142022Kvasir-v2MobileNet-V2 using transfer learning98.022162023Kvasir-v2Vision Transformer model based on hybrid shifted windows95.423172024Kvasir-v2EfficientNet-B098.0624192024Kvasir-v2Xception94.1255302024Kvasir-v2GIT- NET, a weighted average ensemble model956482025Kvasir-v1EfficientNetB3947Proposed Ensemble ModelproposedKvasir-v2Ensemble VIT with HLNN99.9 Table 21Comparative analysis with existing benchmarks.SrTitleYearDatasetModelAccuracy (%)1142022Kvasir-v2MobileNet-V2 using transfer learning98.022162023Kvasir-v2Vision Transformer model based on hybrid shifted windows95.423172024Kvasir-v2EfficientNet-B098.0624192024Kvasir-v2Xception94.1255302024Kvasir-v2GIT- NET, a weighted average ensemble model956482025Kvasir-v1EfficientNetB3947Proposed Ensemble ModelproposedKvasir-v2Ensemble VIT with HLNN99.9 Comparative analysis with existing benchmarks. Direct retraining of all competing methods under identical data splits was not feasible due to unavailable implementations or incompatible training protocols. Therefore, reported comparisons are based on published results. Differences in data splits and evaluation protocols are acknowledged as a limitation. Although the proposed ensemble framework demonstrates strong performance, it has several limitations. First, the competitive analysis is based on reported results from prior studies rather than retraining all competing models under identical data splits and preprocessing pipelines. Differences in dataset versions, training–validation–test splits, and optimization strategies may therefore influence absolute performance comparisons. Second, while a class-holdout evaluation was conducted to approximate external generalization, fully independent external datasets covering all disease categories were not available. Future studies will focus on multi-centre datasets to further assess generalizability. Third, Grad-CAM localization evaluation relied on clinician-annotated lesion regions rather than pixel-precise segmentation masks, and the reported overlap metrics are intended to assess explanation consistency rather than segmentation accuracy. The present study focuses on a ViT-based backbone within the ensemble framework. Although alternative lightweight transformer architectures may offer computational advantages, a systematic backbone comparison was beyond the scope of this work and will be explored in future research. Conclusion In this study, we proposed and evaluated a novel hybrid deep learning model for the classification of gastrointestinal (GI) diseases from endoscopic images. By combining a ViT backbone with HLNN and refining results through bagging and stacking ensemble strategies, our approach achieved state-of-the-art performance on the Kvasir-v2 dataset. The architecture successfully learns both global contextual entanglement and local sequential characteristics, enabling highly discriminative representations of eight GI pathologies. The model achieved a test set accuracy of up to 99.99%, cross-validation accuracy of 99.96%, macro F1-scores of 0.9992, and Cohen’s kappa of 0.994, underscoring both performance and reliability. Through stepwise experimentation of individual ViT and HLNN components and ensemble learning modules, we demonstrated the synergistic value of this hybrid design. Robustness analyses under noise, blur, and brightness perturbations, as well as zero-shot testing on GastroVision and class-holdout evaluation on the held-out Polyps class, confirmed the generalizability of the model to clinical decision-support systems. Perturbation-resilience testing combined with explainability validation further supported its trustworthiness in noisy or ambiguous contexts. Grad-CAM visualizations and clinician review confirmed the clinical interpretability of the model, with 96% agreement between specialists and model predictions. Quantitative explainability measures also demonstrated robustness, with a Grad-CAM IoU of 0.78, localization accuracy of 85%, faithfulness score of 0.87, and sanity check correlation r = 0.91. The human evaluation confirms that the model offers tangible diagnostic and workflow benefits, with accuracy rising from 85.4 to 92.1% and review time reduced by 20.9%, supporting its potential for real deployment pending future multi-centre validation. Practical applicability was demonstrated through deployment as a lightweight 45 MB TFLite mobile app, with inference latencies below 70 ms on mid-range smartphones, enabling point-of-care diagnostics in both advanced hospitals and resource-constrained environments. Usability testing yielded an average score of 4.8/5, with clinicians predicting a 25% reduction in diagnostic interpretation time using the system. Overall, this study sets a new standard in AI-assisted GI disease detection by delivering a high-performance, scalable, and interpretable framework that enhances clinical decision-making. Beyond technical innovation, the practicality of mobile deployment and direct clinician engagement highlight its readiness for real-world use. While some limitations and future improvements remain, the proposed ViT–HLNN ensemble framework represents a promising step toward global adoption of AI in gastroenterology. Future work will focus on external validation, clinical expert assessment, and deployment considerations to bridge the gap between experimental performance and real-world applicability.