Complete machine learning toolkit for iOS 18+ / macOS 15+ with CoreML inference and CreateML training.
- Image classification
- Text classification
- Batch processing
- Actor-based concurrency
- Image classifier training
- Text classifier training
- Tabular data training
- Data augmentation
- Accuracy metrics
- Confusion matrix
- Performance testing
dependencies: [
.package(url: "https://github.com/durellwilson/coreml-createml-kit.git", from: "1.0.0")
]import CoreMLCreateMLKit
let classifier = try ImageClassifier(modelName: "MyImageClassifier")
let image = UIImage(named: "test")!
let results = try await classifier.classify(image)
for result in results {
print("\(result.label): \(result.confidence)")
}let classifier = try TextClassifier(modelName: "SentimentClassifier")
let prediction = await classifier.classify("This is amazing!")
print("Sentiment: \(prediction?.label ?? "unknown")")
// Batch processing
let texts = ["Great!", "Terrible", "Okay"]
let predictions = await classifier.batchClassify(texts)#if os(macOS)
import CreateML
let trainer = CreateMLTrainer()
let model = try await trainer.trainImageClassifier(
trainingData: URL(fileURLWithPath: "/path/to/training"),
validationData: URL(fileURLWithPath: "/path/to/validation"),
iterations: 25
)
try model.write(to: URL(fileURLWithPath: "/path/to/output.mlmodel"))
#endiflet trainer = CreateMLTrainer()
let model = try await trainer.trainTextClassifier(
trainingData: URL(fileURLWithPath: "/path/to/data.csv"),
textColumn: "text",
labelColumn: "sentiment"
)
try model.write(to: URL(fileURLWithPath: "/path/to/sentiment.mlmodel"))let evaluator = ModelEvaluator()
let predictions: [(predicted: String, actual: String)] = [
("cat", "cat"),
("dog", "dog"),
("cat", "dog")
]
let metrics = await evaluator.evaluate(predictions: predictions)
print("Accuracy: \(metrics.percentage)")
let matrix = await evaluator.confusionMatrix(predictions: predictions)
print("Confusion Matrix: \(matrix)")// 1. Prepare data
let trainingURL = URL(fileURLWithPath: "/path/to/training")
// 2. Train model
let trainer = CreateMLTrainer()
let model = try await trainer.trainImageClassifier(
trainingData: trainingURL,
iterations: 50
)
// 3. Evaluate
let testData = loadTestData()
let predictions = try await classify(testData, with: model)
let metrics = await evaluator.evaluate(predictions: predictions)
// 4. Export
try model.write(to: outputURL)// 1. Load model
let classifier = try ImageClassifier(modelName: "MyModel")
// 2. Classify
let results = try await classifier.classify(image)
// 3. Process results
let topResult = results.first!
if topResult.confidence > 0.8 {
print("High confidence: \(topResult.label)")
}struct ContentView: View {
@State private var results: [Classification] = []
let classifier = try! ImageClassifier(modelName: "MobileNetV2")
var body: some View {
VStack {
Image("photo")
Button("Classify") {
Task {
results = try await classifier.classify(image)
}
}
List(results) { result in
HStack {
Text(result.label)
Spacer()
Text("\(Int(result.confidence * 100))%")
}
}
}
}
}let classifier = try TextClassifier(modelName: "SentimentModel")
func analyzeFeedback(_ feedback: [String]) async -> [String: Int] {
let predictions = await classifier.batchClassify(feedback)
var counts: [String: Int] = [:]
for prediction in predictions {
counts[prediction.label, default: 0] += 1
}
return counts
}func testClassifier() async throws {
let classifier = try ImageClassifier(modelName: "TestModel")
let testImage = UIImage(named: "test")!
let results = try await classifier.classify(testImage)
XCTAssertFalse(results.isEmpty)
XCTAssertGreaterThan(results[0].confidence, 0.5)
}- Image classification: ~50ms on device
- Text classification: ~10ms
- Batch processing: Parallel execution
- Memory efficient: Actor isolation
Part of Detroit's ML education initiative!
MIT License
Production ML with CoreML + CreateML π€