-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How Can I convert this model to TFLITE #37
Comments
@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...
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? |
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. |
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. |
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 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? |
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?
The text was updated successfully, but these errors were encountered: