以下の内容はhttps://vengineer.hatenablog.com/entry/71936288より取得しました。


やっぱりKerasだよ。ONNXにも変換できる


モデル作って学習して推論するだけなら、Keras がいいでしょう!とツイートしてきましたが。。。

Keras2ONNXを使えば、もっと楽になりそうです。

コードを引用しますが、こんな感じです。
import numpy as np
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input
import keras2onnx
import onnxruntime

# image preprocessing
img_path = 'elephant.jpg'   # make sure the image is in img_path
img_size = 224
img = image.load_img(img_path, target_size=(img_size, img_size))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

# load keras model
from keras.applications.resnet50 import ResNet50
model = ResNet50(include_top=True, weights='imagenet')

# convert to onnx model
onnx_model = keras2onnx.convert_keras(model, model.name)

# runtime prediction
content = onnx_model.SerializeToString()
sess = onnxruntime.InferenceSession(content)
x = x if isinstance(x, list) else [x]
feed = dict([(input.name, x[n]) for n, input in enumerate(sess.get_inputs())])
pred_onnx = sess.run(None, feed)

 ・イメージの前処理
 ・モデル(Keras)のロード
 ・ONNXモデルに変換
 ・ONNX Runtimeを使って、ONNXモデルにて推論

Kerasには下記のようなモデルがあるので、上記の ResNet50 の部分を変えるだけで、OKですね。

 ・Xception
 ・VGG16
 ・VGG19
 ・ResNet50
 ・InceptionV3
 ・InceptionResNetV2
 ・MobileNet
 ・MobileNetV2
 ・DenseNet121
 ・DenseNet169
 ・DenseNet201
 ・NASNetMobile
 ・NASNetLarge. 

RuntimeにMicrosoftの ONNX Runtime を使っていますが、
ONNX Runtime Provider には、
 ・CPU
 ・MKLDNN
 ・CUDA
 ・TensorRT (NVIDIAのCUDA対応)
 ・MKLDNN
をサポートしているので、x86系のプラットフォームなら大丈夫ですね。

ということで、普通の人は、Keras を使いましょう!キャンペーンでした。


 ・
}}}



以上の内容はhttps://vengineer.hatenablog.com/entry/71936288より取得しました。
このページはhttp://font.textar.tv/のウェブフォントを使用してます

不具合報告/要望等はこちらへお願いします。
モバイルやる夫Viewer Ver0.14