A few days ago to see an interesting share, the main idea is how to use TensorFlow teach neural network automatically create music. It sounds so fun, there's wood! As a Coldplay, the first idea was to automatically generate a music like the Coldplay genre, so I started to follow the tutorial on GitHub (project name: Projects Magenta) Step by step, get three days, The final generated music is here (if someone can tell me how to insert music in the blog please contact me soon!) Thank you! )
- First song: Magenta Melody Result1.mp3
http://yun.baidu.com/share/link?shareid=1799925478&uk=840708891
- Second song: Magenta Melody Result2.mp3
http://yun.baidu.com/share/link?shareid=3718079494&uk=840708891
These two pieces of music I generated more than 10 music sounds good, although still a bit strange, but at least rhythm, hey. Here's how it's done:
1. First download Project Magenta
1 git clone https://github.com/tensorflow/magenta.git
2. Install the required tools:
Install Python, Bazel, and TensorFlow here (https://www.tensorflow.org/versions/r0.9/get_started/os_setup.html)
Note : I have been in the installation of Bazel "Segmentation fault:11" error, Google a lot of solutions found to be a GCC installation version of the problem, if you are a Mac user, downloaded Xcode, Does not mean that you have GCC installed, you must also install command line tools, if the installation is successful, enter "GCC--version" in Linux will appear the corresponding version information, if not, it indicates that the installation failed. If the installation failed, with the download good Bazel and then enter "Bazel install gcc", download the test gcc-v, if it is still "segmentation fault:11" error, congratulations to you, met me the same mistake, Google found this after a half-day:
So,apple now does not have GCC, instead of LLVM. In the future, "clang, clang++" will replace GCC. If you enter Gcc-v, display "Segmentation fault:11", you might as well enter "Clang-v" to see if there is any corresponding version information. If there is a download successful on your behalf. But there is no end, there is one final step, to change the link to GCC to clang. Enter "which GCC" and "which clang" to see where your gcc and clang are located, and then change the soft link:
1 cd/usr/local/bin2sudo mv gcc gcc_old3 sudo ln-s/usr/bin/clang/usr/ local/bin/gcc4 gcc-v5 Apple LLVM version 6.1.0 (clang-602.0.53) (based on LLVM 3.6
.0SVN)
6 target:x86_64-apple-darwin14.5.0
7 Thread model:posix
That's it! Ha ha! Don't ask me why I spend so much time writing this seemingly insignificant thing, because I was tortured by this thing for two days!! It took three days to finish this and it took two days to get this one!! Oh, by the way, if you see here don't know what Bazel is doing, simply say is a compiler tool, the equivalent of Pip Intsall.
Now use Bazel to test if you can run smoothly:
1 bazel test//magenta:all
Note: If all tests are successful, good. If this error occurs:
1Info:found 5 Targets and6Test Targets ...2 info:elapsed time:0.427s, Critical path:0.00s3Magenta:basic_one_hot_encoder_test (cached) PASSEDinch3. 7s4Magenta:convert_midi_dir_to_note_sequences_test (cached) PASSEDinch2. 3s5Magenta:melodies_lib_test (cached) PASSEDinch3. 5s6Magenta:midi_io_test (cached) PASSEDinch5. 5s7Magenta:note_sequence_io_test (cached) PASSEDinch3. 5s8Magenta:sequence_to_melodies_test (cached) PASSEDinch40. 2s9 TenExecuted 0 out of 6 tests:6 testsPass.
OneThere were tests whose specified size is too big. Use the--test_verbose_timeout_warnings command line option to see which ones these is.
Congratulations, and I made the same mistake: the error is that the test file is too large to test all of a sudden (my 16g memory is not enough = =), so you can do the same as Me manual test, with one example:
1 >>>bazel-bin/magenta/basic_one_hot_encoder_test2 >>>------------- ---------------------------------------------------------3 in 0.074s45 OK
Test the above six files in turn and see the next step for success.
3. Create your Melody Data set
and machine learning, we have to enter certain data to let it training, the training data here can download their favorite music, but magenta can not directly read the MP3 file, only read midi files (mp3 too big, A 10M or so mp3 format music can be converted to about 100k MIDI files). Of course, there are many ways to convert to MIDI format, and I've collected a super-usable URL that can be turned online: convert Tool
After reading the MIDI file, magenta to convert the MIDI file into a sequence file for training
# #创建旋律数据库 # Here's your file path, please. sequences_tfrecord=/tmp///magenta:convert_midi_dir_to_note_sequences----midi_dir=-- output_file=--recursive
And then extract the melody from these sequence sequences:
1 ##从Sequences中提取旋律2sequences_tfrecord=/tmp/Notesequences.tfrecord3Train_data=/tmp/training_melodies.tfrecord#the generated training file address4eval_data=/tmp/Evaluation_melodies.tfrecord5eval_ratio=0.10 encoder=Basic_one_hot_encoder6Bazel Run//magenta/models:basic_rnn_create_dataset-- 7--input=$SEQUENCES _tfrecord8--train_output=$TRAIN _data9--eval_output=$EVAL _dataTen--eval_ratio=$EVAL _ratio One--encoder= $ENCODER
OK, here our data processing is finished, the training file generated in "/tmp/training_melodies.tfrecord"
4. Training Neural Network model
After the training data is generated, the model can be trained, using the RNN model:
1 # #训练神经网络模型 2 # first compile BASIC_RNN tools 3 Bazel build//magenta/models:basic_rnn_train45# training model, where "rnn_ Layer_size "is the number of layers of a neural network and can be customized 6 ./bazel-bin/magenta/models/basic_rnn_train--experiment_run_dir=/tmp /basic_rnn/run1--sequence_example_file= $TRAIN _data--eval=false--hparams='{"rnn_layer_sizes": [ ]}' --num_training_steps=2000
5. Generate the melody of the test
The model is very, very time-consuming, with 20,000 iterations set up on GitHub, almost running my computer on fire = =, you can set the number of iterations based on the actual hardware. Test melody and Training melody, are MIDI files, I choose here is Katy Perry Peacock (small yellow song = =, want to see the training data with Coldplay on the Katy Perry test results are what)
1 ##生成旋律2 #Specifies the file address of the test melody3primer_path=/users/shelter/magenta/magenta/music/coldplay/katyperrypeacock.mid #注意这里是绝对地址, only one song can be specified 4Bazel Run//magenta/models:basic_rnn_generate-- 5--experiment_run_dir=/tmp/basic_rnn/run16--hparams='{"rnn_layer_sizes": [[]}' 7--primer_midi=$PRIMER _path8--output_dir=/tmp/Basic_rnn_39--num_steps=64 Ten--num_outputs=16
You can use "Bazel test//magenta:all" To view the results, see the visual results in http://localhost:6006, including the convergence process, accuracy and so on.
The last melody generated is the beginning of the Baidu cloud file. There is another one is tested with light music, the effect is also good.
Summarize:
1. At the beginning of my training is 20,000 times, to 1000 times when the algorithm divergence, the loss value from 20 tens of thousands of to more than 2000 then suddenly rose to 16000 or so, accuracy also dropped, so they withdrew, the number of iterations into 2000 training. Training at the end of the algorithm has not been convergent, but I want to see the results quickly, and the computer ran too slow, it is directly used. If you have a GPU or are willing to wait a few days to run the program, you can set the number of iterations larger, and so the algorithm convergence before testing. model training is good or bad directly determines the final sound of the music, so the best algorithm convergence after the test . Many of the world's documents I've tested are like strum.
2. Shortly after the start of the project, there is a forum dedicated to exchanging learning experiences and questions, point here. The above comment is the problem I encountered, if you encounter a new problem, you can post to the forum for help. I see some people generate music that has that weird Gothic-style haha.
3. The specific principles behind this project I did not write, on GitHub is very clear, can refer to here
4. After the creation of music can be based on their own needs to add beats, should be a little better ~
Anyway, now start doing it, it's fun! ~
Use TensorFlow to let neural networks create music automatically