How to convert Keras model to CoreML package on MacOS and use it.
0. Precondition
Currently (2026. Nov), to install tensorflow on the MacOS, it needs Python 3.12. Higher version returns Errors.
Pyenv : Version manager, it helps to choose Python version from among several versions.
> brew install pyenv
> echo 'eval "$(pyenv init -)" ~/.zshrc
> source ~/.zshrc
Install Python 3.12 globally not locally
> pyenv install 3.12
> pyenv global 3.12
1. Install Tensorflow
> python3 -m pip install --upgrade pip
> python3 -m pip install tensorflow
2. Write Python code to get a model (fashion_mnist)
Write python codes (fashion_mnist_download.py) with gist link
run
> python fashion_mnist_download.py
"fashion_mnist.keras" file will be generated.
3. Install CoreMLTools
> pip install coremltools
4. Convert
> python convert.py
Output file: MyFashion.mlpackage
5. Import mlpackage file into the Xcode project
Drag & Drop mlpackage file in the left project view.

Xcode automatically generate MyFashion.swift
Keep updating...
No comments:
Post a Comment