def test_init(self): try: with open(CONFIG_PATH) as config_file: config = json.loads(config_file.read()) MiniBatchGen.from_config(config) except Exception as e: pytest.fail("Unexpected Error: {}".format(e))
def test_consuming_minibatch(self): with open(CONFIG_PATH) as config_file: config = json.loads(config_file.read()) mb_gen_dict = MiniBatchGen.from_config(config) try: if not os.path.exists(FEATURE_ROOT): os.makedirs(FEATURE_ROOT) for _, mb_gen in mb_gen_dict.items(): mb_gen_e = mb_gen.execute() next(mb_gen_e) except Exception as e: pytest.fail("Unexpected Error: {}".format(e)) finally: shutil.rmtree(FEATURE_ROOT)
def test_data_multiple_features(self): """ Compare data from "manual" constructor to config file-based constructor. """ # construct minibatch generator "manually" sample_rate = 22050 win_size = 256 hop_size = 128 energy_threshold = 0.2 spectral_flatness_threshold = 0.3 seg_duration = 0.2 seg_overlap = 0.5 batch_size = 50 num_features = 64 n_time_bins = 34 af_gen = AudioFrameGen(sample_rate=sample_rate, win_size=win_size, hop_size=hop_size) en_ext = EnergyExtractor() sf_ext = SpectralFlatnessExtractor() mel_ext = MelSpectrumExtractor(sample_rate=sample_rate, fft_size=win_size, n_mels=64, min_freq=0, max_freq=sample_rate / 2) ff_pro = FrameFeatureProcessor(af_gen, [en_ext, sf_ext, mel_ext], feature_container_root=FEATURE_ROOT) ffc_ext = FrameFeatureChunkExtractor("mel_spectrum") act_det = Simple( energy_threshold=energy_threshold, spectral_flatness_threshold=spectral_flatness_threshold) sf_pro = SegmentFeatureProcessor([act_det, ffc_ext], ff_pro=ff_pro, audio_root=DATA_PATH) parser = CSVFileLabelParser(TEST_FILE2LABEL_PATH, label_file=TEST_LABEL_PATH) sc_gen = SegmentContainerGenerator(DATA_PATH, sf_pro, label_parser=parser, seg_duration=seg_duration, seg_overlap=seg_overlap) mb_gen_1 = MiniBatchGen( sc_gen, batch_size, { "mel_spectrum": { "feature_size": num_features, "n_time_bins": n_time_bins } }, 0) # parse json file with open(CONFIG_PATH) as config_file: config = json.loads(config_file.read()) config["features"] = [{ 'name': 'audio_chunk', 'config': {} }, { 'name': 'mel_spectrum', 'config': { 'n_mels': 64, 'min_freq': 0, 'max_freq': 11025, 'log_amp': 1 } }] # construct minibatch generator from config mb_gen_dict = MiniBatchGen.from_config(config) mb_gen_2 = mb_gen_dict["default"] # execute and compare try: mb_gen_1_e = mb_gen_1.execute(active_segments_only=True, with_targets=True, with_filenames=False) mb_gen_2_e = mb_gen_2.execute(active_segments_only=True, with_targets=True, with_filenames=False) if not os.path.exists(FEATURE_ROOT): os.makedirs(FEATURE_ROOT) for mb1, mb2 in zip(mb_gen_1_e, mb_gen_2_e): assert np.all(mb1[0]["mel_spectrum"] == mb2[0]["mel_spectrum"]) assert np.all(mb1[1] == mb2[1]) except Exception as e: pytest.fail("Unexpected Error: {}".format(e)) finally: shutil.rmtree(FEATURE_ROOT)
def test_audio_data(self): """ Compare data from "manual" constructor to config file-based constructor, with raw audio feature. """ # construct minibatch generator "manually" sample_rate = 22050 seg_duration = 0.2 seg_overlap = 0.5 batch_size = 50 num_features = 1 n_time_bins = 4410 ac_ext = AudioChunkExtractor(DATA_PATH, sample_rate) sf_pro = SegmentFeatureProcessor([ac_ext], ff_pro=None, audio_root=DATA_PATH) parser = CSVFileLabelParser(TEST_FILE2LABEL_PATH, label_file=TEST_LABEL_PATH) sc_gen = SegmentContainerGenerator(DATA_PATH, sf_pro, label_parser=parser, seg_duration=seg_duration, seg_overlap=seg_overlap) mb_gen_1 = MiniBatchGen( sc_gen, batch_size, { "audio_chunk": { "feature_size": num_features, "n_time_bins": n_time_bins } }, 0) # parse json file with open(CONFIG_PATH) as config_file: config = json.loads(config_file.read()) # replace feature by audio_chunk and remove activity detection config["features"] = [{"name": "audio_chunk", "config": {}}] config.pop("activity_detection") # construct minibatch generator from config mb_gen_dict = MiniBatchGen.from_config(config) mb_gen_2 = mb_gen_dict["default"] # execute and compare mb_gen_1_e = mb_gen_1.execute(active_segments_only=False, with_targets=True, with_filenames=True) mb_gen_2_e = mb_gen_2.execute(active_segments_only=False, with_targets=True, with_filenames=True) for mb1, mb2 in zip(mb_gen_1_e, mb_gen_2_e): assert np.all(mb1[0]["audio_chunk"] == mb2[0]["audio_chunk"]) assert np.all(mb1[1] == mb2[1]) assert np.all(mb1[2] == mb2[2])