Symbolic Music Generation with Transformer-GANs
Code for the paper "Symbolic Music Generation with Transformer-GANs" (AAAI 2021)
If you use this code, please cite the paper using the bibtex reference below.
@inproceedings{transformer-gan,
title={Symbolic Music Generation with Transformer-GANs},
author={Aashiq Muhamed and Liang Li and Xingjian Shi and Suri Yaddanapudi and Wayne Chi and Dylan Jackson and Rahul Suresh and Zachary C. Lipton and Alexander J. Smola},
booktitle={35th AAAI Conference on Artificial Intelligence, {AAAI} 2021},
year={2021},
}
- Python 3.6+
- Pytorch
- Transformers
You can install all required Python packages with bash requirements.sh
.
- Downloaded data
bash get_data.sh
- Run
music_encoder.py
to generate the encoded numpy files- Messages stating that pitches are out of range are expected behavior
python3 music_encoder.py --encode_official_maestro \
--mode midi_to_npy \
--pitch_transpose_lower -3 \
--pitch_transpose_upper 3 \
--output_folder ./maestro_magenta_s5_t3
- Train a Transformer XL (No GAN)
python3 -m torch.distributed.launch --nproc_per_node=4 ./train.py \
--data_dir ../data/maestro_magenta_s5_t3 \
--cfg ./training_config/experiment_baseline.yml \
--work_dir exp_dir
- Train a Transformer XL (with GAN)
python3 -m torch.distributed.launch --nproc_per_node=4 ./train.py \
--data_dir ../data/maestro_magenta_s5_t3 \
--cfg ./training_config/experiment_spanbert.yml \
--work_dir exp_dir
- Generate unconditional samples
# generate unconditional samples
python3 generate.py --inference_config inference_config/inference_unconditional.yml
Note, if you are loading an old config.yml file which includes None/" " inside, please change it to a string 'Null' to make sure you can do cfg.merge_from_file.
- Extend music to generate conditional samples
# generate conditional samples
python3 generate.py --inference_config inference_config/inference_conditional.yml
- Please set condition_len as well as condition_file
- Change memlen and genlen. memlen=genlen is recommended
- Run the following to get midi files from txt files
- Use
--mode to_midi
for text file conversions. Use--mode npy_to_midi
for numpy file conversions.
- Use
python3 ../data/music_encoder.py --input_folder ./Output_Uncondtitionl --output_folder ./Output_Uncondtitionl_MIDI --mode to_midi
python3 ../data/music_encoder.py --input_folder ./Output_Condtitionl --output_folder ./Output_Condtitionl_MIDI --mode to_midi
different methods inside music_encoder
- encoder.to_text(input.mid, output.txt)
- encoder.from_text(input.txt, out.mid)
- encoder.encode_vocab(input.mid) return list of ids
- encoder.decoder_vocab(list(ids)) return out.mid
- encoder.to_text_argumentaion(input.mid, output.txt)