Skip to content

recall

Recall

Bases: Trace

Compute recall for a classification task and report it back to the logger.

Parameters:

Name Type Description Default
true_key str

Name of the key that corresponds to ground truth in the batch dictionary.

required
pred_key str

Name of the key that corresponds to predicted score in the batch dictionary.

required
mode Union[str, Set[str]]

What mode(s) to execute this Trace 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".

('eval', 'test')
output_name str

Name of the key to store to the state.

'recall'
Source code in fastestimator\fastestimator\trace\metric\recall.py
@traceable()
class Recall(Trace):
    """Compute recall for a classification task and report it back to the logger.

    Args:
        true_key: Name of the key that corresponds to ground truth in the batch dictionary.
        pred_key: Name of the key that corresponds to predicted score in the batch dictionary.
        mode: What mode(s) to execute this Trace 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".
        output_name: Name of the key to store to the state.
    """
    def __init__(self,
                 true_key: str,
                 pred_key: str,
                 mode: Union[str, Set[str]] = ("eval", "test"),
                 output_name: str = "recall") -> None:
        super().__init__(inputs=(true_key, pred_key), outputs=output_name, mode=mode)
        self.binary_classification = None
        self.y_true = []
        self.y_pred = []

    @property
    def true_key(self) -> str:
        return self.inputs[0]

    @property
    def pred_key(self) -> str:
        return self.inputs[1]

    def on_epoch_begin(self, data: Data) -> None:
        self.y_true = []
        self.y_pred = []

    def on_batch_end(self, data: Data) -> None:
        y_true, y_pred = to_number(data[self.true_key]), to_number(data[self.pred_key])
        self.binary_classification = y_pred.shape[-1] == 1
        if y_true.shape[-1] > 1 and y_true.ndim > 1:
            y_true = np.argmax(y_true, axis=-1)
        if y_pred.shape[-1] > 1:
            y_pred = np.argmax(y_pred, axis=-1)
        else:
            y_pred = np.round(y_pred)
        assert y_pred.size == y_true.size
        self.y_pred.extend(y_pred.ravel())
        self.y_true.extend(y_true.ravel())

    def on_epoch_end(self, data: Data) -> None:
        if self.binary_classification:
            score = recall_score(self.y_true, self.y_pred, average='binary')
        else:
            score = recall_score(self.y_true, self.y_pred, average=None)
        data.write_with_log(self.outputs[0], score)