Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversion from tensorflow (.pb) to coreml (.mlmodel) #2435

Open
HISYS-ES-ryuma opened this issue Jan 21, 2025 · 0 comments
Open

Conversion from tensorflow (.pb) to coreml (.mlmodel) #2435

HISYS-ES-ryuma opened this issue Jan 21, 2025 · 0 comments
Labels
bug Unexpected behaviour that should be corrected (type) tf2.x / tf.keras Issue could be related to tf2.x where coremltools isn't supported (component)

Comments

@HISYS-ES-ryuma
Copy link

Hi
I'm trying to convert tensorflow model to coreml model.

OS: macOS Squoia 15.2 (Apple M1)
Python: 3.8.19 (miniforge)
TensorFlow: tensorflow-macos 2.12.0, tensorflow-metal 0.8.0
CoreML Tools: 8.1

This code was generated by ChatGPT.

import tensorflow as tf
import coremltools as ct

pb_model_dir = './ObjectDetection'  # Directory containing the saved_model.pb
loaded_model = tf.saved_model.load(pb_model_dir)

concrete_func = loaded_model.signatures["serving_default"]

input_shape = (1, 224, 224, 3)  # Example input shape, adjust as needed
sample_input = tf.random.uniform(input_shape, dtype=tf.float32)  # Use float32
sample_input_uint8 = tf.cast(sample_input * 255, dtype=tf.uint8)  # Convert to uint8 if required

output_tensors = concrete_func(input_tensor=sample_input_uint8)

print("Output keys:", output_tensors.keys())

output_key = 'detection_boxes'  # Use the appropriate key from the printed keys

@tf.function
def model_func(input_tensor):
    return concrete_func(input_tensor=input_tensor)[output_key]

input_layer = tf.keras.Input(shape=input_shape[1:], dtype=tf.uint8)

output_tensor = tf.keras.layers.Lambda(lambda x: model_func(x))(input_layer)

keras_model = tf.keras.Model(inputs=input_layer, outputs=output_tensor)

mlmodel = ct.convert(
    keras_model,
    inputs=[ct.ImageType(shape=input_shape)],
    minimum_deployment_target=ct.target.iOS14
)

mlmodel.save('./NewModel/coreml.mlmodel')

Then I got an error.

(tf-m1) esadmin@MacBookAir Igarashi % python  convert_20250121.py 
Metal device set to: Apple M1

systemMemory: 8.00 GB
maxCacheSize: 2.67 GB

Output keys: dict_keys(['detection_boxes_strided', 'num_detections', 'detection_boxes', 'detection_multiclass_scores', 'detection_scores', 'detection_classes'])
Running TensorFlow Graph Passes: 100%|████████████████████████████████████████████████████████████████████████████████████████████████████████████| 6/6 [00:01<00:00,  4.09 passes/s]
Converting TF Frontend ==> MIL Ops:   0%|                                                                                                                 | 0/1294 [00:00<?, ? ops/s]Saving value type of int64 into a builtin type of int32, might lose precision!
Converting TF Frontend ==> MIL Ops:   8%|████████▋                                                                                               | 1/12 [00:00<00:00, 10230.01 ops/s]
Converting TF Frontend ==> MIL Ops:  56%|████████████████████████████████████████████████████████▏                                           | 727/1294 [00:00<00:00, 15145.20 ops/s]
Traceback (most recent call last):
  File "convert_20250121.py", line 41, in <module>
    mlmodel = ct.convert(
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/_converters_entry.py", line 635, in convert
    mlmodel = mil_convert(
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 188, in mil_convert
    return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 212, in _mil_convert
    proto, mil_program = mil_convert_to_proto(
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 288, in mil_convert_to_proto
    prog = frontend_converter(model, **kwargs)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/converter.py", line 98, in __call__
    return tf2_loader.load()
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow/load.py", line 82, in load
    program = self._program_from_tf_ssa()
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow2/load.py", line 210, in _program_from_tf_ssa
    return converter.convert()
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow/converter.py", line 524, in convert
    self.convert_main_graph(prog, graph)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow/converter.py", line 421, in convert_main_graph
    outputs = convert_graph(self.context, graph, self.output_names)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow/convert_utils.py", line 191, in convert_graph
    add_op(context, node)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow2/ops.py", line 136, in StatelessWhile
    x = mb.while_loop(_cond=cond, _body=body, loop_vars=loop_vars, name=node.name)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/mil/ops/registry.py", line 183, in add_op
    return cls._add_op(op_cls_to_add, **kwargs)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/mil/builder.py", line 236, in _add_op
    new_op.build_nested_blocks()
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/mil/ops/defs/iOS15/control_flow.py", line 471, in build_nested_blocks
    cond_block, body_block, exit_vars = self._build_block(block_inputs)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/mil/ops/defs/iOS15/control_flow.py", line 396, in _build_block
    cond_var = cond_func(*cond_block.inputs)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow2/ops.py", line 126, in cond
    cond_output_vars = convert_graph(context, cond_graph)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow/convert_utils.py", line 191, in convert_graph
    add_op(context, node)
  File "/Users/esadmin/miniforge3/envs/tf-m1/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow/ops.py", line 754, in Less
    x = context[node.inputs[0]]
IndexError: list index out of range
@HISYS-ES-ryuma HISYS-ES-ryuma added the bug Unexpected behaviour that should be corrected (type) label Jan 21, 2025
@HISYS-ES-ryuma HISYS-ES-ryuma changed the title Convert from tensorflow (.pb) to coreml (.mlmodel) Conversion from tensorflow (.pb) to coreml (.mlmodel) Jan 21, 2025
@jakesabathia2 jakesabathia2 added the tf2.x / tf.keras Issue could be related to tf2.x where coremltools isn't supported (component) label Jan 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Unexpected behaviour that should be corrected (type) tf2.x / tf.keras Issue could be related to tf2.x where coremltools isn't supported (component)
Projects
None yet
Development

No branches or pull requests

2 participants