Hi, I'd like to use a MLX model already in the MLX Community in my App. I understand I first need to convert it to Core ML format.
Is there an easy way to do that considering MLX is an Apple project?
It would be good if it was easier then I'd be more motivated to use MLX to train my models.
Thanks
Richard
Core ML
RSS for tagIntegrate machine learning models into your app using Core ML.
Post
Replies
Boosts
Views
Activity
The DINO v1/v2 models are particularly interesting to me as they produce embeddings for the detected objects rather than ordinary classification indexes.
That makes them so much more useful than the CNN based models.
I would like to prepare some of the models posted on Huggingface to run on Apple Silicon, but it seems that the default conversion with TorchScript will not work. The other default conversions I've looked at so far also don't work. Conversion based on an example input doesn't capture enough of the model.
I know that some have managed to convert it as I have a demo with a coreml model that seems to work, but I would like to know how to do the conversion myself.
Has anyone managed to convert any of the DINOv2 models?
Here is an App using CoreML API with ML package format, it works fine in iOS17, while it is crashed when calling [MLModel modelWithContentsOfURL ] to load model running in iOS18. It seems an exception is raised "Failed to set compute_device_types_mask E5RT: Cannot provide zero compute device types. (1)". Is it a bug of iOS18 beta version , and it will be fixed in the future?
The stack is as below:
Exception Codes: #0 at 0x1e9280254
Crashed Thread: 49
Application Specific Information:
*** Terminating app due to uncaught exception 'NSGenericException', reason: 'Failed to set compute_device_types_mask E5RT: Cannot provide zero compute device types. (1)'
Last Exception Backtrace:
0 CoreFoundation 0x0000000199466418 __exceptionPreprocess + 164
1 libobjc.A.dylib 0x00000001967cde88 objc_exception_throw + 76
2 CoreFoundation 0x0000000199560794 -[NSException initWithCoder:]
3 CoreML 0x00000001b4fcfa8c -[MLE5ProgramLibraryOnDeviceAOTCompilationImpl createProgramLibraryHandleWithRespecialization:error:] + 1584
4 CoreML 0x00000001b4fcf3cc -[MLE5ProgramLibrary _programLibraryHandleWithForceRespecialization:error:] + 96
5 CoreML 0x00000001b4fc23d8 __44-[MLE5ProgramLibrary prepareAndReturnError:]_block_invoke + 60
6 libdispatch.dylib 0x00000001a12e1160 _dispatch_client_callout + 20
7 libdispatch.dylib 0x00000001a12f07b8 _dispatch_lane_barrier_sync_invoke_and_complete + 56
8 CoreML 0x00000001b4fc3e98 -[MLE5ProgramLibrary prepareAndReturnError:] + 220
9 CoreML 0x00000001b4fc3bc0 -[MLE5Engine initWithContainer:configuration:error:] + 220
10 CoreML 0x00000001b4fc3888 +[MLE5Engine loadModelFromCompiledArchive:modelVersionInfo:compilerVersionInfo:configuration:error:] + 344
11 CoreML 0x00000001b4faf53c +[MLLoader _loadModelWithClass:fromArchive:modelVersionInfo:compilerVersionInfo:configuration:error:] + 364
12 CoreML 0x00000001b4faedd4 +[MLLoader _loadModelFromArchive:configuration:modelVersion:compilerVersion:loaderEvent:useUpdatableModelLoaders:loadingClasses:error:] + 540
13 CoreML 0x00000001b4f9b900 +[MLLoader _loadWithModelLoaderFromArchive:configuration:loaderEvent:useUpdatableModelLoaders:error:] + 424
14 CoreML 0x00000001b4faaeac +[MLLoader _loadModelFromArchive:configuration:loaderEvent:useUpdatableModelLoaders:error:] + 460
15 CoreML 0x00000001b4fb0428 +[MLLoader _loadModelFromAssetAtURL:configuration:loaderEvent:error:] + 240
16 CoreML 0x00000001b4fb00c4 +[MLLoader loadModelFromAssetAtURL:configuration:error:] + 104
17 CoreML 0x00000001b5314118 -[MLModelAssetResourceFactoryOnDiskImpl modelWithConfiguration:error:] + 116
18 CoreML 0x00000001b5418cc0 __60-[MLModelAssetResourceFactory modelWithConfiguration:error:]_block_invoke + 72
19 libdispatch.dylib 0x00000001a12e1160 _dispatch_client_callout + 20
20 libdispatch.dylib 0x00000001a12f07b8 _dispatch_lane_barrier_sync_invoke_and_complete + 56
21 CoreML 0x00000001b5418b94 -[MLModelAssetResourceFactory modelWithConfiguration:error:] + 276
22 CoreML 0x00000001b542919c -[MLModelAssetModelVendor modelWithConfiguration:error:] + 152
23 CoreML 0x00000001b5380ce4 -[MLModelAsset modelWithConfiguration:error:] + 112
24 CoreML 0x00000001b4fb0b3c +[MLModel modelWithContentsOfURL:configuration:error:] + 168
I know that I can use face detect with CoreML, but I'm wandering that is there any to identify the same person between two images like Photos app.
I'm trying to convert a TensorFlow model that I didn't create and know approximately nothing about to CoreML so that I can use it in some functional tests. I can't tell you much about the model, but you can read about it on the blog from the team that created it: https://research.google/blog/improving-mobile-app-accessibility-with-icon-detection/
I can't convert this model to a TensorFlow Lite model because it uses a few full TensorFlow operations (which I could work around) and it exceeds the 4-tensor output limit (which I can't, AFAIK). So instead, I'm trying to convert the model to CoreML so that I can run it on-device.
The issue I'm running into is that every approach fails in different ways. If I load the model with tf.saved_model.load and pass that as the first parameter to the convert call, it says
NotImplementedError: Expected model format: [SavedModel | concrete_function | tf.keras.Model | .h5 | GraphDef], got <tensorflow.python.trackable.autotrackable.AutoTrackable object at 0x30d90c250>
If I pass model.signatures['serving_default'] as the first parameter to convert, I get
NotImplementedError: Expected model format: [SavedModel | concrete_function | tf.keras.Model | .h5 | GraphDef], got ConcreteFunction [...a page or two of info about the function here...]
If I try to wrap it in a Keras layer using the instructions provided in the converter, it fails because a sequential model can't have multiple outputs.
If I try to use a tf.keras.layers.TFSMLayer to load the model, it fails because there are multiple tags, and there's no way to specify tags when constructing the layer. (It tells me that I need to add 'tags' to load the model, but if I do that, it tells me that tags isn't a valid parameter to the call.)
If I load the model with tf.saved_model.load and specify a single tag, then re-save it in a different location with tf.saved_model.save to generate a new model with only a single tag, then do
input_layer = tf.keras.Input(shape=(768, 768, 3), dtype="int8")
layer = tf.keras.layers.TFSMLayer("./serve_model", call_endpoint='serving_default')
outputs = layer(input_layer)
model = tf.keras.Model(input_layer, outputs)
I get
AttributeError: 'Functional' object has no attribute '_get_save_spec'
At one point, I also tried this:
class LayerFromSavedModel(tf.keras.layers.Layer):
def __init__(self):
super(LayerFromSavedModel, self).__init__()
self.vars = legacy_model.variables
def call(self, inputs):
return legacy_model.signatures['serving_default'](inputs)
input = tf.keras.Input(shape=(3000, 3000, 3))
model = tf.keras.Model(input, LayerFromSavedModel()(input))
and saw a similar failure.
I've run out of ideas here. Is there simply no support whatsoever in the converter for importing a TensorFlow 2 SavedModel into CoreML, or am I missing something fundamental?
We are currently working on implementing a baby cry detection model in the frontend of our app but have encountered some challenges with the mel spectrogram transformation.
Our mel spectrogram class, developed in python, leverages librosa for generating mel spectrograms (librosa.feature.melspectrogram and librosa.power_to_db). While we have successfully exported the model to a .mlmodel file, the results we obtain in Swift differ significantly from those generated by our Python code.
Could this discrepancy be due to the use of librosa in Python, which might not be directly compatible with Swift? Or should the transformation process be inherently consistent once exported to a .mlmodel file?
Hi,
I have a custom object detection CoreML model and I notice something strange when using the model with the Vision framework.
I have tried two different approaches as to how to process an image and do inference on the CoreML model.
The first one is using the CoreML "raw": initialising the model, getting the input image ready and using the model's .prediction() function to get the models output.
The second one is using Vision to wrap the CoreML model in a VNCoreMLModel, creating a VNCoreMLRequest and using the VNImageRequestHandler to actually perform the model inference. The result of the VNCoreMLRequest is of type VNRecognizedObjectObservation.
The issue I now face is in the difference in the output of both methods. The first method gives back the raw output of the CoreML model: confidence and coordinates. The confidence is an array with size equal to the number of classes in my model (3 in my case). The second method gives back the boundingBox, confidence and labels. However here the confidence is only the confidence for the most likely class (so size is equal to 1). But the confidence I get from the second approach is quite different from the confidence I get during the first approach.
I can use either one of the approaches in my application. However, I really want to find out what is going on and understand how this difference occurred.
Thanks!
I'm using Filemaker, with Monkey Bread Software plugin's CoreML features, to find that it can only write to .mlmodelc.
Are these (.mlmodel = .mlmodelc) the same? If not, how do you generate a .mlmodelc using XCode.
Please let me know, thanks.
I have an mlprogram of size 127.2MB it was created using tensorflow and then converted to CoreML. When I request a prediction the amount of memory shoots up to 2-2.5GB every time. I've tried using the optimization techniques in coremltools but nothing seems to work it still shoots up to the same 2-2.5GB of ram every time. I've attached a graph to see it doesn't seem to be a leak as the memory is then going back down.
I made a model using pytorch and then converted it into a mlmodel file. Next I tried and downloaded (https://developer.apple.com/documentation/vision/recognizing_objects_in_live_capture) which worked! But when I changed the model to my model that I made, the camera worked, but no predictions where shown please
h
elp!
I was watching WWDC20_Model Deployment, but I found that there's no existing documentation backing up this session.
Is model deployment dashboard still available in 2024?
Hi.
A17 Pro Neural Engine has 35 TOPS computational power.
But many third-party benchmarks and articles suggest that it has a little more power than A16 Bionic.
Some references are,
Geekbench ML
Core ML performance benchmark, 2023 edition
How do we use the maximum power of A17 Pro Neural Engine?
For example, I guess that logical devices of ANE on A17 Pro may be two, not one, so we may need to instantiate two Core ML models simultaneously for the purpose.
Please let me know any technical hints.
This model run coreml result is not right, the precision is completely wrong, I posted a PhotoDepthAnythingConv.onnx model: https://github.com/MoonCodeMaster/CoremlErrorModel/tree/main/DepthAnything
The CoreML model worked correctly in the “Preview” of “CreateML”.
However, after it is put into the Xcode project and replaced the “MobileNetV2” , it did not classify the images correctly, it returned one image with high confidence all the time no matter what image it is .
The same code works fine when executed on real device.
Can someone please assist on this ?
In investigating a capture session crash, it's unclear what's causing occasional system pressure interruptions, except that it's happening on older iOS devices. Does Low Power Mode have a meaningful impact on whether these interruptions happen?
I am trying to coremltools.converters.convert a traced PyTorch model and I got an error:
PyTorch convert function for op 'intimplicit' not implemented
I am trying to convert a RVC model from github.
I traced the model with torch.jit.trace and it fails. So I traced down the problematic part to the ** layer : https://github.com/RVC-Project/Retrieval-based-Voice-Conversion-WebUI/blob/main/infer/lib/infer_pack/modules.py#L188
import torch
import coremltools as ct
from infer.lib.infer_pack.modules import **
model = **(192, 5, dilation_rate=1, n_layers=16, ***_channels=256, p_dropout=0)
model.remove_weight_norm()
model.eval()
test_x = torch.rand(1, 192, 200)
test_x_mask = torch.rand(1, 1, 200)
test_g = torch.rand(1, 256, 1)
traced_model = torch.jit.trace(model,
(test_x, test_x_mask, test_g),
check_trace = True)
x = ct.TensorType(name='x', shape=test_x.shape)
x_mask = ct.TensorType(name='x_mask', shape=test_x_mask.shape)
g = ct.TensorType(name='g', shape=test_g.shape)
mlmodel = ct.converters.convert(traced_model,
inputs=[x, x_mask, g])
I got an error RuntimeError: PyTorch convert function for op 'intimplicit' not implemented.
How could I modify the **::forward so it does not generate an intimplicit operator ?
Thanks
David
I run a MiDaS CoreML model on the Device.
It run well on VisionPro Simulator and iOS RealDevice.
But crash on VisionPro device.
crash mssage:
/Library/Caches/com.apple.xbs/Sources/MetalPerformanceShaders/MPSCore/Utility/MPSLibrary.mm:550: failed assertion `MPSKernel MTLComputePipelineStateCache unable to load function ndArrayConvolution2DA14.
Crashlog_com.moemiku.VisionMagicPhoto_2024-01-21-16-01-07.txt
Crashlog_com.moemiku.VisionMagicPhoto_2024-01-21-16-00-39.txt
Have a CoreML model that I run in my app Spatial Media Toolkit which lets you convert 2D photos to Spatial.
Running the model on my 13" M1 mac gets 70ms inference. Running the exact same code on my Vision Pro takes 700ms. I'm working on adding video support but Vision Pro inference is feeling impossible due to 700ms per frame (20x realtime for for 30fps! 1 sec of video takes 20 sec!)
There's a ModelConfiguration you can provide, and when I force CPU I get the same exact performance.
Either it's only running on CPU, the NeuralEngine is throttled, or maybe GPU isn't allowed to help out. Disappointing but also feels like a software issue. Would be curious if anyone else has hit this/have any workarounds
Is 30x30 the maximum grid size on Create ML App?
The input allows me to set any number higher than that, but on starting training, the number falls back to 30x30.
Is that a limitation or a bug in the app?
On tf version 2.11.0.
I have tried to follow on a fairly standard NN example in order to convert to a CoreML model. However, I cannot get this to work and I'm not clear where it is going wrong. It would seem to be a fairly standard task - a toy example - and I can't see why the conversion would fail.
Any help would be appreciated. I have tried the different approaches listed below, but it seems the conversion should just work.
I have also tried running the same code pinned to:
tensorflow==2.6.2
scikit-learn==0.19.2
pandas==1.1.1
And get a different sequence of errors.
The Python code I used mostly comes form this example:
https://lnwatson.co.uk/posts/intro_to_nn/
import pandas as pd
import numpy as np
import tensorflow as tf
import torch
from sklearn.model_selection import train_test_split
from tensorflow import keras
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '1'
np.bool = np.bool_
np.int = np.int_
print("tf version", tf.__version__)
csv_url = 'https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data'
col_names = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width','Class']
df = pd.read_csv(csv_url, names = col_names)
labels = df.pop('Class')
labels = pd.get_dummies(labels)
X = df.values
y = labels.values
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.05)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2)
model = keras.Sequential()
model.add(keras.layers.Dense(16, activation='relu', input_shape=(4,)))
model.add(keras.layers.Dense(3, activation='softmax'))
model.summary()
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(X_train, y_train,
batch_size=12,
epochs=200,
validation_data=(X_val, y_val))
import coremltools as ct
# Pass in `tf.keras.Model` to the Unified Conversion API
mlmodel = ct.convert(model, convert_to="mlprogram")
# mlmodel = ct.convert(model, source="tensorflow")
# mlmodel = ct.convert(model, convert_to="neuralnetwork")
# mlmodel = ct.convert(
# model,
# source="tensorflow",
# inputs=[ct.TensorType(name="input")],
# outputs=[ct.TensorType(name="output")],
# minimum_deployment_target=ct.target.iOS14,
# )
When using either of these 3:
mlmodel = ct.convert(model, convert_to="mlprogram")
mlmodel = ct.convert(model, source="tensorflow")
mlmodel = ct.convert(model, convert_to="neuralnetwork")
I get:
mlmodel2 = ct.convert(model, source="tensorflow")
ValueError: Const node 'sequential_5/dense_10/MatMul/ReadVariableOp' cannot have no value
ERROR:root:sequential_5/dense_11/BiasAdd/ReadVariableOp:0
ERROR:root:[ 0.34652767 0.16202268 -0.3554725 ]
Running TensorFlow Graph Passes: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 5/5 [00:00<00:00, 28.76 passes/s]
Converting Frontend ==> MIL Ops: 8%|█████████████████ | 1/12 [00:00<00:00, 16710.37 ops/s]
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
File ~/Documents/CoreML Basic Models/NN_Keras_Iris.py:142
130 import coremltools as ct
131 # Pass in `tf.keras.Model` to the Unified Conversion API
132 # mlmodel = ct.convert(model, convert_to="mlprogram")
133
(...)
140
141 # ct.convert(mymodel(), source="tensorflow")
--> 142 mlmodel2 = ct.convert(model, source="tensorflow")
144 mlmodel = ct.convert(
145 model,
146 source="tensorflow",
(...)
153 minimum_deployment_target=ct.target.iOS14,
154 )
....
File ~/opt/anaconda3/envs/coreml_env/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow/ops.py:430, in Const(context, node)
427 @register_tf_op
428 def Const(context, node):
429 if node.value is None:
--> 430 raise ValueError("Const node '{}' cannot have no value".format(node.name))
431 mode = get_const_mode(node.value.val)
432 x = mb.const(val=node.value.val, mode=mode, name=node.name)
ValueError: Const node 'sequential_5/dense_10/MatMul/ReadVariableOp' cannot have no value
Second Approach:
A different approach I tried was specifying the inout type TensorType.
However, when specifying the input and outputs I get a different error. I have tried variations on this initialiser but all produce the same error.
The variations revolve around adding input_shape, dtype=np.float32
mlmodel = ct.convert(
model,
source="tensorflow",
inputs=[ct.TensorType(name="input")],
outputs=[ct.TensorType(name="output")],
minimum_deployment_target=ct.target.iOS14,
)
t
File ~/opt/anaconda3/envs/coreml_env/lib/python3.8/site-packages/coremltools/converters/mil/frontend/tensorflow/load.py:106, in <listcomp>(.0)
104 logging.debug(msg.format(outputs))
105 outputs = outputs if isinstance(outputs, list) else [outputs]
--> 106 outputs = [i.split(":")[0] for i in outputs]
107 if _get_version(tf.__version__) < _StrictVersion("1.13.1"):
108 return tf.graph_util.extract_sub_graph(graph_def, outputs)
AttributeError: 'TensorType' object has no attribute 'split'