Ios VNCoremFeatureValueObservation是否输出softmax概率?如果是,如何提取顶级值?

Ios VNCoremFeatureValueObservation是否输出softmax概率?如果是,如何提取顶级值?,ios,swift,machine-learning,coreml,Ios,Swift,Machine Learning,Coreml,但是,我正在尝试让我的第一个图像分类模型工作, vnClassionObservation不工作,而 VNCoreMLFeatureValueObservation 以下是关于我的模型的一些信息: MLModelDescription: MLModelDescription inputDescriptionsByName: { "input_1__0" = "input_1__0 : Image (Color, 299 x 299)"; } outputDescriptionsByName:

但是,我正在尝试让我的第一个图像分类模型工作,
vnClassionObservation
不工作,而
VNCoreMLFeatureValueObservation

以下是关于我的模型的一些信息:

MLModelDescription: MLModelDescription inputDescriptionsByName: {

"input_1__0" = "input_1__0 : Image (Color, 299 x 299)";

} outputDescriptionsByName: {

    "output_node0__0" = "output_node0__0 : MultiArray (MLMultiArrayDataTypeDouble, 43)";

} predictedFeatureName: (null) 
根据文件:

VNClassificationObservation

This type of observation results from performing a VNCoreMLRequest image
analysis with a Core ML model whose role is classification (rather than 
prediction or image-to-image processing).
Vision infers that an MLModel object is a classifier model if that model 
predicts a single feature. 
That is, the model's modelDescription object has a non-nil value for its 
predictedFeatureName property.
首先,我假设当文档说“预测”时,它们指的是带有值预测的回归类型模型。但现在我想他们指的是softmax预测概率?因此,VN分类观测不会输出softmax预测概率

现在,

我被措辞弄糊涂了。这是否意味着多输入、多输出模型? 不是分类,而是预测,也有点混乱,但我假设 由于我得到的输出,softmax出现问题

当我运行下面的代码时,我得到:

let request = VNCoreMLRequest(model: model) { [weak self] request, error in
            guard let results = request.results as? [VNCoreMLFeatureValueObservation],
                let topResult = results.first else {
                    fatalError("unexpected result type from VNCoreMLRequest")
DispatchQueue.main.async { [weak self] in

                print("topResult!", topResult)

                //print(model.debugDescription.outputDescriptionsByName)
            }
        }
    let handler = VNImageRequestHandler(ciImage: image)

    DispatchQueue.global(qos: .userInteractive).async {

        do {

            try handler.perform([request])

        } catch {print(error)}
我得到了一堆价值观:

topResult! Optional(<VNCoreMLFeatureValueObservation: 
0x1c003f0c0> C99BC0A0-7722-4DDC-8FB8-C0FEB1CEEFA5 1 "MultiArray : Double 43 
vector

[ 0.02323521859943867,0.03784361109137535,0.0327669121325016,0.02373981475830078,0.01920632272958755,0.01511944644153118,0.0268220379948616,0.00990589614957571,0.006585873663425446,0.02727104164659977,0.02337176166474819,0.0177282840013504,0.01582957617938519,0.01962342299520969,0.0335112139582634,0.01197215262800455,0.04638960584998131,0.0546870082616806,0.008390620350837708,0.02519697323441505,0.01038128975778818,0.02463733218610287,0.05725555866956711,0.02852404117584229,0.01987413503229618,0.02478211745619774,0.01224409975111485,0.03397252038121223,0.02300941571593285,0.02020683139562607,0.03740271925926208,0.01999092660844326,0.03210178017616272,0.02830206602811813,0.01122485008090734,0.01071082800626755,0.02285266295075417,0.01730070635676384,0.009790488518774509,0.01149104069918394,0.03331543132662773,0.01211327593773603,0.0193191897124052]" (1.000000))
如果这些不是softmax值/概率,那么我是否会着手获取 问题。价值观我正在尝试获取前3个softmax Prob的索引

多谢各位

!!!更新!!!!!!!!!!!:

尝试在函数中执行此操作: var localPrediction:String? 让topResult=results.first?.featureValue.multiArrayValue

 DispatchQueue.main.async { () in
            var max_value : Float32 = 0
            for i in 0..<topResult!.count{
                if max_value < topResult![i].floatValue{
                    max_value = topResult![i].floatValue
                    localPrediction = String(i)}

                                        }
DispatchQueue.main.async{()位于
变量最大值:Float32=0

对于0中的i.当您的模型是分类器时,即mlmodel文件中的
NeuralNetworkClassifier
,则输出为
VNClassificationObservation
对象

如果您的模型不是分类器,即
NeuralNetwork
NeuralNetworkRegressor
,则输出是一个或多个
VNCoreMLFeatureValueObservation
对象,其中包含来自最终层的输出

因此,如果希望在
VNCoreMLFeatureValueObservation
中输出softmax,则需要确保模型的最后一层是softmax

要获取最大元素的索引和值,请使用:

func argmax(_ array: UnsafePointer<Double>, count: Int) -> (Int, Double) {
  var maxValue: Double = 0
  var maxIndex: vDSP_Length = 0
  vDSP_maxviD(array, 1, &maxValue, &maxIndex, vDSP_Length(count))
  return (Int(maxIndex), maxValue)
}

嗨,Matthijis,很抱歉打扰你,我的模型的最终输出应该是一个softmax层。但是在Python中,我会使用方法来提取最大值以及它的标记,并将标记转换为它的标签名。在这里,我目前有兴趣做一些类似的事情,但我似乎无法提取 VNCoreMLFeatureValueObservation
数据类型。谢谢。对象包含一个
MLFeatureValueObservation
对象,该对象包含一个
MLMultiArray
对象(在其
multiArrayValue
属性中)。此
MLMultiArray
将包含最后一层的输出。在您的情况下,这似乎是一个43个值的向量。您现在可以通过这些值循环查找最大值。谢谢。我根据您的建议更新了我的OP。请参阅最后一部分(
编辑!!
)如果不太麻烦的话。这是最干净的方法吗?我只对Python和numpy有经验;在numpy中,我们使用内置方法来查找最大索引。这看起来不错,应该可以正常工作。我个人会使用
vDSP_maxviD()
来自Accelerate framework,类似于Numpy的argmax。在上面的答案中添加了示例代码。
 DispatchQueue.main.async { () in
            var max_value : Float32 = 0
            for i in 0..<topResult!.count{
                if max_value < topResult![i].floatValue{
                    max_value = topResult![i].floatValue
                    localPrediction = String(i)}

                                        }
func argmax(_ array: UnsafePointer<Double>, count: Int) -> (Int, Double) {
  var maxValue: Double = 0
  var maxIndex: vDSP_Length = 0
  vDSP_maxviD(array, 1, &maxValue, &maxIndex, vDSP_Length(count))
  return (Int(maxIndex), maxValue)
}
let featurePointer = UnsafePointer<Double>(OpaquePointer(features.dataPointer))
let (maxIndex, maxValue) = argmax(featurePointer, 43)