apple / coremltools

Core ML tools contain supporting tools for Core ML model conversion, editing, and validation.
https://coremltools.readme.io
BSD 3-Clause "New" or "Revised" License
4.43k stars 641 forks source link

Keras layer '<class 'keras.layers.normalization_v2.BatchNormalization'>' not supported. #1201

Closed Ludotrico closed 2 years ago

Ludotrico commented 3 years ago

I am trying to convert MobileNetV2, which uses batch normalization but I run into this error. Is there a workaround?

File "/Users/ludovico/Documents/Documents2.0/School/Glaucoma/code/env/lib/python3.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 828, in convert use_float_arraytype=use_float_arraytype, File "/Users/ludovico/Documents/Documents2.0/School/Glaucoma/code/env/lib/python3.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 605, in _convert_to_spec use_float_arraytype=use_float_arraytype, File "/Users/ludovico/Documents/Documents2.0/School/Glaucoma/code/env/lib/python3.7/site-packages/coremltools/converters/keras/_keras2_converter.py", line 357, in _convert _check_unsupported_layers(model, add_custom_layers) File "/Users/ludovico/Documents/Documents2.0/School/Glaucoma/code/env/lib/python3.7/site-packages/coremltools/converters/keras/_keras2_converter.py", line 121, in _check_unsupported_layers _check_unsupported_layers(layer) File "/Users/ludovico/Documents/Documents2.0/School/Glaucoma/code/env/lib/python3.7/site-packages/coremltools/converters/keras/_keras2_converter.py", line 124, in _check_unsupported_layers raise ValueError("Keras layer '%s' not supported. " % str(type(layer))) ValueError: Keras layer '<class 'keras.layers.normalization_v2.BatchNormalization'>' not supported.

BrandonKynoch commented 3 years ago

I have the same issue, any solution?

TobyRoseman commented 2 years ago

I don't know what's going on here. We support this layer type. We have unit tests for this functionality.

@Ludotrico or @BrandonKynoch - can one of you give us a simple self-contained code example to reproduce this issue?

tim-xoi commented 2 years ago

@TobyRoseman I found behavior similar to that described above when working in an environment with tensorflow 2.6.0 and coreml 5.2.0. See short example below for reproducing the error.

In my case I was looking for a way to pass the image_input_names value to the converter, which appears to be available through ct.converters.keras.convert but not through ct.convert. Interestingly, converting using ct.convert works but using ct.converters.keras.convert produces the error related to batch normalization. If this should be reported as a separate issue, please let me know.

>>> from tensorflow.keras.applications import MobileNetV3Small
>>> import coremltools as ct
>>> mobilenet = MobileNetV3Small(input_shape=(224,224,3))
>>> converted_mobilenet = ct.convert(mobilenet)
Running TensorFlow Graph Passes: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:00<00:00, 10.07 passes/s]
Converting Frontend ==> MIL Ops: 100%|███████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 543/543 [00:00<00:00, 651.10 ops/s]
Running MIL Common passes: 100%|█████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 34/34 [00:00<00:00, 41.38 passes/s]
Running MIL Clean up passes: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 9/9 [00:00<00:00, 101.55 passes/s]
Translating MIL ==> NeuralNetwork Ops: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 660/660 [00:00<00:00, 2245.55 ops/s]
>>> keras_converted_mobilenet = ct.converters.keras.convert(mobilenet)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/anaconda3/envs/coreml/lib/python3.8/site-packages/coremltools/models/_deprecation.py", line 30, in wrapped
    return obj(*args, **kwargs)
  File "/opt/anaconda3/envs/coreml/lib/python3.8/site-packages/coremltools/converters/keras/_keras_converter.py", line 810, in convert
    spec = _convert_to_spec(
  File "/opt/anaconda3/envs/coreml/lib/python3.8/site-packages/coremltools/converters/keras/_keras_converter.py", line 582, in _convert_to_spec
    from . import _keras2_converter
  File "/opt/anaconda3/envs/coreml/lib/python3.8/site-packages/coremltools/converters/keras/_keras2_converter.py", line 54, in <module>
    _keras.layers.normalization.BatchNormalization: _layers2.convert_batchnorm,
AttributeError: module 'keras.layers.normalization' has no attribute 'BatchNormalization'
TobyRoseman commented 2 years ago

Multi-backend Keras support has been removed in coremltools 6.0.