Setting up Keras Tensorflow2 on M1 Mac

Oct 21, 2022

I am currently reading Deep Learning with TensorFlow and Keras to get started with Machine Learning/Deep Learning. Here is how I setup a local Keras/Tensorflow 2.0 environment on my M1 Max MacBook Pro running macOS 12.6.

Install miniconda.

1
brew install --cask miniconda

I use zsh with iTerm2 as my terminal so I need to initialize conda with the following command.

1
conda init zsh

This will make it so everytime iTerm2 is opened, the conda base environment will be activated. I prefer to activate my environment manually, so I did the below to deactivate the base environment on launch of iTerm2.

In ~/.zshrc add the following section after the # <<< conda initialize <<< line.

1
2
# Deactivate base conda environment. You can activate an environment using conda activate <<environment-name>>
conda deactivate

We now create an environment named tensorflow where we could run our ML/DL Keras training.

1
conda create -n tensorflow python=3.9

We then switch to the tensorflow environment and install dependencies.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# Activate tensorflow environment
conda activate tensorflow

# Install apple tensorflow dependencies
conda install -c apple tensorflow-deps

# Install tensorflow macos
python -m pip install tensorflow-macos

# Install tensorflow metal plugin
python -m pip install tensorflow-metal

# Install tensorflow readymade datasets
python -m pip install tensorflow-datasets

# Install libjpeg (needed by mathplotlib)
brew install libjpeg

# Install mathplotlib and jupyterlab
conda install -y matplotlib jupyterlab

This may not be necessary for future installs. But I ran into an error with numpy when trying to run my notebook code.

module compiled against API version 0x10 but this version of numpy is 0xf

To correct this I had to run this command in my tensorflow environment.

1
python -m pip install numpy --upgrade

Another warning I ran into.

TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html from .autonotebook import tqdm as notebook_tqdm

To resolve I ran the below in my tensorflow environment.

1
python -m pip install ipywidgets widgetsnbextension pandas-profiling

You may now run jupyter notebook command from the tensorflow environment to start the jupyter notebook environment from the command line. I however prefer using Visual Studio Code and start an environment under vscode as documented below.

  • Install the Jupyter extension under vscode.
  • Load the Command Pallet using (Cmd+Shift+P) and select Jupyter: Select Interpreter to Start Jupyter Server and then select Python 3.9.13 ('tensorflow') or whatever environment you want to use.
  • Load the Command Pallet using (Cmd+Shift+P) and select Create: New Jupyter Notebook
  • You may now run all the Jupyter notebook in vscode. When run the code cell, vscode will start the jupyter server if it is not already started in the selected environment.

Code

1
2
3
4
import tensorflow as tf
print("TensorFlow has access to the following devices:")
for device in tf.config.list_physical_devices():
    print(f"* {device}")

Output

1
2
3
TensorFlow has access to the following devices:
* PhysicalDevice(name='/physical_device:CPU:0', device_type='CPU')
* PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')
How TosProgrammingMachine LearningArtificial IntelligenceMachine LearningDeep LearningTensorFlowKeras

Set up Foscam Floodlight Camera in HomeKit

Setting Visual Studio Code as default text editor on macOS