Convert Figma logo to code with AI

danieldjohnson logobiaxial-rnn-music-composition

A recurrent neural network designed to generate classical music.

1,916
380
1,916
10

Top Related Projects

A recurrent neural network designed to generate classical music.

19,130

Magenta: Music and Art Generation with Machine Intelligence

Deep learning driven jazz generation using Keras & Theano!

Magenta.js: Music and Art Generation with Machine Learning in the browser

Quick Overview

The biaxial-rnn-music-composition project is a deep learning model for composing polyphonic music. It uses a biaxial recurrent neural network architecture to generate multi-instrumental music pieces. The project is implemented in Python and TensorFlow, and it can be trained on MIDI files to create new compositions in a similar style.

Pros

  • Capable of generating complex, multi-instrumental music
  • Uses an innovative biaxial RNN architecture for improved musical coherence
  • Can be trained on custom MIDI datasets for personalized compositions
  • Provides a web interface for easy interaction and music generation

Cons

  • Requires significant computational resources for training
  • Limited documentation and setup instructions
  • May produce occasional dissonant or musically inconsistent outputs
  • Dependency on older versions of TensorFlow and Python libraries

Code Examples

  1. Loading and preprocessing MIDI data:
import midi_manipulation

def get_songs(path):
    files = glob.glob('{}/*.mid*'.format(path))
    songs = []
    for f in files:
        try:
            song = midi_manipulation.midiToNoteStateMatrix(f)
            if np.array(song).shape[0] > 50:
                songs.append(song)
        except Exception as e:
            print(f"Error processing {f}: {e}")
    return songs
  1. Defining the biaxial RNN model:
def build_model(t_layer_sizes, p_layer_sizes, dropout):
    input_shape = (None, 2*note_range)
    model = Sequential()
    
    # Time axis layers
    for size in t_layer_sizes:
        model.add(LSTM(size, return_sequences=True, input_shape=input_shape))
        model.add(Dropout(dropout))
    
    # Pitch axis layers
    for size in p_layer_sizes:
        model.add(LSTM(size, return_sequences=True))
        model.add(Dropout(dropout))
    
    model.add(Dense(2*note_range, activation='sigmoid'))
    return model
  1. Generating music:
def generate(model, seed, num_steps):
    generated = []
    current_sequence = seed
    
    for _ in range(num_steps):
        prediction = model.predict(current_sequence)
        generated.append(prediction[-1])
        current_sequence = np.concatenate((current_sequence[1:], prediction[-1:]))
    
    return np.array(generated)

Getting Started

  1. Clone the repository:

    git clone https://github.com/danieldjohnson/biaxial-rnn-music-composition.git
    cd biaxial-rnn-music-composition
    
  2. Install dependencies:

    pip install -r requirements.txt
    
  3. Prepare your MIDI dataset in a folder named midi

  4. Train the model:

    python train.py
    
  5. Generate music using the web interface:

    python server.py
    
  6. Open a web browser and navigate to http://localhost:5000 to interact with the model and generate music.

Competitor Comparisons

A recurrent neural network designed to generate classical music.

Pros of biaxial-rnn-music-composition

  • Innovative approach to music composition using biaxial RNNs
  • Well-documented codebase with clear explanations
  • Includes pre-trained models for quick experimentation

Cons of biaxial-rnn-music-composition

  • Limited updates and maintenance in recent years
  • May require significant computational resources for training

Code Comparison

Both repositories contain the same codebase, as they are identical. Here's a sample from the main model file:

class BiaxialRNNModel():
    def __init__(self, t_layer_sizes, p_layer_sizes, dropout):
        self.t_layer_sizes = t_layer_sizes
        self.p_layer_sizes = p_layer_sizes

    def _build_model(self, time_steps):
        # Model building code...

Since the repositories are identical, there are no differences in the code to compare. The project focuses on using biaxial recurrent neural networks for music composition, implementing a unique approach to generating polyphonic music.

19,130

Magenta: Music and Art Generation with Machine Intelligence

Pros of Magenta

  • Broader scope: Magenta covers various creative AI applications, including music, art, and more
  • Extensive documentation and tutorials, making it more accessible for beginners
  • Backed by Google, ensuring regular updates and a large community

Cons of Magenta

  • Steeper learning curve due to its comprehensive nature
  • May be overkill for users focused solely on music composition
  • Requires more computational resources for some advanced models

Code Comparison

Biaxial-rnn-music-composition:

def getPieceSegment(pieces):
    piece = random.choice(pieces)
    start = random.randrange(0, len(piece) - maxBatchLength, division_len)
    seg = piece[start:start+maxBatchLength]
    return seg

Magenta:

def generate_midi(primer_midi, total_time, temperature=1.0):
    primer_sequence = midi_io.midi_file_to_sequence_proto(primer_midi)
    generated_sequence = model.generate_sequence(
        len(primer_sequence), total_time, temperature)
    midi_io.sequence_proto_to_midi_file(generated_sequence, 'output.mid')

The code snippets show that Biaxial-rnn-music-composition focuses on selecting random segments from existing pieces, while Magenta provides a more comprehensive approach to generating new MIDI sequences based on a primer and model parameters.

Deep learning driven jazz generation using Keras & Theano!

Pros of deepjazz

  • Focuses specifically on jazz music generation, potentially leading to more genre-specific results
  • Utilizes a simpler LSTM-based architecture, which may be easier to understand and implement
  • Includes a web interface for easy interaction and demonstration

Cons of deepjazz

  • Limited to generating monophonic melodies, lacking the complexity of full musical compositions
  • Relies on a smaller dataset, which may result in less diverse outputs
  • Has not been updated recently, potentially missing out on newer deep learning advancements

Code Comparison

biaxial-rnn-music-composition:

def __init__(self, t_layer_sizes, p_layer_sizes, dropout):
    self.t_layer_sizes = t_layer_sizes
    self.p_layer_sizes = p_layer_sizes

deepjazz:

def __init__(self, corpus, values_indices, indices_values, maxlen, step):
    self.corpus = corpus
    self.values_indices = values_indices
    self.indices_values = indices_values

The biaxial-rnn-music-composition initializes with layer sizes and dropout, suggesting a more complex network structure. In contrast, deepjazz initializes with corpus and mapping dictionaries, indicating a focus on data preprocessing and representation.

Magenta.js: Music and Art Generation with Machine Learning in the browser

Pros of magenta-js

  • Comprehensive library with multiple music generation models
  • Active development and maintenance by Google's Magenta team
  • Extensive documentation and examples for easy integration

Cons of magenta-js

  • Larger codebase and potentially higher learning curve
  • May include unnecessary features for simple music composition tasks
  • Requires TensorFlow.js, which can increase project complexity

Code Comparison

biaxial-rnn-music-composition:

def predict_and_sample(state):
    prediction = model.predict(state)
    sample = np.zeros(len(prediction))
    sample[np.random.choice(len(prediction), p=prediction)] = 1
    return sample

magenta-js:

async function generateMelody() {
  const model = new mm.MusicRNN('https://storage.googleapis.com/magentadata/js/checkpoints/music_rnn/melody_rnn');
  await model.initialize();
  const seed = {notes: [{pitch: 60, startTime: 0.0, endTime: 0.5}]};
  const result = await model.continueSequence(seed, 20, 1.5);
  return result;
}

The biaxial-rnn-music-composition code focuses on a single prediction and sampling step, while magenta-js provides a higher-level API for generating complete melodies using pre-trained models. magenta-js offers more abstraction and ease of use for complex music generation tasks, but may be overkill for simpler projects where biaxial-rnn-music-composition's approach could suffice.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Biaxial Recurrent Neural Network for Music Composition

This code implements a recurrent neural network trained to generate classical music. The model, which uses LSTM layers and draws inspiration from convolutional neural networks, learns to predict which notes will be played at each time step of a musical piece.

You can read about its design and hear examples on this blog post.

Requirements

This code is written in Python, and depends on having Theano and theano-lstm (which can be installed with pip) installed. The bare minimum you should need to do to get everything running, assuming you have Python, is

sudo pip install --upgrade theano
sudo pip install numpy scipy theano-lstm python-midi

In addition, the included setup scripts should set up the environment exactly as it was when I trained the network on an Amazon EC2 g2.2xlarge instance with an external EBS volume. Installing it with other setups will likely be slightly different.

Using it

First, you will need to obtain a large selection of midi music, preferably in 4/4 time, with notes correctly aligned to beats. These can be placed in a directory "music".

To use the model, you need to first create an instance of the Model class:

import model
m = model.Model([300,300],[100,50], dropout=0.5)

where the numbers are the sizes of the hidden layers in the two parts of the network architecture. This will take a while, as this is where Theano will compile its optimized functions.

Next, you need to load in the data:

import multi_training
pcs = multi_training.loadPieces("music")

Then, after creating an "output" directory for trained samples, you can start training:

multi_training.trainPiece(m, pcs, 10000)

This will train using 10000 batches of 10 eight-measure segments at a time, and output a sampled output and the learned parameters every 500 iterations.

Finally, you can generate a full composition after training is complete. The function gen_adaptive in main.py will generate a piece and also prevent long empty gaps by increasing note probabilities if the network stops playing for too long.

gen_adaptive(m,pcs,10,name="composition")

There are also mechanisms to observe the hidden activations and memory cells of the network, but these are still a work in progress at the moment.

Right now, there is no separate validation step, because my initial goal was to produce interesting music, not to assess the accuracy of this method. It does, however, print out the cost on the training set after every 100 iterations during training.

If you want to save your model weights, you can do

pickle.dump( m.learned_config, open( "path_to_weight_file.p", "wb" ) )

and if you want to load them, you can do

m.learned_config = pickle.load(open( "path_to_weight_file.p", "rb" ) )