x = to_categorical(x.flatten(), num_classes=DIM).\ reshape(x.shape[0], x.shape[1], DIM) y = y.reshape(x.shape[0], x.shape[1], 1) yield (x, y) # model model = StackedRNN( timespan=LEN, input_dim=DIM, output_dim=DIM, cells=[512, 512, 512], ) # block_kwargs={'kernel_regularizer': l2(1e-5)}) model.build() # model.model.load_weights('temp/stackedrnn_kernel_l2.h5') # model.model.load_weights('temp/simple_rnn.h5') # model.train(data_generator(), # opt=1e-5, # steps_per_epoch=30, # epochs=100, # save_path='temp/simple_rnn.h5') model.build_generator('temp/e-comp_simple_rnn.h5') res = model.generate(seed=32, length=2000) mid = Song() track = mid.add_track() for msgi in AllInOneCoder().decode(res): track.append(msgi) mid.save_as('simple_rnn.mid')
import numpy as np from DeepSymphony.utils.bleu import bleu from DeepSymphony.models import StackedRNN from keras.utils.np_utils import to_categorical from DeepSymphony.coders import AllInOneCoder from keras.models import load_model from keras.layers import Embedding PREFIX = 5 ANSWER = 50 LEN = PREFIX + ANSWER TEST = 100 DIM = 128 + 128 + 100 + 7 coder = AllInOneCoder() RNG = np.random.RandomState(64) def tokenize(x): if x.ndim == 1: return map(coder.code_to_name, x) elif x.ndim == 2: return map(coder.code_to_name, x.argmax(1)) def test_emb_stackrnn(data, weight='temp/emb_stackrnn.h5'): emb_w = load_model('temp/emb.h5').layers[2].layers[1].get_weights() model = StackedRNN(timespan=LEN, input_dim=DIM, output_dim=DIM, cells=[512, 512, 512],
x.append(np.array(pairs)) y.append(np.array(labels)) x = np.array(x) y = np.array(y) yield ([x[:, :, 0], x[:, :, 1]], y) (x1, x2), sim = data_generator().next() model = EmbeddingModel(input_dim=DIM, output_dim=512, seq_len=x1.shape[1]) # model.train(data_generator(), # save_path='/tmp/emb.h5', # steps_per_epoch=50, # epochs=1000) coder = AllInOneCoder() x = np.arange(DIM).reshape(-1, 1) gen = model.build_generator('temp/emb.h5') res = model.generate(x).reshape(DIM, -1) pca = decomposition.PCA(n_components=2, ) tsne = manifold.TSNE(n_components=2, ) plot_embedding(tsne.fit_transform(res), map(coder.code_to_name, x.flatten())) plt.show() print res.shape # while True: # ind = np.random.randint(DIM) for ind in range(DIM): print('Most similar notes with {}'.format(coder.code_to_name(ind)))
from DeepSymphony.coders import AllInOneCoder from DeepSymphony.models import StackedRNN from DeepSymphony.utils import Song from DeepSymphony.utils.stat import LCS from tqdm import tqdm from pprint import pprint from keras import optimizers if __name__ == '__main__': LEN = 100 # length of input DIM = 128+128+100+7 data, filelist = Song.load_from_dir( "./datasets/easymusicnotes/", encoder=AllInOneCoder(return_indices=True), return_list=True) def data_generator(): batch_size = 32 while True: x = [] y = [] for _ in range(batch_size): ind = np.random.randint(len(data)) start = np.random.randint(data[ind].shape[0]-LEN-1) x.append(data[ind][start:start+LEN]) y.append(data[ind][start+1:start+LEN+1]) x = np.array(x) y = np.array(y)
import numpy as np from keras.utils.np_utils import to_categorical from DeepSymphony.coders import AllInOneCoder from DeepSymphony.models import StackedRNN from DeepSymphony.utils import Song from DeepSymphony.utils.constants import NOTE_NUMBER from DeepSymphony.utils.stat import histogram, histogram_onehot,\ min_norm, norm if __name__ == '__main__': LEN = 2000 # length of input DIM_IN = 128 + 128 + 100 + 7 + len(NOTE_NUMBER) DIM_OUT = 128 + 128 + 100 + 7 coder = AllInOneCoder() H_TEMP = 0.05 data = np.load('./datasets/e-comp-allinone.npz')['data'] hist = np.load('./datasets/e-comp-allinone-hist.npz')['hist'] def data_generator(): batch_size = 32 while True: x = [] y = [] h = [] for _ in range(batch_size): ind = np.random.randint(len(data)) start = np.random.randint(data[ind].shape[0] - LEN - 1) x.append(data[ind][start:start + LEN]) y.append(data[ind][start + 1:start + LEN + 1])
import numpy as np from keras.utils.np_utils import to_categorical from DeepSymphony.coders import AllInOneCoder from DeepSymphony.models import StackedRNN from DeepSymphony.utils import Song from DeepSymphony.utils.constants import NOTE_NUMBER from DeepSymphony.utils.stat import histogram, histogram_onehot,\ min_norm, norm if __name__ == '__main__': LEN = 100 # length of input DIM_IN = 128 + 128 + 100 + 7 + len(NOTE_NUMBER) DIM_OUT = 128 + 128 + 100 + 7 coder = AllInOneCoder() H_TEMP = 0.05 data, filelist = Song.load_from_dir( "./datasets/easymusicnotes/", encoder=AllInOneCoder(return_indices=True), return_list=True) hist = np.load('./datasets/e-comp-allinone-hist.npz')['hist'] def data_generator(): batch_size = 32 while True: x = [] y = [] h = [] for _ in range(batch_size): ind = np.random.randint(len(data))
from DeepSymphony.utils import Song from DeepSymphony.utils.stat import LCS from tqdm import tqdm from pprint import pprint from DeepSymphony.coders import AllInOneCoder import numpy as np import matplotlib.pyplot as plt if __name__ == '__main__': coder = AllInOneCoder() song = Song('masterpiece/0035.mid') song = coder.encode(song.midi) data, filelist = Song.load_from_dir( "./datasets/easymusicnotes/", encoder=AllInOneCoder(return_indices=True), return_list=True) # LCS check song = filter(lambda x: x < 128, song.argmax(1)) print len(song) matches = [] for ind, ele in tqdm(enumerate(data)): ele = filter(lambda x: x < 128, ele) matches.append((LCS(ele, song, return_seq=True), filelist[ind])) matches = sorted(matches, key=lambda x: x[0][0])[::-1] # pprint(sorted(matches)[::-1]) for (count, lcs, p, q), filename in matches: print count