0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

coremltoolを使った画像認識でsvm形式のモデルを利用する2

Last updated at Posted at 2017-12-01

開発環境

  • macOS 10.13
  • Swift4
  • XCode9
  • Python2.7

coremltoolsをインストールする

coremltools 0.6.3

と思ったらpipがデフォルトでは無いので、インストールする必要がありました。

coremltoolsを使用する

Appleの用意しているドキュメントにやり方が載っています。
coremltools.converters.libsvm.convert

Examples

がうまく動きませんでしたー。why?

# Make a LIBSVM model
>>> import svmutil
>>> problem = svmutil.svm_problem([0,0,1,1], [[0,1], [1,1], [8,9], [7,7]])
>>> libsvm_model = svmutil.svm_train(problem, svmutil.svm_parameter())

# Convert using default input and output names
>>> import coremltools
>>> coreml_model = coremltools.converters.libsvm.convert(libsvm_model) 

最初の行から躓くので、凹む。libsvmへのパスが通ってるなら

from libsvm import svmutil

通ってないなら、パスを指定

>>> import sys
>>> sys.path.append('/Library/Python/2.7/site-packages/libsvm/')

あと、最後の行でインスタンスの型が違うエラーになります。ちと理由は現在不明。知識が足りない。
回避方法として、一旦、ファイルに落として、それを呼び出してやるとうまくいきます。

>>> svmutil.svm_save_model('test.model', libsvm_model)
>>> import coremltools
>>> coreml_model = coremltools.converters.libsvm.convert('./test.model')
[WARNING] Infering an input lenght of 2. If this is not correct, use the 'input_length' parameter.

input_lengthの数が正しく無い場合は、引数で正確な数を指定しろよとの事。
というわけで、1で作ったmodelを変換します。

>>> coreml_model = coremltools.converters.libsvm.convert('./train.model' input_length=784)
>>> coreml_model.save('./my_model.mlmodel')

これで準備完了。後は簡単なペイントアプリを作成する。
のはまた後日。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?