Skip to content

Latest commit

Β 

History

15 Commits

Folders and files

NameName
Last commit message
Last commit date
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

🌱 PlantDoctor AI

PlantDoctor AI is an AI-powered plant disease detection and diagnosis system that combines Deep Learning and Generative AI.

The system uses a CNN to identify plant diseases from leaf images and a Retrieval-Augmented Generation (RAG) pipeline to provide information about symptoms, causes, prevention, and management.

The backend is built using FastAPI and exposes REST APIs for plant analysis and RAG-based question answering.


πŸ“Œ Problem Statement

Plant diseases can significantly affect crop yield and quality. Early identification of diseases can help farmers take timely preventive and corrective actions.

PlantDoctor AI aims to automate the initial identification of plant diseases by analyzing a plant leaf image using a Convolutional Neural Network (CNN).

The trained model classifies leaf images into 15 plant disease/health categories.


🎯 Objectives

  • Integrate the model into a FastAPI backend
  • Build a Retrieval-Augmented Generation (RAG) pipeline
  • Generate embeddings and store them using FAISS
  • Retrieve relevant plant disease information from a knowledge base
  • Use Gemini for context-based response generation
  • Generate structured responses using Pydantic
  • Expose CNN and RAG functionality through FastAPI REST APIs

πŸ“‚ Dataset

The project uses the PlantVillage dataset.

The dataset is organized into class-specific directories, allowing class labels to be inferred directly from folder names.

The model uses 15 classes:

Pepper__bell___Bacterial_spot
Pepper__bell___healthy
Potato___Early_blight
Potato___Late_blight
Potato___healthy
Tomato_Bacterial_spot
Tomato_Early_blight
Tomato_Late_blight
Tomato_Leaf_Mold
Tomato_Septoria_leaf_spot
Tomato_Spider_mites_Two_spotted_spider_mite
Tomato__Target_Spot
Tomato__Tomato_YellowLeaf__Curl_Virus
Tomato__Tomato_mosaic_virus
Tomato_healthy

πŸ”„ Machine Learning Pipeline

PlantVillage Dataset
        ↓
Dataset Exploration
        ↓
Image Preprocessing
        ↓
Train / Validation / Test Split
        ↓
Baseline CNN
        ↓
Improved CNN
        ↓
Model Training
        ↓
Model Evaluation
        ↓
Model Export
        ↓
FastAPI Backend
        ↓
CNN + RAG Integration
        ↓
REST APIs

πŸ€– Generative AI & RAG

PlantDoctor AI uses Retrieval-Augmented Generation (RAG) to provide detailed information about detected plant diseases.

The RAG pipeline uses a plant disease knowledge base containing information about symptoms, causes, prevention, and management.

RAG Pipeline

Plant Disease Knowledge Base
        ↓
TextLoader
        ↓
RecursiveCharacterTextSplitter
        ↓
Gemini Embeddings
        ↓
FAISS Vector Store
        ↓
Retriever
        ↓
Relevant Documents
        ↓
Gemini
        ↓
Pydantic Structured Response

🧹 Data Preprocessing

The PlantVillage dataset is organized into separate directories for each plant disease and healthy class. The class labels are inferred automatically from the directory structure.

Image Preprocessing

Images are loaded using TensorFlow's image_dataset_from_directory utility.

The following preprocessing configuration is used:

  • Image size: 224 Γ— 224
  • Color channels: RGB
  • Batch size: 32
  • Label mode: categorical
  • Dataset shuffling: Enabled
  • Random seed: 42

Dataset Split

The dataset is divided into training, validation, and testing sets.

Dataset Percentage
Training 70%
Validation 20%
Testing 10%

A 30% portion of the dataset is first selected as a temporary dataset. This temporary dataset is then divided into validation and test sets, resulting in an approximate final split of 70% / 20% / 10%.

Input Format

Each image is resized to:

224 Γ— 224 Γ— 3

🧠 Model Development

Baseline CNN

A baseline Convolutional Neural Network (CNN) was first developed to establish an initial performance benchmark for the plant disease classification task.

The baseline model was trained on the training dataset and evaluated using the validation and test datasets.

Screenshot 2026-08-12 at 5 40 16β€―PM

Improved CNN

An improved CNN architecture was developed to achieve better classification performance.

The final model consists of five convolutional blocks. Each block contains a convolutional layer followed by Batch Normalization and MaxPooling.

Architecture

Input: 224 Γ— 224 Γ— 3
        ↓
Conv2D - 32 filters
        ↓
Batch Normalization
        ↓
MaxPooling2D
        ↓
Conv2D - 64 filters
        ↓
Batch Normalization
        ↓
MaxPooling2D
        ↓
Conv2D - 128 filters
        ↓
Batch Normalization
        ↓
MaxPooling2D
        ↓
Conv2D - 256 filters
        ↓
Batch Normalization
        ↓
MaxPooling2D
        ↓
Conv2D - 512 filters
        ↓
Batch Normalization
        ↓
MaxPooling2D
        ↓
Flatten
        ↓
Dense - 512
        ↓
Dropout
        ↓
Dense - 256
        ↓
Dropout
        ↓
Dense - 15
        ↓
Softmax
Screenshot 2026-08-12 at 5 40 50β€―PM

πŸ“Š Model Evaluation

The final improved CNN was evaluated on the test dataset to measure its performance on previously unseen images.

Test Accuracy

The final model achieved 96.17% Test Accuracy on the test dataset.

Test Accuracy: 0.961706280708313

πŸ’Ύ Model Export

After training and evaluation, the best-performing CNN model was exported in Keras format for use during inference.

The trained model is stored at:

model/improved_cnn.keras
model/class_names.json

πŸš€ FastAPI Backend

The trained CNN model and RAG pipeline are integrated into a FastAPI backend to provide plant disease prediction and AI-powered disease analysis through REST APIs.

API Endpoints

  • POST /predict β€” CNN disease prediction
  • POST /ask β€” RAG-based question answering
  • POST /analyze β€” Combined CNN + RAG analysis

πŸ”Œ API Workflow

The PlantDoctor AI analysis workflow combines CNN-based disease prediction with RAG-based information retrieval and Generative AI.

User
  β”‚
  β”‚ Upload Leaf Image
  β–Ό
FastAPI Server
  β”‚
  β”‚ POST /analyze
  β–Ό
Image Preprocessing
  β”‚
  β”‚ Resize β†’ 224 Γ— 224
  β”‚ Convert β†’ RGB
  β–Ό
CNN Model
  β”‚
  β–Ό
Disease + Confidence
  β”‚
  β–Ό
RAG Retriever
  β”‚
  β–Ό
FAISS Vector Store
  β”‚
  β–Ό
Relevant Context
  β”‚
  β–Ό
Gemini
  β”‚
  β–Ό
Pydantic Structured Output
  β”‚
  β”œβ”€β”€ Symptoms
  β”œβ”€β”€ Cause
  β”œβ”€β”€ Prevention
  └── Management
  β”‚
  β–Ό
JSON Response
  β”‚
  β–Ό
User

πŸ§ͺ Running Locally

  1. Clone the repository and navigate to the project directory.
  2. Create and activate a Python virtual environment.
  3. Install dependencies from requirements.txt.
  4. Configure the required Gemini API key in .env.
  5. Run the FastAPI server using Uvicorn.
  6. Open http://127.0.0.1:8000/docs.
  7. Test the APIs using Swagger UI:
    • POST /predict β†’ CNN disease prediction
    • POST /ask β†’ RAG-based question answering
    • POST /analyze β†’ Complete CNN + RAG analysis

πŸ“– API Documentation

FastAPI provides interactive Swagger documentation at:

http://127.0.0.1:8000/docs

GET /

Checks whether the API is running.

POST /predict

Performs CNN-based plant disease prediction from an uploaded leaf image.

POST /ask

Uses the RAG pipeline with FAISS and Gemini to answer plant disease-related questions.

POST /analyze

Combines CNN prediction with RAG to return disease information including symptoms, cause, prevention, and management.


πŸ“ Project Structure

PlantDoctor-AI/
β”‚
β”œβ”€β”€ app/
β”‚   └── main.py
β”‚
β”œβ”€β”€ knowledge/
β”‚   └── plant_diseases.txt
β”‚
β”œβ”€β”€ model/
β”‚   β”œβ”€β”€ improved_cnn.keras
β”‚   └── class_names.json
β”‚
β”œβ”€β”€ notebooks/
β”‚   β”œβ”€β”€ data-exploration.ipynb
β”‚   β”œβ”€β”€ baseline-cnn.ipynb
β”‚   └── improved-cnn.ipynb
β”‚
β”œβ”€β”€ rag/
β”‚   β”œβ”€β”€ generator.py
β”‚   β”œβ”€β”€ ingest.py
β”‚   └── retriever.py
β”‚
β”œβ”€β”€ frontend/
β”‚   └── index.html
β”‚
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ .gitignore
β”œβ”€β”€ .gitattributes
└── README.md

πŸ› οΈ Technologies Used

  • Python β€” Core programming language
  • TensorFlow & Keras β€” Deep Learning and CNN model
  • NumPy & Pandas β€” Data processing
  • Matplotlib β€” Data visualization and model evaluation
  • LangChain β€” RAG pipeline and document processing
  • Google Gemini β€” Embeddings and Generative AI
  • FAISS β€” Vector storage and similarity search
  • Pydantic β€” Structured AI output
  • FastAPI β€” REST API backend
  • Uvicorn β€” ASGI server
  • Pillow β€” Image processing
  • Git & GitHub β€” Version control
  • Git LFS β€” Storage of the trained .keras model

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages