Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support exports for Apple's new mlmodel format in Keras 2 #7007

Closed
gsabran opened this issue Jun 15, 2017 · 16 comments
Closed

Support exports for Apple's new mlmodel format in Keras 2 #7007

gsabran opened this issue Jun 15, 2017 · 16 comments

Comments

@gsabran
Copy link

gsabran commented Jun 15, 2017

Apple's recently announced support for ML models in iOS11 through a specific format. (WWC17 video, docs)
They provide an opensource conversion python package called coremltools that currently only support conversion from Keras 1.2.2 models. (see in the version of the package I hosted). They seem to expect the open source community to implement conversion from other formats...

Is there any plan from Keras to do so? Else what do you suggest? The code for Keras 1.2.2 is roughly 2000 lines. I guess there's probably a bunch of things that can be reused to support conversion from Keras 2, but I don't have the expertise around Keras's model saving format to make a PR :(

I tried to load a model saved by Keras 2 with coremltools and that failed.

I also opened a question on SO and hosted the package code on github to make the discussion easier: https://github.com/gsabran/coremltools

Extending the save API to something like save(self, filepath, overwrite=True, include_optimizer=True, format='keras' or 'mlmodel') would be great.

@0x00b1
Copy link

0x00b1 commented Jun 15, 2017

Hi, @gsabran!

I spent a few minutes reviewing their code. It appears that it would not take much effort to update their tool to use Keras 2 models. I saw two major issues: naming changes and parameter order changes. A guide to updating to Keras 2 is available from the changelog.

In addition, because Keras currently provides models with weights in a popular interchange format (HDF5) and models without weights in additional two popular interchange formats (JSON and YAML) it seems like the burden should be on the external library (especially when the external library transforms a model available in an easily read format to an unreadable format).

@0x00b1
Copy link

0x00b1 commented Jun 15, 2017

(As an aside, it is noteworthy that CoreML is using the TensorFlow backend but doesn't mention TensorFlow support on their website. I feel like this would be a feature you'd want to emphasize!)

@gsabran
Copy link
Author

gsabran commented Jun 16, 2017

Yes they don't have direct support for TF... I 100% agree that the burden should be on Apple and I'm quite disappointed to not see this in what has been released, especially as you've mentioned since Keras supports standard formats.

Now, assuming that they won't do it soon (which they actually might since they've done a first step by supporting a subset of popular frameworks and it's still early in CoreML days) I'm looking for the best way forward. I'll have a look at the changelogs and see if I can get a good grasp on what changes are required.

@gsabran
Copy link
Author

gsabran commented Jun 16, 2017

Actually they might be working on it: https://forums.developer.apple.com/thread/79505

@karolkulesza
Copy link

Keras 2.0 support is already there (released yesterday), with coremltools v.0.4.0

Also, refer to newest comments on Apple Developer Forums

@gsabran gsabran closed this as completed Jun 29, 2017
@Fenoms
Copy link

Fenoms commented Jul 16, 2017

import coremltools
coreml_model = coremltools.converters.keras.convert('multi_digits_keras.h5')
coreml_model.save('mnist_cnn_keras.mlmodel')

it returns: AttributeError: 'module' object has no attribute 'convert',
Could you give me some advices?

@jbmaxwell
Copy link

same problem here as above: AttributeError: 'module' object has no attribute 'convert'

@JacopoMangiavacchi
Copy link

I've created a very simple Sequential Linear Regression model and my coremltools export is failing with this error message: 'module' object has no attribute 'mobilenet'

@Fenoms @jbmaxwell have you solved your issues ?

@eelretep
Copy link

eelretep commented Sep 4, 2017

@JacopoMangiavacchi I had the same problem. I fixed by installing the latest coremltools version with the latest supported keras version. 0.6.3 coremltools with keras 2.0.6 worked for me

@kinergy
Copy link

kinergy commented Sep 23, 2017

I'm having the same problem. Python 2.7.13, Keras 2.0.8 and coremltools 0.6.3 are installed:

>>> coreml_model = coremltools.converters.keras.convert('lenetdo0123e5.h5')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'convert'

any ideas?

@tatsuya-ogawa
Copy link

tatsuya-ogawa commented Sep 27, 2017

@kinergy I had the same problem. I fixed by installing the latest tensorflow version 1.3.0.
0.6.3 coremltools with keras 2.0.8 worked for me.

@valentinradu
Copy link

@tatsuya-ogawa Thanks mate, that fixed for me. Apparently I didn't had tensorflow installed for python2.7.

@mynameisvinn
Copy link

mynameisvinn commented Jan 5, 2018

coremltools works when keras uses tensorflow, not theano, as its backend. you can change keras' default backend at $HOME/.keras/keras.json, and changing to "backend": "tensorflow".

@zoecarver
Copy link

zoecarver commented Sep 19, 2018

For me downgrading keras to 2.1.6 fixed it.

@Shoshin23
Copy link

I had to install Keras, Tensorflow again and that error seemed to disappear. In my case it happened because one of the components(tensorflow) was missing.

@ugm2
Copy link

ugm2 commented Mar 7, 2019

Actually, it is because Coremltools only works with Python2, so you need to install Keras and Tensorflow on python2 too by typing:
python -m pip install -U keras
python -m pip install -U tensorflow

I hope this helps, at least it worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests