Last active
April 6, 2021 01:59
-
-
Save armchairdeity/98822b07321ab94bd69a0a3fdc322549 to your computer and use it in GitHub Desktop.
Real-Time On-Device Replace CoreML Model With Update
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
@IBAction private func modelVersionPicked() { | |
var modelName:String = ""; | |
switch modelPicker.selectedSegmentIndex { | |
case 0: | |
modelName = "DeviceWornClsfr_20.mlmodel"; | |
case 1: | |
modelName = "DeviceWornClsfr_20.mlmodel"; | |
default: | |
modelName = "this is going to blow up"; | |
} | |
_ = getModel(modelName: modelName) | |
} | |
private func getModel(modelName: String) { | |
let coremlDir = try! FileManager.default.url(for: .applicationSupportDirectory, in: .userDomainMask, appropriateFor: nil, create: false).appendingPathComponent("3M/CoreML/",isDirectory: true) | |
print("CoreML directory at: ",coremlDir.path) | |
// setup the URL objects for source and destination | |
let urlDest:URL = coremlDir.appendingPathComponent(modelName) | |
print("urlDest: ",urlDest.path) | |
let urlSource = URL(string: "http://localhost/coreml/" + modelName) | |
print("Downloading urlSource from: ",urlSource?.absoluteString as Any) | |
print("") | |
if (FileManager.default.fileExists(atPath: urlDest.path)) { | |
print("") | |
print("Deleting ",urlDest.path) | |
try! FileManager.default.removeItem(atPath: urlDest.path) | |
} | |
// setup the alert | |
let alert = UIAlertController(title: "Download complete", message: "We have downloaded the file " + modelName + ".", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) | |
print("Downloading ",urlSource?.absoluteURL as Any," to ",urlDest.path) | |
downloadModel(url: urlSource!, to: coremlDir, completion: { | |
() -> Void in | |
self.present(alert, animated: true) | |
let modelCompiled:URL = try! MLModel.compileModel(at: urlDest) | |
// setup the alert | |
let alert = UIAlertController(title: "Model Compiled", message: "We have compiled the model " + modelCompiled.path + ".", preferredStyle: .alert) | |
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil)) | |
self.present(alert, animated: true) | |
}) | |
} | |
func downloadModel(url: URL, to localUrl: URL, completion: @escaping () -> ()) { | |
let sessionConfig = URLSessionConfiguration.default | |
let session = URLSession(configuration: sessionConfig) | |
let request = URLRequest(url: url) | |
let task = session.downloadTask(with: request) { (tempLocalUrl, response, error) in | |
if let tempLocalUrl = tempLocalUrl, error == nil { | |
// Success | |
if let statusCode = (response as? HTTPURLResponse)?.statusCode { | |
print("Success: \(statusCode)") | |
} | |
do { | |
try FileManager.default.copyItem(at: tempLocalUrl, to: localUrl) | |
completion() | |
} catch (let writeError) { | |
print("error writing file \(localUrl) : \(writeError)") | |
} | |
} else { | |
print("Failure: %@", error?.localizedDescription as Any); | |
} | |
} | |
task.resume() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment