### Run the graph!
# Now it's time to start a session and run the graph! 

with tf.Session() as sess:
    #First, we train the model
    #initialize the variables of the model
    init = tf.initialize_all_variables()
    sess.run(init)
    #Run through all of the training data num_epochs times
    for epoch in tqdm(range(num_epochs)):
        for song in songs:
            #The songs are stored in a time x notes format. The size of each song is timesteps_in_song x 2*note_range
            #Here we reshape the songs so that each training example is a vector with num_timesteps x 2*note_range elements
            song = np.array(song)
            song = song[:np.floor(song.shape[0]/num_timesteps)*num_timesteps]
            song = np.reshape(song, [song.shape[0]/num_timesteps, song.shape[1]*num_timesteps])
            #Train the RBM on batch_size examples at a time
            for i in range(1, len(song), batch_size): 
                tr_x = song[i:i+batch_size]
                sess.run(updt, feed_dict={x: tr_x})

    #Now the model is fully trained, so let's make some music! 
    #Run a gibbs chain where the visible nodes are initialized to 0
    sample = gibbs_sample(1).eval(session=sess, feed_dict={x: np.zeros((10, n_visible))})
    for i in range(sample.shape[0]):
        if not any(sample[i,:]):
            continue
        #Here we reshape the vector to be time x notes, and then save the vector as a midi file
        S = np.reshape(sample[i,:], (num_timesteps, 2*note_range))
        midi_manipulation.noteStateMatrixToMidi(S, "generated_chord_{}".format(i))
            
# author chinabluewu@github

import numpy as np
from tqdm import tqdm
import glob
#sudo pip install msgpack-python
import msgpack
import midi_manipulation

#files = glob.glob('{}/*.mid*'.format(path))
try:
    files = glob.glob('generated*.mid*')
except Exception as e:
    raise e

songs = np.zeros((0,156))
for f in tqdm(files):
    try:
        song = np.array(midi_manipulation.midiToNoteStateMatrix(f))

        if np.array(song).shape[0] > 10:
            #songs.append(song)
            songs = np.concatenate((songs,song))
    except Exception as e:
        raise e
print "samlpes merging ..."
print np.shape(songs)
midi_manipulation.noteStateMatrixToMidi(songs, "final")
# author chinabluewu@github

import numpy as np
from tqdm import tqdm
import glob
#sudo pip install msgpack-python
import msgpack
import midi_manipulation

#files = glob.glob('{}/*.mid*'.format(path))
try:
    files = glob.glob('./generated/generated*.mid*')
except Exception as e:
    raise e

songs = np.zeros((0, 156))
for f in tqdm(files):
    try:
        song = np.array(midi_manipulation.midiToNoteStateMatrix(f))

        #if np.array(song).shape[0] > 10:
        #songs.append(song)
        songs = np.concatenate((songs, song))
    except Exception as e:
        raise e
print("samlpes merging ...")
print(np.shape(songs))
midi_manipulation.noteStateMatrixToMidi(songs, "final")
Exemple #4
0
    print(it, end='  ! ')
    plt.plot(losses)
    plt.savefig('plot_losses')

    if it % 1 == 0:
        samples = torch.randn(8, 300).to(device)
        samples = model.decode(X=samples, z=samples, sample_new=True).cpu()
        samples = samples.detach().numpy()

        samples[samples <= 0.5] = 0
        samples[samples > 0.5] = 1

        for i, sample in enumerate(samples):
            sample = sample.reshape(-1, 156)
            noteStateMatrixToMidi(sample,
                                  name=("samples/" + str(it) + "new_sample_" +
                                        str(i)))

    if it > 2 and it % 4 == 0:
        for param_group in optimizer.param_groups:
            param_group['lr'] *= 0.8

    for ii, X in enumerate(dataloader):
        # X = Variable(torch.from_numpy(X))
        optimizer.zero_grad()

        X = X.type(torch.FloatTensor)
        X = X.to(device)

        # Forward
        X_sample, z_mu, z_var = model(X)
Exemple #5
0
    def launch():
        #TODO All the entry point

        ######################################
        ###    Hypermarameters             ###
        ######################################

        lowest_note = import_midi.lowerBound
        highest_note = import_midi.upperBound
        note_range = highest_note - lowest_note
        truncated_backprop_length = 15
        n_inputs = 2 * note_range * truncated_backprop_length
        num_steps = 15
        num_neurons = 150
        num_outputs = n_inputs

        num_epochs = 100
        batch_size = 10
        state_size = 4
        num_classes = 2
        songs = import_midi.get_songs("../musics/")
        songs = np.array(songs)
        print(songs.shape)
        num_batches = len(songs) // batch_size
        learning_rate = tf.constant(0.005, tf.float32)

        ######################################
        ###    	variables                  ###
        ######################################

        x = tf.placeholder(tf.float32, [None, None, n_inputs])
        y = tf.placeholder(tf.int32, [None, None])
        seqlen = tf.placeholder

        W = tf.Variable(np.random.rand(batch_size, n_inputs, batch_size),
                        dtype=tf.float32)
        b = tf.Variable(np.zeros((1, batch_size)), dtype=tf.float32)

        W2 = tf.Variable(np.random.rand(batch_size, n_inputs, batch_size),
                         dtype=tf.float32)
        b2 = tf.Variable(np.zeros((1, batch_size)), dtype=tf.float32)

        cell = tf.contrib.rnn.BasicRNNCell(num_units=num_neurons)
        outputs, states = tf.nn.dynamic_rnn(cell, x, dtype=tf.float32)

        logits_series = tf.layers.dense(outputs, num_outputs)

        ######################################
        ###    	fonction de perte          ###
        ######################################

        print(logits_series)
        print(y)
        losses = tf.nn.sparse_softmax_cross_entropy_with_logits(
            labels=y, logits=logits_series)
        total_loss = tf.reduce_mean(losses)

        ###########################################
        ###   fonction d optimisation           ###
        ###########################################

        optimizer = tf.train.AdamOptimizer(learning_rate=learning_rate)
        train_step = optimizer.minimize(total_loss)

        ###########################################
        ###   fonction de descente de gradient  ###
        ###########################################

        def sample(probs):
            return tf.floor(probs + tf.random_uniform(tf.shape(probs), 0, 1))

        def gibbs_sample(k):
            def gibbs_step(count, k, xk):
                return count + 1, k, xk

            ct = tf.constant(0)
            [_, _, x_sample] = control_flow_ops.while_loop(
                lambda count, num_iter, *args: count < num_iter, gibbs_step,
                [ct, tf.constant(k), x])
            x_sample = tf.stop_gradient(x_sample)
            return x_sample

######################################
###    	        Run                ###
######################################

        with tf.Session() as sess:
            sess.run(tf.initialize_all_variables())
            updt = [total_loss, train_step, states]

            for epoch_idx in range(num_epochs):
                _states = np.zeros((batch_size, state_size))

                print("New song, epoch :", epoch_idx)
                print(len(songs))
                for song in songs:
                    if len(song) > 1:
                        song = np.array(song)
                        song = song[:int(
                            np.floor(song.shape[0] // num_steps) * num_steps)]
                        print(song)
                        print(song.shape)
                        song = np.reshape(song, [
                            -1, song.shape[0] // num_steps,
                            song.shape[1] * num_steps
                        ])
                        for i in range(1, len(song), batch_size):
                            tr_x = song[i:i + batch_size]
                            print(x.shape)
                            print(tr_x.shape)
                            print(tr_x)
                            _total_loss, _train_step, _states = sess.run(
                                updt, feed_dict={x: tr_x})

            sample = gibbs_sample(1).eval(
                session=sess,
                feed_dict={x: np.zeros((batch_size, num_steps, n_inputs))})
            for i in range(sample.shape[0]):
                smpl = np.array(sample[i, :])
                spml = smpl[:int(
                    np.floor(smpl.shape[0] // num_steps) * num_steps)]
                print(smpl)
                S = np.reshape(smpl, [
                    -1, smpl.shape[0] // num_steps, smpl.shape[1] * num_steps
                ])
                midi_manipulation.noteStateMatrixToMidi(
                    S, "../out/generated_chord_{}".format(i))
Exemple #6
0
        for song in songs:
            # The songs are stored in a time x notes format. The size of each song is timesteps_in_song x 2*note_range
            # Here we reshape the songs so that each training example
            # is a vector with num_timesteps x 2*note_range elements
            song = np.array(song)
            song = song[:int(
                np.floor(song.shape[0] // num_timesteps) * num_timesteps)]
            song = np.reshape(song, [
                song.shape[0] // num_timesteps, song.shape[1] * num_timesteps
            ])
            # Train the RBM on batch_size examples at a time
            for i in range(1, len(song), batch_size):
                tr_x = song[i:i + batch_size]
                sess.run(updt, feed_dict={x: tr_x})
#--------------------------------------------------------------------------------------------------------------------------------------------
# Generating Music
    print("Generating Music...................")
    # Run a gibbs chain where the visible nodes are initialized to 0
    sample = gibbs_sample(1).eval(session=sess,
                                  feed_dict={x: np.zeros((10, n_visible))})
    for i in range(sample.shape[0]):
        if not any(sample[i, :]):
            continue
        # Here we reshape the vector to be time x notes, and then save the vector as a midi file
        S = np.reshape(sample[i, :], (num_timesteps, 2 * note_range))
        if not os.path.exists("out"):
            os.mkdir('out')
        midi_manipulation.noteStateMatrixToMidi(
            S, "out/generated_chord_{}".format(i))
#--------------------------------------------------------------------------------------------------------------------------------------------
Exemple #7
0
# When we do sess.run(updt), TensorFlow will run all 3 update steps
updt = [W.assign_add(W_adder), bv.assign_add(bv_adder), bh.assign_add(bh_adder)]

with tf.Session() as sess:
    # First, we train the model
    # initialize the variables of the model
    init = tf.global_variables_initializer()
    sess.run(init)
    # Run through all of the training data num_epochs times
    for epoch in tqdm(range(num_epochs)):
        for song in songs:
            # The songs are stored in a time x notes format. The size of each song is timesteps_in_song x 2*note_range
            # Here we reshape the songs so that each training example
            # is a vector with num_timesteps x 2*note_range elements
            song = np.array(song)
            song = song[:int(np.floor(song.shape[0] // num_timesteps) * num_timesteps)]
            song = np.reshape(song, [song.shape[0] // num_timesteps, song.shape[1] * num_timesteps])
            # Train the RBM on batch_size examples at a time
            for i in range(1, len(song), batch_size):
                tr_x = song[i:i + batch_size]
                sess.run(updt, feed_dict={x: tr_x})
#make music
    # Run a gibbs chain where the visible nodes are initialized to 0
    sample = gibbs_sample(1).eval(session=sess, feed_dict={x: np.zeros((10, n_visible))})
    for i in range(sample.shape[0]):
        if not any(sample[i, :]):
            continue
        # Here we reshape the vector to be time x notes, and then save the vector as a midi file
        S = np.reshape(sample[i, :], (num_timesteps, 2 * note_range))
        midi_manipulation.noteStateMatrixToMidi(S, "path_to_output_folder/generated_chord_{}".format(i))
Exemple #8
0
    init = tf.compat.v1.global_variables_initializer()
    sess.run(init)
    # Run through all of the training data epochs times
    for epoch in tqdm(range(epochs)):
        for song in songs:
            # The songs are stored in a time x notes format. The size of each song is timesteps_in_song x 2*note_range
            # Here we reshape the songs so that each training example
            # is a vector with num_timesteps x 2*note_range elements
            song = np.array(song)
            song = song[:int(
                np.floor(song.shape[0] // num_timesteps) * num_timesteps)]
            song = np.reshape(song, [
                song.shape[0] // num_timesteps, song.shape[1] * num_timesteps
            ])
            # Train the RBM on batch_size examples at a time
            for i in range(1, len(song), batch_size):
                tr_x = song[i:i + batch_size]
                sess.run(updt, feed_dict={x: tr_x})

    # Now the model is fully trained, so let's make some music!
    # Run a gibbs chain where the visible nodes are initialized to 0
    sample = gibbs_sample(1).eval(session=sess,
                                  feed_dict={x: np.zeros((10, n_visible))})
    for i in range(sample.shape[0]):
        if not any(sample[i, :]):
            continue
        # Here we reshape the vector to be time x notes and then save the vector as a midi file
        S = np.reshape(sample[i, :], (num_timesteps, 2 * note_range))
        midi_manipulation.noteStateMatrixToMidi(S, f"out/generated_chord_{i}")
    print("done training. outputs saved in the out folder")
Exemple #9
0
 def generate_midi_from_sequences(self, sequence, dir_path):
     for i in range(len(sequence)):
         print(sequence[i])
         mm.noteStateMatrixToMidi(sequence[i], dir_path + 'generated_chord_{}'.format(i))
Exemple #10
0
def generate(W, bv, bh):
    """
    We initialize the variables l_note, h_note, music_note_range for creating new music chords
    from the distribution of the emperical data learnt
    """

    l_note = midi_manipulation.lowerBound
    h_note = midi_manipulation.upperBound
    music_note_range = h_note - l_note

    # No of time steps
    number_of_timesteps = 10
    # No of neurons in visible layer(input layer)
    number_of_visible = 2 * music_note_range * number_of_timesteps
    # No of music files to be generated
    number_of_chords = 50

    ########----------------Helper Functions-----------------########
    def sample(vector_probs):
        """
        Sample function will return the uniform randomly sampled vector of zeroes and ones of the size of vector_probs
        """
        return tf.floor(vector_probs +
                        tf.random_uniform(tf.shape(vector_probs), 0, 1))

    def single_gibbs_step(xk):
        """
        this function will be used two times
        1) while training to sample out the data from the network
        2) while generating the output music by sampling from the network by giving random input vectors
        #Runs a single gibbs step. The visible values are initialized to xk
        """
        hk = sample(tf.sigmoid(
            tf.matmul(xk, W) +
            bh))  #Propagate the visible values to sample the hidden values
        xk = sample(tf.sigmoid(
            tf.matmul(hk, tf.transpose(W)) +
            bv))  #Propagate the hidden values to sample the visible values
        return xk

    def get_gibbs_sample(k):
        #Runs a k-step gibbs chain to sample from the probability distribution of the RBM defined by W, bh, bv
        #Run gibbs steps for k iterations
        count = 0
        x_sample = x
        while (count < k):
            x_sample = single_gibbs_step(x_sample)
            count += 1
        x_sample = tf.stop_gradient(x_sample)
        return x_sample

    ##Declaring x tensor place holder to sample the vectors from the distribution learnt

    x = tf.placeholder(tf.float32, [None, number_of_visible],
                       name="x")  #The placeholder variable that holds our data

    #We need to generate music now......Finally :)
    #To do that we need to sample 50 vectors from the gibbs sample(the whole intelligence is now stored in the weights and the biases of the network.)

    with tf.Session() as sess:
        init = tf.global_variables_initializer()
        sess.run(init)
        print(sess.run(W))
        samples = sample = get_gibbs_sample(1).eval(
            session=sess,
            feed_dict={x: np.zeros((number_of_chords, number_of_visible))})
        for i in range(samples.shape[0]):
            if not any(samples[i, :]):
                continue
            #Here we reshape the vector to be time x notes, and then save the vector as a midi file
            S = np.reshape(sample[i, :],
                           (number_of_timesteps, 2 * music_note_range))
            midi_manipulation.noteStateMatrixToMidi(
                S, "generated_new_music/generated_chord_{}".format(i))
plt.plot(loss_test[11:60], label='test loss', c='xkcd:crimson')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend(prop={'size': 16})
plt.subplot(1, 2, 2)
plt.plot(loss_train[11:60] - rec_loss_train[11:60],
         label='train KL divergence',
         c='xkcd:tomato')
plt.plot(loss_test[11:60] - rec_loss_test[11:60],
         label='test KL divergence',
         c='xkcd:crimson')
plt.xlabel('epochs')
plt.ylabel('loss')
plt.legend(prop={'size': 16})

# generate song
# from test sample:
sample = 5
x = model(test[5, :, :, :].reshape([1, timesteps * freqs]).astype('float32'))
# or from randomly sampled z
n_examples = 1
z = chainer.Variable(
    np.random.normal(0, 1, (n_examples, 20)).astype(np.float32))
# z=np.float32(np.reshape(song[160:260, :], [1, 15600]))
x = model.decode(z)
# convert to midi
generated_song = np.reshape(x.data, [timesteps, freqs])
generated_song = (generated_song > 0.2) * 1
#save generated midi
midi_manipulation.noteStateMatrixToMidi(generated_song)