PestReKNet-X: Integrating Explainable AI to enhance pest disease detection and combat crop senescence
Data files
Oct 07, 2025 version files 2.57 MB
-
Classwise_Accuracy_Test.ipynb
154.23 KB
-
gradcam-cropest.ipynb
631.69 KB
-
limeonresnet18kan.ipynb
291.28 KB
-
README.md
7.84 KB
-
Resnet18_with_KAN_9393_Testing.ipynb
355.31 KB
-
Resnet18_with_KAN_9393_Training.ipynb
24.52 KB
-
t_SNE_Visualization.ipynb
1.10 MB
Abstract
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.
๐ 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.ipynbResnet18_with_KAN_9393_Testing.ipynbClasswise_Accuracy_Test.ipynbt_SNE_Visualization.ipynbgradcam-cropest.ipynblimeonresnet18kan.ipynb
โ๏ธ Installation
Prerequisites
- Python 3.8+
- CUDA-compatible GPU (recommended)
- 8GB+ RAM
Setup Instructions
# 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
# Open and run the training notebook (achieves 93.93% validation accuracy)
jupyter notebook notebooks/resnet18withkan9393.ipynb
2. Model Testing
# Evaluate the trained model
jupyter notebook notebooks/Resnet18_with_KAN_9393_Testing.ipynb
3. Class-wise Performance Analysis
# Analyze performance per class
jupyter notebook notebooks/Classwise_Accuracy_Test.ipynb
4. Feature Visualization
# Visualize learned features with t-SNE
jupyter notebook notebooks/t_SNE_Visualization.ipynb
5. Inference Example
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
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: 10spline_order: 4base_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
- KAN Integration: KolmogorovโArnold Networks for enhanced feature learning
- Explainable AI: Grad-CAM and LIME for model interpretability
- Uncertainty Quantification: Monte Carlo Dropout for confidence estimation
- Model Calibration: Temperature scaling and reliability diagrams
Training Configuration
# 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
from src.explainability import generate_gradcam
# Generate Grad-CAM heatmap
heatmap = generate_gradcam(model, image, target_class)
LIME Explanations
from src.explainability import generate_lime_explanation
# Generate LIME explanation
explanation = generate_lime_explanation(model, image)
explanation.show_in_notebook()
