core-ml

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Core ML Skills

Core ML 技能指南

Combined advisory, generator, and workflow skill for integrating machine learning into Apple platform apps. Covers Core ML model integration, Vision framework image analysis, NaturalLanguage framework text processing, Create ML training, and on-device model optimization.
这是一套用于在苹果平台应用中集成机器学习的综合性指导、代码生成与工作流技能,涵盖Core ML模型集成、Vision框架图像分析、NaturalLanguage框架文本处理、Create ML模型训练以及端侧模型优化。

When This Skill Activates

技能适用场景

Use this skill when the user:
  • Wants to add ML capabilities to their app
  • Needs to integrate a Core ML model (.mlmodel) into an Xcode project
  • Wants to use the Vision framework for image analysis (faces, text recognition, body pose, object detection)
  • Wants to use the NaturalLanguage framework for text processing (sentiment, entities, language detection)
  • Needs to train a custom model with Create ML
  • Wants to optimize a model for on-device use (quantization, pruning, palettization)
  • Needs to choose between Core ML and Foundation Models (Apple Intelligence)
  • Asks about image classification, object detection, sound classification, or tabular data prediction
  • Wants real-time camera + ML processing
当用户有以下需求时,可使用本技能:
  • 希望为应用添加ML功能
  • 需要将Core ML模型(.mlmodel)集成到Xcode项目中
  • 想要使用Vision框架进行图像分析(人脸、文字识别、人体姿态、目标检测)
  • 想要使用NaturalLanguage框架进行文本处理(情感分析、实体识别、语言检测)
  • 需要通过Create ML训练自定义模型
  • 想要优化模型以适配端侧运行(量化、剪枝、调色板量化)
  • 需要在Core ML与Foundation Models(Apple Intelligence)之间做选型
  • 咨询图像分类、目标检测、声音分类或表格数据预测相关问题
  • 想要实现实时摄像头+ML处理功能

Decision Guide: Core ML vs Foundation Models

选型指南:Core ML vs Foundation Models

Before generating code, determine which framework is appropriate.
在生成代码前,需确定适用的框架。

Use Foundation Models (Apple Intelligence) When:

优先选择Foundation Models(Apple Intelligence)的场景:

  • You need general-purpose text generation, summarization, or conversational AI
  • Target is iOS 26+ / macOS 26+ (Foundation Models requires Apple Silicon + latest OS)
  • The task is open-ended language understanding or generation
  • You want
    @Generable
    structured output from natural language
  • See
    apple-intelligence/foundation-models/
    skill for implementation
  • 需要通用文本生成、摘要或对话式AI功能
  • 目标平台为iOS 26+ / macOS 26+(Foundation Models需要Apple Silicon芯片+最新系统)
  • 任务为开放式语言理解或生成
  • 希望通过
    @Generable
    从自然语言生成结构化输出
  • 实现细节可参考
    apple-intelligence/foundation-models/
    技能

Use Core ML When:

优先选择Core ML的场景:

  • You need specialized ML: image classification, object detection, sound classification, custom regression/classification
  • You have a trained model (.mlmodel, .mlpackage) or plan to train one
  • You need broad device support (iOS 14+ / macOS 11+)
  • The task requires domain-specific predictions (medical imaging, product recognition, custom NLP)
  • Performance-critical inference on Neural Engine or GPU
  • 需要专用ML功能:图像分类、目标检测、声音分类、自定义回归/分类
  • 已有训练好的模型(.mlmodel、.mlpackage)或计划训练自定义模型
  • 需要广泛的设备支持(iOS 14+ / macOS 11+)
  • 任务需要领域特定的预测(医学影像、产品识别、自定义NLP)
  • 对推理性能要求高,需利用Neural Engine或GPU

Use Vision Framework When (No Custom Model Needed):

优先选择Vision框架的场景(无需自定义模型):

  • Image classification using Apple's built-in models
  • Face detection and facial landmark analysis
  • Text recognition (OCR) with
    VNRecognizeTextRequest
  • Body and hand pose detection
  • Barcode and QR code scanning
  • Image similarity and saliency detection
  • Horizon detection, rectangle detection
  • 使用苹果内置模型进行图像分类
  • 人脸检测与人脸关键点分析
  • 使用
    VNRecognizeTextRequest
    进行文字识别(OCR)
  • 人体与手部姿态检测
  • 条形码与二维码扫描
  • 图像相似度与显著性检测
  • 水平线检测、矩形检测

Use NaturalLanguage Framework When (No Custom Model Needed):

优先选择NaturalLanguage框架的场景(无需自定义模型):

  • Sentiment analysis on text
  • Language identification
  • Tokenization (word, sentence, paragraph boundaries)
  • Named entity recognition (people, places, organizations)
  • Word and sentence embeddings for similarity comparison
  • Lemmatization and part-of-speech tagging
  • 文本情感分析
  • 语言识别
  • 分词(单词、句子、段落边界)
  • 命名实体识别(人物、地点、组织)
  • 用于相似度比较的单词与句子嵌入
  • 词形还原与词性标注

Pre-Generation Checks

代码生成前检查项

1. Project Context Detection

1. 项目上下文检测

  • Check deployment target (Core ML requires iOS 11+ / macOS 10.13+; Vision requires iOS 11+; NaturalLanguage requires iOS 12+)
  • Check for existing ML code or models
  • Identify project structure and source file locations
  • Determine if SwiftUI or UIKit/AppKit
  • 检查部署目标(Core ML要求iOS 11+ / macOS 10.13+;Vision要求iOS 11+;NaturalLanguage要求iOS 12+)
  • 检查是否已有ML代码或模型
  • 识别项目结构与源文件位置
  • 判断项目使用SwiftUI还是UIKit/AppKit

2. Conflict Detection

2. 冲突检测

Search for existing ML integration:
Glob: **/*Model*.swift, **/*Classifier*.swift, **/*Predictor*.swift, **/*.mlmodel, **/*.mlmodelc, **/*.mlpackage
Grep: "import CoreML" or "import Vision" or "import NaturalLanguage"
If found, ask user:
  • Extend existing ML setup?
  • Replace with new implementation?
  • Add additional model/capability?
搜索项目中已有的ML集成:
Glob: **/*Model*.swift, **/*Classifier*.swift, **/*Predictor*.swift, **/*.mlmodel, **/*.mlmodelc, **/*.mlpackage
Grep: "import CoreML" or "import Vision" or "import NaturalLanguage"
如果找到已有集成,询问用户:
  • 是否扩展现有ML设置?
  • 是否替换为新实现?
  • 是否添加额外的模型/功能?

Configuration Questions

配置确认问题

Ask user via AskUserQuestion:
  1. What ML capability do you need?
    • Image classification (identify objects in photos)
    • Object detection (locate objects with bounding boxes)
    • Text analysis (sentiment, entities, language)
    • Custom Core ML model integration
    • Vision framework (OCR, faces, poses)
    • Sound classification
    • Tabular data prediction
  2. Do you have a trained model, or need to train one?
    • I have a .mlmodel / .mlpackage file
    • I want to train with Create ML
    • I want to use Apple's built-in models (Vision / NaturalLanguage)
  3. Performance requirements?
    • Real-time (camera feed, < 33ms per prediction)
    • Interactive (user-initiated, < 500ms acceptable)
    • Background processing (batch, latency not critical)
通过AskUserQuestion询问用户以下问题:
  1. 你需要哪种ML功能?
    • 图像分类(识别照片中的物体)
    • 目标检测(用边界框定位物体)
    • 文本分析(情感、实体、语言识别)
    • 自定义Core ML模型集成
    • Vision框架功能(OCR、人脸、姿态)
    • 声音分类
    • 表格数据预测
  2. 你已有训练好的模型,还是需要训练新模型?
    • 我已有.mlmodel / .mlpackage文件
    • 我想要通过Create ML训练模型
    • 我想要使用苹果内置模型(Vision / NaturalLanguage)
  3. 性能要求是什么?
    • 实时处理(摄像头流,每次预测<33ms)
    • 交互式处理(用户触发,<500ms可接受)
    • 后台处理(批量任务,延迟不敏感)

Core ML Model Integration

Core ML模型集成

Adding a Model to Xcode

向Xcode添加模型

  1. Drag
    .mlmodel
    or
    .mlpackage
    into Xcode project navigator
  2. Xcode auto-generates a Swift class with the model name
  3. The generated class provides type-safe input/output interfaces
  4. Xcode compiles to
    .mlmodelc
    at build time (optimized for device)
  1. .mlmodel
    .mlpackage
    拖拽到Xcode项目导航器中
  2. Xcode会自动生成与模型同名的Swift类
  3. 生成的类提供类型安全的输入/输出接口
  4. 构建时Xcode会将模型编译为
    .mlmodelc
    (针对设备优化)

Loading Models

加载模型

swift
// Option 1: Auto-generated class (simplest)
let model = try MyImageClassifier(configuration: MLModelConfiguration())

// Option 2: Generic MLModel loading (flexible)
let url = Bundle.main.url(forResource: "MyModel", withExtension: "mlmodelc")!
let config = MLModelConfiguration()
config.computeUnits = .all  // CPU + GPU + Neural Engine
let model = try MLModel(contentsOf: url, configuration: config)

// Option 3: Async loading (recommended for large models)
let model = try await MLModel.load(contentsOf: url, configuration: config)
swift
// Option 1: Auto-generated class (simplest)
let model = try MyImageClassifier(configuration: MLModelConfiguration())

// Option 2: Generic MLModel loading (flexible)
let url = Bundle.main.url(forResource: "MyModel", withExtension: "mlmodelc")!
let config = MLModelConfiguration()
config.computeUnits = .all  // CPU + GPU + Neural Engine
let model = try MLModel(contentsOf: url, configuration: config)

// Option 3: Async loading (recommended for large models)
let model = try await MLModel.load(contentsOf: url, configuration: config)

Making Predictions

执行预测

swift
// Type-safe prediction with auto-generated class
let input = MyImageClassifierInput(image: pixelBuffer)
let output = try model.prediction(input: input)
print(output.classLabel)       // "cat"
print(output.classLabelProbs)  // ["cat": 0.95, "dog": 0.04, ...]

// Batch predictions
let batch = MLArrayBatchProvider(array: inputs)
let results = try model.predictions(from: batch)
swift
// Type-safe prediction with auto-generated class
let input = MyImageClassifierInput(image: pixelBuffer)
let output = try model.prediction(input: input)
print(output.classLabel)       // "cat"
print(output.classLabelProbs)  // ["cat": 0.95, "dog": 0.04, ...]

// Batch predictions
let batch = MLArrayBatchProvider(array: inputs)
let results = try model.predictions(from: batch)

Create ML Training Overview

Create ML训练概述

Image Classification

图像分类

  • Minimum: 10 images per category; Recommended: 40+ per category
  • Organize images in folders named by category
  • Supports JPEG, PNG, HEIC formats
  • Data augmentation applied automatically (rotation, flip, crop)
  • Transfer learning from Apple's base models
  • 最低要求:每个类别10张图片;推荐:每个类别40+张图片
  • 按类别将图片组织到对应文件夹中
  • 支持JPEG、PNG、HEIC格式
  • 自动应用数据增强(旋转、翻转、裁剪)
  • 基于苹果基础模型进行迁移学习

Text Classification

文本分类

  • Training data: text samples with labels (CSV or JSON)
  • Use cases: sentiment analysis, spam detection, topic classification, intent recognition
  • Minimum 10 samples per class; 100+ recommended for accuracy
  • 训练数据:带标签的文本样本(CSV或JSON格式)
  • 适用场景:情感分析、垃圾邮件检测、主题分类、意图识别
  • 每个类别至少10个样本;为保证准确率,推荐100+个样本

Tabular Classification / Regression

表格分类/回归

  • Structured data in CSV or JSON
  • Automatic feature engineering
  • Supports: Boosted Tree, Random Forest, Linear Regression, Decision Tree
  • 结构化数据(CSV或JSON格式)
  • 自动特征工程
  • 支持模型:Boosted Tree、Random Forest、Linear Regression、Decision Tree

Sound Classification

声音分类

  • Audio files organized by category
  • Environmental sounds, speech detection, music genre
  • Minimum 10 samples per category at 15+ seconds each
  • 按类别组织音频文件
  • 适用场景:环境声音检测、语音识别、音乐流派分类
  • 每个类别至少10个样本,每个样本时长15秒以上

Object Detection

目标检测

  • Images with bounding box annotations (JSON format)
  • Outputs bounding boxes + class labels + confidence
  • Minimum 30 annotated images per class; 300+ recommended
  • 带边界框标注的图片(JSON格式)
  • 输出结果包含边界框+类别标签+置信度
  • 每个类别至少30张标注图片;为保证准确率,推荐300+张

Training Approach

训练方式

  • Xcode Create ML App: Visual interface, drag-and-drop, no code required
  • CreateML Framework: Programmatic training in Swift Playgrounds or macOS apps
  • coremltools (Python): Convert models from TensorFlow, PyTorch, ONNX to Core ML format
  • Xcode Create ML App:可视化界面,拖拽操作,无需代码
  • CreateML Framework:在Swift Playgrounds或macOS应用中通过代码训练
  • coremltools (Python):将TensorFlow、PyTorch、ONNX格式的模型转换为Core ML格式

Vision Framework Capabilities

Vision框架功能列表

CapabilityRequest ClassCustom Model Needed?
Image classification
VNClassifyImageRequest
No (built-in)
Object detection
VNDetectObjectsRequest
(custom model)
Yes
Face detection
VNDetectFaceRectanglesRequest
No
Face landmarks
VNDetectFaceLandmarksRequest
No
Text recognition (OCR)
VNRecognizeTextRequest
No
Body pose
VNDetectHumanBodyPoseRequest
No
Hand pose
VNDetectHumanHandPoseRequest
No
Barcode detection
VNDetectBarcodesRequest
No
Image saliency
VNGenerateAttentionBasedSaliencyImageRequest
No
Horizon detection
VNDetectHorizonRequest
No
Rectangle detection
VNDetectRectanglesRequest
No
Image similarity
VNGenerateImageFeaturePrintRequest
No
功能请求类是否需要自定义模型
图像分类
VNClassifyImageRequest
否(内置模型)
目标检测
VNDetectObjectsRequest
(自定义模型)
人脸检测
VNDetectFaceRectanglesRequest
人脸关键点
VNDetectFaceLandmarksRequest
文字识别(OCR)
VNRecognizeTextRequest
人体姿态
VNDetectHumanBodyPoseRequest
手部姿态
VNDetectHumanHandPoseRequest
条形码检测
VNDetectBarcodesRequest
图像显著性
VNGenerateAttentionBasedSaliencyImageRequest
水平线检测
VNDetectHorizonRequest
矩形检测
VNDetectRectanglesRequest
图像相似度
VNGenerateImageFeaturePrintRequest

Vision Request Pipeline

Vision请求流水线

swift
// Multiple requests on the same image
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
try handler.perform([
    textRequest,     // OCR
    faceRequest,     // Face detection
    barcodeRequest   // Barcode scanning
])
// Each request's results are populated independently
swift
// Multiple requests on the same image
let handler = VNImageRequestHandler(cgImage: cgImage, options: [:])
try handler.perform([
    textRequest,     // OCR
    faceRequest,     // Face detection
    barcodeRequest   // Barcode scanning
])
// Each request's results are populated independently

NaturalLanguage Framework

NaturalLanguage框架

Sentiment Analysis

情感分析

Returns a score from -1.0 (negative) to +1.0 (positive):
swift
let tagger = NLTagger(tagSchemes: [.sentimentScore])
tagger.string = "This app is amazing!"
let (tag, _) = tagger.tag(at: text.startIndex, unit: .paragraph, scheme: .sentimentScore)
// tag?.rawValue == "0.9" (positive)
返回-1.0(负面)到+1.0(正面)的分数:
swift
let tagger = NLTagger(tagSchemes: [.sentimentScore])
tagger.string = "This app is amazing!"
let (tag, _) = tagger.tag(at: text.startIndex, unit: .paragraph, scheme: .sentimentScore)
// tag?.rawValue == "0.9" (positive)

Language Detection

语言检测

swift
let language = NLLanguageRecognizer.dominantLanguage(for: "Bonjour le monde")
// language == .french
swift
let language = NLLanguageRecognizer.dominantLanguage(for: "Bonjour le monde")
// language == .french

Tokenization

分词

swift
let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = "Hello, world!"
tokenizer.enumerateTokens(in: text.startIndex..<text.endIndex) { range, _ in
    print(text[range])  // "Hello" then "world"
    return true
}
swift
let tokenizer = NLTokenizer(unit: .word)
tokenizer.string = "Hello, world!"
tokenizer.enumerateTokens(in: text.startIndex..<text.endIndex) { range, _ in
    print(text[range])  // "Hello" then "world"
    return true
}

Named Entity Recognition

命名实体识别

swift
let tagger = NLTagger(tagSchemes: [.nameType])
tagger.string = "Tim Cook visited Apple Park in Cupertino."
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .nameType) { tag, range in
    if let tag, tag != .other {
        print("\(text[range]): \(tag.rawValue)")
        // "Tim": PersonalName, "Cook": PersonalName
        // "Apple Park": OrganizationName, "Cupertino": PlaceName
    }
    return true
}
swift
let tagger = NLTagger(tagSchemes: [.nameType])
tagger.string = "Tim Cook visited Apple Park in Cupertino."
tagger.enumerateTags(in: text.startIndex..<text.endIndex, unit: .word, scheme: .nameType) { tag, range in
    if let tag, tag != .other {
        print("\(text[range]): \(tag.rawValue)")
        // "Tim": PersonalName, "Cook": PersonalName
        // "Apple Park": OrganizationName, "Cupertino": PlaceName
    }
    return true
}

Model Optimization

模型优化

Quantization (coremltools Python)

量化(coremltools Python)

Reduces model size by lowering numerical precision:
  • Float32 to Float16: ~50% size reduction, minimal accuracy loss
  • Float16 to Int8: ~50% further reduction, test accuracy carefully
python
import coremltools as ct
from coremltools.models.neural_network import quantization_utils

model = ct.models.MLModel("MyModel.mlmodel")
通过降低数值精度减小模型体积:
  • Float32转Float16:体积减小约50%,准确率损失极小
  • Float16转Int8:体积再减小约50%,需仔细测试准确率
python
import coremltools as ct
from coremltools.models.neural_network import quantization_utils

model = ct.models.MLModel("MyModel.mlmodel")

Float16 quantization (safe default)

Float16 quantization (safe default)

model_fp16 = quantization_utils.quantize_weights(model, nbits=16) model_fp16.save("MyModel_fp16.mlmodel")
model_fp16 = quantization_utils.quantize_weights(model, nbits=16) model_fp16.save("MyModel_fp16.mlmodel")

Int8 quantization (aggressive, test accuracy)

Int8 quantization (aggressive, test accuracy)

model_int8 = quantization_utils.quantize_weights(model, nbits=8) model_int8.save("MyModel_int8.mlmodel")
undefined
model_int8 = quantization_utils.quantize_weights(model, nbits=8) model_int8.save("MyModel_int8.mlmodel")
undefined

Palettization

调色板量化

Reduces unique weight values using k-means clustering:
python
from coremltools.optimize.coreml import palettize_weights, OpPalettizerConfig

config = OpPalettizerConfig(nbits=4)  # 16 unique values per tensor
model_palettized = palettize_weights(model, config)
通过k-means聚类减少唯一权重值:
python
from coremltools.optimize.coreml import palettize_weights, OpPalettizerConfig

config = OpPalettizerConfig(nbits=4)  # 16 unique values per tensor
model_palettized = palettize_weights(model, config)

Pruning

剪枝

Removes near-zero weights (sparse model):
python
from coremltools.optimize.torch.pruning import MagnitudePruner, MagnitudePrunerConfig

config = MagnitudePrunerConfig(target_sparsity=0.75)  # Remove 75% of weights
pruner = MagnitudePruner(model, config)
移除接近零的权重(生成稀疏模型):
python
from coremltools.optimize.torch.pruning import MagnitudePruner, MagnitudePrunerConfig

config = MagnitudePrunerConfig(target_sparsity=0.75)  # Remove 75% of weights
pruner = MagnitudePruner(model, config)

Optimization Guidelines

优化指南

  • Always benchmark accuracy after optimization
  • Start with Float16 (safest, best effort-to-reward ratio)
  • Test on target device (Neural Engine behavior differs from GPU)
  • Profile with Xcode Instruments > Core ML Performance
  • 优化后务必测试准确率
  • 优先尝试Float16量化(最安全,投入产出比最高)
  • 在目标设备上测试(Neural Engine的行为与GPU不同)
  • 使用Xcode Instruments > Core ML Performance进行性能分析

Performance Patterns

性能优化方案

Compute Unit Selection

计算单元选择

swift
let config = MLModelConfiguration()

// Best performance — let system choose CPU, GPU, or Neural Engine
config.computeUnits = .all

// CPU only — predictable latency, no GPU/NE contention
config.computeUnits = .cpuOnly

// CPU + Neural Engine — good balance, avoids GPU contention with UI
config.computeUnits = .cpuAndNeuralEngine

// CPU + GPU — when Neural Engine unavailable
config.computeUnits = .cpuAndGPU
swift
let config = MLModelConfiguration()

// 最佳性能——让系统自动选择CPU、GPU或Neural Engine
config.computeUnits = .all

// 仅使用CPU——延迟可预测,避免与GPU/NE资源竞争
config.computeUnits = .cpuOnly

// CPU + Neural Engine——平衡性能与资源,避免GPU与UI渲染竞争
config.computeUnits = .cpuAndNeuralEngine

// CPU + GPU——当Neural Engine不可用时使用
config.computeUnits = .cpuAndGPU

Async Prediction for UI Responsiveness

异步预测保证UI响应性

swift
func classify(_ image: UIImage) async throws -> String {
    let model = try await MLModelManager.shared.model(named: "Classifier")
    // Prediction runs off main thread via structured concurrency
    let input = try MLDictionaryFeatureProvider(dictionary: ["image": image.pixelBuffer!])
    let result = try await Task.detached {
        try model.prediction(from: input)
    }.value
    return result.featureValue(for: "classLabel")?.stringValue ?? "unknown"
}
swift
func classify(_ image: UIImage) async throws -> String {
    let model = try await MLModelManager.shared.model(named: "Classifier")
    // Prediction runs off main thread via structured concurrency
    let input = try MLDictionaryFeatureProvider(dictionary: ["image": image.pixelBuffer!])
    let result = try await Task.detached {
        try model.prediction(from: input)
    }.value
    return result.featureValue(for: "classLabel")?.stringValue ?? "unknown"
}

Batch Processing

批量处理

swift
// Process multiple images efficiently
let inputs = images.map { MyModelInput(image: $0.pixelBuffer!) }
let batch = MLArrayBatchProvider(array: inputs)
let results = try model.predictions(from: batch)

for i in 0..<results.count {
    let output = results.features(at: i)
    print(output.featureValue(for: "classLabel")?.stringValue ?? "")
}
swift
// Process multiple images efficiently
let inputs = images.map { MyModelInput(image: $0.pixelBuffer!) }
let batch = MLArrayBatchProvider(array: inputs)
let results = try model.predictions(from: batch)

for i in 0..<results.count {
    let output = results.features(at: i)
    print(output.featureValue(for: "classLabel")?.stringValue ?? "")
}

Compile Model at Install Time

安装时编译模型

swift
// Compile .mlmodel to .mlmodelc at install (not runtime)
// This is done automatically when you add .mlmodel to Xcode target
// For downloaded models, compile once and cache:
let compiledURL = try MLModel.compileModel(at: downloadedModelURL)
let permanentURL = appSupportDir.appendingPathComponent("MyModel.mlmodelc")
try FileManager.default.copyItem(at: compiledURL, to: permanentURL)
swift
// Compile .mlmodel to .mlmodelc at install (not runtime)
// This is done automatically when you add .mlmodel to Xcode target
// For downloaded models, compile once and cache:
let compiledURL = try MLModel.compileModel(at: downloadedModelURL)
let permanentURL = appSupportDir.appendingPathComponent("MyModel.mlmodelc")
try FileManager.default.copyItem(at: compiledURL, to: permanentURL)

Generation Process

代码生成流程

Step 1: Determine Capability

步骤1:确定功能

Based on user's answer to configuration questions, select the appropriate template(s) from
templates.md
.
根据用户对配置问题的回答,从
templates.md
中选择合适的模板。

Step 2: Generate Core Files

步骤2:生成核心文件

CapabilityFiles Generated
Any Core ML
MLModelManager.swift
Image classification
ImageClassifier.swift
Text analysis
TextAnalyzer.swift
Vision requests
VisionService.swift
Custom model
ModelConfig.swift
+ model-specific predictor
Camera + ML
CameraMLPipeline.swift
功能生成文件
任意Core ML功能
MLModelManager.swift
图像分类
ImageClassifier.swift
文本分析
TextAnalyzer.swift
Vision请求
VisionService.swift
自定义模型
ModelConfig.swift
+ 模型专属预测器
摄像头+ML
CameraMLPipeline.swift

Step 3: Determine File Location

步骤3:确定文件位置

Check project structure:
  • If
    Sources/
    exists ->
    Sources/ML/
  • If
    App/Services/
    exists ->
    App/Services/ML/
  • If
    App/
    exists ->
    App/ML/
  • Otherwise ->
    ML/
检查项目结构:
  • 若存在
    Sources/
    -> 放入
    Sources/ML/
  • 若存在
    App/Services/
    -> 放入
    App/Services/ML/
  • 若存在
    App/
    -> 放入
    App/ML/
  • 否则 -> 放入
    ML/

Output Format

输出格式

After generation, provide:
代码生成完成后,提供以下内容:

Files Created

创建的文件

ML/
├── MLModelManager.swift        # Central model lifecycle management
├── ImageClassifier.swift       # Vision-based image classification (if needed)
├── TextAnalyzer.swift          # NaturalLanguage wrapper (if needed)
├── ModelConfig.swift           # Compute unit configuration
└── VisionService.swift         # Vision request pipeline (if needed)
ML/
├── MLModelManager.swift        # 集中式模型生命周期管理
├── ImageClassifier.swift       # 基于Vision的图像分类(按需生成)
├── TextAnalyzer.swift          # NaturalLanguage框架封装(按需生成)
├── ModelConfig.swift           # 计算单元配置
└── VisionService.swift         # Vision请求流水线(按需生成)

Integration Steps

集成步骤

  1. Add
    .mlmodel
    file to Xcode project (if using custom model)
  2. Import the generated ML service files
  3. Initialize the service in your app's dependency injection
  4. Call prediction methods from your views/view models
  5. Handle errors and display results
  1. .mlmodel
    文件添加到Xcode项目(使用自定义模型时)
  2. 导入生成的ML服务文件
  3. 在应用的依赖注入中初始化服务
  4. 在视图/视图模型中调用预测方法
  5. 处理错误并展示结果

Testing

测试建议

  • Use known test inputs with expected outputs
  • Verify confidence thresholds
  • Profile prediction latency on target device
  • Test graceful degradation when model unavailable
  • 使用已知输入与预期输出进行测试
  • 验证置信度阈值
  • 在目标设备上分析预测延迟
  • 测试模型不可用时的优雅降级逻辑

References

参考资料