展示 HN:用于处理 Apple Core ML 模型的 CLI
Show HN: CLI for working with Apple Core ML models

原始链接: https://github.com/schappim/coreml-cli

## CoreML-CLI:Core ML 模型命令行工具 CoreML-CLI 是一个 macOS 原生命令行界面,用于与 Apple 的 Core ML 模型交互,无需 Xcode 或 Python。它允许用户**检查**模型结构和元数据,**运行推理**(预测)各种数据类型(图像、文本、JSON),**基准测试**性能,**编译**模型以进行优化,以及**管理**元数据。 **主要特性:** * **检查:** 查看模型详情,如输入、输出和作者信息。 * **预测:** 在单个文件或数据目录上**批量处理**运行推理。支持指定计算设备(CPU、GPU、Apple Neural Engine)。 * **基准测试:** 使用可定制的迭代次数测量推理延迟和吞吐量。 * **编译:** 将 `.mlmodel` 文件转换为优化的 `.mlmodelc` 格式。 * **元数据管理:** 查看和管理模型元数据。 **安装:** 通过 `brew` (`brew tap schappim/coreml-cli brew install coreml-cli`) 或从 GitHub Releases 直接下载。需要 macOS 13+ 和 Swift 5.9+。 **示例用法:** `coreml predict MobileNetV2.mlmodel --input photo.jpg` 或 `coreml benchmark MobileNetV2.mlmodel --input sample.jpg`。输出可以格式化为 JSON,用于脚本编写和 CI/CD 集成。

一个新的命令行界面(CLI)工具,用于处理苹果Core ML模型,已经在Hacker News上分享(github.com/schappim)。该工具专注于*运行*现有的Core ML文件,但不直接处理来自PyTorch等框架的转换。然而,苹果的`coremltools` Python库*确实*支持从PyTorch、TensorFlow和ONNX格式进行转换。 一位评论员指出`coremltools`对TensorFlow Lite和ONNX的支持可能存在限制。尽管如此,该CLI工具被认为是有用的,特别是对于基准测试,因为其Swift实现可能比`coremltools`具有更低的开销。 对未来开发的建议包括将该工具集成为“代理技能”,并添加解析和以更适合AI的格式呈现Xcode性能报告的功能。
相关文章

原文

A native command-line interface for working with Apple Core ML models on macOS. Inspect, run inference, benchmark, and manage Core ML models without Xcode or Python.

  • Inspect - View model structure, inputs/outputs, and metadata
  • Predict - Run inference on images, text, or JSON data
  • Batch - Process multiple files with concurrent execution
  • Benchmark - Measure inference latency and throughput
  • Compile - Convert .mlmodel to optimized .mlmodelc format
  • Metadata - View and manage model metadata
brew tap schappim/coreml-cli
brew install coreml-cli

Download the latest release from GitHub Releases:

curl -L https://github.com/schappim/coreml-cli/releases/download/v1.0.0/coreml-1.0.0-macos.tar.gz -o coreml.tar.gz
tar -xzf coreml.tar.gz
sudo mv coreml /usr/local/bin/

Requires macOS 13+ and Swift 5.9+

git clone https://github.com/schappim/coreml-cli.git
cd coreml-cli
swift build -c release
sudo cp .build/release/coreml /usr/local/bin/
coreml --version
# coreml 1.0.0

View model structure, inputs, outputs, and metadata:

coreml inspect MobileNetV2.mlmodel

Output:

Model: MobileNetV2
Size: 24.7 MB
Compiled: No

Inputs:
  image: image 224x224 BGRA32

Outputs:
  classLabel: string
  classLabelProbs: dictionary

Metadata:
  Author: Original Paper: Mark Sandler, Andrew Howard...
  Description: Detects the dominant objects present in an image...

JSON output for scripting:

coreml inspect MobileNetV2.mlmodel --json

Classify an image:

coreml predict MobileNetV2.mlmodel --input photo.jpg

Output:

Input: photo.jpg
Inference time: 1.66 ms

Outputs:
  classLabel: golden retriever
  classLabelProbs: golden retriever: 0.8721, Labrador retriever: 0.0543...

Save results to file:

coreml predict MobileNetV2.mlmodel --input photo.jpg --output results.json --json

Select compute device:

coreml predict MobileNetV2.mlmodel --input photo.jpg --device ane  # Apple Neural Engine
coreml predict MobileNetV2.mlmodel --input photo.jpg --device gpu  # GPU
coreml predict MobileNetV2.mlmodel --input photo.jpg --device cpu  # CPU only

Process a directory of images:

coreml batch MobileNetV2.mlmodel --dir ./photos --out ./results --format csv

Output:

Found 100 input files
Results written to: ./results/results.csv

Processed 100 files in 892.45 ms
Average inference time: 2.15 ms

Control concurrency:

coreml batch MobileNetV2.mlmodel --dir ./photos --out ./results --concurrency 8

Measure inference latency:

coreml benchmark MobileNetV2.mlmodel --input sample.jpg

Output:

Benchmark Results for: MobileNetV2
==================================================

Configuration:
  Device: all
  Iterations: 100
  Warmup: 10

Latency (ms):
  Mean:      1.279
  Min:       1.008
  Max:       1.602
  StdDev:    0.204

Percentiles (ms):
  P50:       1.200
  P95:       1.523
  P99:       1.589

Throughput: 781.86 inferences/sec

Custom iterations:

coreml benchmark MobileNetV2.mlmodel --input sample.jpg -n 500 --warmup 50

JSON output for CI/CD:

coreml benchmark MobileNetV2.mlmodel --input sample.jpg --json > benchmark.json

Compile .mlmodel to optimized .mlmodelc:

coreml compile MobileNetV2.mlmodel

Output:

Compilation successful!
  Source: /path/to/MobileNetV2.mlmodel
  Output: /path/to/MobileNetV2.mlmodelc
  Original size: 24.7 MB
  Compiled size: 24.5 MB

With validation:

coreml compile MobileNetV2.mlmodel --validate --output-dir ./compiled/

Get model metadata:

coreml meta get MobileNetV2.mlmodel

Output:

Metadata for: MobileNetV2.mlmodel

  Author:      Original Paper: Mark Sandler, Andrew Howard...
  Description: Detects the dominant objects present in an image...
  License:     Please see https://github.com/tensorflow/tensorflow...
  Version:     1.0
Command Description
coreml inspect <model> Inspect model structure and metadata
coreml predict <model> -i <input> Run inference on a single input
coreml batch <model> --dir <dir> --out <dir> Batch process multiple inputs
coreml benchmark <model> -i <input> Benchmark model performance
coreml compile <model> Compile model to optimized format
coreml meta get <model> View model metadata
Option Description
--json, -j Output in JSON format
--device <device> Compute device: cpu, gpu, ane, or all
--help, -h Show help information
--version Show version
Type Extensions Used For
Images .jpg, .jpeg, .png, .heic Vision models
Audio .wav Sound classification
Text .txt NLP models
Tensors .json Custom models

Image Classification Pipeline

#!/bin/bash
# Classify all images in a folder and generate a report

MODEL="MobileNetV2.mlmodel"
INPUT_DIR="./images"
OUTPUT_DIR="./classifications"

# Run batch classification
coreml batch "$MODEL" --dir "$INPUT_DIR" --out "$OUTPUT_DIR" --format csv

# View results
cat "$OUTPUT_DIR/results.csv"
#!/bin/bash
# Compare inference speed across compute devices

MODEL="MobileNetV2.mlmodel"
INPUT="test.jpg"

echo "CPU Only:"
coreml benchmark "$MODEL" -i "$INPUT" --device cpu -n 50 --json | jq '.meanLatencyMs'

echo "GPU:"
coreml benchmark "$MODEL" -i "$INPUT" --device gpu -n 50 --json | jq '.meanLatencyMs'

echo "Neural Engine:"
coreml benchmark "$MODEL" -i "$INPUT" --device ane -n 50 --json | jq '.meanLatencyMs'
# GitHub Actions example
- name: Benchmark Model
  run: |
    coreml benchmark model.mlmodel -i test.jpg --json > benchmark.json

- name: Check Performance Regression
  run: |
    LATENCY=$(jq '.meanLatencyMs' benchmark.json)
    if (( $(echo "$LATENCY > 10" | bc -l) )); then
      echo "Performance regression detected: ${LATENCY}ms"
      exit 1
    fi

For models that accept numeric tensor inputs (not images), you can pass JSON arrays:

Create a JSON input file (input.json):

Run prediction:

coreml predict MyClassifier.mlmodel --input input.json

Output:

Input: input.json
Inference time: 0.12 ms

Outputs:
  probabilities: [0.1377, 0.7100, 0.1522]

Batch process multiple JSON files:

# Create a directory with JSON input files
mkdir json_samples
echo '[5.1, 3.5, 1.4, 0.2]' > json_samples/sample1.json
echo '[6.7, 3.1, 4.7, 1.5]' > json_samples/sample2.json
echo '[5.9, 3.0, 5.1, 1.8]' > json_samples/sample3.json
echo '[4.6, 3.4, 1.4, 0.3]' > json_samples/sample4.json

# Run batch prediction
coreml batch MyClassifier.mlmodel --dir json_samples --out json_results --format csv

Output CSV (json_results/results.csv):

input_file,inference_time_ms,probabilities
sample1.json,0.27,"[0.1377, 0.7100, 0.1522]"
sample2.json,0.22,"[0.0613, 0.5931, 0.3456]"
sample3.json,0.29,"[0.0522, 0.5000, 0.4479]"
sample4.json,0.17,"[0.1406, 0.6825, 0.1769]"

This is useful for models trained on tabular data, embeddings, or any non-image numeric inputs.

  • macOS 13.0 or later
  • Apple Silicon or Intel Mac
  • Core ML models (.mlmodel, .mlpackage, or .mlmodelc)

MIT License - see LICENSE for details.

Contributions are welcome! Please open an issue or submit a pull request.

联系我们 contact @ memedata.com