mirror of
https://git.victorphan.net/basketballcantho/CSIROBoeingPhase5-Vietnam.git
synced 2026-08-05 13:43:11 +07:00
313 lines
8.2 KiB
Plaintext
313 lines
8.2 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "d2134b50",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Training CNN Model with PyTorch for Land Use Classification\n",
|
|
"Huấn luyện mô hình CNN với PyTorch cho phân loại sử dụng đất từ dữ liệu Sentinel-1 và Sentinel-2"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "5b9ca134",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%%time\n",
|
|
"%matplotlib inline\n",
|
|
"\n",
|
|
"import importlib\n",
|
|
"import new_import_ODC \n",
|
|
"\n",
|
|
"importlib.reload(new_import_ODC)\n",
|
|
"\n",
|
|
"from new_import_ODC import *"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "709c2518",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Kiểm tra GPU availability\n",
|
|
"print(f\"PyTorch version: {torch.__version__}\")\n",
|
|
"print(f\"CUDA available: {torch.cuda.is_available()}\")\n",
|
|
"if torch.cuda.is_available():\n",
|
|
" print(f\"CUDA device: {torch.cuda.get_device_name(0)}\")\n",
|
|
" device = 'cuda'\n",
|
|
"else:\n",
|
|
" print(\"Using CPU for training\")\n",
|
|
" device = 'cpu'\n",
|
|
"\n",
|
|
"print(f\"\\nDevice sẽ dùng: {device}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "15f073ca",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%%time\n",
|
|
"# Cấu hình Daskgateway\n",
|
|
"cluster, client = notebook_utils.initialize_dask(use_gateway=True, workers=(1, 10))\n",
|
|
"# Khai báo 1 Datacube là dc\n",
|
|
"dc = datacube.Datacube()\n",
|
|
"\n",
|
|
"# Cấu hình truy cập dịch vụ S3\n",
|
|
"configure_s3_access(aws_unsigned=False, requester_pays=True, client=client)\n",
|
|
"\n",
|
|
"client"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "74a229f6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## cấu hình thời gian lấy ảnh và tọa độ\n",
|
|
"date_range = (\"2022-09-01\", \"2023-10-01\")\n",
|
|
"longtitude_range = (105.5, 106.4)\n",
|
|
"latitude_range = (9.2, 10.0)\n",
|
|
"\n",
|
|
"coordinates = (longtitude_range, latitude_range)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "1ebdade4",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## truy vấn ảnh vệ tinh sen2\n",
|
|
"data = load_data(dc, date_range, longtitude_range, latitude_range)\n",
|
|
"notebook_utils.heading(notebook_utils.xarray_object_size(data))\n",
|
|
"display(data)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "c444f3cc",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%%time\n",
|
|
"# Tiến hành loại bỏ các vị trí bị mây ảnh hưởng\n",
|
|
"result = mask_clean(data)\n",
|
|
"progress(result)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "f0df6581",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Tiến hành tính toán NDVI\n",
|
|
"ds1 = calculate_indices(result, index=\"NDVI\", satellite_mission=\"s2\")\n",
|
|
"ndvi = ds1[\"NDVI\"]\n",
|
|
"display(ndvi)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "430302e3",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## Hiển thị ảnh NDVI chưa điền các giá trị mây (chưa fill nan)\n",
|
|
"plt.imshow(ndvi.isel(time=6))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "6798b0d2",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Thiết lập giá trị trung bình mùa vụ để xử lý các điểm ảnh bị mây dựa vào sự thay đổi theo mùa\n",
|
|
"time_split = [\n",
|
|
" slice(\"2022-09-01\", \"2023-01-01\"),\n",
|
|
" slice(\"2023-01-01\", \"2023-05-01\"),\n",
|
|
" slice(\"2023-05-01\", \"2023-07-01\"),\n",
|
|
" slice(\"2023-07-01\", \"2023-10-01\"),\n",
|
|
"]\n",
|
|
"\n",
|
|
"# Điền mây ở các vị trí mang giá trị nan (fill nan)\n",
|
|
"fill_nan_ndvi = fill_nan(ndvi, time_split)\n",
|
|
"\n",
|
|
"# In kết quả ảnh NDVI đã điền mây (đã fill nan)\n",
|
|
"plt.imshow(fill_nan_ndvi.isel(time=6))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "0e10d6e6",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%%time\n",
|
|
"## tính ndvi theo tháng\n",
|
|
"average_ndvi = fill_nan_ndvi.resample(time=\"1M\").mean().persist()\n",
|
|
"progress(average_ndvi)\n",
|
|
"\n",
|
|
"# compute average_ndvi\n",
|
|
"average_ndvi = average_ndvi.compute()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "7b3b7086",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"#Load dữ liệu ảnh Sentinel 1\n",
|
|
"dsvh, dsvv = load_data_sen1(dc, date_range, coordinates)\n",
|
|
"average_vv = calculate_average(dsvv, time_pattern='1M')\n",
|
|
"average_vh = calculate_average(dsvh, time_pattern='1M')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "6d3f9ab1",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## cấu hình bộ dữ liệu điểm huấn luyện mô hình (train file)\n",
|
|
"train_path = \"train/ST_training data_updated_1130points_new.shp\" # đường dẫn shp file train\n",
|
|
"\n",
|
|
"## load dữ liệu điểm huấn luyện mô hình (train file)\n",
|
|
"train = load_train_data(train_path)\n",
|
|
"train.head()\n",
|
|
"\n",
|
|
"# cấu hình nhãn dữ liệu \n",
|
|
"label_mapping = {\n",
|
|
" \"Lua tom\": \"0\",\n",
|
|
" \"Lua\": \"1\",\n",
|
|
" \"CHN\": \"2\",\n",
|
|
" \"CLN\": \"3\",\n",
|
|
" \"TS\": \"4\",\n",
|
|
" \"Song\": \"5\",\n",
|
|
" \"Dat xay dung\": \"6\",\n",
|
|
" \"Rung\": \"7\",\n",
|
|
"}\n",
|
|
"\n",
|
|
"# xây dựng tập dữ liệu (dataset) chứa dữ liệu VH, VV, NDVI\n",
|
|
"datasets = get_data_sen1_and_sen2(train, average_ndvi, average_vh, average_vv)\n",
|
|
"\n",
|
|
"# chia tập dữ liệu thành các phần theo tỉ lệ 80(80-20)-20 tương ứng với tập train, validate, test\n",
|
|
"X_train, X_val, X_test, y_train, y_val, y_test = split_train_data(\n",
|
|
" train, label_mapping, datasets\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"id": "c0188e1b",
|
|
"metadata": {},
|
|
"source": [
|
|
"## Huấn luyện mô hình CNN với PyTorch"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "ba38ce6a",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"%%time\n",
|
|
"# Huấn luyện mô hình CNN với PyTorch\n",
|
|
"print(\"🔷 Bắt đầu huấn luyện CNN model với PyTorch...\\n\")\n",
|
|
"\n",
|
|
"cnn_model, history, scaler = train_cnn_pytorch(\n",
|
|
" X_train, X_val, X_test, \n",
|
|
" y_train, y_val, y_test,\n",
|
|
" num_classes=8,\n",
|
|
" epochs=100,\n",
|
|
" batch_size=32,\n",
|
|
" learning_rate=1e-3,\n",
|
|
" device=device,\n",
|
|
" verbose=True\n",
|
|
")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "fd19df06",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Vẽ đồ thị huấn luyện\n",
|
|
"plot_pytorch_training_history(history)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "e16d8c20",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Lưu mô hình CNN PyTorch\n",
|
|
"save_pytorch_model(cnn_model, scaler, model_name=\"model_cnn_pytorch.pth\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "adb17862",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# Hiển thị model architecture\n",
|
|
"print(\"\\n📐 Model Architecture:\")\n",
|
|
"print(cnn_model)\n",
|
|
"\n",
|
|
"# Đếm số parameters\n",
|
|
"total_params = sum(p.numel() for p in cnn_model.parameters())\n",
|
|
"trainable_params = sum(p.numel() for p in cnn_model.parameters() if p.requires_grad)\n",
|
|
"print(f\"\\n📊 Model Parameters:\")\n",
|
|
"print(f\" Total parameters: {total_params:,}\")\n",
|
|
"print(f\" Trainable parameters: {trainable_params:,}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"id": "b4963d6f",
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# đóng client, cluster\n",
|
|
"client.close()\n",
|
|
"cluster.close()"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"language_info": {
|
|
"name": "python"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 5
|
|
}
|