Skip to content

Segmentor

Segmentor

Source code in geograypher/predictors/segmentor.py
class Segmentor:
    def __init__(self, num_classes=None):
        self.num_classes = num_classes

    def setup(self, **kwargs) -> None:
        """This is for things like loading a model. It's fine to not override it if there's no setup"""
        pass

    def segment_image(self, image: np.ndarray, **kwargs):
        """Produce a segmentation mask for an image

        Args:
            image (np.ndarray): np
        """
        raise NotImplementedError("Abstract base class")

    def segment_image_batch(self, images: typing.List[np.ndarray], **kwargs):
        """
        Segment a batch of images, to potentially use full compute capacity. The current implementation
        should be overriden when there is a way to get improvements

        Args:
            images (typing.List[np.ndarray]): The list of images
        """
        segmentations = []

        for image in images:
            segmentation = self.segment_image(image, **kwargs)
            segmentations.append(segmentation)
        return segmentations

    @staticmethod
    def inds_to_one_hot(
        inds_image: np.ndarray,
        num_classes: typing.Union[int, None] = None,
        ignore_ind: int = 255,
    ) -> np.ndarray:
        """Convert an image of indices to a one-hot, one-per-channel encoding

        Args:
            inds_image (np.ndarray): Image of integer indices. (m, n)
            num_classes (int, None): The number of classes. If None, computed as the max index provided. Default None
            ignore_ind (inte, optional): This index is an ignored class

        Returns:
            np.ndarray: (m, n, num_classes) boolean array with one channel filled with a True, all else False
        """
        if num_classes is None:
            inds_image_copy = inds_image.copy()
            # Mask out ignore ind so it's not used in computation
            inds_image_copy[inds_image_copy == ignore_ind] == 0
            num_classes = np.max(inds_image_copy) + 1

        one_hot_array = np.zeros(
            (inds_image.shape[0], inds_image.shape[1], num_classes), dtype=bool
        )
        # Iterate up to max ind, not num_classes to avoid wasted computation when there won't be matches
        for i in range(num_classes):
            # TODO determine if there are any more efficient ways to do this
            # Maybe create all these slices and then concatenate
            # Or test equality with an array that has all the values in it
            one_hot_array[..., i] = inds_image == i

        return one_hot_array

Functions

inds_to_one_hot(inds_image, num_classes=None, ignore_ind=255) staticmethod

Convert an image of indices to a one-hot, one-per-channel encoding

Parameters:

Name Type Description Default
inds_image ndarray

Image of integer indices. (m, n)

required
num_classes (int, None)

The number of classes. If None, computed as the max index provided. Default None

None
ignore_ind inte

This index is an ignored class

255

Returns:

Type Description
ndarray

np.ndarray: (m, n, num_classes) boolean array with one channel filled with a True, all else False

Source code in geograypher/predictors/segmentor.py
@staticmethod
def inds_to_one_hot(
    inds_image: np.ndarray,
    num_classes: typing.Union[int, None] = None,
    ignore_ind: int = 255,
) -> np.ndarray:
    """Convert an image of indices to a one-hot, one-per-channel encoding

    Args:
        inds_image (np.ndarray): Image of integer indices. (m, n)
        num_classes (int, None): The number of classes. If None, computed as the max index provided. Default None
        ignore_ind (inte, optional): This index is an ignored class

    Returns:
        np.ndarray: (m, n, num_classes) boolean array with one channel filled with a True, all else False
    """
    if num_classes is None:
        inds_image_copy = inds_image.copy()
        # Mask out ignore ind so it's not used in computation
        inds_image_copy[inds_image_copy == ignore_ind] == 0
        num_classes = np.max(inds_image_copy) + 1

    one_hot_array = np.zeros(
        (inds_image.shape[0], inds_image.shape[1], num_classes), dtype=bool
    )
    # Iterate up to max ind, not num_classes to avoid wasted computation when there won't be matches
    for i in range(num_classes):
        # TODO determine if there are any more efficient ways to do this
        # Maybe create all these slices and then concatenate
        # Or test equality with an array that has all the values in it
        one_hot_array[..., i] = inds_image == i

    return one_hot_array

segment_image(image, **kwargs)

Produce a segmentation mask for an image

Parameters:

Name Type Description Default
image ndarray

np

required
Source code in geograypher/predictors/segmentor.py
def segment_image(self, image: np.ndarray, **kwargs):
    """Produce a segmentation mask for an image

    Args:
        image (np.ndarray): np
    """
    raise NotImplementedError("Abstract base class")

segment_image_batch(images, **kwargs)

Segment a batch of images, to potentially use full compute capacity. The current implementation should be overriden when there is a way to get improvements

Parameters:

Name Type Description Default
images List[ndarray]

The list of images

required
Source code in geograypher/predictors/segmentor.py
def segment_image_batch(self, images: typing.List[np.ndarray], **kwargs):
    """
    Segment a batch of images, to potentially use full compute capacity. The current implementation
    should be overriden when there is a way to get improvements

    Args:
        images (typing.List[np.ndarray]): The list of images
    """
    segmentations = []

    for image in images:
        segmentation = self.segment_image(image, **kwargs)
        segmentations.append(segmentation)
    return segmentations

setup(**kwargs)

This is for things like loading a model. It's fine to not override it if there's no setup

Source code in geograypher/predictors/segmentor.py
def setup(self, **kwargs) -> None:
    """This is for things like loading a model. It's fine to not override it if there's no setup"""
    pass