john-rocky / CoreML-Models

Converted CoreML Model Zoo.
1.37k stars 129 forks source link

a question about EfficientDet Lite2 #31

Open ily-R opened 1 year ago

ily-R commented 1 year ago

Hello,

Have you tried converting one of EfficientDet Lite family like lite2 found here: https://tfhub.dev/tensorflow/efficientdet/lite2/detection/1 using either SavedModel directory or tflite model ? or we we need to build a keras model ?

based on coremltools docs, we can pass savedModel directory to the ct.convert() method, but it doesnt work due to input/output unsupported dtype.

After downloading the SavedModel here's what i did:

import coremltools as ct

mlmodel = ct.convert("efficientdet_lite2_detection_1")

I get this error:

.
.
.
    inputs.append(TensorType(name=inp, shape=shape, dtype=dtype))
  File "/Users/smurf/miniconda3/envs/coreml_conv/lib/python3.9/site-packages/coremltools/converters/mil/input_types.py", line 215, in __init__
    raise TypeError("dtype={} is unsupported for inputs/outputs of the model".format(dtype))
TypeError: dtype=<class 'coremltools.converters.mil.mil.types.type_int.make_int.<locals>.int'> is unsupported for inputs/outputs of the model

I think it's due to "uint8" input type, and they dont support it. I try again with ImageType like this:

model = ct.convert(
    "efficientdet_lite2_detection_1",
    inputs=[ct.ImageType(name="images", shape = (1, 448, 448, 3), scale=1 / 255.0, bias=[0, 0, 0], channel_first=False)],
)

I got this error:

.
.
.
  File "/Users/smurf/miniconda3/envs/coreml_conv/lib/python3.9/site-packages/coremltools/converters/mil/frontend/tensorflow/converter.py", line 374, in convert_main_graph
    func_inputs[input_type.name] = mb.placeholder(
  File "/Users/smurf/miniconda3/envs/coreml_conv/lib/python3.9/site-packages/coremltools/converters/mil/mil/builder.py", line 189, in placeholder
    return Placeholder(shape, dtype, allow_rank0_input=allow_rank0_input)
  File "/Users/smurf/miniconda3/envs/coreml_conv/lib/python3.9/site-packages/coremltools/converters/mil/mil/program.py", line 163, in __init__
    raise ValueError('Rank-0 (input {}) is unsupported'.format(name))
ValueError: Rank-0 (input None) is unsupported

This is probably due to input shape:

import tensorflow as tf
model = tf.saved_model.load("efficientdet_lite2_detection_1")
signature = model.signatures
input_tensors = signature["serving_default"].inputs
output_tensors = signature["serving_default"].outputs
print(input_tensors[0])
>>> Tensor("images:0", shape=(None, None, None, 3), dtype=uint8)

I tried to change input shape:

signature["serving_default"].inputs[0].shape = (1, 448, 448, 3)

>>>
Traceback (most recent call last):
  File "/Users/smurf/Desktop/repositories/playground/convert_coreml.py", line 11, in <module>
    signature["serving_default"].inputs[0].shape = (1, 448, 448, 3)
AttributeError: can't set attribute

Any ideas to solve this problem ?

Thank you