#!/usr/bin/env python # coding: utf-8 # In[1]: get_ipython().run_cell_magic('time', '', '%matplotlib inline\n\nimport importlib\nimport new_import_ODC \n\nimportlib.reload(new_import_ODC)\n\nfrom new_import_ODC import *\n') # In[2]: get_ipython().run_cell_magic('time', '', '# Cấu hình Daskgateway\ncluster, client = notebook_utils.initialize_dask(use_gateway=True, workers=(1, 10))\n# Khai báo 1 Datacube là dc\ndc = None\n\n# Cấu hình truy cập dịch vụ S3\nconfigure_s3_access(aws_unsigned=False, requester_pays=True, client=client)\n\nclient\n') # In[3]: ## cấu hình thời gian lấy ảnh và tọa độ date_range = ("2022-09-01", "2022-10-01") longtitude_range = (105.86, 105.94) latitude_range = (9.65, 9.69) coordinates = (longtitude_range, latitude_range) # In[4]: ## truy vấn ảnh vệ tinh sen2 data = load_data(None, date_range, longtitude_range, latitude_range) notebook_utils.heading(notebook_utils.xarray_object_size(data)) display(data) # In[5]: get_ipython().run_cell_magic('time', '', '# Tiến hành loại bỏ các vị trí bị mây ảnh hưởng\nresult = mask_clean(data)\n# progress(result)\n') # In[6]: # Tiến hành tính toán NDVI ds1 = calculate_indices(result, index="NDVI", satellite_mission="s2") ndvi = ds1["NDVI"] display(ndvi) # In[7]: ## Hiển thị ảnh NDVI chưa điền các giá trị mây (chưa fill nan) plt.imshow(ndvi.isel(time=0)) # In[8]: # 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 time_split = [ slice("2022-09-01", "2023-01-01"), slice("2023-01-01", "2023-05-01"), slice("2023-05-01", "2023-07-01"), slice("2023-07-01", "2022-10-01"), ] # Điền mây ở các vị trí mang giá trị nan (fill nan) fill_nan_ndvi = fill_nan(ndvi, time_split) # In kết quả ảnh NDVI đã điền mây (đã fill nan) plt.imshow(fill_nan_ndvi.isel(time=0)) # In[9]: get_ipython().run_cell_magic('time', '', '## tính ndvi theo tháng\naverage_ndvi = fill_nan_ndvi.resample(time="1M").mean().persist()\n# progress(average_ndvi)\n\n# compute average_ndvi\naverage_ndvi = average_ndvi.compute()\n') # In[10]: #Load dữ liệu ảnh Sentinel 1 dsvh, dsvv = load_data_sen1(None, date_range, coordinates) average_vv = calculate_average(dsvv, time_pattern='1M') average_vh = calculate_average(dsvh, time_pattern='1M') # In[11]: ## cấu hình bộ dữ liệu điểm huấn luyện mô hình (train file) train_path = "train/ST_training_data_updated_1130points_new.shp" # đường dẫn shp file train ## load dữ liệu điểm huấn luyện mô hình (train file) train = load_train_data(train_path) train.head() # cấu hình nhãn dữ liệu label_mapping = { "Lua tom": "0", "Lua": "1", "CHN": "2", "CLN": "3", "TS": "4", "Song": "5", "Dat xay dung": "6", "Rung": "7", } # xây dựng tập dữ liệu (dataset) chứa dữ liệu VH, VV, NDVI datasets = get_data_sen1_and_sen2(train, average_ndvi, average_vh, average_vv) # 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 X_train, X_val, X_test, y_train, y_val, y_test = split_train_data( train, label_mapping, datasets ) # In[ ]: get_ipython().run_cell_magic('time', '', '# Import XGBoost\nimport xgboost as xgb\nfrom sklearn.metrics import accuracy_score\nimport numpy as np\n\n# Convert to numpy arrays\nX_train_np = np.asarray(X_train, dtype=np.float32)\nX_val_np = np.asarray(X_val, dtype=np.float32)\ny_train_np = np.asarray(y_train, dtype=np.int32)\ny_val_np = np.asarray(y_val, dtype=np.int32)\n\nprint("🚀 Training XGBoost model...")\nprint(f" Train samples: {len(X_train_np)}")\nprint(f" Val samples: {len(X_val_np)}")\nprint(f" Features: {X_train_np.shape[1]}")\nprint(f" Classes: 8\\n")\n\n# XGBoost parameters\nparams = {\n \'objective\': \'multi:softmax\', # Multi-class classification\n \'num_class\': 8, # 8 land use classes\n \'max_depth\': 6, # Maximum tree depth\n \'learning_rate\': 0.1, # Learning rate\n \'n_estimators\': 200, # Number of trees\n \'subsample\': 0.8, # Subsample ratio\n \'colsample_bytree\': 0.8, # Feature sampling ratio\n \'random_state\': 42,\n \'n_jobs\': -1, # Use all CPU cores\n \'eval_metric\': \'mlogloss\' # Multi-class log loss\n}\n\n# Train XGBoost model\nmodel = xgb.XGBClassifier(**params)\n\nmodel.fit(\n X_train_np, y_train_np,\n eval_set=[(X_train_np, y_train_np), (X_val_np, y_val_np)],\n verbose=True\n)\n\n# Validation accuracy\ny_val_pred = model.predict(X_val_np)\nval_accuracy = accuracy_score(y_val_np, y_val_pred)\nprint(f"\\n✅ Training completed!")\nprint(f" Validation Accuracy: {val_accuracy:.4f} ({val_accuracy*100:.2f}%)")\n') # In[ ]: get_ipython().run_cell_magic('time', '', '# Evaluate on test set\nX_test_np = np.asarray(X_test, dtype=np.float32)\ny_test_np = np.asarray(y_test, dtype=np.int32)\n\nprint("📊 Evaluating XGBoost model on test set...\\n")\n\n# Predictions\ny_pred_test = model.predict(X_test_np)\n\n# Metrics\nfrom sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, confusion_matrix\n\ntest_accuracy = accuracy_score(y_test_np, y_pred_test)\nprecision = precision_score(y_test_np, y_pred_test, average=\'weighted\', zero_division=0)\nrecall = recall_score(y_test_np, y_pred_test, average=\'weighted\', zero_division=0)\nf1 = f1_score(y_test_np, y_pred_test, average=\'weighted\', zero_division=0)\n\nprint(f"📈 Test Results:")\nprint(f" Accuracy: {test_accuracy:.4f} ({test_accuracy*100:.2f}%)")\nprint(f" Precision: {precision:.4f}")\nprint(f" Recall: {recall:.4f}")\nprint(f" F1-Score: {f1:.4f}\\n")\n\n# Confusion Matrix\nfrom sklearn.metrics import ConfusionMatrixDisplay\nimport matplotlib.pyplot as plt\n\n# Create figure first\nfig, ax = plt.subplots(figsize=(10, 8))\n\nclass_names = list(label_mapping.keys())\ncm = confusion_matrix(y_test_np, y_pred_test)\ndisp = ConfusionMatrixDisplay(confusion_matrix=cm, display_labels=class_names)\ndisp.plot(cmap=\'Blues\', ax=ax)\nplt.xticks(rotation=45, ha=\'right\')\nplt.title(\'XGBoost Confusion Matrix\')\nplt.tight_layout()\nplt.show()\n') # In[ ]: # Lưu mô hình huấn luyện import json import joblib # Save XGBoost model model_path = "model_xgboost.joblib" joblib.dump(model, model_path) print(f"✅ Model saved to {model_path}") # Save model info info = { "model_type": "XGBoost", "num_classes": 8, "classes": list(label_mapping.keys()), "num_features": X_train_np.shape[1], "params": params, "accuracy": float(test_accuracy), "precision": float(precision), "recall": float(recall), "f1_score": float(f1), } with open("model_xgboost_info.json", "w") as f: json.dump(info, f, indent=2) print(f"✅ Model info saved to model_xgboost_info.json") # In[15]: # đóng client, cluster # client.close() # cluster.close()