Пример #1
0
def plot_all_learned_curves(mask_frequencies: List[float],
                            probe_levels: List[int], mask_levels: List[int],
                            losses: List[float],
                            learned_vars: List[List[float]], save_dir: str):
    # Read training data.
    ds = dataset.MaskingDataset()
    for filename in os.listdir("data"):
        if filename.startswith("masker") and filename.endswith(".txt"):
            ds.read_data("data", filename)

    # Prepare gridplot of all learned curves.
    mask_frequencies_sorted = list(set(mask_frequencies.copy()))
    probe_levels_sorted = list(set(probe_levels.copy()))
    mask_frequencies_sorted.sort()
    probe_levels_sorted.sort()
    freq_to_y_axis = {
        freq: i
        for i, freq in enumerate(mask_frequencies_sorted)
    }
    probe_to_x_axis = {probe: i for i, probe in enumerate(probe_levels_sorted)}
    fig, axs = plt.subplots(len(mask_frequencies_sorted),
                            len(probe_levels_sorted),
                            figsize=(14, 20),
                            num=1)
    fig.text(0.5,
             0.02,
             "Probe Frequency (kHz)",
             ha="center",
             va="center",
             fontsize=14)
    fig.text(0.02,
             0.5,
             "Masked SPL (B)",
             ha="center",
             va="center",
             rotation="vertical",
             fontsize=14)
    fig.tight_layout(pad=6.0)
    if len(mask_frequencies_sorted) == 1 and len(probe_levels_sorted) == 1:
        axs = [[axs]]
    for axes in axs:
        for ax in axes:
            ax.set_prop_cycle(color=[
                "#1f77b4", "#aec7e8", "#ff7f0e", "#ffbb78", "#2ca02c",
                "#98df8a", "#d62728", "#ff9896", "#9467bd", "#c5b0d5",
                "#8c564b", "#c49c94", "#e377c2", "#f7b6d2", "#7f7f7f",
                "#c7c7c7", "#bcbd22", "#dbdb8d", "#17becf", "#9edae5"
            ])
    all_losses = []
    baseline_losses = []
    for mask_frequency, probe_level, mask_level, loss, l_vars in zip(
            mask_frequencies, probe_levels, mask_levels, losses, learned_vars):
        model_class = model.Model(mask_frequency, probe_level, mask_level)
        # model_class.learned_pars = learned_vars
        other_axis = axs[freq_to_y_axis[mask_frequency]][
            probe_to_x_axis[probe_level]]
        _, other_axis = plot_curve(model_class, mask_frequency, probe_level,
                                   mask_level, loss, l_vars, save_dir, ds,
                                   other_axis, all_losses, baseline_losses)
        axs[freq_to_y_axis[mask_frequency]][
            probe_to_x_axis[probe_level]] = other_axis
    print("Baseline loss per curve: {}".format(
        sum(baseline_losses) / len(baseline_losses)))
    print("Mean loss per curve: {}".format(sum(all_losses) / len(all_losses)))
    save_filename = os.path.join(save_dir, "all_learned_masking_patterns.png")
    plt.savefig(save_filename, dpi=fig.dpi)
    return save_filename
Пример #2
0
from typing import Tuple, List
import matplotlib.pyplot as plt
from matplotlib import collections as matcoll

from absl import app
from absl import flags

FLAGS = flags.FLAGS

flags.DEFINE_string("logs_dir", "logs", "Where the log file is saved.")
flags.DEFINE_string("logs_file", "logs.txt", "Where the logs are saved.")
flags.DEFINE_string("loss_binary", "./loss.py", "Binary to loss file")
flags.DEFINE_integer("plot_every_n_iterations", 10,
                     "How often to plot learning progress.")

dataset = dataset.MaskingDataset()
for filename in os.listdir("data"):
    if filename.startswith("masker") and filename.endswith(".txt"):
        dataset.read_data("data", filename)

masking_frequency = float(os.environ["MASK_FREQ"])
probe_level = int(os.environ["PROBE_LEVEL"])
masking_level = int(os.environ["MASKING_LEVEL"])
data = dataset.get_curve_data(masking_frequency=masking_frequency,
                              probe_level=probe_level,
                              masking_level=masking_level)
actual_frequencies, actual_amplitudes = zip(*data)

model_class = model.Model(masking_frequency, probe_level, masking_level)

Пример #3
0
def main(argv):
  if len(argv) > 1:
    raise app.UsageError("Too many command-line arguments.")

  if not os.path.exists(FLAGS.vars_file):
    raise ValueError("No data found at %s" % FLAGS.vars_file)

  if not os.path.exists(FLAGS.save_dir):
    os.mkdir(FLAGS.save_dir)

  ds = dataset.MaskingDataset()
  for filename in os.listdir(FLAGS.data_path):
    if filename.startswith("masker") and filename.endswith(".txt"):
      ds.read_data(FLAGS.data_path, filename)


  ds_two = []
  with open("predictions.txt", "r") as infile:
    lines = infile.readlines()
    for i, line in enumerate(lines):
      if i == 0:
        continue
      splitted_line = line.split(";")
      frequencies = splitted_line[0].split(",")
      masker_frequency = float(frequencies[0])
      probe_frequency = float(frequencies[1])
      levels = splitted_line[1].split(",")
      masker_level = float(levels[0])
      probe_level = float(levels[1])
      target_bel_masking = float(splitted_line[5]) / 10
      ds_two.append({
          "probe_frequency": probe_frequency,
          "masker_frequency": masker_frequency,
          "probe_level": probe_level,
          "masker_level": masker_level,
          "target_bel_masking": target_bel_masking
      })

  model_class = model.FullModel()
  model_class.initialize_models(FLAGS.vars_file)
  total_error = 0
  total_points = 0
  baseline_error = 0
  for example in ds.get_all_data():
    # bark_frequency = ds.frequency_to_cb(example["probe_frequency"])
    actual_level = example["probe_level"]
    target_bel_masking = example["target_bel_masking"]
    prediction = model_class.predict(example["masker_frequency"],
                                     example["probe_level"],
                                     example["masker_level"],
                                     example["probe_frequency_bark"])
    error = (target_bel_masking - prediction)**2
    baseline_error += (target_bel_masking - 0)**2
    total_error += error
    total_points += 1
  print("Num predicted: ", total_points)
  print("Baseline Mean squared error: {}".format(baseline_error / total_points))
  print("Mean squared error: {}".format(total_error / total_points))
  if FLAGS.plot_all_curves:
    plot_all_learned_curves(ds,
                            [843.0, 843.0, 843.0, 843.0],
                            [20, 20, 40, 40],
                            [50, 70, 50, 70],
                            model_class,
                            FLAGS.save_dir)
  if FLAGS.plot_ood_curves:
    mask_frequencies = [1370.0]
    probe_levels = [30]
    mask_levels = [80]
    plot_all_learned_curves(ds, mask_frequencies, probe_levels, mask_levels,
                            model_class, FLAGS.save_dir)