def random_training_examples(X_train, X_val=[], seed_len=1, count=1): X_val = np.array(X_val) num_train_examples = X_train.shape[0] num_val_examples = X_val.shape[0] val_split = float(num_val_examples) / (num_train_examples + num_val_examples) val_count = int(val_split*count); train_count = count - val_count; train_examples = np.zeros((0,X_train.shape[1]*seed_len,X_train.shape[2])) for i in xrange(train_count): next_example = seed_generator.generate_copy_seed_sequence(seed_length=seed_len, training_data=X_train) train_examples = np.concatenate((train_examples, next_example), axis=0) val_examples = np.zeros((0,X_val.shape[1]*seed_len,X_val.shape[2])) for i in xrange(val_count): next_example = seed_generator.generate_copy_seed_sequence(seed_length=seed_len, training_data=X_val) val_examples = np.concatenate((val_examples, next_example), axis=0) return (train_examples, val_examples)
def generate_from_data(model, x_data, max_seq_len, seed_len=1, gen_count=1, include_raw_seed=False, include_model_seed=False, uncenter_data=False, X_var=None, X_mean=None): print('Starting generation!') #Here's the interesting part #We need to create some seed sequence for the algorithm to start with #Currently, we just grab an existing seed sequence from our training data and use that #However, this will generally produce verbatum copies of the original songs #In a sense, choosing good seed sequences = how you get interesting compositions #There are many, many ways we can pick these seed sequences such as taking linear combinations of certain songs #We could even provide a uniformly random sequence, but that is highly unlikely to produce good results outputs = [] for i in xrange(gen_count): print("Generating sample {0}/{1}".format(i+1, gen_count)) seed_seq = seed_generator.generate_copy_seed_sequence(seed_length=seed_len, training_data=x_data) output = sequence_generator.generate_from_example_seed(model, seed_seq, max_seq_len, include_raw_seed=include_raw_seed, include_model_seed=include_model_seed, uncenter_data=uncenter_data, data_variance=X_var, data_mean=X_mean) outputs.append(output) model.reset_states() # If model is stateful, states should be reset print('Finished generation!') return np.array(outputs)
def gen(folder, actF, fft, inputSongs): config = nn_config.get_neural_net_configuration() nn = "TOYPAJ-NPWeights50-" model_filename = folder + nn + actF actF = actF + "moid" if actF == "sig" else actF sample_frequency = config['sampling_frequency'] inputFile = folder + "TOYPAYJ-Processed" output_filename = folder + actF + 'generated_' #Load up the training data print ('Loading training data') #X_train is a tensor of size (num_train_examples, num_timesteps, num_frequency_dims) #y_train is a tensor of size (num_train_examples, num_timesteps, num_frequency_dims) #X_mean is a matrix of size (num_frequency_dims,) containing the mean for each frequency dimension #X_var is a matrix of size (num_frequency_dims,) containing the variance for each frequency dimension X_train = np.load(inputFile + '_x.npy') print( X_train.shape) print( type(X_train)) y_train = np.load(inputFile + '_y.npy') X_mean = np.load(inputFile + '_mean.npy') X_var = np.load(inputFile + '_var.npy') print ('Finished loading training data') #Figure out how many frequencies we have in the data freq_space_dims = X_train.shape[1:] hidden_dims = config['hidden_dimension_size'] #Creates a lstm network model = network_utils.create_lstm_network(num_frequency_dimensions=freq_space_dims, num_hidden_dimensions=hidden_dims, actF = actF) #You could also substitute this with a RNN or GRU #model = network_utils.create_gru_network() print( model_filename) #Load existing weights if available if os.path.isfile(model_filename): model.load_weights(model_filename) else: print('Model filename ' + model_filename + ' could not be found!') print ('Starting generation!') #Here's the interesting part #We need to create some seed sequence for the algorithm to start with #Currently, we just grab an existing seed sequence from our training data and use that #However, this will generally produce verbatum copies of the original songs #In a sense, choosing good seed sequences = how you get interesting compositions #There are many, many ways we can pick these seed sequences such as taking linear combinations of certain songs #We could even provide a uniformly random sequence, but that is highly unlikely to produce good results seed_len = 1 block_size = X_train.shape[2] / 2 if fft else X_train.shape[2] for song in inputSongs: name = song[song.rfind('/') + 1:] print( name) """ seed_seq = seed_generator.generate_from_file( filename=song, seed_length = 1, block_size = block_size, seq_len = 40, std = X_var, mean = X_mean, fft = fft, offsetSec = 53) """ seed_seq = seed_generator.generate_copy_seed_sequence(1, X_train) print(seed_seq.shape) max_seq_len = 6; #Defines how long the final song is. Total song length in samples = max_seq_len * example_len output = [] for i in xrange(seed_seq.shape[1]): output.append(seed_seq[0][i].copy()) save_generated_example(folder + "input_3" + name, output, sample_frequency=sample_frequency, useTimeDomain=not fft) output = sequence_generator.generate_from_seed(model=model, seed=seed_seq, sequence_length=max_seq_len, data_variance=X_var, data_mean=X_mean) print( len(output)) print ('Finished generation!') #Save the generated sequence to a WAV file save_generated_example(output_filename + "3" + name, output, sample_frequency=sample_frequency, useTimeDomain=not fft)
# Creates a LSTM network model = network_utils.create_lstm_network(num_frequency_dimensions=freq_space_dims, num_hidden_dimensions=hidden_dims) # You could also substitute this with a RNN or GRU # model = network_utils.create_gru_network() # # Load existing weights if available if os.path.isfile(model_filename): model.load_weights(model_filename) else: print('Model filename ' + model_filename + ' could not be found!') print ('Starting generation!') # Here's the interesting part # We need to create some seed sequence for the algorithm to start with # Currently, we just grab an existing seed sequence from our training data and use that # However, this will generally produce verbatum copies of the original songs # In a sense, choosing good seed sequences = how you get interesting compositions # There are many, many ways we can pick these seed sequences such as taking linear combinations of certain songs # We could even provide a uniformly random sequence, but that is highly unlikely to produce good results seed_len = 1 seed_seq = seed_generator.generate_copy_seed_sequence(seed_length=seed_len, training_data=X_train) max_seq_len = 10 # Defines how long the final song is. Total song length in samples = max_seq_len * example_len output = sequence_generator.generate_from_seed(model=model, seed=seed_seq, sequence_length=max_seq_len, data_variance=X_var, data_mean=X_mean) print ('Finished generation!') # Save the generated sequence to a WAV file save_generated_example(output_filename, output, sample_frequency=sample_frequency)
# Load existing weights if available if os.path.isfile(model_filename): model.load_weights(model_filename) else: print('Model filename ' + model_filename + ' could not be found!') print('Starting generation!') # Here's the interesting part # We need to create some seed sequence for the algorithm to start with # Currently, we just grab an existing seed sequence from our training data and use that # However, this will generally produce verbatum copies of the original songs # In a sense, choosing good seed sequences = how you get interesting compositions # There are many, many ways we can pick these seed sequences such as taking linear combinations of certain songs # We could even provide a uniformly random sequence, but that is highly unlikely to produce good results seed_len = 1 seed_seq = seed_generator.generate_copy_seed_sequence(seed_length=seed_len, training_data=X_train) max_seq_len = 10 # Defines how long the final song is. Total song length in samples = max_seq_len * example_len output = sequence_generator.generate_from_seed(model=model, seed=seed_seq, sequence_length=max_seq_len, data_variance=X_var, data_mean=X_mean, data_size=data_size) print('Finished generation!') # Save the generated sequence to a WAV file save_generated_example(output_filename, output, sample_frequency=sample_frequency)
if os.path.exists(MODEL_FILENAME): MODEL.load_weights(MODEL_FILENAME) else: print('Model filename ' + MODEL_FILENAME + ' could not be found!') def show_values(): for k, v in CONFIG.items(): print(k + ': ' + str(v)) show_values() print('Starting generation!') SEED_LEN = 1 SEED_SEQ = seed_generator.generate_copy_seed_sequence(seed_length=SEED_LEN, training_data=X_TRAIN) # Defines final song length. Total song length in samples = max_seq_len * example_len MAX_SEQ_LEN = 10 OUTPUT = sequence_generator.generate_from_seed(model=MODEL, seed=SEED_SEQ, data_variance=X_VAR, data_mean=X_MEAN, sequence_length=MAX_SEQ_LEN) print('Finished generation!') # Save the generated sequence to a WAV file save_generated_example(str(OUTPUT_FILENAME), OUTPUT,