Skip to content

durellwilson/coreml-createml-kit

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

CoreML + CreateML Kit

Complete machine learning toolkit for iOS 18+ / macOS 15+ with CoreML inference and CreateML training.

πŸ€– Features

CoreML Inference

  • Image classification
  • Text classification
  • Batch processing
  • Actor-based concurrency

CreateML Training

  • Image classifier training
  • Text classifier training
  • Tabular data training
  • Data augmentation

Model Evaluation

  • Accuracy metrics
  • Confusion matrix
  • Performance testing

πŸ“¦ Installation

dependencies: [
    .package(url: "https://github.com/durellwilson/coreml-createml-kit.git", from: "1.0.0")
]

πŸš€ Quick Start

Image Classification

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)")
}

Text Classification

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)

Train Image Classifier (macOS)

#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"))
#endif

Train Text Classifier

let 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"))

Evaluate Model

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

πŸ—οΈ Project Structure

Training Pipeline (macOS)

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

Inference Pipeline (iOS/macOS)

// 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)")
}

🎯 Use Cases

Image Recognition App

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))%")
                }
            }
        }
    }
}

Sentiment Analysis

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
}

πŸ§ͺ Testing

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

πŸ“Š Performance

  • Image classification: ~50ms on device
  • Text classification: ~10ms
  • Batch processing: Parallel execution
  • Memory efficient: Actor isolation

🀝 Contributing

Part of Detroit's ML education initiative!

πŸ“ License

MIT License


Production ML with CoreML + CreateML πŸ€–

About

Complete ML toolkit - CoreML inference, CreateML training, model evaluation for iOS 18+

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages