Example #1
0
def gen_adaptive(m,pcs,times,keep_thoughts=False,name="final"):
    pcs = multi_training.loadPieces("./music/單一小節資料夾")
	xIpt, xOpt = map(lambda x: numpy.array(x, dtype='int8'), multi_training.getPieceSegment(pcs))
	all_outputs = [xOpt[0]]
	if keep_thoughts:
		all_thoughts = []
	m.start_slow_walk(xIpt[0])
	cons = 1
Example #2
0
def test_training_model():
    pcs = multi_training.loadPieces("Train_data")
    data = multi_training.getPieceBatch(pcs)

    model = m.BiaxialRNNModel([300, 300], [100, 50])
    print(model)

    out = model(data, training=True)

    print(out.shape)
Example #3
0
def test_predict_one_step_model():
    pcs = multi_training.loadPieces("Train_data")
    data, _ = multi_training.getPieceSegment(pcs)
    data = torch.Tensor(data[0])
    print(data.shape)

    model = m.BiaxialRNNModel([300, 300], [100, 50])
    print(model)

    out = model(data)  # Done in order to print
Example #4
0
def test_predict_n_step_model():
    pcs = multi_training.loadPieces("Train_data")
    data, _ = multi_training.getPieceSegment(pcs)
    data = torch.Tensor(data[0])
    print(data.shape)

    model = m.BiaxialRNNModel([300, 300], [100, 50])
    print(model)

    out = model(data, 5)
    print(out)
    print(torch.Tensor(np.array(out)).shape)
Example #5
0
def main():
    # load midi
    dirpath = '../'
    pieces = loadPieces(dirpath)

    # divide train valid
    valid_pieces = pieces[:num_valid]
    train_pieces = pieces[num_valid:]
    valid_gen = BatchGenerator(pieces[0], valid_batch_size, 1)
    train_gen = MixedGenarator(pieces, batch_size, num_unrolling)

    # create model ans start training
    model = LSTM_model(layer_size, batch_size, num_unrolling)
    model.train(train_gen, valid_gen, train_step=10000, summary_frequency=100)
def run_training(genre, percentage_of_data):
    name = genre + "_" + str(percentage_of_data)

    create_output_file(name)
    pcs = multi_training.loadPieces("data/" + genre, percentage_of_data)
    print("Loaded")
    m = model.Model([300, 300], [100, 50], dropout=0.5)
    print("Built model")
    m.learned_config = pickle.load(
        open("output/mozart_20_clustering/params7000.p", "rb"))
    #print("Loaded weights")
    #multi_training.trainPiece(m, pcs, 7000, output_dir=name)
    print("Trained model")
    gen_adaptive(m, pcs, 10, name=name)
Example #7
0
import sys

import model

m = model.Model([300, 300], [100, 50], dropout=0.5)

import multi_training
pcs = multi_training.loadPieces(str(sys.argv[1]))

multi_training.trainPiece(m, pcs, 10000)

import main

main.gen_adaptive(m, pcs, 10, name="composition")
Example #8
0
                cons = 1
            cons -= 0.02
        else:
            cons += (1 - cons) * 0.3
        all_outputs.append(resdata[-1])
        if keep_thoughts:
            all_thoughts.append(resdata)
    noteStateMatrixToMidi(numpy.array(all_outputs), 'output/' + name)
    if keep_thoughts:
        pickle.dump(all_thoughts, open('output/' + name + '.p', 'wb'))


def fetch_train_thoughts(m, pcs, batches, name="trainthoughts"):
    all_thoughts = []
    for i in range(batches):
        ipt, opt = multi_training.getPieceBatch(pcs)
        thoughts = m.update_thought_fun(ipt, opt)
        all_thoughts.append((ipt, opt, thoughts))
    pickle.dump(all_thoughts, open('output/' + name + '.p', 'wb'))


if __name__ == '__main__':

    pcs = multi_training.loadPieces("melody", "accompaniment")

    m = model.Model([300, 300], [100, 50], dropout=0.5)

    multi_training.trainPiece(m, pcs, 10000)

    pickle.dump(m.learned_config, open("output/final_learned_config.p", "wb"))
	if keep_thoughts:
		pickle.dump(all_thoughts, open('output/'+name+'.p','wb'))

def fetch_train_thoughts(m,pcs,batches,name="trainthoughts"):
	all_thoughts = []
	for i in range(batches):
		ipt, opt = multi_training.getPieceBatch(pcs)
		thoughts = m.update_thought_fun(ipt,opt)
		all_thoughts.append((ipt,opt,thoughts))
	pickle.dump(all_thoughts, open('output/'+name+'.p','wb'))

if __name__ == '__main__':

	music_path = raw_input("Path to music folder: ")

	pcs = multi_training.loadPieces(music_path)

	print "Pieces loaded, building neural network"
	m = model.Model([300,300],[100,50], dropout=0.5)

	error_interval = 100
	sample_interval = 500
	epochs_per_training = 10000

	composition_name = "composition"
	composition_length = 10

	epochs_trained = 0

	#Create menu
	while True:
Example #10
0
def fetch_train_thoughts(m, pcs, batches, name="trainthoughts"):
    all_thoughts = []
    for i in range(batches):
        ipt, opt = multi_training.getPieceBatch(pcs)
        thoughts = m.update_thought_fun(ipt, opt)
        all_thoughts.append((ipt, opt, thoughts))
    pickle.dump(all_thoughts, open('output/' + name + '.p', 'wb'))


if __name__ == '__main__':
    path = 'C_music'
    batches = 7000
    batches_old = 0

    pieces = multi_training.loadPieces(dirpath=path)

    m = model.Model([300, 300], [100, 50], dropout=0.5)

    m.learned_config = pickle.load(open("output/params11000.p", "rb"))

    gen_adaptive(m,
                 pieces,
                 1,
                 name="composition_{0}".format(batches + batches_old),
                 rbm=True)
    exit()
    print 'Training {0}+{1} batches on {2}'.format(batches, batches_old, path)

    multi_training.trainPiece(
        m, pieces, [batches, batches_old])  #, notes_to_input = None)
Example #11
0
    pickle.dump(all_thoughts, open('output/' + name + '.p', 'wb'))


def get_last_epoch(model_directory):
    # Get all file names in model_directory
    files = [file for file in os.listdir(model_directory) if '.p' in file]
    # Function that go over a string and create in a list all individual
    #   numbers and then return all of them joined as a string.
    get_number = (lambda string: "".join(list(filter(lambda c: c.isdigit(),
                                                     string))))
    # Map the get_number over all the names and return them as integers
    epochs = list(map(lambda string: int(get_number(string)), files))
    epochs.append(0)  # Append 0 in case of void list
    return max(epochs)


if __name__ == '__main__':
    # Directory in which the parameters that have been calculated for the model
    #  are saved.
    music_type_dir = "Scale"
    save_output_dir = music_type_dir + "/output"
    os.makedirs(save_output_dir, exist_ok=True)

    # Create and evaluate model
    pcs = multi_training.loadPieces("Scale2")
    start = get_last_epoch(save_output_dir)

    m = model.BiaxialRNNModel([300, 300], [100, 50])

    multi_training.trainPiece(m, pcs, 10000, "Scale", start)
Example #12
0
    if keep_thoughts:
        pickle.dump(all_thoughts, open('output/' + name + '.p', 'wb'))


def fetch_train_thoughts(m, pcs, batches, name="trainthoughts"):
    all_thoughts = []
    for i in range(batches):
        ipt, opt = multi_training.getPieceBatch(pcs)
        thoughts = m.update_thought_fun(ipt, opt)
        all_thoughts.append((ipt, opt, thoughts))
    pickle.dump(all_thoughts, open('output/' + name + '.p', 'wb'))


if __name__ == '__main__':

    pcs = multi_training.loadPieces("music/beeth/")

    print('nun lädt der eumel das model')

    # To use the model, you need to first create an instance of the Model class
    m = model.Model([300, 300], [100, 50], dropout=0.5)
    # output and the learned parameters and sample every 50 iterations.

    m.learned_config = pickle.load(open("my_final_learned_config.p", "rb"))

    print('nun tranieren')
    multi_training.trainPiece(m, pcs, 5)

    # generate midi with model after training

    gen_adaptive(m, pcs, 5, name="composition9")
Example #13
0
def create_pieces():
    return multi_training.loadPieces("music")
Example #14
0
				cons = 1
			cons -= 0.02
		else:
			cons += (1 - cons)*0.3
		all_outputs.append(resdata[-1])
		if keep_thoughts:
			all_thoughts.append(resdata)
	noteStateMatrixToMidi(numpy.array(all_outputs),'output/'+name)
	if keep_thoughts:
		pickle.dump(all_thoughts, open('output/'+name+'.p','wb'))

def fetch_train_thoughts(m,pcs,batches,name="trainthoughts"):
	all_thoughts = []
	for i in range(batches):
		ipt, opt = multi_training.getPieceBatch(pcs)
		thoughts = m.update_thought_fun(ipt,opt)
		all_thoughts.append((ipt,opt,thoughts))
	pickle.dump(all_thoughts, open('output/'+name+'.p','wb'))

if __name__ == '__main__':

	pcs = multi_training.loadPieces("music")

	print "building model"
	m = model.Model([300,300],[100,50], dropout=0.5)

	print "start training"
	multi_training.trainPiece(m, pcs, 1000)

	pickle.dump( m.learned_config, open( "output/final_learned_config.p", "wb" ) )
Example #15
0
def train(music_folder, out_model_name, just_this_midi=None):

    Working_Directory = os.getcwd()
    Music_Directory = "../proyecto-beatles/midis_por_instrumento/"
    print(Working_Directory)
    Midi_Directories = [music_folder]
    max_time_steps = 256  # only files atleast this many 16th note steps are saved
    num_validation_pieces = 2
    practice_batch_size = 15
    practice_num_timesteps = 128
    num_t_units = [200, 200]

    start_time = time.time()
    max_iteration = 15000  #50000
    iter_midi = 3000
    loss_hist = []
    loss_valid_hist = []
    restore_model_name = None  #'Long_Train'
    save_model_name = out_model_name
    batch_size = 5
    num_timesteps = 256
    keep_prob = .3
    num_n_units = [60, 60]
    limit_train = None

    # Gather the training pieces from the specified directories
    training_pieces = {}

    if just_this_midi:
        training_pieces = {
            just_this_midi:
            midi_musical_matrix.midiToNoteStateMatrix(just_this_midi)
        }
    else:
        for f in range(len(Midi_Directories)):
            Training_Midi_Folder = Music_Directory + Midi_Directories[f]
            training_pieces = {
                **training_pieces,
                **multi_training.loadPieces(Training_Midi_Folder,
                                            max_time_steps,
                                            max_elements=limit_train)
            }

    # Set aside a random set of pieces for validation purposes
    validation_pieces = {}

    if just_this_midi:
        for f in range(1):
            Training_Midi_Folder = Music_Directory + Midi_Directories[f]
            validation_pieces = {
                **validation_pieces,
                **multi_training.loadPieces(Training_Midi_Folder,
                                            max_time_steps,
                                            max_elements=1)
            }
    else:
        for v in range(num_validation_pieces):
            index = random.choice(list(training_pieces.keys()))
            validation_pieces[index] = training_pieces.pop(index)

    print('')
    print('Number of training pieces = ', len(training_pieces))
    print('Number of validation pieces = ', len(validation_pieces))

    # Generate sample Note State Matrix for dimension measurement and numerical checking purposes
    # (Using external code to generate the Note State Matrix but using our own NoteInputForm (as defined in author's code) function

    _, sample_state = multi_training.getPieceBatch(training_pieces,
                                                   practice_batch_size,
                                                   practice_num_timesteps)
    sample_state = np.array(sample_state)
    sample_state = np.swapaxes(sample_state, axis1=1, axis2=2)
    print('Sample of State Input Batch: shape = ', sample_state.shape)

    # Build the Model Graph:
    tf.reset_default_graph()
    print('Building Graph...')
    #Capture number of notes from sample
    num_notes = sample_state.shape[1]

    # Graph Input Placeholders
    Note_State_Batch = tf.placeholder(dtype=tf.float32,
                                      shape=[None, num_notes, None, 2],
                                      name='node_state_batch')
    time_init = tf.placeholder(dtype=tf.int32, shape=(), name='time_init')

    #Generate expanded tensor from batch of note state matrices
    # Essential the CNN 'window' of this network
    Note_State_Expand = Input_Kernel(Note_State_Batch,
                                     Midi_low=24,
                                     Midi_high=101,
                                     time_init=time_init)

    print('Note_State_Expand shape = ', Note_State_Expand.get_shape())

    # lSTM Time Wise Training Graph

    output_keep_prob = tf.placeholder(dtype=tf.float32,
                                      shape=(),
                                      name="output_keep_prob")

    # Generate initial state (at t=0) placeholder
    timewise_state = []
    for i in range(len(num_t_units)):
        timewise_c = tf.placeholder(
            dtype=tf.float32,
            shape=[None, num_t_units[i]],
            name='timewise_c{0}'.format(i))  #None = batch_size * num_notes
        timewise_h = tf.placeholder(dtype=tf.float32,
                                    shape=[None, num_t_units[i]],
                                    name='timewise_h{0}'.format(i))
        timewise_state.append(LSTMStateTuple(timewise_h, timewise_c))

    timewise_state = tuple(timewise_state)

    timewise_out, timewise_state_out = LSTM_TimeWise_Training_Layer(
        input_data=Note_State_Expand,
        state_init=timewise_state,
        output_keep_prob=output_keep_prob)

    print('Time-wise output shape = ', timewise_out.get_shape())

    #LSTM Note Wise Graph

    # Generate initial state (at n=0) placeholder
    notewise_state = []
    for i in range(len(num_n_units)):
        notewise_c = tf.placeholder(
            dtype=tf.float32,
            shape=[None, num_n_units[i]],
            name='notewise_c{0}'.format(i))  #None = batch_size * num_timesteps
        notewise_h = tf.placeholder(dtype=tf.float32,
                                    shape=[None, num_n_units[i]],
                                    name='notewise_h{0}'.format(i))
        notewise_state.append(LSTMStateTuple(notewise_h, notewise_c))

    notewise_state = tuple(notewise_state)

    y_out, note_gen_out = LSTM_NoteWise_Layer(
        timewise_out,
        state_init=notewise_state,
        output_keep_prob=output_keep_prob)
    note_gen_out = tf.identity(note_gen_out, name="note_gen_out")

    p_out = tf.sigmoid(y_out)
    print('y_out shape = ', y_out.get_shape())
    print('generated samples shape = ', note_gen_out.get_shape())

    # Loss Function and Optimizer

    loss, log_likelihood = Loss_Function(Note_State_Batch, y_out)
    optimizer = tf.train.AdadeltaOptimizer(learning_rate=1).minimize(loss)
    #optimizer = tf.train.RMSPropOptimizer
    print('Graph Building Complete')

    # Training

    # Save Model
    Output_Directory = Working_Directory + "/Output/" + save_model_name
    directory = os.path.dirname(Output_Directory)

    try:
        print('creating new destination folder')
        os.mkdir(directory)
    except:
        print('destination folder exists')

    with tf.Session() as sess:
        sess.run(tf.global_variables_initializer())
        saver = tf.train.Saver()

        # try to restore the pre_trained
        if restore_model_name is not None:
            Load_Directory = Working_Directory + "/Output/" + restore_model_name

            print("Load the model from: {}".format(restore_model_name))
            saver.restore(sess,
                          Load_Directory + '/{}'.format(restore_model_name))

        # Training Loop
        for iteration in range(max_iteration + 1):

            # Generate random batch of training data
            if (iteration % 100 == 0):
                print('Obtaining new batch of pieces')
                _, batch_input_state = multi_training.getPieceBatch(
                    training_pieces, batch_size,
                    num_timesteps)  # not using their 'convolution' filter
                batch_input_state = np.array(batch_input_state)
                batch_input_state = np.swapaxes(batch_input_state,
                                                axis1=1,
                                                axis2=2)

            feed_dict = get_feed_dict(batch_input_state, 0, keep_prob)
            feed_initial_st = get_feed_initil_state(batch_size, num_notes,
                                                    num_timesteps, num_t_units,
                                                    num_n_units)
            feed_dict = {**feed_dict, **feed_initial_st}

            # Run Session
            loss_run, log_likelihood_run, _, note_gen_out_run = sess.run(
                [loss, log_likelihood, optimizer, note_gen_out],
                feed_dict=feed_dict)

            # Periodically save model and loss histories
            if (iteration % 1000 == 0) & (iteration > 0):
                save_path = saver.save(
                    sess, Output_Directory + '/{}'.format(save_model_name))
                print("Model saved in file: %s" % save_path)
                np.save(Output_Directory + "/ training_loss.npy", loss_hist)
                np.save(Output_Directory + "/ valid_loss.npy", loss_valid)

            # Regularly Calculate Validation loss and store both training and validation losses
            if (iteration % 100) == 0 & (iteration > 0):
                # Calculation Validation loss
                _, batch_input_state_valid = multi_training.getPieceBatch(
                    validation_pieces, batch_size,
                    num_timesteps)  # not using their 'convolution' filter
                batch_input_state_valid = np.array(batch_input_state_valid)
                batch_input_state_valid = np.swapaxes(batch_input_state_valid,
                                                      axis1=1,
                                                      axis2=2)

                feed_dict = get_feed_dict(batch_input_state_valid, 0,
                                          keep_prob)
                feed_initial_st = get_feed_initil_state(
                    batch_size, num_notes, num_timesteps, num_t_units,
                    num_n_units)
                feed_dict = {**feed_dict, **feed_initial_st}

                loss_valid, log_likelihood_valid = sess.run(
                    [loss, log_likelihood], feed_dict=feed_dict)

                print('epoch = ', iteration, ' / ', max_iteration, ':')
                print('Training loss = ', loss_run,
                      '; Training log likelihood = ', log_likelihood_run)
                print('Validation loss = ', loss_valid,
                      '; Validation log likelihood = ', log_likelihood_valid)

                loss_hist.append(loss_run)
                loss_valid_hist.append(loss_valid)

            # Periodically generate Sample of music
            if (iteration % iter_midi) == 0 and (iteration > 0):
                generate_midi(
                    sess, num_notes, num_t_units, num_n_units,
                    "midi_{1}_iteracion{0}".format(iteration, out_model_name))

    end_time = time.time()

    print('Training time = ', end_time - start_time, ' seconds')

    # Plot the loss histories
    os.makedirs(Output_Directory, exist_ok=True)
    plt.switch_backend("agg")
    plt.plot(loss_hist, label="Training Loss")
    plt.plot(loss_valid_hist, label="Validation Loss")
    plt.legend()
    plt.savefig(
        os.path.join(Output_Directory,
                     "train-val_loss_{0}.png".format(out_model_name)))
Example #16
0
def main():
    convert();
    m = model.Model([300, 300], [100, 50], dropout=0.5)
    pcs = multi_training.loadPieces("music")
    multi_training.trainPiece(m, pcs, 10000)
    gen_adaptive(m, pcs, 10, name="composition")
Example #17
0
def web_endpoint_create():
    pcs = multi_training.loadPieces("music")
    m = create_model()
    return m, pcs
Example #18
0
    // 前三小節
    resdata = m.slow_walk_fun( cons ) 利用model產生一個小節的鼓
	for time in range(三小節):
		nnotes = numpy.sum(resdata[-1][:,0])
		if nnotes < 2:
			if cons > 1:
				cons = 1
			cons -= 0.02
		else:
			cons += (1 - cons)*0.3
		all_outputs.append(resdata[-1])
		if keep_thoughts:
			all_thoughts.append(resdata)
            
    // 開始接上最後一小節
    pcs = multi_training.loadPieces("./music/過門資料夾")
    
	xIpt, xOpt = map(lambda x: numpy.array(x, dtype='int8'), multi_training.getPieceSegment(pcs))
	all_outputs = [xOpt[0]]
	if keep_thoughts:
		all_thoughts = []
	m.start_slow_walk(xIpt[0])
	cons = 1
    
    // 利用model產生過門
    resdata = m.slow_walk_fun( cons )
	nnotes = numpy.sum(resdata[-1][:,0])
	if nnotes < 2:
		if cons > 1:
			cons = 1
		cons -= 0.02
    noteStateMatrixToMidi(numpy.array(all_outputs), 'output/' + name)
    if keep_thoughts:
        pickle.dump(all_thoughts, open('output/' + name + '.p', 'wb'))


def fetch_train_thoughts(m, pcs, batches, name="trainthoughts"):
    all_thoughts = []
    for i in range(batches):
        ipt, opt = multi_training.getPieceBatch(pcs)
        thoughts = m.update_thought_fun(ipt, opt)
        all_thoughts.append((ipt, opt, thoughts))
    pickle.dump(all_thoughts, open('output/' + name + '.p', 'wb'))


if __name__ == '__main__':
    pcs = multi_training.loadPieces("gameboy-music")  #tetris and pokemon
    print "pieces loaded"
    m = model.Model([100, 100], [30, 15], dropout=0.5)
    m.learned_config = pickle.load(open("gameboyx3.p", "rb"))
    print "model created"
    multi_training.trainPiece(m, pcs, 2000)
    print "training complete"
    pickle.dump(m.learned_config, open("output/final_learned_config.p", "wb"))

##if __name__ == '__main__':
##        pcs = multi_training.loadPieces("selected-gameboy")
##        print "pieces loaded"
##        m = model.Model([100,100],[30,15], dropout=0.5)
##        print "model created"
##        m.learned_config = pickle.load(open( "output/params950.p", "rb" ) )
##        print "params loaded"
	noteStateMatrixToMidi(numpy.array(all_outputs),'output/'+name)
	if keep_thoughts:
		pickle.dump(all_thoughts, open('output/'+name+'.p','wb'))

def fetch_train_thoughts(m,pcs,batches,name="trainthoughts"):
	all_thoughts = []
	for i in range(batches):
		ipt, opt = multi_training.getPieceBatch(pcs)
		thoughts = m.update_thought_fun(ipt,opt)
		all_thoughts.append((ipt,opt,thoughts))
	pickle.dump(all_thoughts, open('output/'+name+'.p','wb'))

def create_output_directory():
	outputdir = "output"
	try:
		os.stat(outputdir)
	except:
		os.mkdir(outputdir)

if __name__ == '__main__':

	pcs = multi_training.loadPieces("music")

	m = model.Model([300,300],[100,50], dropout=0.5)

	create_output_directory()

	multi_training.trainPiece(m, pcs, 10000)

	pickle.dump( m.learned_config, open( "output/final_learned_config.p", "wb" ) )
Example #21
0
    if keep_thoughts:
        pickle.dump(all_thoughts, open("output/" + name + ".p", "wb"))


def fetch_train_thoughts(m, pcs, batches, name="trainthoughts"):
    all_thoughts = []
    for i in range(batches):
        ipt, opt = multi_training.getPieceBatch(pcs)
        thoughts = m.update_thought_fun(ipt, opt)
        all_thoughts.append((ipt, opt, thoughts))
    pickle.dump(all_thoughts, open("output/" + name + ".p", "wb"))


if __name__ == "__main__":
    path = "C_music"
    batches = 7000
    batches_old = 0

    pieces = multi_training.loadPieces(dirpath=path)

    m = model.Model([300, 300], [100, 50], dropout=0.5)

    m.learned_config = pickle.load(open("output/params11000.p", "rb"))

    gen_adaptive(m, pieces, 1, name="composition_{0}".format(batches + batches_old), rbm=True)
    exit()
    print "Training {0}+{1} batches on {2}".format(batches, batches_old, path)

    multi_training.trainPiece(m, pieces, [batches, batches_old])  # , notes_to_input = None)
    pickle.dump(m.learned_config, open("output/final_learned_config_{0}.p".format(batches + batches_old), "wb"))