def __init__(self, model_path, prediction_mode='sampling', first_chords=[1, 3, 2, 1, 1, 3, 2, 1], resample='none', dim_factor=2, temperature=1.0): print('loading chord model ...') self.model = keras.models.load_model(model_path) self.model.reset_states() self.embed_layer_output = K.function([self.model.layers[0].input], [self.model.layers[0].output]) # self.embed_model = keras.ModModel(inputs=model.input,outputs=model.get_layer(.output) self.embed_model = keras.models.Model( inputs=self.model.input, outputs=self.model.get_layer(index=0).output) self.chord_to_index, self.index_to_chords = get_chord_dict() self.prediction_mode = prediction_mode self.temperature = temperature self.resample = resample self.dim_factor = dim_factor self.song = [] for chord in first_chords[:-1]: # print(chord) self.model.predict(array([[chord]])) self.song.append(chord) chord = first_chords[-1] self.song.append(chord) self.current_chord = array([[chord]])
def __init__(self, model_path): print('loading chord model ...') model = keras.models.load_model(model_path) model.reset_states() self.embed_layer_output = K.function([model.layers[0].input], [model.layers[0].output]) self.chord_to_index, self.index_to_chords = get_chord_dict()
# Parameters for song generation: BPM = 100 note_cap = 5 chord_temperature = 1 # Params for seed: # length of the predicted song in bars: num_bars = 64 # The first seed_length number of bars from the seed will be used: seed_length = 4 #pred_song_length = 8*16-seed_length with_seed = True chord_to_index, index_to_chord = data_processing.get_chord_dict() def sample_probability_vector(prob_vector): # Sample a probability vector, e.g. [0.1, 0.001, 0.5, 0.9] sum_probas = sum(prob_vector) if sum_probas > note_cap: prob_vector = (prob_vector / sum_probas) * note_cap note_vector = np.zeros((prob_vector.size), dtype=np.int8) for i, prob in enumerate(prob_vector): note_vector[i] = np.random.multinomial(1, [1 - prob, prob])[1] return note_vector
if not os.path.exists(LOG_DIR): os.makedirs(LOG_DIR) # extract embedding from chord model # chord_model = "models/chords/1564233418-Shifted_False_Lr_1e-05_EmDim_10_opt_Adam_bi_False_lstmsize_512_trainsize_4_testsize_1_samples_per_bar8/model_Epoch100_4.pickle" chord_model = "models/chords/1564241436-Shifted_False_Lr_1e-05_EmDim_10_opt_Adam_bi_False_lstmsize_512_trainsize_4_testsize_1_samples_per_bar8_dataset_data/Nottingham_unshifted_dataset/original/model_Epoch30_4.pickle" model = keras.models.load_model(chord_model) embeddings = model.layers[0].get_weights() embeddings = np.array(embeddings) # embeddings shape: (1, 50, 10) embeddings = embeddings.reshape(-1, embeddings.shape[2]) # find the chord embeddings with the name chords_with_name = {} index_to_chords = get_chord_dict()[1] total_data_num = embeddings.shape[0] for i in range(total_data_num): triad_tuple = index_to_chords[i] if triad_tuple in chord_triad_tuple_to_chord_name: chords_with_name[i] = chord_triad_tuple_to_chord_name[triad_tuple] new_embeddings = [] for i in chords_with_name.keys(): new_embeddings.append(embeddings[i]) # high_dimensional_vectors = np.random.rand(50, 10) # tensors_to_visualize = tf.Variable(high_dimensional_vectors, name="high_dim_vectors") tensors_to_visualize = tf.Variable(np.array(new_embeddings),