Segmenter Module
This module provides the Segmenter class, which is designed to handle segmentation tasks using configurable models and datasets. It supports loading models from snapshots, processing data in batches, and saving the segmentation output to a FITS file.
Segmentation Process
The segment method orchestrates the segmentation process. Here’s a detailed explanation of its internal workings:
Initialization: - The method begins by setting the model to evaluation mode and moving it to the appropriate device (CPU or GPU). - A DataLoader is created for processing the dataset in batches.
Batch Processing: - For each batch, the method processes the data through the model to obtain segmented patches. - The segmented patches are then integrated into the overall segmentation map based on their positions.
Handling Missing Data: - If the missing flag is set, the method applies a missing data mask to the segmentation map.
Post-processing: - The segmentation map is normalized using a count map that tracks how many times each pixel was updated. - Morphological operations are applied to refine the segmentation map if segmentation is not disabled.
Saving Output: - The final segmentation map is saved to a FITS file if save_output is enabled.
Subtleties:
Device Management: The segmenter automatically selects the device (CPU or GPU) based on availability.
Missing Data Handling: The method can handle missing data by applying a mask during the segmentation process.
Morphological Operations: Post-processing includes erosion and reconstruction to refine the segmentation map.
Example Usage
Here’s an example of how to use the Segmenter class:
config = {
"model_snapshot": "path/to/best.pt",
"source": "path/to/data/spine_merged.fits",
"dataset": {
"type": "FilamentsDataset",
"dataset_path": "path/to/data/test.h5",
"learning_mode": "onevsall",
"toEncode": ["positions"],
},
"batch_size": 16,
"missing": False,
"output_path": "segmentation_output.fits",
}
segmenter = Segmenter.from_config(config)
segmenter.segment()
Segmenter
The Segmenter class is responsible for performing segmentation tasks using a configurable model and dataset. It supports handling missing data and disabling segmentation if needed.
- class lis2.segmenter.Segmenter(*args, **kwargs)[source]
Bases:
ConfigurableSegmenter class for performing segmentation tasks with configurable models and datasets.
The Segmenter class is designed to handle segmentation tasks using a configurable model and dataset. It supports loading models from snapshots, processing data in batches, and saving the segmentation output to a FITS file. The class also includes options to handle missing data and disable segmentation.
- Configuration:
name (str): The name of the segmentation run.
model_snapshot (Union[Path, str]): Path to the model snapshot file.
source (Union[Path, str]): Path to the source data file.
dataset (Config): Configuration for the dataset.
batch_size (int): Batch size for processing data. Default is 32.
missing (bool): Flag to handle missing data. Default is False.
no_segmenter (bool): Flag to disable segmentation. Default is False.
output_path (str): Path to save the output file. Default is “output.fits”.
- Example Configuration (YAML):
name: "example_segmentation" model_snapshot: "path/to/model_snapshot.pt" source: "path/to/source_data.fits" dataset: type: "ExampleDataset" dataset_path: "path/to/dataset.h5" learning_mode: "onevsall" toEncode: ["positions"] batch_size: 32 missing: False no_segmenter: False output_path: "segmentation_output.fits"
- Aliases:
model source dataset
- config_schema: Dict = {'batch_size': Schema(type=<class 'int'>, aliases=[], optional=True, default=32), 'dataset': Schema(type=typing.Union[dict, str], aliases=['dataset'], optional=False, default=None), 'missing': Schema(type=<class 'bool'>, aliases=[], optional=True, default=False), 'model_snapshot': Schema(type=typing.Union[pathlib.Path, str], aliases=['model'], optional=False, default=None), 'no_segmenter': Schema(type=<class 'bool'>, aliases=[], optional=True, default=False), 'output_path': Schema(type=<class 'str'>, aliases=[], optional=True, default=output.fits), 'source': Schema(type=typing.Union[pathlib.Path, str], aliases=['source'], optional=False, default=None)}