xdem
xdem copied to clipboard
[POC] classif step 3: segmentation classification
Context
After implementing the ClassificationLayer base class, the next step is to develop the SegmentationClassificationLayer. This class handles segmentation-based classification of a DEM, where it classifies DEM pixels based on an external segmentation mask provided by the user.
The SegmentationClassificationLayer will inherit from the ClassificationLayer and will implement the logic for applying the segmentation mask to the DEM.
Code
In the same file (classification.py) as the ClassificationLayer base class, add the SegmentationClassificationLayer to define the segmentation classification logic.
-
__init__()method:- Extend the initialization from the
ClassificationLayer. - Include additional attributes specific to segmentation classification:
-
segmentation_mask_path: A string that represents the path to the external segmentation mask (e.g., a raster file). -
class_names: A dictionary that maps class names to values in the segmentation mask (e.g.,{"valid": 1, "water": 2, "land": 3}). Could be a YAML file too.
-
- Extend the initialization from the
-
apply_classification()method:- Load the segmentation mask from the file specified in
segmentation_mask_path. - For each class defined in
class_names, create a mask where DEM pixels match the segmentation values. - Store the resulting masks in the
classificationattribute, which is ageoutils.Mask3D object, where the first dimension represents the class, and the two others represent the mask values.
- Load the segmentation mask from the file specified in
Tests
- Write unit tests for the
SegmentationClassificationLayerclass intest_classification.pyfile. - The unit tests should:
- Verify that the layer initializes properly with the provided configuration.
- Ensure that the classification logic works as expected.
- Confirm that statistics are computed as expected.
- Validate that the results are saved correctly.