In today’s tutorial, I’ll demonstrate how you can configure your macOS system for deep learning using Python, TensorFlow, and Keras.
This tutorial is the final part of a series on configuring your development environment for deep learning. I created these tutorials to accompany my new book, Deep Learning for Computer Vision with Python; however, you can use these instructions to configure your system regardless if you bought my book or not.
In case you’re on the wrong page (or you don’t have macOS), take a look at the other deep learning development environment tutorials in this series:
- Your deep learning + Python Ubuntu virtual machine
- Pre-configured Amazon AWS deep learning AMI with Python
- Configuring Ubuntu for deep learning with Python (CPU only)
- Setting up Ubuntu 16.04 + CUDA + GPU for deep learning with Python
- macOS for deep learning with Python, TensorFlow, and Keras (this post)
To learn how to configure macOS for deep learning and computer vision with Python, just keep reading.
macOS for deep learning with Python, TensorFlow, and Keras
As you get acclimated in the deep learning domain, you’ll want to perform many experiments to hone your skills and even to solve real-world problems.
You’ll find that for experiments in the most chapters inside the Starter Bundle and half the chapters in the Practitioner Bundle can be executed on your CPU. Readers of the ImageNet Bundle will need a GPU machine in order to perform the more advanced experiments.
I definitely don’t recommend churning through large datasets and deep neural networks on your laptop, but like I said, for small experiments it is just fine.
Today, I’ll walk you through the steps to configure your Mac for deep learning.
First, we’ll install Xcode and Homebrew (a package manager). From there we will create a virtual environment called
dl4cvand install OpenCV, TensorFlow, and Keras into the environment.
Let’s get started.
Step #1: Install Xcode
For starters, you’ll need to get Xcode from the Apple App Store and install it. Don’t worry, it is 100% free.
From there, open a terminal and execute the following command to accept the developer license:
$ sudo xcodebuild -license
The next step is to install Apple command line tools:
$ sudo xcode-select --install
Step #2: Install Homebrew
Homebrew (also known as Brew), is a package manager for macOS. You may already have it on your system, but if you don’t you will want to perform the actions in this section.
First we’ll install Homebrew by copying and pasting the entire command into your terminal:
$ /usr/bin/ruby -e "$(curl -fsSL http://ift.tt/YQTuQh)"
Next we’ll update our package definitions:
$ brew update
Followed by updating your
~/.bash_profileusing the
nanoterminal editor (any other editor should do the trick as well):
$ nano ~/.bash_profile
Add the following lines to the file:
# Homebrew export PATH=/usr/local/bin:$PATH
Next, simply reload your
~/.bash_profile(this happens automatically when a new terminal is opened):
$ source ~/.bash_profile
Now that Brew is ready to go, let’s get Python 3 installed.
Step #3: Install Homebrew Python 3 for macOS
This step is actually very easy, but I want to clear up some possible confusion first.
macOS comes with Python installed; however we will be installing a non-system Python using Brew. While you could use your system Python, it is actually strongly discouraged. Therefore, don’t skip this step — it is very important to your successful install.
To install Python 3 with Homebrew, simply execute this command:
$ brew install python3
Before continuing you’ll want to verify that your Python 3 installation is Homebrew’s rather than the macOS system’s:
$ which python3 /usr/local/bin/python3 $ which pip3 /usr/local/bin/pip3
Ensure that you see “
local” in each path. If you don’t see this output, then you aren’t using Homebrew’s install of Python 3.
Assuming your Python 3 install worked, let’s continue on to Step #4.
Step #4: Create your Python virtual environment
As I’ve stated in other install guides on this site, virtual environments are definitely the way to go when working with Python, enabling you to accommodate different versions in sandboxed environments.
In other words, there is less of a chance that you’ll do something that is a pain in the ass to fix. If you mess up an environment, you can simply delete the environment and rebuild it.
Let’s install virtualenv and virtualenvwrapper via
pip:
$ pip3 install virtualenv virtualenvwrapper
From there, we’ll update our
~/.bash_profileagain:
$ nano ~/.bash_profile
Where we’ll add the following lines to the file:
# virtualenv and virtualenvwrapper export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 source /usr/local/bin/virtualenvwrapper.sh
Followed by reloading the file:
$ source ~/.bash_profile
Creating the ‘dl4cv’ environment
The
dl4cvenvironment will house all of our software for performing experiments associated with my book. You can easily name the environment whatever you want, but from here on we’ll be referring to it as
dl4cv.
To create the dl4cv environment with Python 3 simply enter the following command:
$ mkvirtualenv dl4cv -p python3
After Python 3 and supporting scripts are installed into the new environment, you should actually be inside the environment. This is denoted by a ‘
(dl4cv)‘ at the beginning of your bash prompt as shown in the figure below:
If you do not see the modified bash prompt then you can enter the following command at any time to enter the environment at any time:
$ workon dl4cv
The only Python dependency required by OpenCV is NumPy, which we can install below:
$ pip install numpy
That’s it as far as creating a virtual environment and installing NumPy. Let’s continue to Step #5.
Step #5: Install OpenCV prerequisites using Homebrew
The following tools need to be installed for compilation, image I/O, and optimization:
$ brew install cmake pkg-config wget $ brew install jpeg libpng libtiff openexr $ brew install eigen tbb
After those packages are installed we’re ready to install OpenCV.
Step #6: Compile and Install OpenCV
First, let’s download the source code:
$ cd ~ $ wget -O opencv.zip http://ift.tt/2x4vwWB $ wget -O opencv_contrib.zip http://ift.tt/2xIPYcC
Then unpack the archives:
$ unzip opencv.zip $ unzip opencv_contrib.zip
Followed by configuring the build with CMake (it is very important that you copy the CMake command exactly as it appears here, taking care to copy and past the entire command; I would suggest clicking the “<=>” button in the toolbar below to expand the entire command):
$ cd ~/opencv-3.3.0/ $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \ -D PYTHON3_LIBRARY=`python -c 'import subprocess ; import sys ; s = subprocess.check_output("python-config --configdir", shell=True).decode("utf-8").strip() ; (M, m) = sys.version_info[:2] ; print("{}/libpython{}.{}.dylib".format(s, M, m))'` \ -D PYTHON3_INCLUDE_DIR=`python -c 'import distutils.sysconfig as s; print(s.get_python_inc())'` \ -D PYTHON3_EXECUTABLE=$VIRTUAL_ENV/bin/python \ -D BUILD_opencv_python2=OFF \ -D BUILD_opencv_python3=ON \ -D INSTALL_PYTHON_EXAMPLES=ON \ -D INSTALL_C_EXAMPLES=OFF \ -D BUILD_EXAMPLES=ON ..
Note: For the above CMake command, I spent considerable time creating, testing, and refactoring it. I’m confident that it will save you time and frustration if you use it exactly as it appears. Make sure you click the “<=>” button in the toolbar of the code block above to expand the code block. This will enable you to copy and paste the entire command.
Your output should be similar to the screenshot below which ensures that the correct Python 3 binary/library and NumPy version are utilized:
Then we’re ready to perform the compilation compile OpenCV:
$ make -j4
Note: The number ‘4’ above specifies that we have 4 cores/processors for compiling. If you have a different number of processors you can update the
-jswitch. For only one core/processor simply just use the
makecommand (from the build directory enter
make cleanprior to retrying if your build failed or got stuck).
From there you can install OpenCV:
$ sudo make install
After installing it is necessary to sym-link the
cv2.sofile into the
dl4cvvirtual environment:
$ cd ~/.virtualenvs/dl4cv/lib/python3.6/site-packages/ $ ln -s /usr/local/lib/python3.6/site-packages/cv2.cpython-36m-darwin.so cv2.so $ cd ~
Finally, we can test out the install:
$ python >>> import cv2 >>> cv2.__version__ '3.3.0'
If your output properly shows the version of OpenCV that you installed, then you’re ready to go on to Step #7 where we will install the Keras deep learning library.
Step #7: Install Keras
Before beginning this step, ensure you have activated the
dl4cvvirtualenv. If you aren’t in the environment, simply execute:
$ workon dl4cv
Then, using
pip, install the required Python computer vision, image processing, and machine learning libraries:
$ pip install scipy matplotlib pillow $ pip install imutils h5py requests progressbar2 $ pip install scikit-learn scikit-image
Next, install TensorFlow:
$ pip install tensorflow
Followed by keras:
$ pip install keras
To verify that Keras is installed properly we can import it and check for errors:
$ python >>> import keras Using TensorFlow backend. >>>
Keras should be imported with no errors, while stating that TensorFlow is being utilized as the backend.
At this point, you can familiarize yourself with the
~/.keras/keras.jsonfile:
{ "image_data_format": "channels_last", "backend": "tensorflow", "epsilon": 1e-07, "floatx": "float32" }
Ensure that the
image_data_formatis set to
channels_lastand that the
backendis set to
tensorflow.
Congratulations! You’re now ready to go. If you didn’t open up a beer or coffee during the installation process, now is the time. It’s also the time to find a comfortable spot to read Deep Learning for Computer Vision with Python.
Summary
In today’s post, we configured our macOS box for computer vision and deep learning. The main pieces of software included Python 3, OpenCV, TensorFlow, and Keras accompanied by dependencies and installation/compilation tools.
As you can see, utilizing Homebrew, pip, and virtualenv + virtualenvwrapper made this install rather easy. I spent quite a bit of time creating and testing the CMake command which should work easily on your computer. Be sure to give it a try.
If you encountered any problems along the way, leave a comment in the form below.
If you would like to put your newly configured macOS deep learning environment to good use, I would highly suggest you take a look at my new book, Deep Learning for Computer Vision with Python.
Regardless if you’re new to deep learning or already a seasoned practitioner, the book has content to help you reach deep learning mastery — take a look here.
The post macOS for deep learning with Python, TensorFlow, and Keras appeared first on PyImageSearch.
from PyImageSearch http://ift.tt/2xLkwMM
via IFTTT
No comments:
Post a Comment