简体   繁体   中英

Selecting Multiple .mlModel in swift

i have multiple.mlModels in my xcode project and i want user to select the model and perform prediction

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

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.

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?

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.

Another way is to use the MLModel from the GestureClassifier 's .model property and use that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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