thêm chức năng train trên odc predict trên planetary

This commit is contained in:
Victor Phan
2026-03-04 23:26:06 +07:00
parent add7d16ecf
commit 609847767f
@@ -7,22 +7,27 @@
"source": [
"# 🌍 Decision Tree Land Classification - Planetary Computer\n",
"\n",
"## 📌 Notebook này có thể chạy trên:\n",
"- ✅ **Local machine** (không cần ODC database)\n",
"- ✅ **Server ODC/JupyterHub** (có ODC database)\n",
"## 📌 Notebook này chạy trên:\n",
"- ✅ **Server ODC/JupyterHub** với Dask Gateway\n",
"- ✅ Load dữ liệu từ **Microsoft Planetary Computer** (không cần ODC database)\n",
"\n",
"## 🎯 Nguồn dữ liệu:\n",
"**Microsoft Planetary Computer STAC API**\n",
"- Sentinel-2 L2A (optical)\n",
"- Sentinel-1 RTC (SAR)\n",
"\n",
"## 🚀 Infrastructure:\n",
"- **Dask Gateway**: Adaptive scaling (1-10 workers)\n",
"- **Datacube**: Initialized nhưng không dùng để load data\n",
"- **S3 Access**: Configured với requester_pays\n",
"\n",
"## 🔄 Workflow:\n",
"1. Load data từ Planetary Computer (STAC)\n",
"1. Load data từ Planetary Computer (STAC API)\n",
"2. Preprocessing (cloud mask, NDVI, resampling)\n",
"3. Train Decision Tree model\n",
"4. Evaluate & save model\n",
"\n",
"---"
"---\n"
]
},
{
@@ -39,6 +44,10 @@
"import os\n",
"sys.path.insert(0, '/media/x79/2A7D-FAA0/remote-sensing')\n",
"\n",
"# Import ODC modules for Dask Gateway and Datacube\n",
"import datacube\n",
"from deafrica_tools import notebook_utils\n",
"\n",
"# Import module load dữ liệu không cần ODC database\n",
"import importlib\n",
"import load_data_no_odc\n",
@@ -71,12 +80,9 @@
"import json\n",
"from datetime import datetime\n",
"\n",
"# Dask for parallel processing\n",
"from dask.distributed import Client, LocalCluster\n",
"\n",
"print(\"✅ All modules loaded successfully!\")\n",
"print(\"📡 Data source: Microsoft Planetary Computer\")\n",
"print(\"💻 Environment: Local or Remote compatible\")"
"print(\"🚀 Infrastructure: Dask Gateway + ODC\")\n"
]
},
{
@@ -84,9 +90,9 @@
"id": "53397b2f",
"metadata": {},
"source": [
"## 🚀 Step 1: Initialize Dask Cluster\n",
"## 🚀 Step 1: Initialize Dask Gateway + Datacube\n",
"\n",
"Khởi tạo Dask local cluster để xử lý song song"
"Khởi tạo Dask Gateway với adaptive scaling và cấu hình S3 access\n"
]
},
{
@@ -96,20 +102,24 @@
"metadata": {},
"outputs": [],
"source": [
"# Khởi tạo Dask LocalCluster\n",
"print(\"🚀 Initializing Dask LocalCluster...\")\n",
"%%time\n",
"\n",
"cluster = LocalCluster(\n",
" n_workers=4,\n",
" threads_per_worker=1,\n",
" memory_limit='4GB'\n",
")\n",
"client = Client(cluster)\n",
"print(\"🚀 Step 1: Dask Gateway + Datacube Initialization\")\n",
"print(\"=\" * 70)\n",
"\n",
"print(f\"✅ Dask cluster ready!\")\n",
"print(f\" Workers: {len(cluster.workers)}\")\n",
"print(f\" Dashboard: {client.dashboard_link}\")\n",
"print(\"=\" * 70)"
"# Cấu hình Dask Gateway\n",
"cluster, client = notebook_utils.initialize_dask(use_gateway=True, workers=(1, 10))\n",
"\n",
"# Khai báo Datacube\n",
"dc = datacube.Datacube()\n",
"\n",
"# Cấu hình truy cập dịch vụ S3\n",
"notebook_utils.configure_s3_access(aws_unsigned=False, requester_pays=True, client=client)\n",
"\n",
"print(f\"\\n✅ Dask Gateway + Datacube + S3 ready!\")\n",
"print(f\" Dask dashboard: {client.dashboard_link}\")\n",
"print(f\" Workers: Adaptive scaling (1-10)\")\n",
"print(\"=\" * 70)\n"
]
},
{
@@ -631,13 +641,13 @@
"metadata": {},
"outputs": [],
"source": [
"# Close Dask cluster\n",
"# Close Dask Gateway cluster\n",
"try:\n",
" client.close()\n",
" cluster.close()\n",
" print(\"✅ Dask cluster closed.\")\n",
" print(\"✅ Dask Gateway cluster closed.\")\n",
"except Exception as e:\n",
" print(f\"⚠ Error closing cluster: {e}\")"
" print(f\"⚠ Error closing cluster: {e}\")\n"
]
},
{
@@ -650,23 +660,42 @@
"## ✅ Summary\n",
"\n",
"### Notebook này:\n",
"- ✅ Load dữ liệu từ **Microsoft Planetary Computer**\n",
"- ✅ Không cần **ODC Database**\n",
"- ✅ Có thể chạy trên **local machine** hoặc **server ODC**\n",
"- ✅ Chạy trên **Server ODC/JupyterHub** với **Dask Gateway**\n",
"- ✅ Load dữ liệu từ **Microsoft Planetary Computer** qua STAC API\n",
"- ✅ Không cần **ODC Database** để load data (dùng Planetary Computer)\n",
"- ✅ Sử dụng infrastructure của ODC (Dask Gateway, S3 access)\n",
"- ✅ Tương thích 100% với dữ liệu ODC\n",
"\n",
"### Ưu điểm của approach này:\n",
"1. **Scalability**: Dask Gateway adaptive scaling (1-10 workers)\n",
"2. **Public data**: Planetary Computer không cần VPN/private network\n",
"3. **Consistent**: Cùng infrastructure với ODC training pipeline\n",
"4. **Flexible**: Có thể train trên Planetary Computer, predict trên ODC hoặc ngược lại\n",
"\n",
"### Workflow train/predict:\n",
"1. **Train trên server ODC**: Chạy notebook này với training data đầy đủ\n",
"2. **Save model**: Export `.joblib` file\n",
"3. **Predict trên local**: Load model và sử dụng cùng `load_data_no_odc.py` để load dữ liệu mới\n",
"1. **Train trên server ODC**: Chạy notebook này với Dask Gateway\n",
"2. **Save model**: Export `.joblib` file \n",
"3. **Predict qua API**: api_server.py tự động dùng Planetary Computer\n",
"\n",
"### Next steps:\n",
"1. Cập nhật `train_path` với đường dẫn training data thực tế\n",
"2. Uncomment các TODO cells\n",
"3. Run notebook để train model\n",
"4. Test prediction trên local machine\n",
"### Architecture:\n",
"```\n",
"┌─────────────────────────────────────┐\n",
"│ TRAINING (ODC Infrastructure) │\n",
"│ ✅ Dask Gateway (1-10 workers) │\n",
"│ ✅ Planetary Computer STAC API │\n",
"│ ✅ S3 access configured │\n",
"│ → Model: .joblib │\n",
"└─────────────────────────────────────┘\n",
" ↓\n",
"┌─────────────────────────────────────┐\n",
"│ PREDICTION (API Server) │\n",
"│ ✅ Planetary Computer (public) │\n",
"│ ✅ Same preprocessing pipeline │\n",
"│ → GeoTIFF output │\n",
"└─────────────────────────────────────┘\n",
"```\n",
"\n",
"---"
"---\n"
]
}
],