Files
remote-sensing/__pycache__/train_module.cpython-310.pyc
T

83 lines
7.0 KiB
Plaintext
Raw Normal View History

o
î/<iw)ã
@dZddlZddlZddlZddlmZddl m
Z
ddl m Z m
Z
ddlmZddlZddlmZddlZddlZddlZddlmZdd lmZgd
¢d d d
dddddddddf
ddZdS)zy
Training module for land classification using Sentinel-2 and Sentinel-1 data
from Microsoft Planetary Computer STAC API
éN)Útrain_test_split)Ú LabelEncoder)Úclassification_reportÚconfusion_matrix)Ú
XGBClassifier)Údatetime)ÚClient)Úload)gffffffZ@gš™™™™™"@gÍÌÌÌÌŒZ@gš™™™™™#@z2023-03-01/2023-05-31é ééz1train/ST_training data_updated_1130points_new.shpédgš™™™™™¹?Tc
9
sdo‡fdd„ }
fdd}z·|
dur t ¡ d¡}d|d}
|
d d
ƒt d ¡}|ƒ|
d d
ƒ|jdg||dd|iid}t| ¡ƒ}|ƒt|ƒ|kr`t|ƒ|}|dd|d|}|
dt|ƒddƒ|
ddƒdd|Dƒ}t |gd¢d||t
j dd}|  ddddœ¡}|ƒ|
d d!ƒ|jd"g||d#}t| ¡ƒ}t|ƒ|kr¼t|ƒ|}|dd|d|}|
dt|ƒd$d%ƒ|
d&d'ƒd(d|Dƒ}t |d)d*gd||t
j dd}d
t
 |d) |d)d
k¡¡|d+<d
t
 |d* |d*d
k¡¡|d,<|ƒ|
d-d.ƒ|d|d|d|dd/}|d gd0¢¡}| |¡}|jd1d2}|
d3d4ƒt |¡}|jdkrI| d¡}d}d5D]
}||jvrY|}nqM|durjtd6t|jƒƒ|
d7d8ƒg}g} | ¡D]X\}!}"|"j}#|#j}$|#j}%|"|}&zA|j|$|%d9d:j}'|d,j|$|%d9d:jd1d2j}(|d+j|$|%d9d:jd1d2j})|'|(|)g}*t
 |*¡ ¡sÇ|  |*¡|   |&¡WqwYqwt
 !|¡}t
 !| ¡} |ƒ|
d;t|ƒd<d=ƒt"ƒ}+|+ #| ¡},t$||,d>d?|,d@\}-}.}/}0|
dAdBƒ| rdCndD}1t%||||1dEd?dFd
dG}2|2 &|-|/¡|
dHdIƒ|2 '|-|/¡}3|2 '|.|0¡}4|
dJdKƒt(j)t(j* +|
¡dLdMt, -|2|+dNœ|
¡idOt ¡ dPdQ“dRdd"gdSgdT¢dUt|-ƒdVt|.ƒdWt/|3ƒdXt/|4ƒdYdZ“d[|1“d\dE“d]|d^|d_|d`|da|db|}5|
 0ddc¡}6t|6ddƒ}7t1j-|5|7dedfWdƒn 1s©wY|
dgdhƒdL|
|6|3|4t|-ƒt|.ƒ|+j2 diœWSt4}8z|
djt5|8ƒdkƒdt5|8ƒdLdlœWYd}8~8Sd}8~8wt6y
}8z|
dmt5|8ƒdkƒdt5|8ƒdnœWYd}8~8Sd}8~8ww)pa}
Train a land classification model using Sentinel-2 and Sentinel-1 data
Args:
bbox: [min_lon, min_lat, max_lon, max_lat]
time_range: "YYYY-MM-DD/YYYY-MM-DD"
max_scenes: maximum number of scenes to load
cloud_cover: maximum cloud cover percentage
resolution: resolution in meters (e.g., 20)
training_shapefile: path to training shapefile
n_estimators: number of trees for XGBoost
max_depth: maximum tree depth
learning_rate: learning rate for XGBoost
use_gpu: whether to use GPU for training
output_model_path: path to save trained model (auto-generated if None)
status_callback: Optional callback function to report progress
cancel_check: Optional function that returns True if training should be cancelled
Returns:
Dictionary containing training results
Ncs:ˆrzˆ||ƒWn
tyˆ|ƒYnwt|ƒdS)zHelper to update statusN)Ú TypeErrorÚprint)ÚmessageÚprogress)Ústatus_callback©ú3/home/x79/CSIROBoeingPhase5-Vietnam/train_module.pyÚ
update_status=s  ÿ z"train_model.<locals>.update_statuscsˆr ˆƒr tdƒdSdS)z%Check if training should be cancelledzTraining cancelled by userN)ÚInterruptedErrorr)Ú cancel_checkrrÚcheck_cancellationGs
ÿz'train_model.<locals>.check_cancellationz
%Y%m%d_%H%M%Szmodel_train/model_xgboost_gpu_z.joblibz-Connecting to Microsoft Planetary Computer...rz3https://planetarycomputer.microsoft.com/api/stac/v1z"Searching for Sentinel-2 scenes...é
zsentinel-2-l2azeo:cloud_coverÚlt)Ú collectionsÚbboxrÚqueryzFound z Sentinel-2 scenesr zLoading Sentinel-2 data...écSóg|]}t |¡qSr©Úplanetary_computerÚsign©Ú.0ÚitemrrrÚ
<listcomp>lóztrain_model.<locals>.<listcomp>)ZB04ZB08ZSCLz
EPSG:32648F)ZbandsÚcrsÚ