hollance / YOLO-CoreML-MPSNNGraph

Tiny YOLO for iOS implemented using CoreML but also using the new MPS graph API.
MIT License
933 stars 252 forks source link

- `graphviz` installation as additional prerequisite step for `YAD2K`; #2

Closed karolkulesza closed 7 years ago

karolkulesza commented 7 years ago

Without it, you get:

ImportError: Failed to import pydot. You must install pydot and graphviz forpydotprintto work.

hollance commented 7 years ago

Did you use pydot-ng or just pydot?

karolkulesza commented 7 years ago

pydot-ng

karolkulesza commented 7 years ago
  • Uncommented saving to *.mlmodel;

Would be nice to actually save it ;-)

hollance commented 7 years ago

OK, thanks for reporting this. I think I'll actually change the instructions to not use the -p flag, in which case you don't need pydot-ng or graphviz.

hollance commented 7 years ago

It turns out you need both pydot-ng and graphviz anyway. :-P

karolkulesza commented 7 years ago

👍

karolkulesza commented 7 years ago

BTW: Did you try to convert any other model, apart from tiny-yolo-voc ?

I've tried with yolo (yolo.cfg and yolo.weights) and the conversion to Keras passes successfully, but during the conversion to .mlmodel I'm currently getting:

Traceback (most recent call last):
  File "coreml.py", line 18, in <module>
    image_scale=1/255.)
  File "/Developer/MachineLearning/darknet/models/coreml/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 273, in convert
    model = _keras.models.load_model(model)
  File "/Developer/MachineLearning/darknet/models/coreml/lib/python2.7/site-packages/keras/models.py", line 142, in load_model
    model = model_from_config(model_config, custom_objects=custom_objects)
  File "/Developer/MachineLearning/darknet/models/coreml/lib/python2.7/site-packages/keras/models.py", line 193, in model_from_config
    return layer_from_config(config, custom_objects=custom_objects)
  File "/Developer/MachineLearning/darknet/models/coreml/lib/python2.7/site-packages/keras/utils/layer_utils.py", line 40, in layer_from_config
    custom_objects=custom_objects)
  File "/Developer/MachineLearning/darknet/models/coreml/lib/python2.7/site-packages/keras/engine/topology.py", line 2582, in from_config
    process_layer(layer_data)
  File "/Developer/MachineLearning/darknet/models/coreml/lib/python2.7/site-packages/keras/engine/topology.py", line 2560, in process_layer
    custom_objects=custom_objects)
  File "/Developer/MachineLearning/darknet/models/coreml/lib/python2.7/site-packages/keras/utils/layer_utils.py", line 40, in layer_from_config
    custom_objects=custom_objects)
  File "/Developer/MachineLearning/darknet/models/coreml/lib/python2.7/site-packages/keras/layers/core.py", line 682, in from_config
    function = func_load(config['function'], globs=globs)
  File "/Developer/MachineLearning/darknet/models/coreml/lib/python2.7/site-packages/keras/utils/generic_utils.py", line 183, in func_load
    code = marshal.loads(code.encode('raw_unicode_escape'))

Regards, KK

hollance commented 7 years ago

No, I haven't tried the full YOLO model yet. I did hear from someone else who also had problems with the conversion.

Note that the error message says something about custom_objects. Maybe the full YOLO model has a layer type that isn't supported by Core ML?

karolkulesza commented 7 years ago

Ok, after some non-trivial amount of debugging I've found the root cause of this: During the execution of "coreml.py", when deserializing the keras object there is an attempt to deserialize lambda function, which fails most probably due to the fact that it was serialized with python3 (and the attempt to deserialize is done with python2, as coremltools supports only python2). I've applied a temporary workaround to invoke the function directly, without deserialization phase.

However, as life is not that simple, another issue I've faced during the conversion is:

Keras layer '<class 'keras.layers.core.Lambda'>' not supported.

Seems like I'll have to retrain my YOLO model, not to contain the "Lambda" function after the conversion to Keras ... (If my understanding is correct).

hollance commented 7 years ago

That sounds correct. The Lambda thing is used to create your own layer types. Core ML only supports a fixed number of layer types -- you can't add your own.

karolkulesza commented 7 years ago

Ok, I've finally managed to train my own model in YOLO, convert it to Keras 2.0, then convert it CoreML and test it successfully on device with iOS11 beta. There were few challenges down the road, but it was worth it!

Thanks Matthijs for this repo and your whole blog, which helped me a lot! Keep up the good work 👍 ;-)