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.
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.
- 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
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
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
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.
Plant Disease Knowledge Base
β
TextLoader
β
RecursiveCharacterTextSplitter
β
Gemini Embeddings
β
FAISS Vector Store
β
Retriever
β
Relevant Documents
β
Gemini
β
Pydantic Structured Response
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.
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
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%.
Each image is resized to:
224 Γ 224 Γ 3
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.
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.
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
The final improved CNN was evaluated on the test dataset to measure its performance on previously unseen images.
The final model achieved 96.17% Test Accuracy on the test dataset.
Test Accuracy: 0.961706280708313
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
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.
POST /predictβ CNN disease predictionPOST /askβ RAG-based question answeringPOST /analyzeβ Combined CNN + RAG analysis
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
- Clone the repository and navigate to the project directory.
- Create and activate a Python virtual environment.
- Install dependencies from
requirements.txt. - Configure the required Gemini API key in
.env. - Run the FastAPI server using Uvicorn.
- Open
http://127.0.0.1:8000/docs. - Test the APIs using Swagger UI:
POST /predictβ CNN disease predictionPOST /askβ RAG-based question answeringPOST /analyzeβ Complete CNN + RAG analysis
FastAPI provides interactive Swagger documentation at:
http://127.0.0.1:8000/docs
Checks whether the API is running.
Performs CNN-based plant disease prediction from an uploaded leaf image.
Uses the RAG pipeline with FAISS and Gemini to answer plant disease-related questions.
Combines CNN prediction with RAG to return disease information including symptoms, cause, prevention, and management.
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
- 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
.kerasmodel