macOS for deep learning with Python, TensorFlow, and Keras

macOS for deep learning with Python, TensorFlow, and Keras

In today’s tutorial, I’ll demonstrate how you can configure your macOS system for deep learning using Python, TensorFlow, and Keras.

 

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 dl4cv  and 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.

Figure 1: Selecting Xcode from the Apple App Store.

From there, open a terminal and execute the following command to accept the developer license:

The next step is to install Apple command line tools:

Figure 2: Accepting the Xcode license.

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:

Next we’ll update our package definitions:

Followed by updating your ~/.bash_profile  using the  nano  terminal editor (any other editor should do the trick as well):

Add the following lines to the file:

Figure 3: Changing ~/.bash_profile to accommodate Homebrew.

Next, simply reload your ~/.bash_profile  (this happens automatically when a new terminal is opened):

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:

Before continuing you’ll want to verify that your Python 3 installation is Homebrew’s rather than the macOS system’s:

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.

Figure 4: Executing which python3 and which pip3 to ensure that you are using the Homebrew version of each rather than the system version.

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 :

From there, we’ll update our ~/.bash_profile  again:

Where we’ll add the following lines to the file:

Figure 5: The bottom of our ~/.bash_profile should contain these lines for virtualenv and virtualenvwrapper.

Followed by reloading the file:

Creating the ‘dl4cv’ environment

The dl4cv  environment 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:

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:

Figure 6: The (dl4cv) in the bash prompt signifies that we are working inside the dl4cv virtual environment. If you don’t see this, then execute workon dl4cv to activate the environment.

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:

The only Python dependency required by OpenCV is NumPy, which we can install below:

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:

After those packages are installed we’re ready to install OpenCV.

Step #6: Compile and Install OpenCV

First, let’s download the source code:

Then unpack the archives:

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):

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:

Figure 7: The OpenCV CMake output that ensures the correct Python 3 and NumPy will be used for compiling.

Then we’re ready to perform the compilation compile OpenCV:

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 j  switch. For only one core/processor simply just use the make  command (from the build directory enter make clean  prior to retrying if your build failed or got stuck).

From there you can install OpenCV:

After installing it is necessary to sym-link the cv2.so  file into the dl4cv  virtual environment:

Finally, we can test out the install:

Figure 8: OpenCV 3.3 has been installed and linked as is shown by the successful import and display of the version.

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 dl4cv  virtualenv. If you aren’t in the environment, simply execute:

Then, using pip , install the required Python computer vision, image processing, and machine learning libraries:

Next, install TensorFlow:

Followed by keras:

To verify that Keras is installed properly we can import it and check for errors:

Keras should be imported with no errors, while stating that TensorFlow is being utilized as the backend.

Figure 9: Keras is successfully installed into the dl4cv virtual environment.

At this point, you can familiarize yourself with the ~/.keras/keras.json  file:

Ensure that the image_data_format  is set to channels_last  and that the backend  is 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.

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.

推荐文章

沪公网安备 31010702002009号