From 99c8246645754035ea5574a92df7b94c1fff7dd9 Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Thu, 28 Sep 2017 02:00:16 +0800 Subject: [PATCH 1/2] add the script to parse tuning log --- deep_speech_2/tools/parse_tuning_log.py | 102 ++++++++++++++++++++++++ 1 file changed, 102 insertions(+) create mode 100644 deep_speech_2/tools/parse_tuning_log.py diff --git a/deep_speech_2/tools/parse_tuning_log.py b/deep_speech_2/tools/parse_tuning_log.py new file mode 100644 index 0000000000..fa7873ee95 --- /dev/null +++ b/deep_speech_2/tools/parse_tuning_log.py @@ -0,0 +1,102 @@ +"""Parse the log for tuning and plot error surface.""" +from __future__ import absolute_import +from __future__ import division +from __future__ import print_function + +import os +import re +import numpy as np +import argparse +import functools +import _init_paths +from utils.utility import add_arguments, print_arguments +import matplotlib.pyplot as plt +from mpl_toolkits.mplot3d import Axes3D + +parser = argparse.ArgumentParser(description=__doc__) +add_arg = functools.partial(add_arguments, argparser=parser) +add_arg("log_path", str, '', "log path for parsing") +add_arg("fig_name", str, 'error_surface.png', "name of output figure") +args = parser.parse_args() + + +def plot_error_surface(num_alphas, alphas, betas, error_rate_type, err_ave): + fig = plt.figure(figsize=(8, 6)) + ax = Axes3D(fig) + + num_betas = len(alphas) // num_alphas + alphas_2d = np.reshape(alphas, (num_alphas, num_betas)) + betas_2d = np.reshape(betas, (num_alphas, num_betas)) + err_ave_2d = np.reshape(err_ave, (num_alphas, num_betas)) + + ax.plot_surface( + alphas_2d, + betas_2d, + err_ave_2d, + rstride=1, + cstride=1, + alpha=0.8, + cmap='rainbow') + z_label = 'WER' if error_rate_type == 'wer' else 'CER' + ax.set_xlabel('alpha', fontsize=12) + ax.set_ylabel('beta', fontsize=12) + ax.set_zlabel(z_label, fontsize=12) + plt.savefig(args.fig_name) + plt.show() + + +def parse_log(): + if not os.path.isfile(args.log_path): + raise IOError("Invaid model path: %s" % args.log_path) + + error_rate_type = None + num_alphas, num_betas = 0, 0 + alphas, betas, err_ave = [], [], [] + + err_rate_pat = re.compile( + '\(alpha, beta\) = ' + '\([-+]?\d+(?:\.\d+)?, [-+]?\d+(?:\.\d+)?\), \[[wcer]') + num_pat = re.compile(r'[-+]?\d+(?:\.\d+)?') + + with open(args.log_path, "r") as log_file: + line = log_file.readline() + while line: + if line.find("error_rate_type:") != -1: + error_rate_type = line.strip().split()[1] + elif line.find("num_alphas:") != -1: + num_alphas = int(line.strip().split()[1]) + elif line.find("num_betas:") != -1: + num_betas = int(line.strip().split()[1]) + elif err_rate_pat.match(line) is not None: + tuples = num_pat.findall(line) + alphas.append(float(tuples[0])) + betas.append(float(tuples[1])) + err_ave.append(float(tuples[2])) + line = log_file.readline() + + if error_rate_type == None: + raise ValueError("Illegal log format, cannot find error_rate_type") + + if num_alphas <= 0: + raise ValueError("Illegal log format, invalid num_alphas") + + if num_betas <= 0: + raise ValueError("Illegal log format, invalid num_betas") + + if alphas == []: + raise ValueError("Illegal log format, cannot find grid search result") + + if num_alphas * num_betas != len(alphas): + raise ValueError("Illegal log format, data's shape mismatches") + + return num_alphas, alphas, betas, error_rate_type, err_ave, + + +def main(): + print_arguments(args) + num_alphas, alphas, betas, error_rate_type, err_ave = parse_log() + plot_error_surface(num_alphas, alphas, betas, error_rate_type, err_ave) + + +if __name__ == '__main__': + main() From 8af114e66dd701598b8fe5e2dd0094131c106134 Mon Sep 17 00:00:00 2001 From: Yibing Liu Date: Thu, 28 Sep 2017 10:19:23 +0800 Subject: [PATCH 2/2] adjust parsing order --- deep_speech_2/tools/parse_tuning_log.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/deep_speech_2/tools/parse_tuning_log.py b/deep_speech_2/tools/parse_tuning_log.py index fa7873ee95..26c11ecf3c 100644 --- a/deep_speech_2/tools/parse_tuning_log.py +++ b/deep_speech_2/tools/parse_tuning_log.py @@ -61,17 +61,17 @@ def parse_log(): with open(args.log_path, "r") as log_file: line = log_file.readline() while line: - if line.find("error_rate_type:") != -1: + if err_rate_pat.match(line) is not None: + triple = num_pat.findall(line) + alphas.append(float(triple[0])) + betas.append(float(triple[1])) + err_ave.append(float(triple[2])) + elif line.find("error_rate_type:") != -1: error_rate_type = line.strip().split()[1] elif line.find("num_alphas:") != -1: num_alphas = int(line.strip().split()[1]) elif line.find("num_betas:") != -1: num_betas = int(line.strip().split()[1]) - elif err_rate_pat.match(line) is not None: - tuples = num_pat.findall(line) - alphas.append(float(tuples[0])) - betas.append(float(tuples[1])) - err_ave.append(float(tuples[2])) line = log_file.readline() if error_rate_type == None: