amir-abdi / keras_to_tensorflow

General code to convert a trained keras model into an inference tensorflow model
MIT License
1.67k stars 539 forks source link

TypeError: '<' not supported between instances of 'dict' and 'float' #67

Open JeffBship opened 6 years ago

JeffBship commented 6 years ago

Thanks for all the work you've done! Any chance you have a suggestion for the following error?

F:\keras_to_tensorflow>python keras_to_tensorflow.py --input_model="MobileNet.h5" --output_model="MobileNet.pb" Using TensorFlow backend. Traceback (most recent call last): File "keras_to_tensorflow.py", line 166, in app.run(main) File "F:\Python36\lib\site-packages\absl\app.py", line 300, in run _run_main(main, args) File "F:\Python36\lib\site-packages\absl\app.py", line 251, in _run_main sys.exit(main(argv)) File "keras_to_tensorflow.py", line 112, in main model = load_model(FLAGS.input_model, FLAGS.input_model_json) File "keras_to_tensorflow.py", line 61, in load_model model = keras.models.load_model(input_model_path) File "F:\Python36\lib\site-packages\keras\engine\saving.py", line 419, in load_model model = _deserialize_model(f, custom_objects, compile) File "F:\Python36\lib\site-packages\keras\engine\saving.py", line 225, in _deserialize_model model = model_from_config(model_config, custom_objects=custom_objects) File "F:\Python36\lib\site-packages\keras\engine\saving.py", line 458, in model_from_config return deserialize(config, custom_objects=custom_objects) File "F:\Python36\lib\site-packages\keras\layers__init.py", line 55, in deserialize printable_module_name='layer') File "F:\Python36\lib\site-packages\keras\utils\generic_utils.py", line 145, in deserialize_keras_object list(custom_objects.items()))) File "F:\Python36\lib\site-packages\keras\engine\network.py", line 1022, in from_config process_layer(layer_data) File "F:\Python36\lib\site-packages\keras\engine\network.py", line 1008, in process_layer custom_objects=custom_objects) File "F:\Python36\lib\site-packages\keras\layers__init__.py", line 55, in deserialize printable_module_name='layer') File "F:\Python36\lib\site-packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object return cls.from_config(config['config']) File "F:\Python36\lib\site-packages\keras\engine\base_layer.py", line 1109, in from_config return cls(**config) File "F:\Python36\lib\site-packages\keras\layers\advanced_activations.py", line 292, in init__ if max_value is not None and max_value < 0.: TypeError: '<' not supported between instances of 'dict' and 'float'

amir-abdi commented 6 years ago

Most probably it’s due to a custom layer implemented in keras which is not present in tensorflow.

On Nov 9, 2018, at 9:16 AM, Jeff Blankenship notifications@github.com wrote:

Thanks for all the work you've done! Any chance you have a suggestion for the following error?

F:\keras_to_tensorflow>python keras_to_tensorflow.py --input_model="MobileNet.h5" --output_model="MobileNet.pb" Using TensorFlow backend. Traceback (most recent call last): File "keras_to_tensorflow.py", line 166, in app.run(main) File "F:\Python36\lib\site-packages\absl\app.py", line 300, in run _run_main(main, args) File "F:\Python36\lib\site-packages\absl\app.py", line 251, in _run_main sys.exit(main(argv)) File "keras_to_tensorflow.py", line 112, in main model = load_model(FLAGS.input_model, FLAGS.input_model_json) File "keras_to_tensorflow.py", line 61, in load_model model = keras.models.load_model(input_model_path) File "F:\Python36\lib\site-packages\keras\engine\saving.py", line 419, in load_model model = deserialize_model(f, custom_objects, compile) File "F:\Python36\lib\site-packages\keras\engine\saving.py", line 225, in deserialize_model model = model_from_config(model_config, custom_objects=custom_objects) File "F:\Python36\lib\site-packages\keras\engine\saving.py", line 458, in model_from_config return deserialize(config, custom_objects=custom_objects) File "F:\Python36\lib\site-packages\keras\layers_init.py", line 55, in deserialize printable_module_name='layer') File "F:\Python36\lib\site-packages\keras\utils\generic_utils.py", line 145, in deserialize_keras_object list(custom_objects.items()))) File "F:\Python36\lib\site-packages\keras\engine\network.py", line 1022, in from_config process_layer(layer_data) File "F:\Python36\lib\site-packages\keras\engine\network.py", line 1008, in process_layer custom_objects=custom_objects) File "F:\Python36\lib\site-packages\keras\layers_init.py", line 55, in deserialize printable_module_name='layer') File "F:\Python36\lib\site-packages\keras\utils\generic_utils.py", line 147, in deserialize_keras_object return cls.from_config(config['config']) File "F:\Python36\lib\site-packages\keras\engine\base_layer.py", line 1109, in from_config return cls(**config) File "F:\Python36\lib\site-packages\keras\layers\advanced_activations.py", line 292, in init if max_value is not None and max_value < 0.: TypeError: '<' not supported between instances of 'dict' and 'float'

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub, or mute the thread.

Rathna21 commented 5 years ago

@amir-abdi

Hello, Thanks for your code. I was trying to convert my Keras model to .pb file , I was successfully able to convert my Keras model to .pb file. After that, using winmltools I converted it into ONNX.

Now I am trying to build an Windows ML app in .Net Core. So, while I am loading my onnx model i am getting the following error:

Type Error : Type parameter (T) bound to different types (tensor(float) and tensor(int32) in node (conv2d_transpose_4/mul).

I tried to figure out the error, I found out while using your code to convert my model from Keras to tensorflow, conv2d_transpose is in type int32 but is there a way to keep the type float.

Is there a way I can resolve this error ?

capture

More information : Please see the 2 graphs. unet.h5 ( my pre-trained keras model ) - Here type of conv2d_transpose_4 is float32. But in converted .pb file the type of conv2d_transpose_4 is int32.

unet.h5 file screen shot 2019-01-21 at 5 11 18 pm

unet.pb file screen shot 2019-01-21 at 5 17 44 pm

amir-abdi commented 5 years ago

@Rathna21 That is a weird behaviour; unless if you used "quantization" features of tensorflow. I have not changed the data type for any tensors in this simple conversion tool, so, this behaviour is quite unexpected.