keras-team / keras

Deep Learning for humans
http://keras.io/
Apache License 2.0
61.85k stars 19.44k forks source link

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

Closed gsabran closed 7 years ago

gsabran commented 7 years ago

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 commented 7 years ago

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 commented 7 years ago

(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 commented 7 years ago

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 commented 7 years ago

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

karolkulesza commented 7 years ago

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

Also, refer to newest comments on Apple Developer Forums

Fenoms commented 7 years ago

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 commented 7 years ago

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

JacopoMangiavacchi commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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 commented 7 years ago

@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 commented 7 years ago

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

mynameisvinn commented 6 years ago

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 commented 6 years ago

For me downgrading keras to 2.1.6 fixed it.

Shoshin23 commented 5 years ago

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 commented 5 years ago

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.