Do not compile MLModel in Swift Package Bundle

Viewed 45

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

1 Answers

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)
Related