MLModel.CompileModel(NSUrl, NSError) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
在 編譯模型 modelUrl
。
[Foundation.Export("compileModelAtURL:error:")]
public static Foundation.NSUrl CompileModel (Foundation.NSUrl modelUrl, out Foundation.NSError error);
static member CompileModel : Foundation.NSUrl * -> Foundation.NSUrl
參數
- modelUrl
- NSUrl
要編譯之模型的 URL。
- error
- NSError
失敗時發生錯誤。
傳回
- 屬性
備註
這是昂貴的函式。 執行時間會視模型大小而有所不同,但開發人員應該在背景執行緒上執行此方法,並採取步驟來避免重複執行此方法的需求。
下列範例示範開發人員如何下載模型、編譯模型,以及將編譯的模型移至應用程式的永久儲存體:
NSUrl CompileModel(string modelName)
{
var downloadedFile = modelName + ".mlmodel";
var fileUrl = NSUrl.FromFilename(downloadedFile);
NSError err = null;
var compiledUrl = MLModel.CompileModel(fileUrl, out err);
if (err != null)
{
throw new Exception(err.ToString());
}
return compiledUrl;
}
NSUrl StoreModel(NSUrl sourceUrl)
{
var fileManager = NSFileManager.DefaultManager;
NSError err = null;
var appSupportDirectory = fileManager.GetUrl(NSSearchPathDirectory.ApplicationSupportDirectory, NSSearchPathDomain.User, sourceUrl, true, out err);
if (err != null)
{
throw new Exception(err.ToString());
}
// Create a permanent URL in appSupportDirectory
var destinationUrl = appSupportDirectory.Append(sourceUrl.LastPathComponent, true);
NSUrl resultingUrl = null;
var destPath = destinationUrl.AbsoluteString;
// If the compiled model directory exists, replace it
if (System.IO.Directory.Exists(destinationUrl.Path))
{
fileManager.Replace(destinationUrl, sourceUrl, null, NSFileManagerItemReplacementOptions.None, out resultingUrl, out err);
}
else
{
fileManager.Copy(sourceUrl, destinationUrl, out err);
}
if (err != null)
{
throw new Exception(err.ToString());
}
return resultingUrl;
}
private async Task<NSUrl> DownloadAndStoryCoreMLModelAsync()
{
var modelName = "SomeModel";
var sourceUrl ="https://Contoso.org/SomeModel.mlmodel";
using (var wc = new WebClient())
{
await wc.DownloadFileTaskAsync(sourceUrl, modelName +".mlmodel");
var compiledModelPath = CompileModel(modelName);
var finalPath = StoreModel(compiledModelPath);
return finalPath;
}
}