sglvladi / TensorFlowObjectDetectionTutorial

A tutorial on object detection using TensorFlow
194 stars 128 forks source link

convert model to tflite #48

Closed GarhwalRifles closed 4 years ago

GarhwalRifles commented 4 years ago

hi,

i followed the tutorial and retrained ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8.tar.gz on my custom data, in a ubuntu machine, after exporting model to saved_model i am using below script to convert the saved_model to tflite,

import tensorflow as tf

## TFLite Conversion
# Before conversion, fix the model input size
saved_model_dir="/usr/tensor/projects/tensorflow2/workspace/training_demo/pre-trained-models/ssd_mobilenet_v2_fpnlite_320x320_coco17_tpu-8/saved_model"
#model = tf.saved_model.load("saved_model")
model = tf.saved_model.load(saved_model_dir)

model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY].inputs[0].set_shape([1, 320, 320, 3])
tf.saved_model.save(model, "saved_model_updated", signatures=model.signatures[tf.saved_model.DEFAULT_SERVING_SIGNATURE_DEF_KEY])
# Convert
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir='saved_model_updated', signature_keys=['serving_default'])
converter.optimizations = [tf.lite.Optimize.OPTIMIZE_FOR_LATENCY]
converter.target_spec.supported_ops = [tf.lite.OpsSet.TFLITE_BUILTINS, tf.lite.OpsSet.SELECT_TF_OPS]
tflite_model = converter.convert()

## TFLite Interpreter to check input shape
interpreter = tf.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 the model on random input data.
input_shape = input_details[0]['shape']
print(input_shape)

open("ni.tflite", "wb").write(tflite_model)

tflite model got reduced in size but while inference it takes time around 2 seconds and the saved_model takes around 150ms on the same machine

is there any other workaround i can do to create tflite model.

and i have a doubt about these https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/tf2_detection_zoo.md models mostly are tpu , are these models are build only for TPU devices

i am currently doing the training part on ubuntu gpu machines, in the end i want to run these models on windows cpu.

please give some guidence.

can i retrain tensorflow 1 detection models with tensorflow 2 object detection api

sglvladi commented 4 years ago

@GarhwalRifles I am afraid I cannot answer your question re. tflite models. I would advise that you redirect your question to either the tensorflow/models repository or to Stack Overflow.

In relation to re-training TensorFlow 1 detection models, you can follow these instructions, which have been written with TF 1.14 in mind, but can also be used to train TF1 models under TF 2.x. Hope this helps.