# Buggy code
def on_checkpoint(self, checkpoint):
    """Starts tracking checkpoint metadata on checkpoint.

    Sets the newest checkpoint. For PERSISTENT checkpoints: Deletes
    previous checkpoint as long as it isn't one of the best ones. Also
    deletes the worst checkpoint if at capacity.

    Args:
        checkpoint (Checkpoint): Trial state checkpoint.
    """
    if checkpoint.storage == Checkpoint.MEMORY:
        self.newest_memory_checkpoint = checkpoint
        return

    old_checkpoint = self.newest_persistent_checkpoint
    self.newest_persistent_checkpoint = checkpoint

    # Remove the old checkpoint if it isn't one of the best ones.
    if old_checkpoint.value and old_checkpoint not in self._membership:
        self.delete(old_checkpoint)

    try:
        queue_item = QueueItem(self._priority(checkpoint), checkpoint)
    except KeyError:
        logger.error(
            "Result dict has no key: {}. "
            "checkpoint_score_attr must be set to a key in the "
            "result dict.".format(self._checkpoint_score_attr)
        )
        return

    if len(self._best_checkpoints) < self.keep_checkpoints_num:
        heapq.heappush(self._best_checkpoints, queue_item)
        self._membership.add(checkpoint)
    elif queue_item.priority >= self._best_checkpoints[0].priority:
        worst = heapq.heappushpop(self._best_checkpoints, queue_item).value
        self._membership.add(checkpoint)
        if worst in self._membership:
            self._membership.remove(worst)
        # Don't delete the newest checkpoint. It will be deleted on the
        # next on_checkpoint() call since it isn't in self._membership.
        if worst != checkpoint:
            self.delete(worst)


# Traceback (FileNotFoundError)
Failure # 1 (occurred at 2020-06-19_11-26-36)
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/ray/tune/ray_trial_executor.py", line 294, in start_trial
self._start_trial(trial, checkpoint, train=train)
File "/usr/local/lib/python3.6/dist-packages/ray/tune/ray_trial_executor.py", line 235, in _start_trial
self.restore(trial, checkpoint)
File "/usr/local/lib/python3.6/dist-packages/ray/tune/ray_trial_executor.py", line 673, in restore
data_dict = TrainableUtil.pickle_checkpoint(value)
File "/usr/local/lib/python3.6/dist-packages/ray/tune/trainable.py", line 62, in pickle_checkpoint
checkpoint_dir = TrainableUtil.find_checkpoint_dir(checkpoint_path)
File "/usr/local/lib/python3.6/dist-packages/ray/tune/trainable.py", line 87, in find_checkpoint_dir
raise FileNotFoundError("Path does not exist", checkpoint_path)
FileNotFoundError: [Errno Path does not exist] /content/TRASH_TUNE_PBT_oversampling_mimic_densenet121/TUNE_Model_0_2020-06-19_11-24-215xncry9c/checkpoint_6/

# Fixed code
def on_checkpoint(self, checkpoint):
    """Starts tracking checkpoint metadata on checkpoint.

    Sets the newest checkpoint. For PERSISTENT checkpoints: Deletes
    previous checkpoint as long as it isn't one of the best ones. Also
    deletes the worst checkpoint if at capacity.

    Args:
        checkpoint (Checkpoint): Trial state checkpoint.
    """
    if checkpoint.storage == Checkpoint.MEMORY:
        self.newest_memory_checkpoint = checkpoint
        return

    old_checkpoint = self.newest_persistent_checkpoint

    if old_checkpoint.value == checkpoint.value:
        return

    self.newest_persistent_checkpoint = checkpoint

    # Remove the old checkpoint if it isn't one of the best ones.
    if old_checkpoint.value and old_checkpoint not in self._membership:
        self.delete(old_checkpoint)

    try:
        queue_item = QueueItem(self._priority(checkpoint), checkpoint)
    except KeyError:
        logger.error(
            "Result dict has no key: {}. "
            "checkpoint_score_attr must be set to a key in the "
            "result dict.".format(self._checkpoint_score_attr)
        )
        return

    if len(self._best_checkpoints) < self.keep_checkpoints_num:
        heapq.heappush(self._best_checkpoints, queue_item)
        self._membership.add(checkpoint)
    elif queue_item.priority >= self._best_checkpoints[0].priority:
        worst = heapq.heappushpop(self._best_checkpoints, queue_item).value
        self._membership.add(checkpoint)
        if worst in self._membership:
            self._membership.remove(worst)
        # Don't delete the newest checkpoint. It will be deleted on the
        # next on_checkpoint() call since it isn't in self._membership.
        if worst != checkpoint:
            self.delete(worst)