Google AIの楽曲自動生成ライブラリMagentaをMacbookのvenv環境に入れてimport成功するまで


言語など、時間軸上の順序関係（単語の前後関係）を持つ時系列データを取り扱いに長けたLSTMモデルを、楽曲生成に適用したモデルに、__Google AIの*Magenta*__があります。__2016年に公開__されました。

今回は、この__*Mazenta*__を、Macbookに入れる記事です。

https://research.google/teams/brain/magenta/

https://magenta.tensorflow.org/

https://wired.jp/2016/05/25/google-art-artificial-intelligence/

音楽（楽曲）は、譜面上に音符が時間順に並んでいますので、LSTMでの取り扱いに適した時系列データです。

なお、書き（描き）かけ文章や画像（＝縦横方向の画素値の並び）を途中まで与えて、後続（続き）の文章や画像をAIに生成させることことができるGPTモデル・シリーズを、楽曲生成に取り入れた試みとして、__2020年__にOpenAIの*Jukebox*__も登場しています。

__*Magenta*__の公開によって、深層学習モデルによる楽曲生成に新たな扉を開けたGoogle AIですが、ここに、GPTシリーズを引っ提げて登壇した__Open AIの*Jukebox*__の活躍により、この分野のさらなる発展が期待されます。

https://jukebox.openai.com/

https://openai.com/blog/jukebox/

https://github.com/openai/jukebox/

https://cdn.openai.com/papers/jukebox.pdf

https://colab.research.google.com/github/openai/jukebox/blob/master/jukebox/Interacting_with_Jukebox.ipynb#scrollTo=sAdFGF-bqVMY

https://ledge.ai/jukebox-openai/


##MazengaをMacbookに入れる： 事前準備

Macbookのvenv仮想環境を使用します。
次のサイトの作業を行い、venv環境の中でPython3.7.9が使えるようにします。

https://qiita.com/electronics_diy721/items/64d8624a17b0b4660a90

####venv仮想環境を立ち上げる


```bash:Terminal
electron@diynoMacBook-Pro ~ % python3 -m venv venv-test
electron@diynoMacBook-Pro ~ % source venv-test/bin/activate
(venv-test) electron@diynoMacBook-Pro ~ % python -V
Python 3.7.9
(venv-test) electron@diynoMacBook-Pro ~ % python3 -V
Python 3.7.9
(venv-test) electron@diynoMacBook-Pro ~ % python -version
Unknown option: -e
usage: python [option] ... [-c cmd | -m mod | file | -] [arg] ...
Try `python -h' for more information.
(venv-test) electron@diynoMacBook-Pro ~ % 
```

- TensorFlow 1.15を入れる

```bash:Terminal
(venv-test) electron@diynoMacBook-Pro ~ % pip install tensorflow==1.15
Collecting tensorflow==1.15
  Downloading tensorflow-1.15.0-cp37-cp37m-macosx_10_11_x86_64.whl (124.0 MB)
     |████████████████████████████████| 124.0 MB 115.4 MB/s 
Collecting wrapt>=1.11.1

（ 省略 ）

Successfully installed absl-py-0.13.0 astor-0.8.1 cached-property-1.5.2 gast-0.2.2 google-pasta-0.2.0 grpcio-1.39.0 h5py-3.4.0 importlib-metadata-4.8.0 keras-applications-1.0.8 keras-preprocessing-1.1.2 markdown-3.3.4 numpy-1.21.2 opt-einsum-3.3.0 protobuf-3.17.3 six-1.16.0 tensorboard-1.15.0 tensorflow-1.15.0 tensorflow-estimator-1.15.1 termcolor-1.1.0 typing-extensions-3.10.0.0 werkzeug-2.0.1 wheel-0.37.0 wrapt-1.12.1 zipp-3.5.0
WARNING: You are using pip version 20.1.1; however, version 21.2.4 is available.
You should consider upgrading via the '/Users/electron/venv-test/bin/python3 -m pip install --upgrade pip' command.
(venv-test) electron@diynoMacBook-Pro ~ % 
```
- magentaを入れる

```bash:Terminal
(venv-test) electron@diynoMacBook-Pro ~ % pip install magenta
Collecting magenta
  Downloading magenta-2.1.3-py3-none-any.whl (1.4 MB)
     |████████████████████████████████| 1.4 MB 7.9 MB/s 
Collecting tensorflow-probability
  Using cached tensorflow_probability-0.13.0-py2.py3-none-any.whl (5.4 MB)
Collecting librosa<0.8.0,>=0.6.2
  Downloading librosa-0.7.2.tar.gz (1.6 MB)
     |████████████████████████████████| 1.6 MB 21.6 MB/s 
Requirement already satisfied: six>=1.12.0 in ./venv-test/lib/python3.7/site-packages (from magenta) (1.16.0)

（ 省略 ）

pygments-2.10.0 pygtrie-2.4.2 pyparsing-2.4.7 pypng-0.0.21 python-dateutil-2.8.2 python-rtmidi-1.1.2 pytz-2021.1 requests-2.26.0 resampy-0.2.2 rsa-4.7.2 scikit-image-0.18.3 scikit-learn-0.24.2 scipy-1.7.1 sk-video-1.1.10 sortedcontainers-2.4.0 soundfile-0.10.3.post1 sox-1.4.1 sympy-1.8 tabulate-0.8.9 tensor2tensor-1.15.7 tensorflow-addons-0.14.0 tensorflow-datasets-4.4.0 tensorflow-gan-2.0.0 tensorflow-hub-0.12.0 tensorflow-metadata-1.2.0 tensorflow-probability-0.13.0 tf-slim-1.1.0 threadpoolctl-2.2.0 tifffile-2021.8.8 tornado-6.1 tqdm-4.62.2 traitlets-5.0.5 typeguard-2.12.1 uritemplate-3.0.1 urllib3-1.26.6 wcwidth-0.2.5 zope.event-4.5.0 zope.interface-5.4.0
WARNING: You are using pip version 20.1.1; however, version 21.2.4 is available.
You should consider upgrading via the '/Users/electron/venv-test/bin/python3 -m pip install --upgrade pip' command.
(venv-test) electron@diynoMacBook-Pro ~ % 
```

- Pythonを立ち上げて、magentaをimportできるか確認

```bash:
(venv-test) electron@diynoMacBook-Pro ~ % python
Python 3.7.9 (default, Aug 29 2021, 16:35:31) 
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import magenta
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/electron/venv-test/lib/python3.7/site-packages/magenta/__init__.py", line 17, in <module>
    import magenta.common.beam_search
  File "/Users/electron/venv-test/lib/python3.7/site-packages/magenta/common/__init__.py", line 20, in <module>

（ 省略 ）

  File "/Users/electron/venv-test/lib/python3.7/site-packages/tensorflow_probability/python/__init__.py", line 68, in _validate_tf_environment
    present=tf.__version__))
ImportError: This version of TensorFlow Probability requires TensorFlow version >= 2.5; Detected an installation of version 1.15.0. Please upgrade TensorFlow to proceed.
>>> 
>>> quit()
```

TensorFlowはなージョン2.5以上に上げろと言われた。

教科書では、magentaの2.0.1vを入れていた。
そちらを入れてみる。

```bash:
(venv-test) electron@diynoMacBook-Pro ~ % pip install magenta==2.0.1
Collecting magenta==2.0.1
  Downloading magenta-2.0.1-py3-none-any.whl (1.6 MB)
     |████████████████████████████████| 1.6 MB 13.9 MB/s 
Requirement already satisfied: scipy>=0.18.1 in ./venv-test/lib/python3.7/site-packages (from magenta==2.0.1) (1.7.1)

（ 省略 ）

      Successfully uninstalled gast-0.5.2
Successfully installed apache-beam-2.32.0 avro-python3-1.9.2.1 crcmod-1.7 docopt-0.6.2 fastavro-1.4.4 fasteners-0.16.3 gast-0.2.2 google-apitools-0.5.31 google-auth-1.35.0 google-cloud-bigquery-2.25.1 google-cloud-bigtable-1.7.0 google-cloud-core-1.7.2 google-cloud-datastore-1.15.3 google-cloud-dlp-1.0.0 google-cloud-language-1.3.0 google-cloud-pubsub-1.7.0 google-cloud-recommendations-ai-0.2.0 google-cloud-spanner-1.19.1 google-cloud-videointelligence-1.16.1 google-cloud-vision-1.0.0 google-crc32c-1.1.2 google-resumable-media-2.0.0 grpc-google-iam-v1-0.12.3 grpcio-gcp-0.2.2 hdfs-2.6.0 magenta-2.0.1 orjson-3.6.3 proto-plus-1.19.0 pyarrow-4.0.1 pydot-1.4.2 pymongo-3.12.0
WARNING: You are using pip version 20.1.1; however, version 21.2.4 is available.
You should consider upgrading via the '/Users/electron/venv-test/bin/python3 -m pip install --upgrade pip' command.
(venv-test) electron@diynoMacBook-Pro ~ % 
```

- 再度、import magentaにトライ

```bash:
(venv-test) electron@diynoMacBook-Pro ~ % python
Python 3.7.9 (default, Aug 29 2021, 16:35:31) 
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import magenta
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/electron/venv-test/lib/python3.7/site-packages/magenta/__init__.py", line 26, in <module>
    import magenta.common.beam_search
 
 （ 省略 ）
 
ImportError: This version of TensorFlow Probability requires TensorFlow version >= 2.5; Detected an installation of version 1.15.0. Please upgrade TensorFlow to proceed.
>>> quit()
(venv-test) electron@diynoMacBook-Pro ~ %
```

- 再び、TensorFlow2.5以上を入れるようにお叱りを受けた。
- 素直に応じる。

```bash:
(venv-test) electron@diynoMacBook-Pro ~ % pip install tensorflow==2.5
Collecting tensorflow==2.5
  Downloading tensorflow-2.5.0-cp37-cp37m-macosx_10_11_x86_64.whl (195.6 MB)
     |████████████████████████████████| 195.6 MB 94.9 MB/s 
^CERROR: Operation cancelled by user
WARNING: You are using pip version 20.1.1; however, version 21.2.4 is available.

（ 省略 ）

      Successfully uninstalled tensorflow-1.15.0
Successfully installed astunparse-1.6.3 flatbuffers-1.12 gast-0.4.0 google-auth-oauthlib-0.4.5 grpcio-1.34.1 h5py-3.1.0 keras-nightly-2.5.0.dev2021032900 numpy-1.19.5 oauthlib-3.1.1 requests-oauthlib-1.3.0 six-1.15.0 tensorboard-2.6.0 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.0 tensorflow-2.5.0 tensorflow-estimator-2.5.0 typing-extensions-3.7.4.3
WARNING: You are using pip version 20.1.1; however, version 21.2.4 is available.
You should consider upgrading via the '/Users/electron/venv-test/bin/python3 -m pip install --upgrade pip' command.
(venv-test) electron@diynoMacBook-Pro ~ % 
```

###ついに、__import magenta__に成功

```bash:
(venv-test) electron@diynoMacBook-Pro ~ % python
Python 3.7.9 (default, Aug 29 2021, 16:35:31) 
[Clang 12.0.0 (clang-1200.0.32.27)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> import magenta
/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
>>> 
>>> import magenta
>>> 
>>> quit()
(venv-test) electron@diynoMacBook-Pro ~ % deactivate
electron@diynoMacBook-Pro ~ % 
```

###（参考） GUI環境もあるらしい

https://magenta.tensorflow.org/studio

（解説サイト）
https://tobilog.net/2881/