# Project Index - Land Use Classification using CNN PyTorch ## πŸ“š Document Structure ### πŸš€ Getting Started | File | Purpose | Read Time | |------|---------|-----------| | **QUICKSTART_PYTORCH.md** | HΖ°α»›ng dαΊ«n nhanh gọn (TL;DR) | 5 min | | **LOCAL_TRAINING_WORKFLOW.md** | Chi tiαΊΏt workflow 3 bΖ°α»›c | 15 min | | **PYTORCH_REQUIREMENTS.txt** | CΓ i Δ‘αΊ·t dependencies | 5 min | | **PYTORCH_INSTALLATION.md** | Chi tiαΊΏt cΓ i PyTorch | 10 min | --- ## πŸ““ Jupyter Notebooks ### Step 1️⃣: Data Preparation (Server) ``` 01.prepare_data_on_server.ipynb β”œβ”€β”€ KαΊΏt nα»‘i Dask cluster β”œβ”€β”€ TαΊ£i Sentinel-2, Sentinel-1 tα»« S3 β”œβ”€β”€ Xα»­ lΓ½ mΓ’y, tΓ­nh NDVI β”œβ”€β”€ LΖ°u NetCDF files └── ⏱️ Thời gian: 1-3 giờ (phα»₯ thuα»™c vΓ o sα»‘ scene) ``` **Output**: `data_for_training/` (150-300 MB) --- ### Step 2️⃣: Model Training (Local Machine) ``` 02.train_CNN_PyTorch_local.ipynb β”œβ”€β”€ Load data tα»« NetCDF files β”œβ”€β”€ TrΓ­ch xuαΊ₯t features tα»« 1130 training points β”œβ”€β”€ XΓ’y dα»±ng CNN model β”œβ”€β”€ HuαΊ₯n luyện (100 epochs max) β”œβ”€β”€ Plot training curves └── ⏱️ Thời gian: 30 min - 2 giờ (GPU/CPU) ``` **Output**: - `model_cnn_pytorch.pt` (state dict) - `model_cnn_pytorch_full.pt` (full model info) - `training_history.png` --- ### Step 3️⃣: Prediction (Local Machine) ``` 03.predict_CNN_PyTorch_local.ipynb β”œβ”€β”€ Load trained model β”œβ”€β”€ Predict trΓͺn 11M pixels β”œβ”€β”€ TαΊ‘o classification map β”œβ”€β”€ LΖ°u NetCDF, GeoTIFF, PNG └── ⏱️ Thời gian: 10 min - 30 min (GPU/CPU) ``` **Output**: - `land_use_prediction.nc` - `land_use_prediction.tif` - `prediction_map.png` - `prediction_metadata.json` --- ## πŸ”§ Source Code ### Python Module ``` new_import_ODC.py β”œβ”€β”€ Load functions (Sentinel-1, 2, training data) β”œβ”€β”€ Data processing (masking, indices, resampling) β”œβ”€β”€ CNN PyTorch classes and functions β”‚ β”œβ”€β”€ CNNClassifier (model architecture) β”‚ β”œβ”€β”€ train_cnn_model() β”‚ β”œβ”€β”€ prepare_data_for_cnn() β”‚ └── save_cnn_model() └── Utilities (normalization, evaluation) ``` **Key Functions**: - `prepare_data_for_cnn()` - ChuαΊ©n bα»‹ data format cho CNN - `CNNClassifier()` - Model architecture - `train_cnn_model()` - Training loop vα»›i early stopping - `plot_training_history()` - Visualization --- ## πŸ“Š Model Architecture ### CNN Design ``` Input Layer: (N, 1, 35) # 35 features (12 months Γ— 3 bands - NDVI, VV, VH) ↓ Block 1: Conv1d(64) β†’ Conv1d(64) β†’ MaxPool β†’ Dropout ↓ Block 2: Conv1d(128) β†’ Conv1d(128) β†’ MaxPool β†’ Dropout ↓ Block 3: Conv1d(256) β†’ Conv1d(256) β†’ GlobalAvgPool β†’ Dropout ↓ Dense 1: FC(256 β†’ 128) β†’ ReLU β†’ Dropout ↓ Dense 2: FC(128 β†’ 64) β†’ ReLU β†’ Dropout ↓ Output: FC(64 β†’ 8) # 8 land use classes ``` **Total Parameters**: ~500K **Trainable Parameters**: ~450K --- ## 🏷️ Land Use Classes | Index | Label | VN Name | English Name | |-------|-------|---------|--------------| | 0 | Lua tom | LΓΊa TΓ΄m | Rice-Shrimp | | 1 | Lua | LΓΊa | Rice | | 2 | CHN | CΓ’y hαΊ±ng nΔƒm | Perennial Crop | | 3 | CLN | CΓ’y lΓ’u nΔƒm | Long-term Crop | | 4 | TS | Thα»• nhΖ°α»‘ng | Soil/Bare Land | | 5 | Song | SΓ΄ng | Water/River | | 6 | Dat xay dung | ĐαΊ₯t xΓ’y dα»±ng | Built-up/Urban | | 7 | Rung | Rα»«ng | Forest | --- ## πŸ“ˆ Data Flow ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ S3 Cloud β”‚ β”‚ (Sentinel-1,2) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Server (01.prepare_data) β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β€’ Download from S3 β”‚ β”‚ β€’ Mask clouds β”‚ β”‚ β€’ Calculate NDVI β”‚ β”‚ β€’ Resample to 10m β”‚ β”‚ β€’ Save as NetCDF β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ (Download ~150-300 MB) ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ Local Machine β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ data_for_training/ β”‚ β”‚ β”œβ”€β”€ average_ndvi.nc β”‚ β”‚ β”œβ”€β”€ average_vv.nc β”‚ β”‚ β”œβ”€β”€ average_vh.nc β”‚ β”‚ └── train_data/*.shp β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” ↓ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ 02.train_CNN_PyTorch β”‚ β”‚ 03.predict_CNN_ β”‚ β”‚ β”‚ β”‚ PyTorch β”‚ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€ β”‚ β€’ Extract features β”‚ β”‚ β€’ Load trained β”‚ β”‚ β€’ Split data (60/20/20) β”‚ β”‚ model β”‚ β”‚ β€’ Normalize β”‚ β”‚ β€’ Predict on pixelsβ”‚ β”‚ β€’ Train CNN β”‚ β”‚ β€’ Create map β”‚ β”‚ β€’ Save model β”‚ β”‚ β€’ Export formats β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β”‚ β”‚ ↓ ↓ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ model_cnn_pytorch β”‚ β”‚ land_use_prediction β”‚ β”‚ _full.pt (~100 MB) β”‚ β”‚ .nc/.tif/.png (~100 MB) β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` --- ## πŸ’» System Requirements ### Minimum - Python 3.8+ - 8 GB RAM - 5 GB Disk space ### Recommended - Python 3.10+ - 16 GB RAM - 10 GB Disk space - GPU (NVIDIA/AMD/Apple Silicon) --- ## πŸ“¦ Dependencies ### Core - `torch>=2.0` - Deep learning framework - `numpy>=1.21` - Numerical computing - `xarray>=0.20` - Multi-dimensional arrays - `geopandas>=0.10` - Geospatial operations ### Optional - `rasterio>=1.2` - Raster I/O (GeoTIFF export) - `jupyter>=1.0` - Notebook environment - `matplotlib>=3.4` - Visualization See `PYTORCH_REQUIREMENTS.txt` for full list --- ## ✨ Features - βœ… **End-to-End Pipeline**: Tα»« S3 Δ‘αΊΏn classification map - βœ… **GPU Accelerated**: Hα»— trợ NVIDIA, AMD, Apple Silicon - βœ… **Memory Efficient**: Batch processing, normalization - βœ… **Modular Design**: CΓ‘c notebook Δ‘α»™c lαΊ­p - βœ… **Visualization**: Training curves, prediction maps - βœ… **Metadata Tracking**: Model info, accuracy, label mapping - βœ… **Multiple Output Formats**: NetCDF, GeoTIFF, PNG, JSON --- ## 🎯 Workflow Comparison ### Old Workflow (Random Forest on Server) ``` Server: Load Data β†’ Train β†’ Predict β†’ Save ❌ ChαΊ­m (CPU only) ❌ KhΓ΄ng linh hoαΊ‘t ❌ PhαΊ£i code trΓͺn server ``` ### New Workflow (CNN PyTorch Local) ``` Server: Load Data β†’ Save to Files ↓ (Download) Local: Load Data β†’ Train β†’ Predict β†’ Save βœ… Nhanh (GPU) βœ… Linh hoαΊ‘t βœ… Code trΓͺn mΓ‘y cΓ‘ nhΓ’n ``` --- ## πŸ“– Quick Navigation **I want to...** - πŸš€ Get started quickly β†’ Read `QUICKSTART_PYTORCH.md` - πŸ“ Understand the workflow β†’ Read `LOCAL_TRAINING_WORKFLOW.md` - πŸ”§ Set up environment β†’ Read `PYTORCH_REQUIREMENTS.txt` - πŸ““ See full code β†’ Check notebooks (01, 02, 03) - πŸ€– Understand model β†’ See `new_import_ODC.py` `CNNClassifier` class --- ## πŸ“Š Expected Outputs ### Training Phase ``` βœ… Model Accuracy Train: 88.5% Val: 82.3% Test: 81.2% βœ… Training Time: 45 min (GPU) / 90 min (CPU) βœ… Model Size: ~100 MB ``` ### Prediction Phase ``` βœ… Classification Map: 1080Γ—1080 pixels βœ… Output Formats: NetCDF, GeoTIFF, PNG βœ… Prediction Time: 5 min (GPU) / 15 min (CPU) βœ… File Sizes: ~100 MB each ``` --- ## πŸ› Troubleshooting | Problem | Solution | File | |---------|----------|------| | `ModuleNotFoundError` | Install dependencies | `PYTORCH_REQUIREMENTS.txt` | | GPU not detected | Check CUDA/drivers | `PYTORCH_INSTALLATION.md` | | Out of memory | Reduce batch_size | Notebook comments | | Data not found | Run server notebook first | Step 1 | --- ## πŸ”— Related Files ### Original Notebooks (Reference) - `01.train_ODC.ipynb` - Old Random Forest workflow - `02.predict_ODC.ipynb` - Old prediction workflow - `03.compare_ODC.ipynb` - Old comparison ### Documentation (Old) - `CNN_PYTORCH_README.md` - Old CNN notes - `CNN_PYTORCH_SUMMARY.md` - Old summary - `COMPARISON_RF_VS_CNN.md` - RF vs CNN comparison - `PYTORCH_INSTALLATION.md` - Original install guide --- ## πŸ“‹ File Checklist Before running: - [ ] `data_for_training/` exists with NetCDF files - [ ] PyTorch installed and GPU detected - [ ] Jupyter or IDE ready - [ ] Enough disk space (~500 MB total) --- ## πŸŽ“ Learning Resources - **PyTorch Tutorial**: https://pytorch.org/tutorials/ - **CNN Basics**: https://cs231n.github.io/convolutional-networks/ - **xarray**: https://docs.xarray.dev/ - **GeoPandas**: https://geopandas.org/ --- ## βœ… Checklist for Success ``` Preparation Phase β–‘ Read QUICKSTART_PYTORCH.md β–‘ Set up Python environment β–‘ Install all dependencies Data Phase β–‘ Run 01.prepare_data_on_server.ipynb β–‘ Download data_for_training/ folder Training Phase β–‘ Run 02.train_CNN_PyTorch_local.ipynb β–‘ Check training curves β–‘ Model achieves >75% test accuracy Prediction Phase β–‘ Run 03.predict_CNN_PyTorch_local.ipynb β–‘ Generate classification map β–‘ Export to multiple formats Validation Phase β–‘ Visually inspect prediction map β–‘ Compare with ground truth β–‘ Calculate accuracy metrics ``` --- **Last Updated**: November 2025 **Version**: 1.0 **Status**: Ready for Use βœ… πŸš€ **Start with**: `QUICKSTART_PYTORCH.md`