import json def get_content(filename): with open(filename, "r", encoding="utf-8") as f: return f.read() fe_content = get_content("feature_extractor.py") tm_content = get_content("train_module.py") dt_content = get_content("train_land_decision_tree_gpu.py") notebook = { "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Tải Dữ liệu Vệ tinh qua Colab (Self-contained)\n", "Notebook này đã được nhúng sẵn toàn bộ mã nguồn xử lý. Bạn không cần upload cả thư mục `remote-sensing` nữa.\n", "\n", "## Bước 1: Upload Shapefile (BẮT BUỘC)\n", "Mô hình cần biết các điểm tọa độ đất để lấy dữ liệu. Hãy nén thư mục `train/` trên máy bạn thành `train.zip` và chạy ô dưới đây để upload nó trực tiếp lên Colab." ] }, { "cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": [ "from google.colab import files\n", "import os\n", "\n", "print(\"Hãy chọn file train.zip từ máy tính của bạn:\")\n", "uploaded = files.upload()\n", "\n", "if \"train.zip\" in uploaded:\n", " !unzip -q -o train.zip -d /content/train_tmp/\n", " # Move the extracted files directly to /content/train/\n", " !mkdir -p /content/train\n", " !mv /content/train_tmp/*/* /content/train/ 2>/dev/null || mv /content/train_tmp/* /content/train/\n", " print(\"Đã giải nén shapefile thành công vào thư mục /content/train/\")\n", "else:\n", " print(\"LỖI: Bạn chưa upload file có tên là train.zip!\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bước 2: Cài đặt thư viện & Tạo môi trường" ] }, { "cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": [ "!pip install planetary-computer pystac-client odc-stac geopandas rasterio xarray joblib scikit-learn xgboost lightgbm" ] }, { "cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": [ "%%writefile feature_extractor.py\n" + fe_content ] }, { "cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": [ "%%writefile train_module.py\n" + tm_content ] }, { "cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": [ "%%writefile train_land_decision_tree_gpu.py\n" + dt_content ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bước 3: Chạy tiến trình tải ảnh vệ tinh và tạo Cache" ] }, { "cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": [ "!python train_land_decision_tree_gpu.py" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Bước 4: Tải file Cache về máy" ] }, { "cell_type": "code", "execution_count": None, "metadata": {}, "outputs": [], "source": [ "from google.colab import files\n", "import glob\n", "\n", "cache_files = glob.glob(\"dataset_cache/*.joblib\")\n", "if cache_files:\n", " latest_cache = max(cache_files, key=os.path.getctime)\n", " print(f\"Đang tải file {latest_cache} về máy...\")\n", " files.download(latest_cache)\n", "else:\n", " print(\"Chưa tìm thấy file cache. Hãy chắc chắn bước 3 đã chạy thành công!\")" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 4 } with open("Download_Cache_Colab.ipynb", "w", encoding="utf-8") as f: json.dump(notebook, f, indent=1, ensure_ascii=False) print("Notebook updated successfully!")