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": [ "source": [
"# 🌍 Decision Tree Land Classification - Planetary Computer\n", "# 🌍 Decision Tree Land Classification - Planetary Computer\n",
"\n", "\n",
"## 📌 Notebook này có thể chạy trên:\n", "## 📌 Notebook này chạy trên:\n",
"- ✅ **Local machine** (không cần ODC database)\n", "- ✅ **Server ODC/JupyterHub** với Dask Gateway\n",
"- ✅ **Server ODC/JupyterHub** (có ODC database)\n", "- ✅ Load dữ liệu từ **Microsoft Planetary Computer** (không cần ODC database)\n",
"\n", "\n",
"## 🎯 Nguồn dữ liệu:\n", "## 🎯 Nguồn dữ liệu:\n",
"**Microsoft Planetary Computer STAC API**\n", "**Microsoft Planetary Computer STAC API**\n",
"- Sentinel-2 L2A (optical)\n", "- Sentinel-2 L2A (optical)\n",
"- Sentinel-1 RTC (SAR)\n", "- Sentinel-1 RTC (SAR)\n",
"\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", "## 🔄 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", "2. Preprocessing (cloud mask, NDVI, resampling)\n",
"3. Train Decision Tree model\n", "3. Train Decision Tree model\n",
"4. Evaluate & save model\n", "4. Evaluate & save model\n",
"\n", "\n",
"---" "---\n"
] ]
}, },
{ {
@@ -39,6 +44,10 @@
"import os\n", "import os\n",
"sys.path.insert(0, '/media/x79/2A7D-FAA0/remote-sensing')\n", "sys.path.insert(0, '/media/x79/2A7D-FAA0/remote-sensing')\n",
"\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 module load dữ liệu không cần ODC database\n",
"import importlib\n", "import importlib\n",
"import load_data_no_odc\n", "import load_data_no_odc\n",
@@ -71,12 +80,9 @@
"import json\n", "import json\n",
"from datetime import datetime\n", "from datetime import datetime\n",
"\n", "\n",
"# Dask for parallel processing\n",
"from dask.distributed import Client, LocalCluster\n",
"\n",
"print(\"✅ All modules loaded successfully!\")\n", "print(\"✅ All modules loaded successfully!\")\n",
"print(\"📡 Data source: Microsoft Planetary Computer\")\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", "id": "53397b2f",
"metadata": {}, "metadata": {},
"source": [ "source": [
"## 🚀 Step 1: Initialize Dask Cluster\n", "## 🚀 Step 1: Initialize Dask Gateway + Datacube\n",
"\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": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Khởi tạo Dask LocalCluster\n", "%%time\n",
"print(\"🚀 Initializing Dask LocalCluster...\")\n",
"\n", "\n",
"cluster = LocalCluster(\n", "print(\"🚀 Step 1: Dask Gateway + Datacube Initialization\")\n",
" n_workers=4,\n", "print(\"=\" * 70)\n",
" threads_per_worker=1,\n",
" memory_limit='4GB'\n",
")\n",
"client = Client(cluster)\n",
"\n", "\n",
"print(f\"✅ Dask cluster ready!\")\n", "# Cấu hình Dask Gateway\n",
"print(f\" Workers: {len(cluster.workers)}\")\n", "cluster, client = notebook_utils.initialize_dask(use_gateway=True, workers=(1, 10))\n",
"print(f\" Dashboard: {client.dashboard_link}\")\n", "\n",
"print(\"=\" * 70)" "# 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": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
"# Close Dask cluster\n", "# Close Dask Gateway cluster\n",
"try:\n", "try:\n",
" client.close()\n", " client.close()\n",
" cluster.close()\n", " cluster.close()\n",
" print(\"✅ Dask cluster closed.\")\n", " print(\"✅ Dask Gateway cluster closed.\")\n",
"except Exception as e:\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", "## ✅ Summary\n",
"\n", "\n",
"### Notebook này:\n", "### Notebook này:\n",
"- ✅ Load dữ liệu từ **Microsoft Planetary Computer**\n", "- ✅ Chạy trên **Server ODC/JupyterHub** với **Dask Gateway**\n",
"- ✅ Không cần **ODC Database**\n", "- ✅ Load dữ liệu từ **Microsoft Planetary Computer** qua STAC API\n",
"- ✅ Có thể chạy trên **local machine** hoặc **server ODC**\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", "- ✅ Tương thích 100% với dữ liệu ODC\n",
"\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", "### Workflow train/predict:\n",
"1. **Train trên server ODC**: Chạy notebook này với training data đầy đủ\n", "1. **Train trên server ODC**: Chạy notebook này với Dask Gateway\n",
"2. **Save model**: Export `.joblib` file\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", "3. **Predict qua API**: api_server.py tự động dùng Planetary Computer\n",
"\n", "\n",
"### Next steps:\n", "### Architecture:\n",
"1. Cập nhật `train_path` với đường dẫn training data thực tế\n", "```\n",
"2. Uncomment các TODO cells\n", "┌─────────────────────────────────────┐\n",
"3. Run notebook để train model\n", "│ TRAINING (ODC Infrastructure) │\n",
"4. Test prediction trên local machine\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",
"---" "---\n"
] ]
} }
], ],