Skip to content

Normalizer

This module defines the Normalizer class, which handles the normalization of images and tags based on a given configuration.

The class includes methods to precompile normalization functions, normalize images and tags, and define specific normalization strategies such as Z-score and uniform normalization. The normalization process involves replacing NaN values, creating masks, and scaling values to a specified range. The module also provides functions to recover the original values from the normalized data. The Normalizer class ensures that both images and tags are consistently normalized according to the specified configuration, facilitating further processing and analysis.

Normalizer(conf)

Class to handle the normalization of images and tags.

Parameters:

  • conf (dict) –

    Dictionary containing the configuration

Source code in src/speckcn2/normalizer.py
30
31
def __init__(self, conf: dict):
    self.conf = conf

normalize_imgs_and_tags(all_images, all_tags, all_ensemble_ids)

Normalize both the input images and the tags to be between 0 and 1.

Parameters:

  • all_images (list) –

    List of all images

  • all_tags (list) –

    List of all tags

  • conf (dict) –

    Dictionary containing the configuration

Returns:

  • dataset ( list ) –

    List of tuples (image, normalized_tag)

Source code in src/speckcn2/normalizer.py
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
def normalize_imgs_and_tags(
    self,
    all_images: list[torch.tensor],
    all_tags: list[np.ndarray],
    all_ensemble_ids: list[int],
) -> list[tuple[torch.tensor, np.ndarray, int]]:
    """Normalize both the input images and the tags to be between 0 and 1.

    Parameters
    ----------
    all_images : list
        List of all images
    all_tags : list
        List of all tags
    conf : dict
        Dictionary containing the configuration

    Returns
    -------
    dataset : list
        List of tuples (image, normalized_tag)
    """
    self._normalizing_functions(all_images, all_tags, all_ensemble_ids)

    # Normalize the images
    normalized_images = [self.normalize_img(image) for image in all_images]

    # And normalize the tags
    normalized_tags = np.array([[
        self.normalize_tag[j](tag, tag_id) for j, tag in enumerate(tags)
    ] for tag_id, tags in enumerate(all_tags)],
                               dtype=np.float32)

    # I can now create the dataset
    dataset = [(image, tag, ensemble_id) for image, tag, ensemble_id in
               zip(normalized_images, normalized_tags, all_ensemble_ids)]

    return dataset