A Gentle Introduction to CoreMLtools

The field of machine learning is rapidly evolving, and one of the key challenges facing developers is the ability to efficiently create models that can run on a variety of hardware platforms.

A Gentle Introduction to CoreMLtools
Written by TechnoLynx Published on 18 Apr 2024

The field of machine learning is rapidly evolving, and one of the key challenges facing developers is the ability to efficiently create models that can run on a variety of hardware platforms. This is particularly important given the proliferation of devices such as smartphones, tablets, and laptops, each with its own unique set of capabilities and limitations.

At TechnoLynx, we understand the difficulties associated with developing machine learning models for multiple hardware platforms. In order to address these challenges, we have focused on utilizing strong development frameworks such as TensorFlow and PyTorch, which allow for rapid iteration and experimentation. However, building a model on these frameworks is just the first step in the process. To ensure that our models run efficiently, we also rely on powerful conversion tools that enable us to port our models to target hardware platforms.

In this article, we will be exploring one such tool: CoreMLtools. Developed by Apple, this tool has been critical in our work to produce fast and efficient machine learning models for a range of Apple devices,, including MacBooks, iPads, and iPhones. We will delve into the key features of CoreMLtools and demonstrate how they can be used to streamline the process of model deployment on Apple hardware.

In our previous article, we discovered the intricacies of diffusion models, which have become widely popular in the field of computer vision. This time around we will first train, then convert such a model to Core ML, and finally see how we can use it for inference on a macOS device.

What is CoreMLTools?

CoreMLtools is a Python package developed by Apple that serves as a bridge between popular machine learning frameworks and Core ML, a machine learning framework developed by Apple. It provides a set of APIs and tools to convert, analyze, optimize, and test models in Core ML format.

A Gentle Introduction to CoreMLtools
A Gentle Introduction to CoreMLtools

What sets it apart from other tools is its focus on enabling developers to easily integrate their existing models into Core ML-enabled applications. It provides a streamlined process for converting models from popular frameworks such as TensorFlow, PyTorch, and Scikit-learn to Core ML.

It also provides tools for analyzing and optimizing models to ensure they perform optimally on Apple devices. For example, it can quantize the model’s weights to reduce its size and improve its performance on low-power devices. It can also apply other optimizations to reduce the model’s memory footprint and improve its inference speed. Furthermore, the framework allows models to be run on simulated devices, allowing developers to catch any issues before deploying their models to the actual target hardware.

Code and Model

If you’re looking to get your hands on the code used in this article, you can find it in this repository. Here you’ll find everything you need to replicate our results, including the model code and other utilities that we used throughout the article. It’s worth noting that the diffusion model used in this code was heavily influenced by an excellent collab notebook and guide to diffusion networks.

While we won’t be going into the code relating to the model itself — as we’ve covered diffusion models in detail in our previous article — we will be walking through the steps involved in training the model on our custom dataset, converting the network to Core ML, and finally running inference on macOS.

The Dataset

For training the model, we chose the CelebAMask dataset. It is a readily available large-scale face dataset containing roughly thirty thousand high-resolution images of faces. Our interface to the images is a custom Dataset class that can be found in dataset.py. It is instantiated in main.py and we pass it to a DataLoader.



data = CelebDataset(data_path, transform=data_transform)
dataloader = DataLoader(data, batch_size=BATCH_SIZE, shuffle=True, drop_last=True)


Let’s plot some of the images to help us visualize the data we are working with.

Sampled images from CelebAMask.
Sampled images from CelebAMask.

As we can see these are all photos of faces and we expect our model to output similar images once it’s trained up and ready to be used.

Training the Model

We’ve reorganized the model-related code into a module called Trainer which is responsible for providing a training interface for the U-Net, image sampling, checkpoint handling, and logging.

Let’s take a look at our training loop in main.py.


trainer = Trainer(args.model_dir, args.T, args.START_B,
				  args.END_B, args.IMG_SIZE, args.BATCH_SIZE)
trainer.save_checkpoint()

if args.restart_training:
    trainer.clear_checkpoints()
else:
    trainer.load_checkpoint()

for epoch in tqdm(range(args.epochs)):
    for step, batch in enumerate(dataloader):
        loss = trainer.training_step(batch[0])
        # trainer.log_history(loss)
        print(f"epoch {epoch}, step {step}, loss: {loss}")
        if step == 0:
            trainer.save_checkpoint()
            trainer.log_history(loss)
            print(f"Epoch {epoch} | step {step:03d} Loss: {loss} ")

First, we instantiate the trainer with the following arguments:

  • model_dir: Path to where the model’s checkpoints will be saved and loaded from.
  • T: Step size of the diffusion process.
  • START_B: Beta at timestep 0.
  • END_B: Beta at timestep T.
  • IMG_SIZE: Width and height of the image.
  • BATCH_SIZE: Number of images to process at one training step. Then, we either restart the training by clearing all previous checkpoints or load the latest one. Next, for every epoch we iterate over our dataset using our data loader, calling a training step for the model through our trainer. Lastly, we will save a checkpoint of the model at the start of every epoch as well as log the loss and some sample images.

We’ve trained the model to 40 epochs with a batch size of 4, image size 64 by 64, and default beta values. Let’s look at some of the samples.

Images denoised by the diffusion network.
Images denoised by the diffusion network.

While the output isn’t perfect, we can verify that it has converged to a point where it’s producing images that resemble faces. If we wanted to achieve higher quality images, we could opt for a more complex model and tune up the resolution but for trying out CoreMLtools this will do just fine.

Converting the Model

Now that our model is ready for conversion, we’ve reached the main focus of this article. The relevant code for this part can be found in the export_coreml.py file. Let us go through the steps of converting the network we just trained up.

First things first, we use our trainer class to load our latest checkpoint for our model.



trainer = Trainer(args.model_dir, 0, args.START_B, args.END_B, args.IMG_SIZE, args.BATCH_SIZE)
trainer.load_checkpoint()
model = trainer.model


The second step is to convert our PyTorch model into TorchScript. TorchScript is primarily used to create serializable and optimizable models from PyTorch code that can be saved from a Python script and loaded in environments with no Python dependency such as a C++ or Xcode.

Generating the TorchScript version of the model can be done in two ways:

  • torch.jit.trace: Traces the model with some dummy input, meaning it records all operations for that specific run and will use these in the TorchScript model.
  • torch.jit.script: The script method parses the Python source code of the model, and compiles the code into a graph.

The main difference between the two is that a model converted with jit.script can have dynamic behavior (such as control flows) while jit.trace records one execution path. Since our model doesn’t use control flows nor any conditional logic, we shall use jit.trace.



# Create dummy inputs for tracing the model.
dummy_img = torch.rand((1, 3, args.IMG_SIZE, args.IMG_SIZE)).float()
dummy_timestep = torch.randint(0, 100, (2,)).long()
# Trace the model.
model_ts = torch.jit.trace(model, (dummy_img, dummy_timestep))


While the TorchScript version of the model could already be used for inference on Apple devices, if we want to fully utilize the capabilities of the target hardware we need to convert it to Core ML. The main advantage of Core ML is that it allows us the leverage the Apple Neural Engine (ANE) which is the marketing name for Apple’s neural processing unit (NPU). ANE is designed to accelerate machine learning and deep learning algorithms, similar to GPUs for graphics processing. As you might imagine this can drastically improve inference speeds as opposed to only utilizing GPU and CPU cores.

All that’s left now is to convert the model using CoreMLtools and save it:



model_ct = ct.convert(model_ts,
                      inputs=[ct.TensorType(name="img_input", shape=dummy_img.shape),
                              ct.TensorType(name="timestep_input", shape=dummy_timestep.shape)],
                      outputs=[ct.TensorType(name="noise_prediction")])

mlmodel_path = os.path.join(args.model_dir, "model.mlmodel")
model_ct.save(mlmodel_path)


To save the model we need to specify some things:

  • The TorchScript model to be converted.
  • I/O type: TensorType or ImageType
  • I/O dimensions By default, CoreMLtools generates Core ML models with multidimensional array (MLMultiArray) as the type for both input and output. This is referred to as “TensorType” in CoreMLtools. If the model uses images for input or output, this can be specified as ImageType instead.

Using ImageType offers several advantages. One of the most significant is its ability to efficiently convert various data types for use with the Core ML prediction API. This streamlined approach can help boost performance and save valuable development time. Additionally, ImageType provides a gateway to utilizing Apple’s Vision Framework which has great potential for computer vision tasks.

On the other hand, ImageType is also quite restrictive in its allowed dimension which only accepts RGB and grayscale formats meaning anything that doesn’t conform to these standards will have to use MLMultiArray.

Another important thing to mention is that if ImageType is used, we can specify scaling and bias for both inputs and outputs. For example, if we’d like to use images in the range of [0,255] but our model expects values in the range of [-1,1] we could do something like this:



model_ct = ct.convert(model_ts,
                      inputs=[ct.ImageType(name="img_input", shape=dummy_img.shape,bias=[-1,-1,-1], scale=1/127.5),
                              ct.TensorType(name="timestep_input", shape=dummy_timestep.shape)],
                       outputs=[ct.TensorType(name="noise_prediction")])


Compute Units

In Core ML, compute units refer to the hardware resources used to execute machine learning models. These resources can include the CPU, GPU, and ANE. Generally, we don’t need to set this as the converter picks the default optimized path for fast execution meaning it will use everything available on the machine. However, it might still prove useful to specify this parameter as it can help in debugging our models.

When we create or modify a Core ML model in CoreMLtools, we can specify a preferred device or set of devices for running the model using the preferred_devices argument. For example, if we have a model that you want to run on a CPU, you can create an MLModel instance with preferred_devices=[‘cpu’].



model = ct.models.MLModel(model=model, preferred_devices=['cpu'])


We can also use the available_devices() method to get a list of all the available devices on our system.



devices = ct.utils.available_devices()
print(devices)


It’s worth noting that not all models are compatible with all compute units. For example, some models may only be compatible with GPUs or Apple’s Neural Engine. We can use the get_spec function to check the compatibility of a given model with different compute units.



spec = ct.utils.load_spec('path/to/your/model.mlmodel')
print(ct.utils.get_device_capabilities(spec))


This will give us a dictionary of device capabilities for the given model. We can then use this information to choose the appropriate device for running the model.

Running the Model on macOS

The relevant code for running inference with our converted model can be found in the test_coreml.py file. This is mostly the same code used in the trainer except for a few differences. First off, we load our model with default compute units.



unet_ct = ct.models.MLModel(os.path.join(args.model_dir, "model.mlmodel"))


The code for generating images is mostly reused from the trainer but we need to change how we call inference with the model slightly. The following snippet is the main difference in the sample_timestep() method:



x_np = x.detach().cpu().numpy()
t_np = t.detach().cpu().numpy().astype(np.float32)
model_output = unet_ct.predict({"img_input": x_np, "timestep_input": t_np})
img_out = model_output["noise_prediction"]


Because the trainer uses PyTorch variables we first convert these to numpy arrays, then we need to use the predict() method on our Core ML model. The input and output names we use here were set when we exported the model. Finally, here are the images generated by using the Core ML version of the network:

Images generated by using the Core ML model.
Images generated by using the Core ML model.

As we can see the model still works and it’s generating very similar results as the PyTorch version. At this point, we can confirm that the conversion was successful, and we have a model ready to be deployed on Apple devices. Nice!

Final Thoughts

In conclusion, the process of porting machine learning models to different hardware platforms can be a significant challenge, but with the help of tools such as CoreMLtools, the task becomes much more streamlined and efficient. By leveraging the power of Core ML, we have been able to expand our reach and offer our services on a wider range of devices, opening up new possibilities for our engineers here at TechnoLynx.

As we move forward, we can expect to see new and highly specialized hardware accelerators developed by Apple and other companies, which will further drive the need for tools like CoreMLtools. By continuing to embrace new technologies and techniques, we can ensure that our models remain at the forefront of innovation, delivering real value to our clients and partners. If you are interested in our services in these fields, please head over to our website and check them out. See more articles from us on Medium here.

Real-Time Computer Vision for Live Streaming

Real-Time Computer Vision for Live Streaming

21/07/2025

Understand how real-time computer vision transforms live streaming through object detection, OCR, deep learning models, and fast image processing.

Machine Learning and AI in Communication Systems

Machine Learning and AI in Communication Systems

16/07/2025

Learn how AI and machine learning improve communication. From facial expressions to social media, discover practical applications in modern networks.

Real-Time Edge Processing with GPU Acceleration

Real-Time Edge Processing with GPU Acceleration

10/07/2025

Learn how GPU acceleration and mobile hardware enable real-time processing in edge devices, boosting AI and graphics performance at the edge.

IoT Cybersecurity: Safeguarding against Cyber Threats

IoT Cybersecurity: Safeguarding against Cyber Threats

6/06/2025

Explore how IoT cybersecurity fortifies defences against threats in smart devices, supply chains, and industrial systems using AI and cloud computing.

Large Language Models Transforming Telecommunications

Large Language Models Transforming Telecommunications

5/06/2025

Discover how large language models are enhancing telecommunications through natural language processing, neural networks, and transformer models.

Generative AI Tools in Modern Video Game Creation

Generative AI Tools in Modern Video Game Creation

28/05/2025

Learn how generative AI, machine learning models, and neural networks transform content creation in video game development through real-time image generation, fine-tuning, and large language models.

Machine Learning and AI in Modern Computer Science

Machine Learning and AI in Modern Computer Science

20/05/2025

Discover how computer science drives artificial intelligence and machine learning—from neural networks to NLP, computer vision, and real-world applications. Learn how TechnoLynx can guide your AI journey.

Applying Machine Learning in Computer Vision Systems

Applying Machine Learning in Computer Vision Systems

14/05/2025

Learn how machine learning transforms computer vision—from object detection and medical imaging to autonomous vehicles and image recognition.

AI Object Tracking Solutions: Intelligent Automation

AI Object Tracking Solutions: Intelligent Automation

12/05/2025

AI tracking solutions are incorporating industries in different sectors in safety, autonomous detection and sorting processes. The use of computer vision and high-end computing is key in AI tracking.

The Foundation of Generative AI: Neural Networks Explained

The Foundation of Generative AI: Neural Networks Explained

28/04/2025

Find out how neural networks support generative AI models with applications like content creation, and where these models are used in real-world scenarios.

Automating Assembly Lines with Computer Vision

Automating Assembly Lines with Computer Vision

24/04/2025

Discover how computer vision, AI, and edge tech are transforming assembly lines, boosting quality control, and increasing efficiency in smart manufacturing.

TechnoLynx Named a Top Machine Learning Company

TechnoLynx Named a Top Machine Learning Company

9/04/2025

TechnoLynx named a top machine learning development company by Vendorland. We specialise in AI, supervised learning, and custom machine learning systems that deliver real business results.

XR: The Future of Immersion

7/04/2025

It is really impressive how far technology has come. In some fields, we have reached a point where we don’t always seek revolutionary solutions but fun solutions as well. The idea of Extended Reality (XR) has become a reality in recent years, and it always keeps improving.

Optimising Quality Control Workflows with AI and Computer Vision

24/03/2025

Learn how technologies like AI, computer vision, and generative AI are optimising quality control and maintenance workflows.

Inventory Management Applications: Computer Vision to the Rescue!

17/03/2025

Discover how computer vision is transforming inventory management! Learn how AI-driven image recognition enhances warehouse automation, QC, and retail operations.

Copyright Issues With Generative AI and How to Navigate Them

3/03/2025

Recent discussions about generative AI tools have raised copyright concerns. Explore how AI reinforces ethical practices.

Generative AI is Driving Smarter Business Solutions

17/02/2025

Learn how businesses are using generative AI to improve productivity, streamline operations, and create personalised customer experiences.

Generative AI Development Services for Smarter AI Solutions

12/02/2025

Looking for generative AI development services? Learn how machine learning models, natural language processing, and neural networks improve content creation, image generation, and more.

The Impact of Computer Vision on Real-Time Face Detection

10/02/2025

Learn how computer vision, a branch of AI, drives innovation in face detection with the help of CNNs, real-time video processing, and Generative AI.

Computer Vision In Media And Entertainment

30/01/2025

Discover how computer vision is transforming the media and entertainment industry. Explore advancements in production, audience engagement, and content protection.

AI Assistants: Surpassing the Limits of Productivity

27/01/2025

Don’t we all dream of getting things done with the least amount of effort? AI assistants are here to solve this issue for most. If you are into content creation, if quality control is your main occupation or if your goal is to manage your tasks efficiently and set priorities, don’t miss this article.

Alan Turing: The Father of Artificial Intelligence

23/01/2025

In this era of technological revolution, we see new applications every day. If you take a closer look, almost every platform has some sort of AI-enhanced feature. However, how did this start? Let’s go back to the early 20th century and discover everything about the father of AI.

Generative AI vs. Traditional Machine Learning

10/01/2025

Learn the key differences between generative AI and traditional machine learning. Explore applications, data needs, and how these technologies shape AI innovation.

AI in Security: Defence for All!

6/01/2025

Is it safe to say that we live securely? If not, what can we do to make things safer? Does this apply only to our homes, or are there things that we can do for ourselves? And what about homeland security? The answer lies inside!

Optimising LLMOps: Improvement Beyond Limits!

2/01/2025

If we didn’t have LLMOps, the Internet as it is today simply wouldn’t exist. We live in an era of great automation, where content generation is just two clicks away. How is it that LLMOps are so powerful, though? What technology is behind this success? Let’s find out!

Machine Learning, Deep Learning, LLMs and GenAI Compared

20/12/2024

Explore the differences and connections between machine learning, deep learning, large language models (LLMs), and generative AI (GenAI).

Optimise Your Distribution System with Smart Routing Solutions

16/12/2024

Optimise your routing and distribution systems with AI-driven solutions. Learn to solve the Vehicle Routing Problem (VRP)!

MLOps for Hospitals - Staff Tracking (Part 2)

9/12/2024

Learn how to train, deploy, and monitor a computer vision model for real-time hospital staff tracking.

MLOps for Hospitals - Building a Robust Staff Tracking System (Part 1)

2/12/2024

Learn how to set up an MLOps environment for real-time hospital staff tracking. Explore the core principles, tools, and technologies to improve efficiency and patient care in this first part of our comprehensive guide.

Computer Vision and Image Understanding

28/11/2024

Learn about computer vision, image understanding, and how they work in artificial intelligence, machine learning, and real-time applications.

Machine Learning on GPU: A Faster Future

26/11/2024

Learn how GPUs transform machine learning, including AI tasks, deep learning, and handling large amounts of data efficiently.

MLOps vs LLMOps: Let’s simplify things

25/11/2024

Two concepts that are not exactly clear are MLOps and LLMOps. Despite the fact that these two abbreviations look similar, they are completely different. Or are they?! Well, the answer is not that simple. Let’s dive in and see what each of the two models is, how large language models work, how they differ from each other, and how they can be combined for the creation of NLPs.

Artificial Intelligence (AI) vs. Machine Learning Explained

20/11/2024

Learn the differences between Artificial Intelligence (AI) and Machine Learning. Understand their applications, from NLP to driving cars, and how TechnoLynx can help.

Streamlining Sorting and Counting Processes with AI

19/11/2024

Learn how AI aids in sorting and counting with applications in various industries. Get hands-on with code examples for sorting and counting apples based on size and ripeness using instance segmentation and YOLO-World object detection.

Building Smarter, Building Safer: AI's Role in Construction Innovation

11/11/2024

Find out how AI tech is reimagining the construction sector, enabling extensive safety, efficiency and innovation. Discuss the transforming role of AI in construction workflows.

AI for Textile Industry: Transforming Design and Production

4/11/2024

Find out how AI is transforming the textile industry by improving design processes and production efficiency and obtaining excellent supervision. Invite AI-driven solutions designed to revolutionise fabric manufacturing for various global companies.

AI in Biotechnology: Nature in the Palm of our Hands

28/10/2024

When you think of nature, most likely, what comes to mind are tropical forests, sandy beaches, and steep rocky mountains, blessed with all kinds of species that interact in their habitats. On the other hand, when you think of biotechnology, you most likely think of test tubes, laboratories, and crazy nature-defying experiments. The truth is that biotechnology isn’t necessarily that. If you want to find out what wonderful results biotechnology can bring to the environment, this article is for you!

Maximising Efficiency with AI Acceleration

21/10/2024

Find out how AI acceleration is transforming industries. Learn about the benefits of software and hardware accelerators and the importance of GPUs, TPUs, FPGAs, and ASICs.

AI-Driven Innovation: Integrating AI APIs into Your Business

14/10/2024

Learn how to improve your applications with AI APIs and frameworks. Gain practical insights into integration steps, challenges, and best practices using advanced technologies like TensorFlow and AWS SageMaker to boost your business and streamline operations.

What is logistics regression in machine learning?

8/10/2024

Learn about logistic regression in machine learning, a key model for binary classification, how it works with machine learning algorithms, and its role in data science.

Natural Language Processing and Understanding

7/10/2024

Have you ever been frustrated by the silly automated messages you hear on the phone when calling your ISP? What about those online chatbots that don’t understand anything you say, and all they do is make you mad? Let’s see if AI and NLPs can help!

AI for Video: Transforming How We Make and Watch Videos

30/09/2024

Learn how AI is changing the way we create, analyse, and share videos, bringing new levels of creativity and accessibility to the world. We’ll also explore related concerns about bias and privacy.

AI in Architecture: Structure Beyond Limits

23/09/2024

Cities have different sizes. Some are small with few residents, while some mega cities can be home to more than 30 million people! However, how did some cities become so massive? Was it only a necessity that occurred through development, or did architects find aid in AI? Read and decide!

AI in Maintenance: Taking Care of Everything

17/09/2024

We are adults, and as adults, we need to take care of things or people. It gives us a sense of completion. When we are talking about people, sometimes it is the small things and gestures that mean a lot! When we refer to inanimate things, such as our homes or cars, things can become more complicated. Let us take a look at how AI can be our ally in the endless battle of maintenance.

AI is Reshaping the Automotive Industry

11/09/2024

Join us as we take a look at how AI is driving innovation in the automotive industry with groundbreaking applications in manufacturing, vehicle safety, and smart features.

What is IoT Edge Computing and Its Benefits?

6/09/2024

Learn about IoT edge computing, how it works, and its benefits in real-world applications like smart cities, connected cars, and supply chains.

The Future of Governance: Explainable AI for Public Trust & Transparency

5/09/2024

Explore AI for a more responsive government. See how AI is used for policy creation, resource allocation, and public interaction. Learn more about explainable AI for public trust.

Vet Tech Revolution: AI, VR and Better Animal Wellness

2/09/2024

No more guessing games to know how healthy our pets are, as AI is here to help vets and pet owners detect illnesses and curate specific treatment plans for our furry friends. Can technology make pets happier? Discover how the latest tech is improving veterinary medicine.

← Back to Blog Overview