示例#1
0
def clean_Datafolder(input_dir,
                     output_dir,
                     model,
                     batch_size,
                     cuda=False,
                     samples=None):
    # Load dataset
    make_identical_dir_structure(input_dir, output_dir)
    test_set = Datafolder_soundfiles(y_paths=walk_dir(input_dir),
                                     transform=model.transform)
    output_paths = format_paths(test_set.y_paths,
                                input_dir,
                                output_dir,
                                extention='.wav')

    sampler = None
    if samples is not None:
        if type(slice(0)) == type(samples):
            sampler = SubsetSampler(indices=range(test_set.length),
                                    slice_=samples)
        else:
            sampler = SubsetSampler(indices=samples)

    test_data_loader = DataLoader(test_set,
                                  batch_size=batch_size,
                                  num_workers=2,
                                  pin_memory=cuda,
                                  sampler=sampler)

    if cuda:
        model.cuda()
    model.eval()

    pool = Pool(processes=2)
    jobs = []
    for (indexs, (data, Y_m, Y_a, length)) in test_data_loader:
        try:
            mask = compute_mask_batch(model, data, cuda=cuda)
        except:
            for index in indexs:
                print(index, output_paths[index], 'failed')
        else:
            mask, Y_m, Y_a, length = (k.numpy()
                                      for k in (mask, Y_m, Y_a, length))

            for job in jobs:
                job.wait()
            jobs = []
            for I, index in enumerate(indexs):
                # clean_sample_(model, mask[I], Y_m[I], Y_a[I], length[I], output_paths[index])
                jobs.append(
                    pool.apply_async(
                        clean_sample_(model, mask[I], Y_m[I], Y_a[I],
                                      length[I], output_paths[index])))
    pool.close()
    pool.join()
    print('Finished clearning', input_dir)
示例#2
0
def enhance_Datafolder(model, input_dir, output_dir, batch_size, cuda=False):
    # Load dataset
    make_identical_dir_structure(input_dir, output_dir)
    output_paths = format_paths(test_set.y_paths,
                                input_dir,
                                output_dir,
                                extention='.wav')
    y_paths = walk_dir(input_dir)
    enhance_soundfiles(model, y_paths, output_paths, batch_size, cuda)
示例#3
0
def PESQ_evalfolder(reference_dir, degraded_dir, output_path, fs):
    """Compute the PESQ scores for all wavefiles in a folder.

    Walks though a directory of degraded wavefiles and computes all the scores
    with similar named refrence wavefiles in the refrence directory.
    Saves a csv file with the filenames, mos, and mos_lqo scores.

    Parameters
    ----------
    reference_dir : str
        The path to the refrence wavfile.
    degraded_dir : str
        The path to the degraded wavfile.
    fs : int
        The sample frequency should be 8000 or 16000.

    """
    reference_paths = walk_dir(reference_dir)
    degraded_paths = format_paths(reference_paths, reference_dir, degraded_dir)
    assert set(degraded_paths) == set(walk_dir(degraded_dir))
    print('Warning: PESQ will save a file in the current working directory')
    mos_list, mos_lqo_list = PESQ_evalpaths(reference_paths,
                                            degraded_paths,
                                            output_path,
                                            fs=fs)

    print('PESQ failed for {} samples'.format(np.isnan(mos_list).sum()))

    makedir(os.path.dirname(output_path))
    io.savemat(
        output_path, {
            'data_mos':
            mos_list,
            'data_mos_lqo':
            mos_lqo_list,
            'names':
            list(os.path.relpath(s, reference_dir) for s in reference_paths),
            'reference_dir':
            reference_dir,
            'degraded_dir':
            degraded_dir
        })