Core ML Toolsの環境をDockerで構築してみたけど微妙だった

Core ML Toolsの環境をDockerで構築したけど微妙なことが分かった。環境構築のノウハウは得られたのでメモしておく。Core ML Toolsは機械学習の各種モデルをiOS向けに変換するツールです。

環境

% uname -a
Darwin MacBook-Pro.local 19.6.0 Darwin Kernel Version 19.6.0: Sun Jul 5 00:43:10 PDT 2020; root:xnu-6153.141.1~9/RELEASE_X86_64 x86_64

参考書籍

下記の堤さんの書籍を参考に環境構築することにしました。

Python3.8で試す

とりあえずDockerのpython:3.8-alpineで試してみることに。

# docker run --rm -it python:3.8-alpine sh

以降はコンテナの中で

# python -V
Python 3.8.5
# pip -V
pip 20.1.1 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)

Tensorflow 1.14.0のインストールに失敗します。

# pip install tensorflow==1.14.0
ERROR: Could not find a version that satisfies the requirement tensorflow==1.14.0 (from versions: none)
ERROR: No matching distribution found for tensorflow==1.14.0
WARNING: You are using pip version 20.1.1; however, version 20.2 is available.
You should consider upgrading via the '/usr/local/bin/python -m pip install --upgrade pip' command.

pipのバージョンが古いようなので念のためアップデートして試してみるもダメ。

# pip install --upgrade pip
Collecting pip
 Downloading pip-20.2-py2.py3-none-any.whl (1.5 MB)
    |████████████████████████████████| 1.5 MB 3.8 MB/s 
Installing collected packages: pip
 Attempting uninstall: pip
   Found existing installation: pip 20.1.1
   Uninstalling pip-20.1.1:
     Successfully uninstalled pip-20.1.1
Successfully installed pip-20.2
# pip -V
pip 20.2 from /usr/local/lib/python3.8/site-packages/pip (python 3.8)
# pip install tensorflow==1.14.0
ERROR: Could not find a version that satisfies the requirement tensorflow==1.14.0 (from versions: none)
ERROR: No matching distribution found for tensorflow==1.14.0
# 

Tensorflow 1.14.0を使うならpython3.8は諦めた方が良さそう。Tensorflowの公式ページをみたらシステム要件に3.8がありませんでした。

システム要件
Python 3.5–3.7

Python3.7で試す

% docker run --rm -it python:3.7 sh
# python -V
Python 3.7.7
# pip -V
pip 20.0.2 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)
# pip install --upgrade pip

Tensorflowインストールできました。

# pip install tensorflow==1.14.0
Successfully installed ...

続けてKerasとCore ML Tools本体をインストール。

pip install keras==2.2.4
pip install coremltools==3.4

こちらも正常にインストールできました。

インストール後にツールを使って色々試してみたのですが致命的な問題がありました。Core MLのモデルに対して推論を行うpredict()を呼び出すところでエラーになります。例えば以下のようなコードです。

# Use PIL to load and resize the image to expected size
from PIL import Image
example_image = Image.open("daisy.jpg").resize((224, 224))
# Make a prediction using Core ML
out_dict = model.predict({"input_1": example_image})
# Print out top-1 prediction
print(out_dict["classLabel"])

原因はmodelのpredict()を実行するにはCore ML frameworkが必要なのでmacOS上で実行する必要があるようです。

macOS required for model prediction
For the prediction API, coremltools interacts with the Core ML framework which is available on macOS only. Hence the prediction API is not available on Linux.

結論はDockerを使わないでmacOSに直接構築した方が良いということでした。それからTensorflowの1.x系を使う場合はPython3.7を使う必要があるようです。


この記事が気に入ったらサポートをしてみませんか?