Created
November 12, 2017 20:10
-
-
Save vmalyi/886a3bc44accfce3d202d231c742521d to your computer and use it in GitHub Desktop.
Run or Walk (Part 4): Import Keras Neural Network to iOS app with Core ML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import CoreML | |
enum ClassifierError : Error { | |
case RuntimeError(String) | |
} | |
class Classifier: NSObject { | |
let model = accel_x_model() | |
private func predict(_ input: MLMultiArray) -> MLMultiArray { | |
guard let modelPrediction = try? model.prediction(input: input) else { | |
fatalError("Unable to make prediction") | |
} | |
return modelPrediction.output | |
} | |
public func makePrediction(_ onInputData: MLMultiArray) throws -> Int { | |
let modelPrediction: MLMultiArray? | |
modelPrediction = self.predict(onInputData) | |
guard let predictedClassWalk = modelPrediction?[0], | |
let predictedClassRun = modelPrediction?[1] | |
else { | |
throw ClassifierError.RuntimeError("Predicted values are invalid") | |
} | |
return Double(truncating: predictedClassWalk) > Double(truncating: predictedClassRun) ? 0 : 1 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment