keras -> mlmodel: coreml object has no attribute 'convert'

Viewed 3681

I am trying to convert my keras model into mlmodel using coreml. However, it is saying that coremltools module has no attribute 'convert'.

AttributeError: 'module' object has no attribute 'convert'

My coremltools, keras, tensorflow(tensorflow-gpu) modules are all up to date. I am also using python 2.7.10.

I've used windows and mac, in which, neither worked. However, caffe.convert is working using a caffe model.

Code:

coreml_model = coremltools.converters.keras.convert(MODEL_PATH)

As per the documentation, I expected the converters.keras.convert method to be available in coremltools. Documentation: https://apple.github.io/coremltools/generated/coremltools.converters.keras.convert.html

Please help, thanks in advance!

Edit:

import coremltools
# from keras.models import load_model
import keras
import sys
from keras.applications import MobileNet
from keras.utils.generic_utils import CustomObjectScope

with CustomObjectScope({'relu6': keras.applications.MobileNet.relu6, 'DepthwiseConv2D': keras.applications.mobilenet.DepthwiseConv2D}):
    model = load_model('weights.hdf5')

MODEL_PATH = "data/model_wide_cifar-10_fruits_model.h5"

def main():
    """ Takes in keras model and convert to .mlmodel"""
    print(sys.version)

    # Load in keras model.
    # model = load_model(MODEL_PATH)

    # load labels
    labels=[]
    label_handler = open("fruit-labels.txt", 'r')
    for label in label_handler:
        labels.append(label.rstrip())
    label_handler.close()
    print("[INFO] Labels: {0}".format(labels))

    # Convert to .mlmodel
    coreml_model = coremltools.converters.keras.convert(
        model=MODEL_PATH,
        input_names="image",
        output_names="image",
        class_labels=labels)

    labels = 'fruit-labels.txt'

    # Save .mlmodel
    coreml_model.utils.save_spec('fruitclassifier.mlmodel')
2 Answers

The solution is to use virtualenv. Follow the instructions from the coremltools README:

Installation

We recommend using virtualenv to use, install, or build coremltools. Be sure to install virtualenv using your system pip.

pip install virtualenv

The method for installing coremltools follows the standard python package installation steps. To create a Python virtual environment called pythonenv follow these steps:

# Create a folder for virtualenv
mkdir virtualenvs
cd virtualenvs

# Create a Python virtual environment for your Core ML project
virtualenv coremltools

To activate your new virtual environment and install coremltools in this environment, follow these steps:

# Active your virtual environment
source coremltools/bin/activate


# Install coremltools in the new virtual environment, pythonenv
pip install --upgrade pip
pip install -U coremltools==3.0b5

Install keras and tensorflow

pip install keras tensorflow

Now make sure it works. With the coremltools environment activated, run

>>> python
Python 3.7.4 (v3.7.4:e09359112e, Sep 5 2019, 14:54:52)
>>> import coremltools
>>> coremltools.converters.keras.convert()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: convert() missing 1 required positional argument: 'model'

coremltools documentation

Credit to this guys issue: https://github.com/apple/coremltools/issues/440

Communist Hacker's answer does not work for my current setup:

  • tensorflow 2.4.1
  • coremltools 4.1
  • Python 3.8.7

However, after reviewing the documentation for coremltools here, I was able to fix it by removing keras from the function and the call now works:

import coremltools

coreml_model = coremltools.converters.convert(model, 
                                              input_names="inputname", 
                                              output_names="outputname")

Running the above command now produces this in my Jupyter notebook:

Running TensorFlow Graph Passes: 100%|██████████| 5/5 [00:00<00:00, 37.53 passes/s]
Converting Frontend ==> MIL Ops: 100%|██████████| 6/6 [00:00<00:00, 5764.05 ops/s]
Running MIL optimization passes: 100%|██████████| 17/17 [00:00<00:00, 5633.05 passes/s]
Translating MIL ==> MLModel Ops: 100%|██████████| 3/3 [00:00<00:00, 6864.65 ops/s]

Note - I am a complete noob with this, so I probably described things incorrectly.

Related