You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

CoreML模型输出类型改为字典失败,请求技术解决方案

Fixing CoreML Model Output to Get Labels and Confidence Scores

It looks like you're on the right track, but the issue is that simply changing the output type to a dictionary won't work—CoreML classifiers rely on mapping multiarray indices to your label list via an enum type. Here's how to properly modify your model to get labeled confidence results, plus adjust your Swift code to handle them:

Step 1: Update the CoreML Model with Class Labels

Use coremltools to add your labels to the model's output spec so CoreML recognizes it as a classifier. Replace your existing Python code with this:

import coremltools
from coremltools.proto import FeatureTypes_pb2 as ft

# Load your converted image-input model
spec = coremltools.utils.load_spec("newModel.mlmodel")

# Read and prepare your class labels from dict.txt
with open("dict.txt", "r") as f:
    labels = f.read().splitlines()
# Remove background label (as you did before)
class_labels = labels[1:]
# Ensure all labels are strings (handle any byte encoding issues)
class_labels = [label.decode("utf-8") if isinstance(label, bytes) else label for label in class_labels]

# Get the output feature (scores) from the spec
output = spec.description.output[0]

# Map the multiarray indices to your class labels using enumType
output.type.multiArrayType.enumType.Clear()  # Clear any existing values
for label in class_labels:
    output.type.multiArrayType.enumType.stringValue.append(label)

# Configure metadata to mark this as a classifier (optional but recommended)
spec.description.predictedFeatureName = "classLabel"
spec.description.predictedProbabilitiesName = "scores"
spec.description.metadata.shortDescription = "Image classifier with 100 custom labels"

# Save the updated model
coremltools.utils.save_spec(spec, "finalModel.mlmodel")

This code tells CoreML that each index in the scores multiarray corresponds to a label from your dict.txt. When you use this model with Vision, it will automatically return labeled classification results.

Step 2: Adjust Your Swift Code to Process Labeled Results

Now update your processClassifications function to handle the VNClassificationObservation objects, which include both the label and confidence score:

private func processClassifications(for request: VNRequest, error: Error?) {
    DispatchQueue.main.async {
        guard let results = request.results as? [VNClassificationObservation] else {
            fatalError("Unexpected result type from VNCoreMLRequest")
        }
        
        // Sort results by confidence (highest first)
        let sortedResults = results.sorted { $0.confidence > $1.confidence }
        
        // Example: Print top 5 results
        print("Top Classifications:")
        for result in sortedResults.prefix(5) {
            print("\(result.identifier): \(String(format: "%.2f%%", result.confidence * 100))")
        }
        
        // You can now use these results to update your UI or perform other actions
    }
}

Don't forget to update your model initialization to use the new finalModel.mlmodel:

let classificationModel = try VNCoreMLModel(for: FinalModel().model)

Why Your Previous Attempt Failed

CoreML doesn't use DictionaryFeatureType for classification outputs—instead, it uses a multiarray with an enum type that maps indices to labels. This is the standard way to get labeled confidence scores from image classifiers in CoreML and Vision.

内容的提问来源于stack exchange,提问作者Tomás Santiago

火山引擎 最新活动