/BachGPT

Primary LanguageJupyter NotebookMIT LicenseMIT

BachGPT

DALL·E 2023-12-03 23 39 02 - An artistic representation of Johann Sebastian Bach as 'BachGPT', an AI musician  He is wearing stylish sunglasses, symbolizing the blend of classical

Background

The BachGPT aims to explore the intersection of artificial intelligence and classical music composition. This project utilizes LSTM (Long Short-Term Memory) + attention neural networks, which are particularly suited for sequential data like music. The focus is on generating compositions in the style of Johann Sebastian Bach, a renowned composer known for his complex harmonies and counterpoint techniques.

This endeavor follows in the footsteps of previous works in AI-driven music generation, aiming to capture the essence of Bach's style while exploring the creative capacities of machine learning models in arts.

Dataset

For this project, the dataset consists of MIDI files of various compositions by Bach. These files were processed to extract patterns and sequences characteristic of Bach's musical style. The pipeline involves converting MIDI files into a format suitable for training the LSTM model, ensuring the network learns to mimic the intricacies and nuances of Bach's compositions.

I have 4 sets of dataset

  1. Raw MIDI music
  2. Contrapunctus XIV
  3. JSB Chorales Dataset - A dataset with 4 voice midi files from bach
  4. JS Fake Chorales - A similar dataset to JSB Chorales, but fake, generated by AI and annotated by humans

I have used all 4 of these data sources, but when I finally trained the model with dataset 2. Finally, 3 pieces of music were output. The first and third pieces of music came from dataset 2, and the second piece of music came from dataset 3.

Train methods

LSTM with Attention Our approach employs a Long Short-Term Memory (LSTM) network enhanced with an attention mechanism. LSTM networks are adept at handling sequences with long-range dependencies, making them ideal for music generation tasks.

  • LSTM Overview: LSTMs are capable of learning order dependence in sequence prediction problems.
  • Attention Mechanism: This feature allows the network to focus on specific parts of the input sequence, enhancing the quality and coherence of the generated music.

Evaluations

The model's performance was evaluated based on its ability to authentically replicate Bach's musical style. We conducted several experiments to optimize the model's architecture and parameters.

  • Experiments and Results: The results of these experiments were quantified and are presented in tables and figures, demonstrating the model's accuracy in generating music that aligns with Bach's style.
  • Comparison with Baselines: The model's performance was also compared against established baselines to ascertain its efficacy and uniqueness.

How to run

I highly recommend running the notebook in Colaboratory so you can view the results and change parameters in real-time. The jupyter notebook code is here. However, you can run Python code on your local machine.

1. Train model

python3 bachGPT.py 

2. Use the model to generate predicted output

python3 bach_predict.py ./models/{your_model_name}.pth 

3. Create midi music file

python3 create_midi.py {your_model_name}.pth/output.txt

4. Generate mp3 file

timidity output/output.midi -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 320k output/output.mp3
timidity output/complete.midi -Ow -o - | ffmpeg -i - -acodec libmp3lame -ab 320k output/complete.mp3

The final generated Bach-style music is in the output folder.

Generated music

image

1.mov

image

2.mov

image

3.mov

Reference

  1. Generating Original Classical Music with an LSTM Neural Network and Attention
  2. Generating Long-Term Structure in Songs and Stories
  3. Neural Machine Translation by Jointly Learning to Align and Translate
  4. Baroque Music Generator
  5. Generating Music using LSTM
  6. Music Generation using Deep Learning
  7. How to Generate Music using a LSTM Neural Network in Keras
  8. Classical-Piano-Composer
  9. Classic_Music_Generation
  10. conversion-tool