Spaces:
Sleeping

File size: 6,611 Bytes
fb14bd4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94ce1f9
 
fb14bd4
94ce1f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
title: ScratchNet Flower Classifier
emoji: πŸͺ·
colorFrom: green
colorTo: pink
sdk: gradio
app_file: gradio_app.py
pinned: false
---

# πŸͺ· ScratchNet β€” 7-Class Flower Image Classifier & Web App

An end-to-end Deep Learning computer vision project that classifies flower images across **7 distinct botanical categories** using a custom PyTorch Convolutional Neural Network (CNN). Includes an interactive, modern web application (FastAPI), a command-line interface (CLI), a Gradio interface, and a REST API.

---

## 🌺 Detected Flower Categories
The trained model (`flower_classifier.pth`) detects the following 7 flower species:
1. **Daisy** (*Bellis perennis*) β€” 🌼
2. **Dandelion** (*Taraxacum officinale*) β€” 🦁
3. **Rose** (*Rosa*) β€” 🌹
4. **Sunflower** (*Helianthus annuus*) β€” 🌻
5. **Tulip** (*Tulipa*) β€” 🌷
6. **Bellflower** (*Campanula*) β€” πŸ””
7. **Lotus** (*Nelumbo nucifera*) β€” πŸͺ·

---

## πŸš€ How to Run & Use the Model

### 1. Interactive Web Application (Recommended)
Launch the modern web UI with drag-and-drop file upload, live image preview, verified 1-click flower samples, real-time confidence scores, and animated probability spectrums:

```bash
# Start the FastAPI server (auto-detects port and opens browser)
py app.py

# Or on local network / Wi-Fi to test from your phone:
py app.py --host 0.0.0.0 --port 8000
```
> **Windows Quick Launch**: You can also just double-click `run_app.bat`!

### 2. Command-Line Interface (CLI)
Classify any image directly from your terminal:

```bash
py predict.py "test_images/download (3).jpg"
# Output:
# πŸ”” Top Prediction: BELLFLOWER (100.0%)
# Botanical Name: Campanula
# Hardware:       CUDA
```

### 3. Programmatic Python API
Import the model into your own Python code or Jupyter notebook:

```python
from model import predict_flower

# Run inference on any image path or PIL image
result = predict_flower("my_flower.jpg")

print(f"Prediction: {result['top_flower']} ({result['confidence_pct']})")
print("Top Probabilities:", result['probabilities'])
```

### 4. REST API Endpoint
Send image files via HTTP POST from any client (iOS, Android, React, cURL):

```bash
curl -X POST "http://127.0.0.1:8000/api/predict" \
  -F "file=@test_images/download.jpg"
```

**Response Format**:
```json
{
  "success": true,
  "top_flower": "lotus",
  "display_name": "Lotus",
  "confidence": 0.605,
  "confidence_pct": "60.5%",
  "ranked_list": [
    {
      "name": "lotus",
      "display_name": "Lotus",
      "probability": 0.605,
      "percentage": "60.5%",
      "color": "#06b6d4",
      "icon": "πŸͺ·"
    },
    { "name": "tulip", "percentage": "36.4%", ... }
  ],
  "flower_info": {
    "scientific_name": "Nelumbo nucifera",
    "family": "Nelumbonaceae",
    "icon": "πŸͺ·",
    "season": "Summer"
  },
  "device": "cuda"
}
```

### 5. Hugging Face Spaces (Free Global Cloud Deployment)
Run the included Gradio interface or upload to Hugging Face:

```bash
py gradio_app.py
```
To deploy to the world for free:
1. Create a free account on [Hugging Face](https://huggingface.co/).
2. Create a new Space (choose **Gradio** SDK).
3. Upload `gradio_app.py`, `model.py`, and `flower_classifier.pth`.
4. Anyone on the internet can now upload photos and test your model!

---

## πŸ—οΈ Neural Network Architecture

```text
Input Image (150 Γ— 150 Γ— 3 RGB) [Normalized / 255.0]
  β”‚
  β”œβ”€β”€ [Conv Block 1]: Conv2d(3 -> 32, 3x3) + BatchNorm2d + ReLU + MaxPool2d(2x2)  -> (32, 75, 75)
  β”œβ”€β”€ [Conv Block 2]: Conv2d(32 -> 64, 3x3) + BatchNorm2d + ReLU + MaxPool2d(2x2) -> (64, 37, 37)
  β”œβ”€β”€ [Conv Block 3]: Conv2d(64 -> 128, 3x3) + BatchNorm2d + ReLU + MaxPool2d(2x2)-> (128, 18, 18)
  β”‚
  β”œβ”€β”€ Flatten -> 41,472 features
  β”œβ”€β”€ Linear(41,472 -> 128) + ReLU + Dropout(0.3)
  β”œβ”€β”€ Linear(128 -> 64) + ReLU
  β”œβ”€β”€ Linear(64 -> 32) + ReLU
  └── Output Linear(32 -> 7) -> Softmax Probabilities (7 Classes)
```

- **Total Parameters**: 5,412,807
- **Test Accuracy**: ~79% on 2,240 test samples
- **Training Epochs**: 20 epochs with Adam (`lr=0.001`) & CrossEntropyLoss
- **Hardware Acceleration**: CUDA RTX 5050 GPU (with automatic CPU fallback)

---

## πŸ”¬ Why Switch from an MLP to a CNN? (Engineering Post-Mortem)

After training our two-layer NumPy Multilayer Perceptron (MLP) for 7,000 iterations and achieving ~90% training accuracy, empirical evaluation on unseen test images exposed a severe generalization bottleneck: real-world accuracy dropped to **40% (4/10)**.

### 1. The Core Limitation: Spatial Oblivion
* **Flattening Destroys Geometry**: An MLP flattens a $150 \times 150 \times 3$ RGB image into a flat vector of $67,500$ independent numbers. The network has no geometric awareness that pixel $(x, y)$ is physically adjacent to pixel $(x+1, y)$.
* **Shortcut Memorization**: The model relied on global color histograms and background pixels rather than flower anatomy. For instance, a dandelion in front of a blue sky was classified as a **Bellflower with 99.0% confidence** because the model associated high blue pixel values at the top coordinates with bellflower backgrounds.
* **Lack of Translation Invariance**: If a flower shifts 10 pixels to the right, every input coordinate shifts to entirely different weight connections in an MLP.

### 2. The Architectural Solution: Convolutional Neural Networks (CNNs)
A CNN decouples the task into two sequential stages:
1. **Feature Extraction (The Visual Cortex)**:
   * **Convolutional Layers (`Conv2d`)**: Slide small $3 \times 3$ parameter-sharing kernels across the spatial grid to detect local features (edges, curves, textures) regardless of where they appear in the frame.
   * **Max Pooling (`MaxPool2d`)**: Progressively downsamples spatial dimensions ($150 \to 75 \to 37 \to 18$), shedding redundant pixel noise, preserving only the strongest feature activations, and enforcing translation invariance.
2. **Classification (The Decision Maker)**:
   * Only after the image is compressed into high-level, meaningful feature representations (e.g., "radiating petal patterns") does a dense linear layer classify the flower.

### 3. Transitioning to PyTorch
While implementing an MLP in pure NumPy provided deep mastery of forward/backward propagation and gradient descent mechanics, building a CNN from scratch in NumPy is computationally impractical on CPU. PyTorch provides:
* **CUDA Hardware Acceleration**: Offloading 2D tensor operations to the RTX 5050 GPU (8 GB VRAM).
* **Dynamic Autograd**: Eliminating manual chain-rule calculus for complex layers like 2D convolutions and Batch Normalization.