File size: 12,030 Bytes
0cbbc0f 0c42503 0cbbc0f 0c42503 0cbbc0f ab51159 a363339 ab51159 a363339 ab51159 a363339 ab51159 a363339 ab51159 7902c8d a363339 7902c8d a363339 7902c8d a363339 ab51159 a363339 ab51159 a363339 ab51159 4622aca ab51159 a363339 ab51159 a363339 ab51159 4622aca a363339 ab51159 a363339 7902c8d ab51159 a363339 7902c8d a363339 ab51159 7902c8d a363339 7902c8d a363339 | 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 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 | ---
title: Computer Vison | Image Classification
emoji: π€
colorFrom: blue
colorTo: purple
sdk: docker
app_port: 7860
pinned: false
---
# Intel Scene Classifier β Parfait TOLEFO
> CNN-based image classification Β· 6 scene categories Β· PyTorch & TensorFlow
---
## Table of Contents
- [Intel Scene Classifier β Parfait TOLEFO](#intel-scene-classifier--parfait-tolefo)
- [Table of Contents](#table-of-contents)
- [1. Project Overview](#1-project-overview)
- [2. Dataset](#2-dataset)
- [3. Project Architecture](#3-project-architecture)
- [4. Model Architecture](#4-model-architecture)
- [5. Dependencies \& Installation](#5-dependencies--installation)
- [6. Usage](#6-usage)
- [6.1 Training](#61-training)
- [6.2 Evaluation](#62-evaluation)
- [6.3 Web Application](#63-web-application)
- [7. Performance](#7-performance)
- [8. Preprocessing \& Augmentation](#8-preprocessing--augmentation)
- [Training augmentation pipeline (PyTorch)](#training-augmentation-pipeline-pytorch)
- [Validation / inference (no augmentation)](#validation--inference-no-augmentation)
- [Why ImageNet normalization?](#why-imagenet-normalization)
- [9. Reproducibility (Seed)](#9-reproducibility-seed)
- [10. Deployment](#10-deployment)
- [PythonAnywhere (recommended, free tier available)](#pythonanywhere-recommended-free-tier-available)
- [Railway / Render](#railway--render)
- [Environment variables](#environment-variables)
---
## 1. Project Overview
This project implements a **complete image classification pipeline** for the
Intel Image Classification dataset. It includes:
- Two independent CNN models: one in **PyTorch**, one in **TensorFlow/Keras**
- A unified CLI entry point (`main.py`) with `--mode train` and `--mode eval`
- A **Flask web application** with file upload and URL-based image loading
- A professional green/black UI with real-time probability bars
**Classes** (6 categories):
`buildings` Β· `forest` Β· `glacier` Β· `mountain` Β· `sea` Β· `street`
---
## 2. Dataset
| Property | Value |
|-------------|------------------------------------------------|
| Source | [Kaggle β Intel Image Classification](https://www.kaggle.com/datasets/puneet6060/intel-image-classification) |
| Images | ~25,000 RGB images (150Γ150 px) |
| Train split | ~14,000 images (seg_train) |
| Test split | ~3,000 images (seg_test) |
| Prediction | ~7,000 images (seg_pred β unlabeled) |
| Format | JPEG, organized in class-named subdirectories |
**Expected folder structure after download:**
```
data/
βββ seg_train/
β βββ seg_train/
β βββ buildings/
β βββ forest/
β βββ glacier/
β βββ mountain/
β βββ sea/
β βββ street/
βββ seg_test/
β βββ seg_test/
β βββ (same 6 subdirectories)
βββ seg_pred/
βββ seg_pred/
βββ (unlabeled images)
```
---
## 3. Project Architecture
```
project/
βββ app.py β Flask web server (inference via file or URL)
βββ main.py β Unified CLI: train + eval
βββ models/
β βββ __init__.py β Exports CNN_Torch, build_cnn_tf, Trainer
β βββ cnn.py β CNN architectures (PyTorch + TensorFlow)
β βββ train.py β Trainer class (PyTorch only)
βββ utils/
β βββ __init__.py β Exports all preprocessing functions
β βββ prep.py β Transforms, DataLoaders, inference preprocessing
βββ templates/
β βββ index.html β Web UI (green/black terminal aesthetic)
βββ parfait_model.pth β Trained PyTorch weights (after training)
βββ parfait_model.keras β Trained TensorFlow weights (after training)
βββ requirements.txt
βββ README.md
```
---
## 4. Model Architecture
### 4.1 TensorFlow / Keras model
```
Input: (228, 228, 3)
Block 1: Conv2D(32, 5Γ5, ReLU) β MaxPool(2Γ2) β 224Γ224Γ32 β 112Γ112Γ32
Block 2: Conv2D(32, 5Γ5, ReLU) β MaxPool(2Γ2) β 108Γ108Γ32 β 54Γ54Γ32
Block 3: Conv2D(32, 3Γ3, ReLU) β MaxPool(2Γ2) β 52Γ52Γ32 β 26Γ26Γ32
Block 4: Conv2D(64, 3Γ3, ReLU) β MaxPool(2Γ2) β 24Γ24Γ64 β 12Γ12Γ64
Block 5: Conv2D(64, 3Γ3, ReLU) β MaxPool(2Γ2) β 10Γ10Γ64 β 5Γ5Γ64
Flatten β 1600
Dense(1024, ReLU)
Dropout(0.20)
Dense(124, ReLU)
Dropout(0.20)
Dense(6, Softmax)
Trainable parameters : 1,86M
Input size : 228 Γ 228 Γ 3 (RGB)
```
### 4.1 PyTorch model
```
Input: (B, 3, 150, 150)
Block 1:
Conv2d(3 β 32, 3Γ3, padding=1)
BatchNorm2d(32)
ReLU
Conv2d(32 β 32, 3Γ3, padding=1)
BatchNorm2d(32)
ReLU
MaxPool2d(2)
Block 2:
Conv2d(32 β 64, 3Γ3, padding=1)
BatchNorm2d(64)
ReLU
Conv2d(64 β 64, 3Γ3, padding=1)
BatchNorm2d(64)
ReLU
MaxPool2d(2)
Dropout2d(0.10)
Block 3:
Conv2d(64 β 128, 3Γ3, padding=1)
BatchNorm2d(128)
ReLU
Conv2d(128 β 128, 3Γ3, padding=1)
BatchNorm2d(128)
ReLU
MaxPool2d(2)
Dropout2d(0.15)
Block 4:
Conv2d(128 β 256, 3Γ3, padding=1)
BatchNorm2d(256)
ReLU
Conv2d(256 β 256, 3Γ3, padding=1)
BatchNorm2d(256)
ReLU
MaxPool2d(2)
Dropout2d(0.20)
AdaptiveAvgPool2d(1) β (B, 256, 1, 1)
Flatten β (B, 256)
Linear(256 β 256)
ReLU
Dropout(0.30)
Linear(256 β 6)
Trainable parameters : 1.24M
Input size : 150 Γ 150 Γ 3 (RGB)
```
**Training configuration:**
| Parameter | Value |
|---------------|---------------|
| Optimizer | Adam |
| Learning rate | 1e-4 |
| LR scheduler | ReduceLROnPlateau (factor=0.5, patience=3) |
| Early stopping | patience=5 |
| Batch size | 32 |
| Max epochs | 50 |
| Loss function | CrossEntropyLoss / SparseCategoricalCrossentropy |
---
## 5. Dependencies & Installation
**Python 3.9+** is required.
```bash
# Install dependencies
pip install -r requirements.txt
```
**requirements.txt:**
```
torch>=2.0.0
torchvision>=0.15.0
tensorflow>=2.13.0
flask>=3.0.0
pillow>=10.0.0
numpy>=1.24.0
matplotlib>=3.7.0
tqdm>=4.65.0
scikit-learn>=1.3.0
gunicorn>=21.0.0
```
---
## 6. Usage
### 6.1 Training
```bash
# Train with PyTorch (saves β parfait_model.pth)
python main.py --model pytorch --mode train
# Train with TensorFlow (saves β parfait_model.keras)
python main.py --model tensorflow --mode train
# Full example with all options
python main.py \
--model pytorch \
--mode train \
--data_dir ./data \
--output_dir ./outputs \
--epochs 50 \
--batch_size 32 \
--lr 1e-4 \
--patience 15
```
**All CLI arguments:**
| Argument | Default | Description |
|---------------|----------------------------------------|------------------------------------|
| `--model` | *(required)* | `pytorch` or `tensorflow` |
| `--mode` | *(required)* | `train` or `eval` |
| `--data_dir` | `/kaggle/input/.../intel-image-...` | Root directory of the dataset |
| `--output_dir` | `/kaggle/working` | Where to save models and plots |
| `--epochs` | `50` | Max training epochs |
| `--batch_size` | `32` | Batch size |
| `--lr` | `1e-4` | Initial learning rate |
| `--patience` | `15` | Early stopping patience |
| `--model_path` | *(auto)* | (eval only) Path to .pth or .keras |
**Training outputs:**
```
outputs/
βββ parfait_model.pth β Best PyTorch weights
βββ parfait_model.keras β Best TensorFlow weights
βββ history_pytorch.png β Train/Val Loss & Accuracy curves
βββ history_tf.png
```
---
### 6.2 Evaluation
The `eval` mode loads a saved model and produces a **full diagnostic report**:
- Global accuracy & loss
- Per-class accuracy
- Precision / Recall / F1-score (classification report)
- Confusion matrix (saved as PNG)
- 4Γ4 grid of sample predictions (color-coded: green=correct, red=wrong)
```bash
# Evaluate PyTorch model
python main.py \
--model pytorch \
--mode eval \
--model_path parfait_model.pth \
--data_dir ../data \
--output_dir ./outputs
# Evaluate TensorFlow model
python main.py \
--model tensorflow \
--mode eval \
--model_path parfait_model.keras \
--data_dir ../data \
--output_dir ./outputs
```
**Evaluation outputs:**
```
outputs/
βββ confusion_matrix_pytorch.png β Confusion matrix heatmap
βββ confusion_matrix_tf.png
βββ sample_predictions_pytorch.png β 16-image prediction grid
βββ sample_predictions_tf.png
```
---
### 6.3 Web Application
```bash
# Start Flask server
gunicorn app:app --bind 0.0.0.0:8000 --workers 1 --timeout 120
```
# Live link
For instance the app is available at: https://huggingface.co/spaces/CyberAl/Image_Classification_Parfait_TOLEFO
**Features:**
- Model selector: **PyTorch** or **TensorFlow**
- Input: **file upload** (drag & drop) or **image URL**
- Output: predicted class + confidence score + probability bars for all 6 classes
- Animated plexus background with terminal green/black aesthetic
---
## 7. Performance
> Results on the Intel Image Classification **test set** (3,000 images).
> Reported after training with default hyperparameters on Kaggle GPU T4.
| Model | Test Accuracy | Test Loss |
|-------------|:------------:|:---------:|
| PyTorch CNN | ~89β91% | ~0.30 |
| TF/Keras CNN| ~88β90% | ~0.32 |
**Per-class performance (approximate):**
| Class | Precision | Recall | F1-score |
|-----------|:---------:|:------:|:--------:|
| buildings | 0.87 | 0.85 | 0.86 |
| forest | 0.97 | 0.97 | 0.97 |
| glacier | 0.88 | 0.86 | 0.87 |
| mountain | 0.84 | 0.87 | 0.85 |
| sea | 0.92 | 0.93 | 0.92 |
| street | 0.90 | 0.91 | 0.90 |
> Note: `buildings` vs `street` is the hardest pair due to visual overlap.
---
## 8. Preprocessing & Augmentation
All preprocessing is centralized in `utils/prep.py`.
### Training augmentation pipeline (PyTorch)
```
Resize(150Γ150)
RandomHorizontalFlip(p=0.5)
RandomVerticalFlip(p=0.1)
RandomRotation(Β±40Β°)
ColorJitter(brightness=0.3, contrast=0.2, saturation=0.1, hue=0.05)
RandomGrayscale(p=0.05) β forces texture learning over color
ToTensor()
Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225]) β ImageNet stats
RandomErasing(p=0.15, scale=[0.02,0.15]) β occlusion simulation
```
### Validation / inference (no augmentation)
```
Resize(150Γ150)
ToTensor()
Normalize(mean=[0.485,0.456,0.406], std=[0.229,0.224,0.225])
```
### Why ImageNet normalization?
The dataset consists of natural outdoor scenes (RGB, 3-channel images similar
to ImageNet). Using ImageNet mean/std ensures stable gradient flow and
faster convergence even for a custom-trained CNN.
---
## 9. Reproducibility (Seed)
The project uses a **global seed** (`SEED=42`) to ensure identical results
between runs and between training and production inference.
The seed fixes:
- Python `random` module
- NumPy RNG
- PyTorch CPU and GPU (`torch.manual_seed`, `torch.cuda.manual_seed_all`)
- `cudnn.deterministic=True`, `cudnn.benchmark=False`
- TensorFlow RNG (`tf.random.set_seed`)
- `PYTHONHASHSEED` environment variable
- DataLoader worker seeds (via `worker_init_fn`)
---
|