Open kei9327 opened 6 years ago
@akirasosa please help
Hello @kei9327,
Here is a script that I wrote to convert the model to tf-lite. Also, the model is being tested with the python tf-lite interpreter in the code but you can just comment that part out if you don't need it.
https://gist.github.com/sercant/478cac13391e1b69b2be07654cf3d21e
tf-convert-tflite.py
import argparse
import cv2
import matplotlib.pyplot as plt
import numpy as np
import tensorflow as tf
from data import standardize
prefix = 'hair_recognition'
def main(pb_file, img_file):
"""
Predict and visualize by TensorFlow.
:param pb_file:
:param img_file:
:return:
"""
with tf.gfile.GFile(pb_file, "rb") as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
with tf.Graph().as_default() as graph:
tf.import_graph_def(graph_def, name=prefix)
for op in graph.get_operations():
print(op.name)
x = graph.get_tensor_by_name('%s/input_1:0' % prefix)
y = graph.get_tensor_by_name('%s/output_0:0' % prefix)
loaded_image = cv2.cvtColor(cv2.imread(img_file,-1), cv2.COLOR_BGR2RGB)
resized_image =cv2.resize(loaded_image, (128, 128))
input_image = np.expand_dims(np.float32(resized_image[:128, :128]),axis=0)/255.0
# images = np.load(img_file).astype(float)
# img_h = images.shape[1]
# img_w = images.shape[2]
with tf.Session(graph=graph) as sess:
# for img in images:
# batched = img.reshape(-1, img_h, img_w, 3)
normalized = standardize(input_image)
converter = tf.contrib.lite.TocoConverter.from_session(sess, [x], [y])
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
# Load TFLite model and allocate tensors.
interpreter = tf.contrib.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
# Get input and output tensors.
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Test model on random input data.
# input_shape = input_details[0]['shape']
input_data = np.array(normalized, dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
output_data = interpreter.get_tensor(output_details[0]['index'])
# print(output_data)
# pred = sess.run(y, feed_dict={
# x: normalized
# })
plt.imshow(output_data.reshape(128, 128))
plt.show()
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'--pb_file',
type=str,
default='artifacts/model.pb',
)
parser.add_argument(
'--img_file',
type=str,
default='data/glasshair.jpg',
help='image file as numpy format'
)
args, _ = parser.parse_known_args()
main(**vars(args))
Thanks @sercant
But I hav some problem...
in Shell...
Traceback (most recent call last):
File "tf-convert-tflite.py", line 89, in <module>
main(**vars(args))
File "tf-convert-tflite.py", line 47, in main
tflite_model = converter.convert()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/lite.py", line 439, in convert
**converter_kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 309, in toco_convert_impl
input_data.SerializeToString())
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 109, in toco_convert_protos
(stdout, stderr))
RuntimeError: TOCO failed see console for info.
/bin/sh: toco_from_protos: command not found
None
I can't handling this shell error... So How can i do solve this problem?
Hi, I have rewrite code using PyTorch and tf-lite is TODO now.
did you notice better accuracy when using pytorch?
Thanks @sercant
But I hav some problem...
in Shell...
Traceback (most recent call last): File "tf-convert-tflite.py", line 89, in <module> main(**vars(args)) File "tf-convert-tflite.py", line 47, in main tflite_model = converter.convert() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/lite.py", line 439, in convert **converter_kwargs) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 309, in toco_convert_impl input_data.SerializeToString()) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 109, in toco_convert_protos (stdout, stderr)) RuntimeError: TOCO failed see console for info. /bin/sh: toco_from_protos: command not found None
I can't handling this shell error... So How can i do solve this problem?
Maybe it's because of the python or tensorflow version difference. I am using Python 3.6.5 and Tensorflow 1.12.0.
Thanks @sercant But I hav some problem... in Shell...
Traceback (most recent call last): File "tf-convert-tflite.py", line 89, in <module> main(**vars(args)) File "tf-convert-tflite.py", line 47, in main tflite_model = converter.convert() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/lite.py", line 439, in convert **converter_kwargs) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 309, in toco_convert_impl input_data.SerializeToString()) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 109, in toco_convert_protos (stdout, stderr)) RuntimeError: TOCO failed see console for info. /bin/sh: toco_from_protos: command not found None
I can't handling this shell error... So How can i do solve this problem?
Maybe it's because of the python or tensorflow version difference. I am using Python 3.6.5 and Tensorflow 1.12.0.
If it's not an excuse, Do you have any pretrain data (TFLite)?
Thanks @sercant But I hav some problem... in Shell...
Traceback (most recent call last): File "tf-convert-tflite.py", line 89, in <module> main(**vars(args)) File "tf-convert-tflite.py", line 47, in main tflite_model = converter.convert() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/lite.py", line 439, in convert **converter_kwargs) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 309, in toco_convert_impl input_data.SerializeToString()) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 109, in toco_convert_protos (stdout, stderr)) RuntimeError: TOCO failed see console for info. /bin/sh: toco_from_protos: command not found None
I can't handling this shell error... So How can i do solve this problem?
Maybe it's because of the python or tensorflow version difference. I am using Python 3.6.5 and Tensorflow 1.12.0.
If it's not an excuse, Do you have any pretrain data (TFLite)?
Here is the one I converted using shared pre-trained model.
@sercant how did the model performed on your implementation? I just tried this today and it performed really bad... not only is it slow ~1100ms it also just tries to predict a hair in the middle of the screen everytime.
@sercant how did the model performed on your implementation? I just tried this today and it performed really bad... not only is it slow ~1100ms it also just tries to predict a hair in the middle of the screen everytime.
Yes, it was the same case for me regarding both performance and the behavior. I don't know why it tries to find a hair in the middle of the screen all the time. Maybe the checkpoint provided in #17 was overfitted.
Thanks @sercant
But I hav some problem...
in Shell...
Traceback (most recent call last): File "tf-convert-tflite.py", line 89, in <module> main(**vars(args)) File "tf-convert-tflite.py", line 47, in main tflite_model = converter.convert() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/lite.py", line 439, in convert **converter_kwargs) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 309, in toco_convert_impl input_data.SerializeToString()) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 109, in toco_convert_protos (stdout, stderr)) RuntimeError: TOCO failed see console for info. /bin/sh: toco_from_protos: command not found None
I can't handling this shell error... So How can i do solve this problem?
has anyone solved this error, i'm getting same error for my model
Thanks @sercant But I hav some problem... in Shell...
Traceback (most recent call last): File "tf-convert-tflite.py", line 89, in <module> main(**vars(args)) File "tf-convert-tflite.py", line 47, in main tflite_model = converter.convert() File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/lite.py", line 439, in convert **converter_kwargs) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 309, in toco_convert_impl input_data.SerializeToString()) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/tensorflow/contrib/lite/python/convert.py", line 109, in toco_convert_protos (stdout, stderr)) RuntimeError: TOCO failed see console for info. /bin/sh: toco_from_protos: command not found None
I can't handling this shell error... So How can i do solve this problem?
has anyone solved this error, i'm getting same error for my model
Hey @chin87, I think this is due to a bug in Tensorflow right now. Be sure that toco_from_protos
is in your PATH
variable.
Also, I have an updated version of the script at my repo if you want to check it out. You will need to modify it to work with this repo though.
Hi @sercant @akirasosa sorry for bringing up this 2 year old issue. Any news on adding a script to convert to tflite? @sercant , does your script still works with this repo? Thank you in advance!
I want to build Android App.
So I use Tensorflow Lite for Android.
But I can't get .tflite file.
How Can I convert this model to TFLITE?