Conversion Script:
import tensorflow as tf
import tensorflow_hub as hub
import coremltools as ct
import numpy as np
# Load YAMNet model from TensorFlow Hub
yamnet_layer = hub.KerasLayer("https://tfhub.dev/google/yamnet/1", trainable=False, dtype=tf.float32)
# Define input shape and input layer with explicit dtype
input_shape = (15600,)
inputs = tf.keras.Input(shape=input_shape, name="audio_waveform", dtype=tf.float32)
outputs = yamnet_layer(tf.reshape(inputs, [-1])) # Ensure correct reshaping
model = tf.keras.Model(inputs, outputs)
# Save the model to a .saved_model format
model.save("yamnet_saved_model")
# Convert the saved model to Core ML format
mlmodel = ct.convert(
"yamnet_saved_model",
source="tensorflow",
inputs=[ct.TensorType(shape=(1, 15600), name="audio_waveform", dtype=np.float32)]
)
# Save the converted model as a .mlmodel file
mlmodel.save("YAMNet.mlmodel")
print("Conversion successful! Model saved as YAMNet.mlmodel")
Execution Error:
√ yamnet-tensorflow2-yamnet-v1 % python convertItScript.py
2024-11-09 15:41:35.807906: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 FMA
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
Running TensorFlow Graph Passes: 100%
Converting TF Frontend ==> MIL Ops: 56%
Traceback (most recent call last):
File "~/yamnet-tensorflow2-yamnet-v1/convertItScript.py", line 19, in <module>
mlmodel = ct.convert(
File "~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/coremltools/converters/_converters_entry.py", line 444, in convert
mlmodel = mil_convert(
File "~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 190, in mil_convert
return _mil_convert(model, convert_from, convert_to, ConverterRegistry, MLModel, compute_units, **kwargs)
File "~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/coremltools/converters/mil/converter.py", line 217, in _mil_convert
...
File "~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/coremltools/converters/mil/mil/ops/defs/iOS15/elementwise_unary.py", line 870, in value_inference
return self.get_cast_value(self.x, self.dtype.val)
File "~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/coremltools/converters/mil/mil/ops/defs/iOS15/elementwise_unary.py", line 896, in get_cast_value
return input_var.val.astype(dtype=type_map[dtype_val])
AttributeError: 'float' object has no attribute 'astype'