question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`ValueError: Keras layer '<class 'keras_resnet.layers._batch_normalization.BatchNormalization'>' not supported.` when trying to convert to coreml

See original GitHub issue

I learned a model with keras-retinanet which seems to work pretty well, now I would like to convert it to a coreml model to use it in my iOS app:

import coremltools
coreml_model = coremltools.converters.keras.convert(model)
coreml_model.save('my_model.mlmodel')

Unfortunately I get the following error:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-7-ba230c07a72c> in <module>()
      1 import coremltools
----> 2 coreml_model = coremltools.converters.keras.convert(model)
      3 # Saving the Core ML model to a file.
      4 coreml_model.save('my_model.mlmodel')

/home/jonas/projects/keras/keras-env/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.pyc in convert(model, input_names, output_names, image_input_names, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, model_precision, predicted_probabilities_output, add_custom_layers, custom_conversion_functions)
    743                       predicted_probabilities_output,
    744                       add_custom_layers,
--> 745                       custom_conversion_functions=custom_conversion_functions)
    746 
    747     return _MLModel(spec)

/home/jonas/projects/keras/keras-env/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras_converter.pyc in convertToSpec(model, input_names, output_names, image_input_names, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, model_precision, predicted_probabilities_output, add_custom_layers, custom_conversion_functions, custom_objects)
    541                                            add_custom_layers=add_custom_layers,
    542                                            custom_conversion_functions=custom_conversion_functions,
--> 543                                            custom_objects=custom_objects)
    544     else:
    545         raise RuntimeError(

/home/jonas/projects/keras/keras-env/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras2_converter.pyc in _convert(model, input_names, output_names, image_input_names, is_bgr, red_bias, green_bias, blue_bias, gray_bias, image_scale, class_labels, predicted_feature_name, predicted_probabilities_output, add_custom_layers, custom_conversion_functions, custom_objects)
    185 
    186     # Check valid versions
--> 187     _check_unsupported_layers(model, add_custom_layers)
    188 
    189     # Build network graph to represent Keras model

/home/jonas/projects/keras/keras-env/local/lib/python2.7/site-packages/coremltools/converters/keras/_keras2_converter.pyc in _check_unsupported_layers(model, add_custom_layers)
     98         else:
     99             if type(layer) not in _KERAS_LAYER_REGISTRY:
--> 100                 raise ValueError("Keras layer '%s' not supported. " % str(type(layer)))
    101             if isinstance(layer, _keras.layers.wrappers.TimeDistributed):
    102                 if type(layer.layer) not in _KERAS_LAYER_REGISTRY:

ValueError: Keras layer '<class 'keras_resnet.layers._batch_normalization.BatchNormalization'>' not supported. 

I’m confused by that batchnormalization as i didn’t find that string anywhere in this repo…

Is there a way to convert it to coreml and/or use it in an Android app? I searched for tutorials but didn’t find any unfortunately.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
mxvscommented, Feb 14, 2018

Just want to add the following: I’ve looked into this before, you need to re-implement all custom layers used by keras_resnet and keras-retinenet in Swift for this to work, this is possible since the latest version of coreml but it will be a lot of work to port everything over. Also I have my doubts if the Retinanet architecture will fit on a mobile device.

0reactions
jegarciancommented, Jan 15, 2020

I would also love to see RetinaNet on iOS. If someone manages to convert it a .mlmodel (or find it somewhere in the web), I would love to test the performance on an iPhone 11 Pro.

I was not able to convert it or adapt it. But in case it helps, I ended up using MobileNet and tflite instead. This tutorial was quite helpful.

https://towardsdatascience.com/deeppicar-part-6-963334b2abe0

Read more comments on GitHub >

github_iconTop Results From Across the Web

`ValueError: Keras layer '<class 'keras_resnet.layers ... - GitHub
ValueError: Keras layer '<class 'keras_resnet.layers._batch_normalization.BatchNormalization'>' not supported. when trying to convert to coreml #139.
Read more >
tf.keras model to coreml model, BatchNormalization not ...
Yet, I got the error : ValueError: Keras layer '<class 'keras.layers.normalization_v2.BatchNormalization'>' not supported. when I ran :
Read more >
Advanced Tips for Core ML - Heartbeat
Because Apple's coremltools library does not support keras-contrib, we'll end up with the following error when we try to convert our model: ValueError:...
Read more >
Batch Normalization in practice: an example with Keras and ...
Batch normalization is a technique for training very deep neural networks that standardizes the inputs to a layer for each mini-batch.
Read more >
Working with preprocessing layers | TensorFlow Core
With Keras preprocessing layers, you can build and export models that are truly end-to-end: models that accept raw images or raw structured data ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found