ValueError: Invalid input shape for image. in keras model conversion to core ml
I've trained my YOLOv3 network with darknet to recognize some obj in an image. All is OK. I want to use weight file in iOS app, so following some tutorials I obtained keras h5 model from darknet weight file ancd I check that also h5 model works fine. OK. Last step, using coremltools I tried to convert h5 model in coreml model usable under xcode. Here I have the issue... last conversion is performed with this little py script:
import coremltools
....
coreml_model = coremltools.converters.keras.convert('yolorcgz.h5', input_names='image', class_labels=output_labels, image_input_names='image', input_name_shape_dict={'image': [1, 416, 416, 3]})
coreml_model.input_description['image'] = 'Takes a photo'
coreml_model.output_description['output'] = 'Prediction of obj in the photo'
coreml_model.author = 'SW Team'
coreml_model.license = 'Public Domain'
coreml_model.short_description = "YOLOv3 network trained for obj recognition"
coreml_model.save('yolorcgz.mlmodel')
when I run the script I've always have this error:
Traceback (most recent call last):
File "coreml.py", line 9, in <module>
coreml_model = coremltools.converters.keras.convert('yolorcgz.h5',input_name_shape_dict={'input1': [1, 416, 416, 3]})
File "/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras_converter.py", line 760, in convert
custom_conversion_functions=custom_conversion_functions)
File "/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras_converter.py", line 556, in convertToSpec
custom_objects=custom_objects)
File "/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras2_converter.py", line 305, in _convert
raise ValueError(errMsg)
ValueError: Invalid input shape for image.
Please provide a finite height (H), width (W) & channel value (C) using input_name_shape_dict arg with key = 'image' and value = [None,H,W,C]
Converted .mlmodel can be modified to have flexible input shape using coremltools.models.neural_network.flexible_shape_utils
Any ideas on wht could goes wrong? Thank's a lot
Please see PR #447
If you see any more issues please feel free to reopen this, closed due to inactivity.
I've installed coremltools 3.0 ... but it seems that I have the same issue .... here what I have ....
sudo python coreml.py
WARNING: Logging before flag parsing goes to stderr.
W1031 21:26:44.117631 140015479314240 __init__.py:122] Keras version 2.2.5 detected. Last version known to be fully compatible of Keras is 2.2.4 .
W1031 21:26:44.151905 140015479314240 deprecation_wrapper.py:119] From /usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py:541: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.
W1031 21:26:44.193196 140015479314240 deprecation_wrapper.py:119] From /usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py:4432: The name tf.random_uniform is deprecated. Please use tf.random.uniform instead.
W1031 21:26:44.214406 140015479314240 deprecation_wrapper.py:119] From /usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py:66: The name tf.get_default_graph is deprecated. Please use tf.compat.v1.get_default_graph instead.
W1031 21:26:44.214566 140015479314240 deprecation_wrapper.py:119] From /usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py:190: The name tf.get_default_session is deprecated. Please use tf.compat.v1.get_default_session instead.
W1031 21:26:44.214689 140015479314240 deprecation_wrapper.py:119] From /usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py:197: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.
2019-10-31 21:26:44.214868: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-10-31 21:26:44.251522: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2808000000 Hz
2019-10-31 21:26:44.252041: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5601a03ce8b0 executing computations on platform Host. Devices:
2019-10-31 21:26:44.252067: I tensorflow/compiler/xla/service/service.cc:175] StreamExecutor device (0): <undefined>, <undefined>
2019-10-31 21:26:44.300296: W tensorflow/compiler/jit/mark_for_compilation_pass.cc:1412] (One-time warning): Not using XLA:CPU for cluster because envvar TF_XLA_FLAGS=--tf_xla_cpu_global_jit was not set. If you want XLA:CPU, either set that envvar, or use experimental_jit_scope to enable XLA:CPU. To confirm that XLA is active, pass --vmodule=xla_compilation_cache=1 (as a proper command-line flag, not via TF_XLA_FLAGS) or set the envvar XLA_FLAGS=--xla_hlo_profile.
W1031 21:26:44.308296 140015479314240 deprecation_wrapper.py:119] From /usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py:2041: The name tf.nn.fused_batch_norm is deprecated. Please use tf.compat.v1.nn.fused_batch_norm instead.
W1031 21:26:47.483728 140015479314240 deprecation_wrapper.py:119] From /usr/local/lib/python2.7/dist-packages/keras/backend/tensorflow_backend.py:2239: The name tf.image.resize_nearest_neighbor is deprecated. Please use tf.compat.v1.image.resize_nearest_neighbor instead.
/usr/local/lib/python2.7/dist-packages/keras/engine/saving.py:310: UserWarning: No training configuration found in save file: the model was *not* compiled. Compile it manually.
warnings.warn('No training configuration found in save file: '
Traceback (most recent call last):
File "coreml.py", line 7, in <module>
coreml_model = coremltools.converters.keras.convert('yolocards.h5', input_names='image', class_labels=output_labels, image_input_names='image', input_name_shape_dict={'image': [1, 416, 416, 3]})
File "/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras_converter.py", line 793, in convert
respect_trainable=respect_trainable)
File "/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras_converter.py", line 579, in convertToSpec
respect_trainable=respect_trainable)
File "/usr/local/lib/python2.7/dist-packages/coremltools/converters/keras/_keras2_converter.py", line 434, in _convert
raise ValueError(errMsg)
ValueError: Invalid input shape for 'image'.
Please provide a finite height (H), width (W) & channel value (C) using input_name_shape_dict arg with key = 'image' and value = [None, H, W, C]
Converted .mlmodel can be modified to have flexible input shape using coremltools.models.neural_network.flexible_shape_utils
Thank's again
I see that that argument input_name_shape_dict is indeed being provided hence this error should not be raised.
Can you please provide a script to reproduce the error?
@ricox78 had a similar error.
Please provide a finite height (H), width (W) & channel value (C) using input_name_shape_dict arg with key = 'input1' and value = [None,H,W,C]
I had to adjust the key to input1 (not necessary in your example) and change the first value in the list from 1 to None. After this change it worked for me.
Try your example with: input_name_shape_dict={'image': [None, 416, 416, 3]}
After I save the h5 model (yolov3-keras custom model) to coreML model, I am getting this log:
output { name: "grid" shortDescription: "The 13x13 grid with the bounding box data" type { multiArrayType { shape: 21 dataType: DOUBLE } } }
I thought shape will be 13x13x125. But it is not. h5 model is working well in python but when I am trying to run it on iOS/swift, I am not able to unwrap the model output. Am I doing something wrong?
Still facing this issue in coremltools 3.2. @Sawash 's answer seems to temporarily make it go away, but I'm not sure if it's the best solution in the long term (in the sense that it's not intuitive to do so).
Multi-backend Keras support has been removed in coremltools 6.0.