Help with TensorFlow to CoreML Conversion: AttributeError: 'float' object has no attribute 'astype'

Hello,

I’m attempting to convert a TensorFlow model to CoreML using the coremltools package, but I’m encountering an error during the conversion process. The error traceback points to an issue within the Cast operation in the MIL (Model Intermediate Layer) when it tries to perform type inference:

AttributeError: 'float' object has no attribute 'astype'

Here is the relevant part of the error traceback:

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])

I’ve tried converting a model from the yamnet-tensorflow2 repository, and this error occurs when CoreML tries to cast a float type during the conversion of certain operations. I’m currently using Python 3.10 and coremltools version 6.0.1, with TensorFlow 2.x.

Has anyone encountered a similar issue or can offer suggestions on how to resolve this?

I’ve also considered that this might be related to mismatches in the model’s data types, but I’m not sure how to proceed.

Platform and package versions:

coremltools                  6.1
tensorflow                   2.10.0
tensorflow-estimator         2.10.0
tensorflow-hub               0.16.1
tensorflow-io-gcs-filesystem 0.37.1
Python 3.10.12
pip 24.3.1 from ~/.pyenv/versions/3.10.12/lib/python3.10/site-packages/pip (python 3.10)

Darwin MacBook-Pro.local 24.1.0 Darwin Kernel Version 24.1.0: Thu Oct 10 21:02:27 PDT 2024; root:xnu-11215.41.3~2/RELEASE_X86_64 x86_64

Any help or pointers would be greatly appreciated!

Answered by Blume in 815485022

I wanted to provide an update on this. I’ve made progress using the Core ML Tools documentation, specifically the Introductory Quickstart Guide. This has helped clarify the steps for converting TensorFlow models. I still need to further my understanding of the best way to reference and adapt YAMNet for conversion, but I’m on the right track.

Since there hasn’t been much activity on this thread, I’ll close it for now. Thanks!

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'
Accepted Answer

I wanted to provide an update on this. I’ve made progress using the Core ML Tools documentation, specifically the Introductory Quickstart Guide. This has helped clarify the steps for converting TensorFlow models. I still need to further my understanding of the best way to reference and adapt YAMNet for conversion, but I’m on the right track.

Since there hasn’t been much activity on this thread, I’ll close it for now. Thanks!

Help with TensorFlow to CoreML Conversion: AttributeError: 'float' object has no attribute 'astype'
 
 
Q