Skip to content

Callbacks

MinEpochEarlyStopping ¤

Bases: EarlyStopping

Changed baseline to act as a real baseline.

The number of patience epochs are only counted when baseline loss is achieved.

Reference - Implementation

Author: McLawrence
Source: https://stackoverflow.com/questions/46287403/is-there-a-way-to-implement-early-stopping-in-keras-only-after-the-first-say-1

Source code in aucmedi/utils/callbacks.py
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
class MinEpochEarlyStopping(EarlyStopping):
    """ Changed baseline to act as a real baseline.

    The number of patience epochs are only counted when baseline loss is achieved.

    ??? abstract "Reference - Implementation"
        Author:   McLawrence  <br>
        Source:   https://stackoverflow.com/questions/46287403/is-there-a-way-to-implement-early-stopping-in-keras-only-after-the-first-say-1  <br>
    """
    def __init__(self, monitor='val_loss', min_delta=0, patience=0, verbose=0,
                 mode='auto', start_epoch = 100): # add argument for starting epoch
        super(MinEpochEarlyStopping, self).__init__()
        self.start_epoch = start_epoch

    def on_epoch_end(self, epoch, logs=None):
        if epoch > self.start_epoch:
            super(MinEpochEarlyStopping, self).on_epoch_end(epoch, logs)

ThresholdEarlyStopping ¤

Bases: EarlyStopping

Changed baseline to act as a real baseline.

The number of patience epochs are only counted when baseline loss is achieved.

Reference - Implementation

Author: JBSnorro
Source: https://stackoverflow.com/questions/53500047/stop-training-in-keras-when-accuracy-is-already-1-0

Source code in aucmedi/utils/callbacks.py
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
class ThresholdEarlyStopping(EarlyStopping):
    """ Changed baseline to act as a real baseline.

    The number of patience epochs are only counted when baseline loss is achieved.

    ??? abstract "Reference - Implementation"
        Author:   JBSnorro <br>
        Source:   https://stackoverflow.com/questions/53500047/stop-training-in-keras-when-accuracy-is-already-1-0  <br>
    """
    def __init__(self, *args, **kw):
        super().__init__(*args, **kw)
        self.baseline_attained = False

    def on_epoch_end(self, epoch, logs=None):
        if not self.baseline_attained:
            current = self.get_monitor_value(logs)
            if current is None:
                return
            if self.monitor_op(current, self.baseline):
                if self.verbose > 0:
                    print('Baseline attained.')
                self.baseline_attained = True
            else:
                return
        super(ThresholdEarlyStopping, self).on_epoch_end(epoch, logs)

csv_to_history(input_path) ¤

Utility function for reading a CSV file from the CSVLogger Callback and return a History dictionary object.

Can be utilized in order to pass returned dictionary object to the evaluate_fitting() function of the AUCMEDI evaluation submodule.

Parameters:

Name Type Description Default
input_path str

Path to a CSV file generated by a CSVLogger Callback.

required

Returns:

Name Type Description
history dict

A history dictionary from a Keras history object which contains several logs.

Source code in aucmedi/utils/callbacks.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def csv_to_history(input_path):
    """ Utility function for reading a CSV file from the
    [CSVLogger](https://www.tensorflow.org/api_docs/python/tf/keras/callbacks/CSVLogger)
    Callback and return a History dictionary object.

    Can be utilized in order to pass returned dictionary object to the
    [evaluate_fitting()][aucmedi.evaluation.fitting] function of the AUCMEDI
    [evaluation][aucmedi.evaluation] submodule.

    Args:
        input_path (str):           Path to a CSV file generated by a CSVLogger Callback.

    Returns:
        history (dict):       A history dictionary from a Keras history object which contains several logs.
    """
    # Read logging data
    dt = pd.read_csv(input_path, sep=",")
    # Parse to dict and return results
    return dt.to_dict(orient="list")