Skip to content

Instantly share code, notes, and snippets.

@Makio64
Last active May 9, 2024 14:29
Show Gist options
  • Save Makio64/abe29f93aaf05639a07829084a2910db to your computer and use it in GitHub Desktop.
Save Makio64/abe29f93aaf05639a07829084a2910db to your computer and use it in GitHub Desktop.
Export YoloV8 for TFJS with TensorFlow 2.13.1 compile from source on macOS M3

Export YoloV8 for TFJS with TensorFlow 2.13.1 on macOS M3

The Problem

TensorFlow SavedModel: starting export with tensorflow 2.16.1...
WARNING ⚠️ tensorflow<=2.13.1 is required, but tensorflow==2.16.1 is currently installed https://github.com/ultralytics/ultralytics/issues/5161
  • Compiling yolov8 requires TensorFlow 2.13.1, which is not available via pip install tensorflow==2.13.1.
  • We must compile this tensorflow version from the source, and its a very challenging task.

The Solution (Step-by-Step explain)

1. Install Coreutils

This step resolves the realpath error during tensorflow compilation

brew install coreutils
echo 'export PATH="/opt/homebrew/opt/coreutils/libexec/gnubin:$PATH"' >> ~/.zshrc
source ~/.zshrc
realpath --help  # Verify it uses the GNU version.

2. Install Clang (requires Xcode)

this is need to compile tensorflow

# Install Xcode command Line
xcode-select --install

# Ensure Apple clang is installed
clang --version

3. Install Bazelisk

this is need to compile tensorflow

brew install bazelisk

4. Install Miniconda

This is for the step 6. Follow the quick command line installation from Miniconda's official documentation.

mkdir -p ~/miniconda3
curl -o ~/miniconda3/miniconda.sh https://repo.anaconda.com/miniconda/Miniconda3-latest-MacOSX-arm64.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh
~/miniconda3/bin/conda init bash
~/miniconda3/bin/conda init zsh

5. Clone TensorFlow Source

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow
git checkout r2.13

6. Create a Conda Environment

Conda environment help to avoid conflict with python / pip version and packages

conda create -n tensorflowBuild python=3.11
conda init
conda activate tensorflowBuild
python --version  # Should return Python 3.11.9

7. Install pip dependencies

  • Download or copy requirements.txt ( you can find bellow ) and add it to the tensorflow folder
  • run the command :
pip install -r ./requirements.txt

8. Compile TensorFlow from Source

bazel clean --expunge
./configure

on ./configure step press enter to every question

Lets compile Tensorflow!

run this command, it will take a long time to completed

bazel build //tensorflow/tools/pip_package:build_pip_package

Lets install it

./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
pip install /tmp/tensorflow_pkg/tensorflow-2.13.1-*.whl

Handling Errors

If compilation errors occur, check package versions and resolve conflicts with pip check, then restart the process 7.

pip check

Test Installation, Should show TensorFlow 2.13.1

pip list | grep tensorflow

10. Export YOLO Model to TFJS

yolo export model=yolov8n.pt format=tfjs

Additional Notes

  • Let me know if this step-by-step tutorial helped you!
  • I hope in the futur we dont have to recompile tensorflow2.13.1 as its so long and painful experience.
  • Below is a list of package versions that are confirmed to work with this setup (requirements.txt)

requirements.txt

absl-py==2.1.0
astunparse==1.6.3
cachetools==5.3.3
certifi==2024.2.2
charset-normalizer==3.3.2
chex==0.1.7
cmake==3.29.2
coloredlogs==15.0.1
contourpy==1.2.1
cycler==0.12.1
dm-tree==0.1.8
etils==1.8.0
filelock==3.14.0
flatbuffers==24.3.25
flax==0.6.2
fonttools==4.51.0
fsspec==2024.3.1
gast==0.4.0
google-auth==2.29.0
google-auth-oauthlib==1.0.0
google-pasta==0.2.0
grpcio==1.63.0
h5py==3.11.0
humanfriendly==10.0
idna==3.7
importlib_resources==6.4.0
jax==0.4.16
jaxlib==0.4.16
Jinja2==3.1.4
keras==2.13.1
kiwisolver==1.4.5
libclang==18.1.1
Markdown==3.6
markdown-it-py==3.0.0
MarkupSafe==2.1.5
matplotlib==3.8.4
mdurl==0.1.2
ml-dtypes==0.3.1
mpmath==1.3.0
msgpack==1.0.8
namex==0.0.8
nest-asyncio==1.6.0
networkx==3.3
numpy==1.23.5
oauthlib==3.2.2
onnx==1.16.0
onnx-graphsurgeon==0.5.2
onnx2tf==1.17.5
onnxruntime==1.15.0
onnxsim==0.4.36
opencv-python==4.9.0.80
opt-einsum==3.3.0
optax==0.1.7
optree==0.11.0
orbax-checkpoint==0.5.10
packaging==20.9
pandas==2.2.2
pillow==10.3.0
pip==23.3.1
protobuf==4.25.3
psutil==5.9.8
py-cpuinfo==9.0.0
pyasn1==0.6.0
pyasn1_modules==0.4.0
pybind11==2.12.0
Pygments==2.18.0
pyparsing==3.1.2
python-dateutil==2.9.0.post0
pytz==2024.1
PyYAML==6.0.1
requests==2.31.0
requests-oauthlib==2.0.0
rich==13.7.1
rsa==4.9
scipy==1.13.0
seaborn==0.13.2
setuptools==69.5.1
six==1.16.0
sng4onnx==1.0.2
sympy==1.12
tensorboard==2.13.0
tensorboard-data-server==0.7.2
tensorflow==2.13.1
tensorflow-decision-forests==1.5.0
tensorflow-estimator==2.13.0
tensorflow-hub==0.16.1
tensorflow-io-gcs-filesystem==0.37.0
tensorflow-macos==2.13.1
tensorflowjs==0.0.0
tensorstore==0.1.58
termcolor==2.4.0
tf-keras==2.15.0
tflite-support==0.1.0a1
thop==0.1.1.post2209072238
toolz==0.12.1
torch==2.0.0
torchvision==0.15.1
tqdm==4.66.4
typing_extensions==4.5.0
tzdata==2024.1
ultralytics==8.2.1
urllib3==2.2.1
Werkzeug==3.0.3
wheel==0.43.0
wrapt==1.16.0
wurlitzer==3.1.0
zipp==3.18.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment