Skip to content
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

Open
kei9327 opened this issue Oct 30, 2018 · 13 comments
Open

How Can I convert this model to TFLITE #37

kei9327 opened this issue Oct 30, 2018 · 13 comments

Comments

@kei9327
Copy link

kei9327 commented Oct 30, 2018

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?

@kei9327
Copy link
Author

kei9327 commented Oct 30, 2018

@akirasosa please help

@sercant
Copy link

sercant commented Nov 2, 2018

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))

@kei9327
Copy link
Author

kei9327 commented Nov 8, 2018

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?

@kei9327 kei9327 closed this as completed Nov 8, 2018
@kei9327 kei9327 reopened this Nov 8, 2018
@akirasosa
Copy link
Owner

Hi, I have rewrite code using PyTorch and tf-lite is TODO now.

@ldenoue
Copy link

ldenoue commented Nov 10, 2018

did you notice better accuracy when using pytorch?

@sercant
Copy link

sercant commented Nov 10, 2018

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.

@kei9327
Copy link
Author

kei9327 commented Nov 12, 2018

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)?

@sercant
Copy link

sercant commented Nov 12, 2018

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.

@normandra
Copy link

@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
Copy link

sercant commented Dec 3, 2018

@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.

@chin87
Copy link

chin87 commented Apr 2, 2019

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

@sercant
Copy link

sercant commented Apr 2, 2019

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.

@charliesantos
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants