简体   繁体   English

在 swift 中选择 Multiple.mlModel

[英]Selecting Multiple .mlModel in swift

i have multiple.mlModels in my xcode project and i want user to select the model and perform prediction我的 xcode 项目中有多个.mlModel,我希望用户使用 select model 并执行预测

let gestureClassifier = GestureClassifier() //mlModel

func predictGesture(window: Int) {

    let previousOutput = modelOutputs[window]
       let modelOutput = try? gestureClassifier.prediction(features: modelInput, hiddenIn: previousOutput?.hiddenOut, cellIn: previousOutput?.cellOut)
       modelOutputs[window] = modelOutput

       if let prediction = modelOutput?.activity,
          let probability = modelOutput?.activityProbability[prediction] {
         if prediction == Config.restItValue {
           return
         }
         if probability > Config.predictionThreshold {
            if prediction == Config.chopItValue || prediction == Config.driveItValue || prediction == Config.shakeItValue {
               print("prediction: \(prediction)")
                self.recordGestures(gesture: prediction)
           }

         }
         else{

            print("unrecognised gesture")
            self.recordGestures(gesture: "unRecognised Gesture")
        }
       }
     }

i have an other model gestureClassifier1 i would like to do something like this我有另一个 model gestureClassifier1 我想做这样的事情

func predictGesture(window: Int, **selectedModel**) {

      let previousOutput = modelOutputs[window]
       let modelOutput = try? **selectedModel**.prediction(features: modelInput, hiddenIn: 
   previousOutput?.hiddenOut, cellIn: previousOutput?.cellOut)

    }

how can i achieve this, i tried to used Anyclass as an datatype but class functions such as.prediction(..) is not accessible.我该如何实现这一点,我尝试使用 Anyclass 作为数据类型,但 class 函数(如 .prediction(..))无法访问。

This isn't so much a Core ML question as a general programming question: how can you make a function that accepts different types of objects but treats them in the same way?这与其说是一个 Core ML 问题,不如说是一个一般性的编程问题:如何制作一个 function 接受不同类型的对象但以相同的方式对待它们?

One way to do that is to create a protocol that has the prediction() method in it, then create an extension for each of your GestureClassifier etc classes to make them conform to the protocol.一种方法是创建一个包含prediction()方法的协议,然后为每个GestureClassifier等类创建一个扩展,以使它们符合协议。

Another way is to use the MLModel from the GestureClassifier 's .model property and use that.另一种方法是使用MLModelGestureClassifier属性中的.model并使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM