raspberry pi 1でtensorflow lite その10
3831 ワード
概要
raspberry pi 1でtensorflow liteやってみた。
tfliteファイルを作ってみた。
kerasモデルから作ってみた。
データセットは、九九.
環境
tensorflow 1.12
kerasモデルを学習してセーブする。
import numpy as np
import tensorflow as tf
from tensorflow.contrib.keras.api.keras.models import Sequential
from tensorflow.contrib.keras.api.keras.layers import Dense, Dropout, Embedding, LSTM
def in_encode(i, j):
k = j * 16 + i
return np.array([k >> d & 1 for d in range(8)])
def out_encode(i, j):
k = j * i
return np.array([k >> d & 1 for d in range(7)])
def decode(p):
f = 0
if p[0] > 0.5:
f += 1
if p[1] > 0.5:
f += 2
if p[2] > 0.5:
f += 4
if p[3] > 0.5:
f += 8
if p[4] > 0.5:
f += 16
if p[5] > 0.5:
f += 32
if p[6] > 0.5:
f += 64
return f
trX = np.array([in_encode(i, j) for i in range(1, 10) for j in range(1, 10)])
trY = np.array([out_encode(i, j) for i in range(1, 10) for j in range(1, 10)])
model = Sequential()
model.add(Dense(40, activation = 'tanh', input_shape = (8, )))
model.add(Dense(40, activation = 'tanh'))
model.add(Dense(7, activation = 'linear'))
model.compile(loss = 'mean_squared_error', optimizer = 'adam')
model.fit(trX, trY, batch_size = 60, epochs = 1000, verbose = 1, validation_data = (trX, trY))
p = ' '
j = 1
for i in range(1, 10):
p += '%3d ' % (i * j)
p += '\n'
for j in range(1, 10):
p += '%3d ' % (j)
for i in range(1, 10):
x = np.array([in_encode(i, j)])
pred = model.predict([x])
k = decode(pred[0])
p += '%3d ' % (k)
p += '\n'
print (p)
model.save('kuku.h5')
kerasファイルからtfliteファイルを作る。
import tensorflow as tf
import tensorflow.contrib.lite as lite
converter = lite.TFLiteConverter.from_keras_model_file("kuku.h5")
tflite_model = converter.convert()
open("kuku.tflite", "wb").write(tflite_model)
print ("ok")
tfliteファイルを検証する。
import numpy as np
import tensorflow as tf
import tensorflow.contrib.lite as lite
def in_encode(i, j):
k = j * 16 + i
return np.array([k >> d & 1 for d in range(8)])
def out_encode(i, j):
k = j * i
return np.array([k >> d & 1 for d in range(7)])
def decode(p):
f = 0
if p[0] > 0.5:
f += 1
if p[1] > 0.5:
f += 2
if p[2] > 0.5:
f += 4
if p[3] > 0.5:
f += 8
if p[4] > 0.5:
f += 16
if p[5] > 0.5:
f += 32
if p[6] > 0.5:
f += 64
return f
interpreter = lite.Interpreter(model_path = "kuku.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
#print (input_details)
#print (output_details)
p = ' '
j = 1
for i in range(1, 10):
p += '%3d ' % (i * j)
p += '\n'
for j in range(1, 10):
p += '%3d ' % (j)
for i in range(1, 10):
x = np.array([in_encode(i, j)])
input_data = np.array(x, dtype = np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
pred = interpreter.get_tensor(output_details[0]['index'])
k = decode(pred[0])
p += '%3d ' % (k)
p += '\n'
print (p)
import numpy as np
import tensorflow as tf
from tensorflow.contrib.keras.api.keras.models import Sequential
from tensorflow.contrib.keras.api.keras.layers import Dense, Dropout, Embedding, LSTM
def in_encode(i, j):
k = j * 16 + i
return np.array([k >> d & 1 for d in range(8)])
def out_encode(i, j):
k = j * i
return np.array([k >> d & 1 for d in range(7)])
def decode(p):
f = 0
if p[0] > 0.5:
f += 1
if p[1] > 0.5:
f += 2
if p[2] > 0.5:
f += 4
if p[3] > 0.5:
f += 8
if p[4] > 0.5:
f += 16
if p[5] > 0.5:
f += 32
if p[6] > 0.5:
f += 64
return f
trX = np.array([in_encode(i, j) for i in range(1, 10) for j in range(1, 10)])
trY = np.array([out_encode(i, j) for i in range(1, 10) for j in range(1, 10)])
model = Sequential()
model.add(Dense(40, activation = 'tanh', input_shape = (8, )))
model.add(Dense(40, activation = 'tanh'))
model.add(Dense(7, activation = 'linear'))
model.compile(loss = 'mean_squared_error', optimizer = 'adam')
model.fit(trX, trY, batch_size = 60, epochs = 1000, verbose = 1, validation_data = (trX, trY))
p = ' '
j = 1
for i in range(1, 10):
p += '%3d ' % (i * j)
p += '\n'
for j in range(1, 10):
p += '%3d ' % (j)
for i in range(1, 10):
x = np.array([in_encode(i, j)])
pred = model.predict([x])
k = decode(pred[0])
p += '%3d ' % (k)
p += '\n'
print (p)
model.save('kuku.h5')
import tensorflow as tf
import tensorflow.contrib.lite as lite
converter = lite.TFLiteConverter.from_keras_model_file("kuku.h5")
tflite_model = converter.convert()
open("kuku.tflite", "wb").write(tflite_model)
print ("ok")
tfliteファイルを検証する。
import numpy as np
import tensorflow as tf
import tensorflow.contrib.lite as lite
def in_encode(i, j):
k = j * 16 + i
return np.array([k >> d & 1 for d in range(8)])
def out_encode(i, j):
k = j * i
return np.array([k >> d & 1 for d in range(7)])
def decode(p):
f = 0
if p[0] > 0.5:
f += 1
if p[1] > 0.5:
f += 2
if p[2] > 0.5:
f += 4
if p[3] > 0.5:
f += 8
if p[4] > 0.5:
f += 16
if p[5] > 0.5:
f += 32
if p[6] > 0.5:
f += 64
return f
interpreter = lite.Interpreter(model_path = "kuku.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
#print (input_details)
#print (output_details)
p = ' '
j = 1
for i in range(1, 10):
p += '%3d ' % (i * j)
p += '\n'
for j in range(1, 10):
p += '%3d ' % (j)
for i in range(1, 10):
x = np.array([in_encode(i, j)])
input_data = np.array(x, dtype = np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
pred = interpreter.get_tensor(output_details[0]['index'])
k = decode(pred[0])
p += '%3d ' % (k)
p += '\n'
print (p)
import numpy as np
import tensorflow as tf
import tensorflow.contrib.lite as lite
def in_encode(i, j):
k = j * 16 + i
return np.array([k >> d & 1 for d in range(8)])
def out_encode(i, j):
k = j * i
return np.array([k >> d & 1 for d in range(7)])
def decode(p):
f = 0
if p[0] > 0.5:
f += 1
if p[1] > 0.5:
f += 2
if p[2] > 0.5:
f += 4
if p[3] > 0.5:
f += 8
if p[4] > 0.5:
f += 16
if p[5] > 0.5:
f += 32
if p[6] > 0.5:
f += 64
return f
interpreter = lite.Interpreter(model_path = "kuku.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
#print (input_details)
#print (output_details)
p = ' '
j = 1
for i in range(1, 10):
p += '%3d ' % (i * j)
p += '\n'
for j in range(1, 10):
p += '%3d ' % (j)
for i in range(1, 10):
x = np.array([in_encode(i, j)])
input_data = np.array(x, dtype = np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
pred = interpreter.get_tensor(output_details[0]['index'])
k = decode(pred[0])
p += '%3d ' % (k)
p += '\n'
print (p)
以上。
Author And Source
この問題について(raspberry pi 1でtensorflow lite その10), 我々は、より多くの情報をここで見つけました https://qiita.com/ohisama@github/items/1e710da48b6db97fb014著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .