DeepSpeech icon indicating copy to clipboard operation
DeepSpeech copied to clipboard

Make model convertible by CoreML

Open MatthewWaller opened this issue 8 years ago • 74 comments

It would be wonderful if DeepSpeech models could be converted to CoreML, for offline use in apps. Here is documentation to do just that. https://developer.apple.com/documentation/coreml/converting_trained_models_to_core_ml Thanks!

MatthewWaller avatar Jun 20 '17 22:06 MatthewWaller

@MatthewWaller It doesn't appear as if TenforFlow is supported.

kdavis-mozilla avatar Jul 01 '17 15:07 kdavis-mozilla

Hmmm, so I would probably need to write a custom conversion tool, like it says at the bottom of the page, I guess.

MatthewWaller avatar Jul 01 '17 15:07 MatthewWaller

@MatthewWaller I'd guess so. Which seems like a large outlay in time and knowledge.

kdavis-mozilla avatar Jul 01 '17 15:07 kdavis-mozilla

Just as an update, I'm examining this Keras model converter script, which comes from Apple's own Python CoreML tools. Could be a good precedent for defining the needed layers. You're right though @kdavis-mozilla, looks like a large project.

MatthewWaller avatar Nov 27 '17 18:11 MatthewWaller

@MatthewWaller Thanks for the update!

kdavis-mozilla avatar Nov 28 '17 09:11 kdavis-mozilla

@MatthewWaller Have you been able to make any progress ?

lissyx avatar Feb 01 '18 16:02 lissyx

I think it might be possible to convert with a third party tool. I haven’t written the python conversion scripts myself, but this could be useful (https://github.com/Microsoft/MMdnn/blob/master/README.md). But that’s only half the battle. Then I need to find out how to preprocess the audio, so I’m trying to find out how to get MFCC in Swift. One developer used a C library to do this in iOS, so that might be the way to go.

MatthewWaller avatar Feb 01 '18 16:02 MatthewWaller

@MatthewWaller I lack context here, but we have MFCC computation in C already, can't you leverage that?

lissyx avatar Feb 01 '18 17:02 lissyx

If I want to use any C libraries I have to port them over to objective c or swift to use them in iOS or macOS. And that’s something I haven’t done yet, and I would prefer to do the calculations all in swift, for longevity sake

MatthewWaller avatar Feb 01 '18 17:02 MatthewWaller

@MatthewWaller I came accross https://github.com/tf-coreml/tf-coreml while looking at some tensorflow lite stuff, isn't it already addressing what you want to do?

lissyx avatar Feb 02 '18 10:02 lissyx

It does! I hadn’t seen that one. Well, hopefully we just need to get the MFCC one way or another. I’ve got a couple of projects in the hopper before I get back o this one, but that’s exciting!

MatthewWaller avatar Feb 02 '18 12:02 MatthewWaller

@lissyx I managed to feed mfcc into a core data model, but I'm not sure where to go to implement the link you sent to convert to coreml, specifically, I'm not sure where to find a list of output tensor names present in the TF graph, (the README.md gives an example of output_feature_names = ['softmax:0'])

Any ideas? Would welcome your help as well @kdavis-mozilla !

MatthewWaller avatar Feb 04 '18 16:02 MatthewWaller

Hm I remember documenting that to someone else needing to access some intermediate tensor, on discourse. You should have a look there, I cannot search for it for the moment, I'll try and find it tomorrow if you don't find :-)

lissyx avatar Feb 04 '18 18:02 lissyx

@MatthewWaller Any news on that ? The upcoming #1463 might benefit from such support

lissyx avatar Jul 25 '18 11:07 lissyx

@lissyx unfortunately I haven't been able to convert to CoreML. The https://github.com/tf-coreml/tf-coreml, which Apple also recommends officially, cannot handle cycles. I tried and got the error, and as a limitation is states: "TF graph must be cycle free (cycles are generally created due to control flow ops like if, while, map, etc.)"

Not sure how to get around this at present. You can see my issue here: https://github.com/tf-coreml/tf-coreml/issues/124

The author states: "I think the simplest way to deal with such graphs for now is to abstract the weight matrices and bias vectors from pre-trained TF. And then use them to build a CoreML model directly using the neural network builder API provided by coremltools." But I'm not sure how to practically go about that.

MatthewWaller avatar Jul 26 '18 04:07 MatthewWaller

@MatthewWaller Did you try CoreML on the PR or on master?

I think some, maybe all, cycles should be removed in the PR.

kdavis-mozilla avatar Jul 26 '18 07:07 kdavis-mozilla

I tried an earlier version on master. Is there a pre-trained model I could use? I see an alpha in the release from 3 days ago. Would that work?

MatthewWaller avatar Jul 27 '18 02:07 MatthewWaller

@reuben Can you give @MatthewWaller a preliminary model for the PR to test CoreML?

kdavis-mozilla avatar Jul 27 '18 05:07 kdavis-mozilla

@MatthewWaller The alpha release is only for the inference binaries, so far, it does not bundle any model change.

lissyx avatar Jul 27 '18 06:07 lissyx

@MatthewWaller a preliminary model can be found here: https://github.com/reuben/DeepSpeech/releases/tag/v0.0.1-alpha

reuben avatar Jul 27 '18 16:07 reuben

@MatthewWaller Were you ever able to find the output_feature_names?

wshamp avatar Jul 31 '18 23:07 wshamp

@wshamp I was. I found them to be 'logits:0'. As an update overall, I got the model, but I'm stumped at FailedPreconditionError. Here is the issue I filed with tf-coreml. The full stack trace and my full code for converting is there so far. I haven't heard back yet, but anyone else can troubleshoot as well :)

MatthewWaller avatar Aug 01 '18 00:08 MatthewWaller

Hmm my quick google that error seems to indicate an issue with the graph initializing variables not the converter. I hit the same error.

wshamp avatar Aug 01 '18 07:08 wshamp

@MatthewWaller The branch stores the decoder state in the graph in the variables previous_state_c and previous_state_h. It's a convenient place to store this state info.

As far as I understand, @reuben correct me if I'm mistaken, in exporting[1] the graph the previous_state_c and previous_state_h should be removed[2] or at least not included.

Maybe the model @reuben provided mistakenly included previous_state_c and previous_state_h?

kdavis-mozilla avatar Aug 01 '18 07:08 kdavis-mozilla

That blacklist doesn't remove previous_state_{c,h}, but rather makes the freezing process ignore them, since I want them to be variables (not constants) in the final exported graph.

The idea is that before you start feeding audio features and fetching the logits tensor, you have to run the initialize_state op (see the create_inference_graph function in DeepSpeech.py[1]).

In our C++ code we do it inside DS_SetupStream (deepspeech.cc[2]).

[1] https://github.com/mozilla/DeepSpeech/blob/7b873365f8bfffe2ea84dcd34058b537e9095765/DeepSpeech.py#L1718-L1756 [2] https://github.com/mozilla/DeepSpeech/blob/7b873365f8bfffe2ea84dcd34058b537e9095765/native_client/deepspeech.cc#L567

reuben avatar Aug 01 '18 17:08 reuben

Some other notes: the graph in that URL uses LSTMBlockFusedCell, which is probably not supported by tf-coreml, but the weights are compatible with a normal LSTMCell, so with a bit of massaging on the saver when importing, you can use a static_rnn + LSTMCell.

If you can't workaround the previous_state_{c,h} thing, an alternative is fetching the state and feeding it back every time, eliminating the need for the variable.

static_rnn uses tf.cond OPs when you specify the sequence lengths. If tf.cond OPs are not supported by CoreML, you could try not passing sequence lengths to the RNN. It'll degrade the accuracy, but maybe only by a bit.

Let me know if you run into any other issues.

reuben avatar Aug 01 '18 18:08 reuben

@MatthewWaller We now have TF Lite support, can it be helpful?

lissyx avatar Dec 14 '18 10:12 lissyx

For sure @lissyx ! Here is the official Google page about being able to convert Tensorflow Lite to CoreML.

MatthewWaller avatar Dec 31 '18 00:12 MatthewWaller

@MatthewWaller I think you forgot to add the link.

kdavis-mozilla avatar Dec 31 '18 10:12 kdavis-mozilla

Oops, Yep. Here it is @lissyx and @kdavis-mozilla https://developers.googleblog.com/2017/12/announcing-core-ml-support.html

MatthewWaller avatar Dec 31 '18 17:12 MatthewWaller