We will train a small CNN (a kind of image model) that looks at a 28×28 grayscale picture and picks one of ten clothing labels. The path is simple: train on Fashion-MNIST in PyTorch, save a portable clothing.aimodel file, run it with Core AI, and show the top label and confidence in a SwiftUI image picker.
You will do all of this on a Mac. Train PyTorch with MPS or CPU. There is no CUDA step.
Core AI is Apple's tool for running your own neural networks on the device. It can use the CPU, GPU, or Neural Engine on Apple silicon. This course uses Core AI, not Core ML. Apple also has Foundation Models (a system LLM) and Core ML (the classic ML path). We use Core AI here because it is the runtime for custom networks.
Train the clothing model
Start with a small model. Two convolution layers and two linear layers are enough. It trains quickly, exports easily, and is useful in an app.
Convert the graph
Export the trained model with a fixed NCHW float32 example (batch, channel, height, width). Run the Core AI decomposition table, which breaks complex ops into simpler ones. Then convert the exported program. You get a portable clothing.aimodel file you can add to the app.
Show a label in SwiftUI
Add the file to an Xcode 27 app. Load the model once when the app starts. Turn a picked photo or bundled sample into a 28×28 grayscale tensor. Send it as the image input to Core AI. Then map the ten logits (raw scores) to clothing names.
Measure the real run
Then use the Core AI Debugger, the Xcode Core AI debug gauge, and the Core AI instrument in Instruments. Measure the clothing path on the device that will run the app. Look at first-time setup separately from later runs.
Two model artifacts
Keep the portable .aimodel in source control and add it to the app. An ahead-of-time build (a build done before you run the app) can make .aimodelc, which is compiled for one architecture. Treat the portable file as the source. Compile device-specific copies only when they help launch time or how you ship the app.
Next, train the Fashion-MNIST CNN.
Key concepts
- The path is Fashion-MNIST to
ClothingCNNtoclothing.ptto export toclothing.aimodelto a SwiftUI label. - This course uses Core AI, not Core ML.
clothing.ptis PyTorch weights.clothing.aimodelis the portable file for Xcode.- Train on a Mac with MPS or CPU. No CUDA.
Takeaways
- You can name every step from training data to a label on screen.
- Keep
clothing.ptfor reload and export. Shipclothing.aimodelin the app. - Prepare the model once at startup, not on every photo pick.