Skip to content

normalize

Normalize

Bases: ImageOnlyAlbumentation

Divide pixel values by a maximum value, subtract mean per channel and divide by std per channel.

Parameters:

Name Type Description Default
inputs Union[str, Sequence[str]]

Key(s) of images to be modified.

required
outputs Union[str, Sequence[str]]

Key(s) into which to write the modified images.

required
mode Union[None, str, Iterable[str]]

What mode(s) to execute this Op in. For example, "train", "eval", "test", or "infer". To execute regardless of mode, pass None. To execute in all modes except for a particular one, you can pass an argument like "!infer" or "!train".

None
ds_id Union[None, str, Iterable[str]]

What dataset id(s) to execute this Op in. To execute regardless of ds_id, pass None. To execute in all ds_ids except for a particular one, you can pass an argument like "!ds1".

None
mean Union[float, Tuple[float, ...]]

Mean values to subtract.

(0.485, 0.456, 0.406)
std Union[float, Tuple[float, ...]]

The divisor.

(0.229, 0.224, 0.225)
max_pixel_value float

Maximum possible pixel value.

255.0
Image types

uint8, float32

Source code in fastestimator/fastestimator/op/numpyop/univariate/normalize.py
@traceable()
class Normalize(ImageOnlyAlbumentation):
    """Divide pixel values by a maximum value, subtract mean per channel and divide by std per channel.

    Args:
        inputs: Key(s) of images to be modified.
        outputs: Key(s) into which to write the modified images.
        mode: What mode(s) to execute this Op in. For example, "train", "eval", "test", or "infer". To execute
            regardless of mode, pass None. To execute in all modes except for a particular one, you can pass an argument
            like "!infer" or "!train".
        ds_id: What dataset id(s) to execute this Op in. To execute regardless of ds_id, pass None. To execute in all
            ds_ids except for a particular one, you can pass an argument like "!ds1".
        mean: Mean values to subtract.
        std: The divisor.
        max_pixel_value: Maximum possible pixel value.

    Image types:
        uint8, float32
    """
    def __init__(self,
                 inputs: Union[str, Sequence[str]],
                 outputs: Union[str, Sequence[str]],
                 mode: Union[None, str, Iterable[str]] = None,
                 ds_id: Union[None, str, Iterable[str]] = None,
                 mean: Union[float, Tuple[float, ...]] = (0.485, 0.456, 0.406),
                 std: Union[float, Tuple[float, ...]] = (0.229, 0.224, 0.225),
                 max_pixel_value: float = 255.0):
        super().__init__(NormalizeAlb(mean=mean, std=std, max_pixel_value=max_pixel_value, always_apply=True),
                         inputs=inputs,
                         outputs=outputs,
                         mode=mode,
                         ds_id=ds_id)

    def forward(self, data: List[np.ndarray], state: Dict[str, Any]) -> List[np.ndarray]:
        results = super().forward(data, state)
        # Albumentation library casts the result to float64 iff the image is HxWx3, but we want consistent output
        return [result.astype(np.float32) for result in results]