Skip to content

semsegnetdatalogger

semsegnetdatalogger

Module of the SemSegNetDataLogger

Classes

SemSegNetDataLogger

SemSegNetDataLogger(max_log=10, scale=True)

Bases: NetDataLogger

NetDataLogger for semantic segmentation

initialize SemSegNetDataLogger parameters

Source code in niceml/data/netdataloggers/semsegnetdatalogger.py
def __init__(self, max_log: int = 10, scale: bool = True):
    """initialize SemSegNetDataLogger parameters"""
    super().__init__()
    self.scale: bool = scale  # If true, the masks are scaled to the image size.
    # If false the images are scaled to the mask size.
    self.max_log: int = max_log
    self.log_count: int = 0
    self.mask_colors: List[Tuple[int]] = []
Functions
initialize
initialize(data_description, exp_context, set_name)

initialize SemSegNetDataLogger parameters before training

Source code in niceml/data/netdataloggers/semsegnetdatalogger.py
def initialize(
    self,
    data_description: OutputImageDataDescription,
    exp_context: ExperimentContext,
    set_name: str,
):
    """initialize SemSegNetDataLogger parameters before training"""
    super().initialize(
        data_description=data_description,
        exp_context=exp_context,
        set_name=set_name,
    )

    mask_colors = get_color_array(
        list(range(self.data_description.get_output_channel_count()))
    )
    self.mask_colors = [
        [int(value * 255) for value in color] for color in mask_colors
    ]
log_data
log_data(net_inputs, net_targets, data_info_list)

Saves as many images with corresponding masks as defined in self.max_log. The images are saved into self.output_path. For each input image, the associated masks are added to the image.

Parameters:

  • net_inputs (ndarray) –

    Input images as np.ndarray

  • net_targets (ndarray) –

    Target masks as np.ndarray scaled by OUTPUT_IMAGE_SIZE_DIVISOR

  • data_info_list (List[SemSegDataInfo]) –

    Associated data information of input and

Returns:

  • None

Source code in niceml/data/netdataloggers/semsegnetdatalogger.py
def log_data(
    self,
    net_inputs: np.ndarray,
    net_targets: np.ndarray,
    data_info_list: List[SemSegDataInfo],
):
    """
    Saves as many images with corresponding masks as defined in `self.max_log`.
    The images are saved into `self.output_path`. For each input image,
    the associated masks are added to the image.

    Args:
        net_inputs: Input images as `np.ndarray`
        net_targets: Target masks as `np.ndarray` scaled by OUTPUT_IMAGE_SIZE_DIVISOR
        data_info_list: Associated data information of input and
        destination with extended information

    Returns:
        None
    """
    if self.log_count >= self.max_log:
        return

    for net_input, net_target, data_info in zip(
        net_inputs, net_targets, data_info_list
    ):
        instance_labels = [
            SemSegInstanceLabel(
                class_name=self.data_description.get_output_channel_names()[
                    class_idx
                ],
                class_index=class_idx,
                color=tuple(self.mask_colors[class_idx]),
                active=True,
                mask=net_target[:, :, class_idx] * 255,
                # `draw_error_mask_on_image` doesn't work with binary masks.
                # RGB values are required. * 255 converts mask to RGB
            )
            for class_idx in range(self.data_description.get_output_channel_count())
            if net_target[:, :, class_idx].max() > 0
        ]

        if self.scale:
            factor = ImageSize(
                net_input.shape[1], net_input.shape[0]
            ).get_division_factor(self.data_description.get_output_image_size())
            instance_labels = [
                label.scale_label(scale_factor=factor) for label in instance_labels
            ]

        self._draw_image(
            image=net_input,
            instance_labels=instance_labels,
            data_info=data_info,
        )
        self.log_count += 1
        if self.log_count >= self.max_log:
            break

Functions