Skip to content

haltersweb/Mac-Tensorflow-ObjectDetection-LabelImg-installation

Repository files navigation

Install TensorFlow 2, Object Detection, and LabelImg on Mac OS

This is how I set up my Mac to do ML object detection.

There are certain steps that are different between PC and Mac. When I tried to follow instructions by folks who were using PCs for setting up TensorFlow 2 (TF2), Object Detection, etc. I would end up with errors.

I hope this will help those of you who who are on Mac OS and are struggling to set up your learning environment.

  • I am using a 2017 Macbook Pro with Big Sur OS.
  • I have Python 3.8.10 installed (at time of writing).
  • I am using two package managers for this: PIP and Homebrew.
  • I am not using Conda.

Assumptions

  • You are familiar with Terminal
  • You already have Python 3.8 installed (as of this writing, TF is not compatible with 3.9)
  • You will be using a Python Virtual Environment
  • You are familiar with the Jupyter Notebook IDE
  • You have the latest versions of PIP and Homebrew
  • You have the latest version of Xcode

Coursera has an excellent instruction video by the University of Michigan on installing Homebrew, Python3, spinning up a Python Virtual Environment, and installing Jupyter Notebook.

Step 0. Setting up different Python versions with pyenv (in case you don't have python installed yet)

pyenv is used to install different major and minor releases of Python.

(Instructions: https://youtu.be/-5vd5GEpF-w) (GitHub for pyenv: https://github.com/pyenv/pyenv)

Install pyenv with Homebrew:

  1. Update Homebrew. Brew update usually takes a while. Go get some coffee while you wait.
brew update
  1. Install pyenv
brew install pyenv

(If you just want to update pyenv, update brew first, and then type brew upgrade pyenv)

Configure terminal so that it always loads pyenv whenever we start a new terminal instance

  1. Find out what file changes you need to make for your particular MacOS by typing:
pyenv init
  1. Make any stipulated changes using command-line text editor such as Vim or Nano

For example, I am on BigSur OS. pyenv init instructed me to add 3 lines to .profile and to .zprofile and then to append eval "$(pyenv init -)" into zshrc (the zsh config file). Earlier OS may use bin instead of zsh.

(BTW, later if you don't want pyenv to manage its Python versions, comment out the eval line in the .zshrc file. That way python --version = 2.7 (system version) and python3 = whatever version you may have installed without pyenv)

Installing XZ for Mac OS

This only needs to be done once on your OS. If already done, skip this part about installing XZ.

XZ is not pre-installed on Mac OS. Therefore, if you are using pyenv to manage multiple Python versions you must install XZ Utils on your machine before installing a Python version.

If you have the desired python version already installed, you will need to uninstall it before you install XZ. Then once XZ is installed, you can reinstall the python version. If using pyenv the uninstall command would be:

pyenv uninstall <desired-python-version>
  1. Install XZ with:
brew install xz

Install a python version

  1. To see a full list of every Python version you can install by typing:
pyenv install --list

Besides regular Python versions you can even install anaconda versions and others.

  1. Install version(s). For example let's install 3.8.9 and 3.9.4:
pyenv install 3.9.4
pyenv install 3.8.9
  1. View the list of installed versions
pyenv versions
  1. Choose your default version by modifying the ~/.pyenv/version text file (for example 3.8.9)
echo 3.8.9 > ~/.pyenv/version

Step 1. Set up your Python virtual environment with venv

These instructions assume you already have Python 3.8 installed. (If you don't have Python 3.8 installed here is a video that tells you how to install particular Python versions using pyenv.)

  1. In terminal, create a directory to hold your Python virtual environment(s) and then navigate to it. We'll call ours "projects".
mkdir projects
cd projects
  1. Create a virtual environment (this will also create a new directory). We'll call it "funEnv". You will also be sipulating the Python version to use (you can check it with --version). I will be using Python 3.8.10. (as of this writing, TF is not compatible with 3.9)
/your/python/file/path/python -m venv funEnv

NOTE: if 3.8.10 is the default version on your system, you could just use the aliases python or python3.8 instead of the python file path

  1. Activate your virtual python environment. We'll call ours "funEnv"
source funEnv/bin/activate

When you see the name of the environment in parentheses at the front of the prompt you know it is active. For example:

(funEnv) [haltersweb]projects$
  1. Navigate to your virtual environment
cd funEnv
  1. IMPORTANT: make sure to update PIP and Homebrew before using them. Brew update usually takes a while. Go get some coffee while you wait.
pip install --upgrade pip
brew update

FYI: To deactivate your virtual Python environment just type:

deactivate

IMPORTANT: Is your venv (Python virtual environment) inactive?

Are you having trouble with pip and python or defaulting to older versions of them? Are you unable to start Jupyter Notebook or run TensorFlow?

That's usually a sign that your venv (Python virtual environment) is inactive.

Even opening a new terminal window will default to a deactivated venv state. You need to activate venv on each terminal window if you are working in venv. You will know it's active if the first thing you see in the terminal prompt is the name of the venv directory in parentheses:

(funEnv) [haltersweb]projects$

Step 2. Install Jupyter Notebook IDE

  1. Ensure the virtual environment is active.
  2. Navigate to the virtual environment folder.
  3. You will use pip install notebook. This is the up-to-date Jupyter Notebook. DO NOT USE pip install jupyter.
pip install notebook

Starting Jupyter Notebook

  1. Ensure the virtual environment is active
  2. Navigate to the virtual environment folder
  3. Type jupyter notebook at the terminal prompt.
jupyter notebook
  1. Copy the url from terminal and paste it into your browser. This runs an instance of Jupyter Notebook on localhost.

FYI: To end the Jupyter session use CTRL-C and confirm.

Step 3. Installing TensorFlow 2 with pip (on virtual environment)

For reference: https://www.tensorflow.org/install/pip and https://www.pyimagesearch.com/2019/12/09/how-to-install-tensorflow-2-0-on-macos/

  1. Ensure the virtual environment is active
  2. Navigate to the virtual environment folder
  3. Update pip
pip install --upgrade pip
  1. Install TensorFlow 2
pip install --upgrade tensorflow
  1. Confirm the install was successful
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

something like this should be returned: tf.Tensor(-1402.6809, shape=(), dtype=float32)

install necessary packages

No need to install numpy and keras since they are installed when installing TF2

  1. Make sure the latest Xcode is installed (thru the App Store)
  2. Install image processing libraries
pip install opencv-contrib-python
pip install scikit-image
pip install pillow
pip install imutils
  1. Install ML and support libraries
pip install scikit-learn
pip install matplotlib
pip install progressbar2
pip install beautifulsoup4
pip install pandas

Step 4. Installing Object Detection API with pip and brew

For reference: https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2.md and https://www.tensorflow.org/hub/build_from_source#install_protoc

  1. Ensure the virtual environment is active
  2. Navigate to the virtual environment folder
  3. Make sure Homebrew is also up to date
brew update
  1. Clone the Tensorflow Model Garden. This installs a models directory in the virtual environment root directory.
git clone https://github.com/tensorflow/models.git
  1. Navigate to the models research directory
cd models/research
  1. Install protobuf with Homebrew
brew install protobuf
  1. Compile protos using protoc
protoc object_detection/protos/*.proto --python_out=.
  1. Copy setup file from object detection packages into current directory
cp object_detection/packages/tf2/setup.py .
  1. Install all dependencies neede for our object detection library (ignore warnings)
python -m pip install --use-feature=2020-resolver .
  1. Confirm installation was successful
python object_detection/builders/model_builder_tf2_test.py

It should result in something like:

Ran 21 tests in 27.946s
OK (skipped=1)

Step 5. Installing LabelImg

Use LabelImg to prepare images you want to use for training data. Watch Nicholas Renotte's "Real Time Face Mask Detection" video to see how he uses LabelImg to prep his training data.

I installed LabelImg from tzutalin's version of LabelImg on GitHub since Douglas Meneghetti mentioned it's better than the version you can install with pip.

I am following the Mac instructions from tzutalin's LabelImg GitHub page for "Python 3 Virtualenv"

download LabelImg from GitHub

  1. Ensure the virtual environment is active
  2. Navigate to the virtual environment folder
  3. Clone Tzutalin's LabelImg version
git clone https://github.com/tzutalin/labelImg.git

build the application with pipenv

  1. Navigate to the LabelImg directory
cd labelImg
  1. Install pipenv for better dependency management
pip install pipenv
  1. Build labelImg
pipenv run pip install pyqt5==5.12.1 lxml
pipenv run make qt5py3

run labelImg

  1. Start the LabelImg application
python labelImg.py

Putting it all together

If you are new to TensorFlow, or even machine learning in general take a look at the following resources:

If you are ready to delve into object detection with TensorFlow, check out Nicholas Renotte's YouTube channel for excellent tutorials.

Here are some of my favorites from his channel:

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published