How do I get the .mlmodel to be used in Swift Playground?

风格不统一 提交于 2019-12-13 00:38:20

问题


I have already converted the .mlmodel to .mlmodelc , and I have put it in the resources folder. Im using Vision framework and MNIST model, following this tutorial (https://www.youtube.com/watch?v=bOg8AZSFvOc), but using swift playground.

What am i missing? (im using .playground files, not .playgroundbook files)

line of code:

 guard let visionModel = try? VNCoreMLModel(for: MNIST().modelc) else {fatalError("can not load Vision ML model")}

Error message:

use of undefined identifier 'MNIST'

Screenshot:


回答1:


The compiler raises this error, because it cannot find a declaration of the class MNIST, that you try to instantiate an object of.

First, create a regular Xcode project (an iOS app for example) and integrate your mlmodel there. Xcode will then generate an interface for that model automatically. These are exactly the files that are missing in your project and contain the definition of the MNIST class.

Clicking on the small -> button will then lead you to these classes. Note, that in Apple's example, the model is named "MarsHabitatPricer"




回答2:


Playground doesn't recognize the .mlmodel file by default, so we need an iOS Project to help us find the compiled mlmodelc folder, and copy the mlmodelc folder to playground's resource folder, and the auto generated helper class to playground's source folder and change it to public. Then it works!

Try this project: https://github.com/DocRace/AnimalClassifier-Swift-Playground



来源:https://stackoverflow.com/questions/49505044/how-do-i-get-the-mlmodel-to-be-used-in-swift-playground

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!