# 🎯 Simplified Workflow: Server Loads, Local Processes ## Overview Workflow được Δ‘Ζ‘n giαΊ£n hΓ³a để **tΓ‘ch rΓ΅ trΓ‘ch nhiệm**: - **Server (Notebook 01):** Chỉ tαΊ£i dα»― liệu RAW tα»« S3, lΖ°u NetCDF - **Local (Notebook 02):** TαΊ₯t cαΊ£ xα»­ lΓ½ + training model ## Why This Design? ### Lợi Γ­ch: βœ… **Server:** TrΓ‘nh lΓ£ng phΓ­ tΓ i nguyΓͺn cho xα»­ lΓ½ β†’ TαΊ£i nhanh, lΖ°u ngay βœ… **Local:** Kiểm soΓ‘t toΓ n bα»™ quy trΓ¬nh β†’ Dα»… debug, dα»… thay Δ‘α»•i tham sα»‘ βœ… **TΓ‘ch biệt:** Server chỉ lo load, local chỉ lo xα»­ lΓ½ βœ… **Linh hoαΊ‘t:** CΓ³ thể reprocess dα»― liệu mΓ  khΓ΄ng cαΊ§n quay lαΊ‘i server ### So sΓ‘nh: **CΕ© (All on Server):** ``` Server: Load β†’ CloudMask β†’ NDVI β†’ Fill β†’ Aggregation β†’ Save β†’ Transfer Local: Unzip β†’ Train ``` β†’ Server bα»‹ quΓ‘ tαΊ£i, chαΊ­m **Mα»›i (Simplified):** ``` Server: Load β†’ Save RAW Local: Load β†’ CloudMask β†’ NDVI β†’ Fill β†’ Aggregation β†’ Train ``` β†’ Server chỉ lΓ m việc nαΊ·ng (loading), Local lΓ m việc nhanh (processing) --- ## Workflow Chi TiαΊΏt ### BΖ°α»›c 1: Server - TαΊ£i Data ThΓ΄ (Notebook 01) **Thời gian:** 10-20 phΓΊt **TΓ i nguyΓͺn:** Network (S3 download) **Output:** 2 file NetCDF thΓ΄ (~80-100 GB) ``` 01.prepare_data_on_server.ipynb β”œβ”€ Cell 1: Intro β”œβ”€ Cell 2: Setup Dask + Datacube + S3 β”œβ”€ Cell 3: Set coordinates β”œβ”€ Cell 4: Diagnostic (kiểm tra metadata) β”œβ”€ Cell 5: Load S2 (13 thΓ‘ng) ← Monthly chunking β”œβ”€ Cell 6: Load S1 (raw) β”œβ”€ Cell 7: Save 2 file NetCDF β”‚ - sentinel2_raw.nc (S2 thΓ΄) β”‚ - sentinel1_raw.nc (S1 thΓ΄) └─ Cell 8: Copy training shapefile + Close ``` **Output:** ``` data_for_training/ β”œβ”€ sentinel2_raw.nc (~50 GB) β”œβ”€ sentinel1_raw.nc (~30 GB) └─ train_data/ β”œβ”€ *.shp, *.shx, *.dbf (training points) ``` ### BΖ°α»›c 2: Local - TαΊ£i & Xα»­ LΓ½ Data (Notebook 02) **Thời gian:** 30-60 phΓΊt (CPU) hoαΊ·c 10-15 phΓΊt (GPU) **TΓ i nguyΓͺn:** CPU/GPU cα»§a mΓ‘y local **Output:** Trained PyTorch CNN model ``` 02.process_and_train_local.ipynb β”œβ”€ Cell 1: Import libraries β”œβ”€ Cell 2: Load NetCDF files β”œβ”€ Cell 3: Cloud masking ← Processing starts here β”œβ”€ Cell 4: Calculate NDVI β”œβ”€ Cell 5: Fill NaN (seasonal interpolation) β”œβ”€ Cell 6: Monthly aggregation β”œβ”€ Cell 7: Load training data & extract features β”œβ”€ Cell 8: Split train/val/test β”œβ”€ Cell 9: Train PyTorch CNN β”œβ”€ Cell 10: Evaluate on test set └─ Cell 11: Save model ``` **Output:** ``` model_cnn_pytorch_local.pth (Model weights) model_cnn_pytorch_local_checkpoint.pth (Full checkpoint) ``` ### BΖ°α»›c 3: Local - Dα»± BΓ‘o ToΓ n Bα»™ (Notebook 03) **Thời gian:** 5-10 phΓΊt **Input:** Trained model + processed data **Output:** Classification maps (SHP, TIF) ``` 03.predict_CNN_PyTorch_local.ipynb β”œβ”€ Load trained model β”œβ”€ Prepare full spatial data (cloud mask + NDVI + aggregation) β”œβ”€ Apply model to every pixel └─ Save as shapefile/GeoTIFF ``` --- ## File Structure ``` /home/x79/CSIROBoeingPhase5-Vietnam/ β”‚ β”œβ”€ 01.prepare_data_on_server.ipynb ← RUN ON SERVER β”‚ └─ Output: data_for_training/ (80-100 GB) β”‚ β”œβ”€ 02.process_and_train_local.ipynb ← RUN LOCALLY β”‚ β”œβ”€ Input: data_for_training/ (from server) β”‚ └─ Output: model_cnn_pytorch_local.pth β”‚ β”œβ”€ 03.predict_CNN_PyTorch_local.ipynb ← RUN LOCALLY β”‚ β”œβ”€ Input: model + processed data β”‚ └─ Output: prediction maps (SHP/TIF) β”‚ └─ new_import_ODC.py (helper functions) ``` --- ## Timeline & Resource Usage ### Server Timeline: ``` Time Action Duration CPU Memory Network ──────────────────────────────────────────────────────────────────────────── 00:00 Dask init 30 sec Low Moderate - 00:01 Set coordinates 1 sec - - - 00:02 Diagnostic check 30 sec Low Low High (query) 00:03 Load S2 monthly chunks (13Γ—) 12 min Moderate High High (download) 00:15 Load S1 2 min Moderate High High 00:17 Save NetCDF 3 min Low Moderate - 00:20 Copy training data 1 min - - - 00:21 DONE βœ… Total: ~80-100 GB saved ``` ### Local Timeline (CPU): ``` Time Action Duration ────────────────────────────────────────────────── 00:00 Load NetCDF 2 min 00:02 Cloud mask 3 min 00:05 NDVI + Fill 5 min 00:10 Aggregation 3 min 00:13 Load training data 1 min 00:14 Train CNN (50 epochs) 30-40 min 00:45 Evaluate 1 min 00:46 Save model 1 min 00:47 DONE βœ… Total: ~50 min ``` ### Local Timeline (GPU): ``` Same as above but: - Train CNN: 5-10 min instead of 30-40 min - Total: ~20-30 min ``` --- ## Data Flow Diagram ``` β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ SERVER (Notebook 01) β”‚ β”‚ ──────────────────── β”‚ β”‚ β”‚ β”‚ [AWS S3] β†’ [Datacube] β†’ [NetCDF] β†’ [Download] β”‚ β”‚ 396 scenes monthly 2 files 80-100 GB β”‚ β”‚ (Raw S2+S1) chunks raw data data_for_ β”‚ β”‚ (avoid OOM) training/ β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ⬇️ Transfer (SCP/FTP) β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ LOCAL MACHINE (Notebook 02) β”‚ β”‚ ────────────────────────────────── β”‚ β”‚ β”‚ β”‚ [NetCDF] β†’ [CloudMask] β†’ [NDVI] β†’ [FillNaN] β”‚ β”‚ raw data SCL band red/nir seasonal β”‚ β”‚ interp β”‚ β”‚ ⬇️ β”‚ β”‚ [Aggregation] β†’ [Train Data] β†’ [CNN Training] β”‚ β”‚ monthly extract PyTorch β”‚ β”‚ averages features 50 epochs β”‚ β”‚ β”‚ β”‚ ⬇️ β”‚ β”‚ [Trained Model (100 MB)] β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ⬇️ (Notebook 03) β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β” β”‚ LOCAL MACHINE (Notebook 03) β”‚ β”‚ ────────────────────────────────── β”‚ β”‚ β”‚ β”‚ [Trained Model] + [Aggregated Data] β†’ [Predict] β”‚ β”‚ 100 MB (monthly avg) All pixels β”‚ β”‚ β”‚ β”‚ ⬇️ β”‚ β”‚ [Classification Maps (SHP/TIF)] β”‚ β”‚ β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜ ``` --- ## Processing Parameters ### Notebook 01 (Server): ```python date_range = ("2022-09-01", "2023-10-01") longtitude_range = (105.5, 106.4) # ~90 km latitude_range = (9.2, 10.0) # ~90 km resolution = (-10, 10) # 10 m/pixel dask_chunks = {'x': 512, 'y': 512, 'time': 1} ``` ### Notebook 02 (Local): ```python # Cloud masking: Using SCL band # NDVI calculation: (NIR - Red) / (NIR + Red) # Fill NaN: Seasonal interpolation (4 seasons) # Aggregation: Monthly averages (13 months) # Training: epochs = 50 batch_size = 32 learning_rate = 0.001 patience = 10 (early stopping) split = 80% train, 10% val, 10% test ``` ### Notebook 03 (Local): ```python # Same processing as Notebook 02 # Apply model to every pixel # Output: Classification map (8 classes) ``` --- ## Data Quality Assurance **Server (Notebook 01):** - βœ… Diagnostic cell checks datacube metadata - βœ… Monthly loading prevents OOM - βœ… Error handling skips bad months - βœ… File size validation before download **Local (Notebook 02):** - βœ… Data shape validation after loading - βœ… NaN count reporting (before/after filling) - βœ… Training progress monitoring (val loss, accuracy) - βœ… Test accuracy + confusion matrix reporting **Local (Notebook 03):** - βœ… Prediction shape validation - βœ… Class distribution analysis - βœ… Output file size validation --- ## Troubleshooting ### Problem: "Server load too slow" β†’ Check S3 bandwidth, Dask workers status β†’ Reduce number of workers temporarily ### Problem: "Local processing uses too much RAM" β†’ Cloud mask operation: Reduce `dask_chunks` size β†’ NDVI calculation: Process month by month β†’ Training: Reduce batch size (32 β†’ 16) ### Problem: "Model accuracy too low" β†’ Check training data quality β†’ Verify cloud masking effectiveness β†’ Increase training epochs β†’ Use data augmentation in `new_import_ODC.py` ### Problem: "Prediction takes too long" β†’ Use GPU if available β†’ Batch predictions by month β†’ Reduce output resolution if needed --- ## Success Criteria ### Notebook 01 βœ… - [ ] All 13 months loaded with βœ“ marks - [ ] Data shape correct (~10,000 Γ— 10,000 pixels) - [ ] Memory usage 15-20 GB (not 403 TB!) - [ ] 2 NetCDF files saved (~80-100 GB) - [ ] Training shapefile copied ### Notebook 02 βœ… - [ ] NetCDF files loaded successfully - [ ] Cloud masking reduces NaN count - [ ] NDVI values in expected range [-0.5, 1.0] - [ ] Monthly aggregation produces 13 timesteps - [ ] Training completes without OOM - [ ] Test accuracy β‰₯ 0.70 (70%) - [ ] Model saved as .pth file ### Notebook 03 βœ… - [ ] Model loads successfully - [ ] Predictions on full extent complete - [ ] Classification map generated - [ ] All 8 classes represented - [ ] Output files saved (SHP/TIF) --- ## Advantages of This Design 1. **Resource Efficiency:** - Server: Only download/save (I/O bound) - Local: Only compute (CPU/GPU bound) 2. **Flexibility:** - Can reprocess locally without server - Can experiment with hyperparameters - Can apply to new regions easily 3. **Debugging:** - Local processing is much faster to iterate - Easy to visualize intermediate results - Can save intermediate results for inspection 4. **Scalability:** - Same pattern works for different regions - Can train multiple models in parallel locally - Server freed up for other tasks after initial load 5. **Reproducibility:** - All processing code on local machine - Easy to version control & document - Results fully reproducible --- ## Next Steps 1. βœ… Run Notebook 01 on server (10-20 min) 2. βœ… Download data to local machine (size: 80-100 GB) 3. βœ… Run Notebook 02 on local (30-60 min) 4. βœ… Run Notebook 03 on local (5-10 min) 5. βœ… Evaluate results **Total time:** ~1-2 hours (including transfer) --- **Created:** November 12, 2025 **Status:** βœ… SIMPLIFIED WORKFLOW COMPLETE **Design Pattern:** Server loads β†’ Local processes