Skip to content

binarize

Binarize

Bases: NumpyOp

Binarize the input data such that all elements >= threshold become 1 otherwise 0.

Parameters:

Name Type Description Default
threshold float

Binarization threshold.

required
inputs Union[str, Iterable[str], Callable]

Key(s) of images to be modified.

required
outputs Union[str, Iterable[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
Source code in fastestimator\fastestimator\op\numpyop\univariate\binarize.py
class Binarize(NumpyOp):
    """Binarize the input data such that all elements >= threshold become 1 otherwise 0.

    Args:
        threshold: Binarization threshold.
        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".
    """
    def __init__(self,
                 threshold: float,
                 inputs: Union[str, Iterable[str], Callable],
                 outputs: Union[str, Iterable[str]],
                 mode: Union[None, str, Iterable[str]] = None):
        super().__init__(inputs=inputs, outputs=outputs, mode=mode)
        self.threshold = threshold
        self.in_list, self.out_list = True, True

    def forward(self, data: List[np.ndarray], state: Dict[str, Any]) -> List[np.ndarray]:
        return [(dat >= self.threshold).astype(np.float32) for dat in data]