示例#1
0
    def test_execute(self):

        sample_rate = 22050
        win_size = 256
        hop_size = 128

        fc = FeatureContainer("fake_audio_path", sample_rate, win_size,
                              hop_size)
        fc.features["energy"]["data"] = np.array([1, 1, 0.2, 0.1])
        fc.features["spectral_flatness"]["data"] = np.array(
            [0.1, 0.5, 0.12, 0.3])

        segment_list = []
        for i in range(4):
            segment_list.append(
                Segment((i * hop_size) / sample_rate,
                        ((i * hop_size) + win_size - 1) / sample_rate))

        sc = SegmentContainer("fake_audio_path")
        sc.segments = segment_list

        act_det = Simple(energy_threshold=0.3, spectral_flatness_threshold=0.2)

        act_det.execute(sc, fc)

        assert (sc.segments[0].activity and not sc.segments[1].activity
                and not sc.segments[2].activity
                and not sc.segments[3].activity)
示例#2
0
    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)
示例#3
0
    def test_gen_minibatches_2d_w_pca_scaler(self):

        sample_rate = 22050
        win_size = 256
        hop_size = 128
        energy_threshold = 0.2
        spectral_flatness_threshold = 0.3
        seg_duration = 0.1
        seg_overlap = 0.5

        batch_size = 10
        num_features = 16
        n_time_bins = 17

        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)

        pca = joblib.load(
            os.path.join(DATA_PATH, "transform/mel64_pca16_norm/pca.jl"))
        scaler = joblib.load(
            os.path.join(DATA_PATH, "transform/mel64_pca16_norm/scaler.jl"))

        ffc_ext = FrameFeatureChunkExtractor("mel_spectrum", pca, scaler)
        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)

        sc_gen_e = sc_gen.execute()

        active_segments = []

        # compare data in segment and corresponding data in feature container
        for sc in sc_gen_e:
            fc_path = os.path.join(FEATURE_ROOT,
                                   sc.audio_path.replace(".wav", ".fc.jl"))
            fc = feature_container.FeatureContainer.load(fc_path)
            for s in sc.segments:
                if hasattr(s, 'activity') and s.activity:
                    start_ind = fc.time_to_frame_ind(s.start_time)
                    end_ind = start_ind + n_time_bins
                    data = scaler.transform(
                        pca.transform(fc.features["mel_spectrum"]["data"]
                                      [start_ind:end_ind]))
                    assert np.all(data == s.features["mel_spectrum"])
                    active_segments.append(s)

        # compare data in segment and corresponding data in minibatches

        mb_gen = MiniBatchGen(
            sc_gen, batch_size, {
                "mel_spectrum": {
                    "feature_size": num_features,
                    "n_time_bins": n_time_bins
                }
            }, 0)

        mb_gen_e = mb_gen.execute(active_segments_only=True,
                                  with_targets=False,
                                  with_filenames=False)

        count = 0
        for mb, in mb_gen_e:
            for data in mb["mel_spectrum"]:
                assert np.all(data[0].T ==
                              active_segments[count].features["mel_spectrum"])
                count += 1
示例#4
0
 def test_init(self):
     try:
         Simple(energy_threshold=0.3, spectral_flatness_threshold=0.2)
     except Exception as e:
         pytest.fail("Unexpected Error: {}".format(e))