hollance / YOLO-CoreML-MPSNNGraph

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

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

Closed seantempesta closed 6 years ago

seantempesta commented 6 years ago

This is the full error:

Traceback (most recent call last):
  File "coreml-stopsign.py", line 10, in <module>
    output_names='grid')
  File "/home/sean/.local/lib/python3.6/site-packages/coremltools/converters/keras/_keras_converter.py", line 505, in convert
    predicted_probabilities_output = predicted_probabilities_output)
  File "/home/sean/.local/lib/python3.6/site-packages/coremltools/converters/keras/_keras2_converter.py", line 166, in _convert
    _check_unsupported_layers(model)
  File "/home/sean/.local/lib/python3.6/site-packages/coremltools/converters/keras/_keras2_converter.py", line 89, in _check_unsupported_layers
    "Keras layer '%s' not supported. " % str(type(layer)))
ValueError: Keras layer '<class 'keras.layers.core.Lambda'>' not supported.

It appears that the YAD2K conversion between Darknet and Keras adds a Lambda layer, which is unsupported in Apple's coremltools. How did you work around this?

The lambda layer seems to be added here (yad2k.py):

elif section.startswith('reorg'):
            block_size = int(cfg_parser[section]['stride'])
            assert block_size == 2, 'Only reorg with stride 2 supported.'
            all_layers.append(
                Lambda(
                    space_to_depth_x2,
                    output_shape=space_to_depth_x2_output_shape,
                    name='space_to_depth_x2')(prev_layer))
            prev_layer = all_layers[-1]
hollance commented 6 years ago

You can't work around it, unless it is the first or last layer in the network (in which case you need to remove it and provide the layer's functionality in Swift). Core ML does not support lambda layers.

seantempesta commented 6 years ago

It appears to be the last layer. Any chance you can explain what it's doing and I can try to write the last layer in swift?

oishi89 commented 6 years ago

@seantempesta take a look into my demo https://www.youtube.com/watch?v=p3-3kN_fIz0 I was using FullYolo v2 and the thing I did for working around is remove the reorg in yolo.cfg and retrained your model.

seantempesta commented 6 years ago

Removing the 'reorg' layer did the trick. Thanks @oishi89!

clockwiser commented 6 years ago

After running swiftenv install, curl error happens.


curl: (60) SSL certificate problem: self signed certificate in certificate chain More details here: https://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle" of Certificate Authority (CA) public keys (CA certs). If the default bundle file isn't adequate, you can specify an alternate file using the --cacert option. If this HTTPS server uses a certificate signed by a CA represented in the bundle, the certificate verification probably failed due to a problem with the certificate (it might be expired, or the name might not match the domain name in the URL). If you'd like to turn off curl's verification of the certificate, use the -k (or --insecure) option.