k23064919 commited on
Commit
1fdf57d
Β·
verified Β·
1 Parent(s): a158244

Update README.md

Browse files
Files changed (1) hide show
  1. README.md +2 -384
README.md CHANGED
@@ -1,389 +1,7 @@
1
- <<<<<<< HEAD
2
- # Plant Disease Detection - UI and Deployment
3
-
4
- This directory contains the Gradio-based user interface and deployment code for the Plant Disease Detection project.
5
-
6
- ## Team Information
7
-
8
- **Team Number:** [Add your team number]
9
-
10
- **Team Members:**
11
- - [Add team member names here]
12
-
13
- ## Links
14
-
15
- - **GitHub Repository:** https://github.kcl.ac.uk/K23064919/smallGroupProject
16
- - **Deployed App:** [Add Hugging Face Spaces URL here]
17
- - **Trained Model:** [Add model download link or ClearML model ID here]
18
-
19
- ## Project Structure
20
-
21
- ```
22
- plant-disease-ui/
23
- β”œβ”€β”€ ui/
24
- β”‚ β”œβ”€β”€ app.py # Main Gradio application
25
- β”‚ β”œβ”€β”€ config.py # Configuration (class names, paths, etc.)
26
- β”‚ β”œβ”€β”€ model_loader.py # Model loading utilities
27
- β”‚ β”œβ”€β”€ utils.py # Utility functions (preprocessing, etc.)
28
- β”‚ └── examples/ # Example images for gallery
29
- β”œβ”€β”€ models/
30
- β”‚ β”œβ”€β”€ mock_model.py # Mock model for development
31
- β”‚ └── best_model.pth # (To be added) Trained model weights
32
- β”œβ”€β”€ docs/
33
- β”‚ └── deployment_guide.md # Deployment instructions
34
- β”œβ”€β”€ requirements.txt # Python dependencies
35
- └── README.md # This file
36
- ```
37
-
38
- ## Features
39
-
40
- ### Core Features
41
- - βœ… **Image Upload:** Upload plant leaf images for disease detection
42
- - βœ… **Top-K Predictions:** Display top 10 predictions with confidence scores
43
- - βœ… **Formatted Output:** Clean, readable prediction results
44
-
45
- ### Advanced Features
46
- - βœ… **Multiple Models:** Switch between different trained models (CNN, Transfer Learning)
47
- - βœ… **Example Gallery:** Pre-loaded example images for quick testing
48
- - βœ… **Batch Processing:** Upload and classify multiple images at once
49
- - βœ… **Flag Predictions:** Report incorrect predictions
50
- - βœ… **Confidence Threshold:** Filter predictions by minimum confidence level
51
- - βœ… **Detailed Information:** View plant type, disease name, and health status
52
-
53
- ## Setup Instructions
54
-
55
- ### 1. Install Dependencies
56
-
57
- ```bash
58
- # Create a virtual environment (recommended)
59
- python -m venv venv
60
- source venv/bin/activate # On Windows: venv\Scripts\activate
61
-
62
- # Install required packages
63
- pip install -r requirements.txt
64
- ```
65
-
66
- ### 2. Add Example Images (Optional)
67
-
68
- To enable the example gallery feature:
69
-
70
- ```bash
71
- # Create examples directory
72
- mkdir -p ui/examples
73
-
74
- # Add plant disease images to ui/examples/
75
- # You can download sample images from the PlantVillage dataset
76
- ```
77
-
78
- To download example images programmatically:
79
-
80
- ```python
81
- from datasets import load_dataset
82
-
83
- # Load PlantVillage dataset
84
- dataset = load_dataset("EdBianchi/plant-village")
85
-
86
- # Save some example images
87
- import os
88
- os.makedirs("ui/examples", exist_ok=True)
89
-
90
- for i in range(10): # Save 10 examples
91
- img = dataset['train'][i * 1000]['image'] # Sample every 1000th image
92
- img.save(f"ui/examples/example_{i}.jpg")
93
- ```
94
-
95
- ### 3. Run the App Locally
96
-
97
- **Option A: Using Mock Model (for development)**
98
-
99
- ```bash
100
- cd ui
101
- python app.py
102
- ```
103
-
104
- The app will start at `http://localhost:7860`
105
-
106
- **Option B: Using Your Trained Model**
107
-
108
- First, modify `app.py` to load your real model:
109
-
110
- ```python
111
- # In app.py, change the last line:
112
- demo = create_interface(use_mock=False) # Change to False
113
- ```
114
-
115
- Then run:
116
-
117
- ```bash
118
- cd ui
119
- python app.py
120
- ```
121
-
122
- ### 4. Configure for Real Model
123
-
124
- When your team's model is ready, you have several options:
125
-
126
- #### Option 1: Load from Local File
127
-
128
- ```python
129
- # In model_loader.py, update the model path
130
- MODEL_PATH = "models/best_model.pth"
131
-
132
- # Then in app.py:
133
- app = PlantDiseaseApp(use_mock=False)
134
- ```
135
-
136
- #### Option 2: Load from ClearML
137
-
138
- ```python
139
- # In app.py or model_loader.py:
140
- loader = ModelLoader(use_mock=False)
141
- model = loader.load_from_clearml(
142
- project_name="Plant Disease Detection",
143
- task_name="CNN Training"
144
- )
145
- ```
146
-
147
- #### Option 3: Load from Hugging Face Hub
148
-
149
- ```python
150
- # First, upload your model to HF Hub
151
- # Then in model_loader.py:
152
- loader = ModelLoader(use_mock=False)
153
- model = loader.load_from_huggingface("your-username/plant-disease-model")
154
- ```
155
-
156
- ## Deployment to Hugging Face Spaces
157
-
158
- ### Step 1: Create a Hugging Face Account
159
-
160
- 1. Go to https://huggingface.co/ and create an account
161
- 2. Verify your email address
162
-
163
- ### Step 2: Create a New Space
164
-
165
- 1. Click on your profile β†’ "New Space"
166
- 2. Space name: `plant-disease-detection`
167
- 3. License: Apache 2.0
168
- 4. Select SDK: **Gradio**
169
- 5. Make it **Public**
170
- 6. Click "Create Space"
171
-
172
- ### Step 3: Prepare Files for Deployment
173
-
174
- Create these files in the root of your Space:
175
-
176
- **app.py** (Simplified version for HF Spaces)
177
- ```python
178
- # Copy ui/app.py and modify the imports to work in the flat structure
179
- ```
180
-
181
- **requirements.txt**
182
- ```
183
- torch
184
- torchvision
185
- gradio
186
- Pillow
187
- numpy
188
- huggingface-hub
189
- ```
190
-
191
- **README.md** (for the Space)
192
- ```markdown
193
  ---
194
- title: Plant Disease Detection
195
- emoji: 🌱
196
- colorFrom: green
197
- colorTo: blue
198
  sdk: gradio
199
- sdk_version: 4.0.0
200
  app_file: app.py
201
  pinned: false
202
- ---
203
-
204
- # Plant Disease Detection
205
-
206
- AI-powered plant disease detection from leaf images.
207
- Developed by [Your Team Name] for King's College London.
208
- ```
209
-
210
- ### Step 4: Upload Your Model
211
-
212
- **Option A: Upload weights to the Space**
213
-
214
- 1. Upload your `best_model.pth` to the Space
215
- 2. Modify `app.py` to load from this file
216
-
217
- **Option B: Use Hugging Face Hub**
218
-
219
- 1. Upload model to HF Model Hub:
220
- ```python
221
- from huggingface_hub import HfApi
222
-
223
- api = HfApi()
224
- api.upload_file(
225
- path_or_fileobj="models/best_model.pth",
226
- path_in_repo="model.pth",
227
- repo_id="your-username/plant-disease-model",
228
- repo_type="model"
229
- )
230
- ```
231
-
232
- 2. Load in app:
233
- ```python
234
- from huggingface_hub import hf_hub_download
235
- model_path = hf_hub_download(
236
- repo_id="your-username/plant-disease-model",
237
- filename="model.pth"
238
- )
239
- ```
240
-
241
- **Option C: Fetch from ClearML**
242
-
243
- 1. Add ClearML credentials to Space Secrets
244
- 2. Use the `load_from_clearml()` function
245
-
246
- ### Step 5: Deploy
247
-
248
- 1. Upload all files to your HF Space repository
249
- 2. The app will automatically build and deploy
250
- 3. Test at: `https://huggingface.co/spaces/your-username/plant-disease-detection`
251
-
252
- ## Model Integration Guide
253
-
254
- ### Your CNN Model Structure
255
-
256
- When integrating your actual trained model, make sure to update `model_loader.py` with your actual CNN architecture:
257
-
258
- ```python
259
- class YourCNNModel(nn.Module):
260
- def __init__(self, num_classes=39):
261
- super(YourCNNModel, self).__init__()
262
-
263
- # Add your actual CNN architecture here
264
- # This should match what you used for training
265
-
266
- def forward(self, x):
267
- # Your forward pass
268
- return x
269
- ```
270
-
271
- ### Loading Trained Weights
272
-
273
- ```python
274
- # Load model
275
- model = YourCNNModel(num_classes=39)
276
-
277
- # Load trained weights
278
- checkpoint = torch.load('path/to/best_model.pth', map_location=device)
279
-
280
- # If you saved the entire model:
281
- model = checkpoint
282
-
283
- # If you saved just state_dict:
284
- model.load_state_dict(checkpoint)
285
-
286
- # Or if you saved optimizer and other info:
287
- model.load_state_dict(checkpoint['model_state_dict'])
288
- ```
289
-
290
- ## Testing the UI
291
-
292
- ### Manual Testing Checklist
293
-
294
- - [ ] Upload a single image and get predictions
295
- - [ ] Try different models from the dropdown
296
- - [ ] Adjust confidence threshold slider
297
- - [ ] Test example gallery (if images added)
298
- - [ ] Upload multiple images for batch processing
299
- - [ ] Flag a prediction
300
- - [ ] Check all tabs load correctly
301
- - [ ] Verify predictions match expected classes
302
-
303
- ### Automated Testing
304
-
305
- ```python
306
- # Run tests
307
- cd ui
308
- python -m pytest test_app.py # (Create tests if needed)
309
- ```
310
-
311
- ## Troubleshooting
312
-
313
- ### Common Issues
314
-
315
- **1. ModuleNotFoundError**
316
- ```bash
317
- # Make sure all dependencies are installed
318
- pip install -r requirements.txt
319
- ```
320
-
321
- **2. Model Loading Error**
322
- ```python
323
- # Check that the model architecture matches the saved weights
324
- # Make sure you're using the same num_classes (39)
325
- ```
326
-
327
- **3. Image Size Issues**
328
- ```python
329
- # Ensure images are being resized to (256, 256)
330
- # Check config.py IMAGE_SIZE setting
331
- ```
332
-
333
- **4. CUDA/GPU Errors**
334
- ```python
335
- # The app automatically falls back to CPU
336
- # Check: torch.cuda.is_available()
337
- ```
338
-
339
- ## Contributing
340
-
341
- When contributing to this UI:
342
-
343
- 1. Create a new branch for your feature
344
- 2. Test locally with mock model first
345
- 3. Test with real model before pushing
346
- 4. Update this README if adding new features
347
- 5. Ensure code is well-commented
348
-
349
- ## TODO
350
-
351
- - [ ] Add more example images to gallery
352
- - [ ] Integrate with actual trained models
353
- - [ ] Add disease information/treatment suggestions
354
- - [ ] Implement persistent flagging system (database)
355
- - [ ] Add data visualization for batch results
356
- - [ ] Create comprehensive tests
357
-
358
- ## Resources
359
-
360
- - [Gradio Documentation](https://gradio.app/docs/)
361
- - [HuggingFace Spaces Guide](https://huggingface.co/docs/hub/spaces)
362
- - [ClearML Python API](https://clear.ml/docs/latest/docs/references/sdk/)
363
- - [PlantVillage Dataset](https://huggingface.co/datasets/EdBianchi/plant-village)
364
-
365
- ## License
366
-
367
- [Specify your license here]
368
-
369
- ## Acknowledgments
370
-
371
- - King's College London, 5CCSAGAP Course
372
- - PlantVillage Dataset creators
373
- - Course instructors and TAs
374
- =======
375
- ---
376
- title: SmallGroupProject
377
- emoji: 🏒
378
- colorFrom: pink
379
- colorTo: green
380
- sdk: gradio
381
- sdk_version: 5.49.1
382
- app_file: app.py
383
- pinned: false
384
- short_description: plant disease classifier
385
  base-model: develop
386
- ---
387
-
388
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
389
- >>>>>>> 28a307c67a9240ecbd9634ebcf90deb7de38d076
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: smallGroupProject
 
 
 
3
  sdk: gradio
 
4
  app_file: app.py
5
  pinned: false
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6
  base-model: develop
7
+ ---