Ma-Dan / YOLOv3-CoreML

YOLOv3 for iOS implemented using CoreML.
MIT License
171 stars 46 forks source link

Getting error while trying the conversion #1

Open chinmay5 opened 6 years ago

chinmay5 commented 6 years ago

While I try to convert the model, I keep getting the error where image

I am not sure what is wrong because the Keras model looks fine(I was able to start its training)

Ma-Dan commented 6 years ago

Try assign a fixed shape for input layer when converting yolo weight to h5. Modify convert.py line 83 to input_layer = Input(shape=(416, 416, 3)) and convert h5 again, then run cormel.py to convert h5 to cormel model.

chinmay5 commented 6 years ago

When I do that, I get an error in conversion image

chinmay5 commented 6 years ago

Figured this one out but still ran into the first issue where it expects 3 params but gets only 1. I am not sure what is going wrong

inosms commented 6 years ago

Hi there!

I am stuck with the same error.

The process is the following:

  1. The v3 model is trained
  2. Then conversion is done via https://github.com/qqwweee/.
  3. The converted model should then be convertible using the provided script, but coremltools complains:
...
42 : conv2d_10, <keras.layers.convolutional.Conv2D object at 0x1126a3400>
43 : conv2d_13, <keras.layers.convolutional.Conv2D object at 0x1126a3588>
Traceback (most recent call last):
  File "convertml.py", line 3, in <module>
    coreml_model = coremltools.converters.keras.convert('./model_data/yolo.h5', input_names='input1', image_input_names='input1', output_names=['output1', 'output2', 'output3'], image_scale=1/255.)
  File "/usr/local/lib/python3.6/site-packages/coremltools/converters/keras/_keras_converter.py", line 745, in convert
    custom_conversion_functions=custom_conversion_functions)
  File "/usr/local/lib/python3.6/site-packages/coremltools/converters/keras/_keras_converter.py", line 543, in convertToSpec
    custom_objects=custom_objects)
  File "/usr/local/lib/python3.6/site-packages/coremltools/converters/keras/_keras2_converter.py", line 350, in _convert
    image_scale = image_scale)
  File "/usr/local/lib/python3.6/site-packages/coremltools/models/neural_network.py", line 2542, in set_pre_processing_parameters
    channels, height, width = array_shape
ValueError: not enough values to unpack (expected 3, got 1)

Solution Hints:

the output of the keras application is:

...
Parsing section upsample_0
Parsing section route_1
Concatenating route layers: [<tf.Tensor 'up_sampling2d_1/ResizeNearestNeighbor:0' shape=(?, ?, ?, 128) dtype=float32>, <tf.Tensor 'leaky_re_lu_5/LeakyRelu/Maximum:0' shape=(?, ?, ?, 256) dtype=float32>]
Parsing section convolutional_11
conv2d bn leaky (3, 3, 384, 256)
Parsing section convolutional_12
conv2d    linear (1, 1, 256, 141)
Parsing section yolo_1

What I figured, is that the last conv2d layer is not batch normalized, which has an effect on how keras-yolo3 converts the weights (https://github.com/qqwweee/keras-yolo3/blob/master/convert.py#L93-L182)

Maybe the weights get mangled in the wrong way in the case of non-normalized batches? I will ping you if I find out some more.

If you have any hints, I would be happy to hear about them!

Thanks a lot!

chinmay5 commented 6 years ago

I am not very familiar with the Keras model but it seems strange if the batch-normalizatioon can change the shape because usually we would not expect any change in the shape due to it. Having said that, I am completely clueless anyways.

Ma-Dan commented 6 years ago

The version of used components are python 3.6.5 tensorflow 1.7.0 h5py 2.7.1 Keras 2.1.6 coremltools 0.8

chinmay5 commented 6 years ago

Aha so @Ma-Dan according to you, this failure is because of version mismatch?

chinmay5 commented 6 years ago

@inosms @Ma-Dan I have no idea how to have this working. Any sort of help shall be highly appreciated

chinmay5 commented 6 years ago

I was able to handle the conversion part by using the code:- coreml_model = coremltools.converters.keras.convert('./model_data/yolo.h5', input_names='input_1', image_input_names='image', output_names = ['output','output2'],image_scale=1/255.)

However, when I try running the converted coreml model, I get the error stating:- coremlc: compiler error: Input shapes (height and width dimensions) must be equal for layer: 'concatenate_1'. and I have no idea how in the world to solve it

Ma-Dan commented 6 years ago

Have you specified input width and height when converting yolo weights to Keras h5?

taixhi commented 6 years ago

@Ma-Dan your first solution of input_layer = Input(shape=(416, 416, 3)) worked fine. Thank you.

Yagger commented 5 years ago

Made sure requirements satisfy the ones provided by @Ma-Dan and edited the line in convert.py (it was line 88 for me). Then it worked. Tanks!