示例#1
0
def loadPieces(dirpath):

    pieces = {}

#    for fname in os.listdir(dirpath):
#        if fname[-4:] not in ('.mid','.MID'):
#            continue

#        name = fname[:-4]

#        outMatrix = midiToNoteStateMatrix(os.path.join(dirpath, fname))
        
#        if len(outMatrix) < batch_len:
#            continue

#        pieces[name] = outMatrix
#        print "Loaded {}".format(name)
#        sys.stdout.flush()

#    return pieces

    tmp = readxml.createStateMatrices('../musicxml', batch_len)
    for key in tmp:
      pieces[key] = tmp[key][1]
    return pieces
示例#2
0
def loadPieces(dirpath):

    pieces = {}

    #    for fname in os.listdir(dirpath):
    #        if fname[-4:] not in ('.mid','.MID'):
    #            continue

    #        name = fname[:-4]

    #        outMatrix = midiToNoteStateMatrix(os.path.join(dirpath, fname))

    #        if len(outMatrix) < batch_len:
    #            continue

    #        pieces[name] = outMatrix
    #        print "Loaded {}".format(name)
    #        sys.stdout.flush()

    #    return pieces

    tmp = readxml.createStateMatrices('../musicxml', batch_len)
    for key in tmp:
        pieces[key] = tmp[key][1]
    return pieces
def train_statematrix_net(net,
                          batch_size=128,
                          dropout=.5,
                          output_rate=100,
                          output_length=128,
                          total_epochs=5000,
                          path='net_output/'):
    """
    Trains a neural network, taking time steps from state matrices as input
    and output.
    """
    statematrices = readxml.createStateMatrices()
    batches = []
    for song in statematrices.values():
        matrix = song[1]
        while len(matrix) > 0:
            batch = []
            for i in range(batch_size):
                if len(matrix) == 0:
                    break
                batch.append([[n * 20 - 19 for l in matrix.pop() for n in l]])
            batches.append(zip(batch[:-1], batch[1:]))

    if not os.path.exists(path):
        os.makedirs(path)

    print('\nTraining network:')
    for i in range(0, total_epochs, output_rate):
        last = [[-1] * 156]
        statematrix = []
        for j in range(output_length):
            new = net.run(last)
            statematrix.append(
                estimate_statematrix_from_output(new[0], last[0]))
            last = new
        midi_to_statematrix.noteStateMatrixToMidi(
            statematrix, name=(path + 'example{0}'.format(i)))
        print('\tfile example{0}.mid created'.format(i))
        net.save(path + 'weights{0}'.format(i))
        print('\tweights saved in weights{0}'.format(i))

        for j in range(output_rate):
            print('\t\t' + str(i + j))
            for batch in batches:
                net.train(batch, 1, .1, dropout, .5)
                net.reset()
示例#4
0
def train_statematrix_net(net, batch_size=128, dropout=.5, output_rate = 100,
                          output_length=128, total_epochs=5000,
                          path = 'net_output/'):
    """
    Trains a neural network, taking time steps from state matrices as input
    and output.
    """
    statematrices = readxml.createStateMatrices()
    batches = []
    for song in statematrices.values():
        matrix = song[1]
        while len(matrix) > 0:
            batch = []
            for i in xrange(batch_size):
                if len(matrix) == 0:
                    break
                batch.append([[n * 20 - 19 for l in matrix.pop() for n in l]])
            batches.append(zip(batch[:-1], batch[1:]))

    if not os.path.exists(path):
            os.makedirs(path)

    print('\nTraining network:')
    for i in xrange(0, total_epochs, output_rate):
        last = [[-1] * 156]
        statematrix = []
        for j in xrange(output_length):
            new = net.run(last)
            statematrix.append(estimate_statematrix_from_output(new[0], last[0]))
            last = new
        midi_to_statematrix.noteStateMatrixToMidi(statematrix,
                                    name=(path + 'example{0}'.format(i)))
        print('\tfile example{0}.mid created'.format(i))
        net.save(path + 'weights{0}'.format(i))
        print('\tweights saved in weights{0}'.format(i))
        
        for j in xrange(output_rate):
            print('\t\t' + str(i + j))
            for batch in batches:
                net.train(batch, 1, .1, dropout, .5)
                net.reset()