input = dataset[0][ind][start:start + hparam.timesteps]
                label = dataset[1][ind][start:start + hparam.timesteps]
                inputs.append(input)
                labels.append(label)
            return np.array(inputs), np.array(labels)

        return fetch_data

    fetch_train_data = fetch_data_g(train_set)
    fetch_test_data = fetch_data_g(test_set)
    fetch_one_data = fetch_data_g(([train_set[0][1]], [train_set[1][1]]))

    if mode == 'train':
        model.train(fetch_one_data,
                    fetch_test_data,
                    continued=hparam.continued)
    if mode == 'refine':
        ind = train_ind[1]
        song = notes[ind]
        dura = durations[ind]
        pred_dura = model.predict([song])[0]
        print pred_dura

        coder.decode(song, 2).write('midi', 'quantized.mid')
        coder.decode(song, dura).write('midi', 'truth.mid')
        coder.decode(song, pred_dura).write('midi', 'refine.mid')
    if mode == 'oneshot':
        song = np.load('temp/oneshot_example.npz')['song']
        pred_dura = model.predict([song], max=8)[0]
        coder.decode(song, pred_dura).write('midi', 'theone.mid')
Exemple #2
0
        # for c in code:
        # print np.histogram(c)

    if mode == 'rec':
        seq = train_data[1].copy()
        batch = continuous_sample(seq,
                                  hparam.timesteps,
                                  stride=hparam.timesteps)
        ori = code = model.encode(batch, quantized=True)
        print code
        rec = model.generate(code, quantized=True).flatten()
        print seq.shape
        print batch.shape
        print rec.shape

        coder.decode(seq, 2).write('midi', 'truth.mid')
        coder.decode(rec, 2).write('midi', 'rec.mid')

    if mode == 'plot':
        seq = train_data[0].copy()
        batch = continuous_sample(seq,
                                  hparam.timesteps,
                                  stride=hparam.timesteps)
        code = model.encode(batch, quantized=True)  # first layer
        print code

        from sklearn.manifold import TSNE
        tsne = TSNE(2)
        code = tsne.fit_transform(code)
        print code.shape
        import matplotlib.pyplot as plt
Exemple #3
0
        model.fit_generator(fetch_data(),
                            steps_per_epoch=20,
                            epochs=10,
                            verbose=1)
        model.save('temp/duration.h5')

    if mode == 'test':
        model = load_model('temp/duration.h5')
        model.summary()

        coder = NoteDurationCoder(resolution=1. / 16.)
        notes, duras = coder.encode(ms.converter.parse('example.mid'),
                                    force=True)
        # notes, duras = coder.encode(
        #     ms.converter.parse('datasets/piano-midi.de/schumann/schum_abegg.mid'),
        #     force=True)

        print notes
        print notes.shape
        duration = model.predict([np.array([notes]),
                                  np.array([duras])])[0].argmax(-1) + 1
        print duration - duras
        print 'old', duras
        print 'new', duration
        print duration[-1]
        print 'sqr diff', ((duras - duration)**2).sum()
        coder.decode(notes, duration).write('midi', 'example.d.mid')
        # print zip(notes, duras)

    print 'done'