Learn MIDI files and generate songs with the TensorFlow music generation project "Magenta".

Introduction

In this article, we will prepare the development environment for Magenta, learn the MIDI file at hand, and create a song. It is as per README of basic RNN of magenta, so you may read it.

Please note that there are some unclear points such as terms. Please refer to this article for the preparation of the development environment.

Try TensorFlow music generation project "Magenta" from development environment setting to song generation.

Create a dataset

Before learning MIDI files on hand, you first need to convert them to a format called Note Sequences so that they can be handled by TensorFlow.

Execute the script below. It may throw an error when reading a malformed MIDI file. MIDI files that cannot be parsed will be skipped, so you can rest assured.


MIDI_DIRECTORY=<folder containing MIDI files. can have child folders.>

# TFRecord file that will contain NoteSequence protocol buffers.
SEQUENCES_TFRECORD=/tmp/notesequences.tfrecord

bazel run //magenta/scripts:convert_midi_dir_to_note_sequences -- \
--midi_dir=$MIDI_DIRECTORY \
--output_file=$SEQUENCES_TFRECORD \
--recursive


```

 It looks like you're running `convert_midi_dir_to_note_sequences.py`.

 If the following message is displayed, the process is complete.

```
..............
INFO: Found 1 target...
Target //magenta/scripts:convert_midi_dir_to_note_sequences up-to-date:
  bazel-bin/magenta/scripts/convert_midi_dir_to_note_sequences
INFO: Elapsed time: 7.312s, Critical Path: 0.63s

(Omission)

INFO:tensorflow:Could not parse 0 MIDI files.
INFO:tensorflow:Wrote 6 NoteSequence protos to '/tmp/notesequences.tfrecord'
```


# learn

## Extract the melody
 Execute `basic_rnn_create_dataset.py`.

 This is a script that extracts the melody from the `Note Sequence data` created above based on the MIDI file.
 The output is exported as a `tfrecord` file containing the` SequenceExampleprotos`.

 Loading a TensorFlow of a `basic_rnn` model allows you to load the` SequenceExample protos` directly into the model.

 In this example, we will create the evaluation dataset as a second `tfrecord` file.
 However, it can be omitted by removing the ʻeval output` and ʻeval ratio` flags.

 It is OK if the following message appears.


```
INFO: Found 1 target...
Target //magenta/models/basic_rnn:basic_rnn_create_dataset up-to-date:
  bazel-bin/magenta/models/basic_rnn/basic_rnn_create_dataset
INFO: Elapsed time: 0.573s, Critical Path: 0.01s

INFO: Running command line: bazel-bin/magenta/models/basic_rnn/basic_rnn_create_dataset '--input=/tmp/notesequences.tfrecord' '--output_dir=/tmp/basic_rnn/sequence_examples' '--eval_ratio=0.10'
INFO:tensorflow:

Completed.

INFO:tensorflow:Processed 6 inputs total. Produced 31 outputs.
INFO:tensorflow:DAGPipeline_RandomPartition_training_melodies_count: 29
INFO:tensorflow:DAGPipeline_MonophonicMelodyExtractor_melodies_discarded_too_long: 0
INFO:tensorflow:DAGPipeline_RandomPartition_eval_melodies_count: 2
INFO:tensorflow:DAGPipeline_MonophonicMelodyExtractor_melody_lengths_in_bars:
  [0,1): 10
  [1,3): 6
  [3,6): 17
  [6,7): 2
  [7,8): 1
  [8,10): 4
  [10,20): 21
  [20,30): 5
  [30,40): 6
  [50,100): 4
INFO:tensorflow:DAGPipeline_MonophonicMelodyExtractor_melodies_discarded_too_short: 35
INFO:tensorflow:DAGPipeline_Quantizer_sequences_discarded_because_multiple_time_signatures: 5
INFO:tensorflow:DAGPipeline_MonophonicMelodyExtractor_melodies_truncated: 0
INFO:tensorflow:DAGPipeline_MonophonicMelodyExtractor_polyphonic_tracks_discarded: 43
INFO:tensorflow:DAGPipeline_MonophonicMelodyExtractor_melodies_discarded_too_few_pitches: 10
```

 When completed
 In `/ tmp / basic_rnn / sequence_examples /`
 You will have two files, `training_melodies.tfrecord` and ʻeval_melodies.tfrecord`.
 It seems that `training_melodies.tfrecord` is for training and ʻeval_melodies.tfrecord` is for evaluation.





## Run the training script

 The Magenta developer has created the script for us.


```
$EXPERIMENT_DIR=/tmp/basic_rnn/logdir
```

 Specify the working folder.


```
$HYPERPARAMETER_STRING=`{"rnn_layer_sizes":[50]}’
```

 Specify hyperparameters.
 It seems that you can specify the size of the LSTM model by specifying it like this.
 For LSTM, the following will be helpful (masterpiece).

 [Understanding LSTM-with recent trends](http://qiita.com/t_Signull/items/21b82be280b46f467d1b)

 > LSTM (Long short-term memory) is a type of model or architecture for time series data (sequential data) that appeared in 1995 as an extension of RNN (Recurrent Neural Network).

 > (Omitted)

 > The biggest feature of LSTM is that it can learn long-term dependencies that cannot be learned by conventional RNNs.

```
$NUM_TRAINING_STEPS=20000
```

 Specify the number of trainings. It takes a lot of time on a Mac, so I think it's a good idea to reduce it to a few hundred.


```
$TRAIN_DATA=/tmp/basic_rnn/sequence_examples/training_melodies.tfrecord
```

 The path of the training data generated above is described.

```
$EVAL_DATA=/tmp/basic_rnn/sequence_examples/eval_melodies.tfrecord
```

 As an option, it seems that validation data can be displayed in parallel with training data.

 After setting, execute the script.

```
./run_basic_rnn_train.sh $EXPERIMENT_DIR $HYPERPARAMETER_STRING $NUM_TRAINING_STEPS $TRAIN_DATA [$EVAL_DATA]
```


 The following message will appear.

INFO: Found 1 target... Target //magenta/models/basic_rnn:basic_rnn_train up-to-date: bazel-bin/magenta/models/basic_rnn/basic_rnn_train INFO: Elapsed time: 0.544s, Critical Path: 0.00s INFO:tensorflow:hparams = {'rnn_layer_sizes': [50], 'decay_rate': 0.85, 'dropout_keep_prob': 0.5, 'batch_size': 128, 'decay_steps': 1000, 'clip_norm': 5, 'initial_learning_rate': 0.01, 'skip_first_n_losses': 0} Starting TensorBoard 28 on port 6006 (You can navigate to http://10.200.3.144:6006) INFO:tensorflow:Train dir: /tmp/melody_rnn/logdir/run1/train INFO:tensorflow:Starting training loop... INFO:tensorflow:global_step/sec: 0

(Omission)

INFO:tensorflow:Global Step: 100 - Learning Rate: 0.01000 - Loss: 1.680 - Perplexity: 13.678 - Accuracy: 85.811 INFO:tensorflow:Training complete.



# Generate a song.

 When the above training is completed, the following files will be created under the working directory `/ tmp / basic_rnn / logdir / run1 / train /`.

checkpoint model.ckpt-31 events.out.tfevents.1475664203.HTMac.local model.ckpt-31.meta graph.pbtxt model.ckpt-39 model.ckpt-15 model.ckpt-39.meta model.ckpt-15.meta model.ckpt-47 model.ckpt-23 model.ckpt-47.meta model.ckpt-23.meta


 The checkpoint seems to be used for saving and loading files in TensorFlow.
 Basic_RNN seems to generate a song using the last checkpoint (model.ckpt-47 in the above case).


 Also, to generate a song, you need to give the model a starting melody.
 This is because song generation is based on the melody, predicting the next sound more and more, and extending the song. The melody to be prepared must be a monaural melody (multiple sounds do not sound at the same timing).

 You can create it arbitrarily, or as a sample
 `/magenta/models/shared/primer.mid` is also available.

 This sample is used here.

PRIMER_PATH=

bazel run //magenta/models/basic_rnn:basic_rnn_generate --
--run_dir=/tmp/basic_rnn/logdir/run1
--hparams='{"rnn_layer_sizes":[50]}'
--output_dir=/tmp/basic_rnn/generated
--num_outputs=10
--num_steps=128
--primer_midi=$PRIMER_PATH


 The minimum 3 bars are fixed based on primer.mid.

 [Sample 1](https://clyp.it/iwkk1xg3)
 [Sample 2](https://clyp.it/jebdi2ny)

 I learned Sakanaction, but I could feel the atmosphere.


# Finally

 Due to my knowledge of Deep Learning, knowledge of TensorFlow, and lack of English reading comprehension, there are some parts that I cannot read.
 I will continue to study. .. ..

 Thank you very much.


Recommended Posts

Learn MIDI files and generate songs with the TensorFlow music generation project "Magenta".
Let's try TensorFlow music generation project "Magenta" from development environment setting to song generation.
Learn Wasserstein GAN with Keras model and TensorFlow optimization
[How to!] Learn and play Super Mario with Tensorflow !!