[英]How to save a downloaded CoreML Model with CoreData?
I have downloaded the new trained model from python
backend, with this code: 我已经使用以下代码从
python
后端下载了新的训练模型:
import Foundation
import Alamofire
import AlamofireObjectMapper
import ObjectMapper
import CoreML
class func downloadModel(modelUrl: String) {
let destenation = Support.getDestenationForModel(modelUrl: modelUrl)
AF.download(modelUrl, to: destenation).downloadProgress { progress in
print("Download Progress: \(progress.fractionCompleted)")
}
.response { response in
switch response.result {
case .success(let success):
if let url = success?.absoluteURL {
FTApi.compileNewModel(url: url)
}
break
case .failure(let err):
print(err)
break
}
}
}
where in Support
class the getDestenationForModel
is the following: 在
Support
类中, getDestenationForModel
如下:
class func getDestenationForModel(modelUrl: String) -> DownloadRequest.Destination {
let destenation: DownloadRequest.Destination = { url, options in
guard let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first ,
let modelUrl = URL(string: modelUrl) else {
preconditionFailure("unable to use documents directory")
}
let fileURL = documentsURL.appendingPathComponent(modelUrl.lastPathComponent)
AppPrefrences.pathOfModel = modelUrl.lastPathComponent
return (fileURL, [.removePreviousFile, .createIntermediateDirectories])
}
return destenation
}
when the download is finished I try to save the model to CoreData
(I didn't find any other way, maybe you could recommend something else) using the following code: 下载完成后,我尝试使用以下代码将模型保存到
CoreData
(我没有找到其他方法,也许您可以推荐其他方法):
private class func compileNewModel(url: URL) {
do {
let compiledUrl = try MLModel.compileModel(at: url)
guard let model = try? MLModel(contentsOf: compiledUrl)
else {
print("cannot get content of model")
return
}
let coreMlModel = CML(context: PersistanceService.context)
print(type(of: model)) // => MLNeuralNetworkEngine
coreMlModel.model = (model as NSObject)
PersistanceService.saveContext() // here I have an error
} catch {
print("cannot download the model!")
}
}
the error is: *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MLNeuralNetworkEngine encodeWithCoder:]: unrecognized selector sent to instance 0x104d38280'
错误是:
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MLNeuralNetworkEngine encodeWithCoder:]: unrecognized selector sent to instance 0x104d38280'
where this is my CoreData
entity class: 这是我的
CoreData
实体类:
import Foundation
import CoreData
extension CML {
@nonobjc public class func fetchRequest() -> NSFetchRequest<CML> {
return NSFetchRequest<CML>(entityName: "CML")
}
@NSManaged public var model: NSObject?
}
I tried to change the type from NSObject
to MLModel
, but the result is the same. 我尝试将类型从
NSObject
更改为MLModel
,但结果是相同的。
Why would you even try this? 你为什么还要尝试这个? It's not the sort of thing CoreData is designed for.
这不是CoreData设计的目的。
The correct solution is to copy the folder from compiledURL to a location in your app's Application Support folder. 正确的解决方案是将文件夹从CompiledURL复制到应用程序的“应用程序支持”文件夹中的某个位置。 Then when you create the MLModel object, use the URL from the Application Support folder.
然后,当您创建MLModel对象时,请使用“应用程序支持”文件夹中的URL。
Read "Downloading and Compiling a Model on the User's Device" in the Core ML documentation to see how to do this. 阅读Core ML文档中的“在用户设备上下载和编译模型”以了解如何执行此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.