GoogleAIの音楽生成モデルMagentaをMacbookで動かす：melody_rnn_generate.py（編）



##（ 環境構築 ）

前回の記事では、MacbookでMagentaを動かすための環境構築をしました。
前回は、venv環境にpip installでmagentaを入れて、pythonインタプリタでimportするところまでやりました。

https://qiita.com/electronics_diy721/items/a8f6ec5cc4eb4b9091f1

今回は、その続きで、以下を行いました。

- 1. GitHubリポジトリからMagentaの資源一式を取得
- 2. GitHubから貼られているリンク先から、学習済みのモデル（__*basic_rnn.mag*__）を取得
- 3. 学習済みモデルを格納するフォルダと、生成ファイルを格納するフォルダを作成
- 4. __*melody_rnn_generate.py*__を動かす
- 5. Macbookに標準搭載されているGarageBandアプリで、生成された音楽ファイルを再生する

__これら5つの作業すべて成功しました__:saxophone::guitar::musical_keyboard:

###venv環境に入る

```bash:Terminal
electron@diynoMacBook-Pro ~ % python3 -m venv venv-test    
electron@diynoMacBook-Pro ~ % source venv-test/bin/activate
(venv-test) electron@diynoMacBook-Pro ~ % 
```

###magentaの学習済みモデルを取得

####（ 取得元 ）

```text:
http://download.magenta.tensorflow.org/models/basic_rnn.mag
```

http://download.magenta.tensorflow.org/

![](https://user-images.githubusercontent.com/87643752/131252337-17989998-477b-4c4d-be83-fc5c50175606.png)

- __*Pre-trained*__の下にある青文字の4つが、__4種類の学習済みモデル__（へのリンク）

![](https://user-images.githubusercontent.com/87643752/131252343-b3abc9cd-9d9e-4da7-ad82-c97a32ac2f5d.png)

- __*basic_rnn*__へのリンク先のURLをコピーする

![](https://user-images.githubusercontent.com/87643752/131252349-d8701cf3-3bbf-4752-b792-ca9beb6ff5d6.png)

####*wget*コマンドで取得

>```bash:Terminal
>wget http://download.magenta.tensorflow.org/models/basic_rnn.mag
>```

```bash:Terminal
(venv-test) electron@diynoMacBook-Pro ~ % wget http://download.magenta.tensorflow.org/models/basic_rnn.mag
--2021-08-29 22:09:32--  http://download.magenta.tensorflow.org/models/basic_rnn.mag
download.magenta.tensorflow.org (download.magenta.tensorflow.org) をDNSに問いあわせています... 2404:6800:4004:80b::2010, 172.217.161.80
download.magenta.tensorflow.org (download.magenta.tensorflow.org)|2404:6800:4004:80b::2010|:80 に接続しています... 接続しました。
HTTP による接続要求を送信しました、応答を待っています... 200 OK
長さ: 13044885 (12M) [binary/octet-stream]
`basic_rnn.mag' に保存中

basic_rnn.mag                                100%[============================================================================================>]  12.44M  52.4MB/s 時間 0.2s     

2021-08-29 22:09:32 (52.4 MB/s) - `basic_rnn.mag' へ保存完了 [13044885/13044885]

(venv-test) electron@diynoMacBook-Pro ~ % ls | grep basic_rnn.mag
basic_rnn.mag
(venv-test) electron@diynoMacBook-Pro ~ % pwd   
/Users/electron
(venv-test) electron@diynoMacBook-Pro ~ % 
```

- 任意のフォルダに格納する
- 今回は、Users/ユーザ名/直下に、__*magenta_pretrained_models*__フォルダを作り、そこに格納する

```bash:Terminal
(venv-test) electron@diynoMacBook-Pro ~ % mkdir magenta_pretrained_models
(venv-test) electron@diynoMacBook-Pro ~ % mv basic_rnn.mag ./magenta_pretrained_models/basic_rnn.mag
(venv-test) electron@diynoMacBook-Pro ~ % ls ./magenta_pretrained_models 
basic_rnn.mag
(venv-test) electron@diynoMacBook-Pro ~ % ls | grep basic_rnn.mag                                   
(venv-test) electron@diynoMacBook-Pro ~ % 
```

- magentaモデルから生成される楽曲ファイルの出力先フォルダを作る
- 今回は、Users/ユーザ名/直下に、__*magenta_output_files*__フォルダを作り、そこに格納する
- さっき作った、学習済みモデルの格納先フォルダと、いま作った出力ファイルの格納先フォルダの絶対パスを確認する


```bash:Terminal
(venv-test) electron@diynoMacBook-Pro ~ % mkdir magenta_output_files
(venv-test) electron@diynoMacBook-Pro ~ % pwd
/Users/electron
(venv-test) electron@diynoMacBook-Pro ~ % ls /Users/electron/magenta_pretrained_models 
basic_rnn.mag
(venv-test) electron@diynoMacBook-Pro ~ % ls /Users/electron/magenta_output_files     
(venv-test) electron@diynoMacBook-Pro ~ % 
```

###magentaのコード資源一式を取得

####（ 取得元 ）

```text:
https://github.com/magenta/magenta.git
```

https://github.com/magenta/magenta.git

![](https://user-images.githubusercontent.com/87643752/131252358-d7aeb5f4-e575-4bc4-90df-2521cf0bd9a1.png)


```bash:Terminal
(venv-test) electron@diynoMacBook-Pro ~ % git clone https://github.com/magenta/magenta.git
Cloning into 'magenta'...
remote: Enumerating objects: 15272, done.
remote: Counting objects: 100% (143/143), done.
remote: Compressing objects: 100% (108/108), done.
remote: Total 15272 (delta 56), reused 79 (delta 35), pack-reused 15129
Receiving objects: 100% (15272/15272), 36.27 MiB | 21.67 MiB/s, done.
Resolving deltas: 100% (11488/11488), done.
(venv-test) electron@diynoMacBook-Pro ~ % ls magenta
AUTHORS			README.md		magenta			setup.cfg
LICENSE			demos			magenta-logo-bg.png	setup.py
(venv-test) electron@diynoMacBook-Pro ~ %
```

###__*melody_rnn_generate.py*__の格納場所の相対パスを確認

__（ 相対パス ）__

- magenta/magenta/models/melody_rnn/melody_rnn_generate.py

```bash:Terminal
(venv-test) electron@diynoMacBook-Pro ~ % ls magenta/magenta/models/melody_rnn
README.md				melody_rnn_create_dataset.py		melody_rnn_model.py			melody_rnn_train.py
__init__.py				melody_rnn_create_dataset_test.py	melody_rnn_pipeline.py			primer.mid
melody_rnn_config_flags.py		melody_rnn_generate.py			melody_rnn_sequence_generator.py
(venv-test) electron@diynoMacBook-Pro ~ % 
(venv-test) electron@diynoMacBook-Pro ~ % ls magenta/magenta/models/melody_rnn | grep melody_rnn_generate.py
melody_rnn_generate.py
(venv-test) electron@diynoMacBook-Pro ~ %
```

 ```bash:Terminal
(venv-test) electron@diynoMacBook-Pro ~ % python -V
Python 3.7.9
(venv-test) electron@diynoMacBook-Pro ~ % python --version
Python 3.7.9
(venv-test) electron@diynoMacBook-Pro ~ %
```

###実行

 ```bash:Terminal
(venv-test) electron@diynoMacBook-Pro ~ % python magenta/magenta/models/melody_rnn/melody_rnn_generate.py \
> --config=basic_rnn \
> --bundle_file=/Users/electron/magenta_pretrained_models/basic_rnn.mag \
> --output_dir=/Users/electron/magenta_output_files \
> --num_outputs=5 \
> --qpm=120.0 \
> --primer_melody="[60]"

/Users/electron/venv-test/lib/python3.7/site-packages/librosa/util/decorators.py:9: NumbaDeprecationWarning: An import was requested from a module that has moved location.
Import requested from: 'numba.decorators', please update to use 'numba.core.decorators' or pin to Numba version 0.48.0. This alias will not be present in Numba version 0.50.0.
  from numba.decorators import jit as optional_jit
/Users/electron/venv-test/lib/python3.7/site-packages/librosa/util/decorators.py:9: NumbaDeprecationWarning: An import was requested from a module that has moved location.
Import of 'jit' requested from: 'numba.decorators', please update to use 'numba.core.decorators' or pin to Numba version 0.48.0. This alias will not be present in Numba version 0.50.0.
  from numba.decorators import jit as optional_jit
WARNING:tensorflow:From /Users/electron/venv-test/lib/python3.7/site-packages/tensorflow/python/compat/v2_compat.py:96: disable_resource_variables (from tensorflow.python.ops.variable_scope) is deprecated and will be removed in a future version.
Instructions for updating:
non-resource variables are not supported in the long term
2021-08-29 22:19:19.319838: I tensorflow/core/platform/cpu_feature_guard.cc:142] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations:  AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
2021-08-29 22:19:19.332913: W tensorflow/core/framework/op_def_util.cc:371] Op TensorArray is deprecated. It will cease to work in GraphDef version 16. Use TensorArrayV3.
2021-08-29 22:19:19.333112: W tensorflow/core/framework/op_def_util.cc:371] Op TensorArrayScatter is deprecated. It will cease to work in GraphDef version 19. Use TensorArrayGradV3.
2021-08-29 22:19:19.333369: W tensorflow/core/framework/op_def_util.cc:371] Op TensorArrayRead is deprecated. It will cease to work in GraphDef version 16. Use TensorArrayReadV3.
2021-08-29 22:19:19.334375: W tensorflow/core/framework/op_def_util.cc:371] Op TensorArrayWrite is deprecated. It will cease to work in GraphDef version 16. Use TensorArrayWriteV3.
2021-08-29 22:19:19.334458: W tensorflow/core/framework/op_def_util.cc:371] Op TensorArraySize is deprecated. It will cease to work in GraphDef version 16. Use TensorArraySizeV3.
2021-08-29 22:19:19.334550: W tensorflow/core/framework/op_def_util.cc:371] Op TensorArrayGather is deprecated. It will cease to work in GraphDef version 16. Use TensorArrayGatherV3.
WARNING:tensorflow:The saved meta_graph is possibly from an older release:
'model_variables' collection should be of type 'byte_list', but instead is of type 'node_list'.
W0829 22:19:19.356595 4522139072 meta_graph.py:884] The saved meta_graph is possibly from an older release:
'model_variables' collection should be of type 'byte_list', but instead is of type 'node_list'.
INFO:tensorflow:Restoring parameters from /var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/tmp_ps6istm/model.ckpt
I0829 22:19:19.357554 4522139072 saver.py:1298] Restoring parameters from /var/folders/dw/kq_8pwps5s771nklvc8krws40000gn/T/tmp_ps6istm/model.ckpt
INFO:tensorflow:Beam search yields sequence with log-likelihood: -148.834183 
I0829 22:19:19.736538 4522139072 events_rnn_model.py:380] Beam search yields sequence with log-likelihood: -148.834183 
INFO:tensorflow:Beam search yields sequence with log-likelihood: -147.136459 
I0829 22:19:19.958060 4522139072 events_rnn_model.py:380] Beam search yields sequence with log-likelihood: -147.136459 
INFO:tensorflow:Beam search yields sequence with log-likelihood: -82.408890 
I0829 22:19:20.161226 4522139072 events_rnn_model.py:380] Beam search yields sequence with log-likelihood: -82.408890 
INFO:tensorflow:Beam search yields sequence with log-likelihood: -129.472076 
I0829 22:19:20.370533 4522139072 events_rnn_model.py:380] Beam search yields sequence with log-likelihood: -129.472076 
INFO:tensorflow:Beam search yields sequence with log-likelihood: -150.948441 
I0829 22:19:20.574712 4522139072 events_rnn_model.py:380] Beam search yields sequence with log-likelihood: -150.948441 
INFO:tensorflow:Wrote 5 MIDI files to /Users/electron/magenta_output_files
I0829 22:19:20.578648 4522139072 melody_rnn_generate.py:217] Wrote 5 MIDI files to /Users/electron/magenta_output_files
(venv-test) electron@diynoMacBook-Pro ~ % 
```

###出力ファイルを確認

 ```bash:Terminal
(venv-test) electron@diynoMacBook-Pro ~ % ls /Users/electron/magenta_output_files
2021-08-29_221919_1.mid	2021-08-29_221919_2.mid	2021-08-29_221919_3.mid	2021-08-29_221919_4.mid	2021-08-29_221919_5.mid
(venv-test) electron@diynoMacBook-Pro ~ % 
(venv-test) electron@diynoMacBook-Pro ~ % ls /Users/electron/magenta_output_files | wc -l
       5
(venv-test) electron@diynoMacBook-Pro ~ % 
```

####引数で指定した通り、5個の音楽ファイルがある

![](https://user-images.githubusercontent.com/87643752/131252372-b8defcf6-060a-4015-8a77-cd915e907208.png)


####__*QuickTime Player*__では開けない

![](https://user-images.githubusercontent.com/87643752/131252374-ecbb0851-b002-4d86-9f1e-eb54faea4cb8.png)

！[]（https://user-images.githubusercontent.com/87643752/131252377-b88da114-ad22-4189-be87-942dd3b0c49c.png）

####__*GarageBandr*__で開けた

![](https://user-images.githubusercontent.com/87643752/131252381-b517c361-c633-4aa2-837b-61bc85b87f2d.png)

![](https://user-images.githubusercontent.com/87643752/131252391-c594573e-d0ab-4c97-bfc1-af535b5a5cb7.png)

- 開けた！

![](https://user-images.githubusercontent.com/87643752/131252395-8ed4f678-8e92-47e3-82ec-1c372db583ad.png)

- 再生もできた！
- __単純なメロディですが、音もちゃんと鳴りました。__

![](https://user-images.githubusercontent.com/87643752/131252399-d00b61d1-52a4-4a23-9f85-401e1c9af0f2.png)

- ２つ目の生成ファイルも開けた

![](https://user-images.githubusercontent.com/87643752/131252407-388d2f0c-7d74-41f8-815c-660b598258c5.png)