Ejemplo n.º 1
0
def load_jsb(path):
    mkdir_p(path)
    d = os.path.join(path, 'JSB Chorales')
    if not os.path.isdir(d):
        download_jsb(path)

    train_filenames = os.path.join(path, 'JSB Chorales', 'train', '*.mid')
    valid_filenames = os.path.join(path, 'JSB Chorales', 'valid', '*.mid')
    test_filenames = os.path.join(path, 'JSB Chorales', 'test', '*.mid')
    train_files = glob.glob(train_filenames)
    valid_files = glob.glob(valid_filenames)
    test_files = glob.glob(test_filenames)

    train_datasets = [
        midiread(f, r=(21, 109),
                 dt=0.3).piano_roll.astype(theano.config.floatX)
        for f in train_files
    ]
    valid_datasets = [
        midiread(f, r=(21, 109),
                 dt=0.3).piano_roll.astype(theano.config.floatX)
        for f in valid_files
    ]
    test_datasets = [
        midiread(f, r=(21, 109),
                 dt=0.3).piano_roll.astype(theano.config.floatX)
        for f in test_files
    ]

    return (train_datasets, [None]), (valid_datasets, [None]), (test_datasets,
                                                                [None])
Ejemplo n.º 2
0
	def train(self, file_name, weight_save_file, batch_size=1, num_epoch=200):
                print('load data ---------------')

                file_train=os.path.join(os.path.split(os.path.dirname(__file__))[0],
                                'data',file_name,'train','*.mid')
                dataset = [midiread(f, self.r, self.dt).piano_roll.astype(theano.config.floatX) for f in glob.glob(file_train)]

                file_test=os.path.join(os.path.split(os.path.dirname(__file__))[0],
                                'data',file_name,'test','*.mid')
                testdataset = [midiread(f, self.r, self.dt).piano_roll.astype(theano.config.floatX) for f in glob.glob(file_test)]
                print('load done --------------')
                try:
                        for epoch in range(num_epoch):
                                t0 = time.time()
                                numpy.random.shuffle(dataset)
                                costs = []
                                accuracys = []
                                for s, sequence in enumerate(dataset):
                                        y = numpy.hstack((sequence,numpy.zeros((sequence.shape[0],1)) ))
                                        x = numpy.roll(y, 1, axis=0)
                                        x[0,:]=0
                                        x[0,self.maxFeatures-1]=1
                                        cost, accuracy= self.rnnModel.train_on_batch(numpy.array([x]), numpy.array([y]), accuracy=True)
                                        costs.append(cost)
                                        accuracys.append(accuracy)

                                print('epoch: %i/%i\tcost: %.5f\taccu: %.5f\ttime: %.4f s' % (epoch+1, num_epoch, numpy.mean(costs), numpy.mean(accuracys),time.time()-t0))
                                sys.stdout.flush()
                                test_accu=self.evaluate(testdataset)
                                print('test_accu: %.5f' % ( numpy.mean(test_accu)) )
                        self.rnnModel.save_weights(weight_save_file)
                except KeyboardInterrupt:
                        print('interrupt by user !')
Ejemplo n.º 3
0
def load_generation(N, name):
    re_gen = os.path.join(os.path.split(os.path.dirname(__file__))[0], name, '*.mid')
    files_gen = glob.glob(re_gen)
    dataset_gen = [midiread(f, (21,109), 0.3).piano_roll.astype(theano.config.floatX) for f in files_gen]

    all_gram_gen = dict()
    for i in range(N):
        all_gram_gen[i+1]=dict()

    for i in range(N):
        n = i+1
        n_gram = all_gram_gen.get(n)
        for s, sequence in enumerate(dataset_gen):
            count = dict()
            for l in range(len(sequence)-n+1):
                gram = sequence[l : l+n]
                if count.has_key(str(gram)):
                    count[str(gram)] += 1
                else:
                    count[str(gram)] = 1
            for k in count:
                if n_gram.has_key(k):
                    n_gram[k]=max(count.get(k), n_gram.get(k))
                else:
                    n_gram[k]=count.get(k)
    return all_gram_gen
Ejemplo n.º 4
0
    def train(self, files, batch_size=100, num_epochs=200):
        '''Train the RNN-RBM via stochastic gradient descent (SGD) using MIDI
        files converted to piano-rolls.

        files : list of strings
            List of MIDI files that will be loaded as piano-rolls for training.
        batch_size : integer
            Training sequences will be split into subsequences of at most this
            size before applying the SGD updates.
        num_epochs : integer
            Number of epochs (pass over the training set) performed. The user
            can safely interrupt training with Ctrl+C at any time.'''

        assert len(files) > 0, 'Training set is empty!' \
                               ' (did you download the data files?)'
        dataset = [midiread(f, self.r,
                            self.dt).piano_roll.astype(theano.config.floatX)
                   for f in files]

        try:
            for epoch in range(num_epochs):
                numpy.random.shuffle(dataset)
                costs = []

                for s, sequence in enumerate(dataset):
                    for i in range(0, len(sequence), batch_size):
                        cost = self.train_function(sequence[i:i + batch_size])
                        costs.append(cost)

                print('Epoch %i/%i' % (epoch + 1, num_epochs))
                print(numpy.mean(costs))
                sys.stdout.flush()

        except KeyboardInterrupt:
            print('Interrupted by user.')
Ejemplo n.º 5
0
def modeling_n_gram(n, files):
    
	assert len(files) > 0, 'Training set is empty!' \
					' (did you download the data files?)'
					
	for f in files:

		print 'parsing', f

		each = midiread(f).piano_roll.astype(theano.config.floatX)
		numNote = len(each[0])

		# each [ time ] [ note ]
	
		for timeSlice in range(n-1, len(each)):
			for noteDest in range(numNote):
				valueDest = int(each[timeSlice][noteDest])
				for noteFrom in range(numNote):
					valueFrom = int(each[timeSlice-1][noteFrom])
					
					#print noteDest,valueDest,noteFrom,valueFrom
					
					probability[noteDest][valueDest][noteFrom][valueFrom] \
						= probability[noteDest][valueDest][noteFrom][valueFrom] + 1.0
						
	print 'learning_done'
	
	pkl.dump(probability, open("bi-gram-count.dat", "wb"))
	
	print 'bi-gram-count.dat saved'
Ejemplo n.º 6
0
    def train(self, files, batch_size=100, num_epochs=200):
        '''Train the RNN-RBM via stochastic gradient descent (SGD) using MIDI
        files converted to piano-rolls.

        files : list of strings --- List of MIDI files that will be loaded as piano-rolls for training.
        batch_size : integer    --- Training sequences will be split into subsequences of at most this size before applying the SGD updates.
        num_epochs : integer    --- Number of epochs (pass over the training set) performed. The user can safely interrupt training with Ctrl+C at any time.'''

        assert len(files) > 0, 'Training set is empty!' \
                               ' (did you download the data files?)'
        dataset = [midiread(f, self.r,
                            self.dt).piano_roll.astype(theano.config.floatX)
                   for f in files]

        try:
            for epoch in xrange(num_epochs):
                numpy.random.shuffle(dataset)
                costs = []

                for s, sequence in enumerate(dataset):
                    for i in xrange(0, len(sequence), batch_size):
                        cost = self.train_function(sequence[i:i + batch_size])
                        costs.append(cost)

                print 'Epoch %i/%i' % (epoch + 1, num_epochs),
                print numpy.mean(costs)
                sys.stdout.flush()

        except KeyboardInterrupt:
            print 'Interrupted by user.'
Ejemplo n.º 7
0
def train(r, dt, files, f_grad_shared, f_grad_update, f_pred, \
batch_size=100, num_epochs=20, lr = 1E-6):

    assert len(files) > 0, 'Training set is empty!' \
                           ' (did you download the data files?)'
    dataset = [
        midiread(f, r, dt).piano_roll.astype(theano.config.floatX)
        for f in files
    ]

    #    try:
    seq = None
    for epoch in range(num_epochs):
        numpy.random.shuffle(dataset)
        costs = []

        for s, sequence in enumerate(dataset):
            for i in range(0, len(sequence), batch_size):
                if i + 1 + batch_size < len(sequence):
                    loss = f_grad_shared(sequence[i:i + batch_size],
                                         sequence[i + 1:i + batch_size + 1])
                    costs.append(loss)
                    f_grad_update(lr)
                    if seq is None:
                        seq = sequence[i:i + batch_size]

        print('Epoch %i/%i' % (epoch + 1, num_epochs))
        print(numpy.mean(costs))
        sys.stdout.flush()

        numpy.savetxt('matrix.txt', seq, delimiter=',')
    return seq
Ejemplo n.º 8
0
    def load_dataset(self):
        re = ["data/JSB Chorales/train/*.mid"]
        re.append("data/JSB Chorales/test/*.mid")
        re.append("data/JSB Chorales/valid/*.mid")
        files = []
        for r in re:
            files += glob.glob(r)
        assert(len(files) > 0)
        print "generating dataset..."
        dataset = [midiread(f, self.r, self.dt).piano_roll.astype(theano.config.floatX) for f in files]
    
        memorization_dataset = [[]]  # memorize the first unit for 100 time-steps with binary noise

        n = 0

        for seq in dataset:
            for i in range(0, len(seq), self.seq_length):
                memorization_dataset[0].append(seq[i:i + self.seq_length])

        while False: #n < 100000:
            sequence = random.choice(dataset)
            if len(sequence) < self.seq_length:
                print " to short !"
            i = random.choice(range(0, len(sequence), self.seq_length))
            memorization_dataset[0].append(sequence[i:i + self.seq_length])
            n = n + 1

        print "number of sequence for training : ", len(memorization_dataset[0])

        self.train = [memorization_dataset[0][:-1000]]
        self.valid = [memorization_dataset[0][-1000:]]

        self.gradient_dataset = SequenceDataset(self.train, batch_size=None, number_batches=1000)
        self.cg_dataset = SequenceDataset(self.train, batch_size=None, number_batches=500)
        self.valid_dataset = SequenceDataset(self.valid, batch_size=None, number_batches=500)
Ejemplo n.º 9
0
def load_dataset(N, name):
    re = os.path.join(os.path.split(os.path.dirname(__file__))[0], 'data', name, 'train', '*.mid')
    files = glob.glob(re)
    dataset = [midiread(f, (21,109), 0.3).piano_roll.astype(theano.config.floatX) for f in files]

    all_gram = dict()
    for i in range(N):
        all_gram[i+1]=dict()

    for i in range(N):
        n = i+1
        print ('======'+str(n)+'======')
        n_gram = all_gram.get(n)
        for s, sequence in enumerate(dataset):
            print (s)
            count = dict()
            for l in range(len(sequence)-n+1):
                gram = sequence[l : l+n]
                if count.has_key(str(gram)):
                    count[str(gram)] += 1
                else:
                    count[str(gram)] = 1
            for k in count:
                if n_gram.has_key(k):
                    n_gram[k]=max(count.get(k), n_gram.get(k))
                else:
                    n_gram[k]=count.get(k)
    return all_gram
Ejemplo n.º 10
0
	def __init__(self, dir_path, set_name, pitch_range, dt, batch_size, shuffle=True):
		self.dir_path = dir_path
		self.set_name = set_name
		self.pitch_range = pitch_range
		self.dt = dt
		self.batch_size = batch_size
		self.shuffle = shuffle

		self.curr_epoch = 0
		self.batch_idx = 0

		name_list = os.listdir(self.dir_path+'/'+self.set_name)
		self.data_num = len(name_list)
		self.data = []
		for m in name_list:
			f = self.dir_path+'/'+self.set_name+'/'+m
			wave = midiread(f, self.pitch_range, self.dt)
			self.data.append(wave.piano_roll)
		self.data_list = np.array(range(self.data_num))
		if self.shuffle:
			np.random.shuffle(self.data_list)

		if set_name=='train':
			self.batch_num = self.data_num/self.batch_size
		else:
			self.batch_num = int(np.ceil(1.0*self.data_num/self.batch_size))
Ejemplo n.º 11
0
    def train(self, file_name, weight_save_file, batch_size=1, num_epoch=200):
        print('load data ---------------')

        file_train = os.path.join(
            os.path.split(os.path.dirname(__file__))[0], 'data', file_name,
            'train', '*.mid')
        dataset = [
            midiread(f, self.r,
                     self.dt).piano_roll.astype(theano.config.floatX)
            for f in glob.glob(file_train)
        ]

        file_test = os.path.join(
            os.path.split(os.path.dirname(__file__))[0], 'data', file_name,
            'test', '*.mid')
        testdataset = [
            midiread(f, self.r,
                     self.dt).piano_roll.astype(theano.config.floatX)
            for f in glob.glob(file_test)
        ]
        print('load done --------------')
        try:
            for epoch in range(num_epoch):
                t0 = time.time()
                numpy.random.shuffle(dataset)
                costs = []
                accuracys = []
                for s, sequence in enumerate(dataset):
                    y = numpy.hstack(
                        (sequence, numpy.zeros((sequence.shape[0], 1))))
                    x = numpy.roll(y, 1, axis=0)
                    x[0, :] = 0
                    x[0, self.maxFeatures - 1] = 1
                    cost, accuracy = self.rnnModel.train_on_batch(
                        numpy.array([x]), numpy.array([y]), accuracy=True)
                    costs.append(cost)
                    accuracys.append(accuracy)

                print('epoch: %i/%i\tcost: %.5f\taccu: %.5f\ttime: %.4f s' %
                      (epoch + 1, num_epoch, numpy.mean(costs),
                       numpy.mean(accuracys), time.time() - t0))
                sys.stdout.flush()
                test_accu = self.evaluate(testdataset)
                print('test_accu: %.5f' % (numpy.mean(test_accu)))
            self.rnnModel.save_weights(weight_save_file)
        except KeyboardInterrupt:
            print('interrupt by user !')
Ejemplo n.º 12
0
    def train(self, files, batch_size=100, num_epochs=200):

        assert len(files) > 0, 'Training set is empty!' \
                               ' (did you download the data files?)'
        dataset = [midiread(f, self.r,
                            self.dt).piano_roll.astype(theano.config.floatX)
                   for f in files]

        def accuracy (v, v_sample):
            accs = []
            t, n = v.shape
            for time in range(t):
                tp = 0 # true positive
                fp = 0 # false positive
                fn = 0 # false negative
                for note in range(n):
                    if v[time][note] == 1 and v_sample[time][note] == 1:
                        tp += 1.
                    if v[time][note] == 0 and v_sample[time][note] == 1:
                        fp += 1.
                    if v[time][note] == 1 and v_sample[time][note] == 0:
                        fn += 1.
                if tp + fp + fn != 0:
                    a = tp / (tp + fp + fn)
                else:
                    a = 0
                accs.append(a)

            acc = numpy.mean(accs)
            return acc

        try:
            print ('lstm_rbm, dataset=Nottingham, lr=%f, epoch=%i' %(self.lr, num_epochs))

            for epoch in xrange(num_epochs):
                numpy.random.shuffle(dataset)
                costs = []
                accs = []

                for s, sequence in enumerate(dataset):
                    for i in xrange(0, len(sequence), batch_size):
                        v = sequence[i:i + batch_size]

                        (cost, v_sample) = self.train_function(v)
                        costs.append(cost)

                        acc = accuracy(v, v_sample)
                        accs.append(acc)

                p = 'Epoch %i/%i    LL %f   ACC %f  time %s' % (epoch + 1, num_epochs, numpy.mean(costs), numpy.mean(accs), datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
                print (p)
                if (epoch%100 == 0 or epoch==num_epochs-1):
                    piano_roll = self.generate_function()
                    midiwrite('sample/lstm_rbm%i.mid' %(epoch), piano_roll, self.r, self.dt)
                sys.stdout.flush()

        except KeyboardInterrupt:
            print 'Interrupted by user.'
Ejemplo n.º 13
0
def load_jsb(path):
    mkdir_p(path)
    d = os.path.join(path, 'JSB Chorales')
    if not os.path.isdir(d):
        download_jsb(path)

    train_filenames = os.path.join(path, 'JSB Chorales', 'train', '*.mid')
    valid_filenames = os.path.join(path, 'JSB Chorales', 'valid', '*.mid')
    test_filenames = os.path.join(path, 'JSB Chorales', 'test', '*.mid')
    train_files = glob.glob(train_filenames)
    valid_files = glob.glob(valid_filenames)
    test_files = glob.glob(test_filenames)

    train_datasets = [midiread(f, r=(21, 109), dt=0.3).piano_roll.astype(theano.config.floatX) for f in train_files]
    valid_datasets = [midiread(f, r=(21, 109), dt=0.3).piano_roll.astype(theano.config.floatX) for f in valid_files]
    test_datasets = [midiread(f, r=(21, 109), dt=0.3).piano_roll.astype(theano.config.floatX) for f in test_files]

    return (train_datasets, [None]), (valid_datasets, [None]), (test_datasets, [None])
Ejemplo n.º 14
0
def dataset_load(path,
r=(21, 109), #Key span
dt=0.3):     #Timestep for conversion into piano roll
	#Read files
	files = glob.glob('data/Nottingham/train/*')
	assert len(files) > 0, 'Training set is empty!' \
		                       ' (did you download the data files?)'
	pianorolls = [midiread(f, r, dt).piano_roll for f in files]
	#Concatenate 
	dataset = np.concatenate(pianorolls, axis = 0) #TODO add some zeros, so that one music piece is clearly different from another.
	return dataset
Ejemplo n.º 15
0
def load_midi_data(data_dir):
    import midi.utils as utils
    from midi import MidiInFile as mf
    from midi import MidiToText as mt

    f = open(data_dir, 'rb')
    midiIn = mf.MidiInFile(mt.MidiToText(), f)
    midiIn.read()
    f.close()

    midi_data = utils.midiread(data_dir, dt=0.5)
    return midi_data.piano_roll
Ejemplo n.º 16
0
def loadDataSet(files):
    #File e il path della carterlla contente i file (*.mid)
    assert len(files) > 0, 'Training set is empty!' \
                           ' (did you download the data files?)'
    #mi calcolo quel'el 'esempio di lunghezza massima
    maxLen=0
    dataset=[]
    length=[]
    for f in files:
        currentMidi=midiread(f, (21, 109),0.3).piano_roll.astype(np.float64)
        dataset.append(currentMidi)
        length.append(currentMidi.shape[0])
        if maxLen<currentMidi.shape[0]:
            maxLen=currentMidi.shape[0]
    print "MAXLEN: ",maxLen
    return dataset,maxLen,len(dataset),length
def LoadDataForPreTraining(r=(21, 109), dt=0.3):

    assert len(trainingSet) > 0, 'Training set is empty!' \
                           ' (did you download the data files?)'
    sampleLen=[]
    dataset=[]
    maxLen=0
    nSample=0
    for f in trainingSet:
        currentMidi=midiread(f, (21, 109),0.3).piano_roll
        dataset.append(currentMidi)
        sampleLen.append(currentMidi.shape[0])
        if maxLen< currentMidi.shape[0]:
            maxLen=currentMidi.shape[0]
        nSample=nSample+1
    return (dataset, sampleLen, nSample, maxLen)
Ejemplo n.º 18
0
    def train(self, files, batch_size=100, num_epochs=200):
        '''Train the RNN-RBM via stochastic gradient descent (SGD) using MIDI
        files converted to piano-rolls.

        files : list of strings
            List of MIDI files that will be loaded as piano-rolls for training.
        batch_size : integer
            Training sequences will be split into subsequences of at most this
            size before applying the SGD updates.
        num_epochs : integer
            Number of epochs (pass over the training set) performed. The user
            can safely interrupt training with Ctrl+C at any time.'''

        assert len(files) > 0, 'Training set is empty!' \
                               ' (did you download the data files?)'
        dataset = [
            midiread(f, self.r,
                     self.dt).piano_roll.astype(theano.config.floatX)
            for f in files
        ]
        print "Start training process of the recurrent network RBM machine with the given dataset...",
        print "Lenght of the Dataset: ", len(files)
        print "Interrupt if necessariy by pressing Ctrl+C",
        print "...Might take some time :) ..."
        costst = []
        try:
            for epoch in xrange(num_epochs):
                numpy.random.shuffle(dataset)
                costs = []

                for s, sequence in enumerate(dataset):
                    for i in xrange(0, len(sequence), batch_size):
                        cost = self.train_function(sequence[i:i + batch_size])
                        costs.append(cost)

                print 'Epoch %i/%i' % (epoch + 1, num_epochs),
                print "Current mean Energy cost:", numpy.mean(costs)
                costst.append(numpy.mean(costs))
                print "Training %i percent done; interrupt training by pressing Crtl+C." % (
                    float((float(epoch) + 1.0) * 100.0 / float(num_epochs)))
                sys.stdout.flush()

        except KeyboardInterrupt:
            print 'Training Interrupted.'

        return self, files, costst
Ejemplo n.º 19
0
def fetch_nottingham():
    url = "http://www.iro.umontreal.ca/~lisa/deep/data/Nottingham.zip"
    data_path = "Nottingham.zip"
    if not os.path.exists(data_path):
        download(url, data_path)
    key_range = (21, 109)
    dt = 0.3

    all_data = []
    with zipfile.ZipFile(data_path, "r") as f:
        for name in f.namelist():
            if ".mid" not in name:
                # Skip README
                continue
            data = midiread(f, key_range,
                            dt).piano_roll.astype(theano.config.floatX)
            all_data.extend(data)
    return key_range, dt, all_data
def loadDataSet(files):
    #File e il path della carterlla contente i file (*.mid)
    assert len(files) > 0, 'Training set is empty!' \
                           ' (did you download the data files?)'
    #mi calcolo quel'el 'esempio di lunghezza massima
    maxLen=0
    dataset=[]
    for f in files:
        currentMidi=midiread(f, (21, 109),0.3).piano_roll.astype(theano.config.floatX)
        dataset.append(currentMidi)
        if maxLen< currentMidi.shape[0]:
            maxLen=currentMidi.shape[0]
    #porto tutte le tracce a masima lunghezza aggiongendo silenzio
    for i, seq in enumerate(dataset):
            if seq.shape[0]<maxLen:
                dataset[i]=np.concatenate([seq, np.zeros((maxLen-seq.shape[0], 88))])
                        
    #print dataset[0].shape
    return np.array(dataset, dtype=theano.config.floatX)
Ejemplo n.º 21
0
    def train(self, files, batch_size=100, num_epochs=200):
        '''Train the RNN-RBM via stochastic gradient descent (SGD) using MIDI
        files converted to piano-rolls.

        files : list of strings
            List of MIDI files that will be loaded as piano-rolls for training.
        batch_size : integer
            Training sequences will be split into subsequences of at most this
            size before applying the SGD updates.
        num_epochs : integer
            Number of epochs (pass over the training set) performed. The user
            can safely interrupt training with Ctrl+C at any time.'''

        assert len(files) > 0, 'Training set is empty!' \
                               ' (did you download the data files?)'
        dataset = [midiread(f, self.r,
                            self.dt).piano_roll.astype(theano.config.floatX)
                   for f in files]
        print "Start training process of the recurrent network RBM machine with the given dataset..." ,
        print "Lenght of the Dataset: ", len(files)
        print "Interrupt if necessariy by pressing Ctrl+C",
        print "...Might take some time :) ..."
        costst = []
        try:
            for epoch in xrange(num_epochs):
                numpy.random.shuffle(dataset)
                costs = []

                for s, sequence in enumerate(dataset):
                    for i in xrange(0, len(sequence), batch_size):
                        cost = self.train_function(sequence[i:i + batch_size])
                        costs.append(cost)

                print 'Epoch %i/%i' % (epoch + 1, num_epochs),
                print "Current mean Energy cost:", numpy.mean(costs)
                costst.append(numpy.mean(costs))
                print "Training %i percent done; interrupt training by pressing Crtl+C." % (float((float(epoch) + 1.0)*100.0 / float(num_epochs)))
                sys.stdout.flush()

        except KeyboardInterrupt:
            print 'Training Interrupted.'
            
        return self, files, costst
Ejemplo n.º 22
0
def loadDataSetMin(files):
    #File e il path della carterlla contente i file (*.mid)
    assert len(files) > 0, 'Training set is empty!' \
                           ' (did you download the data files?)'
    #mi calcolo quel'el 'esempio di lunghezza massima
    minLen=sys.maxint
    dataset=[]
    for f in files:
        currentMidi=midiread(f, (21, 109),0.3).piano_roll.astype(theano.config.floatX)
        dataset.append(currentMidi)
        if minLen> currentMidi.shape[0]:
            minLen=currentMidi.shape[0]
    #porto tutte le tracce a masima lunghezza aggiongendo silenzio
    for i, seq in enumerate(dataset):
            if seq.shape[0]>minLen:
                dataset[i]=seq[0:minLen,:] 
                        
    #print dataset[0].shape
    #print "MINLEN: ", minLen
    return np.array(dataset, dtype=theano.config.floatX)
Ejemplo n.º 23
0
    def train(self, files, batch_size=500, num_epochs=300):
        assert len(files) > 0, 'Training set is empty!' \
                               ' (did you download the data files?)'
        dataset = [midiread(f, self.r,
                            self.dt).piano_roll.astype(theano.config.floatX)
                   for f in files]
        try:
            for epoch in xrange(num_epochs):
                numpy.random.shuffle(dataset)
                costs = []

                for s, sequence in enumerate(dataset):
                    for i in xrange(0, len(sequence), batch_size):
                        cost = self.train_function(sequence[i:i + batch_size])
                        costs.append(cost)

                print 'Epoch %i/%i' % (epoch + 1, num_epochs),
                print numpy.mean(costs)
                sys.stdout.flush()

        except KeyboardInterrupt:
            print 'Interrupted by user.'
Ejemplo n.º 24
0
def LoadDataForPreTraining(r=(21, 109), dt=0.3):

    assert len(trainingSet) > 0, 'Training set is empty!' \
                           ' (did you download the data files?)'
    sampleLen=[]
    dataset=[]
    maxLen=0
    nSample=0
    for f in trainingSet:
        
        currentMidi=midiread(f, (21, 109),0.3).piano_roll
        print f,": lenght= ",currentMidi.shape[0]
        sampleLen.append(currentMidi.shape[0])
        
        if maxLen< currentMidi.shape[0]:
            maxLen=currentMidi.shape[0]
        nSample=nSample+1
    print "#-----dataset data-----#"
    print "number of sample: ", len(trainingSet)
    print "max lenght:", maxLen
    print "# rows of matrix M:",sum(sampleLen)
    print "# columns of matrix M:", maxLen*88
    return (dataset, sampleLen, nSample, maxLen)
Ejemplo n.º 25
0
def load_dataset(style='Nottingham',
                 data_type='train',
                 max_length=256,
                 max_key_number=1):
    """
        Load dataset from files
        for mono case: max_key_number would be 1
        for poly case: max_key_number default is 4
    """
    file_paths = get_file_path(style, data_type)
    dataset = [
        midiread(f, R, DT).piano_roll.astype(np.float32) for f in file_paths
    ]
    dataset = [
        np.array([
            get_index(dataset[i][j], data_type, max_key_number)
            for j in xrange(len(dataset[i]))
        ]) for i in xrange(len(dataset))
    ]
    # print('min length:', sum([len(d) for d in dataset])/len(dataset))
    dataset = np.array(
        [np.array(padding(key2frequency(d), max_length)) for d in dataset])
    return dataset
Ejemplo n.º 26
0
    def train(self, session, num_epochs=200):
        dataset = [
            midiread(f, self.r, self.dt).piano_roll.astype(numpy.float32)
            for f in self.train_files
        ][0:5]
        num_samples = len(dataset)
        print 'total samples: %d' % num_samples
        for epoch in xrange(num_epochs):
            numpy.random.shuffle(dataset)
            for s, sequence in enumerate(dataset):
                total_cost = list()
                for i in range(0,
                               len(sequence) - self.batch_size + 1,
                               self.batch_size):
                    v_batch = sequence[i:i + self.batch_size]
                    train_result = session.run([self.cost, self.train_op],
                                               feed_dict={self.v: v_batch})
                    total_cost.append(train_result[0])

                if s % 100 == 0:
                    if len(total_cost) > 0:
                        mean_cost = sum(total_cost) / float(len(total_cost))
                        print 'epoch %d, %dth sample length %d, cost: %f' % (
                            epoch, s, len(sequence), mean_cost)
dt = 0.2
train_files = glob.glob(r'../rbmrnn/Nottingham/train/*.mid')
n_input = r[1] - r[0]
n_hidden = 64
n_output = r[1] - r[0]
n_steps = 50
learning_rate = 0.5
learning_rate_decay = 0.99
batch_size = 32
num_epochs = 2000
check_epochs = 1
generation_path = 'generated/'
filename = '001.mid'

dataset = [
    midiread(f, r, dt).piano_roll.astype(numpy.float32) for f in train_files
]
num_samples = len(dataset)
print 'total samples: %d' % num_samples

x = tf.placeholder(tf.float32, [None, n_steps, n_input])
state = tf.placeholder(tf.float32, [None, n_hidden])
output = tf.placeholder(tf.float32, [None, n_hidden])

learning_rate_variable = tf.Variable(float(learning_rate), trainable=False)
learning_rate_decay_op = learning_rate_variable.assign(learning_rate_variable *
                                                       learning_rate_decay)
cost, train_op, generator = model.lstm(x, state, output, n_steps, n_input,
                                       n_hidden, n_output,
                                       learning_rate_variable)
Ejemplo n.º 28
0
def generation_length():
    re_gen = os.path.join(os.path.split(os.path.dirname(__file__))[0], 'generation', '*.mid')
    files_gen = glob.glob(re_gen)
    dataset_gen = [midiread(f, (21,109), 0.3).piano_roll.astype(theano.config.floatX) for f in files_gen]
    return len(dataset_gen[0])
Ejemplo n.º 29
0
    def train(self, files, batch_size=100, num_epochs=200):
        '''Train the RNN-RBM via stochastic gradient descent (SGD) using MIDI
files converted to piano-rolls.

files : list of strings
  List of MIDI files that will be loaded as piano-rolls for training.
batch_size : integer
  Training sequences will be split into subsequences of at most this size
  before applying the SGD updates.
num_epochs : integer
  Number of epochs (pass over the training set) performed. The user can
  safely interrupt training with Ctrl+C at any time.'''

        assert len(files) > 0, 'Training set is empty!' \
                               ' (did you download the data files?)'
        dataset = [
            midiread(f, self.r,
                     self.dt).piano_roll.astype(theano.config.floatX)
            for f in files
        ]
        print "DONE DOWNLOADING"
        #self.pro.start()
        try:
            count = 0
            processed = 0
            inVecs = [
                self.inVecQueue, self.inVecQueue2, self.inVecQueue3,
                self.inVecQueue4, self.inVecQueue5, self.inVecQueue6,
                self.inVecQueue7, self.inVecQueue8
            ]
            for epoch in xrange(num_epochs):
                numpy.random.shuffle(dataset)
                costs = []
                ''' self.l1.acquire()
                self.l2.acquire()
                self.l3.acquire()
                self.l4.acquire()
                self.l5.acquire()
                self.l6.acquire()
                self.l7.acquire()
                self.l8.acquire()'''
                #ds = chunks(dataset, len(dataset)/4)
                count = 0
                processed = 0
                for s, sequence in enumerate(dataset):
                    inVecs[s % 8].put(sequence)
                    #count = count + len(sequence)/100
                    for j in xrange(0, len(sequence), batch_size):
                        #print "GIVING DATA"
                        count = count + 1
                    #inVecs[s%8].put(sequence[j:j+batch_size])
                '''self.l1.release()
                self.l2.release()
                self.l3.release()
                self.l4.release()
                self.l5.release()
                self.l6.release()
                self.l7.release()
                self.l8.release()'''
                while processed != count:
                    if self.costQueue.qsize() > 0:
                        cost = self.costQueue.get()
                        costs.append(cost)
                        processed = processed + 1
                sums = []
                items = self.weightQueue.qsize()
                #while self.weightQueue.qsize() > 0:
                #   d = self.weightQueue.get()
                #   sums.append(d)
                adder = [None, None, None, None, None, None, None, None]
                for i in xrange(0, 8):
                    q = self.weights[i].get()
                    for j in xrange(0, 8):
                        if adder[j] == None:
                            adder[j] = q[j]
                        else:
                            adder[j] += q[j]
                for i in range(0, 8):
                    adder[i] /= float(8)
                p = adder[0], adder[1], adder[2], adder[3], adder[4], adder[
                    5], adder[6], adder[7]
                if epoch != num_epochs:
                    self.weightQueue.put(p)
                    self.weightQueue2.put(p)
                    self.weightQueue3.put(p)
                    self.weightQueue4.put(p)
                    self.weightQueue5.put(p)
                    self.weightQueue6.put(p)
                    self.weightQueue7.put(p)
                    self.weightQueue8.put(p)
                print 'Epoch %i/%i' % (epoch + 1, num_epochs),
                print numpy.mean(costs)
                #print profmode.print_summary()
                sys.stdout.flush()

        except KeyboardInterrupt:
            print 'Interrupted by user.'
        self.pro.terminate()
        self.pro2.terminate()
        self.pro3.terminate()
        self.pro4.terminate()
        self.pro5.terminate()
        self.pro6.terminate()
        self.pro7.terminate()
        self.pro8.terminate()
        self.weightQueue.close()
        self.weightQueue2.close()
        self.weightQueue3.close()
        self.weightQueue4.close()
        self.weightQueue5.close()
        self.weightQueue6.close()
        self.weightQueue7.close()
        self.weightQueue8.close()
        #print "NOW FETCHING",self.weightQueue.qsize()
        p2 = self.args[0]['params']
        #print "AFTER",len(p2)
        print p2[0] == self.params[0]
        self.W.set_value(p2[0])
        self.bv.set_value(p2[1])
        self.bh.set_value(p2[2])
        self.Wuh.set_value(p2[3])
        self.Wuv.set_value(p2[4])
        self.Wvu.set_value(p2[5])
        self.Wuu.set_value(p2[6])
        self.bu.set_value(p2[7])
Ejemplo n.º 30
0
from scipy.io import wavfile
import numpy as np

THIS_DATA_DIR  = dirname(realpath(__file__))
DOWNLOADED_ZIP = join(THIS_DATA_DIR, "dataset.zip")
DOWNLOADED_DIR = join(THIS_DATA_DIR, "dataset")
FILE_URL="http://c4dm.eecs.qmul.ac.uk/rdr/bitstream/handle/123456789/13/Score-informed%20Piano%20Transcription%20Dataset.zip?sequence=1"

if __name__ == '__main__':
    if not exists(DOWNLOADED_ZIP):
        execute_bash("wget -O {path} {url}".format(url=FILE_URL, path=DOWNLOADED_ZIP))
    if exists(DOWNLOADED_DIR) and isdir(DOWNLOADED_DIR):
        execute_bash("rm -rf %s" % (DOWNLOADED_DIR))
    execute_bash("rm %s " % (join(THIS_DATA_DIR, "*.npy")))
    makedirs(DOWNLOADED_DIR)
    execute_bash("unzip %s -d %s" % (DOWNLOADED_ZIP, DOWNLOADED_DIR))

    files = collect_files_with_ext(DOWNLOADED_DIR, ".wav")

    for subpath, name in files:
        if name.endswith(".wav") and "Chromatic" not in name:
            sampling_rate, music = wavfile.read(subpath)
            np.save(join(THIS_DATA_DIR, name.replace(".wav", ".npy")), music)
            piece = midiread(str(subpath).replace(".wav", "_correct.mid"))
            np.save(join(THIS_DATA_DIR, name.replace(".wav", ".mid.npy")), piece.piano_roll)

    execute_bash("rm -rf %s" % (DOWNLOADED_DIR))
    execute_bash("rm -rf %s" % (DOWNLOADED_ZIP))

Ejemplo n.º 31
0
EPOCHS = 1000
BATCH_SIZE = 128
TICKS_PER_INPUT = MEASURES*TICKS_PER_MEASURE
GEN_LENGTH = TICKS_PER_INPUT*8
DT = 0.3
    
if __name__ == '__main__':

  ### read MIDI
  #data_dir = '../Data/Cirriculum/easy/'
  data_dir = '../biaxial-rnn-music-composition/music/'

  files = os.listdir(data_dir)
  files = [data_dir + f for f in files if '.mid' in f]

  dataset = [midiread(f, MIDI_RANGE, DT).piano_roll for f in files]

  X = []
  y = []

  for song in dataset:
      for i in range(0, len(song) - TICKS_PER_INPUT, STEP):
          X.append(song[i: i + TICKS_PER_INPUT])
          y.append(song[i + TICKS_PER_INPUT])

  max_samples = (len(X) // BATCH_SIZE) * BATCH_SIZE
  X = X[:max_samples]
  y = y[:max_samples]

  X = np.array(X)
  y = np.array(y)
      save_dir = './'

      ### read MIDI
      #data_dir = '../Data/Cirriculum/easy/'
      #data_dir = '../biaxial-rnn-music-composition/music/'

      files = os.listdir(data_dir)
      files = [data_dir + f for f in files if '.mid' in f or '.MID' in f]

      print files 

      dataset = []

      for f in files:
          try:
              dataset.append(midiread(f, MIDI_RANGE, DT).piano_roll)
              print "{} loaded".format(f)
          except IndexError:
              print "Skipping {}".format(f)
              pass

      print np.shape(dataset)

      X = []
      y = []

      for song in dataset:
          for i in range(0, len(song) - TICKS_PER_INPUT, STEP):
              X.append(song[i: i + TICKS_PER_INPUT])
              y.append(song[i + TICKS_PER_INPUT])
Ejemplo n.º 33
0
# In[109]:

model.train(onlyfiles)


# In[117]:

model.generate('testHaydn3.mid')


# In[23]:

get_ipython().magic(u'pylab inline')
f = 'testNotthingham6.mid'
extent = (0, 0.25 * len(piano_roll)) + (21, 109)
piano_roll = midiread(f, (21, 109), 0.25).piano_roll.astype(theano.config.floatX)
pylab.figure()
pylab.imshow(piano_roll.T, origin='lower', aspect='auto',
             interpolation='nearest', cmap=pylab.cm.gray_r,
             extent=extent)
pylab.xlabel('time (s)')
pylab.ylabel('MIDI note number')
pylab.title('generated piano-roll')
pylab.show()


# In[ ]:



Ejemplo n.º 34
0
    def train(self, files, batch_size=100, num_epochs=200):
        '''Train the RNN-RBM via stochastic gradient descent (SGD) using MIDI
files converted to piano-rolls.

files : list of strings
  List of MIDI files that will be loaded as piano-rolls for training.
batch_size : integer
  Training sequences will be split into subsequences of at most this size
  before applying the SGD updates.
num_epochs : integer
  Number of epochs (pass over the training set) performed. The user can
  safely interrupt training with Ctrl+C at any time.'''

        assert len(files) > 0, 'Training set is empty!' \
                               ' (did you download the data files?)'
        dataset = [midiread(f, self.r,
                            self.dt).piano_roll.astype(theano.config.floatX)
                   for f in files]
        print "DONE DOWNLOADING"
        #self.pro.start()
        try:
            count = 0
            processed = 0
            inVecs = [self.inVecQueue,self.inVecQueue2,self.inVecQueue3,self.inVecQueue4,self.inVecQueue5,self.inVecQueue6,self.inVecQueue7,self.inVecQueue8]
            for epoch in xrange(num_epochs):
                numpy.random.shuffle(dataset)
                costs = []
                ''' self.l1.acquire()
                self.l2.acquire()
                self.l3.acquire()
                self.l4.acquire()
                self.l5.acquire()
                self.l6.acquire()
                self.l7.acquire()
                self.l8.acquire()'''
                #ds = chunks(dataset, len(dataset)/4)
                count = 0
                processed = 0
                for s, sequence in enumerate(dataset):
                       inVecs[s%8].put(sequence)
                       #count = count + len(sequence)/100
                       for j in xrange(0, len(sequence), batch_size):
                       #print "GIVING DATA"
                          count = count + 1
                       #inVecs[s%8].put(sequence[j:j+batch_size])
                '''self.l1.release()
                self.l2.release()
                self.l3.release()
                self.l4.release()
                self.l5.release()
                self.l6.release()
                self.l7.release()
                self.l8.release()'''
                while processed != count:
                   if self.costQueue.qsize()>0:
                      cost = self.costQueue.get()
                      costs.append(cost)
                      processed = processed + 1
                sums = []
                items = self.weightQueue.qsize()
                #while self.weightQueue.qsize() > 0:
                #   d = self.weightQueue.get()
                #   sums.append(d)
                adder = [None,None,None,None,None,None,None,None]
                for i in xrange(0,8):
                   q = self.weights[i].get()
                   for j in xrange(0,8):
                      if adder[j] == None:
                         adder[j] = q[j]
                      else:
                         adder[j] += q[j]
                for i in range(0,8):
                   adder[i] /= float(8)
                p = adder[0],adder[1],adder[2],adder[3],adder[4],adder[5],adder[6],adder[7]
                if epoch != num_epochs:
                   self.weightQueue.put(p)
                   self.weightQueue2.put(p)
                   self.weightQueue3.put(p)
                   self.weightQueue4.put(p)
                   self.weightQueue5.put(p)
                   self.weightQueue6.put(p)
                   self.weightQueue7.put(p)
                   self.weightQueue8.put(p)   
                print 'Epoch %i/%i' % (epoch + 1, num_epochs),
                print numpy.mean(costs)
                #print profmode.print_summary()
                sys.stdout.flush()

        except KeyboardInterrupt:
            print 'Interrupted by user.'
        self.pro.terminate()
        self.pro2.terminate()
        self.pro3.terminate()
        self.pro4.terminate()
        self.pro5.terminate()
        self.pro6.terminate()
        self.pro7.terminate()
        self.pro8.terminate()
        self.weightQueue.close()
        self.weightQueue2.close()
        self.weightQueue3.close()
        self.weightQueue4.close()
        self.weightQueue5.close()
        self.weightQueue6.close()
        self.weightQueue7.close()
        self.weightQueue8.close()
        #print "NOW FETCHING",self.weightQueue.qsize()
        p2 = self.args[0]['params']
        #print "AFTER",len(p2)
        print p2[0]==self.params[0]
        self.W.set_value(p2[0])
        self.bv.set_value(p2[1])
        self.bh.set_value(p2[2])
        self.Wuh.set_value(p2[3])
        self.Wuv.set_value(p2[4])
        self.Wvu.set_value(p2[5])
        self.Wuu.set_value(p2[6])
        self.bu.set_value(p2[7])
Ejemplo n.º 35
0
from scipy.io import wavfile
import numpy as np

THIS_DATA_DIR = dirname(realpath(__file__))
DOWNLOADED_ZIP = join(THIS_DATA_DIR, "dataset.zip")
DOWNLOADED_DIR = join(THIS_DATA_DIR, "dataset")
FILE_URL = "http://c4dm.eecs.qmul.ac.uk/rdr/bitstream/handle/123456789/13/Score-informed%20Piano%20Transcription%20Dataset.zip?sequence=1"

if __name__ == '__main__':
    if not exists(DOWNLOADED_ZIP):
        execute_bash("wget -O {path} {url}".format(url=FILE_URL,
                                                   path=DOWNLOADED_ZIP))
    if exists(DOWNLOADED_DIR) and isdir(DOWNLOADED_DIR):
        execute_bash("rm -rf %s" % (DOWNLOADED_DIR))
    execute_bash("rm %s " % (join(THIS_DATA_DIR, "*.npy")))
    makedirs(DOWNLOADED_DIR)
    execute_bash("unzip %s -d %s" % (DOWNLOADED_ZIP, DOWNLOADED_DIR))

    files = collect_files_with_ext(DOWNLOADED_DIR, ".wav")

    for subpath, name in files:
        if name.endswith(".wav") and "Chromatic" not in name:
            sampling_rate, music = wavfile.read(subpath)
            np.save(join(THIS_DATA_DIR, name.replace(".wav", ".npy")), music)
            piece = midiread(str(subpath).replace(".wav", "_correct.mid"))
            np.save(join(THIS_DATA_DIR, name.replace(".wav", ".mid.npy")),
                    piece.piano_roll)

    execute_bash("rm -rf %s" % (DOWNLOADED_DIR))
    execute_bash("rm -rf %s" % (DOWNLOADED_ZIP))
Ejemplo n.º 36
0
    def train(self, files, batch_size=128, num_epochs=200):
        def downsampling(sample):
            # only keep the note on even index position (0, 2, 4 ...)
            downsample = []
            for i, s in enumerate(sample):
                if i % 2 == 0:
                    downsample.append(s)
            return downsample

        def upsampling(sample, length):
            '''
            double each notes in sample
                sample: the melody to be upsampled
                length: the length of the original melody M, sample=Downsampling(M)
            '''
            upsample = []
            for s in sample:
                upsample.append(s)
                if (len(upsample) >= length):
                    # upsampling melody length cannot longer than original melody length
                    break
                upsample.append(s)
            return upsample

        def sampling(sample):
            # to be one hot (to be 0,1)
            ixes = []
            for i, s in enumerate(sample):
                n_step = []
                for n in xrange(20):
                    ix = np.random.choice(range(88), p=s)
                    n_step.append(ix)
                count = Counter(n_step)
                ix = count.most_common(1)[0][0]
                x = np.zeros((88, ))
                x[ix] = 1
                ixes.append(x)

            return ixes

        def accuracy(v, v_sample):
            # ACC
            accs = []
            t = len(v)
            n = len(v[0])
            for time in range(t):
                tp = 0  # true positive
                fp = 0  # false positive
                fn = 0  # false negative
                for note in range(n):
                    if v[time][note] == 1 and v_sample[time][note] == 1:
                        tp += 1.
                    if v[time][note] == 0 and v_sample[time][note] == 1:
                        fp += 1.
                    if v[time][note] == 1 and v_sample[time][note] == 0:
                        fn += 1.
                if tp + fp + fn != 0:
                    a = tp / (tp + fp + fn)
                else:
                    a = 0
                accs.append(a)

            acc = numpy.mean(accs)
            return acc

        def generate():
            # sampling procedure, generating music

            # layer 4
            generate_sharp_4 = self.generate_4()[0]
            generate_sharp_4 = sampling(generate_sharp_4)

            # layer 3
            upsample_3 = upsampling(generate_sharp_4,
                                    2 * len(generate_sharp_4) + 1)
            generate_sharp_3 = self.generate_3(upsample_3, upsample_3)[0]
            generate_sharp_3 = sampling(generate_sharp_3)

            # layer 2
            upsample_2 = upsampling(generate_sharp_3,
                                    2 * len(generate_sharp_3) + 1)
            generate_sharp_2 = self.generate_2(upsample_2, upsample_2)[0]
            generate_sharp_2 = sampling(generate_sharp_2)

            # layer 1
            upsample_1 = upsampling(generate_sharp_2,
                                    2 * len(generate_sharp_2) + 1)
            generate_sharp_1 = self.generate_1(upsample_1, upsample_1)[0]
            generate_sharp_1 = sampling(generate_sharp_1)

            # layer 0
            upsample_0 = upsampling(generate_sharp_1,
                                    2 * len(generate_sharp_1) + 1)
            generate_sharp_0 = self.generate_0(upsample_0, upsample_0)[0]
            generate_sharp_0 = sampling(generate_sharp_0)

            return generate_sharp_0

        # load data
        dataset = [
            midiread(f, self.r,
                     self.dt).piano_roll.astype(theano.config.floatX)
            for f in files
        ]
        print('pyramid_mono, dataset=nottingham_mono, lr=%f, epoch=%i' %
              (self.lr, num_epochs))

        for epoch in range(num_epochs):
            numpy.random.shuffle(dataset)
            monitors_0 = []
            accs_0 = []
            monitors_1 = []
            accs_1 = []
            monitors_2 = []
            accs_2 = []
            monitors_3 = []
            accs_3 = []
            monitors_4 = []
            accs_4 = []

            for s, sequence in enumerate(dataset):
                for i in range(0, len(sequence), batch_size):

                    batch_music = sequence[i:i + batch_size]  # 128

                    # layer 0
                    downsample_0 = downsampling(batch_music)  # 64
                    upsample_0 = upsampling(downsample_0,
                                            len(batch_music))  # 128

                    # layer 1
                    downsample_1 = downsampling(downsample_0)  # 32
                    upsample_1 = upsampling(downsample_1,
                                            len(downsample_0))  # 64

                    # layer 2
                    downsample_2 = downsampling(downsample_1)  # 16
                    upsample_2 = upsampling(downsample_2,
                                            len(downsample_1))  # 32

                    # layer 3
                    downsample_3 = downsampling(downsample_2)  # 8
                    upsample_3 = upsampling(downsample_3,
                                            len(downsample_2))  # 16

                    # layer 0
                    sharp_0, monitor_0 = self.sharp_fun_0(
                        batch_music, upsample_0)
                    monitors_0.append(monitor_0)
                    accs_0.append(accuracy(batch_music, sampling(sharp_0)))

                    # layer 1
                    sharp_1, monitor_1 = self.sharp_fun_1(
                        downsample_0, upsample_1)
                    monitors_1.append(monitor_1)
                    accs_1.append(accuracy(downsample_0, sampling(sharp_1)))

                    # layer 2
                    sharp_2, monitor_2 = self.sharp_fun_2(
                        downsample_1, upsample_2)
                    monitors_2.append(monitor_2)
                    accs_2.append(accuracy(downsample_1, sampling(sharp_2)))

                    # layer 3
                    sharp_3, monitor_3 = self.sharp_fun_3(
                        downsample_2, upsample_3)
                    monitors_3.append(monitor_3)
                    accs_3.append(accuracy(downsample_2, sampling(sharp_3)))

                    # layer 4
                    if (len(downsample_3) == 1):
                        sharp_4, monitor_4 = self.sharp_fun_4(
                            downsample_3, downsample_3)
                        accs_4.append(accuracy(downsample_3,
                                               sampling(sharp_4)))
                    else:
                        sharp_4, monitor_4 = self.sharp_fun_4(
                            downsample_3[1:],
                            downsample_3[:len(downsample_3) - 1])
                        accs_4.append(
                            accuracy(downsample_3[1:], sampling(sharp_3)))
                    monitors_4.append(monitor_4)


            p = 'Epoch %i/%i    layer0:LL %f ACC %f   layer1:LL %f ACC %f  layer2:LL %f ACC %f  layer3:LL %f ACC %f  ' \
                'layer4:LL %f ACC %f  time %s' % \
                (epoch + 1, num_epochs, numpy.mean(monitors_0), numpy.mean(accs_0), numpy.mean(monitors_1), numpy.mean(accs_1),
                 numpy.mean(monitors_2), numpy.mean(accs_2), numpy.mean(monitors_3), numpy.mean(accs_3), numpy.mean(monitors_4),
                 numpy.mean(accs_4),   datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
            print(p)

            if (epoch % 50 == 0 or epoch == num_epochs - 1):

                piano_roll = generate()
                midiwrite('sample/pyramid_mono_%i.mid' % (epoch), piano_roll,
                          self.r, self.dt)
Ejemplo n.º 37
0
        save_dir = './'

        ### read MIDI
        #data_dir = '../Data/Cirriculum/easy/'
        #data_dir = '../biaxial-rnn-music-composition/music/'

        files = os.listdir(data_dir)
        files = [data_dir + f for f in files if '.mid' in f or '.MID' in f]

        print files

        dataset = []

        for f in files:
            try:
                dataset.append(midiread(f, MIDI_RANGE, DT).piano_roll)
                print "{} loaded".format(f)
            except IndexError:
                print "Skipping {}".format(f)
                pass

        print np.shape(dataset)

        X = []
        y = []

        for song in dataset:
            for i in range(0, len(song) - TICKS_PER_INPUT, STEP):
                X.append(song[i:i + TICKS_PER_INPUT])
                y.append(song[i + TICKS_PER_INPUT])
def load_midi_data(data_dir):
    import midi.utils as utils

    midi_data = utils.midiread(data_dir, dt=0.5)

    return midi_data.piano_roll
Ejemplo n.º 39
0
# Compatible with Python 2.7. not 3.x
# Recursively load midi files, extract piano rolls and save as *.mid (file) and *.npy (matrix)

load_root = './MIDI_Data/'
save_root = './MIDI_Data_PianoRolls/'

for dirpath, dirs, files in os.walk(load_root):
    for name in files:
        if name.endswith('.mid'):
            print dirpath, name
            print dirpath.replace(load_root, save_root), name

            load_dirpath = dirpath
            save_dirpath = dirpath.replace(load_root, save_root)

            load_filepath = os.path.join(load_dirpath, name)
            save_filepath = os.path.join(save_dirpath, name)

            # Read MIDI file
            piano_roll = midiread(load_filepath).piano_roll

            if not os.path.exists(save_dirpath):
                os.makedirs(save_dirpath)

            # Save the piano roll as MIDI
            midiwrite(save_filepath, piano_roll=piano_roll)

            # Save the piano roll as *.npy file
            np.save(save_filepath.replace('.mid', '.npy'), piano_roll)
Ejemplo n.º 40
0
def main():

    #--- import data ---#
    sizeOfMiniBatch = 5 #how many tunes per miniBatch
    noOfEpoch = 100 
    noOfEpochPerMB = 2
    lengthOfMB = 100
    sparseParam = np.float32(0.01) #increases with no. of cells
    path = './Piano-midi.de/train-individual/hpps'
    #path = './Piano-midi.de/train'
    files = os.listdir(path)
    assert len(files) > 0, 'Training set is empty!' \
                               ' (did you download the data files?)'
    #pitch range is from 21 to 109
    dataset = [midiread((path + "/" + f), (21, 109),0.3).piano_roll.astype(theano.config.floatX) for f in files]
                  
    #check number of notes for each tune:       
    print(str([np.array(dataset[n]).shape[0] for n in np.arange(np.array(dataset).shape[0])]))

    # set "silent" to zero in 1-hot format
    for k in np.arange(np.array(dataset).shape[0]):
        for n in np.arange(0,np.array(dataset[k]).shape[0],1):
            if np.sum(dataset[k][n], dtype=theano.config.floatX) == 0 :
                dataset[k][n][0] = np.float32(1.0)
                

    #--- training with data ---#
    
    myRNN4Music = RNN4Music(h1_length=176, h2_length=176, h3_length=176, io_length=88, R1=np.float32(0.001), R2=np.float32(0.001), R3=np.float32(0.001), Rout=np.float32(0.001)) 
    
    #myRNN4Music.loadParameters('120_120_120_0_001_xEn_150epoch_hpps')
    #myRNN4Music.loadParameters('120_120_120_0_001_sqr_150epoch_hpps')
    myRNN4Music.loadParameters('176_176_176_0_001_xEn_L1_0_01_100epoch_hpps')
    #myRNN4Music.saveParameters('176_176_176_0_001_xEn_L1_0_1_300epoch_hpps')
    
    #myRNN4Music.train(dataset, noOfEpochPerMB, noOfEpoch, sizeOfMiniBatch, lengthOfMB, sparseParam)
    #myRNN4Music.saveParameters('176_176_176_0_001_xEn_L1_0_01_100epoch_hpps')
    #myRNN4Music.train(dataset, noOfEpochPerMB, noOfEpoch, sizeOfMiniBatch, lengthOfMB, sparseParam)
    #myRNN4Music.saveParameters('176_176_176_0_001_xEn_L1_0_01_200epoch_hpps')
    #myRNN4Music.train(dataset, noOfEpochPerMB, noOfEpoch, sizeOfMiniBatch, lengthOfMB, sparseParam)
    #myRNN4Music.saveParameters('176_176_176_0_001_xEn_L1_0_01_300epoch_hpps')
    #myRNN4Music.train(dataset, noOfEpochPerMB, noOfEpoch, sizeOfMiniBatch, lengthOfMB, sparseParam)
    #myRNN4Music.saveParameters('176_176_176_0_001_xEn_L1_0_01_400epoch_hpps')




    #--- plot some genearted tunes ---#

    for baseSample in np.array([0, 20, 15, 31]):
        exampleLength = 50
        myRNN4Music.resetStates()
        generatedTune = myRNN4Music.genMusic(np.float32(dataset[baseSample][0:exampleLength]), 300)
        midiwrite('176_176_176_0_001_sqr_hpps150_' + str(baseSample) + '.mid', generatedTune[0], (21, 109),0.3)
        #generatedTune[0] is the tune, generatedTune[1] is the probability at each iteration
        
        #plot genearted probability
        plt.figure(0 + baseSample*100)
        plt.imshow(np.array(generatedTune[1][0:50,25:65]), origin = 'lower', extent=[25,65,0,50], aspect=0.5,
                        interpolation = 'nearest', cmap='gist_stern_r')
        plt.title('probability of generated midi note piano-roll')
        plt.xlabel('midi note')
        plt.ylabel('sample number (time steps)')
        plt.colorbar()##
        #plot leading example for generation
        plt.figure(1 + baseSample*100)
        plt.imshow(np.transpose(dataset[baseSample]), origin='lower', aspect='auto',
                                 interpolation='nearest', cmap=pylab.cm.gray_r)
        plt.colorbar()
        plt.title('original piano-roll')
        plt.xlabel('sample number (time steps)')
        plt.ylabel('midi note')

        #plot generated tune
        plt.figure(2 + baseSample*100)
        plt.imshow(np.transpose(np.array(generatedTune[0][0:500])), origin='lower', aspect='auto',
                                 interpolation='nearest', cmap=pylab.cm.gray_r)
        plt.colorbar()
        plt.title('generated piano-roll')
        plt.xlabel('sample number (time steps)')
        plt.ylabel('midi note')
    plt.show()
Ejemplo n.º 41
0
    def train(self, files, batch_size=100, num_epochs=200):

        assert len(files) > 0, 'Training set is empty!' \
                               ' (did you download the data files?)'
        dataset = [
            midiread(f, self.r,
                     self.dt).piano_roll.astype(theano.config.floatX)
            for f in files
        ]

        def accuracy(v, v_sample):
            accs = []
            t, n = v.shape
            for time in range(t):
                tp = 0  # true positive
                fp = 0  # false positive
                fn = 0  # false negative
                for note in range(n):
                    if v[time][note] == 1 and v_sample[time][note] == 1:
                        tp += 1.
                    if v[time][note] == 0 and v_sample[time][note] == 1:
                        fp += 1.
                    if v[time][note] == 1 and v_sample[time][note] == 0:
                        fn += 1.
                if tp + fp + fn != 0:
                    a = tp / (tp + fp + fn)
                else:
                    a = 0
                accs.append(a)

            acc = numpy.mean(accs)
            return acc

        def sampling(sample):  # 01
            s = T.matrix()
            b = rng.binomial(size=s.shape,
                             n=1,
                             p=s,
                             dtype=theano.config.floatX)
            fun = theano.function(inputs=[s], outputs=[b])

            return fun(sample)[0]

        try:
            print('rnn, dataset=Nottingham, lr=%f, epoch=%i' %
                  (self.lr, num_epochs))

            for epoch in range(num_epochs):
                numpy.random.shuffle(dataset)
                costs = []
                monitors = []
                accs = []

                for s, sequence in enumerate(dataset):
                    for i in range(0, len(sequence), batch_size):
                        if i + batch_size + 1 >= len(sequence):
                            break

                        v = sequence[i:i + batch_size]
                        targets = sequence[i + 1:i + batch_size + 1]

                        (cost, monitor,
                         sample) = self.train_function(v, targets)
                        costs.append(cost)
                        monitors.append(monitor)

                        sample = sampling(sample)
                        acc = accuracy(v, sample)
                        accs.append(acc)

                p = 'Epoch %i/%i  LL %f   ACC %f  Cost %f   time %s' \
                    % (epoch + 1, num_epochs, numpy.mean(monitors), numpy.mean(accs), numpy.mean(costs),
                       datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
                print(p)
                if (epoch % 100 == 0 or epoch == num_epochs - 1):
                    piano_roll = sampling(self.generate_function()[0])
                    midiwrite('sample/rnn_%i.mid' % (epoch), piano_roll,
                              self.r, self.dt)
                sys.stdout.flush()

        except KeyboardInterrupt:
            print('Interrupted by user.')
def load_midi_data(data_dir):
    import midi.utils as utils

    midi_data = utils.midiread(data_dir, dt=0.5)

    return midi_data.piano_roll