Files
remote-sensing/.temp/generate_colab_notebook.py
T

59 lines
2.7 KiB
Python

import nbformat as nbf
nb = nbf.v4.new_notebook()
text_1 = """# Script Tải Dữ liệu Vệ tinh (Cache) qua Google Colab
Mục đích của Notebook này là mượn sức mạnh đường truyền và RAM của Google Colab để tải 270 ảnh Sentinel-2 & Sentinel-1 từ Microsoft Planetary Computer. Sau khi xử lý nội suy, nó sẽ sinh ra một file cache `.joblib` duy nhất chứa toàn bộ mảng dữ liệu.
Bạn chỉ cần tải file `.joblib` đó về máy là xong!"""
code_1 = """!pip install planetary-computer pystac-client odc-stac geopandas rasterio xarray joblib scikit-learn xgboost lightgbm"""
code_2 = """from google.colab import drive
drive.mount('/content/drive')"""
text_2 = """## Hướng dẫn:
1. Nén toàn bộ thư mục `remote-sensing` ở máy tính của bạn thành file `remote-sensing.zip`.
2. Upload file `remote-sensing.zip` đó lên Google Drive (để ngay ngoài cùng).
3. Chạy ô lệnh bên dưới để giải nén và chuyển vào thư mục dự án."""
code_3 = """import os
import shutil
# Giải nén dự án từ Google Drive
!unzip -q /content/drive/MyDrive/remote-sensing.zip -d /content/
os.chdir('/content/remote-sensing')
!ls -la"""
text_3 = """## Bắt đầu tải và Cache Dữ Liệu
Chạy một mô hình CPU đơn giản (Decision Tree) để ép hệ thống gọi hàm `FeatureExtractor`. Hàm này sẽ làm mọi việc nặng nhọc: tìm ảnh, ghép mây, tính trung vị và lưu kết quả vào thư mục `dataset_cache/`."""
code_4 = """# Lệnh này sẽ mất khoảng 5-15 phút để tải toàn bộ ảnh từ Microsoft
!python train_land_decision_tree_gpu.py"""
text_4 = """## Hoàn tất
Bạn hãy kiểm tra xem file `.joblib` lớn (khoảng 40-60MB) đã xuất hiện chưa. Nếu rồi, hãy lưu ngược nó lại Google Drive để tải về máy!"""
code_5 = """# Xem file cache đã được tạo thành công chưa
!ls -lh dataset_cache/
# Copy toàn bộ thư mục cache sang Google Drive để tải về máy dễ dàng
!cp -r dataset_cache/ /content/drive/MyDrive/dataset_cache_finished/
print("Hoàn thành! Bạn hãy mở Google Drive của mình, tìm thư mục 'dataset_cache_finished' và tải file .joblib mới nhất về máy tính.")"""
nb['cells'] = [
nbf.v4.new_markdown_cell(text_1),
nbf.v4.new_code_cell(code_1),
nbf.v4.new_code_cell(code_2),
nbf.v4.new_markdown_cell(text_2),
nbf.v4.new_code_cell(code_3),
nbf.v4.new_markdown_cell(text_3),
nbf.v4.new_code_cell(code_4),
nbf.v4.new_markdown_cell(text_4),
nbf.v4.new_code_cell(code_5)
]
with open('Download_Cache_Colab.ipynb', 'w') as f:
nbf.write(nb, f)
print("Created Download_Cache_Colab.ipynb")