def plot_curve(model_class,
               masking_frequency: float,
               probe_level: int,
               masking_level: int,
               loss: float,
               learned_vars: List[float],
               save_path: str,
               dataset,
               other_axis,
               all_losses=List[int],
               baseline_losses=List[int]) -> str:
    other_axis.set_title("Masker Frequency {} Hz, Probe Level {} dB".format(
        masking_frequency, probe_level),
                         fontsize=12)
    masking_level_to_color = {40: "#1f77b4", 60: "#aec7e8", 80: "#ff7f0e"}

    # Get the data and model for this specs
    data = dataset.get_curve_data(masking_frequency=masking_frequency,
                                  probe_level=probe_level,
                                  masking_level=masking_level)
    actual_frequencies, actual_amplitudes = zip(*data)
    highest_y = max(actual_amplitudes) + 1
    all_losses.append(loss / len(actual_frequencies))

    # Do inference with the model
    predicted_amplitudes = []
    for frequency in actual_frequencies:
        current_inputs = [frequency] + learned_vars
        predicted_amplitudes.append(model_class.function(*current_inputs))

    # Sample 1000 points from the learned curve
    sampled_frequencies = np.linspace(min(actual_frequencies),
                                      max(actual_frequencies), 1000)
    sampled_frequencies.sort()
    sampled_amplitudes = []
    for freq in sampled_frequencies:
        current_inputs = [freq] + learned_vars
        amps = model_class.function(*current_inputs)
        sampled_amplitudes.append(amps)

    # Plot everything individually and on the grid
    error_lines = []
    baseline_errors = []
    for f, ampl_tuple in zip(actual_frequencies,
                             zip(actual_amplitudes, predicted_amplitudes)):
        pair = [(f, ampl_tuple[0]), (f, ampl_tuple[1])]
        error_lines.append(pair)
        baseline_errors.append((0 - ampl_tuple[0])**2)
    baseline_losses.append(sum(baseline_errors) / len(baseline_errors))
    linecoll = matcoll.LineCollection(error_lines, colors="k")
    linecoll2 = matcoll.LineCollection(error_lines, colors="k")
    # Plot fitted data
    cur_fig, ax = plt.subplots(num=2)
    other_axis.axvline(x=model_class.masker_frequency_bark, color="C2", ls=":")
    ax.axvline(model_class.masker_frequency_bark,
               c="c",
               label="Fixed Loc",
               ls=":")
    ax.scatter(actual_frequencies, actual_amplitudes, c="b", label="Actual")
    other_axis.scatter(actual_frequencies,
                       actual_amplitudes,
                       label="Actual, Masker Level {}".format(masking_level))
    ax.scatter(actual_frequencies,
               predicted_amplitudes,
               c="r",
               label="Predicted")
    ax.plot(sampled_frequencies, sampled_amplitudes)
    other_axis.plot(sampled_frequencies,
                    sampled_amplitudes,
                    label="Learned, Masker Level {}".format(masking_level))
    ax.add_collection(linecoll)
    ax.legend()
    other_axis.legend()
    other_axis.add_collection(linecoll2)
    plt.rcParams.update({"font.size": 8})
    title = "Nelder-Mead \n Current Loss: %.2f, " % (loss / len(
        actual_frequencies)) + model_class.parameter_repr(learned_vars)
    ax.set_ylim(0, highest_y)
    ax.set_title(title)
    ax.set_xlim(0, 24)
    other_axis.set_ylim(0, highest_y)
    other_axis.set_xlim(0, 24)
    filename = os.path.join(
        save_path, "masker_{}_probe_level_{}_masking_level_{}.png".format(
            masking_frequency, probe_level, masking_level))
    plt.savefig(filename, dpi=cur_fig.dpi)
    plt.close(cur_fig)
    return filename, other_axis
Esempio n. 2
0
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)


def calculate_model_output(inputs: List[float], pars: List[float]) -> float:
    model_vars = inputs + model_class.parameters_from_learned(pars)
    output = model_class.function(*model_vars)
    return output


def plot_simplex(current_vars: List[float], best_vars: List[float],
                 iteration: int, current_loss: float, best_loss: float,
                 highest_y: float, save_path: str) -> str:
Esempio n. 3
0
def plot_curve(model_class, masking_frequency: float, probe_level: int, masker_frequency_bark: int,
               masking_level: int,
               learned_pars: List[float], save_path: str, dataset,
               other_axis=None) -> str:
  if other_axis:
    other_axis.set_title("Masker Frequency {} Hz, Probe Level {} dB".format(
          masking_frequency, probe_level), fontsize=12)
  masking_level_to_color = {40: "#1f77b4", 60: "#aec7e8", 80: "#ff7f0e"}

  # Get the data and model for this specs
  try:
    data = dataset.get_curve_data(
      masking_frequency=masking_frequency,
      probe_level=probe_level,
      masking_level=masking_level)
  except ValueError:
    return "", None
  actual_frequencies, actual_amplitudes = zip(*data)
  highest_y = max(actual_amplitudes) + 1

  # Do inference with the model
  predicted_amplitudes = []
  for frequency in actual_frequencies:
    prediction = model_class.predict(masking_frequency, probe_level,
                                     masking_level, frequency)
    predicted_amplitudes.append(prediction)

  # Sample 1000 points from the learned curve
  sampled_frequencies = list(range(0, 25))
  sampled_frequencies.sort()
  sampled_amplitudes = []
  for freq in sampled_frequencies:
    prediction = model_class.predict(masking_frequency, probe_level,
                                     masking_level, freq)
    sampled_amplitudes.append(prediction)

  # Plot everything individually and on the grid
  error_lines = []
  for f, ampl_tuple in zip(actual_frequencies,
                           zip(actual_amplitudes, predicted_amplitudes)):
    pair = [(f, ampl_tuple[0]), (f, ampl_tuple[1])]
    error_lines.append(pair)
  linecoll = matcoll.LineCollection(error_lines, colors="k")
  linecoll2 = matcoll.LineCollection(error_lines, colors="k")
  # Plot fitted data
  cur_fig, ax = plt.subplots(num=2)
  if other_axis:
    other_axis.axvline(x=masker_frequency_bark, color="C2", ls=":")
    other_axis.scatter(
      actual_frequencies,
      actual_amplitudes,
      label="Actual, Masker Level {}".format(masking_level))
    other_axis.plot(
      sampled_frequencies,
      sampled_amplitudes,
      label="Learned, Masker Level {}".format(masking_level), ls="--")
    other_axis.legend()
    other_axis.add_collection(linecoll2)
    other_axis.set_ylim(0, highest_y)
    other_axis.set_xlim(0, 24)
  ax.axvline(masker_frequency_bark, c="c", label="Fixed Loc", ls=":")
  ax.scatter(actual_frequencies, actual_amplitudes, c="b", label="Actual")

  ax.scatter(actual_frequencies, predicted_amplitudes, c="r", label="Predicted",
             ls="--")
  ax.plot(sampled_frequencies, sampled_amplitudes)

  ax.add_collection(linecoll)
  ax.legend()

  plt.rcParams.update({"font.size": 8})
  title = "Predictions on Test Set"
  ax.set_ylim(0, highest_y)
  ax.set_xlim(0, 24)
  ax.set_title(title)
  filename = os.path.join(
      save_path, "masker_{}_probe_level_{}_masking_level_{}.png".format(
          masking_frequency, probe_level, masking_level))
  plt.savefig(filename, dpi=cur_fig.dpi)
  plt.close(cur_fig)
  return filename, other_axis