i have done tensorflow object detection to counting object. but i want to cange it to tensorflow Lite model. for example i have TF model loader bellow, how to change this code to support tflite models? i'm new on tflite, i'm so confused.
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
with tf.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
any helps will be appriciate
thankyou
Related
I tried to solve the problem by writing the following code but no luck.
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(path, 'r') as fid:
serialized_graph = fid.read()
Then I saw an error like this
NameError: name 'path' is not defined
How can I fix it?
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
I just guess because the infomation you give is not enough. Just try it if your tensorflow has version issue.
Try:
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
Go to environment variables and add it to PATH.
I am trying to convert my saved model to a tflite model, the saved model is saved on my desktop, however when I try and run this code:
I gen an error -
OSError: SavedModel file does not exist at: C:/Users/Omar/Desktop/model00000014.h5/{saved_model.pbtxt|saved_model.pb}.
Not sure what the problem is.
import tensorflow as tf
saved_model_dir = "r"C:/Users/Omar/Desktop/model00000014.h5""
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
If you're trying to convert a .h5 Keras model to a TFLite model, make sure you use TFLiteConverter.from_keras_model() method, as described in the docs,
model = tf.keras.models.load( "C:/Users/Omar/Desktop/model00000014.h5" )
converter = tf.lite.TFLiteConverter.from_keras_model( model )
open( 'model.tflite' , 'wb' ).write( converter.convert() )
In case of a SavedModel, use TFLiteConverter.from_saved_model() and mention the file path of the directory of the SavedModel,
saved_model_dir = 'path/to/savedModelDir'
converter = tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
You're providing a Keras Model to the TFLiteConverter.from_saved_model() method, which might be causing an error.
I'm trying to use Tensorflow's Object Detection API with a pre-trained model. I'm loading the model with this:
model_name='fish_inception_v2_graph2'
PATH_TO_CKPT='models/research/object_detection/'+model_name+'/frozen_inference_graph.pb'
### Load a (frozen) Tensorflow model into memory.
detection_model = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.compat.v1.GraphDef()
with tf.compat.v2.io.gfile.GFile(PATH_TO_CKPT, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
That seems to work fine but then the API has a section to "Check the model's input signature, it expects a batch of 3-color images of type uint8":
print(detection_model.signatures['serving_default'].inputs)
When I run that is when I get the error "AttributeError: 'Graph' object has no attribute 'signatures'".
Anyone know how to fix this? Thank you!
In the path to the checkpoint just mention the model.ckpt-10000 or something of that sort. You are providing a .pb file and hence the error.
I have a model with me named "model.json" and I want to use that trained model in my python code so so you tell me how to convert the code or how can I load the "model.json" file in python to use that for any use.
you must of course also save the model weights in h5 format.
If you want to load the model from json do this
json_file = open('model.json', 'r')
loaded_model_json = json_file.read()
json_file.close()
loaded_model = model_from_json(loaded_model_json)
#load weights into new model
loaded_model.load_weights("model.h5")
From your code i read you upload a dict so try this:
from keras.models import model_from_config
model = model_from_config(model_dict)
the model dict is the json.
For the placeholder problem try:
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
Let me know if you've solved it
https://github.com/tensorflow/models/tree/master/slim
This gives download link for checkpoints for Inception v1-4 pretrained models. However, the tar.gz contains only the .ckpt file.
In the tutorial on using Inception v3 2012 [This link], the tar.gz contains .pb and .pbtxt files which are used for classification.
How can i use just the .ckpt file to generate respective .pb and .pbtxt files?
OR
Is there any alternate way of using the .ckpt file for classification?
Even i am also trying inception_v4 model. During my search i could able to find the the checkpoint files contains the weights. So inorder to use this, inception_v4 graph needed to be loaded from inception_v4.py and the session needed to be restored from the checkpoint file. Following code will read the checkpoint file and create the protobuf file.
import tensorflow as tf
slim = tf.contrib.slim
import tf_slim.models.slim.nets as net
# inception_v3_arg_scope
import tf_slim
import inception_v4 as net
import cv2
# checkpoint file
checkpoint_file = '/home/.../inception_v4.ckpt'
# Load Session
sess = tf.Session()
arg_scope = net.inception_v4_arg_scope()
input_tensor = tf.placeholder(tf.float32, [None, 299, 299, 3])
with slim.arg_scope(arg_scope):
logits, end_points = net.inception_v4(inputs=input_tensor)
saver = tf.train.Saver()
saver.restore(sess, checkpoint_file)
f = tf.gfile.FastGFile('./mynet.pb', "w")
f.write(sess.graph_def.SerializeToString())
f.close()
# reading the graph
#
with tf.gfile.FastGFile('./mynet.pb', 'rb') as fp:
graph_def = tf.GraphDef()
graph_def.ParseFromString(fp.read())
with tf.Session(graph=tf.import_graph_def(graph_def, name='')) as sess:
# op = sess.graph.get_operations()
# with open('./tensors.txt', mode='w') as fp:
# for m in op:
# # print m.values()
# fp.write('%s \n' % str(m.values()))
cell_patch = cv2.imread('./car.jpg')
softmax_tensor = sess.graph.get_tensor_by_name('InceptionV4/Logits/Predictions:0')
predictions = sess.run(softmax_tensor, {'Placeholder:0': cell_patch})
But the above code wont give you the predictions. Because I am facing problem in giving the input to the graph. But It can be of good starting point to work with checkpoint files.
Checkpoint is downloaded from following link checkpoints