LoginSignup
0

More than 5 years have passed since last update.

raspberry pi 1でtensorflow lite その3

Posted at

概要

raspberry pi 1でtensorflow liteやってみた。
tfliteファイルを作ってみた。
kerasモデルから作ってみた。
データセットは、xor.

環境

tensorflow 1.12

kerasモデルを学習してセーブする。

import numpy as np 
from tensorflow.contrib.keras.api.keras.models import Sequential, model_from_json
from tensorflow.contrib.keras.api.keras.layers import Dense, Dropout, Activation
from tensorflow.contrib.keras.api.keras.optimizers import SGD
import os.path

X = np.array([[0, 0], [0, 1.0], [1.0, 0], [1.0, 1.0]])
y = np.array([[0.0], [1.0], [1.0], [0.0]])
model = Sequential()
model.add(Dense(8, input_dim = 2))
model.add(Activation('tanh'))
model.add(Dense(1))
model.add(Activation('sigmoid'))
sgd = SGD(lr = 0.1)
model.compile(loss = 'binary_crossentropy', optimizer = sgd)
model.save('_model.h5')
model.fit(X, y, batch_size = 1, epochs = 300)
model.summary();
print (model.predict_proba(X))
print ('save model')
model.save('c_model.h5')
json_string = model.to_json()
open(os.path.join('./', 'xor_model.json'), 'w').write(json_string)
yaml_string = model.to_yaml()
open(os.path.join('./', 'xor_model.yaml'), 'w').write(yaml_string)
print ('save weights')
model.save_weights(os.path.join('./', 'xor_model.h5'))
model1 = model_from_json(open(os.path.join('./', 'xor_model.json')).read())
model1.load_weights(os.path.join('./', 'xor_model.h5'))
model1.summary();
model1.compile(loss = 'binary_crossentropy', optimizer = 'sgd')
score = model1.evaluate(X, y, verbose = 0)
print (score)
print (model1.predict_proba(X))

kerasモデルをtfliteファイルに変換する。

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

converter = lite.TFLiteConverter.from_keras_model_file("c_model.h5")
tflite_model = converter.convert()
open("xor_model.tflite", "wb").write(tflite_model)
print ("ok")

tfliteファイルを用いて、検証する。

import numpy as np
import tensorflow as tf
import tensorflow.contrib.lite as lite

interpreter = lite.Interpreter(model_path = "xor_model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print (input_details)
print (output_details)
input_shape = input_details[0]['shape']
input_data = np.array([[0.0, 0.0]], dtype = np.float32)
print(input_data)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print (output_data)
input_data = np.array([[1.0, 0.0]], dtype = np.float32)
print(input_data)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print (output_data)
input_data = np.array([[0.0, 1.0]], dtype = np.float32)
print(input_data)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print (output_data)
input_data = np.array([[1.0, 1.0]], dtype = np.float32)
print(input_data)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
print (output_data)

以上。

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