Ejemplo n.º 1
0
    def __init__(self, musdb_root, subset, target_names, n_fft, hop_length, num_frame):

        musdb_root = Path(musdb_root)
        self.root = musdb_root.joinpath(subset)

        if subset == 'test':
            self.musdb_reference = musdb.DB(root=musdb_root, subsets='test', is_wav=True)
        elif subset == 'train':
            self.musdb_reference = musdb.DB(root=musdb_root, subsets='train', split='train', is_wav=True)
        elif subset == 'valid':
            self.musdb_reference = musdb.DB(root=musdb_root, subsets='train', split='valid', is_wav=True)
        else:
            raise ModuleNotFoundError

        check_musdb_valid(self.musdb_reference)

        self.source_names = ['vocals', 'drums', 'bass', 'other']
        if target_names is None:
            self.target_names = self.source_names
        else:
            self.target_names = target_names

        self.num_tracks = len(self.musdb_reference)
        self.num_targets = len(self.target_names)
        # cache wav file dictionary
        self.wav_dict = {i: {s: self.musdb_reference.tracks[i].sources[s].path for s in self.source_names}
                         for i in range(self.num_tracks)}

        for i in range(self.num_tracks):
            self.wav_dict[i]['mixture'] = self.musdb_reference[i].path

        self.lengths = [self.get_audio(i, 'vocals').shape[0] for i in range(self.num_tracks)]
        self.window_length = hop_length * (num_frame - 1)
Ejemplo n.º 2
0
def make_spectrograms(data, test):
	"""
	Generates all of the spectrograms from the musdb18 dataset.
	:return: None
	"""
	if data=="train":
		mus_data = musdb.DB(root="data/musdb18", subsets="train")
	else:
		mus_data = musdb.DB(root="data/musdb18", subsets="test")
	

	dictionary = {}
	dictionary["mix"] = []
	dictionary["vocals"] = []
	dictionary["instrumental"] = []

	# Creating the spectogram arrays from the training data
	num_tracks = len(mus_data)
	percent = 0.1

	for i, track in enumerate(mus_data):

		if i / num_tracks > percent:
			print(int(100 * percent), "%", "of " + data +  " data generated")
			percent += 0.1

		# Converting samples to target rate of 22050
		original_sr = track.rate
		target_sr = 22050
		mix_data = librosa.resample(librosa.to_mono(track.audio.T), orig_sr=original_sr, target_sr=target_sr, res_type='kaiser_best', fix=True, scale=False)
		vocal_data = librosa.resample(librosa.to_mono(track.targets['vocals'].audio.T), orig_sr=original_sr, target_sr=target_sr, res_type='kaiser_best', fix=True, scale=False)
		instrumental_data = librosa.resample(librosa.to_mono(track.targets['accompaniment'].audio.T), orig_sr=original_sr, target_sr=target_sr, res_type='kaiser_best', fix=True, scale=False)

		# Length of frame; 66150 should be 3 seconds (appears as 6 seconds on graph)
		len_frame = target_sr*3
		num_frames = int(len(mix_data)/len_frame)

		# Saving each frame as a spectrogram array (and putting track in mix folders and vocals in vocals folder)
		for frame in range(num_frames):
			
			dictionary["mix"].append(generate_spectrogram_array(mix_data[frame * len_frame : frame * len_frame + len_frame]))
			dictionary["vocals"].append(generate_spectrogram_array(vocal_data[frame * len_frame : frame * len_frame + len_frame]))
			dictionary["instrumental"].append(generate_spectrogram_array(instrumental_data[frame * len_frame : frame * len_frame + len_frame]))

			if test:
				pickle.dump(dictionary, open( "data/spectrograms/" + data + "-1", "wb" ))
				make_spectrogram_image(mix_data[frame * len_frame : frame * len_frame + len_frame],"test-image")
				return

	# pickle dictionary here
	pickle.dump(dictionary, open( "data/spectrograms/" + data, "wb" ))
	return
Ejemplo n.º 3
0
    def __init__(self, musdb_root='data/musdb18_wav/', is_wav=True):
        self.musdb_train = musdb.DB(root=musdb_root,
                                    subsets="train",
                                    split='train',
                                    is_wav=is_wav)
        self.musdb_valid = musdb.DB(root=musdb_root,
                                    subsets="train",
                                    split='valid',
                                    is_wav=is_wav)
        self.musdb_test = musdb.DB(root=musdb_root,
                                   subsets="test",
                                   is_wav=is_wav)

        assert (len(self.musdb_train) > 0)
 def __init__(self, musdbwav_path, subset_split, instruments,
              sample_length):
     assert subset_split in ('train', 'test', 'valid')
     if subset_split in ('train', 'valid'):
         self.mus = musdb.DB(root=musdbwav_path,
                             subsets='train',
                             split=subset_split,
                             is_wav=True)
     elif subset_split == 'test':
         self.mus = musdb.DB(root=musdbwav_path,
                             subsets='test',
                             is_wav=True)
     self.sample_length = sample_length
     self.instruments = instruments
Ejemplo n.º 5
0
def eval_mus_dir(dataset, estimates_dir, output_dir=None, ext='wav'):
    """Run evaluation of musdb estimate dir

    Parameters
    ----------
    dataset : DB(object)
        MUSDB18 Database object.
    estimates_dir : str
        Path to estimates folder.
    output_dir : str
        Output folder where evaluation json files are stored.
    ext : str
        estimate file extension, defaults to `wav`
    """
    # create a new musdb instance for estimates with the same file structure
    est = musdb.DB(root=estimates_dir, is_wav=True)
    # get a list of track names
    tracks_to_be_estimated = [t.name for t in est.tracks]

    for track in dataset:
        if track.name not in tracks_to_be_estimated:
            continue
        _load_track_estimates(track=track,
                              estimates_dir=estimates_dir,
                              output_dir=output_dir)
Ejemplo n.º 6
0
 def __init__(self, root, split, target, samples_per_track, seq_duration, dtype):
     self.mus = musdb.DB(root = root, split = split)
     self.split = split
     self.samples_per_track = samples_per_track
     self.seq_duration = seq_duration
     self.dtype = dtype
     self.target = target
Ejemplo n.º 7
0
def calculate_score(model,
                    model_weights_path,
                    musdb_dir='musdb',
                    n_workers=1,
                    n_fft=2048,
                    hop_length=512,
                    slice_duration=2):
    mus = musdb.DB(root_dir=musdb_dir)
    music_list = mus.load_mus_tracks(subsets='test')

    model_weights = torch.load(model_weights_path)
    model.load_state_dict(model_weights)
    # model.cuda()
    scores = parmap.map(calculate_SDR,
                        music_list,
                        pm_processes=n_workers,
                        pm_pbar=True,
                        model=model,
                        n_fft=n_fft,
                        hop_length=hop_length,
                        slice_duration=slice_duration)

    print(scores)
    print(np.mean(scores))
    print(np.median(scores))

    torch.save(scores, 'scores')
Ejemplo n.º 8
0
 def __init__(self,
              base_path: str,
              subset: str,
              split: str,
              target: str,
              duration: Optional[float],
              samples: int = 1,
              random: bool = False,
              partitions: int = 1) -> None:
     """
     base_path -- Ruta del dataset
     subset -- Nombre del conjunto: 'train' o 'test'
     split -- División del entrenamiento: 'train' o 'valid' cuando subset='train'
     target -- Instrumento que se va a separar 'vocals', 'drums', 'bass' o 'vocal'
     duration -- Duración de cada canción en segundos
     samples -- Cantidad de muestras de cada cancion
     random -- True si se van a mezclar las canciones de forma aleatoria
     partitions -- Cantidad de particiones de las canciones de validación
     """
     super(MUSDB18Dataset, self).__init__()
     self.sample_rate = 44100  # Por ahora no se usa
     self.split = split
     self.target = target
     self.duration = duration
     self.samples = samples
     self.random = random
     self.partitions = partitions
     self.mus = musdb.DB(root=base_path, subsets=subset, split=split)
Ejemplo n.º 9
0
def museval(inargs=None):
    """
    Commandline interface for museval evaluation tools
    """
    parser = argparse.ArgumentParser()

    parser.add_argument('estimates_dir', type=str)

    parser.add_argument('-o', help='output_dir')

    parser.add_argument('--musdb', help='path to musdb', type=str)

    parser.add_argument(
        '--is-wav',
        help='Read musdb wav instead of stems',
        action='store_true',
    )

    args = parser.parse_args(inargs)
    mus = musdb.DB(root=args.musdb, is_wav=args.is_wav)

    if not args.o:
        output_dir = args.estimates_dir
    else:
        output_dir = args.o

    # evaluate an existing estimate folder with wav files
    eval_mus_dir(
        dataset=mus,  # instance of musdb
        estimates_dir=args.estimates_dir,  # path to estiamte folder
        output_dir=output_dir,  # set a folder to write eval json files
    )
Ejemplo n.º 10
0
def prepare_dataset(data_path,
                    subset=None,
                    path_to_save='./numpy_data',
                    processed_csv_path='./processed_dataset.csv',
                    resample_rate=None,
                    n_fft=2048,
                    hop_length=512,
                    slice_duration=2,
                    n_workers=1):
    print('hop_length = ', hop_length)
    mus = musdb.DB(root_dir=data_path)
    music_list = mus.load_mus_tracks(subsets=subset)
    print('Starting preparing dataset...')
    if not os.path.exists(path_to_save):
        os.makedirs(path_to_save)
    processed_csv = pd.DataFrame(columns=['mix'] +
                                 list(music_list[0].targets.keys()))
    # p = multiprocessing.Pool(6)
    rows = parmap.map(process_audio,
                      music_list,
                      processed_csv,
                      pm_pbar=True,
                      pm_processes=n_workers,
                      path_to_save=path_to_save,
                      n_fft=n_fft,
                      resample_rate=resample_rate,
                      hop_length=hop_length,
                      slice_duration=slice_duration)
    for r in rows:
        for n in r:
            processed_csv.loc[len(processed_csv)] = n

    processed_csv.to_csv(processed_csv_path, index=False)
Ejemplo n.º 11
0
def eval_mus_dir(dataset, estimates_dir, output_dir=None, *args, **kwargs):
    """Run musdb.run for the purpose of evaluation of musdb estimate dir

    Parameters
    ----------
    dataset : DB(object)
        Musdb Database object.
    estimates_dir : str
        Path to estimates folder.
    output_dir : str
        Output folder where evaluation json files are stored.
    *args
        Variable length argument list for `musdb.run()`.
    **kwargs
        Arbitrary keyword arguments for `musdb.run()`.
    """
    # create a new musdb instance for estimates with the same file structure
    est = musdb.DB(root_dir=estimates_dir, is_wav=True)
    # load all estimates track_names
    est_tracks = est.load_mus_tracks()
    # get a list of track names
    tracknames = [t.name for t in est_tracks]
    # load only musdb tracks where we have estimate tracks
    tracks = dataset.load_mus_tracks(tracknames=tracknames)
    # wrap the estimate loader
    run_fun = functools.partial(_load_track_estimates,
                                estimates_dir=estimates_dir,
                                output_dir=output_dir)
    # evaluate tracks
    dataset.run(run_fun, estimates_dir=None, tracks=tracks, *args, **kwargs)
    def __init__(
        self,
        frame_length=3,
        sample_rate=44100,
        num_worker=1,
        MUSDB18_PATH="",
        BIG_DATA=False,
        additional_background_data=[],
        additional_vocal_data=[],
    ):
        np.random.seed(1)
        self.sample_rate = sample_rate
        self.wh = WaveHandler()
        self.BIG_DATA = BIG_DATA
        self.music_folders = []
        for each in additional_background_data:
            self.music_folders += self.readList(each)
        self.vocal_folders = []
        for each in additional_vocal_data:
            self.vocal_folders += self.readList(each)
        self.frame_length = frame_length
        self.bac_file_num = len(self.music_folders)
        self.voc_file_num = len(self.vocal_folders)

        self.num_worker = num_worker
        self.mus = musdb.DB(MUSDB18_PATH, is_wav=True, subsets='train')
        self.pitch_shift_high = [2.0, 3.0, 4.0, 5.0, 6.0, 7.0]
        self.pitch_shift_low = [-2.0, -3.0, -4.0, -5.0, -6.0, -7.0]
def score(dataset='musdb18'):

    if dataset == 'musdb18':
        # initiate musdb
        mus = musdb.DB(root_dir="/data/musdb18")
        tracks = mus.load_mus_tracks(
            tracknames=['Music Delta - Gospel',
                        'Music Delta - Reggae',
                        'Music Delta - Rock',
                        'ANiMAL - Rockshow',
                        "Actions - Devil's Words",
                        'Johnny Lokke - Whisper To A Scream',
                        'Auctioneer - Our Future Faces',
                        'St Vitus - Word Gets Around',
                        'Strand Of Oaks - Spacestation',
                        'Sweet Lights - You Let Me Down'])

    elif dataset == 'medleydb':
        with open('preprocessing/resources/index_simple_test00.json', 'r') as f:
            test_songs = json.load(f)
        dataset_subset = mdb.load_multitracks(test_songs['id'])
        tracks = []
        for mtrack in dataset_subset:
            tracks.append(MedleyTrack(mtrack))

    # compute IBM stats
    datas = []
    for track in tracks:
        estimates, data = IRM(track, alpha=2)
        datas.append(data)

    return datas
Ejemplo n.º 14
0
    def __init__(self,
                 batch_size,
                 num_leading_frames,
                 num_trailing_frames,
                 frame_length=1024,
                 frame_step=512,
                 target_stem_id=1):

        # download the db if needed
        self._ensure_dataset_exists_locally()
        self.database = musdb.DB(self.data_dirname() / 'musdb18')

        # configuration
        self.batch_size = batch_size
        self.num_leading_frames = num_leading_frames
        self.num_trailing_frames = num_trailing_frames
        self.frame_length = frame_length
        self.frame_step = frame_step
        self.target_stem_id = target_stem_id

        # expose dataset args for network shape
        num_fft_bins = (self.frame_length // 2) + 1
        frame_len_with_context = self.num_leading_frames + self.num_trailing_frames + 1
        self.input_shape = (num_fft_bins * 2 * frame_len_with_context, )
        self.output_shape = (num_fft_bins * 2, )

        # internal state
        self.train_tracks = None
        self.test_tracks = None
        self.track_idx = None
        self.track_batch_idx = 0
        self.current_track = None
        self.padded_x_stft = None
        self.padded_y_stft = None
Ejemplo n.º 15
0
def make_dataset(data_path, mode=None):
    try:
        mixture_array = sa.attach(f"shm://{mode}_mixture_array")
        vocal_array = sa.attach(f"shm://{mode}_vocal_array")

    except:
        mus = musdb.DB(root=data_path, is_wav=True, subsets=mode)
        mixture_list = list()
        vocal_list = list()
        for track in tqdm(mus):
            #mixture_list.append(track.audio.sum(axis=-1))
            mixture_list.append(norm(track.audio)[0])
            #vocal_list.append(track.targets['vocals'].audio.sum(axis=-1))
            vocal_list.append(norm(track.targets['vocals'].audio)[0])
        mixture_array = np.concatenate(mixture_list)
        vocal_array = np.concatenate(vocal_list)

        assert mixture_array.shape == vocal_array.shape

        mixture_array_sa = sa.create(f"shm://{mode}_mixture_array",
                                     mixture_array.shape)
        vocal_array_sa = sa.create(f"shm://{mode}_vocal_array",
                                   vocal_array.shape)
        mixture_array_sa[::] = mixture_array
        vocal_array_sa[::] = vocal_array

    return dict(mixture_array=mixture_array, vocal_array=vocal_array)
Ejemplo n.º 16
0
def loadSet(subset='train'):
    '''
        I did this so later we can directly call this function instead of running another piece of code
    '''
    # init objects
    mus = musdb.DB(root_dir='../MUS-STEMS-SAMPLE')
    db = loadDatabase()

    ### choose subset 'train' or 'test' or both (don't send any argument)
    track_list = db.chooseSubset(subset)

    #print 'track-list ', track_list
    # check it's correctly working
    #assert mus.test(db.loadTestTrack), "doesn't work"
    if subset is 'train':
        tracks = mus.run(db.loadTrainTrack,
                         estimates_dir='./Estimates',
                         tracks=track_list,
                         subsets=subset)
        for i in tracks:
            print i  # why does this return None?
    else:
        tracks = mus.run(db.loadTestTrack,
                         estimates_dir='./Estimates',
                         tracks=track_list,
                         subsets=subset)
Ejemplo n.º 17
0
def compute_musdb_metrics(musdb_root, subset, result_root, metrics_root,
                          instruments, metrics):
    dataset = musdb.DB(root=musdb_root, is_wav=True, subsets=[subset])
    museval.eval_mus_dir(dataset=dataset,
                         estimates_dir=result_root,
                         output_dir=metrics_root)

    songs = glob(os.path.join(metrics_root, subset, '*.json'))
    metrics = {
        instrument: {k: []
                     for k in metrics}
        for instrument in instruments
    }
    for song in songs:
        with open(song, 'r') as stream:
            data = json.load(stream)
        for target in data['targets']:
            instrument = target['name']
            for metric in metrics:
                sdr_med = np.median([
                    frame['metrics'][metric] for frame in target['frames']
                    if not np.isnan(frame['metrics'][metric])
                ])
                metrics[instrument][metric].append(sdr_med)
    return metrics
    def __init__(
            self,
            target='vocals',
            root=None,
            download=False,
            is_wav=False,
            subsets='train',
            split='train',
            seq_duration=6.0,
            samples_per_track=64,  #64,
            source_augmentations=lambda audio: audio,
            random_track_mix=False,
            dtype=torch.float32,
            seed=42,
            *args,
            **kwargs):

        random.seed(seed)
        self.is_wav = is_wav
        self.seq_duration = seq_duration
        self.target = target
        self.subsets = subsets
        self.split = split
        self.samples_per_track = samples_per_track
        self.source_augmentations = source_augmentations
        self.random_track_mix = random_track_mix
        self.mus = musdb.DB(root=root,
                            is_wav=is_wav,
                            split=split,
                            subsets=subsets,
                            download=download,
                            *args,
                            **kwargs)
        self.sample_rate = 44100  # musdb is fixed sample rate
        self.dtype = dtype
Ejemplo n.º 19
0
def test_audio_regression():
    """test audio loading capabilities"""
    mus = musdb.DB(download=True)
    s = 0
    for track in mus:
        s += track.audio.sum()

    assert np.allclose(s, -24778.126983642578)
Ejemplo n.º 20
0
def evaluate(
    adapter: str = AudioAdapterOption,
    output_path: Path = AudioOutputOption,
    stft_backend: STFTBackend = AudioSTFTBackendOption,
    params_filename: str = ModelParametersOption,
    mus_dir: Path = MUSDBDirectoryOption,
    mwf: bool = MWFOption,
    verbose: bool = VerboseOption,
) -> Dict:
    """
    Evaluate a model on the musDB test dataset
    """
    import numpy as np

    configure_logger(verbose)
    try:
        import musdb
        import museval
    except ImportError:
        logger.error("Extra dependencies musdb and museval not found")
        logger.error("Please install musdb and museval first, abort")
        raise Exit(10)
    # Separate musdb sources.
    songs = glob(join(mus_dir, EVALUATION_SPLIT, "*/"))
    mixtures = [join(song, EVALUATION_MIXTURE) for song in songs]
    audio_output_directory = join(output_path, EVALUATION_AUDIO_DIRECTORY)
    separate(
        deprecated_files=None,
        files=mixtures,
        adapter=adapter,
        bitrate="128k",
        codec=Codec.WAV,
        duration=600.0,
        offset=0,
        output_path=join(audio_output_directory, EVALUATION_SPLIT),
        stft_backend=stft_backend,
        filename_format="{foldername}/{instrument}.{codec}",
        params_filename=params_filename,
        mwf=mwf,
        verbose=verbose,
    )
    # Compute metrics with musdb.
    metrics_output_directory = join(output_path, EVALUATION_METRICS_DIRECTORY)
    logger.info("Starting musdb evaluation (this could be long) ...")
    dataset = musdb.DB(root=mus_dir, is_wav=True, subsets=[EVALUATION_SPLIT])
    museval.eval_mus_dir(
        dataset=dataset,
        estimates_dir=audio_output_directory,
        output_dir=metrics_output_directory,
    )
    logger.info("musdb evaluation done")
    # Compute and pretty print median metrics.
    metrics = _compile_metrics(metrics_output_directory)
    for instrument, metric in metrics.items():
        logger.info(f"{instrument}:")
        for metric, value in metric.items():
            logger.info(f"{metric}: {np.median(value):.3f}")
    return metrics
Ejemplo n.º 21
0
def main():
    source_names = ["drums", "bass", "other", "vocals"]
    random_order = True
    np.random.seed(42)
    channels = [0, 1]

    path = '/tsi/doctorants/gcantisani/Datasets/MUSDB18/'
    new_path = '/tsi/doctorants/gcantisani/Datasets/MUSDB18_UGOSA/'
    os.makedirs(new_path, exist_ok=True)

    # Iterate over all the tracks in the test set
    test_set = musdb.DB(root=path, subsets=["test"], is_wav=False)
    for idx in range(len(test_set)):
        track = test_set.tracks[idx]
        print('-------------------')
        print(idx, str(track.name))

        # copy the track object and associate the new path
        new_track = cp(track)
        new_track.path = os.path.join(new_path, track.subset, track.name)
        os.makedirs(os.path.join(new_path, track.subset, track.name),
                    exist_ok=True)

        # generate a random order of sources
        if random_order:
            sources = random.sample(source_names, 4)
            print(sources)

        # Load the mixture, make STFT, divide it into a number of
        # segments equal to the number of sources and make ISTFT
        # Transoform to STFT and then back to have smoothing at boarders
        linear_mixture = track.targets['linear_mixture'].audio
        stft_mixture = librosa.stft(linear_mixture[:, 0])
        segment_len = stft_mixture.shape[1] // len(source_names)

        new_references = []
        for t, name in enumerate(sources):
            audio = track.targets[name].audio
            audio_new = np.zeros_like(audio)
            win = slice(t * segment_len, (t + 1) * segment_len)
            if t == len(source_names) - 1:
                win = slice(t * segment_len, stft_mixture.shape[1])

            for ch in channels:
                stft = librosa.stft(audio[:, ch])
                stft[:, win] = 0
                istft = librosa.istft(stft)
                audio_new[:, ch] = istft

            new_track.sources[name].audio = audio_new
            sf.write(os.path.join(new_track.path, name + '.wav'), audio_new,
                     track.rate)

        new_references = np.stack(
            [new_track.sources[name].audio for name in source_names])
        audio_mix = new_references.sum(0)
        sf.write(os.path.join(new_track.path, 'mixture.wav'), audio_mix,
                 track.rate)
Ejemplo n.º 22
0
def getMUSDB(database_path):
    mus = musdb.DB(root_dir=database_path, is_wav=False)

    subsets = list()

    for subset in ["train", "test"]:
        tracks = mus.load_mus_tracks(subset)
        samples = list()

        # Go through tracks
        for track in tracks:
            # Skip track if mixture is already written, assuming this track is done already
            track_path = track.path[:-4]
            mix_path = track_path + "_mix.wav"
            acc_path = track_path + "_accompaniment.wav"
            if os.path.exists(mix_path):
                print("WARNING: Skipping track " + mix_path + " since it exists already")

                # Add paths and then skip
                paths = {"mix" : mix_path, "accompaniment" : acc_path}
                paths.update({key : track_path + "_" + key + ".wav" for key in ["bass", "drums", "other", "vocals"]})

                samples.append(paths)

                continue

            rate = track.rate

            # Go through each instrument
            paths = dict()
            stem_audio = dict()
            for stem in ["bass", "drums", "other", "vocals"]:
                path = track_path + "_" + stem + ".wav"
                audio = track.targets[stem].audio
                soundfile.write(path, audio, rate, "PCM_16")
                stem_audio[stem] = audio
                paths[stem] = path

            # Add other instruments to form accompaniment
            acc_audio = np.clip(sum([stem_audio[key] for key in list(stem_audio.keys()) if key != "vocals"]), -1.0, 1.0)
            soundfile.write(acc_path, acc_audio, rate, "PCM_16")
            paths["accompaniment"] = acc_path

            # Create mixture
            mix_audio = track.audio
            soundfile.write(mix_path, mix_audio, rate, "PCM_16")
            paths["mix"] = mix_path

            diff_signal = np.abs(mix_audio - acc_audio - stem_audio["vocals"])
            print("Maximum absolute deviation from source additivity constraint: " + str(np.max(diff_signal)))# Check if acc+vocals=mix
            print("Mean absolute deviation from source additivity constraint:    " + str(np.mean(diff_signal)))

            samples.append(paths)

        subsets.append(samples)

    return subsets
Ejemplo n.º 23
0
def getMUSDB(database_path):
    mus = musdb.DB(root_dir=database_path, is_wav=False)

    subsets = list()

    for subset in ["train", "test"]:
        tracks = mus.load_mus_tracks(subset)
        samples = list()

        for track in tracks:
            # Get mix and instruments
            # Bass
            bass_path = track.path[:-4] + "_bass.wav"
            bass_audio = track.sources["bass"].audio
            write_wav_skip_existing(bass_path, bass_audio, track.rate)
            bass = Sample(bass_path, track.rate, track.audio.shape[1], track.duration)

            # Drums
            drums_path = track.path[:-4] + "_drums.wav"
            drums_audio = track.sources["drums"].audio
            write_wav_skip_existing(drums_path, drums_audio, track.rate)
            drums = Sample(drums_path, track.rate, track.audio.shape[1], track.duration)

            # Other
            other_path = track.path[:-4] + "_other.wav"
            other_audio = track.sources["other"].audio
            write_wav_skip_existing(other_path, other_audio, track.rate)
            other = Sample(other_path, track.rate, track.audio.shape[1], track.duration)

            # Vocals
            vocal_path = track.path[:-4] + "_vocals.wav"
            vocal_audio = track.sources["vocals"].audio
            write_wav_skip_existing(vocal_path, vocal_audio, track.rate)
            vocal = Sample(vocal_path, track.rate, track.audio.shape[1], track.duration)

            # Add other instruments to form accompaniment
            acc_audio = track.sources["drums"].audio + track.sources["bass"].audio + track.sources["other"].audio
            acc_path = track.path[:-4] + "_accompaniment.wav"
            write_wav_skip_existing(acc_path, acc_audio, track.rate)
            acc = Sample(acc_path, track.rate, track.audio.shape[1], track.duration)

            # Create mixture
            mix_path = track.path[:-4] + "_mix.wav"
            mix_audio = track.audio
            write_wav_skip_existing(mix_path, mix_audio, track.rate)
            mix = Sample(mix_path, track.rate, track.audio.shape[1], track.duration)

            diff_signal = np.abs(mix_audio - bass_audio - drums_audio - other_audio - vocal_audio)
            print("Maximum absolute deviation from source additivity constraint: " + str(np.max(diff_signal)))# Check if acc+vocals=mix
            print("Mean absolute deviation from source additivity constraint:    " + str(np.mean(diff_signal)))

            samples.append((mix, acc, bass, drums, other, vocal)) # Collect all sources for now. Later on for SVS: [mix, acc, vocal] Multi-instrument: [mix, bass, drums, other, vocals]

        subsets.append(samples)

    return subsets
Ejemplo n.º 24
0
def test_download_and_validation():
    mus_all = musdb.DB(download=True)

    assert len(mus_all) == 144

    for track in mus_all:
        assert track.audio.shape[1] > 0
        assert track.audio.shape[-1] == 2
        assert track.stems.shape[0] == 5

    mus_test = musdb.DB(download=True, subsets='test', split=None)
    assert len(mus_test) == 50

    mus_train = musdb.DB(download=True, subsets='train', split='train')
    assert len(mus_train) == 80
    # test validation set
    mus_valid = musdb.DB(download=True, subsets='train', split='valid')
    assert len(mus_valid) == 14

    assert len(mus_train) == len(mus_all) - len(mus_test) - len(mus_valid)
Ejemplo n.º 25
0
 def __init__(self, param: DatasetParam, musdb_subsets: str = 'train', max_decoded: int = 100, **kwargs):
     if max_decoded < 1:
         raise ValueError("max_decoded must be greater than 0!")
     self.param = param
     self.tracks = list(
         musdb.DB(root=param.dataset_path, subsets=musdb_subsets))
     self.num_tracks = len(self.tracks)
     self.decoded = [None] * self.num_tracks
     self.num_decoded = 0
     self.max_decoded = max_decoded
     self.ord_decoded = [-1] * self.num_tracks
     self.next_ord = 0
Ejemplo n.º 26
0
def make_dataset(data_path, mode=None):
    mus = musdb.DB(root=data_path, is_wav=True, subsets="test")
    mixture_list = list()
    vocal_list = list()
    for track in tqdm(mus):
        mixture_list.append(track.audio.sum(axis=-1))
        vocal_list.append(track.targets['vocals'].audio.sum(axis=-1))
    mixture_array = np.concatenate(mixture_list)
    vocal_array = np.concatenate(vocal_list)
    assert mixture_array.shape == vocal_array.shape

    return dict(mixture_array=mixture_array, vocal_array=vocal_array)
Ejemplo n.º 27
0
    def __init__(self, musdb_root='data/musdb18_wav/', is_wav=True):
        self.musdb_train = musdb.DB(root=musdb_root,
                                    subsets="train",
                                    split='train',
                                    is_wav=is_wav)
        self.musdb_valid = musdb.DB(root=musdb_root,
                                    subsets="train",
                                    split='valid',
                                    is_wav=is_wav)
        self.musdb_test = musdb.DB(root=musdb_root,
                                   subsets="test",
                                   is_wav=is_wav)

        if len(self.musdb_train) > 0:
            pass
        else:
            raise FileNotFoundError(
                '\n1. musdb_root does exist or is not a valid path. \n'
                '2. please try a valid path for --musdb_root \n'
                '3. You should also check whether the given musdb path is wave-filed version'
                ' (i.e., --musdb_is_wav True) or not.')
Ejemplo n.º 28
0
def main():
    parser = argparse.ArgumentParser('rawset')
    parser.add_argument('--workers', type=int, default=10)
    parser.add_argument('--samplerate', type=int, default=44100)
    parser.add_argument('--channels', type=int, default=2)
    parser.add_argument('musdb', type=Path)
    parser.add_argument('destination', type=Path)

    args = parser.parse_args()

    build_raw(musdb.DB(root=args.musdb, subsets=["train"], split="train"),
              args.destination / "train",
              normalize=True,
              channels=args.channels,
              samplerate=args.samplerate,
              workers=args.workers)
    build_raw(musdb.DB(root=args.musdb, subsets=["train"], split="valid"),
              args.destination / "valid",
              normalize=True,
              samplerate=args.samplerate,
              channels=args.channels,
              workers=args.workers)
Ejemplo n.º 29
0
def produce_musdb_source_estimates(model_config, load_model, musdb_path, output_path, subsets=None):
    '''
    Predicts source estimates for MUSDB for a given model checkpoint and configuration, and evaluate them.
    :param model_config: Model configuration of the model to be evaluated
    :param load_model: Model checkpoint path
    :return: 
    '''
    print("Evaluating trained model saved at " + str(load_model)+ " on MUSDB and saving source estimate audio to " + str(output_path))

    mus = musdb.DB(root_dir=musdb_path)
    predict_fun = lambda track : predict(track, model_config, load_model, output_path)
    assert(mus.test(predict_fun))
    mus.run(predict_fun, estimates_dir=output_path, subsets=subsets)
Ejemplo n.º 30
0
def produce_source_estimates(model_config, load_model, musdb_path, output_path, subsets=None):
    '''
    Predicts source estimates for MUSDB for a given model checkpoint and configuration, and evaluate them.
    :param model_config: Model configuration of the model to be evaluated
    :param load_model: Model checkpoint path
    :return: 
    '''
    prediction_parameters = [model_config, load_model]
    with open("prediction_params.pkl", "wb") as file:
        pickle.dump(prediction_parameters, file)

    mus = musdb.DB(root_dir=musdb_path)
    #if mus.test(predict):
    #    print "Function is valid"
    mus.run(predict, estimates_dir=output_path, subsets=subsets)