Added freezing functions as their files for DUET and NMF MFCC.
ADD
nussl/separation/freeze_nmf_mfcc.py
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+import numpy as np
+import nussl
+import os
+
+#Freeze key NMF MFCC values to the benchmark folder
+def freeze_nmf_mfcc_values():
+ path_to_benchmark_file = os.path.join('..', 'Input', 'piano_and_synth_arp_chord_mono.wav')
+ signal = nussl.AudioSignal(path_to_benchmark_file)
+ nmf_mfcc = nussl.NMF_MFCC(signal, num_sources=2, num_templates=6, distance_measure="euclidean",
+ num_iterations=150)
+ output_folder = os.path.abspath('nmf_mfcc_reference/nmf_mfcc_benchmarks')
+
+ nmf_mfcc.run()
+ np.save(os.path.join(output_folder, "benchmark_activation_matrix"), nmf_mfcc.activation_matrix)
+ np.save(os.path.join(output_folder, "benchmark_templates_matrix"), nmf_mfcc.templates_matrix)
+ np.save(os.path.join(output_folder, "benchmark_labeled_templates"), nmf_mfcc.labeled_templates)
+ np.save(os.path.join(output_folder, "benchmark_masks"), nmf_mfcc.masks)
+
+ nmf_mfcc.make_audio_signals()
+ np.save(os.path.join(output_folder, "benchmark_sources"), nmf_mfcc.sources)
+
+if __name__ == '__main__':
+ freeze_nmf_mfcc_values()

MODIFY
nussl/separation/nmf_mfcc.py
nussl/separation/nmf_mfcc.py
@@ -70,40 +70,8 @@ class NMF_MFCC(mask_separation_base.MaskSeparationBase):
mask_object = masks.BinaryMask(np.array(mask))
self.masks.append(mask_object)
- # self.extract_masks(clustered_templates=self.templates_matrix[:, source_indices],
- # signal_stft=self.signal_stft)
return self.masks # FREEZE!
- # def extract_masks(self, clustered_templates, signal_stft):
- # """ Extracts the binary mask objects for each source.
- # Parameters:
- # clustered_templates (np.matrix): The columns from self.templates_matrix that correspond to the current
- # source being extracted
- # signal_stft (np.matrix): The stft of the input audio signal
- # """
- # new_templates_stft = np.abs(np.squeeze(signal_stft)) # TODO: multichannel
- #
- # # Set up and run NMF using each clustered template
- # reconstruct_nmf = transformer_nmf.TransformerNMF(input_matrix=new_templates_stft, templates=clustered_templates,
- # num_components=clustered_templates.shape[1], should_update_template=False)
- # reconstruct_nmf.should_use_epsilon = False
- # reconstruct_nmf.max_num_iterations = self.num_iterations
- # reconstruct_nmf.distance_measure = self.distance_measure
- # activation_matrix, new_templates_matrix = reconstruct_nmf.transform()
- #
- # # Reconstruct the signal
- # reconstructed_signal = new_templates_matrix.dot(activation_matrix)
- #
- # # Mask the input signal
- # music_stft_max = np.maximum(reconstructed_signal, np.abs(new_templates_stft))
- # mask = np.divide(reconstructed_signal, music_stft_max)
- # mask = np.nan_to_num(mask)
- #
- # # Create the binary mask
- # mask = np.round(mask)
- # mask_object = masks.BinaryMask(np.array(mask))
- # self.masks.append(mask_object)
-
def make_audio_signals(self):
""" Applies each mask in self.masks and returns a list of audio_signal objects for each source.
@@ -117,9 +85,3 @@ class NMF_MFCC(mask_separation_base.MaskSeparationBase):
source_audio_signal.istft(overwrite=True, truncate_to_length=self.audio_signal.signal_length)
self.sources.append(source_audio_signal)
return self.sources # FREEZE!
-
- # # template - extracted template, residual - everything that's leftover.
- # template = np.multiply(np.squeeze(signal_stft), mask)
- # residual = np.multiply(np.squeeze(signal_stft), 1 - mask)
- #
- # return template, residual
\ No newline at end of file

ADD
tests/freeze_duet_values.py
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+from __future__ import division
+
+import unittest
+import nussl
+import numpy as np
+import scipy.io.wavfile as wav
+import scipy.io
+import os
+import warnings
+
+def freeze_duet_values():
+ signal = nussl.AudioSignal(os.path.join('..', 'Input', 'dev1_female3_inst_mix.wav'))
+ duet = nussl.Duet(signal, 3)
+ output_folder = os.path.abspath('duet_reference/duet_benchmarks')
+
+ duet.stft_ch0, duet.stft_ch1, duet.frequency_matrix = duet._compute_spectrogram(duet.sample_rate)
+ np.save(os.path.join(output_folder, "benchmark_stft_ch0"), duet.stft_ch0)
+ np.save(os.path.join(output_folder, "benchmark_stft_ch1"), duet.stft_ch1)
+ np.save(os.path.join(output_folder, "benchmark_wmat"), duet.frequency_matrix)
+
+ duet.symmetric_atn, duet.delay = duet._compute_atn_delay(duet.stft_ch0, duet.stft_ch1, duet.frequency_matrix)
+ np.save(os.path.join(output_folder, "benchmark_sym_atn"), duet.symmetric_atn)
+ np.save(os.path.join(output_folder, "benchmark_delay"), duet.delay)
+
+ duet.normalized_attenuation_delay_histogram, duet.attenuation_bins, duet.delay_bins = duet._make_histogram()
+ np.save(os.path.join(output_folder, "benchmark_hist"), duet.normalized_attenuation_delay_histogram)
+ np.save(os.path.join(output_folder, "benchmark_atn_bins"), duet.attenuation_bins)
+ np.save(os.path.join(output_folder, "benchmark_delay_bins"), duet.delay_bins)
+
+ duet.peak_indices = nussl.utils.find_peak_indices(duet.normalized_attenuation_delay_histogram, duet.num_sources,
+ threshold=duet.peak_threshold,
+ min_dist=[duet.attenuation_min_distance,
+ duet.delay_min_distance])
+ np.save(os.path.join(output_folder,"benchmark_peak_indices"), duet.peak_indices)
+
+ duet.delay_peak, duet.atn_delay_est, duet.atn_peak = duet._convert_peaks()
+ np.save(os.path.join(output_folder, "benchmark_delay_peak"), duet.delay_peak)
+ np.save(os.path.join(output_folder, "benchmark_atn_delay_est"), duet.atn_delay_est)
+ np.save(os.path.join(output_folder, "benchmark_atn_peak"), duet.atn_peak)
+
+ duet.masks = duet._compute_masks()
+ np.save(os.path.join(output_folder, "benchmark_masks"), duet.masks)
+
+ final_signals = duet.make_audio_signals()
+ np.save(os.path.join(output_folder, "benchmark_final_signals"), final_signals)
+
+if __name__ == '__main__':
+ freeze_duet_values()
\ No newline at end of file

MODIFY
tests/test_duet.py
tests/test_duet.py
@@ -197,46 +197,5 @@ class DuetUnitTests(unittest.TestCase):
# TODO: fix this function before writing test for it
-def freeze_duet_values():
- path = DuetUnitTests.path_to_benchmark_file
-
- signal = nussl.AudioSignal(path)
- duet = nussl.Duet(signal, 3)
- output_folder = os.path.abspath('duet_reference/duet_benchmarks')
-
- duet.stft_ch0, duet.stft_ch1, duet.frequency_matrix = duet._compute_spectrogram(duet.sample_rate)
- np.save(os.path.join(output_folder, "benchmark_stft_ch0"), duet.stft_ch0)
- np.save(os.path.join(output_folder, "benchmark_stft_ch1"), duet.stft_ch1)
- np.save(os.path.join(output_folder, "benchmark_wmat"), duet.frequency_matrix)
-
- duet.symmetric_atn, duet.delay = duet._compute_atn_delay(duet.stft_ch0, duet.stft_ch1, duet.frequency_matrix)
- np.save(os.path.join(output_folder, "benchmark_sym_atn"), duet.symmetric_atn)
- np.save(os.path.join(output_folder, "benchmark_delay"), duet.delay)
-
- duet.normalized_attenuation_delay_histogram, duet.attenuation_bins, duet.delay_bins = duet._make_histogram()
- np.save(os.path.join(output_folder, "benchmark_hist"), duet.normalized_attenuation_delay_histogram)
- np.save(os.path.join(output_folder, "benchmark_atn_bins"), duet.attenuation_bins)
- np.save(os.path.join(output_folder, "benchmark_delay_bins"), duet.delay_bins)
-
- duet.peak_indices = nussl.utils.find_peak_indices(duet.normalized_attenuation_delay_histogram, duet.num_sources,
- threshold=duet.peak_threshold,
- min_dist=[duet.attenuation_min_distance,
- duet.delay_min_distance])
- np.save(os.path.join(output_folder,"benchmark_peak_indices"), duet.peak_indices)
-
- duet.delay_peak, duet.atn_delay_est, duet.atn_peak = duet._convert_peaks()
- np.save(os.path.join(output_folder, "benchmark_delay_peak"), duet.delay_peak)
- np.save(os.path.join(output_folder, "benchmark_atn_delay_est"), duet.atn_delay_est)
- np.save(os.path.join(output_folder, "benchmark_atn_peak"), duet.atn_peak)
-
- duet.masks = duet._compute_masks()
- np.save(os.path.join(output_folder, "benchmark_masks"), duet.masks)
-
- final_signals = duet.make_audio_signals()
- np.save(os.path.join(output_folder, "benchmark_final_signals"), final_signals)
-
if __name__ == '__main__':
unittest.main()
\ No newline at end of file
-
-# freeze_duet_values()
-

MODIFY
tests/test_nmf_mfcc.py
tests/test_nmf_mfcc.py
@@ -6,8 +6,6 @@ import numpy as np
import nussl
import os
-
-
class NMFMFCCUnitTests(unittest.TestCase):
# Update this if the benchmark file changes and rerun freeze_duet_values() (below)
@@ -35,6 +33,17 @@ class NMFMFCCUnitTests(unittest.TestCase):
output_file_name = str(i) + '.wav'
source.write_audio_to_file(output_file_name)
+ def load_benchmarks(self):
+ benchmark_dict = {}
+ directory = 'nmf_mfcc_reference/nmf_mfcc_benchmarks'
+ for filename in os.listdir(directory):
+ key = os.path.splitext(filename)[0]
+ file_path = os.path.join('nmf_mfcc_reference', 'nmf_mfcc_benchmarks', filename)
+ value = np.load(file_path)
+ benchmark_dict[key] = value
+ return benchmark_dict
+
+
def test_piano_and_synth(self):
# Load input file
input_file_name = os.path.join('..', 'input', 'piano_and_synth_arp_chord_mono.wav')
@@ -50,21 +59,28 @@ class NMFMFCCUnitTests(unittest.TestCase):
output_file_name = str(i) + '.wav'
source.write_audio_to_file(output_file_name)
-def freeze_nmf_mfcc_values():
- path = NMFMFCCUnitTests.path_to_benchmark_file
+ def benchmark_nmf_mfcc(self):
+ # Load input file
+ input_file_name = os.path.join('..', 'input', 'piano_and_synth_arp_chord_mono.wav')
+ signal = nussl.AudioSignal(path_to_input_file=input_file_name)
- signal = nussl.AudioSignal(path)
+ # Set up NMMF MFCC
nmf_mfcc = nussl.NMF_MFCC(signal, num_sources=2, num_templates=6, distance_measure="euclidean",
num_iterations=150)
+ # and run
+ nmf_mfcc.run()
- output_folder = os.path.abspath('nmf_mfcc_reference/nmf_mfcc_benchmarks')
+ benchmark_activation_matrix = np.load(os.path.join('nmf_mfcc_reference', 'nmf_mfcc_benchmarks', 'benchmark_activation_matrix.npy'))
+ benchmark_templates_matrix = np.load(os.path.join('nmf_mfcc_reference', 'nmf_mfcc_benchmarks', 'benchmark_templates_matrix.npy'))
+ benchmark_labeled_templates = np.load(os.path.join('nmf_mfcc_reference', 'nmf_mfcc_benchmarks', 'benchmark_labeled_templates.npy'))
+ benchmark_masks = np.load(os.path.join('nmf_mfcc_reference', 'nmf_mfcc_benchmarks', 'benchmark_masks.npy'))
- nmf_mfcc.run()
- np.save(os.path.join(output_folder, "benchmark_activation_matrix"), nmf_mfcc.activation_matrix)
- np.save(os.path.join(output_folder, "benchmark_templates_matrix"), nmf_mfcc.templates_matrix)
- np.save(os.path.join(output_folder, "benchmark_labeled_templates"), nmf_mfcc.labeled_templates)
- np.save(os.path.join(output_folder, "benchmark_masks"), nmf_mfcc.masks)
nmf_mfcc.make_audio_signals()
- np.save(os.path.join(output_folder, "benchmark_sources"), nmf_mfcc.sources)
+ benchmark_sources = np.load(os.path.join('nmf_mfcc_reference', 'nmf_mfcc_benchmarks', 'benchmark_sources.npy'))
+
+ assert np.all(benchmark_activation_matrix == nmf_mfcc.activation_matrix)
+ assert np.all(benchmark_templates_matrix == nmf_mfcc.templates_matrix)
+ assert np.all(benchmark_labeled_templates == nmf_mfcc.labeled_templates)
+ assert np.all(benchmark_masks == nmf_mfcc.masks)
+ assert np.all(benchmark_sources == nmf_mfcc.sources)
-freeze_nmf_mfcc_values()
\ No newline at end of file

Added freezing functions as their files for DUET and NMF MFCC.