def delete_token(self):
     """
     Delete the token associated to the model
     :return:
     """
     if self.full_name_i is not None:
         token_path = EPath('temp', f'token_{self.full_name}')
         if token_path.exists():
             token_path.unlink()
    def delete_token_midis_path(self):
        """

        :return:
        """
        if self._save_midis_path_i is not None:
            path = EPath(
                'temp',
                f'token_{self.full_name}-generation({self._save_midis_path_i}).txt'
            )
            if path.exists():
                path.unlink()
Beispiel #3
0
def main(args):
    """

    :param args:
    :return:
    """
    if not args.midi:
        shutil.rmtree(path='generated_midis', ignore_errors=True)
    if not args.hp:
        shutil.rmtree(path='hp_search', ignore_errors=True)
    if not args.model:
        shutil.rmtree(path='saved_models', ignore_errors=True)
    if not args.tensorboard:
        shutil.rmtree(path='tensorboard', ignore_errors=True)
    if not args.temp:
        shutil.rmtree(path='temp', ignore_errors=True)
    if not args.data_temp:
        data_temp = EPath(g.path.get_data_folder_path(args)) / 'temp'
        shutil.rmtree(path=data_temp, ignore_errors=True)
    if not args.zip:
        zip_path = EPath('my_zip.zip')
        if zip_path.exists():
            zip_path.unlink()
    print('Done cleaning')
class CheckPoint(KerasCallback):
    """

    """
    def __init__(self, filepath):
        """

        :param filepath:
        """
        self.filepath = EPath(filepath)
        super(CheckPoint, self).__init__()

        self.best_acc = -np.Inf
        self.best_loss = np.Inf

    def on_epoch_end(self, epoch, logs=None):
        """

        :param epoch:
        :param logs:
        :return:
        """
        logs = logs or {}
        if self.greater_result(logs):
            self.best_acc = self.get_val_acc_mean(logs)
            self.best_loss = self.get_val_loss(logs)
            if self.filepath.exists():
                self.filepath.unlink()
            # self.model.save_weights(self.filepath.as_posix(), overwrite=True)
            # tf.keras.experimental.export_saved_model(self.model, self.filepath.as_posix())
            with open(self.filepath, 'wb') as dump_file:
                pickle.dump(dict(weights=self.model.get_weights()), dump_file)

    def greater_result(self, logs):
        """

        :param logs:
        :return:
        """
        acc = self.get_val_acc_mean(logs)
        return np.greater(
            acc, self.best_acc) or (np.equal(acc, self.best_acc) and np.less(
                self.get_val_loss(logs), self.best_loss))

    @staticmethod
    def get_val_acc_mean(logs):
        """

        :param logs:
        :return:
        """
        i = 0
        res = 0
        while f'val_Output_{i}_acc' in logs:
            res += logs.get(f'val_Output_{i}_acc')
            i += 1
        return res / i

    @staticmethod
    def get_val_loss(logs):
        """

        :param logs:
        :return:
        """
        return logs.get('val_loss')