简体   繁体   中英

Do not compile MLModel in Swift Package Bundle

I want to add a .mlmodel to my swift package to have a test to verify that the compilation is working.

targets: [
    .target(
      name: "packageName",
      dependencies: ["package1"]
    ),
    .testTarget(
      name: "packageNameTests",
      dependencies: ["packageName"],
      resources: [
        .copy("Resources/testmodel.mlmodel"),
      ]
    )
  ]

My problem is the .mlmodel is always as a compiled model after bundling

let docsPath = Bundle.module.resourcePath!
let docsArray = try fileManager.contentsOfDirectory(atPath: docsPath)
    print(docsArray)

leads to ["testmodel.mlmodelc"]

I am excepting testmodel.mlmodel to be present instead. I find it strange that copy is processing the ressource. Is there a workaround ? I am using Xcode 14 beta but there is a similar post on StackOverflow without answer regarding this issue How to add uncompiled .mlmodel to Xcode UnitTests bundle that got this problem with Xcode 12

Thank you

Apple allows you to download and compile a MLModel on the fly.

So if you use a URL for your MLModel , you should be able to compile it following this Apple guide .

let compiledModelURL = try MLModel.compileModel(at: modelDescriptionURL)
let model = try MLModel(contentsOf: compiledModelURL)

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