This project provides a tool to convert Caffe models to Apple's CoreML format, enabling the use of pre-trained Caffe models in iOS applications.
- Convert Caffe models (
.caffemodeland.prototxt) to CoreML models (.mlmodel). - Support for common Caffe layers.
- Easy-to-use command-line interface.
- Python 3.6+
-
Clone the repository:
git clone https://github.com/SakaDream/Caffe2CoreML-Converter.git cd Caffe2CoreML-Converter -
Install the required Python packages:
pip install -r requirements.txt
CoreMLTools 4.1 may not run correctly on Mac Silicon (M1, M1 Pro, M1 Max, M2, etc.). To work around this issue, Mac Silicon users need to install Miniconda 23.1.0 (Python 3.7), create a new conda environment, and then install CoreMLTools and other dependencies within that environment. This Miniconda version has been tested on a MacBook Pro 2021 running macOS 15.2. Follow these steps:
-
Download and install Miniconda 23.1.0 (Python 3.7) from the direct download link.
-
Install Miniconda:
chmod +x Miniconda3-py37_23.1.0-1-MacOSX-x86_64.sh ./Miniconda3-py37_23.1.0-1-MacOSX-x86_64.sh
Follow the prompts during the installation:
- When asked if you want to continue the installation, type
yes. - Press
ENTERto review the license agreement. - Type
yesto accept the license terms. - Press
ENTERto confirm the installation location or specify a different location. - Type
yesto initialize Miniconda3 by runningconda init.
After the installation is complete, close and re-open your current shell for the changes to take effect.
- When asked if you want to continue the installation, type
-
Create a new conda environment:
conda create --name coreml-env python=3.7 conda activate coreml-env
-
Install the required Python packages within the conda environment:
pip install -r requirements.txt
-
Run the Python script normally within the conda environment.
-
Once you are done working with the CoreML environment, you can deactivate the conda environment by running:
conda deactivate
$ python script.py -h
usage: script.py [-h] --caffemodel CAFFEMODEL --prototxt PROTOTXT --labels
LABELS --output OUTPUT
Convert Caffe model to CoreML model.
optional arguments:
-h, --help show this help message and exit
--caffemodel CAFFEMODEL
Path to the Caffe model file.
--prototxt PROTOTXT Path to the Caffe deploy prototxt file.
--labels LABELS Path to the labels file.
--output OUTPUT Path to save the CoreML model.python script.py --caffemodel models/oxford102.caffemodel --prototxt models/deploy.prototxt --labels models/flower-labels.txt --output models/FlowerClassifier.mlmodelYou can also run the converter using Docker.
-
Build the Docker image:
docker build --platform linux/amd64 -t caffe2coreml-converter . -
Run the converter inside the Docker container:
docker run --platform linux/amd64 --rm -v $(pwd):/workdir caffe2coreml-converter \ --caffemodel <path/to/model.caffemodel> \ --prototxt <path/to/deploy.prototxt> \ --labels <path/to/labels.txt> \ --output <path/to/output.mlmodel>
docker run --platform linux/amd64 --rm -v $(pwd):/workdir caffe2coreml-converter \
--caffemodel models/oxford102.caffemodel \
--prototxt models/deploy.prototxt \
--labels models/flower-labels.txt \
--output models/FlowerClassifier.mlmodelContributions are welcome! Please open an issue or submit a pull request.
This project is licensed under the MIT License. See the LICENSE file for details.