1

I have a Keras model and am following the coreml documentation to convert it to coreml. Here is the code - -

import coremltools
import tensorflow 
from tensorflow import keras
coreml_model = coremltools.converters.keras.convert("basic.keras.h5")
coremltools.utils.save_spec(coreml_model, 'basic_v1.mlmodel')

However running this gives me a lot of errors which I assume are with the environment on the machine. I am on a Mac which has python 2 and I installed python3 and am using it. I also installed tensor flow 2 (the latest from the site). Here is the error I get --

W1227 10:13:28.989016 4604394944 __init__.py:74] TensorFlow version 2.0.0 detected. Last version known to be fully compatible is 1.14.0 .
W1227 10:13:29.083386 4604394944 __init__.py:128] Keras version 2.3.1 detected. Last version known to be fully compatible of Keras is 2.2.4 .
Traceback (most recent call last):
File "convert_to_coreml.py", line 4, in <module>
coreml_model = coremltools.converters.keras.convert("basic.keras.h5")
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 792, in convert
respect_trainable=respect_trainable)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/coremltools/converters/keras/_keras_converter.py", line 579, in convertToSpec
respect_trainable=respect_trainable)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/coremltools/converters/keras/_keras2_converter.py", line 317, in _convert
model = _keras.models.load_model(model, custom_objects = custom_objects)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/saving.py", line 492, in load_wrapper
return load_function(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/saving.py", line 584, in load_model
model = _deserialize_model(h5dict, custom_objects, compile)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/saving.py", line 274, in _deserialize_model
model = model_from_config(model_config, custom_objects=custom_objects)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/saving.py", line 627, in model_from_config
return deserialize(config, custom_objects=custom_objects)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/layers/__init__.py", line 168, in deserialize
printable_module_name='layer')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/utils/generic_utils.py", line 147, in deserialize_keras_object
list(custom_objects.items())))
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/sequential.py", line 301, in from_config
custom_objects=custom_objects)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/layers/__init__.py", line 168, in deserialize
printable_module_name='layer')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/utils/generic_utils.py", line 149, in deserialize_keras_object
return cls.from_config(config['config'])
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/base_layer.py", line 1179, in from_config
return cls(**config)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/legacy/interfaces.py", line 91, in wrapper
return func(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/layers/core.py", line 877, in __init__
self.kernel_initializer = initializers.get(kernel_initializer)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/initializers.py", line 515, in get
return deserialize(identifier)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/initializers.py", line 510, in deserialize
printable_module_name='initializer')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/utils/generic_utils.py", line 140, in deserialize_keras_object
': ' + class_name)
ValueError: Unknown initializer: GlorotUniform

I am not a python guy but the last line is pointing to some Keras issue (per google) but I am importing Keras and the version seems ok. Any help is appreciated.

SFlow
  • 283
  • 3
  • 9

1 Answers1

0

You are trying to use glorot_uniform. The library versions used by .h5 file writer and reader are not the same, and are not compatible.

You executed

from tensorflow import keras

@lintex offers the advice to instead:

import keras
J_H
  • 17,926
  • 4
  • 24
  • 44