コード例 #1
0
from midiwrapper import Song
from keras.models import load_model
import numpy as np

if __name__ == '__main__':
    m = load_model('./temp/gan2.h5')
    m.summary()
    bs, seq_len, note_dim, _ = m.output_shape

    code_dim = m.input_shape[-1]

    def code_generator(bs):
        Z = np.random.uniform(-1., 1., size=(bs, code_dim))
        return Z

    mid = Song()
    track = mid.add_track()
    notes = m.predict(code_generator(1))[0, :, :, 0]

    mid._compose(track, notes, deltat=100, threshold=1.)
    mid.save_as('gan2.mid')
コード例 #2
0
    THRESHOLD = 0.85

    hots = Song(FILE).\
        encode_onehot(
                    {'filter_f': lambda x: x.type in ['note_on', 'note_off'],
                     'unit': 'beat'},
                    {'resolution': 0.25})
    print hots.shape
    model = load_model('temp/memorize.h5')

    mid = Song()
    track = mid.add_track()

    seq = [hots[i] for i in range(20)]
#   seq = [np.random.binomial(1, 0.3, (dim,)) for _ in range(LEN)]
    notes = []  # deepcopy(seq)
    accumulate = np.zeros((dim,)).astype('int')
    for _ in range(SONG_LEN):
        note = model.predict(np.array([seq]))[0]
        seq.pop(0)

        print ''.join([('x' if char >= THRESHOLD else '_') for char in note])
        notes.append(note)

        # use output as input
        # note = note * np.random.uniform(0.75, 1.2, size=(dim, ))
        seq.append(note)

    mid._compose(track, np.array(notes), deltat=100, threshold=THRESHOLD)
    mid.save_as('memorize.mid')