File size: 4,096 Bytes
58cb4f6
 
 
 
 
 
 
 
 
 
 
565aecf
 
 
 
 
 
 
 
 
 
445b2a2
565aecf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
445b2a2
 
565aecf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
---
title: LeafScan
emoji: 🌿
colorFrom: green
colorTo: gray
sdk: docker
app_file: app.py
pinned: false
---


# 🌿 LeafScan β€” Plant Disease Detection using Deep Learning

A full-stack AI application that detects plant leaf diseases from real-world images using a fine-tuned **EfficientNetB3** model trained on the PlantVillage dataset.

---

## πŸš€ Live Demo

* 🌐 Hugging Face Space: https://huggingface.co/spaces/tktejask/leafscan

---

## 🧠 Project Overview

LeafScan is a real-time plant disease detection system that:

* Accepts **real-world leaf images**
* Detects **38 disease classes + 1 non-leaf class**
* Provides:

  * Disease name
  * Confidence score
  * Severity
  * Description
  * Treatment suggestion
  * Top-5 predictions

---

## πŸ“Š Dataset

* Source: PlantVillage Dataset
* Link: https://www.kaggle.com/datasets/abdallahalidev/plantvillage-dataset
* Size: ~54,000 images
* Classes: 38 diseases + healthy + 1 synthetic "not a leaf" class

---

## πŸ—οΈ Model Architecture

```
Input Image (300Γ—300)
        ↓
EfficientNetB3 (Pretrained on ImageNet)
        ↓
Feature Vector (1536)
        ↓
Custom Head:
  Dense β†’ GELU β†’ Dropout
  Dense β†’ GELU β†’ Dropout
  Output Layer (39 classes)
        ↓
Softmax Probabilities
```

---

## βš™οΈ Training Strategy

| Phase   | Description                |
| ------- | -------------------------- |
| Phase 1 | Train only classifier head |
| Phase 2 | Unfreeze last layers       |
| Phase 3 | Full fine-tuning           |

Techniques used:

* Transfer Learning
* Test Time Augmentation (TTA Γ—6)
* AdamW optimizer
* Label smoothing
* Class balancing

---

## πŸ”¬ Inference Pipeline

```
Input Image
   ↓
Preprocessing (Resize β†’ Normalize)
   ↓
Model Prediction
   ↓
TTA Averaging
   ↓
Confidence + Decision Logic
   ↓
Final Output + Top-5 Classes
```

---

## πŸ§ͺ Features

* βœ… Works on **real-world images (not just dataset)**
* βœ… Detects **non-leaf images**
* βœ… REST API support
* βœ… Beautiful frontend UI
* βœ… Deployable locally + cloud

---

## πŸ–₯️ Local Deployment

### 1. Setup

```bash
python -m venv venv
venv\Scripts\activate
pip install -r requirements.txt
```

---

### 2. Run server

```bash
python app.py
```

---

### 3. Output

```
Model ready.
 * Running on http://127.0.0.1:7860
```

---

### 4. Open in browser

```
http://localhost:7860
```

---

## 🌐 Hugging Face Deployment

* Platform: Hugging Face Spaces
* Runtime: Flask (Docker/Spaces)
* URL: https://huggingface.co/spaces/tktejask/leafscan

### What was done:

* Uploaded model + backend + frontend
* Configured app.py to run on port 7860
* Added README config block

---

## πŸ”— API Endpoints

| Endpoint              | Description         |
| --------------------- | ------------------- |
| `/api/predict`        | Upload image        |
| `/api/predict-url`    | Predict from URL    |
| `/api/predict-base64` | Predict from base64 |
| `/api/classes`        | List classes        |
| `/api/health`         | Server status       |

---

## πŸ“ˆ Model Performance

* Accuracy: ~96% (on PlantVillage test set)
* Supports: 38 disease classes
* Handles real-world noise via TTA

---

## ⚠️ Limitations

* Trained on controlled dataset β†’ real-world variation may reduce accuracy
* Needs clear leaf image
* Heavy model β†’ slow on CPU

---

## πŸ”₯ Key Highlights (Interview Points)

* Built **end-to-end ML system**
* Used **transfer learning (EfficientNetB3)**
* Implemented **TTA for robustness**
* Designed **Flask API + frontend integration**
* Deployed on **Hugging Face Spaces**
* Handled **real-world inference issues**

---

## πŸ“¦ Project Structure

```
leaf scan/
β”œβ”€β”€ app.py
β”œβ”€β”€ model.py
β”œβ”€β”€ predict.py
β”œβ”€β”€ metrics.py
β”œβ”€β”€ models/
β”‚   └── best_model.pth
β”œβ”€β”€ data/
β”‚   └── classes.txt
β”œβ”€β”€ frontend/
β”‚   └── index.html
└── requirements.txt
```

---

## πŸ› οΈ Tech Stack

* Python
* PyTorch
* timm
* Flask
* HTML/CSS/JS
* Hugging Face Spaces

---

## πŸ“„ License

Educational project. Dataset is public (PlantVillage).