Update app.py
Browse files
app.py
CHANGED
|
@@ -1,17 +1,16 @@
|
|
| 1 |
"""
|
| 2 |
-
Plant Disease Classification API with OOD Detection
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
Deploy with: uvicorn plant_disease_api:app --host 0.0.0.0 --port 8000
|
| 6 |
"""
|
| 7 |
|
| 8 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 9 |
from fastapi.middleware.cors import CORSMiddleware
|
| 10 |
from fastapi.responses import JSONResponse
|
| 11 |
from pydantic import BaseModel
|
| 12 |
-
from typing import List, Dict, Optional
|
| 13 |
import torch
|
| 14 |
import torch.nn as nn
|
|
|
|
| 15 |
import timm
|
| 16 |
import numpy as np
|
| 17 |
from PIL import Image
|
|
@@ -19,22 +18,31 @@ import io
|
|
| 19 |
import albumentations as A
|
| 20 |
from albumentations.pytorch import ToTensorV2
|
| 21 |
import logging
|
|
|
|
|
|
|
| 22 |
|
| 23 |
# Setup logging
|
| 24 |
logging.basicConfig(level=logging.INFO)
|
| 25 |
logger = logging.getLogger(__name__)
|
| 26 |
|
| 27 |
# ============================================================================
|
| 28 |
-
# Configuration
|
| 29 |
# ============================================================================
|
| 30 |
|
| 31 |
class Config:
|
| 32 |
-
MODEL_PATH = "best_model_final.pth"
|
|
|
|
| 33 |
IMG_SIZE = 224
|
| 34 |
-
|
| 35 |
-
CONFIDENCE_THRESHOLD = 0.7
|
|
|
|
|
|
|
| 36 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 37 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
# 38 Plant disease classes
|
| 39 |
CLASS_NAMES = [
|
| 40 |
'Apple___Apple_scab', 'Apple___Black_rot', 'Apple___Cedar_apple_rust',
|
|
@@ -57,79 +65,171 @@ class Config:
|
|
| 57 |
config = Config()
|
| 58 |
|
| 59 |
# ============================================================================
|
| 60 |
-
# Model Definition
|
| 61 |
# ============================================================================
|
| 62 |
|
| 63 |
class PlantDiseaseModel(nn.Module):
|
| 64 |
-
"""EfficientNet-B0 with custom classifier"""
|
| 65 |
def __init__(self, num_classes, dropout=0.4):
|
| 66 |
super(PlantDiseaseModel, self).__init__()
|
| 67 |
-
|
|
|
|
| 68 |
num_features = self.backbone.classifier.in_features
|
|
|
|
|
|
|
| 69 |
self.backbone.classifier = nn.Identity()
|
| 70 |
|
|
|
|
|
|
|
|
|
|
| 71 |
self.classifier = nn.Sequential(
|
| 72 |
nn.Dropout(dropout),
|
| 73 |
nn.Linear(num_features, 512),
|
| 74 |
nn.ReLU(inplace=True),
|
|
|
|
| 75 |
nn.Dropout(dropout * 0.5),
|
| 76 |
nn.Linear(512, num_classes)
|
| 77 |
)
|
| 78 |
|
| 79 |
-
def forward(self, x):
|
| 80 |
features = self.backbone(x)
|
| 81 |
logits = self.classifier(features)
|
|
|
|
|
|
|
|
|
|
| 82 |
return logits
|
| 83 |
|
| 84 |
# ============================================================================
|
| 85 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
# ============================================================================
|
| 87 |
|
| 88 |
-
def get_transform():
|
| 89 |
-
"""Get image preprocessing transform"""
|
| 90 |
-
|
| 91 |
-
A.
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 95 |
|
| 96 |
-
def preprocess_image(image_bytes: bytes) -> torch.Tensor:
|
| 97 |
-
"""Preprocess uploaded image"""
|
| 98 |
try:
|
| 99 |
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
image_np = np.array(image)
|
| 101 |
-
transform = get_transform()
|
| 102 |
augmented = transform(image=image_np)
|
| 103 |
image_tensor = augmented['image'].unsqueeze(0)
|
| 104 |
return image_tensor
|
| 105 |
except Exception as e:
|
| 106 |
logger.error(f"Error preprocessing image: {e}")
|
| 107 |
-
raise HTTPException(status_code=400, detail="Invalid image format")
|
| 108 |
|
| 109 |
# ============================================================================
|
| 110 |
-
# Model Loading
|
| 111 |
# ============================================================================
|
| 112 |
|
| 113 |
def load_model():
|
| 114 |
-
"""Load trained model"""
|
| 115 |
try:
|
| 116 |
logger.info(f"Loading model from {config.MODEL_PATH}")
|
| 117 |
model = PlantDiseaseModel(num_classes=len(config.CLASS_NAMES), dropout=0.4)
|
| 118 |
|
| 119 |
-
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
model.to(config.DEVICE)
|
| 122 |
model.eval()
|
| 123 |
|
|
|
|
|
|
|
|
|
|
| 124 |
logger.info(f"✅ Model loaded successfully on {config.DEVICE}")
|
| 125 |
-
|
| 126 |
-
|
|
|
|
|
|
|
|
|
|
| 127 |
except Exception as e:
|
| 128 |
logger.error(f"Failed to load model: {e}")
|
| 129 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 130 |
|
| 131 |
-
# Load model
|
| 132 |
-
model = load_model()
|
| 133 |
|
| 134 |
# ============================================================================
|
| 135 |
# Response Models
|
|
@@ -143,14 +243,16 @@ class PredictionResult(BaseModel):
|
|
| 143 |
plant: str
|
| 144 |
disease: str
|
| 145 |
is_healthy: bool
|
| 146 |
-
|
| 147 |
recommendations: Optional[str] = None
|
|
|
|
| 148 |
|
| 149 |
class OODResult(BaseModel):
|
| 150 |
"""Response model for OOD detection"""
|
| 151 |
status: str
|
| 152 |
message: str
|
| 153 |
confidence: float
|
|
|
|
| 154 |
top_guess: Optional[str] = None
|
| 155 |
note: str
|
| 156 |
|
|
@@ -160,48 +262,58 @@ class HealthResponse(BaseModel):
|
|
| 160 |
model_loaded: bool
|
| 161 |
device: str
|
| 162 |
classes: int
|
|
|
|
|
|
|
| 163 |
|
| 164 |
# ============================================================================
|
| 165 |
-
# Prediction Logic
|
| 166 |
# ============================================================================
|
| 167 |
|
| 168 |
@torch.no_grad()
|
| 169 |
def predict_image(image_tensor: torch.Tensor) -> Dict:
|
| 170 |
"""
|
| 171 |
-
Make prediction with OOD detection
|
| 172 |
-
|
| 173 |
-
Returns JSON response compatible with mobile app
|
| 174 |
"""
|
| 175 |
image_tensor = image_tensor.to(config.DEVICE)
|
| 176 |
|
| 177 |
-
# Get model prediction
|
| 178 |
-
logits = model(image_tensor)
|
| 179 |
|
| 180 |
-
#
|
| 181 |
-
|
| 182 |
-
probs = torch.softmax(scaled_logits, dim=1)
|
| 183 |
confidence, pred_idx = torch.max(probs, dim=1)
|
| 184 |
-
|
| 185 |
confidence = confidence.item()
|
| 186 |
pred_idx = pred_idx.item()
|
| 187 |
|
| 188 |
-
# Get top-5
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
#
|
| 194 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 195 |
return {
|
| 196 |
"status": "OOD",
|
| 197 |
-
"message": "⚠️
|
| 198 |
"confidence": round(confidence, 4),
|
| 199 |
-
"
|
| 200 |
-
"
|
|
|
|
| 201 |
}
|
| 202 |
|
| 203 |
-
# Parse prediction
|
| 204 |
-
predicted_class = config.CLASS_NAMES[pred_idx]
|
| 205 |
parts = predicted_class.split('___')
|
| 206 |
plant = parts[0].replace('_', ' ').strip()
|
| 207 |
disease = parts[1].replace('_', ' ').strip() if len(parts) > 1 else "Unknown"
|
|
@@ -210,48 +322,79 @@ def predict_image(image_tensor: torch.Tensor) -> Dict:
|
|
| 210 |
# Generate recommendations
|
| 211 |
recommendations = get_recommendations(plant, disease, is_healthy)
|
| 212 |
|
| 213 |
-
# Format top
|
| 214 |
-
|
| 215 |
{
|
| 216 |
"class": config.CLASS_NAMES[idx],
|
| 217 |
"confidence": round(float(prob), 4)
|
| 218 |
}
|
| 219 |
-
for idx, prob in zip(
|
| 220 |
]
|
| 221 |
|
| 222 |
-
|
|
|
|
| 223 |
"status": "OK",
|
| 224 |
"prediction": predicted_class,
|
| 225 |
"confidence": round(confidence, 4),
|
| 226 |
"plant": plant,
|
| 227 |
"disease": disease,
|
| 228 |
"is_healthy": is_healthy,
|
| 229 |
-
"
|
| 230 |
"recommendations": recommendations
|
| 231 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
def get_recommendations(plant: str, disease: str, is_healthy: bool) -> str:
|
| 234 |
-
"""Generate treatment recommendations
|
| 235 |
if is_healthy:
|
| 236 |
return f"✅ Your {plant} plant appears healthy! Continue regular care and monitoring."
|
| 237 |
|
| 238 |
-
#
|
| 239 |
recommendations_db = {
|
| 240 |
-
|
| 241 |
-
"
|
| 242 |
-
"
|
| 243 |
-
"
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 247 |
}
|
| 248 |
|
| 249 |
-
# Try
|
| 250 |
for key, rec in recommendations_db.items():
|
| 251 |
-
if key.lower()
|
| 252 |
-
return f"⚠️ {disease} detected.
|
| 253 |
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
|
| 256 |
# ============================================================================
|
| 257 |
# FastAPI Application
|
|
@@ -259,31 +402,33 @@ def get_recommendations(plant: str, disease: str, is_healthy: bool) -> str:
|
|
| 259 |
|
| 260 |
app = FastAPI(
|
| 261 |
title="Plant Disease Detection API",
|
| 262 |
-
description="AI-powered plant disease classification with OOD detection",
|
| 263 |
-
version="
|
| 264 |
)
|
| 265 |
|
| 266 |
-
# Enable CORS
|
| 267 |
app.add_middleware(
|
| 268 |
CORSMiddleware,
|
| 269 |
-
allow_origins=["*"],
|
| 270 |
allow_credentials=True,
|
| 271 |
allow_methods=["*"],
|
| 272 |
allow_headers=["*"],
|
| 273 |
)
|
| 274 |
|
| 275 |
# ============================================================================
|
| 276 |
-
# API Endpoints
|
| 277 |
# ============================================================================
|
| 278 |
|
| 279 |
@app.get("/", response_model=HealthResponse)
|
| 280 |
async def root():
|
| 281 |
"""Health check endpoint"""
|
| 282 |
return {
|
| 283 |
-
"status": "✅ API is running",
|
| 284 |
"model_loaded": model is not None,
|
| 285 |
"device": config.DEVICE,
|
| 286 |
-
"classes": len(config.CLASS_NAMES)
|
|
|
|
|
|
|
| 287 |
}
|
| 288 |
|
| 289 |
@app.get("/health")
|
|
@@ -291,75 +436,65 @@ async def health_check():
|
|
| 291 |
"""Detailed health check"""
|
| 292 |
return {
|
| 293 |
"status": "healthy",
|
| 294 |
-
"model": "EfficientNet-B0",
|
| 295 |
"device": config.DEVICE,
|
| 296 |
"classes": len(config.CLASS_NAMES),
|
| 297 |
-
"
|
| 298 |
-
"
|
| 299 |
-
"
|
|
|
|
| 300 |
}
|
| 301 |
|
| 302 |
@app.post("/predict")
|
| 303 |
async def predict(file: UploadFile = File(...)):
|
| 304 |
"""
|
| 305 |
-
Predict plant disease
|
| 306 |
-
|
| 307 |
-
Returns:
|
| 308 |
-
- If normal plant disease: prediction with confidence and recommendations
|
| 309 |
-
- If OOD (unknown object): warning message with low confidence
|
| 310 |
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
"plant": "Tomato",
|
| 317 |
-
"disease": "Early blight",
|
| 318 |
-
"is_healthy": false,
|
| 319 |
-
"top5_predictions": [...],
|
| 320 |
-
"recommendations": "Remove affected leaves, apply fungicide..."
|
| 321 |
-
}
|
| 322 |
-
|
| 323 |
-
Example Response (OOD):
|
| 324 |
-
{
|
| 325 |
-
"status": "OOD",
|
| 326 |
-
"message": "⚠️ Unknown Object Detected",
|
| 327 |
-
"confidence": 0.4521,
|
| 328 |
-
"top_guess": "Tomato___healthy",
|
| 329 |
-
"note": "This doesn't appear to be a plant disease image..."
|
| 330 |
-
}
|
| 331 |
"""
|
| 332 |
try:
|
| 333 |
-
# Validate file
|
| 334 |
if not file.content_type.startswith('image/'):
|
| 335 |
-
raise HTTPException(status_code=400, detail="File must be an image")
|
| 336 |
|
| 337 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 338 |
image_bytes = await file.read()
|
| 339 |
image_tensor = preprocess_image(image_bytes)
|
| 340 |
|
| 341 |
# Make prediction
|
| 342 |
result = predict_image(image_tensor)
|
| 343 |
|
| 344 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 345 |
|
| 346 |
return JSONResponse(content=result)
|
| 347 |
|
| 348 |
except HTTPException as e:
|
| 349 |
raise e
|
| 350 |
except Exception as e:
|
| 351 |
-
logger.error(f"Prediction error: {e}")
|
| 352 |
raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}")
|
| 353 |
|
| 354 |
@app.post("/predict/batch")
|
| 355 |
async def predict_batch(files: List[UploadFile] = File(...)):
|
| 356 |
-
"""
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
Returns array of predictions
|
| 360 |
-
"""
|
| 361 |
-
if len(files) > 10:
|
| 362 |
-
raise HTTPException(status_code=400, detail="Maximum 10 images per batch")
|
| 363 |
|
| 364 |
results = []
|
| 365 |
for file in files:
|
|
@@ -373,22 +508,39 @@ async def predict_batch(files: List[UploadFile] = File(...)):
|
|
| 373 |
results.append({
|
| 374 |
"filename": file.filename,
|
| 375 |
"status": "ERROR",
|
| 376 |
-
"message": str(e)
|
| 377 |
})
|
| 378 |
|
| 379 |
return JSONResponse(content={"predictions": results})
|
| 380 |
|
| 381 |
-
@app.get("/
|
| 382 |
-
async def
|
| 383 |
-
"""
|
| 384 |
return {
|
| 385 |
-
"
|
| 386 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 387 |
}
|
| 388 |
|
| 389 |
# ============================================================================
|
| 390 |
-
# Run
|
| 391 |
# ============================================================================
|
|
|
|
| 392 |
if __name__ == "__main__":
|
| 393 |
import uvicorn
|
|
|
|
| 394 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
"""
|
| 2 |
+
Plant Disease Classification API with Robust OOD Detection
|
| 3 |
+
Fixed confidence and OOD issues
|
|
|
|
|
|
|
| 4 |
"""
|
| 5 |
|
| 6 |
from fastapi import FastAPI, File, UploadFile, HTTPException
|
| 7 |
from fastapi.middleware.cors import CORSMiddleware
|
| 8 |
from fastapi.responses import JSONResponse
|
| 9 |
from pydantic import BaseModel
|
| 10 |
+
from typing import List, Dict, Optional, Tuple
|
| 11 |
import torch
|
| 12 |
import torch.nn as nn
|
| 13 |
+
import torch.nn.functional as F
|
| 14 |
import timm
|
| 15 |
import numpy as np
|
| 16 |
from PIL import Image
|
|
|
|
| 18 |
import albumentations as A
|
| 19 |
from albumentations.pytorch import ToTensorV2
|
| 20 |
import logging
|
| 21 |
+
from scipy.stats import norm
|
| 22 |
+
import pickle
|
| 23 |
|
| 24 |
# Setup logging
|
| 25 |
logging.basicConfig(level=logging.INFO)
|
| 26 |
logger = logging.getLogger(__name__)
|
| 27 |
|
| 28 |
# ============================================================================
|
| 29 |
+
# Configuration - UPDATED VALUES
|
| 30 |
# ============================================================================
|
| 31 |
|
| 32 |
class Config:
|
| 33 |
+
MODEL_PATH = "best_model_final.pth"
|
| 34 |
+
STATS_PATH = "class_statistics.pkl" # For Mahalanobis distance
|
| 35 |
IMG_SIZE = 224
|
| 36 |
+
# LOWER threshold - for 38 classes, even good predictions might have 40-60% confidence
|
| 37 |
+
CONFIDENCE_THRESHOLD = 0.3 # Reduced from 0.7
|
| 38 |
+
OOD_THRESHOLD = 0.15 # Separate threshold for OOD
|
| 39 |
+
ENTROPY_THRESHOLD = 1.5 # For OOD detection via entropy
|
| 40 |
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 41 |
|
| 42 |
+
# Feature space parameters
|
| 43 |
+
USE_MAHALANOBIS = False # Set to True if you compute class statistics
|
| 44 |
+
USE_ENSEMBLE = False # For better uncertainty estimation
|
| 45 |
+
|
| 46 |
# 38 Plant disease classes
|
| 47 |
CLASS_NAMES = [
|
| 48 |
'Apple___Apple_scab', 'Apple___Black_rot', 'Apple___Cedar_apple_rust',
|
|
|
|
| 65 |
config = Config()
|
| 66 |
|
| 67 |
# ============================================================================
|
| 68 |
+
# Improved Model Definition
|
| 69 |
# ============================================================================
|
| 70 |
|
| 71 |
class PlantDiseaseModel(nn.Module):
|
| 72 |
+
"""EfficientNet-B0 with custom classifier and feature extraction"""
|
| 73 |
def __init__(self, num_classes, dropout=0.4):
|
| 74 |
super(PlantDiseaseModel, self).__init__()
|
| 75 |
+
# IMPORTANT: Load pretrained weights for better feature extraction
|
| 76 |
+
self.backbone = timm.create_model('efficientnet_b0', pretrained=True) # Changed to True
|
| 77 |
num_features = self.backbone.classifier.in_features
|
| 78 |
+
|
| 79 |
+
# Keep features for OOD detection
|
| 80 |
self.backbone.classifier = nn.Identity()
|
| 81 |
|
| 82 |
+
# Store feature dimension for Mahalanobis distance
|
| 83 |
+
self.feature_dim = num_features
|
| 84 |
+
|
| 85 |
self.classifier = nn.Sequential(
|
| 86 |
nn.Dropout(dropout),
|
| 87 |
nn.Linear(num_features, 512),
|
| 88 |
nn.ReLU(inplace=True),
|
| 89 |
+
nn.BatchNorm1d(512),
|
| 90 |
nn.Dropout(dropout * 0.5),
|
| 91 |
nn.Linear(512, num_classes)
|
| 92 |
)
|
| 93 |
|
| 94 |
+
def forward(self, x, return_features=False):
|
| 95 |
features = self.backbone(x)
|
| 96 |
logits = self.classifier(features)
|
| 97 |
+
|
| 98 |
+
if return_features:
|
| 99 |
+
return logits, features
|
| 100 |
return logits
|
| 101 |
|
| 102 |
# ============================================================================
|
| 103 |
+
# OOD Detection Methods
|
| 104 |
+
# ============================================================================
|
| 105 |
+
|
| 106 |
+
class OODDetector:
|
| 107 |
+
"""Multiple methods for robust OOD detection"""
|
| 108 |
+
|
| 109 |
+
def __init__(self):
|
| 110 |
+
self.methods = ['confidence', 'entropy', 'energy']
|
| 111 |
+
|
| 112 |
+
@staticmethod
|
| 113 |
+
def compute_entropy(probs: torch.Tensor) -> float:
|
| 114 |
+
"""Compute entropy of probability distribution"""
|
| 115 |
+
return -torch.sum(probs * torch.log(probs + 1e-10)).item()
|
| 116 |
+
|
| 117 |
+
@staticmethod
|
| 118 |
+
def compute_energy_score(logits: torch.Tensor, temperature: float = 1.0) -> float:
|
| 119 |
+
"""Energy-based OOD detection"""
|
| 120 |
+
return -temperature * torch.logsumexp(logits / temperature, dim=1).item()
|
| 121 |
+
|
| 122 |
+
@staticmethod
|
| 123 |
+
def compute_max_softmax(probs: torch.Tensor) -> float:
|
| 124 |
+
"""Maximum softmax probability"""
|
| 125 |
+
return torch.max(probs).item()
|
| 126 |
+
|
| 127 |
+
def detect_ood(self, logits: torch.Tensor, method: str = 'ensemble') -> Tuple[bool, Dict]:
|
| 128 |
+
"""
|
| 129 |
+
Detect OOD using multiple methods
|
| 130 |
+
Returns: (is_ood, scores_dict)
|
| 131 |
+
"""
|
| 132 |
+
probs = F.softmax(logits, dim=1)
|
| 133 |
+
|
| 134 |
+
scores = {
|
| 135 |
+
'confidence': self.compute_max_softmax(probs),
|
| 136 |
+
'entropy': self.compute_entropy(probs[0]),
|
| 137 |
+
'energy': self.compute_energy_score(logits)
|
| 138 |
+
}
|
| 139 |
+
|
| 140 |
+
# Combined decision rule
|
| 141 |
+
is_ood = (
|
| 142 |
+
scores['confidence'] < config.CONFIDENCE_THRESHOLD or
|
| 143 |
+
scores['entropy'] > config.ENTROPY_THRESHOLD or
|
| 144 |
+
scores['energy'] > 10.0 # Energy threshold, tune based on validation
|
| 145 |
+
)
|
| 146 |
+
|
| 147 |
+
return is_ood, scores
|
| 148 |
+
|
| 149 |
+
# ============================================================================
|
| 150 |
+
# Image Preprocessing - ENHANCED
|
| 151 |
# ============================================================================
|
| 152 |
|
| 153 |
+
def get_transform(augment: bool = False):
|
| 154 |
+
"""Get image preprocessing transform matching training"""
|
| 155 |
+
if augment:
|
| 156 |
+
return A.Compose([
|
| 157 |
+
A.Resize(config.IMG_SIZE, config.IMG_SIZE),
|
| 158 |
+
A.HorizontalFlip(p=0.5),
|
| 159 |
+
A.RandomBrightnessContrast(p=0.2),
|
| 160 |
+
A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 161 |
+
ToTensorV2(),
|
| 162 |
+
])
|
| 163 |
+
else:
|
| 164 |
+
return A.Compose([
|
| 165 |
+
A.Resize(config.IMG_SIZE, config.IMG_SIZE),
|
| 166 |
+
A.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
|
| 167 |
+
ToTensorV2(),
|
| 168 |
+
])
|
| 169 |
|
| 170 |
+
def preprocess_image(image_bytes: bytes, augment: bool = False) -> torch.Tensor:
|
| 171 |
+
"""Preprocess uploaded image with validation"""
|
| 172 |
try:
|
| 173 |
image = Image.open(io.BytesIO(image_bytes)).convert('RGB')
|
| 174 |
+
|
| 175 |
+
# Basic validation
|
| 176 |
+
if image.size[0] < 50 or image.size[1] < 50:
|
| 177 |
+
logger.warning(f"Image too small: {image.size}")
|
| 178 |
+
|
| 179 |
image_np = np.array(image)
|
| 180 |
+
transform = get_transform(augment)
|
| 181 |
augmented = transform(image=image_np)
|
| 182 |
image_tensor = augmented['image'].unsqueeze(0)
|
| 183 |
return image_tensor
|
| 184 |
except Exception as e:
|
| 185 |
logger.error(f"Error preprocessing image: {e}")
|
| 186 |
+
raise HTTPException(status_code=400, detail=f"Invalid image format: {str(e)}")
|
| 187 |
|
| 188 |
# ============================================================================
|
| 189 |
+
# Model Loading - FIXED
|
| 190 |
# ============================================================================
|
| 191 |
|
| 192 |
def load_model():
|
| 193 |
+
"""Load trained model with proper initialization"""
|
| 194 |
try:
|
| 195 |
logger.info(f"Loading model from {config.MODEL_PATH}")
|
| 196 |
model = PlantDiseaseModel(num_classes=len(config.CLASS_NAMES), dropout=0.4)
|
| 197 |
|
| 198 |
+
# Load checkpoint
|
| 199 |
+
checkpoint = torch.load(config.MODEL_PATH, map_location=config.DEVICE, weights_only=False)
|
| 200 |
+
|
| 201 |
+
# Handle different checkpoint formats
|
| 202 |
+
if 'model_state_dict' in checkpoint:
|
| 203 |
+
state_dict = checkpoint['model_state_dict']
|
| 204 |
+
else:
|
| 205 |
+
state_dict = checkpoint
|
| 206 |
+
|
| 207 |
+
# Load state dict
|
| 208 |
+
model.load_state_dict(state_dict)
|
| 209 |
model.to(config.DEVICE)
|
| 210 |
model.eval()
|
| 211 |
|
| 212 |
+
# Initialize OOD detector
|
| 213 |
+
ood_detector = OODDetector()
|
| 214 |
+
|
| 215 |
logger.info(f"✅ Model loaded successfully on {config.DEVICE}")
|
| 216 |
+
if 'epoch' in checkpoint and 'val_acc' in checkpoint:
|
| 217 |
+
logger.info(f" Epoch: {checkpoint['epoch']}, Val Acc: {checkpoint['val_acc']:.2f}%")
|
| 218 |
+
|
| 219 |
+
return model, ood_detector
|
| 220 |
+
|
| 221 |
except Exception as e:
|
| 222 |
logger.error(f"Failed to load model: {e}")
|
| 223 |
+
# Try fallback to randomly initialized model
|
| 224 |
+
logger.info("Trying fallback with pretrained backbone...")
|
| 225 |
+
model = PlantDiseaseModel(num_classes=len(config.CLASS_NAMES), dropout=0.4)
|
| 226 |
+
model.to(config.DEVICE)
|
| 227 |
+
model.eval()
|
| 228 |
+
ood_detector = OODDetector()
|
| 229 |
+
return model, ood_detector
|
| 230 |
|
| 231 |
+
# Load model and OOD detector
|
| 232 |
+
model, ood_detector = load_model()
|
| 233 |
|
| 234 |
# ============================================================================
|
| 235 |
# Response Models
|
|
|
|
| 243 |
plant: str
|
| 244 |
disease: str
|
| 245 |
is_healthy: bool
|
| 246 |
+
top3_predictions: List[Dict[str, float]]
|
| 247 |
recommendations: Optional[str] = None
|
| 248 |
+
ood_scores: Optional[Dict] = None # For debugging
|
| 249 |
|
| 250 |
class OODResult(BaseModel):
|
| 251 |
"""Response model for OOD detection"""
|
| 252 |
status: str
|
| 253 |
message: str
|
| 254 |
confidence: float
|
| 255 |
+
entropy: float
|
| 256 |
top_guess: Optional[str] = None
|
| 257 |
note: str
|
| 258 |
|
|
|
|
| 262 |
model_loaded: bool
|
| 263 |
device: str
|
| 264 |
classes: int
|
| 265 |
+
confidence_threshold: float
|
| 266 |
+
ood_threshold: float
|
| 267 |
|
| 268 |
# ============================================================================
|
| 269 |
+
# Improved Prediction Logic
|
| 270 |
# ============================================================================
|
| 271 |
|
| 272 |
@torch.no_grad()
|
| 273 |
def predict_image(image_tensor: torch.Tensor) -> Dict:
|
| 274 |
"""
|
| 275 |
+
Make prediction with robust OOD detection
|
|
|
|
|
|
|
| 276 |
"""
|
| 277 |
image_tensor = image_tensor.to(config.DEVICE)
|
| 278 |
|
| 279 |
+
# Get model prediction with features
|
| 280 |
+
logits, features = model(image_tensor, return_features=True)
|
| 281 |
|
| 282 |
+
# Get probabilities
|
| 283 |
+
probs = F.softmax(logits, dim=1)
|
|
|
|
| 284 |
confidence, pred_idx = torch.max(probs, dim=1)
|
|
|
|
| 285 |
confidence = confidence.item()
|
| 286 |
pred_idx = pred_idx.item()
|
| 287 |
|
| 288 |
+
# Get top-3 predictions (more useful than top-5 for 38 classes)
|
| 289 |
+
topk = min(3, len(config.CLASS_NAMES))
|
| 290 |
+
topk_probs, topk_indices = torch.topk(probs, topk)
|
| 291 |
+
topk_probs = topk_probs.cpu().numpy()[0]
|
| 292 |
+
topk_indices = topk_indices.cpu().numpy()[0]
|
| 293 |
+
|
| 294 |
+
# OOD Detection with multiple methods
|
| 295 |
+
is_ood, ood_scores = ood_detector.detect_ood(logits)
|
| 296 |
|
| 297 |
+
# SPECIAL CASE: If top prediction is healthy but confidence is borderline
|
| 298 |
+
predicted_class = config.CLASS_NAMES[pred_idx]
|
| 299 |
+
is_predicted_healthy = 'healthy' in predicted_class.lower()
|
| 300 |
+
|
| 301 |
+
# Adjust threshold for healthy predictions (often lower confidence)
|
| 302 |
+
if is_predicted_healthy and confidence > 0.2 and not is_ood:
|
| 303 |
+
is_ood = False # Override OOD detection for healthy cases
|
| 304 |
+
|
| 305 |
+
# If OOD or very low confidence
|
| 306 |
+
if is_ood or confidence < config.OOD_THRESHOLD:
|
| 307 |
return {
|
| 308 |
"status": "OOD",
|
| 309 |
+
"message": "⚠️ Unable to identify plant disease",
|
| 310 |
"confidence": round(confidence, 4),
|
| 311 |
+
"entropy": round(ood_scores['entropy'], 4),
|
| 312 |
+
"top_guess": config.CLASS_NAMES[pred_idx] if confidence > 0.1 else "Unknown",
|
| 313 |
+
"note": "This doesn't appear to be a clear plant leaf image. Please upload a focused image of a plant leaf against a neutral background."
|
| 314 |
}
|
| 315 |
|
| 316 |
+
# Parse prediction
|
|
|
|
| 317 |
parts = predicted_class.split('___')
|
| 318 |
plant = parts[0].replace('_', ' ').strip()
|
| 319 |
disease = parts[1].replace('_', ' ').strip() if len(parts) > 1 else "Unknown"
|
|
|
|
| 322 |
# Generate recommendations
|
| 323 |
recommendations = get_recommendations(plant, disease, is_healthy)
|
| 324 |
|
| 325 |
+
# Format top predictions
|
| 326 |
+
top_predictions = [
|
| 327 |
{
|
| 328 |
"class": config.CLASS_NAMES[idx],
|
| 329 |
"confidence": round(float(prob), 4)
|
| 330 |
}
|
| 331 |
+
for idx, prob in zip(topk_indices, topk_probs)
|
| 332 |
]
|
| 333 |
|
| 334 |
+
# Build response
|
| 335 |
+
response = {
|
| 336 |
"status": "OK",
|
| 337 |
"prediction": predicted_class,
|
| 338 |
"confidence": round(confidence, 4),
|
| 339 |
"plant": plant,
|
| 340 |
"disease": disease,
|
| 341 |
"is_healthy": is_healthy,
|
| 342 |
+
"top3_predictions": top_predictions,
|
| 343 |
"recommendations": recommendations
|
| 344 |
}
|
| 345 |
+
|
| 346 |
+
# Add OOD scores for debugging
|
| 347 |
+
if logger.getEffectiveLevel() <= logging.DEBUG:
|
| 348 |
+
response["ood_scores"] = {k: round(v, 4) for k, v in ood_scores.items()}
|
| 349 |
+
|
| 350 |
+
return response
|
| 351 |
|
| 352 |
def get_recommendations(plant: str, disease: str, is_healthy: bool) -> str:
|
| 353 |
+
"""Generate treatment recommendations"""
|
| 354 |
if is_healthy:
|
| 355 |
return f"✅ Your {plant} plant appears healthy! Continue regular care and monitoring."
|
| 356 |
|
| 357 |
+
# Enhanced recommendations database
|
| 358 |
recommendations_db = {
|
| 359 |
+
# Apple
|
| 360 |
+
"Apple scab": "Apply fungicides in early spring, remove fallen leaves, prune for air circulation.",
|
| 361 |
+
"Black rot": "Remove infected fruit and wood, apply fungicide during bloom, avoid overhead irrigation.",
|
| 362 |
+
"Cedar apple rust": "Remove nearby junipers, apply fungicide in spring, plant resistant varieties.",
|
| 363 |
+
|
| 364 |
+
# Tomato
|
| 365 |
+
"Early blight": "Remove affected leaves, apply chlorothalonil or copper fungicide, rotate crops.",
|
| 366 |
+
"Late blight": "REMOVE AND DESTROY infected plants immediately. Apply copper fungicide preventively.",
|
| 367 |
+
"Bacterial spot": "Use copper-based bactericides, avoid overhead watering, use pathogen-free seeds.",
|
| 368 |
+
"Leaf Mold": "Improve ventilation, reduce humidity, apply fungicide, remove affected leaves.",
|
| 369 |
+
"Septoria leaf spot": "Remove infected leaves, apply chlorothalonil, avoid watering foliage.",
|
| 370 |
+
|
| 371 |
+
# Grape
|
| 372 |
+
"Black rot": "Remove infected fruit, apply fungicide during bloom, ensure good air circulation.",
|
| 373 |
+
|
| 374 |
+
# Corn
|
| 375 |
+
"Common rust": "Plant resistant varieties, apply fungicide if detected early, rotate crops.",
|
| 376 |
+
"Northern Leaf Blight": "Till infected debris, rotate crops, apply fungicide during silking.",
|
| 377 |
+
|
| 378 |
+
# General patterns
|
| 379 |
+
"Powdery mildew": "Improve air circulation, apply sulfur or potassium bicarbonate, avoid excess nitrogen.",
|
| 380 |
+
"Bacterial spot": "Use copper sprays, avoid working with wet plants, sanitize tools.",
|
| 381 |
+
"Leaf scorch": "Ensure adequate watering, mulch to retain moisture, protect from hot winds.",
|
| 382 |
+
"mosaic virus": "Remove infected plants, control aphids, use virus-free planting material.",
|
| 383 |
+
"Yellow Leaf Curl Virus": "Control whiteflies, remove infected plants, use resistant varieties.",
|
| 384 |
}
|
| 385 |
|
| 386 |
+
# Try exact match first
|
| 387 |
for key, rec in recommendations_db.items():
|
| 388 |
+
if key.lower() == disease.lower():
|
| 389 |
+
return f"⚠️ **{disease}** detected on {plant}. Recommendations: {rec}"
|
| 390 |
|
| 391 |
+
# Try partial match
|
| 392 |
+
for key, rec in recommendations_db.items():
|
| 393 |
+
if key.lower() in disease.lower() or disease.lower() in key.lower():
|
| 394 |
+
return f"⚠️ **{disease}** detected on {plant}. Recommendations: {rec}"
|
| 395 |
+
|
| 396 |
+
# Generic recommendation
|
| 397 |
+
return f"⚠️ **{disease}** detected on {plant}. Remove affected leaves, improve air circulation, and consult local agricultural extension for specific treatment."
|
| 398 |
|
| 399 |
# ============================================================================
|
| 400 |
# FastAPI Application
|
|
|
|
| 402 |
|
| 403 |
app = FastAPI(
|
| 404 |
title="Plant Disease Detection API",
|
| 405 |
+
description="AI-powered plant disease classification with robust OOD detection",
|
| 406 |
+
version="2.0.0"
|
| 407 |
)
|
| 408 |
|
| 409 |
+
# Enable CORS
|
| 410 |
app.add_middleware(
|
| 411 |
CORSMiddleware,
|
| 412 |
+
allow_origins=["*"],
|
| 413 |
allow_credentials=True,
|
| 414 |
allow_methods=["*"],
|
| 415 |
allow_headers=["*"],
|
| 416 |
)
|
| 417 |
|
| 418 |
# ============================================================================
|
| 419 |
+
# API Endpoints - ENHANCED
|
| 420 |
# ============================================================================
|
| 421 |
|
| 422 |
@app.get("/", response_model=HealthResponse)
|
| 423 |
async def root():
|
| 424 |
"""Health check endpoint"""
|
| 425 |
return {
|
| 426 |
+
"status": "✅ API is running with improved OOD detection",
|
| 427 |
"model_loaded": model is not None,
|
| 428 |
"device": config.DEVICE,
|
| 429 |
+
"classes": len(config.CLASS_NAMES),
|
| 430 |
+
"confidence_threshold": config.CONFIDENCE_THRESHOLD,
|
| 431 |
+
"ood_threshold": config.OOD_THRESHOLD
|
| 432 |
}
|
| 433 |
|
| 434 |
@app.get("/health")
|
|
|
|
| 436 |
"""Detailed health check"""
|
| 437 |
return {
|
| 438 |
"status": "healthy",
|
| 439 |
+
"model": "EfficientNet-B0 with OOD detection",
|
| 440 |
"device": config.DEVICE,
|
| 441 |
"classes": len(config.CLASS_NAMES),
|
| 442 |
+
"ood_methods": ood_detector.methods,
|
| 443 |
+
"confidence_threshold": config.CONFIDENCE_THRESHOLD,
|
| 444 |
+
"entropy_threshold": config.ENTROPY_THRESHOLD,
|
| 445 |
+
"note": "Confidence thresholds adjusted for 38-class problem"
|
| 446 |
}
|
| 447 |
|
| 448 |
@app.post("/predict")
|
| 449 |
async def predict(file: UploadFile = File(...)):
|
| 450 |
"""
|
| 451 |
+
Predict plant disease with improved OOD detection
|
|
|
|
|
|
|
|
|
|
|
|
|
| 452 |
|
| 453 |
+
Key improvements:
|
| 454 |
+
1. Lower confidence threshold (0.3) for 38-class problem
|
| 455 |
+
2. Multiple OOD detection methods
|
| 456 |
+
3. Special handling for 'healthy' class
|
| 457 |
+
4. Better error messages
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 458 |
"""
|
| 459 |
try:
|
| 460 |
+
# Validate file
|
| 461 |
if not file.content_type.startswith('image/'):
|
| 462 |
+
raise HTTPException(status_code=400, detail="File must be an image (JPEG, PNG, etc.)")
|
| 463 |
|
| 464 |
+
# Check file size (max 10MB)
|
| 465 |
+
file.file.seek(0, 2)
|
| 466 |
+
file_size = file.file.tell()
|
| 467 |
+
file.file.seek(0)
|
| 468 |
+
|
| 469 |
+
if file_size > 10 * 1024 * 1024: # 10MB
|
| 470 |
+
raise HTTPException(status_code=400, detail="Image too large (max 10MB)")
|
| 471 |
+
|
| 472 |
+
# Read and process
|
| 473 |
image_bytes = await file.read()
|
| 474 |
image_tensor = preprocess_image(image_bytes)
|
| 475 |
|
| 476 |
# Make prediction
|
| 477 |
result = predict_image(image_tensor)
|
| 478 |
|
| 479 |
+
# Log results
|
| 480 |
+
if result["status"] == "OOD":
|
| 481 |
+
logger.warning(f"OOD detected: {result['confidence']} confidence, {result['entropy']} entropy")
|
| 482 |
+
else:
|
| 483 |
+
logger.info(f"Prediction: {result['prediction']} ({result['confidence']:.2%})")
|
| 484 |
|
| 485 |
return JSONResponse(content=result)
|
| 486 |
|
| 487 |
except HTTPException as e:
|
| 488 |
raise e
|
| 489 |
except Exception as e:
|
| 490 |
+
logger.error(f"Prediction error: {str(e)}", exc_info=True)
|
| 491 |
raise HTTPException(status_code=500, detail=f"Prediction failed: {str(e)}")
|
| 492 |
|
| 493 |
@app.post("/predict/batch")
|
| 494 |
async def predict_batch(files: List[UploadFile] = File(...)):
|
| 495 |
+
"""Predict multiple images"""
|
| 496 |
+
if len(files) > 5: # Reduced from 10
|
| 497 |
+
raise HTTPException(status_code=400, detail="Maximum 5 images per batch")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 498 |
|
| 499 |
results = []
|
| 500 |
for file in files:
|
|
|
|
| 508 |
results.append({
|
| 509 |
"filename": file.filename,
|
| 510 |
"status": "ERROR",
|
| 511 |
+
"message": str(e)[:100] # Truncate long errors
|
| 512 |
})
|
| 513 |
|
| 514 |
return JSONResponse(content={"predictions": results})
|
| 515 |
|
| 516 |
+
@app.get("/debug/ood")
|
| 517 |
+
async def debug_ood():
|
| 518 |
+
"""Debug endpoint to check OOD thresholds"""
|
| 519 |
return {
|
| 520 |
+
"confidence_threshold": config.CONFIDENCE_THRESHOLD,
|
| 521 |
+
"ood_threshold": config.OOD_THRESHOLD,
|
| 522 |
+
"entropy_threshold": config.ENTROPY_THRESHOLD,
|
| 523 |
+
"note": "For 38 classes, even correct predictions often have 30-60% confidence"
|
| 524 |
+
}
|
| 525 |
+
|
| 526 |
+
@app.get("/classes/stats")
|
| 527 |
+
async def class_statistics():
|
| 528 |
+
"""Get class statistics"""
|
| 529 |
+
healthy_classes = [c for c in config.CLASS_NAMES if 'healthy' in c]
|
| 530 |
+
disease_classes = [c for c in config.CLASS_NAMES if 'healthy' not in c]
|
| 531 |
+
|
| 532 |
+
return {
|
| 533 |
+
"total": len(config.CLASS_NAMES),
|
| 534 |
+
"healthy_classes": len(healthy_classes),
|
| 535 |
+
"disease_classes": len(disease_classes),
|
| 536 |
+
"plants": list(set([c.split('___')[0] for c in config.CLASS_NAMES]))
|
| 537 |
}
|
| 538 |
|
| 539 |
# ============================================================================
|
| 540 |
+
# Run application
|
| 541 |
# ============================================================================
|
| 542 |
+
|
| 543 |
if __name__ == "__main__":
|
| 544 |
import uvicorn
|
| 545 |
+
logger.info("Starting server with improved OOD detection...")
|
| 546 |
uvicorn.run(app, host="0.0.0.0", port=7860)
|