module deeplearning¶
function processing¶
1 2 3 4 5 6 7 8 9 10 11 12 | |
Reads raster and labels, splits them into patches with optional overlap and padding.
Args:
raster_path(str): Path to raster filelabel_path(str, optional): Path to label rasterpatch_size(int): Patch sizeexport_patches(bool): Whether to export image patchesoutput_dir(str, optional): Directory to export image patchesexport_labels(bool): Whether to export label patcheslabels_output_dir(str, optional): Directory to export label patchespadding_mode(str): 'constant' or 'reflect'padding_value: Value for constant paddingoverlap(int): Number of pixels of overlap between patches
Returns:
tuple: (X_patches, y_patches) or X_patches if no labels are provided
function make_mean_iou_metric¶
1 | |
Create a stable Keras metric named 'mean_iou' to allow monitoring 'val_mean_iou'.
function trainUnet¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | |
Train a U-Net model for binary or multiclass semantic segmentation.
Parameters
---------- X_train : np.ndarray Training feature patches of shape (N, H, W, C). y_train : np.ndarray Training labels of shape (N, H, W, 1) for binary or (N, H, W) with integer class labels for multiclass. input_shape : tuple Shape of input tensors (H, W, C). num_classes : int Number of output classes. Use 1 for binary segmentation. dropout_rate : float, optional Dropout rate for regularization (default=0.2). learning_rate : float, optional Learning rate for Adam optimizer (default=1e-4). batch_size : int, optional Number of samples per gradient update (default=16). epochs : int, optional Number of training epochs (default=50). validation_data : tuple, optional Tuple (X_val, y_val) used for validation. If None, a split from training data can be created with validation_split. validation_split : float, optional Fraction of training data reserved for validation if validation_data is not provided (default=0.0). data_augmentation : bool, optional If True, applies random flips and rotations for augmentation (default=False). normalize : bool, optional If True, applies per-band normalization using StandardScaler (default=True). fill_nulls : bool, optional If True, replaces NaN values with null_value (default=True). null_value : float, optional Value used to replace NaN values (default=0.0). save_best_model : bool, optional If True, saves the best model according to validation IoU (default=True). model_path : str, optional File path to save the best model (default="best_model.keras").
Returns ------- model : tensorflow.keras.Model The trained U-Net model. history : keras.callbacks.History Training history with loss and metric evolution.
function predictRaster¶
1 2 3 4 5 6 7 8 9 10 11 | |
Predicts over a complete raster file and reconstructs the original image.
Args:
model: Trained model (U-Net)raster_path(str): Path to the .tif file for predictionpatch_size(int): Patch size used during trainingnum_classes(int): Number of classes of the modeloutput_path(str): Path to save the predicted raster (optional)fill_nulls(bool): Whether to replace null values with null_valuenull_value: Value to replace nullsnormalize(bool): Whether to apply normalizationoverlap(int): Number of overlapping pixels between patches
Returns:
np.array: Predicted reconstructed image
function reconstruct_from_patches¶
1 2 3 4 5 6 7 | |
Reconstructs a full image from patches, averaging values in overlapped regions.
function save_prediction_raster¶
1 2 3 4 5 6 | |
Saves prediction as a georeferenced raster.
This file was automatically generated via lazydocs.