当前位置:   article > 正文

RNN生成音乐_自动生成音乐命令示例

自动生成音乐命令示例

计算图模型


1.    下载midi文件

 下载midi格式音乐的网站:freemidi.org


2.创建NoteSequences

即创建旋律数据库,将MIDI集合转化为NoteSequences。(NoteSequences是协议缓冲区,它是一种快速有效的数据格式,并且比MIDI文件更易于使用)

  1. INPUT_DIRECTORY=/Users/mac/Desktop/MIDI
  2. SEQUENCES_TFRECORD=/Users/mac/Desktop/train/notesequences.tfrecord
  3. convert_dir_to_note_sequences\
  4. --input_dir=$INPUT_DIRECTORY \
  5. --output_file=$SEQUENCES_TFRECORD \
  6. --recursive

注意:路径中不能有空格

显示如下图则操作成功:


  1. (magenta)zhu-Macs-MacBook-Pro:magenta zhujianing$INPUT_DIRECTORY=/Users/mac/Desktop/MIDI
  2. (magenta)zhu-Macs-MacBook-Pro:magenta zhujianing$ # TFRecord file that will containNoteSequence protocol buffers.
  3. (magenta)zhu-Macs-MacBook-Pro:magenta zhujianing$SEQUENCES_TFRECORD=/Users/mac/Desktop/train/notesequences.tfrecord
  4. (magenta)zhu-Macs-MacBook-Pro:magenta zhujianing$
  5. (magenta)zhu-Macs-MacBook-Pro:magenta zhujianing$ convert_dir_to_note_sequences \
  6. > --input_dir=$INPUT_DIRECTORY \
  7. > --output_file=$SEQUENCES_TFRECORD \
  8. > --recursive
  9. INFO:tensorflow:Convertingfiles in '/Users/mac/Desktop/MIDI/'.
  10. /Users/mac/miniconda2/envs/magenta/lib/python2.7/site-packages/pretty_midi/pretty_midi.py:100:RuntimeWarning: Tempo, Key or Time signature change events found on non-zerotracks. This is not a valid type 0 ortype 1 MIDI file. Tempo, Key or TimeSignature may be wrong.
  11. RuntimeWarning)
  12. INFO:tensorflow:Converted70 files in '/Users/mac/Desktop/MIDI/'.
  13. INFO:tensorflow:Could not parse 0 files.
  14. INFO:tensorflow:Wrote70 NoteSequence protos to '/Users/mac/Desktop/train/notesequences.tfrecord'
  15. (magenta)zhu-Macs-MacBook-Pro:magenta zhujianing$

3.创建SequenceExamples

SequenceExamples被提供给模型用来训练和评估。每个SequenceExample包含一个输入序列和代表一个旋律的一系列标签。下面的命令是从NoteSequences中提取旋律,并将它们保存为SequenceExamples将生成两个SequenceExamples集合,一个用于训练,另一个用于评估,其中评估集中SequenceExamples的分数由-eval_ratio确定。评估率为0.10,提取的旋律的10%将保存在评估系统中,90%将被保存在训练集中。

 

代码:

  1. melody_rnn_create_dataset\
  2. --config=lookback_rnn\
  3. --input=/Users/mac/Desktop/train/notesequences.tfrecord\
  4. --output_dir=/Users/mac/Desktop/train/melody_rnn/sequence_examples\
  5. --eval_ratio=0.10

代码运行后生成的文件


显示如下图则操作成功:

  1. (magenta)zhu-Macs-MacBook-Pro:magenta zhujianing$ melody_rnn_create_dataset \
  2. >--config=lookback_rnn \
  3. >--input=/Users/mac/Desktop/train/notesequences.tfrecord \
  4. >--output_dir=/Users/mac/Desktop/train/melody_rnn/sequence_examples \
  5. >--eval_ratio=0.10
  6. INFO:tensorflow:
  7. Completed.
  8. INFO:tensorflow:Processed70 inputs total. Produced 264 outputs.
  9. INFO:tensorflow:DAGPipeline_MelodyExtractor_eval_melodies_discarded_too_few_pitches:4
  10. INFO:tensorflow:DAGPipeline_MelodyExtractor_eval_melodies_discarded_too_long:0
  11. INFO:tensorflow:DAGPipeline_MelodyExtractor_eval_melodies_discarded_too_short:40
  12. INFO:tensorflow:DAGPipeline_MelodyExtractor_eval_melodies_truncated:2
  13. INFO:tensorflow:DAGPipeline_MelodyExtractor_eval_melody_lengths_in_bars:
  14. [7,8): 1
  15. [8,10): 1
  16. [10,20): 6
  17. [20,30): 3
  18. [30,40): 1
  19. INFO:tensorflow:DAGPipeline_MelodyExtractor_eval_polyphonic_tracks_discarded:34
  20. INFO:tensorflow:DAGPipeline_MelodyExtractor_training_melodies_discarded_too_few_pitches:87
  21. INFO:tensorflow:DAGPipeline_MelodyExtractor_training_melodies_discarded_too_long:0
  22. INFO:tensorflow:DAGPipeline_MelodyExtractor_training_melodies_discarded_too_short:1001
  23. INFO:tensorflow:DAGPipeline_MelodyExtractor_training_melodies_truncated:54
  24. INFO:tensorflow:DAGPipeline_MelodyExtractor_training_melody_lengths_in_bars:
  25. [7,8): 38
  26. [8,10): 28
  27. [10,20): 92
  28. [20,30): 39
  29. [30,40): 52
  30. [40,50): 3
  31. INFO:tensorflow:DAGPipeline_MelodyExtractor_training_polyphonic_tracks_discarded:744
  32. INFO:tensorflow:DAGPipeline_RandomPartition_eval_melodies_count:4
  33. INFO:tensorflow:DAGPipeline_RandomPartition_training_melodies_count:66
  34. (magenta)zhu-Macs-MacBook-Pro:magenta zhujianing$

4.训练并测试模型

  1. melody_rnn_train\
  2. --config=lookback_rnn\
  3. --run_dir=/Users/mac/Desktop/train/melody_rnn/logdir/run1\
  4. --sequence_example_file=/Users/mac/Desktop/train/melody_rnn/sequence_examples/training_melodies.tfrecord\
  5. --hparams="{'batch_size':64,'rnn_layer_sizes':[64,64]}"\
  6. --num_training_steps=20000

5. 根据开头给定的旋律给出midi格式音乐

  1. melody_rnn_generate\
  2. --config=lookback_rnn\
  3. --run_dir=/Users/mac/Desktop/train/melody_rnn/logdir/run1\
  4. --output_dir=/Users/mac/Desktop/train/melody_rnn/generated\
  5. --num_outputs=10\
  6. --num_steps=512\
  7. --hparams="{'batch_size':64,'rnn_layer_sizes':[64,64]}"\
  8. --primer_midi=/Users/mac/Desktop/MIDI/21Guns.mid

播放midi文件方式

  1. $ brew install timidity
  2. $timidity /Users/mac/Desktop/MIDI/21Guns\(1\).mid

结果如下图就可以成功播放mid文件了。



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Li_阴宅/article/detail/819064
推荐阅读
相关标签
  

闽ICP备14008679号