Skip to content

semsegpredictionhandler

semsegpredictionhandler

module for semseg prediction handlers

Classes

SemSegBBoxPredictionHandler

SemSegBBoxPredictionHandler(
    instance_finder,
    prediction_prefix="pred",
    pred_identifier="image_filepath",
    detection_idx_col=DETECTION_INDEX_COLUMN_NAME,
)

Bases: PredictionHandler

Prediction handler to convert a tensor to bounding box information corresponding to ObjDetPredictionHandler based on found instances in a prediction mask.

Initialize SemSegBBoxPredictionHandler. Takes a list of arguments, which are passed to it when an object of this type is created.

Parameters:

  • instance_finder (InstanceFinder) –

    InstanceFinder for prediction identification

  • prediction_prefix (str, default: 'pred' ) –

    Prefix the column names of the predictions

  • pred_identifier (str, default: 'image_filepath' ) –

    Specify the column name of the image filepaths

  • detection_idx_col (str, default: DETECTION_INDEX_COLUMN_NAME ) –

    Specify the name of the column in which detection indices are stored

Source code in niceml/mlcomponents/predictionhandlers/semsegpredictionhandler.py
def __init__(
    self,
    instance_finder: InstanceFinder,
    prediction_prefix: str = "pred",
    pred_identifier: str = "image_filepath",
    detection_idx_col: str = DETECTION_INDEX_COLUMN_NAME,
):
    """
    Initialize SemSegBBoxPredictionHandler. Takes a list of
    arguments, which are passed to it when an object of this type is
    created.

    Args:
        instance_finder: InstanceFinder for prediction identification
        prediction_prefix: Prefix the column names of the predictions
        pred_identifier: Specify the column name of the image filepaths
        detection_idx_col: Specify the name of the column in which detection indices are stored
    """
    super().__init__()
    self.prediction_prefix = prediction_prefix
    self.instance_finder = instance_finder
    self.pred_identifier = pred_identifier
    self.detection_idx_col = detection_idx_col
    self.data = None
    self.data_columns = [pred_identifier, detection_idx_col]
    self.data_columns += get_bounding_box_attributes()
Functions
__enter__
__enter__()

Enter SemSegBBoxPredictionHandler

Source code in niceml/mlcomponents/predictionhandlers/semsegpredictionhandler.py
def __enter__(self):
    """Enter SemSegBBoxPredictionHandler"""
    self.data = []
    if isinstance(self.data_description, OutputImageDataDescription):
        for class_count in range(self.data_description.get_output_channel_count()):
            self.data_columns.append(f"{self.prediction_prefix}_{class_count:04d}")
    return self
__exit__
__exit__(exc_type, exc_value, exc_traceback)

Save the data in self.data as a parquet file

Source code in niceml/mlcomponents/predictionhandlers/semsegpredictionhandler.py
def __exit__(self, exc_type, exc_value, exc_traceback):
    """Save the data in `self.data` as a parquet file"""
    if self.data is None:
        logging.getLogger(__name__).warning(
            "PredictionHandler: %s has no data to write!",
            self.filename,
        )
    else:
        data_frame: pd.DataFrame = pd.DataFrame(self.data)
        self.exp_context.write_parquet(
            data_frame,
            join(ExperimentFilenames.PREDICTION_FOLDER, self.filename + ".parq"),
        )
add_prediction
add_prediction(data_info_list, prediction_batch)

After each prediction, this is processed to find instances in a mask and create bounding box coordinates from the found instances

Source code in niceml/mlcomponents/predictionhandlers/semsegpredictionhandler.py
def add_prediction(self, data_info_list: List[DataInfo], prediction_batch):
    """After each prediction, this is processed to find instances in a mask
    and create bounding box coordinates from the found instances"""

    expected_shape_dimensions = 4
    if (
        len(prediction_batch.shape) < expected_shape_dimensions
    ):  # If the batch size is 1, an additional dimension is necessary and added below
        prediction_batch = np.expand_dims(prediction_batch, 0)

    output_data_description: OutputImageDataDescription = check_instance(
        self.data_description, OutputImageDataDescription
    )

    for prediction, data_info in zip(prediction_batch, data_info_list):
        if output_data_description.get_use_void_class():
            # remove background class from prediction array
            prediction = prediction[:, :, :-1]
        values = np.max(prediction, axis=2)

        value_idxes = np.argmax(prediction, axis=2)

        prediction_container = SemSegPredictionContainer(
            image_id=None,
            max_prediction_idxes=value_idxes,
            max_prediction_values=values,
        )

        mask_instances: List[MaskInstance] = self.instance_finder.analyse_datapoint(
            data_key="",
            data_predicted=prediction_container,
            data_loaded=None,
            additional_data={},
        )

        bbox_pred_data = create_bbox_prediction_from_mask_instances(
            prediction=prediction,
            mask_instances=mask_instances,
        )
        for detection_idx, predictions in bbox_pred_data:
            self._add_data(
                identifier=data_info.get_identifier(),
                predictions=predictions,
                detection_index=detection_idx,
            )
set_params
set_params(exp_context, filename, data_description)

The set_params function is called by the ExperimentContext object when it is time to set up a new experiment.

Parameters:

  • exp_context (ExperimentContext) –

    experiment context to pass to the instance finder

  • filename (str) –

    Specifies the name of the dataset

  • data_description (DataDescription) –

    Stores the data description of the dataset

Source code in niceml/mlcomponents/predictionhandlers/semsegpredictionhandler.py
def set_params(
    self,
    exp_context: ExperimentContext,
    filename: str,
    data_description: DataDescription,
):
    """
    The set_params function is called by the ExperimentContext object when it
    is time to set up a new experiment.

    Args:
        exp_context: experiment context to pass to the instance finder
        filename: Specifies the name of the dataset
        data_description: Stores the data description of the dataset
    """
    super().set_params(exp_context, filename, data_description)
    self.instance_finder.initialize(
        data_description=data_description,
        exp_context=exp_context,
        dataset_name=filename,
    )

SemSegMaskPredictionHandler

SemSegMaskPredictionHandler(
    img_extension=".png", prediction_suffix="_pred"
)

Bases: PredictionHandler

Prediction handler to convert a tensor to channel images for SemSeg

This prediction handler converts a tensor to the maximum prediction image

Parameters:

  • img_extension (str, default: '.png' ) –

    Type of the images to write

  • prediction_suffix (str, default: '_pred' ) –

    Suffix for prediction columns

Source code in niceml/mlcomponents/predictionhandlers/semsegpredictionhandler.py
def __init__(self, img_extension: str = ".png", prediction_suffix: str = "_pred"):
    """
    This prediction handler converts a tensor to the maximum prediction image

    Args:
        img_extension: Type of the images to write
        prediction_suffix: Suffix for prediction columns
    """
    super().__init__()
    self.img_extension = img_extension
    self.prediction_suffix = prediction_suffix
Functions
__enter__
__enter__()

Enter SemSegMaskPredictionHandler

Source code in niceml/mlcomponents/predictionhandlers/semsegpredictionhandler.py
def __enter__(self):
    """Enter SemSegMaskPredictionHandler"""
    return self
__exit__
__exit__(exc_type, exc_value, exc_traceback)

Exit SemSegMaskPredictionHandler

Source code in niceml/mlcomponents/predictionhandlers/semsegpredictionhandler.py
def __exit__(self, exc_type, exc_value, exc_traceback):
    """Exit SemSegMaskPredictionHandler"""
    pass
add_prediction
add_prediction(data_info_list, prediction_batch)

After each prediction this is processed to store the images.

Source code in niceml/mlcomponents/predictionhandlers/semsegpredictionhandler.py
def add_prediction(self, data_info_list: List[DataInfo], prediction_batch):
    """After each prediction this is processed to store the images."""

    output_data_description: OutputImageDataDescription = check_instance(
        self.data_description, OutputImageDataDescription
    )

    expected_shape_dimensions = 4
    if (
        len(prediction_batch.shape) < expected_shape_dimensions
    ):  # If the batch size is 1, an additional dimension is necessary and added below
        prediction_batch = np.expand_dims(prediction_batch, 0)
    for prediction, data_info in zip(prediction_batch, data_info_list):
        if output_data_description.get_use_void_class():
            # remove background class from prediction array
            prediction = prediction[:, :, :-1]
        values = np.max(prediction, axis=2) * 255
        value_idxes = np.argmax(prediction, axis=2)
        target_array = np.stack(
            (values, value_idxes, np.zeros_like(values)), axis=2
        ).astype(dtype=np.uint8)
        target_image = Image.fromarray(target_array)
        self.exp_context.write_image(
            target_image,
            join(
                ExperimentFilenames.PREDICTION_FOLDER,
                self.filename,
                f"{data_info.get_identifier()}{self.prediction_suffix}{self.img_extension}",
            ),
        )

Functions

create_bbox_prediction_from_mask_instances

create_bbox_prediction_from_mask_instances(
    prediction, mask_instances
)

Creates a prepared list of bounding box prediction information based on the result of a semantic segmentation Args: prediction: raw prediction data with the shape (image_width, image_height, channel_count) mask_instances: found instances of a mask

Returns:

  • List[Tuple[int, List[float]]]

    List of Tuples (detection_index, list of prediction data

  • List[Tuple[int, List[float]]]

    (bbox coordinates and prediction scores of each output channel))

Source code in niceml/mlcomponents/predictionhandlers/semsegpredictionhandler.py
def create_bbox_prediction_from_mask_instances(
    prediction: np.ndarray,
    mask_instances: List[MaskInstance],
) -> List[Tuple[int, List[float]]]:
    """
    Creates a prepared list of bounding box prediction information
    based on the result of a semantic segmentation
    Args:
        prediction: raw prediction data with the shape
                    (image_width, image_height, channel_count)
        mask_instances: found instances of a mask

    Returns:
        List of Tuples (detection_index, list of prediction data
        (bbox coordinates and prediction scores of each output channel))

    """
    detection_idx_count: int = 0
    bbox_prediction_data: List[Tuple[int, List[float]]] = []

    if (
        len(mask_instances) == 0
        and sum(len(error_info.instance_contours) for error_info in mask_instances) == 0
    ):
        bbox_prediction_data.append(
            (-1, [0.0 for _ in range(4 + prediction.shape[-1])])
        )

    for error_info in mask_instances:
        # pylint: disable = no-member
        for error in error_info.instance_contours:
            poly = cv2.approxPolyDP(error.contour, epsilon=1, closed=True)
            # epsilon is the approximation accuracy
            # (max difference between the original and the approximation)

            x_pos, y_pos, width, height = cv2.boundingRect(poly)
            predictions_of_error = prediction[
                y_pos : y_pos + height, x_pos : x_pos + width, :
            ]
            bbox_prediction_data.append(
                (
                    detection_idx_count,
                    [float(coord) for coord in [x_pos, y_pos, width, height]]
                    + list(np.max(predictions_of_error, axis=(0, 1))),
                )
            )
            detection_idx_count += 1
    return bbox_prediction_data