Esempio n. 1
0
    def __fill_transform_list(
            transforms: Union[Namespace, List[dict]]) -> list:
        """
        AUTHORS:
        --------

        author: Alix Leroy

        DESCRIPTION:
        ------------

        Fill the list of transforms with the corresponding methods and arguments

        PARAMETERS:
        -----------

        :param transforms (Union[Namespace, list): A list of transforms

        RETURN:
        -------

        :return loaded_transforms (list): The list of loaded transforms
        """

        loaded_transforms = []
        if transforms is not None:
            for transform in transforms:

                # Switch from Namespace to dict
                #transform = transform.get_all()    # ONly when there is no name
                transform_name = list(transform.get_all())[0]
                transform = transform.get_all()[transform_name]

                if "module" not in transform:
                    transform["module"] = None

                module, module_path = get_module(name=transform["name"],
                                                 module=transform["module"],
                                                 browse=DEEP_MODULE_TRANSFORMS)

                t = TransformData(name=transform["name"],
                                  method=module,
                                  module_path=module_path,
                                  kwargs=transform["kwargs"])
                loaded_transforms.append(t)

        return loaded_transforms
Esempio n. 2
0
def random_blur(image: np.array, kernel_size_min: int,
                kernel_size_max: int) -> Tuple[Any, TransformData]:
    """
    AUTHORS:
    --------

    :author: Alix Leroy

    DESCRIPTION:
    ------------

    Apply a random blur to the image


    PARAMETERS:
    -----------

    :param image: np.array: The image to transform
    :param kernel_size_min: int: Min size of the kernel
    :param kernel_size_max: int: Max size of the kernel


    RETURN:
    -------

    :return: The blurred image
    :return: The last transform data
    """
    # Compute kernel size
    kernel_size = (random.randint(kernel_size_min // 2,
                                  kernel_size_max // 2)) * 2 + 1

    # Blur the image
    image, _ = blur(image, kernel_size)

    # Store the parameters of the random blur
    transform = TransformData(name="blur",
                              method=blur,
                              module_path=__name__,
                              kwargs={"kernel_size": kernel_size})

    # Return the blurred image and the stored parameters
    return image, transform
Esempio n. 3
0
def semi_random_rotate(image: np.array,
                       angle: float) -> Tuple[np.array, TransformData]:
    """
    AUTHORS:
    --------

    :author: Samuel Westlake
    :author: Alix Leroy

    DESCRIPTION:
    ------------

    Rotate an image around a given angle

    PARAMETERS:
    -----------

    :param image: The image
    :param angle: The given angle

    RETURN:
    -------

    :return image(np.array): The rotated image
    :return transform(TransformData): The info of the random transform
    """

    # Select a random angle within a given range
    angle = (2 * np.random.rand() - 1) * angle

    # Rotate the image
    image, _ = rotate(image, angle)

    # Store the random parameters
    transform = TransformData(name="rotate",
                              method=rotate,
                              module_path=__name__,
                              kwargs={"angle": angle})

    # Return the rotated image and the random parameters
    return image, transform
Esempio n. 4
0
def random_channel_shift(image: np.array,
                         shift: int) -> Tuple[np.array, TransformData]:
    """
    AUTHORS:
    --------

    :author: Samuel Westlake
    :author: Alix Leroy

    DESCRIPTION:
    ------------

    Select a random value within a given range and shift all the channel of the image of this intensity value

    PARAMETERS:
    -----------

    :param image:
    :param shift (int): The index

    RETURN:
    -------

    :return image(np.array): The image with the channel shifted
    :return transform (TransformData): The parameters of the random shift
    """
    # Get the shifting value
    shift = np.random.randint(-shift, shift, image.shape[2])

    # Shift the channel
    image, _ = channel_shift(image, shift)

    # Store the parameters
    transform = TransformData(name="channel_shift",
                              method=channel_shift,
                              module_path=__name__,
                              kwargs={"shift": shift})

    # Return the image with the shift channel and the stored parameters
    return image, transform
Esempio n. 5
0
def random_rotate(image: np.array) -> Tuple[np.array, TransformData]:
    """
    AUTHORS:
    --------

    :author: Alix Leroy

    DESCRIPTION:
    ------------

    Rotate an image randomly

    PARAMETERS:
    -----------

    :param image: The image
    :param angle: The given angle

    RETURN:
    -------

    :return image (np.array): The rotated image
    :return transform (TransformData): The info of the random transform
    """
    # Pick a random angle value
    angle = (np.random.uniform(0.0, 360.0))

    # Rotate the image
    image, _ = rotate(image, angle)

    # Store the random transform parameters
    transform = TransformData(name="rotate",
                              method=rotate,
                              module_path=__name__,
                              kwargs={"angle": angle})

    # return the rotated image and the random parameters
    return image, transform
Esempio n. 6
0
def reformat_pointer(x, **kwargs):
    kwargs["image_shape"] = x.shape[0:2]
    return x, TransformData(name="reformat",
                            method=reformat,
                            module_path="modules.transforms.label",
                            kwargs=kwargs)
Esempio n. 7
0
def resize(image, shape):
    image = cv2.resize(image, tuple(shape))
    return image, TransformData(name="scale",
                                module_path="modules.transforms.label",
                                method=scale,
                                kwargs={"image_shape": shape})
Esempio n. 8
0
def random_crop(image: np.array,
                output_size=None,
                output_ratio=None,
                scale=None) -> Tuple[np.array, TransformData]:
    """
    AUTHORS:
    --------

    :author: Samuel Westlake

    DESCRIPTION:
    ------------

    Crop an image with random coordinates

    PARAMETERS:
    -----------

    :param image (np.array): The input image to crop
    :param output_size:
    :param output_ratio:
    :param scale:

    RETURN:
    -------

    :return (np.array): The cropped image
    :return transform(dict): The parameters of the crop
    """
    # Set the cropped image width and height (dx, dy)
    if output_size is not None:
        # If output_size is a list of lists, i.e. ((lower_bound, upper_bound), (lower_bound, upper_bound))
        if any(
                isinstance(item, list) or isinstance(item, tuple)
                for item in output_size):
            # Define a random output_size between the given bounds
            output_size = {
                np.random.randint(*output_size[0]),
                np.random.randint(*output_size[1])
            }
        # Calculate the height and width of the cropped patch
        dx = output_size[0]
        dy = output_size[1]
    elif output_ratio is not None:
        # If output_ratio is a list of lists, i.e. ((lower_bound, upper_bound), (lower_bound, upper_bound))
        if any(
                isinstance(item, list) or isinstance(item, tuple)
                for item in output_ratio):
            # Define a random output_ratio between the given bounds
            output_ratio = (image.shape[1] /
                            np.random.randint(*output_size[0]),
                            image.shape[0] /
                            np.random.randint(*output_size[1]))
        # Calculate the height and width of the cropped patch
        dx = int(image.shape[1] / output_ratio[0])
        dy = int(image.shape[0] / output_ratio[1])
    elif scale is not None:
        if isinstance(scale, list) or isinstance(scale, tuple):
            # Define a random scale between the given bounds
            scale = np.random.randint(*scale)
        # Calculate the height and width of the cropped patch
        dx = int(image.shape[1] * scale)
        dy = int(image.shape[0] * scale)
    else:
        # Set the crop size to the size of the image
        dx, dy = image.shape[0:2]

    # Define the coordinates of the crop
    x0 = np.random.randint(0, image.shape[1] - dx)
    y0 = np.random.randint(0, image.shape[0] - dy)
    x1 = x0 + dx
    y1 = y0 + dy

    # Store the parameters that were selected
    transform = TransformData(name="crop",
                              method=crop,
                              module_path=__name__,
                              kwargs={"coords": (x0, y0, x1, y1)})

    cropped_image, _ = crop(image, (x0, y0, x1, y1))

    # Return the cropped image and transform kwargs
    return cropped_image, transform
Esempio n. 9
0
def random_crop(image: np.array,
                crop_size=None,
                crop_ratio=None,
                scale=None,
                resize=False) -> Tuple[np.array, TransformData]:
    """
    AUTHORS:
    --------

    :author: Samuel Westlake

    DESCRIPTION:
    ------------

    Crop an image with random coordinates

    PARAMETERS:
    -----------

    :param image: (np.array): The input image to crop
    :param crop_size:
    :param crop_ratio:
    :param scale:
    :param resize: (bool) Whether or not to resize the image to the original shape after cropping

    RETURN:
    -------

    :return (np.array): The cropped image
    :return transform(dict): The parameters of the crop
    """
    # Set the cropped image width and height (dx, dy)
    if crop_size is not None:
        # If crop_size is a list of lists, i.e. ((lower_bound, upper_bound), (lower_bound, upper_bound))
        if any(
                isinstance(item, list) or isinstance(item, tuple)
                for item in crop_size):
            # Define a random crop_size between the given bounds
            crop_size = {
                np.random.randint(*crop_size[0]),
                np.random.randint(*crop_size[1])
            }
        # Calculate the height and width of the cropped patch
        dx = crop_size[0]
        dy = crop_size[1]
    elif crop_ratio is not None:
        # If crop_ratio is a list of lists, i.e. ((lower_bound, upper_bound), (lower_bound, upper_bound))
        if any(
                isinstance(item, list) or isinstance(item, tuple)
                for item in crop_ratio):
            # Define a random crop_ratio between the given bounds
            crop_ratio = (image.shape[1] / np.random.randint(*crop_size[0]),
                          image.shape[0] / np.random.randint(*crop_size[1]))
        # Calculate the height and width of the cropped patch
        dx = int(image.shape[1] / crop_ratio[0])
        dy = int(image.shape[0] / crop_ratio[1])
    elif scale is not None:
        if scale > 1:
            Notification(DEEP_NOTIF_FATAL,
                         " : random_crop : scale must be less than 1")
        if isinstance(scale, list) or isinstance(scale, tuple):
            # Define a random scale between the given bounds
            scale = np.random.random() * (scale[1] - scale[0]) + scale[0]
        # Calculate the height and width of the cropped patch
        dx = int(image.shape[1] * scale)
        dy = int(image.shape[0] * scale)
    else:
        # Set the crop size to the size of the image
        dx, dy = image.shape[0:2]

    # Define the coordinates of the crop
    x0 = np.random.randint(0, image.shape[1] - dx)
    y0 = np.random.randint(0, image.shape[0] - dy)
    x1 = x0 + dx
    y1 = y0 + dy

    # Store the parameters that were selected
    transform = TransformData(name="crop",
                              method=crop,
                              module_path=__name__,
                              kwargs={
                                  "coords": (x0, y0, x1, y1),
                                  "resize": True
                              })

    cropped_image, _ = crop(image, (x0, y0, x1, y1), resize=resize)

    # Return the cropped image and transform kwargs
    return cropped_image, transform
Esempio n. 10
0
    def __fill_transform_list(
            transforms: Union[Namespace, List[dict]]) -> list:
        """
        AUTHORS:
        --------

        author: Alix Leroy

        DESCRIPTION:
        ------------

        Fill the list of transforms with the corresponding methods and arguments

        PARAMETERS:
        -----------

        :param transforms (Union[Namespace, list): A list of transforms

        RETURN:
        -------

        :return loaded_transforms (list): The list of loaded transforms
        """

        loaded_transforms = []
        if transforms is not None:
            for transform in transforms:

                # Switch from Namespace to dict
                #transform = transform.get_all()    # ONly when there is no name
                transform_name = list(transform.get_all())[0]
                transform = transform.get_all()[transform_name]

                if "module" not in transform:
                    transform["module"] = None

                m = "default modules" if transform[
                    "module"] is None else transform["module"]
                Notification(
                    DEEP_NOTIF_INFO,
                    DEEP_MSG_LOADING % ("transform", transform["name"], m))

                method, module_path = get_module(name=transform["name"],
                                                 module=transform["module"],
                                                 browse=DEEP_MODULE_TRANSFORMS)
                if method is not None:
                    Notification(
                        DEEP_NOTIF_SUCCESS, DEEP_MSG_LOADED %
                        ("transform", transform["name"], module_path))
                else:
                    Notification(
                        DEEP_NOTIF_FATAL, DEEP_MSG_MODULE_NOT_FOUND %
                        ("transform", transform["name"], m))

                t = TransformData(name=transform["name"],
                                  method=method,
                                  module_path=module_path,
                                  kwargs=transform["kwargs"])
                loaded_transforms.append(t)

        return loaded_transforms