LoginSignup
2

More than 5 years have passed since last update.

raspberry pi 1でtensorflow lite その2

Last updated at Posted at 2018-12-12

概要

raspberry pi 1でtensorflow liteやってみた。
tfliteファイルの作り方を調査してみた。
4つの方法がある。

環境

tensorflow 1.12

tf.Sessionから作る。

import tensorflow as tf
import tensorflow.contrib.lite as lite

img = tf.placeholder(name = "img", dtype = tf.float32, shape = (1, 64, 64, 3))
var = tf.get_variable("weights", dtype = tf.float32, shape = (1, 64, 64, 3))
val = img + var
out = tf.identity(val, name = "out")
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    converter = lite.TFLiteConverter.from_session(sess, [img], [out])
    tflite_model = converter.convert()
    open("converted_model.tflite", "wb").write(tflite_model)

freeze_graphなpbファイルから作る。

import tensorflow as tf
import tensorflow.contrib.lite as lite

graph_def_file = "/path/to/Downloads/mobilenet_v1_1.0_224/frozen_graph.pb"
input_arrays = ["input"]
output_arrays = ["MobilenetV1/Predictions/Softmax"]
converter = lite.TFLiteConverter.from_frozen_graph(graph_def_file, input_arrays, output_arrays)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

高級APIのSavedModelから作る。

import tensorflow as tf
import tensorflow.contrib.lite as lite

converter = lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

kerasモデルから作る。

import tensorflow as tf
import tensorflow.contrib.lite as lite

converter = lite.TFLiteConverter.from_keras_model_file("keras_model.h5")
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)

以上。

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
2