How to get TensorFlow running on a MacBookPro running Sierra
As I have been working with clients, one thing is becoming incredibly true – Machine Learning and AI are becoming pre-requisite skills. Certainly modern clouds including Amazon, Azure and Google are providing GPUs or even TPU’s to assist researchers, engineers and corporations take advantage of training and evaluation acceleration. One easy and active way to move forward is with TensorFlow, which leads me to – how do I get started?
I’m a Mac guy, just more productive here… but for whatever reason, they can be difficult with cutting edge capabilities. So here is my jumpstart to help you get TF running on your laptop… just to get going!
About My Mac:
Note that this MacBookPro 13,2 does not have an Nvidia card, and as such cannot exploit the CUDA libraries that seem to be du jour within the TensorFlow community.
Install Python 2.7 – independent of pre-installed Sierra OS X 10.12.6
Install Homebrew
Install brew (https://brew.sh):
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)”
Update the formulae via:
brew upgrade
Get a few logical packages:
brew install wget
Install Python
Install an independent [and up to date] python environment ( via http://docs.python-guide.org/en/latest/starting/install/osx/ )
DHushon-Mac:~$ brew install python
#…
#now test the configuration
DHushon-Mac:~$ python -V
Python 2.7.13
DHushon-Mac:~$ pip -V
pip 9.0.1 from /usr/local/lib/python2.7/site-packages (python 2.7)
#note that brew installs into /usr/local/lib if you have questions please look at the above referenced python-guide
VirtualEnv
VirtualEnv is a critical library that helps maintain independence of python library sets across projects for each project, it will create an independent set of libraries to enable everything from switching between python versions, to conflicting package sets across python projects with non-interference.
http://docs.python-guide.org/en/latest/dev/virtualenvs/
DHushon-Mac:~$ pip install virtualenv
Now you need a working environment to hold the setup instructions and any project libraries for python virtualenv
virtualenv --system-site-packages tensorflow
activate the environment and you will see that your cursor will change
DHushon-Mac:~/work$ source ./tensorflow/bin/activate
(tensorflow) DHushon-Mac:~/work$
Install Stock TensorFlow
Stock TensorFlow is available as a standard python whl.
(tensorflow) DHushon-Mac:~/work$ pip install tensorflow
Collecting tensorflow
Using cached tensorflow-1.3.0-cp27-cp27m-macosx_10_11_x86_64.whl
Requirement already satisfied: wheel in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: backports.weakref>=1.0rc1 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: protobuf>=3.3.0 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: numpy>=1.11.0 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: mock>=2.0.0 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: tensorflow-tensorboard<0.2.0,>=0.1.0 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: six>=1.10.0 in /usr/local/lib/python2.7/site-packages (from tensorflow)
Requirement already satisfied: setuptools in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from protobuf>=3.3.0->tensorflow)
Requirement already satisfied: funcsigs>=1; python_version < "3.3" in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from mock>=2.0.0->tensorflow)
Requirement already satisfied: pbr>=0.11 in /usr/local/lib/python2.7/site-packages (from mock>=2.0.0->tensorflow)
Requirement already satisfied: bleach==1.5.0 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Requirement already satisfied: werkzeug>=0.11.10 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Requirement already satisfied: html5lib==0.9999999 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Requirement already satisfied: markdown>=2.6.8 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow)
Installing collected packages: tensorflow
Successfully installed tensorflow-1.3.0
Now we can run a simple test of TF just to validate that it is appropriately installed and running, create a file (for me it’s testTensor.py as below) and then test the installation:
(tensorflow) DHushon-Mac:~/work$ cat testTensor.py
import tensorflow as tf
# Creates a graph.
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
# Creates a session with log_device_placement set to True.
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
# Runs the op.
print sess.run(c)
(tensorflow) DHushon-Mac:~/work$ python testTensor.py
2017-08-24 10:47:18.394664: 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-24 10:47:18.394690: 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-24 10:47:18.394713: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2017-08-24 10:47:18.394718: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
Device mapping: no known devices.
2017-08-24 10:47:18.394985: I tensorflow/core/common_runtime/direct_session.cc:300] Device mapping:
MatMul: (MatMul): /job:localhost/replica:0/task:0/cpu:0
2017-08-24 10:47:18.397003: I tensorflow/core/common_runtime/simple_placer.cc:872] MatMul: (MatMul)/job:localhost/replica:0/task:0/cpu:0
b: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-08-24 10:47:18.397016: I tensorflow/core/common_runtime/simple_placer.cc:872] b: (Const)/job:localhost/replica:0/task:0/cpu:0
a: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-08-24 10:47:18.397024: I tensorflow/core/common_runtime/simple_placer.cc:872] a: (Const)/job:localhost/replica:0/task:0/cpu:0
[[ 22. 28.]
[ 49. 64.]]
You will see that the stock TensorFlow package is compiled to just use “any” CPU, but the Mac can use basic acceleration to help us speed things along… so how do we enable the additional instructions?
Optimized TensorFlow on Sierra 10.12.x FMA,AVX,AVX2,SSE4.1,SSE4.2
Sure, you could build your own FMA,AVX,AVX2,SSE4.1,SSE4.2 optimized TF binaries, but someone has already done it for us – https://github.com/lakshayg/tensorflow-build.
(tensorflow) DHushon-Mac:~/Downloads$ wget https://github.com/lakshayg/tensorflow-build/raw/72454268db8ce69e0fe3c7b23c17aad6ea69b257/tensorflow-1.2.1-cp36-cp36m-macosx_10_12_x86_64.whl --2017-08-24 10:37:37-- https://github.com/lakshayg/tensorflow-build/raw/72454268db8ce69e0fe3c7b23c17aad6ea69b257/tensorflow-1.2.1-cp36-cp36m-macosx_10_12_x86_64.whl Resolving github.com... 192.30.253.112, 192.30.253.113 Connecting to github.com|192.30.253.112|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://raw.githubusercontent.com/lakshayg/tensorflow-build/72454268db8ce69e0fe3c7b23c17aad6ea69b257/tensorflow-1.2.1-cp36-cp36m-macosx_10_12_x86_64.whl [following] --2017-08-24 10:37:37-- https://raw.githubusercontent.com/lakshayg/tensorflow-build/72454268db8ce69e0fe3c7b23c17aad6ea69b257/tensorflow-1.2.1-cp36-cp36m-macosx_10_12_x86_64.whl Resolving raw.githubusercontent.com... 151.101.184.133 Connecting to raw.githubusercontent.com|151.101.184.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 32419060 (31M) [application/octet-stream] Saving to: ‘tensorflow-1.2.1-cp36-cp36m-macosx_10_12_x86_64.whl.1’
tensorflow-1.2.1-cp36-cp36m-macosx_10_12_x86_64.whl.1 100%[=====================================================================================================================================================================================>] 30.92M 19.1MB/s in 1.6s 2017-08-24 10:37:40 (19.1 MB/s) - ‘tensorflow-1.2.1-cp36-cp36m-macosx_10_12_x86_64.whl.1’ saved [32419060/32419060]
Now, we have to install the whl, so pip is again our tool of choice (to abide by the virtualenv that we have setup)
((tensorflow) DHushon-Mac:~/Downloads$ pip install --upgrade ~/Downloads/tensorflow-1.3.0rc2-cp27-cp27m-macosx_10_12_intel.whl
Processing ./tensorflow-1.3.0rc2-cp27-cp27m-macosx_10_12_intel.whl
Requirement already up-to-date: wheel in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow==1.3.0rc2)
Requirement already up-to-date: backports.weakref>=1.0rc1 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow==1.3.0rc2)
Collecting autograd>=1.1.11 (from tensorflow==1.3.0rc2)
Downloading autograd-1.1.12.tar.gz
Requirement already up-to-date: protobuf>=3.3.0 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow==1.3.0rc2)
Requirement already up-to-date: numpy>=1.11.0 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow==1.3.0rc2)
Requirement already up-to-date: mock>=2.0.0 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow==1.3.0rc2)
Requirement already up-to-date: tensorflow-tensorboard<0.2.0,>=0.1.0 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow==1.3.0rc2)
Requirement already up-to-date: six>=1.10.0 in /usr/local/lib/python2.7/site-packages (from tensorflow==1.3.0rc2)
Requirement already up-to-date: scipy>=0.17 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from autograd>=1.1.11->tensorflow==1.3.0rc2)
Collecting future>=0.15.2 (from autograd>=1.1.11->tensorflow==1.3.0rc2)
Downloading future-0.16.0.tar.gz (824kB)
100% |████████████████████████████████| 829kB 1.2MB/s
Requirement already up-to-date: setuptools in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from protobuf>=3.3.0->tensorflow==1.3.0rc2)
Requirement already up-to-date: funcsigs>=1; python_version < "3.3" in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from mock>=2.0.0->tensorflow==1.3.0rc2)
Requirement already up-to-date: pbr>=0.11 in /usr/local/lib/python2.7/site-packages (from mock>=2.0.0->tensorflow==1.3.0rc2)
Requirement already up-to-date: markdown>=2.6.8 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow==1.3.0rc2)
Requirement already up-to-date: bleach==1.5.0 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow==1.3.0rc2)
Requirement already up-to-date: werkzeug>=0.11.10 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow==1.3.0rc2)
Requirement already up-to-date: html5lib==0.9999999 in /Users/dhushon/work/tensorflow/lib/python2.7/site-packages (from tensorflow-tensorboard<0.2.0,>=0.1.0->tensorflow==1.3.0rc2)
Building wheels for collected packages: autograd, future
Running setup.py bdist_wheel for autograd ... done
Stored in directory: /Users/dhushon/Library/Caches/pip/wheels/fb/33/cc/df1662f8be45065d2a3afbe4c4288b6c2a089c8791ddaa48f3
Running setup.py bdist_wheel for future ... done
Stored in directory: /Users/dhushon/Library/Caches/pip/wheels/c2/50/7c/0d83b4baac4f63ff7a765bd16390d2ab43c93587fac9d6017a
Successfully built autograd future
Installing collected packages: future, autograd, tensorflow
Successfully installed autograd-1.1.12 future-0.16.0 tensorflow-1.3.0rc2
And then repeat the test just to prove that TF is working->
(tensorflow) DHushon-Mac:~/work$ python testTensor.py
Device mapping: no known devices.
2017-08-24 10:05:35.933399: I tensorflow/core/common_runtime/direct_session.cc:300] Device mapping:
MatMul: (MatMul): /job:localhost/replica:0/task:0/cpu:0
2017-08-24 10:05:35.935933: I tensorflow/core/common_runtime/simple_placer.cc:872] MatMul: (MatMul)/job:localhost/replica:0/task:0/cpu:0
b: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-08-24 10:05:35.935947: I tensorflow/core/common_runtime/simple_placer.cc:872] b: (Const)/job:localhost/replica:0/task:0/cpu:0
a: (Const): /job:localhost/replica:0/task:0/cpu:0
2017-08-24 10:05:35.935953: I tensorflow/core/common_runtime/simple_placer.cc:872] a: (Const)/job:localhost/replica:0/task:0/cpu:0
[[ 22. 28.]
[ 49. 64.]]
Next Up: TensorBox & other techniques for shape recognition in images -> video