How To Install Keras On Mac
Inside this tutorial, you will larn how to configure macOS Mojave for deep learning.
Afterwards you've gone through this tutorial, your macOS Mojave system will be ready for (one) deep learning with Keras and TensorFlow, and (2) ready for Deep Learning for Computer Vision with Python.
A tutorial on configuring Mojave has been a long fourth dimension coming on my blog since the Mojave OS was officially released in September 2022.
The OS was plagued with bug from the beginning, and I decided to hold off. I'm still actually running High Sierra on my machines, but later putting this guide together I feel confident in recommending Mojave to PyImageSearch readers.
Apple has fixed most of the bugs, simply equally yous'll see in this guide, Homebrew (an unofficial packet manager for macOS) doesn't brand everything particularly easy.
If you're ready with a fresh install of macOS Mojave and are upwardly for today's challenge, permit's get started configuring your system for deep learning.
Also released today is my Ubuntu 18.04 deep learning configuration guide with optional GPU back up. Be sure to bank check it out!
To larn how to configure macOS for deep learning and computer vision with Python, just continue reading.
macOS Mojave: Install TensorFlow and Keras for Deep Learning
Inside of this tutorial, we'll review the seven steps to configuring Mojave for deep learning.
Inside of Step #iii , we'll exercise some Homebrew formulae kung fu to get Python 3.vi installed.
Yous see, Homebrew now by default installs Python iii.7.
This presents a challenge to united states of america in the deep learning customs considering Tensorflow does not all the same officially support Python 3.seven.
The TensorFlow squad is definitely working on Python iii.seven support — simply if y'all're running macOS Mojave you probably don't want to twiddle your thumbs and look until Python 3.7 support is officially released.
If you've meet this puzzler, then my install guide is for you.
Let's begin!
Stride #1: Install and configure Xcode
For starters, you'll demand to get Xcode from the Apple tree App Shop and install it. Don't worry, information technology is 100% gratuitous.
Afterward Xcode has been downloaded and installed from the App Store, open a terminal and execute the following command to accept the developer license:
$ sudo xcodebuild -license
Press "enter" then scroll to the bottom with the "space" primal and so type "hold".
The side by side step is to install Apple command line tools:
$ sudo xcode-select --install
This will launch a window where you need to press "Install". From there, you'll have to have another understanding (this time with a button). Finally, a download progress window will launch and y'all'll need to await a few minutes.
Step #2: Install Homebrew on macOS Mojave
Homebrew (besides known as Brew), is a parcel manager for macOS. Yous may already accept it on your system, but if y'all don't you will want to follow the commands in this department to install it.
First, we'll install Homebrew past copying and pasting the unabridged command into your terminal:
$ /usr/bin/ruddy -due east "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/main/install)"
Next, we'll update our bundle definitions:
$ brew update
Followed by updating your ~/.bash_profile
using the nano
terminal 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
To salve and shut, press "ctrl + o" (save), then "enter" to proceed the filename, and finally "ctrl + 10" (exit).
Let's reload our profile:
$ source ~/.bash_profile
Now that Brew is set up to go, let's get Python iii.6 installed.
Step #3: Downgrade Python 3.seven to Python 3.6 on macOS Mojave
I take a love-hate relationship with Homebrew. I dear how convenient it is and how the volunteer team supports so much software. They practice a really dandy job. They're always on top of their game supporting the latest software.
The trouble with Mojave is that past default Homebrew will install Python 3.vii, but 3.seven is non (all the same) supported by TensorFlow.
Therefore, we need to practise some Kung Fu to get Python 3.vi installed on Mojave.
If yous try to install Python three.half-dozen straight, you lot'll encounter this problem:
The problem is that sphinx-doc
depends on Python 3.7, and Python 3.6.v depends on sphinx-doc
which depends on Python three.vii.
Reading that sentence may give you a headache, but I think you become the point that we have a circular dependency problem.
Note: The following steps worked for me and I tested them twice on ii fresh instances of Mojave. If you know of an improved manner to install Python 3.vi, please permit me and the community know in the comments.
Let's have steps to fix the circular dependency issue.
Beginning, install Python (this installs Python 3.7 which we will later downgrade):
$ brew install python3
At present nosotros need to remove the circular dependency.
Let'south go ahead and edit the Homebrew formulae for sphinx-doc
every bit that is where the trouble lies:
$ nano /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/sphinx-doctor.rb
Now scroll down and remove the Python dependency by placing a #
in forepart of information technology to comment it out:
Once you lot've added the #
to comment this line out, go ahead and salve + exit.
From in that location, just reinstall sphinx-doctor:
$ brew reinstall sphinx-doc
Now information technology is time to install Python 3.6.5.
The first step is to unlink Python:
$ mash unlink python
And from there we tin can actually install Python three.6:
$ mash install https://raw.githubusercontent.com/Homebrew/homebrew-core/f2a764ef944b1080be64bd88dca9a1d80130c558/Formula/python.rb
From here you should check which Python is existence used past querying the version:
$ which python3 /usr/local/bin/python3 $ python3 --version Python 3.6.5
Inspect the output of the starting time control ensuring that you encounter local/
in between /usr/
and bin/python3
.
Equally our output indicates, nosotros are at present using Python three.half-dozen.5!
Step #4: Install brew packages for OpenCV on macOS Mojave
The following tools need to be installed for compilation, image I/O, and optimization:
$ brew install cmake pkg-config wget $ mash install jpeg libpng libtiff openexr $ brew install eigen tbb hdf5
Subsequently those packages are installed nosotros're set to create our Python virtual environment.
Step #five: Create your Python virtual environs in macOS Mojave
As I've stated in other install guides on this site, virtual environments are definitely the style to become when working with Python, enabling you to adjust dissimilar versions in sandboxed environments.
If you lot mess upwardly an environment, you lot can simply delete the environment and rebuild it, without affecting other Python virtual environments.
Let's install virtualenv and virtualenvwrapper via pip
:
$ pip3 install virtualenv virtualenvwrapper
From at that place, we'll update our ~/.bash_profile
again:
$ nano ~/.bash_profile
Where we'll add the following lines to the file:
# virtualenv and virtualenvwrapper export WORKON_HOME=$Home/.virtualenvs export VIRTUALENVWRAPPER_PYTHON=/usr/local/bin/python3 source /usr/local/bin/virtualenvwrapper.sh
Followed by reloading the contour:
$ source ~/.bash_profile
Check for any errors in the terminal output. If virtualenvwrapper
and Python 3.6 are playing nice together, you should exist ready to create a new virtual environment.
Creating the dl4cv
virtual environment on macOS Mojave
The dl4cv
surround will house TensorFlow, Keras, OpenCV and all other associated Python packages for my deep learning volume. Y'all can of course proper noun the environment whatever you want, simply from here on we'll exist referring to information technology as dl4cv
.
To create the dl4cv
environment with Python 3 merely enter the post-obit control:
$ mkvirtualenv dl4cv -p python3
Later Python 3 and supporting scripts are installed into the new environment, you lot should actually be inside the environment. This is denoted by (dl4cv)
at the beginning of your bash prompt as shown in the figure below:
If you do not come across the modified bash prompt then you tin can enter the following control at any fourth dimension to enter the environs at whatsoever time:
$ workon dl4cv
But to be on the safety side, let'due south bank check which Python our environment is using and query the version one time once more:
$ workon dl4cv $ which python /Users/admin/.virtualenvs/dl4cv/bin/python $ python --version Python 3.6.5
Notice that the python executable is in ~/.virtualenvs/dl4cv/bin/
, our dl4cv
virtual surround. Also triple bank check here that you lot're using Python 3.half-dozen.5.
When y'all're sure your virtual environment is properly configured with Python 3.6.five it is safe to motion on and install software into the environment.
Let'due south continue to Step #half-dozen.
Step #6: Install OpenCV on macOS Mojave
We have ii options for installing OpenCV for compatibility with my deep learning book.
The first method ( Step #6a ) is by using a precompiled binary available in the Python Parcel Index (where pip pulls from). The disadvantage is that the maintainer has chosen not to compile patented algorithms into the binary.
The 2d pick ( Step #6b ) is to compile OpenCV from source. This method allows for full control over the compile including optimizations and patented algorithms ("nonfree").
I recommend going with the first option if you are a beginner, constrained by time, or if yous know y'all don't demand patented algorithms (DL4CV does non require the added functionality). The first selection will crave just 5 minutes.
Ability users should go with the second choice while allowing for almost 40 to 60 minutes to compile.
Step #6a: Install OpenCV with pip
Ensure y'all're working in the dl4cv
surround and so enter the pip install
command with the package name as shown:
$ workon dl4cv $ pip install opencv-contrib-python
Note: If you require a specific version you can use the following syntax: pip install opencv-contrib-python==3.iv.4
.
Congrats! Yous at present have OpenCV installed.
From here you can skip to Step #7.
Step #6b: Compile and Install OpenCV
If you lot performed Stride #6a you should skip this option and go to Step #7 .
Let's compile OpenCV from source.
The only Python dependency required by OpenCV is NumPy, which we can install via:
$ workon dl4cv $ pip install numpy
First, allow'southward download the source code:
$ cd ~ $ wget -O opencv.zero https://github.com/opencv/opencv/annal/3.4.4.zip $ wget -O opencv_contrib.null https://github.com/opencv/opencv_contrib/archive/3.iv.4.zip
Note: You can replace three.4.iv.zip
with four.0.0.zilch
or higher if you'd similar to use a different version of OpenCV. Only brand certain that both the opencv
and opencv_contrib
downloads are for the same version!
Adjacent unpack the athenaeum:
$ unzip opencv.zip $ unzip opencv_contrib.nix
And rename the directories:
$ mv opencv-3.4.four opencv $ mv opencv_contrib-3.iv.four opencv_contrib
Notation: Supervene upon the folder name in the command with the i corresponding to your version of OpenCV.
To prepare our compilation process we utilize CMake.
It is very important that you copy the CMake command exactly as information technology appears hither, taking intendance to re-create and past the entire command; I would suggest clicking the "<>" push button in the toolbar below to expand the entire control:
$ cd ~/opencv $ mkdir build $ cd build $ cmake -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib/modules \ -D PYTHON3_LIBRARY=`python -c 'import subprocess ; import sys ; south = subprocess.check_output("python-config --configdir", beat out=True).decode("utf-8").strip() ; (M, k) = sys.version_info[:2] ; print("{}/libpython{}.{}.dylib".format(s, 1000, m))'` \ -D PYTHON3_INCLUDE_DIR=`python -c 'import distutils.sysconfig as south; 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 OPENCV_ENABLE_NONFREE=ON \ -D BUILD_EXAMPLES=ON ..
Note: For the higher up CMake command, I spent considerable time creating, testing, and refactoring it. I'm confident that information technology volition salvage y'all time and frustration if you use information technology exactly as it appears. Make sure you click the "<>" button in the toolbar of the lawmaking block to a higher place to expand the code block. This will enable you to copy and paste the entire command.
Running CMake will take ii-five minutes.
Yous should always inspect your CMake output for errors and to ensure your compile settings are set as intended.
Your output should be similar to the screenshots below which ensure that the correct Python 3 binary/library and NumPy version are utilized equally well equally "non-gratuitous algorithms" being on:
If your CMake output for OpenCV matches mine, and so we're ready to really compile OpenCV:
$ make -j4
Note: About macOS machines volition accept at least 4 cores/CPUs. Y'all can (and should) edit the flag above with a number according to your organization's processing specs to speedup the compile process.
From there you can install OpenCV:
$ sudo make install
After installing information technology is necessary to sym-link the cv2.so
file into the dl4cv
virtual environs.
What nosotros demand to do is create a link from where OpenCV was installed into the virtual environment itself. This is known equally a symbolic link.
Permit's go ahead and take intendance of that at present, kickoff by grabbing the proper name of the .and then
file:
$ cd /usr/local/python/cv2/python-3.6 $ ls cv2.cpython-36m-darwin.so
And now let'due south rename the .and so
file:
$ sudo mv cv2.cpython-36m-darwin.so cv2.opencv3.4.four.and then $ cd ~/.virtualenvs/dl4cv/lib/python3.6/site-packages $ ln -s /usr/local/python/cv2/python-3.6/cv2.opencv3.4.4.so cv2.and then
Notation: If you lot have multiple OpenCV versions ever installed in your organisation, you tin can utilize this same naming convention and symbolic linking method.
Finally, we can test out the install:
$ cd ~ $ python >>> import cv2 >>> cv2.__version__ '3.4.4'
If your output properly shows the version of OpenCV that y'all installed, then you're fix to continue to Step #seven where we volition install the Keras deep learning library.
Step #seven: Install TensorFlow and Keras on macOS Mojave
Before beginning this pace, ensure you have activated the dl4cv
virtual environment. If you aren't in the environment, just execute:
$ workon dl4cv
Then, using pip
, install the required Python computer vision, image processing, and machine learning libraries:
$ pip install scipy pillow $ pip install imutils h5py requests progressbar2 $ pip install scikit-learn scikit-prototype
Adjacent, install matplotlib and update the rendering backend:
$ pip install matplotlib $ mkdir ~/.matplotlib $ bear on ~/.matplotlib/matplotlibrc $ echo "backend: TkAgg" >> ~/.matplotlib/matplotlibrc
Be sure to read this guide about Working with Matplotlib on OSX, if you're always having trouble with plots not showing up.
Then, install TensorFlow:
$ pip install tensorflow
Followed by Keras:
$ pip install keras
To verify that Keras is installed properly we tin import it and check for errors:
$ workon dl4cv $ 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 indicate, you tin 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 fix to tensorflow
.
Pre-configured environments
Congratulations! you lot've successfully configured your macOS Mojave desktop/laptop for deep learning!
You're now ready to go. If yous didn't grab upwardly a tea or coffee during the installation process, now is the time. It'due south besides the fourth dimension to observe a comfy spot to read Deep Learning for Calculator Vision with Python.
Did you have any trouble configuring your Mojave deep learning system?
If you struggled along the style, I encourage you lot to re-read the instructions and attempt to debug. If yous're still struggling, y'all can reach out in the DL4CV companion website issue tracker (there's a registration link in the front end of your book) or past contacting me.
I also want to take the opportunity to inform yous almost the pre-configured instances that come along with your book:
- The DL4CV VirtualBox VM is pre-configured and set up to go with Ubuntu eighteen.04 and all other necessary deep learning packages/libraries. This VM is able to run in isolation on elevation of your macOS operating organisation inside of a tool called VirtualBox. It volition help you through well-nigh all experiments in the Starter andPractitioner bundles. For theImageNet parcel, a GPU is a necessity and this VM does not support GPUs.
- My DL4CV Amazon Motorcar Image for the AWS cloud is freely open to the internet — no buy required (other than AWS charges, of course). Getting started with a GPU in the cloud simply takes about 4-6 minutes. For less than the price of a loving cup of coffee, y'all can apply a GPU case for an hour or two which is simply plenty time to complete some (definitely not all) of the more advanced lessons in DL4CV. The following environments are pre-configured:
dl4cv
,mxnet
,tfod_api
,mask_rcnn
, andretinanet
.
Azure users should consider the Azure DSVM. You can read my review of the Microsoft Azure DSVM here. All lawmaking from 1 of the first releases of DL4CV in 2022 was tested using Microsoft'southward DSVM. Additional configuration is required for the DSVM to back up the Bonus Bundle capacity of DL4CV, but other than that y'all won't find yourself installing very many tools. If Azure is your preferred cloud provider, I encourage you to stick with Azure take advantage of what the DSVM has to offer.
What'south next? I recommend PyImageSearch University.
Course information:
35+ total classes • 39h 44m video • Last updated: February 2022
★★★★★ 4.84 (128 Ratings) • 3,000+ Students Enrolled
I strongly believe that if you had the correct teacher you could master figurer vision and deep learning.
Do yous think learning computer vision and deep learning has to exist time-consuming, overwhelming, and complicated? Or has to involve complex mathematics and equations? Or requires a caste in computer science?
That's non the example.
All yous need to main estimator vision and deep learning is for someone to explain things to yous in simple, intuitive terms. And that's exactly what I exercise. My mission is to alter education and how circuitous Artificial Intelligence topics are taught.
If you lot're serious about learning figurer vision, your side by side stop should be PyImageSearch University, the most comprehensive computer vision, deep learning, and OpenCV course online today. Here yous'll learn how to successfully and confidently use estimator vision to your work, research, and projects. Join me in computer vision mastery.
Within PyImageSearch University you'll discover:
- ✓ 35+ courses on essential calculator vision, deep learning, and OpenCV topics
- ✓ 35+ Certificates of Completion
- ✓ 39h 44m on-demand video
- ✓ Brand new courses released every month , ensuring y'all can keep up with state-of-the-art techniques
- ✓ Pre-configured Jupyter Notebooks in Google Colab
- ✓ Run all lawmaking examples in your spider web browser — works on Windows, macOS, and Linux (no dev environment configuration required!)
- ✓ Access to centralized code repos for all 500+ tutorials on PyImageSearch
- ✓ Easy one-click downloads for code, datasets, pre-trained models, etc.
- ✓ Access on mobile, laptop, desktop, etc.
Click here to join PyImageSearch Academy
Summary
In today's post, we configured our macOS Mojave box for computer vision and deep learning. The primary pieces of software included Python 3.6, OpenCV, TensorFlow, and Keras accompanied by dependencies and installation/compilation tools.
Python 3.7 is non yet officialy supported by TensorFlow so you lot should avoid it at all costs (for the time being).
Instead, we learned how to downgrade from Python iii.7 to Python 3.6 on macOS Mojave and put all of the software into a Python 3.6 virtual surround named dl4cv
.
If you would like to put your newly configured macOS deep learning environs to good utilise, I would highly suggest you accept a look at my new book, Deep Learning for Reckoner Vision with Python.
Regardless if you're new to deep learning or already a seasoned practitioner, the volume has content to assistance y'all accomplish deep learning mastery — take a expect hither.
To be notified when future blog posts are published hither on PyImageSearch (and grab my 17-page Deep Learning and Computer Vision Resource Guide PDF), just enter your e-mail address in the class below!
Join the PyImageSearch Newsletter and Catch My FREE 17-page Resource Guide PDF
Enter your electronic mail address below to join the PyImageSearch Newsletter and download my FREE 17-page Resources Guide PDF on Computer Vision, OpenCV, and Deep Learning.
Source: https://pyimagesearch.com/2019/01/30/macos-mojave-install-tensorflow-and-keras-for-deep-learning/
Posted by: platzlogy2000.blogspot.com
0 Response to "How To Install Keras On Mac"
Post a Comment