macでTensorFlowを動かしてみる

Googleが開発した機械学習ライブラリのTensorFlow。
これをmacOS Sierra 10.12.6に入れて動かしてみる。

動作にはこの記事を参考にさせていただいた。


まずはコマンドラインでインストール。
インストールはたったこれだけ。

$sudo pip install tensorflow

だったが、エラーが大量発生。

DEPRECATION: Uninstalling a distutils installed project (six) has been deprecated and will be removed in a future version. This is due to the fact that uninstalling a distutils project will only partially uninstall the project. Uninstalling six-1.4.1: Exception: Traceback (most recent call last): File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/basecommand.py", line 215, in main status = self.run(options, args) File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/commands/install.py", line 342, in run prefix=options.prefix_path, File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_set.py", line 778, in install requirement.uninstall(auto_confirm=True) File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_install.py", line 754, in uninstall paths_to_remove.remove(auto_confirm) File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/req/req_uninstall.py", line 115, in remove renames(path, new_path) File "/Library/Python/2.7/site-packages/pip-9.0.1-py2.7.egg/pip/utils/__init__.py", line 267, in renames shutil.move(old, new) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 302, in move copy2(src, real_dst) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 131, in copy2 copystat(src, dst) File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/shutil.py", line 103, in copystat os.chflags(dst, st.st_flags) OSError: [Errno 1] Operation not permitted: '/tmp/pip-_DmMWn-uninstall/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/six-1.4.1-py2.7.egg-info'

エラー内容について検索すると、対処法が載っていた。
sixのOSError:でtensorFlowがインストールできんがな

原因:
El Capitanからsixが入っているが、このsixはPermissionの問題で削除できないとのこと
ちなみにsixはpython2とpython3の互換ライブラリ。

なので、sixを入れないコマンドでインストール。
$ sudo pip install tensorflow --ignore-installed six
インストール成功。

さっそく王道のHello worldを表示させる。
ちゃんと出力されたので成功。
$>>> import tensorflow as tf
$>>> hello = tf.constant("Hello, TensorFlow!")
$>>> sess = tf.Session() $>>> print(sess.run(hello))
$Hello, TensorFlow!

ここから本題。
tensorflowライブラリをダウンロードする。
gpuのサポートの質問にはnとしておく。
$ git clone -b v0.6.0 --recurse-submodules https://github.com/tensorflow/tensorflow.git
$ cd tensorflow
$ ./configure
Do you wish to build TensorFlow with GPU support? [y/N] n

早速サンプルを実行する。

$ python /Users/ユーザー名/tensorflow/tensorflow/models/image/imagenet/classify_image.py

結果はやはりジャイアントパンダとなった。

>> Downloading inception-2015-12-05.tgz 100.0%() ('Succesfully downloaded', 'inception-2015-12-05.tgz', 88931400, 'bytes.') 2017-08-30 13:10:03.558550: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations. 2017-08-30 13:10:03.558589: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations. 2017-08-30 13:10:05.446109: W tensorflow/core/framework/op_def_util.cc:333] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().
giant panda, panda, panda bear, coon bear, Ailuropoda melanoleuca (score = 0.89107)
indri, indris, Indri indri, Indri brevicaudatus (score = 0.00779)
lesser panda, red panda, panda, bear cat, cat bear, Ailurus fulgens (score = 0.00296)
custard apple (score = 0.00147)
earthstar (score = 0.00117)

ソースコード内のリンク先tgzファイルを落として解凍すると、パンダの画像があった。

なので次は、下の画像でやってみる。

















画像を指定するには、引数で指定すれば良いらしい。
$ python classify_image.py --image_file test.jpg

実行結果は、

tabby, tabby cat (score = 0.47896)
tiger cat (score = 0.24390)
Egyptian cat (score = 0.13316)
lynx, catamount (score = 0.00955)
plastic bag (score = 0.00435)

tabby cat、つまりぶち猫ですね。
一番確信度が低いものに、plastic bag(ビニール袋)が入っているとは。

それでは。