DOI: 10.5061/dryad.tx95x6b99
title: PestReKNet-X: Integrating Explainable AI to enhance pest disease detection and combat crop senescence

description: Crop pests and diseases remain a significant obstacle to sustainable
 agriculture, necessitating innovative and eco-friendly detection
 solutions. This study introduces PestReKNet-X, a state-of-the-art
 explainable deep learning framework that combines a ResNet18 backbone with
 a custom Kolmogorov Arnold Network (KAN) Linear layer (KANLinear), which
 captures complex non-linear patterns, surpassing the limits of traditional
 fully connected layers. The framework is evaluated on the benchmark CCMT
 crop pest and disease dataset, containing 102,097 images across 22
 classes. To tackle class imbalance, the Mean Intersection over Union (IoU)
 metric is used alongside accuracy for robust performance evaluation. The
 model achieves a testing accuracy of 95.09% and a Mean IoU of 0.9133,
 reflecting strong generalization across diverse categories. Moreover,
 PestReKNet-X surpasses advanced architectures like Swin Transformer and
 MobileNetV3Large concerning performance and reliability. Monte Carlo
 Dropout for uncertainty estimation and model calibration is used to
 achieve reliable predictions with well-calibrated probabilities. A strong
 focus on explainable AI (XAI) ensures transparency and interpretability,
 addressing gaps in recent studies. The inclusion of Grad-CAM and LIME
 provides intuitive visualizations and localized insights, enhancing
 understanding of predictions. With high accuracy, efficiency, and
 interpretability, PestReKNet-X provides a scalable solution for precise
 pest and disease monitoring.
description: # PestReKNet-X: Integrating Explainable AI to enhance pest disease
 detection and combat crop senescence
 [![Python](https://img.shields.io/badge/Python-3.8+-blue.svg)](https://python.org) [![PyTorch](https://img.shields.io/badge/PyTorch-2.0+-red.svg)](https://pytorch.org) [![Accuracy](https://img.shields.io/badge/Test%20Accuracy-95.09%25-brightgreen.svg)](#key-results) [![DOI](https://zenodo.org/badge/DOI/10.5281/zenodo.16829681.svg)](https://doi.org/10.5281/zenodo.16829681) ## 📌 Overview PestReKNet-X is a novel deep learning framework for **crop pest and disease classification** that combines a **ResNet18 backbone** with a **custom Kolmogorov–Arnold Network (KAN) layer** for enhanced feature learning. The framework integrates **Explainable AI (XAI)** techniques — **Grad-CAM** and **LIME** — alongside **Monte Carlo Dropout** for uncertainty estimation and **model calibration**. ### Key Features * 🎯 **High Accuracy**: Achieves 95.09% testing accuracy * 🔍 **Explainable AI**: Built-in Grad-CAM and LIME visualizations * 📊 **Uncertainty Quantification**: Monte Carlo Dropout implementation * ⚖️ **Model Calibration**: Reliability diagrams and ECE metrics * 🚀 **Easy Deployment**: Pre-trained weights and inference scripts --- ## 📊 Key Results | Metric | Value | | ----------------------- | ---------------------- | | **Testing Accuracy** | 95.09% | | **Training Accuracy** | 99.13% (Final epoch) | | **Validation Accuracy** | 93.93% (Best achieved) | | **Mean IoU** | 0.9133 | | **Precision** | 94.87% | | **Recall** | 95.09% | | **F1-Score** | 94.98% | | **Number of Classes** | 22 | | **Early Stopping** | Epoch 39/40 | --- ## 📂 Repository Structure The following code notebooks are available in this repository: * `Resnet18_with_KAN_9393_Training.ipynb` * `Resnet18_with_KAN_9393_Testing.ipynb` * `Classwise_Accuracy_Test.ipynb` * `t_SNE_Visualization.ipynb` * `gradcam-cropest.ipynb` * `limeonresnet18kan.ipynb` --- ## ⚙️ Installation ### Prerequisites * Python 3.8+ * CUDA-compatible GPU (recommended) * 8GB+ RAM ### Setup Instructions ```bash # Create and activate a virtual environment python3 -m venv pestreknet_env source pestreknet_env/bin/activate # Linux/Mac # pestreknet_env\Scripts\activate # Windows # Install dependencies pip install -r requirements.txt # Verify installation python -c "import torch; print(f'PyTorch: {torch.__version__}')" ``` --- ## 🚀 Quick Start ### 1. Model Training ```bash # Open and run the training notebook (achieves 93.93% validation accuracy) jupyter notebook notebooks/resnet18withkan9393.ipynb ``` ### 2. Model Testing ```bash # Evaluate the trained model jupyter notebook notebooks/Resnet18_with_KAN_9393_Testing.ipynb ``` ### 3. Class-wise Performance Analysis ```bash # Analyze performance per class jupyter notebook notebooks/Classwise_Accuracy_Test.ipynb ``` ### 4. Feature Visualization ```bash # Visualize learned features with t-SNE jupyter notebook notebooks/t_SNE_Visualization.ipynb ``` ### 5. Inference Example ```python import torch import torchvision.transforms as transforms from torchvision import models from KANLinear import KANLinear # Import your KAN implementation # Load pre-trained model model = models.resnet18(pretrained=False) model.fc = KANLinear(in_features=512, out_features=22) model.load_state_dict(torch.load('models/PestReKNet-X_Model.pth')) model.eval() # Define preprocessing transforms val_transforms = transforms.Compose([ transforms.Resize((224, 224)), transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]), ]) # Predict on a new image def predict_image(image_path, model, transforms, device='cuda'): from PIL import Image image = Image.open(image_path).convert('RGB') image_tensor = transforms(image).unsqueeze(0).to(device) with torch.no_grad(): outputs = model(image_tensor) probabilities = torch.nn.functional.softmax(outputs, dim=1) confidence, predicted = torch.max(probabilities, 1) return predicted.item(), confidence.item() # Usage image_path = 'path/to/your/image.jpg' class_idx, confidence = predict_image(image_path, model, val_transforms) print(f"Predicted class: {class_idx} (confidence: {confidence:.4f})") ``` --- ## 📈 Dataset Information ### CCMT Dataset We used the **CCMT: Dataset for crop pest and disease detection** 📥 **Download**: [Mendeley Data Repository](https://data.mendeley.com/datasets/bwh3zbpkpv/1) ### Dataset Statistics | Split | Images | Percentage | | -------------- | ------- | --------------------------- | | **Training** | 69,333 | 68.0% | | **Validation** | 7,697 | 7.5% | | **Testing** | 24,981 | 24.5% | | **Total** | 102,011 | 100% | | **Classes** | 22 | Pest and Disease Categories | ### Supported Classes The model can classify the following categories: 🐛 Pest Detection Cashew: Leaf Miner Cassava: Green Mite Maize: Fall Armyworm, Grasshopper, Leaf Beetle 🦠 Disease Classification Cashew: Anthracnose, Gummosis, Red Rust Cassava: Bacterial Blight, Brown Spot, Mosaic Maize: Leaf Blight, Leaf Spot, Streak Virus Tomato: Leaf Blight, Leaf Curl, Septoria Leaf Spot, Verticillium Wilt 🌱 Healthy Crops Cashew: Healthy Cassava: Healthy Maize: Healthy Tomato: Healthy 🍂 Senescence Detection Biotic stress-induced senescence signs across crops, particularly visible in aging or diseased leaves (e.g., Yellowing in Late-Stage Blight or Wilt). --- ## 🧠 Model Architecture ### Architecture Components * **Backbone**: ResNet18 (pre-trained on ImageNet) * **Custom Final Layer**: KANLinear layer replacing the standard fully connected layer * `in_features`: 512 (from ResNet18 feature extractor) * `out_features`: 22 (number of pest/disease classes) * `grid_size`: 10 * `spline_order`: 4 * `base_activation`: ReLU * **Mixed Precision Training**: GradScaler for efficient GPU utilization * **Regularization**: Built-in KAN regularization with activation and entropy terms * **Output**: Softmax classification for 22 classes ### Key Innovations 1. **KAN Integration**: Kolmogorov–Arnold Networks for enhanced feature learning 2. **Explainable AI**: Grad-CAM and LIME for model interpretability 3. **Uncertainty Quantification**: Monte Carlo Dropout for confidence estimation 4. **Model Calibration**: Temperature scaling and reliability diagrams ### Training Configuration ```python # Training hyperparameters (from actual implementation) BATCH_SIZE = 256 IMG_HEIGHT, IMG_WIDTH = 224, 224 EPOCHS = 40 # Early stopping at epoch 39 LEARNING_RATE = 1e-4 OPTIMIZER = 'Adam' NUM_CLASSES = 22 DEVICE = 'cuda' if available else 'cpu' PATIENCE = 7 # Early stopping patience # Data Augmentation TRAIN_TRANSFORMS = [ 'Resize(224, 224)', 'RandomHorizontalFlip()', 'RandomRotation(20)', 'ToTensor()', 'Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])' ] # Mixed Precision Training with GradScaler # KAN Layer Configuration: grid_size=10, spline_order=4 ``` --- ## 🔍 Explainable AI Features ### Grad-CAM Visualizations ```python from src.explainability import generate_gradcam # Generate Grad-CAM heatmap heatmap = generate_gradcam(model, image, target_class) ``` ### LIME Explanations ```python from src.explainability import generate_lime_explanation # Generate LIME explanation explanation = generate_lime_explanation(model, image) explanation.show_in_notebook() ``` ---